Skip to content

Commit

Permalink
feat(cpu, mem, sensors)(darwin): cgo-free implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb committed Sep 4, 2024
1 parent 211eb8c commit 701a74b
Show file tree
Hide file tree
Showing 16 changed files with 735 additions and 696 deletions.
92 changes: 90 additions & 2 deletions cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ package cpu

import (
"context"
"fmt"
"strconv"
"strings"
"unsafe"

"github.com/shoenig/go-m1cpu"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v4/internal/common"
)

// sys/resource.h
Expand All @@ -23,6 +27,24 @@ const (
cpUStates = 5
)

// mach/machine.h
const (
cpuStateUser = 0
cpuStateSystem = 1
cpuStateIdle = 2
cpuStateNice = 3
cpuStateMax = 4
)

// mach/processor_info.h
const (
processorCpuLoadInfo = 2
)

type hostCpuLoadInfoData struct {
cpuTicks [cpuStateMax]uint32
}

// default value. from time.h
var ClocksPerSec = float64(128)

Expand All @@ -39,11 +61,17 @@ func Times(percpu bool) ([]TimesStat, error) {
}

func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
lib, err := common.NewLibrary(common.Kernel)
if err != nil {
return nil, err
}
defer lib.Close()

if percpu {
return perCPUTimes()
return perCPUTimes(lib)
}

return allCPUTimes()
return allCPUTimes(lib)
}

// Returns only one CPUInfoStat on FreeBSD
Expand Down Expand Up @@ -115,3 +143,63 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {

return int(count), nil
}

func perCPUTimes(machLib *common.Library) ([]TimesStat, error) {
machHostSelf := common.GetFunc[common.MachHostSelfFunc](machLib, common.MachHostSelfSym)
machTaskSelf := common.GetFunc[common.MachTaskSelfFunc](machLib, common.MachTaskSelfSym)
hostProcessorInfo := common.GetFunc[common.HostProcessorInfoFunc](machLib, common.HostProcessorInfoSym)
vmDeallocate := common.GetFunc[common.VMDeallocateFunc](machLib, common.VMDeallocateSym)

var count, ncpu uint32
var cpuload *hostCpuLoadInfoData

status := hostProcessorInfo(machHostSelf(), processorCpuLoadInfo, &ncpu, uintptr(unsafe.Pointer(&cpuload)), &count)

if status != common.KERN_SUCCESS {
return nil, fmt.Errorf("host_processor_info error=%d", status)
}

defer vmDeallocate(machTaskSelf(), uintptr(unsafe.Pointer(cpuload)), uintptr(ncpu))

ret := []TimesStat{}
loads := unsafe.Slice(cpuload, ncpu)

for i := 0; i < int(ncpu); i++ {
c := TimesStat{
CPU: fmt.Sprintf("cpu%d", i),
User: float64(loads[i].cpuTicks[cpuStateUser]) / ClocksPerSec,
System: float64(loads[i].cpuTicks[cpuStateSystem]) / ClocksPerSec,
Nice: float64(loads[i].cpuTicks[cpuStateNice]) / ClocksPerSec,
Idle: float64(loads[i].cpuTicks[cpuStateIdle]) / ClocksPerSec,
}

ret = append(ret, c)
}

return ret, nil
}

func allCPUTimes(machLib *common.Library) ([]TimesStat, error) {
machHostSelf := common.GetFunc[common.MachHostSelfFunc](machLib, common.MachHostSelfSym)
hostStatistics := common.GetFunc[common.HostStatisticsFunc](machLib, common.HostStatisticsSym)

var cpuload hostCpuLoadInfoData
count := uint32(cpuStateMax)

status := hostStatistics(machHostSelf(), common.HOST_CPU_LOAD_INFO,
uintptr(unsafe.Pointer(&cpuload)), &count)

if status != common.KERN_SUCCESS {
return nil, fmt.Errorf("host_statistics error=%d", status)
}

c := TimesStat{
CPU: "cpu-total",
User: float64(cpuload.cpuTicks[cpuStateUser]) / ClocksPerSec,
System: float64(cpuload.cpuTicks[cpuStateSystem]) / ClocksPerSec,
Nice: float64(cpuload.cpuTicks[cpuStateNice]) / ClocksPerSec,
Idle: float64(cpuload.cpuTicks[cpuStateIdle]) / ClocksPerSec,
}

return []TimesStat{c}, nil
}
111 changes: 0 additions & 111 deletions cpu/cpu_darwin_cgo.go

This file was deleted.

14 changes: 0 additions & 14 deletions cpu/cpu_darwin_nocgo.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/shirou/gopsutil/v4
go 1.18

require (
github.com/ebitengine/purego v0.7.1
github.com/google/go-cmp v0.6.0
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA=
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down
Loading

0 comments on commit 701a74b

Please sign in to comment.