2021-12-23 05:54:41 +08:00
|
|
|
//go:build darwin && cgo
|
|
|
|
// +build darwin,cgo
|
2015-10-10 21:13:38 +08:00
|
|
|
|
|
|
|
package cpu
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <mach/mach_init.h>
|
|
|
|
#include <mach/mach_host.h>
|
|
|
|
#include <mach/host_info.h>
|
2021-03-02 01:41:31 +08:00
|
|
|
#include <TargetConditionals.h>
|
2016-05-11 03:27:10 +08:00
|
|
|
#if TARGET_OS_MAC
|
2015-10-10 21:13:38 +08:00
|
|
|
#include <libproc.h>
|
2016-05-11 03:27:10 +08:00
|
|
|
#endif
|
2015-10-10 21:13:38 +08:00
|
|
|
#include <mach/processor_info.h>
|
|
|
|
#include <mach/vm_map.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// these CPU times for darwin is borrowed from influxdb/telegraf.
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func perCPUTimes() ([]TimesStat, error) {
|
2015-10-10 21:13:38 +08:00
|
|
|
var (
|
|
|
|
count C.mach_msg_type_number_t
|
|
|
|
cpuload *C.processor_cpu_load_info_data_t
|
|
|
|
ncpu C.natural_t
|
|
|
|
)
|
|
|
|
|
|
|
|
status := C.host_processor_info(C.host_t(C.mach_host_self()),
|
|
|
|
C.PROCESSOR_CPU_LOAD_INFO,
|
|
|
|
&ncpu,
|
|
|
|
(*C.processor_info_array_t)(unsafe.Pointer(&cpuload)),
|
|
|
|
&count)
|
|
|
|
|
|
|
|
if status != C.KERN_SUCCESS {
|
|
|
|
return nil, fmt.Errorf("host_processor_info error=%d", status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// jump through some cgo casting hoops and ensure we properly free
|
|
|
|
// the memory that cpuload points to
|
|
|
|
target := C.vm_map_t(C.mach_task_self_)
|
|
|
|
address := C.vm_address_t(uintptr(unsafe.Pointer(cpuload)))
|
|
|
|
defer C.vm_deallocate(target, address, C.vm_size_t(ncpu))
|
|
|
|
|
|
|
|
// the body of struct processor_cpu_load_info
|
|
|
|
// aka processor_cpu_load_info_data_t
|
|
|
|
var cpu_ticks [C.CPU_STATE_MAX]uint32
|
|
|
|
|
|
|
|
// copy the cpuload array to a []byte buffer
|
|
|
|
// where we can binary.Read the data
|
|
|
|
size := int(ncpu) * binary.Size(cpu_ticks)
|
2016-11-14 23:53:20 +08:00
|
|
|
buf := (*[1 << 30]byte)(unsafe.Pointer(cpuload))[:size:size]
|
2015-10-10 21:13:38 +08:00
|
|
|
|
|
|
|
bbuf := bytes.NewBuffer(buf)
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
var ret []TimesStat
|
2015-10-10 21:13:38 +08:00
|
|
|
|
|
|
|
for i := 0; i < int(ncpu); i++ {
|
|
|
|
err := binary.Read(bbuf, binary.LittleEndian, &cpu_ticks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
c := TimesStat{
|
2015-10-10 21:13:38 +08:00
|
|
|
CPU: fmt.Sprintf("cpu%d", i),
|
|
|
|
User: float64(cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec,
|
|
|
|
System: float64(cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec,
|
|
|
|
Nice: float64(cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec,
|
|
|
|
Idle: float64(cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec,
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func allCPUTimes() ([]TimesStat, error) {
|
2016-08-23 05:20:29 +08:00
|
|
|
var count C.mach_msg_type_number_t
|
2015-10-10 21:13:38 +08:00
|
|
|
var cpuload C.host_cpu_load_info_data_t
|
|
|
|
|
2016-08-23 05:20:29 +08:00
|
|
|
count = C.HOST_CPU_LOAD_INFO_COUNT
|
|
|
|
|
2015-10-10 21:13:38 +08:00
|
|
|
status := C.host_statistics(C.host_t(C.mach_host_self()),
|
|
|
|
C.HOST_CPU_LOAD_INFO,
|
|
|
|
C.host_info_t(unsafe.Pointer(&cpuload)),
|
|
|
|
&count)
|
|
|
|
|
|
|
|
if status != C.KERN_SUCCESS {
|
|
|
|
return nil, fmt.Errorf("host_statistics error=%d", status)
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
c := TimesStat{
|
2015-10-10 21:13:38 +08:00
|
|
|
CPU: "cpu-total",
|
|
|
|
User: float64(cpuload.cpu_ticks[C.CPU_STATE_USER]) / ClocksPerSec,
|
|
|
|
System: float64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM]) / ClocksPerSec,
|
|
|
|
Nice: float64(cpuload.cpu_ticks[C.CPU_STATE_NICE]) / ClocksPerSec,
|
|
|
|
Idle: float64(cpuload.cpu_ticks[C.CPU_STATE_IDLE]) / ClocksPerSec,
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
return []TimesStat{c}, nil
|
2015-10-10 21:13:38 +08:00
|
|
|
}
|