2021-12-23 05:54:41 +08:00
|
|
|
//go:build openbsd
|
2016-11-22 06:18:52 +08:00
|
|
|
// +build openbsd
|
|
|
|
|
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2016-11-22 06:18:52 +08:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-02-12 17:24:52 +08:00
|
|
|
|
2021-11-06 17:53:56 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2020-08-31 18:31:15 +08:00
|
|
|
"golang.org/x/sys/unix"
|
2016-11-22 06:18:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetPageSize() (uint64, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return GetPageSizeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPageSizeWithContext(ctx context.Context) (uint64, error) {
|
2020-08-31 18:31:15 +08:00
|
|
|
uvmexp, err := unix.SysctlUvmexp("vm.uvmexp")
|
2016-11-22 06:18:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint64(uvmexp.Pagesize), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return VirtualMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
2020-08-31 18:31:15 +08:00
|
|
|
uvmexp, err := unix.SysctlUvmexp("vm.uvmexp")
|
2016-11-22 06:18:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p := uint64(uvmexp.Pagesize)
|
|
|
|
|
|
|
|
ret := &VirtualMemoryStat{
|
2017-02-02 07:05:29 +08:00
|
|
|
Total: uint64(uvmexp.Npages) * p,
|
|
|
|
Free: uint64(uvmexp.Free) * p,
|
|
|
|
Active: uint64(uvmexp.Active) * p,
|
|
|
|
Inactive: uint64(uvmexp.Inactive) * p,
|
|
|
|
Cached: 0, // not available
|
|
|
|
Wired: uint64(uvmexp.Wired) * p,
|
2016-11-22 06:18:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.Available = ret.Inactive + ret.Cached + ret.Free
|
|
|
|
ret.Used = ret.Total - ret.Available
|
|
|
|
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
|
|
|
|
|
2020-08-31 18:31:15 +08:00
|
|
|
mib := []int32{CTLVfs, VfsGeneric, VfsBcacheStat}
|
|
|
|
buf, length, err := common.CallSyscall(mib)
|
2016-11-22 06:18:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if length < sizeOfBcachestats {
|
|
|
|
return nil, fmt.Errorf("short syscall ret %d bytes", length)
|
|
|
|
}
|
|
|
|
var bcs Bcachestats
|
2020-08-31 18:31:15 +08:00
|
|
|
br := bytes.NewReader(buf)
|
2016-11-22 06:18:52 +08:00
|
|
|
err = common.Read(br, binary.LittleEndian, &bcs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.Buffers = uint64(bcs.Numbufpages) * p
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return swapctl summary info
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return SwapMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
2022-03-05 00:18:03 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, "swapctl", "-sk")
|
2016-11-22 06:18:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return &SwapMemoryStat{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
line := string(out)
|
|
|
|
var total, used, free uint64
|
|
|
|
|
|
|
|
_, err = fmt.Sscanf(line,
|
2017-02-02 07:05:29 +08:00
|
|
|
"total: %d 1K-blocks allocated, %d used, %d available",
|
|
|
|
&total, &used, &free)
|
2016-11-22 06:18:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("failed to parse swapctl output")
|
|
|
|
}
|
|
|
|
|
|
|
|
percent := float64(used) / float64(total) * 100
|
|
|
|
return &SwapMemoryStat{
|
|
|
|
Total: total * 1024,
|
|
|
|
Used: used * 1024,
|
|
|
|
Free: free * 1024,
|
|
|
|
UsedPercent: percent,
|
|
|
|
}, nil
|
|
|
|
}
|