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

	var res []*DiskIOStats
	do(func() {
		var num_diskio_stats C.size_t
		var cArray *C.sg_disk_io_stats = C.sg_get_disk_io_stats_diff(&num_diskio_stats)
		length := int(num_diskio_stats)
		slice := (*[1 << 16]C.sg_disk_io_stats)(unsafe.Pointer(cArray))[:length:length]

		for _, v := range slice {
			f := &DiskIOStats{
				DiskName:   C.GoString(v.disk_name),
				ReadBytes:  int(v.read_bytes),
				WriteBytes: int(v.write_bytes),
				Period:     time.Duration(int(v.systime)) * time.Second,
				TimeTaken:  time.Now(),
			}
			res = append(res, f)
		}
	})

	return res

}
Ejemplo n.º 2
0
// GetDiskIoStatsDiff returns information about I/O transfers the last call for all disks, see sg_get_disk_io_stats_diff(3).
func GetDiskIoStatsDiff() ([]*DiskIoStats, error) {
	var ct C.int
	c := C.sg_get_disk_io_stats_diff(&ct)
	if c == nil {
		return nil, getError()
	}
	return asDiskIoStatsArray(c, int(ct)), nil
}
Ejemplo n.º 3
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) DiskIOStats() []*DiskIOStats {
	s.Lock()
	defer s.Unlock()

	var num_diskio_stats C.size_t
	var cArray *C.sg_disk_io_stats = C.sg_get_disk_io_stats_diff(&num_diskio_stats)
	length := int(num_diskio_stats)
	slice := (*[1 << 16]C.sg_disk_io_stats)(unsafe.Pointer(cArray))[:length:length]

	var res []*DiskIOStats

	for _, v := range slice {
		f := &DiskIOStats{
			DiskName:   C.GoString(v.disk_name),
			ReadBytes:  int(v.read_bytes),
			WriteBytes: int(v.write_bytes),
		}
		res = append(res, f)
	}
	return res

}
Ejemplo n.º 4
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
}