macmon 0.8.2

Apple Silicon system monitor & Rust library — CPU/GPU power, temperature, RAM. No sudo. TUI, JSON pipe, Prometheus exporter.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! Metrics model and hardware sampler.

use core_foundation::dictionary::CFDictionaryRef;

use serde::Serialize;
use std::{collections::HashMap, time::Duration};

use crate::shared::{ioreport_channels_filter, zero_div};
use crate::sources::{
  IOHIDSensors, IOReport, SMC, SocInfo, cfio_get_residencies, cfio_watts, get_soc_info, libc_ram,
  libc_swap,
};

type WithError<T> = Result<T, Box<dyn std::error::Error>>;
type CpuCoreKey = String;
type FreqMetrics = (u32, f32, f32);

// const CPU_FREQ_DICE_SUBG: &str = "CPU Complex Performance States";
const CPU_FREQ_CORE_SUBG: &str = "CPU Core Performance States";
const GPU_FREQ_DICE_SUBG: &str = "GPU Performance States";

// MARK: Structs

/// Average hardware temperatures.
#[derive(Debug, Default, Serialize)]
pub struct TempMetrics {
  /// Average CPU temperature in Celsius.
  pub cpu_temp_avg: f32,
  /// Average GPU temperature in Celsius.
  pub gpu_temp_avg: f32,
}

/// Memory and swap usage.
#[derive(Debug, Default, Serialize)]
pub struct MemMetrics {
  /// Total physical memory in bytes.
  pub ram_total: u64,
  /// Used physical memory in bytes.
  pub ram_usage: u64,
  /// Total configured swap in bytes.
  pub swap_total: u64,
  /// Used swap in bytes.
  pub swap_usage: u64,
}

/// Fan speed metrics.
#[derive(Debug, Default, Serialize)]
pub struct FanMetric {
  /// Stable fan name derived from the fan order, e.g. `fan0`.
  pub name: String,
  /// Current fan speed in revolutions per minute.
  pub rpm: u32,
  /// Maximum fan speed in revolutions per minute, when reported by SMC.
  pub max_rpm: Option<u32>,
}

/// Metrics for one CPU core.
#[derive(Debug, Default, Serialize)]
pub struct CpuCoreMetrics {
  /// Die index reported by the IOReport channel.
  pub die_id: usize,
  /// Core index within the die.
  pub core_id: usize,
  /// Average frequency in MHz while the core was active.
  pub freq_mhz: u32,
  /// Active residency weighted by operating frequency relative to the core maximum.
  pub scaled_ratio: f32,
  /// Fraction of the sampling interval spent in active frequency states.
  pub active_ratio: f32,
}

struct SmcSensors {
  smc: SMC,
  cpu_keys: Vec<String>,
  gpu_keys: Vec<String>,
  fan_keys: Vec<String>,
}

