Skip to content

Commit f1031be

Browse files
committed
node: cpumgr: metrics: add metrics for allocation per NUMA
Signed-off-by: Swati Sehgal <[email protected]>
1 parent 0446f6c commit f1031be

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

pkg/kubelet/cm/cpumanager/policy_static.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cpumanager
1818

1919
import (
2020
"fmt"
21+
"strconv"
2122

2223
v1 "k8s.io/api/core/v1"
2324
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -389,7 +390,7 @@ func (p *staticPolicy) Allocate(s state.State, pod *v1.Pod, container *v1.Contai
389390

390391
s.SetCPUSet(string(pod.UID), container.Name, cpuAllocation.CPUs)
391392
p.updateCPUsToReuse(pod, container, cpuAllocation.CPUs)
392-
p.updateMetricsOnAllocate(cpuAllocation)
393+
p.updateMetricsOnAllocate(s, cpuAllocation)
393394

394395
klog.V(4).InfoS("Allocated exclusive CPUs", "pod", klog.KObj(pod), "containerName", container.Name, "cpuset", cpuAllocation.CPUs.String())
395396
return nil
@@ -416,7 +417,8 @@ func (p *staticPolicy) RemoveContainer(s state.State, podUID string, containerNa
416417
// Mutate the shared pool, adding released cpus.
417418
toRelease = toRelease.Difference(cpusInUse)
418419
s.SetDefaultCPUSet(s.GetDefaultCPUSet().Union(toRelease))
419-
p.updateMetricsOnRelease(toRelease)
420+
p.updateMetricsOnRelease(s, toRelease)
421+
420422
}
421423
return nil
422424
}
@@ -755,33 +757,60 @@ func (p *staticPolicy) getAlignedCPUs(numaAffinity bitmask.BitMask, allocatableC
755757

756758
func (p *staticPolicy) initializeMetrics(s state.State) {
757759
metrics.CPUManagerSharedPoolSizeMilliCores.Set(float64(p.GetAvailableCPUs(s).Size() * 1000))
758-
metrics.CPUManagerExclusiveCPUsAllocationCount.Set(float64(countExclusiveCPUs(s)))
759760
metrics.ContainerAlignedComputeResourcesFailure.WithLabelValues(metrics.AlignScopeContainer, metrics.AlignedPhysicalCPU).Add(0) // ensure the value exists
760761
metrics.ContainerAlignedComputeResources.WithLabelValues(metrics.AlignScopeContainer, metrics.AlignedPhysicalCPU).Add(0) // ensure the value exists
761762
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)
762766
}
763767

764-
func (p *staticPolicy) updateMetricsOnAllocate(cpuAlloc topology.Allocation) {
768+
func (p *staticPolicy) updateMetricsOnAllocate(s state.State, cpuAlloc topology.Allocation) {
765769
ncpus := cpuAlloc.CPUs.Size()
766770
metrics.CPUManagerExclusiveCPUsAllocationCount.Add(float64(ncpus))
767771
metrics.CPUManagerSharedPoolSizeMilliCores.Add(float64(-ncpus * 1000))
768772
if cpuAlloc.Aligned.UncoreCache {
769773
metrics.ContainerAlignedComputeResources.WithLabelValues(metrics.AlignScopeContainer, metrics.AlignedUncoreCache).Inc()
770774
}
775+
totalAssignedCPUs := getTotalAssignedExclusiveCPUs(s)
776+
updateAllocationPerNUMAMetric(p.topology, totalAssignedCPUs)
771777
}
772778

773-
func (p *staticPolicy) updateMetricsOnRelease(cset cpuset.CPUSet) {
779+
func (p *staticPolicy) updateMetricsOnRelease(s state.State, cset cpuset.CPUSet) {
774780
ncpus := cset.Size()
775781
metrics.CPUManagerExclusiveCPUsAllocationCount.Add(float64(-ncpus))
776782
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
777796
}
778797

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)
784808
}
809+
numaCount[numaNode]++
810+
}
811+
812+
// Update metric
813+
for numaNode, count := range numaCount {
814+
metrics.CPUManagerAllocationPerNUMA.WithLabelValues(strconv.Itoa(numaNode)).Set(float64(count))
785815
}
786-
return exclusiveCPUs
787816
}

pkg/kubelet/metrics/metrics.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ const (
113113
CPUManagerPinningErrorsTotalKey = "cpu_manager_pinning_errors_total"
114114
CPUManagerSharedPoolSizeMilliCoresKey = "cpu_manager_shared_pool_size_millicores"
115115
CPUManagerExclusiveCPUsAllocationCountKey = "cpu_manager_exclusive_cpu_allocation_count"
116+
CPUManagerAllocationPerNUMAKey = "cpu_manager_allocation_per_numa"
116117

117118
// Metrics to track the Memory manager behavior
118119
MemoryManagerPinningRequestsTotalKey = "memory_manager_pinning_requests_total"
@@ -815,6 +816,17 @@ var (
815816
},
816817
)
817818

819+
// CPUManagerAllocationPerNUMA tracks the count of CPUs allocated per NUMA node
820+
CPUManagerAllocationPerNUMA = metrics.NewGaugeVec(
821+
&metrics.GaugeOpts{
822+
Subsystem: KubeletSubsystem,
823+
Name: CPUManagerAllocationPerNUMAKey,
824+
Help: "Number of CPUs allocated per NUMA node",
825+
StabilityLevel: metrics.ALPHA,
826+
},
827+
[]string{AlignedNUMANode},
828+
)
829+
818830
// ContainerAlignedComputeResources reports the count of resources allocation which granted aligned resources, per alignment boundary
819831
ContainerAlignedComputeResources = metrics.NewCounterVec(
820832
&metrics.CounterOpts{
@@ -1126,6 +1138,7 @@ func Register(collectors ...metrics.StableCollector) {
11261138
legacyregistry.MustRegister(CPUManagerPinningErrorsTotal)
11271139
legacyregistry.MustRegister(CPUManagerSharedPoolSizeMilliCores)
11281140
legacyregistry.MustRegister(CPUManagerExclusiveCPUsAllocationCount)
1141+
legacyregistry.MustRegister(CPUManagerAllocationPerNUMA)
11291142
legacyregistry.MustRegister(ContainerAlignedComputeResources)
11301143
legacyregistry.MustRegister(ContainerAlignedComputeResourcesFailure)
11311144
legacyregistry.MustRegister(MemoryManagerPinningRequestTotal)

0 commit comments

Comments
 (0)