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 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame^] | 417 | void SetRuntimeAffinity(const cpu_set_t & /*cpuset*/) override { |
| 418 | CHECK(!is_running()) << ": Cannot set affinity while running."; |
| 419 | } |
| 420 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 421 | void Setup() { |
| 422 | MaybeScheduleTimingReports(); |
| 423 | if (!skip_logger_) { |
| 424 | logging::ScopedLogRestorer prev_logger; |
| 425 | log_sender_.Initialize(MakeSender<logging::LogMessageFbs>("/aos")); |
| 426 | log_impl_ = logging::GetImplementation(); |
| 427 | } |
| 428 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 429 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 430 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 431 | friend class SimulatedTimerHandler; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 432 | friend class SimulatedPhasedLoopHandler; |
| 433 | friend class SimulatedWatcher; |
| 434 | |
| 435 | void HandleEvent() { |
| 436 | while (true) { |
| 437 | if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) { |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | EventLoopEvent *event = PopEvent(); |
| 442 | event->HandleEvent(); |
| 443 | } |
| 444 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 445 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 446 | pid_t GetTid() override { return tid_; } |
| 447 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 448 | EventScheduler *scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 449 | NodeEventLoopFactory *node_event_loop_factory_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 450 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_; |
| 451 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 452 | *raw_event_loops_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | |
| 454 | ::std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 455 | |
| 456 | int priority_ = 0; |
| 457 | |
| 458 | bool has_setup_ = false; |
| 459 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 460 | std::chrono::nanoseconds send_delay_; |
| 461 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 462 | const Node *const node_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 463 | const pid_t tid_; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 464 | |
| 465 | AosLogToFbs log_sender_; |
| 466 | logging::LogImplementation *log_impl_ = nullptr; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 469 | void SimulatedEventLoopFactory::set_send_delay( |
| 470 | std::chrono::nanoseconds send_delay) { |
| 471 | send_delay_ = send_delay; |
| 472 | for (std::pair<EventLoop *, std::function<void(bool)>> &loop : |
| 473 | raw_event_loops_) { |
| 474 | reinterpret_cast<SimulatedEventLoop *>(loop.first) |
| 475 | ->set_send_delay(send_delay_); |
| 476 | } |
| 477 | } |
| 478 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 479 | void SimulatedEventLoop::MakeRawWatcher( |
| 480 | const Channel *channel, |
| 481 | std::function<void(const Context &channel, const void *message)> watcher) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 482 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 483 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 484 | std::unique_ptr<SimulatedWatcher> shm_watcher( |
| 485 | new SimulatedWatcher(this, scheduler_, channel, std::move(watcher))); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 486 | |
| 487 | GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get()); |
| 488 | NewWatcher(std::move(shm_watcher)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 492 | const Channel *channel) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 493 | TakeSender(channel); |
| 494 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 495 | return GetSimulatedChannel(channel)->MakeRawSender(this); |
| 496 | } |
| 497 | |
| 498 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 499 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 500 | ChannelIndex(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 501 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 502 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 503 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 504 | << "\", \"type\": \"" << channel->type()->string_view() |
| 505 | << "\" } is not able to be fetched on this node. Check your " |
| 506 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 509 | return GetSimulatedChannel(channel)->MakeRawFetcher(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel( |
| 513 | const Channel *channel) { |
| 514 | auto it = channels_->find(SimpleChannel(channel)); |
| 515 | if (it == channels_->end()) { |
| 516 | it = channels_ |
| 517 | ->emplace(SimpleChannel(channel), |
| 518 | std::unique_ptr<SimulatedChannel>( |
| 519 | new SimulatedChannel(channel, scheduler_))) |
| 520 | .first; |
| 521 | } |
| 522 | return it->second.get(); |
| 523 | } |
| 524 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 525 | SimulatedWatcher::SimulatedWatcher( |
| 526 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 527 | const Channel *channel, |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 528 | std::function<void(const Context &context, const void *message)> fn) |
| 529 | : WatcherState(simulated_event_loop, channel, std::move(fn)), |
| 530 | simulated_event_loop_(simulated_event_loop), |
| 531 | event_(this), |
| 532 | scheduler_(scheduler), |
| 533 | token_(scheduler_->InvalidToken()) {} |
| 534 | |
| 535 | SimulatedWatcher::~SimulatedWatcher() { |
| 536 | simulated_event_loop_->RemoveEvent(&event_); |
| 537 | if (token_ != scheduler_->InvalidToken()) { |
| 538 | scheduler_->Deschedule(token_); |
| 539 | } |
| 540 | simulated_channel_->RemoveWatcher(this); |
| 541 | } |
| 542 | |
| 543 | void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) { |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 544 | monotonic_clock::time_point event_time = |
| 545 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 546 | |
| 547 | // Messages are queued in order. If we are the first, add ourselves. |
| 548 | // Otherwise, don't. |
| 549 | if (msgs_.size() == 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 550 | event_.set_event_time(message->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 551 | simulated_event_loop_->AddEvent(&event_); |
| 552 | |
| 553 | DoSchedule(event_time); |
| 554 | } |
| 555 | |
| 556 | msgs_.emplace_back(message); |
| 557 | } |
| 558 | |
| 559 | void SimulatedWatcher::HandleEvent() { |
| 560 | CHECK_NE(msgs_.size(), 0u) << ": No events to handle."; |
| 561 | |
| 562 | const monotonic_clock::time_point monotonic_now = |
| 563 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 564 | logging::ScopedLogRestorer prev_logger; |
| 565 | if (simulated_event_loop_->log_impl_ != nullptr) { |
| 566 | logging::SetImplementation(simulated_event_loop_->log_impl_); |
| 567 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 568 | Context context = msgs_.front()->context; |
| 569 | |
| 570 | if (context.remote_queue_index == 0xffffffffu) { |
| 571 | context.remote_queue_index = context.queue_index; |
| 572 | } |
| 573 | if (context.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 574 | context.monotonic_remote_time = context.monotonic_event_time; |
| 575 | } |
| 576 | if (context.realtime_remote_time == aos::realtime_clock::min_time) { |
| 577 | context.realtime_remote_time = context.realtime_event_time; |
| 578 | } |
| 579 | |
| 580 | DoCallCallback([monotonic_now]() { return monotonic_now; }, context); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 581 | |
| 582 | msgs_.pop_front(); |
| 583 | if (msgs_.size() != 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 584 | event_.set_event_time(msgs_.front()->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 585 | simulated_event_loop_->AddEvent(&event_); |
| 586 | |
| 587 | DoSchedule(event_.event_time()); |
| 588 | } else { |
| 589 | token_ = scheduler_->InvalidToken(); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 594 | token_ = |
| 595 | scheduler_->Schedule(event_time + simulated_event_loop_->send_delay(), |
| 596 | [this]() { simulated_event_loop_->HandleEvent(); }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 600 | watcher->SetSimulatedChannel(this); |
| 601 | watchers_.emplace_back(watcher); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | ::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender( |
| 605 | EventLoop *event_loop) { |
| 606 | return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
| 607 | } |
| 608 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 609 | ::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher( |
| 610 | EventLoop *event_loop) { |
| 611 | ::std::unique_ptr<SimulatedFetcher> fetcher( |
| 612 | new SimulatedFetcher(event_loop, this)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 613 | fetchers_.push_back(fetcher.get()); |
| 614 | return ::std::move(fetcher); |
| 615 | } |
| 616 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 617 | uint32_t SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) { |
| 618 | const uint32_t queue_index = next_queue_index_.index(); |
| 619 | message->context.queue_index = queue_index; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 620 | message->context.data = message->data(channel()->max_size()) + |
| 621 | channel()->max_size() - message->context.size; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 622 | next_queue_index_ = next_queue_index_.Increment(); |
| 623 | |
| 624 | latest_message_ = message; |
| 625 | if (scheduler_->is_running()) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 626 | for (SimulatedWatcher *watcher : watchers_) { |
| 627 | watcher->Schedule(message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | for (auto &fetcher : fetchers_) { |
| 631 | fetcher->Enqueue(message); |
| 632 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 633 | |
| 634 | return queue_index; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) { |
| 638 | fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 639 | } |
| 640 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 641 | SimulatedTimerHandler::SimulatedTimerHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 642 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 643 | ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 644 | : TimerHandler(simulated_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 645 | simulated_event_loop_(simulated_event_loop), |
| 646 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 647 | scheduler_(scheduler), |
| 648 | token_(scheduler_->InvalidToken()) {} |
| 649 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 650 | void SimulatedTimerHandler::Setup(monotonic_clock::time_point base, |
| 651 | monotonic_clock::duration repeat_offset) { |
| 652 | Disable(); |
| 653 | const ::aos::monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 654 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 655 | base_ = base; |
| 656 | repeat_offset_ = repeat_offset; |
| 657 | if (base < monotonic_now) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 658 | token_ = scheduler_->Schedule( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 659 | monotonic_now, [this]() { simulated_event_loop_->HandleEvent(); }); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 660 | } else { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 661 | token_ = scheduler_->Schedule( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 662 | base, [this]() { simulated_event_loop_->HandleEvent(); }); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 663 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 664 | event_.set_event_time(base_); |
| 665 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | void SimulatedTimerHandler::HandleEvent() { |
| 669 | const ::aos::monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 670 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 671 | logging::ScopedLogRestorer prev_logger; |
| 672 | if (simulated_event_loop_->log_impl_ != nullptr) { |
| 673 | logging::SetImplementation(simulated_event_loop_->log_impl_); |
| 674 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 675 | if (repeat_offset_ != ::aos::monotonic_clock::zero()) { |
| 676 | // Reschedule. |
| 677 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 678 | token_ = scheduler_->Schedule( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 679 | base_, [this]() { simulated_event_loop_->HandleEvent(); }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 680 | event_.set_event_time(base_); |
| 681 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 682 | } else { |
| 683 | token_ = scheduler_->InvalidToken(); |
| 684 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 685 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 686 | Call([monotonic_now]() { return monotonic_now; }, monotonic_now); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 689 | void SimulatedTimerHandler::Disable() { |
| 690 | simulated_event_loop_->RemoveEvent(&event_); |
| 691 | if (token_ != scheduler_->InvalidToken()) { |
| 692 | scheduler_->Deschedule(token_); |
| 693 | token_ = scheduler_->InvalidToken(); |
| 694 | } |
| 695 | } |
| 696 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 697 | SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 698 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 699 | ::std::function<void(int)> fn, const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 700 | const monotonic_clock::duration offset) |
| 701 | : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset), |
| 702 | simulated_event_loop_(simulated_event_loop), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 703 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 704 | scheduler_(scheduler), |
| 705 | token_(scheduler_->InvalidToken()) {} |
| 706 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 707 | SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() { |
| 708 | if (token_ != scheduler_->InvalidToken()) { |
| 709 | scheduler_->Deschedule(token_); |
| 710 | token_ = scheduler_->InvalidToken(); |
| 711 | } |
| 712 | simulated_event_loop_->RemoveEvent(&event_); |
| 713 | } |
| 714 | |
| 715 | void SimulatedPhasedLoopHandler::HandleEvent() { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 716 | monotonic_clock::time_point monotonic_now = |
| 717 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 718 | logging::ScopedLogRestorer prev_logger; |
| 719 | if (simulated_event_loop_->log_impl_ != nullptr) { |
| 720 | logging::SetImplementation(simulated_event_loop_->log_impl_); |
| 721 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 722 | Call( |
| 723 | [monotonic_now]() { return monotonic_now; }, |
| 724 | [this](monotonic_clock::time_point sleep_time) { Schedule(sleep_time); }); |
| 725 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 726 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 727 | void SimulatedPhasedLoopHandler::Schedule( |
| 728 | monotonic_clock::time_point sleep_time) { |
| 729 | token_ = scheduler_->Schedule( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 730 | sleep_time, [this]() { simulated_event_loop_->HandleEvent(); }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 731 | event_.set_event_time(sleep_time); |
| 732 | simulated_event_loop_->AddEvent(&event_); |
| 733 | } |
| 734 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 735 | NodeEventLoopFactory::NodeEventLoopFactory( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 736 | EventSchedulerScheduler *scheduler_scheduler, |
| 737 | SimulatedEventLoopFactory *factory, const Node *node, |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 738 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 739 | *raw_event_loops) |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 740 | : factory_(factory), node_(node), raw_event_loops_(raw_event_loops) { |
| 741 | scheduler_scheduler->AddEventScheduler(&scheduler_); |
| 742 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 743 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 744 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 745 | const Configuration *configuration) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 746 | : configuration_(CHECK_NOTNULL(configuration)), |
| 747 | nodes_(configuration::GetNodes(configuration_)) { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 748 | for (const Node *node : nodes_) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 749 | node_factories_.emplace_back(new NodeEventLoopFactory( |
| 750 | &scheduler_scheduler_, this, node, &raw_event_loops_)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 751 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 752 | |
| 753 | if (configuration::MultiNode(configuration)) { |
| 754 | bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this); |
| 755 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 758 | SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {} |
| 759 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 760 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
| 761 | const Node *node) { |
| 762 | auto result = std::find_if( |
| 763 | node_factories_.begin(), node_factories_.end(), |
| 764 | [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) { |
| 765 | return node_factory->node() == node; |
| 766 | }); |
| 767 | |
| 768 | CHECK(result != node_factories_.end()) |
| 769 | << ": Failed to find node " << FlatbufferToJson(node); |
| 770 | |
| 771 | return result->get(); |
| 772 | } |
| 773 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 774 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 775 | std::string_view name, const Node *node) { |
| 776 | if (node == nullptr) { |
| 777 | CHECK(!configuration::MultiNode(configuration())) |
| 778 | << ": Can't make a single node event loop in a multi-node world."; |
| 779 | } else { |
| 780 | CHECK(configuration::MultiNode(configuration())) |
| 781 | << ": Can't make a multi-node event loop in a single-node world."; |
| 782 | } |
| 783 | return GetNodeEventLoopFactory(node)->MakeEventLoop(name); |
| 784 | } |
| 785 | |
| 786 | ::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop( |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 787 | std::string_view name) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 788 | CHECK(!scheduler_.is_running()) |
| 789 | << ": Can't create an event loop while running"; |
| 790 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 791 | pid_t tid = tid_; |
| 792 | ++tid_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 793 | ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 794 | &scheduler_, this, &channels_, factory_->configuration(), |
| 795 | raw_event_loops_, node_, tid)); |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 796 | result->set_name(name); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 797 | result->set_send_delay(factory_->send_delay()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 798 | return std::move(result); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) { |
| 802 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 803 | raw_event_loops_) { |
| 804 | event_loop.second(true); |
| 805 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 806 | scheduler_scheduler_.RunFor(duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 807 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 808 | raw_event_loops_) { |
| 809 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | void SimulatedEventLoopFactory::Run() { |
| 814 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 815 | raw_event_loops_) { |
| 816 | event_loop.second(true); |
| 817 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 818 | scheduler_scheduler_.Run(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 819 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 820 | raw_event_loops_) { |
| 821 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 825 | void SimulatedEventLoopFactory::DisableForwarding(const Channel *channel) { |
| 826 | bridge_->DisableForwarding(channel); |
| 827 | } |
| 828 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 829 | } // namespace aos |