Skip to content

Commit 78065a7

Browse files
authored
Merge pull request #1177 from chbuescher/master
add initial aix support
2 parents 0ce33db + d8728fc commit 78065a7

File tree

8 files changed

+293
-4
lines changed

8 files changed

+293
-4
lines changed

v3/cpu/cpu_aix.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// +build aix
2+
3+
package cpu
4+
5+
import (
6+
"context"
7+
8+
"github.com/power-devops/perfstat"
9+
)
10+
11+
func Times(percpu bool) ([]TimesStat, error) {
12+
return TimesWithContext(context.Background(), percpu)
13+
}
14+
15+
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
16+
var ret []TimesStat
17+
if percpu {
18+
cpus, err := perfstat.CpuStat()
19+
if err != nil {
20+
return nil, err
21+
}
22+
for _, c := range cpus {
23+
ct := &TimesStat{
24+
CPU: c.Name,
25+
Idle: float64(c.Idle),
26+
User: float64(c.User),
27+
System: float64(c.Sys),
28+
Iowait: float64(c.Wait),
29+
}
30+
ret = append(ret, *ct)
31+
}
32+
} else {
33+
c, err := perfstat.CpuUtilTotalStat()
34+
if err != nil {
35+
return nil, err
36+
}
37+
ct := &TimesStat{
38+
CPU: "cpu-total",
39+
Idle: float64(c.IdlePct),
40+
User: float64(c.UserPct),
41+
System: float64(c.KernPct),
42+
Iowait: float64(c.WaitPct),
43+
}
44+
ret = append(ret, *ct)
45+
}
46+
return ret, nil
47+
}
48+
49+
func Info() ([]InfoStat, error) {
50+
return InfoWithContext(context.Background())
51+
}
52+
53+
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
54+
c, err := perfstat.CpuTotalStat()
55+
if err != nil {
56+
return nil, err
57+
}
58+
info := InfoStat{
59+
CPU: 0,
60+
Mhz: float64(c.ProcessorHz / 1000000),
61+
Cores: int32(c.NCpusCfg),
62+
}
63+
result := []InfoStat{info};
64+
return result, nil
65+
}
66+
67+
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
68+
c, err := perfstat.CpuTotalStat()
69+
if err != nil {
70+
return 0, err
71+
}
72+
return c.NCpusCfg, nil
73+
}
74+

v3/cpu/cpu_fallback.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9
1+
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9,!aix
22

33
package cpu
44

v3/disk/disk_aix.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// +build aix
2+
3+
package disk
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/power-devops/perfstat"
10+
"github.com/shirou/gopsutil/v3/internal/common"
11+
)
12+
13+
var FSType map[int]string
14+
15+
func init() {
16+
FSType = map[int]string{0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc",
17+
16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs",
18+
33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs",
19+
39: "ahafs", 40: "sterm-nfs", 41: "asmfs" }
20+
}
21+
22+
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
23+
f, err := perfstat.FileSystemStat()
24+
if err != nil {
25+
return nil, err
26+
}
27+
ret := make([]PartitionStat, len(f))
28+
29+
for _, fs := range f {
30+
fstyp, exists := FSType[fs.FSType]
31+
if ! exists {
32+
fstyp = "unknown"
33+
}
34+
info := PartitionStat{
35+
Device: fs.Device,
36+
Mountpoint: fs.MountPoint,
37+
Fstype: fstyp,
38+
}
39+
ret = append(ret,info)
40+
}
41+
42+
return ret, err
43+
}
44+
45+
func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
46+
return nil, common.ErrNotImplementedError
47+
}
48+
49+
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
50+
f, err := perfstat.FileSystemStat()
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
blocksize := uint64(512)
56+
for _, fs := range f {
57+
if path == fs.MountPoint {
58+
fstyp, exists := FSType[fs.FSType]
59+
if ! exists {
60+
fstyp = "unknown"
61+
}
62+
info := UsageStat{
63+
Path: path,
64+
Fstype: fstyp,
65+
Total: uint64(fs.TotalBlocks) * blocksize,
66+
Free: uint64(fs.FreeBlocks) * blocksize,
67+
Used: uint64(fs.TotalBlocks - fs.FreeBlocks) * blocksize,
68+
InodesTotal: uint64(fs.TotalInodes),
69+
InodesFree: uint64(fs.FreeInodes),
70+
InodesUsed: uint64(fs.TotalInodes - fs.FreeInodes),
71+
}
72+
info.UsedPercent = (float64(info.Used) / float64(info.Total)) * 100.0
73+
info.InodesUsedPercent = (float64(info.InodesUsed) / float64(info.InodesTotal)) * 100.0
74+
return &info, nil
75+
}
76+
}
77+
return nil, fmt.Errorf("mountpoint %s not found", path)
78+
}
79+
80+
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
81+
return "", common.ErrNotImplementedError
82+
}
83+
84+
func LabelWithContext(ctx context.Context, name string) (string, error) {
85+
return "", common.ErrNotImplementedError
86+
}

