-
Notifications
You must be signed in to change notification settings - Fork 52
/
resources.go
244 lines (218 loc) · 6.14 KB
/
resources.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
// Copyright 2017 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package reflow
import (
"bytes"
"fmt"
"math"
"sort"
"github.com/grailbio/base/data"
)
const (
mem = "mem"
cpu = "cpu"
disk = "disk"
)
// ResourcesKeys are the type of labeled resources typically used.
// ResourcesKeys does not include disk because it is not updated/used for most practical purposes.
var ResourcesKeys = []string{mem, cpu}
// Resources describes a set of labeled resources. Each resource is
// described by a string label and assigned a value. The zero value
// of Resources represents the resources with zeros for all labels.
type Resources map[string]float64
// String renders a Resources. All nonzero-valued labels are included;
// mem, cpu, and disk are always included regardless of their value.
func (r Resources) String() string {
var b bytes.Buffer
b.WriteString("{")
r.writeResources(&b)
b.WriteString("}")
return b.String()
}
func (r Resources) writeResources(b *bytes.Buffer) {
if r[mem] != 0 || r[cpu] != 0 || r[disk] != 0 {
fmt.Fprintf(b, "mem:%s cpu:%g disk:%s", data.Size(r[mem]), r[cpu], data.Size(r[disk]))
}
var keys []string
for key := range r {
switch key {
case mem, cpu, disk:
default:
keys = append(keys, key)
}
}
sort.Strings(keys)
for _, key := range keys {
if r[key] == 0 {
continue
}
fmt.Fprintf(b, " %s:%g", key, r[key])
}
}
// Available tells if s resources are available from r.
func (r Resources) Available(s Resources) bool {
for key := range s {
if r[key] < s[key] {
return false
}
}
return true
}
// Sub sets r to the difference x[key]-y[key] for all keys and returns r.
func (r *Resources) Sub(x, y Resources) *Resources {
r.Set(x)
for key := range y {
(*r)[key] = x[key] - y[key]
}
return r
}
// Add sets r to the sum x[key]+y[key] for all keys and returns r.
func (r *Resources) Add(x, y Resources) *Resources {
r.Set(x)
for key := range y {
(*r)[key] += y[key]
}
return r
}
// Set sets r[key]=s[key] for all keys and returns r.
func (r *Resources) Set(s Resources) *Resources {
*r = make(Resources)
for key, val := range s {
(*r)[key] = val
}
return r
}
// Min sets r to the minimum min(x[key], y[key]) for all keys
// and returns r.
func (r *Resources) Min(x, y Resources) *Resources {
r.Set(x)
for key, val := range y {
if val < (*r)[key] {
(*r)[key] = val
}
}
return r
}
// Max sets r to the maximum max(x[key], y[key]) for all keys
// and returns r.
func (r *Resources) Max(x, y Resources) *Resources {
r.Set(x)
for key, val := range y {
if val > (*r)[key] {
(*r)[key] = val
}
}
return r
}
// Scale sets r to the scaled resources s[key]*factor for all keys
// and returns r.
func (r *Resources) Scale(s Resources, factor float64) *Resources {
if *r == nil {
*r = make(Resources)
}
for key, val := range s {
(*r)[key] = val * factor
}
return r
}
// ScaledDistance returns the distance between two resources computed as a sum
// of the differences in memory, cpu and disk with some predefined scaling.
func (r Resources) ScaledDistance(u Resources) float64 {
// Consider 8G Memory and 1 CPU are somewhat the same cost
// when we compute "distance" between the resources.
// % reflow ec2instances | grep -v usable | awk '{print $2, $4}' | sed 's/GiB/*1024/g' | sed 's/TiB/*1024*1024/g' | awk '{s += $1/$2; n++} END{print s/n}'
// 8.06203
const (
G = 1 << 30
memoryScaling = 1.0 / (8 * G)
cpuScaling = 1
)
return math.Abs(float64(r[mem])-float64(u[mem]))*memoryScaling +
math.Abs(float64(r[cpu])-float64(u[cpu]))*cpuScaling
}
// Equal tells whether the resources r and s are equal in all dimensions
// of both r and s.
func (r Resources) Equal(s Resources) bool {
for key, val := range s {
if r[key] != val {
return false
}
}
for key, val := range r {
if s[key] != val {
return false
}
}
return true
}
// Div returns a mapping of the intersection of keys in r and s to the fraction r[key]/s[key].
// Since the returned value cannot be treated as Resources, Div simply returns a map.
func (r Resources) Div(s Resources) map[string]float64 {
f := make(map[string]float64)
for key, rv := range r {
if sv, ok := s[key]; ok {
f[key] = rv / sv
}
}
return f
}
// MaxRatio computes the max across ratios of values in r to s for the intersection of keys in r and s.
func (r Resources) MaxRatio(s Resources) (max float64) {
for _, v := range r.Div(s) {
if v > max {
max = v
}
}
return
}
// Requirements stores resource requirements, comprising the minimum
// amount of acceptable resources and a width.
type Requirements struct {
// Min is the smallest amount of resources that must be allocated
// to satisfy the requirements.
Min Resources
// Width is the width of the requirements. A width of zero indicates
// a "narrow" job: minimum describes the exact resources needed.
// Widths greater than zero require a multiple (ie, 1 + Width) of the minimum requirement.
Width int
}
// AddParallel adds the provided resources s to the requirements,
// and also increases the requirement's width by one.
func (r *Requirements) AddParallel(s Resources) {
empty := r == nil || r.Min.Equal(nil)
r.Min.Max(r.Min, s)
if !empty {
r.Width++
}
}
// AddSerial adds the provided resources s to the requirements.
func (r *Requirements) AddSerial(s Resources) {
r.Min.Max(r.Min, s)
}
// Max is the maximum amount of resources represented by this
// resource request.
func (r *Requirements) Max() Resources {
var max Resources
max.Scale(r.Min, float64(1+r.Width))
return max
}
// Add adds the provided requirements s to the requirements r.
// R's minimum requirements are set to the larger of the two;
// the two widths are added.
func (r *Requirements) Add(s Requirements) {
r.Min.Max(r.Min, s.Min)
r.Width += s.Width
}
// Equal reports whether r and s represent the same requirements.
func (r Requirements) Equal(s Requirements) bool {
return r.Min.Equal(s.Min) && r.Width == s.Width
}
// String renders a human-readable representation of r.
func (r Requirements) String() string {
s := r.Min.String()
if r.Width > 1 {
return s + fmt.Sprintf("#%d", r.Width)
}
return s
}