mirror of
https://github.com/clearml/go-nvlib
synced 2025-01-31 02:47:02 +00:00
Allow options to be passed when creating an instance of the nvpci interface
Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
This commit is contained in:
parent
76018d282e
commit
066d8f30bc
@ -380,5 +380,7 @@ func (p *ParentDevice) GetAvailableMDEVInstances(mdevType string) (int, error) {
|
|||||||
func newNvidiaPCIDeviceFromPath(devicePath string) (*nvpci.NvidiaPCIDevice, error) {
|
func newNvidiaPCIDeviceFromPath(devicePath string) (*nvpci.NvidiaPCIDevice, error) {
|
||||||
root := filepath.Dir(devicePath)
|
root := filepath.Dir(devicePath)
|
||||||
address := filepath.Base(devicePath)
|
address := filepath.Base(devicePath)
|
||||||
return nvpci.NewFrom(root).GetGPUByPciBusID(address)
|
return nvpci.New(
|
||||||
|
nvpci.WithPCIDevicesRoot(root),
|
||||||
|
).GetGPUByPciBusID(address)
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ func NewMockNvpci() (mock *MockNvpci, rerr error) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
mock = &MockNvpci{
|
mock = &MockNvpci{
|
||||||
NewFrom(rootDir).(*nvpci),
|
New(WithPCIDevicesRoot(rootDir)).(*nvpci),
|
||||||
}
|
}
|
||||||
|
|
||||||
return mock, nil
|
return mock, nil
|
||||||
|
@ -65,6 +65,7 @@ type ResourceInterface interface {
|
|||||||
|
|
||||||
type nvpci struct {
|
type nvpci struct {
|
||||||
pciDevicesRoot string
|
pciDevicesRoot string
|
||||||
|
pcidbPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Interface = (*nvpci)(nil)
|
var _ Interface = (*nvpci)(nil)
|
||||||
@ -124,14 +125,33 @@ func (d *NvidiaPCIDevice) Reset() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New interface that allows us to get a list of all NVIDIA PCI devices
|
// New interface that allows us to get a list of all NVIDIA PCI devices
|
||||||
func New() Interface {
|
func New(opts ...Option) Interface {
|
||||||
return NewFrom(PCIDevicesRoot)
|
n := &nvpci{}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(n)
|
||||||
|
}
|
||||||
|
if n.pciDevicesRoot == "" {
|
||||||
|
n.pciDevicesRoot = PCIDevicesRoot
|
||||||
|
}
|
||||||
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFrom interface allows us to get a list of all NVIDIA PCI devices at a specific root directory
|
// Option defines a function for passing options to the New() call
|
||||||
func NewFrom(root string) Interface {
|
type Option func(*nvpci)
|
||||||
return &nvpci{
|
|
||||||
pciDevicesRoot: root,
|
// WithPCIDevicesRoot provides an Option to set the root path
|
||||||
|
// for PCI devices on the system.
|
||||||
|
func WithPCIDevicesRoot(root string) Option {
|
||||||
|
return func(n *nvpci) {
|
||||||
|
n.pciDevicesRoot = root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPCIDatabasePath provides an Option to set the path
|
||||||
|
// to the pciids database file.
|
||||||
|
func WithPCIDatabasePath(path string) Option {
|
||||||
|
return func(n *nvpci) {
|
||||||
|
n.pcidbPath = path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user