/*
#cgo LDFLAGS:
#include <stdlib.h>
#include <limits.h>
#include <sys/sysctl.h>
#include <sys/mount.h>
#include <mach/mach_init.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
#if TARGET_OS_MAC
#include <libproc.h>
#endif
#include <mach/processor_info.h>
#include <mach/vm_map.h>
*/import"C"funcgetcpu()error{var(countC.mach_msg_type_number_tcpuload*C.processor_cpu_load_info_data_tncpuC.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)ifstatus!=C.KERN_SUCCESS{returnfmt.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)))deferC.vm_deallocate(target,address,C.vm_size_t(ncpu))// the body of struct processor_cpu_load_info
// aka processor_cpu_load_info_data_t
varcpuTicks[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(cpuTicks)buf:=(*[1<<30]byte)(unsafe.Pointer(cpuload))[:size:size]bbuf:=bytes.NewBuffer(buf)fori:=0;i<int(ncpu);i++{err:=binary.Read(bbuf,binary.LittleEndian,&cpuTicks)iferr!=nil{returnerr}fork,v:=rangemap[string]int{"user":C.CPU_STATE_USER,"system":C.CPU_STATE_SYSTEM,"nice":C.CPU_STATE_NICE,"idle":C.CPU_STATE_IDLE,}{...// do something with float64(cpuTicks[v])/ClocksPerSec
}}returnnil}
/*
#cgo LDFLAGS:
#include <stdlib.h>
#include <limits.h>
#include <sys/sysctl.h>
#include <sys/mount.h>
#include <mach/mach_init.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
#if TARGET_OS_MAC
#include <libproc.h>
#endif
#include <mach/processor_info.h>
#include <mach/vm_map.h>
*/import"C"funcgetcpu()error{var(countC.mach_msg_type_number_tcpuload*C.processor_cpu_load_info_data_tncpuC.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)ifstatus!=C.KERN_SUCCESS{returnfmt.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)))deferC.vm_deallocate(target,address,C.vm_size_t(ncpu))// the body of struct processor_cpu_load_info
// aka processor_cpu_load_info_data_t
varcpuTicks[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(cpuTicks)buf:=(*[1<<30]byte)(unsafe.Pointer(cpuload))[:size:size]bbuf:=bytes.NewBuffer(buf)fori:=0;i<int(ncpu);i++{err:=binary.Read(bbuf,binary.LittleEndian,&cpuTicks)iferr!=nil{returnerr}fork,v:=rangemap[string]int{"user":C.CPU_STATE_USER,"system":C.CPU_STATE_SYSTEM,"nice":C.CPU_STATE_NICE,"idle":C.CPU_STATE_IDLE,}{...// do something with float64(cpuTicks[v])/ClocksPerSec
}}returnnil}