-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alarm overflow - gettimeasticks #366
Conversation
This looks reasonable to me. The CI is failing though |
Formatting CI is passing now, but the build CI is still failing. Looking into the logs it appears to be an issue with |
Seems that we have a combination of a missing dependency specification (newlib for CXX targets), as well as a race condition in the CI:
When I try to reproduce this locally I'll attempt to fix and open a PR. |
@tyler-potyondy Try rebasing on #367 and see if that helps. |
a64295f
to
d953035
Compare
I rebased on #367 and that seems to have fixed the CI issue. @lschuermann thanks for resolving the issue! |
Marking as blocked on #367 then. Nice to see that that fixed it! |
Need rebase |
Previous implementation overflowed an intermediate calculation when computing the time in microseconds. This implementation resolves the overflow and also provides comments and assertion statements to guard against future overflow bugs.
d953035
to
a938d03
Compare
Should be rebased and good to merge now. |
This change improves the fix done by tock#366 to make the arithemetic calculation uphold `gettimeasticks` promise of less than "1us of error".
This change improves the fix done by tock#366 to make the arithmetic calculation uphold `gettimeasticks` promise of less than "1us of error".
While working on the OpenThread libtock-c port, @atar13 and @Samir-Rashid observed a bug on the nrf52840dk board attempting to use the
gettimeasticks
functionality. When tested with the following code:they observed that the the time in milliseconds wrapped around to zero approximately every 128 ms (see below)
Upon looking into this, it appears this is caused by a buffer overflow when performing the microsecond calculation in
libtock/alarm_timer.c
:The nrf52840DK board is configured to$remainder = frequency - 1$ . Depending on $(remainder * 1000 * 1000)$ will overflow and result in the timer microsecond value appearing to rollover and reset.
frequency = 32768Hz
. The greatest possible remainder value is__USE_TIME_BITS64
,tv_usec
is eitheruint32_t
oruint64_t
. In the case oftv_usec
being of typeunint32_t
, the intermediate calculationA proposed fix to this (at the loss of ~1us of precision for the frequency = 32768Hz case) is to adjust the microsecond calculation. I detailed this precision concern and overflow concerns for varied frequencies in a comment within the function. This loss of precision only occurs for the frequency value of 32768Hz. All other frequencies defined in the kernel at
kernel/src/hil/time.rs
(e.g. 1KHz, 16MHz, etc) will not experience this loss of precision as the 3 least significant digits do not encode data.@alistair23 your input would be appreciated on this since I believe you were using the
gettimeasticks
function. Please let me know if I overlooked some detail related to other board platforms timer frequency configuration.Testing this on the nrf52840dk, this implementation now returns the expected behavior and no longer exhibits the "rollover" at 128 ms.