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

   ## Summary
   
   `CONFIG_TIMER_ADJUST_NSEC` (b65f29da9e) is meant to fire timers early by the 
interrupt latency, so that callbacks and woken tasks run on time. It has no 
effect: tickless watchdogs (`wd_adjust_next_tick()`) and hrtimers 
(`hrtimer_reprogram()`) arm the hardware early, but `wd_expiration()` and 
`hrtimer_process()` still only expire timers that are due at the current time. 
When the early interrupt comes, nothing is due yet, so the timer is re-armed 
for its expiration minus the adjustment, which has already passed. The 
interrupt fires again, repeatedly, until the original expiration time.
   
   Trace of one `usleep(100)` on master with `CONFIG_TIMER_ADJUST_NSEC=7000`, 
alarm mode, 1 µs tick (cycle counter timestamps, TIM2 counter values, test-only 
instrumentation):
   
   ```
   trace usleep(100): TIM2 at call 10011040, returned after 109.80 us
     +    2.19 us  alarm_start tm    10011134    <- due at 10011141, armed 7 
early
     +   93.79 us  ISR entry cnt     10011134    <- nothing due
     +   95.89 us  alarm_start tm    10011134    <- re-armed in the past
     +   97.79 us  ISR entry cnt     10011138    <- nothing due
     +   99.90 us  alarm_start tm    10011134
     +  101.79 us  ISR entry cnt     10011142    <- watchdog expires, as 
without the option
   ```
   
   This PR treats a watchdog (tickless, `WDOG_ADJUST_TICKS`) or hrtimer as 
expired when it is due within the adjustment, so the early interrupt runs it. 
The re-arm target (next expiration minus the adjustment) is then always in the 
future.
   
   With the option at 0 (the default) nothing changes.
   
   Note that the option now does what its help text says: callbacks run up to 
`CONFIG_TIMER_ADJUST_NSEC` early. It must not exceed the best-case latency from 
the timer interrupt to the callback, or watchdog callbacks run before their 
time; `ostest`'s wdog test asserts on that (see Testing).
   
   ## Impact
   
   Only configurations with `CONFIG_TIMER_ADJUST_NSEC > 0`: tickless watchdogs, 
and hrtimers. For these, timers now actually fire up to the adjustment early, 
and the extra interrupts are gone. Sleeps shorter than the adjustment arm the 
alarm for "now" and complete on the next interrupt. Tick-based (non-tickless) 
watchdogs are unchanged (`WDOG_ADJUST_TICKS` is 0 there).
   
   ## Testing
   
   NUCLEO-H753ZI (Cortex-M7, 400 MHz), master fc8d354f9a, `nucleo-h753zi:nsh` + 
`CONFIG_SCHED_TICKLESS`, `CONFIG_USEC_PER_TICK=1`, TIM2, 
`CONFIG_DEBUG_FULLOPT`. Arm GNU Toolchain 13.3.Rel1. `tools/checkpatch.sh -g 
HEAD` passes.
   
   A test app sleeps N times with `usleep()` and measures each call with 
DWT_CYCCNT (200 runs, 20 for 10 ms). Average time over the requested delay 
(`nanosleep()` gives the same numbers):
   
   | mode, `CONFIG_TIMER_ADJUST_NSEC` | master | this PR |
   |---|---|---|
   | alarm, 0 | +8 µs | +8 µs |
   | alarm, 7000 | +8 µs | +1 µs (min +0) |
   | interval, 7000 | +8 µs | +4..5 µs |
   | hrtimer (alarm), 7000 | +9 µs | +3 µs |
   | alarm, 2000 | +8 µs | +6 µs |
   
   (for delays of 50 µs to 10 ms; 1 µs and 10 µs sleeps take 12..16 µs in all 
cases)
   
   `ostest`, alarm mode, `CONFIG_DEBUG_ASSERTIONS`:
   
   | `CONFIG_TIMER_ADJUST_NSEC` | master | this PR |
   |---|---|---|
   | 2000 | exit 0 (4 runs); "wdog latency" warnings: 41, 30, 24, 45 | exit 0 
(4 runs); warnings: 17, 17, 21, 15 |
   | 7000 | exit 0 | wdtest asserts (callback before its deadline): 7 µs is 
more than the ~2 µs ISR-to-callback latency on this board, so callbacks now 
really run early |
   
   The "wdog latency ticks N (> 10 ...)" lines are warnings, not failures: 
`WDOGTEST_TOLERENT_TICK` is 10 ticks, which is only 10 µs with a 1 µs tick, and 
all of them are 11..13 ticks (master also has 14 and 16). The 2 µs adjustment 
moves the 11..12 tick cases under the limit; the rest come from the timer 
interrupt being held off by other interrupts or critical sections (the test 
prints to the UART console right before each measurement), which no constant 
offset removes.
   
   Related: #8207 (usleep accuracy in tickless mode). On this board most of the 
overshoot in that issue comes from `-O0` (`CONFIG_DEBUG_NOOPT`, the default 
with `CONFIG_DEBUG_SYMBOLS`): +20 µs (alarm) / +30 µs (interval) at `-O0`, +8 
µs at `-O2`.


-- 
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