From d8728fc85b8c29e5859f520e4dda93528339fcfc Mon Sep 17 00:00:00 2001 From: xca1075 Date: Fri, 12 Nov 2021 11:39:46 +0100 Subject: [PATCH] add initial aix support --- v3/cpu/cpu_aix.go | 74 ++++++++++++++++++++++++++++++++++ v3/cpu/cpu_fallback.go | 2 +- v3/disk/disk_aix.go | 86 ++++++++++++++++++++++++++++++++++++++++ v3/disk/disk_fallback.go | 2 +- v3/load/load_aix.go | 71 +++++++++++++++++++++++++++++++++ v3/load/load_fallback.go | 2 +- v3/mem/mem_aix.go | 58 +++++++++++++++++++++++++++ v3/mem/mem_fallback.go | 2 +- 8 files changed, 293 insertions(+), 4 deletions(-) create mode 100644 v3/cpu/cpu_aix.go create mode 100644 v3/disk/disk_aix.go create mode 100644 v3/load/load_aix.go create mode 100644 v3/mem/mem_aix.go diff --git a/v3/cpu/cpu_aix.go b/v3/cpu/cpu_aix.go new file mode 100644 index 00000000..a0c6946e --- /dev/null +++ b/v3/cpu/cpu_aix.go @@ -0,0 +1,74 @@ +// +build aix + +package cpu + +import ( + "context" + + "github.com/power-devops/perfstat" +) + +func Times(percpu bool) ([]TimesStat, error) { + return TimesWithContext(context.Background(), percpu) +} + +func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { + var ret []TimesStat + if percpu { + cpus, err := perfstat.CpuStat() + if err != nil { + return nil, err + } + for _, c := range cpus { + ct := &TimesStat{ + CPU: c.Name, + Idle: float64(c.Idle), + User: float64(c.User), + System: float64(c.Sys), + Iowait: float64(c.Wait), + } + ret = append(ret, *ct) + } + } else { + c, err := perfstat.CpuUtilTotalStat() + if err != nil { + return nil, err + } + ct := &TimesStat{ + CPU: "cpu-total", + Idle: float64(c.IdlePct), + User: float64(c.UserPct), + System: float64(c.KernPct), + Iowait: float64(c.WaitPct), + } + ret = append(ret, *ct) + } + return ret, nil +} + +func Info() ([]InfoStat, error) { + return InfoWithContext(context.Background()) +} + +func InfoWithContext(ctx context.Context) ([]InfoStat, error) { + c, err := perfstat.CpuTotalStat() + if err != nil { + return nil, err + } + info := InfoStat{ + CPU: 0, + Mhz: float64(c.ProcessorHz / 1000000), + Cores: int32(c.NCpusCfg), + } + result := []InfoStat{info}; + return result, nil +} + +func CountsWithContext(ctx context.Context, logical bool) (int, error) { + c, err := perfstat.CpuTotalStat() + if err != nil { + return 0, err + } + return c.NCpusCfg, nil +} + diff --git a/v3/cpu/cpu_fallback.go b/v3/cpu/cpu_fallback.go index 1de597f8..5f0440be 100644 --- a/v3/cpu/cpu_fallback.go +++ b/v3/cpu/cpu_fallback.go @@ -1,4 +1,4 @@ -// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9 +// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9,!aix package cpu diff --git a/v3/disk/disk_aix.go b/v3/disk/disk_aix.go new file mode 100644 index 00000000..bbebd176 --- /dev/null +++ b/v3/disk/disk_aix.go @@ -0,0 +1,86 @@ +// +build aix + +package disk + +import ( + "context" + "fmt" + + "github.com/power-devops/perfstat" + "github.com/shirou/gopsutil/v3/internal/common" +) + +var FSType map[int]string + +func init() { + FSType = map[int]string{0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc", + 16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs", + 33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs", + 39: "ahafs", 40: "sterm-nfs", 41: "asmfs" } +} + +func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + f, err := perfstat.FileSystemStat() + if err != nil { + return nil, err + } + ret := make([]PartitionStat, len(f)) + + for _, fs := range f { + fstyp, exists := FSType[fs.FSType] + if ! exists { + fstyp = "unknown" + } + info := PartitionStat{ + Device: fs.Device, + Mountpoint: fs.MountPoint, + Fstype: fstyp, + } + ret = append(ret,info) + } + + return ret, err +} + +func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { + return nil, common.ErrNotImplementedError +} + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + f, err := perfstat.FileSystemStat() + if err != nil { + return nil, err + } + + blocksize := uint64(512) + for _, fs := range f { + if path == fs.MountPoint { + fstyp, exists := FSType[fs.FSType] + if ! exists { + fstyp = "unknown" + } + info := UsageStat{ + Path: path, + Fstype: fstyp, + Total: uint64(fs.TotalBlocks) * blocksize, + Free: uint64(fs.FreeBlocks) * blocksize, + Used: uint64(fs.TotalBlocks - fs.FreeBlocks) * blocksize, + InodesTotal: uint64(fs.TotalInodes), + InodesFree: uint64(fs.FreeInodes), + InodesUsed: uint64(fs.TotalInodes - fs.FreeInodes), + } + info.UsedPercent = (float64(info.Used) / float64(info.Total)) * 100.0 + info.InodesUsedPercent = (float64(info.InodesUsed) / float64(info.InodesTotal)) * 100.0 + return &info, nil + } + } + return nil, fmt.Errorf("mountpoint %s not found", path) +} + +func SerialNumberWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} + +func LabelWithContext(ctx context.Context, name string) (string, error) { + return "", common.ErrNotImplementedError +} diff --git a/v3/disk/disk_fallback.go b/v3/disk/disk_fallback.go index f50ebf19..4bb4633a 100644 --- a/v3/disk/disk_fallback.go +++ b/v3/disk/disk_fallback.go @@ -1,4 +1,4 @@ -// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris +// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!aix package disk diff --git a/v3/load/load_aix.go b/v3/load/load_aix.go new file mode 100644 index 00000000..4df26df2 --- /dev/null +++ b/v3/load/load_aix.go @@ -0,0 +1,71 @@ +// +build aix + +package load + +/* +#cgo LDFLAGS: -L/usr/lib -lperfstat + +#include +#include +*/ +import "C" + +import ( + "context" + "unsafe" + + "github.com/power-devops/perfstat" +) + +func Avg() (*AvgStat, error) { + return AvgWithContext(context.Background()) +} + +func AvgWithContext(ctx context.Context) (*AvgStat, error) { + c, err := perfstat.CpuTotalStat() + if err != nil { + return nil, err + } + ret := &AvgStat{ + Load1: float64(c.LoadAvg1), + Load5: float64(c.LoadAvg5), + Load15: float64(c.LoadAvg15), + } + + return ret, nil +} + +// Misc returns miscellaneous host-wide statistics. +// darwin use ps command to get process running/blocked count. +// Almost same as Darwin implementation, but state is different. +func Misc() (*MiscStat, error) { + return MiscWithContext(context.Background()) +} + +func MiscWithContext(ctx context.Context) (*MiscStat, error) { + info := C.struct_procentry64{} + cpid := C.pid_t(0) + + ret := MiscStat{} + for { + // getprocs first argument is a void* + num, err := C.getprocs64(unsafe.Pointer(&info), C.sizeof_struct_procentry64, nil, 0, &cpid, 1) + if err != nil { + return nil, err + } + + ret.ProcsTotal++ + switch info.pi_state { + case C.SACTIVE: + ret.ProcsRunning++ + case C.SSTOP: + ret.ProcsBlocked++ + } + + if num == 0 { + break + } + } + return &ret, nil +} + diff --git a/v3/load/load_fallback.go b/v3/load/load_fallback.go index 631e0ffe..17126ac4 100644 --- a/v3/load/load_fallback.go +++ b/v3/load/load_fallback.go @@ -1,4 +1,4 @@ -// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris +// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!aix package load diff --git a/v3/mem/mem_aix.go b/v3/mem/mem_aix.go new file mode 100644 index 00000000..6ba5d28e --- /dev/null +++ b/v3/mem/mem_aix.go @@ -0,0 +1,58 @@ +// +build aix + +package mem + +import ( + "context" + + "github.com/power-devops/perfstat" +) + +func VirtualMemory() (*VirtualMemoryStat, error) { + return VirtualMemoryWithContext(context.Background()) +} + +func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { + m, err := perfstat.MemoryTotalStat() + if err != nil { + return nil, err + } + pagesize := uint64(4096) + ret := VirtualMemoryStat{ + Total: uint64(m.RealTotal) * pagesize, + Available: uint64(m.RealAvailable) * pagesize, + Free: uint64(m.RealFree) * pagesize, + Used: uint64(m.RealInUse) * pagesize, + UsedPercent: 100 * float64(m.RealInUse) / float64(m.RealTotal), + Active: uint64(m.VirtualActive) * pagesize, + SwapTotal: uint64(m.PgSpTotal) * pagesize, + SwapFree: uint64(m.PgSpFree) * pagesize, + } + return &ret, nil +} + +func SwapMemory() (*SwapMemoryStat, error) { + return SwapMemoryWithContext(context.Background()) +} + +func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { + m, err := perfstat.MemoryTotalStat() + if err != nil { + return nil, err + } + pagesize := uint64(4096) + swapUsed := uint64(m.PgSpTotal - m.PgSpFree - m.PgSpRsvd) * pagesize + swapTotal := uint64(m.PgSpTotal) * pagesize + ret := SwapMemoryStat{ + Total: swapTotal, + Free: uint64(m.PgSpFree) * pagesize, + Used: swapUsed, + UsedPercent: float64(100 * swapUsed) / float64(swapTotal), + Sin: uint64(m.PgSpIn), + Sout: uint64(m.PgSpOut), + PgIn: uint64(m.PageIn), + PgOut: uint64(m.PageOut), + PgFault: uint64(m.PageFaults), + } + return &ret, nil +} diff --git a/v3/mem/mem_fallback.go b/v3/mem/mem_fallback.go index e6c8c094..6890f2b9 100644 --- a/v3/mem/mem_fallback.go +++ b/v3/mem/mem_fallback.go @@ -1,4 +1,4 @@ -// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!plan9 +// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!plan9,!aix package mem