Skip to content

Commit 8278d11

Browse files
klueskaConor Nolan
andcommitted
Consume TopologyHints in the CPUManager
Co-Authored-By: Conor Nolan <[email protected]>
1 parent 7c626a2 commit 8278d11

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

pkg/kubelet/cm/cpumanager/policy_static.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology"
2727
"k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
2828
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
29+
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/socketmask"
2930
)
3031

3132
// PolicyStatic is the name of the static policy
@@ -215,7 +216,12 @@ func (p *staticPolicy) AddContainer(s state.State, pod *v1.Pod, container *v1.Co
215216
}
216217
}
217218

218-
cpuset, err := p.allocateCPUs(s, numCPUs)
219+
// Call Topology Manager to get the aligned socket affinity across all hint providers.
220+
hint := p.affinity.GetAffinity(string(pod.UID), container.Name)
221+
klog.Infof("[cpumanager] Pod %v, Container %v Topology Affinity is: %v", pod.UID, container.Name, hint)
222+
223+
// Allocate CPUs according to the socket affinity contained in the hint.
224+
cpuset, err := p.allocateCPUs(s, numCPUs, hint.SocketAffinity)
219225
if err != nil {
220226
klog.Errorf("[cpumanager] unable to allocate %d CPUs (container id: %s, error: %v)", numCPUs, containerID, err)
221227
return err
@@ -244,12 +250,37 @@ func (p *staticPolicy) RemoveContainer(s state.State, containerID string) (rerr
244250
return nil
245251
}
246252

247-
func (p *staticPolicy) allocateCPUs(s state.State, numCPUs int) (cpuset.CPUSet, error) {
248-
klog.Infof("[cpumanager] allocateCpus: (numCPUs: %d)", numCPUs)
249-
result, err := takeByTopology(p.topology, p.assignableCPUs(s), numCPUs)
253+
func (p *staticPolicy) allocateCPUs(s state.State, numCPUs int, socketmask socketmask.SocketMask) (cpuset.CPUSet, error) {
254+
klog.Infof("[cpumanager] allocateCpus: (numCPUs: %d, socket: %v)", numCPUs, socketmask)
255+
256+
// If there are aligned CPUs in the socketmask, attempt to take those first.
257+
result := cpuset.NewCPUSet()
258+
if socketmask != nil {
259+
alignedCPUs := cpuset.NewCPUSet()
260+
for _, socketID := range socketmask.GetSockets() {
261+
alignedCPUs = alignedCPUs.Union(p.assignableCPUs(s).Intersection(p.topology.CPUDetails.CPUsInSocket(socketID)))
262+
}
263+
264+
numAlignedToAlloc := alignedCPUs.Size()
265+
if numCPUs < numAlignedToAlloc {
266+
numAlignedToAlloc = numCPUs
267+
}
268+
269+
alignedCPUs, err := takeByTopology(p.topology, alignedCPUs, numAlignedToAlloc)
270+
if err != nil {
271+
return cpuset.NewCPUSet(), err
272+
}
273+
274+
result = result.Union(alignedCPUs)
275+
}
276+
277+
// Get any remaining CPUs from what's leftover after attempting to grab aligned ones.
278+
remainingCPUs, err := takeByTopology(p.topology, p.assignableCPUs(s).Difference(result), numCPUs-result.Size())
250279
if err != nil {
251280
return cpuset.NewCPUSet(), err
252281
}
282+
result = result.Union(remainingCPUs)
283+
253284
// Remove allocated CPUs from the shared CPUSet.
254285
s.SetDefaultCPUSet(s.GetDefaultCPUSet().Difference(result))
255286

0 commit comments

Comments
 (0)