Skip to content

Commit

Permalink
Merge pull request #41 from elezar/add-get-pci-busid
Browse files Browse the repository at this point in the history
Add function to get the PCI bus ID for a device
  • Loading branch information
elezar authored Jun 17, 2024
2 parents 9ff9246 + d60aa34 commit a408243
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/nvlib/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package device

import (
"fmt"
"strings"

"github.com/NVIDIA/go-nvml/pkg/nvml"
)
Expand All @@ -30,6 +31,7 @@ type Device interface {
GetCudaComputeCapabilityAsString() (string, error)
GetMigDevices() ([]MigDevice, error)
GetMigProfiles() ([]MigProfile, error)
GetPCIBusID() (string, error)
IsMigCapable() (bool, error)
IsMigEnabled() (bool, error)
VisitMigDevices(func(j int, m MigDevice) error) error
Expand Down Expand Up @@ -140,6 +142,29 @@ func (d *device) GetBrandAsString() (string, error) {
return "", fmt.Errorf("error interpreting device brand as string: %v", brand)
}

// GetPCIBusID returns the string representation of the bus ID.
func (d *device) GetPCIBusID() (string, error) {
info, ret := d.GetPciInfo()
if ret != nvml.SUCCESS {
return "", fmt.Errorf("error getting PCI info: %w", ret)
}

var bytes []byte
for _, b := range info.BusId {
if byte(b) == '\x00' {
break
}
bytes = append(bytes, byte(b))
}
id := strings.ToLower(string(bytes))

if id != "0000" {
id = strings.TrimPrefix(id, "0000")
}

return id, nil
}

// GetCudaComputeCapabilityAsString returns the Device's CUDA compute capability as a version string.
func (d *device) GetCudaComputeCapabilityAsString() (string, error) {
major, minor, ret := d.GetCudaComputeCapability()
Expand Down

0 comments on commit a408243

Please sign in to comment.