Exemple #1
0
func (s *CPUStat) Collect() {

	// collect CPU stats for All cpus aggregated
	var cpuinfo C.host_cpu_load_info_data_t
	count := C.mach_msg_type_number_t(C.HOST_CPU_LOAD_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)), &count)

	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]))

}
Exemple #2
0
// stolen from https://github.com/cloudfoundry/gosigar
func getCPUTotalTimes() (*CPUTimesStat, error) {
	var count C.mach_msg_type_number_t = C.HOST_CPU_LOAD_INFO_COUNT
	var cpuload C.host_cpu_load_info_data_t

	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)
	}

	cpu := CPUTimesStat{
		CPU: "total"}

	var cpu_ticks = make([]uint32, len(cpuload.cpu_ticks))
	for i := range cpuload.cpu_ticks {
		cpu_ticks[i] = uint32(cpuload.cpu_ticks[i])
	}

	fillCPUStats(cpu_ticks, &cpu)

	return &cpu, nil
}
Exemple #3
0
func allCPUTimes() ([]CPUTimesStat, error) {
	var count C.mach_msg_type_number_t = C.HOST_CPU_LOAD_INFO_COUNT
	var cpuload C.host_cpu_load_info_data_t

	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)
	}

	c := CPUTimesStat{
		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,
		Iowait:    -1,
		Irq:       -1,
		Softirq:   -1,
		Steal:     -1,
		Guest:     -1,
		GuestNice: -1,
		Stolen:    -1,
	}

	return []CPUTimesStat{c}, nil

}
Exemple #4
0
func (h Host) LoadInfo() *LoadInfo {
	host_load_info := new(LoadInfo)
	count := C.mach_msg_type_number_t(C.HOST_LOAD_INFO_COUNT)
	if C.host_statistics(C.host_t(h), C.HOST_LOAD_INFO, (*C.integer_t)(unsafe.Pointer(host_load_info)), &count) != 0 {
		return nil
	}

	return host_load_info
}
Exemple #5
0
func (h Host) VmInfo() (*VmInfo, os.Error) {
	vmstat := new(C.vm_statistics_data_t)
	nummsg := C.mach_msg_type_number_t(C.HOST_VM_INFO_COUNT)
	ret := C.host_statistics(C.host_t(h), C.HOST_VM_INFO, (*C.integer_t)(unsafe.Pointer(vmstat)), &nummsg)
	if ret != 0 {
		return nil, fmt.Errorf("host_statistics: %s", C.mach_error_string(C.mach_error_t(ret)))
	}
	return &VmInfo{uint64(vmstat.pageins), uint64(vmstat.pageouts)}, nil
}
func cpuTimeTotal() int {
	selfHost := C.mach_host_self()
	hostInfo := C.malloc(C.size_t(C.HOST_CPU_LOAD_INFO_COUNT))
	count := C.mach_msg_type_number_t(C.HOST_CPU_LOAD_INFO_COUNT)

	err := C.host_statistics(C.host_t(selfHost), C.HOST_CPU_LOAD_INFO, C.host_info_t(hostInfo), &count)
	defer C.free(hostInfo)

	if err != C.kern_return_t(C.KERN_SUCCESS) {
		return 0
	}

	return -1
}
func vm_info(vmstat *C.vm_statistics_data_t) error {
	var count C.mach_msg_type_number_t = C.HOST_VM_INFO_COUNT

	status := C.host_statistics(
		C.host_t(C.mach_host_self()),
		C.HOST_VM_INFO,
		C.host_info_t(unsafe.Pointer(vmstat)),
		&count)

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

	return nil
}
Exemple #8
0
func HostStatisticsCpuLoadInfo() (*HostCpuLoadInfoData, error) {
	var (
		count   C.mach_msg_type_number_t = C.HOST_CPU_LOAD_INFO_COUNT
		cpuload C.host_cpu_load_info_data_t
	)

	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)
	}

	return (*HostCpuLoadInfoData)(&cpuload), nil
}
func (self *Cpu) Get() error {
	var count C.mach_msg_type_number_t = C.HOST_CPU_LOAD_INFO_COUNT
	var cpuload C.host_cpu_load_info_data_t

	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 fmt.Errorf("host_statistics error=%d", status)
	}

	self.User = uint64(cpuload.cpu_ticks[C.CPU_STATE_USER])
	self.Sys = uint64(cpuload.cpu_ticks[C.CPU_STATE_SYSTEM])
	self.Idle = uint64(cpuload.cpu_ticks[C.CPU_STATE_IDLE])
	self.Nice = uint64(cpuload.cpu_ticks[C.CPU_STATE_NICE])

	return nil
}
Exemple #10
0
// VirtualMemory returns VirtualmemoryStat.
func VirtualMemory() (*VirtualMemoryStat, error) {
	count := C.mach_msg_type_number_t(C.HOST_VM_INFO_COUNT)
	var vmstat C.vm_statistics_data_t

	status := C.host_statistics(C.host_t(C.mach_host_self()),
		C.HOST_VM_INFO,
		C.host_info_t(unsafe.Pointer(&vmstat)),
		&count)

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

	pageSize := uint64(syscall.Getpagesize())
	total, err := getHwMemsize()
	if err != nil {
		return nil, err
	}
	totalCount := C.natural_t(total / pageSize)

	availableCount := vmstat.inactive_count + vmstat.free_count
	usedPercent := 100 * float64(totalCount-availableCount) / float64(totalCount)

	usedCount := totalCount - availableCount

	return &VirtualMemoryStat{
		Total:       total,
		Available:   pageSize * uint64(availableCount),
		Used:        pageSize * uint64(usedCount),
		UsedPercent: usedPercent,
		Free:        pageSize * uint64(vmstat.free_count),
		Active:      pageSize * uint64(vmstat.active_count),
		Inactive:    pageSize * uint64(vmstat.inactive_count),
		Wired:       pageSize * uint64(vmstat.wire_count),
	}, nil
}
Exemple #11
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))
}