Skip to content
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

feat(blockdevice): added sysblockdevicesize method and test #650

Closed
wants to merge 6 commits into from
Closed
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
12 changes: 12 additions & 0 deletions blockdevice/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ const (
sysBlockQueue = "queue"
sysBlockDM = "dm"
sysUnderlyingDev = "slaves"
sysBlockSize = "size"
sectorSize = 512
)

// FS represents the pseudo-filesystems proc and sys, which provides an
Expand Down Expand Up @@ -474,3 +476,13 @@ func (fs FS) SysBlockDeviceUnderlyingDevices(device string) (UnderlyingDeviceInf
return UnderlyingDeviceInfo{DeviceNames: underlying}, nil

}

// SysBlockDeviceSizeBytes returns the size of the block device from /sys/block/<device>/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 sectorSize * size, nil
}
19 changes: 19 additions & 0 deletions blockdevice/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
sizeBytes7, err := blockdevice.SysBlockDeviceSizeBytes(devices[7])
if err != nil {
t.Fatal(err)
}
sizeBytes7Expected := uint64(3750748848)
if sizeBytes7 != sizeBytes7Expected {
t.Errorf("Incorrect BlockDeviceSize, expected: \n%+v, got: \n%+v", sizeBytes7Expected, sizeBytes7)
}
}
5 changes: 5 additions & 0 deletions testdata/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions testdata/fixtures/sys/block/sda/size
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3750748848