/// A complete metrics snapshot returned by [`Sampler`].
///
/// Scaled ratios weight active-state residency by operating frequency relative
/// to the hardware maximum. Active ratios count all active-state residency
/// equally, regardless of frequency. Both are in the `0.0..=1.0` range.
/// CPU cluster frequencies use the arithmetic mean of per-core frequencies
/// with a minimum-frequency floor. Per-core and GPU frequencies are averaged
/// over active residency.
/// Power values are reported in Watts.
///
/// This struct may gain new metrics in future releases. When constructing it
/// manually, for example in tests, use struct update syntax:
///
/// ```
/// # use macmon::Metrics;
/// let metrics = Metrics { cpu_power: 1.0, ..Default::default() };
/// ```
#[derive(Debug, Default, Serialize)]
pub struct Metrics {
  /// Temperature metrics.
  pub temp: TempMetrics,
  /// Memory and swap metrics.
  pub memory: MemMetrics,
  /// Fan metrics ordered by stable SMC fan key order.
  pub fans: Vec<FanMetric>,
  /// Combined frequency-weighted active residency across all CPU cores.
  pub cpu_scaled_ratio: f32,
  /// Combined fraction of the sampling interval spent in active CPU frequency states.
  pub cpu_active_ratio: f32,
  /// Efficiency-cluster frequency in MHz.
  pub ecpu_freq_mhz: u32,
  /// Mean frequency-weighted active residency across efficiency cores.
  pub ecpu_scaled_ratio: f32,
  /// Mean fraction of the sampling interval spent in active efficiency-core frequency states.
  pub ecpu_active_ratio: f32,
  /// Performance-cluster frequency in MHz.
  pub pcpu_freq_mhz: u32,
  /// Mean frequency-weighted active residency across performance cores.
  pub pcpu_scaled_ratio: f32,
  /// Mean fraction of the sampling interval spent in active performance-core frequency states.
  pub pcpu_active_ratio: f32,
  /// Metrics for efficiency cores, ordered by die and core index.
  pub ecpu_cores: Vec<CpuCoreMetrics>,
  /// Metrics for performance cores, ordered by die and core index.
  pub pcpu_cores: Vec<CpuCoreMetrics>,
  /// GPU frequency in MHz, averaged over active residency.
  pub gpu_freq_mhz: u32,
  /// GPU active residency weighted by operating frequency relative to its maximum.
  pub gpu_scaled_ratio: f32,
  /// Fraction of the sampling interval spent in active GPU frequency states.
  pub gpu_active_ratio: f32,
  /// CPU package power in Watts.
  pub cpu_power: f32,
  /// GPU power in Watts.
  pub gpu_power: f32,
  /// Apple Neural Engine power in Watts.
  pub ane_power: f32,
  /// Sum of CPU, GPU, and ANE power in Watts.
  pub all_power: f32,
  /// System power estimate in Watts, when available.
  pub sys_power: f32,
  /// DRAM power in Watts.
  pub ram_power: f32,
  /// GPU SRAM power in Watts.
  pub gpu_ram_power: f32,
}

// MARK: Helpers

fn is_valid_temp(val: f32) -> bool {
  val > 0.0 && val <= 150.0
}

fn is_valid_fan_rpm(val: f32) -> bool {
  (0.0..=100_000.0).contains(&val)
}

fn fan_rpm_value(val: f32) -> Option<u32> {
  if is_valid_fan_rpm(val) { Some(val.trunc() as u32) } else { None }
}

fn aggregate_frequency(cores: &[CpuCoreMetrics], min_frequency_mhz: u32) -> u32 {
  let average =
    zero_div(cores.iter().map(|core| core.freq_mhz as f64).sum::<f64>(), cores.len() as f64);
  average.max(min_frequency_mhz as f64) as u32
}

fn aggregate_ioreport_metrics(mut rs: Metrics, soc: &SocInfo) -> Metrics {
  let ecpu_total_scaled: f32 = rs.ecpu_cores.iter().map(|core| core.scaled_ratio).sum();
  let pcpu_total_scaled: f32 = rs.pcpu_cores.iter().map(|core| core.scaled_ratio).sum();
  let ecpu_total_active: f32 = rs.ecpu_cores.iter().map(|core| core.active_ratio).sum();
  let pcpu_total_active: f32 = rs.pcpu_cores.iter().map(|core| core.active_ratio).sum();
  let ecores = rs.ecpu_cores.len().max(soc.ecpu_cores as usize) as f32;
  let pcores = rs.pcpu_cores.len().max(soc.pcpu_cores as usize) as f32;
  let tcores = ecores + pcores;

  let ecpu_min_frequency_mhz = soc.ecpu_freqs.first().copied().unwrap_or_default();
  let pcpu_min_frequency_mhz = soc.pcpu_freqs.first().copied().unwrap_or_default();
  rs.ecpu_freq_mhz = aggregate_frequency(&rs.ecpu_cores, ecpu_min_frequency_mhz);
  rs.ecpu_scaled_ratio = zero_div(ecpu_total_scaled, ecores);
  rs.ecpu_active_ratio = zero_div(ecpu_total_active, ecores);
  rs.pcpu_freq_mhz = aggregate_frequency(&rs.pcpu_cores, pcpu_min_frequency_mhz);
  rs.pcpu_scaled_ratio = zero_div(pcpu_total_scaled, pcores);
  rs.pcpu_active_ratio = zero_div(pcpu_total_active, pcores);
  rs.cpu_scaled_ratio = zero_div(ecpu_total_scaled + pcpu_total_scaled, tcores);
  rs.cpu_active_ratio = zero_div(ecpu_total_active + pcpu_total_active, tcores);
  rs.all_power = rs.cpu_power + rs.gpu_power + rs.ane_power;
  rs
}

