Skip to content

Commit 33c2264

Browse files
committed
fix: fix no swap info on darwin and windows
probably caused by 6cb452b See: cokemine/nodestatus-client-go#18 (comment)
1 parent 5b35f8f commit 33c2264

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

pkg/status/mem.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !linux
2+
// +build !linux
3+
4+
package status
5+
6+
import "github.com/shirou/gopsutil/v3/mem"
7+
8+
func Memory() (uint64, uint64, uint64, uint64) {
9+
memory, _ := mem.VirtualMemory()
10+
swap, _ := mem.SwapMemory()
11+
return memory.Total / 1024.0, memory.Used / 1024.0, swap.Total / 1024.0, swap.Used / 1024.0
12+
}

pkg/status/mem_linux.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build linux
2+
// +build linux
3+
4+
package status
5+
6+
import "github.com/shirou/gopsutil/v3/mem"
7+
8+
func Memory() (uint64, uint64, uint64, uint64) {
9+
memory, _ := mem.VirtualMemory()
10+
return memory.Total / 1024.0, memory.Used / 1024.0, memory.SwapTotal / 1024.0, (memory.SwapTotal - memory.SwapFree) / 1024.0
11+
}

pkg/status/status.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"github.com/shirou/gopsutil/v3/disk"
66
"github.com/shirou/gopsutil/v3/host"
77
"github.com/shirou/gopsutil/v3/load"
8-
"github.com/shirou/gopsutil/v3/mem"
98
psutilNet "github.com/shirou/gopsutil/v3/net"
109
"math"
1110
"net"
@@ -36,11 +35,6 @@ func Uptime() uint64 {
3635
return uint64(time.Now().Unix()) - bootTime
3736
}
3837

39-
func Memory() (uint64, uint64, uint64, uint64) {
40-
memory, _ := mem.VirtualMemory()
41-
return memory.Total / 1024.0, memory.Used / 1024.0, memory.SwapTotal / 1024.0, (memory.SwapTotal - memory.SwapFree) / 1024.0
42-
}
43-
4438
func Load() float64 {
4539
theLoad, _ := load.Avg()
4640
return theLoad.Load1

0 commit comments

Comments
 (0)