Skip to content

Commit 26e1c1c

Browse files
committed
kubelet/cm: code optimization for the static policy
Minor optimization in the code that attempts to assign whole sockets/cores in case the number of CPUs requested is higher than CPUs-per-socket/core: check if the number of requested CPUs is higher than CPUs-per-socket/core before retrieving and iterating the free sockets/cores, and break the loops when that is no longer the case. Signed-off-by: Arik Hadas <[email protected]>
1 parent f8024ab commit 26e1c1c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pkg/kubelet/cm/cpumanager/cpu_assignment.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,31 @@ func takeByTopology(topo *topology.CPUTopology, availableCPUs cpuset.CPUSet, num
158158
// Algorithm: topology-aware best-fit
159159
// 1. Acquire whole sockets, if available and the container requires at
160160
// least a socket's-worth of CPUs.
161-
for _, s := range acc.freeSockets() {
162-
if acc.needs(acc.topo.CPUsPerSocket()) {
161+
if acc.needs(acc.topo.CPUsPerSocket()) {
162+
for _, s := range acc.freeSockets() {
163163
klog.V(4).Infof("[cpumanager] takeByTopology: claiming socket [%d]", s)
164164
acc.take(acc.details.CPUsInSocket(s))
165165
if acc.isSatisfied() {
166166
return acc.result, nil
167167
}
168+
if !acc.needs(acc.topo.CPUsPerSocket()) {
169+
break
170+
}
168171
}
169172
}
170173

171174
// 2. Acquire whole cores, if available and the container requires at least
172175
// a core's-worth of CPUs.
173-
for _, c := range acc.freeCores() {
174-
if acc.needs(acc.topo.CPUsPerCore()) {
176+
if acc.needs(acc.topo.CPUsPerCore()) {
177+
for _, c := range acc.freeCores() {
175178
klog.V(4).Infof("[cpumanager] takeByTopology: claiming core [%d]", c)
176179
acc.take(acc.details.CPUsInCore(c))
177180
if acc.isSatisfied() {
178181
return acc.result, nil
179182
}
183+
if !acc.needs(acc.topo.CPUsPerCore()) {
184+
break
185+
}
180186
}
181187
}
182188

0 commit comments

Comments
 (0)