k_uptime_delta(): Defective by design #16195
Labels
area: API
Changes to public APIs
area: Kernel
bug
The issue is a bug, or the PR is fixing a bug
priority: low
Low impact/importance bug
Stale
The k_uptime_delta() API is widely used in Zephyr tests. However due to flaw in the design, the values returned by k_uptime_delta() depends on the uptime of the system if (1000 / CONFIG_SYS_CLOCK_TICKS_PER_SEC) is not an integer. As result, multiple tests are failing (example: #15983).
Cause:
The k_uptime_delta() calculates delta time using two timestamps (expressed in milliseconds) obtained form the system uptime (measured in system ticks). Unfortunately, the uptime conversion from ticks to milliseconds is inaccurate when (1000 / CONFIG_SYS_CLOCK_TICKS_PER_SEC) is not an integer. The rounding, which is applied in such case, leads to errors in delta time calculation.
Example (for CONFIG_SYS_CLOCK_TICKS_PER_SEC = 128):
Please note, that ticks 1 and 6 are reported as shorter than others, which is not true.
Possible solution:
The k_uptime_delta() API in current form cannot be fixed. The reference time has to be stored in ticks (or other unit used by kernel) and all calculations should be performed using this unit. Only the result of calculations should be converted to ms.
The text was updated successfully, but these errors were encountered: