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