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