-
Notifications
You must be signed in to change notification settings - Fork 639
/
resource.go
316 lines (264 loc) · 8.41 KB
/
resource.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
/**
# 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 lm
import (
"fmt"
"regexp"
"strings"
spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1"
"github.com/NVIDIA/k8s-device-plugin/internal/resource"
)
const fullGPUResourceName = "nvidia.com/gpu"
// NewGPUResourceLabelerWithoutSharing creates a resource labeler for the specified device that does not apply sharing labels.
func NewGPUResourceLabelerWithoutSharing(device resource.Device, count int) (Labeler, error) {
// NOTE: We use a nil config to signal that sharing is disabled.
return NewGPUResourceLabeler(nil, device, count)
}
// NewGPUResourceLabeler creates a resource labeler for the specified full GPU device with the specified count
func NewGPUResourceLabeler(config *spec.Config, device resource.Device, count int) (Labeler, error) {
if count == 0 {
return empty{}, nil
}
model, err := device.GetName()
if err != nil {
return nil, fmt.Errorf("failed to get device model: %v", err)
}
totalMemoryMB, err := device.GetTotalMemoryMB()
if err != nil {
return nil, fmt.Errorf("failed to get memory info for device: %v", err)
}
resourceLabeler := newResourceLabeler(fullGPUResourceName, config)
architectureLabels, err := newArchitectureLabels(resourceLabeler, device)
if err != nil {
return nil, fmt.Errorf("failed to create architecture labels: %v", err)
}
memoryLabeler := (Labeler)(&empty{})
if totalMemoryMB != 0 {
memoryLabeler = resourceLabeler.single("memory", totalMemoryMB)
}
labelers := Merge(
resourceLabeler.baseLabeler(count, model),
memoryLabeler,
architectureLabels,
)
return labelers, nil
}
// NewMIGResourceLabeler creates a resource labeler for the specified full GPU device with the specified resource name.
func NewMIGResourceLabeler(resourceName spec.ResourceName, config *spec.Config, device resource.Device, count int) (Labeler, error) {
if count == 0 {
return empty{}, nil
}
parent, err := device.GetDeviceHandleFromMigDeviceHandle()
if err != nil {
return nil, fmt.Errorf("failed to get parent of MIG device: %v", err)
}
model, err := parent.GetName()
if err != nil {
return nil, fmt.Errorf("failed to get device model: %v", err)
}
migProfile, err := device.GetName()
if err != nil {
return nil, fmt.Errorf("failed to get MIG profile name: %v", err)
}
resourceLabeler := newResourceLabeler(resourceName, config)
attributeLabels, err := newMigAttributeLabels(resourceLabeler, device)
if err != nil {
return nil, fmt.Errorf("faled to get MIG attribute labels: %v", err)
}
labelers := Merge(
resourceLabeler.baseLabeler(count, model, "MIG", migProfile),
attributeLabels,
)
return labelers, nil
}
func newResourceLabeler(resourceName spec.ResourceName, config *spec.Config) resourceLabeler {
var sharing *spec.Sharing
if config != nil {
sharing = &config.Sharing
}
return resourceLabeler{
resourceName: resourceName,
sharing: sharing,
}
}
type resourceLabeler struct {
resourceName spec.ResourceName
sharing *spec.Sharing
}
// single creates a single label for the resource. The label key is
// <fully-qualified-resource-name>.suffix
func (rl resourceLabeler) single(suffix string, value interface{}) Labels {
return rl.labels(map[string]interface{}{suffix: value})
}
// labels creates a set of labels from the specified map for the resource.
// Each key in the map corresponds to a label <fully-qualified-resource-name>.key
func (rl resourceLabeler) labels(suffixValues map[string]interface{}) Labels {
labels := make(Labels)
for suffix, value := range suffixValues {
rl.updateLabel(labels, suffix, value)
}
return labels
}
// updateLabel modifies the specified labels, updating <fully-qualified-resource-name>.suffix with
// the provided value.
func (rl resourceLabeler) updateLabel(labels Labels, suffix string, value interface{}) {
key := rl.key(suffix)
labels[key] = fmt.Sprintf("%v", value)
}
// key generates the label key for the specified suffix. The key is generated as
// <fully-qualified-resource-name>.suffix
func (rl resourceLabeler) key(suffix string) string {
return string(rl.resourceName) + "." + suffix
}
// baseLabeler generates the product, count, and replicas labels for the resource
func (rl resourceLabeler) baseLabeler(count int, parts ...string) Labeler {
replicas := rl.getReplicas()
strategy := spec.SharingStrategyNone
if rl.sharing != nil && replicas > 1 {
strategy = rl.sharing.SharingStrategy()
}
rawLabels := map[string]interface{}{
"product": rl.getProductName(parts...),
"count": count,
"replicas": replicas,
"sharing-strategy": strategy,
}
labels := make(Labels)
for k, v := range rawLabels {
labels[rl.key(k)] = fmt.Sprintf("%v", v)
}
return labels
}
// Deprecated
func (rl resourceLabeler) productLabel(parts ...string) Labels {
name := rl.getProductName(parts...)
if name == "" {
return make(Labels)
}
return rl.single("product", name)
}
func (rl resourceLabeler) getProductName(parts ...string) string {
var strippedParts []string
for _, p := range parts {
if p != "" {
sanitisedPart := sanitise(p)
strippedParts = append(strippedParts, sanitisedPart)
}
}
if len(strippedParts) == 0 {
return ""
}
if rl.isShared() && !rl.isRenamed() {
strippedParts = append(strippedParts, "SHARED")
}
return strings.Join(strippedParts, "-")
}
func (rl resourceLabeler) getReplicas() int {
if rl.sharingDisabled() {
return 0
} else if r := rl.replicationInfo(); r != nil && r.Replicas > 0 {
return r.Replicas
}
return 1
}
// sharingDisabled checks whether the resourceLabeler has sharing disabled
// TODO: The nil check here is because we call NewGPUResourceLabeler with a nil config when sharing is disabled.
func (rl resourceLabeler) sharingDisabled() bool {
return rl.sharing == nil
}
// isShared checks whether the resource is shared.
func (rl resourceLabeler) isShared() bool {
if r := rl.replicationInfo(); r != nil && r.Replicas > 1 {
return true
}
return false
}
// isRenamed checks whether the resource is renamed.
func (rl resourceLabeler) isRenamed() bool {
if r := rl.replicationInfo(); r != nil && r.Rename != "" {
return true
}
return false
}
// replicationInfo searches the associated config for the resource and returns the replication info
func (rl resourceLabeler) replicationInfo() *spec.ReplicatedResource {
if rl.sharingDisabled() {
return nil
}
for _, r := range rl.sharing.ReplicatedResources().Resources {
if r.Name == rl.resourceName {
return &r
}
}
return nil
}
func newMigAttributeLabels(rl resourceLabeler, device resource.Device) (Labels, error) {
attributes, err := device.GetAttributes()
if err != nil {
return nil, fmt.Errorf("unable to get attributes of MIG device: %v", err)
}
labels := rl.labels(attributes)
return labels, nil
}
func newArchitectureLabels(rl resourceLabeler, device resource.Device) (Labels, error) {
computeMajor, computeMinor, err := device.GetCudaComputeCapability()
if err != nil {
return nil, fmt.Errorf("failed to determine CUDA compute capability: %v", err)
}
if computeMajor == 0 {
return make(Labels), nil
}
family := getArchFamily(computeMajor, computeMinor)
labels := rl.labels(map[string]interface{}{
"family": family,
"compute.major": computeMajor,
"compute.minor": computeMinor,
})
return labels, nil
}
// TODO: This should a function in go-nvlib
func getArchFamily(computeMajor, computeMinor int) string {
switch computeMajor {
case 1:
return "tesla"
case 2:
return "fermi"
case 3:
return "kepler"
case 5:
return "maxwell"
case 6:
return "pascal"
case 7:
if computeMinor < 5 {
return "volta"
}
return "turing"
case 8:
return "ampere"
case 9:
return "hopper"
}
return "undefined"
}
func sanitise(input string) string {
var sanitised string
re := regexp.MustCompile("[^A-Za-z0-9-_. ]")
input = re.ReplaceAllString(input, "")
// remove redundant blank spaces
sanitised = strings.Join(strings.Fields(input), "-")
return sanitised
}