blob: 5f87bd040c25d273c799a496a6495976733e7783 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/util/phased_loop.h"
brians343bc112013-02-10 01:53:46 +00002
Stephan Pleinesb1177672024-05-27 17:48:32 -07003#include <compare>
4#include <ratio>
5
Austin Schuhf257f3c2019-10-27 21:00:43 -07006#include "glog/logging.h"
7
Stephan Pleinesf63bde82024-01-13 15:59:33 -08008namespace aos::time {
brians343bc112013-02-10 01:53:46 +00009
Austin Schuhf257f3c2019-10-27 21:00:43 -070010PhasedLoop::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
20void PhasedLoop::set_interval_and_offset(
21 const monotonic_clock::duration interval,
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080022 const monotonic_clock::duration offset,
23 std::optional<monotonic_clock::time_point> monotonic_now) {
Milind Upadhyay42589bb2021-05-19 20:05:16 -070024 // Update last_time_ to the new offset so that we have an even interval
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080025 // 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 Upadhyay42589bb2021-05-19 20:05:16 -070029
Austin Schuhf257f3c2019-10-27 21:00:43 -070030 interval_ = interval;
31 offset_ = offset;
32 CHECK(offset_ >= monotonic_clock::duration(0));
33 CHECK(interval_ > monotonic_clock::duration(0));
34 CHECK(offset_ < interval_);
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080035 // 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 Schuhf257f3c2019-10-27 21:00:43 -070042}
43
44monotonic_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 Schuh8aec1ed2016-05-01 13:29:20 -070055int PhasedLoop::Iterate(const monotonic_clock::time_point now) {
Brian Silverman8babd8f2020-06-23 16:38:50 -070056 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 Silvermandcaa3f72015-11-29 05:32:08 +000072
Austin Schuh8aec1ed2016-05-01 13:29:20 -070073 const monotonic_clock::duration difference = next_time - last_time_;
Milind Upadhyay42589bb2021-05-19 20:05:16 -070074
Austin Schuh8aec1ed2016-05-01 13:29:20 -070075 const int result = difference / interval_;
Austin Schuh8aec1ed2016-05-01 13:29:20 -070076 CHECK_EQ(
77 0, (next_time - offset_).time_since_epoch().count() % interval_.count());
Brian Silverman8babd8f2020-06-23 16:38:50 -070078 CHECK(next_time > now);
Austin Schuhf257f3c2019-10-27 21:00:43 -070079 CHECK(next_time - now <= interval_);
Brian Silvermandcaa3f72015-11-29 05:32:08 +000080 last_time_ = next_time;
81 return result;
82}
83
Stephan Pleinesf63bde82024-01-13 15:59:33 -080084} // namespace aos::time