Skip to content

Implement times(2) syscall#162

Merged
jserv merged 1 commit into
sysprog21:mainfrom
Max042004:impl-times
Jul 9, 2026
Merged

Implement times(2) syscall#162
jserv merged 1 commit into
sysprog21:mainfrom
Max042004:impl-times

Conversation

@Max042004

@Max042004 Max042004 commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

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

  • New test-times (registered in manifest.txt): tick rate via
    sysconf(_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 check green (driver 81 passed / 0 failed / 3 skipped;
    skips are pre-existing fixture-absence).
  • check-syscall-coverage passes.

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.

  • New Features
    • Added SYS_times to the ABI/dispatch; wired sc_times.
    • Implemented sys_times: self utime/stime from getrusage(RUSAGE_SELF); child cutime/cstime from proc accumulators updated at each guest-child reap (sys_wait4, sys_waitid, table reaper, CLONE_VFORK). Credit only on terminal exit/signal; ignore WUNTRACED, WCONTINUED, and WNOWAIT peeks; count only proc-table children, not helpers.
    • Returns CLOCK_MONOTONIC in 100 Hz ticks (matches AT_CLKTCK); accepts NULL; -EFAULT on bad pointer.
    • Added test-times: verifies _SC_CLK_TCK==100, tick advance, self and child CPU accumulation (including cstime), no credit on waitid(WNOWAIT), NULL buffer, and bad-pointer -EFAULT.

Written for commit 7cd2c0e. Summary will update on new commits.

Review in cubic

cubic-dev-ai[bot]

This comment was marked as resolved.

@Max042004 Max042004 force-pushed the impl-times branch 2 times, most recently from 657daea to ac520fa Compare July 7, 2026 09:22
Comment thread src/syscall/proc.c Outdated
Comment thread src/syscall/proc.c Outdated
Comment thread src/syscall/proc.c
Comment thread src/syscall/time.c
Comment thread src/syscall/time.c Outdated
Comment thread tests/test-times.c

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@jserv jserv merged commit 74ab093 into sysprog21:main Jul 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants