@@ -18,6 +18,7 @@ package cpumanager
18
18
19
19
import (
20
20
"fmt"
21
+ "strconv"
21
22
22
23
v1 "k8s.io/api/core/v1"
23
24
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -389,7 +390,7 @@ func (p *staticPolicy) Allocate(s state.State, pod *v1.Pod, container *v1.Contai
389
390
390
391
s .SetCPUSet (string (pod .UID ), container .Name , cpuAllocation .CPUs )
391
392
p .updateCPUsToReuse (pod , container , cpuAllocation .CPUs )
392
- p .updateMetricsOnAllocate (cpuAllocation )
393
+ p .updateMetricsOnAllocate (s , cpuAllocation )
393
394
394
395
klog .V (4 ).InfoS ("Allocated exclusive CPUs" , "pod" , klog .KObj (pod ), "containerName" , container .Name , "cpuset" , cpuAllocation .CPUs .String ())
395
396
return nil
@@ -416,7 +417,8 @@ func (p *staticPolicy) RemoveContainer(s state.State, podUID string, containerNa
416
417
// Mutate the shared pool, adding released cpus.
417
418
toRelease = toRelease .Difference (cpusInUse )
418
419
s .SetDefaultCPUSet (s .GetDefaultCPUSet ().Union (toRelease ))
419
- p .updateMetricsOnRelease (toRelease )
420
+ p .updateMetricsOnRelease (s , toRelease )
421
+
420
422
}
421
423
return nil
422
424
}
@@ -755,33 +757,60 @@ func (p *staticPolicy) getAlignedCPUs(numaAffinity bitmask.BitMask, allocatableC
755
757
756
758
func (p * staticPolicy ) initializeMetrics (s state.State ) {
757
759
metrics .CPUManagerSharedPoolSizeMilliCores .Set (float64 (p .GetAvailableCPUs (s ).Size () * 1000 ))
758
- metrics .CPUManagerExclusiveCPUsAllocationCount .Set (float64 (countExclusiveCPUs (s )))
759
760
metrics .ContainerAlignedComputeResourcesFailure .WithLabelValues (metrics .AlignScopeContainer , metrics .AlignedPhysicalCPU ).Add (0 ) // ensure the value exists
760
761
metrics .ContainerAlignedComputeResources .WithLabelValues (metrics .AlignScopeContainer , metrics .AlignedPhysicalCPU ).Add (0 ) // ensure the value exists
761
762
metrics .ContainerAlignedComputeResources .WithLabelValues (metrics .AlignScopeContainer , metrics .AlignedUncoreCache ).Add (0 ) // ensure the value exists
763
+ totalAssignedCPUs := getTotalAssignedExclusiveCPUs (s )
764
+ metrics .CPUManagerExclusiveCPUsAllocationCount .Set (float64 (totalAssignedCPUs .Size ()))
765
+ updateAllocationPerNUMAMetric (p .topology , totalAssignedCPUs )
762
766
}
763
767
764
- func (p * staticPolicy ) updateMetricsOnAllocate (cpuAlloc topology.Allocation ) {
768
+ func (p * staticPolicy ) updateMetricsOnAllocate (s state. State , cpuAlloc topology.Allocation ) {
765
769
ncpus := cpuAlloc .CPUs .Size ()
766
770
metrics .CPUManagerExclusiveCPUsAllocationCount .Add (float64 (ncpus ))
767
771
metrics .CPUManagerSharedPoolSizeMilliCores .Add (float64 (- ncpus * 1000 ))
768
772
if cpuAlloc .Aligned .UncoreCache {
769
773
metrics .ContainerAlignedComputeResources .WithLabelValues (metrics .AlignScopeContainer , metrics .AlignedUncoreCache ).Inc ()
770
774
}
775
+ totalAssignedCPUs := getTotalAssignedExclusiveCPUs (s )
776
+ updateAllocationPerNUMAMetric (p .topology , totalAssignedCPUs )
771
777
}
772
778
773
- func (p * staticPolicy ) updateMetricsOnRelease (cset cpuset.CPUSet ) {
779
+ func (p * staticPolicy ) updateMetricsOnRelease (s state. State , cset cpuset.CPUSet ) {
774
780
ncpus := cset .Size ()
775
781
metrics .CPUManagerExclusiveCPUsAllocationCount .Add (float64 (- ncpus ))
776
782
metrics .CPUManagerSharedPoolSizeMilliCores .Add (float64 (ncpus * 1000 ))
783
+ totalAssignedCPUs := getTotalAssignedExclusiveCPUs (s )
784
+ updateAllocationPerNUMAMetric (p .topology , totalAssignedCPUs .Difference (cset ))
785
+ }
786
+
787
+ func getTotalAssignedExclusiveCPUs (s state.State ) cpuset.CPUSet {
788
+ totalAssignedCPUs := cpuset .New ()
789
+ for _ , assignment := range s .GetCPUAssignments () {
790
+ for _ , cset := range assignment {
791
+ totalAssignedCPUs = totalAssignedCPUs .Union (cset )
792
+ }
793
+
794
+ }
795
+ return totalAssignedCPUs
777
796
}
778
797
779
- func countExclusiveCPUs (s state.State ) int {
780
- exclusiveCPUs := 0
781
- for _ , cpuAssign := range s .GetCPUAssignments () {
782
- for _ , cset := range cpuAssign {
783
- exclusiveCPUs += cset .Size ()
798
+ func updateAllocationPerNUMAMetric (topo * topology.CPUTopology , allocatedCPUs cpuset.CPUSet ) {
799
+ numaCount := make (map [int ]int )
800
+
801
+ // Count CPUs allocated per NUMA node
802
+ for _ , cpuID := range allocatedCPUs .UnsortedList () {
803
+ numaNode , err := topo .CPUNUMANodeID (cpuID )
804
+ if err != nil {
805
+ //NOTE: We are logging the error but it is highly unlikely to happen as the CPUset
806
+ // is already computed, evaluated and there is no room for user tampering.
807
+ klog .ErrorS (err , "Unable to determine NUMA node" , "cpuID" , cpuID )
784
808
}
809
+ numaCount [numaNode ]++
810
+ }
811
+
812
+ // Update metric
813
+ for numaNode , count := range numaCount {
814
+ metrics .CPUManagerAllocationPerNUMA .WithLabelValues (strconv .Itoa (numaNode )).Set (float64 (count ))
785
815
}
786
- return exclusiveCPUs
787
816
}
0 commit comments