From 5e99343e8951e6511a7b9b21c0223bff35b35542 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Sat, 16 Feb 2019 22:48:44 -0800 Subject: top: make the task/thread count add up. Linux has more states than we were giving it credit for, which led to our numbers not adding up. Since the exact details seem to change between versions, and since having code specific to each kernel version is unattractive, go with the heuristic that there are relatively fewer "stopped" states (and they change less frequently), so all unknowns are "sleeping". --- toys/posix/ps.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 279a947a..c0e7e9e9 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -385,8 +385,10 @@ struct typography { {"STIME", "Start time (ISO 8601)", 5, SLOT_starttime}, {"F", "Flags 1=FORKNOEXEC 4=SUPERPRIV", 1, XX|SLOT_flags}, {"S", "Process state:\n" - "\t R (running) S (sleeping) D (device I/O) T (stopped) t (traced)\n" - "\t Z (zombie) X (deader) x (dead) K (wakekill) W (waking)", + "\t R (running) S (sleeping) D (device I/O) T (stopped) t (trace stop)\n" + "\t X (dead) Z (zombie) P (parked) I (idle)\n" + "\t Also between Linux 2.6.33 and 3.13:\n" + "\t x (dead) K (wakekill) W (waking)\n", -1, XX}, {"STAT", "Process state (S) plus:\n" "\t < high priority N low priority L locked memory\n" @@ -1553,13 +1555,18 @@ static void top_common( int j; // Count running, sleeping, stopped, zombie processes. + // The kernel has more states (and different sets in different + // versions), so we need to map them. (R)unning and (Z)ombie are + // easy enough, and since "stopped" is rare (just T and t as of + // Linux 4.20), we assume everything else is "sleeping". field.which = PS_S; memset(run, 0, sizeof(run)); for (i = 0; i