Skip to content

Commit

Permalink
Merge pull request shirou#1302 from chbuescher/master
Browse files Browse the repository at this point in the history
get IOCounters for aix with perfstat
  • Loading branch information
shirou authored Jun 7, 2022
2 parents c4a0f46 + 7dd9668 commit e978987
Show file tree
Hide file tree
Showing 15 changed files with 475 additions and 302 deletions.
58 changes: 0 additions & 58 deletions cpu/cpu_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,12 @@ 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
}
66 changes: 66 additions & 0 deletions cpu/cpu_aix_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//go:build aix && cgo
// +build aix,cgo

package cpu

import (
"context"

"github.com/power-devops/perfstat"
)

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 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
}
37 changes: 37 additions & 0 deletions cpu/cpu_aix_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build aix && !cgo
// +build aix,!cgo

package cpu

import (
"context"
"fmt"
"strings"
"strconv"

"github.com/shirou/gopsutil/v3/internal/common"
)

func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
return []TimesStat{}, common.ErrNotImplementedError
}

func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
return []InfoStat{}, common.ErrNotImplementedError
}

func CountsWithContext(ctx context.Context, logical bool) (int, error) {
prtConfOut, err := invoke.CommandWithContext(ctx, "prtconf")
if err != nil {
return 0, fmt.Errorf("cannot execute prtconf: %s", err)
}
for _, line := range strings.Split(string(prtConfOut), "\n") {
parts := strings.Split(line, ": ")
if len(parts) > 1 && parts[0] == "Number Of Processors" {
if ncpu, err := strconv.Atoi(parts[1]); err == nil {
return ncpu, nil
}
}
}
return 0, fmt.Errorf("number of processors not found")
}
67 changes: 0 additions & 67 deletions disk/disk_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,14 @@ 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
}
Expand Down
76 changes: 76 additions & 0 deletions disk/disk_aix_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//go:build aix && cgo
// +build aix,cgo

package disk

import (
"context"
"fmt"

"github.com/power-devops/perfstat"
)

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 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)
}
18 changes: 18 additions & 0 deletions disk/disk_aix_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build aix && !cgo
// +build aix,!cgo

package disk

import (
"context"

"github.com/shirou/gopsutil/v3/internal/common"
)

func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
return []PartitionStat{}, common.ErrNotImplementedError
}

func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return nil, common.ErrNotImplementedError
}
Loading

0 comments on commit e978987

Please sign in to comment.