Skip to content

Add support for Linux flushes and discards in IOCountersStat #1307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ type PartitionStat struct {
}

type IOCountersStat struct {
ReadCount uint64 `json:"readCount"`
MergedReadCount uint64 `json:"mergedReadCount"`
WriteCount uint64 `json:"writeCount"`
MergedWriteCount uint64 `json:"mergedWriteCount"`
ReadBytes uint64 `json:"readBytes"`
WriteBytes uint64 `json:"writeBytes"`
ReadTime uint64 `json:"readTime"`
WriteTime uint64 `json:"writeTime"`
IopsInProgress uint64 `json:"iopsInProgress"`
IoTime uint64 `json:"ioTime"`
WeightedIO uint64 `json:"weightedIO"`
Name string `json:"name"`
SerialNumber string `json:"serialNumber"`
Label string `json:"label"`
ReadCount uint64 `json:"readCount"`
MergedReadCount uint64 `json:"mergedReadCount"`
WriteCount uint64 `json:"writeCount"`
MergedWriteCount uint64 `json:"mergedWriteCount"`
ReadBytes uint64 `json:"readBytes"`
WriteBytes uint64 `json:"writeBytes"`
ReadTime uint64 `json:"readTime"`
WriteTime uint64 `json:"writeTime"`
IopsInProgress uint64 `json:"iopsInProgress"`
IoTime uint64 `json:"ioTime"`
WeightedIO uint64 `json:"weightedIO"`
DiscardCount uint64 `json:"discardCount"`
MergedDiscardCount uint64 `json:"mergedDiscardCount"`
DiscardBytes uint64 `json:"discardBytes"`
DiscardTime uint64 `json:"discardTime"`
FlushCount uint64 `json:"flushCount"`
FlushTime uint64 `json:"flushTime"`
Name string `json:"name"`
SerialNumber string `json:"serialNumber"`
Label string `json:"label"`
}

