Add function to get the PCI bus ID for a device

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2024-06-11 15:05:40 +02:00
parent 20ba32166d
commit d60aa34a78

View File

@ -18,6 +18,7 @@ package device
import ( import (
"fmt" "fmt"
"strings"
"github.com/NVIDIA/go-nvml/pkg/nvml" "github.com/NVIDIA/go-nvml/pkg/nvml"
) )
@ -30,6 +31,7 @@ type Device interface {
GetCudaComputeCapabilityAsString() (string, error) GetCudaComputeCapabilityAsString() (string, error)
GetMigDevices() ([]MigDevice, error) GetMigDevices() ([]MigDevice, error)
GetMigProfiles() ([]MigProfile, error) GetMigProfiles() ([]MigProfile, error)
GetPCIBusID() (string, error)
IsMigCapable() (bool, error) IsMigCapable() (bool, error)
IsMigEnabled() (bool, error) IsMigEnabled() (bool, error)
VisitMigDevices(func(j int, m MigDevice) error) error VisitMigDevices(func(j int, m MigDevice) error) error
@ -140,6 +142,29 @@ func (d *device) GetBrandAsString() (string, error) {
return "", fmt.Errorf("error interpreting device brand as string: %v", brand) 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. // GetCudaComputeCapabilityAsString returns the Device's CUDA compute capability as a version string.
func (d *device) GetCudaComputeCapabilityAsString() (string, error) { func (d *device) GetCudaComputeCapabilityAsString() (string, error) {
major, minor, ret := d.GetCudaComputeCapability() major, minor, ret := d.GetCudaComputeCapability()