forked from NVIDIA/go-dcgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
419 lines (370 loc) · 10.9 KB
/
policy.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package dcgm
/*
#include "dcgm_agent.h"
#include "dcgm_structs.h"
// wrapper for go callback function
extern int violationNotify(void* p);
*/
import "C"
import (
"encoding/binary"
"fmt"
"log"
"math/rand"
"sync"
"time"
"unsafe"
)
type policyCondition string
const (
DbePolicy = policyCondition("Double-bit ECC error")
PCIePolicy = policyCondition("PCI error")
MaxRtPgPolicy = policyCondition("Max Retired Pages Limit")
ThermalPolicy = policyCondition("Thermal Limit")
PowerPolicy = policyCondition("Power Limit")
NvlinkPolicy = policyCondition("Nvlink Error")
XidPolicy = policyCondition("XID Error")
)
type PolicyViolation struct {
Condition policyCondition
Timestamp time.Time
Data interface{}
}
type policyIndex int
const (
dbePolicyIndex policyIndex = iota
pciePolicyIndex
maxRtPgPolicyIndex
thermalPolicyIndex
powerPolicyIndex
nvlinkPolicyIndex
xidPolicyIndex
)
type policyConditionParam struct {
typ uint32
value uint32
}
type dbePolicyCondition struct {
Location string
NumErrors uint
}
type pciPolicyCondition struct {
ReplayCounter uint
}
type retiredPagesPolicyCondition struct {
SbePages uint
DbePages uint
}
type thermalPolicyCondition struct {
ThermalViolation uint
}
type powerPolicyCondition struct {
PowerViolation uint
}
type nvlinkPolicyCondition struct {
FieldId uint16
Counter uint
}
type xidPolicyCondition struct {
ErrNum uint
}
var (
policyChanOnce sync.Once
policyMapOnce sync.Once
// callbacks maps PolicyViolation channels with policy
// captures C callback() value for each violation condition
callbacks map[string]chan PolicyViolation
// paramMap maps C.dcgmPolicy_t.parms index and limits
// to be used in setPolicy() for setting user selected policies
paramMap map[policyIndex]policyConditionParam
)
func makePolicyChannels() {
policyChanOnce.Do(func() {
callbacks = make(map[string]chan PolicyViolation)
callbacks["dbe"] = make(chan PolicyViolation, 1)
callbacks["pcie"] = make(chan PolicyViolation, 1)
callbacks["maxrtpg"] = make(chan PolicyViolation, 1)
callbacks["thermal"] = make(chan PolicyViolation, 1)
callbacks["power"] = make(chan PolicyViolation, 1)
callbacks["nvlink"] = make(chan PolicyViolation, 1)
callbacks["xid"] = make(chan PolicyViolation, 1)
})
}
func makePolicyParmsMap() {
const (
policyFieldTypeBool = 0
policyFieldTypeLong = 1
policyBoolValue = 1
policyMaxRtPgThreshold = 10
policyThermalThreshold = 100
policyPowerThreshold = 250
)
policyMapOnce.Do(func() {
paramMap = make(map[policyIndex]policyConditionParam)
paramMap[dbePolicyIndex] = policyConditionParam{
typ: policyFieldTypeBool,
value: policyBoolValue,
}
paramMap[pciePolicyIndex] = policyConditionParam{
typ: policyFieldTypeBool,
value: policyBoolValue,
}
paramMap[maxRtPgPolicyIndex] = policyConditionParam{
typ: policyFieldTypeLong,
value: policyMaxRtPgThreshold,
}
paramMap[thermalPolicyIndex] = policyConditionParam{
typ: policyFieldTypeLong,
value: policyThermalThreshold,
}
paramMap[powerPolicyIndex] = policyConditionParam{
typ: policyFieldTypeLong,
value: policyPowerThreshold,
}
paramMap[nvlinkPolicyIndex] = policyConditionParam{
typ: policyFieldTypeBool,
value: policyBoolValue,
}
paramMap[xidPolicyIndex] = policyConditionParam{
typ: policyFieldTypeBool,
value: policyBoolValue,
}
})
}
// ViolationRegistration is a go callback function for dcgmPolicyRegister() wrapped in C.violationNotify()
//export ViolationRegistration
func ViolationRegistration(data unsafe.Pointer) int {
var con policyCondition
var timestamp time.Time
var val interface{}
response := *(*C.dcgmPolicyCallbackResponse_t)(unsafe.Pointer(data))
switch response.condition {
case C.DCGM_POLICY_COND_DBE:
dbe := (*C.dcgmPolicyConditionDbe_t)(unsafe.Pointer(&response.val))
con = DbePolicy
timestamp = createTimeStamp(dbe.timestamp)
val = dbePolicyCondition{
Location: dbeLocation(int(dbe.location)),
NumErrors: *uintPtr(dbe.numerrors),
}
case C.DCGM_POLICY_COND_PCI:
pci := (*C.dcgmPolicyConditionPci_t)(unsafe.Pointer(&response.val))
con = PCIePolicy
timestamp = createTimeStamp(pci.timestamp)
val = pciPolicyCondition{
ReplayCounter: *uintPtr(pci.counter),
}
case C.DCGM_POLICY_COND_MAX_PAGES_RETIRED:
mpr := (*C.dcgmPolicyConditionMpr_t)(unsafe.Pointer(&response.val))
con = MaxRtPgPolicy
timestamp = createTimeStamp(mpr.timestamp)
val = retiredPagesPolicyCondition{
SbePages: *uintPtr(mpr.sbepages),
DbePages: *uintPtr(mpr.dbepages),
}
case C.DCGM_POLICY_COND_THERMAL:
thermal := (*C.dcgmPolicyConditionThermal_t)(unsafe.Pointer(&response.val))
con = ThermalPolicy
timestamp = createTimeStamp(thermal.timestamp)
val = thermalPolicyCondition{
ThermalViolation: *uintPtr(thermal.thermalViolation),
}
case C.DCGM_POLICY_COND_POWER:
pwr := (*C.dcgmPolicyConditionPower_t)(unsafe.Pointer(&response.val))
con = PowerPolicy
timestamp = createTimeStamp(pwr.timestamp)
val = powerPolicyCondition{
PowerViolation: *uintPtr(pwr.powerViolation),
}
case C.DCGM_POLICY_COND_NVLINK:
nvlink := (*C.dcgmPolicyConditionNvlink_t)(unsafe.Pointer(&response.val))
con = NvlinkPolicy
timestamp = createTimeStamp(nvlink.timestamp)
val = nvlinkPolicyCondition{
FieldId: uint16(nvlink.fieldId),
Counter: *uintPtr(nvlink.counter),
}
case C.DCGM_POLICY_COND_XID:
xid := (*C.dcgmPolicyConditionXID_t)(unsafe.Pointer(&response.val))
con = XidPolicy
timestamp = createTimeStamp(xid.timestamp)
val = xidPolicyCondition{
ErrNum: *uintPtr(xid.errnum),
}
}
err := PolicyViolation{
Condition: con,
Timestamp: timestamp,
Data: val,
}
switch con {
case DbePolicy:
callbacks["dbe"] <- err
case PCIePolicy:
callbacks["pcie"] <- err
case MaxRtPgPolicy:
callbacks["maxrtpg"] <- err
case ThermalPolicy:
callbacks["thermal"] <- err
case PowerPolicy:
callbacks["power"] <- err
case NvlinkPolicy:
callbacks["nvlink"] <- err
case XidPolicy:
callbacks["xid"] <- err
}
return 0
}
func setPolicy(groupId GroupHandle, condition C.dcgmPolicyCondition_t, paramList []policyIndex) (err error) {
var policy C.dcgmPolicy_t
policy.version = makeVersion1(unsafe.Sizeof(policy))
policy.mode = C.dcgmPolicyMode_t(C.DCGM_OPERATION_MODE_AUTO)
policy.action = C.DCGM_POLICY_ACTION_NONE
policy.isolation = C.DCGM_POLICY_ISOLATION_NONE
policy.validation = C.DCGM_POLICY_VALID_NONE
policy.condition = condition
// iterate on paramMap for given policy conditions
for _, key := range paramList {
conditionParam, exists := paramMap[policyIndex(key)]
if !exists {
return fmt.Errorf("Error: Invalid Policy condition, %v does not exist.\n", key)
}
// set policy condition parameters
// set condition type (bool or longlong)
policy.parms[key].tag = conditionParam.typ
// set condition val (violation threshold)
// policy.parms.val is a C union type
// cgo docs: Go doesn't have support for C's union type
// C union types are represented as a Go byte array
binary.LittleEndian.PutUint32(policy.parms[key].val[:], conditionParam.value)
}
var statusHandle C.dcgmStatus_t
result := C.dcgmPolicySet(handle.handle, groupId.handle, &policy, statusHandle)
if err = errorString(result); err != nil {
return fmt.Errorf("Error setting policies: %s", err)
}
log.Println("Policy successfully set.")
return
}
func registerPolicy(gpuId uint, typ ...policyCondition) (violation chan PolicyViolation, err error) {
// init policy globals for internal API
makePolicyChannels()
makePolicyParmsMap()
name := fmt.Sprintf("policy%d", rand.Uint64())
groupId, err := CreateGroup(name)
if err != nil {
return
}
if err = AddToGroup(groupId, gpuId); err != nil {
return
}
// make a list of all callback channels
var channels []chan PolicyViolation
// make a list of policy conditions for setting their parameters
var paramKeys []policyIndex
// get all conditions to be set in setPolicy()
var condition C.dcgmPolicyCondition_t = 0
for _, t := range typ {
switch t {
case DbePolicy:
paramKeys = append(paramKeys, dbePolicyIndex)
condition |= C.DCGM_POLICY_COND_DBE
channels = append(channels, callbacks["dbe"])
case PCIePolicy:
paramKeys = append(paramKeys, pciePolicyIndex)
condition |= C.DCGM_POLICY_COND_PCI
channels = append(channels, callbacks["pcie"])
case MaxRtPgPolicy:
paramKeys = append(paramKeys, maxRtPgPolicyIndex)
condition |= C.DCGM_POLICY_COND_MAX_PAGES_RETIRED
channels = append(channels, callbacks["maxrtpg"])
case ThermalPolicy:
paramKeys = append(paramKeys, thermalPolicyIndex)
condition |= C.DCGM_POLICY_COND_THERMAL
channels = append(channels, callbacks["thermal"])
case PowerPolicy:
paramKeys = append(paramKeys, powerPolicyIndex)
condition |= C.DCGM_POLICY_COND_POWER
channels = append(channels, callbacks["power"])
case NvlinkPolicy:
paramKeys = append(paramKeys, nvlinkPolicyIndex)
condition |= C.DCGM_POLICY_COND_NVLINK
channels = append(channels, callbacks["nvlink"])
case XidPolicy:
paramKeys = append(paramKeys, xidPolicyIndex)
condition |= C.DCGM_POLICY_COND_XID
channels = append(channels, callbacks["xid"])
}
}
if err = setPolicy(groupId, condition, paramKeys); err != nil {
return
}
result := C.dcgmPolicyRegister(handle.handle, groupId.handle, C.dcgmPolicyCondition_t(condition), C.fpRecvUpdates(C.violationNotify), C.fpRecvUpdates(C.violationNotify))
if err = errorString(result); err != nil {
return violation, fmt.Errorf("Error registering policy: %s", err)
}
log.Println("Listening for violations...")
// create a publisher
publisher := newPublisher()
_ = publisher.add()
_ = publisher.add()
// broadcast
go publisher.broadcast()
go func() {
for {
select {
case dbe := <-callbacks["dbe"]:
publisher.send(dbe)
case pcie := <-callbacks["pcie"]:
publisher.send(pcie)
case maxrtpg := <-callbacks["maxrtpg"]:
publisher.send(maxrtpg)
case thermal := <-callbacks["thermal"]:
publisher.send(thermal)
case power := <-callbacks["power"]:
publisher.send(power)
case nvlink := <-callbacks["nvlink"]:
publisher.send(nvlink)
case xid := <-callbacks["xid"]:
publisher.send(xid)
}
}
}()
// merge
violation = make(chan PolicyViolation, len(channels))
go func() {
for _, c := range channels {
val := <-c
violation <- val
}
close(violation)
}()
_ = DestroyGroup(groupId)
return
}
func unregisterPolicy(groupId GroupHandle, condition C.dcgmPolicyCondition_t) {
result := C.dcgmPolicyUnregister(handle.handle, groupId.handle, condition)
if err := errorString(result); err != nil {
fmt.Errorf("Error unregistering policy: %s", err)
}
}
func createTimeStamp(t C.longlong) time.Time {
tm := int64(t) / 1000000
ts := time.Unix(tm, 0)
return ts
}
func dbeLocation(location int) string {
switch location {
case 0:
return "L1"
case 1:
return "L2"
case 2:
return "Device"
case 3:
return "Register"
case 4:
return "Texture"
}
return "N/A"
}