From fc5bdcd3d39c5908b0560b9539d55f6225af0145 Mon Sep 17 00:00:00 2001 From: Rafal Szypulka Date: Thu, 23 Jul 2026 10:31:14 +0200 Subject: [PATCH] Add InfiniBandClassDevices and InfiniBandDevice with tests Signed-off-by: Rafal Szypulka --- sysfs/class_infiniband.go | 32 +++++++++++++++++++---- sysfs/class_infiniband_test.go | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/sysfs/class_infiniband.go b/sysfs/class_infiniband.go index 4a893ae6..d84924d3 100644 --- a/sysfs/class_infiniband.go +++ b/sysfs/class_infiniband.go @@ -136,9 +136,9 @@ type InfiniBandDevice struct { // The map keys are the names of the InfiniBand devices. type InfiniBandClass map[string]InfiniBandDevice -// InfiniBandClass returns info for all InfiniBand devices read from -// /sys/class/infiniband. -func (fs FS) InfiniBandClass() (InfiniBandClass, error) { +// InfiniBandClassDevices returns the names of the InfiniBand devices found in +// /sys/class/infiniband without reading any device attributes or counters. +func (fs FS) InfiniBandClassDevices() ([]string, error) { path := fs.sys.Path(infinibandClassPath) dirs, err := os.ReadDir(path) @@ -146,9 +146,25 @@ func (fs FS) InfiniBandClass() (InfiniBandClass, error) { return nil, err } - ibc := make(InfiniBandClass, len(dirs)) + devices := make([]string, 0, len(dirs)) for _, d := range dirs { - device, err := fs.parseInfiniBandDevice(d.Name()) + devices = append(devices, d.Name()) + } + + return devices, nil +} + +// InfiniBandClass returns info for all InfiniBand devices read from +// /sys/class/infiniband. +func (fs FS) InfiniBandClass() (InfiniBandClass, error) { + devices, err := fs.InfiniBandClassDevices() + if err != nil { + return nil, err + } + + ibc := make(InfiniBandClass, len(devices)) + for _, name := range devices { + device, err := fs.InfiniBandDevice(name) if err != nil { return nil, err } @@ -159,6 +175,12 @@ func (fs FS) InfiniBandClass() (InfiniBandClass, error) { return ibc, nil } +// InfiniBandDevice returns info for a single InfiniBand device read from +// /sys/class/infiniband/. +func (fs FS) InfiniBandDevice(name string) (*InfiniBandDevice, error) { + return fs.parseInfiniBandDevice(name) +} + // Parse one InfiniBand device. // Refer to https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-class-infiniband func (fs FS) parseInfiniBandDevice(name string) (*InfiniBandDevice, error) { diff --git a/sysfs/class_infiniband_test.go b/sysfs/class_infiniband_test.go index 66a8fdee..99153aed 100644 --- a/sysfs/class_infiniband_test.go +++ b/sysfs/class_infiniband_test.go @@ -16,11 +16,37 @@ package sysfs import ( + "os" + "path/filepath" "testing" "github.com/google/go-cmp/cmp" ) +func TestInfiniBandClassDevicesDoesNotReadDevice(t *testing.T) { + sysPath := t.TempDir() + for _, name := range []string{"mlx5_6", "mlx5_5"} { + if err := os.MkdirAll(filepath.Join(sysPath, "class", "infiniband", name), 0o755); err != nil { + t.Fatal(err) + } + } + + fs, err := NewFS(sysPath) + if err != nil { + t.Fatal(err) + } + + got, err := fs.InfiniBandClassDevices() + if err != nil { + t.Fatal(err) + } + + want := []string{"mlx5_5", "mlx5_6"} + if diff := cmp.Diff(want, got); diff != "" { + t.Fatalf("unexpected InfiniBand device names (-want +got):\n%s", diff) + } +} + func TestParseSlowRate(t *testing.T) { tests := []struct { rate string @@ -344,3 +370,25 @@ func TestInfiniBandClass(t *testing.T) { t.Fatalf("unexpected InfiniBand class (-want +got):\n%s", diff) } } + +func TestInfiniBandDevice(t *testing.T) { + fs, err := NewFS(sysTestFixtures) + if err != nil { + t.Fatal(err) + } + + class, err := fs.InfiniBandClass() + if err != nil { + t.Fatal(err) + } + + got, err := fs.InfiniBandDevice("mlx5_0") + if err != nil { + t.Fatal(err) + } + + want := class["mlx5_0"] + if diff := cmp.Diff(want, *got); diff != "" { + t.Fatalf("unexpected InfiniBand device (-want +got):\n%s", diff) + } +}