-
Notifications
You must be signed in to change notification settings - Fork 90
/
cpu_assignment.go
340 lines (297 loc) · 9.73 KB
/
cpu_assignment.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
/*
Copyright 2022 The Katalyst Authors.
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 calculator
import (
"errors"
"fmt"
"sort"
"k8s.io/klog/v2"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)
// cpuAccumulator is used as a helper function to calculate cpu detailed
// allocation results according to machine topology and cpu requirements;
// it uses a stable allocation strategy, meaning that every time we need
// a fixed number of cpu cores based a fixed cpu topology, we will always
// get a fixed allocation results.
type cpuAccumulator struct {
// numCPUsNeeded records the account of cpus that we needed
numCPUsNeeded int
cpuDetails machine.CPUDetails
cpuTopology *machine.CPUTopology
result machine.CPUSet
}
func newCPUAccumulator(machineInfo *machine.KatalystMachineInfo, availableCPUs machine.CPUSet, numCPUs int) *cpuAccumulator {
return &cpuAccumulator{
numCPUsNeeded: numCPUs,
cpuTopology: machineInfo.CPUTopology,
cpuDetails: machineInfo.CPUDetails.KeepOnly(availableCPUs),
result: machine.NewCPUSet(),
}
}
func (a *cpuAccumulator) getDetails() machine.CPUDetails {
return a.cpuDetails
}
func (a *cpuAccumulator) getTopology() *machine.CPUTopology {
return a.cpuTopology
}
// freeCPUsInNUMANode returns free cpu ids in specified NUMA,
// and those returned cpu slices have already been sorted
func (a *cpuAccumulator) freeCPUsInNUMANode(numaID int) []int {
return a.cpuDetails.CPUsInNUMANodes(numaID).ToSliceInt()
}
// freeCoresInNUMANode returns free core ids in specified NUMA
func (a *cpuAccumulator) freeCoresInNUMANode(numaID int) []int {
return a.cpuDetails.CoresInNUMANodes(numaID).Filter(a.isCoreFree).ToSliceInt()
}
// isSocketFree returns true if the supplied socket is fully available
func (a *cpuAccumulator) isSocketFree(socketID int) bool {
return a.cpuDetails.CPUsInSockets(socketID).Size() == a.getTopology().CPUsPerSocket()
}
// isCoreFree returns true if the supplied core is fully available
func (a *cpuAccumulator) isCoreFree(coreID int) bool {
return a.cpuDetails.CPUsInCores(coreID).Size() == a.getTopology().CPUsPerCore()
}
// freeSockets returns free socket IDs as a slice sorted by sortAvailableSockets().
func (a *cpuAccumulator) freeSockets() []int {
free := []int{}
for _, socket := range a.sortAvailableSockets() {
if a.isSocketFree(socket) {
free = append(free, socket)
}
}
return free
}
// freeCores returns free core IDs as a slice sorted by sortAvailableCores().
func (a *cpuAccumulator) freeCores() []int {
free := []int{}
for _, core := range a.sortAvailableCores() {
if a.isCoreFree(core) {
free = append(free, core)
}
}
return free
}
// freeCPUs returns free CPU IDs as a slice sorted by sortAvailableCPUs().
func (a *cpuAccumulator) freeCPUs() []int {
return a.sortAvailableCPUs()
}
// sort the provided list of sockets/cores/cpus referenced in 'ids' by the
// number of available CPUs contained within them (smallest to largest). The
// 'getCPU()' parameter defines the function that should be called to retrieve
// the list of available CPUs for the type of socket/core/cpu being referenced.
// If two sockets/cores/cpus have the same number of available CPUs, they are
// sorted in ascending order by their id.
func (a *cpuAccumulator) sort(ids []int, getCPUs func(ids ...int) machine.CPUSet) {
sort.Slice(ids,
func(i, j int) bool {
iCPUs := getCPUs(ids[i])
jCPUs := getCPUs(ids[j])
if iCPUs.Size() < jCPUs.Size() {
return true
}
if iCPUs.Size() > jCPUs.Size() {
return false
}
return ids[i] < ids[j]
})
}
// Sort all sockets with free CPUs using the sort() algorithm defined above.
func (a *cpuAccumulator) sortAvailableSockets() []int {
sockets := a.cpuDetails.Sockets().ToSliceNoSortInt()
a.sort(sockets, a.cpuDetails.CPUsInSockets)
return sockets
}
// Sort all cores with free CPUs:
// - First by socket using sortAvailableSockets().
// - Then within each socket, using the sort() algorithm defined above.
func (a *cpuAccumulator) sortAvailableCores() []int {
var result []int
for _, socket := range a.sortAvailableSockets() {
cores := a.cpuDetails.CoresInSockets(socket).ToSliceNoSortInt()
a.sort(cores, a.cpuDetails.CPUsInCores)
result = append(result, cores...)
}
return result
}
// Sort all available CPUs:
// - First by core using sortAvailableCores().
// - Then within each core, using the sort() algorithm defined above.
func (a *cpuAccumulator) sortAvailableCPUs() []int {
var result []int
for _, core := range a.sortAvailableCores() {
cpus := a.cpuDetails.CPUsInCores(core).ToSliceNoSortInt()
sort.Ints(cpus)
result = append(result, cpus...)
}
return result
}
func (a *cpuAccumulator) take(cpus machine.CPUSet) {
a.result = a.result.Union(cpus)
a.cpuDetails = a.cpuDetails.KeepOnly(a.cpuDetails.CPUs().Difference(a.result))
a.numCPUsNeeded -= cpus.Size()
}
func (a *cpuAccumulator) takeFullSockets() {
for _, socket := range a.freeSockets() {
cpusInSocket := a.getTopology().CPUDetails.CPUsInSockets(socket)
if !a.needs(cpusInSocket.Size()) {
continue
}
klog.V(4).InfoS("takeFullSockets: claiming socket", "socket", socket)
a.take(cpusInSocket)
}
}
func (a *cpuAccumulator) takeFullCores() {
for _, core := range a.freeCores() {
cpusInCore := a.getTopology().CPUDetails.CPUsInCores(core)
if !a.needs(cpusInCore.Size()) {
continue
}
klog.V(4).InfoS("takeFullCores: claiming core", "core", core)
a.take(cpusInCore)
}
}
func (a *cpuAccumulator) takeRemainingCPUs() {
for _, cpu := range a.sortAvailableCPUs() {
klog.V(4).InfoS("takeRemainingCPUs: claiming CPU", "cpu", cpu)
a.take(machine.NewCPUSet(cpu))
if a.isSatisfied() {
return
}
}
}
func (a *cpuAccumulator) needs(n int) bool {
return a.numCPUsNeeded >= n
}
func (a *cpuAccumulator) isSatisfied() bool {
return a.numCPUsNeeded < 1
}
func (a *cpuAccumulator) isFailed() bool {
return a.numCPUsNeeded > a.cpuDetails.CPUs().Size()
}
// TakeByTopology tries to allocate those required cpus in the same socket or cores
func TakeByTopology(info *machine.KatalystMachineInfo, availableCPUs machine.CPUSet,
cpuRequirement int,
) (machine.CPUSet, error) {
acc := newCPUAccumulator(info, availableCPUs, cpuRequirement)
if acc.isSatisfied() {
return acc.result.Clone(), nil
}
if acc.isFailed() {
return machine.NewCPUSet(), fmt.Errorf("not enough cpus available to satisfy request")
}
// Algorithm: topology-aware best-fit
// 1. Acquire whole sockets, if available and the container requires at
// least a socket's-worth of CPUs.
acc.takeFullSockets()
if acc.isSatisfied() {
return acc.result.Clone(), nil
}
// 2. Acquire whole cores, if available and the container requires at least
// a core's-worth of CPUs.
acc.takeFullCores()
if acc.isSatisfied() {
return acc.result.Clone(), nil
}
// 3. Acquire single threads, preferring to fill partially-allocated cores
// on the same sockets as the whole cores we have already taken in this
// allocation.
acc.takeRemainingCPUs()
if acc.isSatisfied() {
return acc.result.Clone(), nil
}
return machine.NewCPUSet(), fmt.Errorf("failed to allocate cpus")
}
// TakeByNUMABalance tries to make the allocated cpu spread on different
// sockets, and it uses cpu Cores as the basic allocation unit
func TakeByNUMABalance(info *machine.KatalystMachineInfo, availableCPUs machine.CPUSet,
cpuRequirement int,
) (machine.CPUSet, machine.CPUSet, error) {
var err error
acc := newCPUAccumulator(info, availableCPUs, cpuRequirement)
if acc.isSatisfied() {
goto successful
}
for {
if acc.isFailed() {
err = fmt.Errorf("not enough cpus available to satisfy request")
goto failed
}
numaLoop:
for _, s := range info.CPUDetails.NUMANodes().ToSliceInt() {
if acc.needs(acc.getTopology().CPUsPerCore()) && len(acc.freeCores()) > 0 {
for _, c := range acc.freeCoresInNUMANode(s) {
acc.take(acc.getDetails().CPUsInCores(c))
if acc.isSatisfied() {
goto successful
} else {
continue numaLoop
}
}
continue
}
for _, c := range acc.freeCPUsInNUMANode(s) {
if acc.needs(1) {
acc.take(machine.NewCPUSet(c))
}
if acc.isSatisfied() {
goto successful
} else {
break
}
}
}
}
failed:
if err == nil {
err = errors.New("failed to allocate cpus")
}
return availableCPUs, availableCPUs, err
successful:
return acc.result.Clone(), availableCPUs.Difference(acc.result), nil
}
// TakeHTByNUMABalance tries to make the allocated cpu spread on different
// sockets, and it uses cpu HT as the basic allocation unit
func TakeHTByNUMABalance(info *machine.KatalystMachineInfo, availableCPUs machine.CPUSet,
cpuRequirement int,
) (machine.CPUSet, machine.CPUSet, error) {
var err error
acc := newCPUAccumulator(info, availableCPUs, cpuRequirement)
if acc.isSatisfied() {
goto successful
}
for {
if acc.isFailed() {
err = fmt.Errorf("not enough cpus available to satisfy request")
goto failed
}
for _, s := range info.CPUDetails.NUMANodes().ToSliceInt64() {
for _, c := range acc.freeCPUsInNUMANode(int(s)) {
if acc.needs(1) {
acc.take(machine.NewCPUSet(c))
}
if acc.isSatisfied() {
goto successful
} else {
break
}
}
}
}
failed:
if err == nil {
err = errors.New("failed to allocate cpus")
}
return availableCPUs, availableCPUs, err
successful:
return acc.result.Clone(), availableCPUs.Difference(acc.result), nil
}