fn smc_numeric_value(data: &[u8], unit: &str) -> Option<f32> {
  match unit {
    "flt " if data.len() == 4 => Some(f32::from_le_bytes(data.try_into().ok()?)),
    "fpe2" if data.len() >= 2 => Some(((data[0] as u16) << 6 | ((data[1] as u16) >> 2)) as f32),
    "ui8 " if !data.is_empty() => Some(data[0] as f32),
    "ui16" if data.len() >= 2 => Some(u16::from_be_bytes(data[0..2].try_into().ok()?) as f32),
    "ui32" if data.len() >= 4 => Some(u32::from_be_bytes(data[0..4].try_into().ok()?) as f32),
    _ => None,
  }
}

fn read_smc_numeric_u32(smc: &mut SMC, key: &str) -> Option<u32> {
  let val = smc.read_val(key).ok()?;
  let val = smc_numeric_value(&val.data, &val.unit)?;
  fan_rpm_value(val)
}

fn calc_freq_from_residencies(items: &[(String, i64)], freqs: &[u32]) -> FreqMetrics {
  let (len1, len2) = (items.len(), freqs.len());
  assert!(len1 > len2, "calc_freq invalid data: {len1} vs {len2}"); // todo?

  // CPU layouts are [IDLE, frequencies...] or [DOWN, IDLE, frequencies...];
  // GPU uses OFF before frequency states.
  let offset = items
    .iter()
    .position(|x| x.0 != "IDLE" && x.0 != "DOWN" && x.0 != "OFF")
    .expect("calc_freq missing active states");

  let usage = items.iter().skip(offset).take(freqs.len()).map(|x| x.1 as f64).sum::<f64>();
  let total = items.iter().map(|x| x.1 as f64).sum::<f64>();

  let mut avg_freq = 0f64;
  for i in 0..freqs.len() {
    let percent = zero_div(items[i + offset].1 as _, usage);
    avg_freq += percent * freqs[i] as f64;
  }

  let active_ratio = zero_div(usage, total);
  let min_freq = *freqs.first().unwrap() as f64;
  let max_freq = *freqs.last().unwrap() as f64;
  let scaled_ratio = (avg_freq.max(min_freq) * active_ratio) / max_freq;

  (avg_freq as u32, scaled_ratio as f32, active_ratio as f32)
}

fn calc_freq(item: CFDictionaryRef, freqs: &[u32]) -> FreqMetrics {
  calc_freq_from_residencies(&cfio_get_residencies(item), freqs)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CpuCoreKind {
  E,
  P,
}

fn parse_cpu_core_id(channel: &str, prefix: &str) -> Option<usize> {
  let start = channel.find(prefix)? + prefix.len();
  let digits = channel[start..].chars().take_while(|c| c.is_ascii_digit()).collect::<String>();
  if digits.is_empty() { None } else { digits.parse().ok() }
}

fn parse_die_id(channel: &str) -> usize {
  let Some(rest) = channel.strip_prefix("DIE_") else { return 0 };
  rest.split_once('_').and_then(|(id, _)| id.parse().ok()).unwrap_or(0)
}

fn cpu_core_prefix(channel: &str) -> Option<&'static str> {
  ["PCPU", "ECPU", "MCPU"].into_iter().find(|prefix| channel.contains(prefix))
}

