mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-26 13:48:59 +08:00
more aix nocgo implementations
This commit is contained in:
parent
f9d3b96126
commit
b3ab1561bc
@ -5,33 +5,91 @@ package cpu
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/internal/common"
|
"github.com/shirou/gopsutil/v3/internal/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var whiteSpaces = regexp.MustCompile(`\s+`)
|
||||||
|
|
||||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||||
|
if percpu {
|
||||||
return []TimesStat{}, common.ErrNotImplementedError
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if t, err := strconv.ParseUint(v[i], 10, 64); err == nil {
|
||||||
|
switch header {
|
||||||
|
case `%usr`:
|
||||||
|
ret.User = float64(t)
|
||||||
|
case `%sys`:
|
||||||
|
ret.System = float64(t)
|
||||||
|
case `%wio`:
|
||||||
|
ret.Iowait = float64(t)
|
||||||
|
case `%idle`:
|
||||||
|
ret.Idle = float64(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return []TimesStat{ret}, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||||
return []InfoStat{}, common.ErrNotImplementedError
|
out, err := invoke.CommandWithContext(ctx, "prtconf")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if t, err := strconv.ParseUint(p[3], 10, 64); err == nil {
|
||||||
|
switch strings.ToUpper(p[4]) {
|
||||||
|
case "MHZ":
|
||||||
|
ret.Mhz = float64(t)
|
||||||
|
case "GHZ":
|
||||||
|
ret.Mhz = float64(t) * 1000.0
|
||||||
|
case "KHZ":
|
||||||
|
ret.Mhz = float64(t) / 1000.0
|
||||||
|
default:
|
||||||
|
ret.Mhz = float64(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []InfoStat{ret}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||||
prtConfOut, err := invoke.CommandWithContext(ctx, "prtconf")
|
info, err := InfoWithContext(ctx)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
return 0, fmt.Errorf("cannot execute prtconf: %s", err)
|
return int(info[0].Cores), nil
|
||||||
}
|
}
|
||||||
for _, line := range strings.Split(string(prtConfOut), "\n") {
|
return 0, err
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,62 @@ package load
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/internal/common"
|
"github.com/shirou/gopsutil/v3/internal/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var separator = regexp.MustCompile(`,?\s+`)
|
||||||
|
|
||||||
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
|
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
|
||||||
|
line, err := invoke.CommandWithContext(ctx, "uptime")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := strings.Index(string(line), "load average:")
|
||||||
|
if idx < 0 {
|
||||||
|
return nil, common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
ret := &AvgStat{}
|
||||||
|
|
||||||
|
p := separator.Split(string(line[idx:len(line)]), 5)
|
||||||
|
if 4 < len(p) && p[0] == "load" && p[1] == "average:" {
|
||||||
|
if t, err := strconv.ParseFloat(p[2], 64); err == nil {
|
||||||
|
ret.Load1 = t
|
||||||
|
}
|
||||||
|
if t, err := strconv.ParseFloat(p[3], 64); err == nil {
|
||||||
|
ret.Load5 = t
|
||||||
|
}
|
||||||
|
if t, err := strconv.ParseFloat(p[4], 64); err == nil {
|
||||||
|
ret.Load15 = t
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
return nil, common.ErrNotImplementedError
|
return nil, common.ErrNotImplementedError
|
||||||
}
|
}
|
||||||
|
|
||||||
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
|
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
|
||||||
return nil, common.ErrNotImplementedError
|
out, err := invoke.CommandWithContext(ctx, "ps", "-Ao", "state")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &MiscStat{}
|
||||||
|
for _, line := range strings.Split(string(out), "\n") {
|
||||||
|
ret.ProcsTotal++
|
||||||
|
switch line {
|
||||||
|
case "R":
|
||||||
|
case "A":
|
||||||
|
ret.ProcsRunning++
|
||||||
|
case "T":
|
||||||
|
ret.ProcsBlocked++
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,77 @@ package mem
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/internal/common"
|
"github.com/shirou/gopsutil/v3/internal/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var whiteSpaces = regexp.MustCompile(`\s+`)
|
||||||
|
|
||||||
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
||||||
|
vmem, swap, err := callSVMon(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if vmem.Total == 0 {
|
||||||
return nil, common.ErrNotImplementedError
|
return nil, common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
vmem.SwapTotal = swap.Total
|
||||||
|
vmem.SwapFree = swap.Free
|
||||||
|
return vmem, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
||||||
|
_, swap, err := callSVMon(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if swap.Total == 0 {
|
||||||
return nil, common.ErrNotImplementedError
|
return nil, common.ErrNotImplementedError
|
||||||
|
}
|
||||||
|
return swap, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func callSVMon(ctx context.Context) (*VirtualMemoryStat, *SwapMemoryStat, error) {
|
||||||
|
out, err := invoke.CommandWithContext(ctx, "svmon", "-G")
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pagesize := uint64(4096)
|
||||||
|
vmem := &VirtualMemoryStat{}
|
||||||
|
swap := &SwapMemoryStat{}
|
||||||
|
for _, line := range strings.Split(string(out), "\n") {
|
||||||
|
if strings.HasPrefix(line, "memory") {
|
||||||
|
p := whiteSpaces.Split(line, 7)
|
||||||
|
if len(p) > 2 {
|
||||||
|
if t, err := strconv.ParseUint(p[1], 10, 64); err == nil {
|
||||||
|
vmem.Total = t * pagesize
|
||||||
|
}
|
||||||
|
if t, err := strconv.ParseUint(p[2], 10, 64); err == nil {
|
||||||
|
vmem.Used = t * pagesize
|
||||||
|
if vmem.Total > 0 {
|
||||||
|
vmem.UsedPercent = 100 * float64(vmem.Used) / float64(vmem.Total)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if t, err := strconv.ParseUint(p[3], 10, 64); err == nil {
|
||||||
|
vmem.Free = t * pagesize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if strings.HasPrefix(line, "pg space") {
|
||||||
|
p := whiteSpaces.Split(line, 4)
|
||||||
|
if len(p) > 3 {
|
||||||
|
if t, err := strconv.ParseUint(p[2], 10, 64); err == nil {
|
||||||
|
swap.Total = t * pagesize
|
||||||
|
}
|
||||||
|
if t, err := strconv.ParseUint(p[3], 10, 64); err == nil {
|
||||||
|
swap.Free = swap.Total - t * pagesize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vmem, swap, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user