func (d UsageStat) String() string {
Expand Down
62 changes: 51 additions & 11 deletions disk/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,58 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
if err != nil {
return ret, err
}
discards := uint64(0)
mergedDiscards := uint64(0)
dbytes := uint64(0)
dtime := uint64(0)
if len(fields) >= 18 {
discards, err = strconv.ParseUint((fields[14]), 10, 64)
if err != nil {
return ret, err
}
mergedDiscards, err = strconv.ParseUint((fields[15]), 10, 64)
if err != nil {
return ret, err
}
dbytes, err = strconv.ParseUint((fields[16]), 10, 64)
if err != nil {
return ret, err
}
dtime, err = strconv.ParseUint((fields[17]), 10, 64)
if err != nil {
return ret, err
}
}
flushes := uint64(0)
ftime := uint64(0)
if len(fields) >= 20 {
flushes, err = strconv.ParseUint((fields[18]), 10, 64)
if err != nil {
return ret, err
}
ftime, err = strconv.ParseUint((fields[19]), 10, 64)
if err != nil {
return ret, err
}
}
d := IOCountersStat{
ReadBytes: rbytes * sectorSize,
WriteBytes: wbytes * sectorSize,
ReadCount: reads,
WriteCount: writes,
MergedReadCount: mergedReads,
MergedWriteCount: mergedWrites,
ReadTime: rtime,
WriteTime: wtime,
IopsInProgress: iopsInProgress,
IoTime: iotime,
WeightedIO: weightedIO,
ReadBytes: rbytes * sectorSize,
WriteBytes: wbytes * sectorSize,
ReadCount: reads,
WriteCount: writes,
MergedReadCount: mergedReads,
MergedWriteCount: mergedWrites,
ReadTime: rtime,
WriteTime: wtime,
IopsInProgress: iopsInProgress,
IoTime: iotime,
WeightedIO: weightedIO,
DiscardCount: discards,
MergedDiscardCount: mergedDiscards,
DiscardBytes: dbytes * sectorSize,
DiscardTime: dtime,
FlushCount: flushes,
FlushTime: ftime,
}
if d == empty {
continue
Expand Down
122 changes: 122 additions & 0 deletions disk/disk_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//go:build linux
// +build linux

package disk

import (
"os"
"testing"
)

func TestDiskStatsPost55(t *testing.T) {
orig := os.Getenv("HOST_PROC")
defer os.Setenv("HOST_PROC", orig)

os.Setenv("HOST_PROC", "testdata/linux/diskstats-post5.5/proc")
ios, err := IOCounters("sda", "sdc")
if err != nil {
t.Error("IOCounters failed")
}

expected := IOCountersStat{
ReadCount: 256838102,
MergedReadCount: 620512,
ReadBytes: 5028599594 * sectorSize,
ReadTime: 226563271,
WriteCount: 418236058,
MergedWriteCount: 7573415,
WriteBytes: 8577305933 * sectorSize,
WriteTime: 171833267,
IopsInProgress: 0,
IoTime: 141604084,
WeightedIO: 402232601,
DiscardCount: 168817,
MergedDiscardCount: 110,
DiscardBytes: 4991981424 * sectorSize,
DiscardTime: 387582,
FlushCount: 983197,
FlushTime: 3448479,
Name: "sdc",
SerialNumber: "",
Label: "",
}
if ios["sdc"] != expected {
t.Logf("IOCounterStats gave wrong results: expected %v actual %v", expected, ios["sdc"])
t.Error("IOCounterStats gave wrong results")
}
}

func TestDiskStatsPre55(t *testing.T) {
orig := os.Getenv("HOST_PROC")
os.Setenv("HOST_PROC", "testdata/linux/diskstats-pre5.5/proc")
defer os.Setenv("HOST_PROC", orig)

ios, err := IOCounters("sda", "sdc")
if err != nil {
t.Error("IOCounters failed")
}
expected := IOCountersStat{
ReadCount: 256838102,
MergedReadCount: 620512,
ReadBytes: 5028599594 * sectorSize,
ReadTime: 226563271,
WriteCount: 418236058,
MergedWriteCount: 7573415,
WriteBytes: 8577305933 * sectorSize,
WriteTime: 171833267,
IopsInProgress: 0,
IoTime: 141604084,
WeightedIO: 402232601,
DiscardCount: 168817,
MergedDiscardCount: 110,
DiscardBytes: 4991981424 * sectorSize,
DiscardTime: 387582,
FlushCount: 0,
FlushTime: 0,
Name: "sdc",
SerialNumber: "",
Label: "",
}
if ios["sdc"] != expected {
t.Logf("IOCounterStats gave wrong results: expected %v actual %v", expected, ios)
t.Error("IOCounterStats gave wrong results")
}

}

func TestDiskStatsPre418(t *testing.T) {
orig := os.Getenv("HOST_PROC")
defer os.Setenv("HOST_PROC", orig)

os.Setenv("HOST_PROC", "testdata/linux/diskstats-pre4.18/proc")
ios, err := IOCounters("sda", "sdc")
if err != nil {
t.Error("IOCounters failed")
}
expected := IOCountersStat{
ReadCount: 256838102,
MergedReadCount: 620512,
ReadBytes: 5028599594 * sectorSize,
ReadTime: 226563271,
WriteCount: 418236058,
MergedWriteCount: 7573415,
WriteBytes: 8577305933 * sectorSize,
WriteTime: 171833267,
IopsInProgress: 0,
IoTime: 141604084,
WeightedIO: 402232601,
DiscardCount: 0,
MergedDiscardCount: 0,
DiscardBytes: 0,
DiscardTime: 0,
FlushCount: 0,
FlushTime: 0,
Name: "sdc",
SerialNumber: "",
Label: "",
}
if ios["sdc"] != expected {
t.Logf("IOCounterStats gave wrong results: expected %v actual %v", expected, ios["sdc"])
t.Error("IOCounterStats gave wrong results")
}
}
4 changes: 3 additions & 1 deletion disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ func TestDiskIOCountersStat_String(t *testing.T) {
WriteCount: 200,
ReadBytes: 300,
WriteBytes: 400,
DiscardCount: 500,
FlushCount: 600,
SerialNumber: "SERIAL",
}
e := `{"readCount":100,"mergedReadCount":0,"writeCount":200,"mergedWriteCount":0,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"sd01","serialNumber":"SERIAL","label":""}`
e := `{"readCount":100,"mergedReadCount":0,"writeCount":200,"mergedWriteCount":0,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"discardCount":500,"mergedDiscardCount":0,"discardBytes":0,"discardTime":0,"flushCount":600,"flushTime":0,"name":"sd01","serialNumber":"SERIAL","label":""}`
if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v)
}
Expand Down
4 changes: 4 additions & 0 deletions disk/testdata/linux/diskstats-post5.5/proc/diskstats
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
8 0 sda 251490562 587923 4907991349 207940386 418695352 7259232 8579533525 94584901 0 140322864 306361336 169975 208 4992138504 371249 984400 3464799
8 1 sda1 251488338 587923 4907983293 207938421 418695352 7259232 8579533525 94584901 0 140322088 302894572 169975 208 4992138504 371249 0 0
8 32 sdc 256838102 620512 5028599594 226563271 418236058 7573415 8577305933 171833267 0 141604084 402232601 168817 110 4991981424 387582 983197 3448479
8 33 sdc1 256835879 620512 5028591546 226561154 418236058 7573415 8577305933 171833267 0 141603284 398782004 168817 110 4991981424 387582 0 0
4 changes: 4 additions & 0 deletions disk/testdata/linux/diskstats-pre4.18/diskstats
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
8 0 sda 251490562 587923 4907991349 207940386 418695352 7259232 8579533525 94584901 0 140322864 306361336
8 1 sda1 251488338 587923 4907983293 207938421 418695352 7259232 8579533525 94584901 0 140322088 302894572
8 32 sdc 256838102 620512 5028599594 226563271 418236058 7573415 8577305933 171833267 0 141604084 402232601
8 33 sdc1 256835879 620512 5028591546 226561154 418236058 7573415 8577305933 171833267 0 141603284 398782004
4 changes: 4 additions & 0 deletions disk/testdata/linux/diskstats-pre4.18/proc/diskstats
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
8 0 sda 251490562 587923 4907991349 207940386 418695352 7259232 8579533525 94584901 0 140322864 306361336
8 1 sda1 251488338 587923 4907983293 207938421 418695352 7259232 8579533525 94584901 0 140322088 302894572
8 32 sdc 256838102 620512 5028599594 226563271 418236058 7573415 8577305933 171833267 0 141604084 402232601
8 33 sdc1 256835879 620512 5028591546 226561154 418236058 7573415 8577305933 171833267 0 141603284 398782004
4 changes: 4 additions & 0 deletions disk/testdata/linux/diskstats-pre5.5/proc/diskstats
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
8 0 sda 251490562 587923 4907991349 207940386 418695352 7259232 8579533525 94584901 0 140322864 306361336 169975 208 4992138504 371249
8 1 sda1 251488338 587923 4907983293 207938421 418695352 7259232 8579533525 94584901 0 140322088 302894572 169975 208 4992138504 371249
8 32 sdc 256838102 620512 5028599594 226563271 418236058 7573415 8577305933 171833267 0 141604084 402232601 168817 110 4991981424 387582
8 33 sdc1 256835879 620512 5028591546 226561154 418236058 7573415 8577305933 171833267 0 141603284 398782004 168817 110 4991981424 387582