time enhancements - reset epoch, wrapped millis#4935
Closed
jepler wants to merge 5 commits into
Closed
Conversation
* At soft reset, always reset the monotonic epoch. This means that a "fresh" session will not have the accuracy problems that result when `time.monotonic()` becomes a large float and the precision decreases * A new function `reset_monotonic_epoch` in supervisor can reset the epoch to now; Careful use within a program can prevent `time.monotonic()` from losing "too much" accuracy. * A new function `ticks_ms` in supervisor returns the _possibly wrapped_ time since the epoch. The wrapping number is chosen for the reasons given in adafruit#3410: math on these values can be performed in Python without the need to create long integer values (and thus avoids allocations and can work on platforms without longs) It could be argued whether `ticks_ms` should have its epoch reset with the others. I chose not to do so, because programs should be written so that they function properly when `ticks_ms` wraps
… 1 minute of 'boot' .. this will "encourage" software authors to ensure their code works over the wraparound, making it easy (and mandatory) to test. Otherwise, problems might only be seen after 150 hours.
dhalbert
requested changes
Jun 27, 2021
Collaborator
dhalbert
left a comment
There was a problem hiding this comment.
This is a simple set of additions and is elegant. Just one doc typo.
dhalbert
requested changes
Jun 27, 2021
| STATIC mp_obj_t supervisor_ticks_ms(void) { | ||
| uint64_t ticks_ms = common_hal_time_monotonic_ms(); | ||
| return mp_obj_new_int(ticks_ms % (1 << 29)); | ||
| return mp_obj_new_int((ticks_ms + 0x1fff7777) % (1 << 29)); |
Collaborator
There was a problem hiding this comment.
I think it would be good to explain this in a comment. It seems like a pretty random number.
Author
|
This filled up a couple of m0 builds (non-adafruit products). I turned off rotaryio to let them fit. |
Author
|
we may want to decide whether resetting "monotonic" or providing a wrapping ticks_ms is a better use of limited flash, rather than trying to do both. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
At soft reset, always reset the monotonic epoch. This means that a "fresh" session will not have the accuracy problems that result when
time.monotonic()becomes a large float and the precision decreasesA new function
reset_monotonic_epochin supervisor can reset the epoch to now; Careful use within a program can preventtime.monotonic()from losing "too much" accuracy.A new function
ticks_msin supervisor returns the possibly wrapped time since the epoch. The wrapping number is chosen for the reasons given in RFC: routines for handling wrapped tick counts #3410: math on these values can be performed in Python without the need to create long integer values (and thus avoids allocations and can work on platforms without longs)It could be argued whether
ticks_msshould have its epoch reset with the others. I chose not to do so, because programs should be written so that they function properly whenticks_mswraps