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 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame^] | 349 | CHECK(!fell_behind_) << ": Got behind on " |
| 350 | << configuration::StrippedChannelToString( |
| 351 | simulated_channel_->channel()); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 352 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 353 | SetMsg(msgs_.front()); |
| 354 | msgs_.pop_front(); |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 355 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 358 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 359 | if (msgs_.size() == 0) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 360 | // TODO(austin): Can we just do this logic unconditionally? It is a lot |
| 361 | // simpler. And call clear, obviously. |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 362 | if (!msg_ && simulated_channel_->latest_message()) { |
| 363 | SetMsg(simulated_channel_->latest_message()); |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 364 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 365 | } else { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 366 | return std::make_pair(false, monotonic_clock::min_time); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | // We've had a message enqueued, so we don't need to go looking for the |
| 371 | // latest message from before we started. |
| 372 | SetMsg(msgs_.back()); |
| 373 | msgs_.clear(); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 374 | fell_behind_ = false; |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 375 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | private: |
| 379 | friend class SimulatedChannel; |
| 380 | |
| 381 | // Updates the state inside RawFetcher to point to the data in msg_. |
| 382 | void SetMsg(std::shared_ptr<SimulatedMessage> msg) { |
| 383 | msg_ = msg; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 384 | context_ = msg_->context; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 385 | if (channel()->read_method() != ReadMethod::PIN) { |
| 386 | context_.buffer_index = -1; |
| 387 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 388 | if (context_.remote_queue_index == 0xffffffffu) { |
| 389 | context_.remote_queue_index = context_.queue_index; |
| 390 | } |
| 391 | if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 392 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 393 | } |
| 394 | if (context_.realtime_remote_time == aos::realtime_clock::min_time) { |
| 395 | context_.realtime_remote_time = context_.realtime_event_time; |
| 396 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // Internal method for Simulation to add a message to the buffer. |
| 400 | void Enqueue(std::shared_ptr<SimulatedMessage> buffer) { |
| 401 | msgs_.emplace_back(buffer); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 402 | if (fell_behind_ || |
| 403 | msgs_.size() > static_cast<size_t>(simulated_channel_->queue_size())) { |
| 404 | fell_behind_ = true; |
| 405 | // Might as well empty out all the intermediate messages now. |
| 406 | while (msgs_.size() > 1) { |
| 407 | msgs_.pop_front(); |
| 408 | } |
| 409 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 412 | SimulatedChannel *simulated_channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 413 | std::shared_ptr<SimulatedMessage> msg_; |
| 414 | |
| 415 | // Messages queued up but not in use. |
| 416 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 417 | |
| 418 | // Whether we're currently "behind", which means a FetchNext call will fail. |
| 419 | bool fell_behind_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | class SimulatedTimerHandler : public TimerHandler { |
| 423 | public: |
| 424 | explicit SimulatedTimerHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 425 | SimulatedEventLoop *simulated_event_loop, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 426 | ::std::function<void()> fn); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 427 | ~SimulatedTimerHandler() { Disable(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 428 | |
| 429 | void Setup(monotonic_clock::time_point base, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 430 | monotonic_clock::duration repeat_offset) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 431 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 432 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 433 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 434 | void Disable() override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 435 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 436 | private: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 437 | SimulatedEventLoop *simulated_event_loop_; |
| 438 | EventHandler<SimulatedTimerHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 439 | EventScheduler *scheduler_; |
| 440 | EventScheduler::Token token_; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 441 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 442 | monotonic_clock::time_point base_; |
| 443 | monotonic_clock::duration repeat_offset_; |
| 444 | }; |
| 445 | |
| 446 | class SimulatedPhasedLoopHandler : public PhasedLoopHandler { |
| 447 | public: |
| 448 | SimulatedPhasedLoopHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 449 | SimulatedEventLoop *simulated_event_loop, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 450 | ::std::function<void(int)> fn, |
| 451 | const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 452 | const monotonic_clock::duration offset); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 453 | ~SimulatedPhasedLoopHandler(); |
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 HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 456 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 457 | void Schedule(monotonic_clock::time_point sleep_time) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 458 | |
| 459 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 460 | SimulatedEventLoop *simulated_event_loop_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 461 | EventHandler<SimulatedPhasedLoopHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 462 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 463 | EventScheduler *scheduler_; |
| 464 | EventScheduler::Token token_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | class SimulatedEventLoop : public EventLoop { |
| 468 | public: |
| 469 | explicit SimulatedEventLoop( |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 470 | EventScheduler *scheduler, NodeEventLoopFactory *node_event_loop_factory, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 471 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> |
| 472 | *channels, |
| 473 | const Configuration *configuration, |
| 474 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 475 | *raw_event_loops, |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 476 | const Node *node, pid_t tid) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 477 | : EventLoop(CHECK_NOTNULL(configuration)), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 478 | scheduler_(scheduler), |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 479 | node_event_loop_factory_(node_event_loop_factory), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 480 | channels_(channels), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 481 | raw_event_loops_(raw_event_loops), |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 482 | node_(node), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 483 | tid_(tid) { |
| 484 | raw_event_loops_->push_back(std::make_pair(this, [this](bool value) { |
| 485 | if (!has_setup_) { |
| 486 | Setup(); |
| 487 | has_setup_ = true; |
| 488 | } |
| 489 | set_is_running(value); |
| 490 | })); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 491 | } |
| 492 | ~SimulatedEventLoop() override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 493 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 494 | // the event loop so the book keeping matches. |
| 495 | timing_report_sender_.reset(); |
| 496 | |
| 497 | // Force everything with a registered fd with epoll to be destroyed now. |
| 498 | timers_.clear(); |
| 499 | phased_loops_.clear(); |
| 500 | watchers_.clear(); |
| 501 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 502 | for (auto it = raw_event_loops_->begin(); it != raw_event_loops_->end(); |
| 503 | ++it) { |
| 504 | if (it->first == this) { |
| 505 | raw_event_loops_->erase(it); |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 511 | std::chrono::nanoseconds send_delay() const { return send_delay_; } |
| 512 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
| 513 | send_delay_ = send_delay; |
| 514 | } |
| 515 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 516 | ::aos::monotonic_clock::time_point monotonic_now() override { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 517 | return node_event_loop_factory_->monotonic_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | ::aos::realtime_clock::time_point realtime_now() override { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 521 | return node_event_loop_factory_->realtime_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | ::std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override; |
| 525 | |
| 526 | ::std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override; |
| 527 | |
| 528 | void MakeRawWatcher( |
| 529 | const Channel *channel, |
| 530 | ::std::function<void(const Context &context, const void *message)> |
| 531 | watcher) override; |
| 532 | |
| 533 | TimerHandler *AddTimer(::std::function<void()> callback) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 534 | CHECK(!is_running()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 535 | return NewTimer(::std::unique_ptr<TimerHandler>( |
| 536 | new SimulatedTimerHandler(scheduler_, this, callback))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback, |
| 540 | const monotonic_clock::duration interval, |
| 541 | const monotonic_clock::duration offset = |
| 542 | ::std::chrono::seconds(0)) override { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 543 | return NewPhasedLoop( |
| 544 | ::std::unique_ptr<PhasedLoopHandler>(new SimulatedPhasedLoopHandler( |
| 545 | scheduler_, this, callback, interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | void OnRun(::std::function<void()> on_run) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 549 | scheduler_->ScheduleOnRun(on_run); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 552 | const Node *node() const override { return node_; } |
| 553 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 554 | void set_name(const std::string_view name) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 555 | name_ = std::string(name); |
| 556 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 557 | const std::string_view name() const override { return name_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 558 | |
| 559 | SimulatedChannel *GetSimulatedChannel(const Channel *channel); |
| 560 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 561 | void SetRuntimeRealtimePriority(int priority) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 562 | CHECK(!is_running()) << ": Cannot set realtime priority while running."; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 563 | priority_ = priority; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 566 | int priority() const override { return priority_; } |
| 567 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 568 | void SetRuntimeAffinity(const cpu_set_t & /*cpuset*/) override { |
| 569 | CHECK(!is_running()) << ": Cannot set affinity while running."; |
| 570 | } |
| 571 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 572 | void Setup() { |
| 573 | MaybeScheduleTimingReports(); |
| 574 | if (!skip_logger_) { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 575 | log_sender_.Initialize(MakeSender<logging::LogMessageFbs>("/aos")); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 576 | log_impl_ = log_sender_.implementation(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 577 | } |
| 578 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 579 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 580 | int NumberBuffers(const Channel *channel) override; |
| 581 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 582 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 583 | friend class SimulatedTimerHandler; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 584 | friend class SimulatedPhasedLoopHandler; |
| 585 | friend class SimulatedWatcher; |
| 586 | |
| 587 | void HandleEvent() { |
| 588 | while (true) { |
| 589 | if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) { |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | EventLoopEvent *event = PopEvent(); |
| 594 | event->HandleEvent(); |
| 595 | } |
| 596 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 597 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 598 | pid_t GetTid() override { return tid_; } |
| 599 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 600 | EventScheduler *scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 601 | NodeEventLoopFactory *node_event_loop_factory_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 602 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_; |
| 603 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 604 | *raw_event_loops_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 605 | |
| 606 | ::std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 607 | |
| 608 | int priority_ = 0; |
| 609 | |
| 610 | bool has_setup_ = false; |
| 611 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 612 | std::chrono::nanoseconds send_delay_; |
| 613 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 614 | const Node *const node_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 615 | const pid_t tid_; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 616 | |
| 617 | AosLogToFbs log_sender_; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 618 | std::shared_ptr<logging::LogImplementation> log_impl_ = nullptr; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 619 | }; |
| 620 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 621 | void SimulatedEventLoopFactory::set_send_delay( |
| 622 | std::chrono::nanoseconds send_delay) { |
| 623 | send_delay_ = send_delay; |
| 624 | for (std::pair<EventLoop *, std::function<void(bool)>> &loop : |
| 625 | raw_event_loops_) { |
| 626 | reinterpret_cast<SimulatedEventLoop *>(loop.first) |
| 627 | ->set_send_delay(send_delay_); |
| 628 | } |
| 629 | } |
| 630 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 631 | void SimulatedEventLoop::MakeRawWatcher( |
| 632 | const Channel *channel, |
| 633 | std::function<void(const Context &channel, const void *message)> watcher) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 634 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 635 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 636 | std::unique_ptr<SimulatedWatcher> shm_watcher( |
| 637 | new SimulatedWatcher(this, scheduler_, channel, std::move(watcher))); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 638 | |
| 639 | GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get()); |
| 640 | NewWatcher(std::move(shm_watcher)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 644 | const Channel *channel) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 645 | TakeSender(channel); |
| 646 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 647 | return GetSimulatedChannel(channel)->MakeRawSender(this); |
| 648 | } |
| 649 | |
| 650 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 651 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 652 | ChannelIndex(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 653 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 654 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 655 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 656 | << "\", \"type\": \"" << channel->type()->string_view() |
| 657 | << "\" } is not able to be fetched on this node. Check your " |
| 658 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 661 | return GetSimulatedChannel(channel)->MakeRawFetcher(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel( |
| 665 | const Channel *channel) { |
| 666 | auto it = channels_->find(SimpleChannel(channel)); |
| 667 | if (it == channels_->end()) { |
| 668 | it = channels_ |
| 669 | ->emplace(SimpleChannel(channel), |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 670 | std::unique_ptr<SimulatedChannel>(new SimulatedChannel( |
| 671 | channel, scheduler_, |
| 672 | std::chrono::nanoseconds( |
| 673 | configuration()->channel_storage_duration())))) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 674 | .first; |
| 675 | } |
| 676 | return it->second.get(); |
| 677 | } |
| 678 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 679 | int SimulatedEventLoop::NumberBuffers(const Channel *channel) { |
| 680 | return GetSimulatedChannel(channel)->number_buffers(); |
| 681 | } |
| 682 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 683 | SimulatedWatcher::SimulatedWatcher( |
| 684 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 685 | const Channel *channel, |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 686 | std::function<void(const Context &context, const void *message)> fn) |
| 687 | : WatcherState(simulated_event_loop, channel, std::move(fn)), |
| 688 | simulated_event_loop_(simulated_event_loop), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 689 | channel_(channel), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 690 | scheduler_(scheduler), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 691 | event_(this), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 692 | token_(scheduler_->InvalidToken()) {} |
| 693 | |
| 694 | SimulatedWatcher::~SimulatedWatcher() { |
| 695 | simulated_event_loop_->RemoveEvent(&event_); |
| 696 | if (token_ != scheduler_->InvalidToken()) { |
| 697 | scheduler_->Deschedule(token_); |
| 698 | } |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 699 | CHECK_NOTNULL(simulated_channel_)->RemoveWatcher(this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) { |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 703 | monotonic_clock::time_point event_time = |
| 704 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 705 | |
| 706 | // Messages are queued in order. If we are the first, add ourselves. |
| 707 | // Otherwise, don't. |
| 708 | if (msgs_.size() == 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 709 | event_.set_event_time(message->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 710 | simulated_event_loop_->AddEvent(&event_); |
| 711 | |
| 712 | DoSchedule(event_time); |
| 713 | } |
| 714 | |
| 715 | msgs_.emplace_back(message); |
| 716 | } |
| 717 | |
| 718 | void SimulatedWatcher::HandleEvent() { |
| 719 | CHECK_NE(msgs_.size(), 0u) << ": No events to handle."; |
| 720 | |
| 721 | const monotonic_clock::time_point monotonic_now = |
| 722 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 723 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 724 | if (simulated_event_loop_->log_impl_) { |
| 725 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 726 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 727 | Context context = msgs_.front()->context; |
| 728 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 729 | if (channel_->read_method() != ReadMethod::PIN) { |
| 730 | context.buffer_index = -1; |
| 731 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 732 | if (context.remote_queue_index == 0xffffffffu) { |
| 733 | context.remote_queue_index = context.queue_index; |
| 734 | } |
| 735 | if (context.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 736 | context.monotonic_remote_time = context.monotonic_event_time; |
| 737 | } |
| 738 | if (context.realtime_remote_time == aos::realtime_clock::min_time) { |
| 739 | context.realtime_remote_time = context.realtime_event_time; |
| 740 | } |
| 741 | |
| 742 | DoCallCallback([monotonic_now]() { return monotonic_now; }, context); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 743 | |
| 744 | msgs_.pop_front(); |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 745 | if (token_ != scheduler_->InvalidToken()) { |
| 746 | scheduler_->Deschedule(token_); |
| 747 | token_ = scheduler_->InvalidToken(); |
| 748 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 749 | if (msgs_.size() != 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 750 | event_.set_event_time(msgs_.front()->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 751 | simulated_event_loop_->AddEvent(&event_); |
| 752 | |
| 753 | DoSchedule(event_.event_time()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) { |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 758 | CHECK(token_ == scheduler_->InvalidToken()) |
| 759 | << ": May not schedule multiple times"; |
| 760 | token_ = scheduler_->Schedule( |
| 761 | event_time + simulated_event_loop_->send_delay(), [this]() { |
| 762 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 763 | token_ = scheduler_->InvalidToken(); |
| 764 | simulated_event_loop_->HandleEvent(); |
| 765 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 769 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 770 | watcher->SetSimulatedChannel(this); |
| 771 | watchers_.emplace_back(watcher); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | ::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender( |
| 775 | EventLoop *event_loop) { |
| 776 | return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
| 777 | } |
| 778 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 779 | ::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher( |
| 780 | EventLoop *event_loop) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 781 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 782 | ::std::unique_ptr<SimulatedFetcher> fetcher( |
| 783 | new SimulatedFetcher(event_loop, this)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 784 | fetchers_.push_back(fetcher.get()); |
| 785 | return ::std::move(fetcher); |
| 786 | } |
| 787 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 788 | uint32_t SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) { |
| 789 | const uint32_t queue_index = next_queue_index_.index(); |
| 790 | message->context.queue_index = queue_index; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 791 | message->context.data = message->data(channel()->max_size()) + |
| 792 | channel()->max_size() - message->context.size; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 793 | next_queue_index_ = next_queue_index_.Increment(); |
| 794 | |
| 795 | latest_message_ = message; |
| 796 | if (scheduler_->is_running()) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 797 | for (SimulatedWatcher *watcher : watchers_) { |
| 798 | watcher->Schedule(message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | for (auto &fetcher : fetchers_) { |
| 802 | fetcher->Enqueue(message); |
| 803 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 804 | |
| 805 | return queue_index; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) { |
| 809 | fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 810 | } |
| 811 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 812 | SimulatedTimerHandler::SimulatedTimerHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 813 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 814 | ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 815 | : TimerHandler(simulated_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 816 | simulated_event_loop_(simulated_event_loop), |
| 817 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 818 | scheduler_(scheduler), |
| 819 | token_(scheduler_->InvalidToken()) {} |
| 820 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 821 | void SimulatedTimerHandler::Setup(monotonic_clock::time_point base, |
| 822 | monotonic_clock::duration repeat_offset) { |
| 823 | Disable(); |
| 824 | const ::aos::monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 825 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 826 | base_ = base; |
| 827 | repeat_offset_ = repeat_offset; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 828 | token_ = scheduler_->Schedule(std::max(base, monotonic_now), [this]() { |
| 829 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 830 | token_ = scheduler_->InvalidToken(); |
| 831 | simulated_event_loop_->HandleEvent(); |
| 832 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 833 | event_.set_event_time(base_); |
| 834 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | void SimulatedTimerHandler::HandleEvent() { |
| 838 | const ::aos::monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 839 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 840 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 841 | if (simulated_event_loop_->log_impl_) { |
| 842 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 843 | } |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 844 | if (token_ != scheduler_->InvalidToken()) { |
| 845 | scheduler_->Deschedule(token_); |
| 846 | token_ = scheduler_->InvalidToken(); |
| 847 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 848 | if (repeat_offset_ != ::aos::monotonic_clock::zero()) { |
| 849 | // Reschedule. |
| 850 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 851 | token_ = scheduler_->Schedule(base_, [this]() { |
| 852 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 853 | token_ = scheduler_->InvalidToken(); |
| 854 | simulated_event_loop_->HandleEvent(); |
| 855 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 856 | event_.set_event_time(base_); |
| 857 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 858 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 859 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 860 | Call([monotonic_now]() { return monotonic_now; }, monotonic_now); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 863 | void SimulatedTimerHandler::Disable() { |
| 864 | simulated_event_loop_->RemoveEvent(&event_); |
| 865 | if (token_ != scheduler_->InvalidToken()) { |
| 866 | scheduler_->Deschedule(token_); |
| 867 | token_ = scheduler_->InvalidToken(); |
| 868 | } |
| 869 | } |
| 870 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 871 | SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 872 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 873 | ::std::function<void(int)> fn, const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 874 | const monotonic_clock::duration offset) |
| 875 | : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset), |
| 876 | simulated_event_loop_(simulated_event_loop), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 877 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 878 | scheduler_(scheduler), |
| 879 | token_(scheduler_->InvalidToken()) {} |
| 880 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 881 | SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() { |
| 882 | if (token_ != scheduler_->InvalidToken()) { |
| 883 | scheduler_->Deschedule(token_); |
| 884 | token_ = scheduler_->InvalidToken(); |
| 885 | } |
| 886 | simulated_event_loop_->RemoveEvent(&event_); |
| 887 | } |
| 888 | |
| 889 | void SimulatedPhasedLoopHandler::HandleEvent() { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 890 | monotonic_clock::time_point monotonic_now = |
| 891 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 892 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 893 | if (simulated_event_loop_->log_impl_) { |
| 894 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 895 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 896 | Call( |
| 897 | [monotonic_now]() { return monotonic_now; }, |
| 898 | [this](monotonic_clock::time_point sleep_time) { Schedule(sleep_time); }); |
| 899 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 900 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 901 | void SimulatedPhasedLoopHandler::Schedule( |
| 902 | monotonic_clock::time_point sleep_time) { |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 903 | if (token_ != scheduler_->InvalidToken()) { |
| 904 | scheduler_->Deschedule(token_); |
| 905 | token_ = scheduler_->InvalidToken(); |
| 906 | } |
| 907 | token_ = scheduler_->Schedule(sleep_time, [this]() { |
| 908 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 909 | token_ = scheduler_->InvalidToken(); |
| 910 | simulated_event_loop_->HandleEvent(); |
| 911 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 912 | event_.set_event_time(sleep_time); |
| 913 | simulated_event_loop_->AddEvent(&event_); |
| 914 | } |
| 915 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 916 | NodeEventLoopFactory::NodeEventLoopFactory( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 917 | EventSchedulerScheduler *scheduler_scheduler, |
| 918 | SimulatedEventLoopFactory *factory, const Node *node, |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 919 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 920 | *raw_event_loops) |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 921 | : factory_(factory), node_(node), raw_event_loops_(raw_event_loops) { |
| 922 | scheduler_scheduler->AddEventScheduler(&scheduler_); |
| 923 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 924 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 925 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 926 | const Configuration *configuration) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 927 | : configuration_(CHECK_NOTNULL(configuration)), |
| 928 | nodes_(configuration::GetNodes(configuration_)) { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 929 | for (const Node *node : nodes_) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 930 | node_factories_.emplace_back(new NodeEventLoopFactory( |
| 931 | &scheduler_scheduler_, this, node, &raw_event_loops_)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 932 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 933 | |
| 934 | if (configuration::MultiNode(configuration)) { |
| 935 | bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this); |
| 936 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 939 | SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {} |
| 940 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 941 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
| 942 | const Node *node) { |
| 943 | auto result = std::find_if( |
| 944 | node_factories_.begin(), node_factories_.end(), |
| 945 | [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) { |
| 946 | return node_factory->node() == node; |
| 947 | }); |
| 948 | |
| 949 | CHECK(result != node_factories_.end()) |
| 950 | << ": Failed to find node " << FlatbufferToJson(node); |
| 951 | |
| 952 | return result->get(); |
| 953 | } |
| 954 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 955 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 956 | std::string_view name, const Node *node) { |
| 957 | if (node == nullptr) { |
| 958 | CHECK(!configuration::MultiNode(configuration())) |
| 959 | << ": Can't make a single node event loop in a multi-node world."; |
| 960 | } else { |
| 961 | CHECK(configuration::MultiNode(configuration())) |
| 962 | << ": Can't make a multi-node event loop in a single-node world."; |
| 963 | } |
| 964 | return GetNodeEventLoopFactory(node)->MakeEventLoop(name); |
| 965 | } |
| 966 | |
| 967 | ::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop( |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 968 | std::string_view name) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 969 | CHECK(!scheduler_.is_running()) |
| 970 | << ": Can't create an event loop while running"; |
| 971 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 972 | pid_t tid = tid_; |
| 973 | ++tid_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 974 | ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 975 | &scheduler_, this, &channels_, factory_->configuration(), |
| 976 | raw_event_loops_, node_, tid)); |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 977 | result->set_name(name); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 978 | result->set_send_delay(factory_->send_delay()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 979 | return std::move(result); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) { |
| 983 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 984 | raw_event_loops_) { |
| 985 | event_loop.second(true); |
| 986 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 987 | scheduler_scheduler_.RunFor(duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 988 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 989 | raw_event_loops_) { |
| 990 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
| 994 | void SimulatedEventLoopFactory::Run() { |
| 995 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 996 | raw_event_loops_) { |
| 997 | event_loop.second(true); |
| 998 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 999 | scheduler_scheduler_.Run(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1000 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 1001 | raw_event_loops_) { |
| 1002 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1006 | void SimulatedEventLoopFactory::DisableForwarding(const Channel *channel) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1007 | CHECK(bridge_) << ": Can't disable forwarding without a message bridge."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1008 | bridge_->DisableForwarding(channel); |
| 1009 | } |
| 1010 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1011 | void SimulatedEventLoopFactory::DisableStatistics() { |
| 1012 | CHECK(bridge_) << ": Can't disable statistics without a message bridge."; |
| 1013 | bridge_->DisableStatistics(); |
| 1014 | } |
| 1015 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1016 | } // namespace aos |