casaroli opened a new pull request, #21966:
URL: https://github.com/apache/nuttx/pull/21966

   ## Summary
   
   On x86_64 (intel64) with `CONFIG_RTC_HIRES`, the RTC is a counter of the 
TSC. `rtc_read()` in `intel64_rtc.c` converts TSC cycles to nanoseconds with 
`(tsc / (g_x86_64_timer_freq / 1000000)) * 1000`. This has two problems: 
`g_x86_64_timer_freq / 1000000` drops the part of the frequency below 1 MHz, so 
the clock runs fast (for example 164 ppm, 14 s per day, at 2112.346 MHz); and 
the result is a multiple of 1000 ns, so `CLOCK_MONOTONIC` and `CLOCK_REALTIME` 
have a step of 1 us.
   
   `rtc_read()` also adds `0xffffffffffffffff - last` when the TSC value is 
smaller than the last value read, for a wrap that the 64-bit TSC does not do 
(2^64 cycles at 4 GHz is 146 years). On SMP, a CPU with a TSC a little behind 
the TSC of the CPU that read last gets there, and the time goes forward by 
about 2^64 cycles (225 years at 2.6 GHz).
   
   This PR calculates the seconds and the remainder separately, as 
`up_perf_convert()` in `intel64_perf.c` does. The remainder times 10^9 fits in 
64 bits for all frequencies below 18 GHz. When the TSC value is smaller than 
the last value, `rtc_read()` now keeps the last value, so the time does not go 
back and does not jump.
   
   ## Impact
   
   Only x86_64 intel64 with `CONFIG_RTC_HIRES`. The clock now runs at the 
configured TSC frequency and has a resolution of 1 ns. No defconfig sets 
`CONFIG_RTC_HIRES` on x86_64. No API change, no Kconfig change, no build file 
change.
   
   ## Testing
   
   Host test of the arithmetic (master and this PR against exact 128-bit math, 
GCC 16.2 on x86_64 Linux): this PR is within 1 ns of the exact value for 2.6 
GHz, 2112.345678 MHz, 4147.2 MHz, 1999.999999 MHz and 4294.967295 MHz, and 
uptimes from 1 s to 30 days. Master is 333 ns low at 2.6 GHz and 14.1 s per day 
fast at 2112.345678 MHz, 43.2 s per day at 1999.999999 MHz. SMP case (last read 
at 10 s, next read 1 us behind): master returns 224.8 years, this PR 10.000000 
s.
   
   QEMU 10.2.2 under KVM, GCC 12.3 in the NuttX CI image, make. A test program 
reads `CLOCK_MONOTONIC` and the TSC before and after `usleep()`, and prints the 
TSC frequency that the clock implies and the nanoseconds of ten reads in a row. 
`qemu-intel64:nsh` (flat), TSC deadline timer, `CONFIG_RTC`, 
`CONFIG_RTC_HIRES`, `CONFIG_ARCH_INTEL64_CORE_FREQ_KHZ=2112346`:
   
   | | master | this PR |
   |---|---|---|
   | implied TSC frequency (2 s, 5 s) | 2112000182 Hz, 2112000043 Hz | 
2112345992 Hz, 2112346015 Hz |
   | `CLOCK_MONOTONIC` ns % 1000, ten reads | 0 0 0 0 0 0 0 0 0 0 | 254 465 580 
660 735 812 892 967 46 135 |
   | `date`, `sleep 2`, `date` | +2 s | +2 s |
   
   `qemu-intel64:knsh_romfs` (kernel build), TSC deadline timer, `CONFIG_RTC`, 
`CONFIG_RTC_HIRES`: boots, `date`, `sleep 2`, `date` gives 00:00:00 and 
00:00:02.
   
   `qemu-intel64:nsh` with `CONFIG_SMP`, 4 CPUs (`-smp 4`), TSC deadline timer, 
`CONFIG_RTC`, `CONFIG_RTC_HIRES`, the defconfig 
`CONFIG_ARCH_INTEL64_CORE_FREQ_KHZ=2600000`: master implies 2599999280 Hz and 
2600000110 Hz with ns % 1000 always 0; this PR 2600000259 Hz and 2600000320 Hz 
with ns % 1000 from 7 to 967; `date`, `sleep 2`, `date` gives +2 or +3 s on 
both. KVM keeps the TSCs of the CPUs equal, so the SMP step does not show in 
QEMU: the host test above shows it.
   
   x86_64 has no protected build. No CI test covers this: no x86_64 CI 
configuration sets `CONFIG_RTC_HIRES`.
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to