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/<pid>/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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016bqGA6s9tTUFzYpbeW3tai
This commit is contained in:
Levi Neuwirth 2026-08-19 14:31:53 +02:00
parent 952f8d5bc2
commit 38f2af41f7
No known key found for this signature in database
1 changed files with 17 additions and 21 deletions

View File

@ -222,22 +222,8 @@ mod crdt {
" pid {pid} ppid={} pgid={} sid={} state={} threads={}\n\ " pid {pid} ppid={} pgid={} sid={} state={} threads={}\n\
\x20 SigIgn={} SigCgt={} SigPnd={} ShdPnd={}\n", \x20 SigIgn={} SigCgt={} SigPnd={} ShdPnd={}\n",
field("PPid:"), field("PPid:"),
fs::read_to_string(format!("/proc/{pid}/stat")) d12_stat_field_after_comm(pid, 2),
.ok() d12_stat_field_after_comm(pid, 3),
.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()),
field("State:"), field("State:"),
field("Threads:"), field("Threads:"),
field("SigIgn:"), field("SigIgn:"),
@ -283,11 +269,21 @@ mod crdt {
out out
} }
/// The `)` that closes comm in `/proc/<pid>/stat`; comm may itself /// Field `n` of `/proc/<pid>/stat`, counted from the first field
/// contain spaces or parentheses, so the fields after it are only /// AFTER `comm`.
/// safe to index from the LAST `)`. ///
fn african_close() -> &'static str { /// `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 { fn wait_for_exit(child: &mut Child, timeout: Duration) -> std::process::ExitStatus {