Change to using gopsutil for cross-OS process ops
Instead of trying to write out own code to do basic process operations (e.g. checking if a PID is still running in a multi-OS friendly manner), use shirou/gopsutil, a multi-platform library that should abstract all the complexity away. Unlike our previous approach on Windows, this one should actually work. Signed-off-by: Matt Heon <mheon@redhat.com>
This commit is contained in:
parent
642fa98976
commit
d9c388e2fe
|
|
@ -8,11 +8,12 @@ import (
|
|||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/containers/common/pkg/completion"
|
||||
"github.com/containers/podman/v4/cmd/podman/registry"
|
||||
"github.com/containers/podman/v4/pkg/fileserver"
|
||||
"github.com/containers/podman/v4/pkg/util"
|
||||
psutil "github.com/shirou/gopsutil/v3/process"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
|
@ -78,9 +79,20 @@ func remoteDirServer(cmd *cobra.Command, args []string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Wait for the given PID to exit
|
||||
if err := util.WaitForPIDExit(uint(pid)); err != nil {
|
||||
return err
|
||||
p, err := psutil.NewProcess(int32(pid))
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening gvproxy PID: %w", err)
|
||||
}
|
||||
for {
|
||||
running, err := p.IsRunning()
|
||||
if err != nil {
|
||||
return fmt.Errorf("checking is gvproxy is dead: %w", err)
|
||||
}
|
||||
if !running {
|
||||
break
|
||||
}
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
||||
logrus.Infof("Exiting cleanly as PID %d has died", pid)
|
||||
|
|
|
|||
7
go.mod
7
go.mod
|
|
@ -57,6 +57,7 @@ require (
|
|||
github.com/opencontainers/selinux v1.11.0
|
||||
github.com/openshift/imagebuilder v1.2.5
|
||||
github.com/rootless-containers/rootlesskit v1.1.1
|
||||
github.com/shirou/gopsutil/v3 v3.23.9
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/spf13/cobra v1.7.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
|
|
@ -148,6 +149,7 @@ require (
|
|||
github.com/kr/fs v0.1.0 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/letsencrypt/boulder v0.0.0-20230213213521-fdfea0d469b6 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/manifoldco/promptui v0.9.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
|
|
@ -169,11 +171,13 @@ require (
|
|||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pkg/sftp v1.13.6 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/proglottis/gpgme v0.1.3 // indirect
|
||||
github.com/rivo/uniseg v0.4.4 // indirect
|
||||
github.com/seccomp/libseccomp-golang v0.10.0 // indirect
|
||||
github.com/secure-systems-lab/go-securesystemslib v0.7.0 // indirect
|
||||
github.com/segmentio/ksuid v1.0.4 // indirect
|
||||
github.com/shoenig/go-m1cpu v0.1.6 // indirect
|
||||
github.com/sigstore/fulcio v1.4.0 // indirect
|
||||
github.com/sigstore/rekor v1.2.2 // indirect
|
||||
github.com/sigstore/sigstore v1.7.3 // indirect
|
||||
|
|
@ -183,11 +187,14 @@ require (
|
|||
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
|
||||
github.com/theupdateframework/go-tuf v0.5.2 // indirect
|
||||
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.12 // indirect
|
||||
github.com/tklauser/numcpus v0.6.1 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/u-root/uio v0.0.0-20230305220412-3e8cd9d6bf63 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
github.com/vbatts/tar-split v0.11.5 // indirect
|
||||
github.com/vishvananda/netns v0.0.4 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.3 // indirect
|
||||
go.mongodb.org/mongo-driver v1.11.3 // indirect
|
||||
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
|
||||
go.opencensus.io v0.24.0 // indirect
|
||||
|
|
|
|||
17
go.sum
17
go.sum
|
|
@ -723,6 +723,8 @@ github.com/letsencrypt/boulder v0.0.0-20230213213521-fdfea0d469b6/go.mod h1:PUgW
|
|||
github.com/linuxkit/virtsock v0.0.0-20201010232012-f8cee7dfc7a3/go.mod h1:3r6x7q95whyfWQpmGZTu3gk3v2YkMi05HEzl7Tf7YEo=
|
||||
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2 h1:DZMFueDbfz6PNc1GwDRA8+6lBx1TB9UnxDQliCqR73Y=
|
||||
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2/go.mod h1:SWzULI85WerrFt3u+nIm5F9l7EvxZTKQvd0InF3nmgM=
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
|
|
@ -887,6 +889,8 @@ github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
|
|||
github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
|
||||
github.com/proglottis/gpgme v0.1.3 h1:Crxx0oz4LKB3QXc5Ea0J19K/3ICfy3ftr5exgUK1AU0=
|
||||
github.com/proglottis/gpgme v0.1.3/go.mod h1:fPbW/EZ0LvwQtH8Hy7eixhp1eF3G39dtx7GUN+0Gmy0=
|
||||
|
|
@ -948,6 +952,12 @@ github.com/secure-systems-lab/go-securesystemslib v0.7.0/go.mod h1:/2gYnlnHVQ6xe
|
|||
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
|
||||
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
|
||||
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
|
||||
github.com/shirou/gopsutil/v3 v3.23.9 h1:ZI5bWVeu2ep4/DIxB4U9okeYJ7zp/QLTO4auRb/ty/E=
|
||||
github.com/shirou/gopsutil/v3 v3.23.9/go.mod h1:x/NWSb71eMcjFIO0vhyGW5nZ7oSIgVjrCnADckb85GA=
|
||||
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
|
||||
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
|
||||
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
|
||||
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/sigstore/fulcio v1.4.0 h1:05+k8BFvwTQzfCkVxESWzCN4b70KIRliGYz0Upmdrs8=
|
||||
github.com/sigstore/fulcio v1.4.0/go.mod h1:wcjlktbhoy6+ZTxO3yXpvqUxsLV+JEH4FF3a5Jz4VPI=
|
||||
|
|
@ -1028,6 +1038,10 @@ github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhV
|
|||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 h1:e/5i7d4oYZ+C1wj2THlRK+oAhjeS/TRQwMfkIuet3w0=
|
||||
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399/go.mod h1:LdwHTNJT99C5fTAzDz0ud328OgXz+gierycbcIx2fRs=
|
||||
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
|
||||
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
|
||||
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
|
||||
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
|
|
@ -1090,6 +1104,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
|
|||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
|
||||
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
|
||||
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
|
||||
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
|
||||
|
|
@ -1328,6 +1344,7 @@ golang.org/x/sys v0.0.0-20201117170446-d9b008d0a637/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201202213521-69691e467435/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
|
|
|||
|
|
@ -1,38 +1,41 @@
|
|||
package machine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
psutil "github.com/shirou/gopsutil/v3/process"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
loops = 10
|
||||
loops = 8
|
||||
sleepTime = time.Millisecond * 1
|
||||
)
|
||||
|
||||
// backoffForProcess checks if the process still exists, for something like
|
||||
// sigterm. If the process still exists after loops and sleep time are exhausted,
|
||||
// an error is returned
|
||||
func backoffForProcess(pid int) error {
|
||||
func backoffForProcess(p *psutil.Process) error {
|
||||
sleepInterval := sleepTime
|
||||
for i := 0; i < loops; i++ {
|
||||
logrus.Debugf("Sleeping for %d milliseconds", sleepInterval.Milliseconds())
|
||||
proxyProc, err := findProcess(pid)
|
||||
if proxyProc == nil && err != nil {
|
||||
logrus.Debugf("Error opening process %d: %v", pid, err)
|
||||
// process is killed, gone
|
||||
return nil //nolint: nilerr
|
||||
running, err := p.IsRunning()
|
||||
if err != nil {
|
||||
return fmt.Errorf("checking if process running: %w", err)
|
||||
}
|
||||
if !running {
|
||||
return nil
|
||||
}
|
||||
|
||||
time.Sleep(sleepInterval)
|
||||
// double the time
|
||||
sleepInterval += sleepInterval
|
||||
}
|
||||
return fmt.Errorf("process %d has not ended", pid)
|
||||
return fmt.Errorf("process %d has not ended", p.Pid)
|
||||
}
|
||||
|
||||
// waitOnProcess takes a pid and sends a sigterm to it. it then waits for the
|
||||
|
|
@ -41,40 +44,41 @@ func backoffForProcess(pid int) error {
|
|||
func waitOnProcess(processID int) error {
|
||||
logrus.Infof("Going to stop gvproxy (PID %d)", processID)
|
||||
|
||||
proxyProc, err := findProcess(processID)
|
||||
p, err := psutil.NewProcess(int32(processID))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("looking up PID %d: %w", processID, err)
|
||||
}
|
||||
|
||||
// Try to kill the pid with sigterm
|
||||
if runtime.GOOS != "windows" { // FIXME: temporary work around because signals are lame in windows
|
||||
if err := proxyProc.Signal(syscall.SIGTERM); err != nil {
|
||||
if err == syscall.ESRCH {
|
||||
if err := p.SendSignal(syscall.SIGTERM); err != nil {
|
||||
if errors.Is(err, syscall.ESRCH) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
return fmt.Errorf("sending SIGTERM to grproxy: %w", err)
|
||||
}
|
||||
|
||||
if err := backoffForProcess(processID); err == nil {
|
||||
if err := backoffForProcess(p); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// sigterm has not killed it yet, lets send a sigkill
|
||||
proxyProc, err = findProcess(processID)
|
||||
if proxyProc == nil && err != nil {
|
||||
// process is killed, gone
|
||||
logrus.Debugf("Error opening gvproxy process: %v", err)
|
||||
return nil //nolint: nilerr
|
||||
running, err := p.IsRunning()
|
||||
if err != nil {
|
||||
return fmt.Errorf("checking if gvproxy is running: %w", err)
|
||||
}
|
||||
if err := proxyProc.Signal(syscall.SIGKILL); err != nil {
|
||||
if err == syscall.ESRCH {
|
||||
if !running {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.Kill(); err != nil {
|
||||
if errors.Is(err, syscall.ESRCH) {
|
||||
logrus.Debugf("Gvproxy already dead, exiting cleanly")
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return backoffForProcess(processID)
|
||||
return backoffForProcess(p)
|
||||
}
|
||||
|
||||
// CleanupGVProxy reads the --pid-file for gvproxy attempts to stop it
|
||||
|
|
|
|||
|
|
@ -1,24 +0,0 @@
|
|||
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
|
||||
// +build darwin dragonfly freebsd linux netbsd openbsd
|
||||
|
||||
package machine
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func findProcess(pid int) (*os.Process, error) {
|
||||
p, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// On unix, findprocess will always return a process even
|
||||
// if the process is not found. you must send a 0 signal
|
||||
// to the process to see if it is alive.
|
||||
// https://pkg.go.dev/os#FindProcess
|
||||
if err := p.Signal(syscall.Signal(0)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package machine
|
||||
|
||||
import "os"
|
||||
|
||||
func findProcess(pid int) (*os.Process, error) {
|
||||
return os.FindProcess(pid)
|
||||
}
|
||||
|
|
@ -14,7 +14,6 @@ import (
|
|||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/containers/podman/v4/libpod/define"
|
||||
"github.com/containers/podman/v4/pkg/rootless"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
|
@ -119,9 +118,3 @@ func GetRootlessPauseProcessPidPath() (string, error) {
|
|||
// the tmpdir which can be changed via --tmpdir.
|
||||
return filepath.Join(runtimeDir, "libpod", "tmp", "pause.pid"), nil
|
||||
}
|
||||
|
||||
// WaitForPIDExit waits for a PID to exit.
|
||||
// Not implemented for Linux at this time, only for Windows.
|
||||
func WaitForPIDExit(pid uint) error {
|
||||
return define.ErrNotImplemented
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"github.com/containers/storage/pkg/homedir"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
var errNotImplemented = errors.New("not yet implemented")
|
||||
|
|
@ -45,35 +44,3 @@ func GetRuntimeDir() (string, error) {
|
|||
func GetRootlessConfigHomeDir() (string, error) {
|
||||
return "", errors.New("this function is not implemented for windows")
|
||||
}
|
||||
|
||||
// Wait until the given PID exits. Returns nil if wait was successful, errors on
|
||||
// unexpected condition (IE, pid was not valid)
|
||||
func WaitForPIDExit(pid uint) error {
|
||||
const PROCESS_ALL_ACCESS = 0x1F0FFF
|
||||
|
||||
// We need to turn the PID into a Windows handle.
|
||||
// To do this we need Windows' OpenProcess func.
|
||||
// To get that, we need to open the kernel32 DLL.
|
||||
kernel32, err := windows.LoadDLL("kernel32.dll")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading kernel32 dll: %w", err)
|
||||
}
|
||||
|
||||
openProc, err := kernel32.FindProc("OpenProcess")
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading OpenProcess API: %w", err)
|
||||
}
|
||||
|
||||
handle, _, err := openProc.Call(uintptr(PROCESS_ALL_ACCESS), uintptr(1), uintptr(pid))
|
||||
if err != nil {
|
||||
return fmt.Errorf("converting PID to handle: %w", err)
|
||||
}
|
||||
|
||||
// We can now wait for the handle.
|
||||
_, err = windows.WaitForSingleObject(windows.Handle(handle), 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("waiting for handle: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,100 @@
|
|||
// +build windows
|
||||
|
||||
package oleutil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
|
||||
ole "github.com/go-ole/go-ole"
|
||||
)
|
||||
|
||||
type stdDispatch struct {
|
||||
lpVtbl *stdDispatchVtbl
|
||||
ref int32
|
||||
iid *ole.GUID
|
||||
iface interface{}
|
||||
funcMap map[string]int32
|
||||
}
|
||||
|
||||
type stdDispatchVtbl struct {
|
||||
pQueryInterface uintptr
|
||||
pAddRef uintptr
|
||||
pRelease uintptr
|
||||
pGetTypeInfoCount uintptr
|
||||
pGetTypeInfo uintptr
|
||||
pGetIDsOfNames uintptr
|
||||
pInvoke uintptr
|
||||
}
|
||||
|
||||
func dispQueryInterface(this *ole.IUnknown, iid *ole.GUID, punk **ole.IUnknown) uint32 {
|
||||
pthis := (*stdDispatch)(unsafe.Pointer(this))
|
||||
*punk = nil
|
||||
if ole.IsEqualGUID(iid, ole.IID_IUnknown) ||
|
||||
ole.IsEqualGUID(iid, ole.IID_IDispatch) {
|
||||
dispAddRef(this)
|
||||
*punk = this
|
||||
return ole.S_OK
|
||||
}
|
||||
if ole.IsEqualGUID(iid, pthis.iid) {
|
||||
dispAddRef(this)
|
||||
*punk = this
|
||||
return ole.S_OK
|
||||
}
|
||||
return ole.E_NOINTERFACE
|
||||
}
|
||||
|
||||
func dispAddRef(this *ole.IUnknown) int32 {
|
||||
pthis := (*stdDispatch)(unsafe.Pointer(this))
|
||||
pthis.ref++
|
||||
return pthis.ref
|
||||
}
|
||||
|
||||
func dispRelease(this *ole.IUnknown) int32 {
|
||||
pthis := (*stdDispatch)(unsafe.Pointer(this))
|
||||
pthis.ref--
|
||||
return pthis.ref
|
||||
}
|
||||
|
||||
func dispGetIDsOfNames(this *ole.IUnknown, iid *ole.GUID, wnames []*uint16, namelen int, lcid int, pdisp []int32) uintptr {
|
||||
pthis := (*stdDispatch)(unsafe.Pointer(this))
|
||||
names := make([]string, len(wnames))
|
||||
for i := 0; i < len(names); i++ {
|
||||
names[i] = ole.LpOleStrToString(wnames[i])
|
||||
}
|
||||
for n := 0; n < namelen; n++ {
|
||||
if id, ok := pthis.funcMap[names[n]]; ok {
|
||||
pdisp[n] = id
|
||||
}
|
||||
}
|
||||
return ole.S_OK
|
||||
}
|
||||
|
||||
func dispGetTypeInfoCount(pcount *int) uintptr {
|
||||
if pcount != nil {
|
||||
*pcount = 0
|
||||
}
|
||||
return ole.S_OK
|
||||
}
|
||||
|
||||
func dispGetTypeInfo(ptypeif *uintptr) uintptr {
|
||||
return ole.E_NOTIMPL
|
||||
}
|
||||
|
||||
func dispInvoke(this *ole.IDispatch, dispid int32, riid *ole.GUID, lcid int, flags int16, dispparams *ole.DISPPARAMS, result *ole.VARIANT, pexcepinfo *ole.EXCEPINFO, nerr *uint) uintptr {
|
||||
pthis := (*stdDispatch)(unsafe.Pointer(this))
|
||||
found := ""
|
||||
for name, id := range pthis.funcMap {
|
||||
if id == dispid {
|
||||
found = name
|
||||
}
|
||||
}
|
||||
if found != "" {
|
||||
rv := reflect.ValueOf(pthis.iface).Elem()
|
||||
rm := rv.MethodByName(found)
|
||||
rr := rm.Call([]reflect.Value{})
|
||||
println(len(rr))
|
||||
return ole.S_OK
|
||||
}
|
||||
return ole.E_NOTIMPL
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
// +build !windows
|
||||
|
||||
package oleutil
|
||||
|
||||
import ole "github.com/go-ole/go-ole"
|
||||
|
||||
// ConnectObject creates a connection point between two services for communication.
|
||||
func ConnectObject(disp *ole.IDispatch, iid *ole.GUID, idisp interface{}) (uint32, error) {
|
||||
return 0, ole.NewError(ole.E_NOTIMPL)
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
// +build windows
|
||||
|
||||
package oleutil
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
ole "github.com/go-ole/go-ole"
|
||||
)
|
||||
|
||||
// ConnectObject creates a connection point between two services for communication.
|
||||
func ConnectObject(disp *ole.IDispatch, iid *ole.GUID, idisp interface{}) (cookie uint32, err error) {
|
||||
unknown, err := disp.QueryInterface(ole.IID_IConnectionPointContainer)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
container := (*ole.IConnectionPointContainer)(unsafe.Pointer(unknown))
|
||||
var point *ole.IConnectionPoint
|
||||
err = container.FindConnectionPoint(iid, &point)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if edisp, ok := idisp.(*ole.IUnknown); ok {
|
||||
cookie, err = point.Advise(edisp)
|
||||
container.Release()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
rv := reflect.ValueOf(disp).Elem()
|
||||
if rv.Type().Kind() == reflect.Struct {
|
||||
dest := &stdDispatch{}
|
||||
dest.lpVtbl = &stdDispatchVtbl{}
|
||||
dest.lpVtbl.pQueryInterface = syscall.NewCallback(dispQueryInterface)
|
||||
dest.lpVtbl.pAddRef = syscall.NewCallback(dispAddRef)
|
||||
dest.lpVtbl.pRelease = syscall.NewCallback(dispRelease)
|
||||
dest.lpVtbl.pGetTypeInfoCount = syscall.NewCallback(dispGetTypeInfoCount)
|
||||
dest.lpVtbl.pGetTypeInfo = syscall.NewCallback(dispGetTypeInfo)
|
||||
dest.lpVtbl.pGetIDsOfNames = syscall.NewCallback(dispGetIDsOfNames)
|
||||
dest.lpVtbl.pInvoke = syscall.NewCallback(dispInvoke)
|
||||
dest.iface = disp
|
||||
dest.iid = iid
|
||||
cookie, err = point.Advise((*ole.IUnknown)(unsafe.Pointer(dest)))
|
||||
container.Release()
|
||||
if err != nil {
|
||||
point.Release()
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
container.Release()
|
||||
|
||||
return 0, ole.NewError(ole.E_INVALIDARG)
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
// This file is here so go get succeeds as without it errors with:
|
||||
// no buildable Go source files in ...
|
||||
//
|
||||
// +build !windows
|
||||
|
||||
package oleutil
|
||||
|
|
@ -0,0 +1,127 @@
|
|||
package oleutil
|
||||
|
||||
import ole "github.com/go-ole/go-ole"
|
||||
|
||||
// ClassIDFrom retrieves class ID whether given is program ID or application string.
|
||||
func ClassIDFrom(programID string) (classID *ole.GUID, err error) {
|
||||
return ole.ClassIDFrom(programID)
|
||||
}
|
||||
|
||||
// CreateObject creates object from programID based on interface type.
|
||||
//
|
||||
// Only supports IUnknown.
|
||||
//
|
||||
// Program ID can be either program ID or application string.
|
||||
func CreateObject(programID string) (unknown *ole.IUnknown, err error) {
|
||||
classID, err := ole.ClassIDFrom(programID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
unknown, err = ole.CreateInstance(classID, ole.IID_IUnknown)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetActiveObject retrieves active object for program ID and interface ID based
|
||||
// on interface type.
|
||||
//
|
||||
// Only supports IUnknown.
|
||||
//
|
||||
// Program ID can be either program ID or application string.
|
||||
func GetActiveObject(programID string) (unknown *ole.IUnknown, err error) {
|
||||
classID, err := ole.ClassIDFrom(programID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
unknown, err = ole.GetActiveObject(classID, ole.IID_IUnknown)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CallMethod calls method on IDispatch with parameters.
|
||||
func CallMethod(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT, err error) {
|
||||
return disp.InvokeWithOptionalArgs(name, ole.DISPATCH_METHOD, params)
|
||||
}
|
||||
|
||||
// MustCallMethod calls method on IDispatch with parameters or panics.
|
||||
func MustCallMethod(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT) {
|
||||
r, err := CallMethod(disp, name, params...)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// GetProperty retrieves property from IDispatch.
|
||||
func GetProperty(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT, err error) {
|
||||
return disp.InvokeWithOptionalArgs(name, ole.DISPATCH_PROPERTYGET, params)
|
||||
}
|
||||
|
||||
// MustGetProperty retrieves property from IDispatch or panics.
|
||||
func MustGetProperty(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT) {
|
||||
r, err := GetProperty(disp, name, params...)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// PutProperty mutates property.
|
||||
func PutProperty(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT, err error) {
|
||||
return disp.InvokeWithOptionalArgs(name, ole.DISPATCH_PROPERTYPUT, params)
|
||||
}
|
||||
|
||||
// MustPutProperty mutates property or panics.
|
||||
func MustPutProperty(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT) {
|
||||
r, err := PutProperty(disp, name, params...)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// PutPropertyRef mutates property reference.
|
||||
func PutPropertyRef(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT, err error) {
|
||||
return disp.InvokeWithOptionalArgs(name, ole.DISPATCH_PROPERTYPUTREF, params)
|
||||
}
|
||||
|
||||
// MustPutPropertyRef mutates property reference or panics.
|
||||
func MustPutPropertyRef(disp *ole.IDispatch, name string, params ...interface{}) (result *ole.VARIANT) {
|
||||
r, err := PutPropertyRef(disp, name, params...)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func ForEach(disp *ole.IDispatch, f func(v *ole.VARIANT) error) error {
|
||||
newEnum, err := disp.GetProperty("_NewEnum")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer newEnum.Clear()
|
||||
|
||||
enum, err := newEnum.ToIUnknown().IEnumVARIANT(ole.IID_IEnumVariant)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer enum.Release()
|
||||
|
||||
for item, length, err := enum.Next(1); length > 0; item, length, err = enum.Next(1) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ferr := f(&item); ferr != nil {
|
||||
return ferr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, build with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
BSD 3-Clause License
|
||||
|
||||
Copyright (c) 2019, KADOTA, Kyohei
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
# plan9stats
|
||||
A module for retrieving statistics of Plan 9
|
||||
|
|
@ -0,0 +1,288 @@
|
|||
package stats
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CPUType represents /dev/cputype.
|
||||
type CPUType struct {
|
||||
Name string
|
||||
Clock int // clock rate in MHz
|
||||
}
|
||||
|
||||
func ReadCPUType(ctx context.Context, opts ...Option) (*CPUType, error) {
|
||||
cfg := newConfig(opts...)
|
||||
var c CPUType
|
||||
if err := readCPUType(cfg.rootdir, &c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
type SysStats struct {
|
||||
ID int
|
||||
NumCtxSwitch int64
|
||||
NumInterrupt int64
|
||||
NumSyscall int64
|
||||
NumFault int64
|
||||
NumTLBFault int64
|
||||
NumTLBPurge int64
|
||||
LoadAvg int64 // in units of milli-CPUs and is decayed over time
|
||||
Idle int // percentage
|
||||
Interrupt int // percentage
|
||||
}
|
||||
|
||||
// ReadSysStats reads system statistics from /dev/sysstat.
|
||||
func ReadSysStats(ctx context.Context, opts ...Option) ([]*SysStats, error) {
|
||||
cfg := newConfig(opts...)
|
||||
file := filepath.Join(cfg.rootdir, "/dev/sysstat")
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
var stats []*SysStats
|
||||
for scanner.Scan() {
|
||||
a := strings.Fields(scanner.Text())
|
||||
if len(a) != 10 {
|
||||
continue
|
||||
}
|
||||
var (
|
||||
p intParser
|
||||
stat SysStats
|
||||
)
|
||||
stat.ID = p.ParseInt(a[0], 10)
|
||||
stat.NumCtxSwitch = p.ParseInt64(a[1], 10)
|
||||
stat.NumInterrupt = p.ParseInt64(a[2], 10)
|
||||
stat.NumSyscall = p.ParseInt64(a[3], 10)
|
||||
stat.NumFault = p.ParseInt64(a[4], 10)
|
||||
stat.NumTLBFault = p.ParseInt64(a[5], 10)
|
||||
stat.NumTLBPurge = p.ParseInt64(a[6], 10)
|
||||
stat.LoadAvg = p.ParseInt64(a[7], 10)
|
||||
stat.Idle = p.ParseInt(a[8], 10)
|
||||
stat.Interrupt = p.ParseInt(a[9], 10)
|
||||
if err := p.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats = append(stats, &stat)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func readCPUType(rootdir string, c *CPUType) error {
|
||||
file := filepath.Join(rootdir, "/dev/cputype")
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b = bytes.TrimSpace(b)
|
||||
i := bytes.LastIndexByte(b, ' ')
|
||||
if i < 0 {
|
||||
return fmt.Errorf("%s: invalid format", file)
|
||||
}
|
||||
clock, err := strconv.Atoi(string(b[i+1:]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Name = string(b[:i])
|
||||
c.Clock = clock
|
||||
return nil
|
||||
}
|
||||
|
||||
// Time represents /dev/time.
|
||||
type Time struct {
|
||||
Unix time.Duration
|
||||
UnixNano time.Duration
|
||||
Ticks int64 // clock ticks
|
||||
Freq int64 //cloc frequency
|
||||
}
|
||||
|
||||
// Uptime returns uptime.
|
||||
func (t *Time) Uptime() time.Duration {
|
||||
v := float64(t.Ticks) / float64(t.Freq)
|
||||
return time.Duration(v*1000_000_000) * time.Nanosecond
|
||||
}
|
||||
|
||||
func ReadTime(ctx context.Context, opts ...Option) (*Time, error) {
|
||||
cfg := newConfig(opts...)
|
||||
file := filepath.Join(cfg.rootdir, "/dev/time")
|
||||
var t Time
|
||||
if err := readTime(file, &t); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
// ProcStatus represents a /proc/n/status.
|
||||
type ProcStatus struct {
|
||||
Name string
|
||||
User string
|
||||
State string
|
||||
Times CPUTime
|
||||
MemUsed int64 // in units of 1024 bytes
|
||||
BasePriority uint32 // 0(low) to 19(high)
|
||||
Priority uint32 // 0(low) to 19(high)
|
||||
}
|
||||
|
||||
// CPUTime represents /dev/cputime or a part of /proc/n/status.
|
||||
type CPUTime struct {
|
||||
User time.Duration // the time in user mode (millisecconds)
|
||||
Sys time.Duration
|
||||
Real time.Duration
|
||||
ChildUser time.Duration // exited children and descendants time in user mode
|
||||
ChildSys time.Duration
|
||||
ChildReal time.Duration
|
||||
}
|
||||
|
||||
// CPUStats emulates Linux's /proc/stat.
|
||||
type CPUStats struct {
|
||||
User time.Duration
|
||||
Sys time.Duration
|
||||
Idle time.Duration
|
||||
}
|
||||
|
||||
func ReadCPUStats(ctx context.Context, opts ...Option) (*CPUStats, error) {
|
||||
cfg := newConfig(opts...)
|
||||
a, err := ReadSysStats(ctx, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dir := filepath.Join(cfg.rootdir, "/proc")
|
||||
d, err := os.Open(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer d.Close()
|
||||
|
||||
names, err := d.Readdirnames(0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var up uint32parser
|
||||
pids := make([]uint32, len(names))
|
||||
for i, s := range names {
|
||||
pids[i] = up.Parse(s)
|
||||
}
|
||||
if up.err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Slice(pids, func(i, j int) bool {
|
||||
return pids[i] < pids[j]
|
||||
})
|
||||
|
||||
var stat CPUStats
|
||||
for _, pid := range pids {
|
||||
s := strconv.FormatUint(uint64(pid), 10)
|
||||
file := filepath.Join(dir, s, "status")
|
||||
var p ProcStatus
|
||||
if err := readProcStatus(file, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stat.User += p.Times.User
|
||||
stat.Sys += p.Times.Sys
|
||||
}
|
||||
|
||||
var t Time
|
||||
file := filepath.Join(cfg.rootdir, "/dev/time")
|
||||
if err := readTime(file, &t); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// In multi-processor host, Idle should multiple by number of cores.
|
||||
u := t.Uptime() * time.Duration(len(a))
|
||||
stat.Idle = u - stat.User - stat.Sys
|
||||
return &stat, nil
|
||||
}
|
||||
|
||||
func readProcStatus(file string, p *ProcStatus) error {
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
fields := strings.Fields(string(b))
|
||||
if len(fields) != 12 {
|
||||
return errors.New("invalid format")
|
||||
}
|
||||
p.Name = string(fields[0])
|
||||
p.User = string(fields[1])
|
||||
p.State = string(fields[2])
|
||||
var up uint32parser
|
||||
p.Times.User = time.Duration(up.Parse(fields[3])) * time.Millisecond
|
||||
p.Times.Sys = time.Duration(up.Parse(fields[4])) * time.Millisecond
|
||||
p.Times.Real = time.Duration(up.Parse(fields[5])) * time.Millisecond
|
||||
p.Times.ChildUser = time.Duration(up.Parse(fields[6])) * time.Millisecond
|
||||
p.Times.ChildSys = time.Duration(up.Parse(fields[7])) * time.Millisecond
|
||||
p.Times.ChildReal = time.Duration(up.Parse(fields[8])) * time.Millisecond
|
||||
p.MemUsed, err = strconv.ParseInt(fields[9], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.BasePriority = up.Parse(fields[10])
|
||||
p.Priority = up.Parse(fields[11])
|
||||
return up.err
|
||||
}
|
||||
|
||||
func readTime(file string, t *Time) error {
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fields := strings.Fields(string(b))
|
||||
if len(fields) != 4 {
|
||||
return errors.New("invalid format")
|
||||
}
|
||||
n, err := strconv.ParseInt(fields[0], 10, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.Unix = time.Duration(n) * time.Second
|
||||
v, err := strconv.ParseInt(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.UnixNano = time.Duration(v) * time.Nanosecond
|
||||
t.Ticks, err = strconv.ParseInt(fields[2], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.Freq, err = strconv.ParseInt(fields[3], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type uint32parser struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (p *uint32parser) Parse(s string) uint32 {
|
||||
if p.err != nil {
|
||||
return 0
|
||||
}
|
||||
n, err := strconv.ParseUint(s, 10, 32)
|
||||
if err != nil {
|
||||
p.err = err
|
||||
return 0
|
||||
}
|
||||
return uint32(n)
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
// Package stats provides statistic utilities for Plan 9.
|
||||
package stats
|
||||
|
|
@ -0,0 +1,303 @@
|
|||
package stats
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
delim = []byte{' '}
|
||||
)
|
||||
|
||||
// Host represents host status.
|
||||
type Host struct {
|
||||
Sysname string
|
||||
Storages []*Storage
|
||||
Interfaces []*Interface
|
||||
}
|
||||
|
||||
// MemStats represents the memory statistics.
|
||||
type MemStats struct {
|
||||
Total int64 // total memory in byte
|
||||
PageSize int64 // a page size in byte
|
||||
KernelPages int64
|
||||
UserPages Gauge
|
||||
SwapPages Gauge
|
||||
|
||||
Malloced Gauge // kernel malloced data in byte
|
||||
Graphics Gauge // kernel graphics data in byte
|
||||
}
|
||||
|
||||
// Gauge is used/available gauge.
|
||||
type Gauge struct {
|
||||
Used int64
|
||||
Avail int64
|
||||
}
|
||||
|
||||
func (g Gauge) Free() int64 {
|
||||
return g.Avail - g.Used
|
||||
}
|
||||
|
||||
// ReadMemStats reads memory statistics from /dev/swap.
|
||||
func ReadMemStats(ctx context.Context, opts ...Option) (*MemStats, error) {
|
||||
cfg := newConfig(opts...)
|
||||
swap := filepath.Join(cfg.rootdir, "/dev/swap")
|
||||
f, err := os.Open(swap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var stat MemStats
|
||||
m := map[string]interface{}{
|
||||
"memory": &stat.Total,
|
||||
"pagesize": &stat.PageSize,
|
||||
"kernel": &stat.KernelPages,
|
||||
"user": &stat.UserPages,
|
||||
"swap": &stat.SwapPages,
|
||||
"kernel malloc": &stat.Malloced,
|
||||
"kernel draw": &stat.Graphics,
|
||||
}
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
fields := bytes.SplitN(scanner.Bytes(), delim, 2)
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
switch key := string(fields[1]); key {
|
||||
case "memory", "pagesize", "kernel":
|
||||
v := m[key].(*int64)
|
||||
n, err := strconv.ParseInt(string(fields[0]), 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
*v = n
|
||||
case "user", "swap", "kernel malloc", "kernel draw":
|
||||
v := m[key].(*Gauge)
|
||||
if err := parseGauge(string(fields[0]), v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &stat, nil
|
||||
}
|
||||
|
||||
func parseGauge(s string, r *Gauge) error {
|
||||
a := strings.SplitN(s, "/", 2)
|
||||
if len(a) != 2 {
|
||||
return fmt.Errorf("can't parse ratio: %s", s)
|
||||
}
|
||||
var p intParser
|
||||
u := p.ParseInt64(a[0], 10)
|
||||
n := p.ParseInt64(a[1], 10)
|
||||
if err := p.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Used = u
|
||||
r.Avail = n
|
||||
return nil
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
Name string
|
||||
Model string
|
||||
Capacity int64
|
||||
}
|
||||
|
||||
type Interface struct {
|
||||
Name string
|
||||
Addr string
|
||||
}
|
||||
|
||||
const (
|
||||
numEther = 8 // see ether(3)
|
||||
numIpifc = 16 // see ip(3)
|
||||
)
|
||||
|
||||
// ReadInterfaces reads network interfaces from etherN.
|
||||
func ReadInterfaces(ctx context.Context, opts ...Option) ([]*Interface, error) {
|
||||
cfg := newConfig(opts...)
|
||||
var a []*Interface
|
||||
for i := 0; i < numEther; i++ {
|
||||
p, err := readInterface(cfg.rootdir, i)
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a = append(a, p)
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func readInterface(netroot string, i int) (*Interface, error) {
|
||||
ether := fmt.Sprintf("ether%d", i)
|
||||
dir := filepath.Join(netroot, ether)
|
||||
info, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return nil, fmt.Errorf("%s: is not directory", dir)
|
||||
}
|
||||
|
||||
addr, err := ioutil.ReadFile(filepath.Join(dir, "addr"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Interface{
|
||||
Name: ether,
|
||||
Addr: string(addr),
|
||||
}, nil
|
||||
}
|
||||
|
||||
var (
|
||||
netdirs = []string{"/net", "/net.alt"}
|
||||
)
|
||||
|
||||
// ReadHost reads host status.
|
||||
func ReadHost(ctx context.Context, opts ...Option) (*Host, error) {
|
||||
cfg := newConfig(opts...)
|
||||
var h Host
|
||||
name, err := readSysname(cfg.rootdir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.Sysname = name
|
||||
|
||||
a, err := readStorages(cfg.rootdir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.Storages = a
|
||||
|
||||
for _, s := range netdirs {
|
||||
netroot := filepath.Join(cfg.rootdir, s)
|
||||
ifaces, err := ReadInterfaces(ctx, WithRootDir(netroot))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.Interfaces = append(h.Interfaces, ifaces...)
|
||||
}
|
||||
return &h, nil
|
||||
}
|
||||
|
||||
func readSysname(rootdir string) (string, error) {
|
||||
file := filepath.Join(rootdir, "/dev/sysname")
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bytes.TrimSpace(b)), nil
|
||||
}
|
||||
|
||||
func readStorages(rootdir string) ([]*Storage, error) {
|
||||
sdctl := filepath.Join(rootdir, "/dev/sdctl")
|
||||
f, err := os.Open(sdctl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var a []*Storage
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
fields := bytes.Split(scanner.Bytes(), delim)
|
||||
if len(fields) == 0 {
|
||||
continue
|
||||
}
|
||||
exp := string(fields[0]) + "*"
|
||||
if !strings.HasPrefix(exp, "sd") {
|
||||
continue
|
||||
}
|
||||
dir := filepath.Join(rootdir, "/dev", exp)
|
||||
m, err := filepath.Glob(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, dir := range m {
|
||||
s, err := readStorage(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a = append(a, s)
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func readStorage(dir string) (*Storage, error) {
|
||||
ctl := filepath.Join(dir, "ctl")
|
||||
f, err := os.Open(ctl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var s Storage
|
||||
s.Name = filepath.Base(dir)
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
switch {
|
||||
case bytes.HasPrefix(line, []byte("inquiry")):
|
||||
s.Model = string(bytes.TrimSpace(line[7:]))
|
||||
case bytes.HasPrefix(line, []byte("geometry")):
|
||||
fields := bytes.Split(line, delim)
|
||||
if len(fields) < 3 {
|
||||
continue
|
||||
}
|
||||
var p intParser
|
||||
sec := p.ParseInt64(string(fields[1]), 10)
|
||||
size := p.ParseInt64(string(fields[2]), 10)
|
||||
if err := p.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.Capacity = sec * size
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
type IPStats struct {
|
||||
ID int // number of interface in ipifc dir
|
||||
Device string // associated physical device
|
||||
MTU int // max transfer unit
|
||||
Sendra6 uint8 // on == send router adv
|
||||
Recvra6 uint8 // on == recv router adv
|
||||
|
||||
Pktin int64 // packets read
|
||||
Pktout int64 // packets written
|
||||
Errin int64 // read errors
|
||||
Errout int64 // write errors
|
||||
}
|
||||
|
||||
type Iplifc struct {
|
||||
IP net.IP
|
||||
Mask net.IPMask
|
||||
Net net.IP // ip & mask
|
||||
PerfLifetime int64 // preferred lifetime
|
||||
ValidLifetime int64 // valid lifetime
|
||||
}
|
||||
|
||||
type Ipv6rp struct {
|
||||
// TODO(lufia): see ip(2)
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package stats
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type intParser struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (p *intParser) ParseInt(s string, base int) int {
|
||||
if p.err != nil {
|
||||
return 0
|
||||
}
|
||||
var n int64
|
||||
n, p.err = strconv.ParseInt(s, base, 0)
|
||||
return int(n)
|
||||
}
|
||||
|
||||
func (p *intParser) ParseInt64(s string, base int) int64 {
|
||||
if p.err != nil {
|
||||
return 0
|
||||
}
|
||||
var n int64
|
||||
n, p.err = strconv.ParseInt(s, base, 64)
|
||||
return n
|
||||
}
|
||||
|
||||
func (p *intParser) Err() error {
|
||||
return p.err
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package stats
|
||||
|
||||
type Config struct {
|
||||
rootdir string
|
||||
}
|
||||
|
||||
type Option func(*Config)
|
||||
|
||||
func newConfig(opts ...Option) *Config {
|
||||
var cfg Config
|
||||
for _, opt := range opts {
|
||||
opt(&cfg)
|
||||
}
|
||||
return &cfg
|
||||
}
|
||||
|
||||
func WithRootDir(dir string) Option {
|
||||
return func(cfg *Config) {
|
||||
cfg.rootdir = dir
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
package stats
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type InterfaceStats struct {
|
||||
PacketsReceived int64 // in packets
|
||||
Link int // link status
|
||||
PacketsSent int64 // out packets
|
||||
NumCRCErr int // input CRC errors
|
||||
NumOverflows int // packet overflows
|
||||
NumSoftOverflows int // software overflow
|
||||
NumFramingErr int // framing errors
|
||||
NumBufferingErr int // buffering errors
|
||||
NumOutputErr int // output errors
|
||||
Promiscuous int // number of promiscuous opens
|
||||
Mbps int // megabits per sec
|
||||
Addr string
|
||||
}
|
||||
|
||||
func ReadInterfaceStats(ctx context.Context, opts ...Option) (*InterfaceStats, error) {
|
||||
cfg := newConfig(opts...)
|
||||
file := filepath.Join(cfg.rootdir, "stats")
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var stats InterfaceStats
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
s := strings.TrimSpace(scanner.Text())
|
||||
a := strings.SplitN(s, ":", 2)
|
||||
if len(a) != 2 {
|
||||
continue
|
||||
}
|
||||
var p intParser
|
||||
v := strings.TrimSpace(a[1])
|
||||
switch a[0] {
|
||||
case "in":
|
||||
stats.PacketsReceived = p.ParseInt64(v, 10)
|
||||
case "link":
|
||||
stats.Link = p.ParseInt(v, 10)
|
||||
case "out":
|
||||
stats.PacketsSent = p.ParseInt64(v, 10)
|
||||
case "crc":
|
||||
stats.NumCRCErr = p.ParseInt(v, 10)
|
||||
case "overflows":
|
||||
stats.NumOverflows = p.ParseInt(v, 10)
|
||||
case "soft overflows":
|
||||
stats.NumSoftOverflows = p.ParseInt(v, 10)
|
||||
case "framing errs":
|
||||
stats.NumFramingErr = p.ParseInt(v, 10)
|
||||
case "buffer errs":
|
||||
stats.NumBufferingErr = p.ParseInt(v, 10)
|
||||
case "output errs":
|
||||
stats.NumOutputErr = p.ParseInt(v, 10)
|
||||
case "prom":
|
||||
stats.Promiscuous = p.ParseInt(v, 10)
|
||||
case "mbps":
|
||||
stats.Mbps = p.ParseInt(v, 10)
|
||||
case "addr":
|
||||
stats.Addr = v
|
||||
}
|
||||
if err := p.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &stats, nil
|
||||
}
|
||||
|
||||
type TCPStats struct {
|
||||
MaxConn int
|
||||
MaxSegment int
|
||||
ActiveOpens int
|
||||
PassiveOpens int
|
||||
EstablishedResets int
|
||||
CurrentEstablished int
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2020 Power DevOps
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,159 @@
|
|||
#include "c_helpers.h"
|
||||
|
||||
GETFUNC(cpu)
|
||||
GETFUNC(disk)
|
||||
GETFUNC(diskadapter)
|
||||
GETFUNC(diskpath)
|
||||
GETFUNC(fcstat)
|
||||
GETFUNC(logicalvolume)
|
||||
GETFUNC(memory_page)
|
||||
GETFUNC(netadapter)
|
||||
GETFUNC(netbuffer)
|
||||
GETFUNC(netinterface)
|
||||
GETFUNC(pagingspace)
|
||||
GETFUNC(process)
|
||||
GETFUNC(thread)
|
||||
GETFUNC(volumegroup)
|
||||
|
||||
double get_partition_mhz(perfstat_partition_config_t pinfo) {
|
||||
return pinfo.processorMHz;
|
||||
}
|
||||
|
||||
char *get_ps_hostname(perfstat_pagingspace_t *ps) {
|
||||
return ps->u.nfs_paging.hostname;
|
||||
}
|
||||
|
||||
char *get_ps_filename(perfstat_pagingspace_t *ps) {
|
||||
return ps->u.nfs_paging.filename;
|
||||
}
|
||||
|
||||
char *get_ps_vgname(perfstat_pagingspace_t *ps) {
|
||||
return ps->u.lv_paging.vgname;
|
||||
}
|
||||
|
||||
time_t boottime()
|
||||
{
|
||||
register struct utmpx *utmp;
|
||||
|
||||
setutxent();
|
||||
while ( (utmp = getutxent()) != NULL ) {
|
||||
if (utmp->ut_type == BOOT_TIME) {
|
||||
return utmp->ut_tv.tv_sec;
|
||||
}
|
||||
}
|
||||
endutxent();
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct fsinfo *get_filesystem_stat(struct fsinfo *fs_all, int n) {
|
||||
if (!fs_all) return NULL;
|
||||
return &(fs_all[n]);
|
||||
}
|
||||
|
||||
int get_mounts(struct vmount **vmountpp) {
|
||||
int size;
|
||||
struct vmount *vm;
|
||||
int nmounts;
|
||||
|
||||
size = BUFSIZ;
|
||||
|
||||
while (1) {
|
||||
if ((vm = (struct vmount *)malloc((size_t)size)) == NULL) {
|
||||
perror("malloc failed");
|
||||
exit(-1);
|
||||
}
|
||||
if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
|
||||
*vmountpp = vm;
|
||||
return nmounts;
|
||||
} else if (nmounts == 0) {
|
||||
size = *(int *)vm;
|
||||
free((void *)vm);
|
||||
} else {
|
||||
free((void *)vm);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fill_fsinfo(struct statfs statbuf, struct fsinfo *fs) {
|
||||
fsblkcnt_t freeblks, totblks, usedblks;
|
||||
fsblkcnt_t tinodes, ninodes, ifree;
|
||||
uint cfactor;
|
||||
|
||||
if (statbuf.f_blocks == -1) {
|
||||
fs->totalblks = 0;
|
||||
fs->freeblks = 0;
|
||||
fs->totalinodes = 0;
|
||||
fs->freeinodes = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
cfactor = statbuf.f_bsize / 512;
|
||||
fs->freeblks = statbuf.f_bavail * cfactor;
|
||||
fs->totalblks = statbuf.f_blocks * cfactor;
|
||||
|
||||
fs->freeinodes = statbuf.f_ffree;
|
||||
fs->totalinodes = statbuf.f_files;
|
||||
|
||||
if (fs->freeblks < 0)
|
||||
fs->freeblks = 0;
|
||||
}
|
||||
|
||||
int getfsinfo(char *fsname, char *devname, char *host, char *options, int flags, int fstype, struct fsinfo *fs) {
|
||||
struct statfs statbuf;
|
||||
int devname_size = strlen(devname);
|
||||
int fsname_size = strlen(fsname);
|
||||
char buf[BUFSIZ];
|
||||
char *p;
|
||||
|
||||
if (fs == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (p = strtok(options, ","); p != NULL; p = strtok(NULL, ","))
|
||||
if (strcmp(p, "ignore") == 0)
|
||||
return 0;
|
||||
|
||||
if (*host != 0 && strcmp(host, "-") != 0) {
|
||||
sprintf(buf, "%s:%s", host, devname);
|
||||
devname = buf;
|
||||
}
|
||||
fs->devname = (char *)calloc(devname_size+1, 1);
|
||||
fs->fsname = (char *)calloc(fsname_size+1, 1);
|
||||
strncpy(fs->devname, devname, devname_size);
|
||||
strncpy(fs->fsname, fsname, fsname_size);
|
||||
fs->flags = flags;
|
||||
fs->fstype = fstype;
|
||||
|
||||
if (statfs(fsname,&statbuf) < 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
fill_fsinfo(statbuf, fs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct fsinfo *get_all_fs(int *rc) {
|
||||
struct vmount *mnt;
|
||||
struct fsinfo *fs_all;
|
||||
int nmounts;
|
||||
|
||||
*rc = -1;
|
||||
if ((nmounts = get_mounts(&mnt)) <= 0) {
|
||||
perror("Can't get mount table info");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fs_all = (struct fsinfo *)calloc(sizeof(struct fsinfo), nmounts);
|
||||
while ((*rc)++, nmounts--) {
|
||||
getfsinfo(vmt2dataptr(mnt, VMT_STUB),
|
||||
vmt2dataptr(mnt, VMT_OBJECT),
|
||||
vmt2dataptr(mnt, VMT_HOST),
|
||||
vmt2dataptr(mnt, VMT_ARGS),
|
||||
mnt->vmt_flags,
|
||||
mnt->vmt_gfstype,
|
||||
&fs_all[*rc]);
|
||||
mnt = (struct vmount *)((char *)mnt + mnt->vmt_length);
|
||||
}
|
||||
return fs_all;
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
#ifndef C_HELPERS_H
|
||||
#define C_HELPERS_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/mntctl.h>
|
||||
#include <sys/vmount.h>
|
||||
#include <sys/statfs.h>
|
||||
#include <libperfstat.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <utmpx.h>
|
||||
|
||||
#define GETFUNC(TYPE) perfstat_##TYPE##_t *get_##TYPE##_stat(perfstat_##TYPE##_t *b, int n) { \
|
||||
if (!b) return NULL; \
|
||||
return &(b[n]); \
|
||||
}
|
||||
|
||||
#define GETFUNC_EXT(TYPE) extern perfstat_##TYPE##_t *get_##TYPE##_stat(perfstat_##TYPE##_t *, int);
|
||||
|
||||
GETFUNC_EXT(cpu)
|
||||
GETFUNC_EXT(disk)
|
||||
GETFUNC_EXT(diskadapter)
|
||||
GETFUNC_EXT(diskpath)
|
||||
GETFUNC_EXT(fcstat)
|
||||
GETFUNC_EXT(logicalvolume)
|
||||
GETFUNC_EXT(memory_page)
|
||||
GETFUNC_EXT(netadapter)
|
||||
GETFUNC_EXT(netbuffer)
|
||||
GETFUNC_EXT(netinterface)
|
||||
GETFUNC_EXT(pagingspace)
|
||||
GETFUNC_EXT(process)
|
||||
GETFUNC_EXT(thread)
|
||||
GETFUNC_EXT(volumegroup)
|
||||
|
||||
struct fsinfo {
|
||||
char *devname;
|
||||
char *fsname;
|
||||
int flags;
|
||||
int fstype;
|
||||
unsigned long totalblks;
|
||||
unsigned long freeblks;
|
||||
unsigned long totalinodes;
|
||||
unsigned long freeinodes;
|
||||
};
|
||||
|
||||
extern double get_partition_mhz(perfstat_partition_config_t);
|
||||
extern char *get_ps_hostname(perfstat_pagingspace_t *);
|
||||
extern char *get_ps_filename(perfstat_pagingspace_t *);
|
||||
extern char *get_ps_vgname(perfstat_pagingspace_t *);
|
||||
extern time_t boottime();
|
||||
struct fsinfo *get_filesystem_stat(struct fsinfo *, int);
|
||||
int get_mounts(struct vmount **);
|
||||
void fill_statfs(struct statfs, struct fsinfo *);
|
||||
int getfsinfo(char *, char *, char *, char *, int, int, struct fsinfo *);
|
||||
struct fsinfo *get_all_fs(int *);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
func EnableLVMStat() {
|
||||
C.perfstat_config(C.PERFSTAT_ENABLE|C.PERFSTAT_LV|C.PERFSTAT_VG, nil)
|
||||
}
|
||||
|
||||
func DisableLVMStat() {
|
||||
C.perfstat_config(C.PERFSTAT_DISABLE|C.PERFSTAT_LV|C.PERFSTAT_VG, nil)
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func CpuStat() ([]CPU, error) {
|
||||
var cpustat *C.perfstat_cpu_t
|
||||
var cpu C.perfstat_id_t
|
||||
|
||||
ncpu := runtime.NumCPU()
|
||||
|
||||
cpustat_len := C.sizeof_perfstat_cpu_t * C.ulong(ncpu)
|
||||
cpustat = (*C.perfstat_cpu_t)(C.malloc(cpustat_len))
|
||||
defer C.free(unsafe.Pointer(cpustat))
|
||||
C.strcpy(&cpu.name[0], C.CString(C.FIRST_CPU))
|
||||
r := C.perfstat_cpu(&cpu, cpustat, C.sizeof_perfstat_cpu_t, C.int(ncpu))
|
||||
if r <= 0 {
|
||||
return nil, fmt.Errorf("error perfstat_cpu()")
|
||||
}
|
||||
c := make([]CPU, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
n := C.get_cpu_stat(cpustat, C.int(i))
|
||||
if n != nil {
|
||||
c[i] = perfstatcpu2cpu(n)
|
||||
}
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func CpuTotalStat() (*CPUTotal, error) {
|
||||
var cpustat *C.perfstat_cpu_total_t
|
||||
|
||||
cpustat = (*C.perfstat_cpu_total_t)(C.malloc(C.sizeof_perfstat_cpu_total_t))
|
||||
defer C.free(unsafe.Pointer(cpustat))
|
||||
r := C.perfstat_cpu_total(nil, cpustat, C.sizeof_perfstat_cpu_total_t, 1)
|
||||
if r <= 0 {
|
||||
return nil, fmt.Errorf("error perfstat_cpu_total()")
|
||||
}
|
||||
c := perfstatcputotal2cputotal(cpustat)
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func CpuUtilStat(intvl time.Duration) (*CPUUtil, error) {
|
||||
var cpuutil *C.perfstat_cpu_util_t
|
||||
var newt *C.perfstat_cpu_total_t
|
||||
var oldt *C.perfstat_cpu_total_t
|
||||
var data C.perfstat_rawdata_t
|
||||
|
||||
oldt = (*C.perfstat_cpu_total_t)(C.malloc(C.sizeof_perfstat_cpu_total_t))
|
||||
newt = (*C.perfstat_cpu_total_t)(C.malloc(C.sizeof_perfstat_cpu_total_t))
|
||||
cpuutil = (*C.perfstat_cpu_util_t)(C.malloc(C.sizeof_perfstat_cpu_util_t))
|
||||
defer C.free(unsafe.Pointer(oldt))
|
||||
defer C.free(unsafe.Pointer(newt))
|
||||
defer C.free(unsafe.Pointer(cpuutil))
|
||||
|
||||
r := C.perfstat_cpu_total(nil, oldt, C.sizeof_perfstat_cpu_total_t, 1)
|
||||
if r <= 0 {
|
||||
return nil, fmt.Errorf("error perfstat_cpu_total()")
|
||||
}
|
||||
|
||||
time.Sleep(intvl)
|
||||
|
||||
r = C.perfstat_cpu_total(nil, newt, C.sizeof_perfstat_cpu_total_t, 1)
|
||||
if r <= 0 {
|
||||
return nil, fmt.Errorf("error perfstat_cpu_total()")
|
||||
}
|
||||
|
||||
data._type = C.UTIL_CPU_TOTAL
|
||||
data.curstat = unsafe.Pointer(newt)
|
||||
data.prevstat = unsafe.Pointer(oldt)
|
||||
data.sizeof_data = C.sizeof_perfstat_cpu_total_t
|
||||
data.cur_elems = 1
|
||||
data.prev_elems = 1
|
||||
|
||||
r = C.perfstat_cpu_util(&data, cpuutil, C.sizeof_perfstat_cpu_util_t, 1)
|
||||
if r <= 0 {
|
||||
return nil, fmt.Errorf("error perfstat_cpu_util()")
|
||||
}
|
||||
u := perfstatcpuutil2cpuutil(cpuutil)
|
||||
return &u, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,137 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func DiskTotalStat() (*DiskTotal, error) {
|
||||
var disk C.perfstat_disk_total_t
|
||||
|
||||
rc := C.perfstat_disk_total(nil, &disk, C.sizeof_perfstat_disk_total_t, 1)
|
||||
if rc != 1 {
|
||||
return nil, fmt.Errorf("perfstat_disk_total() error")
|
||||
}
|
||||
d := perfstatdisktotal2disktotal(disk)
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func DiskAdapterStat() ([]DiskAdapter, error) {
|
||||
var adapter *C.perfstat_diskadapter_t
|
||||
var adptname C.perfstat_id_t
|
||||
|
||||
numadpt := C.perfstat_diskadapter(nil, nil, C.sizeof_perfstat_diskadapter_t, 0)
|
||||
if numadpt <= 0 {
|
||||
return nil, fmt.Errorf("perfstat_diskadapter() error")
|
||||
}
|
||||
|
||||
adapter_len := C.sizeof_perfstat_diskadapter_t * C.ulong(numadpt)
|
||||
adapter = (*C.perfstat_diskadapter_t)(C.malloc(adapter_len))
|
||||
defer C.free(unsafe.Pointer(adapter))
|
||||
C.strcpy(&adptname.name[0], C.CString(C.FIRST_DISKADAPTER))
|
||||
r := C.perfstat_diskadapter(&adptname, adapter, C.sizeof_perfstat_diskadapter_t, numadpt)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_diskadapter() error")
|
||||
}
|
||||
da := make([]DiskAdapter, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
d := C.get_diskadapter_stat(adapter, C.int(i))
|
||||
if d != nil {
|
||||
da[i] = perfstatdiskadapter2diskadapter(d)
|
||||
}
|
||||
}
|
||||
return da, nil
|
||||
}
|
||||
|
||||
func DiskStat() ([]Disk, error) {
|
||||
var disk *C.perfstat_disk_t
|
||||
var diskname C.perfstat_id_t
|
||||
|
||||
numdisk := C.perfstat_disk(nil, nil, C.sizeof_perfstat_disk_t, 0)
|
||||
if numdisk <= 0 {
|
||||
return nil, fmt.Errorf("perfstat_disk() error")
|
||||
}
|
||||
|
||||
disk_len := C.sizeof_perfstat_disk_t * C.ulong(numdisk)
|
||||
disk = (*C.perfstat_disk_t)(C.malloc(disk_len))
|
||||
defer C.free(unsafe.Pointer(disk))
|
||||
C.strcpy(&diskname.name[0], C.CString(C.FIRST_DISK))
|
||||
r := C.perfstat_disk(&diskname, disk, C.sizeof_perfstat_disk_t, numdisk)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_disk() error")
|
||||
}
|
||||
d := make([]Disk, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
ds := C.get_disk_stat(disk, C.int(i))
|
||||
if ds != nil {
|
||||
d[i] = perfstatdisk2disk(ds)
|
||||
}
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func DiskPathStat() ([]DiskPath, error) {
|
||||
var diskpath *C.perfstat_diskpath_t
|
||||
var pathname C.perfstat_id_t
|
||||
|
||||
numpaths := C.perfstat_diskpath(nil, nil, C.sizeof_perfstat_diskpath_t, 0)
|
||||
if numpaths <= 0 {
|
||||
return nil, fmt.Errorf("perfstat_diskpath() error")
|
||||
}
|
||||
|
||||
path_len := C.sizeof_perfstat_diskpath_t * C.ulong(numpaths)
|
||||
diskpath = (*C.perfstat_diskpath_t)(C.malloc(path_len))
|
||||
defer C.free(unsafe.Pointer(diskpath))
|
||||
C.strcpy(&pathname.name[0], C.CString(C.FIRST_DISKPATH))
|
||||
r := C.perfstat_diskpath(&pathname, diskpath, C.sizeof_perfstat_diskpath_t, numpaths)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_diskpath() error")
|
||||
}
|
||||
d := make([]DiskPath, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
p := C.get_diskpath_stat(diskpath, C.int(i))
|
||||
if p != nil {
|
||||
d[i] = perfstatdiskpath2diskpath(p)
|
||||
}
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func FCAdapterStat() ([]FCAdapter, error) {
|
||||
var fcstat *C.perfstat_fcstat_t
|
||||
var fcname C.perfstat_id_t
|
||||
|
||||
numadpt := C.perfstat_fcstat(nil, nil, C.sizeof_perfstat_fcstat_t, 0)
|
||||
if numadpt <= 0 {
|
||||
return nil, fmt.Errorf("perfstat_fcstat() error")
|
||||
}
|
||||
|
||||
fcstat_len := C.sizeof_perfstat_fcstat_t * C.ulong(numadpt)
|
||||
fcstat = (*C.perfstat_fcstat_t)(C.malloc(fcstat_len))
|
||||
defer C.free(unsafe.Pointer(fcstat))
|
||||
C.strcpy(&fcname.name[0], C.CString(C.FIRST_NETINTERFACE))
|
||||
r := C.perfstat_fcstat(&fcname, fcstat, C.sizeof_perfstat_fcstat_t, numadpt)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_fcstat() error")
|
||||
}
|
||||
fca := make([]FCAdapter, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
f := C.get_fcstat_stat(fcstat, C.int(i))
|
||||
if f != nil {
|
||||
fca[i] = perfstatfcstat2fcadapter(f)
|
||||
}
|
||||
}
|
||||
return fca, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,315 @@
|
|||
// +build !aix
|
||||
|
||||
// Copyright 2020 Power-Devops.com. All rights reserved.
|
||||
// Use of this source code is governed by the license
|
||||
// that can be found in the LICENSE file.
|
||||
/*
|
||||
Package perfstat is Go interface to IBM AIX libperfstat.
|
||||
To use it you need AIX with installed bos.perf.libperfstat. You can check, if is installed using the following command:
|
||||
|
||||
$ lslpp -L bos.perf.perfstat
|
||||
|
||||
The package is written using Go 1.14.7 and AIX 7.2 TL5. It should work with earlier TLs of AIX 7.2, but I
|
||||
can't guarantee that perfstat structures in the TLs have all the same fields as the structures in AIX 7.2 TL5.
|
||||
|
||||
For documentation of perfstat on AIX and using it in programs refer to the official IBM documentation:
|
||||
https://www.ibm.com/support/knowledgecenter/ssw_aix_72/performancetools/idprftools_perfstat.html
|
||||
*/
|
||||
package perfstat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// EnableLVMStat() switches on LVM (logical volumes and volume groups) performance statistics.
|
||||
// With this enabled you can use fields KBReads, KBWrites, and IOCnt
|
||||
// in LogicalVolume and VolumeGroup data types.
|
||||
func EnableLVMStat() {}
|
||||
|
||||
// DisableLVMStat() switchess of LVM (logical volumes and volume groups) performance statistics.
|
||||
// This is the default state. In this case LogicalVolume and VolumeGroup data types are
|
||||
// populated with informations about LVM structures, but performance statistics fields
|
||||
// (KBReads, KBWrites, IOCnt) are empty.
|
||||
func DisableLVMStat() {}
|
||||
|
||||
// CpuStat() returns array of CPU structures with information about
|
||||
// logical CPUs on the system.
|
||||
// IBM documentation:
|
||||
// * https://www.ibm.com/support/knowledgecenter/ssw_aix_72/performancetools/idprftools_perfstat_int_cpu.html
|
||||
// * https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/p_bostechref/perfstat_cpu.html
|
||||
func CpuStat() ([]CPU, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
// CpuTotalStat() returns general information about CPUs on the system.
|
||||
// IBM documentation:
|
||||
// * https://www.ibm.com/support/knowledgecenter/ssw_aix_72/performancetools/idprftools_perfstat_glob_cpu.html
|
||||
// * https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/p_bostechref/perfstat_cputot.html
|
||||
func CpuTotalStat() (*CPUTotal, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
// CpuUtilStat() calculates CPU utilization.
|
||||
// IBM documentation:
|
||||
// * https://www.ibm.com/support/knowledgecenter/ssw_aix_72/performancetools/idprftools_perfstat_cpu_util.html
|
||||
// * https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/p_bostechref/perfstat_cpu_util.html
|
||||
func CpuUtilStat(intvl time.Duration) (*CPUUtil, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func DiskTotalStat() (*DiskTotal, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func DiskAdapterStat() ([]DiskAdapter, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func DiskStat() ([]Disk, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func DiskPathStat() ([]DiskPath, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func FCAdapterStat() ([]FCAdapter, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func PartitionStat() (*PartitionConfig, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func LogicalVolumeStat() ([]LogicalVolume, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func VolumeGroupStat() ([]VolumeGroup, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func MemoryTotalStat() (*MemoryTotal, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func MemoryPageStat() ([]MemoryPage, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func PagingSpaceStat() ([]PagingSpace, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func NetIfaceTotalStat() (*NetIfaceTotal, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func NetBufferStat() ([]NetBuffer, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func NetIfaceStat() ([]NetIface, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func NetAdapterStat() ([]NetAdapter, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func ProcessStat() ([]Process, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func ThreadStat() ([]Thread, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func Sysconf(name int32) (int64, error) {
|
||||
return 0, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func GetCPUImplementation() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func POWER9OrNewer() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER9() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER8OrNewer() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER8() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER7OrNewer() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER7() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func HasTransactionalMemory() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func Is64Bit() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func IsSMP() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func HasVMX() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func HasVSX() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func HasDFP() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func HasNxGzip() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func PksCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func PksEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func CPUMode() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func KernelBits() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func IsLPAR() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func CpuAddCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func CpuRemoveCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func MemoryAddCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func MemoryRemoveCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func DLparCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func IsNUMA() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func KernelKeys() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func RecoveryMode() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func EnhancedAffinity() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func VTpmEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func IsVIOS() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func MLSEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func SPLparCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func SPLparEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func DedicatedLpar() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func SPLparCapped() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func SPLparDonating() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func SmtCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func SmtEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func VrmCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func VrmEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func AmeEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func EcoCapable() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func EcoEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func BootTime() (uint64, error) {
|
||||
return 0, fmt.Errorf("Not implemented")
|
||||
}
|
||||
|
||||
func UptimeSeconds() (uint64, error) {
|
||||
return 0, fmt.Errorf("Not implemented")
|
||||
}
|
||||
|
||||
func FileSystemStat() ([]FileSystem, error) {
|
||||
return nil, fmt.Errorf("Not implemented")
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func FileSystemStat() ([]FileSystem, error) {
|
||||
var fsinfo *C.struct_fsinfo
|
||||
var nmounts C.int
|
||||
|
||||
fsinfo = C.get_all_fs(&nmounts)
|
||||
if nmounts <= 0 {
|
||||
return nil, fmt.Errorf("No mounts found")
|
||||
}
|
||||
|
||||
fs := make([]FileSystem, nmounts)
|
||||
for i := 0; i < int(nmounts); i++ {
|
||||
f := C.get_filesystem_stat(fsinfo, C.int(i))
|
||||
if f != nil {
|
||||
fs[i] = fsinfo2filesystem(f)
|
||||
}
|
||||
}
|
||||
return fs, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,764 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
#include <sys/proc.h>
|
||||
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
func perfstatcpu2cpu(n *C.perfstat_cpu_t) CPU {
|
||||
var c CPU
|
||||
c.Name = C.GoString(&n.name[0])
|
||||
c.User = int64(n.user)
|
||||
c.Sys = int64(n.sys)
|
||||
c.Idle = int64(n.idle)
|
||||
c.Wait = int64(n.wait)
|
||||
c.PSwitch = int64(n.pswitch)
|
||||
c.Syscall = int64(n.syscall)
|
||||
c.Sysread = int64(n.sysread)
|
||||
c.Syswrite = int64(n.syswrite)
|
||||
c.Sysfork = int64(n.sysfork)
|
||||
c.Sysexec = int64(n.sysexec)
|
||||
c.Readch = int64(n.readch)
|
||||
c.Writech = int64(n.writech)
|
||||
c.Bread = int64(n.bread)
|
||||
c.Bwrite = int64(n.bwrite)
|
||||
c.Lread = int64(n.lread)
|
||||
c.Lwrite = int64(n.lwrite)
|
||||
c.Phread = int64(n.phread)
|
||||
c.Phwrite = int64(n.phwrite)
|
||||
c.Iget = int64(n.iget)
|
||||
c.Namei = int64(n.namei)
|
||||
c.Dirblk = int64(n.dirblk)
|
||||
c.Msg = int64(n.msg)
|
||||
c.Sema = int64(n.sema)
|
||||
c.MinFaults = int64(n.minfaults)
|
||||
c.MajFaults = int64(n.majfaults)
|
||||
c.PUser = int64(n.puser)
|
||||
c.PSys = int64(n.psys)
|
||||
c.PIdle = int64(n.pidle)
|
||||
c.PWait = int64(n.pwait)
|
||||
c.RedispSD0 = int64(n.redisp_sd0)
|
||||
c.RedispSD1 = int64(n.redisp_sd1)
|
||||
c.RedispSD2 = int64(n.redisp_sd2)
|
||||
c.RedispSD3 = int64(n.redisp_sd3)
|
||||
c.RedispSD4 = int64(n.redisp_sd4)
|
||||
c.RedispSD5 = int64(n.redisp_sd5)
|
||||
c.MigrationPush = int64(n.migration_push)
|
||||
c.MigrationS3grq = int64(n.migration_S3grq)
|
||||
c.MigrationS3pul = int64(n.migration_S3pul)
|
||||
c.InvolCSwitch = int64(n.invol_cswitch)
|
||||
c.VolCSwitch = int64(n.vol_cswitch)
|
||||
c.RunQueue = int64(n.runque)
|
||||
c.Bound = int64(n.bound)
|
||||
c.DecrIntrs = int64(n.decrintrs)
|
||||
c.MpcRIntrs = int64(n.mpcrintrs)
|
||||
c.MpcSIntrs = int64(n.mpcsintrs)
|
||||
c.SoftIntrs = int64(n.softintrs)
|
||||
c.DevIntrs = int64(n.devintrs)
|
||||
c.PhantIntrs = int64(n.phantintrs)
|
||||
c.IdleDonatedPurr = int64(n.idle_donated_purr)
|
||||
c.IdleDonatedSpurr = int64(n.idle_donated_spurr)
|
||||
c.BusyDonatedPurr = int64(n.busy_donated_purr)
|
||||
c.BusyDonatedSpurr = int64(n.busy_donated_spurr)
|
||||
c.IdleStolenPurr = int64(n.idle_stolen_purr)
|
||||
c.IdleStolenSpurr = int64(n.idle_stolen_spurr)
|
||||
c.BusyStolenPurr = int64(n.busy_stolen_purr)
|
||||
c.BusyStolenSpurr = int64(n.busy_stolen_spurr)
|
||||
c.Hpi = int64(n.hpi)
|
||||
c.Hpit = int64(n.hpit)
|
||||
c.PUserSpurr = int64(n.puser_spurr)
|
||||
c.PSysSpurr = int64(n.psys_spurr)
|
||||
c.PIdleSpurr = int64(n.pidle_spurr)
|
||||
c.PWaitSpurr = int64(n.pwait_spurr)
|
||||
c.SpurrFlag = int32(n.spurrflag)
|
||||
c.LocalDispatch = int64(n.localdispatch)
|
||||
c.NearDispatch = int64(n.neardispatch)
|
||||
c.FarDispatch = int64(n.fardispatch)
|
||||
c.CSwitches = int64(n.cswitches)
|
||||
c.Version = int64(n.version)
|
||||
c.TbLast = int64(n.tb_last)
|
||||
c.State = int(n.state)
|
||||
c.VtbLast = int64(n.vtb_last)
|
||||
c.ICountLast = int64(n.icount_last)
|
||||
return c
|
||||
}
|
||||
|
||||
func perfstatcputotal2cputotal(n *C.perfstat_cpu_total_t) CPUTotal {
|
||||
var c CPUTotal
|
||||
c.NCpus = int(n.ncpus)
|
||||
c.NCpusCfg = int(n.ncpus_cfg)
|
||||
c.Description = C.GoString(&n.description[0])
|
||||
c.ProcessorHz = int64(n.processorHZ)
|
||||
c.User = int64(n.user)
|
||||
c.Sys = int64(n.sys)
|
||||
c.Idle = int64(n.idle)
|
||||
c.Wait = int64(n.wait)
|
||||
c.PSwitch = int64(n.pswitch)
|
||||
c.Syscall = int64(n.syscall)
|
||||
c.Sysread = int64(n.sysread)
|
||||
c.Syswrite = int64(n.syswrite)
|
||||
c.Sysfork = int64(n.sysfork)
|
||||
c.Sysexec = int64(n.sysexec)
|
||||
c.Readch = int64(n.readch)
|
||||
c.Writech = int64(n.writech)
|
||||
c.DevIntrs = int64(n.devintrs)
|
||||
c.SoftIntrs = int64(n.softintrs)
|
||||
c.Lbolt = int64(n.lbolt)
|
||||
c.LoadAvg1 = (float32(n.loadavg[0]) / (1 << C.SBITS))
|
||||
c.LoadAvg5 = (float32(n.loadavg[1]) / (1 << C.SBITS))
|
||||
c.LoadAvg15 = (float32(n.loadavg[2]) / (1 << C.SBITS))
|
||||
c.RunQueue = int64(n.runque)
|
||||
c.SwpQueue = int64(n.swpque)
|
||||
c.Bread = int64(n.bread)
|
||||
c.Bwrite = int64(n.bwrite)
|
||||
c.Lread = int64(n.lread)
|
||||
c.Lwrite = int64(n.lwrite)
|
||||
c.Phread = int64(n.phread)
|
||||
c.Phwrite = int64(n.phwrite)
|
||||
c.RunOcc = int64(n.runocc)
|
||||
c.SwpOcc = int64(n.swpocc)
|
||||
c.Iget = int64(n.iget)
|
||||
c.Namei = int64(n.namei)
|
||||
c.Dirblk = int64(n.dirblk)
|
||||
c.Msg = int64(n.msg)
|
||||
c.Sema = int64(n.sema)
|
||||
c.RcvInt = int64(n.rcvint)
|
||||
c.XmtInt = int64(n.xmtint)
|
||||
c.MdmInt = int64(n.mdmint)
|
||||
c.TtyRawInch = int64(n.tty_rawinch)
|
||||
c.TtyCanInch = int64(n.tty_caninch)
|
||||
c.TtyRawOutch = int64(n.tty_rawoutch)
|
||||
c.Ksched = int64(n.ksched)
|
||||
c.Koverf = int64(n.koverf)
|
||||
c.Kexit = int64(n.kexit)
|
||||
c.Rbread = int64(n.rbread)
|
||||
c.Rcread = int64(n.rcread)
|
||||
c.Rbwrt = int64(n.rbwrt)
|
||||
c.Rcwrt = int64(n.rcwrt)
|
||||
c.Traps = int64(n.traps)
|
||||
c.NCpusHigh = int64(n.ncpus_high)
|
||||
c.PUser = int64(n.puser)
|
||||
c.PSys = int64(n.psys)
|
||||
c.PIdle = int64(n.pidle)
|
||||
c.PWait = int64(n.pwait)
|
||||
c.DecrIntrs = int64(n.decrintrs)
|
||||
c.MpcRIntrs = int64(n.mpcrintrs)
|
||||
c.MpcSIntrs = int64(n.mpcsintrs)
|
||||
c.PhantIntrs = int64(n.phantintrs)
|
||||
c.IdleDonatedPurr = int64(n.idle_donated_purr)
|
||||
c.IdleDonatedSpurr = int64(n.idle_donated_spurr)
|
||||
c.BusyDonatedPurr = int64(n.busy_donated_purr)
|
||||
c.BusyDonatedSpurr = int64(n.busy_donated_spurr)
|
||||
c.IdleStolenPurr = int64(n.idle_stolen_purr)
|
||||
c.IdleStolenSpurr = int64(n.idle_stolen_spurr)
|
||||
c.BusyStolenPurr = int64(n.busy_stolen_purr)
|
||||
c.BusyStolenSpurr = int64(n.busy_stolen_spurr)
|
||||
c.IOWait = int32(n.iowait)
|
||||
c.PhysIO = int32(n.physio)
|
||||
c.TWait = int64(n.twait)
|
||||
c.Hpi = int64(n.hpi)
|
||||
c.Hpit = int64(n.hpit)
|
||||
c.PUserSpurr = int64(n.puser_spurr)
|
||||
c.PSysSpurr = int64(n.psys_spurr)
|
||||
c.PIdleSpurr = int64(n.pidle_spurr)
|
||||
c.PWaitSpurr = int64(n.pwait_spurr)
|
||||
c.SpurrFlag = int(n.spurrflag)
|
||||
c.Version = int64(n.version)
|
||||
c.TbLast = int64(n.tb_last)
|
||||
c.PurrCoalescing = int64(n.purr_coalescing)
|
||||
c.SpurrCoalescing = int64(n.spurr_coalescing)
|
||||
return c
|
||||
}
|
||||
|
||||
func perfstatcpuutil2cpuutil(n *C.perfstat_cpu_util_t) CPUUtil {
|
||||
var c CPUUtil
|
||||
|
||||
c.Version = int64(n.version)
|
||||
c.CpuID = C.GoString(&n.cpu_id[0])
|
||||
c.Entitlement = float32(n.entitlement)
|
||||
c.UserPct = float32(n.user_pct)
|
||||
c.KernPct = float32(n.kern_pct)
|
||||
c.IdlePct = float32(n.idle_pct)
|
||||
c.WaitPct = float32(n.wait_pct)
|
||||
c.PhysicalBusy = float32(n.physical_busy)
|
||||
c.PhysicalConsumed = float32(n.physical_consumed)
|
||||
c.FreqPct = float32(n.freq_pct)
|
||||
c.EntitlementPct = float32(n.entitlement_pct)
|
||||
c.BusyPct = float32(n.busy_pct)
|
||||
c.IdleDonatedPct = float32(n.idle_donated_pct)
|
||||
c.BusyDonatedPct = float32(n.busy_donated_pct)
|
||||
c.IdleStolenPct = float32(n.idle_stolen_pct)
|
||||
c.BusyStolenPct = float32(n.busy_stolen_pct)
|
||||
c.LUserPct = float32(n.l_user_pct)
|
||||
c.LKernPct = float32(n.l_kern_pct)
|
||||
c.LIdlePct = float32(n.l_idle_pct)
|
||||
c.LWaitPct = float32(n.l_wait_pct)
|
||||
c.DeltaTime = int64(n.delta_time)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func perfstatdisktotal2disktotal(n C.perfstat_disk_total_t) DiskTotal {
|
||||
var d DiskTotal
|
||||
|
||||
d.Number = int32(n.number)
|
||||
d.Size = int64(n.size)
|
||||
d.Free = int64(n.free)
|
||||
d.XRate = int64(n.xrate)
|
||||
d.Xfers = int64(n.xfers)
|
||||
d.Wblks = int64(n.wblks)
|
||||
d.Rblks = int64(n.rblks)
|
||||
d.Time = int64(n.time)
|
||||
d.Version = int64(n.version)
|
||||
d.Rserv = int64(n.rserv)
|
||||
d.MinRserv = int64(n.min_rserv)
|
||||
d.MaxRserv = int64(n.max_rserv)
|
||||
d.RTimeOut = int64(n.rtimeout)
|
||||
d.RFailed = int64(n.rfailed)
|
||||
d.Wserv = int64(n.wserv)
|
||||
d.MinWserv = int64(n.min_wserv)
|
||||
d.MaxWserv = int64(n.max_wserv)
|
||||
d.WTimeOut = int64(n.wtimeout)
|
||||
d.WFailed = int64(n.wfailed)
|
||||
d.WqDepth = int64(n.wq_depth)
|
||||
d.WqTime = int64(n.wq_time)
|
||||
d.WqMinTime = int64(n.wq_min_time)
|
||||
d.WqMaxTime = int64(n.wq_max_time)
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func perfstatdiskadapter2diskadapter(n *C.perfstat_diskadapter_t) DiskAdapter {
|
||||
var d DiskAdapter
|
||||
|
||||
d.Name = C.GoString(&n.name[0])
|
||||
d.Description = C.GoString(&n.description[0])
|
||||
d.Number = int32(n.number)
|
||||
d.Size = int64(n.size)
|
||||
d.Free = int64(n.free)
|
||||
d.XRate = int64(n.xrate)
|
||||
d.Xfers = int64(n.xfers)
|
||||
d.Rblks = int64(n.rblks)
|
||||
d.Wblks = int64(n.wblks)
|
||||
d.Time = int64(n.time)
|
||||
d.Version = int64(n.version)
|
||||
d.AdapterType = int64(n.adapter_type)
|
||||
d.DkBSize = int64(n.dk_bsize)
|
||||
d.DkRserv = int64(n.dk_rserv)
|
||||
d.DkWserv = int64(n.dk_wserv)
|
||||
d.MinRserv = int64(n.min_rserv)
|
||||
d.MaxRserv = int64(n.max_rserv)
|
||||
d.MinWserv = int64(n.min_wserv)
|
||||
d.MaxWserv = int64(n.max_wserv)
|
||||
d.WqDepth = int64(n.wq_depth)
|
||||
d.WqSampled = int64(n.wq_sampled)
|
||||
d.WqTime = int64(n.wq_time)
|
||||
d.WqMinTime = int64(n.wq_min_time)
|
||||
d.WqMaxTime = int64(n.wq_max_time)
|
||||
d.QFull = int64(n.q_full)
|
||||
d.QSampled = int64(n.q_sampled)
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func perfstatpartitionconfig2partitionconfig(n C.perfstat_partition_config_t) PartitionConfig {
|
||||
var p PartitionConfig
|
||||
p.Version = int64(n.version)
|
||||
p.Name = C.GoString(&n.partitionname[0])
|
||||
p.Node = C.GoString(&n.nodename[0])
|
||||
p.Conf.SmtCapable = (n.conf[0] & (1 << 7)) > 0
|
||||
p.Conf.SmtEnabled = (n.conf[0] & (1 << 6)) > 0
|
||||
p.Conf.LparCapable = (n.conf[0] & (1 << 5)) > 0
|
||||
p.Conf.LparEnabled = (n.conf[0] & (1 << 4)) > 0
|
||||
p.Conf.SharedCapable = (n.conf[0] & (1 << 3)) > 0
|
||||
p.Conf.SharedEnabled = (n.conf[0] & (1 << 2)) > 0
|
||||
p.Conf.DLparCapable = (n.conf[0] & (1 << 1)) > 0
|
||||
p.Conf.Capped = (n.conf[0] & (1 << 0)) > 0
|
||||
p.Conf.Kernel64bit = (n.conf[1] & (1 << 7)) > 0
|
||||
p.Conf.PoolUtilAuthority = (n.conf[1] & (1 << 6)) > 0
|
||||
p.Conf.DonateCapable = (n.conf[1] & (1 << 5)) > 0
|
||||
p.Conf.DonateEnabled = (n.conf[1] & (1 << 4)) > 0
|
||||
p.Conf.AmsCapable = (n.conf[1] & (1 << 3)) > 0
|
||||
p.Conf.AmsEnabled = (n.conf[1] & (1 << 2)) > 0
|
||||
p.Conf.PowerSave = (n.conf[1] & (1 << 1)) > 0
|
||||
p.Conf.AmeEnabled = (n.conf[1] & (1 << 0)) > 0
|
||||
p.Conf.SharedExtended = (n.conf[2] & (1 << 7)) > 0
|
||||
p.Number = int32(n.partitionnum)
|
||||
p.GroupID = int32(n.groupid)
|
||||
p.ProcessorFamily = C.GoString(&n.processorFamily[0])
|
||||
p.ProcessorModel = C.GoString(&n.processorModel[0])
|
||||
p.MachineID = C.GoString(&n.machineID[0])
|
||||
p.ProcessorMhz = float64(C.get_partition_mhz(n))
|
||||
p.NumProcessors.Online = int64(n.numProcessors.online)
|
||||
p.NumProcessors.Max = int64(n.numProcessors.max)
|
||||
p.NumProcessors.Min = int64(n.numProcessors.min)
|
||||
p.NumProcessors.Desired = int64(n.numProcessors.desired)
|
||||
p.OSName = C.GoString(&n.OSName[0])
|
||||
p.OSVersion = C.GoString(&n.OSVersion[0])
|
||||
p.OSBuild = C.GoString(&n.OSBuild[0])
|
||||
p.LCpus = int32(n.lcpus)
|
||||
p.SmtThreads = int32(n.smtthreads)
|
||||
p.Drives = int32(n.drives)
|
||||
p.NetworkAdapters = int32(n.nw_adapters)
|
||||
p.CpuCap.Online = int64(n.cpucap.online)
|
||||
p.CpuCap.Max = int64(n.cpucap.max)
|
||||
p.CpuCap.Min = int64(n.cpucap.min)
|
||||
p.CpuCap.Desired = int64(n.cpucap.desired)
|
||||
p.Weightage = int32(n.cpucap_weightage)
|
||||
p.EntCapacity = int32(n.entitled_proc_capacity)
|
||||
p.VCpus.Online = int64(n.vcpus.online)
|
||||
p.VCpus.Max = int64(n.vcpus.max)
|
||||
p.VCpus.Min = int64(n.vcpus.min)
|
||||
p.VCpus.Desired = int64(n.vcpus.desired)
|
||||
p.PoolID = int32(n.processor_poolid)
|
||||
p.ActiveCpusInPool = int32(n.activecpusinpool)
|
||||
p.PoolWeightage = int32(n.cpupool_weightage)
|
||||
p.SharedPCpu = int32(n.sharedpcpu)
|
||||
p.MaxPoolCap = int32(n.maxpoolcap)
|
||||
p.EntPoolCap = int32(n.entpoolcap)
|
||||
p.Mem.Online = int64(n.mem.online)
|
||||
p.Mem.Max = int64(n.mem.max)
|
||||
p.Mem.Min = int64(n.mem.min)
|
||||
p.Mem.Desired = int64(n.mem.desired)
|
||||
p.MemWeightage = int32(n.mem_weightage)
|
||||
p.TotalIOMemoryEntitlement = int64(n.totiomement)
|
||||
p.MemPoolID = int32(n.mempoolid)
|
||||
p.HyperPgSize = int64(n.hyperpgsize)
|
||||
p.ExpMem.Online = int64(n.exp_mem.online)
|
||||
p.ExpMem.Max = int64(n.exp_mem.max)
|
||||
p.ExpMem.Min = int64(n.exp_mem.min)
|
||||
p.ExpMem.Desired = int64(n.exp_mem.desired)
|
||||
p.TargetMemExpFactor = int64(n.targetmemexpfactor)
|
||||
p.TargetMemExpSize = int64(n.targetmemexpsize)
|
||||
p.SubProcessorMode = int32(n.subprocessor_mode)
|
||||
return p
|
||||
}
|
||||
|
||||
func perfstatmemorytotal2memorytotal(n C.perfstat_memory_total_t) MemoryTotal {
|
||||
var m MemoryTotal
|
||||
m.VirtualTotal = int64(n.virt_total)
|
||||
m.RealTotal = int64(n.real_total)
|
||||
m.RealFree = int64(n.real_free)
|
||||
m.RealPinned = int64(n.real_pinned)
|
||||
m.RealInUse = int64(n.real_inuse)
|
||||
m.BadPages = int64(n.pgbad)
|
||||
m.PageFaults = int64(n.pgexct)
|
||||
m.PageIn = int64(n.pgins)
|
||||
m.PageOut = int64(n.pgouts)
|
||||
m.PgSpIn = int64(n.pgspins)
|
||||
m.PgSpOut = int64(n.pgspouts)
|
||||
m.Scans = int64(n.scans)
|
||||
m.Cycles = int64(n.cycles)
|
||||
m.PgSteals = int64(n.pgsteals)
|
||||
m.NumPerm = int64(n.numperm)
|
||||
m.PgSpTotal = int64(n.pgsp_total)
|
||||
m.PgSpFree = int64(n.pgsp_free)
|
||||
m.PgSpRsvd = int64(n.pgsp_rsvd)
|
||||
m.RealSystem = int64(n.real_system)
|
||||
m.RealUser = int64(n.real_user)
|
||||
m.RealProcess = int64(n.real_process)
|
||||
m.VirtualActive = int64(n.virt_active)
|
||||
m.IOME = int64(n.iome)
|
||||
m.IOMU = int64(n.iomu)
|
||||
m.IOHWM = int64(n.iohwm)
|
||||
m.PMem = int64(n.pmem)
|
||||
m.CompressedTotal = int64(n.comprsd_total)
|
||||
m.CompressedWSegPg = int64(n.comprsd_wseg_pgs)
|
||||
m.CPgIn = int64(n.cpgins)
|
||||
m.CPgOut = int64(n.cpgouts)
|
||||
m.TrueSize = int64(n.true_size)
|
||||
m.ExpandedMemory = int64(n.expanded_memory)
|
||||
m.CompressedWSegSize = int64(n.comprsd_wseg_size)
|
||||
m.TargetCPoolSize = int64(n.target_cpool_size)
|
||||
m.MaxCPoolSize = int64(n.max_cpool_size)
|
||||
m.MinUCPoolSize = int64(n.min_ucpool_size)
|
||||
m.CPoolSize = int64(n.cpool_size)
|
||||
m.UCPoolSize = int64(n.ucpool_size)
|
||||
m.CPoolInUse = int64(n.cpool_inuse)
|
||||
m.UCPoolInUse = int64(n.ucpool_inuse)
|
||||
m.Version = int64(n.version)
|
||||
m.RealAvailable = int64(n.real_avail)
|
||||
m.BytesCoalesced = int64(n.bytes_coalesced)
|
||||
m.BytesCoalescedMemPool = int64(n.bytes_coalesced_mempool)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func perfstatnetinterfacetotal2netifacetotal(n C.perfstat_netinterface_total_t) NetIfaceTotal {
|
||||
var i NetIfaceTotal
|
||||
|
||||
i.Number = int32(n.number)
|
||||
i.IPackets = int64(n.ipackets)
|
||||
i.IBytes = int64(n.ibytes)
|
||||
i.IErrors = int64(n.ierrors)
|
||||
i.OPackets = int64(n.opackets)
|
||||
i.OBytes = int64(n.obytes)
|
||||
i.OErrors = int64(n.oerrors)
|
||||
i.Collisions = int64(n.collisions)
|
||||
i.XmitDrops = int64(n.xmitdrops)
|
||||
i.Version = int64(n.version)
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func perfstatdisk2disk(n *C.perfstat_disk_t) Disk {
|
||||
var d Disk
|
||||
|
||||
d.Name = C.GoString(&n.name[0])
|
||||
d.Description = C.GoString(&n.description[0])
|
||||
d.VGName = C.GoString(&n.vgname[0])
|
||||
d.Size = int64(n.size)
|
||||
d.Free = int64(n.free)
|
||||
d.BSize = int64(n.bsize)
|
||||
d.XRate = int64(n.xrate)
|
||||
d.Xfers = int64(n.xfers)
|
||||
d.Wblks = int64(n.wblks)
|
||||
d.Rblks = int64(n.rblks)
|
||||
d.QDepth = int64(n.qdepth)
|
||||
d.Time = int64(n.time)
|
||||
d.Adapter = C.GoString(&n.adapter[0])
|
||||
d.PathsCount = int32(n.paths_count)
|
||||
d.QFull = int64(n.q_full)
|
||||
d.Rserv = int64(n.rserv)
|
||||
d.RTimeOut = int64(n.rtimeout)
|
||||
d.Rfailed = int64(n.rfailed)
|
||||
d.MinRserv = int64(n.min_rserv)
|
||||
d.MaxRserv = int64(n.max_rserv)
|
||||
d.Wserv = int64(n.wserv)
|
||||
d.WTimeOut = int64(n.wtimeout)
|
||||
d.Wfailed = int64(n.wfailed)
|
||||
d.MinWserv = int64(n.min_wserv)
|
||||
d.MaxWserv = int64(n.max_wserv)
|
||||
d.WqDepth = int64(n.wq_depth)
|
||||
d.WqSampled = int64(n.wq_sampled)
|
||||
d.WqTime = int64(n.wq_time)
|
||||
d.WqMinTime = int64(n.wq_min_time)
|
||||
d.WqMaxTime = int64(n.wq_max_time)
|
||||
d.QSampled = int64(n.q_sampled)
|
||||
d.Version = int64(n.version)
|
||||
d.PseudoDisk = (n.dk_type[0] & (1 << 7)) > 0
|
||||
d.VTDisk = (n.dk_type[0] & (1 << 6)) > 0
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func perfstatdiskpath2diskpath(n *C.perfstat_diskpath_t) DiskPath {
|
||||
var d DiskPath
|
||||
|
||||
d.Name = C.GoString(&n.name[0])
|
||||
d.XRate = int64(n.xrate)
|
||||
d.Xfers = int64(n.xfers)
|
||||
d.Rblks = int64(n.rblks)
|
||||
d.Wblks = int64(n.wblks)
|
||||
d.Time = int64(n.time)
|
||||
d.Adapter = C.GoString(&n.adapter[0])
|
||||
d.QFull = int64(n.q_full)
|
||||
d.Rserv = int64(n.rserv)
|
||||
d.RTimeOut = int64(n.rtimeout)
|
||||
d.Rfailed = int64(n.rfailed)
|
||||
d.MinRserv = int64(n.min_rserv)
|
||||
d.MaxRserv = int64(n.max_rserv)
|
||||
d.Wserv = int64(n.wserv)
|
||||
d.WTimeOut = int64(n.wtimeout)
|
||||
d.Wfailed = int64(n.wfailed)
|
||||
d.MinWserv = int64(n.min_wserv)
|
||||
d.MaxWserv = int64(n.max_wserv)
|
||||
d.WqDepth = int64(n.wq_depth)
|
||||
d.WqSampled = int64(n.wq_sampled)
|
||||
d.WqTime = int64(n.wq_time)
|
||||
d.WqMinTime = int64(n.wq_min_time)
|
||||
d.WqMaxTime = int64(n.wq_max_time)
|
||||
d.QSampled = int64(n.q_sampled)
|
||||
d.Version = int64(n.version)
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func perfstatfcstat2fcadapter(n *C.perfstat_fcstat_t) FCAdapter {
|
||||
var f FCAdapter
|
||||
|
||||
f.Version = int64(n.version)
|
||||
f.Name = C.GoString(&n.name[0])
|
||||
f.State = int32(n.state)
|
||||
f.InputRequests = int64(n.InputRequests)
|
||||
f.OutputRequests = int64(n.OutputRequests)
|
||||
f.InputBytes = int64(n.InputBytes)
|
||||
f.OutputBytes = int64(n.OutputBytes)
|
||||
f.EffMaxTransfer = int64(n.EffMaxTransfer)
|
||||
f.NoDMAResourceCnt = int64(n.NoDMAResourceCnt)
|
||||
f.NoCmdResourceCnt = int64(n.NoCmdResourceCnt)
|
||||
f.AttentionType = int32(n.AttentionType)
|
||||
f.SecondsSinceLastReset = int64(n.SecondsSinceLastReset)
|
||||
f.TxFrames = int64(n.TxFrames)
|
||||
f.TxWords = int64(n.TxWords)
|
||||
f.RxFrames = int64(n.RxFrames)
|
||||
f.RxWords = int64(n.RxWords)
|
||||
f.LIPCount = int64(n.LIPCount)
|
||||
f.NOSCount = int64(n.NOSCount)
|
||||
f.ErrorFrames = int64(n.ErrorFrames)
|
||||
f.DumpedFrames = int64(n.DumpedFrames)
|
||||
f.LinkFailureCount = int64(n.LinkFailureCount)
|
||||
f.LossofSyncCount = int64(n.LossofSyncCount)
|
||||
f.LossofSignal = int64(n.LossofSignal)
|
||||
f.PrimitiveSeqProtocolErrCount = int64(n.PrimitiveSeqProtocolErrCount)
|
||||
f.InvalidTxWordCount = int64(n.InvalidTxWordCount)
|
||||
f.InvalidCRCCount = int64(n.InvalidCRCCount)
|
||||
f.PortFcId = int64(n.PortFcId)
|
||||
f.PortSpeed = int64(n.PortSpeed)
|
||||
f.PortType = C.GoString(&n.PortType[0])
|
||||
f.PortWWN = int64(n.PortWWN)
|
||||
f.PortSupportedSpeed = int64(n.PortSupportedSpeed)
|
||||
f.AdapterType = int(n.adapter_type)
|
||||
f.VfcName = C.GoString(&n.vfc_name[0])
|
||||
f.ClientPartName = C.GoString(&n.client_part_name[0])
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func perfstatlogicalvolume2logicalvolume(n *C.perfstat_logicalvolume_t) LogicalVolume {
|
||||
var l LogicalVolume
|
||||
|
||||
l.Name = C.GoString(&n.name[0])
|
||||
l.VGName = C.GoString(&n.vgname[0])
|
||||
l.OpenClose = int64(n.open_close)
|
||||
l.State = int64(n.state)
|
||||
l.MirrorPolicy = int64(n.mirror_policy)
|
||||
l.MirrorWriteConsistency = int64(n.mirror_write_consistency)
|
||||
l.WriteVerify = int64(n.write_verify)
|
||||
l.PPsize = int64(n.ppsize)
|
||||
l.LogicalPartitions = int64(n.logical_partitions)
|
||||
l.Mirrors = int32(n.mirrors)
|
||||
l.IOCnt = int64(n.iocnt)
|
||||
l.KBReads = int64(n.kbreads)
|
||||
l.KBWrites = int64(n.kbwrites)
|
||||
l.Version = int64(n.version)
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
func perfstatvolumegroup2volumegroup(n *C.perfstat_volumegroup_t) VolumeGroup {
|
||||
var v VolumeGroup
|
||||
|
||||
v.Name = C.GoString(&n.name[0])
|
||||
v.TotalDisks = int64(n.total_disks)
|
||||
v.ActiveDisks = int64(n.active_disks)
|
||||
v.TotalLogicalVolumes = int64(n.total_logical_volumes)
|
||||
v.OpenedLogicalVolumes = int64(n.opened_logical_volumes)
|
||||
v.IOCnt = int64(n.iocnt)
|
||||
v.KBReads = int64(n.kbreads)
|
||||
v.KBWrites = int64(n.kbwrites)
|
||||
v.Version = int64(n.version)
|
||||
v.VariedState = int(n.variedState)
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func perfstatmemorypage2memorypage(n *C.perfstat_memory_page_t) MemoryPage {
|
||||
var m MemoryPage
|
||||
|
||||
m.PSize = int64(n.psize)
|
||||
m.RealTotal = int64(n.real_total)
|
||||
m.RealFree = int64(n.real_free)
|
||||
m.RealPinned = int64(n.real_pinned)
|
||||
m.RealInUse = int64(n.real_inuse)
|
||||
m.PgExct = int64(n.pgexct)
|
||||
m.PgIns = int64(n.pgins)
|
||||
m.PgOuts = int64(n.pgouts)
|
||||
m.PgSpIns = int64(n.pgspins)
|
||||
m.PgSpOuts = int64(n.pgspouts)
|
||||
m.Scans = int64(n.scans)
|
||||
m.Cycles = int64(n.cycles)
|
||||
m.PgSteals = int64(n.pgsteals)
|
||||
m.NumPerm = int64(n.numperm)
|
||||
m.NumPgSp = int64(n.numpgsp)
|
||||
m.RealSystem = int64(n.real_system)
|
||||
m.RealUser = int64(n.real_user)
|
||||
m.RealProcess = int64(n.real_process)
|
||||
m.VirtActive = int64(n.virt_active)
|
||||
m.ComprsdTotal = int64(n.comprsd_total)
|
||||
m.ComprsdWsegPgs = int64(n.comprsd_wseg_pgs)
|
||||
m.CPgIns = int64(n.cpgins)
|
||||
m.CPgOuts = int64(n.cpgouts)
|
||||
m.CPoolInUse = int64(n.cpool_inuse)
|
||||
m.UCPoolSize = int64(n.ucpool_size)
|
||||
m.ComprsdWsegSize = int64(n.comprsd_wseg_size)
|
||||
m.Version = int64(n.version)
|
||||
m.RealAvail = int64(n.real_avail)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func perfstatnetbuffer2netbuffer(n *C.perfstat_netbuffer_t) NetBuffer {
|
||||
var b NetBuffer
|
||||
|
||||
b.Name = C.GoString(&n.name[0])
|
||||
b.InUse = int64(n.inuse)
|
||||
b.Calls = int64(n.calls)
|
||||
b.Delayed = int64(n.delayed)
|
||||
b.Free = int64(n.free)
|
||||
b.Failed = int64(n.failed)
|
||||
b.HighWatermark = int64(n.highwatermark)
|
||||
b.Freed = int64(n.freed)
|
||||
b.Version = int64(n.version)
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
func perfstatnetinterface2netiface(n *C.perfstat_netinterface_t) NetIface {
|
||||
var i NetIface
|
||||
|
||||
i.Name = C.GoString(&n.name[0])
|
||||
i.Description = C.GoString(&n.description[0])
|
||||
i.Type = uint8(n._type)
|
||||
i.MTU = int64(n.mtu)
|
||||
i.IPackets = int64(n.ipackets)
|
||||
i.IBytes = int64(n.ibytes)
|
||||
i.IErrors = int64(n.ierrors)
|
||||
i.OPackets = int64(n.opackets)
|
||||
i.OBytes = int64(n.obytes)
|
||||
i.OErrors = int64(n.oerrors)
|
||||
i.Collisions = int64(n.collisions)
|
||||
i.Bitrate = int64(n.bitrate)
|
||||
i.XmitDrops = int64(n.xmitdrops)
|
||||
i.Version = int64(n.version)
|
||||
i.IfIqDrops = int64(n.if_iqdrops)
|
||||
i.IfArpDrops = int64(n.if_arpdrops)
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func perfstatnetadapter2netadapter(n *C.perfstat_netadapter_t) NetAdapter {
|
||||
var i NetAdapter
|
||||
|
||||
i.Version = int64(n.version)
|
||||
i.Name = C.GoString(&n.name[0])
|
||||
i.TxPackets = int64(n.tx_packets)
|
||||
i.TxBytes = int64(n.tx_bytes)
|
||||
i.TxInterrupts = int64(n.tx_interrupts)
|
||||
i.TxErrors = int64(n.tx_errors)
|
||||
i.TxPacketsDropped = int64(n.tx_packets_dropped)
|
||||
i.TxQueueSize = int64(n.tx_queue_size)
|
||||
i.TxQueueLen = int64(n.tx_queue_len)
|
||||
i.TxQueueOverflow = int64(n.tx_queue_overflow)
|
||||
i.TxBroadcastPackets = int64(n.tx_broadcast_packets)
|
||||
i.TxMulticastPackets = int64(n.tx_multicast_packets)
|
||||
i.TxCarrierSense = int64(n.tx_carrier_sense)
|
||||
i.TxDMAUnderrun = int64(n.tx_DMA_underrun)
|
||||
i.TxLostCTSErrors = int64(n.tx_lost_CTS_errors)
|
||||
i.TxMaxCollisionErrors = int64(n.tx_max_collision_errors)
|
||||
i.TxLateCollisionErrors = int64(n.tx_late_collision_errors)
|
||||
i.TxDeferred = int64(n.tx_deferred)
|
||||
i.TxTimeoutErrors = int64(n.tx_timeout_errors)
|
||||
i.TxSingleCollisionCount = int64(n.tx_single_collision_count)
|
||||
i.TxMultipleCollisionCount = int64(n.tx_multiple_collision_count)
|
||||
i.RxPackets = int64(n.rx_packets)
|
||||
i.RxBytes = int64(n.rx_bytes)
|
||||
i.RxInterrupts = int64(n.rx_interrupts)
|
||||
i.RxErrors = int64(n.rx_errors)
|
||||
i.RxPacketsDropped = int64(n.rx_packets_dropped)
|
||||
i.RxBadPackets = int64(n.rx_bad_packets)
|
||||
i.RxMulticastPackets = int64(n.rx_multicast_packets)
|
||||
i.RxBroadcastPackets = int64(n.rx_broadcast_packets)
|
||||
i.RxCRCErrors = int64(n.rx_CRC_errors)
|
||||
i.RxDMAOverrun = int64(n.rx_DMA_overrun)
|
||||
i.RxAlignmentErrors = int64(n.rx_alignment_errors)
|
||||
i.RxNoResourceErrors = int64(n.rx_noresource_errors)
|
||||
i.RxCollisionErrors = int64(n.rx_collision_errors)
|
||||
i.RxPacketTooShortErrors = int64(n.rx_packet_tooshort_errors)
|
||||
i.RxPacketTooLongErrors = int64(n.rx_packet_toolong_errors)
|
||||
i.RxPacketDiscardedByAdapter = int64(n.rx_packets_discardedbyadapter)
|
||||
i.AdapterType = int32(n.adapter_type)
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func perfstatpagingspace2pagingspace(n *C.perfstat_pagingspace_t) PagingSpace {
|
||||
var i PagingSpace
|
||||
|
||||
i.Name = C.GoString(&n.name[0])
|
||||
i.Type = uint8(n._type)
|
||||
i.VGName = C.GoString(C.get_ps_vgname(n))
|
||||
i.Hostname = C.GoString(C.get_ps_hostname(n))
|
||||
i.Filename = C.GoString(C.get_ps_filename(n))
|
||||
i.LPSize = int64(n.lp_size)
|
||||
i.MBSize = int64(n.mb_size)
|
||||
i.MBUsed = int64(n.mb_used)
|
||||
i.IOPending = int64(n.io_pending)
|
||||
i.Active = uint8(n.active)
|
||||
i.Automatic = uint8(n.automatic)
|
||||
i.Version = int64(n.version)
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func perfstatprocess2process(n *C.perfstat_process_t) Process {
|
||||
var i Process
|
||||
|
||||
i.Version = int64(n.version)
|
||||
i.PID = int64(n.pid)
|
||||
i.ProcessName = C.GoString(&n.proc_name[0])
|
||||
i.Priority = int32(n.proc_priority)
|
||||
i.NumThreads = int64(n.num_threads)
|
||||
i.UID = int64(n.proc_uid)
|
||||
i.ClassID = int64(n.proc_classid)
|
||||
i.Size = int64(n.proc_size)
|
||||
i.RealMemData = int64(n.proc_real_mem_data)
|
||||
i.RealMemText = int64(n.proc_real_mem_text)
|
||||
i.VirtMemData = int64(n.proc_virt_mem_data)
|
||||
i.VirtMemText = int64(n.proc_virt_mem_text)
|
||||
i.SharedLibDataSize = int64(n.shared_lib_data_size)
|
||||
i.HeapSize = int64(n.heap_size)
|
||||
i.RealInUse = int64(n.real_inuse)
|
||||
i.VirtInUse = int64(n.virt_inuse)
|
||||
i.Pinned = int64(n.pinned)
|
||||
i.PgSpInUse = int64(n.pgsp_inuse)
|
||||
i.FilePages = int64(n.filepages)
|
||||
i.RealInUseMap = int64(n.real_inuse_map)
|
||||
i.VirtInUseMap = int64(n.virt_inuse_map)
|
||||
i.PinnedInUseMap = int64(n.pinned_inuse_map)
|
||||
i.UCpuTime = float64(n.ucpu_time)
|
||||
i.SCpuTime = float64(n.scpu_time)
|
||||
i.LastTimeBase = int64(n.last_timebase)
|
||||
i.InBytes = int64(n.inBytes)
|
||||
i.OutBytes = int64(n.outBytes)
|
||||
i.InOps = int64(n.inOps)
|
||||
i.OutOps = int64(n.outOps)
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func perfstatthread2thread(n *C.perfstat_thread_t) Thread {
|
||||
var i Thread
|
||||
|
||||
i.TID = int64(n.tid)
|
||||
i.PID = int64(n.pid)
|
||||
i.CpuID = int64(n.cpuid)
|
||||
i.UCpuTime = float64(n.ucpu_time)
|
||||
i.SCpuTime = float64(n.scpu_time)
|
||||
i.LastTimeBase = int64(n.last_timebase)
|
||||
i.Version = int64(n.version)
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
func fsinfo2filesystem(n *C.struct_fsinfo) FileSystem {
|
||||
var i FileSystem
|
||||
|
||||
i.Device = C.GoString(n.devname)
|
||||
i.MountPoint = C.GoString(n.fsname)
|
||||
i.FSType = int(n.fstype)
|
||||
i.Flags = int(n.flags)
|
||||
i.TotalBlocks = int64(n.totalblks)
|
||||
i.FreeBlocks = int64(n.freeblks)
|
||||
i.TotalInodes = int64(n.totalinodes)
|
||||
i.FreeInodes = int64(n.freeinodes)
|
||||
|
||||
return i
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func PartitionStat() (*PartitionConfig, error) {
|
||||
var part C.perfstat_partition_config_t
|
||||
|
||||
rc := C.perfstat_partition_config(nil, &part, C.sizeof_perfstat_partition_config_t, 1)
|
||||
if rc != 1 {
|
||||
return nil, fmt.Errorf("perfstat_partition_config() error")
|
||||
}
|
||||
p := perfstatpartitionconfig2partitionconfig(part)
|
||||
return &p, nil
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func LogicalVolumeStat() ([]LogicalVolume, error) {
|
||||
var lv *C.perfstat_logicalvolume_t
|
||||
var lvname C.perfstat_id_t
|
||||
|
||||
numlvs := C.perfstat_logicalvolume(nil, nil, C.sizeof_perfstat_logicalvolume_t, 0)
|
||||
if numlvs <= 0 {
|
||||
return nil, fmt.Errorf("perfstat_logicalvolume() error")
|
||||
}
|
||||
|
||||
lv_len := C.sizeof_perfstat_logicalvolume_t * C.ulong(numlvs)
|
||||
lv = (*C.perfstat_logicalvolume_t)(C.malloc(lv_len))
|
||||
defer C.free(unsafe.Pointer(lv))
|
||||
C.strcpy(&lvname.name[0], C.CString(""))
|
||||
r := C.perfstat_logicalvolume(&lvname, lv, C.sizeof_perfstat_logicalvolume_t, numlvs)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_logicalvolume() error")
|
||||
}
|
||||
lvs := make([]LogicalVolume, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
l := C.get_logicalvolume_stat(lv, C.int(i))
|
||||
if l != nil {
|
||||
lvs[i] = perfstatlogicalvolume2logicalvolume(l)
|
||||
}
|
||||
}
|
||||
return lvs, nil
|
||||
}
|
||||
|
||||
func VolumeGroupStat() ([]VolumeGroup, error) {
|
||||
var vg *C.perfstat_volumegroup_t
|
||||
var vgname C.perfstat_id_t
|
||||
|
||||
numvgs := C.perfstat_volumegroup(nil, nil, C.sizeof_perfstat_volumegroup_t, 0)
|
||||
if numvgs <= 0 {
|
||||
return nil, fmt.Errorf("perfstat_volumegroup() error")
|
||||
}
|
||||
|
||||
vg_len := C.sizeof_perfstat_volumegroup_t * C.ulong(numvgs)
|
||||
vg = (*C.perfstat_volumegroup_t)(C.malloc(vg_len))
|
||||
defer C.free(unsafe.Pointer(vg))
|
||||
C.strcpy(&vgname.name[0], C.CString(""))
|
||||
r := C.perfstat_volumegroup(&vgname, vg, C.sizeof_perfstat_volumegroup_t, numvgs)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_volumegroup() error")
|
||||
}
|
||||
vgs := make([]VolumeGroup, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
v := C.get_volumegroup_stat(vg, C.int(i))
|
||||
if v != nil {
|
||||
vgs[i] = perfstatvolumegroup2volumegroup(v)
|
||||
}
|
||||
}
|
||||
return vgs, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func MemoryTotalStat() (*MemoryTotal, error) {
|
||||
var memory C.perfstat_memory_total_t
|
||||
|
||||
rc := C.perfstat_memory_total(nil, &memory, C.sizeof_perfstat_memory_total_t, 1)
|
||||
if rc != 1 {
|
||||
return nil, fmt.Errorf("perfstat_memory_total() error")
|
||||
}
|
||||
m := perfstatmemorytotal2memorytotal(memory)
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
func MemoryPageStat() ([]MemoryPage, error) {
|
||||
var mempage *C.perfstat_memory_page_t
|
||||
var fps C.perfstat_psize_t
|
||||
|
||||
numps := C.perfstat_memory_page(nil, nil, C.sizeof_perfstat_memory_page_t, 0)
|
||||
if numps < 1 {
|
||||
return nil, fmt.Errorf("perfstat_memory_page() error")
|
||||
}
|
||||
|
||||
mp_len := C.sizeof_perfstat_memory_page_t * C.ulong(numps)
|
||||
mempage = (*C.perfstat_memory_page_t)(C.malloc(mp_len))
|
||||
defer C.free(unsafe.Pointer(mempage))
|
||||
fps.psize = C.FIRST_PSIZE
|
||||
r := C.perfstat_memory_page(&fps, mempage, C.sizeof_perfstat_memory_page_t, numps)
|
||||
if r < 1 {
|
||||
return nil, fmt.Errorf("perfstat_memory_page() error")
|
||||
}
|
||||
ps := make([]MemoryPage, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
p := C.get_memory_page_stat(mempage, C.int(i))
|
||||
if p != nil {
|
||||
ps[i] = perfstatmemorypage2memorypage(p)
|
||||
}
|
||||
}
|
||||
return ps, nil
|
||||
}
|
||||
|
||||
func PagingSpaceStat() ([]PagingSpace, error) {
|
||||
var pspace *C.perfstat_pagingspace_t
|
||||
var fps C.perfstat_id_t
|
||||
|
||||
numps := C.perfstat_pagingspace(nil, nil, C.sizeof_perfstat_pagingspace_t, 0)
|
||||
if numps <= 0 {
|
||||
return nil, fmt.Errorf("perfstat_pagingspace() error")
|
||||
}
|
||||
|
||||
ps_len := C.sizeof_perfstat_pagingspace_t * C.ulong(numps)
|
||||
pspace = (*C.perfstat_pagingspace_t)(C.malloc(ps_len))
|
||||
defer C.free(unsafe.Pointer(pspace))
|
||||
C.strcpy(&fps.name[0], C.CString(C.FIRST_PAGINGSPACE))
|
||||
r := C.perfstat_pagingspace(&fps, pspace, C.sizeof_perfstat_pagingspace_t, numps)
|
||||
if r < 1 {
|
||||
return nil, fmt.Errorf("perfstat_pagingspace() error")
|
||||
}
|
||||
ps := make([]PagingSpace, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
p := C.get_pagingspace_stat(pspace, C.int(i))
|
||||
if p != nil {
|
||||
ps[i] = perfstatpagingspace2pagingspace(p)
|
||||
}
|
||||
}
|
||||
return ps, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func NetIfaceTotalStat() (*NetIfaceTotal, error) {
|
||||
var nif C.perfstat_netinterface_total_t
|
||||
|
||||
rc := C.perfstat_netinterface_total(nil, &nif, C.sizeof_perfstat_netinterface_total_t, 1)
|
||||
if rc != 1 {
|
||||
return nil, fmt.Errorf("perfstat_netinterface_total() error")
|
||||
}
|
||||
n := perfstatnetinterfacetotal2netifacetotal(nif)
|
||||
return &n, nil
|
||||
}
|
||||
|
||||
func NetBufferStat() ([]NetBuffer, error) {
|
||||
var nbuf *C.perfstat_netbuffer_t
|
||||
var first C.perfstat_id_t
|
||||
|
||||
numbuf := C.perfstat_netbuffer(nil, nil, C.sizeof_perfstat_netbuffer_t, 0)
|
||||
if numbuf < 1 {
|
||||
return nil, fmt.Errorf("perfstat_netbuffer() error")
|
||||
}
|
||||
|
||||
nblen := C.sizeof_perfstat_netbuffer_t * C.ulong(numbuf)
|
||||
nbuf = (*C.perfstat_netbuffer_t)(C.malloc(nblen))
|
||||
defer C.free(unsafe.Pointer(nbuf))
|
||||
C.strcpy(&first.name[0], C.CString(C.FIRST_NETBUFFER))
|
||||
r := C.perfstat_netbuffer(&first, nbuf, C.sizeof_perfstat_netbuffer_t, numbuf)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_netbuffer() error")
|
||||
}
|
||||
nb := make([]NetBuffer, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
b := C.get_netbuffer_stat(nbuf, C.int(i))
|
||||
if b != nil {
|
||||
nb[i] = perfstatnetbuffer2netbuffer(b)
|
||||
}
|
||||
}
|
||||
return nb, nil
|
||||
}
|
||||
|
||||
func NetIfaceStat() ([]NetIface, error) {
|
||||
var nif *C.perfstat_netinterface_t
|
||||
var first C.perfstat_id_t
|
||||
|
||||
numif := C.perfstat_netinterface(nil, nil, C.sizeof_perfstat_netinterface_t, 0)
|
||||
if numif < 0 {
|
||||
return nil, fmt.Errorf("perfstat_netinterface() error")
|
||||
}
|
||||
if numif == 0 {
|
||||
return []NetIface{}, fmt.Errorf("no network interfaces found")
|
||||
}
|
||||
|
||||
iflen := C.sizeof_perfstat_netinterface_t * C.ulong(numif)
|
||||
nif = (*C.perfstat_netinterface_t)(C.malloc(iflen))
|
||||
defer C.free(unsafe.Pointer(nif))
|
||||
C.strcpy(&first.name[0], C.CString(C.FIRST_NETINTERFACE))
|
||||
r := C.perfstat_netinterface(&first, nif, C.sizeof_perfstat_netinterface_t, numif)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_netinterface() error")
|
||||
}
|
||||
ifs := make([]NetIface, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
b := C.get_netinterface_stat(nif, C.int(i))
|
||||
if b != nil {
|
||||
ifs[i] = perfstatnetinterface2netiface(b)
|
||||
}
|
||||
}
|
||||
return ifs, nil
|
||||
}
|
||||
|
||||
func NetAdapterStat() ([]NetAdapter, error) {
|
||||
var adapters *C.perfstat_netadapter_t
|
||||
var first C.perfstat_id_t
|
||||
|
||||
numad := C.perfstat_netadapter(nil, nil, C.sizeof_perfstat_netadapter_t, 0)
|
||||
if numad < 0 {
|
||||
return nil, fmt.Errorf("perfstat_netadater() error")
|
||||
}
|
||||
if numad == 0 {
|
||||
return []NetAdapter{}, fmt.Errorf("no network adapters found")
|
||||
}
|
||||
|
||||
adplen := C.sizeof_perfstat_netadapter_t * C.ulong(numad)
|
||||
adapters = (*C.perfstat_netadapter_t)(C.malloc(adplen))
|
||||
defer C.free(unsafe.Pointer(adapters))
|
||||
C.strcpy(&first.name[0], C.CString(C.FIRST_NETINTERFACE))
|
||||
r := C.perfstat_netadapter(&first, adapters, C.sizeof_perfstat_netadapter_t, numad)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_netadapter() error")
|
||||
}
|
||||
ads := make([]NetAdapter, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
b := C.get_netadapter_stat(adapters, C.int(i))
|
||||
if b != nil {
|
||||
ads[i] = perfstatnetadapter2netadapter(b)
|
||||
}
|
||||
}
|
||||
return ads, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -lperfstat
|
||||
|
||||
#include <libperfstat.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func ProcessStat() ([]Process, error) {
|
||||
var proc *C.perfstat_process_t
|
||||
var first C.perfstat_id_t
|
||||
|
||||
numproc := C.perfstat_process(nil, nil, C.sizeof_perfstat_process_t, 0)
|
||||
if numproc < 1 {
|
||||
return nil, fmt.Errorf("perfstat_process() error")
|
||||
}
|
||||
|
||||
plen := C.sizeof_perfstat_process_t * C.ulong(numproc)
|
||||
proc = (*C.perfstat_process_t)(C.malloc(plen))
|
||||
defer C.free(unsafe.Pointer(proc))
|
||||
C.strcpy(&first.name[0], C.CString(""))
|
||||
r := C.perfstat_process(&first, proc, C.sizeof_perfstat_process_t, numproc)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_process() error")
|
||||
}
|
||||
|
||||
ps := make([]Process, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
p := C.get_process_stat(proc, C.int(i))
|
||||
if p != nil {
|
||||
ps[i] = perfstatprocess2process(p)
|
||||
}
|
||||
}
|
||||
return ps, nil
|
||||
}
|
||||
|
||||
func ThreadStat() ([]Thread, error) {
|
||||
var thread *C.perfstat_thread_t
|
||||
var first C.perfstat_id_t
|
||||
|
||||
numthr := C.perfstat_thread(nil, nil, C.sizeof_perfstat_thread_t, 0)
|
||||
if numthr < 1 {
|
||||
return nil, fmt.Errorf("perfstat_thread() error")
|
||||
}
|
||||
|
||||
thlen := C.sizeof_perfstat_thread_t * C.ulong(numthr)
|
||||
thread = (*C.perfstat_thread_t)(C.malloc(thlen))
|
||||
defer C.free(unsafe.Pointer(thread))
|
||||
C.strcpy(&first.name[0], C.CString(""))
|
||||
r := C.perfstat_thread(&first, thread, C.sizeof_perfstat_thread_t, numthr)
|
||||
if r < 0 {
|
||||
return nil, fmt.Errorf("perfstat_thread() error")
|
||||
}
|
||||
|
||||
th := make([]Thread, r)
|
||||
for i := 0; i < int(r); i++ {
|
||||
t := C.get_thread_stat(thread, C.int(i))
|
||||
if t != nil {
|
||||
th[i] = perfstatthread2thread(t)
|
||||
}
|
||||
}
|
||||
return th, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,195 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#include <unistd.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
SC_ARG_MAX = 0
|
||||
SC_CHILD_MAX = 1
|
||||
SC_CLK_TCK = 2
|
||||
SC_NGROUPS_MAX = 3
|
||||
SC_OPEN_MAX = 4
|
||||
SC_STREAM_MAX = 5
|
||||
SC_TZNAME_MAX = 6
|
||||
SC_JOB_CONTROL = 7
|
||||
SC_SAVED_IDS = 8
|
||||
SC_VERSION = 9
|
||||
SC_POSIX_ARG_MAX = 10
|
||||
SC_POSIX_CHILD_MAX = 11
|
||||
SC_POSIX_LINK_MAX = 12
|
||||
SC_POSIX_MAX_CANON = 13
|
||||
SC_POSIX_MAX_INPUT = 14
|
||||
SC_POSIX_NAME_MAX = 15
|
||||
SC_POSIX_NGROUPS_MAX = 16
|
||||
SC_POSIX_OPEN_MAX = 17
|
||||
SC_POSIX_PATH_MAX = 18
|
||||
SC_POSIX_PIPE_BUF = 19
|
||||
SC_POSIX_SSIZE_MAX = 20
|
||||
SC_POSIX_STREAM_MAX = 21
|
||||
SC_POSIX_TZNAME_MAX = 22
|
||||
SC_BC_BASE_MAX = 23
|
||||
SC_BC_DIM_MAX = 24
|
||||
SC_BC_SCALE_MAX = 25
|
||||
SC_BC_STRING_MAX = 26
|
||||
SC_EQUIV_CLASS_MAX = 27
|
||||
SC_EXPR_NEST_MAX = 28
|
||||
SC_LINE_MAX = 29
|
||||
SC_RE_DUP_MAX = 30
|
||||
SC_2_VERSION = 31
|
||||
SC_2_C_DEV = 32
|
||||
SC_2_FORT_DEV = 33
|
||||
SC_2_FORT_RUN = 34
|
||||
SC_2_LOCALEDEF = 35
|
||||
SC_2_SW_DEV = 36
|
||||
SC_POSIX2_BC_BASE_MAX = 37
|
||||
SC_POSIX2_BC_DIM_MAX = 38
|
||||
SC_POSIX2_BC_SCALE_MAX = 39
|
||||
SC_POSIX2_BC_STRING_MAX = 40
|
||||
SC_POSIX2_BC_EQUIV_CLASS_MAX = 41
|
||||
SC_POSIX2_BC_EXPR_NEST_MAX = 42
|
||||
SC_POSIX2_BC_LINE_MAX = 43
|
||||
SC_POSIX2_BC_RE_DUP_MAX = 44
|
||||
SC_PASS_MAX = 45
|
||||
SC_XOPEN_VERSION = 46
|
||||
SC_ATEXIT_MAX = 47
|
||||
SC_PAGE_SIZE = 48
|
||||
SC_PAGESIZE = SC_PAGE_SIZE
|
||||
SC_AES_OS_VERSION = 49
|
||||
SC_COLL_WEIGHTS_MAX = 50
|
||||
SC_2_C_WIND = 51
|
||||
SC_2_C_VERSION = 52
|
||||
SC_2_UPE = 53
|
||||
SC_2_CHAR_TERM = 54
|
||||
SC_XOPEN_SHM = 55
|
||||
SC_XOPEN_CRYPT = 56
|
||||
SC_XOPEN_ENH_I18N = 57
|
||||
SC_IOV_MAX = 58
|
||||
SC_THREAD_SAFE_FUNCTIONS = 59
|
||||
SC_THREADS = 60
|
||||
SC_THREAD_ATTR_STACKADDR = 61
|
||||
SC_THREAD_ATTR_STACKSIZE = 62
|
||||
SC_THREAD_FORKALL = 63
|
||||
SC_THREAD_PRIORITY_SCHEDULING = 64
|
||||
SC_THREAD_PRIO_INHERIT = 65
|
||||
SC_THREAD_PRIO_PROTECT = 66
|
||||
SC_THREAD_PROCESS_SHARED = 67
|
||||
SC_THREAD_KEYS_MAX = 68
|
||||
SC_THREAD_DATAKEYS_MAX = SC_THREAD_KEYS_MAX
|
||||
SC_THREAD_STACK_MIN = 69
|
||||
SC_THREAD_THREADS_MAX = 70
|
||||
SC_NPROCESSORS_CONF = 71
|
||||
SC_NPROCESSORS_ONLN = 72
|
||||
SC_XOPEN_UNIX = 73
|
||||
SC_AIO_LISTIO_MAX = 75
|
||||
SC_AIO_MAX = 76
|
||||
SC_AIO_PRIO_DELTA_MAX = 77
|
||||
SC_ASYNCHRONOUS_IO = 78
|
||||
SC_DELAYTIMER_MAX = 79
|
||||
SC_FSYNC = 80
|
||||
SC_GETGR_R_SIZE_MAX = 81
|
||||
SC_GETPW_R_SIZE_MAX = 82
|
||||
SC_LOGIN_NAME_MAX = 83
|
||||
SC_MAPPED_FILES = 84
|
||||
SC_MEMLOCK = 85
|
||||
SC_MEMLOCK_RANGE = 86
|
||||
SC_MEMORY_PROTECTION = 87
|
||||
SC_MESSAGE_PASSING = 88
|
||||
SC_MQ_OPEN_MAX = 89
|
||||
SC_MQ_PRIO_MAX = 90
|
||||
SC_PRIORITIZED_IO = 91
|
||||
SC_PRIORITY_SCHEDULING = 92
|
||||
SC_REALTIME_SIGNALS = 93
|
||||
SC_RTSIG_MAX = 94
|
||||
SC_SEMAPHORES = 95
|
||||
SC_SEM_NSEMS_MAX = 96
|
||||
SC_SEM_VALUE_MAX = 97
|
||||
SC_SHARED_MEMORY_OBJECTS = 98
|
||||
SC_SIGQUEUE_MAX = 99
|
||||
SC_SYNCHRONIZED_IO = 100
|
||||
SC_THREAD_DESTRUCTOR_ITERATIONS = 101
|
||||
SC_TIMERS = 102
|
||||
SC_TIMER_MAX = 103
|
||||
SC_TTY_NAME_MAX = 104
|
||||
SC_XBS5_ILP32_OFF32 = 105
|
||||
SC_XBS5_ILP32_OFFBIG = 106
|
||||
SC_XBS5_LP64_OFF64 = 107
|
||||
SC_XBS5_LPBIG_OFFBIG = 108
|
||||
SC_XOPEN_XCU_VERSION = 109
|
||||
SC_XOPEN_REALTIME = 110
|
||||
SC_XOPEN_REALTIME_THREADS = 111
|
||||
SC_XOPEN_LEGACY = 112
|
||||
SC_REENTRANT_FUNCTIONS = SC_THREAD_SAFE_FUNCTIONS
|
||||
SC_PHYS_PAGES = 113
|
||||
SC_AVPHYS_PAGES = 114
|
||||
SC_LPAR_ENABLED = 115
|
||||
SC_LARGE_PAGESIZE = 116
|
||||
SC_AIX_KERNEL_BITMODE = 117
|
||||
SC_AIX_REALMEM = 118
|
||||
SC_AIX_HARDWARE_BITMODE = 119
|
||||
SC_AIX_MP_CAPABLE = 120
|
||||
SC_V6_ILP32_OFF32 = 121
|
||||
SC_V6_ILP32_OFFBIG = 122
|
||||
SC_V6_LP64_OFF64 = 123
|
||||
SC_V6_LPBIG_OFFBIG = 124
|
||||
SC_XOPEN_STREAMS = 125
|
||||
SC_HOST_NAME_MAX = 126
|
||||
SC_REGEXP = 127
|
||||
SC_SHELL = 128
|
||||
SC_SYMLOOP_MAX = 129
|
||||
SC_ADVISORY_INFO = 130
|
||||
SC_FILE_LOCKING = 131
|
||||
SC_2_PBS = 132
|
||||
SC_2_PBS_ACCOUNTING = 133
|
||||
SC_2_PBS_CHECKPOINT = 134
|
||||
SC_2_PBS_LOCATE = 135
|
||||
SC_2_PBS_MESSAGE = 136
|
||||
SC_2_PBS_TRACK = 137
|
||||
SC_BARRIERS = 138
|
||||
SC_CLOCK_SELECTION = 139
|
||||
SC_CPUTIME = 140
|
||||
SC_MONOTONIC_CLOCK = 141
|
||||
SC_READER_WRITER_LOCKS = 142
|
||||
SC_SPAWN = 143
|
||||
SC_SPIN_LOCKS = 144
|
||||
SC_SPORADIC_SERVER = 145
|
||||
SC_THREAD_CPUTIME = 146
|
||||
SC_THREAD_SPORADIC_SERVER = 147
|
||||
SC_TIMEOUTS = 148
|
||||
SC_TRACE = 149
|
||||
SC_TRACE_EVENT_FILTER = 150
|
||||
SC_TRACE_INHERIT = 151
|
||||
SC_TRACE_LOG = 152
|
||||
SC_TYPED_MEMORY_OBJECTS = 153
|
||||
SC_IPV6 = 154
|
||||
SC_RAW_SOCKETS = 155
|
||||
SC_SS_REPL_MAX = 156
|
||||
SC_TRACE_EVENT_NAME_MAX = 157
|
||||
SC_TRACE_NAME_MAX = 158
|
||||
SC_TRACE_SYS_MAX = 159
|
||||
SC_TRACE_USER_EVENT_MAX = 160
|
||||
SC_AIX_UKEYS = 161
|
||||
SC_AIX_ENHANCED_AFFINITY = 162
|
||||
SC_V7_ILP32_OFF32 = 163
|
||||
SC_V7_ILP32_OFFBIG = 164
|
||||
SC_V7_LP64_OFF64 = 165
|
||||
SC_V7_LPBIG_OFFBIG = 166
|
||||
SC_THREAD_ROBUST_PRIO_INHERIT = 167
|
||||
SC_THREAD_ROBUST_PRIO_PROTECT = 168
|
||||
SC_XOPEN_UUCP = 169
|
||||
SC_XOPEN_ARMOR = 170
|
||||
)
|
||||
|
||||
func Sysconf(name int32) (int64, error) {
|
||||
r := C.sysconf(C.int(name))
|
||||
if r == -1 {
|
||||
return 0, fmt.Errorf("sysconf error")
|
||||
} else {
|
||||
return int64(r), nil
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,635 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
// function Getsystemcfg() is defined in golang.org/x/sys/unix
|
||||
// we define here just missing constants for the function and some helpers
|
||||
|
||||
// Calls to getsystemcfg()
|
||||
const (
|
||||
SC_ARCH = 1 /* processor architecture */
|
||||
SC_IMPL = 2 /* processor implementation */
|
||||
SC_VERS = 3 /* processor version */
|
||||
SC_WIDTH = 4 /* width (32 || 64) */
|
||||
SC_NCPUS = 5 /* 1 = UP, n = n-way MP */
|
||||
SC_L1C_ATTR = 6 /* L1 cache attributes (bit flags) */
|
||||
SC_L1C_ISZ = 7 /* size of L1 instruction cache */
|
||||
SC_L1C_DSZ = 8 /* size of L1 data cache */
|
||||
SC_L1C_ICA = 9 /* L1 instruction cache associativity */
|
||||
SC_L1C_DCA = 10 /* L1 data cache associativity */
|
||||
SC_L1C_IBS = 11 /* L1 instruction cache block size */
|
||||
SC_L1C_DBS = 12 /* L1 data cache block size */
|
||||
SC_L1C_ILS = 13 /* L1 instruction cache line size */
|
||||
SC_L1C_DLS = 14 /* L1 data cache line size */
|
||||
SC_L2C_SZ = 15 /* size of L2 cache, 0 = No L2 cache */
|
||||
SC_L2C_AS = 16 /* L2 cache associativity */
|
||||
SC_TLB_ATTR = 17 /* TLB attributes (bit flags) */
|
||||
SC_ITLB_SZ = 18 /* entries in instruction TLB */
|
||||
SC_DTLB_SZ = 19 /* entries in data TLB */
|
||||
SC_ITLB_ATT = 20 /* instruction tlb associativity */
|
||||
SC_DTLB_ATT = 21 /* data tlb associativity */
|
||||
SC_RESRV_SZ = 22 /* size of reservation */
|
||||
SC_PRI_LC = 23 /* spin lock count in supevisor mode */
|
||||
SC_PRO_LC = 24 /* spin lock count in problem state */
|
||||
SC_RTC_TYPE = 25 /* RTC type */
|
||||
SC_VIRT_AL = 26 /* 1 if hardware aliasing is supported */
|
||||
SC_CAC_CONG = 27 /* number of page bits for cache synonym */
|
||||
SC_MOD_ARCH = 28 /* used by system for model determination */
|
||||
SC_MOD_IMPL = 29 /* used by system for model determination */
|
||||
SC_XINT = 30 /* used by system for time base conversion */
|
||||
SC_XFRAC = 31 /* used by system for time base conversion */
|
||||
SC_KRN_ATTR = 32 /* kernel attributes, see below */
|
||||
SC_PHYSMEM = 33 /* bytes of OS available memory */
|
||||
SC_SLB_ATTR = 34 /* SLB attributes */
|
||||
SC_SLB_SZ = 35 /* size of slb (0 = no slb) */
|
||||
SC_ORIG_NCPUS = 36 /* original number of CPUs */
|
||||
SC_MAX_NCPUS = 37 /* max cpus supported by this AIX image */
|
||||
SC_MAX_REALADDR = 38 /* max supported real memory address +1 */
|
||||
SC_ORIG_ENT_CAP = 39 /* configured entitled processor capacity at boot required by cross-partition LPAR tools. */
|
||||
SC_ENT_CAP = 40 /* entitled processor capacity */
|
||||
SC_DISP_WHE = 41 /* Dispatch wheel time period (TB units) */
|
||||
SC_CAPINC = 42 /* delta by which capacity can change */
|
||||
SC_VCAPW = 43 /* priority weight for idle capacity distribution */
|
||||
SC_SPLP_STAT = 44 /* State of SPLPAR enablement: 0x1 => 1=SPLPAR capable; 0=not, 0x2 => SPLPAR enabled 0=dedicated, 1=shared */
|
||||
SC_SMT_STAT = 45 /* State of SMT enablement: 0x1 = SMT Capable 0=no/1=yes, 0x2 = SMT Enabled 0=no/1=yes, 0x4 = SMT threads bound true 0=no/1=yes */
|
||||
SC_SMT_TC = 46 /* Number of SMT Threads per Physical CPU */
|
||||
SC_VMX_VER = 47 /* RPA defined VMX version: 0 = VMX not available or disabled, 1 = VMX capable, 2 = VMX and VSX capable */
|
||||
SC_LMB_SZ = 48 /* Size of an LMB on this system. */
|
||||
SC_MAX_XCPU = 49 /* Number of exclusive cpus on line */
|
||||
SC_EC_LVL = 50 /* Kernel error checking level */
|
||||
SC_AME_STAT = 51 /* AME status */
|
||||
SC_ECO_STAT = 52 /* extended cache options */
|
||||
SC_DFP_STAT = 53 /* RPA defined DFP version, 0=none/disabled */
|
||||
SC_VRM_STAT = 54 /* VRM Capable/enabled */
|
||||
SC_PHYS_IMP = 55 /* physical processor implementation */
|
||||
SC_PHYS_VER = 56 /* physical processor version */
|
||||
SC_SPCM_STATUS = 57
|
||||
SC_SPCM_MAX = 58
|
||||
SC_TM_VER = 59 /* Transaction Memory version, 0 - not capable */
|
||||
SC_NX_CAP = 60 /* NX GZIP capable */
|
||||
SC_PKS_STATE = 61 /* Platform KeyStore */
|
||||
)
|
||||
|
||||
/* kernel attributes */
|
||||
/* bit 0/1 meaning */
|
||||
/* -----------------------------------------*/
|
||||
/* 31 32-bit kernel / 64-bit kernel */
|
||||
/* 30 non-LPAR / LPAR */
|
||||
/* 29 old 64bit ABI / 64bit Large ABI */
|
||||
/* 28 non-NUMA / NUMA */
|
||||
/* 27 UP / MP */
|
||||
/* 26 no DR CPU add / DR CPU add support */
|
||||
/* 25 no DR CPU rm / DR CPU rm support */
|
||||
/* 24 no DR MEM add / DR MEM add support */
|
||||
/* 23 no DR MEM rm / DR MEM rm support */
|
||||
/* 22 kernel keys disabled / enabled */
|
||||
/* 21 no recovery / recovery enabled */
|
||||
/* 20 non-MLS / MLS enabled */
|
||||
/* 19 enhanced affinity indicator */
|
||||
/* 18 non-vTPM / vTPM enabled */
|
||||
/* 17 non-VIOS / VIOS */
|
||||
|
||||
// Values for architecture field
|
||||
const (
|
||||
ARCH_POWER_RS = 0x0001 /* Power Classic architecture */
|
||||
ARCH_POWER_PC = 0x0002 /* Power PC architecture */
|
||||
ARCH_IA64 = 0x0003 /* Intel IA64 architecture */
|
||||
)
|
||||
|
||||
// Values for implementation field for POWER_PC Architectures
|
||||
const (
|
||||
IMPL_POWER_RS1 = 0x00001 /* RS1 class CPU */
|
||||
IMPL_POWER_RSC = 0x00002 /* RSC class CPU */
|
||||
IMPL_POWER_RS2 = 0x00004 /* RS2 class CPU */
|
||||
IMPL_POWER_601 = 0x00008 /* 601 class CPU */
|
||||
IMPL_POWER_603 = 0x00020 /* 603 class CPU */
|
||||
IMPL_POWER_604 = 0x00010 /* 604 class CPU */
|
||||
IMPL_POWER_620 = 0x00040 /* 620 class CPU */
|
||||
IMPL_POWER_630 = 0x00080 /* 630 class CPU */
|
||||
IMPL_POWER_A35 = 0x00100 /* A35 class CPU */
|
||||
IMPL_POWER_RS64II = 0x0200 /* RS64-II class CPU */
|
||||
IMPL_POWER_RS64III = 0x0400 /* RS64-III class CPU */
|
||||
IMPL_POWER4 = 0x0800 /* 4 class CPU */
|
||||
IMPL_POWER_RS64IV = IMPL_POWER4 /* 4 class CPU */
|
||||
IMPL_POWER_MPC7450 = 0x1000 /* MPC7450 class CPU */
|
||||
IMPL_POWER5 = 0x2000 /* 5 class CPU */
|
||||
IMPL_POWER6 = 0x4000 /* 6 class CPU */
|
||||
IMPL_POWER7 = 0x8000 /* 7 class CPU */
|
||||
IMPL_POWER8 = 0x10000 /* 8 class CPU */
|
||||
IMPL_POWER9 = 0x20000 /* 9 class CPU */
|
||||
)
|
||||
|
||||
// Values for implementation field for IA64 Architectures
|
||||
const (
|
||||
IMPL_IA64_M1 = 0x0001 /* IA64 M1 class CPU (Itanium) */
|
||||
IMPL_IA64_M2 = 0x0002 /* IA64 M2 class CPU */
|
||||
)
|
||||
|
||||
// Values for the version field
|
||||
const (
|
||||
PV_601 = 0x010001 /* Power PC 601 */
|
||||
PV_601A = 0x010002 /* Power PC 601 */
|
||||
PV_603 = 0x060000 /* Power PC 603 */
|
||||
PV_604 = 0x050000 /* Power PC 604 */
|
||||
PV_620 = 0x070000 /* Power PC 620 */
|
||||
PV_630 = 0x080000 /* Power PC 630 */
|
||||
PV_A35 = 0x090000 /* Power PC A35 */
|
||||
PV_RS64II = 0x0A0000 /* Power PC RS64II */
|
||||
PV_RS64III = 0x0B0000 /* Power PC RS64III */
|
||||
PV_4 = 0x0C0000 /* Power PC 4 */
|
||||
PV_RS64IV = PV_4 /* Power PC 4 */
|
||||
PV_MPC7450 = 0x0D0000 /* Power PC MPC7450 */
|
||||
PV_4_2 = 0x0E0000 /* Power PC 4 */
|
||||
PV_4_3 = 0x0E0001 /* Power PC 4 */
|
||||
PV_5 = 0x0F0000 /* Power PC 5 */
|
||||
PV_5_2 = 0x0F0001 /* Power PC 5 */
|
||||
PV_5_3 = 0x0F0002 /* Power PC 5 */
|
||||
PV_6 = 0x100000 /* Power PC 6 */
|
||||
PV_6_1 = 0x100001 /* Power PC 6 DD1.x */
|
||||
PV_7 = 0x200000 /* Power PC 7 */
|
||||
PV_8 = 0x300000 /* Power PC 8 */
|
||||
PV_9 = 0x400000 /* Power PC 9 */
|
||||
PV_5_Compat = 0x0F8000 /* Power PC 5 */
|
||||
PV_6_Compat = 0x108000 /* Power PC 6 */
|
||||
PV_7_Compat = 0x208000 /* Power PC 7 */
|
||||
PV_8_Compat = 0x308000 /* Power PC 8 */
|
||||
PV_9_Compat = 0x408000 /* Power PC 9 */
|
||||
PV_RESERVED_2 = 0x0A0000 /* source compatability */
|
||||
PV_RESERVED_3 = 0x0B0000 /* source compatability */
|
||||
PV_RS2 = 0x040000 /* Power RS2 */
|
||||
PV_RS1 = 0x020000 /* Power RS1 */
|
||||
PV_RSC = 0x030000 /* Power RSC */
|
||||
PV_M1 = 0x008000 /* Intel IA64 M1 */
|
||||
PV_M2 = 0x008001 /* Intel IA64 M2 */
|
||||
)
|
||||
|
||||
// Values for rtc_type
|
||||
const (
|
||||
RTC_POWER = 1 /* rtc as defined by Power Arch. */
|
||||
RTC_POWER_PC = 2 /* rtc as defined by Power PC Arch. */
|
||||
RTC_IA64 = 3 /* rtc as defined by IA64 Arch. */
|
||||
)
|
||||
|
||||
const NX_GZIP_PRESENT = 0x00000001
|
||||
|
||||
const (
|
||||
PKS_STATE_CAPABLE = 1
|
||||
PKS_STATE_ENABLED = 2
|
||||
)
|
||||
|
||||
// Macros for identifying physical processor
|
||||
const (
|
||||
PPI4_1 = 0x35
|
||||
PPI4_2 = 0x38
|
||||
PPI4_3 = 0x39
|
||||
PPI4_4 = 0x3C
|
||||
PPI4_5 = 0x44
|
||||
PPI5_1 = 0x3A
|
||||
PPI5_2 = 0x3B
|
||||
PPI6_1 = 0x3E
|
||||
PPI7_1 = 0x3F
|
||||
PPI7_2 = 0x4A
|
||||
PPI8_1 = 0x4B
|
||||
PPI8_2 = 0x4D
|
||||
PPI9 = 0x4E
|
||||
)
|
||||
|
||||
// Macros for kernel attributes
|
||||
const (
|
||||
KERN_TYPE = 0x1
|
||||
KERN_LPAR = 0x2
|
||||
KERN_64BIT_LARGE_ABI = 0x4
|
||||
KERN_NUMA = 0x8
|
||||
KERN_UPMP = 0x10
|
||||
KERN_DR_CPU_ADD = 0x20
|
||||
KERN_DR_CPU_RM = 0x40
|
||||
KERN_DR_MEM_ADD = 0x80
|
||||
KERN_DR_MEM_RM = 0x100
|
||||
KERN_KKEY_ENABLED = 0x200
|
||||
KERN_RECOVERY = 0x400
|
||||
KERN_MLS = 0x800
|
||||
KERN_ENH_AFFINITY = 0x1000
|
||||
KERN_VTPM = 0x2000
|
||||
KERN_VIOS = 0x4000
|
||||
)
|
||||
|
||||
// macros for SPLPAR environment.
|
||||
const (
|
||||
SPLPAR_CAPABLE = 0x1
|
||||
SPLPAR_ENABLED = 0x2
|
||||
SPLPAR_DONATE_CAPABLE = 0x4
|
||||
)
|
||||
|
||||
// Macros for SMT status determination
|
||||
const (
|
||||
SMT_CAPABLE = 0x1
|
||||
SMT_ENABLE = 0x2
|
||||
SMT_BOUND = 0x4
|
||||
SMT_ORDER = 0x8
|
||||
)
|
||||
|
||||
// Macros for VRM status determination
|
||||
const (
|
||||
VRM_CAPABLE = 0x1
|
||||
VRM_ENABLE = 0x2
|
||||
CMOX_CAPABLE = 0x4
|
||||
)
|
||||
|
||||
// Macros for AME status determination
|
||||
const AME_ENABLE = 0x1
|
||||
|
||||
// Macros for extended cache options
|
||||
const (
|
||||
ECO_CAPABLE = 0x1
|
||||
ECO_ENABLE = 0x2
|
||||
)
|
||||
|
||||
// These define blocks of values for model_arch and model_impl that are reserved for OEM use.
|
||||
const (
|
||||
MODEL_ARCH_RSPC = 2
|
||||
MODEL_ARCH_CHRP = 3
|
||||
MODEL_ARCH_IA64 = 4
|
||||
MODEL_ARCH_OEM_START = 1024
|
||||
MODEL_ARCH_OEM_END = 2047
|
||||
MODEL_IMPL_RS6K_UP_MCA = 1
|
||||
MODEL_IMPL_RS6K_SMP_MCA = 2
|
||||
MODEL_IMPL_RSPC_UP_PCI = 3
|
||||
MODEL_IMPL_RSPC_SMP_PCI = 4
|
||||
MODEL_IMPL_CHRP_UP_PCI = 5
|
||||
MODEL_IMPL_CHRP_SMP_PCI = 6
|
||||
MODEL_IMPL_IA64_COM = 7
|
||||
MODEL_IMPL_IA64_SOFTSDV = 8
|
||||
MODEL_IMPL_MAMBO_SIM = 9
|
||||
MODEL_IMPL_POWER_KVM = 10
|
||||
MODEL_IMPL_OEM_START = 1024
|
||||
MODEL_IMPL_OEM_END = 2047
|
||||
)
|
||||
|
||||
// example determining processor compatibilty mode on AIX:
|
||||
// impl := unix.Getsystemcfg(SC_IMPL)
|
||||
// if impl&IMPL_POWER8 != 0 {
|
||||
// // we are running on POWER8
|
||||
// }
|
||||
// if impl&IMPL_POWER9 != 0 {
|
||||
// // we are running on POWER9
|
||||
// }
|
||||
|
||||
func GetCPUImplementation() string {
|
||||
impl := unix.Getsystemcfg(SC_IMPL)
|
||||
switch {
|
||||
case impl&IMPL_POWER4 != 0:
|
||||
return "POWER4"
|
||||
case impl&IMPL_POWER5 != 0:
|
||||
return "POWER5"
|
||||
case impl&IMPL_POWER6 != 0:
|
||||
return "POWER6"
|
||||
case impl&IMPL_POWER7 != 0:
|
||||
return "POWER7"
|
||||
case impl&IMPL_POWER8 != 0:
|
||||
return "POWER8"
|
||||
case impl&IMPL_POWER9 != 0:
|
||||
return "POWER9"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func POWER9OrNewer() bool {
|
||||
impl := unix.Getsystemcfg(SC_IMPL)
|
||||
if impl&IMPL_POWER9 != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER9() bool {
|
||||
impl := unix.Getsystemcfg(SC_IMPL)
|
||||
if impl&IMPL_POWER9 != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER8OrNewer() bool {
|
||||
impl := unix.Getsystemcfg(SC_IMPL)
|
||||
if impl&IMPL_POWER9 != 0 || impl&IMPL_POWER8 != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER8() bool {
|
||||
impl := unix.Getsystemcfg(SC_IMPL)
|
||||
if impl&IMPL_POWER8 != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER7OrNewer() bool {
|
||||
impl := unix.Getsystemcfg(SC_IMPL)
|
||||
if impl&IMPL_POWER9 != 0 || impl&IMPL_POWER8 != 0 || impl&IMPL_POWER7 != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func POWER7() bool {
|
||||
impl := unix.Getsystemcfg(SC_IMPL)
|
||||
if impl&IMPL_POWER7 != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func HasTransactionalMemory() bool {
|
||||
impl := unix.Getsystemcfg(SC_TM_VER)
|
||||
if impl > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Is64Bit() bool {
|
||||
impl := unix.Getsystemcfg(SC_WIDTH)
|
||||
if impl == 64 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsSMP() bool {
|
||||
impl := unix.Getsystemcfg(SC_NCPUS)
|
||||
if impl > 1 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func HasVMX() bool {
|
||||
impl := unix.Getsystemcfg(SC_VMX_VER)
|
||||
if impl > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func HasVSX() bool {
|
||||
impl := unix.Getsystemcfg(SC_VMX_VER)
|
||||
if impl > 1 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func HasDFP() bool {
|
||||
impl := unix.Getsystemcfg(SC_DFP_STAT)
|
||||
if impl > 1 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func HasNxGzip() bool {
|
||||
impl := unix.Getsystemcfg(SC_NX_CAP)
|
||||
if impl&NX_GZIP_PRESENT > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func PksCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_PKS_STATE)
|
||||
if impl&PKS_STATE_CAPABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func PksEnabled() bool {
|
||||
impl := unix.Getsystemcfg(SC_PKS_STATE)
|
||||
if impl&PKS_STATE_ENABLED > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func CPUMode() string {
|
||||
impl := unix.Getsystemcfg(SC_VERS)
|
||||
switch impl {
|
||||
case PV_9, PV_9_Compat:
|
||||
return "POWER9"
|
||||
case PV_8, PV_8_Compat:
|
||||
return "POWER8"
|
||||
case PV_7, PV_7_Compat:
|
||||
return "POWER7"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func KernelBits() int {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_TYPE == KERN_TYPE {
|
||||
return 64
|
||||
}
|
||||
return 32
|
||||
}
|
||||
|
||||
func IsLPAR() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_LPAR == KERN_LPAR {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func CpuAddCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_DR_CPU_ADD == KERN_DR_CPU_ADD {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func CpuRemoveCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_DR_CPU_RM == KERN_DR_CPU_RM {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func MemoryAddCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_DR_MEM_ADD == KERN_DR_MEM_ADD {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func MemoryRemoveCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_DR_MEM_RM == KERN_DR_MEM_RM {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func DLparCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&(KERN_DR_CPU_ADD|KERN_DR_CPU_RM|KERN_DR_MEM_ADD|KERN_DR_MEM_RM) > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsNUMA() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_NUMA > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func KernelKeys() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_KKEY_ENABLED > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func RecoveryMode() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_RECOVERY > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func EnhancedAffinity() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_ENH_AFFINITY > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func VTpmEnabled() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_VTPM > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsVIOS() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_VIOS > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func MLSEnabled() bool {
|
||||
impl := unix.Getsystemcfg(SC_KRN_ATTR)
|
||||
if impl&KERN_MLS > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SPLparCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_SPLP_STAT)
|
||||
if impl&SPLPAR_CAPABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SPLparEnabled() bool {
|
||||
impl := unix.Getsystemcfg(SC_SPLP_STAT)
|
||||
if impl&SPLPAR_ENABLED > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func DedicatedLpar() bool {
|
||||
return !SPLparEnabled()
|
||||
}
|
||||
|
||||
func SPLparCapped() bool {
|
||||
impl := unix.Getsystemcfg(SC_VCAPW)
|
||||
if impl == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SPLparDonating() bool {
|
||||
impl := unix.Getsystemcfg(SC_SPLP_STAT)
|
||||
if impl&SPLPAR_DONATE_CAPABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SmtCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_SMT_STAT)
|
||||
if impl&SMT_CAPABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SmtEnabled() bool {
|
||||
impl := unix.Getsystemcfg(SC_SMT_STAT)
|
||||
if impl&SMT_ENABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func VrmCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_VRM_STAT)
|
||||
if impl&VRM_CAPABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func VrmEnabled() bool {
|
||||
impl := unix.Getsystemcfg(SC_VRM_STAT)
|
||||
if impl&VRM_ENABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func AmeEnabled() bool {
|
||||
impl := unix.Getsystemcfg(SC_AME_STAT)
|
||||
if impl&AME_ENABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func EcoCapable() bool {
|
||||
impl := unix.Getsystemcfg(SC_ECO_STAT)
|
||||
if impl&ECO_CAPABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func EcoEnabled() bool {
|
||||
impl := unix.Getsystemcfg(SC_ECO_STAT)
|
||||
if impl&ECO_ENABLE > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
@ -0,0 +1,186 @@
|
|||
package perfstat
|
||||
|
||||
type CPU struct {
|
||||
Name string /* logical processor name (cpu0, cpu1, ..) */
|
||||
User int64 /* raw number of clock ticks spent in user mode */
|
||||
Sys int64 /* raw number of clock ticks spent in system mode */
|
||||
Idle int64 /* raw number of clock ticks spent idle */
|
||||
Wait int64 /* raw number of clock ticks spent waiting for I/O */
|
||||
PSwitch int64 /* number of context switches (changes of currently running process) */
|
||||
Syscall int64 /* number of system calls executed */
|
||||
Sysread int64 /* number of read system calls executed */
|
||||
Syswrite int64 /* number of write system calls executed */
|
||||
Sysfork int64 /* number of fork system call executed */
|
||||
Sysexec int64 /* number of exec system call executed */
|
||||
Readch int64 /* number of characters tranferred with read system call */
|
||||
Writech int64 /* number of characters tranferred with write system call */
|
||||
Bread int64 /* number of block reads */
|
||||
Bwrite int64 /* number of block writes */
|
||||
Lread int64 /* number of logical read requests */
|
||||
Lwrite int64 /* number of logical write requests */
|
||||
Phread int64 /* number of physical reads (reads on raw device) */
|
||||
Phwrite int64 /* number of physical writes (writes on raw device) */
|
||||
Iget int64 /* number of inode lookups */
|
||||
Namei int64 /* number of vnode lookup from a path name */
|
||||
Dirblk int64 /* number of 512-byte block reads by the directory search routine to locate an entry for a file */
|
||||
Msg int64 /* number of IPC message operations */
|
||||
Sema int64 /* number of IPC semaphore operations */
|
||||
MinFaults int64 /* number of page faults with no I/O */
|
||||
MajFaults int64 /* number of page faults with disk I/O */
|
||||
PUser int64 /* raw number of physical processor tics in user mode */
|
||||
PSys int64 /* raw number of physical processor tics in system mode */
|
||||
PIdle int64 /* raw number of physical processor tics idle */
|
||||
PWait int64 /* raw number of physical processor tics waiting for I/O */
|
||||
RedispSD0 int64 /* number of thread redispatches within the scheduler affinity domain 0 */
|
||||
RedispSD1 int64 /* number of thread redispatches within the scheduler affinity domain 1 */
|
||||
RedispSD2 int64 /* number of thread redispatches within the scheduler affinity domain 2 */
|
||||
RedispSD3 int64 /* number of thread redispatches within the scheduler affinity domain 3 */
|
||||
RedispSD4 int64 /* number of thread redispatches within the scheduler affinity domain 4 */
|
||||
RedispSD5 int64 /* number of thread redispatches within the scheduler affinity domain 5 */
|
||||
MigrationPush int64 /* number of thread migrations from the local runque to another queue due to starvation load balancing */
|
||||
MigrationS3grq int64 /* number of thread migrations from the global runque to the local runque resulting in a move accross scheduling domain 3 */
|
||||
MigrationS3pul int64 /* number of thread migrations from another processor's runque resulting in a move accross scheduling domain 3 */
|
||||
InvolCSwitch int64 /* number of involuntary thread context switches */
|
||||
VolCSwitch int64 /* number of voluntary thread context switches */
|
||||
RunQueue int64 /* number of threads on the runque */
|
||||
Bound int64 /* number of bound threads */
|
||||
DecrIntrs int64 /* number of decrementer tics interrupts */
|
||||
MpcRIntrs int64 /* number of mpc's received interrupts */
|
||||
MpcSIntrs int64 /* number of mpc's sent interrupts */
|
||||
DevIntrs int64 /* number of device interrupts */
|
||||
SoftIntrs int64 /* number of offlevel handlers called */
|
||||
PhantIntrs int64 /* number of phantom interrupts */
|
||||
IdleDonatedPurr int64 /* number of idle cycles donated by a dedicated partition enabled for donation */
|
||||
IdleDonatedSpurr int64 /* number of idle spurr cycles donated by a dedicated partition enabled for donation */
|
||||
BusyDonatedPurr int64 /* number of busy cycles donated by a dedicated partition enabled for donation */
|
||||
BusyDonatedSpurr int64 /* number of busy spurr cycles donated by a dedicated partition enabled for donation */
|
||||
IdleStolenPurr int64 /* number of idle cycles stolen by the hypervisor from a dedicated partition */
|
||||
IdleStolenSpurr int64 /* number of idle spurr cycles stolen by the hypervisor from a dedicated partition */
|
||||
BusyStolenPurr int64 /* number of busy cycles stolen by the hypervisor from a dedicated partition */
|
||||
BusyStolenSpurr int64 /* number of busy spurr cycles stolen by the hypervisor from a dedicated partition */
|
||||
Hpi int64 /* number of hypervisor page-ins */
|
||||
Hpit int64 /* Time spent in hypervisor page-ins (in nanoseconds)*/
|
||||
PUserSpurr int64 /* number of spurr cycles spent in user mode */
|
||||
PSysSpurr int64 /* number of spurr cycles spent in kernel mode */
|
||||
PIdleSpurr int64 /* number of spurr cycles spent in idle mode */
|
||||
PWaitSpurr int64 /* number of spurr cycles spent in wait mode */
|
||||
SpurrFlag int32 /* set if running in spurr mode */
|
||||
LocalDispatch int64 /* number of local thread dispatches on this logical CPU */
|
||||
NearDispatch int64 /* number of near thread dispatches on this logical CPU */
|
||||
FarDispatch int64 /* number of far thread dispatches on this logical CPU */
|
||||
CSwitches int64 /* Context switches */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
TbLast int64 /* timebase counter */
|
||||
State int /* Show whether the CPU is offline or online */
|
||||
VtbLast int64 /* Last virtual timebase read */
|
||||
ICountLast int64 /* Last instruction count read */
|
||||
}
|
||||
|
||||
type CPUTotal struct {
|
||||
NCpus int /* number of active logical processors */
|
||||
NCpusCfg int /* number of configured processors */
|
||||
Description string /* processor description (type/official name) */
|
||||
ProcessorHz int64 /* processor speed in Hz */
|
||||
User int64 /* raw total number of clock ticks spent in user mode */
|
||||
Sys int64 /* raw total number of clock ticks spent in system mode */
|
||||
Idle int64 /* raw total number of clock ticks spent idle */
|
||||
Wait int64 /* raw total number of clock ticks spent waiting for I/O */
|
||||
PSwitch int64 /* number of process switches (change in currently running process) */
|
||||
Syscall int64 /* number of system calls executed */
|
||||
Sysread int64 /* number of read system calls executed */
|
||||
Syswrite int64 /* number of write system calls executed */
|
||||
Sysfork int64 /* number of forks system calls executed */
|
||||
Sysexec int64 /* number of execs system calls executed */
|
||||
Readch int64 /* number of characters tranferred with read system call */
|
||||
Writech int64 /* number of characters tranferred with write system call */
|
||||
DevIntrs int64 /* number of device interrupts */
|
||||
SoftIntrs int64 /* number of software interrupts */
|
||||
Lbolt int64 /* number of ticks since last reboot */
|
||||
LoadAvg1 float32 /* times the average number of runnables processes during the last 1, 5 and 15 minutes. */
|
||||
LoadAvg5 float32 /* times the average number of runnables processes during the last 1, 5 and 15 minutes. */
|
||||
LoadAvg15 float32 /* times the average number of runnables processes during the last 1, 5 and 15 minutes. */
|
||||
RunQueue int64 /* length of the run queue (processes ready) */
|
||||
SwpQueue int64 /* length of the swap queue (processes waiting to be paged in) */
|
||||
Bread int64 /* number of blocks read */
|
||||
Bwrite int64 /* number of blocks written */
|
||||
Lread int64 /* number of logical read requests */
|
||||
Lwrite int64 /* number of logical write requests */
|
||||
Phread int64 /* number of physical reads (reads on raw devices) */
|
||||
Phwrite int64 /* number of physical writes (writes on raw devices) */
|
||||
RunOcc int64 /* updated whenever runque is updated, i.e. the runqueue is occupied. This can be used to compute the simple average of ready processes */
|
||||
SwpOcc int64 /* updated whenever swpque is updated. i.e. the swpqueue is occupied. This can be used to compute the simple average processes waiting to be paged in */
|
||||
Iget int64 /* number of inode lookups */
|
||||
Namei int64 /* number of vnode lookup from a path name */
|
||||
Dirblk int64 /* number of 512-byte block reads by the directory search routine to locate an entry for a file */
|
||||
Msg int64 /* number of IPC message operations */
|
||||
Sema int64 /* number of IPC semaphore operations */
|
||||
RcvInt int64 /* number of tty receive interrupts */
|
||||
XmtInt int64 /* number of tyy transmit interrupts */
|
||||
MdmInt int64 /* number of modem interrupts */
|
||||
TtyRawInch int64 /* number of raw input characters */
|
||||
TtyCanInch int64 /* number of canonical input characters (always zero) */
|
||||
TtyRawOutch int64 /* number of raw output characters */
|
||||
Ksched int64 /* number of kernel processes created */
|
||||
Koverf int64 /* kernel process creation attempts where: -the user has forked to their maximum limit -the configuration limit of processes has been reached */
|
||||
Kexit int64 /* number of kernel processes that became zombies */
|
||||
Rbread int64 /* number of remote read requests */
|
||||
Rcread int64 /* number of cached remote reads */
|
||||
Rbwrt int64 /* number of remote writes */
|
||||
Rcwrt int64 /* number of cached remote writes */
|
||||
Traps int64 /* number of traps */
|
||||
NCpusHigh int64 /* index of highest processor online */
|
||||
PUser int64 /* raw number of physical processor tics in user mode */
|
||||
PSys int64 /* raw number of physical processor tics in system mode */
|
||||
PIdle int64 /* raw number of physical processor tics idle */
|
||||
PWait int64 /* raw number of physical processor tics waiting for I/O */
|
||||
DecrIntrs int64 /* number of decrementer tics interrupts */
|
||||
MpcRIntrs int64 /* number of mpc's received interrupts */
|
||||
MpcSIntrs int64 /* number of mpc's sent interrupts */
|
||||
PhantIntrs int64 /* number of phantom interrupts */
|
||||
IdleDonatedPurr int64 /* number of idle cycles donated by a dedicated partition enabled for donation */
|
||||
IdleDonatedSpurr int64 /* number of idle spurr cycles donated by a dedicated partition enabled for donation */
|
||||
BusyDonatedPurr int64 /* number of busy cycles donated by a dedicated partition enabled for donation */
|
||||
BusyDonatedSpurr int64 /* number of busy spurr cycles donated by a dedicated partition enabled for donation */
|
||||
IdleStolenPurr int64 /* number of idle cycles stolen by the hypervisor from a dedicated partition */
|
||||
IdleStolenSpurr int64 /* number of idle spurr cycles stolen by the hypervisor from a dedicated partition */
|
||||
BusyStolenPurr int64 /* number of busy cycles stolen by the hypervisor from a dedicated partition */
|
||||
BusyStolenSpurr int64 /* number of busy spurr cycles stolen by the hypervisor from a dedicated partition */
|
||||
IOWait int32 /* number of processes that are asleep waiting for buffered I/O */
|
||||
PhysIO int32 /* number of processes waiting for raw I/O */
|
||||
TWait int64 /* number of threads that are waiting for filesystem direct(cio) */
|
||||
Hpi int64 /* number of hypervisor page-ins */
|
||||
Hpit int64 /* Time spent in hypervisor page-ins (in nanoseconds) */
|
||||
PUserSpurr int64 /* number of spurr cycles spent in user mode */
|
||||
PSysSpurr int64 /* number of spurr cycles spent in kernel mode */
|
||||
PIdleSpurr int64 /* number of spurr cycles spent in idle mode */
|
||||
PWaitSpurr int64 /* number of spurr cycles spent in wait mode */
|
||||
SpurrFlag int /* set if running in spurr mode */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
TbLast int64 /*time base counter */
|
||||
PurrCoalescing int64 /* If the calling partition is authorized to see pool wide statistics then PURR cycles consumed to coalesce data else set to zero.*/
|
||||
SpurrCoalescing int64 /* If the calling partition is authorized to see pool wide statistics then SPURR cycles consumed to coalesce data else set to zero. */
|
||||
}
|
||||
|
||||
type CPUUtil struct {
|
||||
Version int64
|
||||
CpuID string /* holds the id of the cpu */
|
||||
Entitlement float32 /* Partition's entitlement */
|
||||
UserPct float32 /* % of utilization in user mode */
|
||||
KernPct float32 /* % of utilization in kernel mode */
|
||||
IdlePct float32 /* % of utilization in idle mode */
|
||||
WaitPct float32 /* % of utilization in wait mode */
|
||||
PhysicalBusy float32 /* physical cpus busy */
|
||||
PhysicalConsumed float32 /* total cpus consumed by the partition */
|
||||
FreqPct float32 /* Average freq% over the last interval */
|
||||
EntitlementPct float32 /* % of entitlement used */
|
||||
BusyPct float32 /* % of entitlement busy */
|
||||
IdleDonatedPct float32 /* % idle cycles donated */
|
||||
BusyDonatedPct float32 /* % of busy cycles donated */
|
||||
IdleStolenPct float32 /* % idle cycles stolen */
|
||||
BusyStolenPct float32 /* % busy cycles stolen */
|
||||
LUserPct float32 /* % of utilization in user mode, in terms of logical processor ticks */
|
||||
LKernPct float32 /* % of utilization in kernel mode, in terms of logical processor ticks*/
|
||||
LIdlePct float32 /* % of utilization in idle mode, in terms of logical processor ticks */
|
||||
LWaitPct float32 /* % of utilization in wait mode, in terms of logical processor ticks */
|
||||
DeltaTime int64 /* delta time in milliseconds, for which utilization is evaluated */
|
||||
}
|
||||
|
|
@ -0,0 +1,176 @@
|
|||
package perfstat
|
||||
|
||||
type DiskTotal struct {
|
||||
Number int32 /* total number of disks */
|
||||
Size int64 /* total size of all disks (in MB) */
|
||||
Free int64 /* free portion of all disks (in MB) */
|
||||
XRate int64 /* __rxfers: total number of transfers from disk */
|
||||
Xfers int64 /* total number of transfers to/from disk */
|
||||
Wblks int64 /* 512 bytes blocks written to all disks */
|
||||
Rblks int64 /* 512 bytes blocks read from all disks */
|
||||
Time int64 /* amount of time disks are active */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
Rserv int64 /* Average read or receive service time */
|
||||
MinRserv int64 /* min read or receive service time */
|
||||
MaxRserv int64 /* max read or receive service time */
|
||||
RTimeOut int64 /* number of read request timeouts */
|
||||
RFailed int64 /* number of failed read requests */
|
||||
Wserv int64 /* Average write or send service time */
|
||||
MinWserv int64 /* min write or send service time */
|
||||
MaxWserv int64 /* max write or send service time */
|
||||
WTimeOut int64 /* number of write request timeouts */
|
||||
WFailed int64 /* number of failed write requests */
|
||||
WqDepth int64 /* instantaneous wait queue depth (number of requests waiting to be sent to disk) */
|
||||
WqTime int64 /* accumulated wait queueing time */
|
||||
WqMinTime int64 /* min wait queueing time */
|
||||
WqMaxTime int64 /* max wait queueing time */
|
||||
}
|
||||
|
||||
// Disk Adapter Types
|
||||
const (
|
||||
DA_SCSI = 0 /* 0 ==> SCSI, SAS, other legacy adapter types */
|
||||
DA_VSCSI /* 1 ==> Virtual SCSI/SAS Adapter */
|
||||
DA_FCA /* 2 ==> Fiber Channel Adapter */
|
||||
)
|
||||
|
||||
type DiskAdapter struct {
|
||||
Name string /* name of the adapter (from ODM) */
|
||||
Description string /* adapter description (from ODM) */
|
||||
Number int32 /* number of disks connected to adapter */
|
||||
Size int64 /* total size of all disks (in MB) */
|
||||
Free int64 /* free portion of all disks (in MB) */
|
||||
XRate int64 /* __rxfers: total number of reads via adapter */
|
||||
Xfers int64 /* total number of transfers via adapter */
|
||||
Rblks int64 /* 512 bytes blocks written via adapter */
|
||||
Wblks int64 /* 512 bytes blocks read via adapter */
|
||||
Time int64 /* amount of time disks are active */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
AdapterType int64 /* 0 ==> SCSI, SAS, other legacy adapter types, 1 ==> Virtual SCSI/SAS Adapter, 2 ==> Fiber Channel Adapter */
|
||||
DkBSize int64 /* Number of Bytes in a block for this disk*/
|
||||
DkRxfers int64 /* Number of transfers from disk */
|
||||
DkRserv int64 /* read or receive service time */
|
||||
DkWserv int64 /* write or send service time */
|
||||
MinRserv int64 /* Minimum read service time */
|
||||
MaxRserv int64 /* Maximum read service time */
|
||||
MinWserv int64 /* Minimum Write service time */
|
||||
MaxWserv int64 /* Maximum write service time */
|
||||
WqDepth int64 /* driver wait queue depth */
|
||||
WqSampled int64 /* accumulated sampled dk_wq_depth */
|
||||
WqTime int64 /* accumulated wait queueing time */
|
||||
WqMinTime int64 /* minimum wait queueing time */
|
||||
WqMaxTime int64 /* maximum wait queueing time */
|
||||
QFull int64 /* "Service" queue full occurrence count (number of times the adapter/devices connected to the adapter is not accepting any more request) */
|
||||
QSampled int64 /* accumulated sampled */
|
||||
}
|
||||
|
||||
type Disk struct {
|
||||
Name string /* name of the disk */
|
||||
Description string /* disk description (from ODM) */
|
||||
VGName string /* volume group name (from ODM) */
|
||||
Size int64 /* size of the disk (in MB) */
|
||||
Free int64 /* free portion of the disk (in MB) */
|
||||
BSize int64 /* disk block size (in bytes) */
|
||||
XRate int64 /* number of transfers from disk */
|
||||
Xfers int64 /* number of transfers to/from disk */
|
||||
Wblks int64 /* number of blocks written to disk */
|
||||
Rblks int64 /* number of blocks read from disk */
|
||||
QDepth int64 /* instantaneous "service" queue depth (number of requests sent to disk and not completed yet) */
|
||||
Time int64 /* amount of time disk is active */
|
||||
Adapter string /* disk adapter name */
|
||||
PathsCount int32 /* number of paths to this disk */
|
||||
QFull int64 /* "service" queue full occurrence count (number of times the disk is not accepting any more request) */
|
||||
Rserv int64 /* read or receive service time */
|
||||
RTimeOut int64 /* number of read request timeouts */
|
||||
Rfailed int64 /* number of failed read requests */
|
||||
MinRserv int64 /* min read or receive service time */
|
||||
MaxRserv int64 /* max read or receive service time */
|
||||
Wserv int64 /* write or send service time */
|
||||
WTimeOut int64 /* number of write request timeouts */
|
||||
Wfailed int64 /* number of failed write requests */
|
||||
MinWserv int64 /* min write or send service time */
|
||||
MaxWserv int64 /* max write or send service time */
|
||||
WqDepth int64 /* instantaneous wait queue depth (number of requests waiting to be sent to disk) */
|
||||
WqSampled int64 /* accumulated sampled dk_wq_depth */
|
||||
WqTime int64 /* accumulated wait queueing time */
|
||||
WqMinTime int64 /* min wait queueing time */
|
||||
WqMaxTime int64 /* max wait queueing time */
|
||||
QSampled int64 /* accumulated sampled dk_q_depth */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
PseudoDisk bool /*Indicates whether pseudo or physical disk */
|
||||
VTDisk bool /* 1- Virtual Target Disk, 0 - Others */
|
||||
}
|
||||
|
||||
type DiskPath struct {
|
||||
Name string /* name of the path */
|
||||
XRate int64 /* __rxfers: number of reads via the path */
|
||||
Xfers int64 /* number of transfers via the path */
|
||||
Rblks int64 /* 512 bytes blocks written via the path */
|
||||
Wblks int64 /* 512 bytes blocks read via the path */
|
||||
Time int64 /* amount of time disks are active */
|
||||
Adapter string /* disk adapter name (from ODM) */
|
||||
QFull int64 /* "service" queue full occurrence count (number of times the disk is not accepting any more request) */
|
||||
Rserv int64 /* read or receive service time */
|
||||
RTimeOut int64 /* number of read request timeouts */
|
||||
Rfailed int64 /* number of failed read requests */
|
||||
MinRserv int64 /* min read or receive service time */
|
||||
MaxRserv int64 /* max read or receive service time */
|
||||
Wserv int64 /* write or send service time */
|
||||
WTimeOut int64 /* number of write request timeouts */
|
||||
Wfailed int64 /* number of failed write requests */
|
||||
MinWserv int64 /* min write or send service time */
|
||||
MaxWserv int64 /* max write or send service time */
|
||||
WqDepth int64 /* instantaneous wait queue depth (number of requests waiting to be sent to disk) */
|
||||
WqSampled int64 /* accumulated sampled dk_wq_depth */
|
||||
WqTime int64 /* accumulated wait queueing time */
|
||||
WqMinTime int64 /* min wait queueing time */
|
||||
WqMaxTime int64 /* max wait queueing time */
|
||||
QSampled int64 /* accumulated sampled dk_q_depth */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
}
|
||||
|
||||
const (
|
||||
FC_DOWN = 0 // FC Adapter state is DOWN
|
||||
FC_UP = 1 // FC Adapter state is UP
|
||||
)
|
||||
|
||||
const (
|
||||
FCT_FCHBA = 0 // FC type - real Fiber Channel Adapter
|
||||
FCT_VFC = 1 // FC type - virtual Fiber Channel
|
||||
)
|
||||
|
||||
type FCAdapter struct {
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
Name string /* name of the adapter */
|
||||
State int32 /* FC Adapter state UP or DOWN */
|
||||
InputRequests int64 /* Number of Input Requests*/
|
||||
OutputRequests int64 /* Number of Output Requests */
|
||||
InputBytes int64 /* Number of Input Bytes */
|
||||
OutputBytes int64 /* Number of Output Bytes */
|
||||
EffMaxTransfer int64 /* Adapter's Effective Maximum Transfer Value */
|
||||
NoDMAResourceCnt int64 /* Count of DMA failures due to no DMA Resource available */
|
||||
NoCmdResourceCnt int64 /* Count of failures to allocate a command due to no command resource available */
|
||||
AttentionType int32 /* Link up or down Indicator */
|
||||
SecondsSinceLastReset int64 /* Displays the seconds since last reset of the statistics on the adapter */
|
||||
TxFrames int64 /* Number of frames transmitted */
|
||||
TxWords int64 /* Fiber Channel Kbytes transmitted */
|
||||
RxFrames int64 /* Number of Frames Received */
|
||||
RxWords int64 /* Fiber Channel Kbytes Received */
|
||||
LIPCount int64 /* Count of LIP (Loop Initialization Protocol) Events received in case we have FC-AL */
|
||||
NOSCount int64 /* Count of NOS (Not_Operational) Events. This indicates a link failure state. */
|
||||
ErrorFrames int64 /* Number of frames received with the CRC Error */
|
||||
DumpedFrames int64 /* Number of lost frames */
|
||||
LinkFailureCount int64 /* Count of Link failures */
|
||||
LossofSyncCount int64 /* Count of loss of sync */
|
||||
LossofSignal int64 /* Count of loss of Signal */
|
||||
PrimitiveSeqProtocolErrCount int64 /* number of times a primitive sequence was in error */
|
||||
InvalidTxWordCount int64 /* Count of Invalid Transmission words received */
|
||||
InvalidCRCCount int64 /* Count of CRC Errors in a Received Frame */
|
||||
PortFcId int64 /* SCSI Id of the adapter */
|
||||
PortSpeed int64 /* Speed of Adapter in GBIT */
|
||||
PortType string /* Type of connection. The Possible Values are Fabric, Private Loop, Point-to-Point, unknown */
|
||||
PortWWN int64 /* World Wide Port name */
|
||||
PortSupportedSpeed int64 /* Supported Port Speed in GBIT */
|
||||
AdapterType int /* 0 - Fiber Chanel, 1 - Virtual Fiber Chanel Adapter */
|
||||
VfcName string /* name of the Virtual Fiber Chanel(VFC) adapter */
|
||||
ClientPartName string /* name of the client partition */
|
||||
}
|
||||
|
|
@ -0,0 +1,195 @@
|
|||
package perfstat
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FileSystem struct {
|
||||
Device string /* name of the mounted device */
|
||||
MountPoint string /* where the device is mounted */
|
||||
FSType int /* File system type, see the constants below */
|
||||
Flags int /* Flags of the file system */
|
||||
TotalBlocks int64 /* number of 512 bytes blocks in the filesystem */
|
||||
FreeBlocks int64 /* number of free 512 bytes block in the filesystem */
|
||||
TotalInodes int64 /* total number of inodes in the filesystem */
|
||||
FreeInodes int64 /* number of free inodes in the filesystem */
|
||||
}
|
||||
|
||||
func (f *FileSystem) TypeString() string {
|
||||
switch f.FSType {
|
||||
case FS_JFS2:
|
||||
return "jfs2"
|
||||
case FS_NAMEFS:
|
||||
return "namefs"
|
||||
case FS_NFS:
|
||||
return "nfs"
|
||||
case FS_JFS:
|
||||
return "jfs"
|
||||
case FS_CDROM:
|
||||
return "cdrfs"
|
||||
case FS_PROCFS:
|
||||
return "procfs"
|
||||
case FS_SFS:
|
||||
return "sfs"
|
||||
case FS_CACHEFS:
|
||||
return "cachefs"
|
||||
case FS_NFS3:
|
||||
return "nfs3"
|
||||
case FS_AUTOFS:
|
||||
return "autofs"
|
||||
case FS_POOLFS:
|
||||
return "poolfs"
|
||||
case FS_VXFS:
|
||||
return "vxfs"
|
||||
case FS_VXODM:
|
||||
return "vxodm"
|
||||
case FS_UDF:
|
||||
return "udfs"
|
||||
case FS_NFS4:
|
||||
return "nfs4"
|
||||
case FS_RFS4:
|
||||
return "rfs4"
|
||||
case FS_CIFS:
|
||||
return "cifs"
|
||||
case FS_PMEMFS:
|
||||
return "pmemfs"
|
||||
case FS_AHAFS:
|
||||
return "ahafs"
|
||||
case FS_STNFS:
|
||||
return "stnfs"
|
||||
case FS_ASMFS:
|
||||
return "asmfs"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func (f *FileSystem) FlagsString() string {
|
||||
var flags []string
|
||||
|
||||
switch {
|
||||
case f.Flags&VFS_READONLY != 0:
|
||||
flags = append(flags, "ro")
|
||||
case f.Flags&VFS_REMOVABLE != 0:
|
||||
flags = append(flags, "removable")
|
||||
case f.Flags&VFS_DEVMOUNT != 0:
|
||||
flags = append(flags, "local")
|
||||
case f.Flags&VFS_REMOTE != 0:
|
||||
flags = append(flags, "remote")
|
||||
case f.Flags&VFS_SYSV_MOUNT != 0:
|
||||
flags = append(flags, "sysv")
|
||||
case f.Flags&VFS_UNMOUNTING != 0:
|
||||
flags = append(flags, "unmounting")
|
||||
case f.Flags&VFS_NOSUID != 0:
|
||||
flags = append(flags, "nosuid")
|
||||
case f.Flags&VFS_NODEV != 0:
|
||||
flags = append(flags, "nodev")
|
||||
case f.Flags&VFS_NOINTEG != 0:
|
||||
flags = append(flags, "nointeg")
|
||||
case f.Flags&VFS_NOMANAGER != 0:
|
||||
flags = append(flags, "nomanager")
|
||||
case f.Flags&VFS_NOCASE != 0:
|
||||
flags = append(flags, "nocase")
|
||||
case f.Flags&VFS_UPCASE != 0:
|
||||
flags = append(flags, "upcase")
|
||||
case f.Flags&VFS_NBC != 0:
|
||||
flags = append(flags, "nbc")
|
||||
case f.Flags&VFS_MIND != 0:
|
||||
flags = append(flags, "mind")
|
||||
case f.Flags&VFS_RBR != 0:
|
||||
flags = append(flags, "rbr")
|
||||
case f.Flags&VFS_RBW != 0:
|
||||
flags = append(flags, "rbw")
|
||||
case f.Flags&VFS_DISCONNECTED != 0:
|
||||
flags = append(flags, "disconnected")
|
||||
case f.Flags&VFS_SHUTDOWN != 0:
|
||||
flags = append(flags, "shutdown")
|
||||
case f.Flags&VFS_VMOUNTOK != 0:
|
||||
flags = append(flags, "vmountok")
|
||||
case f.Flags&VFS_SUSER != 0:
|
||||
flags = append(flags, "suser")
|
||||
case f.Flags&VFS_SOFT_MOUNT != 0:
|
||||
flags = append(flags, "soft")
|
||||
case f.Flags&VFS_UNMOUNTED != 0:
|
||||
flags = append(flags, "unmounted")
|
||||
case f.Flags&VFS_DEADMOUNT != 0:
|
||||
flags = append(flags, "deadmount")
|
||||
case f.Flags&VFS_SNAPSHOT != 0:
|
||||
flags = append(flags, "snapshot")
|
||||
case f.Flags&VFS_VCM_ON != 0:
|
||||
flags = append(flags, "vcm_on")
|
||||
case f.Flags&VFS_VCM_MONITOR != 0:
|
||||
flags = append(flags, "vcm_monitor")
|
||||
case f.Flags&VFS_ATIMEOFF != 0:
|
||||
flags = append(flags, "noatime")
|
||||
case f.Flags&VFS_READMOSTLY != 0:
|
||||
flags = append(flags, "readmostly")
|
||||
case f.Flags&VFS_CIOR != 0:
|
||||
flags = append(flags, "cior")
|
||||
case f.Flags&VFS_CIO != 0:
|
||||
flags = append(flags, "cio")
|
||||
case f.Flags&VFS_DIO != 0:
|
||||
flags = append(flags, "dio")
|
||||
}
|
||||
|
||||
return strings.Join(flags, ",")
|
||||
}
|
||||
|
||||
// Filesystem types
|
||||
const (
|
||||
FS_JFS2 = 0 /* AIX physical fs "jfs2" */
|
||||
FS_NAMEFS = 1 /* AIX pseudo fs "namefs" */
|
||||
FS_NFS = 2 /* SUN Network File System "nfs" */
|
||||
FS_JFS = 3 /* AIX R3 physical fs "jfs" */
|
||||
FS_CDROM = 5 /* CDROM File System "cdrom" */
|
||||
FS_PROCFS = 6 /* PROCFS File System "proc" */
|
||||
FS_SFS = 16 /* AIX Special FS (STREAM mounts) */
|
||||
FS_CACHEFS = 17 /* Cachefs file system */
|
||||
FS_NFS3 = 18 /* NFSv3 file system */
|
||||
FS_AUTOFS = 19 /* Automount file system */
|
||||
FS_POOLFS = 20 /* Pool file system */
|
||||
FS_VXFS = 32 /* THRPGIO File System "vxfs" */
|
||||
FS_VXODM = 33 /* For Veritas File System */
|
||||
FS_UDF = 34 /* UDFS file system */
|
||||
FS_NFS4 = 35 /* NFSv4 file system */
|
||||
FS_RFS4 = 36 /* NFSv4 Pseudo file system */
|
||||
FS_CIFS = 37 /* AIX SMBFS (CIFS client) */
|
||||
FS_PMEMFS = 38 /* MCR Async Mobility pseudo file system */
|
||||
FS_AHAFS = 39 /* AHAFS File System "aha" */
|
||||
FS_STNFS = 40 /* Short-Term NFS */
|
||||
FS_ASMFS = 41 /* Oracle ASM FS */
|
||||
)
|
||||
|
||||
// Filesystem flags
|
||||
const (
|
||||
VFS_READONLY = 0x00000001 /* rdonly access to vfs */
|
||||
VFS_REMOVABLE = 0x00000002 /* removable (diskette) media */
|
||||
VFS_DEVMOUNT = 0x00000004 /* physical device mount */
|
||||
VFS_REMOTE = 0x00000008 /* file system is on network */
|
||||
VFS_SYSV_MOUNT = 0x00000010 /* System V style mount */
|
||||
VFS_UNMOUNTING = 0x00000020 /* originated by unmount() */
|
||||
VFS_NOSUID = 0x00000040 /* don't maintain suid-ness across this mount */
|
||||
VFS_NODEV = 0x00000080 /* don't allow device access across this mount */
|
||||
VFS_NOINTEG = 0x00000100 /* no integrity mount option */
|
||||
VFS_NOMANAGER = 0x00000200 /* mount managed fs w/o manager */
|
||||
VFS_NOCASE = 0x00000400 /* do not map dir names */
|
||||
VFS_UPCASE = 0x00000800 /* map dir names to uppercase */
|
||||
VFS_NBC = 0x00001000 /* NBC cached file in this vfs */
|
||||
VFS_MIND = 0x00002000 /* multi-segment .indirect */
|
||||
VFS_RBR = 0x00004000 /* Release-behind when reading */
|
||||
VFS_RBW = 0x00008000 /* Release-behind when writing */
|
||||
VFS_DISCONNECTED = 0x00010000 /* file mount not in use */
|
||||
VFS_SHUTDOWN = 0x00020000 /* forced unmount for shutdown */
|
||||
VFS_VMOUNTOK = 0x00040000 /* dir/file mnt permission flag */
|
||||
VFS_SUSER = 0x00080000 /* client-side suser perm. flag */
|
||||
VFS_SOFT_MOUNT = 0x00100000 /* file-over-file or directory over directory "soft" mount */
|
||||
VFS_UNMOUNTED = 0x00200000 /* unmount completed, stale vnodes are left in the vfs */
|
||||
VFS_DEADMOUNT = 0x00400000 /* softmount vfs should be disconnected at last vnode free */
|
||||
VFS_SNAPSHOT = 0x00800000 /* snapshot mount */
|
||||
VFS_VCM_ON = 0x01000000 /* VCM is currently active */
|
||||
VFS_VCM_MONITOR = 0x02000000 /* VCM monitoring is active */
|
||||
VFS_ATIMEOFF = 0x04000000 /* no atime updates during i/o */
|
||||
VFS_READMOSTLY = 0x10000000 /* ROFS allows open for write */
|
||||
VFS_CIOR = 0x20000000 /* O_CIOR mount */
|
||||
VFS_CIO = 0x40000000 /* O_CIO mount */
|
||||
VFS_DIO = 0x80000000 /* O_DIRECT mount */
|
||||
)
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package perfstat
|
||||
|
||||
type PartitionType struct {
|
||||
SmtCapable bool /* OS supports SMT mode */
|
||||
SmtEnabled bool /* SMT mode is on */
|
||||
LparCapable bool /* OS supports logical partitioning */
|
||||
LparEnabled bool /* logical partitioning is on */
|
||||
SharedCapable bool /* OS supports shared processor LPAR */
|
||||
SharedEnabled bool /* partition runs in shared mode */
|
||||
DLparCapable bool /* OS supports dynamic LPAR */
|
||||
Capped bool /* partition is capped */
|
||||
Kernel64bit bool /* kernel is 64 bit */
|
||||
PoolUtilAuthority bool /* pool utilization available */
|
||||
DonateCapable bool /* capable of donating cycles */
|
||||
DonateEnabled bool /* enabled for donating cycles */
|
||||
AmsCapable bool /* 1 = AMS(Active Memory Sharing) capable, 0 = Not AMS capable */
|
||||
AmsEnabled bool /* 1 = AMS(Active Memory Sharing) enabled, 0 = Not AMS enabled */
|
||||
PowerSave bool /*1= Power saving mode is enabled*/
|
||||
AmeEnabled bool /* Active Memory Expansion is enabled */
|
||||
SharedExtended bool
|
||||
}
|
||||
|
||||
type PartitionValue struct {
|
||||
Online int64
|
||||
Max int64
|
||||
Min int64
|
||||
Desired int64
|
||||
}
|
||||
|
||||
type PartitionConfig struct {
|
||||
Version int64 /* Version number */
|
||||
Name string /* Partition Name */
|
||||
Node string /* Node Name */
|
||||
Conf PartitionType /* Partition Properties */
|
||||
Number int32 /* Partition Number */
|
||||
GroupID int32 /* Group ID */
|
||||
ProcessorFamily string /* Processor Type */
|
||||
ProcessorModel string /* Processor Model */
|
||||
MachineID string /* Machine ID */
|
||||
ProcessorMhz float64 /* Processor Clock Speed in MHz */
|
||||
NumProcessors PartitionValue /* Number of Configured Physical Processors in frame*/
|
||||
OSName string /* Name of Operating System */
|
||||
OSVersion string /* Version of operating System */
|
||||
OSBuild string /* Build of Operating System */
|
||||
LCpus int32 /* Number of Logical CPUs */
|
||||
SmtThreads int32 /* Number of SMT Threads */
|
||||
Drives int32 /* Total Number of Drives */
|
||||
NetworkAdapters int32 /* Total Number of Network Adapters */
|
||||
CpuCap PartitionValue /* Min, Max and Online CPU Capacity */
|
||||
Weightage int32 /* Variable Processor Capacity Weightage */
|
||||
EntCapacity int32 /* number of processor units this partition is entitled to receive */
|
||||
VCpus PartitionValue /* Min, Max and Online Virtual CPUs */
|
||||
PoolID int32 /* Shared Pool ID of physical processors, to which this partition belongs*/
|
||||
ActiveCpusInPool int32 /* Count of physical CPUs in the shared processor pool, to which this partition belongs */
|
||||
PoolWeightage int32 /* Pool Weightage */
|
||||
SharedPCpu int32 /* Number of physical processors allocated for shared processor use */
|
||||
MaxPoolCap int32 /* Maximum processor capacity of partition's pool */
|
||||
EntPoolCap int32 /* Entitled processor capacity of partition's pool */
|
||||
Mem PartitionValue /* Min, Max and Online Memory */
|
||||
MemWeightage int32 /* Variable Memory Capacity Weightage */
|
||||
TotalIOMemoryEntitlement int64 /* I/O Memory Entitlement of the partition in bytes */
|
||||
MemPoolID int32 /* AMS pool id of the pool the LPAR belongs to */
|
||||
HyperPgSize int64 /* Hypervisor page size in KB*/
|
||||
ExpMem PartitionValue /* Min, Max and Online Expanded Memory */
|
||||
TargetMemExpFactor int64 /* Target Memory Expansion Factor scaled by 100 */
|
||||
TargetMemExpSize int64 /* Expanded Memory Size in MB */
|
||||
SubProcessorMode int32 /* Split core mode, its value can be 0,1,2 or 4. 0 for unsupported, 1 for capable but not enabled, 2 or 4 for enabled*/
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package perfstat
|
||||
|
||||
type LogicalVolume struct {
|
||||
Name string /* logical volume name */
|
||||
VGName string /* volume group name */
|
||||
OpenClose int64 /* LVM_QLVOPEN, etc. (see lvm.h) */
|
||||
State int64 /* LVM_UNDEF, etc. (see lvm.h) */
|
||||
MirrorPolicy int64 /* LVM_PARALLEL, etc. (see lvm.h) */
|
||||
MirrorWriteConsistency int64 /* LVM_CONSIST, etc. (see lvm.h) */
|
||||
WriteVerify int64 /* LVM_VERIFY, etc. (see lvm.h) */
|
||||
PPsize int64 /* physical partition size in MB */
|
||||
LogicalPartitions int64 /* total number of logical paritions configured for this logical volume */
|
||||
Mirrors int32 /* number of physical mirrors for each logical partition */
|
||||
IOCnt int64 /* Number of read and write requests */
|
||||
KBReads int64 /* Number of Kilobytes read */
|
||||
KBWrites int64 /* Number of Kilobytes written */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
}
|
||||
|
||||
type VolumeGroup struct {
|
||||
Name string /* volume group name */
|
||||
TotalDisks int64 /* number of physical volumes in the volume group */
|
||||
ActiveDisks int64 /* number of active physical volumes in the volume group */
|
||||
TotalLogicalVolumes int64 /* number of logical volumes in the volume group */
|
||||
OpenedLogicalVolumes int64 /* number of logical volumes opened in the volume group */
|
||||
IOCnt int64 /* Number of read and write requests */
|
||||
KBReads int64 /* Number of Kilobytes read */
|
||||
KBWrites int64 /* Number of Kilobytes written */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
VariedState int /* Indicates volume group available or not */
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
package perfstat
|
||||
|
||||
type MemoryTotal struct {
|
||||
VirtualTotal int64 /* total virtual memory (in 4KB pages) */
|
||||
RealTotal int64 /* total real memory (in 4KB pages) */
|
||||
RealFree int64 /* free real memory (in 4KB pages) */
|
||||
RealPinned int64 /* real memory which is pinned (in 4KB pages) */
|
||||
RealInUse int64 /* real memory which is in use (in 4KB pages) */
|
||||
BadPages int64 /* number of bad pages */
|
||||
PageFaults int64 /* number of page faults */
|
||||
PageIn int64 /* number of pages paged in */
|
||||
PageOut int64 /* number of pages paged out */
|
||||
PgSpIn int64 /* number of page ins from paging space */
|
||||
PgSpOut int64 /* number of page outs from paging space */
|
||||
Scans int64 /* number of page scans by clock */
|
||||
Cycles int64 /* number of page replacement cycles */
|
||||
PgSteals int64 /* number of page steals */
|
||||
NumPerm int64 /* number of frames used for files (in 4KB pages) */
|
||||
PgSpTotal int64 /* total paging space (in 4KB pages) */
|
||||
PgSpFree int64 /* free paging space (in 4KB pages) */
|
||||
PgSpRsvd int64 /* reserved paging space (in 4KB pages) */
|
||||
RealSystem int64 /* real memory used by system segments (in 4KB pages). */
|
||||
RealUser int64 /* real memory used by non-system segments (in 4KB pages). */
|
||||
RealProcess int64 /* real memory used by process segments (in 4KB pages). */
|
||||
VirtualActive int64 /* Active virtual pages. Virtual pages are considered active if they have been accessed */
|
||||
IOME int64 /* I/O memory entitlement of the partition in bytes*/
|
||||
IOMU int64 /* I/O memory entitlement of the partition in use in bytes*/
|
||||
IOHWM int64 /* High water mark of I/O memory entitlement used in bytes*/
|
||||
PMem int64 /* Amount of physical mmeory currently backing partition's logical memory in bytes*/
|
||||
CompressedTotal int64 /* Total numbers of pages in compressed pool (in 4KB pages) */
|
||||
CompressedWSegPg int64 /* Number of compressed working storage pages */
|
||||
CPgIn int64 /* number of page ins to compressed pool */
|
||||
CPgOut int64 /* number of page outs from compressed pool */
|
||||
TrueSize int64 /* True Memory Size in 4KB pages */
|
||||
ExpandedMemory int64 /* Expanded Memory Size in 4KB pages */
|
||||
CompressedWSegSize int64 /* Total size of the compressed working storage pages in the pool */
|
||||
TargetCPoolSize int64 /* Target Compressed Pool Size in bytes */
|
||||
MaxCPoolSize int64 /* Max Size of Compressed Pool in bytes */
|
||||
MinUCPoolSize int64 /* Min Size of Uncompressed Pool in bytes */
|
||||
CPoolSize int64 /* Compressed Pool size in bytes */
|
||||
UCPoolSize int64 /* Uncompressed Pool size in bytes */
|
||||
CPoolInUse int64 /* Compressed Pool Used in bytes */
|
||||
UCPoolInUse int64 /* Uncompressed Pool Used in bytes */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
RealAvailable int64 /* number of pages (in 4KB pages) of memory available without paging out working segments */
|
||||
BytesCoalesced int64 /* The number of bytes of the calling partition.s logical real memory coalesced because they contained duplicated data */
|
||||
BytesCoalescedMemPool int64 /* number of bytes of logical real memory coalesced because they contained duplicated data in the calling partition.s memory */
|
||||
}
|
||||
|
||||
type MemoryPage struct {
|
||||
PSize int64 /* page size in bytes */
|
||||
RealTotal int64 /* number of real memory frames of this page size */
|
||||
RealFree int64 /* number of pages on free list */
|
||||
RealPinned int64 /* number of pages pinned */
|
||||
RealInUse int64 /* number of pages in use */
|
||||
PgExct int64 /* number of page faults */
|
||||
PgIns int64 /* number of pages paged in */
|
||||
PgOuts int64 /* number of pages paged out */
|
||||
PgSpIns int64 /* number of page ins from paging space */
|
||||
PgSpOuts int64 /* number of page outs from paging space */
|
||||
Scans int64 /* number of page scans by clock */
|
||||
Cycles int64 /* number of page replacement cycles */
|
||||
PgSteals int64 /* number of page steals */
|
||||
NumPerm int64 /* number of frames used for files */
|
||||
NumPgSp int64 /* number of pages with allocated paging space */
|
||||
RealSystem int64 /* number of pages used by system segments. */
|
||||
RealUser int64 /* number of pages used by non-system segments. */
|
||||
RealProcess int64 /* number of pages used by process segments. */
|
||||
VirtActive int64 /* Active virtual pages. */
|
||||
ComprsdTotal int64 /* Number of pages of this size compressed */
|
||||
ComprsdWsegPgs int64 /* Number of compressed working storage pages */
|
||||
CPgIns int64 /* number of page ins of this page size to compressed pool */
|
||||
CPgOuts int64 /* number of page outs of this page size from compressed pool */
|
||||
CPoolInUse int64 /* Compressed Size of this page size in Compressed Pool */
|
||||
UCPoolSize int64 /* Uncompressed Pool size in bytes of this page size */
|
||||
ComprsdWsegSize int64 /* Total size of the compressed working storage pages in the pool */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
RealAvail int64 /* number of pages (in 4KB pages) of memory available without paging out working segments */
|
||||
}
|
||||
|
||||
// paging space types
|
||||
const (
|
||||
LV_PAGING = 1
|
||||
NFS_PAGING = 2
|
||||
UNKNOWN_PAGING = 3
|
||||
)
|
||||
|
||||
type PagingSpace struct {
|
||||
Name string /* Paging space name */
|
||||
Type uint8 /* type of paging device (LV_PAGING or NFS_PAGING) */
|
||||
VGName string /* volume group name */
|
||||
Hostname string /* host name of paging server */
|
||||
Filename string /* swap file name on server */
|
||||
LPSize int64 /* size in number of logical partitions */
|
||||
MBSize int64 /* size in megabytes */
|
||||
MBUsed int64 /* portion used in megabytes */
|
||||
IOPending int64 /* number of pending I/O */
|
||||
Active uint8 /* indicates if active (1 if so, 0 if not) */
|
||||
Automatic uint8 /* indicates if automatic (1 if so, 0 if not) */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
}
|
||||
|
|
@ -0,0 +1,163 @@
|
|||
package perfstat
|
||||
|
||||
// Network Interface types
|
||||
const (
|
||||
IFT_OTHER = 0x1
|
||||
IFT_1822 = 0x2 /* old-style arpanet imp */
|
||||
IFT_HDH1822 = 0x3 /* HDH arpanet imp */
|
||||
IFT_X25DDN = 0x4 /* x25 to imp */
|
||||
IFT_X25 = 0x5 /* PDN X25 interface (RFC877) */
|
||||
IFT_ETHER = 0x6 /* Ethernet CSMACD */
|
||||
IFT_ISO88023 = 0x7 /* CMSA CD */
|
||||
IFT_ISO88024 = 0x8 /* Token Bus */
|
||||
IFT_ISO88025 = 0x9 /* Token Ring */
|
||||
IFT_ISO88026 = 0xa /* MAN */
|
||||
IFT_STARLAN = 0xb
|
||||
IFT_P10 = 0xc /* Proteon 10MBit ring */
|
||||
IFT_P80 = 0xd /* Proteon 10MBit ring */
|
||||
IFT_HY = 0xe /* Hyperchannel */
|
||||
IFT_FDDI = 0xf
|
||||
IFT_LAPB = 0x10
|
||||
IFT_SDLC = 0x11
|
||||
IFT_T1 = 0x12
|
||||
IFT_CEPT = 0x13 /* E1 - european T1 */
|
||||
IFT_ISDNBASIC = 0x14
|
||||
IFT_ISDNPRIMARY = 0x15
|
||||
IFT_PTPSERIAL = 0x16 /* Proprietary PTP serial */
|
||||
IFT_PPP = 0x17 /* RFC 1331 */
|
||||
IFT_LOOP = 0x18 /* loopback */
|
||||
IFT_EON = 0x19 /* ISO over IP */
|
||||
IFT_XETHER = 0x1a /* obsolete 3MB experimental ethernet */
|
||||
IFT_NSIP = 0x1b /* XNS over IP */
|
||||
IFT_SLIP = 0x1c /* IP over generic TTY */
|
||||
IFT_ULTRA = 0x1d /* Ultra Technologies */
|
||||
IFT_DS3 = 0x1e /* Generic T3 */
|
||||
IFT_SIP = 0x1f /* SMDS */
|
||||
IFT_FRELAY = 0x20 /* Frame Relay DTE only */
|
||||
IFT_RS232 = 0x21
|
||||
IFT_PARA = 0x22 /* parallel-port */
|
||||
IFT_ARCNET = 0x23
|
||||
IFT_ARCNETPLUS = 0x24
|
||||
IFT_ATM = 0x25 /* ATM cells */
|
||||
IFT_MIOX25 = 0x26
|
||||
IFT_SONET = 0x27 /* SONET or SDH */
|
||||
IFT_X25PLE = 0x28
|
||||
IFT_ISO88022LLC = 0x29
|
||||
IFT_LOCALTALK = 0x2a
|
||||
IFT_SMDSDXI = 0x2b
|
||||
IFT_FRELAYDCE = 0x2c /* Frame Relay DCE */
|
||||
IFT_V35 = 0x2d
|
||||
IFT_HSSI = 0x2e
|
||||
IFT_HIPPI = 0x2f
|
||||
IFT_MODEM = 0x30 /* Generic Modem */
|
||||
IFT_AAL5 = 0x31 /* AAL5 over ATM */
|
||||
IFT_SONETPATH = 0x32
|
||||
IFT_SONETVT = 0x33
|
||||
IFT_SMDSICIP = 0x34 /* SMDS InterCarrier Interface */
|
||||
IFT_PROPVIRTUAL = 0x35 /* Proprietary Virtual/internal */
|
||||
IFT_PROPMUX = 0x36 /* Proprietary Multiplexing */
|
||||
IFT_VIPA = 0x37 /* Virtual Interface */
|
||||
IFT_SN = 0x38 /* Federation Switch */
|
||||
IFT_SP = 0x39 /* SP switch */
|
||||
IFT_FCS = 0x3a /* IP over Fiber Channel */
|
||||
IFT_TUNNEL = 0x3b
|
||||
IFT_GIFTUNNEL = 0x3c /* IPv4 over IPv6 tunnel */
|
||||
IFT_HF = 0x3d /* Support for PERCS HFI*/
|
||||
IFT_CLUSTER = 0x3e /* cluster pseudo network interface */
|
||||
IFT_FB = 0xc7 /* IP over Infiniband. Number by IANA */
|
||||
)
|
||||
|
||||
type NetIfaceTotal struct {
|
||||
Number int32 /* number of network interfaces */
|
||||
IPackets int64 /* number of packets received on interface */
|
||||
IBytes int64 /* number of bytes received on interface */
|
||||
IErrors int64 /* number of input errors on interface */
|
||||
OPackets int64 /* number of packets sent on interface */
|
||||
OBytes int64 /* number of bytes sent on interface */
|
||||
OErrors int64 /* number of output errors on interface */
|
||||
Collisions int64 /* number of collisions on csma interface */
|
||||
XmitDrops int64 /* number of packets not transmitted */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
}
|
||||
|
||||
type NetIface struct {
|
||||
Name string /* name of the interface */
|
||||
Description string /* interface description (from ODM, similar to lscfg output) */
|
||||
Type uint8 /* ethernet, tokenring, etc. interpretation can be done using /usr/include/net/if_types.h */
|
||||
MTU int64 /* network frame size */
|
||||
IPackets int64 /* number of packets received on interface */
|
||||
IBytes int64 /* number of bytes received on interface */
|
||||
IErrors int64 /* number of input errors on interface */
|
||||
OPackets int64 /* number of packets sent on interface */
|
||||
OBytes int64 /* number of bytes sent on interface */
|
||||
OErrors int64 /* number of output errors on interface */
|
||||
Collisions int64 /* number of collisions on csma interface */
|
||||
Bitrate int64 /* adapter rating in bit per second */
|
||||
XmitDrops int64 /* number of packets not transmitted */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
IfIqDrops int64 /* Dropped on input, this interface */
|
||||
IfArpDrops int64 /* Dropped because no arp response */
|
||||
}
|
||||
|
||||
type NetBuffer struct {
|
||||
Name string /* size in ascii, always power of 2 (ex: "32", "64", "128") */
|
||||
InUse int64 /* number of buffer currently allocated */
|
||||
Calls int64 /* number of buffer allocations since last reset */
|
||||
Delayed int64 /* number of delayed allocations */
|
||||
Free int64 /* number of free calls */
|
||||
Failed int64 /* number of failed allocations */
|
||||
HighWatermark int64 /* high threshold for number of buffer allocated */
|
||||
Freed int64 /* number of buffers freed */
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
}
|
||||
|
||||
// Network adapter types
|
||||
const (
|
||||
NET_PHY = 0 /* physical device */
|
||||
NET_SEA = 1 /* shared ethernet adapter */
|
||||
NET_VIR = 2 /* virtual device */
|
||||
NET_HEA = 3 /* host ethernet adapter */
|
||||
NET_EC = 4 /* etherchannel */
|
||||
NET_VLAN = 5 /* vlan pseudo device */
|
||||
)
|
||||
|
||||
type NetAdapter struct {
|
||||
Version int64 /* version number (1,2, etc) */
|
||||
Name string /* name of the adapter */
|
||||
TxPackets int64 /* Transmit Packets on interface */
|
||||
TxBytes int64 /* Transmit Bytes on interface */
|
||||
TxInterrupts int64 /* Transfer Interrupts */
|
||||
TxErrors int64 /* Transmit Errors */
|
||||
TxPacketsDropped int64 /* Packets Dropped at the time of Data Transmission */
|
||||
TxQueueSize int64 /* Maximum Packets on Software Transmit Queue */
|
||||
TxQueueLen int64 /* Transmission Queue Length */
|
||||
TxQueueOverflow int64 /* Transmission Queue Overflow */
|
||||
TxBroadcastPackets int64 /* Number of Broadcast Packets Transmitted */
|
||||
TxMulticastPackets int64 /* Number of Multicast packets Transmitted */
|
||||
TxCarrierSense int64 /* Lost Carrier Sense signal count */
|
||||
TxDMAUnderrun int64 /* Count of DMA Under-runs for Transmission */
|
||||
TxLostCTSErrors int64 /* The number of unsuccessful transmissions due to the loss of the Clear-to-Send signal error */
|
||||
TxMaxCollisionErrors int64 /* Maximum Collision Errors at Transmission */
|
||||
TxLateCollisionErrors int64 /* Late Collision Errors at Transmission */
|
||||
TxDeferred int64 /* The number of packets deferred for Transmission. */
|
||||
TxTimeoutErrors int64 /* Time Out Errors for Transmission */
|
||||
TxSingleCollisionCount int64 /* Count of Single Collision error at Transmission */
|
||||
TxMultipleCollisionCount int64 /* Count of Multiple Collision error at Transmission */
|
||||
RxPackets int64 /* Receive Packets on interface */
|
||||
RxBytes int64 /* Receive Bytes on interface */
|
||||
RxInterrupts int64 /* Receive Interrupts */
|
||||
RxErrors int64 /* Input errors on interface */
|
||||
RxPacketsDropped int64 /* The number of packets accepted by the device driver for transmission which were not (for any reason) given to the device. */
|
||||
RxBadPackets int64 /* Count of Bad Packets Received. */
|
||||
RxMulticastPackets int64 /* Number of MultiCast Packets Received */
|
||||
RxBroadcastPackets int64 /* Number of Broadcast Packets Received */
|
||||
RxCRCErrors int64 /* Count of Packets Received with CRC errors */
|
||||
RxDMAOverrun int64 /* Count of DMA over-runs for Data Receival. */
|
||||
RxAlignmentErrors int64 /* Packets Received with Alignment Error */
|
||||
RxNoResourceErrors int64 /* Packets Received with No Resource Errors */
|
||||
RxCollisionErrors int64 /* Packets Received with Collision errors */
|
||||
RxPacketTooShortErrors int64 /* Count of Short Packets Received. */
|
||||
RxPacketTooLongErrors int64 /* Count of Too Long Packets Received. */
|
||||
RxPacketDiscardedByAdapter int64 /* Count of Received Packets discarded by Adapter. */
|
||||
AdapterType int32 /* 0 - Physical, 1 - SEA, 2 - Virtual, 3 -HEA */
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package perfstat
|
||||
|
||||
type Process struct {
|
||||
Version int64 /* version number (1, 2, etc.,) */
|
||||
PID int64 /* Process ID */
|
||||
ProcessName string /* Name of The Process */
|
||||
Priority int32 /* Process Priority */
|
||||
NumThreads int64 /* Thread Count */
|
||||
UID int64 /* Owner Info */
|
||||
ClassID int64 /* WLM Class Name */
|
||||
Size int64 /* Virtual Size of the Process in KB(Exclusive Usage, Leaving all Shared Library Text & Shared File Pages, Shared Memory, Memory Mapped) */
|
||||
RealMemData int64 /* Real Memory used for Data in KB */
|
||||
RealMemText int64 /* Real Memory used for Text in KB */
|
||||
VirtMemData int64 /* Virtual Memory used to Data in KB */
|
||||
VirtMemText int64 /* Virtual Memory used for Text in KB */
|
||||
SharedLibDataSize int64 /* Data Size from Shared Library in KB */
|
||||
HeapSize int64 /* Heap Size in KB */
|
||||
RealInUse int64 /* The Real memory in use(in KB) by the process including all kind of segments (excluding system segments). This includes Text, Data, Shared Library Text, Shared Library Data, File Pages, Shared Memory & Memory Mapped */
|
||||
VirtInUse int64 /* The Virtual memory in use(in KB) by the process including all kind of segments (excluding system segments). This includes Text, Data, Shared Library Text, Shared Library Data, File Pages, Shared Memory & Memory Mapped */
|
||||
Pinned int64 /* Pinned Memory(in KB) for this process inclusive of all segments */
|
||||
PgSpInUse int64 /* Paging Space used(in KB) inclusive of all segments */
|
||||
FilePages int64 /* File Pages used(in KB) including shared pages */
|
||||
RealInUseMap int64 /* Real memory used(in KB) for Shared Memory and Memory Mapped regions */
|
||||
VirtInUseMap int64 /* Virtual Memory used(in KB) for Shared Memory and Memory Mapped regions */
|
||||
PinnedInUseMap int64 /* Pinned memory(in KB) for Shared Memory and Memory Mapped regions */
|
||||
UCpuTime float64 /* User Mode CPU time will be in percentage or milliseconds based on, whether it is filled by perfstat_process_util or perfstat_process respectively. */
|
||||
SCpuTime float64 /* System Mode CPU time will be in percentage or milliseconds based on, whether it is filled by perfstat_process_util or perfstat_process respectively. */
|
||||
LastTimeBase int64 /* Timebase Counter */
|
||||
InBytes int64 /* Bytes Read from Disk */
|
||||
OutBytes int64 /* Bytes Written to Disk */
|
||||
InOps int64 /* In Operations from Disk */
|
||||
OutOps int64 /* Out Operations from Disk */
|
||||
}
|
||||
|
||||
type Thread struct {
|
||||
TID int64 /* thread identifier */
|
||||
PID int64 /* process identifier */
|
||||
CpuID int64 /* processor on which I'm bound */
|
||||
UCpuTime float64 /* User Mode CPU time will be in percentage or milliseconds based on, whether it is filled by perfstat_thread_util or perfstat_thread respectively. */
|
||||
SCpuTime float64 /* System Mode CPU time will be in percentage or milliseconds based on, whether it is filled by perfstat_thread_util or perfstat_thread respectively. */
|
||||
LastTimeBase int64 /* Timebase Counter */
|
||||
Version int64
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
// +build aix
|
||||
|
||||
package perfstat
|
||||
|
||||
/*
|
||||
#include "c_helpers.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func timeSince(ts uint64) uint64 {
|
||||
return uint64(time.Now().Unix()) - ts
|
||||
}
|
||||
|
||||
// BootTime() returns the time of the last boot in UNIX seconds
|
||||
func BootTime() (uint64, error) {
|
||||
sec := C.boottime()
|
||||
if sec == -1 {
|
||||
return 0, fmt.Errorf("Can't determine boot time")
|
||||
}
|
||||
return uint64(sec), nil
|
||||
}
|
||||
|
||||
// UptimeSeconds() calculates uptime in seconds
|
||||
func UptimeSeconds() (uint64, error) {
|
||||
boot, err := BootTime()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return timeSince(boot), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
gopsutil is distributed under BSD license reproduced below.
|
||||
|
||||
Copyright (c) 2014, WAKAYAMA Shirou
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of the gopsutil authors nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
-------
|
||||
internal/common/binary.go in the gopsutil is copied and modified from golang/encoding/binary.go.
|
||||
|
||||
|
||||
|
||||
Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package common
|
||||
|
||||
type EnvKeyType string
|
||||
|
||||
// EnvKey is a context key that can be used to set programmatically the environment
|
||||
// gopsutil relies on to perform calls against the OS.
|
||||
// Example of use:
|
||||
//
|
||||
// ctx := context.WithValue(context.Background(), common.EnvKey, EnvMap{common.HostProcEnvKey: "/myproc"})
|
||||
// avg, err := load.AvgWithContext(ctx)
|
||||
var EnvKey = EnvKeyType("env")
|
||||
|
||||
const (
|
||||
HostProcEnvKey EnvKeyType = "HOST_PROC"
|
||||
HostSysEnvKey EnvKeyType = "HOST_SYS"
|
||||
HostEtcEnvKey EnvKeyType = "HOST_ETC"
|
||||
HostVarEnvKey EnvKeyType = "HOST_VAR"
|
||||
HostRunEnvKey EnvKeyType = "HOST_RUN"
|
||||
HostDevEnvKey EnvKeyType = "HOST_DEV"
|
||||
HostRootEnvKey EnvKeyType = "HOST_ROOT"
|
||||
)
|
||||
|
||||
type EnvMap map[EnvKeyType]string
|
||||
|
|
@ -0,0 +1,200 @@
|
|||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
// TimesStat contains the amounts of time the CPU has spent performing different
|
||||
// kinds of work. Time units are in seconds. It is based on linux /proc/stat file.
|
||||
type TimesStat struct {
|
||||
CPU string `json:"cpu"`
|
||||
User float64 `json:"user"`
|
||||
System float64 `json:"system"`
|
||||
Idle float64 `json:"idle"`
|
||||
Nice float64 `json:"nice"`
|
||||
Iowait float64 `json:"iowait"`
|
||||
Irq float64 `json:"irq"`
|
||||
Softirq float64 `json:"softirq"`
|
||||
Steal float64 `json:"steal"`
|
||||
Guest float64 `json:"guest"`
|
||||
GuestNice float64 `json:"guestNice"`
|
||||
}
|
||||
|
||||
type InfoStat struct {
|
||||
CPU int32 `json:"cpu"`
|
||||
VendorID string `json:"vendorId"`
|
||||
Family string `json:"family"`
|
||||
Model string `json:"model"`
|
||||
Stepping int32 `json:"stepping"`
|
||||
PhysicalID string `json:"physicalId"`
|
||||
CoreID string `json:"coreId"`
|
||||
Cores int32 `json:"cores"`
|
||||
ModelName string `json:"modelName"`
|
||||
Mhz float64 `json:"mhz"`
|
||||
CacheSize int32 `json:"cacheSize"`
|
||||
Flags []string `json:"flags"`
|
||||
Microcode string `json:"microcode"`
|
||||
}
|
||||
|
||||
type lastPercent struct {
|
||||
sync.Mutex
|
||||
lastCPUTimes []TimesStat
|
||||
lastPerCPUTimes []TimesStat
|
||||
}
|
||||
|
||||
var (
|
||||
lastCPUPercent lastPercent
|
||||
invoke common.Invoker = common.Invoke{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
lastCPUPercent.Lock()
|
||||
lastCPUPercent.lastCPUTimes, _ = Times(false)
|
||||
lastCPUPercent.lastPerCPUTimes, _ = Times(true)
|
||||
lastCPUPercent.Unlock()
|
||||
}
|
||||
|
||||
// Counts returns the number of physical or logical cores in the system
|
||||
func Counts(logical bool) (int, error) {
|
||||
return CountsWithContext(context.Background(), logical)
|
||||
}
|
||||
|
||||
func (c TimesStat) String() string {
|
||||
v := []string{
|
||||
`"cpu":"` + c.CPU + `"`,
|
||||
`"user":` + strconv.FormatFloat(c.User, 'f', 1, 64),
|
||||
`"system":` + strconv.FormatFloat(c.System, 'f', 1, 64),
|
||||
`"idle":` + strconv.FormatFloat(c.Idle, 'f', 1, 64),
|
||||
`"nice":` + strconv.FormatFloat(c.Nice, 'f', 1, 64),
|
||||
`"iowait":` + strconv.FormatFloat(c.Iowait, 'f', 1, 64),
|
||||
`"irq":` + strconv.FormatFloat(c.Irq, 'f', 1, 64),
|
||||
`"softirq":` + strconv.FormatFloat(c.Softirq, 'f', 1, 64),
|
||||
`"steal":` + strconv.FormatFloat(c.Steal, 'f', 1, 64),
|
||||
`"guest":` + strconv.FormatFloat(c.Guest, 'f', 1, 64),
|
||||
`"guestNice":` + strconv.FormatFloat(c.GuestNice, 'f', 1, 64),
|
||||
}
|
||||
|
||||
return `{` + strings.Join(v, ",") + `}`
|
||||
}
|
||||
|
||||
// Deprecated: Total returns the total number of seconds in a CPUTimesStat
|
||||
// Please do not use this internal function.
|
||||
func (c TimesStat) Total() float64 {
|
||||
total := c.User + c.System + c.Idle + c.Nice + c.Iowait + c.Irq +
|
||||
c.Softirq + c.Steal + c.Guest + c.GuestNice
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
func (c InfoStat) String() string {
|
||||
s, _ := json.Marshal(c)
|
||||
return string(s)
|
||||
}
|
||||
|
||||
func getAllBusy(t TimesStat) (float64, float64) {
|
||||
tot := t.Total()
|
||||
if runtime.GOOS == "linux" {
|
||||
tot -= t.Guest // Linux 2.6.24+
|
||||
tot -= t.GuestNice // Linux 3.2.0+
|
||||
}
|
||||
|
||||
busy := tot - t.Idle - t.Iowait
|
||||
|
||||
return tot, busy
|
||||
}
|
||||
|
||||
func calculateBusy(t1, t2 TimesStat) float64 {
|
||||
t1All, t1Busy := getAllBusy(t1)
|
||||
t2All, t2Busy := getAllBusy(t2)
|
||||
|
||||
if t2Busy <= t1Busy {
|
||||
return 0
|
||||
}
|
||||
if t2All <= t1All {
|
||||
return 100
|
||||
}
|
||||
return math.Min(100, math.Max(0, (t2Busy-t1Busy)/(t2All-t1All)*100))
|
||||
}
|
||||
|
||||
func calculateAllBusy(t1, t2 []TimesStat) ([]float64, error) {
|
||||
// Make sure the CPU measurements have the same length.
|
||||
if len(t1) != len(t2) {
|
||||
return nil, fmt.Errorf(
|
||||
"received two CPU counts: %d != %d",
|
||||
len(t1), len(t2),
|
||||
)
|
||||
}
|
||||
|
||||
ret := make([]float64, len(t1))
|
||||
for i, t := range t2 {
|
||||
ret[i] = calculateBusy(t1[i], t)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Percent calculates the percentage of cpu used either per CPU or combined.
|
||||
// If an interval of 0 is given it will compare the current cpu times against the last call.
|
||||
// Returns one value per cpu, or a single value if percpu is set to false.
|
||||
func Percent(interval time.Duration, percpu bool) ([]float64, error) {
|
||||
return PercentWithContext(context.Background(), interval, percpu)
|
||||
}
|
||||
|
||||
func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool) ([]float64, error) {
|
||||
if interval <= 0 {
|
||||
return percentUsedFromLastCallWithContext(ctx, percpu)
|
||||
}
|
||||
|
||||
// Get CPU usage at the start of the interval.
|
||||
cpuTimes1, err := TimesWithContext(ctx, percpu)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := common.Sleep(ctx, interval); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// And at the end of the interval.
|
||||
cpuTimes2, err := TimesWithContext(ctx, percpu)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return calculateAllBusy(cpuTimes1, cpuTimes2)
|
||||
}
|
||||
|
||||
func percentUsedFromLastCall(percpu bool) ([]float64, error) {
|
||||
return percentUsedFromLastCallWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func percentUsedFromLastCallWithContext(ctx context.Context, percpu bool) ([]float64, error) {
|
||||
cpuTimes, err := TimesWithContext(ctx, percpu)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lastCPUPercent.Lock()
|
||||
defer lastCPUPercent.Unlock()
|
||||
var lastTimes []TimesStat
|
||||
if percpu {
|
||||
lastTimes = lastCPUPercent.lastPerCPUTimes
|
||||
lastCPUPercent.lastPerCPUTimes = cpuTimes
|
||||
} else {
|
||||
lastTimes = lastCPUPercent.lastCPUTimes
|
||||
lastCPUPercent.lastCPUTimes = cpuTimes
|
||||
}
|
||||
|
||||
if lastTimes == nil {
|
||||
return nil, fmt.Errorf("error getting times for cpu percent. lastTimes was nil")
|
||||
}
|
||||
return calculateAllBusy(lastTimes, cpuTimes)
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
//go:build aix
|
||||
// +build aix
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
//go:build aix && !cgo
|
||||
// +build aix,!cgo
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
var whiteSpaces = regexp.MustCompile(`\s+`)
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
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
|
||||
}
|
||||
|
||||
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.ParseFloat(v[i], 64); err == nil {
|
||||
switch header {
|
||||
case `%usr`:
|
||||
ret.User = t
|
||||
case `%sys`:
|
||||
ret.System = t
|
||||
case `%wio`:
|
||||
ret.Iowait = t
|
||||
case `%idle`:
|
||||
ret.Idle = t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return []TimesStat{ret}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
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.ParseFloat(p[3], 64); err == nil {
|
||||
switch strings.ToUpper(p[4]) {
|
||||
case "MHZ":
|
||||
ret.Mhz = t
|
||||
case "GHZ":
|
||||
ret.Mhz = t * 1000.0
|
||||
case "KHZ":
|
||||
ret.Mhz = t / 1000.0
|
||||
default:
|
||||
ret.Mhz = t
|
||||
}
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/shoenig/go-m1cpu"
|
||||
"github.com/tklauser/go-sysconf"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// sys/resource.h
|
||||
const (
|
||||
CPUser = 0
|
||||
cpNice = 1
|
||||
cpSys = 2
|
||||
cpIntr = 3
|
||||
cpIdle = 4
|
||||
cpUStates = 5
|
||||
)
|
||||
|
||||
// default value. from time.h
|
||||
var ClocksPerSec = float64(128)
|
||||
|
||||
func init() {
|
||||
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
||||
// ignore errors
|
||||
if err == nil {
|
||||
ClocksPerSec = float64(clkTck)
|
||||
}
|
||||
}
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
if percpu {
|
||||
return perCPUTimes()
|
||||
}
|
||||
|
||||
return allCPUTimes()
|
||||
}
|
||||
|
||||
// Returns only one CPUInfoStat on FreeBSD
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
var ret []InfoStat
|
||||
|
||||
c := InfoStat{}
|
||||
c.ModelName, _ = unix.Sysctl("machdep.cpu.brand_string")
|
||||
family, _ := unix.SysctlUint32("machdep.cpu.family")
|
||||
c.Family = strconv.FormatUint(uint64(family), 10)
|
||||
model, _ := unix.SysctlUint32("machdep.cpu.model")
|
||||
c.Model = strconv.FormatUint(uint64(model), 10)
|
||||
stepping, _ := unix.SysctlUint32("machdep.cpu.stepping")
|
||||
c.Stepping = int32(stepping)
|
||||
features, err := unix.Sysctl("machdep.cpu.features")
|
||||
if err == nil {
|
||||
for _, v := range strings.Fields(features) {
|
||||
c.Flags = append(c.Flags, strings.ToLower(v))
|
||||
}
|
||||
}
|
||||
leaf7Features, err := unix.Sysctl("machdep.cpu.leaf7_features")
|
||||
if err == nil {
|
||||
for _, v := range strings.Fields(leaf7Features) {
|
||||
c.Flags = append(c.Flags, strings.ToLower(v))
|
||||
}
|
||||
}
|
||||
extfeatures, err := unix.Sysctl("machdep.cpu.extfeatures")
|
||||
if err == nil {
|
||||
for _, v := range strings.Fields(extfeatures) {
|
||||
c.Flags = append(c.Flags, strings.ToLower(v))
|
||||
}
|
||||
}
|
||||
cores, _ := unix.SysctlUint32("machdep.cpu.core_count")
|
||||
c.Cores = int32(cores)
|
||||
cacheSize, _ := unix.SysctlUint32("machdep.cpu.cache.size")
|
||||
c.CacheSize = int32(cacheSize)
|
||||
c.VendorID, _ = unix.Sysctl("machdep.cpu.vendor")
|
||||
|
||||
if m1cpu.IsAppleSilicon() {
|
||||
c.Mhz = float64(m1cpu.PCoreHz() / 1_000_000)
|
||||
} else {
|
||||
// Use the rated frequency of the CPU. This is a static value and does not
|
||||
// account for low power or Turbo Boost modes.
|
||||
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
|
||||
if err == nil {
|
||||
c.Mhz = float64(cpuFrequency) / 1000000.0
|
||||
}
|
||||
}
|
||||
|
||||
return append(ret, c), nil
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
var cpuArgument string
|
||||
if logical {
|
||||
cpuArgument = "hw.logicalcpu"
|
||||
} else {
|
||||
cpuArgument = "hw.physicalcpu"
|
||||
}
|
||||
|
||||
count, err := unix.SysctlUint32(cpuArgument)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int(count), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
//go:build darwin && cgo
|
||||
// +build darwin,cgo
|
||||
|
||||
package cpu
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/mount.h>
|
||||
#include <mach/mach_init.h>
|
||||
#include <mach/mach_host.h>
|
||||
#include <mach/host_info.h>
|
||||
#include <TargetConditionals.h>
|
||||
#if TARGET_OS_MAC
|
||||
#include <libproc.h>
|
||||
#endif
|
||||
#include <mach/processor_info.h>
|
||||
#include <mach/vm_map.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// these CPU times for darwin is borrowed from influxdb/telegraf.
|
||||
|
||||
func perCPUTimes() ([]TimesStat, error) {
|
||||
var (
|
||||
count C.mach_msg_type_number_t
|
||||
cpuload *C.processor_cpu_load_info_data_t
|
||||
ncpu C.natural_t
|
||||
)
|
||||
|
||||
status := C.host_processor_info(C.host_t(C.mach_host_self()),
|
||||
C.PROCESSOR_CPU_LOAD_INFO,
|
||||
&ncpu,
|
||||
(*C.processor_info_array_t)(unsafe.Pointer(&cpuload)),
|
||||
&count)
|
||||
|
||||
if status != C.KERN_SUCCESS {
|
||||
return nil, fmt.Errorf("host_processor_info error=%d", status)
|
||||
}
|
||||
|
||||
// jump through some cgo casting hoops and ensure we properly free
|
||||
// the memory that cpuload points to
|
||||
target := C.vm_map_t(C.mach_task_self_)
|
||||
address := C.vm_address_t(uintptr(unsafe.Pointer(cpuload)))
|
||||
defer C.vm_deallocate(target, address, C.vm_size_t(ncpu))
|
||||
|
||||
// the body of struct processor_cpu_load_info
|
||||
// aka processor_cpu_load_info_data_t
|
||||
var cpu_ticks [C.CPU_STATE_MAX]uint32
|
||||
|
||||
// copy the cpuload array to a []byte buffer
|
||||
// where we can binary.Read the data
|
||||
size := int(ncpu) * binary.Size(cpu_ticks)
|
||||
buf := (*[1 << 30]byte)(unsafe.Pointer(cpuload))[:size:size]
|
||||
|
||||
bbuf := bytes.NewBuffer(buf)
|
||||
|
||||
var ret []TimesStat
|
||||
|
||||
for i := 0; i < int(ncpu); i++ {
|
||||
err := binary.Read(bbuf, binary.LittleEndian, &cpu_ticks)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := TimesStat{
|
||||
CPU: fmt.Sprintf("cpu%d", i),
|
||||
User: float64(cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec,
|
||||
System: float64(cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec,
|
||||
Nice: float64(cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec,
|
||||
Idle: float64(cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec,
|
||||
}
|
||||
|
||||
ret = append(ret, c)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func allCPUTimes() ([]TimesStat, error) {
|
||||
var count C.mach_msg_type_number_t
|
||||
var cpuload C.host_cpu_load_info_data_t
|
||||
|
||||
count = C.HOST_CPU_LOAD_INFO_COUNT
|
||||
|
||||
status := C.host_statistics(C.host_t(C.mach_host_self()),
|
||||
C.HOST_CPU_LOAD_INFO,
|
||||
C.host_info_t(unsafe.Pointer(&cpuload)),
|
||||
&count)
|
||||
|
||||
if status != C.KERN_SUCCESS {
|
||||
return nil, fmt.Errorf("host_statistics error=%d", status)
|
||||
}
|
||||
|
||||
c := TimesStat{
|
||||
CPU: "cpu-total",
|
||||
User: float64(cpuload.cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec,
|
||||
System: float64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec,
|
||||
Nice: float64(cpuload.cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec,
|
||||
Idle: float64(cpuload.cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec,
|
||||
}
|
||||
|
||||
return []TimesStat{c}, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
//go:build darwin && !cgo
|
||||
// +build darwin,!cgo
|
||||
|
||||
package cpu
|
||||
|
||||
import "github.com/shirou/gopsutil/v3/internal/common"
|
||||
|
||||
func perCPUTimes() ([]TimesStat, error) {
|
||||
return []TimesStat{}, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func allCPUTimes() ([]TimesStat, error) {
|
||||
return []TimesStat{}, common.ErrNotImplementedError
|
||||
}
|
||||
|
|
@ -0,0 +1,156 @@
|
|||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
"github.com/tklauser/go-sysconf"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var (
|
||||
ClocksPerSec = float64(128)
|
||||
cpuMatch = regexp.MustCompile(`^CPU:`)
|
||||
originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`)
|
||||
featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`)
|
||||
featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`)
|
||||
cpuEnd = regexp.MustCompile(`^Trying to mount root`)
|
||||
cpuTimesSize int
|
||||
emptyTimes cpuTimes
|
||||
)
|
||||
|
||||
func init() {
|
||||
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
||||
// ignore errors
|
||||
if err == nil {
|
||||
ClocksPerSec = float64(clkTck)
|
||||
}
|
||||
}
|
||||
|
||||
func timeStat(name string, t *cpuTimes) *TimesStat {
|
||||
return &TimesStat{
|
||||
User: float64(t.User) / ClocksPerSec,
|
||||
Nice: float64(t.Nice) / ClocksPerSec,
|
||||
System: float64(t.Sys) / ClocksPerSec,
|
||||
Idle: float64(t.Idle) / ClocksPerSec,
|
||||
Irq: float64(t.Intr) / ClocksPerSec,
|
||||
CPU: name,
|
||||
}
|
||||
}
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
if percpu {
|
||||
buf, err := unix.SysctlRaw("kern.cp_times")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We can't do this in init due to the conflict with cpu.init()
|
||||
if cpuTimesSize == 0 {
|
||||
cpuTimesSize = int(reflect.TypeOf(cpuTimes{}).Size())
|
||||
}
|
||||
|
||||
ncpus := len(buf) / cpuTimesSize
|
||||
ret := make([]TimesStat, 0, ncpus)
|
||||
for i := 0; i < ncpus; i++ {
|
||||
times := (*cpuTimes)(unsafe.Pointer(&buf[i*cpuTimesSize]))
|
||||
if *times == emptyTimes {
|
||||
// CPU not present
|
||||
continue
|
||||
}
|
||||
ret = append(ret, *timeStat(fmt.Sprintf("cpu%d", len(ret)), times))
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
buf, err := unix.SysctlRaw("kern.cp_time")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
|
||||
return []TimesStat{*timeStat("cpu-total", times)}, nil
|
||||
}
|
||||
|
||||
// Returns only one InfoStat on DragonflyBSD. The information regarding core
|
||||
// count, however is accurate and it is assumed that all InfoStat attributes
|
||||
// are the same across CPUs.
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
const dmesgBoot = "/var/run/dmesg.boot"
|
||||
|
||||
c, err := parseDmesgBoot(dmesgBoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var u32 uint32
|
||||
if u32, err = unix.SysctlUint32("hw.clockrate"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Mhz = float64(u32)
|
||||
|
||||
var num int
|
||||
var buf string
|
||||
if buf, err = unix.Sysctl("hw.cpu_topology.tree"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
num = strings.Count(buf, "CHIP")
|
||||
c.Cores = int32(strings.Count(string(buf), "CORE") / num)
|
||||
|
||||
if c.ModelName, err = unix.Sysctl("hw.model"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret := make([]InfoStat, num)
|
||||
for i := 0; i < num; i++ {
|
||||
ret[i] = c
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func parseDmesgBoot(fileName string) (InfoStat, error) {
|
||||
c := InfoStat{}
|
||||
lines, _ := common.ReadLines(fileName)
|
||||
for _, line := range lines {
|
||||
if matches := cpuEnd.FindStringSubmatch(line); matches != nil {
|
||||
break
|
||||
} else if matches := originMatch.FindStringSubmatch(line); matches != nil {
|
||||
c.VendorID = matches[1]
|
||||
t, err := strconv.ParseInt(matches[2], 10, 32)
|
||||
if err != nil {
|
||||
return c, fmt.Errorf("unable to parse DragonflyBSD CPU stepping information from %q: %v", line, err)
|
||||
}
|
||||
c.Stepping = int32(t)
|
||||
} else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {
|
||||
for _, v := range strings.Split(matches[1], ",") {
|
||||
c.Flags = append(c.Flags, strings.ToLower(v))
|
||||
}
|
||||
} else if matches := featuresMatch2.FindStringSubmatch(line); matches != nil {
|
||||
for _, v := range strings.Split(matches[1], ",") {
|
||||
c.Flags = append(c.Flags, strings.ToLower(v))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
return runtime.NumCPU(), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint64
|
||||
Nice uint64
|
||||
Sys uint64
|
||||
Intr uint64
|
||||
Idle uint64
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
//go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !solaris && !windows && !dragonfly && !plan9 && !aix
|
||||
// +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!solaris,!windows,!dragonfly,!plan9,!aix
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
return []TimesStat{}, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
return []InfoStat{}, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
return runtime.NumCPU(), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,168 @@
|
|||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
"github.com/tklauser/go-sysconf"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var (
|
||||
ClocksPerSec = float64(128)
|
||||
cpuMatch = regexp.MustCompile(`^CPU:`)
|
||||
originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Family\s*=\s*(.+)\s+Model\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`)
|
||||
featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`)
|
||||
featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`)
|
||||
cpuEnd = regexp.MustCompile(`^Trying to mount root`)
|
||||
cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`)
|
||||
cpuTimesSize int
|
||||
emptyTimes cpuTimes
|
||||
)
|
||||
|
||||
func init() {
|
||||
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
||||
// ignore errors
|
||||
if err == nil {
|
||||
ClocksPerSec = float64(clkTck)
|
||||
}
|
||||
}
|
||||
|
||||
func timeStat(name string, t *cpuTimes) *TimesStat {
|
||||
return &TimesStat{
|
||||
User: float64(t.User) / ClocksPerSec,
|
||||
Nice: float64(t.Nice) / ClocksPerSec,
|
||||
System: float64(t.Sys) / ClocksPerSec,
|
||||
Idle: float64(t.Idle) / ClocksPerSec,
|
||||
Irq: float64(t.Intr) / ClocksPerSec,
|
||||
CPU: name,
|
||||
}
|
||||
}
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
if percpu {
|
||||
buf, err := unix.SysctlRaw("kern.cp_times")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We can't do this in init due to the conflict with cpu.init()
|
||||
if cpuTimesSize == 0 {
|
||||
cpuTimesSize = int(reflect.TypeOf(cpuTimes{}).Size())
|
||||
}
|
||||
|
||||
ncpus := len(buf) / cpuTimesSize
|
||||
ret := make([]TimesStat, 0, ncpus)
|
||||
for i := 0; i < ncpus; i++ {
|
||||
times := (*cpuTimes)(unsafe.Pointer(&buf[i*cpuTimesSize]))
|
||||
if *times == emptyTimes {
|
||||
// CPU not present
|
||||
continue
|
||||
}
|
||||
ret = append(ret, *timeStat(fmt.Sprintf("cpu%d", len(ret)), times))
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
buf, err := unix.SysctlRaw("kern.cp_time")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
|
||||
return []TimesStat{*timeStat("cpu-total", times)}, nil
|
||||
}
|
||||
|
||||
// Returns only one InfoStat on FreeBSD. The information regarding core
|
||||
// count, however is accurate and it is assumed that all InfoStat attributes
|
||||
// are the same across CPUs.
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
const dmesgBoot = "/var/run/dmesg.boot"
|
||||
|
||||
c, num, err := parseDmesgBoot(dmesgBoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var u32 uint32
|
||||
if u32, err = unix.SysctlUint32("hw.clockrate"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Mhz = float64(u32)
|
||||
|
||||
if u32, err = unix.SysctlUint32("hw.ncpu"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Cores = int32(u32)
|
||||
|
||||
if c.ModelName, err = unix.Sysctl("hw.model"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret := make([]InfoStat, num)
|
||||
for i := 0; i < num; i++ {
|
||||
ret[i] = c
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func parseDmesgBoot(fileName string) (InfoStat, int, error) {
|
||||
c := InfoStat{}
|
||||
lines, _ := common.ReadLines(fileName)
|
||||
cpuNum := 1 // default cpu num is 1
|
||||
for _, line := range lines {
|
||||
if matches := cpuEnd.FindStringSubmatch(line); matches != nil {
|
||||
break
|
||||
} else if matches := originMatch.FindStringSubmatch(line); matches != nil {
|
||||
c.VendorID = matches[1]
|
||||
c.Family = matches[3]
|
||||
c.Model = matches[4]
|
||||
t, err := strconv.ParseInt(matches[5], 10, 32)
|
||||
if err != nil {
|
||||
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %v", line, err)
|
||||
}
|
||||
c.Stepping = int32(t)
|
||||
} else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {
|
||||
for _, v := range strings.Split(matches[1], ",") {
|
||||
c.Flags = append(c.Flags, strings.ToLower(v))
|
||||
}
|
||||
} else if matches := featuresMatch2.FindStringSubmatch(line); matches != nil {
|
||||
for _, v := range strings.Split(matches[1], ",") {
|
||||
c.Flags = append(c.Flags, strings.ToLower(v))
|
||||
}
|
||||
} else if matches := cpuCores.FindStringSubmatch(line); matches != nil {
|
||||
t, err := strconv.ParseInt(matches[1], 10, 32)
|
||||
if err != nil {
|
||||
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %v", line, err)
|
||||
}
|
||||
cpuNum = int(t)
|
||||
t2, err := strconv.ParseInt(matches[2], 10, 32)
|
||||
if err != nil {
|
||||
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %v", line, err)
|
||||
}
|
||||
c.Cores = int32(t2)
|
||||
}
|
||||
}
|
||||
|
||||
return c, cpuNum, nil
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
return runtime.NumCPU(), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint32
|
||||
Nice uint32
|
||||
Sys uint32
|
||||
Intr uint32
|
||||
Idle uint32
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint64
|
||||
Nice uint64
|
||||
Sys uint64
|
||||
Intr uint64
|
||||
Idle uint64
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint32
|
||||
Nice uint32
|
||||
Sys uint32
|
||||
Intr uint32
|
||||
Idle uint32
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint64
|
||||
Nice uint64
|
||||
Sys uint64
|
||||
Intr uint64
|
||||
Idle uint64
|
||||
}
|
||||
|
|
@ -0,0 +1,479 @@
|
|||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tklauser/go-sysconf"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
var ClocksPerSec = float64(100)
|
||||
|
||||
var armModelToModelName = map[uint64]string{
|
||||
0x810: "ARM810",
|
||||
0x920: "ARM920",
|
||||
0x922: "ARM922",
|
||||
0x926: "ARM926",
|
||||
0x940: "ARM940",
|
||||
0x946: "ARM946",
|
||||
0x966: "ARM966",
|
||||
0xa20: "ARM1020",
|
||||
0xa22: "ARM1022",
|
||||
0xa26: "ARM1026",
|
||||
0xb02: "ARM11 MPCore",
|
||||
0xb36: "ARM1136",
|
||||
0xb56: "ARM1156",
|
||||
0xb76: "ARM1176",
|
||||
0xc05: "Cortex-A5",
|
||||
0xc07: "Cortex-A7",
|
||||
0xc08: "Cortex-A8",
|
||||
0xc09: "Cortex-A9",
|
||||
0xc0d: "Cortex-A17",
|
||||
0xc0f: "Cortex-A15",
|
||||
0xc0e: "Cortex-A17",
|
||||
0xc14: "Cortex-R4",
|
||||
0xc15: "Cortex-R5",
|
||||
0xc17: "Cortex-R7",
|
||||
0xc18: "Cortex-R8",
|
||||
0xc20: "Cortex-M0",
|
||||
0xc21: "Cortex-M1",
|
||||
0xc23: "Cortex-M3",
|
||||
0xc24: "Cortex-M4",
|
||||
0xc27: "Cortex-M7",
|
||||
0xc60: "Cortex-M0+",
|
||||
0xd01: "Cortex-A32",
|
||||
0xd02: "Cortex-A34",
|
||||
0xd03: "Cortex-A53",
|
||||
0xd04: "Cortex-A35",
|
||||
0xd05: "Cortex-A55",
|
||||
0xd06: "Cortex-A65",
|
||||
0xd07: "Cortex-A57",
|
||||
0xd08: "Cortex-A72",
|
||||
0xd09: "Cortex-A73",
|
||||
0xd0a: "Cortex-A75",
|
||||
0xd0b: "Cortex-A76",
|
||||
0xd0c: "Neoverse-N1",
|
||||
0xd0d: "Cortex-A77",
|
||||
0xd0e: "Cortex-A76AE",
|
||||
0xd13: "Cortex-R52",
|
||||
0xd20: "Cortex-M23",
|
||||
0xd21: "Cortex-M33",
|
||||
0xd40: "Neoverse-V1",
|
||||
0xd41: "Cortex-A78",
|
||||
0xd42: "Cortex-A78AE",
|
||||
0xd43: "Cortex-A65AE",
|
||||
0xd44: "Cortex-X1",
|
||||
0xd46: "Cortex-A510",
|
||||
0xd47: "Cortex-A710",
|
||||
0xd48: "Cortex-X2",
|
||||
0xd49: "Neoverse-N2",
|
||||
0xd4a: "Neoverse-E1",
|
||||
0xd4b: "Cortex-A78C",
|
||||
0xd4c: "Cortex-X1C",
|
||||
0xd4d: "Cortex-A715",
|
||||
0xd4e: "Cortex-X3",
|
||||
}
|
||||
|
||||
func init() {
|
||||
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
||||
// ignore errors
|
||||
if err == nil {
|
||||
ClocksPerSec = float64(clkTck)
|
||||
}
|
||||
}
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
filename := common.HostProcWithContext(ctx, "stat")
|
||||
lines := []string{}
|
||||
if percpu {
|
||||
statlines, err := common.ReadLines(filename)
|
||||
if err != nil || len(statlines) < 2 {
|
||||
return []TimesStat{}, nil
|
||||
}
|
||||
for _, line := range statlines[1:] {
|
||||
if !strings.HasPrefix(line, "cpu") {
|
||||
break
|
||||
}
|
||||
lines = append(lines, line)
|
||||
}
|
||||
} else {
|
||||
lines, _ = common.ReadLinesOffsetN(filename, 0, 1)
|
||||
}
|
||||
|
||||
ret := make([]TimesStat, 0, len(lines))
|
||||
|
||||
for _, line := range lines {
|
||||
ct, err := parseStatLine(line)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, *ct)
|
||||
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func sysCPUPath(ctx context.Context, cpu int32, relPath string) string {
|
||||
return common.HostSysWithContext(ctx, fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
|
||||
}
|
||||
|
||||
func finishCPUInfo(ctx context.Context, c *InfoStat) {
|
||||
var lines []string
|
||||
var err error
|
||||
var value float64
|
||||
|
||||
if len(c.CoreID) == 0 {
|
||||
lines, err = common.ReadLines(sysCPUPath(ctx, c.CPU, "topology/core_id"))
|
||||
if err == nil {
|
||||
c.CoreID = lines[0]
|
||||
}
|
||||
}
|
||||
|
||||
// override the value of c.Mhz with cpufreq/cpuinfo_max_freq regardless
|
||||
// of the value from /proc/cpuinfo because we want to report the maximum
|
||||
// clock-speed of the CPU for c.Mhz, matching the behaviour of Windows
|
||||
lines, err = common.ReadLines(sysCPUPath(ctx, c.CPU, "cpufreq/cpuinfo_max_freq"))
|
||||
// if we encounter errors below such as there are no cpuinfo_max_freq file,
|
||||
// we just ignore. so let Mhz is 0.
|
||||
if err != nil || len(lines) == 0 {
|
||||
return
|
||||
}
|
||||
value, err = strconv.ParseFloat(lines[0], 64)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
c.Mhz = value / 1000.0 // value is in kHz
|
||||
if c.Mhz > 9999 {
|
||||
c.Mhz = c.Mhz / 1000.0 // value in Hz
|
||||
}
|
||||
}
|
||||
|
||||
// CPUInfo on linux will return 1 item per physical thread.
|
||||
//
|
||||
// CPUs have three levels of counting: sockets, cores, threads.
|
||||
// Cores with HyperThreading count as having 2 threads per core.
|
||||
// Sockets often come with many physical CPU cores.
|
||||
// For example a single socket board with two cores each with HT will
|
||||
// return 4 CPUInfoStat structs on Linux and the "Cores" field set to 1.
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
filename := common.HostProcWithContext(ctx, "cpuinfo")
|
||||
lines, _ := common.ReadLines(filename)
|
||||
|
||||
var ret []InfoStat
|
||||
var processorName string
|
||||
|
||||
c := InfoStat{CPU: -1, Cores: 1}
|
||||
for _, line := range lines {
|
||||
fields := strings.Split(line, ":")
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(fields[0])
|
||||
value := strings.TrimSpace(fields[1])
|
||||
|
||||
switch key {
|
||||
case "Processor":
|
||||
processorName = value
|
||||
case "processor", "cpu number":
|
||||
if c.CPU >= 0 {
|
||||
finishCPUInfo(ctx, &c)
|
||||
ret = append(ret, c)
|
||||
}
|
||||
c = InfoStat{Cores: 1, ModelName: processorName}
|
||||
t, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
c.CPU = int32(t)
|
||||
case "vendorId", "vendor_id":
|
||||
c.VendorID = value
|
||||
if strings.Contains(value, "S390") {
|
||||
processorName = "S390"
|
||||
}
|
||||
case "CPU implementer":
|
||||
if v, err := strconv.ParseUint(value, 0, 8); err == nil {
|
||||
switch v {
|
||||
case 0x41:
|
||||
c.VendorID = "ARM"
|
||||
case 0x42:
|
||||
c.VendorID = "Broadcom"
|
||||
case 0x43:
|
||||
c.VendorID = "Cavium"
|
||||
case 0x44:
|
||||
c.VendorID = "DEC"
|
||||
case 0x46:
|
||||
c.VendorID = "Fujitsu"
|
||||
case 0x48:
|
||||
c.VendorID = "HiSilicon"
|
||||
case 0x49:
|
||||
c.VendorID = "Infineon"
|
||||
case 0x4d:
|
||||
c.VendorID = "Motorola/Freescale"
|
||||
case 0x4e:
|
||||
c.VendorID = "NVIDIA"
|
||||
case 0x50:
|
||||
c.VendorID = "APM"
|
||||
case 0x51:
|
||||
c.VendorID = "Qualcomm"
|
||||
case 0x56:
|
||||
c.VendorID = "Marvell"
|
||||
case 0x61:
|
||||
c.VendorID = "Apple"
|
||||
case 0x69:
|
||||
c.VendorID = "Intel"
|
||||
case 0xc0:
|
||||
c.VendorID = "Ampere"
|
||||
}
|
||||
}
|
||||
case "cpu family":
|
||||
c.Family = value
|
||||
case "model", "CPU part":
|
||||
c.Model = value
|
||||
// if CPU is arm based, model name is found via model number. refer to: arch/arm64/kernel/cpuinfo.c
|
||||
if c.VendorID == "ARM" {
|
||||
if v, err := strconv.ParseUint(c.Model, 0, 16); err == nil {
|
||||
modelName, exist := armModelToModelName[v]
|
||||
if exist {
|
||||
c.ModelName = modelName
|
||||
} else {
|
||||
c.ModelName = "Undefined"
|
||||
}
|
||||
}
|
||||
}
|
||||
case "Model Name", "model name", "cpu":
|
||||
c.ModelName = value
|
||||
if strings.Contains(value, "POWER") {
|
||||
c.Model = strings.Split(value, " ")[0]
|
||||
c.Family = "POWER"
|
||||
c.VendorID = "IBM"
|
||||
}
|
||||
case "stepping", "revision", "CPU revision":
|
||||
val := value
|
||||
|
||||
if key == "revision" {
|
||||
val = strings.Split(value, ".")[0]
|
||||
}
|
||||
|
||||
t, err := strconv.ParseInt(val, 10, 64)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
c.Stepping = int32(t)
|
||||
case "cpu MHz", "clock", "cpu MHz dynamic":
|
||||
// treat this as the fallback value, thus we ignore error
|
||||
if t, err := strconv.ParseFloat(strings.Replace(value, "MHz", "", 1), 64); err == nil {
|
||||
c.Mhz = t
|
||||
}
|
||||
case "cache size":
|
||||
t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 64)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
c.CacheSize = int32(t)
|
||||
case "physical id":
|
||||
c.PhysicalID = value
|
||||
case "core id":
|
||||
c.CoreID = value
|
||||
case "flags", "Features":
|
||||
c.Flags = strings.FieldsFunc(value, func(r rune) bool {
|
||||
return r == ',' || r == ' '
|
||||
})
|
||||
case "microcode":
|
||||
c.Microcode = value
|
||||
}
|
||||
}
|
||||
if c.CPU >= 0 {
|
||||
finishCPUInfo(ctx, &c)
|
||||
ret = append(ret, c)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func parseStatLine(line string) (*TimesStat, error) {
|
||||
fields := strings.Fields(line)
|
||||
|
||||
if len(fields) == 0 {
|
||||
return nil, errors.New("stat does not contain cpu info")
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(fields[0], "cpu") {
|
||||
return nil, errors.New("not contain cpu")
|
||||
}
|
||||
|
||||
cpu := fields[0]
|
||||
if cpu == "cpu" {
|
||||
cpu = "cpu-total"
|
||||
}
|
||||
user, err := strconv.ParseFloat(fields[1], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nice, err := strconv.ParseFloat(fields[2], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
system, err := strconv.ParseFloat(fields[3], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
idle, err := strconv.ParseFloat(fields[4], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iowait, err := strconv.ParseFloat(fields[5], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
irq, err := strconv.ParseFloat(fields[6], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
softirq, err := strconv.ParseFloat(fields[7], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ct := &TimesStat{
|
||||
CPU: cpu,
|
||||
User: user / ClocksPerSec,
|
||||
Nice: nice / ClocksPerSec,
|
||||
System: system / ClocksPerSec,
|
||||
Idle: idle / ClocksPerSec,
|
||||
Iowait: iowait / ClocksPerSec,
|
||||
Irq: irq / ClocksPerSec,
|
||||
Softirq: softirq / ClocksPerSec,
|
||||
}
|
||||
if len(fields) > 8 { // Linux >= 2.6.11
|
||||
steal, err := strconv.ParseFloat(fields[8], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ct.Steal = steal / ClocksPerSec
|
||||
}
|
||||
if len(fields) > 9 { // Linux >= 2.6.24
|
||||
guest, err := strconv.ParseFloat(fields[9], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ct.Guest = guest / ClocksPerSec
|
||||
}
|
||||
if len(fields) > 10 { // Linux >= 3.2.0
|
||||
guestNice, err := strconv.ParseFloat(fields[10], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ct.GuestNice = guestNice / ClocksPerSec
|
||||
}
|
||||
|
||||
return ct, nil
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
if logical {
|
||||
ret := 0
|
||||
// https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_pslinux.py#L599
|
||||
procCpuinfo := common.HostProcWithContext(ctx, "cpuinfo")
|
||||
lines, err := common.ReadLines(procCpuinfo)
|
||||
if err == nil {
|
||||
for _, line := range lines {
|
||||
line = strings.ToLower(line)
|
||||
if strings.HasPrefix(line, "processor") {
|
||||
_, err = strconv.Atoi(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]))
|
||||
if err == nil {
|
||||
ret++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ret == 0 {
|
||||
procStat := common.HostProcWithContext(ctx, "stat")
|
||||
lines, err = common.ReadLines(procStat)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, line := range lines {
|
||||
if len(line) >= 4 && strings.HasPrefix(line, "cpu") && '0' <= line[3] && line[3] <= '9' { // `^cpu\d` regexp matching
|
||||
ret++
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
// physical cores
|
||||
// https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/_pslinux.py#L615-L628
|
||||
threadSiblingsLists := make(map[string]bool)
|
||||
// These 2 files are the same but */core_cpus_list is newer while */thread_siblings_list is deprecated and may disappear in the future.
|
||||
// https://www.kernel.org/doc/Documentation/admin-guide/cputopology.rst
|
||||
// https://github.com/giampaolo/psutil/pull/1727#issuecomment-707624964
|
||||
// https://lkml.org/lkml/2019/2/26/41
|
||||
for _, glob := range []string{"devices/system/cpu/cpu[0-9]*/topology/core_cpus_list", "devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list"} {
|
||||
if files, err := filepath.Glob(common.HostSysWithContext(ctx, glob)); err == nil {
|
||||
for _, file := range files {
|
||||
lines, err := common.ReadLines(file)
|
||||
if err != nil || len(lines) != 1 {
|
||||
continue
|
||||
}
|
||||
threadSiblingsLists[lines[0]] = true
|
||||
}
|
||||
ret := len(threadSiblingsLists)
|
||||
if ret != 0 {
|
||||
return ret, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
// https://github.com/giampaolo/psutil/blob/122174a10b75c9beebe15f6c07dcf3afbe3b120d/psutil/_pslinux.py#L631-L652
|
||||
filename := common.HostProcWithContext(ctx, "cpuinfo")
|
||||
lines, err := common.ReadLines(filename)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
mapping := make(map[int]int)
|
||||
currentInfo := make(map[string]int)
|
||||
for _, line := range lines {
|
||||
line = strings.ToLower(strings.TrimSpace(line))
|
||||
if line == "" {
|
||||
// new section
|
||||
id, okID := currentInfo["physical id"]
|
||||
cores, okCores := currentInfo["cpu cores"]
|
||||
if okID && okCores {
|
||||
mapping[id] = cores
|
||||
}
|
||||
currentInfo = make(map[string]int)
|
||||
continue
|
||||
}
|
||||
fields := strings.Split(line, ":")
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
fields[0] = strings.TrimSpace(fields[0])
|
||||
if fields[0] == "physical id" || fields[0] == "cpu cores" {
|
||||
val, err := strconv.Atoi(strings.TrimSpace(fields[1]))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
currentInfo[fields[0]] = val
|
||||
}
|
||||
}
|
||||
ret := 0
|
||||
for _, v := range mapping {
|
||||
ret += v
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
//go:build netbsd
|
||||
// +build netbsd
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
"github.com/tklauser/go-sysconf"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const (
|
||||
// sys/sysctl.h
|
||||
ctlKern = 1 // "high kernel": proc, limits
|
||||
ctlHw = 6 // CTL_HW
|
||||
kernCpTime = 51 // KERN_CPTIME
|
||||
)
|
||||
|
||||
var ClocksPerSec = float64(100)
|
||||
|
||||
func init() {
|
||||
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
||||
// ignore errors
|
||||
if err == nil {
|
||||
ClocksPerSec = float64(clkTck)
|
||||
}
|
||||
}
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
|
||||
if !percpu {
|
||||
mib := []int32{ctlKern, kernCpTime}
|
||||
buf, _, err := common.CallSyscall(mib)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
|
||||
stat := TimesStat{
|
||||
CPU: "cpu-total",
|
||||
User: float64(times.User),
|
||||
Nice: float64(times.Nice),
|
||||
System: float64(times.Sys),
|
||||
Idle: float64(times.Idle),
|
||||
Irq: float64(times.Intr),
|
||||
}
|
||||
return []TimesStat{stat}, nil
|
||||
}
|
||||
|
||||
ncpu, err := unix.SysctlUint32("hw.ncpu")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var i uint32
|
||||
for i = 0; i < ncpu; i++ {
|
||||
mib := []int32{ctlKern, kernCpTime, int32(i)}
|
||||
buf, _, err := common.CallSyscall(mib)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
|
||||
stats := (*cpuTimes)(unsafe.Pointer(&buf[0]))
|
||||
ret = append(ret, TimesStat{
|
||||
CPU: fmt.Sprintf("cpu%d", i),
|
||||
User: float64(stats.User),
|
||||
Nice: float64(stats.Nice),
|
||||
System: float64(stats.Sys),
|
||||
Idle: float64(stats.Idle),
|
||||
Irq: float64(stats.Intr),
|
||||
})
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Returns only one (minimal) CPUInfoStat on NetBSD
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
var ret []InfoStat
|
||||
var err error
|
||||
|
||||
c := InfoStat{}
|
||||
|
||||
mhz, err := unix.Sysctl("machdep.dmi.processor-frequency")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = fmt.Sscanf(mhz, "%f", &c.Mhz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ncpu, err := unix.SysctlUint32("hw.ncpuonline")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Cores = int32(ncpu)
|
||||
|
||||
if c.ModelName, err = unix.Sysctl("machdep.dmi.processor-version"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return append(ret, c), nil
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
return runtime.NumCPU(), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint64
|
||||
Nice uint64
|
||||
Sys uint64
|
||||
Intr uint64
|
||||
Idle uint64
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint64
|
||||
Nice uint64
|
||||
Sys uint64
|
||||
Intr uint64
|
||||
Idle uint64
|
||||
}
|
||||
|
|
@ -0,0 +1,137 @@
|
|||
//go:build openbsd
|
||||
// +build openbsd
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
"github.com/tklauser/go-sysconf"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const (
|
||||
// sys/sched.h
|
||||
cpuOnline = 0x0001 // CPUSTATS_ONLINE
|
||||
|
||||
// sys/sysctl.h
|
||||
ctlKern = 1 // "high kernel": proc, limits
|
||||
ctlHw = 6 // CTL_HW
|
||||
smt = 24 // HW_SMT
|
||||
kernCpTime = 40 // KERN_CPTIME
|
||||
kernCPUStats = 85 // KERN_CPUSTATS
|
||||
)
|
||||
|
||||
var ClocksPerSec = float64(128)
|
||||
|
||||
type cpuStats struct {
|
||||
// cs_time[CPUSTATES]
|
||||
User uint64
|
||||
Nice uint64
|
||||
Sys uint64
|
||||
Spin uint64
|
||||
Intr uint64
|
||||
Idle uint64
|
||||
|
||||
// cs_flags
|
||||
Flags uint64
|
||||
}
|
||||
|
||||
func init() {
|
||||
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
||||
// ignore errors
|
||||
if err == nil {
|
||||
ClocksPerSec = float64(clkTck)
|
||||
}
|
||||
}
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err error) {
|
||||
if !percpu {
|
||||
mib := []int32{ctlKern, kernCpTime}
|
||||
buf, _, err := common.CallSyscall(mib)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
|
||||
stat := TimesStat{
|
||||
CPU: "cpu-total",
|
||||
User: float64(times.User) / ClocksPerSec,
|
||||
Nice: float64(times.Nice) / ClocksPerSec,
|
||||
System: float64(times.Sys) / ClocksPerSec,
|
||||
Idle: float64(times.Idle) / ClocksPerSec,
|
||||
Irq: float64(times.Intr) / ClocksPerSec,
|
||||
}
|
||||
return []TimesStat{stat}, nil
|
||||
}
|
||||
|
||||
ncpu, err := unix.SysctlUint32("hw.ncpu")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var i uint32
|
||||
for i = 0; i < ncpu; i++ {
|
||||
mib := []int32{ctlKern, kernCPUStats, int32(i)}
|
||||
buf, _, err := common.CallSyscall(mib)
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
|
||||
stats := (*cpuStats)(unsafe.Pointer(&buf[0]))
|
||||
if (stats.Flags & cpuOnline) == 0 {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, TimesStat{
|
||||
CPU: fmt.Sprintf("cpu%d", i),
|
||||
User: float64(stats.User) / ClocksPerSec,
|
||||
Nice: float64(stats.Nice) / ClocksPerSec,
|
||||
System: float64(stats.Sys) / ClocksPerSec,
|
||||
Idle: float64(stats.Idle) / ClocksPerSec,
|
||||
Irq: float64(stats.Intr) / ClocksPerSec,
|
||||
})
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Returns only one (minimal) CPUInfoStat on OpenBSD
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
var ret []InfoStat
|
||||
var err error
|
||||
|
||||
c := InfoStat{}
|
||||
|
||||
mhz, err := unix.SysctlUint32("hw.cpuspeed")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Mhz = float64(mhz)
|
||||
|
||||
ncpu, err := unix.SysctlUint32("hw.ncpuonline")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Cores = int32(ncpu)
|
||||
|
||||
if c.ModelName, err = unix.Sysctl("hw.model"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return append(ret, c), nil
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
return runtime.NumCPU(), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint32
|
||||
Nice uint32
|
||||
Sys uint32
|
||||
Spin uint32
|
||||
Intr uint32
|
||||
Idle uint32
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint64
|
||||
Nice uint64
|
||||
Sys uint64
|
||||
Spin uint64
|
||||
Intr uint64
|
||||
Idle uint64
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint32
|
||||
Nice uint32
|
||||
Sys uint32
|
||||
Spin uint32
|
||||
Intr uint32
|
||||
Idle uint32
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package cpu
|
||||
|
||||
type cpuTimes struct {
|
||||
User uint64
|
||||
Nice uint64
|
||||
Sys uint64
|
||||
Spin uint64
|
||||
Intr uint64
|
||||
Idle uint64
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
//go:build plan9
|
||||
// +build plan9
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
stats "github.com/lufia/plan9stats"
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
// BUG: percpu flag is not supported yet.
|
||||
root := os.Getenv("HOST_ROOT")
|
||||
c, err := stats.ReadCPUType(ctx, stats.WithRootDir(root))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s, err := stats.ReadCPUStats(ctx, stats.WithRootDir(root))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []TimesStat{
|
||||
{
|
||||
CPU: c.Name,
|
||||
User: s.User.Seconds(),
|
||||
System: s.Sys.Seconds(),
|
||||
Idle: s.Idle.Seconds(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
return []InfoStat{}, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
return runtime.NumCPU(), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,268 @@
|
|||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tklauser/go-sysconf"
|
||||
)
|
||||
|
||||
var ClocksPerSec = float64(128)
|
||||
|
||||
func init() {
|
||||
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
||||
// ignore errors
|
||||
if err == nil {
|
||||
ClocksPerSec = float64(clkTck)
|
||||
}
|
||||
}
|
||||
|
||||
// sum all values in a float64 map with float64 keys
|
||||
func msum(x map[float64]float64) float64 {
|
||||
total := 0.0
|
||||
for _, y := range x {
|
||||
total += y
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot execute kstat: %s", err)
|
||||
}
|
||||
cpu := make(map[float64]float64)
|
||||
idle := make(map[float64]float64)
|
||||
user := make(map[float64]float64)
|
||||
kern := make(map[float64]float64)
|
||||
iowt := make(map[float64]float64)
|
||||
// swap := make(map[float64]float64)
|
||||
re := regexp.MustCompile(`[:\s]+`)
|
||||
for _, line := range strings.Split(string(kstatSysOut), "\n") {
|
||||
fields := re.Split(line, -1)
|
||||
if fields[0] != "cpu_stat" {
|
||||
continue
|
||||
}
|
||||
cpuNumber, err := strconv.ParseFloat(fields[1], 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse cpu number: %s", err)
|
||||
}
|
||||
cpu[cpuNumber] = cpuNumber
|
||||
switch fields[3] {
|
||||
case "idle":
|
||||
idle[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse idle: %s", err)
|
||||
}
|
||||
case "user":
|
||||
user[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse user: %s", err)
|
||||
}
|
||||
case "kernel":
|
||||
kern[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse kernel: %s", err)
|
||||
}
|
||||
case "iowait":
|
||||
iowt[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse iowait: %s", err)
|
||||
}
|
||||
//not sure how this translates, don't report, add to kernel, something else?
|
||||
/*case "swap":
|
||||
swap[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse swap: %s", err)
|
||||
} */
|
||||
}
|
||||
}
|
||||
ret := make([]TimesStat, 0, len(cpu))
|
||||
if percpu {
|
||||
for _, c := range cpu {
|
||||
ct := &TimesStat{
|
||||
CPU: fmt.Sprintf("cpu%d", int(cpu[c])),
|
||||
Idle: idle[c] / ClocksPerSec,
|
||||
User: user[c] / ClocksPerSec,
|
||||
System: kern[c] / ClocksPerSec,
|
||||
Iowait: iowt[c] / ClocksPerSec,
|
||||
}
|
||||
ret = append(ret, *ct)
|
||||
}
|
||||
} else {
|
||||
ct := &TimesStat{
|
||||
CPU: "cpu-total",
|
||||
Idle: msum(idle) / ClocksPerSec,
|
||||
User: msum(user) / ClocksPerSec,
|
||||
System: msum(kern) / ClocksPerSec,
|
||||
Iowait: msum(iowt) / ClocksPerSec,
|
||||
}
|
||||
ret = append(ret, *ct)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
psrInfoOut, err := invoke.CommandWithContext(ctx, "psrinfo", "-p", "-v")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot execute psrinfo: %s", err)
|
||||
}
|
||||
|
||||
procs, err := parseProcessorInfo(string(psrInfoOut))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing psrinfo output: %s", err)
|
||||
}
|
||||
|
||||
isaInfoOut, err := invoke.CommandWithContext(ctx, "isainfo", "-b", "-v")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot execute isainfo: %s", err)
|
||||
}
|
||||
|
||||
flags, err := parseISAInfo(string(isaInfoOut))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing isainfo output: %s", err)
|
||||
}
|
||||
|
||||
result := make([]InfoStat, 0, len(flags))
|
||||
for _, proc := range procs {
|
||||
procWithFlags := proc
|
||||
procWithFlags.Flags = flags
|
||||
result = append(result, procWithFlags)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var flagsMatch = regexp.MustCompile(`[\w\.]+`)
|
||||
|
||||
func parseISAInfo(cmdOutput string) ([]string, error) {
|
||||
words := flagsMatch.FindAllString(cmdOutput, -1)
|
||||
|
||||
// Sanity check the output
|
||||
if len(words) < 4 || words[1] != "bit" || words[3] != "applications" {
|
||||
return nil, errors.New("attempted to parse invalid isainfo output")
|
||||
}
|
||||
|
||||
flags := make([]string, len(words)-4)
|
||||
for i, val := range words[4:] {
|
||||
flags[i] = val
|
||||
}
|
||||
sort.Strings(flags)
|
||||
|
||||
return flags, nil
|
||||
}
|
||||
|
||||
var psrInfoMatch = regexp.MustCompile(`The physical processor has (?:([\d]+) virtual processors? \(([\d-]+)\)|([\d]+) cores and ([\d]+) virtual processors[^\n]+)\n(?:\s+ The core has.+\n)*\s+.+ \((\w+) ([\S]+) family (.+) model (.+) step (.+) clock (.+) MHz\)\n[\s]*(.*)`)
|
||||
|
||||
const (
|
||||
psrNumCoresOffset = 1
|
||||
psrNumCoresHTOffset = 3
|
||||
psrNumHTOffset = 4
|
||||
psrVendorIDOffset = 5
|
||||
psrFamilyOffset = 7
|
||||
psrModelOffset = 8
|
||||
psrStepOffset = 9
|
||||
psrClockOffset = 10
|
||||
psrModelNameOffset = 11
|
||||
)
|
||||
|
||||
func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
|
||||
matches := psrInfoMatch.FindAllStringSubmatch(cmdOutput, -1)
|
||||
|
||||
var infoStatCount int32
|
||||
result := make([]InfoStat, 0, len(matches))
|
||||
for physicalIndex, physicalCPU := range matches {
|
||||
var step int32
|
||||
var clock float64
|
||||
|
||||
if physicalCPU[psrStepOffset] != "" {
|
||||
stepParsed, err := strconv.ParseInt(physicalCPU[psrStepOffset], 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse value %q for step as 32-bit integer: %s", physicalCPU[9], err)
|
||||
}
|
||||
step = int32(stepParsed)
|
||||
}
|
||||
|
||||
if physicalCPU[psrClockOffset] != "" {
|
||||
clockParsed, err := strconv.ParseInt(physicalCPU[psrClockOffset], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse value %q for clock as 32-bit integer: %s", physicalCPU[10], err)
|
||||
}
|
||||
clock = float64(clockParsed)
|
||||
}
|
||||
|
||||
var err error
|
||||
var numCores int64
|
||||
var numHT int64
|
||||
switch {
|
||||
case physicalCPU[psrNumCoresOffset] != "":
|
||||
numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresOffset], 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[1], err)
|
||||
}
|
||||
|
||||
for i := 0; i < int(numCores); i++ {
|
||||
result = append(result, InfoStat{
|
||||
CPU: infoStatCount,
|
||||
PhysicalID: strconv.Itoa(physicalIndex),
|
||||
CoreID: strconv.Itoa(i),
|
||||
Cores: 1,
|
||||
VendorID: physicalCPU[psrVendorIDOffset],
|
||||
ModelName: physicalCPU[psrModelNameOffset],
|
||||
Family: physicalCPU[psrFamilyOffset],
|
||||
Model: physicalCPU[psrModelOffset],
|
||||
Stepping: step,
|
||||
Mhz: clock,
|
||||
})
|
||||
infoStatCount++
|
||||
}
|
||||
case physicalCPU[psrNumCoresHTOffset] != "":
|
||||
numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresHTOffset], 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[3], err)
|
||||
}
|
||||
|
||||
numHT, err = strconv.ParseInt(physicalCPU[psrNumHTOffset], 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse value %q for hyperthread count as 32-bit integer: %s", physicalCPU[4], err)
|
||||
}
|
||||
|
||||
for i := 0; i < int(numCores); i++ {
|
||||
result = append(result, InfoStat{
|
||||
CPU: infoStatCount,
|
||||
PhysicalID: strconv.Itoa(physicalIndex),
|
||||
CoreID: strconv.Itoa(i),
|
||||
Cores: int32(numHT) / int32(numCores),
|
||||
VendorID: physicalCPU[psrVendorIDOffset],
|
||||
ModelName: physicalCPU[psrModelNameOffset],
|
||||
Family: physicalCPU[psrFamilyOffset],
|
||||
Model: physicalCPU[psrModelOffset],
|
||||
Stepping: step,
|
||||
Mhz: clock,
|
||||
})
|
||||
infoStatCount++
|
||||
}
|
||||
default:
|
||||
return nil, errors.New("values for cores with and without hyperthreading are both set")
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
return runtime.NumCPU(), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,229 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package cpu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
"github.com/yusufpapurcu/wmi"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
var (
|
||||
procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo")
|
||||
)
|
||||
|
||||
type win32_Processor struct {
|
||||
Family uint16
|
||||
Manufacturer string
|
||||
Name string
|
||||
NumberOfLogicalProcessors uint32
|
||||
NumberOfCores uint32
|
||||
ProcessorID *string
|
||||
Stepping *string
|
||||
MaxClockSpeed uint32
|
||||
}
|
||||
|
||||
// SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
|
||||
// defined in windows api doc with the following
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/winternl/nf-winternl-ntquerysysteminformation#system_processor_performance_information
|
||||
// additional fields documented here
|
||||
// https://www.geoffchappell.com/studies/windows/km/ntoskrnl/api/ex/sysinfo/processor_performance.htm
|
||||
type win32_SystemProcessorPerformanceInformation struct {
|
||||
IdleTime int64 // idle time in 100ns (this is not a filetime).
|
||||
KernelTime int64 // kernel time in 100ns. kernel time includes idle time. (this is not a filetime).
|
||||
UserTime int64 // usertime in 100ns (this is not a filetime).
|
||||
DpcTime int64 // dpc time in 100ns (this is not a filetime).
|
||||
InterruptTime int64 // interrupt time in 100ns
|
||||
InterruptCount uint32
|
||||
}
|
||||
|
||||
const (
|
||||
ClocksPerSec = 10000000.0
|
||||
|
||||
// systemProcessorPerformanceInformationClass information class to query with NTQuerySystemInformation
|
||||
// https://processhacker.sourceforge.io/doc/ntexapi_8h.html#ad5d815b48e8f4da1ef2eb7a2f18a54e0
|
||||
win32_SystemProcessorPerformanceInformationClass = 8
|
||||
|
||||
// size of systemProcessorPerformanceInfoSize in memory
|
||||
win32_SystemProcessorPerformanceInfoSize = uint32(unsafe.Sizeof(win32_SystemProcessorPerformanceInformation{}))
|
||||
)
|
||||
|
||||
// Times returns times stat per cpu and combined for all CPUs
|
||||
func Times(percpu bool) ([]TimesStat, error) {
|
||||
return TimesWithContext(context.Background(), percpu)
|
||||
}
|
||||
|
||||
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
||||
if percpu {
|
||||
return perCPUTimes()
|
||||
}
|
||||
|
||||
var ret []TimesStat
|
||||
var lpIdleTime common.FILETIME
|
||||
var lpKernelTime common.FILETIME
|
||||
var lpUserTime common.FILETIME
|
||||
r, _, _ := common.ProcGetSystemTimes.Call(
|
||||
uintptr(unsafe.Pointer(&lpIdleTime)),
|
||||
uintptr(unsafe.Pointer(&lpKernelTime)),
|
||||
uintptr(unsafe.Pointer(&lpUserTime)))
|
||||
if r == 0 {
|
||||
return ret, windows.GetLastError()
|
||||
}
|
||||
|
||||
LOT := float64(0.0000001)
|
||||
HIT := (LOT * 4294967296.0)
|
||||
idle := ((HIT * float64(lpIdleTime.DwHighDateTime)) + (LOT * float64(lpIdleTime.DwLowDateTime)))
|
||||
user := ((HIT * float64(lpUserTime.DwHighDateTime)) + (LOT * float64(lpUserTime.DwLowDateTime)))
|
||||
kernel := ((HIT * float64(lpKernelTime.DwHighDateTime)) + (LOT * float64(lpKernelTime.DwLowDateTime)))
|
||||
system := (kernel - idle)
|
||||
|
||||
ret = append(ret, TimesStat{
|
||||
CPU: "cpu-total",
|
||||
Idle: float64(idle),
|
||||
User: float64(user),
|
||||
System: float64(system),
|
||||
})
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func Info() ([]InfoStat, error) {
|
||||
return InfoWithContext(context.Background())
|
||||
}
|
||||
|
||||
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
||||
var ret []InfoStat
|
||||
var dst []win32_Processor
|
||||
q := wmi.CreateQuery(&dst, "")
|
||||
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
|
||||
return ret, err
|
||||
}
|
||||
|
||||
var procID string
|
||||
for i, l := range dst {
|
||||
procID = ""
|
||||
if l.ProcessorID != nil {
|
||||
procID = *l.ProcessorID
|
||||
}
|
||||
|
||||
cpu := InfoStat{
|
||||
CPU: int32(i),
|
||||
Family: fmt.Sprintf("%d", l.Family),
|
||||
VendorID: l.Manufacturer,
|
||||
ModelName: l.Name,
|
||||
Cores: int32(l.NumberOfLogicalProcessors),
|
||||
PhysicalID: procID,
|
||||
Mhz: float64(l.MaxClockSpeed),
|
||||
Flags: []string{},
|
||||
}
|
||||
ret = append(ret, cpu)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// perCPUTimes returns times stat per cpu, per core and overall for all CPUs
|
||||
func perCPUTimes() ([]TimesStat, error) {
|
||||
var ret []TimesStat
|
||||
stats, err := perfInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for core, v := range stats {
|
||||
c := TimesStat{
|
||||
CPU: fmt.Sprintf("cpu%d", core),
|
||||
User: float64(v.UserTime) / ClocksPerSec,
|
||||
System: float64(v.KernelTime-v.IdleTime) / ClocksPerSec,
|
||||
Idle: float64(v.IdleTime) / ClocksPerSec,
|
||||
Irq: float64(v.InterruptTime) / ClocksPerSec,
|
||||
}
|
||||
ret = append(ret, c)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// makes call to Windows API function to retrieve performance information for each core
|
||||
func perfInfo() ([]win32_SystemProcessorPerformanceInformation, error) {
|
||||
// Make maxResults large for safety.
|
||||
// We can't invoke the api call with a results array that's too small.
|
||||
// If we have more than 2056 cores on a single host, then it's probably the future.
|
||||
maxBuffer := 2056
|
||||
// buffer for results from the windows proc
|
||||
resultBuffer := make([]win32_SystemProcessorPerformanceInformation, maxBuffer)
|
||||
// size of the buffer in memory
|
||||
bufferSize := uintptr(win32_SystemProcessorPerformanceInfoSize) * uintptr(maxBuffer)
|
||||
// size of the returned response
|
||||
var retSize uint32
|
||||
|
||||
// Invoke windows api proc.
|
||||
// The returned err from the windows dll proc will always be non-nil even when successful.
|
||||
// See https://godoc.org/golang.org/x/sys/windows#LazyProc.Call for more information
|
||||
retCode, _, err := common.ProcNtQuerySystemInformation.Call(
|
||||
win32_SystemProcessorPerformanceInformationClass, // System Information Class -> SystemProcessorPerformanceInformation
|
||||
uintptr(unsafe.Pointer(&resultBuffer[0])), // pointer to first element in result buffer
|
||||
bufferSize, // size of the buffer in memory
|
||||
uintptr(unsafe.Pointer(&retSize)), // pointer to the size of the returned results the windows proc will set this
|
||||
)
|
||||
|
||||
// check return code for errors
|
||||
if retCode != 0 {
|
||||
return nil, fmt.Errorf("call to NtQuerySystemInformation returned %d. err: %s", retCode, err.Error())
|
||||
}
|
||||
|
||||
// calculate the number of returned elements based on the returned size
|
||||
numReturnedElements := retSize / win32_SystemProcessorPerformanceInfoSize
|
||||
|
||||
// trim results to the number of returned elements
|
||||
resultBuffer = resultBuffer[:numReturnedElements]
|
||||
|
||||
return resultBuffer, nil
|
||||
}
|
||||
|
||||
// SystemInfo is an equivalent representation of SYSTEM_INFO in the Windows API.
|
||||
// https://msdn.microsoft.com/en-us/library/ms724958%28VS.85%29.aspx?f=255&MSPPError=-2147217396
|
||||
// https://github.com/elastic/go-windows/blob/bb1581babc04d5cb29a2bfa7a9ac6781c730c8dd/kernel32.go#L43
|
||||
type systemInfo struct {
|
||||
wProcessorArchitecture uint16
|
||||
wReserved uint16
|
||||
dwPageSize uint32
|
||||
lpMinimumApplicationAddress uintptr
|
||||
lpMaximumApplicationAddress uintptr
|
||||
dwActiveProcessorMask uintptr
|
||||
dwNumberOfProcessors uint32
|
||||
dwProcessorType uint32
|
||||
dwAllocationGranularity uint32
|
||||
wProcessorLevel uint16
|
||||
wProcessorRevision uint16
|
||||
}
|
||||
|
||||
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
||||
if logical {
|
||||
// https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L97
|
||||
ret := windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS)
|
||||
if ret != 0 {
|
||||
return int(ret), nil
|
||||
}
|
||||
var systemInfo systemInfo
|
||||
_, _, err := procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo)))
|
||||
if systemInfo.dwNumberOfProcessors == 0 {
|
||||
return 0, err
|
||||
}
|
||||
return int(systemInfo.dwNumberOfProcessors), nil
|
||||
}
|
||||
// physical cores https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L499
|
||||
// for the time being, try with unreliable and slow WMI call…
|
||||
var dst []win32_Processor
|
||||
q := wmi.CreateQuery(&dst, "")
|
||||
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var count uint32
|
||||
for _, d := range dst {
|
||||
count += d.NumberOfCores
|
||||
}
|
||||
return int(count), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,637 @@
|
|||
package common
|
||||
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package binary implements simple translation between numbers and byte
|
||||
// sequences and encoding and decoding of varints.
|
||||
//
|
||||
// Numbers are translated by reading and writing fixed-size values.
|
||||
// A fixed-size value is either a fixed-size arithmetic
|
||||
// type (int8, uint8, int16, float32, complex64, ...)
|
||||
// or an array or struct containing only fixed-size values.
|
||||
//
|
||||
// The varint functions encode and decode single integer values using
|
||||
// a variable-length encoding; smaller values require fewer bytes.
|
||||
// For a specification, see
|
||||
// http://code.google.com/apis/protocolbuffers/docs/encoding.html.
|
||||
//
|
||||
// This package favors simplicity over efficiency. Clients that require
|
||||
// high-performance serialization, especially for large data structures,
|
||||
// should look at more advanced solutions such as the encoding/gob
|
||||
// package or protocol buffers.
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"math"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// A ByteOrder specifies how to convert byte sequences into
|
||||
// 16-, 32-, or 64-bit unsigned integers.
|
||||
type ByteOrder interface {
|
||||
Uint16([]byte) uint16
|
||||
Uint32([]byte) uint32
|
||||
Uint64([]byte) uint64
|
||||
PutUint16([]byte, uint16)
|
||||
PutUint32([]byte, uint32)
|
||||
PutUint64([]byte, uint64)
|
||||
String() string
|
||||
}
|
||||
|
||||
// LittleEndian is the little-endian implementation of ByteOrder.
|
||||
var LittleEndian littleEndian
|
||||
|
||||
// BigEndian is the big-endian implementation of ByteOrder.
|
||||
var BigEndian bigEndian
|
||||
|
||||
type littleEndian struct{}
|
||||
|
||||
func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 }
|
||||
|
||||
func (littleEndian) PutUint16(b []byte, v uint16) {
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> 8)
|
||||
}
|
||||
|
||||
func (littleEndian) Uint32(b []byte) uint32 {
|
||||
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
|
||||
}
|
||||
|
||||
func (littleEndian) PutUint32(b []byte, v uint32) {
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> 8)
|
||||
b[2] = byte(v >> 16)
|
||||
b[3] = byte(v >> 24)
|
||||
}
|
||||
|
||||
func (littleEndian) Uint64(b []byte) uint64 {
|
||||
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
|
||||
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
|
||||
}
|
||||
|
||||
func (littleEndian) PutUint64(b []byte, v uint64) {
|
||||
b[0] = byte(v)
|
||||
b[1] = byte(v >> 8)
|
||||
b[2] = byte(v >> 16)
|
||||
b[3] = byte(v >> 24)
|
||||
b[4] = byte(v >> 32)
|
||||
b[5] = byte(v >> 40)
|
||||
b[6] = byte(v >> 48)
|
||||
b[7] = byte(v >> 56)
|
||||
}
|
||||
|
||||
func (littleEndian) String() string { return "LittleEndian" }
|
||||
|
||||
func (littleEndian) GoString() string { return "binary.LittleEndian" }
|
||||
|
||||
type bigEndian struct{}
|
||||
|
||||
func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 }
|
||||
|
||||
func (bigEndian) PutUint16(b []byte, v uint16) {
|
||||
b[0] = byte(v >> 8)
|
||||
b[1] = byte(v)
|
||||
}
|
||||
|
||||
func (bigEndian) Uint32(b []byte) uint32 {
|
||||
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
|
||||
}
|
||||
|
||||
func (bigEndian) PutUint32(b []byte, v uint32) {
|
||||
b[0] = byte(v >> 24)
|
||||
b[1] = byte(v >> 16)
|
||||
b[2] = byte(v >> 8)
|
||||
b[3] = byte(v)
|
||||
}
|
||||
|
||||
func (bigEndian) Uint64(b []byte) uint64 {
|
||||
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
|
||||
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
|
||||
}
|
||||
|
||||
func (bigEndian) PutUint64(b []byte, v uint64) {
|
||||
b[0] = byte(v >> 56)
|
||||
b[1] = byte(v >> 48)
|
||||
b[2] = byte(v >> 40)
|
||||
b[3] = byte(v >> 32)
|
||||
b[4] = byte(v >> 24)
|
||||
b[5] = byte(v >> 16)
|
||||
b[6] = byte(v >> 8)
|
||||
b[7] = byte(v)
|
||||
}
|
||||
|
||||
func (bigEndian) String() string { return "BigEndian" }
|
||||
|
||||
func (bigEndian) GoString() string { return "binary.BigEndian" }
|
||||
|
||||
// Read reads structured binary data from r into data.
|
||||
// Data must be a pointer to a fixed-size value or a slice
|
||||
// of fixed-size values.
|
||||
// Bytes read from r are decoded using the specified byte order
|
||||
// and written to successive fields of the data.
|
||||
// When reading into structs, the field data for fields with
|
||||
// blank (_) field names is skipped; i.e., blank field names
|
||||
// may be used for padding.
|
||||
// When reading into a struct, all non-blank fields must be exported.
|
||||
func Read(r io.Reader, order ByteOrder, data interface{}) error {
|
||||
// Fast path for basic types and slices.
|
||||
if n := intDataSize(data); n != 0 {
|
||||
var b [8]byte
|
||||
var bs []byte
|
||||
if n > len(b) {
|
||||
bs = make([]byte, n)
|
||||
} else {
|
||||
bs = b[:n]
|
||||
}
|
||||
if _, err := io.ReadFull(r, bs); err != nil {
|
||||
return err
|
||||
}
|
||||
switch data := data.(type) {
|
||||
case *int8:
|
||||
*data = int8(b[0])
|
||||
case *uint8:
|
||||
*data = b[0]
|
||||
case *int16:
|
||||
*data = int16(order.Uint16(bs))
|
||||
case *uint16:
|
||||
*data = order.Uint16(bs)
|
||||
case *int32:
|
||||
*data = int32(order.Uint32(bs))
|
||||
case *uint32:
|
||||
*data = order.Uint32(bs)
|
||||
case *int64:
|
||||
*data = int64(order.Uint64(bs))
|
||||
case *uint64:
|
||||
*data = order.Uint64(bs)
|
||||
case []int8:
|
||||
for i, x := range bs { // Easier to loop over the input for 8-bit values.
|
||||
data[i] = int8(x)
|
||||
}
|
||||
case []uint8:
|
||||
copy(data, bs)
|
||||
case []int16:
|
||||
for i := range data {
|
||||
data[i] = int16(order.Uint16(bs[2*i:]))
|
||||
}
|
||||
case []uint16:
|
||||
for i := range data {
|
||||
data[i] = order.Uint16(bs[2*i:])
|
||||
}
|
||||
case []int32:
|
||||
for i := range data {
|
||||
data[i] = int32(order.Uint32(bs[4*i:]))
|
||||
}
|
||||
case []uint32:
|
||||
for i := range data {
|
||||
data[i] = order.Uint32(bs[4*i:])
|
||||
}
|
||||
case []int64:
|
||||
for i := range data {
|
||||
data[i] = int64(order.Uint64(bs[8*i:]))
|
||||
}
|
||||
case []uint64:
|
||||
for i := range data {
|
||||
data[i] = order.Uint64(bs[8*i:])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Fallback to reflect-based decoding.
|
||||
v := reflect.ValueOf(data)
|
||||
size := -1
|
||||
switch v.Kind() {
|
||||
case reflect.Ptr:
|
||||
v = v.Elem()
|
||||
size = dataSize(v)
|
||||
case reflect.Slice:
|
||||
size = dataSize(v)
|
||||
}
|
||||
if size < 0 {
|
||||
return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String())
|
||||
}
|
||||
d := &decoder{order: order, buf: make([]byte, size)}
|
||||
if _, err := io.ReadFull(r, d.buf); err != nil {
|
||||
return err
|
||||
}
|
||||
d.value(v)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write writes the binary representation of data into w.
|
||||
// Data must be a fixed-size value or a slice of fixed-size
|
||||
// values, or a pointer to such data.
|
||||
// Bytes written to w are encoded using the specified byte order
|
||||
// and read from successive fields of the data.
|
||||
// When writing structs, zero values are written for fields
|
||||
// with blank (_) field names.
|
||||
func Write(w io.Writer, order ByteOrder, data interface{}) error {
|
||||
// Fast path for basic types and slices.
|
||||
if n := intDataSize(data); n != 0 {
|
||||
var b [8]byte
|
||||
var bs []byte
|
||||
if n > len(b) {
|
||||
bs = make([]byte, n)
|
||||
} else {
|
||||
bs = b[:n]
|
||||
}
|
||||
switch v := data.(type) {
|
||||
case *int8:
|
||||
bs = b[:1]
|
||||
b[0] = byte(*v)
|
||||
case int8:
|
||||
bs = b[:1]
|
||||
b[0] = byte(v)
|
||||
case []int8:
|
||||
for i, x := range v {
|
||||
bs[i] = byte(x)
|
||||
}
|
||||
case *uint8:
|
||||
bs = b[:1]
|
||||
b[0] = *v
|
||||
case uint8:
|
||||
bs = b[:1]
|
||||
b[0] = byte(v)
|
||||
case []uint8:
|
||||
bs = v
|
||||
case *int16:
|
||||
bs = b[:2]
|
||||
order.PutUint16(bs, uint16(*v))
|
||||
case int16:
|
||||
bs = b[:2]
|
||||
order.PutUint16(bs, uint16(v))
|
||||
case []int16:
|
||||
for i, x := range v {
|
||||
order.PutUint16(bs[2*i:], uint16(x))
|
||||
}
|
||||
case *uint16:
|
||||
bs = b[:2]
|
||||
order.PutUint16(bs, *v)
|
||||
case uint16:
|
||||
bs = b[:2]
|
||||
order.PutUint16(bs, v)
|
||||
case []uint16:
|
||||
for i, x := range v {
|
||||
order.PutUint16(bs[2*i:], x)
|
||||
}
|
||||
case *int32:
|
||||
bs = b[:4]
|
||||
order.PutUint32(bs, uint32(*v))
|
||||
case int32:
|
||||
bs = b[:4]
|
||||
order.PutUint32(bs, uint32(v))
|
||||
case []int32:
|
||||
for i, x := range v {
|
||||
order.PutUint32(bs[4*i:], uint32(x))
|
||||
}
|
||||
case *uint32:
|
||||
bs = b[:4]
|
||||
order.PutUint32(bs, *v)
|
||||
case uint32:
|
||||
bs = b[:4]
|
||||
order.PutUint32(bs, v)
|
||||
case []uint32:
|
||||
for i, x := range v {
|
||||
order.PutUint32(bs[4*i:], x)
|
||||
}
|
||||
case *int64:
|
||||
bs = b[:8]
|
||||
order.PutUint64(bs, uint64(*v))
|
||||
case int64:
|
||||
bs = b[:8]
|
||||
order.PutUint64(bs, uint64(v))
|
||||
case []int64:
|
||||
for i, x := range v {
|
||||
order.PutUint64(bs[8*i:], uint64(x))
|
||||
}
|
||||
case *uint64:
|
||||
bs = b[:8]
|
||||
order.PutUint64(bs, *v)
|
||||
case uint64:
|
||||
bs = b[:8]
|
||||
order.PutUint64(bs, v)
|
||||
case []uint64:
|
||||
for i, x := range v {
|
||||
order.PutUint64(bs[8*i:], x)
|
||||
}
|
||||
}
|
||||
_, err := w.Write(bs)
|
||||
return err
|
||||
}
|
||||
|
||||
// Fallback to reflect-based encoding.
|
||||
v := reflect.Indirect(reflect.ValueOf(data))
|
||||
size := dataSize(v)
|
||||
if size < 0 {
|
||||
return errors.New("binary.Write: invalid type " + reflect.TypeOf(data).String())
|
||||
}
|
||||
buf := make([]byte, size)
|
||||
e := &encoder{order: order, buf: buf}
|
||||
e.value(v)
|
||||
_, err := w.Write(buf)
|
||||
return err
|
||||
}
|
||||
|
||||
// Size returns how many bytes Write would generate to encode the value v, which
|
||||
// must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.
|
||||
// If v is neither of these, Size returns -1.
|
||||
func Size(v interface{}) int {
|
||||
return dataSize(reflect.Indirect(reflect.ValueOf(v)))
|
||||
}
|
||||
|
||||
// dataSize returns the number of bytes the actual data represented by v occupies in memory.
|
||||
// For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
|
||||
// it returns the length of the slice times the element size and does not count the memory
|
||||
// occupied by the header. If the type of v is not acceptable, dataSize returns -1.
|
||||
func dataSize(v reflect.Value) int {
|
||||
if v.Kind() == reflect.Slice {
|
||||
if s := sizeof(v.Type().Elem()); s >= 0 {
|
||||
return s * v.Len()
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return sizeof(v.Type())
|
||||
}
|
||||
|
||||
// sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable.
|
||||
func sizeof(t reflect.Type) int {
|
||||
switch t.Kind() {
|
||||
case reflect.Array:
|
||||
if s := sizeof(t.Elem()); s >= 0 {
|
||||
return s * t.Len()
|
||||
}
|
||||
|
||||
case reflect.Struct:
|
||||
sum := 0
|
||||
for i, n := 0, t.NumField(); i < n; i++ {
|
||||
s := sizeof(t.Field(i).Type)
|
||||
if s < 0 {
|
||||
return -1
|
||||
}
|
||||
sum += s
|
||||
}
|
||||
return sum
|
||||
|
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Ptr:
|
||||
return int(t.Size())
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
type coder struct {
|
||||
order ByteOrder
|
||||
buf []byte
|
||||
}
|
||||
|
||||
type (
|
||||
decoder coder
|
||||
encoder coder
|
||||
)
|
||||
|
||||
func (d *decoder) uint8() uint8 {
|
||||
x := d.buf[0]
|
||||
d.buf = d.buf[1:]
|
||||
return x
|
||||
}
|
||||
|
||||
func (e *encoder) uint8(x uint8) {
|
||||
e.buf[0] = x
|
||||
e.buf = e.buf[1:]
|
||||
}
|
||||
|
||||
func (d *decoder) uint16() uint16 {
|
||||
x := d.order.Uint16(d.buf[0:2])
|
||||
d.buf = d.buf[2:]
|
||||
return x
|
||||
}
|
||||
|
||||
func (e *encoder) uint16(x uint16) {
|
||||
e.order.PutUint16(e.buf[0:2], x)
|
||||
e.buf = e.buf[2:]
|
||||
}
|
||||
|
||||
func (d *decoder) uint32() uint32 {
|
||||
x := d.order.Uint32(d.buf[0:4])
|
||||
d.buf = d.buf[4:]
|
||||
return x
|
||||
}
|
||||
|
||||
func (e *encoder) uint32(x uint32) {
|
||||
e.order.PutUint32(e.buf[0:4], x)
|
||||
e.buf = e.buf[4:]
|
||||
}
|
||||
|
||||
func (d *decoder) uint64() uint64 {
|
||||
x := d.order.Uint64(d.buf[0:8])
|
||||
d.buf = d.buf[8:]
|
||||
return x
|
||||
}
|
||||
|
||||
func (e *encoder) uint64(x uint64) {
|
||||
e.order.PutUint64(e.buf[0:8], x)
|
||||
e.buf = e.buf[8:]
|
||||
}
|
||||
|
||||
func (d *decoder) int8() int8 { return int8(d.uint8()) }
|
||||
|
||||
func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
|
||||
|
||||
func (d *decoder) int16() int16 { return int16(d.uint16()) }
|
||||
|
||||
func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
|
||||
|
||||
func (d *decoder) int32() int32 { return int32(d.uint32()) }
|
||||
|
||||
func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
|
||||
|
||||
func (d *decoder) int64() int64 { return int64(d.uint64()) }
|
||||
|
||||
func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
|
||||
|
||||
func (d *decoder) value(v reflect.Value) {
|
||||
switch v.Kind() {
|
||||
case reflect.Array:
|
||||
l := v.Len()
|
||||
for i := 0; i < l; i++ {
|
||||
d.value(v.Index(i))
|
||||
}
|
||||
|
||||
case reflect.Struct:
|
||||
t := v.Type()
|
||||
l := v.NumField()
|
||||
for i := 0; i < l; i++ {
|
||||
// Note: Calling v.CanSet() below is an optimization.
|
||||
// It would be sufficient to check the field name,
|
||||
// but creating the StructField info for each field is
|
||||
// costly (run "go test -bench=ReadStruct" and compare
|
||||
// results when making changes to this code).
|
||||
if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
|
||||
d.value(v)
|
||||
} else {
|
||||
d.skip(v)
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.Slice:
|
||||
l := v.Len()
|
||||
for i := 0; i < l; i++ {
|
||||
d.value(v.Index(i))
|
||||
}
|
||||
|
||||
case reflect.Int8:
|
||||
v.SetInt(int64(d.int8()))
|
||||
case reflect.Int16:
|
||||
v.SetInt(int64(d.int16()))
|
||||
case reflect.Int32:
|
||||
v.SetInt(int64(d.int32()))
|
||||
case reflect.Int64:
|
||||
v.SetInt(d.int64())
|
||||
|
||||
case reflect.Uint8:
|
||||
v.SetUint(uint64(d.uint8()))
|
||||
case reflect.Uint16:
|
||||
v.SetUint(uint64(d.uint16()))
|
||||
case reflect.Uint32:
|
||||
v.SetUint(uint64(d.uint32()))
|
||||
case reflect.Uint64:
|
||||
v.SetUint(d.uint64())
|
||||
|
||||
case reflect.Float32:
|
||||
v.SetFloat(float64(math.Float32frombits(d.uint32())))
|
||||
case reflect.Float64:
|
||||
v.SetFloat(math.Float64frombits(d.uint64()))
|
||||
|
||||
case reflect.Complex64:
|
||||
v.SetComplex(complex(
|
||||
float64(math.Float32frombits(d.uint32())),
|
||||
float64(math.Float32frombits(d.uint32())),
|
||||
))
|
||||
case reflect.Complex128:
|
||||
v.SetComplex(complex(
|
||||
math.Float64frombits(d.uint64()),
|
||||
math.Float64frombits(d.uint64()),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
func (e *encoder) value(v reflect.Value) {
|
||||
switch v.Kind() {
|
||||
case reflect.Array:
|
||||
l := v.Len()
|
||||
for i := 0; i < l; i++ {
|
||||
e.value(v.Index(i))
|
||||
}
|
||||
|
||||
case reflect.Struct:
|
||||
t := v.Type()
|
||||
l := v.NumField()
|
||||
for i := 0; i < l; i++ {
|
||||
// see comment for corresponding code in decoder.value()
|
||||
if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
|
||||
e.value(v)
|
||||
} else {
|
||||
e.skip(v)
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.Slice:
|
||||
l := v.Len()
|
||||
for i := 0; i < l; i++ {
|
||||
e.value(v.Index(i))
|
||||
}
|
||||
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Int8:
|
||||
e.int8(int8(v.Int()))
|
||||
case reflect.Int16:
|
||||
e.int16(int16(v.Int()))
|
||||
case reflect.Int32:
|
||||
e.int32(int32(v.Int()))
|
||||
case reflect.Int64:
|
||||
e.int64(v.Int())
|
||||
}
|
||||
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Uint8:
|
||||
e.uint8(uint8(v.Uint()))
|
||||
case reflect.Uint16:
|
||||
e.uint16(uint16(v.Uint()))
|
||||
case reflect.Uint32:
|
||||
e.uint32(uint32(v.Uint()))
|
||||
case reflect.Uint64:
|
||||
e.uint64(v.Uint())
|
||||
}
|
||||
|
||||
case reflect.Float32, reflect.Float64:
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Float32:
|
||||
e.uint32(math.Float32bits(float32(v.Float())))
|
||||
case reflect.Float64:
|
||||
e.uint64(math.Float64bits(v.Float()))
|
||||
}
|
||||
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Complex64:
|
||||
x := v.Complex()
|
||||
e.uint32(math.Float32bits(float32(real(x))))
|
||||
e.uint32(math.Float32bits(float32(imag(x))))
|
||||
case reflect.Complex128:
|
||||
x := v.Complex()
|
||||
e.uint64(math.Float64bits(real(x)))
|
||||
e.uint64(math.Float64bits(imag(x)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *decoder) skip(v reflect.Value) {
|
||||
d.buf = d.buf[dataSize(v):]
|
||||
}
|
||||
|
||||
func (e *encoder) skip(v reflect.Value) {
|
||||
n := dataSize(v)
|
||||
for i := range e.buf[0:n] {
|
||||
e.buf[i] = 0
|
||||
}
|
||||
e.buf = e.buf[n:]
|
||||
}
|
||||
|
||||
// intDataSize returns the size of the data required to represent the data when encoded.
|
||||
// It returns zero if the type cannot be implemented by the fast path in Read or Write.
|
||||
func intDataSize(data interface{}) int {
|
||||
switch data := data.(type) {
|
||||
case int8, *int8, *uint8:
|
||||
return 1
|
||||
case []int8:
|
||||
return len(data)
|
||||
case []uint8:
|
||||
return len(data)
|
||||
case int16, *int16, *uint16:
|
||||
return 2
|
||||
case []int16:
|
||||
return 2 * len(data)
|
||||
case []uint16:
|
||||
return 2 * len(data)
|
||||
case int32, *int32, *uint32:
|
||||
return 4
|
||||
case []int32:
|
||||
return 4 * len(data)
|
||||
case []uint32:
|
||||
return 4 * len(data)
|
||||
case int64, *int64, *uint64:
|
||||
return 8
|
||||
case []int64:
|
||||
return 8 * len(data)
|
||||
case []uint64:
|
||||
return 8 * len(data)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
|
@ -0,0 +1,464 @@
|
|||
package common
|
||||
|
||||
//
|
||||
// gopsutil is a port of psutil(http://pythonhosted.org/psutil/).
|
||||
// This covers these architectures.
|
||||
// - linux (amd64, arm)
|
||||
// - freebsd (amd64)
|
||||
// - windows (amd64)
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/common"
|
||||
)
|
||||
|
||||
var (
|
||||
Timeout = 3 * time.Second
|
||||
ErrTimeout = errors.New("command timed out")
|
||||
)
|
||||
|
||||
type Invoker interface {
|
||||
Command(string, ...string) ([]byte, error)
|
||||
CommandWithContext(context.Context, string, ...string) ([]byte, error)
|
||||
}
|
||||
|
||||
type Invoke struct{}
|
||||
|
||||
func (i Invoke) Command(name string, arg ...string) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
|
||||
defer cancel()
|
||||
return i.CommandWithContext(ctx, name, arg...)
|
||||
}
|
||||
|
||||
func (i Invoke) CommandWithContext(ctx context.Context, name string, arg ...string) ([]byte, error) {
|
||||
cmd := exec.CommandContext(ctx, name, arg...)
|
||||
|
||||
var buf bytes.Buffer
|
||||
cmd.Stdout = &buf
|
||||
cmd.Stderr = &buf
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
type FakeInvoke struct {
|
||||
Suffix string // Suffix species expected file name suffix such as "fail"
|
||||
Error error // If Error specified, return the error.
|
||||
}
|
||||
|
||||
// Command in FakeInvoke returns from expected file if exists.
|
||||
func (i FakeInvoke) Command(name string, arg ...string) ([]byte, error) {
|
||||
if i.Error != nil {
|
||||
return []byte{}, i.Error
|
||||
}
|
||||
|
||||
arch := runtime.GOOS
|
||||
|
||||
commandName := filepath.Base(name)
|
||||
|
||||
fname := strings.Join(append([]string{commandName}, arg...), "")
|
||||
fname = url.QueryEscape(fname)
|
||||
fpath := path.Join("testdata", arch, fname)
|
||||
if i.Suffix != "" {
|
||||
fpath += "_" + i.Suffix
|
||||
}
|
||||
if PathExists(fpath) {
|
||||
return os.ReadFile(fpath)
|
||||
}
|
||||
return []byte{}, fmt.Errorf("could not find testdata: %s", fpath)
|
||||
}
|
||||
|
||||
func (i FakeInvoke) CommandWithContext(ctx context.Context, name string, arg ...string) ([]byte, error) {
|
||||
return i.Command(name, arg...)
|
||||
}
|
||||
|
||||
var ErrNotImplementedError = errors.New("not implemented yet")
|
||||
|
||||
// ReadFile reads contents from a file
|
||||
func ReadFile(filename string) (string, error) {
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
// ReadLines reads contents from a file and splits them by new lines.
|
||||
// A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).
|
||||
func ReadLines(filename string) ([]string, error) {
|
||||
return ReadLinesOffsetN(filename, 0, -1)
|
||||
}
|
||||
|
||||
// ReadLine reads a file and returns the first occurrence of a line that is prefixed with prefix.
|
||||
func ReadLine(filename string, prefix string) (string, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
r := bufio.NewReader(f)
|
||||
for {
|
||||
line, err := r.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if strings.HasPrefix(line, prefix) {
|
||||
return line, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// ReadLinesOffsetN reads contents from file and splits them by new line.
|
||||
// The offset tells at which line number to start.
|
||||
// The count determines the number of lines to read (starting from offset):
|
||||
// n >= 0: at most n lines
|
||||
// n < 0: whole file
|
||||
func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return []string{""}, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var ret []string
|
||||
|
||||
r := bufio.NewReader(f)
|
||||
for i := 0; i < n+int(offset) || n < 0; i++ {
|
||||
line, err := r.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF && len(line) > 0 {
|
||||
ret = append(ret, strings.Trim(line, "\n"))
|
||||
}
|
||||
break
|
||||
}
|
||||
if i < int(offset) {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, strings.Trim(line, "\n"))
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func IntToString(orig []int8) string {
|
||||
ret := make([]byte, len(orig))
|
||||
size := -1
|
||||
for i, o := range orig {
|
||||
if o == 0 {
|
||||
size = i
|
||||
break
|
||||
}
|
||||
ret[i] = byte(o)
|
||||
}
|
||||
if size == -1 {
|
||||
size = len(orig)
|
||||
}
|
||||
|
||||
return string(ret[0:size])
|
||||
}
|
||||
|
||||
func UintToString(orig []uint8) string {
|
||||
ret := make([]byte, len(orig))
|
||||
size := -1
|
||||
for i, o := range orig {
|
||||
if o == 0 {
|
||||
size = i
|
||||
break
|
||||
}
|
||||
ret[i] = byte(o)
|
||||
}
|
||||
if size == -1 {
|
||||
size = len(orig)
|
||||
}
|
||||
|
||||
return string(ret[0:size])
|
||||
}
|
||||
|
||||
func ByteToString(orig []byte) string {
|
||||
n := -1
|
||||
l := -1
|
||||
for i, b := range orig {
|
||||
// skip left side null
|
||||
if l == -1 && b == 0 {
|
||||
continue
|
||||
}
|
||||
if l == -1 {
|
||||
l = i
|
||||
}
|
||||
|
||||
if b == 0 {
|
||||
break
|
||||
}
|
||||
n = i + 1
|
||||
}
|
||||
if n == -1 {
|
||||
return string(orig)
|
||||
}
|
||||
return string(orig[l:n])
|
||||
}
|
||||
|
||||
// ReadInts reads contents from single line file and returns them as []int32.
|
||||
func ReadInts(filename string) ([]int64, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return []int64{}, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var ret []int64
|
||||
|
||||
r := bufio.NewReader(f)
|
||||
|
||||
// The int files that this is concerned with should only be one liners.
|
||||
line, err := r.ReadString('\n')
|
||||
if err != nil {
|
||||
return []int64{}, err
|
||||
}
|
||||
|
||||
i, err := strconv.ParseInt(strings.Trim(line, "\n"), 10, 32)
|
||||
if err != nil {
|
||||
return []int64{}, err
|
||||
}
|
||||
ret = append(ret, i)
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Parse Hex to uint32 without error
|
||||
func HexToUint32(hex string) uint32 {
|
||||
vv, _ := strconv.ParseUint(hex, 16, 32)
|
||||
return uint32(vv)
|
||||
}
|
||||
|
||||
// Parse to int32 without error
|
||||
func mustParseInt32(val string) int32 {
|
||||
vv, _ := strconv.ParseInt(val, 10, 32)
|
||||
return int32(vv)
|
||||
}
|
||||
|
||||
// Parse to uint64 without error
|
||||
func mustParseUint64(val string) uint64 {
|
||||
vv, _ := strconv.ParseInt(val, 10, 64)
|
||||
return uint64(vv)
|
||||
}
|
||||
|
||||
// Parse to Float64 without error
|
||||
func mustParseFloat64(val string) float64 {
|
||||
vv, _ := strconv.ParseFloat(val, 64)
|
||||
return vv
|
||||
}
|
||||
|
||||
// StringsHas checks the target string slice contains src or not
|
||||
func StringsHas(target []string, src string) bool {
|
||||
for _, t := range target {
|
||||
if strings.TrimSpace(t) == src {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StringsContains checks the src in any string of the target string slice
|
||||
func StringsContains(target []string, src string) bool {
|
||||
for _, t := range target {
|
||||
if strings.Contains(t, src) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IntContains checks the src in any int of the target int slice.
|
||||
func IntContains(target []int, src int) bool {
|
||||
for _, t := range target {
|
||||
if src == t {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// get struct attributes.
|
||||
// This method is used only for debugging platform dependent code.
|
||||
func attributes(m interface{}) map[string]reflect.Type {
|
||||
typ := reflect.TypeOf(m)
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
|
||||
attrs := make(map[string]reflect.Type)
|
||||
if typ.Kind() != reflect.Struct {
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
p := typ.Field(i)
|
||||
if !p.Anonymous {
|
||||
attrs[p.Name] = p.Type
|
||||
}
|
||||
}
|
||||
|
||||
return attrs
|
||||
}
|
||||
|
||||
func PathExists(filename string) bool {
|
||||
if _, err := os.Stat(filename); err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PathExistsWithContents returns the filename exists and it is not empty
|
||||
func PathExistsWithContents(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return info.Size() > 4 // at least 4 bytes
|
||||
}
|
||||
|
||||
// GetEnvWithContext retrieves the environment variable key. If it does not exist it returns the default.
|
||||
// The context may optionally contain a map superseding os.EnvKey.
|
||||
func GetEnvWithContext(ctx context.Context, key string, dfault string, combineWith ...string) string {
|
||||
var value string
|
||||
if env, ok := ctx.Value(common.EnvKey).(common.EnvMap); ok {
|
||||
value = env[common.EnvKeyType(key)]
|
||||
}
|
||||
if value == "" {
|
||||
value = os.Getenv(key)
|
||||
}
|
||||
if value == "" {
|
||||
value = dfault
|
||||
}
|
||||
|
||||
return combine(value, combineWith)
|
||||
}
|
||||
|
||||
// GetEnv retrieves the environment variable key. If it does not exist it returns the default.
|
||||
func GetEnv(key string, dfault string, combineWith ...string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
value = dfault
|
||||
}
|
||||
|
||||
return combine(value, combineWith)
|
||||
}
|
||||
|
||||
func combine(value string, combineWith []string) string {
|
||||
switch len(combineWith) {
|
||||
case 0:
|
||||
return value
|
||||
case 1:
|
||||
return filepath.Join(value, combineWith[0])
|
||||
default:
|
||||
all := make([]string, len(combineWith)+1)
|
||||
all[0] = value
|
||||
copy(all[1:], combineWith)
|
||||
return filepath.Join(all...)
|
||||
}
|
||||
}
|
||||
|
||||
func HostProc(combineWith ...string) string {
|
||||
return GetEnv("HOST_PROC", "/proc", combineWith...)
|
||||
}
|
||||
|
||||
func HostSys(combineWith ...string) string {
|
||||
return GetEnv("HOST_SYS", "/sys", combineWith...)
|
||||
}
|
||||
|
||||
func HostEtc(combineWith ...string) string {
|
||||
return GetEnv("HOST_ETC", "/etc", combineWith...)
|
||||
}
|
||||
|
||||
func HostVar(combineWith ...string) string {
|
||||
return GetEnv("HOST_VAR", "/var", combineWith...)
|
||||
}
|
||||
|
||||
func HostRun(combineWith ...string) string {
|
||||
return GetEnv("HOST_RUN", "/run", combineWith...)
|
||||
}
|
||||
|
||||
func HostDev(combineWith ...string) string {
|
||||
return GetEnv("HOST_DEV", "/dev", combineWith...)
|
||||
}
|
||||
|
||||
func HostRoot(combineWith ...string) string {
|
||||
return GetEnv("HOST_ROOT", "/", combineWith...)
|
||||
}
|
||||
|
||||
func HostProcWithContext(ctx context.Context, combineWith ...string) string {
|
||||
return GetEnvWithContext(ctx, "HOST_PROC", "/proc", combineWith...)
|
||||
}
|
||||
|
||||
func HostProcMountInfoWithContext(ctx context.Context, combineWith ...string) string {
|
||||
return GetEnvWithContext(ctx, "HOST_PROC_MOUNTINFO", "", combineWith...)
|
||||
}
|
||||
|
||||
func HostSysWithContext(ctx context.Context, combineWith ...string) string {
|
||||
return GetEnvWithContext(ctx, "HOST_SYS", "/sys", combineWith...)
|
||||
}
|
||||
|
||||
func HostEtcWithContext(ctx context.Context, combineWith ...string) string {
|
||||
return GetEnvWithContext(ctx, "HOST_ETC", "/etc", combineWith...)
|
||||
}
|
||||
|
||||
func HostVarWithContext(ctx context.Context, combineWith ...string) string {
|
||||
return GetEnvWithContext(ctx, "HOST_VAR", "/var", combineWith...)
|
||||
}
|
||||
|
||||
func HostRunWithContext(ctx context.Context, combineWith ...string) string {
|
||||
return GetEnvWithContext(ctx, "HOST_RUN", "/run", combineWith...)
|
||||
}
|
||||
|
||||
func HostDevWithContext(ctx context.Context, combineWith ...string) string {
|
||||
return GetEnvWithContext(ctx, "HOST_DEV", "/dev", combineWith...)
|
||||
}
|
||||
|
||||
func HostRootWithContext(ctx context.Context, combineWith ...string) string {
|
||||
return GetEnvWithContext(ctx, "HOST_ROOT", "/", combineWith...)
|
||||
}
|
||||
|
||||
// getSysctrlEnv sets LC_ALL=C in a list of env vars for use when running
|
||||
// sysctl commands (see DoSysctrl).
|
||||
func getSysctrlEnv(env []string) []string {
|
||||
foundLC := false
|
||||
for i, line := range env {
|
||||
if strings.HasPrefix(line, "LC_ALL") {
|
||||
env[i] = "LC_ALL=C"
|
||||
foundLC = true
|
||||
}
|
||||
}
|
||||
if !foundLC {
|
||||
env = append(env, "LC_ALL=C")
|
||||
}
|
||||
return env
|
||||
}
|
||||
66
vendor/github.com/shirou/gopsutil/v3/internal/common/common_darwin.go
generated
vendored
Normal file
66
vendor/github.com/shirou/gopsutil/v3/internal/common/common_darwin.go
generated
vendored
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func DoSysctrlWithContext(ctx context.Context, mib string) ([]string, error) {
|
||||
cmd := exec.CommandContext(ctx, "sysctl", "-n", mib)
|
||||
cmd.Env = getSysctrlEnv(os.Environ())
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
v := strings.Replace(string(out), "{ ", "", 1)
|
||||
v = strings.Replace(string(v), " }", "", 1)
|
||||
values := strings.Fields(string(v))
|
||||
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func CallSyscall(mib []int32) ([]byte, uint64, error) {
|
||||
miblen := uint64(len(mib))
|
||||
|
||||
// get required buffer size
|
||||
length := uint64(0)
|
||||
_, _, err := unix.Syscall6(
|
||||
202, // unix.SYS___SYSCTL https://github.com/golang/sys/blob/76b94024e4b621e672466e8db3d7f084e7ddcad2/unix/zsysnum_darwin_amd64.go#L146
|
||||
uintptr(unsafe.Pointer(&mib[0])),
|
||||
uintptr(miblen),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&length)),
|
||||
0,
|
||||
0)
|
||||
if err != 0 {
|
||||
var b []byte
|
||||
return b, length, err
|
||||
}
|
||||
if length == 0 {
|
||||
var b []byte
|
||||
return b, length, err
|
||||
}
|
||||
// get proc info itself
|
||||
buf := make([]byte, length)
|
||||
_, _, err = unix.Syscall6(
|
||||
202, // unix.SYS___SYSCTL https://github.com/golang/sys/blob/76b94024e4b621e672466e8db3d7f084e7ddcad2/unix/zsysnum_darwin_amd64.go#L146
|
||||
uintptr(unsafe.Pointer(&mib[0])),
|
||||
uintptr(miblen),
|
||||
uintptr(unsafe.Pointer(&buf[0])),
|
||||
uintptr(unsafe.Pointer(&length)),
|
||||
0,
|
||||
0)
|
||||
if err != 0 {
|
||||
return buf, length, err
|
||||
}
|
||||
|
||||
return buf, length, nil
|
||||
}
|
||||
82
vendor/github.com/shirou/gopsutil/v3/internal/common/common_freebsd.go
generated
vendored
Normal file
82
vendor/github.com/shirou/gopsutil/v3/internal/common/common_freebsd.go
generated
vendored
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
//go:build freebsd || openbsd
|
||||
// +build freebsd openbsd
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func SysctlUint(mib string) (uint64, error) {
|
||||
buf, err := unix.SysctlRaw(mib)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if len(buf) == 8 { // 64 bit
|
||||
return *(*uint64)(unsafe.Pointer(&buf[0])), nil
|
||||
}
|
||||
if len(buf) == 4 { // 32bit
|
||||
t := *(*uint32)(unsafe.Pointer(&buf[0]))
|
||||
return uint64(t), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unexpected size: %s, %d", mib, len(buf))
|
||||
}
|
||||
|
||||
func DoSysctrl(mib string) ([]string, error) {
|
||||
cmd := exec.Command("sysctl", "-n", mib)
|
||||
cmd.Env = getSysctrlEnv(os.Environ())
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
v := strings.Replace(string(out), "{ ", "", 1)
|
||||
v = strings.Replace(string(v), " }", "", 1)
|
||||
values := strings.Fields(string(v))
|
||||
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func CallSyscall(mib []int32) ([]byte, uint64, error) {
|
||||
mibptr := unsafe.Pointer(&mib[0])
|
||||
miblen := uint64(len(mib))
|
||||
|
||||
// get required buffer size
|
||||
length := uint64(0)
|
||||
_, _, err := unix.Syscall6(
|
||||
unix.SYS___SYSCTL,
|
||||
uintptr(mibptr),
|
||||
uintptr(miblen),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&length)),
|
||||
0,
|
||||
0)
|
||||
if err != 0 {
|
||||
var b []byte
|
||||
return b, length, err
|
||||
}
|
||||
if length == 0 {
|
||||
var b []byte
|
||||
return b, length, err
|
||||
}
|
||||
// get proc info itself
|
||||
buf := make([]byte, length)
|
||||
_, _, err = unix.Syscall6(
|
||||
unix.SYS___SYSCTL,
|
||||
uintptr(mibptr),
|
||||
uintptr(miblen),
|
||||
uintptr(unsafe.Pointer(&buf[0])),
|
||||
uintptr(unsafe.Pointer(&length)),
|
||||
0,
|
||||
0)
|
||||
if err != 0 {
|
||||
return buf, length, err
|
||||
}
|
||||
|
||||
return buf, length, nil
|
||||
}
|
||||
331
vendor/github.com/shirou/gopsutil/v3/internal/common/common_linux.go
generated
vendored
Normal file
331
vendor/github.com/shirou/gopsutil/v3/internal/common/common_linux.go
generated
vendored
Normal file
|
|
@ -0,0 +1,331 @@
|
|||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func DoSysctrl(mib string) ([]string, error) {
|
||||
cmd := exec.Command("sysctl", "-n", mib)
|
||||
cmd.Env = getSysctrlEnv(os.Environ())
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
v := strings.Replace(string(out), "{ ", "", 1)
|
||||
v = strings.Replace(string(v), " }", "", 1)
|
||||
values := strings.Fields(string(v))
|
||||
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func NumProcs() (uint64, error) {
|
||||
return NumProcsWithContext(context.Background())
|
||||
}
|
||||
|
||||
func NumProcsWithContext(ctx context.Context) (uint64, error) {
|
||||
f, err := os.Open(HostProcWithContext(ctx))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
list, err := f.Readdirnames(-1)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var cnt uint64
|
||||
|
||||
for _, v := range list {
|
||||
if _, err = strconv.ParseUint(v, 10, 64); err == nil {
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
|
||||
return cnt, nil
|
||||
}
|
||||
|
||||
func BootTimeWithContext(ctx context.Context) (uint64, error) {
|
||||
system, role, err := VirtualizationWithContext(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
useStatFile := true
|
||||
if system == "lxc" && role == "guest" {
|
||||
// if lxc, /proc/uptime is used.
|
||||
useStatFile = false
|
||||
} else if system == "docker" && role == "guest" {
|
||||
// also docker, guest
|
||||
useStatFile = false
|
||||
}
|
||||
|
||||
if useStatFile {
|
||||
return readBootTimeStat(ctx)
|
||||
}
|
||||
|
||||
filename := HostProcWithContext(ctx, "uptime")
|
||||
lines, err := ReadLines(filename)
|
||||
if err != nil {
|
||||
return handleBootTimeFileReadErr(err)
|
||||
}
|
||||
if len(lines) != 1 {
|
||||
return 0, fmt.Errorf("wrong uptime format")
|
||||
}
|
||||
f := strings.Fields(lines[0])
|
||||
b, err := strconv.ParseFloat(f[0], 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
currentTime := float64(time.Now().UnixNano()) / float64(time.Second)
|
||||
t := currentTime - b
|
||||
return uint64(t), nil
|
||||
}
|
||||
|
||||
func handleBootTimeFileReadErr(err error) (uint64, error) {
|
||||
if os.IsPermission(err) {
|
||||
var info syscall.Sysinfo_t
|
||||
err := syscall.Sysinfo(&info)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
currentTime := time.Now().UnixNano() / int64(time.Second)
|
||||
t := currentTime - int64(info.Uptime)
|
||||
return uint64(t), nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
|
||||
func readBootTimeStat(ctx context.Context) (uint64, error) {
|
||||
filename := HostProcWithContext(ctx, "stat")
|
||||
line, err := ReadLine(filename, "btime")
|
||||
if err != nil {
|
||||
return handleBootTimeFileReadErr(err)
|
||||
}
|
||||
if strings.HasPrefix(line, "btime") {
|
||||
f := strings.Fields(line)
|
||||
if len(f) != 2 {
|
||||
return 0, fmt.Errorf("wrong btime format")
|
||||
}
|
||||
b, err := strconv.ParseInt(f[1], 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
t := uint64(b)
|
||||
return t, nil
|
||||
}
|
||||
return 0, fmt.Errorf("could not find btime")
|
||||
}
|
||||
|
||||
func Virtualization() (string, string, error) {
|
||||
return VirtualizationWithContext(context.Background())
|
||||
}
|
||||
|
||||
// required variables for concurrency safe virtualization caching
|
||||
var (
|
||||
cachedVirtMap map[string]string
|
||||
cachedVirtMutex sync.RWMutex
|
||||
cachedVirtOnce sync.Once
|
||||
)
|
||||
|
||||
func VirtualizationWithContext(ctx context.Context) (string, string, error) {
|
||||
var system, role string
|
||||
|
||||
// if cached already, return from cache
|
||||
cachedVirtMutex.RLock() // unlock won't be deferred so concurrent reads don't wait for long
|
||||
if cachedVirtMap != nil {
|
||||
cachedSystem, cachedRole := cachedVirtMap["system"], cachedVirtMap["role"]
|
||||
cachedVirtMutex.RUnlock()
|
||||
return cachedSystem, cachedRole, nil
|
||||
}
|
||||
cachedVirtMutex.RUnlock()
|
||||
|
||||
filename := HostProcWithContext(ctx, "xen")
|
||||
if PathExists(filename) {
|
||||
system = "xen"
|
||||
role = "guest" // assume guest
|
||||
|
||||
if PathExists(filepath.Join(filename, "capabilities")) {
|
||||
contents, err := ReadLines(filepath.Join(filename, "capabilities"))
|
||||
if err == nil {
|
||||
if StringsContains(contents, "control_d") {
|
||||
role = "host"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
filename = HostProcWithContext(ctx, "modules")
|
||||
if PathExists(filename) {
|
||||
contents, err := ReadLines(filename)
|
||||
if err == nil {
|
||||
if StringsContains(contents, "kvm") {
|
||||
system = "kvm"
|
||||
role = "host"
|
||||
} else if StringsContains(contents, "hv_util") {
|
||||
system = "hyperv"
|
||||
role = "guest"
|
||||
} else if StringsContains(contents, "vboxdrv") {
|
||||
system = "vbox"
|
||||
role = "host"
|
||||
} else if StringsContains(contents, "vboxguest") {
|
||||
system = "vbox"
|
||||
role = "guest"
|
||||
} else if StringsContains(contents, "vmware") {
|
||||
system = "vmware"
|
||||
role = "guest"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
filename = HostProcWithContext(ctx, "cpuinfo")
|
||||
if PathExists(filename) {
|
||||
contents, err := ReadLines(filename)
|
||||
if err == nil {
|
||||
if StringsContains(contents, "QEMU Virtual CPU") ||
|
||||
StringsContains(contents, "Common KVM processor") ||
|
||||
StringsContains(contents, "Common 32-bit KVM processor") {
|
||||
system = "kvm"
|
||||
role = "guest"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
filename = HostProcWithContext(ctx, "bus/pci/devices")
|
||||
if PathExists(filename) {
|
||||
contents, err := ReadLines(filename)
|
||||
if err == nil {
|
||||
if StringsContains(contents, "virtio-pci") {
|
||||
role = "guest"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
filename = HostProcWithContext(ctx)
|
||||
if PathExists(filepath.Join(filename, "bc", "0")) {
|
||||
system = "openvz"
|
||||
role = "host"
|
||||
} else if PathExists(filepath.Join(filename, "vz")) {
|
||||
system = "openvz"
|
||||
role = "guest"
|
||||
}
|
||||
|
||||
// not use dmidecode because it requires root
|
||||
if PathExists(filepath.Join(filename, "self", "status")) {
|
||||
contents, err := ReadLines(filepath.Join(filename, "self", "status"))
|
||||
if err == nil {
|
||||
if StringsContains(contents, "s_context:") ||
|
||||
StringsContains(contents, "VxID:") {
|
||||
system = "linux-vserver"
|
||||
}
|
||||
// TODO: guest or host
|
||||
}
|
||||
}
|
||||
|
||||
if PathExists(filepath.Join(filename, "1", "environ")) {
|
||||
contents, err := ReadFile(filepath.Join(filename, "1", "environ"))
|
||||
|
||||
if err == nil {
|
||||
if strings.Contains(contents, "container=lxc") {
|
||||
system = "lxc"
|
||||
role = "guest"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if PathExists(filepath.Join(filename, "self", "cgroup")) {
|
||||
contents, err := ReadLines(filepath.Join(filename, "self", "cgroup"))
|
||||
if err == nil {
|
||||
if StringsContains(contents, "lxc") {
|
||||
system = "lxc"
|
||||
role = "guest"
|
||||
} else if StringsContains(contents, "docker") {
|
||||
system = "docker"
|
||||
role = "guest"
|
||||
} else if StringsContains(contents, "machine-rkt") {
|
||||
system = "rkt"
|
||||
role = "guest"
|
||||
} else if PathExists("/usr/bin/lxc-version") {
|
||||
system = "lxc"
|
||||
role = "host"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if PathExists(HostEtcWithContext(ctx, "os-release")) {
|
||||
p, _, err := GetOSReleaseWithContext(ctx)
|
||||
if err == nil && p == "coreos" {
|
||||
system = "rkt" // Is it true?
|
||||
role = "host"
|
||||
}
|
||||
}
|
||||
|
||||
if PathExists(HostRootWithContext(ctx, ".dockerenv")) {
|
||||
system = "docker"
|
||||
role = "guest"
|
||||
}
|
||||
|
||||
// before returning for the first time, cache the system and role
|
||||
cachedVirtOnce.Do(func() {
|
||||
cachedVirtMutex.Lock()
|
||||
defer cachedVirtMutex.Unlock()
|
||||
cachedVirtMap = map[string]string{
|
||||
"system": system,
|
||||
"role": role,
|
||||
}
|
||||
})
|
||||
|
||||
return system, role, nil
|
||||
}
|
||||
|
||||
func GetOSRelease() (platform string, version string, err error) {
|
||||
return GetOSReleaseWithContext(context.Background())
|
||||
}
|
||||
|
||||
func GetOSReleaseWithContext(ctx context.Context) (platform string, version string, err error) {
|
||||
contents, err := ReadLines(HostEtcWithContext(ctx, "os-release"))
|
||||
if err != nil {
|
||||
return "", "", nil // return empty
|
||||
}
|
||||
for _, line := range contents {
|
||||
field := strings.Split(line, "=")
|
||||
if len(field) < 2 {
|
||||
continue
|
||||
}
|
||||
switch field[0] {
|
||||
case "ID": // use ID for lowercase
|
||||
platform = trimQuotes(field[1])
|
||||
case "VERSION":
|
||||
version = trimQuotes(field[1])
|
||||
}
|
||||
}
|
||||
|
||||
// cleanup amazon ID
|
||||
if platform == "amzn" {
|
||||
platform = "amazon"
|
||||
}
|
||||
|
||||
return platform, version, nil
|
||||
}
|
||||
|
||||
// Remove quotes of the source string
|
||||
func trimQuotes(s string) string {
|
||||
if len(s) >= 2 {
|
||||
if s[0] == '"' && s[len(s)-1] == '"' {
|
||||
return s[1 : len(s)-1]
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
66
vendor/github.com/shirou/gopsutil/v3/internal/common/common_netbsd.go
generated
vendored
Normal file
66
vendor/github.com/shirou/gopsutil/v3/internal/common/common_netbsd.go
generated
vendored
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
//go:build netbsd
|
||||
// +build netbsd
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func DoSysctrl(mib string) ([]string, error) {
|
||||
cmd := exec.Command("sysctl", "-n", mib)
|
||||
cmd.Env = getSysctrlEnv(os.Environ())
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
v := strings.Replace(string(out), "{ ", "", 1)
|
||||
v = strings.Replace(string(v), " }", "", 1)
|
||||
values := strings.Fields(string(v))
|
||||
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func CallSyscall(mib []int32) ([]byte, uint64, error) {
|
||||
mibptr := unsafe.Pointer(&mib[0])
|
||||
miblen := uint64(len(mib))
|
||||
|
||||
// get required buffer size
|
||||
length := uint64(0)
|
||||
_, _, err := unix.Syscall6(
|
||||
unix.SYS___SYSCTL,
|
||||
uintptr(mibptr),
|
||||
uintptr(miblen),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&length)),
|
||||
0,
|
||||
0)
|
||||
if err != 0 {
|
||||
var b []byte
|
||||
return b, length, err
|
||||
}
|
||||
if length == 0 {
|
||||
var b []byte
|
||||
return b, length, err
|
||||
}
|
||||
// get proc info itself
|
||||
buf := make([]byte, length)
|
||||
_, _, err = unix.Syscall6(
|
||||
unix.SYS___SYSCTL,
|
||||
uintptr(mibptr),
|
||||
uintptr(miblen),
|
||||
uintptr(unsafe.Pointer(&buf[0])),
|
||||
uintptr(unsafe.Pointer(&length)),
|
||||
0,
|
||||
0)
|
||||
if err != 0 {
|
||||
return buf, length, err
|
||||
}
|
||||
|
||||
return buf, length, nil
|
||||
}
|
||||
66
vendor/github.com/shirou/gopsutil/v3/internal/common/common_openbsd.go
generated
vendored
Normal file
66
vendor/github.com/shirou/gopsutil/v3/internal/common/common_openbsd.go
generated
vendored
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
//go:build openbsd
|
||||
// +build openbsd
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func DoSysctrl(mib string) ([]string, error) {
|
||||
cmd := exec.Command("sysctl", "-n", mib)
|
||||
cmd.Env = getSysctrlEnv(os.Environ())
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
v := strings.Replace(string(out), "{ ", "", 1)
|
||||
v = strings.Replace(string(v), " }", "", 1)
|
||||
values := strings.Fields(string(v))
|
||||
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func CallSyscall(mib []int32) ([]byte, uint64, error) {
|
||||
mibptr := unsafe.Pointer(&mib[0])
|
||||
miblen := uint64(len(mib))
|
||||
|
||||
// get required buffer size
|
||||
length := uint64(0)
|
||||
_, _, err := unix.Syscall6(
|
||||
unix.SYS___SYSCTL,
|
||||
uintptr(mibptr),
|
||||
uintptr(miblen),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&length)),
|
||||
0,
|
||||
0)
|
||||
if err != 0 {
|
||||
var b []byte
|
||||
return b, length, err
|
||||
}
|
||||
if length == 0 {
|
||||
var b []byte
|
||||
return b, length, err
|
||||
}
|
||||
// get proc info itself
|
||||
buf := make([]byte, length)
|
||||
_, _, err = unix.Syscall6(
|
||||
unix.SYS___SYSCTL,
|
||||
uintptr(mibptr),
|
||||
uintptr(miblen),
|
||||
uintptr(unsafe.Pointer(&buf[0])),
|
||||
uintptr(unsafe.Pointer(&length)),
|
||||
0,
|
||||
0)
|
||||
if err != 0 {
|
||||
return buf, length, err
|
||||
}
|
||||
|
||||
return buf, length, nil
|
||||
}
|
||||
62
vendor/github.com/shirou/gopsutil/v3/internal/common/common_unix.go
generated
vendored
Normal file
62
vendor/github.com/shirou/gopsutil/v3/internal/common/common_unix.go
generated
vendored
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
//go:build linux || freebsd || darwin || openbsd
|
||||
// +build linux freebsd darwin openbsd
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CallLsofWithContext(ctx context.Context, invoke Invoker, pid int32, args ...string) ([]string, error) {
|
||||
var cmd []string
|
||||
if pid == 0 { // will get from all processes.
|
||||
cmd = []string{"-a", "-n", "-P"}
|
||||
} else {
|
||||
cmd = []string{"-a", "-n", "-P", "-p", strconv.Itoa(int(pid))}
|
||||
}
|
||||
cmd = append(cmd, args...)
|
||||
out, err := invoke.CommandWithContext(ctx, "lsof", cmd...)
|
||||
if err != nil {
|
||||
if errors.Is(err, exec.ErrNotFound) {
|
||||
return []string{}, err
|
||||
}
|
||||
// if no pid found, lsof returns code 1.
|
||||
if err.Error() == "exit status 1" && len(out) == 0 {
|
||||
return []string{}, nil
|
||||
}
|
||||
}
|
||||
lines := strings.Split(string(out), "\n")
|
||||
|
||||
var ret []string
|
||||
for _, l := range lines[1:] {
|
||||
if len(l) == 0 {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, l)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func CallPgrepWithContext(ctx context.Context, invoke Invoker, pid int32) ([]int32, error) {
|
||||
out, err := invoke.CommandWithContext(ctx, "pgrep", "-P", strconv.Itoa(int(pid)))
|
||||
if err != nil {
|
||||
return []int32{}, err
|
||||
}
|
||||
lines := strings.Split(string(out), "\n")
|
||||
ret := make([]int32, 0, len(lines))
|
||||
for _, l := range lines {
|
||||
if len(l) == 0 {
|
||||
continue
|
||||
}
|
||||
i, err := strconv.ParseInt(l, 10, 32)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, int32(i))
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
304
vendor/github.com/shirou/gopsutil/v3/internal/common/common_windows.go
generated
vendored
Normal file
304
vendor/github.com/shirou/gopsutil/v3/internal/common/common_windows.go
generated
vendored
Normal file
|
|
@ -0,0 +1,304 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/yusufpapurcu/wmi"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// for double values
|
||||
type PDH_FMT_COUNTERVALUE_DOUBLE struct {
|
||||
CStatus uint32
|
||||
DoubleValue float64
|
||||
}
|
||||
|
||||
// for 64 bit integer values
|
||||
type PDH_FMT_COUNTERVALUE_LARGE struct {
|
||||
CStatus uint32
|
||||
LargeValue int64
|
||||
}
|
||||
|
||||
// for long values
|
||||
type PDH_FMT_COUNTERVALUE_LONG struct {
|
||||
CStatus uint32
|
||||
LongValue int32
|
||||
padding [4]byte
|
||||
}
|
||||
|
||||
// windows system const
|
||||
const (
|
||||
ERROR_SUCCESS = 0
|
||||
ERROR_FILE_NOT_FOUND = 2
|
||||
DRIVE_REMOVABLE = 2
|
||||
DRIVE_FIXED = 3
|
||||
HKEY_LOCAL_MACHINE = 0x80000002
|
||||
RRF_RT_REG_SZ = 0x00000002
|
||||
RRF_RT_REG_DWORD = 0x00000010
|
||||
PDH_FMT_LONG = 0x00000100
|
||||
PDH_FMT_DOUBLE = 0x00000200
|
||||
PDH_FMT_LARGE = 0x00000400
|
||||
PDH_INVALID_DATA = 0xc0000bc6
|
||||
PDH_INVALID_HANDLE = 0xC0000bbc
|
||||
PDH_NO_DATA = 0x800007d5
|
||||
|
||||
STATUS_BUFFER_OVERFLOW = 0x80000005
|
||||
STATUS_BUFFER_TOO_SMALL = 0xC0000023
|
||||
STATUS_INFO_LENGTH_MISMATCH = 0xC0000004
|
||||
)
|
||||
|
||||
const (
|
||||
ProcessBasicInformation = 0
|
||||
ProcessWow64Information = 26
|
||||
ProcessQueryInformation = windows.PROCESS_DUP_HANDLE | windows.PROCESS_QUERY_INFORMATION
|
||||
|
||||
SystemExtendedHandleInformationClass = 64
|
||||
)
|
||||
|
||||
var (
|
||||
Modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
||||
ModNt = windows.NewLazySystemDLL("ntdll.dll")
|
||||
ModPdh = windows.NewLazySystemDLL("pdh.dll")
|
||||
ModPsapi = windows.NewLazySystemDLL("psapi.dll")
|
||||
|
||||
ProcGetSystemTimes = Modkernel32.NewProc("GetSystemTimes")
|
||||
ProcNtQuerySystemInformation = ModNt.NewProc("NtQuerySystemInformation")
|
||||
ProcRtlGetNativeSystemInformation = ModNt.NewProc("RtlGetNativeSystemInformation")
|
||||
ProcRtlNtStatusToDosError = ModNt.NewProc("RtlNtStatusToDosError")
|
||||
ProcNtQueryInformationProcess = ModNt.NewProc("NtQueryInformationProcess")
|
||||
ProcNtReadVirtualMemory = ModNt.NewProc("NtReadVirtualMemory")
|
||||
ProcNtWow64QueryInformationProcess64 = ModNt.NewProc("NtWow64QueryInformationProcess64")
|
||||
ProcNtWow64ReadVirtualMemory64 = ModNt.NewProc("NtWow64ReadVirtualMemory64")
|
||||
|
||||
PdhOpenQuery = ModPdh.NewProc("PdhOpenQuery")
|
||||
PdhAddEnglishCounterW = ModPdh.NewProc("PdhAddEnglishCounterW")
|
||||
PdhCollectQueryData = ModPdh.NewProc("PdhCollectQueryData")
|
||||
PdhGetFormattedCounterValue = ModPdh.NewProc("PdhGetFormattedCounterValue")
|
||||
PdhCloseQuery = ModPdh.NewProc("PdhCloseQuery")
|
||||
|
||||
procQueryDosDeviceW = Modkernel32.NewProc("QueryDosDeviceW")
|
||||
)
|
||||
|
||||
type FILETIME struct {
|
||||
DwLowDateTime uint32
|
||||
DwHighDateTime uint32
|
||||
}
|
||||
|
||||
// borrowed from net/interface_windows.go
|
||||
func BytePtrToString(p *uint8) string {
|
||||
a := (*[10000]uint8)(unsafe.Pointer(p))
|
||||
i := 0
|
||||
for a[i] != 0 {
|
||||
i++
|
||||
}
|
||||
return string(a[:i])
|
||||
}
|
||||
|
||||
// CounterInfo struct is used to track a windows performance counter
|
||||
// copied from https://github.com/mackerelio/mackerel-agent/
|
||||
type CounterInfo struct {
|
||||
PostName string
|
||||
CounterName string
|
||||
Counter windows.Handle
|
||||
}
|
||||
|
||||
// CreateQuery with a PdhOpenQuery call
|
||||
// copied from https://github.com/mackerelio/mackerel-agent/
|
||||
func CreateQuery() (windows.Handle, error) {
|
||||
var query windows.Handle
|
||||
r, _, err := PdhOpenQuery.Call(0, 0, uintptr(unsafe.Pointer(&query)))
|
||||
if r != 0 {
|
||||
return 0, err
|
||||
}
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// CreateCounter with a PdhAddEnglishCounterW call
|
||||
func CreateCounter(query windows.Handle, pname, cname string) (*CounterInfo, error) {
|
||||
var counter windows.Handle
|
||||
r, _, err := PdhAddEnglishCounterW.Call(
|
||||
uintptr(query),
|
||||
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(cname))),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&counter)))
|
||||
if r != 0 {
|
||||
return nil, err
|
||||
}
|
||||
return &CounterInfo{
|
||||
PostName: pname,
|
||||
CounterName: cname,
|
||||
Counter: counter,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetCounterValue get counter value from handle
|
||||
// adapted from https://github.com/mackerelio/mackerel-agent/
|
||||
func GetCounterValue(counter windows.Handle) (float64, error) {
|
||||
var value PDH_FMT_COUNTERVALUE_DOUBLE
|
||||
r, _, err := PdhGetFormattedCounterValue.Call(uintptr(counter), PDH_FMT_DOUBLE, uintptr(0), uintptr(unsafe.Pointer(&value)))
|
||||
if r != 0 && r != PDH_INVALID_DATA {
|
||||
return 0.0, err
|
||||
}
|
||||
return value.DoubleValue, nil
|
||||
}
|
||||
|
||||
type Win32PerformanceCounter struct {
|
||||
PostName string
|
||||
CounterName string
|
||||
Query windows.Handle
|
||||
Counter windows.Handle
|
||||
}
|
||||
|
||||
func NewWin32PerformanceCounter(postName, counterName string) (*Win32PerformanceCounter, error) {
|
||||
query, err := CreateQuery()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counter := Win32PerformanceCounter{
|
||||
Query: query,
|
||||
PostName: postName,
|
||||
CounterName: counterName,
|
||||
}
|
||||
r, _, err := PdhAddEnglishCounterW.Call(
|
||||
uintptr(counter.Query),
|
||||
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(counter.CounterName))),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&counter.Counter)),
|
||||
)
|
||||
if r != 0 {
|
||||
return nil, err
|
||||
}
|
||||
return &counter, nil
|
||||
}
|
||||
|
||||
func (w *Win32PerformanceCounter) GetValue() (float64, error) {
|
||||
r, _, err := PdhCollectQueryData.Call(uintptr(w.Query))
|
||||
if r != 0 && err != nil {
|
||||
if r == PDH_NO_DATA {
|
||||
return 0.0, fmt.Errorf("%w: this counter has not data", err)
|
||||
}
|
||||
return 0.0, err
|
||||
}
|
||||
|
||||
return GetCounterValue(w.Counter)
|
||||
}
|
||||
|
||||
func ProcessorQueueLengthCounter() (*Win32PerformanceCounter, error) {
|
||||
return NewWin32PerformanceCounter("processor_queue_length", `\System\Processor Queue Length`)
|
||||
}
|
||||
|
||||
// WMIQueryWithContext - wraps wmi.Query with a timed-out context to avoid hanging
|
||||
func WMIQueryWithContext(ctx context.Context, query string, dst interface{}, connectServerArgs ...interface{}) error {
|
||||
if _, ok := ctx.Deadline(); !ok {
|
||||
ctxTimeout, cancel := context.WithTimeout(ctx, Timeout)
|
||||
defer cancel()
|
||||
ctx = ctxTimeout
|
||||
}
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
go func() {
|
||||
errChan <- wmi.Query(query, dst, connectServerArgs...)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case err := <-errChan:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Convert paths using native DOS format like:
|
||||
//
|
||||
// "\Device\HarddiskVolume1\Windows\systemew\file.txt"
|
||||
//
|
||||
// into:
|
||||
//
|
||||
// "C:\Windows\systemew\file.txt"
|
||||
func ConvertDOSPath(p string) string {
|
||||
rawDrive := strings.Join(strings.Split(p, `\`)[:3], `\`)
|
||||
|
||||
for d := 'A'; d <= 'Z'; d++ {
|
||||
szDeviceName := string(d) + ":"
|
||||
szTarget := make([]uint16, 512)
|
||||
ret, _, _ := procQueryDosDeviceW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(szDeviceName))),
|
||||
uintptr(unsafe.Pointer(&szTarget[0])),
|
||||
uintptr(len(szTarget)))
|
||||
if ret != 0 && windows.UTF16ToString(szTarget[:]) == rawDrive {
|
||||
return filepath.Join(szDeviceName, p[len(rawDrive):])
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
type NtStatus uint32
|
||||
|
||||
func (s NtStatus) Error() error {
|
||||
if s == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("NtStatus 0x%08x", uint32(s))
|
||||
}
|
||||
|
||||
func (s NtStatus) IsError() bool {
|
||||
return s>>30 == 3
|
||||
}
|
||||
|
||||
type SystemExtendedHandleTableEntryInformation struct {
|
||||
Object uintptr
|
||||
UniqueProcessId uintptr
|
||||
HandleValue uintptr
|
||||
GrantedAccess uint32
|
||||
CreatorBackTraceIndex uint16
|
||||
ObjectTypeIndex uint16
|
||||
HandleAttributes uint32
|
||||
Reserved uint32
|
||||
}
|
||||
|
||||
type SystemExtendedHandleInformation struct {
|
||||
NumberOfHandles uintptr
|
||||
Reserved uintptr
|
||||
Handles [1]SystemExtendedHandleTableEntryInformation
|
||||
}
|
||||
|
||||
// CallWithExpandingBuffer https://github.com/hillu/go-ntdll
|
||||
func CallWithExpandingBuffer(fn func() NtStatus, buf *[]byte, resultLength *uint32) NtStatus {
|
||||
for {
|
||||
if st := fn(); st == STATUS_BUFFER_OVERFLOW || st == STATUS_BUFFER_TOO_SMALL || st == STATUS_INFO_LENGTH_MISMATCH {
|
||||
if int(*resultLength) <= cap(*buf) {
|
||||
(*reflect.SliceHeader)(unsafe.Pointer(buf)).Len = int(*resultLength)
|
||||
} else {
|
||||
*buf = make([]byte, int(*resultLength))
|
||||
}
|
||||
continue
|
||||
} else {
|
||||
if !st.IsError() {
|
||||
*buf = (*buf)[:int(*resultLength)]
|
||||
}
|
||||
return st
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NtQuerySystemInformation(
|
||||
SystemInformationClass uint32,
|
||||
SystemInformation *byte,
|
||||
SystemInformationLength uint32,
|
||||
ReturnLength *uint32,
|
||||
) NtStatus {
|
||||
r0, _, _ := ProcNtQuerySystemInformation.Call(
|
||||
uintptr(SystemInformationClass),
|
||||
uintptr(unsafe.Pointer(SystemInformation)),
|
||||
uintptr(SystemInformationLength),
|
||||
uintptr(unsafe.Pointer(ReturnLength)))
|
||||
return NtStatus(r0)
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package common
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// IsLittleEndian checks if the current platform uses little-endian.
|
||||
// copied from https://github.com/ntrrg/ntgo/blob/v0.8.0/runtime/infrastructure.go#L16 (MIT License)
|
||||
func IsLittleEndian() bool {
|
||||
var x int16 = 0x0011
|
||||
return *(*byte)(unsafe.Pointer(&x)) == 0x11
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Sleep awaits for provided interval.
|
||||
// Can be interrupted by context cancelation.
|
||||
func Sleep(ctx context.Context, interval time.Duration) error {
|
||||
timer := time.NewTimer(interval)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package common
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Warnings struct {
|
||||
List []error
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
func (w *Warnings) Add(err error) {
|
||||
w.List = append(w.List, err)
|
||||
}
|
||||
|
||||
func (w *Warnings) Reference() error {
|
||||
if len(w.List) > 0 {
|
||||
return w
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Warnings) Error() string {
|
||||
if w.Verbose {
|
||||
str := ""
|
||||
for i, e := range w.List {
|
||||
str += fmt.Sprintf("\tError %d: %s\n", i, e.Error())
|
||||
}
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("Number of warnings: %v", len(w.List))
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
package mem
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
var invoke common.Invoker = common.Invoke{}
|
||||
|
||||
// Memory usage statistics. Total, Available and Used contain numbers of bytes
|
||||
// for human consumption.
|
||||
//
|
||||
// The other fields in this struct contain kernel specific values.
|
||||
type VirtualMemoryStat struct {
|
||||
// Total amount of RAM on this system
|
||||
Total uint64 `json:"total"`
|
||||
|
||||
// RAM available for programs to allocate
|
||||
//
|
||||
// This value is computed from the kernel specific values.
|
||||
Available uint64 `json:"available"`
|
||||
|
||||
// RAM used by programs
|
||||
//
|
||||
// This value is computed from the kernel specific values.
|
||||
Used uint64 `json:"used"`
|
||||
|
||||
// Percentage of RAM used by programs
|
||||
//
|
||||
// This value is computed from the kernel specific values.
|
||||
UsedPercent float64 `json:"usedPercent"`
|
||||
|
||||
// This is the kernel's notion of free memory; RAM chips whose bits nobody
|
||||
// cares about the value of right now. For a human consumable number,
|
||||
// Available is what you really want.
|
||||
Free uint64 `json:"free"`
|
||||
|
||||
// OS X / BSD specific numbers:
|
||||
// http://www.macyourself.com/2010/02/17/what-is-free-wired-active-and-inactive-system-memory-ram/
|
||||
Active uint64 `json:"active"`
|
||||
Inactive uint64 `json:"inactive"`
|
||||
Wired uint64 `json:"wired"`
|
||||
|
||||
// FreeBSD specific numbers:
|
||||
// https://reviews.freebsd.org/D8467
|
||||
Laundry uint64 `json:"laundry"`
|
||||
|
||||
// Linux specific numbers
|
||||
// https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html
|
||||
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
|
||||
// https://www.kernel.org/doc/Documentation/vm/overcommit-accounting
|
||||
// https://www.kernel.org/doc/Documentation/vm/transhuge.txt
|
||||
Buffers uint64 `json:"buffers"`
|
||||
Cached uint64 `json:"cached"`
|
||||
WriteBack uint64 `json:"writeBack"`
|
||||
Dirty uint64 `json:"dirty"`
|
||||
WriteBackTmp uint64 `json:"writeBackTmp"`
|
||||
Shared uint64 `json:"shared"`
|
||||
Slab uint64 `json:"slab"`
|
||||
Sreclaimable uint64 `json:"sreclaimable"`
|
||||
Sunreclaim uint64 `json:"sunreclaim"`
|
||||
PageTables uint64 `json:"pageTables"`
|
||||
SwapCached uint64 `json:"swapCached"`
|
||||
CommitLimit uint64 `json:"commitLimit"`
|
||||
CommittedAS uint64 `json:"committedAS"`
|
||||
HighTotal uint64 `json:"highTotal"`
|
||||
HighFree uint64 `json:"highFree"`
|
||||
LowTotal uint64 `json:"lowTotal"`
|
||||
LowFree uint64 `json:"lowFree"`
|
||||
SwapTotal uint64 `json:"swapTotal"`
|
||||
SwapFree uint64 `json:"swapFree"`
|
||||
Mapped uint64 `json:"mapped"`
|
||||
VmallocTotal uint64 `json:"vmallocTotal"`
|
||||
VmallocUsed uint64 `json:"vmallocUsed"`
|
||||
VmallocChunk uint64 `json:"vmallocChunk"`
|
||||
HugePagesTotal uint64 `json:"hugePagesTotal"`
|
||||
HugePagesFree uint64 `json:"hugePagesFree"`
|
||||
HugePagesRsvd uint64 `json:"hugePagesRsvd"`
|
||||
HugePagesSurp uint64 `json:"hugePagesSurp"`
|
||||
HugePageSize uint64 `json:"hugePageSize"`
|
||||
AnonHugePages uint64 `json:"anonHugePages"`
|
||||
}
|
||||
|
||||
type SwapMemoryStat struct {
|
||||
Total uint64 `json:"total"`
|
||||
Used uint64 `json:"used"`
|
||||
Free uint64 `json:"free"`
|
||||
UsedPercent float64 `json:"usedPercent"`
|
||||
Sin uint64 `json:"sin"`
|
||||
Sout uint64 `json:"sout"`
|
||||
PgIn uint64 `json:"pgIn"`
|
||||
PgOut uint64 `json:"pgOut"`
|
||||
PgFault uint64 `json:"pgFault"`
|
||||
|
||||
// Linux specific numbers
|
||||
// https://www.kernel.org/doc/Documentation/cgroup-v2.txt
|
||||
PgMajFault uint64 `json:"pgMajFault"`
|
||||
}
|
||||
|
||||
func (m VirtualMemoryStat) String() string {
|
||||
s, _ := json.Marshal(m)
|
||||
return string(s)
|
||||
}
|
||||
|
||||
func (m SwapMemoryStat) String() string {
|
||||
s, _ := json.Marshal(m)
|
||||
return string(s)
|
||||
}
|
||||
|
||||
type SwapDevice struct {
|
||||
Name string `json:"name"`
|
||||
UsedBytes uint64 `json:"usedBytes"`
|
||||
FreeBytes uint64 `json:"freeBytes"`
|
||||
}
|
||||
|
||||
func (m SwapDevice) String() string {
|
||||
s, _ := json.Marshal(m)
|
||||
return string(s)
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
//go:build aix
|
||||
// +build aix
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||
return VirtualMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapMemory() (*SwapMemoryStat, error) {
|
||||
return SwapMemoryWithContext(context.Background())
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
//go:build aix && cgo
|
||||
// +build aix,cgo
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/power-devops/perfstat"
|
||||
)
|
||||
|
||||
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 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
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
//go:build aix && !cgo
|
||||
// +build aix,!cgo
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
var whiteSpaces = regexp.MustCompile(`\s+`)
|
||||
|
||||
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
|
||||
}
|
||||
vmem.SwapTotal = swap.Total
|
||||
vmem.SwapFree = swap.Free
|
||||
return vmem, nil
|
||||
}
|
||||
|
||||
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 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
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
//go:build freebsd || openbsd || netbsd
|
||||
// +build freebsd openbsd netbsd
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const swapCommand = "swapctl"
|
||||
|
||||
// swapctl column indexes
|
||||
const (
|
||||
nameCol = 0
|
||||
totalKiBCol = 1
|
||||
usedKiBCol = 2
|
||||
)
|
||||
|
||||
func SwapDevices() ([]*SwapDevice, error) {
|
||||
return SwapDevicesWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
||||
output, err := invoke.CommandWithContext(ctx, swapCommand, "-lk")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err)
|
||||
}
|
||||
|
||||
return parseSwapctlOutput(string(output))
|
||||
}
|
||||
|
||||
func parseSwapctlOutput(output string) ([]*SwapDevice, error) {
|
||||
lines := strings.Split(output, "\n")
|
||||
if len(lines) == 0 {
|
||||
return nil, fmt.Errorf("could not parse output of %q: no lines in %q", swapCommand, output)
|
||||
}
|
||||
|
||||
// Check header headerFields are as expected.
|
||||
header := lines[0]
|
||||
header = strings.ToLower(header)
|
||||
header = strings.ReplaceAll(header, ":", "")
|
||||
headerFields := strings.Fields(header)
|
||||
if len(headerFields) < usedKiBCol {
|
||||
return nil, fmt.Errorf("couldn't parse %q: too few fields in header %q", swapCommand, header)
|
||||
}
|
||||
if headerFields[nameCol] != "device" {
|
||||
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[nameCol], "device")
|
||||
}
|
||||
if headerFields[totalKiBCol] != "1kb-blocks" && headerFields[totalKiBCol] != "1k-blocks" {
|
||||
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[totalKiBCol], "1kb-blocks")
|
||||
}
|
||||
if headerFields[usedKiBCol] != "used" {
|
||||
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[usedKiBCol], "used")
|
||||
}
|
||||
|
||||
var swapDevices []*SwapDevice
|
||||
for _, line := range lines[1:] {
|
||||
if line == "" {
|
||||
continue // the terminal line is typically empty
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < usedKiBCol {
|
||||
return nil, fmt.Errorf("couldn't parse %q: too few fields", swapCommand)
|
||||
}
|
||||
|
||||
totalKiB, err := strconv.ParseUint(fields[totalKiBCol], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't parse 'Size' column in %q: %w", swapCommand, err)
|
||||
}
|
||||
|
||||
usedKiB, err := strconv.ParseUint(fields[usedKiBCol], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't parse 'Used' column in %q: %w", swapCommand, err)
|
||||
}
|
||||
|
||||
swapDevices = append(swapDevices, &SwapDevice{
|
||||
Name: fields[nameCol],
|
||||
UsedBytes: usedKiB * 1024,
|
||||
FreeBytes: (totalKiB - usedKiB) * 1024,
|
||||
})
|
||||
}
|
||||
|
||||
return swapDevices, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
func getHwMemsize() (uint64, error) {
|
||||
total, err := unix.SysctlUint64("hw.memsize")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// xsw_usage in sys/sysctl.h
|
||||
type swapUsage struct {
|
||||
Total uint64
|
||||
Avail uint64
|
||||
Used uint64
|
||||
Pagesize int32
|
||||
Encrypted bool
|
||||
}
|
||||
|
||||
// SwapMemory returns swapinfo.
|
||||
func SwapMemory() (*SwapMemoryStat, error) {
|
||||
return SwapMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
||||
// https://github.com/yanllearnn/go-osstat/blob/ae8a279d26f52ec946a03698c7f50a26cfb427e3/memory/memory_darwin.go
|
||||
var ret *SwapMemoryStat
|
||||
|
||||
value, err := unix.SysctlRaw("vm.swapusage")
|
||||
if err != nil {
|
||||
return ret, err
|
||||
}
|
||||
if len(value) != 32 {
|
||||
return ret, fmt.Errorf("unexpected output of sysctl vm.swapusage: %v (len: %d)", value, len(value))
|
||||
}
|
||||
swap := (*swapUsage)(unsafe.Pointer(&value[0]))
|
||||
|
||||
u := float64(0)
|
||||
if swap.Total != 0 {
|
||||
u = ((float64(swap.Total) - float64(swap.Avail)) / float64(swap.Total)) * 100.0
|
||||
}
|
||||
|
||||
ret = &SwapMemoryStat{
|
||||
Total: swap.Total,
|
||||
Used: swap.Used,
|
||||
Free: swap.Avail,
|
||||
UsedPercent: u,
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func SwapDevices() ([]*SwapDevice, error) {
|
||||
return SwapDevicesWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
||||
return nil, common.ErrNotImplementedError
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
//go:build darwin && cgo
|
||||
// +build darwin,cgo
|
||||
|
||||
package mem
|
||||
|
||||
/*
|
||||
#include <mach/mach_host.h>
|
||||
#include <mach/vm_page_size.h>
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// VirtualMemory returns VirtualmemoryStat.
|
||||
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||
return VirtualMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
||||
count := C.mach_msg_type_number_t(C.HOST_VM_INFO_COUNT)
|
||||
var vmstat C.vm_statistics_data_t
|
||||
|
||||
status := C.host_statistics(C.host_t(C.mach_host_self()),
|
||||
C.HOST_VM_INFO,
|
||||
C.host_info_t(unsafe.Pointer(&vmstat)),
|
||||
&count)
|
||||
|
||||
if status != C.KERN_SUCCESS {
|
||||
return nil, fmt.Errorf("host_statistics error=%d", status)
|
||||
}
|
||||
|
||||
pageSize := uint64(C.vm_kernel_page_size)
|
||||
total, err := getHwMemsize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
totalCount := C.natural_t(total / pageSize)
|
||||
|
||||
availableCount := vmstat.inactive_count + vmstat.free_count
|
||||
usedPercent := 100 * float64(totalCount-availableCount) / float64(totalCount)
|
||||
|
||||
usedCount := totalCount - availableCount
|
||||
|
||||
return &VirtualMemoryStat{
|
||||
Total: total,
|
||||
Available: pageSize * uint64(availableCount),
|
||||
Used: pageSize * uint64(usedCount),
|
||||
UsedPercent: usedPercent,
|
||||
Free: pageSize * uint64(vmstat.free_count),
|
||||
Active: pageSize * uint64(vmstat.active_count),
|
||||
Inactive: pageSize * uint64(vmstat.inactive_count),
|
||||
Wired: pageSize * uint64(vmstat.wire_count),
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
//go:build darwin && !cgo
|
||||
// +build darwin,!cgo
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Runs vm_stat and returns Free and inactive pages
|
||||
func getVMStat(vms *VirtualMemoryStat) error {
|
||||
out, err := invoke.Command("vm_stat")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return parseVMStat(string(out), vms)
|
||||
}
|
||||
|
||||
func parseVMStat(out string, vms *VirtualMemoryStat) error {
|
||||
var err error
|
||||
|
||||
lines := strings.Split(out, "\n")
|
||||
pagesize := uint64(unix.Getpagesize())
|
||||
for _, line := range lines {
|
||||
fields := strings.Split(line, ":")
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(fields[0])
|
||||
value := strings.Trim(fields[1], " .")
|
||||
switch key {
|
||||
case "Pages free":
|
||||
free, e := strconv.ParseUint(value, 10, 64)
|
||||
if e != nil {
|
||||
err = e
|
||||
}
|
||||
vms.Free = free * pagesize
|
||||
case "Pages inactive":
|
||||
inactive, e := strconv.ParseUint(value, 10, 64)
|
||||
if e != nil {
|
||||
err = e
|
||||
}
|
||||
vms.Inactive = inactive * pagesize
|
||||
case "Pages active":
|
||||
active, e := strconv.ParseUint(value, 10, 64)
|
||||
if e != nil {
|
||||
err = e
|
||||
}
|
||||
vms.Active = active * pagesize
|
||||
case "Pages wired down":
|
||||
wired, e := strconv.ParseUint(value, 10, 64)
|
||||
if e != nil {
|
||||
err = e
|
||||
}
|
||||
vms.Wired = wired * pagesize
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// VirtualMemory returns VirtualmemoryStat.
|
||||
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||
return VirtualMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
||||
ret := &VirtualMemoryStat{}
|
||||
|
||||
total, err := getHwMemsize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = getVMStat(ret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret.Available = ret.Free + ret.Inactive
|
||||
ret.Total = total
|
||||
|
||||
ret.Used = ret.Total - ret.Available
|
||||
ret.UsedPercent = 100 * float64(ret.Used) / float64(ret.Total)
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows && !plan9 && !aix && !netbsd
|
||||
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!plan9,!aix,!netbsd
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||
return VirtualMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
||||
return nil, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func SwapMemory() (*SwapMemoryStat, error) {
|
||||
return SwapMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
||||
return nil, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func SwapDevices() ([]*SwapDevice, error) {
|
||||
return SwapDevicesWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
||||
return nil, common.ErrNotImplementedError
|
||||
}
|
||||
|
|
@ -0,0 +1,167 @@
|
|||
//go:build freebsd
|
||||
// +build freebsd
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"unsafe"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||
return VirtualMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
||||
pageSize, err := common.SysctlUint("vm.stats.vm.v_page_size")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
physmem, err := common.SysctlUint("hw.physmem")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
free, err := common.SysctlUint("vm.stats.vm.v_free_count")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
active, err := common.SysctlUint("vm.stats.vm.v_active_count")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inactive, err := common.SysctlUint("vm.stats.vm.v_inactive_count")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buffers, err := common.SysctlUint("vfs.bufspace")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wired, err := common.SysctlUint("vm.stats.vm.v_wire_count")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cached, laundry uint64
|
||||
osreldate, _ := common.SysctlUint("kern.osreldate")
|
||||
if osreldate < 1102000 {
|
||||
cached, err = common.SysctlUint("vm.stats.vm.v_cache_count")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
laundry, err = common.SysctlUint("vm.stats.vm.v_laundry_count")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
p := pageSize
|
||||
ret := &VirtualMemoryStat{
|
||||
Total: physmem,
|
||||
Free: free * p,
|
||||
Active: active * p,
|
||||
Inactive: inactive * p,
|
||||
Cached: cached * p,
|
||||
Buffers: buffers,
|
||||
Wired: wired * p,
|
||||
Laundry: laundry * p,
|
||||
}
|
||||
|
||||
ret.Available = ret.Inactive + ret.Cached + ret.Free + ret.Laundry
|
||||
ret.Used = ret.Total - ret.Available
|
||||
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Return swapinfo
|
||||
func SwapMemory() (*SwapMemoryStat, error) {
|
||||
return SwapMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
// Constants from vm/vm_param.h
|
||||
// nolint: golint
|
||||
const (
|
||||
XSWDEV_VERSION11 = 1
|
||||
XSWDEV_VERSION = 2
|
||||
)
|
||||
|
||||
// Types from vm/vm_param.h
|
||||
type xswdev struct {
|
||||
Version uint32 // Version is the version
|
||||
Dev uint64 // Dev is the device identifier
|
||||
Flags int32 // Flags is the swap flags applied to the device
|
||||
NBlks int32 // NBlks is the total number of blocks
|
||||
Used int32 // Used is the number of blocks used
|
||||
}
|
||||
|
||||
// xswdev11 is a compatibility for under FreeBSD 11
|
||||
// sys/vm/swap_pager.c
|
||||
type xswdev11 struct {
|
||||
Version uint32 // Version is the version
|
||||
Dev uint32 // Dev is the device identifier
|
||||
Flags int32 // Flags is the swap flags applied to the device
|
||||
NBlks int32 // NBlks is the total number of blocks
|
||||
Used int32 // Used is the number of blocks used
|
||||
}
|
||||
|
||||
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
||||
// FreeBSD can have multiple swap devices so we total them up
|
||||
i, err := common.SysctlUint("vm.nswapdev")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if i == 0 {
|
||||
return nil, errors.New("no swap devices found")
|
||||
}
|
||||
|
||||
c := int(i)
|
||||
|
||||
i, err = common.SysctlUint("vm.stats.vm.v_page_size")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pageSize := i
|
||||
|
||||
var buf []byte
|
||||
s := &SwapMemoryStat{}
|
||||
for n := 0; n < c; n++ {
|
||||
buf, err = unix.SysctlRaw("vm.swap_info", n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// first, try to parse with version 2
|
||||
xsw := (*xswdev)(unsafe.Pointer(&buf[0]))
|
||||
if xsw.Version == XSWDEV_VERSION11 {
|
||||
// this is version 1, so try to parse again
|
||||
xsw := (*xswdev11)(unsafe.Pointer(&buf[0]))
|
||||
if xsw.Version != XSWDEV_VERSION11 {
|
||||
return nil, errors.New("xswdev version mismatch(11)")
|
||||
}
|
||||
s.Total += uint64(xsw.NBlks)
|
||||
s.Used += uint64(xsw.Used)
|
||||
} else if xsw.Version != XSWDEV_VERSION {
|
||||
return nil, errors.New("xswdev version mismatch")
|
||||
} else {
|
||||
s.Total += uint64(xsw.NBlks)
|
||||
s.Used += uint64(xsw.Used)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if s.Total != 0 {
|
||||
s.UsedPercent = float64(s.Used) / float64(s.Total) * 100
|
||||
}
|
||||
s.Total *= pageSize
|
||||
s.Used *= pageSize
|
||||
s.Free = s.Total - s.Used
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,532 @@
|
|||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
)
|
||||
|
||||
type VirtualMemoryExStat struct {
|
||||
ActiveFile uint64 `json:"activefile"`
|
||||
InactiveFile uint64 `json:"inactivefile"`
|
||||
ActiveAnon uint64 `json:"activeanon"`
|
||||
InactiveAnon uint64 `json:"inactiveanon"`
|
||||
Unevictable uint64 `json:"unevictable"`
|
||||
}
|
||||
|
||||
func (v VirtualMemoryExStat) String() string {
|
||||
s, _ := json.Marshal(v)
|
||||
return string(s)
|
||||
}
|
||||
|
||||
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||
return VirtualMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
||||
vm, _, err := fillFromMeminfoWithContext(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vm, nil
|
||||
}
|
||||
|
||||
func VirtualMemoryEx() (*VirtualMemoryExStat, error) {
|
||||
return VirtualMemoryExWithContext(context.Background())
|
||||
}
|
||||
|
||||
func VirtualMemoryExWithContext(ctx context.Context) (*VirtualMemoryExStat, error) {
|
||||
_, vmEx, err := fillFromMeminfoWithContext(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vmEx, nil
|
||||
}
|
||||
|
||||
func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *VirtualMemoryExStat, error) {
|
||||
filename := common.HostProcWithContext(ctx, "meminfo")
|
||||
lines, _ := common.ReadLines(filename)
|
||||
|
||||
// flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
|
||||
memavail := false
|
||||
activeFile := false // "Active(file)" not available: 2.6.28 / Dec 2008
|
||||
inactiveFile := false // "Inactive(file)" not available: 2.6.28 / Dec 2008
|
||||
sReclaimable := false // "Sreclaimable:" not available: 2.6.19 / Nov 2006
|
||||
|
||||
ret := &VirtualMemoryStat{}
|
||||
retEx := &VirtualMemoryExStat{}
|
||||
|
||||
for _, line := range lines {
|
||||
fields := strings.Split(line, ":")
|
||||
if len(fields) != 2 {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(fields[0])
|
||||
value := strings.TrimSpace(fields[1])
|
||||
value = strings.Replace(value, " kB", "", -1)
|
||||
|
||||
switch key {
|
||||
case "MemTotal":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Total = t * 1024
|
||||
case "MemFree":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Free = t * 1024
|
||||
case "MemAvailable":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
memavail = true
|
||||
ret.Available = t * 1024
|
||||
case "Buffers":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Buffers = t * 1024
|
||||
case "Cached":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Cached = t * 1024
|
||||
case "Active":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Active = t * 1024
|
||||
case "Inactive":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Inactive = t * 1024
|
||||
case "Active(anon)":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
retEx.ActiveAnon = t * 1024
|
||||
case "Inactive(anon)":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
retEx.InactiveAnon = t * 1024
|
||||
case "Active(file)":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
activeFile = true
|
||||
retEx.ActiveFile = t * 1024
|
||||
case "Inactive(file)":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
inactiveFile = true
|
||||
retEx.InactiveFile = t * 1024
|
||||
case "Unevictable":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
retEx.Unevictable = t * 1024
|
||||
case "Writeback":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.WriteBack = t * 1024
|
||||
case "WritebackTmp":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.WriteBackTmp = t * 1024
|
||||
case "Dirty":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Dirty = t * 1024
|
||||
case "Shmem":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Shared = t * 1024
|
||||
case "Slab":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Slab = t * 1024
|
||||
case "SReclaimable":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
sReclaimable = true
|
||||
ret.Sreclaimable = t * 1024
|
||||
case "SUnreclaim":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Sunreclaim = t * 1024
|
||||
case "PageTables":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.PageTables = t * 1024
|
||||
case "SwapCached":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.SwapCached = t * 1024
|
||||
case "CommitLimit":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.CommitLimit = t * 1024
|
||||
case "Committed_AS":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.CommittedAS = t * 1024
|
||||
case "HighTotal":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.HighTotal = t * 1024
|
||||
case "HighFree":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.HighFree = t * 1024
|
||||
case "LowTotal":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.LowTotal = t * 1024
|
||||
case "LowFree":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.LowFree = t * 1024
|
||||
case "SwapTotal":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.SwapTotal = t * 1024
|
||||
case "SwapFree":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.SwapFree = t * 1024
|
||||
case "Mapped":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.Mapped = t * 1024
|
||||
case "VmallocTotal":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.VmallocTotal = t * 1024
|
||||
case "VmallocUsed":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.VmallocUsed = t * 1024
|
||||
case "VmallocChunk":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.VmallocChunk = t * 1024
|
||||
case "HugePages_Total":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.HugePagesTotal = t
|
||||
case "HugePages_Free":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.HugePagesFree = t
|
||||
case "HugePages_Rsvd":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.HugePagesRsvd = t
|
||||
case "HugePages_Surp":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.HugePagesSurp = t
|
||||
case "Hugepagesize":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.HugePageSize = t * 1024
|
||||
case "AnonHugePages":
|
||||
t, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
return ret, retEx, err
|
||||
}
|
||||
ret.AnonHugePages = t * 1024
|
||||
}
|
||||
}
|
||||
|
||||
ret.Cached += ret.Sreclaimable
|
||||
|
||||
if !memavail {
|
||||
if activeFile && inactiveFile && sReclaimable {
|
||||
ret.Available = calculateAvailVmem(ctx, ret, retEx)
|
||||
} else {
|
||||
ret.Available = ret.Cached + ret.Free
|
||||
}
|
||||
}
|
||||
|
||||
ret.Used = ret.Total - ret.Free - ret.Buffers - ret.Cached
|
||||
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
|
||||
|
||||
return ret, retEx, nil
|
||||
}
|
||||
|
||||
func SwapMemory() (*SwapMemoryStat, error) {
|
||||
return SwapMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
||||
sysinfo := &unix.Sysinfo_t{}
|
||||
|
||||
if err := unix.Sysinfo(sysinfo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret := &SwapMemoryStat{
|
||||
Total: uint64(sysinfo.Totalswap) * uint64(sysinfo.Unit),
|
||||
Free: uint64(sysinfo.Freeswap) * uint64(sysinfo.Unit),
|
||||
}
|
||||
ret.Used = ret.Total - ret.Free
|
||||
// check Infinity
|
||||
if ret.Total != 0 {
|
||||
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
|
||||
} else {
|
||||
ret.UsedPercent = 0
|
||||
}
|
||||
filename := common.HostProcWithContext(ctx, "vmstat")
|
||||
lines, _ := common.ReadLines(filename)
|
||||
for _, l := range lines {
|
||||
fields := strings.Fields(l)
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
switch fields[0] {
|
||||
case "pswpin":
|
||||
value, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ret.Sin = value * 4 * 1024
|
||||
case "pswpout":
|
||||
value, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ret.Sout = value * 4 * 1024
|
||||
case "pgpgin":
|
||||
value, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ret.PgIn = value * 4 * 1024
|
||||
case "pgpgout":
|
||||
value, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ret.PgOut = value * 4 * 1024
|
||||
case "pgfault":
|
||||
value, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ret.PgFault = value * 4 * 1024
|
||||
case "pgmajfault":
|
||||
value, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ret.PgMajFault = value * 4 * 1024
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// calculateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide
|
||||
// "MemAvailable:" column. It reimplements an algorithm from the link below
|
||||
// https://github.com/giampaolo/psutil/pull/890
|
||||
func calculateAvailVmem(ctx context.Context, ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint64 {
|
||||
var watermarkLow uint64
|
||||
|
||||
fn := common.HostProcWithContext(ctx, "zoneinfo")
|
||||
lines, err := common.ReadLines(fn)
|
||||
if err != nil {
|
||||
return ret.Free + ret.Cached // fallback under kernel 2.6.13
|
||||
}
|
||||
|
||||
pagesize := uint64(os.Getpagesize())
|
||||
watermarkLow = 0
|
||||
|
||||
for _, line := range lines {
|
||||
fields := strings.Fields(line)
|
||||
|
||||
if strings.HasPrefix(fields[0], "low") {
|
||||
lowValue, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
lowValue = 0
|
||||
}
|
||||
watermarkLow += lowValue
|
||||
}
|
||||
}
|
||||
|
||||
watermarkLow *= pagesize
|
||||
|
||||
availMemory := ret.Free - watermarkLow
|
||||
pageCache := retEx.ActiveFile + retEx.InactiveFile
|
||||
pageCache -= uint64(math.Min(float64(pageCache/2), float64(watermarkLow)))
|
||||
availMemory += pageCache
|
||||
availMemory += ret.Sreclaimable - uint64(math.Min(float64(ret.Sreclaimable/2.0), float64(watermarkLow)))
|
||||
|
||||
if availMemory < 0 {
|
||||
availMemory = 0
|
||||
}
|
||||
|
||||
return availMemory
|
||||
}
|
||||
|
||||
const swapsFilename = "swaps"
|
||||
|
||||
// swaps file column indexes
|
||||
const (
|
||||
nameCol = 0
|
||||
// typeCol = 1
|
||||
totalCol = 2
|
||||
usedCol = 3
|
||||
// priorityCol = 4
|
||||
)
|
||||
|
||||
func SwapDevices() ([]*SwapDevice, error) {
|
||||
return SwapDevicesWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
||||
swapsFilePath := common.HostProcWithContext(ctx, swapsFilename)
|
||||
f, err := os.Open(swapsFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return parseSwapsFile(ctx, f)
|
||||
}
|
||||
|
||||
func parseSwapsFile(ctx context.Context, r io.Reader) ([]*SwapDevice, error) {
|
||||
swapsFilePath := common.HostProcWithContext(ctx, swapsFilename)
|
||||
scanner := bufio.NewScanner(r)
|
||||
if !scanner.Scan() {
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read file %q: %w", swapsFilePath, err)
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected end-of-file in %q", swapsFilePath)
|
||||
|
||||
}
|
||||
|
||||
// Check header headerFields are as expected
|
||||
headerFields := strings.Fields(scanner.Text())
|
||||
if len(headerFields) < usedCol {
|
||||
return nil, fmt.Errorf("couldn't parse %q: too few fields in header", swapsFilePath)
|
||||
}
|
||||
if headerFields[nameCol] != "Filename" {
|
||||
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsFilePath, headerFields[nameCol], "Filename")
|
||||
}
|
||||
if headerFields[totalCol] != "Size" {
|
||||
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsFilePath, headerFields[totalCol], "Size")
|
||||
}
|
||||
if headerFields[usedCol] != "Used" {
|
||||
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsFilePath, headerFields[usedCol], "Used")
|
||||
}
|
||||
|
||||
var swapDevices []*SwapDevice
|
||||
for scanner.Scan() {
|
||||
fields := strings.Fields(scanner.Text())
|
||||
if len(fields) < usedCol {
|
||||
return nil, fmt.Errorf("couldn't parse %q: too few fields", swapsFilePath)
|
||||
}
|
||||
|
||||
totalKiB, err := strconv.ParseUint(fields[totalCol], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't parse 'Size' column in %q: %w", swapsFilePath, err)
|
||||
}
|
||||
|
||||
usedKiB, err := strconv.ParseUint(fields[usedCol], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't parse 'Used' column in %q: %w", swapsFilePath, err)
|
||||
}
|
||||
|
||||
swapDevices = append(swapDevices, &SwapDevice{
|
||||
Name: fields[nameCol],
|
||||
UsedBytes: usedKiB * 1024,
|
||||
FreeBytes: (totalKiB - usedKiB) * 1024,
|
||||
})
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, fmt.Errorf("couldn't read file %q: %w", swapsFilePath, err)
|
||||
}
|
||||
|
||||
return swapDevices, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
//go:build netbsd
|
||||
// +build netbsd
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func GetPageSize() (uint64, error) {
|
||||
return GetPageSizeWithContext(context.Background())
|
||||
}
|
||||
|
||||
func GetPageSizeWithContext(ctx context.Context) (uint64, error) {
|
||||
uvmexp, err := unix.SysctlUvmexp("vm.uvmexp2")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint64(uvmexp.Pagesize), nil
|
||||
}
|
||||
|
||||
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||
return VirtualMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
||||
uvmexp, err := unix.SysctlUvmexp("vm.uvmexp2")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p := uint64(uvmexp.Pagesize)
|
||||
|
||||
ret := &VirtualMemoryStat{
|
||||
Total: uint64(uvmexp.Npages) * p,
|
||||
Free: uint64(uvmexp.Free) * p,
|
||||
Active: uint64(uvmexp.Active) * p,
|
||||
Inactive: uint64(uvmexp.Inactive) * p,
|
||||
Cached: 0, // not available
|
||||
Wired: uint64(uvmexp.Wired) * p,
|
||||
}
|
||||
|
||||
ret.Available = ret.Inactive + ret.Cached + ret.Free
|
||||
ret.Used = ret.Total - ret.Available
|
||||
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
|
||||
|
||||
// Get buffers from vm.bufmem sysctl
|
||||
ret.Buffers, err = unix.SysctlUint64("vm.bufmem")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Return swapctl summary info
|
||||
func SwapMemory() (*SwapMemoryStat, error) {
|
||||
return SwapMemoryWithContext(context.Background())
|
||||
}
|
||||
|
||||
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
||||
out, err := invoke.CommandWithContext(ctx, "swapctl", "-sk")
|
||||
if err != nil {
|
||||
return &SwapMemoryStat{}, nil
|
||||
}
|
||||
|
||||
line := string(out)
|
||||
var total, used, free uint64
|
||||
|
||||
_, err = fmt.Sscanf(line,
|
||||
"total: %d 1K-blocks allocated, %d used, %d available",
|
||||
&total, &used, &free)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to parse swapctl output")
|
||||
}
|
||||
|
||||
percent := float64(used) / float64(total) * 100
|
||||
return &SwapMemoryStat{
|
||||
Total: total * 1024,
|
||||
Used: used * 1024,
|
||||
Free: free * 1024,
|
||||
UsedPercent: percent,
|
||||
}, nil
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue