1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-06 19:29:13 +08:00

Merge pull request #623 from Lomanic/issue621

Fix #621 Add OpenBSD 6.4+ CPU States
This commit is contained in:
Lomanic 2018-12-31 16:08:26 +01:00 committed by GitHub
commit db425313bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 40 deletions

View File

@ -16,7 +16,7 @@ import (
) )
// sys/sched.h // sys/sched.h
const ( var (
CPUser = 0 CPUser = 0
CPNice = 1 CPNice = 1
CPSys = 2 CPSys = 2
@ -35,6 +35,7 @@ const (
var ClocksPerSec = float64(128) var ClocksPerSec = float64(128)
func init() { func init() {
func() {
getconf, err := exec.LookPath("/usr/bin/getconf") getconf, err := exec.LookPath("/usr/bin/getconf")
if err != nil { if err != nil {
return return
@ -47,6 +48,23 @@ func init() {
ClocksPerSec = float64(i) 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) { 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++ { for i := 0; i < ncpu; i++ {
var cpuTimes [CPUStates]int64 var cpuTimes = make([]int64, CPUStates)
var mib []int32 var mib []int32
if percpu { if percpu {
mib = []int32{CTLKern, KernCptime} mib = []int32{CTLKern, KernCptime}

View File

@ -18,6 +18,7 @@ import (
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/process" "github.com/shirou/gopsutil/process"
"golang.org/x/sys/unix"
) )
// from utmpx.h // from utmpx.h
@ -180,17 +181,13 @@ func PlatformInformationWithContext(ctx context.Context) (string, string, string
if err != nil { if err != nil {
return "", "", "", err 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 { 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 { if err == nil {
pver = strings.ToLower(strings.TrimSpace(string(out))) pver = strings.ToLower(strings.TrimSpace(string(out)))
} }
@ -211,16 +208,8 @@ func KernelVersion() (string, error) {
} }
func KernelVersionWithContext(ctx context.Context) (string, error) { func KernelVersionWithContext(ctx context.Context) (string, error) {
uname, err := exec.LookPath("uname") version, err := unix.Sysctl("kern.osrelease")
if err != nil { return strings.ToLower(version), err
return "", err
}
out, err := invoke.CommandWithContext(ctx, uname, "-r")
if err != nil {
return "", err
}
version := strings.ToLower(strings.TrimSpace(string(out)))
return version, err
} }
func SensorsTemperatures() ([]TemperatureStat, error) { func SensorsTemperatures() ([]TemperatureStat, error) {

View File

@ -8,7 +8,6 @@ import (
"encoding/binary" "encoding/binary"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -17,6 +16,7 @@ import (
"github.com/shirou/gopsutil/internal/common" "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/process" "github.com/shirou/gopsutil/process"
"golang.org/x/sys/unix"
) )
const ( const (
@ -108,19 +108,14 @@ func PlatformInformationWithContext(ctx context.Context) (string, string, string
platform := "" platform := ""
family := "" family := ""
version := "" 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 { if err == nil {
platform = strings.ToLower(strings.TrimSpace(string(out))) platform = strings.ToLower(p)
} }
v, err := unix.Sysctl("kern.osrelease")
out, err = invoke.CommandWithContext(ctx, uname, "-r")
if err == nil { if err == nil {
version = strings.ToLower(strings.TrimSpace(string(out))) version = strings.ToLower(v)
} }
return platform, family, version, nil return platform, family, version, nil

View File

@ -7,6 +7,8 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/binary" "encoding/binary"
"os/exec"
"strconv"
"strings" "strings"
"unsafe" "unsafe"