-
Notifications
You must be signed in to change notification settings - Fork 30
/
mem_stats.go
70 lines (58 loc) · 1.32 KB
/
mem_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package statgo
// #cgo LDFLAGS: -lstatgrab
// #include <statgrab.h>
import "C"
import "fmt"
// MemStats contains memory & swap stats
// expressed in bytes
type MemStats struct {
// The total amount of real memory in bytes
Total int
// Theount of real memory in bytes.
Free int
// The used amount of real memory in bytes
Used int
// The amount of real memory in bytes used for caching
Cache int
// The total swap space in bytes.
SwapTotal int
// The used swap in bytes
SwapUsed int
// The free swap in bytes
SwapFree int
}
// MemStats get memory & swap related stats
// Go equivalent to sg_get_mem_stats & sg_get_swap_stats
func (s *Stat) MemStats() *MemStats {
s.Lock()
defer s.Unlock()
memStats := C.sg_get_mem_stats(nil)
swapStats := C.sg_get_swap_stats(nil)
m := &MemStats{
Total: int(memStats.total),
Free: int(memStats.free),
Used: int(memStats.used),
Cache: int(memStats.cache),
SwapTotal: int(swapStats.total),
SwapUsed: int(swapStats.used),
SwapFree: int(swapStats.free),
}
return m
}
func (m *MemStats) String() string {
return fmt.Sprintf(
"Total:\t%d\n"+
"Free:\t\t%d\n"+
"Used:\t\t%d\n"+
"Cache:\t\t%d\n"+
"SwapTotal:\t%d\n"+
"SwapUsed:\t%d\n"+
"SwapFree:\t%d\n",
m.Total,
m.Free,
m.Used,
m.Cache,
m.SwapTotal,
m.SwapUsed,
m.SwapFree)
}