2017-02-01 18:26:00 +00:00
|
|
|
// +build openbsd
|
|
|
|
|
|
|
|
package cpu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2017-02-01 18:26:00 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2020-07-01 12:47:56 +00:00
|
|
|
"runtime"
|
2017-02-01 18:26:00 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-07-01 12:47:56 +00:00
|
|
|
"syscall"
|
2017-02-01 18:26:00 +00:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// sys/sched.h
|
2020-07-01 12:47:56 +00:00
|
|
|
var (
|
2017-02-01 18:26:00 +00:00
|
|
|
CPUser = 0
|
|
|
|
CPNice = 1
|
|
|
|
CPSys = 2
|
|
|
|
CPIntr = 3
|
|
|
|
CPIdle = 4
|
|
|
|
CPUStates = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
// sys/sysctl.h
|
|
|
|
const (
|
2017-02-04 06:31:11 +00:00
|
|
|
CTLKern = 1 // "high kernel": proc, limits
|
2020-07-01 12:47:56 +00:00
|
|
|
CTLHw = 6 // CTL_HW
|
|
|
|
SMT = 24 // HW_SMT
|
2017-02-04 06:31:11 +00:00
|
|
|
KernCptime = 40 // KERN_CPTIME
|
|
|
|
KernCptime2 = 71 // KERN_CPTIME2
|
2017-02-01 18:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ClocksPerSec = float64(128)
|
|
|
|
|
|
|
|
func init() {
|
2020-07-01 12:47:56 +00:00
|
|
|
func() {
|
|
|
|
getconf, err := exec.LookPath("getconf")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out, err := invoke.Command(getconf, "CLK_TCK")
|
|
|
|
// ignore errors
|
2017-02-01 18:26:00 +00:00
|
|
|
if err == nil {
|
2020-07-01 12:47:56 +00:00
|
|
|
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
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func smt() (bool, error) {
|
|
|
|
mib := []int32{CTLHw, SMT}
|
|
|
|
buf, _, err := common.CallSyscall(mib)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret bool
|
|
|
|
br := bytes.NewReader(buf)
|
|
|
|
if err := binary.Read(br, binary.LittleEndian, &ret); err != nil {
|
|
|
|
return false, err
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
|
|
|
|
return ret, nil
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Times(percpu bool) ([]TimesStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return TimesWithContext(context.Background(), percpu)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
var ret []TimesStat
|
|
|
|
|
|
|
|
var ncpu int
|
|
|
|
if percpu {
|
|
|
|
ncpu, _ = Counts(true)
|
|
|
|
} else {
|
|
|
|
ncpu = 1
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
smt, err := smt()
|
|
|
|
if err == syscall.EOPNOTSUPP {
|
|
|
|
// if hw.smt is not applicable for this platform (e.g. i386),
|
|
|
|
// pretend it's enabled
|
|
|
|
smt = true
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:00 +00:00
|
|
|
for i := 0; i < ncpu; i++ {
|
2020-07-01 12:47:56 +00:00
|
|
|
j := i
|
|
|
|
if !smt {
|
|
|
|
j *= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
var cpuTimes = make([]int32, CPUStates)
|
2017-02-01 18:26:00 +00:00
|
|
|
var mib []int32
|
|
|
|
if percpu {
|
2020-07-01 12:47:56 +00:00
|
|
|
mib = []int32{CTLKern, KernCptime2, int32(j)}
|
2017-02-01 18:26:00 +00:00
|
|
|
} else {
|
2020-07-01 12:47:56 +00:00
|
|
|
mib = []int32{CTLKern, KernCptime}
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
buf, _, err := common.CallSyscall(mib)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
br := bytes.NewReader(buf)
|
|
|
|
err = binary.Read(br, binary.LittleEndian, &cpuTimes)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
c := TimesStat{
|
|
|
|
User: float64(cpuTimes[CPUser]) / ClocksPerSec,
|
|
|
|
Nice: float64(cpuTimes[CPNice]) / ClocksPerSec,
|
2017-02-04 06:31:11 +00:00
|
|
|
System: float64(cpuTimes[CPSys]) / ClocksPerSec,
|
2017-02-01 18:26:00 +00:00
|
|
|
Idle: float64(cpuTimes[CPIdle]) / ClocksPerSec,
|
|
|
|
Irq: float64(cpuTimes[CPIntr]) / ClocksPerSec,
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
if percpu {
|
|
|
|
c.CPU = fmt.Sprintf("cpu%d", j)
|
2017-02-01 18:26:00 +00:00
|
|
|
} else {
|
2020-07-01 12:47:56 +00:00
|
|
|
c.CPU = "cpu-total"
|
2017-02-01 18:26:00 +00:00
|
|
|
}
|
|
|
|
ret = append(ret, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns only one (minimal) CPUInfoStat on OpenBSD
|
|
|
|
func Info() ([]InfoStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
2017-02-01 18:26:00 +00:00
|
|
|
var ret []InfoStat
|
2020-07-01 12:47:56 +00:00
|
|
|
var err error
|
2017-02-01 18:26:00 +00:00
|
|
|
|
|
|
|
c := InfoStat{}
|
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
mhz, err := unix.SysctlUint32("hw.cpuspeed")
|
2017-02-01 18:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-07 16:57:18 +00:00
|
|
|
c.Mhz = float64(mhz)
|
2020-07-01 12:47:56 +00:00
|
|
|
|
2020-10-07 16:57:18 +00:00
|
|
|
ncpu, err := unix.SysctlUint32("hw.ncpuonline")
|
2020-07-01 12:47:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-07 16:57:18 +00:00
|
|
|
c.Cores = int32(ncpu)
|
2020-07-01 12:47:56 +00:00
|
|
|
|
|
|
|
if c.ModelName, err = unix.Sysctl("hw.model"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-01 18:26:00 +00:00
|
|
|
|
|
|
|
return append(ret, c), nil
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
|
|
|
|
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
|
|
|
return runtime.NumCPU(), nil
|
|
|
|
}
|