Skip to content

Commit 969849f

Browse files
authored
feat(blockdevice): added sysblockdevicesize method and test (#658)
* added blockdevice size method + test + data --------- Signed-off-by: fs185143 <fs185143@ncr.com> Signed-off-by: Finn Snape <145353420+fs185143@users.noreply.github.com>
1 parent 9c0d20a commit 969849f

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

blockdevice/stats.go

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"strings"
2323

24+
"github.com/prometheus/procfs"
2425
"github.com/prometheus/procfs/internal/fs"
2526
"github.com/prometheus/procfs/internal/util"
2627
)
@@ -214,6 +215,7 @@ const (
214215
sysBlockQueue = "queue"
215216
sysBlockDM = "dm"
216217
sysUnderlyingDev = "slaves"
218+
sysBlockSize = "size"
217219
sysDevicePath = "device"
218220
)
219221

@@ -481,6 +483,16 @@ func (fs FS) SysBlockDeviceUnderlyingDevices(device string) (UnderlyingDeviceInf
481483

482484
}
483485

486+
// SysBlockDeviceSize returns the size of the block device from /sys/block/<device>/size.
487+
// in bytes by multiplying the value by the Linux sector length of 512.
488+
func (fs FS) SysBlockDeviceSize(device string) (uint64, error) {
489+
size, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize))
490+
if err != nil {
491+
return 0, err
492+
}
493+
return procfs.SectorSize * size, nil
494+
}
495+
484496
// SysBlockDeviceIO returns stats for the block device io counters
485497
// IO done count: /sys/block/<disk>/device/iodone_cnt
486498
// IO error count: /sys/block/<disk>/device/ioerr_cnt.

blockdevice/stats_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,22 @@ func TestSysBlockDeviceUnderlyingDevices(t *testing.T) {
219219
t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", underlying0Expected, underlying0)
220220
}
221221
}
222+
223+
func TestSysBlockDeviceSize(t *testing.T) {
224+
blockdevice, err := NewFS("testdata/fixtures/proc", "testdata/fixtures/sys")
225+
if err != nil {
226+
t.Fatalf("failed to access blockdevice fs: %v", err)
227+
}
228+
devices, err := blockdevice.SysBlockDevices()
229+
if err != nil {
230+
t.Fatal(err)
231+
}
232+
size7, err := blockdevice.SysBlockDeviceSize(devices[7])
233+
if err != nil {
234+
t.Fatal(err)
235+
}
236+
size7Expected := uint64(1920383410176)
237+
if size7 != size7Expected {
238+
t.Errorf("Incorrect BlockDeviceSize, expected: \n%+v, got: \n%+v", size7Expected, size7)
239+
}
240+
}

btrfs/get.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ import (
2222
"strconv"
2323
"strings"
2424

25+
"github.com/prometheus/procfs"
2526
"github.com/prometheus/procfs/internal/fs"
2627
"github.com/prometheus/procfs/internal/util"
2728
)
2829

29-
// SectorSize contains the Linux sector size.
30-
// > Linux always considers sectors to be 512 bytes long independently
31-
// > of the devices real block size.
32-
const SectorSize = 512
33-
3430
// FS represents the pseudo-filesystem sys, which provides an interface to
3531
// kernel data structures.
3632
type FS struct {
@@ -213,7 +209,7 @@ func (r *reader) readDeviceInfo(d string) map[string]*Device {
213209
info := make(map[string]*Device, len(devs))
214210
for _, n := range devs {
215211
info[n] = &Device{
216-
Size: SectorSize * r.readValue("devices/"+n+"/size"),
212+
Size: procfs.SectorSize * r.readValue("devices/"+n+"/size"),
217213
}
218214
}
219215

fs.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ type FS struct {
2424
isReal bool
2525
}
2626

27-
// DefaultMountPoint is the common mount point of the proc filesystem.
28-
const DefaultMountPoint = fs.DefaultProcMountPoint
27+
const (
28+
// DefaultMountPoint is the common mount point of the proc filesystem.
29+
DefaultMountPoint = fs.DefaultProcMountPoint
30+
31+
// SectorSize represents the size of a sector in bytes.
32+
// It is specific to Linux block I/O operations.
33+
SectorSize = 512
34+
)
2935

3036
// NewDefaultFS returns a new proc FS mounted under the default proc mountPoint.
3137
// It will error if the mount point directory can't be read or is a file.

testdata/fixtures.ttar

+5
Original file line numberDiff line numberDiff line change
@@ -4335,6 +4335,11 @@ Lines: 1
43354335
none
43364336
Mode: 444
43374337
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
4338+
Path: fixtures/sys/block/sda/size
4339+
Lines: 1
4340+
3750748848EOF
4341+
Mode: 644
4342+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43384343
Path: fixtures/sys/block/sda/stat
43394344
Lines: 1
43404345
9652963 396792 759304206 412943 8422549 6731723 286915323 13947418 0 5658367 19174573 1 2 3 12

0 commit comments

Comments
 (0)