proc: real CPU time, a tty number, and an exe that is a symlink

Four Gate 0 rows, and a fifth found while checking one of them.

**getrusage() did not distinguish RUSAGE_CHILDREN from RUSAGE_SELF**, which
undersells it: it ignored `who` entirely and reported the machine's *uptime*
as the caller's user time. Every process of every age answered with the same
number. Per-process CPU time is sampled at the timer tick now and split on the
interrupted privilege level; children's time is rolled up in wait() rather than
exit(), because RUSAGE_CHILDREN means *reaped* children and a running one has
not contributed yet.

**And wait4()'s fourth argument is a `struct rusage *` that was read off the
stack and thrown away.** That is the one shells actually use -- wait3() is the
same syscall with pid -1 -- so zsh's `time` reported

    16777216.01s user 1073.78s system 927983249% cpu

for every command. 2^24 seconds is uninitialised memory, and it did not vary
with the workload because it was never written. Now:

    busy loop   2.66s user  97% cpu   2.720 total
    sleep 2     0.02s user   0% cpu   2.248 total
    ls -lR /    0.70s user  65% cpu   1.070 total

**ru_stime is a structural zero and says so.** MSR_SFMASK includes FL_IF, so
SYSCALL clears interrupts and no tick can land while a process is inside a
syscall. Tick sampling cannot see kernel time at all here. Charging it needs
TSC deltas closed and reopened around every sched(), which is scheduler
accounting rather than sampling; the limitation is written down in
docs/architecture/kernel.md rather than papered over.

**/proc/<pid>/exe is a symlink.** It also needed a real st_size, and every
other procfs file deliberately reports zero -- readlink(2) bounds its copy by
it, so a symlink of size 0 resolves to the empty string, which looks like it
worked and is worse than the regular file was. Generating that one path in
ilock() costs one small render per stat() of one file.

**OVMF memory reporting is closed**: 1 GB reports 1017 MB usable, 2 GB reports
2041 MB. The firmware keeps about 7 MB either way, which is the right shape.

**And `ps` printed a header and nothing else**, which is not in any list
because nobody had run it. `ps` with no arguments shows only processes on
*this* terminal; /proc/<pid>/stat hardcoded tty_nr to 0, so nothing could ever
match. `ps -e` always worked, which is exactly what hid it. p->ctty was already
tracked -- procfs simply was not asked. Fields 14..17 were hardcoded zero too,
so TIME read 00:00:00 however long a process had run; they carry the new
counters now, and a shell that has burned 653 ticks reports 00:00:06.

Verified: utime climbs 18 -> 653 across a loop in the shell itself, `ps` lists
its own terminal, `ps -e` still lists everything, `readlink /proc/1/exe` gives
/bin/schinit, and sigtest/$()/rename/dmesg are all still green.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-09-22 02:45:00 +02:00
commit a5ff83de46
8 changed files with 199 additions and 26 deletions

View file

