1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-29 13:49:21 +08:00
shirou_gopsutil/cpu/cpu_aix_nocgo.go

96 lines
2.1 KiB
Go
Raw Normal View History

//go:build aix && !cgo
// +build aix,!cgo
package cpu
import (
"context"
2022-06-17 14:53:16 +02:00
"regexp"
"strings"
"strconv"
2022-06-03 17:43:52 +02:00
"github.com/shirou/gopsutil/v3/internal/common"
)
2022-06-17 14:53:16 +02:00
var whiteSpaces = regexp.MustCompile(`\s+`)
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
2022-06-17 14:53:16 +02:00
if percpu {
return []TimesStat{}, common.ErrNotImplementedError
} else {
out, err := invoke.CommandWithContext(ctx, "sar", "-u", "10", "1")
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
if len(lines) < 5 {
return []TimesStat{}, common.ErrNotImplementedError
}
2022-06-17 14:53:16 +02:00
ret := TimesStat{CPU: "cpu-total"}
h := whiteSpaces.Split(lines[len(lines)-3], -1) // headers
v := whiteSpaces.Split(lines[len(lines)-2], -1) // values
for i, header := range h {
2022-06-17 15:10:37 +02:00
if t, err := strconv.ParseFloat(v[i], 64); err == nil {
2022-06-17 14:53:16 +02:00
switch header {
case `%usr`:
2022-06-17 15:10:37 +02:00
ret.User = t
2022-06-17 14:53:16 +02:00
case `%sys`:
2022-06-17 15:10:37 +02:00
ret.System = t
2022-06-17 14:53:16 +02:00
case `%wio`:
2022-06-17 15:10:37 +02:00
ret.Iowait = t
2022-06-17 14:53:16 +02:00
case `%idle`:
2022-06-17 15:10:37 +02:00
ret.Idle = t
2022-06-17 14:53:16 +02:00
}
}
}
return []TimesStat{ret}, nil
}
}
2022-06-17 14:53:16 +02:00
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
out, err := invoke.CommandWithContext(ctx, "prtconf")
if err != nil {
2022-06-17 14:53:16 +02:00
return nil, err
}
2022-06-17 14:53:16 +02:00
ret := InfoStat{}
for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, "Number Of Processors:") {
p := whiteSpaces.Split(line, 4)
if len(p) > 3 {
if t, err := strconv.ParseUint(p[3], 10, 64); err == nil {
ret.Cores = int32(t)
}
}
} else if strings.HasPrefix(line, "Processor Clock Speed:") {
p := whiteSpaces.Split(line, 5)
if len(p) > 4 {
2022-06-17 15:10:37 +02:00
if t, err := strconv.ParseFloat(p[3], 64); err == nil {
2022-06-17 14:53:16 +02:00
switch strings.ToUpper(p[4]) {
case "MHZ":
2022-06-17 15:10:37 +02:00
ret.Mhz = t
2022-06-17 14:53:16 +02:00
case "GHZ":
2022-06-17 15:10:37 +02:00
ret.Mhz = t * 1000.0
2022-06-17 14:53:16 +02:00
case "KHZ":
2022-06-17 15:10:37 +02:00
ret.Mhz = t / 1000.0
2022-06-17 14:53:16 +02:00
default:
2022-06-17 15:10:37 +02:00
ret.Mhz = t
2022-06-17 14:53:16 +02:00
}
}
}
2022-06-17 14:53:16 +02:00
break
}
}
2022-06-17 14:53:16 +02:00
return []InfoStat{ret}, nil
}
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
info, err := InfoWithContext(ctx)
if err == nil {
return int(info[0].Cores), nil
}
return 0, err
}