Skip to content

Commit ac7e7f2

Browse files
committed
gpu: Add GetNvidiaDeviceByPciBusID and fix GetGPUByPciBusID
- Add GetNvidiaDeviceByPciBusID to return any NVIDIA PCI device (GPU, NVSwitch, etc.) at a given PCI Bus ID - Refactor GetGPUByPciBusID to wrap GetNvidiaDeviceByPciBusID and filter to return only GPUs (returns nil for non-GPU devices) - Rename internal function to getNvidiaDeviceByPciBusID Signed-off-by: Zvonko Kaiser <zkaiser@nvidia.com>
1 parent 82a46a9 commit ac7e7f2

2 files changed

Lines changed: 75 additions & 16 deletions

File tree

pkg/nvpci/nvpci.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Interface interface {
5656
GetGPUs() ([]*NvidiaPCIDevice, error)
5757
GetGPUByIndex(int) (*NvidiaPCIDevice, error)
5858
GetGPUByPciBusID(string) (*NvidiaPCIDevice, error)
59+
GetNvidiaDeviceByPciBusID(string) (*NvidiaPCIDevice, error)
5960
GetNetworkControllers() ([]*NvidiaPCIDevice, error)
6061
GetPciBridges() ([]*NvidiaPCIDevice, error)
6162
GetDPUs() ([]*NvidiaPCIDevice, error)
@@ -211,7 +212,7 @@ func (p *nvpci) GetAllDevices() ([]*NvidiaPCIDevice, error) {
211212
cache := make(map[string]*NvidiaPCIDevice)
212213
for _, deviceDir := range deviceDirs {
213214
deviceAddress := deviceDir.Name()
214-
nvdevice, err := p.getGPUByPciBusID(deviceAddress, cache)
215+
nvdevice, err := p.getNvidiaDeviceByPciBusID(deviceAddress, cache)
215216
if err != nil {
216217
return nil, fmt.Errorf("error constructing NVIDIA PCI device %s: %v", deviceAddress, err)
217218
}
@@ -235,13 +236,27 @@ func (p *nvpci) GetAllDevices() ([]*NvidiaPCIDevice, error) {
235236
return nvdevices, nil
236237
}
237238

238-
// GetGPUByPciBusID constructs an NvidiaPCIDevice for the specified address (PCI Bus ID).
239+
// GetGPUByPciBusID returns an NvidiaPCIDevice for the specified address (PCI Bus ID)
240+
// only if the device is a GPU. Returns nil if the device exists but is not a GPU.
239241
func (p *nvpci) GetGPUByPciBusID(address string) (*NvidiaPCIDevice, error) {
240-
// Pass nil as to force reading device information from sysfs.
241-
return p.getGPUByPciBusID(address, nil)
242+
dev, err := p.GetNvidiaDeviceByPciBusID(address)
243+
if err != nil {
244+
return nil, err
245+
}
246+
if dev == nil || !dev.IsGPU() {
247+
return nil, nil
248+
}
249+
return dev, nil
250+
}
251+
252+
// GetNvidiaDeviceByPciBusID constructs an NvidiaPCIDevice for the specified
253+
// address (PCI Bus ID). This returns any NVIDIA PCI device at the given
254+
// address, including GPUs, NVSwitches, and other NVIDIA devices.
255+
func (p *nvpci) GetNvidiaDeviceByPciBusID(address string) (*NvidiaPCIDevice, error) {
256+
return p.getNvidiaDeviceByPciBusID(address, nil)
242257
}
243258

244-
func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevice) (*NvidiaPCIDevice, error) {
259+
func (p *nvpci) getNvidiaDeviceByPciBusID(address string, cache map[string]*NvidiaPCIDevice) (*NvidiaPCIDevice, error) {
245260
if cache != nil {
246261
if pciDevice, exists := cache[address]; exists {
247262
return pciDevice, nil
@@ -357,7 +372,7 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
357372
physFnAddress, err := filepath.EvalSymlinks(path.Join(devicePath, "physfn"))
358373
switch {
359374
case err == nil:
360-
physFn, err := p.getGPUByPciBusID(filepath.Base(physFnAddress), cache)
375+
physFn, err := p.getNvidiaDeviceByPciBusID(filepath.Base(physFnAddress), cache)
361376
if err != nil {
362377
return nil, fmt.Errorf("unable to detect physfn for %s: %v", address, err)
363378
}

pkg/nvpci/nvpci_mock.go

Lines changed: 54 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)