Skip to content

Stdlib: hrtime() — sub-microsecond nanoseconds always zero (/proc/uptime µs path) (ext/standard/hrtime.c) #12225

Closed

Description

@PurHur

Summary

hrtime() array form tv_nsec and hrtime(true) integer nanoseconds should retain sub-microsecond precision from clock_gettime(CLOCK_MONOTONIC) (php-src). This VM reads monotonic time via /proc/uptime in VmHrtimeNative (µs precision documented in-file), so the low three decimal digits of nanoseconds are always 000.

clock_gettime() shares the same VmHrtimeNative reader (#11624) — fix both when upgrading the clock source.

php-src reference: ext/standard/hrtime.c — clock_gettime(CLOCK_MONOTONIC).

PHP implementation target: ext/standard/VmHrtimeNative.php — prefer clock_gettime FFI / VmClockGettime path with ns fidelity; shrink µs-only /proc/uptime to bootstrap fallback only.

Repro (php-src-strict)

./script/docker-exec.sh -- bash -lc '
  php test/repro/maintainer_gap_hrtime_nsec_precision.php
  php bin/vm.php test/repro/maintainer_gap_hrtime_nsec_precision.php
'

Zend: hrtime()[1] % 1000 and hrtime(true) % 1000 are usually non-zero (e.g. 147, 757)
VM: both always 0

Done when

  • hrtime() array and hrtime(true) preserve ns precision matching php-src on Linux.
  • clock_gettime(CLOCK_MONOTONIC)['tv_nsec'] shares the same high-fidelity source.
  • Existing test/unit/VmHrtimeTest.php sub-ms assertion stays green.
  • Compliance .phpt; php-src-strict.

Activity

  1. added
    enhancementNew feature or request
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jun 26, 2026
  2. PurHur commented on Jun 26, 2026

    @PurHur
    OwnerAuthor

    claim: PHP Compiler STD Lib — batch (issues #12225, #12224, #12223)

  3. PurHur commented on Jun 26, 2026

    @PurHur
    OwnerAuthor

    Merged via #12228. hrtime() and clock_gettime(CLOCK_MONOTONIC) now preserve sub-microsecond nanoseconds via libc clock_gettime FFI.

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

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions