Skip to content

Fix VehicleClock::Now() mixing clocks (#310) - #329

Open
mattday-etas wants to merge 7 commits into
eclipse-score:mainfrom
etas-contrib:fix-mixed-clocks-for-vehicle-time
Open

mattday-etas wants to merge 7 commits into
eclipse-score:mainfrom
etas-contrib:fix-mixed-clocks-for-vehicle-time

Conversation

@mattday-etas

Copy link
Copy Markdown
Contributor

Summary

Fixes #310. VehicleClock::Now() computes ptp_at_capture + (now_local - local_at_capture).
local_at_capture comes from the time slave, which stamps it with CLOCK_MONOTONIC.
now_local came from HighResSteadyClock, which was on a different timeline:

  • Linux: std::chrono::high_resolution_clock, which is CLOCK_REALTIME. Vehicle time came out about grandmaster time plus wall-clock time.
  • QNX: ClockCycles(), a different epoch. This gave about a 0.4 s offset under QEMU.

The now_local < local_at_capture guard never fired, so the error was silent.

Changes

  • HighResSteadyClockBackendImpl now reads clock_gettime(CLOCK_MONOTONIC).
  • The Linux and QNX backends became identical and are now merged. Deleted details/qtime/ (HighResSteadyQClock, its tests and BUILD targets). high_res_steady_clock_impl now depends on system_clock on all platforms, with no OS select().
  • Removed the QNX tick_provider* files and the cycles-to-nanoseconds conversion, which are no longer used. This also drops the Neutrino dependency.
  • Removed the qtime entry from the docs_bundle code targets in score/time/BUILD.
  • Updated the detailed design doc. The other docs already describe a single CLOCK_MONOTONIC backend.
  • No other code consumes the slave's local_time.

Tests

  • New regression test: Now() must lie between two direct CLOCK_MONOTONIC reads and be far from CLOCK_REALTIME.
  • Moved the CreateBackend<HighResSteadyTime>() factory test into high_res_steady_clock_backend_impl_test. It keeps the comp_req__time__supported_platforms property.
  • Removed the obsolete cycles-conversion, tick-provider and qclock tests.
  • bazel test //score/time/high_res_steady_time/... passes on host.

Notes

  • With QNX SDP 8.0, no kernel call is required with CLOCK_MONOTONIC. However, this could differ with older versions of QNX, or with non-standard versions of libc.so.6.
  • Per the issue, the safety manager and integrators should be notified through the release notes.

HighResSteadyClock used CLOCK_REALTIME (Linux) and ClockCycles() (QNX),
while the time slave stamps local_time with CLOCK_MONOTONIC, so
VehicleClock::Now() subtracted values from different epochs. Use
CLOCK_MONOTONIC in both HighResSteadyClock backends, drop the unused
QNX tick provider, and add regression tests.
Both backends now read clock_gettime(CLOCK_MONOTONIC) with identical
code, so the separate qtime/qclock implementation is redundant. Remove
it and build the system_clock backend on all platforms.

- Delete score/time/high_res_steady_time/src/details/qtime/
- Drop the OS select() in high_res_steady_clock_impl
- Remove the qtime entry from the docs_bundle code targets
- Move the CreateBackend factory test into the system_clock backend
  test so it keeps covering comp_req__time__supported_platforms
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

License Check Results

🚀 The license check job ran with the Bazel command:

bazel run //:license-check

Status: ⚠️ Needs Review

Click to expand output
[License Check Output]
Extracting Bazel installation...
Starting local Bazel server (8.8.2) and connecting to it...
INFO: Invocation ID: bfe56f89-1e3e-4136-9d50-e647f33dc7ce
Computing main repo mapping: 
Computing main repo mapping: 
WARNING: For repository 'nlohmann_json', the root module requires module version nlohmann_json@3.11.3, but got nlohmann_json@3.12.0.bcr.1 in the resolved dependency graph. Please update the version in your MODULE.bazel or set --check_direct_dependencies=off
Computing main repo mapping: 
Computing main repo mapping: 
Computing main repo mapping: 
Loading: 
Loading: 2 packages loaded
Loading: 2 packages loaded
    currently loading: 
Loading: 2 packages loaded
    currently loading: 
Loading: 2 packages loaded
    currently loading: 
Loading: 2 packages loaded
    currently loading: 
Loading: 2 packages loaded
    currently loading: 
Loading: 2 packages loaded
    currently loading: 
WARNING: Target pattern parsing failed.
ERROR: Skipping '//:license-check': no such target '//:license-check': target 'license-check' not declared in package '' defined by /home/runner/work/time/time/BUILD
ERROR: no such target '//:license-check': target 'license-check' not declared in package '' defined by /home/runner/work/time/time/BUILD
INFO: Elapsed time: 15.021s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Quality pack traceability report for this pull request:

Status: PASS (workflow run)

Traceability gate input: /home/runner/work/time/time/_build/metrics.json
------------------------------------------------------------------------
[comp_req]
  Requirements with source links: 40/58 (68.97%)
  Requirements with test links:   5/58 (8.62%)
  Requirements fully linked:      5/58 (8.62%)
  Tests linked to requirements:   9/492 (1.83%)
  Broken test references:         0
------------------------------------------------------------------------
Threshold check passed.

@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview for this pull request is available at:
pr-329: https://eclipse-score.github.io/time/pr-329/

@mattday-etas
mattday-etas marked this pull request as ready for review November 13, 2026 17:05

@jorgecasal jorgecasal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Apart from the suggested changes, looks good to me. Happy to accept it as is.

Comment on lines +33 to +39
``HighResSteadyClock`` reads POSIX ``CLOCK_MONOTONIC`` on all platforms — the same clock the
time slave uses to stamp Sync frames, so its readings can be combined with ``VehicleTime``
reference stamps. The read normally involves no kernel call: on QNX SDP 8.0, libc computes
``CLOCK_MONOTONIC`` in userspace from the hardware cycle counter and the syspage ``qtime`` data;
on Linux it is served by the vDSO when the active clocksource supports it (e.g. TSC,
``arch_sys_counter``). Resolution is that of the hardware counter. It uses the same ``Now()``
snapshot pattern as all clock domains, with no status check required.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I've drafted a simpler version that keeps the key technical facts but makes them a bit easier to digest (assuming that this document will have a broader audience):

Suggested change
``HighResSteadyClock`` reads POSIX ``CLOCK_MONOTONIC`` on all platforms — the same clock the
time slave uses to stamp Sync frames, so its readings can be combined with ``VehicleTime``
reference stamps. The read normally involves no kernel call: on QNX SDP 8.0, libc computes
``CLOCK_MONOTONIC`` in userspace from the hardware cycle counter and the syspage ``qtime`` data;
on Linux it is served by the vDSO when the active clocksource supports it (e.g. TSC,
``arch_sys_counter``). Resolution is that of the hardware counter. It uses the same ``Now()``
snapshot pattern as all clock domains, with no status check required.
``HighResSteadyClock`` uses a standard system clock (``CLOCK_MONOTONIC``) that measures elapsed time since the system started up. Because this is the exact same clock used to timestamp Sync frames, you can seamlessly combine its readings with ``VehicleTime`` timestamps.
To keep things fast, checking this clock doesn't require a slow request to the operating system's kernel. Instead, on both QNX and Linux, the system reads the CPU's built-in hardware counter directly. This gives the most precise timing possible with zero lag or extra system overhead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that reads much better and seems more appropriate for the context of this documentation.

To be safe, I think I'd just qualify the second paragraph with ".. on both QNX SDP 8.0 and Linux, ...", because I only checked this applies to the current QNX version, and I suspect the reason for the original implementation using ClockCycles() was because earlier QNX may have involved the kernel for CLOCK_MONOTONIC. This is just a guess, as I don't have any knowledge about the original implementation.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd not use QNX SDP 8.0, instead write QNX OS 8.0 or simply QNX 8.0.

Co-authored-by: Jorge C. Santos <156771132+jorgecasal@users.noreply.github.com>
Signed-off-by: Matt Day <Matt.Day@etas.com>
low-overhead timing. On QNX the backend reads the hardware cycle counter directly via
``ClockCycles()`` — no kernel call, no scheduler interaction. On Linux it delegates to
``std::chrono::high_resolution_clock``.
low-overhead timing. On all platforms the backend reads POSIX ``CLOCK_MONOTONIC``, the

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.

Why did you drop the ClockCycles impelemnetion?
clock monotonic will not be necessary be mapped there on the hw
even if it is, clock cycles provides better precisios and less performance impat.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. If it wasn't clear from the subsequent lines of index.rst, the PR description, and #310, I'll try to explain why the ClockCycles() backend was removed, because I think it's the right call here.

It was the cause of the bug. VehicleClock::Now() computes ptp_at_capture + (now_local - local_at_capture). The time slave stamps local_at_capture with CLOCK_MONOTONIC, and ClockCycles() is on a different epoch. The subtraction therefore gave a meaningless result. Under QEMU on QNX that was a vehicle time offset of about 0.4 s, and nothing flagged it. Precision and overhead don't help if the two operands aren't on the same timeline.

On your two points:

  • "clock monotonic will not be necessary be mapped there on the hw" I don't think that matters here. The only requirement is that both sides of the subtraction use the same clock, and the slave already uses CLOCK_MONOTONIC.
  • "clock cycles provides better precisios and less performance impat." On QNX SDP 8.0, CLOCK_MONOTONIC needs no kernel call and is nanosecond-resolution. If you have measurements from your target that show a difference, I'd be glad to look at them. Without them, I don't see a concrete benefit.

The change is also a simplification. The Linux and QNX backends were doing the same job with different clocks, and they are now a single implementation. The PR removes details/qtime/, the tick provider, the cycles-to-nanoseconds conversion, their mocks and tests, the OS select(), and the Neutrino dependency. That leaves significantly less code to maintain and less platform-specific behaviour to verify.

Keeping ClockCycles() would mean one of two things. Either the slave also stamps with ClockCycles(), which is a larger cross-cutting change, or we keep a calibrated offset between the two clocks, which adds the complexity this PR removes. I can't see a demonstrated benefit for either. If you think one is worthwhile, I'd suggest opening a separate issue with supporting measurements, so we can discuss it properly without holding up the fix for #310.

Could you let me know whether this addresses your concern, or point me to a specific problem with CLOCK_MONOTONIC on a supported target? I'd also really appreciate your thoughts on the rest of the PR (the regression test, the merged backends, the docs), since this is the only inline comment so far.

Thanks again for your time.

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.

I don#t agree to drop the ClockCycles support jsut becuase thre is a bug

  1. TimeSlav shall timestamp it with the same time base, which we are trying to compare later - definitely. it might be clock_monotonic or clock cycles. there should not be themix, I agree. and main issue is here - they are mixed up, as far as I see. but then we need to adjust the root cause
  2. "I don't think that matters here." it matters, because of if you will drop the CC support, you will not get a srapped ccess to it. everyhere will be monotonic. but the thing is, the timeSlave could be different on project side. then, what you will do?
  3. clock_monotonic you can alsredy wrap with steady_clock wrapper if you like
  4. clock_monotonic is mapped to clock cylcles, but it''s missing the boot time, which might be neeed sometimes

we can discuss mayeb a bit differnt design in the meeting, if you like


auto backend = detail::CreateBackend<HighResSteadyTime>();
ASSERT_NE(backend, nullptr);
EXPECT_NE(dynamic_cast<HighResSteadyClockBackendImpl*>(backend.get()), nullptr);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using typeid would make the intent clearer in my opinion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, updated in 6f590a1

This branch is waiting to be deployed

1 waiting deployment
workflow-approval — 34e6bfbd Waiting Nov 26, 2026 by mattday-etas via qnx-build (x86_64-qnx) / approval #1433
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

VehicleClock::Now() mixes clocks

4 participants