Skip to content

Commit 1c7023e

Browse files
authored
Merge pull request #251 from sustainable-computing-io/perf-metrics
add cpu instructions perf metrics
2 parents 5403881 + 0fa5913 commit 1c7023e

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

pkg/attacher/bcc_attacher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ type BpfModuleTables struct {
4949

5050
const (
5151
CPUCycleLable = "cpu_cycles"
52-
CPUInstructionLable = "cpu_instr"
52+
CPUInstructionLabel = "cpu_instr"
5353
CacheMissLabel = "cache_miss"
5454
)
5555

5656
var (
5757
Counters = map[string]perfCounter{
5858
CPUCycleLable: {unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_CPU_CYCLES, true},
59-
CPUInstructionLable: {unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_INSTRUCTIONS, true},
59+
CPUInstructionLabel: {unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_INSTRUCTIONS, true},
6060
CacheMissLabel: {unix.PERF_TYPE_HARDWARE, unix.PERF_COUNT_HW_CACHE_MISSES, true},
6161
}
6262
EnableCPUFreq = true

pkg/attacher/bcc_attacher_stub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
const (
2727
CPUCycleLable = "cpu_cycles"
28-
CPUInstructionLable = "cpu_instr"
28+
CPUInstructionLabel = "cpu_instr"
2929
CacheMissLabel = "cache_miss"
3030
)
3131

pkg/collector/collector.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ import (
2626
)
2727

2828
const (
29-
nodeEnergyStatMetric = "node_energy_stat"
30-
podEnergyStatMetric = "pod_energy_stat"
31-
nodeEnergyMetric = "node_hwmon_energy_joule_total"
32-
freqMetric = "node_cpu_scaling_frequency_hertz"
33-
pkgEnergyMetric = "node_package_energy_millijoule"
34-
perCPUStatMetric = "pod_cpu_cpu_time_us"
29+
nodeEnergyStatMetric = "node_energy_stat"
30+
podEnergyStatMetric = "pod_energy_stat"
31+
nodeEnergyMetric = "node_hwmon_energy_joule_total"
32+
freqMetric = "node_cpu_scaling_frequency_hertz"
33+
pkgEnergyMetric = "node_package_energy_millijoule"
34+
perCPUStatMetric = "pod_cpu_cpu_time_us"
35+
podCPUInstructionMetric = "pod_cpu_instructions"
3536

3637
podLabelPrefix = "pod_"
3738
nodeLabelPrefix = "node_"
@@ -265,6 +266,18 @@ func (c *Collector) getPodDetailedCPUTimeDescription() *prometheus.Desc {
265266
)
266267
}
267268

269+
func (c *Collector) getPodInstructionDescription() *prometheus.Desc {
270+
return prometheus.NewDesc(
271+
podCPUInstructionMetric,
272+
"Recorded CPU instructions during the period.",
273+
[]string{
274+
"pod_name",
275+
"pod_namespace",
276+
},
277+
nil,
278+
)
279+
}
280+
268281
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
269282
lock.Lock()
270283
defer lock.Unlock()
@@ -284,6 +297,9 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
284297
ch <- c.getFreqDescription()
285298
ch <- c.getPackageEnergyDescription()
286299
ch <- c.getPodDetailedCPUTimeDescription()
300+
if cpuInstrCounterEnabled {
301+
ch <- c.getPodInstructionDescription()
302+
}
287303
}
288304

289305
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
@@ -390,6 +406,16 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
390406
)
391407
ch <- metric
392408
}
409+
if cpuInstrCounterEnabled &&
410+
v.CounterStats[attacher.CPUInstructionLabel] != nil {
411+
// all curr pod cpu instructions
412+
edesc = prometheus.MustNewConstMetric(
413+
c.getPodInstructionDescription(),
414+
prometheus.GaugeValue,
415+
float64(v.CounterStats[attacher.CPUInstructionLabel].Curr),
416+
v.PodName, v.Namespace)
417+
ch <- edesc
418+
}
393419
}
394420

395421
// de_node_energy and desc_node_energy give indexable values for total energy consumptions of a node

pkg/collector/metrics.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ var (
4747
uintFeatures []string = getUIntFeatures()
4848
features []string = append(FloatFeatures, uintFeatures...)
4949
metricNames []string = getEstimatorMetrics()
50+
51+
cpuInstrCounterEnabled = isCounterStatEnabled(attacher.CPUInstructionLabel)
5052
)
5153

5254
func getUIntFeatures() []string {
@@ -88,3 +90,12 @@ func getEstimatorMetrics() []string {
8890
model.InitMetricIndexes(names)
8991
return names
9092
}
93+
94+
func isCounterStatEnabled(label string) bool {
95+
for _, counter := range availableCounters {
96+
if counter == label {
97+
return true
98+
}
99+
}
100+
return false
101+
}

pkg/collector/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (c *Collector) readBPFEvent() (pidPodName map[uint32]string, containerIDPod
160160
switch counterKey {
161161
case attacher.CPUCycleLable:
162162
val = ct.CPUCycles
163-
case attacher.CPUInstructionLable:
163+
case attacher.CPUInstructionLabel:
164164
val = ct.CPUInstr
165165
case attacher.CacheMissLabel:
166166
val = ct.CacheMisses

0 commit comments

Comments
 (0)