mirror of
https://github.com/clearml/go-nvlib
synced 2025-01-31 02:47:02 +00:00
Move the nvinfo package into pkg/nvlib/info
Also build an interface around the API so that it can more easily be mocked. Signed-off-by: Kevin Klues <kklues@nvidia.com>
This commit is contained in:
parent
ad3fa31634
commit
d23f460ad3
@ -14,18 +14,31 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
package nvinfo
|
package info
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/NVIDIA/go-nvml/pkg/dl"
|
"github.com/NVIDIA/go-nvml/pkg/dl"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HasNVML returns true if NVML is detected on the system
|
// Interface provides the API to the info package
|
||||||
func HasNVML() (bool, string) {
|
type Interface interface {
|
||||||
|
HasNvml() (bool, string)
|
||||||
|
IsTegraSystem() (bool, string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type infolib struct {
|
||||||
|
root string
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Interface = &infolib{}
|
||||||
|
|
||||||
|
// HasNvml returns true if NVML is detected on the system
|
||||||
|
func (i *infolib) HasNvml() (bool, string) {
|
||||||
const (
|
const (
|
||||||
nvmlLibraryName = "libnvidia-ml.so.1"
|
nvmlLibraryName = "libnvidia-ml.so.1"
|
||||||
nvmlLibraryLoadFlags = dl.RTLD_LAZY
|
nvmlLibraryLoadFlags = dl.RTLD_LAZY
|
||||||
@ -40,9 +53,9 @@ func HasNVML() (bool, string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsTegraSystem returns true if the system is detected as a Tegra-based system
|
// IsTegraSystem returns true if the system is detected as a Tegra-based system
|
||||||
func IsTegraSystem() (bool, string) {
|
func (i *infolib) IsTegraSystem() (bool, string) {
|
||||||
const tegraReleaseFile = "/etc/nv_tegra_release"
|
tegraReleaseFile := filepath.Join(i.root, "/etc/nv_tegra_release")
|
||||||
const tegraFamilyFile = "/sys/devices/soc0/family"
|
tegraFamilyFile := filepath.Join(i.root, "/sys/devices/soc0/family")
|
||||||
|
|
||||||
if info, err := os.Stat(tegraReleaseFile); err == nil && !info.IsDir() {
|
if info, err := os.Stat(tegraReleaseFile); err == nil && !info.IsDir() {
|
||||||
return true, fmt.Sprintf("%v found", tegraReleaseFile)
|
return true, fmt.Sprintf("%v found", tegraReleaseFile)
|
39
pkg/nvlib/info/options.go
Normal file
39
pkg/nvlib/info/options.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
# Copyright (c) 2022, 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 info
|
||||||
|
|
||||||
|
// Option defines a function for passing options to the New() call
|
||||||
|
type Option func(*infolib)
|
||||||
|
|
||||||
|
// New creates a new instance of the 'info' interface
|
||||||
|
func New(opts ...Option) Interface {
|
||||||
|
i := &infolib{}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(i)
|
||||||
|
}
|
||||||
|
if i.root == "" {
|
||||||
|
i.root = "/"
|
||||||
|
}
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRoot provides a Option to set the root of the 'info' interface
|
||||||
|
func WithRoot(root string) Option {
|
||||||
|
return func(i *infolib) {
|
||||||
|
i.root = root
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user