Пример #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()

	// 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
}
Пример #2
0
// GetCpuStatsDiff is like GetCpuStats, but only returns ticks since the last call, see sg_get_cpu_stats_diff(3).
func GetCpuStatsDiff() (*CpuStats, error) {
	c := C.sg_get_cpu_stats_diff()
	if c == nil {
		return nil, getError()
	}
	return &CpuStats{
		int64(c.user),
		int64(c.kernel),
		int64(c.idle),
		int64(c.iowait),
		int64(c.swap),
		int64(c.nice),
		int64(c.total),
		time.Duration(time.Duration(c.systime) * time.Second)}, nil
	// WARNING: modern unixes only; will horribly confuse your PDP.
}
Пример #3
0
// NewStat return a new Stat handle
func NewStat() *Stat {
	s := &Stat{}
	runtime.SetFinalizer(s, (*Stat).free)

	initDone := make(chan bool)
	s.exitMessage = make(chan bool)

	C.sg_init(1)

	go func() {
		// We need some function calls to be performed on the same thread
		// Those for which statgrab is using a thread local
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()

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

		initDone <- true

		for {
			select {
			case <-s.exitMessage:
				break
			case f := <-mainfunc:
				f()
			}
		}

	}()

	<-initDone

	return s
}