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