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