diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 882bead77..4a42a2ecf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 diff --git a/sigar_windows.go b/sigar_windows.go index 2d501e51a..f75ab2fdc 100644 --- a/sigar_windows.go +++ b/sigar_windows.go @@ -1,11 +1,8 @@ package sigar import ( - "bytes" "errors" "fmt" - "os/exec" - "strconv" "syscall" "time" "unsafe" @@ -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 diff --git a/sigar_windows_test.go b/sigar_windows_test.go index 2bc0c74e7..5b0772ac6 100644 --- a/sigar_windows_test.go +++ b/sigar_windows_test.go @@ -2,7 +2,6 @@ package sigar import ( "os" - "strings" "time" . "github.com/onsi/ginkgo/v2" @@ -10,7 +9,6 @@ import ( ) var _ = Describe("SigarWindows", func() { - Describe("Uptime", func() { It("returns the uptime", func() { var u Uptime @@ -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 @@ -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))) - }) - - }) })