Skip to content

Commit

Permalink
Add a locateVendor function for seeking position in pci.ids file for …
Browse files Browse the repository at this point in the history
…a specific vendor

Signed-off-by: Christopher Desiniotis <[email protected]>
  • Loading branch information
cdesiniotis committed Jun 9, 2023
1 parent fd2bd2d commit 919d8f1
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions pkg/device_plugin/device_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ import (
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)

const (
nvidiaVendorID = "10de"
)

//Structure to hold details about Nvidia GPU Device
type NvidiaGpuDevice struct {
addr string // PCI address of device
Expand Down Expand Up @@ -309,13 +313,11 @@ func getDeviceName(deviceID string) string {
}
defer file.Close()

// Find beginning of NVIDIA device list
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "10de") {
break
}
// Locate beginning of NVIDIA device list in pci.ids file
scanner, err := locateVendor(file, nvidiaVendorID)
if err != nil {
log.Printf("Error locating NVIDIA in pci.ds file: %v", err)
return ""
}

// Find NVIDIA device by device id
Expand Down Expand Up @@ -354,3 +356,19 @@ func getDeviceName(deviceID string) string {
}
return deviceName
}

func locateVendor(pciIdsFile *os.File, vendorID string) (*bufio.Scanner, error) {
scanner := bufio.NewScanner(pciIdsFile)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, vendorID) {
return scanner, nil
}
}

if err := scanner.Err(); err != nil {
return scanner, fmt.Errorf("error reading pci.ids file: %v", err)
}

return scanner, fmt.Errorf("failed to find vendor id in pci.ids file: %s", vendorID)
}

0 comments on commit 919d8f1

Please sign in to comment.