Skip to content

Commit

Permalink
Add windows version
Browse files Browse the repository at this point in the history
- Fix sigar_unix file to not compile on windows
- Start work on windows implementation
  • Loading branch information
Brannon authored and Max Brunsfeld committed Jun 25, 2014
1 parent 3641d81 commit 7f596b5
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sigar_unix.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) 2012 VMware, Inc.

package sigar

// +build darwin freebsd linux netbsd openbsd

package sigar

import "syscall"

func (self *FileSystemUsage) Get(path string) error {
Expand Down
100 changes: 100 additions & 0 deletions sigar_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2012 VMware, Inc.

package sigar

// #include <stdlib.h>
// #include <windows.h>
import "C"

import (
"fmt"
"unsafe"
)

func init() {
}

func (self *LoadAverage) Get() error {
return nil
}

func (self *Uptime) Get() error {
return nil
}

func (self *Mem) Get() error {
var statex C.MEMORYSTATUSEX
statex.dwLength = C.DWORD(unsafe.Sizeof(statex))

succeeded := C.GlobalMemoryStatusEx(&statex)
if succeeded == C.FALSE {
lastError := C.GetLastError()
return fmt.Errorf("GlobalMemoryStatusEx failed with error: %d", int(lastError))
}

self.Total = uint64(statex.ullTotalPhys)
return nil
}

func (self *Swap) Get() error {
return notImplemented()
}

func (self *Cpu) Get() error {
return notImplemented()
}

func (self *CpuList) Get() error {
return notImplemented()
}

func (self *FileSystemList) Get() error {
return notImplemented()
}

func (self *ProcList) Get() error {
return notImplemented()
}

func (self *ProcState) Get(pid int) error {
return notImplemented()
}

func (self *ProcMem) Get(pid int) error {
return notImplemented()
}

func (self *ProcTime) Get(pid int) error {
return notImplemented()
}

func (self *ProcArgs) Get(pid int) error {
return notImplemented()
}

func (self *ProcExe) Get(pid int) error {
return notImplemented()
}

func (self *FileSystemUsage) Get(path string) error {
var availableBytes C.ULARGE_INTEGER
var totalBytes C.ULARGE_INTEGER
var totalFreeBytes C.ULARGE_INTEGER

pathChars := C.CString(path)
defer C.free(unsafe.Pointer(pathChars))

succeeded := C.GetDiskFreeSpaceEx((*C.CHAR)(pathChars), &availableBytes, &totalBytes, &totalFreeBytes)
if succeeded == C.FALSE {
lastError := C.GetLastError()
return fmt.Errorf("GetDiskFreeSpaceEx failed with error: %d", int(lastError))
}

self.Total = *(*uint64)(unsafe.Pointer(&totalBytes))
return nil
}

func notImplemented() error {
panic("Not Implemented")
return nil
}
32 changes: 32 additions & 0 deletions sigar_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sigar_test

import (
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

sigar "github.com/cloudfoundry/gosigar"
)

var _ = Describe("SigarWindows", func() {
Describe("Memory", func() {
It("gets the total memory", func() {
mem := sigar.Mem{}
err := mem.Get()

Ω(err).ShouldNot(HaveOccurred())
Ω(mem.Total).Should(BeNumerically(">", 0))
})
})

Describe("Disk", func() {
It("gets the total disk space", func() {
usage := sigar.FileSystemUsage{}
err := usage.Get(os.TempDir())

Ω(err).ShouldNot(HaveOccurred())
Ω(usage.Total).Should(BeNumerically(">", 0))
})
})
})

0 comments on commit 7f596b5

Please sign in to comment.