@@ -26,6 +26,7 @@ import (
26
26
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology"
27
27
"k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
28
28
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
29
+ "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/socketmask"
29
30
)
30
31
31
32
// 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
215
216
}
216
217
}
217
218
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 )
219
225
if err != nil {
220
226
klog .Errorf ("[cpumanager] unable to allocate %d CPUs (container id: %s, error: %v)" , numCPUs , containerID , err )
221
227
return err
@@ -244,12 +250,37 @@ func (p *staticPolicy) RemoveContainer(s state.State, containerID string) (rerr
244
250
return nil
245
251
}
246
252
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 ())
250
279
if err != nil {
251
280
return cpuset .NewCPUSet (), err
252
281
}
282
+ result = result .Union (remainingCPUs )
283
+
253
284
// Remove allocated CPUs from the shared CPUSet.
254
285
s .SetDefaultCPUSet (s .GetDefaultCPUSet ().Difference (result ))
255
286
0 commit comments