NVIDIA GPU loadable plugin
Source
/*
** Zabbix
** Copyright (C) 2001-2024 Zabbix SIA
**
** 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 nvml //nolint:gocritic
/*
#cgo CFLAGS: -I${SRCDIR}/nvml-sdk/include
#cgo CFLAGS: -DNVML_NO_UNVERSIONED_FUNC_DEFS=1
#cgo linux LDFLAGS: -Wl,--export-dynamic -Wl,--unresolved-symbols=ignore-in-object-files
#include "nvml.h"
#cgo linux LDFLAGS: -ldl
#include <dlfcn.h>
#include <stdlib.h>
*/
import "C" //nolint:gocritic,gci
import (
"sync"
"unsafe" //nolint:gocritic
"golang.zabbix.com/sdk/errs"
)
var (
_ Device = (*NVMLDevice)(nil)
_ Runner = (*NVMLRunner)(nil)
)
// NVMLRunner represents the NVML runner, responsible for managing
// the dynamic loading and initialization of the NVML library.
// It holds a pointer to the dynamically loaded library.
type NVMLRunner struct {
dynamicLib unsafe.Pointer // Pointer to the dynamically loaded NVML library
procListMux *sync.Mutex
procList map[string]struct{}
}
// NVMLDevice represents an NVIDIA device and provides methods to interact with and retrieve information from it.
// It holds a handle to the NVML device and a reference to the Runner, which manages NVML operations and symbol loading.
type NVMLDevice struct {
handle C.nvmlDevice_t // Handle to the NVML device
runner *NVMLRunner // Reference to the Runner for managing NVML operations
}
// NewNVMLRunner creates a new NVML Runner instance, loading the NVML library.
func NewNVMLRunner() (*NVMLRunner, error) {
dynamicLib, err := loadLibrary()