-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdiskio_stats.go
67 lines (57 loc) · 1.34 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package statgo
// #cgo LDFLAGS: -lstatgrab
// #include <statgrab.h>
import "C"
import (
"fmt"
"time"
"unsafe"
)
// DiskIOStats contains disk io stats
// Expressed in bytes
type DiskIOStats struct {
DiskName string
ReadBytes int
WriteBytes int
// The time period over which read_bytes and write_bytes were transferred.
Period time.Duration
TimeTaken time.Time
}
// DiskIOStats get I/Os 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 stSize C.size_t
var cArray *C.sg_disk_io_stats = C.sg_get_disk_io_stats_diff(&stSize)
length := int(stSize)
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
}
func (d *DiskIOStats) String() string {
return fmt.Sprintf(
"DiskName:\t%s\n"+
"ReadBytes:\t%d\n"+
"WriteBytes:\t%d\n"+
"Period:\t%v\n"+
"TimeTaken:\t%s\n",
d.DiskName,
d.ReadBytes,
d.WriteBytes,
d.Period,
d.TimeTaken)
}