2022-12-02 13:17:52 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package nvcdi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/device"
|
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
|
|
|
|
)
|
|
|
|
|
|
|
|
type nvcdilib struct {
|
|
|
|
logger *logrus.Logger
|
|
|
|
nvmllib nvml.Interface
|
2023-02-16 14:52:39 +00:00
|
|
|
mode string
|
2022-12-02 13:17:52 +00:00
|
|
|
devicelib device.Interface
|
|
|
|
deviceNamer DeviceNamer
|
|
|
|
driverRoot string
|
|
|
|
nvidiaCTKPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new nvcdi library
|
|
|
|
func New(opts ...Option) Interface {
|
|
|
|
l := &nvcdilib{}
|
2023-02-15 00:32:40 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(l)
|
|
|
|
}
|
2023-02-16 15:29:53 +00:00
|
|
|
if l.mode == "" {
|
|
|
|
l.mode = "nvml"
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
if l.logger == nil {
|
|
|
|
l.logger = logrus.StandardLogger()
|
|
|
|
}
|
|
|
|
if l.deviceNamer == nil {
|
|
|
|
l.deviceNamer, _ = NewDeviceNamer(DeviceNameStrategyIndex)
|
|
|
|
}
|
|
|
|
if l.driverRoot == "" {
|
|
|
|
l.driverRoot = "/"
|
|
|
|
}
|
|
|
|
if l.nvidiaCTKPath == "" {
|
|
|
|
l.nvidiaCTKPath = "/usr/bin/nvidia-ctk"
|
|
|
|
}
|
|
|
|
|
2023-02-16 15:29:53 +00:00
|
|
|
switch l.mode {
|
|
|
|
case "nvml":
|
|
|
|
if l.nvmllib == nil {
|
|
|
|
l.nvmllib = nvml.New()
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
2023-02-16 15:29:53 +00:00
|
|
|
if l.devicelib == nil {
|
|
|
|
l.devicelib = device.New(device.WithNvml(l.nvmllib))
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 15:29:53 +00:00
|
|
|
return (*nvmllib)(l)
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
2023-02-16 15:29:53 +00:00
|
|
|
|
|
|
|
// TODO: We want an error here.
|
|
|
|
return nil
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|