John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/util/phased_loop.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 3 | #include <compare> |
| 4 | #include <ratio> |
| 5 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 6 | #include "glog/logging.h" |
| 7 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 8 | namespace aos::time { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 10 | PhasedLoop::PhasedLoop(const monotonic_clock::duration interval, |
| 11 | const monotonic_clock::time_point monotonic_now, |
| 12 | const monotonic_clock::duration offset) |
| 13 | : interval_(interval), offset_(offset), last_time_(offset) { |
| 14 | CHECK(offset >= monotonic_clock::duration(0)); |
| 15 | CHECK(interval > monotonic_clock::duration(0)); |
| 16 | CHECK(offset < interval); |
| 17 | Reset(monotonic_now); |
| 18 | } |
| 19 | |
| 20 | void PhasedLoop::set_interval_and_offset( |
| 21 | const monotonic_clock::duration interval, |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 22 | const monotonic_clock::duration offset, |
| 23 | std::optional<monotonic_clock::time_point> monotonic_now) { |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 24 | // Update last_time_ to the new offset so that we have an even interval |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 25 | // In doing so, set things so that last_time_ will only ever decrease on calls |
| 26 | // to set_interval_and_offset. |
| 27 | last_time_ += offset - offset_ - |
| 28 | (offset > offset_ ? interval : monotonic_clock::duration(0)); |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 29 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 30 | interval_ = interval; |
| 31 | offset_ = offset; |
| 32 | CHECK(offset_ >= monotonic_clock::duration(0)); |
| 33 | CHECK(interval_ > monotonic_clock::duration(0)); |
| 34 | CHECK(offset_ < interval_); |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 35 | // Reset effectively clears the skipped iteration count and ensures that the |
| 36 | // last time is in the interval (monotonic_now - interval, monotonic_now], |
| 37 | // which means that a call to Iterate(monotonic_now) will return 1 and set a |
| 38 | // wakeup time after monotonic_now. |
| 39 | if (monotonic_now.has_value()) { |
| 40 | Iterate(monotonic_now.value()); |
| 41 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | monotonic_clock::duration PhasedLoop::OffsetFromIntervalAndTime( |
| 45 | const monotonic_clock::duration interval, |
| 46 | const monotonic_clock::time_point monotonic_trigger) { |
| 47 | CHECK(interval > monotonic_clock::duration(0)); |
| 48 | return monotonic_trigger.time_since_epoch() - |
| 49 | (monotonic_trigger.time_since_epoch() / interval) * interval + |
| 50 | ((monotonic_trigger.time_since_epoch() >= monotonic_clock::zero()) |
| 51 | ? monotonic_clock::zero() |
| 52 | : interval); |
| 53 | } |
| 54 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 55 | int PhasedLoop::Iterate(const monotonic_clock::time_point now) { |
Brian Silverman | 8babd8f | 2020-06-23 16:38:50 -0700 | [diff] [blame] | 56 | auto next_time = monotonic_clock::epoch(); |
| 57 | // Round up to the next whole interval, ignoring offset_. |
| 58 | { |
| 59 | const auto offset_now = (now - offset_).time_since_epoch(); |
| 60 | monotonic_clock::duration prerounding; |
| 61 | if (now.time_since_epoch() >= offset_) { |
| 62 | // We're above 0, so rounding up means away from 0. |
| 63 | prerounding = offset_now + interval_; |
| 64 | } else { |
| 65 | // We're below 0, so rounding up means towards 0. |
| 66 | prerounding = offset_now + monotonic_clock::duration(1); |
| 67 | } |
| 68 | next_time += (prerounding / interval_) * interval_; |
| 69 | } |
| 70 | // Add offset_ back in. |
| 71 | next_time += offset_; |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 72 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 73 | const monotonic_clock::duration difference = next_time - last_time_; |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 74 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 75 | const int result = difference / interval_; |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 76 | CHECK_EQ( |
| 77 | 0, (next_time - offset_).time_since_epoch().count() % interval_.count()); |
Brian Silverman | 8babd8f | 2020-06-23 16:38:50 -0700 | [diff] [blame] | 78 | CHECK(next_time > now); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 79 | CHECK(next_time - now <= interval_); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 80 | last_time_ = next_time; |
| 81 | return result; |
| 82 | } |
| 83 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 84 | } // namespace aos::time |