Esempio n. 1
0
// CPUStats get cpu related stats
// note that 1st call to 100ms may return NaN as values
// Go equivalent to sg_cpu_percents
func (s *Stat) CPUStats() *CPUStats {
	s.Lock()
	defer s.Unlock()

	var cpu *CPUStats
	do(func() {

		cpu_percent := C.sg_get_cpu_percents_of(C.sg_new_diff_cpu_percent, nil)

		load_stat := C.sg_get_load_stats(nil)

		cpu = &CPUStats{
			User:      float64(cpu_percent.user),
			Kernel:    float64(cpu_percent.kernel),
			Idle:      float64(cpu_percent.idle),
			IOWait:    float64(cpu_percent.iowait),
			Swap:      float64(cpu_percent.swap),
			Nice:      float64(cpu_percent.nice),
			LoadMin1:  float64(load_stat.min1),
			LoadMin5:  float64(load_stat.min5),
			LoadMin15: float64(load_stat.min15),
			Period:    time.Duration(int(cpu_percent.time_taken)) * time.Second,
			TimeTaken: time.Now(),
		}
	})
	return cpu
}
Esempio n. 2
0
// CPUStats get cpu related stats
// note that 1st call to 100ms may return NaN as values
// Go equivalent to sg_cpu_percents
func (s *Stat) CPUStats() *CPUStats {
	s.Lock()
	defer s.Unlock()

	// Throw away the first reading as thats averaged over the machines uptime
	C.sg_snapshot()
	C.sg_get_cpu_stats_diff(nil)

	cpu_percent := C.sg_get_cpu_percents_of(C.sg_last_diff_cpu_percent, nil)

	C.sg_snapshot()

	load_stat := C.sg_get_load_stats(nil)

	cpu := &CPUStats{
		User:      float64(cpu_percent.user),
		Kernel:    float64(cpu_percent.kernel),
		Idle:      float64(cpu_percent.idle),
		IOWait:    float64(cpu_percent.iowait),
		Swap:      float64(cpu_percent.swap),
		Nice:      float64(cpu_percent.nice),
		LoadMin1:  float64(load_stat.min1),
		LoadMin5:  float64(load_stat.min5),
		LoadMin15: float64(load_stat.min15),
		//TODO: timetaken
	}
	return cpu
}