Skip to content
Open
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
32 changes: 27 additions & 5 deletions sysfs/class_infiniband.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,35 @@ 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)
if err != nil {
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
}
Expand All @@ -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/<Name>.
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) {
Expand Down
48 changes: 48 additions & 0 deletions sysfs/class_infiniband_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}