mirror of https://github.com/shirou/gopsutil
Merge remote-tracking branch 'downstream/master' into elfrucool/fix-windows-load-avg
commit
e787f1a2b3
@ -0,0 +1,119 @@
|
|||||||
|
//go:build netbsd
|
||||||
|
// +build netbsd
|
||||||
|
|
||||||
|
package cpu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/shirou/gopsutil/v3/internal/common"
|
||||||
|
"github.com/tklauser/go-sysconf"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// sys/sysctl.h
|
||||||
|
ctlKern = 1 // "high kernel": proc, limits
|
||||||
|
ctlHw = 6 // CTL_HW
|
||||||
|
kernCpTime = 51 // KERN_CPTIME
|
||||||
|
)
|
||||||
|
|
||||||
|
var ClocksPerSec = float64(100)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
||||||
|
// ignore errors
|
||||||
|
if err == nil {
|
||||||
|
ClocksPerSec = float64(clkTck)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Times(percpu bool) ([]TimesStat, error) {
|
||||||
|
return TimesWithContext(context.Background(), percpu)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
|
||||||
|
if !percpu {
|
||||||
|
mib := []int32{ctlKern, kernCpTime}
|
||||||
|
buf, _, err := common.CallSyscall(mib)
|
||||||
|
if err != nil {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
|
||||||
|
stat := TimesStat{
|
||||||
|
CPU: "cpu-total",
|
||||||
|
User: float64(times.User),
|
||||||
|
Nice: float64(times.Nice),
|
||||||
|
System: float64(times.Sys),
|
||||||
|
Idle: float64(times.Idle),
|
||||||
|
Irq: float64(times.Intr),
|
||||||
|
}
|
||||||
|
return []TimesStat{stat}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ncpu, err := unix.SysctlUint32("hw.ncpu")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var i uint32
|
||||||
|
for i = 0; i < ncpu; i++ {
|
||||||
|
mib := []int32{ctlKern, kernCpTime, int32(i)}
|
||||||
|
buf, _, err := common.CallSyscall(mib)
|
||||||
|
if err != nil {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := (*cpuTimes)(unsafe.Pointer(&buf[0]))
|
||||||
|
ret = append(ret, TimesStat{
|
||||||
|
CPU: fmt.Sprintf("cpu%d", i),
|
||||||
|
User: float64(stats.User),
|
||||||
|
Nice: float64(stats.Nice),
|
||||||
|
System: float64(stats.Sys),
|
||||||
|
Idle: float64(stats.Idle),
|
||||||
|
Irq: float64(stats.Intr),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns only one (minimal) CPUInfoStat on NetBSD
|
||||||
|
func Info() ([]InfoStat, error) {
|
||||||
|
return InfoWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||||
|
var ret []InfoStat
|
||||||
|
var err error
|
||||||
|
|
||||||
|
c := InfoStat{}
|
||||||
|
|
||||||
|
mhz, err := unix.Sysctl("machdep.dmi.processor-frequency")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_, err = fmt.Sscanf(mhz, "%f", &c.Mhz)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ncpu, err := unix.SysctlUint32("hw.ncpuonline")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.Cores = int32(ncpu)
|
||||||
|
|
||||||
|
if c.ModelName, err = unix.Sysctl("machdep.dmi.processor-version"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return append(ret, c), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||||
|
return runtime.NumCPU(), nil
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package cpu
|
||||||
|
|
||||||
|
type cpuTimes struct {
|
||||||
|
User uint64
|
||||||
|
Nice uint64
|
||||||
|
Sys uint64
|
||||||
|
Intr uint64
|
||||||
|
Idle uint64
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package cpu
|
||||||
|
|
||||||
|
type cpuTimes struct {
|
||||||
|
User uint64
|
||||||
|
Nice uint64
|
||||||
|
Sys uint64
|
||||||
|
Intr uint64
|
||||||
|
Idle uint64
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package cpu
|
||||||
|
|
||||||
|
type cpuTimes struct {
|
||||||
|
User uint64
|
||||||
|
Nice uint64
|
||||||
|
Sys uint64
|
||||||
|
Spin uint64
|
||||||
|
Intr uint64
|
||||||
|
Idle uint64
|
||||||
|
}
|
@ -0,0 +1,152 @@
|
|||||||
|
//go:build netbsd
|
||||||
|
// +build netbsd
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/shirou/gopsutil/v3/internal/common"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// see sys/fstypes.h and `man 5 statvfs`
|
||||||
|
MNT_RDONLY = 0x00000001 /* read only filesystem */
|
||||||
|
MNT_SYNCHRONOUS = 0x00000002 /* file system written synchronously */
|
||||||
|
MNT_NOEXEC = 0x00000004 /* can't exec from filesystem */
|
||||||
|
MNT_NOSUID = 0x00000008 /* don't honor setuid bits on fs */
|
||||||
|
MNT_NODEV = 0x00000010 /* don't interpret special files */
|
||||||
|
MNT_ASYNC = 0x00000040 /* file system written asynchronously */
|
||||||
|
MNT_NOATIME = 0x04000000 /* Never update access times in fs */
|
||||||
|
MNT_SOFTDEP = 0x80000000 /* Use soft dependencies */
|
||||||
|
)
|
||||||
|
|
||||||
|
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
|
||||||
|
var ret []PartitionStat
|
||||||
|
|
||||||
|
flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h
|
||||||
|
|
||||||
|
// get required buffer size
|
||||||
|
emptyBufSize := 0
|
||||||
|
r, _, err := unix.Syscall(
|
||||||
|
483, // SYS___getvfsstat90 syscall
|
||||||
|
uintptr(unsafe.Pointer(nil)),
|
||||||
|
uintptr(unsafe.Pointer(&emptyBufSize)),
|
||||||
|
uintptr(unsafe.Pointer(&flag)),
|
||||||
|
)
|
||||||
|
if err != 0 {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
mountedFsCount := uint64(r)
|
||||||
|
|
||||||
|
// calculate the buffer size
|
||||||
|
bufSize := sizeOfStatvfs * mountedFsCount
|
||||||
|
buf := make([]Statvfs, mountedFsCount)
|
||||||
|
|
||||||
|
// request agian to get desired mount data
|
||||||
|
_, _, err = unix.Syscall(
|
||||||
|
483, // SYS___getvfsstat90 syscall
|
||||||
|
uintptr(unsafe.Pointer(&buf[0])),
|
||||||
|
uintptr(unsafe.Pointer(&bufSize)),
|
||||||
|
uintptr(unsafe.Pointer(&flag)),
|
||||||
|
)
|
||||||
|
if err != 0 {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, stat := range buf {
|
||||||
|
opts := []string{"rw"}
|
||||||
|
if stat.Flag&MNT_RDONLY != 0 {
|
||||||
|
opts = []string{"rw"}
|
||||||
|
}
|
||||||
|
if stat.Flag&MNT_SYNCHRONOUS != 0 {
|
||||||
|
opts = append(opts, "sync")
|
||||||
|
}
|
||||||
|
if stat.Flag&MNT_NOEXEC != 0 {
|
||||||
|
opts = append(opts, "noexec")
|
||||||
|
}
|
||||||
|
if stat.Flag&MNT_NOSUID != 0 {
|
||||||
|
opts = append(opts, "nosuid")
|
||||||
|
}
|
||||||
|
if stat.Flag&MNT_NODEV != 0 {
|
||||||
|
opts = append(opts, "nodev")
|
||||||
|
}
|
||||||
|
if stat.Flag&MNT_ASYNC != 0 {
|
||||||
|
opts = append(opts, "async")
|
||||||
|
}
|
||||||
|
if stat.Flag&MNT_SOFTDEP != 0 {
|
||||||
|
opts = append(opts, "softdep")
|
||||||
|
}
|
||||||
|
if stat.Flag&MNT_NOATIME != 0 {
|
||||||
|
opts = append(opts, "noatime")
|
||||||
|
}
|
||||||
|
|
||||||
|
d := PartitionStat{
|
||||||
|
Device: common.ByteToString([]byte(stat.Mntfromname[:])),
|
||||||
|
Mountpoint: common.ByteToString([]byte(stat.Mntonname[:])),
|
||||||
|
Fstype: common.ByteToString([]byte(stat.Fstypename[:])),
|
||||||
|
Opts: opts,
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = append(ret, d)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
|
||||||
|
ret := make(map[string]IOCountersStat)
|
||||||
|
return ret, common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
|
||||||
|
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
|
||||||
|
stat := Statvfs{}
|
||||||
|
flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h
|
||||||
|
|
||||||
|
_path, e := unix.BytePtrFromString(path)
|
||||||
|
if e != nil {
|
||||||
|
return nil, e
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err := unix.Syscall(
|
||||||
|
484, // SYS___statvfs190, see sys/syscall.h
|
||||||
|
uintptr(unsafe.Pointer(_path)),
|
||||||
|
uintptr(unsafe.Pointer(&stat)),
|
||||||
|
uintptr(unsafe.Pointer(&flag)),
|
||||||
|
)
|
||||||
|
if err != 0 {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// frsize is the real block size on NetBSD. See discuss here: https://bugzilla.samba.org/show_bug.cgi?id=11810
|
||||||
|
bsize := stat.Frsize
|
||||||
|
ret := &UsageStat{
|
||||||
|
Path: path,
|
||||||
|
Fstype: getFsType(stat),
|
||||||
|
Total: (uint64(stat.Blocks) * uint64(bsize)),
|
||||||
|
Free: (uint64(stat.Bavail) * uint64(bsize)),
|
||||||
|
InodesTotal: (uint64(stat.Files)),
|
||||||
|
InodesFree: (uint64(stat.Ffree)),
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.InodesUsed = (ret.InodesTotal - ret.InodesFree)
|
||||||
|
ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0
|
||||||
|
ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize)
|
||||||
|
ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getFsType(stat Statvfs) string {
|
||||||
|
return common.ByteToString(stat.Fstypename[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
|
||||||
|
return "", common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
|
||||||
|
func LabelWithContext(ctx context.Context, name string) (string, error) {
|
||||||
|
return "", common.ErrNotImplementedError
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
//go:build netbsd && amd64
|
||||||
|
// +build netbsd,amd64
|
||||||
|
|
||||||
|
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
|
||||||
|
// cgo -godefs types_netbsd.go
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
const (
|
||||||
|
sizeOfStatvfs = 0xce0
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Statvfs struct {
|
||||||
|
Flag uint64
|
||||||
|
Bsize uint64
|
||||||
|
Frsize uint64
|
||||||
|
Iosize uint64
|
||||||
|
Blocks uint64
|
||||||
|
Bfree uint64
|
||||||
|
Bavail uint64
|
||||||
|
Bresvd uint64
|
||||||
|
Files uint64
|
||||||
|
Ffree uint64
|
||||||
|
Favail uint64
|
||||||
|
Fresvd uint64
|
||||||
|
Syncreads uint64
|
||||||
|
Syncwrites uint64
|
||||||
|
Asyncreads uint64
|
||||||
|
Asyncwrites uint64
|
||||||
|
Fsidx _Ctype_struct___0
|
||||||
|
Fsid uint64
|
||||||
|
Namemax uint64
|
||||||
|
Owner uint32
|
||||||
|
Spare [4]uint64
|
||||||
|
Fstypename [32]uint8
|
||||||
|
Mntonname [1024]uint8
|
||||||
|
Mntfromname [1024]uint8
|
||||||
|
Mntfromlabel [1024]uint8
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type _Ctype_struct___0 struct {
|
||||||
|
FsidVal [2]int32
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
//go:build netbsd && arm64
|
||||||
|
// +build netbsd,arm64
|
||||||
|
|
||||||
|
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
|
||||||
|
// cgo -godefs types_netbsd.go
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
const (
|
||||||
|
sizeOfStatvfs = 0xce0
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Statvfs struct {
|
||||||
|
Flag uint64
|
||||||
|
Bsize uint64
|
||||||
|
Frsize uint64
|
||||||
|
Iosize uint64
|
||||||
|
Blocks uint64
|
||||||
|
Bfree uint64
|
||||||
|
Bavail uint64
|
||||||
|
Bresvd uint64
|
||||||
|
Files uint64
|
||||||
|
Ffree uint64
|
||||||
|
Favail uint64
|
||||||
|
Fresvd uint64
|
||||||
|
Syncreads uint64
|
||||||
|
Syncwrites uint64
|
||||||
|
Asyncreads uint64
|
||||||
|
Asyncwrites uint64
|
||||||
|
Fsidx _Ctype_struct___0
|
||||||
|
Fsid uint64
|
||||||
|
Namemax uint64
|
||||||
|
Owner uint32
|
||||||
|
Spare [4]uint64
|
||||||
|
Fstypename [32]uint8
|
||||||
|
Mntonname [1024]uint8
|
||||||
|
Mntfromname [1024]uint8
|
||||||
|
Mntfromlabel [1024]uint8
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type _Ctype_struct___0 struct {
|
||||||
|
FsidVal [2]int32
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
//go:build openbsd && riscv64
|
||||||
|
// +build openbsd,riscv64
|
||||||
|
|
||||||
|
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
|
||||||
|
// cgo -godefs disk/types_openbsd.go
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
const (
|
||||||
|
devstat_NO_DATA = 0x00
|
||||||
|
devstat_READ = 0x01
|
||||||
|
devstat_WRITE = 0x02
|
||||||
|
devstat_FREE = 0x03
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sizeOfDiskstats = 0x70
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Diskstats struct {
|
||||||
|
Name [16]int8
|
||||||
|
Busy int32
|
||||||
|
Rxfer uint64
|
||||||
|
Wxfer uint64
|
||||||
|
Seek uint64
|
||||||
|
Rbytes uint64
|
||||||
|
Wbytes uint64
|
||||||
|
Attachtime Timeval
|
||||||
|
Timestamp Timeval
|
||||||
|
Time Timeval
|
||||||
|
}
|
||||||
|
Timeval struct {
|
||||||
|
Sec int64
|
||||||
|
Usec int64
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type Diskstat struct{}
|
||||||
|
type bintime struct{}
|
@ -0,0 +1,30 @@
|
|||||||
|
//go:build ignore
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
// Hand writing: _Ctype_struct___0
|
||||||
|
|
||||||
|
/*
|
||||||
|
Input to cgo -godefs.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/statvfs.h>
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
#include <sys/featuretest.h>
|
||||||
|
#include <sys/stdint.h>
|
||||||
|
#include <machine/ansi.h>
|
||||||
|
#include <sys/ansi.h>
|
||||||
|
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
const (
|
||||||
|
sizeOfStatvfs = C.sizeof_struct_statvfs
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Statvfs C.struct_statvfs
|
||||||
|
)
|
@ -0,0 +1,55 @@
|
|||||||
|
//go:build netbsd
|
||||||
|
// +build netbsd
|
||||||
|
|
||||||
|
package host
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/shirou/gopsutil/v3/internal/common"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HostIDWithContext(ctx context.Context) (string, error) {
|
||||||
|
return "", common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
|
||||||
|
func numProcs(ctx context.Context) (uint64, error) {
|
||||||
|
return 0, common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
|
||||||
|
func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
|
||||||
|
platform := ""
|
||||||
|
family := ""
|
||||||
|
version := ""
|
||||||
|
|
||||||
|
p, err := unix.Sysctl("kern.ostype")
|
||||||
|
if err == nil {
|
||||||
|
platform = strings.ToLower(p)
|
||||||
|
}
|
||||||
|
v, err := unix.Sysctl("kern.osrelease")
|
||||||
|
if err == nil {
|
||||||
|
version = strings.ToLower(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return platform, family, version, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
|
||||||
|
return "", "", common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
|
||||||
|
func UsersWithContext(ctx context.Context) ([]UserStat, error) {
|
||||||
|
var ret []UserStat
|
||||||
|
return ret, common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
|
||||||
|
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
|
||||||
|
return []TemperatureStat{}, common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
|
||||||
|
func KernelVersionWithContext(ctx context.Context) (string, error) {
|
||||||
|
_, _, version, err := PlatformInformationWithContext(ctx)
|
||||||
|
return version, err
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
//go:build openbsd && riscv64
|
||||||
|
// +build openbsd,riscv64
|
||||||
|
|
||||||
|
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
|
||||||
|
// cgo -godefs host/types_openbsd.go
|
||||||
|
|
||||||
|
package host
|
||||||
|
|
||||||
|
const (
|
||||||
|
sizeofPtr = 0x8
|
||||||
|
sizeofShort = 0x2
|
||||||
|
sizeofInt = 0x4
|
||||||
|
sizeofLong = 0x8
|
||||||
|
sizeofLongLong = 0x8
|
||||||
|
sizeOfUtmp = 0x130
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
_C_short int16
|
||||||
|
_C_int int32
|
||||||
|
_C_long int64
|
||||||
|
_C_long_long int64
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Utmp struct {
|
||||||
|
Line [8]int8
|
||||||
|
Name [32]int8
|
||||||
|
Host [256]int8
|
||||||
|
Time int64
|
||||||
|
}
|
||||||
|
Timeval struct {
|
||||||
|
Sec int64
|
||||||
|
Usec int64
|
||||||
|
}
|
||||||
|
)
|
@ -0,0 +1,66 @@
|
|||||||
|
//go:build netbsd
|
||||||
|
// +build netbsd
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DoSysctrl(mib string) ([]string, error) {
|
||||||
|
cmd := exec.Command("sysctl", "-n", mib)
|
||||||
|
cmd.Env = getSysctrlEnv(os.Environ())
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
v := strings.Replace(string(out), "{ ", "", 1)
|
||||||
|
v = strings.Replace(string(v), " }", "", 1)
|
||||||
|
values := strings.Fields(string(v))
|
||||||
|
|
||||||
|
return values, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CallSyscall(mib []int32) ([]byte, uint64, error) {
|
||||||
|
mibptr := unsafe.Pointer(&mib[0])
|
||||||
|
miblen := uint64(len(mib))
|
||||||
|
|
||||||
|
// get required buffer size
|
||||||
|
length := uint64(0)
|
||||||
|
_, _, err := unix.Syscall6(
|
||||||
|
unix.SYS___SYSCTL,
|
||||||
|
uintptr(mibptr),
|
||||||
|
uintptr(miblen),
|
||||||
|
0,
|
||||||
|
uintptr(unsafe.Pointer(&length)),
|
||||||
|
0,
|
||||||
|
0)
|
||||||
|
if err != 0 {
|
||||||
|
var b []byte
|
||||||
|
return b, length, err
|
||||||
|
}
|
||||||
|
if length == 0 {
|
||||||
|
var b []byte
|
||||||
|
return b, length, err
|
||||||
|
}
|
||||||
|
// get proc info itself
|
||||||
|
buf := make([]byte, length)
|
||||||
|
_, _, err = unix.Syscall6(
|
||||||
|
unix.SYS___SYSCTL,
|
||||||
|
uintptr(mibptr),
|
||||||
|
uintptr(miblen),
|
||||||
|
uintptr(unsafe.Pointer(&buf[0])),
|
||||||
|
uintptr(unsafe.Pointer(&length)),
|
||||||
|
0,
|
||||||
|
0)
|
||||||
|
if err != 0 {
|
||||||
|
return buf, length, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf, length, nil
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
//go:build netbsd
|
||||||
|
// +build netbsd
|
||||||
|
|
||||||
|
package mem
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetPageSize() (uint64, error) {
|
||||||
|
return GetPageSizeWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPageSizeWithContext(ctx context.Context) (uint64, error) {
|
||||||
|
uvmexp, err := unix.SysctlUvmexp("vm.uvmexp2")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return uint64(uvmexp.Pagesize), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||||
|
return VirtualMemoryWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
||||||
|
uvmexp, err := unix.SysctlUvmexp("vm.uvmexp2")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p := uint64(uvmexp.Pagesize)
|
||||||
|
|
||||||
|
ret := &VirtualMemoryStat{
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.Available = ret.Inactive + ret.Cached + ret.Free
|
||||||
|
ret.Used = ret.Total - ret.Available
|
||||||
|
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
|
||||||
|
|
||||||
|
// Get buffers from vm.bufmem sysctl
|
||||||
|
ret.Buffers, err = unix.SysctlUint64("vm.bufmem")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return swapctl summary info
|
||||||
|
func SwapMemory() (*SwapMemoryStat, error) {
|
||||||
|
return SwapMemoryWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
||||||
|
out, err := invoke.CommandWithContext(ctx, "swapctl", "-sk")
|
||||||
|
if err != nil {
|
||||||
|
return &SwapMemoryStat{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
line := string(out)
|
||||||
|
var total, used, free uint64
|
||||||
|
|
||||||
|
_, err = fmt.Sscanf(line,
|
||||||
|
"total: %d 1K-blocks allocated, %d used, %d available",
|
||||||
|
&total, &used, &free)
|
||||||
|
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
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
//go:build openbsd && riscv64
|
||||||
|
// +build openbsd,riscv64
|
||||||
|
|
||||||
|
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
|
||||||
|
// cgo -godefs mem/types_openbsd.go
|
||||||
|
|
||||||
|
package mem
|
||||||
|
|
||||||
|
const (
|
||||||
|
CTLVfs = 10
|
||||||
|
VfsGeneric = 0
|
||||||
|
VfsBcacheStat = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sizeOfBcachestats = 0x90
|
||||||
|
)
|
||||||
|
|
||||||
|
type Bcachestats struct {
|
||||||
|
Numbufs int64
|
||||||
|
Numbufpages int64
|
||||||
|
Numdirtypages int64
|
||||||
|
Numcleanpages int64
|
||||||
|
Pendingwrites int64
|
||||||
|
Pendingreads int64
|
||||||
|
Numwrites int64
|
||||||
|
Numreads int64
|
||||||
|
Cachehits int64
|
||||||
|
Busymapped int64
|
||||||
|
Dmapages int64
|
||||||
|
Highpages int64
|
||||||
|
Delwribufs int64
|
||||||
|
Kvaslots int64
|
||||||
|
Avail int64
|
||||||
|
Highflips int64
|
||||||
|
Highflops int64
|
||||||
|
Dmaflips int64
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
MemTotal: 260799420 kB
|
||||||
|
MemFree: 119443248 kB
|
||||||
|
MemAvailable: 127880216 kB
|
||||||
|
AnonHugePages: 50409472 kB
|
@ -0,0 +1,204 @@
|
|||||||
|
//go:build openbsd && riscv64
|
||||||
|
// +build openbsd,riscv64
|
||||||
|
|
||||||
|
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
|
||||||
|
// cgo -godefs process/types_openbsd.go
|
||||||
|
|
||||||
|
package process
|
||||||
|
|
||||||
|
const (
|
||||||
|
CTLKern = 1
|
||||||
|
KernProc = 66
|
||||||
|
KernProcAll = 0
|
||||||
|
KernProcPID = 1
|
||||||
|
KernProcProc = 8
|
||||||
|
KernProcPathname = 12
|
||||||
|
KernProcArgs = 55
|
||||||
|
KernProcArgv = 1
|
||||||
|
KernProcEnv = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ArgMax = 256 * 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sizeofPtr = 0x8
|
||||||
|
sizeofShort = 0x2
|
||||||
|
sizeofInt = 0x4
|
||||||
|
sizeofLong = 0x8
|
||||||
|
sizeofLongLong = 0x8
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sizeOfKinfoVmentry = 0x50
|
||||||
|
sizeOfKinfoProc = 0x288
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SIDL = 1
|
||||||
|
SRUN = 2
|
||||||
|
SSLEEP = 3
|
||||||
|
SSTOP = 4
|
||||||
|
SZOMB = 5
|
||||||
|
SDEAD = 6
|
||||||
|
SONPROC = 7
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
_C_short int16
|
||||||
|
_C_int int32
|
||||||
|
_C_long int64
|
||||||
|
_C_long_long int64
|
||||||
|
)
|
||||||
|
|
||||||
|
type Timespec struct {
|
||||||
|
Sec int64
|
||||||
|
Nsec int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Timeval struct {
|
||||||
|
Sec int64
|
||||||
|
Usec int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Rusage struct {
|
||||||
|
Utime Timeval
|
||||||
|
Stime Timeval
|
||||||
|
Maxrss int64
|
||||||
|
Ixrss int64
|
||||||
|
Idrss int64
|
||||||
|
Isrss int64
|
||||||
|
Minflt int64
|
||||||
|
Majflt int64
|
||||||
|
Nswap int64
|
||||||
|
Inblock int64
|
||||||
|
Oublock int64
|
||||||
|
Msgsnd int64
|
||||||
|
Msgrcv int64
|
||||||
|
Nsignals int64
|
||||||
|
Nvcsw int64
|
||||||
|
Nivcsw int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Rlimit struct {
|
||||||
|
Cur uint64
|
||||||
|
Max uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type KinfoProc struct {
|
||||||
|
Forw uint64
|
||||||
|
Back uint64
|
||||||
|
Paddr uint64
|
||||||
|
Addr uint64
|
||||||
|
Fd uint64
|
||||||
|
Stats uint64
|
||||||
|
Limit uint64
|
||||||
|
Vmspace uint64
|
||||||
|
Sigacts uint64
|
||||||
|
Sess uint64
|
||||||
|
Tsess uint64
|
||||||
|
Ru uint64
|
||||||
|
Eflag int32
|
||||||
|
Exitsig int32
|
||||||
|
Flag int32
|
||||||
|
Pid int32
|
||||||
|
Ppid int32
|
||||||
|
Sid int32
|
||||||
|
X_pgid int32
|
||||||
|
Tpgid int32
|
||||||
|
Uid uint32
|
||||||
|
Ruid uint32
|
||||||
|
Gid uint32
|
||||||
|
Rgid uint32
|
||||||
|
Groups [16]uint32
|
||||||
|
Ngroups int16
|
||||||
|
Jobc int16
|
||||||
|
Tdev uint32
|
||||||
|
Estcpu uint32
|
||||||
|
Rtime_sec uint32
|
||||||
|
Rtime_usec uint32
|
||||||
|
Cpticks int32
|
||||||
|
Pctcpu uint32
|
||||||
|
Swtime uint32
|
||||||
|
Slptime uint32
|
||||||
|
Schedflags int32
|
||||||
|
Uticks uint64
|
||||||
|
Sticks uint64
|
||||||
|
Iticks uint64
|
||||||
|
Tracep uint64
|
||||||
|
Traceflag int32
|
||||||
|
Holdcnt int32
|
||||||
|
Siglist int32
|
||||||
|
Sigmask uint32
|
||||||
|
Sigignore uint32
|
||||||
|
Sigcatch uint32
|
||||||
|
Stat int8
|
||||||
|
Priority uint8
|
||||||
|
Usrpri uint8
|
||||||
|
Nice uint8
|
||||||
|
Xstat uint16
|
||||||
|
Spare uint16
|
||||||
|
Comm [24]int8
|
||||||
|
Wmesg [8]uint8
|
||||||
|
Wchan uint64
|
||||||
|
Login [32]uint8
|
||||||
|
Vm_rssize int32
|
||||||
|
Vm_tsize int32
|
||||||
|
Vm_dsize int32
|
||||||
|
Vm_ssize int32
|
||||||
|
Uvalid int64
|
||||||
|
Ustart_sec uint64
|
||||||
|
Ustart_usec uint32
|
||||||
|
Uutime_sec uint32
|
||||||
|
Uutime_usec uint32
|
||||||
|
Ustime_sec uint32
|
||||||
|
Ustime_usec uint32
|
||||||
|
Uru_maxrss uint64
|
||||||
|
Uru_ixrss uint64
|
||||||
|
Uru_idrss uint64
|
||||||
|
Uru_isrss uint64
|
||||||
|
Uru_minflt uint64
|
||||||
|
Uru_majflt uint64
|
||||||
|
Uru_nswap uint64
|
||||||
|
Uru_inblock uint64
|
||||||
|
Uru_oublock uint64
|
||||||
|
Uru_msgsnd uint64
|
||||||
|
Uru_msgrcv uint64
|
||||||
|
Uru_nsignals uint64
|
||||||
|
Uru_nvcsw uint64
|
||||||
|
Uru_nivcsw uint64
|
||||||
|
Uctime_sec uint32
|
||||||
|
Uctime_usec uint32
|
||||||
|
Psflags uint32
|
||||||
|
Acflag uint32
|
||||||
|
Svuid uint32
|
||||||
|
Svgid uint32
|
||||||
|
Emul [8]uint8
|
||||||
|
Rlim_rss_cur uint64
|
||||||
|
Cpuid uint64
|
||||||
|
Vm_map_size uint64
|
||||||
|
Tid int32
|
||||||
|
Rtableid uint32
|
||||||
|
Pledge uint64
|
||||||
|
Name [24]uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
type Priority struct{}
|
||||||
|
|
||||||
|
type KinfoVmentry struct {
|
||||||
|
Start uint64
|
||||||
|
End uint64
|
||||||
|
Guard uint64
|
||||||
|
Fspace uint64
|
||||||
|
Fspace_augment uint64
|
||||||
|
Offset uint64
|
||||||
|
Wired_count int32
|
||||||
|
Etype int32
|
||||||
|
Protection int32
|
||||||
|
Max_protection int32
|
||||||
|
Advice int32
|
||||||
|
Inheritance int32
|
||||||
|
Flags uint8
|
||||||
|
Pad_cgo_0 [7]byte
|
||||||
|
}
|
Loading…
Reference in New Issue