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