Skip to content

fix: read mclock64() in os.clock(), not clock64() - #9

Merged
mgrossmann merged 1 commit into
mainfrom
fix/os-clock-mclock64
Aug 7, 2026
Merged

fix: read mclock64() in os.clock(), not clock64()#9
mgrossmann merged 1 commit into
mainfrom
fix/os-clock-mclock64

Conversation

@mgrossmann

Copy link
Copy Markdown
Contributor

Fixes #8.

Why

libc370 #49 landed as mvslovers/libc370@f6d1888, changing clock64() to seconds
resolution — the behaviour time64.h:45 had documented all along.

os_clock() divides its source by CLOCKS_PER_SEC (1000) to split it into whole seconds
plus a millisecond remainder, so it needs a millisecond source. Reading clock64()
there now produces a value 1000x too small. Modelling the exact arithmetic against a real
STCK-derived timestamp:

true seconds since epoch : 1777488851.576
mclock64() -> os.clock() : 1777488851.576   <- this PR
clock64()  -> os.clock() : 1777488.851      <- current main
ratio (unfixed / true)   : 0.001000

The build does not catch it. clock64_t and mclock64_t are both plain uint64_t, so
the resolution mismatch is silent — os.clock() keeps compiling, linking and returning a
plausible-looking number.

lua370 is exposed automatically: mbt's reusable workflows pin cc370 via
inputs.cc370_ref but clone libc370 at main HEAD with no ref, and the toolchain cache
key embeds that SHA.

What changed

One line, plus the comment above it:

-	c.u64 = clock64();
+	c.u64 = mclock64();

mclock64() stayed at milliseconds through #49. clock64() had exactly one production
use in this repo, so the rest of os_clock() is untouched.

The comment was rewritten to record why this is deliberately mclock64(), and its stale
crent370 / crent470 paths dropped in favour of naming the functions — the original
note about libc370's clock() stub being the underlying reason is kept.

Verification

  • Object level: loslib.o's external reference goes TM64CLCKTM64MCLK (decoded
    from the EBCDIC object); confirmed against a rebuild of the unmodified file.
  • Clean rebuild against the post-#49 sysroot: make, make lib, make test all rc=0
    under -Wall -Werror.
  • Sysroot confirmed to carry post-#49 libc370 (the rebuilt tm64clck.o is byte-identical
    to the copy inside the installed libc.a).

Not affected

os.date, os.time, os.difftime use time64() / gmtime64_r() / localtime64_r() /
mktime64() / difftime64(). #49 moved time64() in the same commit but deliberately
kept its value unchanged — it used to divide clock64()'s milliseconds by
CLOCKS_PER_SEC and now returns clock64()'s seconds directly. Two cancelling constants
were removed together, so no change is needed here.

Left open (see #8 for detail)

Neither is closed by this PR, and neither is in this diff:

  • os_clock() still divides a millisecond source by CLOCKS_PER_SEC, correct only while
    that macro is 1000. #49 removed libc370's last reader of it, so this is now effectively
    the only place using it as a wall-clock scaler.
  • os.clock() returns elapsed wall-clock time rather than CPU time as Lua specifies. The
    root cause is libc370's clock() stub returning -1; once that is implemented for real
    (TCB / TIMEUSED), os_clock() should move to the existing #else branch.

Ref: mvslovers/libc370#49

libc370 #49 (mvslovers/libc370@f6d1888) changed clock64() to seconds
resolution, matching what time64.h had documented all along. os_clock()
divides its source by CLOCKS_PER_SEC (1000) to split it into whole
seconds plus a millisecond remainder, so it needs a millisecond source.
Reading clock64() there now yields a value 1000x too small, and so is
every elapsed time derived from it.

mclock64() stayed at milliseconds through #49, so switching the single
call site restores the previous values. The rest of os_clock() is
unchanged; clock64() had exactly one production use in this repo.

Nothing catches this at build time: clock64_t and mclock64_t are both
plain uint64_t, so the resolution mismatch is silent and the module
still compiles, links and runs.

os.date, os.time and os.difftime are unaffected. #49 moved time64() in
the same commit but deliberately kept its value unchanged -- it used to
divide clock64()'s milliseconds by CLOCKS_PER_SEC and now returns
clock64()'s seconds directly.

Refs #8
@mgrossmann
mgrossmann force-pushed the fix/os-clock-mclock64 branch from e24bffc to 2758cce Compare August 7, 2026 17:16
@mgrossmann
mgrossmann merged commit 7d1afe7 into main Aug 7, 2026
1 check passed
@mgrossmann
mgrossmann deleted the fix/os-clock-mclock64 branch August 7, 2026 17:22
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.

os.clock() returns a value 1000x too small since libc370 #49 landed

1 participant