/
resource.go
343 lines (304 loc) · 8.15 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
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
package models
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/BTBurke/k8sresource"
"github.com/dustin/go-humanize"
"github.com/hashicorp/go-multierror"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
)
type ResourcesConfig struct {
// CPU https://github.com/BTBurke/k8sresource string
CPU string `json:"CPU,omitempty"`
// Memory github.com/dustin/go-humanize string
Memory string `json:"Memory,omitempty"`
// Memory github.com/dustin/go-humanize string
Disk string `json:"Disk,omitempty"`
GPU string `json:"GPU,omitempty"`
}
// Normalize normalizes the resources
func (r *ResourcesConfig) Normalize() {
if r == nil {
return
}
sanitizeResourceString := func(s string) string {
// lower case and remove spaces
s = strings.ToLower(s)
s = strings.ReplaceAll(s, " ", "")
s = strings.ReplaceAll(s, "\n", "")
return s
}
r.CPU = sanitizeResourceString(r.CPU)
r.Memory = sanitizeResourceString(r.Memory)
r.Disk = sanitizeResourceString(r.Disk)
r.GPU = sanitizeResourceString(r.GPU)
}
// Copy returns a deep copy of the resources
func (r *ResourcesConfig) Copy() *ResourcesConfig {
if r == nil {
return nil
}
newR := new(ResourcesConfig)
*newR = *r
return newR
}
// Validate returns an error if the resources are invalid
func (r *ResourcesConfig) Validate() error {
if r == nil {
return errors.New("missing resources")
}
resources, err := r.ToResources()
if err != nil {
return err
}
return resources.Validate()
}
// ToResources converts the resources config to resources
func (r *ResourcesConfig) ToResources() (*Resources, error) {
if r == nil {
return nil, errors.New("missing resources")
}
r.Normalize()
var mErr multierror.Error
res := &Resources{}
if r.CPU != "" {
cpu, err := k8sresource.NewCPUFromString(r.CPU)
if err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("invalid CPU value: %s", r.CPU))
}
res.CPU = cpu.ToFloat64()
}
if r.Memory != "" {
mem, err := humanize.ParseBytes(r.Memory)
if err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("invalid memory value: %s", r.Memory))
}
res.Memory = mem
}
if r.Disk != "" {
disk, err := humanize.ParseBytes(r.Disk)
if err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("invalid disk value: %s", r.Disk))
}
res.Disk = disk
}
if r.GPU != "" {
gpu, err := strconv.ParseUint(r.GPU, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid GPU value: %s", r.GPU)
}
res.GPU = gpu
}
return res, mErr.ErrorOrNil()
}
type ResourcesConfigBuilder struct {
resources *ResourcesConfig
}
func NewResourcesConfigBuilder() *ResourcesConfigBuilder {
return &ResourcesConfigBuilder{resources: &ResourcesConfig{}}
}
func (r *ResourcesConfigBuilder) CPU(cpu string) *ResourcesConfigBuilder {
r.resources.CPU = cpu
return r
}
func (r *ResourcesConfigBuilder) Memory(memory string) *ResourcesConfigBuilder {
r.resources.Memory = memory
return r
}
func (r *ResourcesConfigBuilder) Disk(disk string) *ResourcesConfigBuilder {
r.resources.Disk = disk
return r
}
func (r *ResourcesConfigBuilder) GPU(gpu string) *ResourcesConfigBuilder {
r.resources.GPU = gpu
return r
}
func (r *ResourcesConfigBuilder) Build() (*ResourcesConfig, error) {
r.resources.Normalize()
return r.resources, r.resources.Validate()
}
// BuildOrDie is the same as Build, but panics if an error occurs
func (r *ResourcesConfigBuilder) BuildOrDie() *ResourcesConfig {
resources, err := r.Build()
if err != nil {
panic(err)
}
return resources
}
type GPUVendor string
const (
GPUVendorNvidia GPUVendor = "NVIDIA"
GPUVendorAMDATI GPUVendor = "AMD/ATI"
GPUVendorIntel GPUVendor = "Intel"
)
type GPU struct {
// Self-reported index of the device in the system
Index uint64
// Model name of the GPU e.g. Tesla T4
Name string
// Maker of the GPU, e.g. NVidia, AMD, Intel
Vendor GPUVendor
// Total GPU memory in mebibytes (MiB)
Memory uint64
// PCI address of the device, in the format AAAA:BB:CC.C
// Used to discover the correct device rendering cards
PCIAddress string
}
type Resources struct {
// CPU units
CPU float64 `json:"CPU,omitempty"`
// Memory in bytes
Memory uint64 `json:"Memory,omitempty"`
// Disk in bytes
Disk uint64 `json:"Disk,omitempty"`
// GPU units
GPU uint64 `json:"GPU,omitempty"`
// GPU details
GPUs []GPU `json:"GPUs,omitempty"`
}
// Copy returns a deep copy of the resources
func (r *Resources) Copy() *Resources {
if r == nil {
return nil
}
newR := new(Resources)
*newR = *r
return newR
}
// Validate returns an error if the resources are invalid
func (r *Resources) Validate() error {
if r == nil {
return errors.New("missing resources")
}
var mErr multierror.Error
if r.CPU < 0 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("invalid CPU value: %f", r.CPU))
}
if len(r.GPUs) > int(r.GPU) {
// It's not an error for the GPUs specified to be less than the number
// given by the GPU field – that just signifies that either:
// - the user is requesting "generic GPUs" without specifying more information
// - the system knows it has GPUs but no further information about them
// But the number should always be at least the length of the GPUs array
mErr.Errors = append(mErr.Errors, fmt.Errorf("%d GPUs specified but have details for %d", r.GPU, len(r.GPUs)))
}
return mErr.ErrorOrNil()
}
// Merge merges the resources, preferring the current resources
func (r *Resources) Merge(other Resources) *Resources {
newR := r.Copy()
if newR.CPU <= 0 {
newR.CPU = other.CPU
}
if newR.Memory <= 0 {
newR.Memory = other.Memory
}
if newR.Disk <= 0 {
newR.Disk = other.Disk
}
if newR.GPU <= 0 {
newR.GPU = other.GPU
}
if len(newR.GPUs) <= 0 {
newR.GPUs = other.GPUs
}
return newR
}
// Add returns the sum of the resources
func (r *Resources) Add(other Resources) *Resources {
return &Resources{
CPU: r.CPU + other.CPU,
Memory: r.Memory + other.Memory,
Disk: r.Disk + other.Disk,
GPU: r.GPU + other.GPU,
GPUs: append(r.GPUs, other.GPUs...),
}
}
func (r *Resources) Sub(other Resources) *Resources {
usage := &Resources{
CPU: r.CPU - other.CPU,
Memory: r.Memory - other.Memory,
Disk: r.Disk - other.Disk,
GPU: r.GPU - other.GPU,
}
usage.GPUs, _ = lo.Difference(r.GPUs, other.GPUs)
if r.LessThan(other) {
log.Warn().Msgf("Subtracting larger resource usage %s from %s. Replacing negative values with zeros",
other.String(), r.String())
if other.CPU > r.CPU {
usage.CPU = 0
}
if other.Memory > r.Memory {
usage.Memory = 0
}
if other.Disk > r.Disk {
usage.Disk = 0
}
if other.GPU > r.GPU {
usage.GPU = 0
}
}
return usage
}
func (r *Resources) LessThan(other Resources) bool {
return r.CPU < other.CPU && r.Memory < other.Memory && r.Disk < other.Disk && r.GPU < other.GPU
}
func (r *Resources) LessThanEq(other Resources) bool {
return r.CPU <= other.CPU && r.Memory <= other.Memory && r.Disk <= other.Disk && r.GPU <= other.GPU
}
func (r *Resources) Max(other Resources) *Resources {
newR := r.Copy()
if newR.CPU < other.CPU {
newR.CPU = other.CPU
}
if newR.Memory < other.Memory {
newR.Memory = other.Memory
}
if newR.Disk < other.Disk {
newR.Disk = other.Disk
}
if newR.GPU < other.GPU {
newR.GPU = other.GPU
}
return newR
}
func (r *Resources) IsZero() bool {
return r.CPU == 0 && r.Memory == 0 && r.Disk == 0 && r.GPU == 0
}
// return string representation of ResourceUsageData
func (r *Resources) String() string {
mem := humanize.Bytes(r.Memory)
disk := humanize.Bytes(r.Disk)
return fmt.Sprintf("{CPU: %f, Memory: %s, Disk: %s, GPU: %d}", r.CPU, mem, disk, r.GPU)
}
// AllocatedResources is the set of resources to be used by an execution, which
// maybe be resources allocated to a single task or a set of tasks in the future.
type AllocatedResources struct {
Tasks map[string]*Resources `json:"Tasks"`
}
func (a *AllocatedResources) Copy() *AllocatedResources {
if a == nil {
return a
}
tasks := make(map[string]*Resources)
for k, v := range a.Tasks {
tasks[k] = v.Copy()
}
return &AllocatedResources{
Tasks: tasks,
}
}
// Total returns the total resources allocated
func (a *AllocatedResources) Total() *Resources {
if a == nil {
return nil
}
total := &Resources{}
for _, task := range a.Tasks {
total = total.Add(*task)
}
return total
}