forked from akhenakh/statgo
/
diskio_stats.go
52 lines (44 loc) · 1.03 KB
/
diskio_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package statgo
// #cgo LDFLAGS: -lstatgrab
// #include <statgrab.h>
import "C"
import (
"fmt"
"unsafe"
)
// DiskIOStats contains disk io stats
type DiskIOStats struct {
DiskName string
ReadBytes int
WriteBytes int
}
// 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
}
func (d *DiskIOStats) String() string {
return fmt.Sprintf(
"DiskName:\t%s\n"+
"ReadBytes:\t%d\n"+
"WriteBytes:\t%d\n",
d.DiskName,
d.ReadBytes,
d.WriteBytes)
}