Skip to content

Commit 7a44272

Browse files
author
Sergei Sementchouk
authored
Add flush request and time spend flushing diskstats for kernel 5.5+ (#289)
* add flush request and time spend flushing diskstats for kernel 5.5+ Signed-off-by: binjip978 <[email protected]> * add diskstats if at least 14 params are present Signed-off-by: binjip978 <[email protected]>
1 parent d1b8c1e commit 7a44272

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

blockdevice/stats.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,26 @@ type IOStats struct {
6969
DiscardSectors uint64
7070
// DiscardTicks is the total number of milliseconds spent by all discards.
7171
DiscardTicks uint64
72+
// FlushRequestsCompleted is the total number of flush request completed successfully.
73+
FlushRequestsCompleted uint64
74+
// TimeSpentFlushing is the total number of milliseconds spent flushing.
75+
TimeSpentFlushing uint64
7276
}
7377

7478
// Diskstats combines the device Info and IOStats
7579
type Diskstats struct {
7680
Info
7781
IOStats
78-
// IoStatsCount contains the number of io stats read. For kernel versions
79-
// 4.18+, there should be 18 fields read. For earlier kernel versions this
82+
// IoStatsCount contains the number of io stats read. For kernel versions 5.5+,
83+
// there should be 20 fields read. For kernel versions 4.18+,
84+
// there should be 18 fields read. For earlier kernel versions this
8085
// will be 14 because the discard values are not available.
8186
IoStatsCount int
8287
}
8388

8489
const (
8590
procDiskstatsPath = "diskstats"
86-
procDiskstatsFormat = "%d %d %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d"
91+
procDiskstatsFormat = "%d %d %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d"
8792
sysBlockPath = "block"
8893
sysBlockStatFormat = "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d"
8994
)
@@ -153,13 +158,15 @@ func (fs FS) ProcDiskstats() ([]Diskstats, error) {
153158
&d.DiscardMerges,
154159
&d.DiscardSectors,
155160
&d.DiscardTicks,
161+
&d.FlushRequestsCompleted,
162+
&d.TimeSpentFlushing,
156163
)
157164
// The io.EOF error can be safely ignored because it just means we read fewer than
158-
// the full 18 fields.
165+
// the full 20 fields.
159166
if err != nil && err != io.EOF {
160167
return diskstats, err
161168
}
162-
if d.IoStatsCount == 14 || d.IoStatsCount == 18 {
169+
if d.IoStatsCount >= 14 {
163170
diskstats = append(diskstats, *d)
164171
}
165172
}

blockdevice/stats_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestDiskstats(t *testing.T) {
3232
if err != nil {
3333
t.Fatal(err)
3434
}
35-
expectedNumOfDevices := 49
35+
expectedNumOfDevices := 52
3636
if len(diskstats) != expectedNumOfDevices {
3737
t.Errorf(failMsgFormat, "Incorrect number of devices", expectedNumOfDevices, len(diskstats))
3838
}
@@ -51,6 +51,15 @@ func TestDiskstats(t *testing.T) {
5151
if diskstats[48].IoStatsCount != 18 {
5252
t.Errorf(failMsgFormat, "Incorrect number of stats read", 18, diskstats[48].IoStatsCount)
5353
}
54+
if diskstats[49].IoStatsCount != 20 {
55+
t.Errorf(failMsgFormat, "Incorrect number of stats read", 20, diskstats[50].IoStatsCount)
56+
}
57+
if diskstats[49].FlushRequestsCompleted != 127 {
58+
t.Errorf(failMsgFormat, "Incorrect number of flash requests completed", 127, diskstats[50].FlushRequestsCompleted)
59+
}
60+
if diskstats[49].TimeSpentFlushing != 182 {
61+
t.Errorf(failMsgFormat, "Incorrect time spend flushing", 182, diskstats[50].TimeSpentFlushing)
62+
}
5463
}
5564

5665
func TestBlockDevice(t *testing.T) {

fixtures.ttar

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ max keysize : 32
18411841
Mode: 444
18421842
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18431843
Path: fixtures/proc/diskstats
1844-
Lines: 49
1844+
Lines: 52
18451845
1 0 ram0 0 0 0 0 0 0 0 0 0 0 0
18461846
1 1 ram1 0 0 0 0 0 0 0 0 0 0 0
18471847
1 2 ram2 0 0 0 0 0 0 0 0 0 0 0
@@ -1891,6 +1891,9 @@ Lines: 49
18911891
8 0 sdb 326552 841 9657779 84 41822 2895 1972905 5007 0 60730 67070 68851 0 1925173784 11130
18921892
8 1 sdb1 231 3 34466 4 24 23 106 0 0 64 64 0 0 0 0
18931893
8 2 sdb2 326310 838 9622281 67 40726 2872 1972799 4924 0 58250 64567 68851 0 1925173784 11130
1894+
8 0 sdc 14202 71 579164 21861 2995 1589 180500 40875 0 11628 55200 0 0 0 0 127 182
1895+
8 1 sdc1 1027 0 13795 5021 2 0 4096 3 0 690 4579 0 0 0 0 0 0
1896+
8 2 sdc2 13126 71 561749 16802 2830 1589 176404 40620 0 10931 50449 0 0 0 0 0 0
18941897
Mode: 664
18951898
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18961899
Directory: fixtures/proc/fs

0 commit comments

Comments
 (0)