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