Skip to content

File times keep nanoseconds; Process.clock_gettime refuses unknown clocks - #994

Open
tannevaled wants to merge 3 commits into
mainfrom
stat-time-precision
Open

tannevaled wants to merge 3 commits into
mainfrom
stat-time-precision

Conversation

@tannevaled

@tannevaled tannevaled commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #705.

What was wrong (measured on main against MRI 4.0.10, arm64-darwin)

Probe MRI 4.0.10 rbgo before
File.stat(f).mtime.nsec sub-second value 0 (int64-seconds carrier)
File.atime(f) / File.ctime(f) st_atime / st_ctime the mtime
File.birthtime(f) on darwin a Time NotImplementedError
File#mtime / #atime / #ctime / #birthtime exist, arity 0 NoMethodError
File.method(:mtime).arity 1 -1
File.utime(Time.at(1, 5, :nsec), …) then .nsec 5 0
Process::CLOCK_PROCESS_CPUTIME_ID & 3 more 12, 16, 4, 8 NameError
Process.clock_gettime(999) Errno::EINVAL … clock_gettime(999) a wall-clock Float
Process.clock_gettime(:GETTIMEOFDAY_BASED_CLOCK_REALTIME) Float TypeError
Process.clock_getres(Process::CLOCK_MONOTONIC_RAW, :nanosecond) 42 1000
Process.clock_gettime(0, :foo) ArgumentError: unexpected unit: foo a Float
Process.clock_gettime(2**40) RangeError … to 'unsigned int' (EINVAL after the first fix)
File.utime(nil, 5, f) TypeError: can't convert NilClass into time succeeds (a lone nil read as "now")
File.utime("x", nil, f) TypeError: can't convert String into time no implicit conversion of "x" into Time
File.utime(1) ArgumentError: wrong number of arguments (given 1, expected 2+) ArgumentError: utime: runtime error: index out of range [1]

The third row of #705 (clock_gettime(999)) is the one the issue ranks first: an instrument that answers a plausible number instead of failing.

The change

  • filestat.go: the timestamps are time.Time with Nsec, rendered in the local zone (stat_time → rb_time_nano_new). One timeOf serves File::Stat#…, File.… and the new File#… (IOError "closed stream" when closed).
  • file.go: File.mtime/atime/ctime/birthtime read the stat (rb_file_s_<name> in ENOENT, arity 1). File.utime/lutime take a time.Time through the seam on every platform (unix, plan9, windows, wasm).
  • process_clock.go: rb_clock_gettime / rb_clock_getres / make_clock_result / clock_failed, read from process.c at v4.0.10. The platform half is in process_clock_{darwin,linux,other}.go: constants from golang.org/x/sys/unix in InitVM_process order, NUM2CLOCKID's C type (darwin unsigned int, measured; linux int), and darwin's clock_getres table (x/sys does not wrap it there, so it was measured on MRI). CLOCK_REALTIME and CLOCK_MONOTONIC still read through the processMonoNow seam the tests and Benchmark rely on.
  • math.go: floatToLong (rb_num2long's Float stage) is extracted from mathFloatToInt and shared.
  • File.utime / lutime follow utime_args_init: only a nil pair means "now". Otherwise both go through rb_time_timespec, atime first, and time_timespec's message names the class. numFloat, whose Integer case had become dead, is folded into utimeArg.

The first CI run failed on the ubuntu coverage ratchet: numFloat (that dead case) and timeOf (the recorded-birthtime branch, which a Linux stat never takes). A stub stat with a creation time now drives that branch on every host.

Measured

  • Five probes (File times, constants, symbolic clocks, utime nanoseconds, conversion edges) agree with MRI 4.0.10 byte for byte through rubydiff.
  • gotest -run 'Process|File|Time|Stat|Clock|Utime|Lutime|Bench' ./internal/vm/: 313 tests pass.
  • Cross-builds: linux, linux/386, windows, freebsd, plan9, wasip1, darwin/amd64.
  • mutsweep -sites all over process_clock.go and process_clock_darwin.go: 18 mutations, 18 caught. The first sweep left 2 survivors, the float-range and darwin id-range guards. They were rewritten to MRI's measured NUM2UINT/NUM2INT messages and are now caught.

Tests changed, and why

  • TestFileLevelPredicates asserted File.birthtime(existing) raises NotImplementedError. On darwin MRI 4.0.10 answers a Time. It now asserts that File.birthtime agrees with File::Stat#birthtime on the host.
  • TestProcessStdlib asserted CLOCK_MONOTONIC == 6, the darwin value. On Linux the host's id is 1.
  • TestProcessResiduals asserted 1000 ns for the numeric clocks. Linux's clock_getres says 1 ns, so the portable bound is asserted instead. The Float#inspect witness moved to a symbolic clock.

Not done here

  • The BSDs, Solaris and Windows keep the two clocks rbgo had (now with EINVAL for any other id). Their clockid_t tables differ (FreeBSD: MONOTONIC 4, PROCESS_CPUTIME_ID 15), and there is no MRI on those hosts to measure against.
  • On Linux, MRI's File.birthtime uses statx. rbgo reads syscall.Stat_t, which has no birth time, so it still raises NotImplementedError there. That is a separate defect.

🤖 Generated with Claude Code

tannevaled and others added 3 commits November 30, 2026 20:59
File::Stat, File.mtime/atime/ctime/birthtime and the new File#mtime &
co. carry the stat struct's nanoseconds in the local zone, as MRI's
stat_time does; File.atime and File.ctime read st_atime/st_ctime instead
of answering the modification time; File.birthtime works where the
stat struct has a creation time; each singleton names its own C function
in ENOENT and declares arity 1. File.utime and File.lutime write the
nanoseconds too: their carrier was int64 seconds.

Process.clock_gettime and clock_getres follow process.c v4.0.10: the
platform's CLOCK_* constants with the host's clockid_t values (darwin and
linux), the real clock_gettime(2) for every id, Errno::EINVAL naming the
id for one the host does not have (it used to answer the wall clock),
MRI's symbolic emulations with their resolutions, NUM2UINT/NUM2INT range
errors, and "unexpected unit". floatToLong is shared with Math.ldexp.

Closes #705

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The ubuntu coverage ratchet failed on two functions. Fixing them showed
three divergences from MRI 4.0.10, measured with rubydiff:

- file.c utime_args_init leaves the timespec NULL ("now") only when BOTH
  times are nil; otherwise each goes through rb_time_timespec, atime
  first. rbgo read each nil as "now" on its own, so File.utime(nil, 5, f)
  succeeded where MRI raises "can't convert NilClass into time".
- time.c time_timespec names the argument's class: "can't convert String
  into time". rbgo said "no implicit conversion of \"x\" into Time".
- File.utime with fewer than two arguments reported a Go index panic
  ("utime: runtime error: index out of range") instead of
  "wrong number of arguments (given 1, expected 2+)".

numFloat had a single caller, which already handled Integer first, so
its Integer case was dead (85.7% on linux). It is folded into utimeArg.
timeOf's recorded-birthtime branch runs only where the stat struct has
a creation time, never on linux; a stub stat now drives it everywhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The windows test lane failed TestProcessClockSymbolic with a nil
pointer dereference: Process.clock_gettime(:CLOCK_BASED_CLOCK_PROCESS_
CPUTIME_ID), the one CPU-time emulation process.c compiles with no
#ifdef guard, reads procRusage, and spawn_windows.go and spawn_wasm.go
declared that seam without ever assigning it.

Windows now reads GetProcessTimes, where MRI's win32 clock() and
Process.times read the process's CPU time; a FILETIME duration counts
100 ns ticks. wasm has no getrusage and reports zero, as Plan 9 does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant