Skip to content

Commit

Permalink
Use /proc/partitions to get device names
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Ivanov <rbtz@dropbox.com>
  • Loading branch information
SaveTheRbtz committed May 5, 2021
1 parent b9de8a2 commit 1a4509d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
9 changes: 6 additions & 3 deletions blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (b *blkioController) Stat(path string, stats *v1.Metrics) error {
}
}

f, err := os.Open(filepath.Join(b.procRoot, "diskstats"))
f, err := os.Open(filepath.Join(b.procRoot, "partitions"))
if err != nil {
return err
}
Expand Down Expand Up @@ -335,7 +335,10 @@ func getDevices(r io.Reader) (map[deviceKey]string, error) {
s = bufio.NewScanner(r)
devices = make(map[deviceKey]string)
)
for s.Scan() {
for i := 0; s.Scan(); i++ {
if i < 2 {
continue
}
fields := strings.Fields(s.Text())
major, err := strconv.Atoi(fields[0])
if err != nil {
Expand All @@ -352,7 +355,7 @@ func getDevices(r io.Reader) (map[deviceKey]string, error) {
if _, ok := devices[key]; ok {
continue
}
devices[key] = filepath.Join("/dev", fields[2])
devices[key] = filepath.Join("/dev", fields[3])
}
return devices, s.Err()
}
44 changes: 25 additions & 19 deletions blkio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,38 @@ import (
v1 "github.com/containerd/cgroups/stats/v1"
)

const data = ` 7 0 loop0 0 0 0 0 0 0 0 0 0 0 0
7 1 loop1 0 0 0 0 0 0 0 0 0 0 0
7 2 loop2 0 0 0 0 0 0 0 0 0 0 0
7 3 loop3 0 0 0 0 0 0 0 0 0 0 0
7 4 loop4 0 0 0 0 0 0 0 0 0 0 0
7 5 loop5 0 0 0 0 0 0 0 0 0 0 0
7 6 loop6 0 0 0 0 0 0 0 0 0 0 0
7 7 loop7 0 0 0 0 0 0 0 0 0 0 0
8 0 sda 1892042 187697 63489222 1246284 1389086 2887005 134903104 11390608 1 1068060 12692228
8 1 sda1 1762875 37086 61241570 1200512 1270037 2444415 131214808 11152764 1 882624 12409308
8 2 sda2 2 0 4 0 0 0 0 0 0 0 0
8 5 sda5 129102 150611 2244440 45716 18447 442590 3688296 67268 0 62584 112984`
const data = `major minor #blocks name
7 0 4 loop0
7 1 163456 loop1
7 2 149616 loop2
7 3 147684 loop3
7 4 122572 loop4
7 5 8936 loop5
7 6 31464 loop6
7 7 182432 loop7
259 0 937692504 nvme0n1
259 1 31744 nvme0n1p1
`

func TestGetDevices(t *testing.T) {
r := strings.NewReader(data)
devices, err := getDevices(r)
if err != nil {
t.Fatal(err)
}
name, ok := devices[deviceKey{8, 0}]
if !ok {
t.Fatal("no device found for 8,0")
}
const expected = "/dev/sda"
if name != expected {
t.Fatalf("expected device name %q but received %q", expected, name)
for dev, expected := range map[deviceKey]string{
deviceKey{7, 0}: "/dev/loop0",
deviceKey{259, 0}: "/dev/nvme0n1",
deviceKey{259, 1}: "/dev/nvme0n1p1",
} {
name, ok := devices[dev]
if !ok {
t.Fatalf("no device found for %d:%d", dev.major, dev.minor)
}
if name != expected {
t.Fatalf("expected device name %q but received %q", expected, name)
}
}
}

Expand Down

0 comments on commit 1a4509d

Please sign in to comment.