nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: prometheus/procfs
Pull request #362 (2021-02-14 07:37:29)

Fix infiniband rate parsing
Fix parsing of infiniband rate for unconnected/unpopulated ports.

Fixes: https://github.com/prometheus/procfs/issues/361

Signed-off-by: Ben Kochie <superq@gmail.com>


Patch:
diff --git a/sysfs/class_infiniband.go b/sysfs/class_infiniband.go
index e74b7b9..80c3fcd 100644
--- a/sysfs/class_infiniband.go
+++ b/sysfs/class_infiniband.go
@@ -175,9 +175,9 @@ func parseState(s string) (uint, string, error) {
 
 // Parse rate (example: "100 Gb/sec (4X EDR)") and return it as bytes/second
 func parseRate(s string) (uint64, error) {
-	parts := strings.Split(s, "Gb/sec")
+	parts := strings.SplitAfterN(s, " ", 2)
 	if len(parts) != 2 {
-		return 0, fmt.Errorf("failed to split '%s' by 'Gb/sec'", s)
+		return 0, fmt.Errorf("failed to split %q", s)
 	}
 	value, err := strconv.ParseFloat(strings.TrimSpace(parts[0]), 32)
 	if err != nil {


Test patch:
diff --git a/sysfs/class_infiniband_test.go b/sysfs/class_infiniband_test.go
index 3eff19a..5c04dd5 100644
--- a/sysfs/class_infiniband_test.go
+++ b/sysfs/class_infiniband_test.go
@@ -26,6 +26,10 @@ func TestParseSlowRate(t *testing.T) {
 		rate string
 		want uint64
 	}{
+		{
+			rate: "0 GB/sec",
+			want: 0,
+		},
 		{
 			rate: "2.5 Gb/sec (1X SDR)",
 			want: 312500000,
Repository: prometheus/procfs
Pull request #376 (2021-04-11 08:57:28)

Add NetClassByIface
See also: prometheus/node_exporter#1915

Do we need to keep the `NetClass()` method?


Patch:
diff --git a/sysfs/net_class.go b/sysfs/net_class.go
index e6e2239..c1fed2b 100644
--- a/sysfs/net_class.go
+++ b/sysfs/net_class.go
@@ -82,6 +82,19 @@ func (fs FS) NetClassDevices() ([]string, error) {
 	return res, nil
 }
 
+// NetClassByIface returns info for a single net interfaces (iface)
+func (fs FS) NetClassByIface(devicePath string) (*NetClassIface, error) {
+	path := fs.sys.Path(netclassPath)
+
+	interfaceClass, err := parseNetClassIface(filepath.Join(path, devicePath))
+	if err != nil {
+		return nil, err
+	}
+	interfaceClass.Name = devicePath
+
+	return interfaceClass, nil
+}
+
 // NetClass returns info for all net interfaces (iface) read from /sys/class/net/<iface>.
 func (fs FS) NetClass() (NetClass, error) {
 	devices, err := fs.NetClassDevices()
@@ -91,20 +104,21 @@ func (fs FS) NetClass() (NetClass, error) {
 
 	path := fs.sys.Path(netclassPath)
 	netClass := NetClass{}
-	for _, deviceDir := range devices {
-		interfaceClass, err := netClass.parseNetClassIface(filepath.Join(path, deviceDir))
+	for _, devicePath := range devices {
+		interfaceClass, err := parseNetClassIface(filepath.Join(path, devicePath))
 		if err != nil {
 			return nil, err
 		}
-		interfaceClass.Name = deviceDir
-		netClass[deviceDir] = *interfaceClass
+		interfaceClass.Name = devicePath
+		netClass[devicePath] = *interfaceClass
 	}
+
 	return netClass, nil
 }
 
 // parseNetClassIface scans predefined files in /sys/class/net/<iface>
 // directory and gets their contents.
-func (nc NetClass) parseNetClassIface(devicePath string) (*NetClassIface, error) {
+func parseNetClassIface(devicePath string) (*NetClassIface, error) {
 	interfaceClass := NetClassIface{}
 
 	files, err := ioutil.ReadDir(devicePath)
@@ -180,6 +194,6 @@ func (nc NetClass) parseNetClassIface(devicePath string) (*NetClassIface, error)
 			interfaceClass.Type = vp.PInt64()
 		}
 	}
-	return &interfaceClass, nil
 
+	return &interfaceClass, nil
 }


Test patch:
diff --git a/sysfs/net_class_test.go b/sysfs/net_class_test.go
index bc219f3..f856607 100644
--- a/sysfs/net_class_test.go
+++ b/sysfs/net_class_test.go
@@ -39,6 +39,27 @@ func TestNewNetClassDevices(t *testing.T) {
 	}
 }
 
+func TestNewNetClassDevicesByIface(t *testing.T) {
+	fs, err := NewFS(sysTestFixtures)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	_, err = fs.NetClassByIface("non-existent")
+	if err == nil {
+		t.Fatal("expected error, have none")
+	}
+
+	device, err := fs.NetClassByIface("eth0")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if device.Name != "eth0" {
+		t.Errorf("Found unexpected device, want %s, have %s", "eth0", device.Name)
+	}
+}
+
 func TestNetClass(t *testing.T) {
 	fs, err := NewFS(sysTestFixtures)
 	if err != nil {