From aea532f280201311558abb8b561ecdecc91fd754 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Wed, 3 Jul 2024 08:59:11 +0000 Subject: [PATCH 1/6] added sysblockdevicesize method and test --- blockdevice/stats.go | 11 +++++++++++ blockdevice/stats_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/blockdevice/stats.go b/blockdevice/stats.go index 22533002d..73d5dffe9 100644 --- a/blockdevice/stats.go +++ b/blockdevice/stats.go @@ -209,6 +209,7 @@ const ( sysBlockQueue = "queue" sysBlockDM = "dm" sysUnderlyingDev = "slaves" + sysBlockSize = "size" ) // FS represents the pseudo-filesystems proc and sys, which provides an @@ -474,3 +475,13 @@ func (fs FS) SysBlockDeviceUnderlyingDevices(device string) (UnderlyingDeviceInf return UnderlyingDeviceInfo{DeviceNames: underlying}, nil } + +// SysBlockDeviceSize returns the size of the block device from /sys/block//size. +// Note that this needs to be multiplied by LogicalBlockSize to get the size in bytes. +func (fs FS) SysBlockDeviceSize(device string) (uint64, error) { + size, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize)) + if err != nil { + return 0, err + } + return size, nil +} diff --git a/blockdevice/stats_test.go b/blockdevice/stats_test.go index c065c2944..dce07fe5e 100644 --- a/blockdevice/stats_test.go +++ b/blockdevice/stats_test.go @@ -219,3 +219,22 @@ func TestSysBlockDeviceUnderlyingDevices(t *testing.T) { t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", underlying0Expected, underlying0) } } + +func TestSysBlockDeviceSize(t *testing.T) { + blockdevice, err := NewFS("testdata/fixtures/proc", "testdata/fixtures/sys") + if err != nil { + t.Fatalf("failed to access blockdevice fs: %v", err) + } + devices, err := blockdevice.SysBlockDevices() + if err != nil { + t.Fatal(err) + } + size7, err := blockdevice.SysBlockDeviceSize(devices[7]) + if err != nil { + t.Fatal(err) + } + size7Expected := uint64(3750748848) + if size7 != size7Expected { + t.Errorf("Incorrect BlockDeviceSize, expected: \n%+v, got: \n%+v", size7Expected, size7) + } +} From 3ea9799b074a2ea8b440079416d822365926d8ca Mon Sep 17 00:00:00 2001 From: fs185143 Date: Wed, 3 Jul 2024 09:19:45 +0000 Subject: [PATCH 2/6] simplified sysblockdevicesize method --- blockdevice/stats.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/blockdevice/stats.go b/blockdevice/stats.go index 73d5dffe9..010d1decf 100644 --- a/blockdevice/stats.go +++ b/blockdevice/stats.go @@ -479,9 +479,5 @@ func (fs FS) SysBlockDeviceUnderlyingDevices(device string) (UnderlyingDeviceInf // SysBlockDeviceSize returns the size of the block device from /sys/block//size. // Note that this needs to be multiplied by LogicalBlockSize to get the size in bytes. func (fs FS) SysBlockDeviceSize(device string) (uint64, error) { - size, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize)) - if err != nil { - return 0, err - } - return size, nil + return util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize)) } From 6d2ee3360a0a31780a1d4ac0990eb1b1cdb9c262 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 9 Jul 2024 12:20:40 +0000 Subject: [PATCH 3/6] swapped SysBlockDeviceSize to SysBlockDeviceSizeBytes --- blockdevice/stats.go | 12 ++++++++---- blockdevice/stats_test.go | 8 ++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/blockdevice/stats.go b/blockdevice/stats.go index 010d1decf..af3eab5bf 100644 --- a/blockdevice/stats.go +++ b/blockdevice/stats.go @@ -476,8 +476,12 @@ func (fs FS) SysBlockDeviceUnderlyingDevices(device string) (UnderlyingDeviceInf } -// SysBlockDeviceSize returns the size of the block device from /sys/block//size. -// Note that this needs to be multiplied by LogicalBlockSize to get the size in bytes. -func (fs FS) SysBlockDeviceSize(device string) (uint64, error) { - return util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize)) +// SysBlockDeviceSizeBytes returns the size of the block device from /sys/block//size +// in bytes by multiplying the value by the Linux sector length of 512. +func (fs FS) SysBlockDeviceSizeBytes(device string) (uint64, error) { + size, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize)) + if err != nil { + return 0, err + } + return 512 * size, nil } diff --git a/blockdevice/stats_test.go b/blockdevice/stats_test.go index dce07fe5e..1d04d2086 100644 --- a/blockdevice/stats_test.go +++ b/blockdevice/stats_test.go @@ -229,12 +229,12 @@ func TestSysBlockDeviceSize(t *testing.T) { if err != nil { t.Fatal(err) } - size7, err := blockdevice.SysBlockDeviceSize(devices[7]) + sizeBytes7, err := blockdevice.SysBlockDeviceSizeBytes(devices[7]) if err != nil { t.Fatal(err) } - size7Expected := uint64(3750748848) - if size7 != size7Expected { - t.Errorf("Incorrect BlockDeviceSize, expected: \n%+v, got: \n%+v", size7Expected, size7) + sizeBytes7Expected := uint64(3750748848) + if sizeBytes7 != sizeBytes7Expected { + t.Errorf("Incorrect BlockDeviceSize, expected: \n%+v, got: \n%+v", sizeBytes7Expected, sizeBytes7) } } From 8dc60f1a18569b30c70a335511368658435c5e49 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 9 Jul 2024 12:24:42 +0000 Subject: [PATCH 4/6] added testdata size file --- testdata/fixtures/sys/block/sda/size | 1 + 1 file changed, 1 insertion(+) create mode 100644 testdata/fixtures/sys/block/sda/size diff --git a/testdata/fixtures/sys/block/sda/size b/testdata/fixtures/sys/block/sda/size new file mode 100644 index 000000000..117c151bb --- /dev/null +++ b/testdata/fixtures/sys/block/sda/size @@ -0,0 +1 @@ +3750748848 \ No newline at end of file From 1e0b4b89bbe0393352d3aabc257798c304ae018a Mon Sep 17 00:00:00 2001 From: fs185143 Date: Wed, 14 Aug 2024 13:50:00 +0000 Subject: [PATCH 5/6] added fixture correctly --- testdata/fixtures.ttar | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/testdata/fixtures.ttar b/testdata/fixtures.ttar index 4bc508b8d..350a4aa24 100644 --- a/testdata/fixtures.ttar +++ b/testdata/fixtures.ttar @@ -4335,6 +4335,11 @@ Lines: 1 none Mode: 444 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/block/sda/size +Lines: 1 +3750748848EOF +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: fixtures/sys/block/sda/stat Lines: 1 9652963 396792 759304206 412943 8422549 6731723 286915323 13947418 0 5658367 19174573 1 2 3 12 From b0b6389726f550d383fe2ddb183484fda8921921 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Mon, 15 Jul 2024 11:19:18 +0100 Subject: [PATCH 6/6] add const sectorSize --- blockdevice/stats.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blockdevice/stats.go b/blockdevice/stats.go index af3eab5bf..087137a0a 100644 --- a/blockdevice/stats.go +++ b/blockdevice/stats.go @@ -210,6 +210,7 @@ const ( sysBlockDM = "dm" sysUnderlyingDev = "slaves" sysBlockSize = "size" + sectorSize = 512 ) // FS represents the pseudo-filesystems proc and sys, which provides an @@ -483,5 +484,5 @@ func (fs FS) SysBlockDeviceSizeBytes(device string) (uint64, error) { if err != nil { return 0, err } - return 512 * size, nil + return sectorSize * size, nil }