1use std::borrow::Cow;
30
31use super::{MetricDef, metric_def};
32
33macro_rules! builtin_metrics {
38 ($($variant:ident => $wire:literal),* $(,)?) => {
39 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
44 pub enum BuiltinMetric { $($variant),* }
45
46 impl BuiltinMetric {
47 pub const fn wire_name(self) -> &'static str {
50 match self { $(Self::$variant => $wire,)* }
51 }
52
53 pub fn from_wire_name(name: &str) -> Option<Self> {
56 match name { $($wire => Some(Self::$variant),)* _ => None }
57 }
58
59 pub const ALL: &'static [BuiltinMetric] = &[$(Self::$variant),*];
62 }
63 };
64}
65
66builtin_metrics! {
67 WorstSpread => "worst_spread",
70 WorstGapMs => "worst_gap_ms",
71 TotalMigrations => "total_migrations",
72 WorstMigrationRatio => "worst_migration_ratio",
73 MaxImbalanceRatio => "max_imbalance_ratio",
74 AvgImbalanceRatio => "avg_imbalance_ratio",
75 MaxDsqDepth => "max_dsq_depth",
76 AvgDsqDepth => "avg_dsq_depth",
77 StuckCount => "stuck_count",
78 TotalFallback => "total_fallback",
79 TotalKeepLast => "total_keep_last",
80 TotalFallbackPooled => "total_fallback_pooled",
81 TotalKeepLastPooled => "total_keep_last_pooled",
82 TotalEventVcpuSec => "total_event_vcpu_sec",
83 FallbackPerVcpuSec => "fallback_per_vcpu_sec",
84 KeepLastPerVcpuSec => "keep_last_per_vcpu_sec",
85 TotalRunDelay => "total_run_delay",
86 TotalPcount => "total_pcount",
87 TotalSchedCount => "total_sched_count",
88 TotalYldCount => "total_yld_count",
89 TotalSchedGoidle => "total_sched_goidle",
90 TotalTtwuCount => "total_ttwu_count",
91 TotalTtwuLocal => "total_ttwu_local",
92 TotalRunDelayNsPerSched => "total_run_delay_ns_per_sched",
93 TtwuLocalFraction => "ttwu_local_fraction",
94 SchedGoidleFraction => "sched_goidle_fraction",
95 TotalSchedstatVcpuSec => "total_schedstat_vcpu_sec",
98 RunDelayPerVcpuSec => "run_delay_per_vcpu_sec",
99 PcountPerVcpuSec => "pcount_per_vcpu_sec",
100 SchedCountPerVcpuSec => "sched_count_per_vcpu_sec",
101 YldCountPerVcpuSec => "yld_count_per_vcpu_sec",
102 TtwuCountPerVcpuSec => "ttwu_count_per_vcpu_sec",
103 SchedGoidlePerVcpuSec => "sched_goidle_per_vcpu_sec",
104 AvgNrRunning => "avg_nr_running",
105 WorstP99WakeLatencyUs => "worst_p99_wake_latency_us",
106 WorstMedianWakeLatencyUs => "worst_median_wake_latency_us",
107 WorstWakeLatencyCv => "worst_wake_latency_cv",
108 WorstP99TimerLatencyUs => "worst_p99_timer_latency_us",
109 WorstMedianTimerLatencyUs => "worst_median_timer_latency_us",
110 WorstP999TimerLatencyUs => "worst_p999_timer_latency_us",
111 WorstTimerLatencyUs => "worst_timer_latency_us",
112 IterationRate => "iteration_rate",
113 TotalIterations => "total_iterations",
114 TotalCpuTimeSec => "total_cpu_time_sec",
115 TotalIterationsPooled => "total_iterations_pooled",
116 TotalTaobenchOps => "total_taobench_ops",
121 TotalTaobenchFastOps => "total_taobench_fast_ops",
122 TotalTaobenchSlowOps => "total_taobench_slow_ops",
123 TotalTaobenchWallSec => "total_taobench_wall_sec",
124 TotalTaobenchCpuSec => "total_taobench_cpu_sec",
125 TaobenchTotalOpsPerCpuSecWhole => "taobench_total_ops_per_cpu_sec_whole",
126 TaobenchFastOpsPerCpuSecWhole => "taobench_fast_ops_per_cpu_sec_whole",
127 TaobenchSlowOpsPerCpuSecWhole => "taobench_slow_ops_per_cpu_sec_whole",
128 TaobenchHitFraction => "taobench_hit_fraction",
129 TaobenchServeP50UsWhole => "taobench_serve_p50_us_whole",
131 TaobenchServeP90UsWhole => "taobench_serve_p90_us_whole",
132 TaobenchServeP99UsWhole => "taobench_serve_p99_us_whole",
133 TaobenchServeP999UsWhole => "taobench_serve_p999_us_whole",
134 TaobenchServeMinUsWhole => "taobench_serve_min_us_whole",
135 TaobenchServeMaxUsWhole => "taobench_serve_max_us_whole",
136 TotalTaobenchGetCmds => "total_taobench_get_cmds",
139 TotalTaobenchGetHits => "total_taobench_get_hits",
140 TaobenchCommandHitRate => "taobench_command_hit_rate",
141 TotalSchbenchMsgRunDelayNs => "total_schbench_msg_run_delay_ns",
144 TotalSchbenchMsgPcount => "total_schbench_msg_pcount",
145 TotalSchbenchWorkerRunDelayNs => "total_schbench_worker_run_delay_ns",
146 TotalSchbenchWorkerPcount => "total_schbench_worker_pcount",
147 TotalSchbenchLoops => "total_schbench_loops",
148 TotalSchbenchWorkerCpuSec => "total_schbench_worker_cpu_sec",
149 SchbenchTotalLoopsPerCpuSec => "schbench_total_loops_per_cpu_sec",
150 SchbenchLoopsPerCpuSec => "schbench_loops_per_cpu_sec",
151 SchbenchMsgRunDelayNsPerSched => "schbench_msg_run_delay_ns_per_sched",
152 SchbenchWorkerRunDelayNsPerSched => "schbench_worker_run_delay_ns_per_sched",
153 WakeupP50LatencyUsWhole => "wakeup_p50_latency_us_whole",
157 WakeupP90LatencyUsWhole => "wakeup_p90_latency_us_whole",
158 WakeupP99LatencyUsWhole => "wakeup_p99_latency_us_whole",
159 WakeupP999LatencyUsWhole => "wakeup_p999_latency_us_whole",
160 WakeupMinLatencyUsWhole => "wakeup_min_latency_us_whole",
161 WakeupMaxLatencyUsWhole => "wakeup_max_latency_us_whole",
162 RequestP50LatencyUsWhole => "request_p50_latency_us_whole",
163 RequestP90LatencyUsWhole => "request_p90_latency_us_whole",
164 RequestP99LatencyUsWhole => "request_p99_latency_us_whole",
165 RequestP999LatencyUsWhole => "request_p999_latency_us_whole",
166 RequestMinLatencyUsWhole => "request_min_latency_us_whole",
167 RequestMaxLatencyUsWhole => "request_max_latency_us_whole",
168 RpsP20Whole => "rps_p20_whole",
169 RpsP50Whole => "rps_p50_whole",
170 RpsP90Whole => "rps_p90_whole",
171 RpsMinWhole => "rps_min_whole",
172 RpsMaxWhole => "rps_max_whole",
173 SystemTimeNs => "system_time_ns",
174 UserTimeNs => "user_time_ns",
175 ObservedTaskCpuTimeNs => "observed_task_cpu_time_ns",
176 SystemCpuFraction => "system_cpu_fraction",
177 WorstMeanRunDelayUs => "worst_mean_run_delay_us",
178 WorstRunDelayUs => "worst_run_delay_us",
179 WorstWakeLatencyTailRatio => "worst_wake_latency_tail_ratio",
180 WorstIterationsPerWorker => "worst_iterations_per_worker",
181 WorstIterationsPerCpuSec => "worst_iterations_per_cpu_sec",
182 WorstPageLocality => "worst_page_locality",
183 WorstCrossNodeMigrationRatio => "worst_cross_node_migration_ratio",
184 TotalCpuTimeNs => "total_cpu_time_ns",
185
186 TotalHardirqs => "total_hardirqs",
196 TotalSoftirqNetRx => "total_softirq_net_rx",
197 TotalSoftirqNetTx => "total_softirq_net_tx",
198 TotalSoftirqTimer => "total_softirq_timer",
199 TotalSoftirqSched => "total_softirq_sched",
200 TotalIrqTimeNs => "total_irq_time_ns",
201 TotalSoftirqTimeNs => "total_softirq_time_ns",
202 TotalStealTimeNs => "total_steal_time_ns",
203 AvgIrqUtil => "avg_irq_util",
204 MaxAvgIrqUtil => "max_avg_irq_util",
205 HardirqsPerCpuSec => "hardirqs_per_cpu_sec",
206 NetRxSoftirqsPerCpuSec => "net_rx_softirqs_per_cpu_sec",
207 IrqTimeFraction => "irq_time_fraction",
208 TotalPhaseGuestCpuSec => "total_phase_guest_cpu_sec",
212 TotalPhaseGuestCpuNs => "total_phase_guest_cpu_ns",
213
214 MaxCpuHardirqs => "max_cpu_hardirqs",
220 MaxCpuHardirqConcentration => "max_cpu_hardirq_concentration",
221 MaxCpuSoftirqNetRx => "max_cpu_softirq_net_rx",
225 MaxCpuSoftirqNetRxConcentration => "max_cpu_softirq_net_rx_concentration",
226 AvgCpuUtilCompScale => "avg_cpu_util_comp_scale",
231
232 AvgTaskLatCri => "avg_task_lat_cri",
239 MaxTaskLatCri => "max_task_lat_cri",
240
241 MaxCgroupIrqPressure => "max_cgroup_irq_pressure",
248 MaxCgroupIrqPressureConcentration => "max_cgroup_irq_pressure_concentration",
249 MaxCgroupPsiIrqAvg10 => "max_cgroup_psi_irq_avg10",
250
251 PsiIrqFullAvg10 => "psi_irq_full_avg10",
257 TotalIrqPressureUs => "total_irq_pressure_us",
258
259 WakeupP50LatencyUs => "wakeup_p50_latency_us",
262 WakeupP90LatencyUs => "wakeup_p90_latency_us",
263 WakeupP99LatencyUs => "wakeup_p99_latency_us",
264 WakeupP999LatencyUs => "wakeup_p999_latency_us",
265 RequestP50LatencyUs => "request_p50_latency_us",
266 RequestP90LatencyUs => "request_p90_latency_us",
267 RequestP99LatencyUs => "request_p99_latency_us",
268 RequestP999LatencyUs => "request_p999_latency_us",
269 SchedDelayMsgUs => "sched_delay_msg_us",
270 SchedDelayWorkerUs => "sched_delay_worker_us",
271 SchbenchLoopCount => "schbench_loop_count",
272 TaobenchTotalOpsPerCpuSec => "taobench_total_ops_per_cpu_sec",
273 TaobenchFastOpsPerCpuSec => "taobench_fast_ops_per_cpu_sec",
274 TaobenchSlowOpsPerCpuSec => "taobench_slow_ops_per_cpu_sec",
275 TaobenchHitRatio => "taobench_hit_ratio",
276 TaobenchHitRate => "taobench_hit_rate",
277 TaobenchServeP50Us => "taobench_serve_p50_us",
279 TaobenchServeP90Us => "taobench_serve_p90_us",
280 TaobenchServeP99Us => "taobench_serve_p99_us",
281 TaobenchServeP999Us => "taobench_serve_p999_us",
282 TaobenchServeMinUs => "taobench_serve_min_us",
283 TaobenchServeMaxUs => "taobench_serve_max_us",
284 WakeupMinLatencyUs => "wakeup_min_latency_us",
285 WakeupMaxLatencyUs => "wakeup_max_latency_us",
286 RequestMinLatencyUs => "request_min_latency_us",
287 RequestMaxLatencyUs => "request_max_latency_us",
288 RpsP20 => "rps_p20",
289 RpsP50 => "rps_p50",
290 RpsP90 => "rps_p90",
291 RpsMin => "rps_min",
292 RpsMax => "rps_max",
293
294 P99WakeLatencyUs => "p99_wake_latency_us",
298 MedianWakeLatencyUs => "median_wake_latency_us",
299 WakeLatencyCv => "wake_latency_cv",
300 P99TimerLatencyUs => "p99_timer_latency_us",
301 MedianTimerLatencyUs => "median_timer_latency_us",
302 P999TimerLatencyUs => "p999_timer_latency_us",
303 MeanRunDelayUs => "mean_run_delay_us",
304 MaxRunDelayUs => "max_run_delay_us",
305 AvgOffCpuPct => "avg_off_cpu_pct",
306 MinOffCpuPct => "min_off_cpu_pct",
307 MaxOffCpuPct => "max_off_cpu_pct",
308 OffCpuSpreadPct => "off_cpu_spread_pct",
309 MigrationRatio => "migration_ratio",
310 IterationsPerWorker => "iterations_per_worker",
311 PageLocality => "page_locality",
312 CrossNodeMigrationRatio => "cross_node_migration_ratio",
313}
314
315impl BuiltinMetric {
316 pub fn def(self) -> &'static MetricDef {
320 metric_def(self.wire_name())
321 .expect("BuiltinMetric variant must resolve in METRICS (pinned by test)")
322 }
323}
324
325#[derive(Debug, Clone, PartialEq, Eq, Hash)]
330pub enum MetricId {
331 Builtin(BuiltinMetric),
334 Dynamic(Cow<'static, str>),
336}
337
338impl MetricId {
339 pub fn as_str(&self) -> &str {
343 match self {
344 MetricId::Builtin(b) => b.wire_name(),
345 MetricId::Dynamic(s) => s.as_ref(),
346 }
347 }
348
349 pub fn def(&self) -> Option<&'static MetricDef> {
358 match self {
359 MetricId::Builtin(b) => Some(b.def()),
360 MetricId::Dynamic(s) => metric_def(s.as_ref()),
361 }
362 }
363}
364
365impl From<BuiltinMetric> for MetricId {
366 fn from(b: BuiltinMetric) -> Self {
367 MetricId::Builtin(b)
368 }
369}
370
371impl From<&BuiltinMetric> for MetricId {
372 fn from(b: &BuiltinMetric) -> Self {
375 MetricId::Builtin(*b)
376 }
377}
378
379impl From<&str> for MetricId {
380 fn from(s: &str) -> Self {
385 match BuiltinMetric::from_wire_name(s) {
386 Some(b) => MetricId::Builtin(b),
387 None => MetricId::Dynamic(Cow::Owned(s.to_owned())),
388 }
389 }
390}
391
392impl From<String> for MetricId {
393 fn from(s: String) -> Self {
394 match BuiltinMetric::from_wire_name(&s) {
395 Some(b) => MetricId::Builtin(b),
396 None => MetricId::Dynamic(Cow::Owned(s)),
397 }
398 }
399}
400
401impl From<&String> for MetricId {
402 fn from(s: &String) -> Self {
406 Self::from(s.as_str())
407 }
408}
409
410#[cfg(test)]
411mod tests {
412 use super::*;
413 use crate::stats::METRICS;
414
415 #[test]
418 fn builtin_metric_is_one_to_one_with_registry() {
419 for b in BuiltinMetric::ALL {
421 assert!(
422 metric_def(b.wire_name()).is_some(),
423 "BuiltinMetric::{b:?} (wire {:?}) is not in METRICS",
424 b.wire_name()
425 );
426 }
427 for m in METRICS {
429 assert!(
430 BuiltinMetric::from_wire_name(m.name).is_some(),
431 "METRICS entry {:?} has no BuiltinMetric variant",
432 m.name
433 );
434 }
435 assert_eq!(
438 BuiltinMetric::ALL.len(),
439 METRICS.len(),
440 "BuiltinMetric::ALL ({}) != METRICS ({})",
441 BuiltinMetric::ALL.len(),
442 METRICS.len()
443 );
444 }
445
446 #[test]
448 fn wire_name_roundtrips() {
449 for b in BuiltinMetric::ALL {
450 assert_eq!(BuiltinMetric::from_wire_name(b.wire_name()), Some(*b));
451 }
452 }
453
454 #[test]
458 fn metric_id_canonicalizes_builtin_and_quarantines_dynamic() {
459 let typed: MetricId = BuiltinMetric::TaobenchTotalOpsPerCpuSec.into();
460 let from_str: MetricId = "taobench_total_ops_per_cpu_sec".into();
461 assert_eq!(
462 typed, from_str,
463 "a built-in string canonicalizes to Builtin"
464 );
465 assert_eq!(typed.as_str(), "taobench_total_ops_per_cpu_sec");
466 assert!(typed.def().is_some(), "a built-in carries its registry def");
467
468 let dynamic: MetricId = "scx_layered_layer0_depth".into();
469 assert!(
470 matches!(dynamic, MetricId::Dynamic(_)),
471 "a non-registered key stays Dynamic"
472 );
473 assert!(
474 dynamic.def().is_none(),
475 "a Dynamic key has no def() -> caller must not guess its kind"
476 );
477 }
478}