From 9bbff2a36b22c77ddfe3dd0a052eb23300f85f1a Mon Sep 17 00:00:00 2001 From: Will Jordan Date: Wed, 26 Mar 2025 15:40:25 -0700 Subject: [PATCH] add SysBlockDeviceQueueStat SysBlockDeviceQueueStat returns a single stat from /sys/block/xxx/queue where xxx is a device name and stat is a file. Allow fetching a single queue stat without parsing the entire queue sysfs structure. --- blockdevice/stats.go | 9 +++++++++ blockdevice/stats_test.go | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/blockdevice/stats.go b/blockdevice/stats.go index 277f2428..139975e0 100644 --- a/blockdevice/stats.go +++ b/blockdevice/stats.go @@ -442,6 +442,15 @@ func (fs FS) SysBlockDeviceQueueStats(device string) (BlockQueueStats, error) { return stat, nil } +// SysBlockDeviceQueueStat returns a single stat from /sys/block/xxx/queue where xxx is a device name and stat is a file. +func (fs FS) SysBlockDeviceQueueStat(device string, file string) (string, error) { + stat, err := util.SysReadFile(fs.sys.Path(sysBlockPath, device, sysBlockQueue, file)) + if err != nil { + return "", err + } + return stat, nil +} + func (fs FS) SysBlockDeviceMapperInfo(device string) (DeviceMapperInfo, error) { info := DeviceMapperInfo{} // Files with uint64 fields diff --git a/blockdevice/stats_test.go b/blockdevice/stats_test.go index 0bd6016b..95dd6c22 100644 --- a/blockdevice/stats_test.go +++ b/blockdevice/stats_test.go @@ -152,6 +152,13 @@ func TestBlockDevice(t *testing.T) { if !reflect.DeepEqual(blockQueueStat, blockQueueStatExpected) { t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", blockQueueStatExpected, blockQueueStat) } + rotational, err := blockdevice.SysBlockDeviceQueueStat(devices[7], "rotational") + if err != nil { + t.Fatal(err) + } + if rotational != "1" { + t.Errorf("Incorrect BlockQueueStat('rotational'), expected: \n%+v, got: \n%+v", 1, rotational) + } } func TestBlockDmInfo(t *testing.T) {