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"
|
2020-07-01 12:47:56 +00:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
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) {
|
2020-07-01 12:47:56 +00:00
|
|
|
pageSize, err := common.SysctlUint("vm.stats.vm.v_page_size")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
physmem, err := common.SysctlUint("hw.physmem")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
|
|
|
|
free, err := common.SysctlUint("vm.stats.vm.v_free_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
active, err := common.SysctlUint("vm.stats.vm.v_active_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
inactive, err := common.SysctlUint("vm.stats.vm.v_inactive_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
buffers, err := common.SysctlUint("vfs.bufspace")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
wired, err := common.SysctlUint("vm.stats.vm.v_wire_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
var cached, laundry uint64
|
|
|
|
osreldate, _ := common.SysctlUint("kern.osreldate")
|
2018-10-19 18:33:23 +00:00
|
|
|
if osreldate < 1102000 {
|
2020-07-01 12:47:56 +00:00
|
|
|
cached, err = common.SysctlUint("vm.stats.vm.v_cache_count")
|
2018-10-19 18:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-01 12:47:56 +00:00
|
|
|
laundry, err = common.SysctlUint("vm.stats.vm.v_laundry_count")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
p := pageSize
|
2017-02-01 18:26:00 +00:00
|
|
|
ret := &VirtualMemoryStat{
|
2020-07-01 12:47:56 +00:00
|
|
|
Total: physmem,
|
|
|
|
Free: free * p,
|
|
|
|
Active: active * p,
|
|
|
|
Inactive: inactive * p,
|
|
|
|
Cached: cached * p,
|
|
|
|
Buffers: buffers,
|
|
|
|
Wired: wired * p,
|
|
|
|
Laundry: 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 (
|
2020-07-01 12:47:56 +00:00
|
|
|
XSWDEV_VERSION11 = 1
|
|
|
|
XSWDEV_VERSION = 2
|
2018-10-19 18:33:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Types from vm/vm_param.h
|
|
|
|
type xswdev struct {
|
2020-07-01 12:47:56 +00:00
|
|
|
Version uint32 // Version is the version
|
|
|
|
Dev uint64 // 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// xswdev11 is a compatibility for under FreeBSD 11
|
|
|
|
// sys/vm/swap_pager.c
|
|
|
|
type xswdev11 struct {
|
2018-10-19 18:33:23 +00:00
|
|
|
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
|
2020-07-01 12:47:56 +00:00
|
|
|
i, err := common.SysctlUint("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)
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
i, err = common.SysctlUint("vm.stats.vm.v_page_size")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
pageSize := 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
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
// first, try to parse with version 2
|
2018-10-19 18:33:23 +00:00
|
|
|
xsw := (*xswdev)(unsafe.Pointer(&buf[0]))
|
2020-07-01 12:47:56 +00:00
|
|
|
if xsw.Version == XSWDEV_VERSION11 {
|
|
|
|
// this is version 1, so try to parse again
|
|
|
|
xsw := (*xswdev11)(unsafe.Pointer(&buf[0]))
|
|
|
|
if xsw.Version != XSWDEV_VERSION11 {
|
|
|
|
return nil, errors.New("xswdev version mismatch(11)")
|
|
|
|
}
|
|
|
|
s.Total += uint64(xsw.NBlks)
|
|
|
|
s.Used += uint64(xsw.Used)
|
|
|
|
} else if xsw.Version != XSWDEV_VERSION {
|
2018-10-19 18:33:23 +00:00
|
|
|
return nil, errors.New("xswdev version mismatch")
|
2020-07-01 12:47:56 +00:00
|
|
|
} else {
|
|
|
|
s.Total += uint64(xsw.NBlks)
|
|
|
|
s.Used += uint64(xsw.Used)
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
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
|
|
|
}
|