forked from shirou/gopsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_freebsd.go
68 lines (55 loc) · 1.79 KB
/
mem_freebsd.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
// +build freebsd
package gopsutil
import (
"os/exec"
"strings"
)
func VirtualMemory() (*VirtualMemoryStat, error) {
pageSize, _ := doSysctrl("vm.stats.vm.v_page_size")
p := mustParseUint64(pageSize[0])
pageCount, _ := doSysctrl("vm.stats.vm.v_page_count")
free, _ := doSysctrl("vm.stats.vm.v_free_count")
active, _ := doSysctrl("vm.stats.vm.v_active_count")
inactive, _ := doSysctrl("vm.stats.vm.v_inactive_count")
cache, _ := doSysctrl("vm.stats.vm.v_cache_count")
buffer, _ := doSysctrl("vfs.bufspace")
wired, _ := doSysctrl("vm.stats.vm.v_wire_count")
ret := &VirtualMemoryStat{
Total: mustParseUint64(pageCount[0]) * p,
Free: mustParseUint64(free[0]) * p,
Active: mustParseUint64(active[0]) * p,
Inactive: mustParseUint64(inactive[0]) * p,
Cached: mustParseUint64(cache[0]) * p,
Buffers: mustParseUint64(buffer[0]),
Wired: mustParseUint64(wired[0]) * p,
}
// TODO: platform independent (worked freebsd?)
ret.Available = ret.Free + ret.Buffers + ret.Cached
ret.Used = ret.Total - ret.Free
ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0
return ret, nil
}
// Return swapinfo
// FreeBSD can have multiple swap devices. but use only first device
func SwapMemory() (*SwapMemoryStat, error) {
out, err := exec.Command("swapinfo").Output()
if err != nil {
return nil, err
}
var ret *SwapMemoryStat
for _, line := range strings.Split(string(out), "\n") {
values := strings.Fields(line)
// skip title line
if len(values) == 0 || values[0] == "Device" {
continue
}
u := strings.Replace(values[4], "%", "", 1)
ret = &SwapMemoryStat{
Total: mustParseUint64(values[1]),
Used: mustParseUint64(values[2]),
Free: mustParseUint64(values[3]),
UsedPercent: mustParseFloat64(u),
}
}
return ret, nil
}