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