2017-02-01 18:26:00 +00:00
|
|
|
// +build freebsd
|
|
|
|
|
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2017-02-01 18:26:00 +00:00
|
|
|
"errors"
|
2018-10-19 18:33:23 +00:00
|
|
|
"unsafe"
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return VirtualMemoryWithContext(context.Background())
|
|
|
|
}
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
|
|
|
pageSize, err := unix.SysctlUint32("vm.stats.vm.v_page_size")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
physmem, err := unix.SysctlUint64("hw.physmem")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
free, err := unix.SysctlUint32("vm.stats.vm.v_free_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
active, err := unix.SysctlUint32("vm.stats.vm.v_active_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
inactive, err := unix.SysctlUint32("vm.stats.vm.v_inactive_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
buffers, err := unix.SysctlUint64("vfs.bufspace")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
wired, err := unix.SysctlUint32("vm.stats.vm.v_wire_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
var cached, laundry uint32
|
|
|
|
osreldate, _ := unix.SysctlUint32("kern.osreldate")
|
|
|
|
if osreldate < 1102000 {
|
|
|
|
cached, err = unix.SysctlUint32("vm.stats.vm.v_cache_count")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
laundry, err = unix.SysctlUint32("vm.stats.vm.v_laundry_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
p := uint64(pageSize)
|
2017-02-01 18:26:00 +00:00
|
|
|
ret := &VirtualMemoryStat{
|
2018-10-19 18:33:23 +00:00
|
|
|
Total: uint64(physmem),
|
|
|
|
Free: uint64(free) * p,
|
|
|
|
Active: uint64(active) * p,
|
|
|
|
Inactive: uint64(inactive) * p,
|
|
|
|
Cached: uint64(cached) * p,
|
|
|
|
Buffers: uint64(buffers),
|
|
|
|
Wired: uint64(wired) * p,
|
|
|
|
Laundry: uint64(laundry) * p,
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
ret.Available = ret.Inactive + ret.Cached + ret.Free + ret.Laundry
|
2017-02-01 18:26:00 +00:00
|
|
|
ret.Used = ret.Total - ret.Available
|
|
|
|
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return swapinfo
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return SwapMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constants from vm/vm_param.h
|
|
|
|
// nolint: golint
|
|
|
|
const (
|
|
|
|
XSWDEV_VERSION = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// Types from vm/vm_param.h
|
|
|
|
type xswdev struct {
|
|
|
|
Version uint32 // Version is the version
|
|
|
|
Dev uint32 // Dev is the device identifier
|
|
|
|
Flags int32 // Flags is the swap flags applied to the device
|
|
|
|
NBlks int32 // NBlks is the total number of blocks
|
|
|
|
Used int32 // Used is the number of blocks used
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
|
|
|
// FreeBSD can have multiple swap devices so we total them up
|
|
|
|
i, err := unix.SysctlUint32("vm.nswapdev")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
if i == 0 {
|
|
|
|
return nil, errors.New("no swap devices found")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := int(i)
|
|
|
|
|
|
|
|
i, err = unix.SysctlUint32("vm.stats.vm.v_page_size")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
pageSize := uint64(i)
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
var buf []byte
|
|
|
|
s := &SwapMemoryStat{}
|
|
|
|
for n := 0; n < c; n++ {
|
|
|
|
buf, err = unix.SysctlRaw("vm.swap_info", n)
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
xsw := (*xswdev)(unsafe.Pointer(&buf[0]))
|
|
|
|
if xsw.Version != XSWDEV_VERSION {
|
|
|
|
return nil, errors.New("xswdev version mismatch")
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
s.Total += uint64(xsw.NBlks)
|
|
|
|
s.Used += uint64(xsw.Used)
|
|
|
|
}
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
if s.Total != 0 {
|
|
|
|
s.UsedPercent = float64(s.Used) / float64(s.Total) * 100
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
s.Total *= pageSize
|
|
|
|
s.Used *= pageSize
|
|
|
|
s.Free = s.Total - s.Used
|
2017-02-01 18:26:00 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
return s, nil
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|