From 66d8e5e3d8594066da97a9502693a47ab2a35469 Mon Sep 17 00:00:00 2001 From: Alex Zorin Date: Mon, 21 Sep 2015 22:26:35 +1000 Subject: [PATCH] Add memoryStats and getVcpus[Flags] for #52 Closes #52 --- domain.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ domain_test.go | 48 +++++++++++++++++++++++++++++++++ integration_test.go | 31 +++++++++++++++++++++ 3 files changed, 144 insertions(+) diff --git a/domain.go b/domain.go index e13abe1..99531b2 100644 --- a/domain.go +++ b/domain.go @@ -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 +} diff --git a/domain_test.go b/domain_test.go index 368abee..07a737a 100644 --- a/domain_test.go +++ b/domain_test.go @@ -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) + } +} diff --git a/integration_test.go b/integration_test.go index 0ec6eb8..167b484 100644 --- a/integration_test.go +++ b/integration_test.go @@ -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)) + } +}*/