From 7a44272e0d68eea081a6f7e08430150a286eb734 Mon Sep 17 00:00:00 2001 From: Sergei Sementchouk Date: Tue, 12 May 2020 12:05:25 +0300 Subject: [PATCH] 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 * add diskstats if at least 14 params are present Signed-off-by: binjip978 --- blockdevice/stats.go | 17 ++++++++++++----- blockdevice/stats_test.go | 11 ++++++++++- fixtures.ttar | 5 ++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/blockdevice/stats.go b/blockdevice/stats.go index c8735cc5c..1381d6c58 100644 --- a/blockdevice/stats.go +++ b/blockdevice/stats.go @@ -69,21 +69,26 @@ type IOStats struct { DiscardSectors uint64 // DiscardTicks is the total number of milliseconds spent by all discards. DiscardTicks uint64 + // FlushRequestsCompleted is the total number of flush request completed successfully. + FlushRequestsCompleted uint64 + // TimeSpentFlushing is the total number of milliseconds spent flushing. + TimeSpentFlushing uint64 } // Diskstats combines the device Info and IOStats type Diskstats struct { Info IOStats - // IoStatsCount contains the number of io stats read. For kernel versions - // 4.18+, there should be 18 fields read. For earlier kernel versions this + // IoStatsCount contains the number of io stats read. For kernel versions 5.5+, + // there should be 20 fields read. For kernel versions 4.18+, + // there should be 18 fields read. For earlier kernel versions this // will be 14 because the discard values are not available. IoStatsCount int } const ( procDiskstatsPath = "diskstats" - procDiskstatsFormat = "%d %d %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" + procDiskstatsFormat = "%d %d %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" sysBlockPath = "block" sysBlockStatFormat = "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" ) @@ -153,13 +158,15 @@ func (fs FS) ProcDiskstats() ([]Diskstats, error) { &d.DiscardMerges, &d.DiscardSectors, &d.DiscardTicks, + &d.FlushRequestsCompleted, + &d.TimeSpentFlushing, ) // The io.EOF error can be safely ignored because it just means we read fewer than - // the full 18 fields. + // the full 20 fields. if err != nil && err != io.EOF { return diskstats, err } - if d.IoStatsCount == 14 || d.IoStatsCount == 18 { + if d.IoStatsCount >= 14 { diskstats = append(diskstats, *d) } } diff --git a/blockdevice/stats_test.go b/blockdevice/stats_test.go index 2fea2c872..d90b66804 100644 --- a/blockdevice/stats_test.go +++ b/blockdevice/stats_test.go @@ -32,7 +32,7 @@ func TestDiskstats(t *testing.T) { if err != nil { t.Fatal(err) } - expectedNumOfDevices := 49 + expectedNumOfDevices := 52 if len(diskstats) != expectedNumOfDevices { t.Errorf(failMsgFormat, "Incorrect number of devices", expectedNumOfDevices, len(diskstats)) } @@ -51,6 +51,15 @@ func TestDiskstats(t *testing.T) { if diskstats[48].IoStatsCount != 18 { t.Errorf(failMsgFormat, "Incorrect number of stats read", 18, diskstats[48].IoStatsCount) } + if diskstats[49].IoStatsCount != 20 { + t.Errorf(failMsgFormat, "Incorrect number of stats read", 20, diskstats[50].IoStatsCount) + } + if diskstats[49].FlushRequestsCompleted != 127 { + t.Errorf(failMsgFormat, "Incorrect number of flash requests completed", 127, diskstats[50].FlushRequestsCompleted) + } + if diskstats[49].TimeSpentFlushing != 182 { + t.Errorf(failMsgFormat, "Incorrect time spend flushing", 182, diskstats[50].TimeSpentFlushing) + } } func TestBlockDevice(t *testing.T) { diff --git a/fixtures.ttar b/fixtures.ttar index eb8841134..855587160 100644 --- a/fixtures.ttar +++ b/fixtures.ttar @@ -1841,7 +1841,7 @@ max keysize : 32 Mode: 444 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: fixtures/proc/diskstats -Lines: 49 +Lines: 52 1 0 ram0 0 0 0 0 0 0 0 0 0 0 0 1 1 ram1 0 0 0 0 0 0 0 0 0 0 0 1 2 ram2 0 0 0 0 0 0 0 0 0 0 0 @@ -1891,6 +1891,9 @@ Lines: 49 8 0 sdb 326552 841 9657779 84 41822 2895 1972905 5007 0 60730 67070 68851 0 1925173784 11130 8 1 sdb1 231 3 34466 4 24 23 106 0 0 64 64 0 0 0 0 8 2 sdb2 326310 838 9622281 67 40726 2872 1972799 4924 0 58250 64567 68851 0 1925173784 11130 + 8 0 sdc 14202 71 579164 21861 2995 1589 180500 40875 0 11628 55200 0 0 0 0 127 182 + 8 1 sdc1 1027 0 13795 5021 2 0 4096 3 0 690 4579 0 0 0 0 0 0 + 8 2 sdc2 13126 71 561749 16802 2830 1589 176404 40620 0 10931 50449 0 0 0 0 0 0 Mode: 664 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: fixtures/proc/fs