Implement times(2) syscall#162
Merged
Merged
Conversation
657daea to
ac520fa
Compare
jserv
reviewed
Jul 7, 2026
jserv
requested changes
Jul 9, 2026
jserv
left a comment
Contributor
There was a problem hiding this comment.
Rebase latest main branch and resolve conflicts.
Guests calling times(2) got -ENOSYS: the syscall was absent from dispatch.tbl and abi.h had no SYS_times entry. Shell timing builtins and build tools that poll times() for elapsed/CPU tick accounting fall over on the error return. sys_times reports tms_utime/tms_stime from host getrusage(RUSAGE_SELF). tms_cutime/tms_cstime cannot come from RUSAGE_CHILDREN: the emulator also waits on helper subprocesses (rosettad translate, sysroot tooling), so that aggregate would masquerade helper CPU as guest child time. Instead the proc layer accumulates each reaped guest child's wait4 rusage -- at sys_wait4, sys_waitid, the table-pressure reaper, and the CLONE_VFORK wait -- and times() reads that sum. Fresh guest children start with zeroed counters for free because fork spawns a new emulator process. POSIX leaves autoreaped-children inclusion unspecified, so counting reaper-collected children is conformant. The return value is host CLOCK_MONOTONIC converted to USER_HZ (100) ticks: Linux returns jiffies-since-boot, and POSIX only requires an arbitrary-but-fixed reference point that successive calls can difference. Tick conversion uses the same 100Hz that core/stack.c advertises via AT_CLKTCK, so libc's sysconf(_SC_CLK_TCK) scaling stays consistent. A NULL buffer is accepted as on Linux, returning only the tick count. test-times covers the tick clock advancing with wall time, self and waited-for-child CPU accumulation against a measured burn, the NULL buffer, and EFAULT on a bad pointer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Guests calling times(2) got -ENOSYS: the syscall was absent from
dispatch.tbl and abi.h had no SYS_times entry. Shell timing builtins and
build tools that poll times() for elapsed/CPU tick accounting fall over
on the error return.
sys_times fills the four struct tms fields from host getrusage:
RUSAGE_SELF supplies tms_utime/tms_stime and RUSAGE_CHILDREN supplies
tms_cutime/tms_cstime. Guest fork() maps 1:1 to a host fork and
sys_wait4 reaps through host waitpid, so the host's children accounting
covers exactly the guest's waited-for children. The return value is host
CLOCK_MONOTONIC converted to USER_HZ (100) ticks: Linux returns
jiffies-since-boot, and POSIX only requires an arbitrary-but-fixed
reference point that successive calls can difference. Tick conversion
uses the same 100Hz that core/stack.c advertises via AT_CLKTCK, so
libc's sysconf(_SC_CLK_TCK) scaling stays consistent. A NULL buffer is
accepted as on Linux, returning only the tick count.
Testing
test-times(registered in manifest.txt): tick rate viasysconf(_SC_CLK_TCK), basic call, NULL buffer through the raw syscall,
return value advancing across a 60ms sleep, self utime+stime growth
against a measured 100ms CPU burn, cutime/cstime growth after reaping
a child that burns 100ms CPU, and EFAULT on a bad pointer — 7/7.
make checkgreen (driver 81 passed / 0 failed / 3 skipped;skips are pre-existing fixture-absence).
check-syscall-coveragepasses.Summary by cubic
Add the times(2) syscall so guests get correct CPU accounting and an elapsed tick count instead of -ENOSYS. Restores shell timing and tools that poll times(), and excludes emulator helper subprocess CPU from child totals.
SYS_timesto the ABI/dispatch; wiredsc_times.sys_times: selfutime/stimefromgetrusage(RUSAGE_SELF); childcutime/cstimefrom proc accumulators updated at each guest-child reap (sys_wait4,sys_waitid, table reaper,CLONE_VFORK). Credit only on terminal exit/signal; ignoreWUNTRACED,WCONTINUED, andWNOWAITpeeks; count only proc-table children, not helpers.CLOCK_MONOTONICin 100 Hz ticks (matchesAT_CLKTCK); accepts NULL;-EFAULTon bad pointer.test-times: verifies_SC_CLK_TCK==100, tick advance, self and child CPU accumulation (includingcstime), no credit onwaitid(WNOWAIT), NULL buffer, and bad-pointer-EFAULT.Written for commit 7cd2c0e. Summary will update on new commits.