Skip to content

getrusage(RUSAGE_CHILDREN) counts host reaps, not guest waits #537

Open

Description

@Max042004

sys_getrusage (src/syscall/sys.c) forwards RUSAGE_CHILDREN to the host's
getrusage. The host adds a child's usage when the host process is reaped,
which is not when the guest reaps it, and not under the guest's SIGCHLD
disposition. Two results differ from Linux.

  1. An auto-reaped child is counted. Under SIG_IGN or SA_NOCLDWAIT, Linux
    never adds the child: exit_notify() gets autoreap from do_notify_parent()
    and calls release_task(), and the only writer of the parent's c* fields is
    wait_task_zombie() (kernel/exit.c). The elfuse host process does not
    ignore SIGCHLD itself, so XNU's reap_child_locked() rolls the child up into
    p_cru, which it skips only for a parent with P_NOCLDWAIT
    (bsd/kern/kern_exit.c).

  2. A waited child is counted late. wait4 can return the child's status from
    the lifecycle registry while its host process is still tearing down, and
    leave the host reap to proc_deferred_reap_poll(). A getrusage right after
    the wait misses the child; its usage shows up after a later wait4 entry or
    the SIGALRM tick runs the sweep.

times() is not affected in either case: it reads proc_children_cpu_us(),
which the guest-side reap paths fill.

Reproduction: each child spins on CLOCK_PROCESS_CPUTIME_ID for 300 ms. Each
row is the change since the previous row, in ms.

1. SIGCHLD SIG_IGN, fork, sleep 1 s, waitpid(-1, WNOHANG) (ECHILD), sleep 1 s
2. SIGCHLD SIG_DFL, fork a child that exits at once, waitpid
3. fork the 300 ms child, waitpid
4. sleep 1 s, waitpid(-1, WNOHANG) (ECHILD)
step Linux 7.0 arm64: ru utime / stime elfuse 281f0ce: ru utime / stime
1. auto-reaped +0 / +0 +221 / +89
2. idle child, waited +0 / +1 +0 / +0
3. 300 ms child, waited +111 / +189 +5 / +7
4. 1 s later +0 / +0 +232 / +76

On elfuse, step 1 should be 0, and the usage step 4 reports belongs to step
3. The same rows come out on 69f9c7f.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions