The best way to control Claude Code, Codex and more from any computer or phone
perf(resource-monitor): Linux terminal process checks use a third of the CPU (#30381)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Jai Khurana committed
Nov 6, 2026 at 09:57 UTC
f63e23e9479e07ba5a944ed4b12c1a3018b6932f
1 file changed
+84
native/resource-monitor/src/main.rs
+84
@@ -375,6 +375,35 @@ impl Collector {
375
self.cpu_baseline_refreshed_at = Some(Instant::now());
376
}
377
378
+ // Terminal polling asks for this every second while a shell is open. On
379
+ // Linux one read of /proc/<pid>/stat has everything; sysinfo also opens
380
+ // /proc/<pid>/status and builds a full record per process, ~3x the CPU.
381
+ #[cfg(target_os = "linux")]
382
+ fn process_table(&self) -> Vec<ProcessTableEntry> {
383
+ use std::io::Read;
384
+
385
+ let Ok(entries) = std::fs::read_dir("/proc") else {
386
+ return Vec::new();
387
+ };
388
+ let mut buffer = Vec::with_capacity(512);
389
+ let mut processes = entries
390
+ .filter_map(|entry| {
391
+ let entry = entry.ok()?;
392
+ let pid = entry.file_name().to_str()?.parse::<u32>().ok()?;
393
+ buffer.clear();
394
+ std::fs::File::open(entry.path().join("stat"))
395
+ .ok()?
396
+ .read_to_end(&mut buffer)
397
+ .ok()?;
398
+ let (name, ppid) = parse_proc_stat(&buffer)?;
399
+ Some(ProcessTableEntry { pid, ppid, name })
400
+ })
401
+ .collect::<Vec<_>>();
402
+ processes.sort_by_key(|process| process.pid);
403
+ processes
404
+ }
405
+
406
+ #[cfg(not(target_os = "linux"))]
407
fn process_table(&self) -> Vec<ProcessTableEntry> {
408
// A separate System preserves snapshot CPU baselines.
409
let mut process_table_system = System::new();
@@ -575,6 +604,24 @@ fn process_refresh_kind() -> ProcessRefreshKind {
604
.without_tasks()
605
}
606
607
+/// Name and parent pid from `/proc/<pid>/stat`: `pid (comm) state ppid ...`.
608
+/// The name may itself contain spaces and parentheses, so it ends at the last `)`.
609
+#[cfg(any(target_os = "linux", test))]
610
+fn parse_proc_stat(stat: &[u8]) -> Option<(String, u32)> {
611
+ let open = stat.iter().position(|&byte| byte == b'(')?;
612
+ let close = stat.iter().rposition(|&byte| byte == b')')?;
613
+ let name = stat.get(open + 1..close)?;
614
+ let mut fields = stat.get(close + 1..)?.split(|&byte| byte == b' ');
615
+ fields.next()?;
616
+ fields.next()?;
617
+ let ppid = std::str::from_utf8(fields.next()?).ok()?.parse().ok()?;
618
+ let name = truncate_utf8(
619
+ String::from_utf8_lossy(name).into_owned(),
620
+ MAX_PROCESS_NAME_BYTES,
621
+ );
622
+ Some((name, ppid))
623
+}
624
+
625
fn inaccessible_process_count(selected: usize, materialized: usize) -> usize {
626
selected.saturating_sub(materialized)
627
}
@@ -1338,6 +1385,43 @@ mod tests {
1385
assert!(truncated.is_char_boundary(truncated.len()));
1386
}
1387
1388
+ #[test]
1389
+ fn parses_proc_stat_names_with_spaces_and_parentheses() {
1390
+ assert_eq!(
1391
+ parse_proc_stat(b"4242 (tmux: server) S 1 4242 4242 0 -1 4194560 3101"),
1392
+ Some(("tmux: server".to_owned(), 1))
1393
+ );
1394
+ assert_eq!(
1395
+ parse_proc_stat(b"77 (a) b (c)) R 4242 77 77 34816 77 4194304"),
1396
+ Some(("a) b (c)".to_owned(), 4242))
1397
+ );
1398
+ assert_eq!(parse_proc_stat(b"77 (truncated"), None);
1399
+ }
1400
+
1401
+ #[cfg(target_os = "linux")]
1402
+ #[test]
1403
+ fn linux_process_table_reports_own_process_like_sysinfo() {
1404
+ let collector = Collector::new();
1405
+ let mut system = System::new();
1406
+ system.refresh_processes_specifics(
1407
+ ProcessesToUpdate::All,
1408
+ true,
1409
+ ProcessRefreshKind::nothing().without_tasks(),
1410
+ );
1411
+ let own_pid = std::process::id();
1412
+ let table = collector.process_table();
1413
+ let own = table
1414
+ .iter()
1415
+ .find(|process| process.pid == own_pid)
1416
+ .expect("own process");
1417
+ let expected = system
1418
+ .process(Pid::from_u32(own_pid))
1419
+ .expect("sysinfo own process");
1420
+ assert_eq!(own.ppid, expected.parent().map(Pid::as_u32).unwrap_or(0));
1421
+ assert_eq!(own.name, expected.name().to_string_lossy());
1422
+ assert!(table.windows(2).all(|pair| pair[0].pid < pair[1].pid));
1423
+ }
1424
+
1425
#[test]
1426
fn refreshes_commands_without_enumerating_linux_tasks() {
1427
let refresh_kind = process_refresh_kind();