Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
test-unit: # <- name
strategy:
matrix:
os: [macos-latest, windows-2022, ubuntu-latest]
os: [macos-latest, windows-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v5
Expand Down
35 changes: 6 additions & 29 deletions sigar_windows.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package sigar

import (
"bytes"
"errors"
"fmt"
"os/exec"
"strconv"
"syscall"
"time"
"unsafe"
Expand Down Expand Up @@ -74,35 +71,15 @@ func (m *Mem) GetIgnoringCGroups() error { //nolint:staticcheck
}

func (s *Swap) Get() error { //nolint:staticcheck
const MB = 1024 * 1024
out, err := exec.Command("wmic", "pagefile", "list", "full").Output()
memoryStatusEx, err := windows.GlobalMemoryStatusEx()
if err != nil {
return err
return fmt.Errorf("GlobalMemoryStatusEx: %w", err)
}
total, err := parseWmicOutput(out, []byte("AllocatedBaseSize"))
if err != nil {
return err
}
used, err := parseWmicOutput(out, []byte("CurrentUsage"))
if err != nil {
return err
}
s.Total = total * MB
s.Used = used * MB
s.Free = s.Total - s.Used
return nil
}

func parseWmicOutput(s, sep []byte) (uint64, error) {
bb := bytes.Split(s, []byte("\n"))
for i := 0; i < len(bb); i++ {
b := bytes.TrimSpace(bb[i])
n := bytes.IndexByte(b, '=')
if n > 0 && bytes.Equal(sep, b[:n]) {
return strconv.ParseUint(string(b[n+1:]), 10, 64)
}
}
return 0, errors.New("parseWmicOutput: missing field: " + string(sep))
s.Total = memoryStatusEx.TotalPageFile
s.Free = memoryStatusEx.AvailPageFile
s.Used = s.Total - s.Free
return nil
}

func (c *Cpu) Get() error { //nolint:staticcheck
Expand Down
45 changes: 10 additions & 35 deletions sigar_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package sigar

import (
"os"
"strings"
"time"

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

var _ = Describe("SigarWindows", func() {

Describe("Uptime", func() {
It("returns the uptime", func() {
var u Uptime
Expand All @@ -30,6 +28,16 @@ var _ = Describe("SigarWindows", func() {
})
})

Describe("Swap", func() {
It("gets the total memory", func() {
var swap Swap
Expect(swap.Get()).To(Succeed())
Expect(swap.Total).To(BeNumerically(">", 0))
Expect(swap.Free).To(BeNumerically(">", 0))
Expect(swap.Used).To(BeNumerically(">", 0))
})
})

Describe("Disk", func() {
It("gets the total disk space", func() {
var usage FileSystemUsage
Expand Down Expand Up @@ -62,37 +70,4 @@ var _ = Describe("SigarWindows", func() {
}, time.Second*10).Should(BeNumerically(">", old.Sys))
})
})

Context("when parsing wmic output", func() {
It("should parse the output", func() {
res := strings.Join([]string{
`AllocatedBaseSize=4791`,
`CurrentUsage=393`,
`Description=C:\pagefile.sys`,
`InstallDate=20151221103329.285091-480`,
`Name=C:\pagefile.sys`,
`PeakUsage=2916`,
`Status=`,
`TempPageFile=FALSE`,
}, "\r\n")

out := []byte(res)
num, err := parseWmicOutput(out, []byte("CurrentUsage"))
Expect(err).To(BeNil())
Expect(num).To(Equal(uint64(393)))

num, err = parseWmicOutput(out, []byte("AllocatedBaseSize"))
Expect(err).To(BeNil())
Expect(num).To(Equal(uint64(4791)))

num, err = parseWmicOutput(out, []byte("Status"))
Expect(err).To(HaveOccurred())
Expect(num).To(Equal(uint64(0)))

num, err = parseWmicOutput(out, []byte("Current"))
Expect(err).To(HaveOccurred())
Expect(num).To(Equal(uint64(0)))
})

})
})
Loading