Skip to content

Commit ebaffcb

Browse files
authored
Merge pull request #2302 from microsoft/agents/issue-2301-stat-field-numbers
nvx: name the stat fields that the Linux process start-time reader takes
2 parents c391433 + 58ef44d commit ebaffcb

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

‎nvx/src/host/platform/linux/process.rs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ pub(crate) fn start_time_from_stat(
5858
}
5959
}
6060

61+
/// The number of the `state` field of a `/proc/<pid>/stat` line, as proc_pid_stat(5) numbers the
62+
/// fields from 1: the first after the PID and the command name.
63+
const STAT_STATE_FIELD: usize = 3;
64+
65+
/// The number of the `starttime` field of a `/proc/<pid>/stat` line in proc_pid_stat(5).
66+
const STAT_START_TIME_FIELD: usize = 22;
67+
6168
/// Returns the state and the start time in the `/proc/<pid>/stat` line of `pid`.
6269
fn parse_stat(pid: u32, stat: &[u8]) -> io::Result<(&str, u64)> {
6370
let malformed = || io::Error::other(format!("/proc/{pid}/stat has an unexpected format"));
@@ -71,12 +78,13 @@ fn parse_stat(pid: u32, stat: &[u8]) -> io::Result<(&str, u64)> {
7178
.map_err(|_| malformed())?
7279
.split_whitespace()
7380
.collect();
74-
// Field 22 of the stat line; the fields after the command name start at field 3.
81+
// `fields` starts at the state field.
7582
let start_time = fields
76-
.get(19)
83+
.get(STAT_START_TIME_FIELD - STAT_STATE_FIELD)
7784
.and_then(|value| value.parse().ok())
7885
.ok_or_else(malformed)?;
79-
Ok((fields[0], start_time))
86+
let state = fields.first().copied().ok_or_else(malformed)?;
87+
Ok((state, start_time))
8088
}
8189

8290
/// Returns whether a process has threads besides its main thread.

0 commit comments

Comments
 (0)