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
//! Apple Silicon monitoring via powermetrics and IOKit
//!
//! This module implements comprehensive monitoring for Apple Silicon (M1/M2/M3/M4 series)
//! using macOS's powermetrics utility and IOKit framework.
//!
//! Based on asitop: <https://github.com/tlkh/asitop>
use super::*;
use crate::error::{Error, Result};
use std::collections::HashMap;
use std::process::{Child, Command, Stdio};
#[cfg(all(feature = "apple", target_os = "macos"))]
use plist::Value;
/// Apple Silicon monitor
pub struct AppleSiliconMonitor {
powermetrics_process: Option<Child>,
soc_info: SocInfo,
}
/// SOC information
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct SocInfo {
name: String,
e_core_count: u32,
p_core_count: u32,
}
/// Powermetrics data
#[derive(Debug, Default)]
pub struct PowermetricsData {
// CPU metrics
pub e_cluster_freq_mhz: u32,
pub e_cluster_active: u8,
pub p_cluster_freq_mhz: u32,
pub p_cluster_active: u8,
// Per-core metrics
pub e_cores: HashMap<u32, (u32, u8)>, // (freq, utilization)
pub p_cores: HashMap<u32, (u32, u8)>,
// GPU metrics
pub gpu_freq_mhz: u32,
pub gpu_active: u8,
// Power metrics
pub cpu_power_mw: u32,
pub gpu_power_mw: u32,
pub ane_power_mw: u32,
pub package_power_mw: u32,
// Thermal
pub thermal_pressure: String,
}
impl AppleSiliconMonitor {
/// Create new Apple Silicon monitor
pub fn new() -> Result<Self> {
let soc_info = Self::detect_soc_info()?;
Ok(Self {
powermetrics_process: None,
soc_info,
})
}
/// Detect SOC information using sysctl and system_profiler
fn detect_soc_info() -> Result<SocInfo> {
// Get CPU brand string
let output = Command::new("sysctl")
.args(["-n", "machdep.cpu.brand_string"])
.output()
.map_err(|e| Error::CommandExecutionFailed(format!("sysctl: {}", e)))?;
let name = String::from_utf8_lossy(&output.stdout).trim().to_string();
// Core counts, or zero where the sysctl did not answer.
//
// These fell back to `4`, `4` and `8` -- **a specific and entirely
// plausible machine**, which is what makes the fabrication worse than
// the usual one. An M1 is 4+4 with 8 GPU cores, so a failed read
// described the hardware as a base M1 whatever it actually was, and
// nothing downstream could tell the difference: `core_ids` came out as
// `0..4` and looked like a topology somebody had measured.
//
// A `1` is implausible enough to be noticed. A `4` is not, and that is
// the argument against "sensible defaults" in a reader: the more
// reasonable the invented value, the longer it survives.
//
// Zero here means the count was not read. `(0..0)` yields an empty
// `core_ids`, and the cluster still carries the frequency and
// utilisation `powermetrics` actually measured -- an unknown
// composition, not an unknown cluster.
let e_cores = Self::get_sysctl_value("hw.perflevel1.logicalcpu").unwrap_or(0);
let p_cores = Self::get_sysctl_value("hw.perflevel0.logicalcpu").unwrap_or(0);
// There was a per-chip power table here -- CPU, GPU and ANE ceilings
// by brand-string match, with `(20.0, 20.0, 8.0)` for any chip it did
// not name. Its only reader divided the ANE's measured draw by that
// ceiling and reported the result as utilisation; see `npu_info`. A GPU
// core count read from `system_profiler` sat beside it, stored and
// never read. Both are gone, and with them a `system_profiler` spawn on
// every construction.
Ok(SocInfo {
name,
e_core_count: e_cores,
p_core_count: p_cores,
})
}
/// Get sysctl value as u32
fn get_sysctl_value(key: &str) -> Option<u32> {
let output = Command::new("sysctl").args(["-n", key]).output().ok()?;
String::from_utf8_lossy(&output.stdout).trim().parse().ok()
}
/// Start powermetrics process
#[allow(dead_code)]
fn start_powermetrics(&mut self, interval_ms: u32) -> Result<()> {
let temp_file = format!("/tmp/ironmon_powermetrics_{}", std::process::id());
let child = Command::new("sudo")
.args([
"powermetrics",
"--samplers",
"cpu_power,gpu_power,thermal",
"-o",
&temp_file,
"-f",
"plist",
"-i",
&interval_ms.to_string(),
])
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.map_err(|e| Error::CommandExecutionFailed(format!("powermetrics: {}", e)))?;
self.powermetrics_process = Some(child);
// Wait for first data
std::thread::sleep(std::time::Duration::from_millis(interval_ms as u64 + 100));
Ok(())
}
/// Parse powermetrics output
pub fn parse_powermetrics(&self) -> Result<PowermetricsData> {
#[cfg(all(feature = "apple", target_os = "macos"))]
{
let temp_file = format!("/tmp/ironmon_powermetrics_{}", std::process::id());
// Read the plist file
let data = std::fs::read(&temp_file).map_err(Error::Io)?;
// Split by null bytes (powermetrics appends multiple plists)
let parts: Vec<&[u8]> = data.split(|&b| b == 0).collect();
// Parse the last complete plist
if let Some(last_plist) = parts.iter().rev().find(|p| !p.is_empty()) {
let value = Value::from_reader(std::io::Cursor::new(last_plist))
.map_err(|e| Error::ParseError(format!("plist: {}", e)))?;
return self.parse_plist_data(&value);
}
}
// Fallback to default data
Ok(PowermetricsData::default())
}
#[cfg(all(feature = "apple", target_os = "macos"))]
#[allow(dead_code)]
/// Parse plist data into PowermetricsData
fn parse_plist_data(&self, value: &Value) -> Result<PowermetricsData> {
let mut data = PowermetricsData::default();
if let Some(dict) = value.as_dictionary() {
// Parse thermal pressure
if let Some(Value::String(thermal)) = dict.get("thermal_pressure") {
data.thermal_pressure = thermal.clone();
}
// Parse processor data
if let Some(Value::Dictionary(processor)) = dict.get("processor") {
// Parse clusters
if let Some(Value::Array(clusters)) = processor.get("clusters") {
for cluster in clusters {
if let Some(cluster_dict) = cluster.as_dictionary() {
if let Some(Value::String(name)) = cluster_dict.get("name") {
// Get cluster metrics
let freq_hz = cluster_dict
.get("freq_hz")
.and_then(|v| v.as_unsigned_integer())
.unwrap_or(0);
let freq_mhz = (freq_hz / 1_000_000) as u32;
let idle_ratio = cluster_dict
.get("idle_ratio")
.and_then(|v| v.as_real())
.unwrap_or(1.0);
let active = ((1.0 - idle_ratio) * 100.0) as u8;
// Assign to E or P cluster
if name.starts_with('E') {
data.e_cluster_freq_mhz = freq_mhz;
data.e_cluster_active = active;
} else if name.starts_with('P') {
data.p_cluster_freq_mhz = freq_mhz;
data.p_cluster_active = active;
}
// Parse individual cores
if let Some(Value::Array(cpus)) = cluster_dict.get("cpus") {
for cpu in cpus {
if let Some(cpu_dict) = cpu.as_dictionary() {
let cpu_num = cpu_dict
.get("cpu")
.and_then(|v| v.as_unsigned_integer())
.unwrap_or(0)
as u32;
let cpu_freq_hz = cpu_dict
.get("freq_hz")
.and_then(|v| v.as_unsigned_integer())
.unwrap_or(0);
let cpu_freq_mhz = (cpu_freq_hz / 1_000_000) as u32;
let cpu_idle = cpu_dict
.get("idle_ratio")
.and_then(|v| v.as_real())
.unwrap_or(1.0);
let cpu_active = ((1.0 - cpu_idle) * 100.0) as u8;
if name.starts_with('E') {
data.e_cores
.insert(cpu_num, (cpu_freq_mhz, cpu_active));
} else {
data.p_cores
.insert(cpu_num, (cpu_freq_mhz, cpu_active));
}
}
}
}
}
}
}
}
// Parse power metrics
if let Some(Value::Real(cpu_power)) = processor.get("cpu_energy") {
data.cpu_power_mw = *cpu_power as u32;
}
if let Some(Value::Real(gpu_power)) = processor.get("gpu_energy") {
data.gpu_power_mw = *gpu_power as u32;
}
if let Some(Value::Real(ane_power)) = processor.get("ane_energy") {
data.ane_power_mw = *ane_power as u32;
}
if let Some(Value::Real(package_power)) = processor.get("combined_power") {
data.package_power_mw = *package_power as u32;
}
}
// Parse GPU data
if let Some(Value::Dictionary(gpu)) = dict.get("gpu") {
if let Some(Value::Integer(freq_hz)) = gpu.get("freq_hz") {
// `plist::Integer` is not a primitive; it must be asked for its value.
data.gpu_freq_mhz = (freq_hz.as_unsigned().unwrap_or(0) / 1_000_000) as u32;
}
if let Some(Value::Real(idle_ratio)) = gpu.get("idle_ratio") {
data.gpu_active = ((1.0 - idle_ratio) * 100.0) as u8;
}
}
}
Ok(data)
}
#[cfg(not(all(feature = "apple", target_os = "macos")))]
#[allow(dead_code)]
fn parse_plist_data(&self, _value: &()) -> Result<PowermetricsData> {
Ok(PowermetricsData::default())
}
}
impl SiliconMonitor for AppleSiliconMonitor {
fn cpu_info(&self) -> Result<(Vec<CpuCore>, Vec<CpuCluster>)> {
let data = self.parse_powermetrics()?;
let mut cores = Vec::new();
let mut clusters = Vec::new();
// E-cores
for (id, (freq, util)) in &data.e_cores {
cores.push(CpuCore {
id: *id,
cluster: CpuClusterType::Efficiency,
frequency_mhz: Some(*freq),
utilization: Some(*util),
temperature: None,
});
}
// P-cores
for (id, (freq, util)) in &data.p_cores {
cores.push(CpuCore {
id: *id,
cluster: CpuClusterType::Performance,
frequency_mhz: Some(*freq),
utilization: Some(*util),
temperature: None,
});
}
// E-cluster. An empty `core_ids` means the per-level core count was
// not read, not that the cluster has no cores; the figures beside it
// are measured either way.
//
// Neither cluster reports power. `powermetrics` gives one CPU figure,
// and this used to split it 40/60 between the clusters -- a fixed
// ratio presented as two per-cluster readings, the E-cluster's
// unchanged whether it was idle or saturated.
clusters.push(CpuCluster {
cluster_type: CpuClusterType::Efficiency,
core_ids: (0..self.soc_info.e_core_count).collect(),
frequency_mhz: Some(data.e_cluster_freq_mhz),
utilization: Some(data.e_cluster_active),
power_watts: None,
});
// P-cluster
clusters.push(CpuCluster {
cluster_type: CpuClusterType::Performance,
core_ids: (0..self.soc_info.p_core_count).collect(),
frequency_mhz: Some(data.p_cluster_freq_mhz),
utilization: Some(data.p_cluster_active),
power_watts: None,
});
Ok((cores, clusters))
}
fn npu_info(&self) -> Result<Vec<NpuInfo>> {
let data = self.parse_powermetrics()?;
Ok(vec![NpuInfo {
name: "Apple Neural Engine".to_string(),
vendor: "Apple".to_string(),
// "Most" is not this machine. The ANE core count is not read.
cores: None,
// Not reported. This was the ANE's measured draw divided by a
// ceiling from a brand-string table -- 8 W for any chip the table
// did not name -- which is the GPU power-percentage defect over
// again: a real numerator over an invented denominator, reaching
// the TUI's accelerator panel as a utilisation. `powermetrics`
// reports no ANE activity figure, so there is nothing to put here.
utilization: None,
power_watts: Some(data.ane_power_mw as f32 / 1000.0),
frequency_mhz: None,
}])
}
fn io_info(&self) -> Result<Vec<IoController>> {
let mut controllers = Vec::new();
// Get disk I/O stats from iostat
if let Ok(output) = Command::new("iostat").args(["-d", "-c", "1"]).output() {
let text = String::from_utf8_lossy(&output.stdout);
// iostat -d outputs: device, KB/t, tps, MB/s
for line in text.lines().skip(2) {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 4 {
let name = parts[0].to_string();
let mbps: f64 = parts[3].parse().unwrap_or(0.0);
controllers.push(IoController {
controller_type: if name.starts_with("disk") {
"NVMe".to_string()
} else {
"Storage".to_string()
},
name,
bandwidth_mbps: Some(mbps),
// 7000 was the spec figure for Apple Silicon's fastest
// NVMe, applied to whatever this disk is. Nothing about
// the actual link is read here.
max_bandwidth_mbps: None,
power_watts: None,
});
}
}
}
// Thunderbolt controllers via system_profiler
if let Ok(output) = Command::new("system_profiler")
.args(["-detailLevel", "mini", "SPThunderboltDataType"])
.output()
{
let text = String::from_utf8_lossy(&output.stdout);
let mut tb_count = 0u32;
for line in text.lines() {
if (line.trim().starts_with("Speed:") || line.trim().contains("Thunderbolt"))
&& line.contains("Up to")
{
tb_count += 1;
controllers.push(IoController {
controller_type: "Thunderbolt".to_string(),
name: format!("Thunderbolt Port {}", tb_count),
// The *current* bandwidth was set to the theoretical
// maximum, so an idle Thunderbolt port reported 5000
// MB/s of traffic — a fabricated saturated bus, not a
// sentinel zero. Nothing here reads either figure.
bandwidth_mbps: None,
max_bandwidth_mbps: None,
power_watts: None,
});
}
}
}
Ok(controllers)
}
fn network_info(&self) -> Result<Vec<NetworkSilicon>> {
let mut networks = Vec::new();
// Get active network interfaces via ifconfig
if let Ok(output) = Command::new("ifconfig").output() {
let text = String::from_utf8_lossy(&output.stdout);
let mut current_iface = String::new();
let mut is_up = false;
for line in text.lines() {
if !line.starts_with('\t') && !line.starts_with(' ') && line.contains(':') {
// New interface line like "en0: flags=8863<UP,...>"
let iface = line.split(':').next().unwrap_or("").to_string();
if !current_iface.is_empty() && is_up && !current_iface.starts_with("lo") {
let interface_type = classify_macos_interface(¤t_iface);
networks.push(NetworkSilicon {
interface: interface_type,
link_speed_mbps: None,
rx_bandwidth_mbps: 0.0,
tx_bandwidth_mbps: 0.0,
packet_rate: 0,
power_state: Some("active".to_string()),
});
}
current_iface = iface;
is_up = line.contains("UP");
}
}
// Push last
if !current_iface.is_empty() && is_up && !current_iface.starts_with("lo") {
let interface_type = classify_macos_interface(¤t_iface);
networks.push(NetworkSilicon {
interface: interface_type,
link_speed_mbps: None,
rx_bandwidth_mbps: 0.0,
tx_bandwidth_mbps: 0.0,
packet_rate: 0,
power_state: Some("active".to_string()),
});
}
}
// Get bandwidth from netstat
if let Ok(output) = Command::new("netstat").args(["-ib"]).output() {
let text = String::from_utf8_lossy(&output.stdout);
for line in text.lines().skip(1) {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 7 {
let iface = parts[0];
// Find matching network entry and update bytes
if let Some(net) = networks.iter_mut().find(|n| {
(n.interface == "WiFi" && iface == "en0")
|| (n.interface == "Ethernet" && iface.starts_with("en"))
}) {
if let Ok(ibytes) = parts[6].parse::<u64>() {
net.rx_bandwidth_mbps = ibytes as f64 / (1024.0 * 1024.0);
}
if parts.len() > 9 {
if let Ok(obytes) = parts[9].parse::<u64>() {
net.tx_bandwidth_mbps = obytes as f64 / (1024.0 * 1024.0);
}
}
}
}
}
}
Ok(networks)
}
}
impl Drop for AppleSiliconMonitor {
fn drop(&mut self) {
if let Some(mut child) = self.powermetrics_process.take() {
let _ = child.kill();
}
}
}
/// Classify macOS interface name to type
fn classify_macos_interface(iface: &str) -> String {
if iface == "en0" {
"WiFi".to_string()
} else if iface.starts_with("en") {
"Ethernet".to_string()
} else if iface.starts_with("bridge") {
"Bridge".to_string()
} else if iface.starts_with("awdl") {
"AirDrop".to_string()
} else if iface.starts_with("utun") {
"VPN Tunnel".to_string()
} else {
iface.to_string()
}
}
// `estimate_link_speed` was here, and its own doc comment said what was wrong
// with it: "These are guesses keyed off the interface name, not measurements:
// `en0` is assumed to be Wi-Fi 6 and anything else `en*`/`bridge*` gigabit
// Ethernet. Callers must not present the result as a read link rate."
//
// The caller two lines away assigned it straight to `link_speed_mbps`. A
// warning in a doc comment does not bind the caller; a type does. The field is
// `Option<u32>` and reads `None` here until something calls `IOKit` for the
// interface's active media type.
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]
#[cfg(target_os = "macos")]
fn test_detect_soc() {
let soc = AppleSiliconMonitor::detect_soc_info();
if let Ok(soc_info) = soc {
println!("Detected SOC: {}", soc_info.name);
println!(
"E-cores: {}, P-cores: {}",
soc_info.e_core_count, soc_info.p_core_count
);
}
}
}