1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-02 22:17:08 +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
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}

View File

@ -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) {

View File

@ -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

View File

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