fn cpu_core_sort_key(channel: &str) -> (usize, usize, usize) {
  let die_id = parse_die_id(channel);
  let Some(prefix) = cpu_core_prefix(channel) else { return (die_id, 0, 0) };
  let start = channel.find(prefix).unwrap_or_default() + prefix.len();
  let suffix = &channel[start..];

  if let Some((cluster_id, core_id)) = suffix.split_once("_CPU") {
    let cluster_id = if cluster_id.is_empty() { 0 } else { cluster_id.parse().unwrap_or(0) };
    return (die_id, cluster_id, core_id.parse().unwrap_or(0));
  }

  (die_id, 0, parse_cpu_core_id(channel, prefix).unwrap_or(0))
}

fn parse_cpu_core_channel(channel: &str) -> Option<(CpuCoreKind, CpuCoreKey)> {
  let kind = if channel.contains("PCPU") {
    CpuCoreKind::P
  } else if channel.contains("ECPU") || channel.contains("MCPU") {
    CpuCoreKind::E
  } else {
    return None;
  };

  // Ultra channel numbers identify a cluster and a core separately, so the complete channel name
  // is the only collision-free key shared by both regular and Ultra chips.
  Some((kind, channel.to_owned()))
}

fn collect_cpu_core_metrics(metrics: HashMap<CpuCoreKey, FreqMetrics>) -> Vec<CpuCoreMetrics> {
  let mut metrics: Vec<_> = metrics.into_iter().collect();
  metrics.sort_by_key(|(channel, _)| cpu_core_sort_key(channel));
  let mut next_clustered_core_id = HashMap::<usize, usize>::new();

  metrics
    .into_iter()
    .map(|(channel, (freq_mhz, scaled_ratio, active_ratio))| {
      let die_id = parse_die_id(&channel);
      let core_id = if channel.contains("_CPU") {
        let next = next_clustered_core_id.entry(die_id).or_default();
        let core_id = *next;
        *next += 1;
        core_id
      } else {
        cpu_core_prefix(&channel)
          .and_then(|prefix| parse_cpu_core_id(&channel, prefix))
          .unwrap_or_default()
      };

      CpuCoreMetrics { die_id, core_id, freq_mhz, scaled_ratio, active_ratio }
    })
    .collect()
}

fn init_smc() -> WithError<SmcSensors> {
  let mut smc = SMC::new()?;

  let mut cpu_sensors = Vec::new();
  let mut gpu_sensors = Vec::new();
  let mut fan_sensors = Vec::new();

  let names = smc.read_all_keys().unwrap_or(vec![]);
  for name in &names {
    if name.len() == 4 && name.starts_with('F') && name.ends_with("Ac") {
      fan_sensors.push(name.clone());
      continue;
    }
    // Unfortunately, it is not known which keys are responsible for what.
    // Basically in the code that can be found publicly "Tp" is used for CPU and "Tg" for GPU.

    let is_cpu = name.starts_with("Tp") || name.starts_with("Te") || name.starts_with("Ts");
    let is_gpu = name.starts_with("Tg");
    if !is_cpu && !is_gpu {
      continue;
    }

    if smc.read_float_val(name).is_err() {
      continue;
    }

    if is_cpu {
      cpu_sensors.push(name.clone());
    } else if is_gpu {
      gpu_sensors.push(name.clone());
    }
  }

  // Sort first so fan order is stable and any duplicate keys become adjacent for dedup().
  fan_sensors.sort();
  fan_sensors.dedup();

  // println!("{} {}", cpu_sensors.len(), gpu_sensors.len());
  Ok(SmcSensors { smc, cpu_keys: cpu_sensors, gpu_keys: gpu_sensors, fan_keys: fan_sensors })
}

// MARK: Sampler

/// Hardware metrics sampler for Apple Silicon Macs.
///
/// Create one sampler and call [`Sampler::get_metrics`] in a continuous polling
/// loop. Run the sampler in a worker thread when sampling must not block the
/// application thread.
pub struct Sampler {
  soc: SocInfo,
  ior: IOReport,
  hid: IOHIDSensors,
  smc: SMC,
  smc_cpu_keys: Vec<String>,
  smc_gpu_keys: Vec<String>,
  smc_fan_keys: Vec<String>,
}