@ -52,6 +52,9 @@ sys_kill(void)
return sigsend(pid, sig) < 0 ? -ESRCH : 0;
}
// Defined with getrusage() further down; wait4() needs it too.
static void rusage_fill(struct rusage *ru, uint64 ut, uint64 st);
addr_t sys_getpid(void) { return myproc()->pid; }
addr_t sys_getppid(void) { return myproc()->parent ? myproc()->parent->pid : 1; }
addr_t sys_gettid(void) { return myproc()->pid; } // single-threaded: tid == pid
@ -67,26 +70,45 @@ sys_waitpid(void)
if (argint(0, &pid) < 0 || argaddr(1, &statusp) < 0 || argint(2, &options) < 0)
return -1;
int status;
int wpid = waitpid(pid, &status, options);
int wpid = waitpid(pid, &status, options, 0, 0);
if (wpid > 0 && statusp)
put_user(status, statusp);
return wpid;
}
// wait4(2) -- and its fourth argument is a `struct rusage *`.
//
// It was read off the stack and thrown away. So a caller that passed a buffer
// got it back untouched, and read whatever had been there before: zsh's
// `time` reported 16777216.01s user for every command, which is 2^24 seconds,
// which is uninitialised memory that happened to look like a number. The
// figure did not vary with the workload because it was never written.
//
// wait3() is the same syscall with pid -1, and it is the one shells actually
// call, so this is the path every `time` goes through.
addr_t
sys_wait4(void)
{
int pid, options;
addr_t statusp;
addr_t statusp, rup;
uint64 ut = 0, st = 0;
argint(0, &pid);
argaddr(1, &statusp);
argint(2, &options);
argaddr(3, &rup);
int status;
int wpid = waitpid(pid, &status, options);
int wpid = waitpid(pid, &status, options, &ut, &st);
if (wpid <= 0)
return wpid;
if (statusp)
put_user(status, statusp);
if (rup) {
struct rusage *ru = (struct rusage*)user_ptr(rup, sizeof(*ru));
if (ru)
rusage_fill(ru, ut, st);
}
return wpid;
}
@ -385,27 +407,65 @@ addr_t sys_pause(void)
return -EINTR;
}
// getrusage(2).
//
// This used to ignore `who` entirely and report the machine's *uptime* as the
// caller's user time. Every process of every age answered with the same
// number, and RUSAGE_CHILDREN answered with it too -- so a shell timing a
// command got the age of the machine, and `time` on a pipeline was
// meaningless rather than merely imprecise.
//
// Ticks, converted at the boundary. The kernel counts whole timer ticks
// because that is what the sampling gives it; microseconds are what the
// interface asks for, so the multiply happens here rather than pretending to a
// resolution the counter does not have.
static void
rusage_fill(struct rusage *ru, uint64 ut, uint64 st)
{
memset(ru, 0, sizeof(*ru));
ru->ru_utime.tv_sec = (long)(ut / HZ);
ru->ru_utime.tv_usec = (long)((ut % HZ) * (1000000 / HZ));
ru->ru_stime.tv_sec = (long)(st / HZ);
ru->ru_stime.tv_usec = (long)((st % HZ) * (1000000 / HZ));
}
addr_t sys_getrusage(void)
{
int who;
struct rusage *ru;
argint(0, &who);
if (argptr(1, (char**)&ru, sizeof(*ru)) < 0) return -1;
memset(ru, 0, sizeof(*ru));
uint64 rsec, rusec;
monotonic(&rsec, &rusec);
ru->ru_utime.tv_sec = (long)rsec;
ru->ru_utime.tv_usec = (long)rusec;
return 0;
if (argptr(1, (char**)&ru, sizeof(*ru)) < 0) return -EFAULT;
switch (who) {
case RUSAGE_SELF:
rusage_fill(ru, myproc()->utime, myproc()->stime);
return 0;
case RUSAGE_CHILDREN:
rusage_fill(ru, myproc()->cutime, myproc()->cstime);
return 0;
default:
// RUSAGE_THREAD is -1 and would be RUSAGE_SELF here, since a process
// has exactly one thread. It is refused rather than aliased: answering
// it would imply threads exist.
return -EINVAL;
}
}
addr_t sys_times(void)
{
addr_t bufaddr;
argaddr(0, &bufaddr);
// The tms fields are in clock ticks, which is what the kernel counts, so
// this one needs no conversion -- USER_HZ and HZ are both 100. They used
// to be memset to zero, which is why `times()` agreed with nothing.
if (bufaddr) {
struct tms *t = (struct tms*)user_ptr(bufaddr, sizeof(*t));
if (t) memset(t, 0, sizeof(*t));
if (t) {
t->tms_utime = (long)myproc()->utime;
t->tms_stime = (long)myproc()->stime;
t->tms_cutime = (long)myproc()->cutime;
t->tms_cstime = (long)myproc()->cstime;
}
}
// USER_HZ units, measured from the TSC.
//