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