Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/simulated_event_loop.h" |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <deque> |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 5 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | |
| 7 | #include "absl/container/btree_map.h" |
| 8 | #include "absl/container/btree_set.h" |
| 9 | #include "aos/json_to_flatbuffer.h" |
| 10 | #include "aos/util/phased_loop.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | // Container for both a message, and the context for it for simulation. This |
| 15 | // makes tracking the timestamps associated with the data easy. |
| 16 | struct SimulatedMessage { |
| 17 | // Struct to let us force data to be well aligned. |
| 18 | struct OveralignedChar { |
| 19 | char data alignas(32); |
| 20 | }; |
| 21 | |
| 22 | // Context for the data. |
| 23 | Context context; |
| 24 | |
| 25 | // The data. |
| 26 | char *data() { return reinterpret_cast<char *>(&actual_data[0]); } |
| 27 | |
| 28 | // Then the data. |
| 29 | OveralignedChar actual_data[]; |
| 30 | }; |
| 31 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 32 | class SimulatedEventLoop; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | class SimulatedFetcher; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 34 | class SimulatedChannel; |
| 35 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 36 | class SimulatedWatcher : public WatcherState { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 37 | public: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 38 | SimulatedWatcher( |
| 39 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
| 40 | const Channel *channel, |
| 41 | std::function<void(const Context &context, const void *message)> fn); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 43 | ~SimulatedWatcher() override; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 44 | |
| 45 | void Startup(EventLoop * /*event_loop*/) override {} |
| 46 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 47 | void Schedule(std::shared_ptr<SimulatedMessage> message); |
| 48 | |
| 49 | void HandleEvent(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 50 | |
| 51 | void SetSimulatedChannel(SimulatedChannel *channel) { |
| 52 | simulated_channel_ = channel; |
| 53 | } |
| 54 | |
| 55 | private: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 56 | void DoSchedule(monotonic_clock::time_point event_time); |
| 57 | |
| 58 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
| 59 | |
| 60 | SimulatedEventLoop *simulated_event_loop_; |
| 61 | EventHandler<SimulatedWatcher> event_; |
| 62 | EventScheduler *scheduler_; |
| 63 | EventScheduler::Token token_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 64 | SimulatedChannel *simulated_channel_ = nullptr; |
| 65 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 66 | |
| 67 | class SimulatedChannel { |
| 68 | public: |
| 69 | explicit SimulatedChannel(const Channel *channel, EventScheduler *scheduler) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 70 | : channel_(channel), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 71 | scheduler_(scheduler), |
| 72 | next_queue_index_(ipc_lib::QueueIndex::Zero(channel->max_size())) {} |
| 73 | |
| 74 | ~SimulatedChannel() { CHECK_EQ(0u, fetchers_.size()); } |
| 75 | |
| 76 | // Makes a connected raw sender which calls Send below. |
| 77 | ::std::unique_ptr<RawSender> MakeRawSender(EventLoop *event_loop); |
| 78 | |
| 79 | // Makes a connected raw fetcher. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 80 | ::std::unique_ptr<RawFetcher> MakeRawFetcher(EventLoop *event_loop); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | |
| 82 | // Registers a watcher for the queue. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 83 | void MakeRawWatcher(SimulatedWatcher *watcher); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 84 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 85 | void RemoveWatcher(SimulatedWatcher *watcher) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 86 | watchers_.erase(std::find(watchers_.begin(), watchers_.end(), watcher)); |
| 87 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 89 | // Sends the message to all the connected receivers and fetchers. Returns the |
| 90 | // sent queue index. |
| 91 | uint32_t Send(std::shared_ptr<SimulatedMessage> message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | |
| 93 | // Unregisters a fetcher. |
| 94 | void UnregisterFetcher(SimulatedFetcher *fetcher); |
| 95 | |
| 96 | std::shared_ptr<SimulatedMessage> latest_message() { return latest_message_; } |
| 97 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 98 | size_t max_size() const { return channel()->max_size(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 99 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 100 | const std::string_view name() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 101 | return channel()->name()->string_view(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 104 | const Channel *channel() const { return channel_; } |
| 105 | |
| 106 | ::aos::monotonic_clock::time_point monotonic_now() const { |
| 107 | return scheduler_->monotonic_now(); |
| 108 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 109 | |
| 110 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 111 | const Channel *channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 112 | |
| 113 | // List of all watchers. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 114 | ::std::vector<SimulatedWatcher *> watchers_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | |
| 116 | // List of all fetchers. |
| 117 | ::std::vector<SimulatedFetcher *> fetchers_; |
| 118 | std::shared_ptr<SimulatedMessage> latest_message_; |
| 119 | EventScheduler *scheduler_; |
| 120 | |
| 121 | ipc_lib::QueueIndex next_queue_index_; |
| 122 | }; |
| 123 | |
| 124 | namespace { |
| 125 | |
| 126 | // Creates a SimulatedMessage with size bytes of storage. |
| 127 | // This is a shared_ptr so we don't have to implement refcounting or copying. |
| 128 | std::shared_ptr<SimulatedMessage> MakeSimulatedMessage(size_t size) { |
| 129 | SimulatedMessage *message = reinterpret_cast<SimulatedMessage *>( |
| 130 | malloc(sizeof(SimulatedMessage) + size)); |
| 131 | message->context.size = size; |
| 132 | message->context.data = message->data(); |
| 133 | |
| 134 | return std::shared_ptr<SimulatedMessage>(message, free); |
| 135 | } |
| 136 | |
| 137 | class SimulatedSender : public RawSender { |
| 138 | public: |
| 139 | SimulatedSender(SimulatedChannel *simulated_channel, EventLoop *event_loop) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 140 | : RawSender(event_loop, simulated_channel->channel()), |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 141 | simulated_channel_(simulated_channel), |
| 142 | event_loop_(event_loop) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 143 | ~SimulatedSender() {} |
| 144 | |
| 145 | void *data() override { |
| 146 | if (!message_) { |
| 147 | message_ = MakeSimulatedMessage(simulated_channel_->max_size()); |
| 148 | } |
| 149 | return message_->data(); |
| 150 | } |
| 151 | |
| 152 | size_t size() override { return simulated_channel_->max_size(); } |
| 153 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 154 | bool DoSend(size_t length, |
| 155 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 156 | aos::realtime_clock::time_point realtime_remote_time, |
| 157 | uint32_t remote_queue_index) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 158 | CHECK_LE(length, size()) << ": Attempting to send too big a message."; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 159 | message_->context.monotonic_event_time = event_loop_->monotonic_now(); |
| 160 | message_->context.monotonic_remote_time = monotonic_remote_time; |
| 161 | message_->context.remote_queue_index = remote_queue_index; |
| 162 | message_->context.realtime_event_time = event_loop_->realtime_now(); |
| 163 | message_->context.realtime_remote_time = realtime_remote_time; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | CHECK_LE(length, message_->context.size); |
| 165 | message_->context.size = length; |
| 166 | |
| 167 | // TODO(austin): Track sending too fast. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 168 | sent_queue_index_ = simulated_channel_->Send(message_); |
| 169 | monotonic_sent_time_ = event_loop_->monotonic_now(); |
| 170 | realtime_sent_time_ = event_loop_->realtime_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 171 | |
| 172 | // Drop the reference to the message so that we allocate a new message for |
| 173 | // next time. Otherwise we will continue to reuse the same memory for all |
| 174 | // messages and corrupt it. |
| 175 | message_.reset(); |
| 176 | return true; |
| 177 | } |
| 178 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 179 | bool DoSend(const void *msg, size_t size, |
| 180 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 181 | aos::realtime_clock::time_point realtime_remote_time, |
| 182 | uint32_t remote_queue_index) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 183 | CHECK_LE(size, this->size()) << ": Attempting to send too big a message."; |
| 184 | |
| 185 | // This is wasteful, but since flatbuffers fill from the back end of the |
| 186 | // queue, we need it to be full sized. |
| 187 | message_ = MakeSimulatedMessage(simulated_channel_->max_size()); |
| 188 | |
| 189 | // Now fill in the message. size is already populated above, and |
| 190 | // queue_index will be populated in queue_. Put this at the back of the |
| 191 | // data segment. |
| 192 | memcpy(message_->data() + simulated_channel_->max_size() - size, msg, size); |
| 193 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 194 | return Send(size, monotonic_remote_time, realtime_remote_time, |
| 195 | remote_queue_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 198 | private: |
| 199 | SimulatedChannel *simulated_channel_; |
| 200 | EventLoop *event_loop_; |
| 201 | |
| 202 | std::shared_ptr<SimulatedMessage> message_; |
| 203 | }; |
| 204 | } // namespace |
| 205 | |
| 206 | class SimulatedFetcher : public RawFetcher { |
| 207 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 208 | explicit SimulatedFetcher(EventLoop *event_loop, SimulatedChannel *queue) |
| 209 | : RawFetcher(event_loop, queue->channel()), queue_(queue) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 210 | ~SimulatedFetcher() { queue_->UnregisterFetcher(this); } |
| 211 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 212 | std::pair<bool, monotonic_clock::time_point> DoFetchNext() override { |
| 213 | if (msgs_.size() == 0) { |
| 214 | return std::make_pair(false, monotonic_clock::min_time); |
| 215 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 216 | |
| 217 | SetMsg(msgs_.front()); |
| 218 | msgs_.pop_front(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 219 | return std::make_pair(true, queue_->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 222 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 223 | if (msgs_.size() == 0) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 224 | // TODO(austin): Can we just do this logic unconditionally? It is a lot |
| 225 | // simpler. And call clear, obviously. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 226 | if (!msg_ && queue_->latest_message()) { |
| 227 | SetMsg(queue_->latest_message()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 228 | return std::make_pair(true, queue_->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 229 | } else { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 230 | return std::make_pair(false, monotonic_clock::min_time); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | // We've had a message enqueued, so we don't need to go looking for the |
| 235 | // latest message from before we started. |
| 236 | SetMsg(msgs_.back()); |
| 237 | msgs_.clear(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 238 | return std::make_pair(true, queue_->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | private: |
| 242 | friend class SimulatedChannel; |
| 243 | |
| 244 | // Updates the state inside RawFetcher to point to the data in msg_. |
| 245 | void SetMsg(std::shared_ptr<SimulatedMessage> msg) { |
| 246 | msg_ = msg; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 247 | context_ = msg_->context; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 248 | if (context_.remote_queue_index == 0xffffffffu) { |
| 249 | context_.remote_queue_index = context_.queue_index; |
| 250 | } |
| 251 | if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 252 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 253 | } |
| 254 | if (context_.realtime_remote_time == aos::realtime_clock::min_time) { |
| 255 | context_.realtime_remote_time = context_.realtime_event_time; |
| 256 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Internal method for Simulation to add a message to the buffer. |
| 260 | void Enqueue(std::shared_ptr<SimulatedMessage> buffer) { |
| 261 | msgs_.emplace_back(buffer); |
| 262 | } |
| 263 | |
| 264 | SimulatedChannel *queue_; |
| 265 | std::shared_ptr<SimulatedMessage> msg_; |
| 266 | |
| 267 | // Messages queued up but not in use. |
| 268 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
| 269 | }; |
| 270 | |
| 271 | class SimulatedTimerHandler : public TimerHandler { |
| 272 | public: |
| 273 | explicit SimulatedTimerHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 274 | SimulatedEventLoop *simulated_event_loop, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 275 | ::std::function<void()> fn); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 276 | ~SimulatedTimerHandler() { Disable(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 277 | |
| 278 | void Setup(monotonic_clock::time_point base, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 279 | monotonic_clock::duration repeat_offset) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 280 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 281 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 282 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 283 | void Disable() override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 284 | |
| 285 | ::aos::monotonic_clock::time_point monotonic_now() const { |
| 286 | return scheduler_->monotonic_now(); |
| 287 | } |
| 288 | |
| 289 | private: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 290 | SimulatedEventLoop *simulated_event_loop_; |
| 291 | EventHandler<SimulatedTimerHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 292 | EventScheduler *scheduler_; |
| 293 | EventScheduler::Token token_; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 294 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 295 | monotonic_clock::time_point base_; |
| 296 | monotonic_clock::duration repeat_offset_; |
| 297 | }; |
| 298 | |
| 299 | class SimulatedPhasedLoopHandler : public PhasedLoopHandler { |
| 300 | public: |
| 301 | SimulatedPhasedLoopHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 302 | SimulatedEventLoop *simulated_event_loop, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 303 | ::std::function<void(int)> fn, |
| 304 | const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 305 | const monotonic_clock::duration offset); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 306 | ~SimulatedPhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 307 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 308 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 309 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 310 | void Schedule(monotonic_clock::time_point sleep_time) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 311 | |
| 312 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 313 | SimulatedEventLoop *simulated_event_loop_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 314 | EventHandler<SimulatedPhasedLoopHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 315 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 316 | EventScheduler *scheduler_; |
| 317 | EventScheduler::Token token_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | class SimulatedEventLoop : public EventLoop { |
| 321 | public: |
| 322 | explicit SimulatedEventLoop( |
| 323 | EventScheduler *scheduler, |
| 324 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> |
| 325 | *channels, |
| 326 | const Configuration *configuration, |
| 327 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 328 | *raw_event_loops, |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 329 | const Node *node, pid_t tid) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 330 | : EventLoop(CHECK_NOTNULL(configuration)), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 331 | scheduler_(scheduler), |
| 332 | channels_(channels), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 333 | raw_event_loops_(raw_event_loops), |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 334 | node_(node), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 335 | tid_(tid) { |
| 336 | raw_event_loops_->push_back(std::make_pair(this, [this](bool value) { |
| 337 | if (!has_setup_) { |
| 338 | Setup(); |
| 339 | has_setup_ = true; |
| 340 | } |
| 341 | set_is_running(value); |
| 342 | })); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 343 | } |
| 344 | ~SimulatedEventLoop() override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 345 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 346 | // the event loop so the book keeping matches. |
| 347 | timing_report_sender_.reset(); |
| 348 | |
| 349 | // Force everything with a registered fd with epoll to be destroyed now. |
| 350 | timers_.clear(); |
| 351 | phased_loops_.clear(); |
| 352 | watchers_.clear(); |
| 353 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 354 | for (auto it = raw_event_loops_->begin(); it != raw_event_loops_->end(); |
| 355 | ++it) { |
| 356 | if (it->first == this) { |
| 357 | raw_event_loops_->erase(it); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 363 | std::chrono::nanoseconds send_delay() const { return send_delay_; } |
| 364 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
| 365 | send_delay_ = send_delay; |
| 366 | } |
| 367 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 368 | ::aos::monotonic_clock::time_point monotonic_now() override { |
| 369 | return scheduler_->monotonic_now(); |
| 370 | } |
| 371 | |
| 372 | ::aos::realtime_clock::time_point realtime_now() override { |
| 373 | return scheduler_->realtime_now(); |
| 374 | } |
| 375 | |
| 376 | ::std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override; |
| 377 | |
| 378 | ::std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override; |
| 379 | |
| 380 | void MakeRawWatcher( |
| 381 | const Channel *channel, |
| 382 | ::std::function<void(const Context &context, const void *message)> |
| 383 | watcher) override; |
| 384 | |
| 385 | TimerHandler *AddTimer(::std::function<void()> callback) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 386 | CHECK(!is_running()); |
| 387 | return NewTimer(::std::unique_ptr<TimerHandler>( |
| 388 | new SimulatedTimerHandler(scheduler_, this, callback))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback, |
| 392 | const monotonic_clock::duration interval, |
| 393 | const monotonic_clock::duration offset = |
| 394 | ::std::chrono::seconds(0)) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 395 | return NewPhasedLoop( |
| 396 | ::std::unique_ptr<PhasedLoopHandler>(new SimulatedPhasedLoopHandler( |
| 397 | scheduler_, this, callback, interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void OnRun(::std::function<void()> on_run) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 401 | scheduler_->ScheduleOnRun(on_run); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 404 | const Node *node() const override { return node_; } |
| 405 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 406 | void set_name(const std::string_view name) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 407 | name_ = std::string(name); |
| 408 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 409 | const std::string_view name() const override { return name_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 410 | |
| 411 | SimulatedChannel *GetSimulatedChannel(const Channel *channel); |
| 412 | |
| 413 | void Take(const Channel *channel); |
| 414 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 415 | void SetRuntimeRealtimePriority(int priority) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 416 | CHECK(!is_running()) << ": Cannot set realtime priority while running."; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 417 | priority_ = priority; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 420 | int priority() const override { return priority_; } |
| 421 | |
| 422 | void Setup() { MaybeScheduleTimingReports(); } |
| 423 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 424 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 425 | friend class SimulatedTimerHandler; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 426 | friend class SimulatedPhasedLoopHandler; |
| 427 | friend class SimulatedWatcher; |
| 428 | |
| 429 | void HandleEvent() { |
| 430 | while (true) { |
| 431 | if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) { |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | EventLoopEvent *event = PopEvent(); |
| 436 | event->HandleEvent(); |
| 437 | } |
| 438 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 439 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 440 | pid_t GetTid() override { return tid_; } |
| 441 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 442 | EventScheduler *scheduler_; |
| 443 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_; |
| 444 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 445 | *raw_event_loops_; |
| 446 | absl::btree_set<SimpleChannel> taken_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 447 | |
| 448 | ::std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 449 | |
| 450 | int priority_ = 0; |
| 451 | |
| 452 | bool has_setup_ = false; |
| 453 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 454 | std::chrono::nanoseconds send_delay_; |
| 455 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 456 | const Node *const node_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 457 | const pid_t tid_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 458 | }; |
| 459 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 460 | void SimulatedEventLoopFactory::set_send_delay( |
| 461 | std::chrono::nanoseconds send_delay) { |
| 462 | send_delay_ = send_delay; |
| 463 | for (std::pair<EventLoop *, std::function<void(bool)>> &loop : |
| 464 | raw_event_loops_) { |
| 465 | reinterpret_cast<SimulatedEventLoop *>(loop.first) |
| 466 | ->set_send_delay(send_delay_); |
| 467 | } |
| 468 | } |
| 469 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 470 | void SimulatedEventLoop::MakeRawWatcher( |
| 471 | const Channel *channel, |
| 472 | std::function<void(const Context &channel, const void *message)> watcher) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 473 | ChannelIndex(channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 474 | Take(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 475 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 476 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 477 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 478 | << "\", \"type\": \"" << channel->type()->string_view() |
| 479 | << "\" } is not able to be watched on this node. Check your " |
| 480 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 483 | std::unique_ptr<SimulatedWatcher> shm_watcher( |
| 484 | new SimulatedWatcher(this, scheduler_, channel, std::move(watcher))); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 485 | |
| 486 | GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get()); |
| 487 | NewWatcher(std::move(shm_watcher)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 491 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 492 | ChannelIndex(channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 493 | Take(channel); |
| 494 | return GetSimulatedChannel(channel)->MakeRawSender(this); |
| 495 | } |
| 496 | |
| 497 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 498 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 499 | ChannelIndex(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 500 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 501 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 502 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 503 | << "\", \"type\": \"" << channel->type()->string_view() |
| 504 | << "\" } is not able to be fetched on this node. Check your " |
| 505 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 508 | return GetSimulatedChannel(channel)->MakeRawFetcher(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel( |
| 512 | const Channel *channel) { |
| 513 | auto it = channels_->find(SimpleChannel(channel)); |
| 514 | if (it == channels_->end()) { |
| 515 | it = channels_ |
| 516 | ->emplace(SimpleChannel(channel), |
| 517 | std::unique_ptr<SimulatedChannel>( |
| 518 | new SimulatedChannel(channel, scheduler_))) |
| 519 | .first; |
| 520 | } |
| 521 | return it->second.get(); |
| 522 | } |
| 523 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 524 | SimulatedWatcher::SimulatedWatcher( |
| 525 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
| 526 | const Channel *channel, |
| 527 | std::function<void(const Context &context, const void *message)> fn) |
| 528 | : WatcherState(simulated_event_loop, channel, std::move(fn)), |
| 529 | simulated_event_loop_(simulated_event_loop), |
| 530 | event_(this), |
| 531 | scheduler_(scheduler), |
| 532 | token_(scheduler_->InvalidToken()) {} |
| 533 | |
| 534 | SimulatedWatcher::~SimulatedWatcher() { |
| 535 | simulated_event_loop_->RemoveEvent(&event_); |
| 536 | if (token_ != scheduler_->InvalidToken()) { |
| 537 | scheduler_->Deschedule(token_); |
| 538 | } |
| 539 | simulated_channel_->RemoveWatcher(this); |
| 540 | } |
| 541 | |
| 542 | void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) { |
| 543 | monotonic_clock::time_point event_time = scheduler_->monotonic_now(); |
| 544 | |
| 545 | // Messages are queued in order. If we are the first, add ourselves. |
| 546 | // Otherwise, don't. |
| 547 | if (msgs_.size() == 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 548 | event_.set_event_time(message->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 549 | simulated_event_loop_->AddEvent(&event_); |
| 550 | |
| 551 | DoSchedule(event_time); |
| 552 | } |
| 553 | |
| 554 | msgs_.emplace_back(message); |
| 555 | } |
| 556 | |
| 557 | void SimulatedWatcher::HandleEvent() { |
| 558 | CHECK_NE(msgs_.size(), 0u) << ": No events to handle."; |
| 559 | |
| 560 | const monotonic_clock::time_point monotonic_now = |
| 561 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 562 | Context context = msgs_.front()->context; |
| 563 | |
| 564 | if (context.remote_queue_index == 0xffffffffu) { |
| 565 | context.remote_queue_index = context.queue_index; |
| 566 | } |
| 567 | if (context.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 568 | context.monotonic_remote_time = context.monotonic_event_time; |
| 569 | } |
| 570 | if (context.realtime_remote_time == aos::realtime_clock::min_time) { |
| 571 | context.realtime_remote_time = context.realtime_event_time; |
| 572 | } |
| 573 | |
| 574 | DoCallCallback([monotonic_now]() { return monotonic_now; }, context); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 575 | |
| 576 | msgs_.pop_front(); |
| 577 | if (msgs_.size() != 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 578 | event_.set_event_time(msgs_.front()->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 579 | simulated_event_loop_->AddEvent(&event_); |
| 580 | |
| 581 | DoSchedule(event_.event_time()); |
| 582 | } else { |
| 583 | token_ = scheduler_->InvalidToken(); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) { |
| 588 | token_ = |
| 589 | scheduler_->Schedule(event_time + simulated_event_loop_->send_delay(), |
| 590 | [this]() { simulated_event_loop_->HandleEvent(); }); |
| 591 | } |
| 592 | |
| 593 | void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 594 | watcher->SetSimulatedChannel(this); |
| 595 | watchers_.emplace_back(watcher); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | ::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender( |
| 599 | EventLoop *event_loop) { |
| 600 | return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
| 601 | } |
| 602 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 603 | ::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher( |
| 604 | EventLoop *event_loop) { |
| 605 | ::std::unique_ptr<SimulatedFetcher> fetcher( |
| 606 | new SimulatedFetcher(event_loop, this)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 607 | fetchers_.push_back(fetcher.get()); |
| 608 | return ::std::move(fetcher); |
| 609 | } |
| 610 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 611 | uint32_t SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) { |
| 612 | const uint32_t queue_index = next_queue_index_.index(); |
| 613 | message->context.queue_index = queue_index; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 614 | message->context.data = |
| 615 | message->data() + channel()->max_size() - message->context.size; |
| 616 | next_queue_index_ = next_queue_index_.Increment(); |
| 617 | |
| 618 | latest_message_ = message; |
| 619 | if (scheduler_->is_running()) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 620 | for (SimulatedWatcher *watcher : watchers_) { |
| 621 | watcher->Schedule(message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | for (auto &fetcher : fetchers_) { |
| 625 | fetcher->Enqueue(message); |
| 626 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 627 | |
| 628 | return queue_index; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) { |
| 632 | fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 633 | } |
| 634 | |
| 635 | SimpleChannel::SimpleChannel(const Channel *channel) |
| 636 | : name(CHECK_NOTNULL(CHECK_NOTNULL(channel)->name())->str()), |
| 637 | type(CHECK_NOTNULL(CHECK_NOTNULL(channel)->type())->str()) {} |
| 638 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 639 | SimulatedTimerHandler::SimulatedTimerHandler( |
| 640 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 641 | ::std::function<void()> fn) |
| 642 | : TimerHandler(simulated_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 643 | simulated_event_loop_(simulated_event_loop), |
| 644 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 645 | scheduler_(scheduler), |
| 646 | token_(scheduler_->InvalidToken()) {} |
| 647 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 648 | void SimulatedTimerHandler::Setup(monotonic_clock::time_point base, |
| 649 | monotonic_clock::duration repeat_offset) { |
| 650 | Disable(); |
| 651 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 652 | scheduler_->monotonic_now(); |
| 653 | base_ = base; |
| 654 | repeat_offset_ = repeat_offset; |
| 655 | if (base < monotonic_now) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 656 | token_ = scheduler_->Schedule( |
| 657 | monotonic_now, [this]() { simulated_event_loop_->HandleEvent(); }); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 658 | } else { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 659 | token_ = scheduler_->Schedule( |
| 660 | base, [this]() { simulated_event_loop_->HandleEvent(); }); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 661 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 662 | event_.set_event_time(base_); |
| 663 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void SimulatedTimerHandler::HandleEvent() { |
| 667 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 668 | scheduler_->monotonic_now(); |
| 669 | if (repeat_offset_ != ::aos::monotonic_clock::zero()) { |
| 670 | // Reschedule. |
| 671 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 672 | token_ = scheduler_->Schedule( |
| 673 | base_, [this]() { simulated_event_loop_->HandleEvent(); }); |
| 674 | event_.set_event_time(base_); |
| 675 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 676 | } else { |
| 677 | token_ = scheduler_->InvalidToken(); |
| 678 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 679 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 680 | Call([monotonic_now]() { return monotonic_now; }, monotonic_now); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 683 | void SimulatedTimerHandler::Disable() { |
| 684 | simulated_event_loop_->RemoveEvent(&event_); |
| 685 | if (token_ != scheduler_->InvalidToken()) { |
| 686 | scheduler_->Deschedule(token_); |
| 687 | token_ = scheduler_->InvalidToken(); |
| 688 | } |
| 689 | } |
| 690 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 691 | SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler( |
| 692 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 693 | ::std::function<void(int)> fn, const monotonic_clock::duration interval, |
| 694 | const monotonic_clock::duration offset) |
| 695 | : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset), |
| 696 | simulated_event_loop_(simulated_event_loop), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 697 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 698 | scheduler_(scheduler), |
| 699 | token_(scheduler_->InvalidToken()) {} |
| 700 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 701 | SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() { |
| 702 | if (token_ != scheduler_->InvalidToken()) { |
| 703 | scheduler_->Deschedule(token_); |
| 704 | token_ = scheduler_->InvalidToken(); |
| 705 | } |
| 706 | simulated_event_loop_->RemoveEvent(&event_); |
| 707 | } |
| 708 | |
| 709 | void SimulatedPhasedLoopHandler::HandleEvent() { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 710 | monotonic_clock::time_point monotonic_now = |
| 711 | simulated_event_loop_->monotonic_now(); |
| 712 | Call( |
| 713 | [monotonic_now]() { return monotonic_now; }, |
| 714 | [this](monotonic_clock::time_point sleep_time) { Schedule(sleep_time); }); |
| 715 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 716 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 717 | void SimulatedPhasedLoopHandler::Schedule( |
| 718 | monotonic_clock::time_point sleep_time) { |
| 719 | token_ = scheduler_->Schedule( |
| 720 | sleep_time, [this]() { simulated_event_loop_->HandleEvent(); }); |
| 721 | event_.set_event_time(sleep_time); |
| 722 | simulated_event_loop_->AddEvent(&event_); |
| 723 | } |
| 724 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 725 | void SimulatedEventLoop::Take(const Channel *channel) { |
| 726 | CHECK(!is_running()) << ": Cannot add new objects while running."; |
| 727 | |
| 728 | auto result = taken_.insert(SimpleChannel(channel)); |
| 729 | CHECK(result.second) << ": " << FlatbufferToJson(channel) |
| 730 | << " is already being used."; |
| 731 | } |
| 732 | |
| 733 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 734 | const Configuration *configuration) |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 735 | : configuration_(CHECK_NOTNULL(configuration)), node_(nullptr) { |
| 736 | CHECK(!configuration_->has_nodes()) |
| 737 | << ": Got a configuration with multiple nodes and no node was selected."; |
| 738 | } |
| 739 | |
| 740 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 741 | const Configuration *configuration, std::string_view node_name) |
| 742 | : configuration_(CHECK_NOTNULL(configuration)), |
| 743 | node_(configuration::GetNode(configuration, node_name)) { |
| 744 | CHECK(configuration_->has_nodes()) |
| 745 | << ": Got a configuration with no nodes and node \"" << node_name |
| 746 | << "\" was selected."; |
| 747 | CHECK(node_ != nullptr) << ": Can't find node \"" << node_name |
| 748 | << "\" in the configuration."; |
| 749 | } |
| 750 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 751 | SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {} |
| 752 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 753 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop( |
| 754 | std::string_view name) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 755 | pid_t tid = tid_; |
| 756 | ++tid_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 757 | ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop( |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 758 | &scheduler_, &channels_, configuration_, &raw_event_loops_, node_, tid)); |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 759 | result->set_name(name); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 760 | result->set_send_delay(send_delay_); |
| 761 | return std::move(result); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) { |
| 765 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 766 | raw_event_loops_) { |
| 767 | event_loop.second(true); |
| 768 | } |
| 769 | scheduler_.RunFor(duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 770 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 771 | raw_event_loops_) { |
| 772 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
| 776 | void SimulatedEventLoopFactory::Run() { |
| 777 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 778 | raw_event_loops_) { |
| 779 | event_loop.second(true); |
| 780 | } |
| 781 | scheduler_.Run(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 782 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 783 | raw_event_loops_) { |
| 784 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | } // namespace aos |