2018-09-01 00:08:36 +00:00
|
|
|
package nvidia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-09-18 16:57:39 +00:00
|
|
|
"strings"
|
2018-09-01 00:08:36 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-10-05 20:50:18 +00:00
|
|
|
"github.com/hashicorp/nomad/devices/gpu/nvidia/nvml"
|
2019-01-23 20:53:33 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/loader"
|
2018-09-01 00:08:36 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
"github.com/hashicorp/nomad/plugins/device"
|
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// pluginName is the name of the plugin
|
|
|
|
pluginName = "nvidia-gpu"
|
|
|
|
|
|
|
|
// vendor is the vendor providing the devices
|
|
|
|
vendor = "nvidia"
|
|
|
|
|
|
|
|
// deviceType is the type of device being returned
|
|
|
|
deviceType = device.DeviceTypeGPU
|
|
|
|
|
|
|
|
// notAvailable value is returned to nomad server in case some properties were
|
|
|
|
// undetected by nvml driver
|
|
|
|
notAvailable = "N/A"
|
|
|
|
|
2018-09-18 16:57:39 +00:00
|
|
|
// Nvidia-container-runtime environment variable names
|
2018-12-18 01:03:43 +00:00
|
|
|
NvidiaVisibleDevices = "NVIDIA_VISIBLE_DEVICES"
|
2018-09-18 16:57:39 +00:00
|
|
|
)
|
|
|
|
|
2018-09-01 00:08:36 +00:00
|
|
|
var (
|
2019-01-23 18:43:13 +00:00
|
|
|
// PluginID is the nvidia plugin metadata registered in the plugin
|
|
|
|
// catalog.
|
|
|
|
PluginID = loader.PluginID{
|
|
|
|
Name: pluginName,
|
|
|
|
PluginType: base.PluginTypeDevice,
|
|
|
|
}
|
|
|
|
|
|
|
|
// PluginConfig is the nvidia factory function registered in the
|
|
|
|
// plugin catalog.
|
|
|
|
PluginConfig = &loader.InternalPluginConfig{
|
2020-05-26 13:44:26 +00:00
|
|
|
Factory: func(ctx context.Context, l log.Logger) interface{} { return NewNvidiaDevice(ctx, l) },
|
2019-01-23 18:43:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 00:08:36 +00:00
|
|
|
// pluginInfo describes the plugin
|
|
|
|
pluginInfo = &base.PluginInfoResponse{
|
2018-12-18 00:40:58 +00:00
|
|
|
Type: base.PluginTypeDevice,
|
2018-12-18 23:07:30 +00:00
|
|
|
PluginApiVersions: []string{device.ApiVersion010},
|
2018-12-18 00:40:58 +00:00
|
|
|
PluginVersion: "0.1.0",
|
|
|
|
Name: pluginName,
|
2018-09-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// configSpec is the specification of the plugin's configuration
|
|
|
|
configSpec = hclspec.NewObject(map[string]*hclspec.Spec{
|
2020-07-21 14:11:16 +00:00
|
|
|
"enabled": hclspec.NewDefault(
|
|
|
|
hclspec.NewAttr("enabled", "bool", false),
|
|
|
|
hclspec.NewLiteral("true"),
|
|
|
|
),
|
2018-09-01 00:08:36 +00:00
|
|
|
"ignored_gpu_ids": hclspec.NewDefault(
|
|
|
|
hclspec.NewAttr("ignored_gpu_ids", "list(string)", false),
|
|
|
|
hclspec.NewLiteral("[]"),
|
|
|
|
),
|
|
|
|
"fingerprint_period": hclspec.NewDefault(
|
|
|
|
hclspec.NewAttr("fingerprint_period", "string", false),
|
2018-09-28 17:09:01 +00:00
|
|
|
hclspec.NewLiteral("\"1m\""),
|
2018-09-07 17:13:50 +00:00
|
|
|
),
|
2018-09-01 00:08:36 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains configuration information for the plugin.
|
|
|
|
type Config struct {
|
2020-07-21 14:11:16 +00:00
|
|
|
Enabled bool `codec:"enabled"`
|
2018-09-01 00:08:36 +00:00
|
|
|
IgnoredGPUIDs []string `codec:"ignored_gpu_ids"`
|
|
|
|
FingerprintPeriod string `codec:"fingerprint_period"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NvidiaDevice contains all plugin specific data
|
|
|
|
type NvidiaDevice struct {
|
2020-07-21 14:11:16 +00:00
|
|
|
// enabled indicates whether the plugin should be enabled
|
|
|
|
enabled bool
|
|
|
|
|
2018-09-01 00:08:36 +00:00
|
|
|
// nvmlClient is used to get data from nvidia
|
|
|
|
nvmlClient nvml.NvmlClient
|
|
|
|
|
2018-10-05 20:50:18 +00:00
|
|
|
// initErr holds an error retrieved during
|
2018-09-01 00:08:36 +00:00
|
|
|
// nvmlClient initialization
|
2018-10-05 20:50:18 +00:00
|
|
|
initErr error
|
2018-09-01 00:08:36 +00:00
|
|
|
|
|
|
|
// ignoredGPUIDs is a set of UUIDs that would not be exposed to nomad
|
|
|
|
ignoredGPUIDs map[string]struct{}
|
|
|
|
|
|
|
|
// fingerprintPeriod is how often we should call nvml to get list of devices
|
|
|
|
fingerprintPeriod time.Duration
|
|
|
|
|
|
|
|
// devices is the set of detected eligible devices
|
|
|
|
devices map[string]struct{}
|
|
|
|
deviceLock sync.RWMutex
|
|
|
|
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNvidiaDevice returns a new nvidia device plugin.
|
2020-05-26 13:44:26 +00:00
|
|
|
func NewNvidiaDevice(_ context.Context, log log.Logger) *NvidiaDevice {
|
2018-10-05 20:50:18 +00:00
|
|
|
nvmlClient, err := nvml.NewNvmlClient()
|
2018-09-01 00:08:36 +00:00
|
|
|
logger := log.Named(pluginName)
|
2018-10-05 20:50:18 +00:00
|
|
|
if err != nil && err.Error() != nvml.UnavailableLib.Error() {
|
|
|
|
logger.Error("unable to initialize Nvidia driver", "reason", err)
|
2018-09-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
return &NvidiaDevice{
|
2018-10-05 20:50:18 +00:00
|
|
|
logger: logger,
|
|
|
|
devices: make(map[string]struct{}),
|
|
|
|
ignoredGPUIDs: make(map[string]struct{}),
|
|
|
|
nvmlClient: nvmlClient,
|
|
|
|
initErr: err,
|
2018-09-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PluginInfo returns information describing the plugin.
|
|
|
|
func (d *NvidiaDevice) PluginInfo() (*base.PluginInfoResponse, error) {
|
|
|
|
return pluginInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigSchema returns the plugins configuration schema.
|
|
|
|
func (d *NvidiaDevice) ConfigSchema() (*hclspec.Spec, error) {
|
|
|
|
return configSpec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig is used to set the configuration of the plugin.
|
2018-12-18 00:40:58 +00:00
|
|
|
func (d *NvidiaDevice) SetConfig(cfg *base.Config) error {
|
2018-09-01 00:08:36 +00:00
|
|
|
var config Config
|
2018-12-18 00:40:58 +00:00
|
|
|
if len(cfg.PluginConfig) != 0 {
|
|
|
|
if err := base.MsgPackDecode(cfg.PluginConfig, &config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 14:11:16 +00:00
|
|
|
d.enabled = config.Enabled
|
|
|
|
|
2018-09-01 00:08:36 +00:00
|
|
|
for _, ignoredGPUId := range config.IgnoredGPUIDs {
|
|
|
|
d.ignoredGPUIDs[ignoredGPUId] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
period, err := time.ParseDuration(config.FingerprintPeriod)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse fingerprint period %q: %v", config.FingerprintPeriod, err)
|
|
|
|
}
|
|
|
|
d.fingerprintPeriod = period
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fingerprint streams detected devices. If device changes are detected or the
|
|
|
|
// devices health changes, messages will be emitted.
|
|
|
|
func (d *NvidiaDevice) Fingerprint(ctx context.Context) (<-chan *device.FingerprintResponse, error) {
|
2020-07-21 14:11:16 +00:00
|
|
|
if !d.enabled {
|
|
|
|
return nil, device.ErrPluginDisabled
|
|
|
|
}
|
|
|
|
|
2018-09-01 00:08:36 +00:00
|
|
|
outCh := make(chan *device.FingerprintResponse)
|
|
|
|
go d.fingerprint(ctx, outCh)
|
|
|
|
return outCh, nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 16:57:39 +00:00
|
|
|
type reservationError struct {
|
|
|
|
notExistingIDs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *reservationError) Error() string {
|
|
|
|
return fmt.Sprintf("unknown device IDs: %s", strings.Join(e.notExistingIDs, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reserve returns information on how to mount given devices.
|
|
|
|
// Assumption is made that nomad server is responsible for correctness of
|
|
|
|
// GPU allocations, handling tricky cases such as double-allocation of single GPU
|
2018-09-01 00:08:36 +00:00
|
|
|
func (d *NvidiaDevice) Reserve(deviceIDs []string) (*device.ContainerReservation, error) {
|
2018-09-18 16:57:39 +00:00
|
|
|
if len(deviceIDs) == 0 {
|
|
|
|
return &device.ContainerReservation{}, nil
|
|
|
|
}
|
2020-07-21 14:11:16 +00:00
|
|
|
if !d.enabled {
|
|
|
|
return nil, device.ErrPluginDisabled
|
|
|
|
}
|
|
|
|
|
2018-09-18 16:57:39 +00:00
|
|
|
// Due to the asynchronous nature of NvidiaPlugin, there is a possibility
|
|
|
|
// of race condition
|
|
|
|
//
|
|
|
|
// Timeline:
|
|
|
|
// 1 - fingerprint reports that GPU with id "1" is present
|
|
|
|
// 2 - the following events happen at the same time:
|
|
|
|
// a) server decides to allocate GPU with id "1"
|
|
|
|
// b) fingerprint check reports that GPU with id "1" is no more present
|
|
|
|
//
|
|
|
|
// The latest and always valid version of fingerprinted ids are stored in
|
|
|
|
// d.devices map. To avoid this race condition an error is returned if
|
|
|
|
// any of provided deviceIDs is not found in d.devices map
|
|
|
|
d.deviceLock.RLock()
|
|
|
|
var notExistingIDs []string
|
|
|
|
for _, id := range deviceIDs {
|
|
|
|
if _, deviceIDExists := d.devices[id]; !deviceIDExists {
|
|
|
|
notExistingIDs = append(notExistingIDs, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.deviceLock.RUnlock()
|
|
|
|
if len(notExistingIDs) != 0 {
|
|
|
|
return nil, &reservationError{notExistingIDs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &device.ContainerReservation{
|
|
|
|
Envs: map[string]string{
|
2018-12-18 01:03:43 +00:00
|
|
|
NvidiaVisibleDevices: strings.Join(deviceIDs, ","),
|
2018-09-18 16:57:39 +00:00
|
|
|
},
|
|
|
|
}, nil
|
2018-09-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stats streams statistics for the detected devices.
|
2018-09-28 17:09:01 +00:00
|
|
|
func (d *NvidiaDevice) Stats(ctx context.Context, interval time.Duration) (<-chan *device.StatsResponse, error) {
|
2020-07-21 14:11:16 +00:00
|
|
|
if !d.enabled {
|
|
|
|
return nil, device.ErrPluginDisabled
|
|
|
|
}
|
|
|
|
|
2018-09-07 17:13:50 +00:00
|
|
|
outCh := make(chan *device.StatsResponse)
|
2018-09-28 17:09:01 +00:00
|
|
|
go d.stats(ctx, outCh, interval)
|
2018-09-07 17:13:50 +00:00
|
|
|
return outCh, nil
|
2018-09-01 00:08:36 +00:00
|
|
|
}
|