From 8294f67566ebefb55e9659ff6ad7049c873335ce Mon Sep 17 00:00:00 2001 From: Lomanic Date: Thu, 27 Dec 2018 21:23:47 +0100 Subject: [PATCH 1/4] [host][openbsd] Remove external calls to uname in PlatformInformation() --- host/host_openbsd.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/host/host_openbsd.go b/host/host_openbsd.go index 2ad64d77..bb16fca2 100644 --- a/host/host_openbsd.go +++ b/host/host_openbsd.go @@ -8,7 +8,6 @@ import ( "encoding/binary" "io/ioutil" "os" - "os/exec" "runtime" "strconv" "strings" @@ -17,6 +16,7 @@ import ( "github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/process" + "golang.org/x/sys/unix" ) const ( @@ -108,19 +108,14 @@ func PlatformInformationWithContext(ctx context.Context) (string, string, string platform := "" family := "" version := "" - uname, err := exec.LookPath("uname") - if err != nil { - return "", "", "", err - } - out, err := invoke.CommandWithContext(ctx, uname, "-s") + p, err := unix.Sysctl("kern.ostype") if err == nil { - platform = strings.ToLower(strings.TrimSpace(string(out))) + platform = strings.ToLower(p) } - - out, err = invoke.CommandWithContext(ctx, uname, "-r") + v, err := unix.Sysctl("kern.osrelease") if err == nil { - version = strings.ToLower(strings.TrimSpace(string(out))) + version = strings.ToLower(v) } return platform, family, version, nil From 738b966ed1f511d4ec9c9a9a2b3f7a8e204e6952 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 29 Dec 2018 13:38:21 +0100 Subject: [PATCH 2/4] [host][darwin] Use unix.Sysctl to get kernel version and platform name --- host/host_darwin.go | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/host/host_darwin.go b/host/host_darwin.go index 8241fc08..5cf22b7c 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -18,6 +18,7 @@ import ( "github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/process" + "golang.org/x/sys/unix" ) // from utmpx.h @@ -180,17 +181,13 @@ func PlatformInformationWithContext(ctx context.Context) (string, string, string if err != nil { return "", "", "", err } - uname, err := exec.LookPath("uname") - if err != nil { - return "", "", "", err - } - out, err := invoke.CommandWithContext(ctx, uname, "-s") + p, err := unix.Sysctl("kern.ostype") if err == nil { - platform = strings.ToLower(strings.TrimSpace(string(out))) + platform = strings.ToLower(p) } - out, err = invoke.CommandWithContext(ctx, sw_vers, "-productVersion") + out, err := invoke.CommandWithContext(ctx, sw_vers, "-productVersion") if err == nil { pver = strings.ToLower(strings.TrimSpace(string(out))) } @@ -211,16 +208,8 @@ func KernelVersion() (string, error) { } func KernelVersionWithContext(ctx context.Context) (string, error) { - uname, err := exec.LookPath("uname") - if err != nil { - return "", err - } - out, err := invoke.CommandWithContext(ctx, uname, "-r") - if err != nil { - return "", err - } - version := strings.ToLower(strings.TrimSpace(string(out))) - return version, err + version, err := unix.Sysctl("kern.osrelease") + return strings.ToLower(version), err } func SensorsTemperatures() ([]TemperatureStat, error) { From 5f8353c741433c351a7123cd74c2e6a9c3d2c6a7 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 29 Dec 2018 14:44:01 +0100 Subject: [PATCH 3/4] [cpu][openbsd] Fix #621, define CPU states according to OpenBSD version --- cpu/cpu_openbsd.go | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/cpu/cpu_openbsd.go b/cpu/cpu_openbsd.go index 82b920f6..7a3c2a6a 100644 --- a/cpu/cpu_openbsd.go +++ b/cpu/cpu_openbsd.go @@ -16,7 +16,7 @@ import ( ) // sys/sched.h -const ( +var ( CPUser = 0 CPNice = 1 CPSys = 2 @@ -35,18 +35,36 @@ const ( var ClocksPerSec = float64(128) func init() { - getconf, err := exec.LookPath("/usr/bin/getconf") - if err != nil { - return - } - out, err := invoke.Command(getconf, "CLK_TCK") - // ignore errors - if err == nil { - i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) - if err == nil { - ClocksPerSec = float64(i) + func() { + getconf, err := exec.LookPath("/usr/bin/getconf") + if err != nil { + return } - } + out, err := invoke.Command(getconf, "CLK_TCK") + // ignore errors + if err == nil { + i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64) + if err == nil { + ClocksPerSec = float64(i) + } + } + }() + func() { + v, err := unix.Sysctl("kern.osrelease") // can't reuse host.PlatformInformation because of circular import + if err != nil { + return + } + v = strings.ToLower(v) + version, err := strconv.ParseFloat(v, 64) + if err != nil { + return + } + if version >= 6.4 { + CPIntr = 4 + CPIdle = 5 + CPUStates = 6 + } + }() } func Times(percpu bool) ([]TimesStat, error) { @@ -64,7 +82,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { } for i := 0; i < ncpu; i++ { - var cpuTimes [CPUStates]int64 + var cpuTimes = make([]int64, CPUStates) var mib []int32 if percpu { mib = []int32{CTLKern, KernCptime} From 56acda8a5b6b4740713d4bc418717fb650470ea0 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 29 Dec 2018 15:19:23 +0100 Subject: [PATCH 4/4] [process][openbsd] Fix compilation on OpenBSD --- process/process_openbsd.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/process/process_openbsd.go b/process/process_openbsd.go index f9e0a086..155ed6ec 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -7,6 +7,8 @@ import ( "bytes" "context" "encoding/binary" + "os/exec" + "strconv" "strings" "unsafe"