From 38f2af41f75912cd322f9a2a4e0c6c60d795a7ec Mon Sep 17 00:00:00 2001 From: Levi Neuwirth Date: Wed, 19 Aug 2026 14:31:53 +0200 Subject: [PATCH] test(diag): name the /proc/stat field helper for what it does The D1/D2 instrument carried a helper called african_close() that returned ")". The name was meaningless --- it described nothing about /proc//stat --- and the two call sites duplicated an awkward rsplit/nth chain around it. Replaced by d12_stat_field_after_comm(pid, n), which says what it reads and documents the field numbering it anchors: comm is parenthesised and may contain spaces and parentheses, so the only safe anchor is the last ")", after which 0=state, 1=ppid, 2=pgrp, 3=session. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_016bqGA6s9tTUFzYpbeW3tai --- tests/gpu_invocation_acceptance.rs | 38 +++++++++++++----------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/tests/gpu_invocation_acceptance.rs b/tests/gpu_invocation_acceptance.rs index d8d5b4e..a8f226b 100644 --- a/tests/gpu_invocation_acceptance.rs +++ b/tests/gpu_invocation_acceptance.rs @@ -222,22 +222,8 @@ mod crdt { " pid {pid} ppid={} pgid={} sid={} state={} threads={}\n\ \x20 SigIgn={} SigCgt={} SigPnd={} ShdPnd={}\n", field("PPid:"), - fs::read_to_string(format!("/proc/{pid}/stat")) - .ok() - .and_then(|st| st.rsplit_once(african_close()).map(|(_, rest)| rest - .split_whitespace() - .nth(2) - .unwrap_or("?") - .to_owned())) - .unwrap_or_else(|| "?".to_owned()), - fs::read_to_string(format!("/proc/{pid}/stat")) - .ok() - .and_then(|st| st.rsplit_once(african_close()).map(|(_, rest)| rest - .split_whitespace() - .nth(3) - .unwrap_or("?") - .to_owned())) - .unwrap_or_else(|| "?".to_owned()), + d12_stat_field_after_comm(pid, 2), + d12_stat_field_after_comm(pid, 3), field("State:"), field("Threads:"), field("SigIgn:"), @@ -283,11 +269,21 @@ mod crdt { out } - /// The `)` that closes comm in `/proc//stat`; comm may itself - /// contain spaces or parentheses, so the fields after it are only - /// safe to index from the LAST `)`. - fn african_close() -> &'static str { - ")" + /// Field `n` of `/proc//stat`, counted from the first field + /// AFTER `comm`. + /// + /// `comm` is parenthesised and may itself contain spaces and + /// parentheses, so the only safe anchor is the **last** `)`. + /// Counting from there: 0 = state, 1 = ppid, 2 = **pgrp**, + /// 3 = **session**. + fn d12_stat_field_after_comm(pid: u32, n: usize) -> String { + fs::read_to_string(format!("/proc/{pid}/stat")) + .ok() + .and_then(|st| { + st.rsplit_once(')') + .and_then(|(_, rest)| rest.split_whitespace().nth(n).map(ToOwned::to_owned)) + }) + .unwrap_or_else(|| "?".to_owned()) } fn wait_for_exit(child: &mut Child, timeout: Duration) -> std::process::ExitStatus {