Skip to content

Commit 91d7216

Browse files
authored
Merge pull request #89 from shivamerla/fix_pci_bus_id_prefix
fix(device): only trim PCI bus ID prefix for padded 8-digit domains
2 parents 8ff29bb + 60ec6dc commit 91d7216

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

pkg/nvlib/device/device.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package device
1818

1919
import (
2020
"fmt"
21+
"regexp"
2122
"strings"
2223

2324
"github.com/NVIDIA/go-nvml/pkg/nvml"
@@ -48,6 +49,7 @@ type device struct {
4849
}
4950

5051
var _ Device = &device{}
52+
var pciBusIDPrefixRE = regexp.MustCompile(`^0{4}[0-9a-f]{4}:`)
5153

5254
// NewDevice builds a new Device from an nvml.Device.
5355
func (d *devicelib) NewDevice(dev nvml.Device) (Device, error) {
@@ -193,7 +195,7 @@ func (d *device) GetPCIBusID() (string, error) {
193195
}
194196
id := strings.ToLower(string(bytes))
195197

196-
if id != "0000" {
198+
if pciBusIDPrefixRE.MatchString(id) {
197199
id = strings.TrimPrefix(id, "0000")
198200
}
199201

pkg/nvlib/device/device_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) NVIDIA CORPORATION. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package device
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/NVIDIA/go-nvml/pkg/nvml"
25+
"github.com/NVIDIA/go-nvml/pkg/nvml/mock"
26+
)
27+
28+
func pciInfoWithBusID(busID string) nvml.PciInfo {
29+
var info nvml.PciInfo
30+
for i := 0; i < len(busID) && i < len(info.BusId); i++ {
31+
info.BusId[i] = uint8(busID[i])
32+
}
33+
return info
34+
}
35+
36+
func deviceWithPCIBusID(busID string) *device {
37+
return &device{
38+
Device: &mock.Device{
39+
GetPciInfoFunc: func() (nvml.PciInfo, nvml.Return) {
40+
return pciInfoWithBusID(busID), nvml.SUCCESS
41+
},
42+
},
43+
}
44+
}
45+
46+
func TestGetPCIBusID(t *testing.T) {
47+
testCases := []struct {
48+
name string
49+
busIDFromNVML string
50+
expected string
51+
}{
52+
{
53+
// Typical legacy NVML 4-digit domain: must not strip "0000".
54+
name: "four_digit_legacy_domain",
55+
busIDFromNVML: "0000:0A:00.0",
56+
expected: "0000:0a:00.0",
57+
},
58+
{
59+
// Non-zero 4-digit domain: must not trim.
60+
name: "nonzero_four_digit_domain",
61+
busIDFromNVML: "0001:03:00.0",
62+
expected: "0001:03:00.0",
63+
},
64+
{
65+
// 8-digit domain 00000000: trim prefix "0000".
66+
name: "eight_digit_domain_padded_with_zeros",
67+
busIDFromNVML: "00000000:0a:00.0",
68+
expected: "0000:0a:00.0",
69+
},
70+
{
71+
// 8-digit domain does not match padded "0000xxxx": leave unchanged.
72+
name: "eight_digit_domain_id",
73+
busIDFromNVML: "0001ABCD:03:00.0",
74+
expected: "0001abcd:03:00.0",
75+
},
76+
}
77+
78+
for _, tc := range testCases {
79+
t.Run(tc.name, func(t *testing.T) {
80+
got, err := deviceWithPCIBusID(tc.busIDFromNVML).GetPCIBusID()
81+
require.NoError(t, err)
82+
require.Equal(t, tc.expected, got)
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)