Skip to content

Commit

Permalink
Add memoryStats and getVcpus[Flags] for rgbkrk#52
Browse files Browse the repository at this point in the history
Closes rgbkrk#52
  • Loading branch information
alexzorin committed Sep 22, 2015
1 parent 2515b1b commit 66d8e5e
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
65 changes: 65 additions & 0 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
48 changes: 48 additions & 0 deletions domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
31 changes: 31 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}*/

0 comments on commit 66d8e5e

Please sign in to comment.