-
Notifications
You must be signed in to change notification settings - Fork 83
/
iostat.go
164 lines (135 loc) · 4.33 KB
/
iostat.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package nux
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"time"
"github.com/toolkits/file"
)
type DiskStats struct {
Major int
Minor int
Device string
ReadRequests uint64 // Total number of reads completed successfully.
ReadMerged uint64 // Adjacent read requests merged in a single req.
ReadSectors uint64 // Total number of sectors read successfully.
MsecRead uint64 // Total number of ms spent by all reads.
WriteRequests uint64 // total number of writes completed successfully.
WriteMerged uint64 // Adjacent write requests merged in a single req.
WriteSectors uint64 // total number of sectors written successfully.
MsecWrite uint64 // Total number of ms spent by all writes.
IosInProgress uint64 // Number of actual I/O requests currently in flight.
MsecTotal uint64 // Amount of time during which ios_in_progress >= 1.
MsecWeightedTotal uint64 // Measure of recent I/O completion time and backlog.
DiscardRequests uint64 // total number of discards completed successfully.
DiscardMerged uint64 // Adjacent discard requests merged in a single req.
DiscardSectors uint64 // total number of sectors discarded successfully.
MsecDiscard uint64 // Total number of ms spent by all discards.
TS time.Time
}
func (this *DiskStats) String() string {
return fmt.Sprintf("<Device:%s, Major:%d, Minor:%d, ReadRequests:%d...>", this.Device, this.Major, this.Minor, this.ReadRequests)
}
func ListDiskStats() ([]*DiskStats, error) {
proc_diskstats := Root() + "/proc/diskstats"
if !file.IsExist(proc_diskstats) {
return nil, fmt.Errorf("%s not exists", proc_diskstats)
}
contents, err := ioutil.ReadFile(proc_diskstats)
if err != nil {
return nil, err
}
ret := make([]*DiskStats, 0)
reader := bufio.NewReader(bytes.NewBuffer(contents))
for {
line, err := file.ReadLine(reader)
if err == io.EOF {
err = nil
break
} else if err != nil {
return nil, err
}
fields := strings.Fields(string(line))
// shortcut the deduper and just skip disks that
// haven't done a single read. This elimiates a bunch
// of loopback, ramdisk, and cdrom devices but still
// lets us report on the rare case that we actually use
// a ramdisk.
if fields[3] == "0" {
continue
}
size := len(fields)
// kernel version too low
if size < 14 {
continue
}
item := &DiskStats{}
if item.Major, err = strconv.Atoi(fields[0]); err != nil {
return nil, err
}
if item.Minor, err = strconv.Atoi(fields[1]); err != nil {
return nil, err
}
item.Device = fields[2]
if item.ReadRequests, err = strconv.ParseUint(fields[3], 10, 64); err != nil {
return nil, err
}
if item.ReadMerged, err = strconv.ParseUint(fields[4], 10, 64); err != nil {
return nil, err
}
if item.ReadSectors, err = strconv.ParseUint(fields[5], 10, 64); err != nil {
return nil, err
}
if item.MsecRead, err = strconv.ParseUint(fields[6], 10, 64); err != nil {
return nil, err
}
if item.WriteRequests, err = strconv.ParseUint(fields[7], 10, 64); err != nil {
return nil, err
}
if item.WriteMerged, err = strconv.ParseUint(fields[8], 10, 64); err != nil {
return nil, err
}
if item.WriteSectors, err = strconv.ParseUint(fields[9], 10, 64); err != nil {
return nil, err
}
if item.MsecWrite, err = strconv.ParseUint(fields[10], 10, 64); err != nil {
return nil, err
}
if item.IosInProgress, err = strconv.ParseUint(fields[11], 10, 64); err != nil {
return nil, err
}
if item.MsecTotal, err = strconv.ParseUint(fields[12], 10, 64); err != nil {
return nil, err
}
if item.MsecWeightedTotal, err = strconv.ParseUint(fields[13], 10, 64); err != nil {
return nil, err
}
if size > 14 {
if item.DiscardRequests, err = strconv.ParseUint(fields[14], 10, 64); err != nil {
return nil, err
}
}
if size > 15 {
if item.DiscardMerged, err = strconv.ParseUint(fields[15], 10, 64); err != nil {
return nil, err
}
}
if size > 16 {
if item.DiscardSectors, err = strconv.ParseUint(fields[16], 10, 64); err != nil {
return nil, err
}
}
if size > 17 {
if item.MsecDiscard, err = strconv.ParseUint(fields[17], 10, 64); err != nil {
return nil, err
}
}
item.TS = time.Now()
ret = append(ret, item)
}
return ret, nil
}