impl Sampler {
  /// Initialize hardware metric sources.
  pub fn new() -> WithError<Self> {
    let soc = get_soc_info()?;
    let ior = IOReport::with_filter(Some(ioreport_channels_filter))?;
    let hid = IOHIDSensors::new()?;
    let smc_sensors = init_smc()?;

    Ok(Sampler {
      soc,
      ior,
      hid,
      smc: smc_sensors.smc,
      smc_cpu_keys: smc_sensors.cpu_keys,
      smc_gpu_keys: smc_sensors.gpu_keys,
      smc_fan_keys: smc_sensors.fan_keys,
    })
  }

  fn get_temp_smc(&mut self) -> WithError<TempMetrics> {
    let mut cpu_metrics = Vec::new();
    for sensor in &self.smc_cpu_keys {
      let val = self.smc.read_float_val(sensor)?;
      if is_valid_temp(val) {
        cpu_metrics.push(val);
      }
    }

    let mut gpu_metrics = Vec::new();
    for sensor in &self.smc_gpu_keys {
      let val = self.smc.read_float_val(sensor)?;
      if is_valid_temp(val) {
        gpu_metrics.push(val);
      }
    }

    let cpu_temp_avg = zero_div(cpu_metrics.iter().sum::<f32>(), cpu_metrics.len() as f32);
    let gpu_temp_avg = zero_div(gpu_metrics.iter().sum::<f32>(), gpu_metrics.len() as f32);

    Ok(TempMetrics { cpu_temp_avg, gpu_temp_avg })
  }

  fn get_temp_hid(&mut self) -> WithError<TempMetrics> {
    let metrics = self.hid.get_metrics();

    let mut cpu_values = Vec::new();
    let mut gpu_values = Vec::new();

    for (name, value) in &metrics {
      if name.starts_with("pACC MTR Temp Sensor") || name.starts_with("eACC MTR Temp Sensor") {
        // println!("{}: {}", name, value);
        if is_valid_temp(*value) {
          cpu_values.push(*value);
        }
        continue;
      }

      if name.starts_with("GPU MTR Temp Sensor") {
        // println!("{}: {}", name, value);
        if is_valid_temp(*value) {
          gpu_values.push(*value);
        }
        continue;
      }
    }

    let cpu_temp_avg = zero_div(cpu_values.iter().sum(), cpu_values.len() as f32);
    let gpu_temp_avg = zero_div(gpu_values.iter().sum(), gpu_values.len() as f32);

    Ok(TempMetrics { cpu_temp_avg, gpu_temp_avg })
  }

  fn get_temp(&mut self) -> WithError<TempMetrics> {
    // HID for M1, SMC for M2/M3
    // UPD: Looks like HID/SMC related to OS version, not to the chip (SMC available from macOS 14)
    match !self.smc_cpu_keys.is_empty() {
      true => self.get_temp_smc(),
      false => self.get_temp_hid(),
    }
  }

  fn get_fans(&mut self) -> Vec<FanMetric> {
    let mut fans = Vec::new();
    for (i, key) in self.smc_fan_keys.iter().enumerate() {
      let Some(rpm) = read_smc_numeric_u32(&mut self.smc, key) else { continue };
      let name = format!("fan{i}");
      let max_rpm = match key.strip_suffix("Ac") {
        Some(prefix) => {
          read_smc_numeric_u32(&mut self.smc, &format!("{prefix}Mx")).filter(|rpm| *rpm > 0)
        }
        None => None,
      };
      fans.push(FanMetric { name, rpm, max_rpm });
    }
    fans
  }

  fn get_mem(&mut self) -> WithError<MemMetrics> {
    let (ram_usage, ram_total) = libc_ram()?;
    let (swap_usage, swap_total) = libc_swap()?;
    Ok(MemMetrics { ram_total, ram_usage, swap_total, swap_usage })
  }

  fn get_sys_power(&mut self) -> WithError<f32> {
    self.smc.read_float_val("PSTR")
  }

