Repository navigation
Fix VehicleClock::Now() mixing clocks (#310) - #329
mattday-etas wants to merge 7 commits into
Conversation
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
License Check Results🚀 The license check job ran with the Bazel command: bazel run //:license-checkStatus: Click to expand output |
|
Quality pack traceability report for this pull request: Status: PASS (workflow run) |
|
Documentation preview for this pull request is available at: |
jorgecasal
left a comment
There was a problem hiding this comment.
Apart from the suggested changes, looks good to me. Happy to accept it as is.
| ``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. |
There was a problem hiding this comment.
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):
| ``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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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_MONOTONICneeds 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.
There was a problem hiding this comment.
I don#t agree to drop the ClockCycles support jsut becuase thre is a bug
- 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
- "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?
- clock_monotonic you can alsredy wrap with steady_clock wrapper if you like
- 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); |
There was a problem hiding this comment.
Using typeid would make the intent clearer in my opinion.
Summary
Fixes #310.
VehicleClock::Now()computesptp_at_capture + (now_local - local_at_capture).local_at_capturecomes from the time slave, which stamps it withCLOCK_MONOTONIC.now_localcame fromHighResSteadyClock, which was on a different timeline:std::chrono::high_resolution_clock, which isCLOCK_REALTIME. Vehicle time came out about grandmaster time plus wall-clock time.ClockCycles(), a different epoch. This gave about a 0.4 s offset under QEMU.The
now_local < local_at_captureguard never fired, so the error was silent.Changes
HighResSteadyClockBackendImplnow readsclock_gettime(CLOCK_MONOTONIC).details/qtime/(HighResSteadyQClock, its tests and BUILD targets).high_res_steady_clock_implnow depends onsystem_clockon all platforms, with no OSselect().tick_provider*files and the cycles-to-nanoseconds conversion, which are no longer used. This also drops the Neutrino dependency.qtimeentry from thedocs_bundlecode targets inscore/time/BUILD.CLOCK_MONOTONICbackend.local_time.Tests
Now()must lie between two directCLOCK_MONOTONICreads and be far fromCLOCK_REALTIME.CreateBackend<HighResSteadyTime>()factory test intohigh_res_steady_clock_backend_impl_test. It keeps thecomp_req__time__supported_platformsproperty.bazel test //score/time/high_res_steady_time/...passes on host.Notes