Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/event_scheduler.h" |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <deque> |
| 5 | |
| 6 | #include "aos/events/event_loop.h" |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 7 | #include "aos/logging/implementations.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 11 | EventScheduler::Token EventScheduler::Schedule(monotonic_clock::time_point time, |
| 12 | Event *callback) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | return events_list_.emplace(time, callback); |
| 14 | } |
| 15 | |
| 16 | void EventScheduler::Deschedule(EventScheduler::Token token) { |
Brian Silverman | 7026e2d | 2021-11-11 16:15:35 -0800 | [diff] [blame] | 17 | // We basically want to DCHECK some nontrivial logic. Guard it with NDEBUG to |
| 18 | // ensure the compiler realizes it's all unnecessary when not doing debug |
| 19 | // checks. |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 20 | #ifndef NDEBUG |
| 21 | { |
| 22 | bool found = false; |
| 23 | auto i = events_list_.begin(); |
| 24 | while (i != events_list_.end()) { |
| 25 | if (i == token) { |
| 26 | CHECK(!found) << ": The same iterator is in the multimap twice??"; |
| 27 | found = true; |
| 28 | } |
| 29 | ++i; |
| 30 | } |
| 31 | CHECK(found) << ": Trying to deschedule an event which is not scheduled"; |
| 32 | } |
| 33 | #endif |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | events_list_.erase(token); |
| 35 | } |
| 36 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 37 | aos::monotonic_clock::time_point EventScheduler::OldestEvent() { |
| 38 | if (events_list_.empty()) { |
| 39 | return monotonic_clock::max_time; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 40 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 41 | |
| 42 | return events_list_.begin()->first; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Brian Silverman | 7026e2d | 2021-11-11 16:15:35 -0800 | [diff] [blame] | 45 | void EventScheduler::Shutdown() { on_shutdown_(); } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 46 | |
| 47 | void EventScheduler::Startup() { |
| 48 | ++boot_count_; |
| 49 | RunOnStartup(); |
| 50 | } |
| 51 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 52 | void EventScheduler::CallOldestEvent() { |
| 53 | CHECK_GT(events_list_.size(), 0u); |
| 54 | auto iter = events_list_.begin(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 55 | const logger::BootTimestamp t = |
| 56 | FromDistributedClock(scheduler_scheduler_->distributed_now()); |
| 57 | VLOG(1) << "Got time back " << t; |
| 58 | CHECK_EQ(t.boot, boot_count_); |
| 59 | CHECK_EQ(t.time, iter->first) << ": Time is wrong on node " << node_index_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 60 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 61 | Event *callback = iter->second; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 62 | events_list_.erase(iter); |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 63 | callback->Handle(); |
Austin Schuh | b7c8d2a | 2021-07-19 19:22:12 -0700 | [diff] [blame] | 64 | |
| 65 | converter_->ObserveTimePassed(scheduler_scheduler_->distributed_now()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void EventScheduler::RunOnRun() { |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 69 | while (!on_run_.empty()) { |
| 70 | std::function<void()> fn = std::move(*on_run_.begin()); |
| 71 | on_run_.erase(on_run_.begin()); |
| 72 | fn(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 73 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 76 | void EventScheduler::RunOnStartup() noexcept { |
| 77 | while (!on_startup_.empty()) { |
| 78 | std::function<void()> fn = std::move(*on_startup_.begin()); |
| 79 | on_startup_.erase(on_startup_.begin()); |
| 80 | fn(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 81 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 84 | void EventScheduler::RunStarted() { |
| 85 | if (started_) { |
| 86 | started_(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void EventScheduler::RunStopped() { |
| 91 | if (stopped_) { |
| 92 | stopped_(); |
| 93 | } |
| 94 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 95 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 96 | std::ostream &operator<<(std::ostream &stream, |
| 97 | const aos::distributed_clock::time_point &now) { |
| 98 | // Print it the same way we print a monotonic time. Literally. |
| 99 | stream << monotonic_clock::time_point(now.time_since_epoch()); |
| 100 | return stream; |
| 101 | } |
| 102 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 103 | void EventSchedulerScheduler::AddEventScheduler(EventScheduler *scheduler) { |
| 104 | CHECK(std::find(schedulers_.begin(), schedulers_.end(), scheduler) == |
| 105 | schedulers_.end()); |
| 106 | CHECK(scheduler->scheduler_scheduler_ == nullptr); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 107 | CHECK_EQ(scheduler->node_index(), schedulers_.size()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 108 | |
| 109 | schedulers_.emplace_back(scheduler); |
| 110 | scheduler->scheduler_scheduler_ = this; |
| 111 | } |
| 112 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 113 | void EventSchedulerScheduler::Reboot() { |
| 114 | const std::vector<logger::BootTimestamp> × = |
| 115 | std::get<1>(reboots_.front()); |
| 116 | CHECK_EQ(times.size(), schedulers_.size()); |
| 117 | |
| 118 | VLOG(1) << "Rebooting at " << now_; |
| 119 | for (const auto &time : times) { |
| 120 | VLOG(1) << " " << time; |
| 121 | } |
| 122 | |
| 123 | is_running_ = false; |
| 124 | |
| 125 | // Shut everything down. |
| 126 | std::vector<size_t> rebooted; |
| 127 | for (size_t node_index = 0; node_index < schedulers_.size(); ++node_index) { |
| 128 | if (schedulers_[node_index]->boot_count() == times[node_index].boot) { |
| 129 | continue; |
| 130 | } else { |
| 131 | rebooted.emplace_back(node_index); |
| 132 | CHECK_EQ(schedulers_[node_index]->boot_count() + 1, |
| 133 | times[node_index].boot); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 134 | schedulers_[node_index]->RunStopped(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 135 | schedulers_[node_index]->Shutdown(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // And start it back up again to reboot. When something starts back up |
| 140 | // (especially message_bridge), it could try to send stuff out. We want |
| 141 | // to move everything over to the new boot before doing that. |
| 142 | for (const size_t node_index : rebooted) { |
| 143 | CHECK_EQ(schedulers_[node_index]->boot_count() + 1, times[node_index].boot); |
| 144 | schedulers_[node_index]->Startup(); |
| 145 | } |
| 146 | |
| 147 | for (const size_t node_index : rebooted) { |
| 148 | schedulers_[node_index]->RunStarted(); |
| 149 | } |
| 150 | |
| 151 | for (const size_t node_index : rebooted) { |
| 152 | schedulers_[node_index]->RunOnRun(); |
| 153 | } |
| 154 | is_running_ = true; |
| 155 | } |
| 156 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 157 | void EventSchedulerScheduler::RunFor(distributed_clock::duration duration) { |
| 158 | distributed_clock::time_point end_time = now_ + duration; |
| 159 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 160 | RunOnStartup(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 161 | RunOnRun(); |
| 162 | |
| 163 | // Run all the sub-event-schedulers. |
| 164 | while (is_running_) { |
| 165 | std::tuple<distributed_clock::time_point, EventScheduler *> oldest_event = |
| 166 | OldestEvent(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 167 | if (!reboots_.empty() && |
| 168 | std::get<0>(reboots_.front()) <= std::get<0>(oldest_event)) { |
| 169 | // Reboot is next. |
| 170 | if (std::get<0>(reboots_.front()) > end_time) { |
| 171 | // Reboot is after our end time, give up. |
| 172 | is_running_ = false; |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | CHECK_LE(now_, |
| 177 | std::get<0>(reboots_.front()) + std::chrono::nanoseconds(1)) |
| 178 | << ": Simulated time went backwards by too much. Please " |
| 179 | "investigate."; |
| 180 | now_ = std::get<0>(reboots_.front()); |
| 181 | Reboot(); |
| 182 | reboots_.erase(reboots_.begin()); |
| 183 | continue; |
| 184 | } |
| 185 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 186 | // No events left, bail. |
| 187 | if (std::get<0>(oldest_event) == distributed_clock::max_time || |
| 188 | std::get<0>(oldest_event) > end_time) { |
| 189 | is_running_ = false; |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | // We get to pick our tradeoffs here. Either we assume that there are no |
| 194 | // backward step changes in our time function for each node, or we have to |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 195 | // let time go backwards. We currently only really see this happen when 2 |
| 196 | // events are scheduled for "now", time changes, and there is a nanosecond |
| 197 | // or two of rounding due to integer math. |
| 198 | // |
| 199 | // //aos/events/logging:logger_test triggers this. |
| 200 | CHECK_LE(now_, std::get<0>(oldest_event) + std::chrono::nanoseconds(1)) |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 201 | << ": Simulated time went backwards by too much. Please investigate."; |
| 202 | now_ = std::get<0>(oldest_event); |
| 203 | |
| 204 | std::get<1>(oldest_event)->CallOldestEvent(); |
| 205 | } |
| 206 | |
| 207 | now_ = end_time; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 208 | |
| 209 | RunStopped(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void EventSchedulerScheduler::Run() { |
| 213 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 214 | RunOnStartup(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 215 | RunOnRun(); |
| 216 | // Run all the sub-event-schedulers. |
| 217 | while (is_running_) { |
| 218 | std::tuple<distributed_clock::time_point, EventScheduler *> oldest_event = |
| 219 | OldestEvent(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 220 | if (!reboots_.empty() && |
| 221 | std::get<0>(reboots_.front()) <= std::get<0>(oldest_event)) { |
| 222 | // Reboot is next. |
| 223 | CHECK_LE(now_, |
| 224 | std::get<0>(reboots_.front()) + std::chrono::nanoseconds(1)) |
| 225 | << ": Simulated time went backwards by too much. Please " |
| 226 | "investigate."; |
| 227 | now_ = std::get<0>(reboots_.front()); |
| 228 | Reboot(); |
| 229 | reboots_.erase(reboots_.begin()); |
| 230 | continue; |
| 231 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 232 | // No events left, bail. |
| 233 | if (std::get<0>(oldest_event) == distributed_clock::max_time) { |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | // We get to pick our tradeoffs here. Either we assume that there are no |
| 238 | // backward step changes in our time function for each node, or we have to |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 239 | // let time go backwards. We currently only really see this happen when 2 |
| 240 | // events are scheduled for "now", time changes, and there is a nanosecond |
| 241 | // or two of rounding due to integer math. |
| 242 | // |
| 243 | // //aos/events/logging:logger_test triggers this. |
| 244 | CHECK_LE(now_, std::get<0>(oldest_event) + std::chrono::nanoseconds(1)) |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 245 | << ": Simulated time went backwards by too much. Please investigate."; |
| 246 | now_ = std::get<0>(oldest_event); |
| 247 | |
| 248 | std::get<1>(oldest_event)->CallOldestEvent(); |
| 249 | } |
| 250 | |
| 251 | is_running_ = false; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 252 | |
| 253 | RunStopped(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | std::tuple<distributed_clock::time_point, EventScheduler *> |
| 257 | EventSchedulerScheduler::OldestEvent() { |
| 258 | distributed_clock::time_point min_event_time = distributed_clock::max_time; |
| 259 | EventScheduler *min_scheduler = nullptr; |
| 260 | |
| 261 | // TODO(austin): Don't linearly search... But for N=3, it is probably the |
| 262 | // fastest way to do this. |
| 263 | for (EventScheduler *scheduler : schedulers_) { |
| 264 | const monotonic_clock::time_point monotonic_event_time = |
| 265 | scheduler->OldestEvent(); |
| 266 | if (monotonic_event_time != monotonic_clock::max_time) { |
| 267 | const distributed_clock::time_point event_time = |
| 268 | scheduler->ToDistributedClock(monotonic_event_time); |
| 269 | if (event_time < min_event_time) { |
| 270 | min_event_time = event_time; |
| 271 | min_scheduler = scheduler; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 276 | if (min_scheduler) { |
| 277 | VLOG(1) << "Oldest event " << min_event_time << " on scheduler " |
| 278 | << min_scheduler->node_index_; |
| 279 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 280 | return std::make_tuple(min_event_time, min_scheduler); |
| 281 | } |
| 282 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 283 | void EventSchedulerScheduler::TemporarilyStopAndRun(std::function<void()> fn) { |
| 284 | const bool was_running = is_running_; |
| 285 | if (is_running_) { |
| 286 | is_running_ = false; |
| 287 | RunStopped(); |
| 288 | } |
| 289 | fn(); |
| 290 | if (was_running) { |
| 291 | RunOnStartup(); |
| 292 | RunOnRun(); |
| 293 | } |
| 294 | } |
| 295 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 296 | } // namespace aos |