  fn get_ioreport_metrics(
    &self,
    sample: crate::sources::IOReportIterator,
    dt: Duration,
  ) -> WithError<Metrics> {
    let mut ecpu_map: HashMap<CpuCoreKey, FreqMetrics> = HashMap::new();
    let mut pcpu_map: HashMap<CpuCoreKey, FreqMetrics> = HashMap::new();
    let mut rs = Metrics::default();

    // Keep this channel handling in sync with ioreport_channels_filter.
    for x in sample {
      if x.group == "CPU Stats" && x.subgroup == CPU_FREQ_CORE_SUBG {
        match parse_cpu_core_channel(&x.channel) {
          Some((CpuCoreKind::P, key)) => {
            let metrics = calc_freq(x.item, &self.soc.pcpu_freqs);
            pcpu_map.insert(key, metrics);
            continue;
          }
          Some((CpuCoreKind::E, key)) => {
            ecpu_map.insert(key, calc_freq(x.item, &self.soc.ecpu_freqs));
            continue;
          }
          None => {}
        }
      }

      if x.group == "GPU Stats" && x.subgroup == GPU_FREQ_DICE_SUBG {
        match x.channel.as_str() {
          "GPUPH" => {
            let (freq, scaled_ratio, active_ratio) = calc_freq(x.item, &self.soc.gpu_freqs[1..]);
            rs.gpu_freq_mhz = freq;
            rs.gpu_scaled_ratio = scaled_ratio;
            rs.gpu_active_ratio = active_ratio;
          }
          _ => {}
        }
      }

      if x.group == "Energy Model" {
        match x.channel.as_str() {
          "GPU Energy" => rs.gpu_power += cfio_watts(x.item, &x.unit, dt)?,
          // "CPU Energy" for Basic / Max, "DIE_{}_CPU Energy" for Ultra
          c if c.ends_with("CPU Energy") => rs.cpu_power += cfio_watts(x.item, &x.unit, dt)?,
          // same pattern next keys: "ANE" for Basic, "ANE0" for Max, "ANE0_{}" for Ultra
          c if c.starts_with("ANE") => rs.ane_power += cfio_watts(x.item, &x.unit, dt)?,
          c if c.starts_with("DRAM") => rs.ram_power += cfio_watts(x.item, &x.unit, dt)?,
          c if c.starts_with("GPU SRAM") => rs.gpu_ram_power += cfio_watts(x.item, &x.unit, dt)?,
          _ => {}
        }
      }
    }

    rs.ecpu_cores = collect_cpu_core_metrics(ecpu_map);
    rs.pcpu_cores = collect_cpu_core_metrics(pcpu_map);

    Ok(rs)
  }

  /// Collect metrics for the next polling interval.
  ///
  /// Intended to be called continuously in a polling loop. The sampler keeps
  /// an IOReport baseline between calls and derives metrics from the complete
  /// interval between consecutive samples.
  ///
  /// `duration` is the requested polling interval in milliseconds.
  pub fn get_metrics(&mut self, duration: u32) -> WithError<Metrics> {
    // CPU Stats channel naming by chip family (see: https://github.com/vladkens/macmon/issues/47)
    //   M1-M4:  ECPU* = efficiency cores (lower tier)
    //           PCPU* = performance cores (top tier)
    //   M5:     The family has three core tiers, but current chips expose two at a time:
    //             Base:    PCPU* = Super, ECPU* = Efficiency
    //             Pro/Max: PCPU* = Super, MCPU* = Performance
    //           MCPU is a separate middle-tier design, not a renamed ECPU core.
    //   Ultra:  Any-generation Ultra chips prefix channels with "DIE_N_"
    //           and include cluster/core separators (e.g. "DIE_0_PCPU1_CPU0").

    let duration = Duration::from_millis(duration as u64);
    let (sample, elapsed) = self.ior.get_sample_interval(duration);
    let mut rs = aggregate_ioreport_metrics(self.get_ioreport_metrics(sample, elapsed)?, &self.soc);

    rs.memory = self.get_mem()?;
    rs.temp = self.get_temp()?;
    rs.fans = self.get_fans();

    rs.sys_power = match self.get_sys_power() {
      Ok(val) => val.max(rs.all_power),
      Err(_) => 0.0,
    };

    Ok(rs)
  }

