Commit 66d8e5e3 authored by alexzorin's avatar alexzorin

Add memoryStats and getVcpus[Flags] for #52

Closes #52
parent 2515b1b2
......@@ -31,6 +31,18 @@ type VirTypedParameter struct {
Value interface{}
}
type VirDomainMemoryStat struct {
Tag int32
Val uint64
}
type VirVcpuInfo struct {
Number uint32
State int32
CpuTime uint64
Cpu int32
}
type VirTypedParameters []VirTypedParameter
func (dest *VirTypedParameters) loadFromCPtr(params C.virTypedParameterPtr, nParams int) {
......@@ -572,3 +584,56 @@ func (d *VirDomain) InterfaceStats(path string) (VirDomainInterfaceStats, error)
TxDrop: int64(cStats.tx_drop),
}, nil
}
func (d *VirDomain) MemoryStats(nrStats uint32, flags uint32) ([]VirDomainMemoryStat, error) {
ptr := make([]C.virDomainMemoryStatStruct, nrStats)
result := C.virDomainMemoryStats(
d.ptr, (C.virDomainMemoryStatPtr)(unsafe.Pointer(&ptr[0])),
C.uint(nrStats), C.uint(flags))
if result == -1 {
return []VirDomainMemoryStat{}, GetLastError()
}
out := make([]VirDomainMemoryStat, result)
for i := 0; i < int(result); i++ {
out = append(out, VirDomainMemoryStat{
Tag: int32(ptr[i].tag),
Val: uint64(ptr[i].val),
})
}
return out, nil
}
func (d *VirDomain) GetVcpus(maxInfo int32) ([]VirVcpuInfo, error) {
ptr := make([]C.virVcpuInfo, maxInfo)
result := C.virDomainGetVcpus(
d.ptr, (C.virVcpuInfoPtr)(unsafe.Pointer(&ptr[0])),
C.int(maxInfo), nil, C.int(0))
if result == -1 {
return []VirVcpuInfo{}, GetLastError()
}
out := make([]VirVcpuInfo, 0)
for i := 0; i < int(result); i++ {
out = append(out, VirVcpuInfo{
Number: uint32(ptr[i].number),
State: int32(ptr[i].state),
CpuTime: uint64(ptr[i].cpuTime),
Cpu: int32(ptr[i].cpu),
})
}
return out, nil
}
func (d *VirDomain) GetVcpusFlags(flags uint32) (int32, error) {
result := C.virDomainGetVcpusFlags(d.ptr, C.uint(flags))
if result == -1 {
return 0, GetLastError()
}
return int32(result), nil
}
......@@ -481,3 +481,51 @@ func TestDomainScreenshot(t *testing.T) {
t.Fatalf("Wanted image/*, got %s", mime)
}
}
func TestDomainGetVcpus(t *testing.T) {
dom, conn := buildTestDomain()
defer func() {
dom.Free()
conn.CloseConnection()
}()
if err := dom.Create(); err != nil {
t.Error(err)
return
}
defer dom.Destroy()
stats, err := dom.GetVcpus(1)
if err != nil {
t.Fatal(err)
}
if len(stats) != 1 {
t.Fatal("should have 1 cpu")
}
if stats[0].State != 1 {
t.Fatal("state should be 1")
}
}
func TestDomainGetVcpusFlags(t *testing.T) {
dom, conn := buildTestDomain()
defer func() {
dom.Free()
conn.CloseConnection()
}()
if err := dom.Create(); err != nil {
t.Error(err)
return
}
defer dom.Destroy()
num, err := dom.GetVcpusFlags(0)
if err != nil {
t.Fatal(err)
}
if num != 1 {
t.Fatal("should have 1 cpu", num)
}
}
......@@ -960,3 +960,34 @@ func TestStorageVolUploadDownload(t *testing.T) {
t.Fatal(err)
}
}
/*func TestDomainMemoryStats(t *testing.T) {
conn, err := NewVirConnection("lxc:///")
if err != nil {
t.Error(err)
return
}
defer conn.CloseConnection()
dom, err := defineTestLxcDomain(conn, "")
if err != nil {
t.Error(err)
return
}
defer func() {
dom.Undefine()
dom.Free()
}()
if err := dom.Create(); err != nil {
t.Fatal(err)
}
defer dom.Destroy()
ms, err := dom.MemoryStats(1, 0)
if err != nil {
t.Fatal(err)
}
if len(ms) != 1 {
t.Fatal("Should have got one result, got", len(ms))
}
}*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment