-
Notifications
You must be signed in to change notification settings - Fork 8
/
fingerprint.go
232 lines (204 loc) · 7.12 KB
/
fingerprint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package nvidia
import (
"context"
"time"
"github.com/hashicorp/nomad-device-nvidia/nvml"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/hashicorp/nomad/plugins/device"
"github.com/hashicorp/nomad/plugins/shared/structs"
)
const (
// Attribute names and units for reporting Fingerprint output
MemoryAttr = "memory"
PowerAttr = "power"
BAR1Attr = "bar1"
DriverVersionAttr = "driver_version"
CoresClockAttr = "cores_clock"
MemoryClockAttr = "memory_clock"
PCIBandwidthAttr = "pci_bandwidth"
DisplayStateAttr = "display_state"
PersistenceModeAttr = "persistence_mode"
)
// fingerprint is the long running goroutine that detects hardware
func (d *NvidiaDevice) fingerprint(ctx context.Context, devices chan<- *device.FingerprintResponse) {
defer close(devices)
if d.initErr != nil {
if d.initErr.Error() != nvml.UnavailableLib.Error() {
d.logger.Error("exiting fingerprinting due to problems with NVML loading", "error", d.initErr)
devices <- device.NewFingerprintError(d.initErr)
}
// Just close the channel to let server know that there are no working
// Nvidia GPU units
return
}
// Create a timer that will fire immediately for the first detection
ticker := time.NewTimer(0)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
ticker.Reset(d.fingerprintPeriod)
}
d.writeFingerprintToChannel(devices)
}
}
// writeFingerprintToChannel makes nvml call and writes response to channel
func (d *NvidiaDevice) writeFingerprintToChannel(devices chan<- *device.FingerprintResponse) {
fingerprintData, err := d.nvmlClient.GetFingerprintData()
if err != nil {
d.logger.Error("failed to get fingerprint nvidia devices", "error", err)
devices <- device.NewFingerprintError(err)
return
}
// ignore devices from fingerprint output
fingerprintDevices := ignoreFingerprintedDevices(fingerprintData.Devices, d.ignoredGPUIDs)
// check if any device health was updated or any device was added to host
if !d.fingerprintChanged(fingerprintDevices) {
return
}
commonAttributes := map[string]*structs.Attribute{
DriverVersionAttr: {
String: pointer.Of(fingerprintData.DriverVersion),
},
}
// Group all FingerprintDevices by DeviceName attribute
deviceListByDeviceName := make(map[string][]*nvml.FingerprintDeviceData)
for _, device := range fingerprintDevices {
deviceName := device.DeviceName
if deviceName == nil {
// nvml driver was not able to detect device name. This kind
// of devices are placed to single group with 'notAvailable' name
notAvailableCopy := notAvailable
deviceName = ¬AvailableCopy
}
deviceListByDeviceName[*deviceName] = append(deviceListByDeviceName[*deviceName], device)
}
// Build Fingerprint response with computed groups and send it over the channel
deviceGroups := make([]*device.DeviceGroup, 0, len(deviceListByDeviceName))
for groupName, devices := range deviceListByDeviceName {
deviceGroups = append(deviceGroups, deviceGroupFromFingerprintData(groupName, devices, commonAttributes))
}
devices <- device.NewFingerprint(deviceGroups...)
}
// ignoreFingerprintedDevices excludes ignored devices from fingerprint output
func ignoreFingerprintedDevices(deviceData []*nvml.FingerprintDeviceData, ignoredGPUIDs map[string]struct{}) []*nvml.FingerprintDeviceData {
var result []*nvml.FingerprintDeviceData
for _, fingerprintDevice := range deviceData {
if _, ignored := ignoredGPUIDs[fingerprintDevice.UUID]; !ignored {
result = append(result, fingerprintDevice)
}
}
return result
}
// fingerprintChanged checks if there are any previously unseen nvidia devices located
// or any of fingerprinted nvidia devices disappeared since the last fingerprint run.
// Also, this func updates device map on NvidiaDevice with the latest data
func (d *NvidiaDevice) fingerprintChanged(allDevices []*nvml.FingerprintDeviceData) bool {
d.deviceLock.Lock()
defer d.deviceLock.Unlock()
changeDetected := false
// check if every device in allDevices is in d.devices
for _, device := range allDevices {
if _, ok := d.devices[device.UUID]; !ok {
changeDetected = true
}
}
// check if every device in d.devices is in allDevices
fingerprintDeviceMap := make(map[string]struct{})
for _, device := range allDevices {
fingerprintDeviceMap[device.UUID] = struct{}{}
}
for id := range d.devices {
if _, ok := fingerprintDeviceMap[id]; !ok {
changeDetected = true
}
}
d.devices = fingerprintDeviceMap
return changeDetected
}
// deviceGroupFromFingerprintData composes deviceGroup from FingerprintDeviceData slice
func deviceGroupFromFingerprintData(groupName string, deviceList []*nvml.FingerprintDeviceData, commonAttributes map[string]*structs.Attribute) *device.DeviceGroup {
// deviceGroup without devices makes no sense -> return nil when no devices are provided
if len(deviceList) == 0 {
return nil
}
devices := make([]*device.Device, len(deviceList))
for index, dev := range deviceList {
devices[index] = &device.Device{
ID: dev.UUID,
// all fingerprinted devices are "healthy" for now
// to get real health data -> dcgm bindings should be used
Healthy: true,
HwLocality: &device.DeviceLocality{
PciBusID: dev.PCIBusID,
},
}
}
deviceGroup := &device.DeviceGroup{
Vendor: vendor,
Type: deviceType,
Name: groupName,
Devices: devices,
// Assumption made that devices with the same DeviceName have the same
// attributes like amount of memory, power, bar1memory etc
Attributes: attributesFromFingerprintDeviceData(deviceList[0]),
}
// Extend attribute map with common attributes
for attributeKey, attributeValue := range commonAttributes {
deviceGroup.Attributes[attributeKey] = attributeValue
}
return deviceGroup
}
// attributesFromFingerprintDeviceData converts nvml.FingerprintDeviceData
// struct to device.DeviceGroup.Attributes format (map[string]string)
// this function performs all nil checks for FingerprintDeviceData pointers
func attributesFromFingerprintDeviceData(d *nvml.FingerprintDeviceData) map[string]*structs.Attribute {
attrs := map[string]*structs.Attribute{
DisplayStateAttr: {
String: pointer.Of(d.DisplayState),
},
PersistenceModeAttr: {
String: pointer.Of(d.PersistenceMode),
},
}
if d.MemoryMiB != nil {
attrs[MemoryAttr] = &structs.Attribute{
Int: pointer.Of(int64(*d.MemoryMiB)),
Unit: structs.UnitMiB,
}
}
if d.PowerW != nil {
attrs[PowerAttr] = &structs.Attribute{
Int: pointer.Of(int64(*d.PowerW)),
Unit: structs.UnitW,
}
}
if d.BAR1MiB != nil {
attrs[BAR1Attr] = &structs.Attribute{
Int: pointer.Of(int64(*d.BAR1MiB)),
Unit: structs.UnitMiB,
}
}
if d.CoresClockMHz != nil {
attrs[CoresClockAttr] = &structs.Attribute{
Int: pointer.Of(int64(*d.CoresClockMHz)),
Unit: structs.UnitMHz,
}
}
if d.MemoryClockMHz != nil {
attrs[MemoryClockAttr] = &structs.Attribute{
Int: pointer.Of(int64(*d.MemoryClockMHz)),
Unit: structs.UnitMHz,
}
}
if d.PCIBandwidthMBPerS != nil {
attrs[PCIBandwidthAttr] = &structs.Attribute{
Int: pointer.Of(int64(*d.PCIBandwidthMBPerS)),
Unit: structs.UnitMBPerS,
}
}
return attrs
}