Esempio n. 1
0
// Gathers CPU statistics and fills the global cpu stat variable.
func gatherCpuInfo() {
	// Collect the current usage info
	if C.CollectCpuInfo() == 0 {
		panic(fmt.Errorf("faield to gther CPU usage info"))
	}
	// Extract the CPU counters
	user := int64(C.UserTime())
	kernel := int64(C.KernelTime())
	idle := int64(C.IdleTime())

	// Update the local and global state
	cpu.lock.Lock()
	defer cpu.lock.Unlock()

	prev := cpuWindows.user + cpuWindows.kernel - cpuWindows.idle
	busy := user + kernel - idle

	work := busy - prev
	wait := idle - cpuWindows.idle

	cpu.usage = float32(work) / float32(work+wait)
	cpuWindows.user = user
	cpuWindows.kernel = kernel
	cpuWindows.idle = idle
}
Esempio n. 2
0
// Gathers CPU statistics and fills the global cpu stat variable.
func gatherCpuInfo() {
	// Collect the current usage info
	if C.CollectCpuInfo() == 0 {
		panic(fmt.Errorf("faield to gther CPU usage info"))
	}
	// Extract the CPU counters
	total := int64(C.TotalTime())
	idle := int64(C.IdleTime())

	// Update the local and global state
	cpu.lock.Lock()
	defer cpu.lock.Unlock()

	prev := cpuDarwin.total + -cpuDarwin.idle
	busy := total - idle

	work := busy - prev
	wait := idle - cpuDarwin.idle

	cpu.usage = float32(work) / float32(work+wait)
	cpuDarwin.total = total
	cpuDarwin.idle = idle
}