  /// Return static SoC information used by this sampler.
  pub fn get_soc_info(&self) -> &SocInfo {
    &self.soc
  }
}

#[cfg(test)]
mod tests {
  use std::collections::{HashMap, HashSet};

  use crate::sources::SocInfo;

  use super::{
    CpuCoreKind, CpuCoreMetrics, Metrics, aggregate_ioreport_metrics, calc_freq_from_residencies,
    collect_cpu_core_metrics, parse_cpu_core_channel, smc_numeric_value,
  };

  fn core(
    die_id: usize,
    core_id: usize,
    freq_mhz: u32,
    scaled_ratio: f32,
    active_ratio: f32,
  ) -> CpuCoreMetrics {
    CpuCoreMetrics { die_id, core_id, freq_mhz, scaled_ratio, active_ratio }
  }

  fn soc_info(ecpu_cores: u8, pcpu_cores: u8) -> SocInfo {
    SocInfo {
      ecpu_cores,
      pcpu_cores,
      ecpu_freqs: vec![800],
      pcpu_freqs: vec![1800],
      ..Default::default()
    }
  }

  #[test]
  fn parse_smc_numeric_values() {
    assert_eq!(smc_numeric_value(&42.5f32.to_le_bytes(), "flt "), Some(42.5));
    assert_eq!(smc_numeric_value(&[0x13, 0x88], "fpe2"), Some(1250.0));
    assert_eq!(smc_numeric_value(&[0x04, 0xd2], "ui16"), Some(1234.0));
    assert_eq!(smc_numeric_value(&[0x00, 0x00, 0x04, 0xd2], "ui32"), Some(1234.0));
  }

  #[test]
  fn aggregates_ioreport_metrics() {
    let rs = aggregate_ioreport_metrics(
      Metrics {
        ecpu_cores: vec![core(0, 0, 2000, 1.0, 1.0)],
        pcpu_cores: vec![core(0, 0, 0, 0.0, 0.0), core(0, 1, 4000, 1.0, 1.0)],
        cpu_power: 1.5,
        gpu_power: 2.0,
        ane_power: 0.5,
        ..Default::default()
      },
      &soc_info(2, 1),
    );

    assert_eq!(rs.ecpu_freq_mhz, 2000);
    assert_eq!(rs.ecpu_scaled_ratio, 0.5);
    assert_eq!(rs.ecpu_active_ratio, 0.5);
    assert_eq!(rs.pcpu_freq_mhz, 2000);
    assert_eq!(rs.pcpu_scaled_ratio, 0.5);
    assert_eq!(rs.pcpu_active_ratio, 0.5);
    assert_eq!(rs.cpu_scaled_ratio, 0.5);
    assert_eq!(rs.cpu_active_ratio, 0.5);
    assert_eq!(rs.all_power, 4.0);
  }

  #[test]
  fn frequency_uses_minimum_floor_when_cluster_is_idle() {
    let rs = aggregate_ioreport_metrics(
      Metrics {
        ecpu_cores: vec![core(0, 0, 0, 0.0, 0.0), core(0, 1, 0, 0.0, 0.0)],
        pcpu_cores: vec![core(0, 0, 0, 0.0, 0.0)],
        ..Default::default()
      },
      &soc_info(0, 0),
    );

    assert_eq!(rs.ecpu_freq_mhz, 800);
    assert_eq!(rs.pcpu_freq_mhz, 1800);
  }

  #[test]
  fn orders_named_core_metrics() {
    let cores = collect_cpu_core_metrics(HashMap::from([
      ("DIE_1_ECPU0".into(), (2000, 0.50, 0.75)),
      ("DIE_0_ECPU1".into(), (1000, 0.25, 0.50)),
    ]));

    assert_eq!((cores[0].die_id, cores[0].core_id), (0, 1));
    assert_eq!((cores[1].die_id, cores[1].core_id), (1, 0));
  }

