Skip to content

time enhancements - reset epoch, wrapped millis#4935

Closed
jepler wants to merge 5 commits into
adafruit:mainfrom
jepler:monotonics-ticks-reset-epoch
Closed

time enhancements - reset epoch, wrapped millis#4935
jepler wants to merge 5 commits into
adafruit:mainfrom
jepler:monotonics-ticks-reset-epoch

Conversation

@jepler
Copy link
Copy Markdown

@jepler jepler commented Jun 27, 2021

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

jepler added 2 commits June 27, 2021 11:34
 * 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.
Copy link
Copy Markdown
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a simple set of additions and is elegant. Just one doc typo.

Comment thread shared-bindings/supervisor/__init__.c Outdated
Comment thread shared-bindings/supervisor/__init__.c Outdated
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));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to explain this in a comment. It seems like a pretty random number.

@jepler
Copy link
Copy Markdown
Author

jepler commented Jun 27, 2021

This filled up a couple of m0 builds (non-adafruit products). I turned off rotaryio to let them fit.

@jepler
Copy link
Copy Markdown
Author

jepler commented Jun 27, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants