Skip to content

Commit

Permalink
feat(blockdevice): added sysblockdevicesize method and test (#658)
Browse files Browse the repository at this point in the history
* added blockdevice size method + test + data

---------

Signed-off-by: fs185143 <[email protected]>
Signed-off-by: Finn Snape <[email protected]>
  • Loading branch information
fs185143 authored Nov 26, 2024
1 parent 9c0d20a commit 969849f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
12 changes: 12 additions & 0 deletions blockdevice/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"strings"

"github.com/prometheus/procfs"
"github.com/prometheus/procfs/internal/fs"
"github.com/prometheus/procfs/internal/util"
)
Expand Down Expand Up @@ -214,6 +215,7 @@ const (
sysBlockQueue = "queue"
sysBlockDM = "dm"
sysUnderlyingDev = "slaves"
sysBlockSize = "size"
sysDevicePath = "device"
)

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

}

// SysBlockDeviceSize 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) SysBlockDeviceSize(device string) (uint64, error) {
size, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize))
if err != nil {
return 0, err
}
return procfs.SectorSize * size, nil
}

// SysBlockDeviceIO returns stats for the block device io counters
// IO done count: /sys/block/<disk>/device/iodone_cnt
// IO error count: /sys/block/<disk>/device/ioerr_cnt.
Expand Down
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)
}
size7, err := blockdevice.SysBlockDeviceSize(devices[7])
if err != nil {
t.Fatal(err)
}
size7Expected := uint64(1920383410176)
if size7 != size7Expected {
t.Errorf("Incorrect BlockDeviceSize, expected: \n%+v, got: \n%+v", size7Expected, size7)
}
}
8 changes: 2 additions & 6 deletions btrfs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ import (
"strconv"
"strings"

"github.com/prometheus/procfs"
"github.com/prometheus/procfs/internal/fs"
"github.com/prometheus/procfs/internal/util"
)

// SectorSize contains the Linux sector size.
// > Linux always considers sectors to be 512 bytes long independently
// > of the devices real block size.
const SectorSize = 512

// FS represents the pseudo-filesystem sys, which provides an interface to
// kernel data structures.
type FS struct {
Expand Down Expand Up @@ -213,7 +209,7 @@ func (r *reader) readDeviceInfo(d string) map[string]*Device {
info := make(map[string]*Device, len(devs))
for _, n := range devs {
info[n] = &Device{
Size: SectorSize * r.readValue("devices/"+n+"/size"),
Size: procfs.SectorSize * r.readValue("devices/"+n+"/size"),
}
}

Expand Down
10 changes: 8 additions & 2 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ type FS struct {
isReal bool
}

// DefaultMountPoint is the common mount point of the proc filesystem.
const DefaultMountPoint = fs.DefaultProcMountPoint
const (
// DefaultMountPoint is the common mount point of the proc filesystem.
DefaultMountPoint = fs.DefaultProcMountPoint

// SectorSize represents the size of a sector in bytes.
// It is specific to Linux block I/O operations.
SectorSize = 512
)

// NewDefaultFS returns a new proc FS mounted under the default proc mountPoint.
// It will error if the mount point directory can't be read or is a file.
Expand Down
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

0 comments on commit 969849f

Please sign in to comment.