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