  #[test]
  fn calculates_frequency_over_the_complete_residency_window() {
    let (frequency, scaled_ratio, active_ratio) = calc_freq_from_residencies(
      &[
        ("DOWN".into(), 0),
        ("IDLE".into(), 500),
        ("1000 MHz".into(), 100),
        ("2000 MHz".into(), 400),
      ],
      &[1000, 2000],
    );

    assert_eq!(frequency, 1800);
    assert!((scaled_ratio - 0.45).abs() < f32::EPSILON);
    assert!((active_ratio - 0.5).abs() < f32::EPSILON);
  }

  #[test]
  fn treats_down_as_dynamic_inactive_residency() {
    let (frequency, scaled_ratio, active_ratio) = calc_freq_from_residencies(
      &[
        ("DOWN".into(), 800),
        ("IDLE".into(), 100),
        ("1000 MHz".into(), 100),
        ("2000 MHz".into(), 0),
      ],
      &[1000, 2000],
    );

    assert_eq!(frequency, 1000);
    assert!((scaled_ratio - 0.05).abs() < f32::EPSILON);
    assert!((active_ratio - 0.1).abs() < f32::EPSILON);
  }

  #[test]
  fn ultra_cpu_channel_matching() {
    // On Ultra chips (M1/M2/M3 Ultra) IOReport CPU Stats channels are prefixed "DIE_N_".
    // The DIE_N prefix must not be mistaken for the core id.
    let cases = [
      ("DIE_0_ECPU0", Some(CpuCoreKind::E)),
      ("DIE_1_ECPU0", Some(CpuCoreKind::E)),
      ("DIE_0_PCPU0", Some(CpuCoreKind::P)),
      ("DIE_1_PCPU0", Some(CpuCoreKind::P)),
      ("ECPU7", Some(CpuCoreKind::E)),
      ("PCPU12", Some(CpuCoreKind::P)),
      ("MCPU3", Some(CpuCoreKind::E)), // M5+ performance cores map to ecpu slot
      ("GPU0", None),
    ];
    for (channel, expected) in cases {
      assert_eq!(
        parse_cpu_core_channel(channel).map(|(kind, _)| kind),
        expected,
        "channel {channel}"
      );
    }
  }

  #[test]
  fn parses_real_m3_ultra_core_channels_without_collisions() {
    let channels = [
      "DIE_0_ECPU_CPU0",
      "DIE_0_ECPU_CPU1",
      "DIE_0_PCPU_CPU0",
      "DIE_0_PCPU_CPU1",
      "DIE_0_PCPU1_CPU0",
      "DIE_0_PCPU1_CPU1",
      "DIE_1_ECPU_CPU0",
      "DIE_1_PCPU_CPU0",
      "DIE_1_PCPU1_CPU0",
    ];
    let parsed = channels.map(parse_cpu_core_channel);

    assert!(parsed.iter().all(Option::is_some));
    let unique = parsed
      .into_iter()
      .flatten()
      .map(|(kind, key)| (kind == CpuCoreKind::P, key))
      .collect::<HashSet<_>>();
    assert_eq!(unique.len(), channels.len());
  }

  #[test]
  fn flattens_real_ultra_cluster_channels_into_core_ids() {
    let cores = collect_cpu_core_metrics(HashMap::from([
      ("DIE_0_PCPU1_CPU0".into(), (2000, 0.50, 0.75)),
      ("DIE_0_PCPU_CPU1".into(), (1100, 0.25, 0.50)),
      ("DIE_0_PCPU_CPU0".into(), (1000, 0.20, 0.40)),
      ("DIE_1_PCPU_CPU0".into(), (1200, 0.30, 0.60)),
    ]));

    assert_eq!(
      cores.iter().map(|core| (core.die_id, core.core_id, core.freq_mhz)).collect::<Vec<_>>(),
      [(0, 0, 1000), (0, 1, 1100), (0, 2, 2000), (1, 0, 1200)]
    );
  }
}