shirou_gopsutil/mem/mem_freebsd.go

130 lines
2.7 KiB
Go
Raw Normal View History

// +build freebsd
2014-12-30 21:09:05 +08:00
package mem
import (
"os/exec"
2014-09-19 23:15:35 +08:00
"strconv"
"strings"
2015-10-19 23:04:57 +08:00
"github.com/shirou/gopsutil/internal/common"
)
func VirtualMemory() (*VirtualMemoryStat, error) {
pageSize, err := common.DoSysctrl("vm.stats.vm.v_page_size")
2014-09-19 23:15:35 +08:00
if err != nil {
return nil, err
}
p, err := strconv.ParseUint(pageSize[0], 10, 64)
if err != nil {
return nil, err
}
pageCount, err := common.DoSysctrl("vm.stats.vm.v_page_count")
2014-09-19 23:15:35 +08:00
if err != nil {
return nil, err
}
free, err := common.DoSysctrl("vm.stats.vm.v_free_count")
2014-09-19 23:15:35 +08:00
if err != nil {
return nil, err
}
active, err := common.DoSysctrl("vm.stats.vm.v_active_count")
2014-09-19 23:15:35 +08:00
if err != nil {
return nil, err
}
inactive, err := common.DoSysctrl("vm.stats.vm.v_inactive_count")
2014-09-19 23:15:35 +08:00
if err != nil {
return nil, err
}
cache, err := common.DoSysctrl("vm.stats.vm.v_cache_count")
2014-09-19 23:15:35 +08:00
if err != nil {
return nil, err
}
buffer, err := common.DoSysctrl("vfs.bufspace")
2014-09-19 23:15:35 +08:00
if err != nil {
return nil, err
}
wired, err := common.DoSysctrl("vm.stats.vm.v_wire_count")
2014-09-19 23:15:35 +08:00
if err != nil {
return nil, err
}
2014-09-19 23:15:35 +08:00
parsed := make([]uint64, 0, 7)
vv := []string{
pageCount[0],
free[0],
active[0],
inactive[0],
cache[0],
buffer[0],
wired[0],
}
for _, target := range vv {
t, err := strconv.ParseUint(target, 10, 64)
if err != nil {
return nil, err
}
parsed = append(parsed, t)
}
ret := &VirtualMemoryStat{
2014-09-19 23:15:35 +08:00
Total: parsed[0] * p,
Free: parsed[1] * p,
Active: parsed[2] * p,
Inactive: parsed[3] * p,
Cached: parsed[4] * p,
Buffers: parsed[5],
Wired: parsed[6] * p,
}
ret.Available = ret.Inactive + ret.Cached + ret.Free
ret.Used = ret.Active + ret.Wired + ret.Cached
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)
2014-09-19 23:15:35 +08:00
total_v, err := strconv.ParseUint(values[1], 10, 64)
if err != nil {
return nil, err
}
used_v, err := strconv.ParseUint(values[2], 10, 64)
if err != nil {
return nil, err
}
free_v, err := strconv.ParseUint(values[3], 10, 64)
if err != nil {
return nil, err
}
up_v, err := strconv.ParseFloat(u, 64)
if err != nil {
return nil, err
}
ret = &SwapMemoryStat{
2014-09-19 23:15:35 +08:00
Total: total_v,
Used: used_v,
Free: free_v,
UsedPercent: up_v,
}
}
return ret, nil
}