v3/disk/disk_fallback.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris
1+
// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!aix
22

33
package disk
44

v3/load/load_aix.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// +build aix
2+
3+
package load
4+
5+
/*
6+
#cgo LDFLAGS: -L/usr/lib -lperfstat
7+
8+
#include <libperfstat.h>
9+
#include <procinfo.h>
10+
*/
11+
import "C"
12+
13+
import (
14+
"context"
15+
"unsafe"
16+
17+
"github.com/power-devops/perfstat"
18+
)
19+
20+
func Avg() (*AvgStat, error) {
21+
return AvgWithContext(context.Background())
22+
}
23+
24+
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
25+
c, err := perfstat.CpuTotalStat()
26+
if err != nil {
27+
return nil, err
28+
}
29+
ret := &AvgStat{
30+
Load1: float64(c.LoadAvg1),
31+
Load5: float64(c.LoadAvg5),
32+
Load15: float64(c.LoadAvg15),
33+
}
34+
35+
return ret, nil
36+
}
37+
38+
// Misc returns miscellaneous host-wide statistics.
39+
// darwin use ps command to get process running/blocked count.
40+
// Almost same as Darwin implementation, but state is different.
41+
func Misc() (*MiscStat, error) {
42+
return MiscWithContext(context.Background())
43+
}
44+
45+
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
46+
info := C.struct_procentry64{}
47+
cpid := C.pid_t(0)
48+
49+
ret := MiscStat{}
50+
for {
51+
// getprocs first argument is a void*
52+
num, err := C.getprocs64(unsafe.Pointer(&info), C.sizeof_struct_procentry64, nil, 0, &cpid, 1)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
ret.ProcsTotal++
58+
switch info.pi_state {
59+
case C.SACTIVE:
60+
ret.ProcsRunning++
61+
case C.SSTOP:
62+
ret.ProcsBlocked++
63+
}
64+
65+
if num == 0 {
66+
break
67+
}
68+
}
69+
return &ret, nil
70+
}
71+

v3/load/load_fallback.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris
1+
// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!aix
22

33
package load
44

v3/mem/mem_aix.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// +build aix
2+
3+
package mem
4+
5+
import (
6+
"context"
7+
8+
"github.com/power-devops/perfstat"
9+
)
10+
11+
func VirtualMemory() (*VirtualMemoryStat, error) {
12+
return VirtualMemoryWithContext(context.Background())
13+
}
14+
15+
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
16+
m, err := perfstat.MemoryTotalStat()
17+
if err != nil {
18+
return nil, err
19+
}
20+
pagesize := uint64(4096)
21+
ret := VirtualMemoryStat{
22+
Total: uint64(m.RealTotal) * pagesize,
23+
Available: uint64(m.RealAvailable) * pagesize,
24+
Free: uint64(m.RealFree) * pagesize,
25+
Used: uint64(m.RealInUse) * pagesize,
26+
UsedPercent: 100 * float64(m.RealInUse) / float64(m.RealTotal),
27+
Active: uint64(m.VirtualActive) * pagesize,
28+
SwapTotal: uint64(m.PgSpTotal) * pagesize,
29+
SwapFree: uint64(m.PgSpFree) * pagesize,
30+
}
31+
return &ret, nil
32+
}
33+
34+
func SwapMemory() (*SwapMemoryStat, error) {
35+
return SwapMemoryWithContext(context.Background())
36+
}
37+
38+
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
39+
m, err := perfstat.MemoryTotalStat()
40+
if err != nil {
41+
return nil, err
42+
}
43+
pagesize := uint64(4096)
44+
swapUsed := uint64(m.PgSpTotal - m.PgSpFree - m.PgSpRsvd) * pagesize
45+
swapTotal := uint64(m.PgSpTotal) * pagesize
46+
ret := SwapMemoryStat{
47+
Total: swapTotal,
48+
Free: uint64(m.PgSpFree) * pagesize,
49+
Used: swapUsed,
50+
UsedPercent: float64(100 * swapUsed) / float64(swapTotal),
51+
Sin: uint64(m.PgSpIn),
52+
Sout: uint64(m.PgSpOut),
53+
PgIn: uint64(m.PageIn),
54+
PgOut: uint64(m.PageOut),
55+
PgFault: uint64(m.PageFaults),
56+
}
57+
return &ret, nil
58+
}

v3/mem/mem_fallback.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!plan9
1+
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!plan9,!aix
22

33
package mem
44

0 commit comments

Comments
 (0)