Beispiel #1
0
func (h Host) BasicInfo() *BasicInfo {
	host_info := new(BasicInfo)
	count := C.mach_msg_type_number_t(C.HOST_BASIC_INFO_COUNT)
	if C.host_info(C.host_t(h), C.HOST_BASIC_INFO, (*C.integer_t)(unsafe.Pointer(host_info)), &count) != 0 {
		return nil
	}

	return host_info
}
Beispiel #2
0
// Collect populates various cpu performance statistics - use MACH interface
func (s *CPUStat) Collect() {

	// collect CPU stats for All cpus aggregated
	var cpuinfo C.host_cpu_load_info_data_t
	var hostinfo C.host_basic_info_data_t

	cpuloadnumber := C.mach_msg_type_number_t(C.HOST_CPU_LOAD_INFO_COUNT)
	hostnumber := C.mach_msg_type_number_t(C.HOST_BASIC_INFO_COUNT)
	host := C.mach_host_self()
	ret := C.host_statistics(C.host_t(host), C.HOST_CPU_LOAD_INFO,
		C.host_info_t(unsafe.Pointer(&cpuinfo)), &cpuloadnumber)

	if ret != C.KERN_SUCCESS {
		return
	}

	ret = C.host_info(C.host_t(host), C.HOST_BASIC_INFO,
		C.host_info_t(unsafe.Pointer(&hostinfo)), &hostnumber)
	if ret != C.KERN_SUCCESS {
		return
	}

	s.All.User.Set(uint64(cpuinfo.cpu_ticks[C.CPU_STATE_USER]))
	s.All.UserLowPrio.Set(uint64(cpuinfo.cpu_ticks[C.CPU_STATE_NICE]))
	s.All.System.Set(uint64(cpuinfo.cpu_ticks[C.CPU_STATE_SYSTEM]))
	s.All.Idle.Set(uint64(cpuinfo.cpu_ticks[C.CPU_STATE_IDLE]))

	s.All.Total.Set(uint64(cpuinfo.cpu_ticks[C.CPU_STATE_USER]) +
		uint64(cpuinfo.cpu_ticks[C.CPU_STATE_SYSTEM]) +
		uint64(cpuinfo.cpu_ticks[C.CPU_STATE_NICE]) +
		uint64(cpuinfo.cpu_ticks[C.CPU_STATE_IDLE]))
	s.All.UsageCount.Set(s.All.Usage())
	s.All.UserSpaceCount.Set(s.All.UserSpace())
	s.All.KernelCount.Set(s.All.Kernel())
	s.All.TotalCount.Set(float64(hostinfo.logical_cpu_max))
}