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 | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 302 | bool DoSend(size_t length, monotonic_clock::time_point monotonic_remote_time, |
| 303 | realtime_clock::time_point realtime_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 304 | uint32_t remote_queue_index, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 305 | const UUID &source_boot_uuid) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 306 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 307 | bool DoSend(const void *msg, size_t size, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 308 | monotonic_clock::time_point monotonic_remote_time, |
| 309 | realtime_clock::time_point realtime_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 310 | uint32_t remote_queue_index, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 311 | const UUID &source_boot_uuid) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 312 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 313 | int buffer_index() override { |
| 314 | // First, ensure message_ is allocated. |
| 315 | data(); |
| 316 | return message_->context.buffer_index; |
| 317 | } |
| 318 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 319 | private: |
| 320 | SimulatedChannel *simulated_channel_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 321 | SimulatedEventLoop *simulated_event_loop_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 322 | |
| 323 | std::shared_ptr<SimulatedMessage> message_; |
| 324 | }; |
| 325 | } // namespace |
| 326 | |
| 327 | class SimulatedFetcher : public RawFetcher { |
| 328 | public: |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 329 | explicit SimulatedFetcher(EventLoop *event_loop, |
| 330 | SimulatedChannel *simulated_channel) |
| 331 | : RawFetcher(event_loop, simulated_channel->channel()), |
| 332 | simulated_channel_(simulated_channel) {} |
| 333 | ~SimulatedFetcher() { simulated_channel_->UnregisterFetcher(this); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 334 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 335 | std::pair<bool, monotonic_clock::time_point> DoFetchNext() override { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 336 | // The allocations in here are due to infrastructure and don't count in the |
| 337 | // no mallocs in RT code. |
| 338 | ScopedNotRealtime nrt; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 339 | if (msgs_.size() == 0) { |
| 340 | return std::make_pair(false, monotonic_clock::min_time); |
| 341 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 342 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 343 | CHECK(!fell_behind_) << ": Got behind on " |
| 344 | << configuration::StrippedChannelToString( |
| 345 | simulated_channel_->channel()); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 346 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 347 | SetMsg(msgs_.front()); |
| 348 | msgs_.pop_front(); |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 349 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 352 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 353 | // The allocations in here are due to infrastructure and don't count in the |
| 354 | // no mallocs in RT code. |
| 355 | ScopedNotRealtime nrt; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 356 | if (msgs_.size() == 0) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 357 | // TODO(austin): Can we just do this logic unconditionally? It is a lot |
| 358 | // simpler. And call clear, obviously. |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 359 | if (!msg_ && simulated_channel_->latest_message()) { |
| 360 | SetMsg(simulated_channel_->latest_message()); |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 361 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 362 | } else { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 363 | return std::make_pair(false, monotonic_clock::min_time); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | // We've had a message enqueued, so we don't need to go looking for the |
| 368 | // latest message from before we started. |
| 369 | SetMsg(msgs_.back()); |
| 370 | msgs_.clear(); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 371 | fell_behind_ = false; |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 372 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | private: |
| 376 | friend class SimulatedChannel; |
| 377 | |
| 378 | // Updates the state inside RawFetcher to point to the data in msg_. |
| 379 | void SetMsg(std::shared_ptr<SimulatedMessage> msg) { |
| 380 | msg_ = msg; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 381 | context_ = msg_->context; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 382 | if (channel()->read_method() != ReadMethod::PIN) { |
| 383 | context_.buffer_index = -1; |
| 384 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 385 | if (context_.remote_queue_index == 0xffffffffu) { |
| 386 | context_.remote_queue_index = context_.queue_index; |
| 387 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 388 | if (context_.monotonic_remote_time == monotonic_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 389 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 390 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 391 | if (context_.realtime_remote_time == realtime_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 392 | context_.realtime_remote_time = context_.realtime_event_time; |
| 393 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // Internal method for Simulation to add a message to the buffer. |
| 397 | void Enqueue(std::shared_ptr<SimulatedMessage> buffer) { |
| 398 | msgs_.emplace_back(buffer); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 399 | if (fell_behind_ || |
| 400 | msgs_.size() > static_cast<size_t>(simulated_channel_->queue_size())) { |
| 401 | fell_behind_ = true; |
| 402 | // Might as well empty out all the intermediate messages now. |
| 403 | while (msgs_.size() > 1) { |
| 404 | msgs_.pop_front(); |
| 405 | } |
| 406 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 409 | SimulatedChannel *simulated_channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 410 | std::shared_ptr<SimulatedMessage> msg_; |
| 411 | |
| 412 | // Messages queued up but not in use. |
| 413 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 414 | |
| 415 | // Whether we're currently "behind", which means a FetchNext call will fail. |
| 416 | bool fell_behind_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | class SimulatedTimerHandler : public TimerHandler { |
| 420 | public: |
| 421 | explicit SimulatedTimerHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 422 | SimulatedEventLoop *simulated_event_loop, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 423 | ::std::function<void()> fn); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 424 | ~SimulatedTimerHandler() { Disable(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 425 | |
| 426 | void Setup(monotonic_clock::time_point base, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 427 | monotonic_clock::duration repeat_offset) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 428 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 429 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 430 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 431 | void Disable() override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 432 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 433 | private: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 434 | SimulatedEventLoop *simulated_event_loop_; |
| 435 | EventHandler<SimulatedTimerHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 436 | EventScheduler *scheduler_; |
| 437 | EventScheduler::Token token_; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 438 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 439 | monotonic_clock::time_point base_; |
| 440 | monotonic_clock::duration repeat_offset_; |
| 441 | }; |
| 442 | |
| 443 | class SimulatedPhasedLoopHandler : public PhasedLoopHandler { |
| 444 | public: |
| 445 | SimulatedPhasedLoopHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 446 | SimulatedEventLoop *simulated_event_loop, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 447 | ::std::function<void(int)> fn, |
| 448 | const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 449 | const monotonic_clock::duration offset); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 450 | ~SimulatedPhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 451 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 452 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 454 | void Schedule(monotonic_clock::time_point sleep_time) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 455 | |
| 456 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 457 | SimulatedEventLoop *simulated_event_loop_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 458 | EventHandler<SimulatedPhasedLoopHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 459 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 460 | EventScheduler *scheduler_; |
| 461 | EventScheduler::Token token_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
| 464 | class SimulatedEventLoop : public EventLoop { |
| 465 | public: |
| 466 | explicit SimulatedEventLoop( |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 467 | EventScheduler *scheduler, NodeEventLoopFactory *node_event_loop_factory, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 468 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> |
| 469 | *channels, |
| 470 | const Configuration *configuration, |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 471 | std::vector<SimulatedEventLoop *> *event_loops_, const Node *node, |
| 472 | pid_t tid) |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 473 | : EventLoop(CHECK_NOTNULL(configuration)), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 474 | scheduler_(scheduler), |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 475 | node_event_loop_factory_(node_event_loop_factory), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 476 | channels_(channels), |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 477 | event_loops_(event_loops_), |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 478 | node_(node), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 479 | tid_(tid), |
| 480 | startup_tracker_(std::make_shared<StartupTracker>()) { |
| 481 | startup_tracker_->loop = this; |
| 482 | scheduler_->ScheduleOnStartup([startup_tracker = startup_tracker_]() { |
| 483 | if (startup_tracker->loop) { |
| 484 | startup_tracker->loop->Setup(); |
| 485 | startup_tracker->has_setup = true; |
| 486 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 487 | }); |
| 488 | |
| 489 | event_loops_->push_back(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 490 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 491 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 492 | ~SimulatedEventLoop() override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 493 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 494 | // the event loop so the book keeping matches. |
| 495 | timing_report_sender_.reset(); |
| 496 | |
| 497 | // Force everything with a registered fd with epoll to be destroyed now. |
| 498 | timers_.clear(); |
| 499 | phased_loops_.clear(); |
| 500 | watchers_.clear(); |
| 501 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 502 | for (auto it = event_loops_->begin(); it != event_loops_->end(); ++it) { |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 503 | if (*it == this) { |
| 504 | event_loops_->erase(it); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 508 | VLOG(1) << scheduler_->distributed_now() << " " << NodeName(node()) |
| 509 | << monotonic_now() << " ~SimulatedEventLoop(\"" << name_ << "\")"; |
| 510 | startup_tracker_->loop = nullptr; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 513 | void SetIsRunning(bool running) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 514 | VLOG(1) << scheduler_->distributed_now() << " " << NodeName(node()) |
| 515 | << monotonic_now() << " " << name_ << " set_is_running(" << running |
| 516 | << ")"; |
| 517 | CHECK(startup_tracker_->has_setup); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 518 | |
| 519 | set_is_running(running); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 520 | if (running) { |
| 521 | has_run_ = true; |
| 522 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 525 | bool has_run() const { return has_run_; } |
| 526 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 527 | std::chrono::nanoseconds send_delay() const { return send_delay_; } |
| 528 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
| 529 | send_delay_ = send_delay; |
| 530 | } |
| 531 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 532 | monotonic_clock::time_point monotonic_now() override { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 533 | return node_event_loop_factory_->monotonic_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 536 | realtime_clock::time_point realtime_now() override { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 537 | return node_event_loop_factory_->realtime_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 540 | distributed_clock::time_point distributed_now() { |
| 541 | return scheduler_->distributed_now(); |
| 542 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 543 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 544 | std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override; |
| 545 | |
| 546 | std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 547 | |
| 548 | void MakeRawWatcher( |
| 549 | const Channel *channel, |
| 550 | ::std::function<void(const Context &context, const void *message)> |
| 551 | watcher) override; |
| 552 | |
| 553 | TimerHandler *AddTimer(::std::function<void()> callback) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 554 | CHECK(!is_running()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 555 | return NewTimer(::std::unique_ptr<TimerHandler>( |
| 556 | new SimulatedTimerHandler(scheduler_, this, callback))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback, |
| 560 | const monotonic_clock::duration interval, |
| 561 | const monotonic_clock::duration offset = |
| 562 | ::std::chrono::seconds(0)) override { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 563 | return NewPhasedLoop( |
| 564 | ::std::unique_ptr<PhasedLoopHandler>(new SimulatedPhasedLoopHandler( |
| 565 | scheduler_, this, callback, interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void OnRun(::std::function<void()> on_run) override { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 569 | CHECK(!is_running()) << ": Cannot register OnRun callback while running."; |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 570 | scheduler_->ScheduleOnRun([this, on_run = std::move(on_run)]() { |
| 571 | ScopedMarkRealtimeRestorer rt(priority() > 0); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 572 | SetTimerContext(monotonic_now()); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 573 | on_run(); |
| 574 | }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 577 | const Node *node() const override { return node_; } |
| 578 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 579 | void set_name(const std::string_view name) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 580 | name_ = std::string(name); |
| 581 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 582 | const std::string_view name() const override { return name_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 583 | |
| 584 | SimulatedChannel *GetSimulatedChannel(const Channel *channel); |
| 585 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 586 | void SetRuntimeRealtimePriority(int priority) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 587 | CHECK(!is_running()) << ": Cannot set realtime priority while running."; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 588 | priority_ = priority; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 591 | int priority() const override { return priority_; } |
| 592 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 593 | void SetRuntimeAffinity(const cpu_set_t & /*cpuset*/) override { |
| 594 | CHECK(!is_running()) << ": Cannot set affinity while running."; |
| 595 | } |
| 596 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 597 | void Setup() { |
| 598 | MaybeScheduleTimingReports(); |
| 599 | if (!skip_logger_) { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 600 | log_sender_.Initialize(MakeSender<logging::LogMessageFbs>("/aos")); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 601 | log_impl_ = log_sender_.implementation(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 602 | } |
| 603 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 604 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 605 | int NumberBuffers(const Channel *channel) override; |
| 606 | |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 607 | const UUID &boot_uuid() const override { |
| 608 | return node_event_loop_factory_->boot_uuid(); |
| 609 | } |
| 610 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 611 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 612 | friend class SimulatedTimerHandler; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 613 | friend class SimulatedPhasedLoopHandler; |
| 614 | friend class SimulatedWatcher; |
| 615 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 616 | // We have a condition where we register a startup handler, but then get shut |
| 617 | // down before it runs. This results in a segfault if we are lucky, and |
| 618 | // corruption otherwise. To handle that, allocate a small object which points |
| 619 | // back to us and can be freed when the function is freed. That object can |
| 620 | // then be updated when we get destroyed so setup is not called. |
| 621 | struct StartupTracker { |
| 622 | SimulatedEventLoop *loop = nullptr; |
| 623 | bool has_setup = false; |
| 624 | }; |
| 625 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 626 | void HandleEvent() { |
| 627 | while (true) { |
| 628 | if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) { |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | EventLoopEvent *event = PopEvent(); |
| 633 | event->HandleEvent(); |
| 634 | } |
| 635 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 636 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 637 | pid_t GetTid() override { return tid_; } |
| 638 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 639 | EventScheduler *scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 640 | NodeEventLoopFactory *node_event_loop_factory_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 641 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_; |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 642 | std::vector<SimulatedEventLoop *> *event_loops_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 643 | |
| 644 | ::std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 645 | |
| 646 | int priority_ = 0; |
| 647 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 648 | std::chrono::nanoseconds send_delay_; |
| 649 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 650 | const Node *const node_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 651 | const pid_t tid_; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 652 | |
| 653 | AosLogToFbs log_sender_; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 654 | std::shared_ptr<logging::LogImplementation> log_impl_ = nullptr; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 655 | |
| 656 | bool has_run_ = false; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 657 | |
| 658 | std::shared_ptr<StartupTracker> startup_tracker_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 659 | }; |
| 660 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 661 | void SimulatedEventLoopFactory::set_send_delay( |
| 662 | std::chrono::nanoseconds send_delay) { |
| 663 | send_delay_ = send_delay; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 664 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 665 | if (node) { |
| 666 | for (SimulatedEventLoop *loop : node->event_loops_) { |
| 667 | loop->set_send_delay(send_delay_); |
| 668 | } |
| 669 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 673 | void SimulatedEventLoop::MakeRawWatcher( |
| 674 | const Channel *channel, |
| 675 | std::function<void(const Context &channel, const void *message)> watcher) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 676 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 677 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 678 | std::unique_ptr<SimulatedWatcher> shm_watcher = |
| 679 | std::make_unique<SimulatedWatcher>(this, scheduler_, channel, |
| 680 | std::move(watcher)); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 681 | |
| 682 | GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get()); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 683 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 684 | NewWatcher(std::move(shm_watcher)); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 685 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 686 | << " " << name() << " MakeRawWatcher(\"" |
| 687 | << configuration::StrippedChannelToString(channel) << "\")"; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 688 | |
| 689 | // Order of operations gets kinda wonky if we let people make watchers after |
| 690 | // running once. If someone has a valid use case, we can reconsider. |
| 691 | CHECK(!has_run()) << ": Can't add a watcher after running."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 695 | const Channel *channel) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 696 | TakeSender(channel); |
| 697 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 698 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 699 | << " " << name() << " MakeRawSender(\"" |
| 700 | << configuration::StrippedChannelToString(channel) << "\")"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 701 | return GetSimulatedChannel(channel)->MakeRawSender(this); |
| 702 | } |
| 703 | |
| 704 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 705 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 706 | ChannelIndex(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 707 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 708 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 709 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 710 | << "\", \"type\": \"" << channel->type()->string_view() |
| 711 | << "\" } is not able to be fetched on this node. Check your " |
| 712 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 713 | } |
| 714 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 715 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 716 | << " " << name() << " MakeRawFetcher(\"" |
| 717 | << configuration::StrippedChannelToString(channel) << "\")"; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 718 | return GetSimulatedChannel(channel)->MakeRawFetcher(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel( |
| 722 | const Channel *channel) { |
| 723 | auto it = channels_->find(SimpleChannel(channel)); |
| 724 | if (it == channels_->end()) { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 725 | it = |
| 726 | channels_ |
| 727 | ->emplace( |
| 728 | SimpleChannel(channel), |
| 729 | std::unique_ptr<SimulatedChannel>(new SimulatedChannel( |
| 730 | channel, std::chrono::nanoseconds( |
| 731 | configuration()->channel_storage_duration())))) |
| 732 | .first; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 733 | } |
| 734 | return it->second.get(); |
| 735 | } |
| 736 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 737 | int SimulatedEventLoop::NumberBuffers(const Channel *channel) { |
| 738 | return GetSimulatedChannel(channel)->number_buffers(); |
| 739 | } |
| 740 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 741 | SimulatedWatcher::SimulatedWatcher( |
| 742 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 743 | const Channel *channel, |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 744 | std::function<void(const Context &context, const void *message)> fn) |
| 745 | : WatcherState(simulated_event_loop, channel, std::move(fn)), |
| 746 | simulated_event_loop_(simulated_event_loop), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 747 | channel_(channel), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 748 | scheduler_(scheduler), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 749 | event_(this), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 750 | token_(scheduler_->InvalidToken()) { |
| 751 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 752 | << NodeName(simulated_event_loop_->node()) |
| 753 | << simulated_event_loop_->monotonic_now() << " " |
| 754 | << simulated_event_loop_->name() << " Watching " |
| 755 | << configuration::StrippedChannelToString(channel_); |
| 756 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 757 | |
| 758 | SimulatedWatcher::~SimulatedWatcher() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 759 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 760 | << NodeName(simulated_event_loop_->node()) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 761 | << simulated_event_loop_->monotonic_now() << " " |
| 762 | << simulated_event_loop_->name() << " ~Watching " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 763 | << configuration::StrippedChannelToString(channel_); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 764 | simulated_event_loop_->RemoveEvent(&event_); |
| 765 | if (token_ != scheduler_->InvalidToken()) { |
| 766 | scheduler_->Deschedule(token_); |
| 767 | } |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 768 | CHECK_NOTNULL(simulated_channel_)->RemoveWatcher(this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 771 | bool SimulatedWatcher::has_run() const { |
| 772 | return simulated_event_loop_->has_run(); |
| 773 | } |
| 774 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 775 | void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) { |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 776 | monotonic_clock::time_point event_time = |
| 777 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 778 | |
| 779 | // Messages are queued in order. If we are the first, add ourselves. |
| 780 | // Otherwise, don't. |
| 781 | if (msgs_.size() == 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 782 | event_.set_event_time(message->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 783 | simulated_event_loop_->AddEvent(&event_); |
| 784 | |
| 785 | DoSchedule(event_time); |
| 786 | } |
| 787 | |
| 788 | msgs_.emplace_back(message); |
| 789 | } |
| 790 | |
| 791 | void SimulatedWatcher::HandleEvent() { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 792 | const monotonic_clock::time_point monotonic_now = |
| 793 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 794 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 795 | << NodeName(simulated_event_loop_->node()) |
| 796 | << simulated_event_loop_->monotonic_now() << " " |
| 797 | << simulated_event_loop_->name() << " Watcher " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 798 | << configuration::StrippedChannelToString(channel_); |
| 799 | CHECK_NE(msgs_.size(), 0u) << ": No events to handle."; |
| 800 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 801 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 802 | if (simulated_event_loop_->log_impl_) { |
| 803 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 804 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 805 | Context context = msgs_.front()->context; |
| 806 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 807 | if (channel_->read_method() != ReadMethod::PIN) { |
| 808 | context.buffer_index = -1; |
| 809 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 810 | if (context.remote_queue_index == 0xffffffffu) { |
| 811 | context.remote_queue_index = context.queue_index; |
| 812 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 813 | if (context.monotonic_remote_time == monotonic_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 814 | context.monotonic_remote_time = context.monotonic_event_time; |
| 815 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 816 | if (context.realtime_remote_time == realtime_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 817 | context.realtime_remote_time = context.realtime_event_time; |
| 818 | } |
| 819 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 820 | { |
| 821 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 822 | DoCallCallback([monotonic_now]() { return monotonic_now; }, context); |
| 823 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 824 | |
| 825 | msgs_.pop_front(); |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 826 | if (token_ != scheduler_->InvalidToken()) { |
| 827 | scheduler_->Deschedule(token_); |
| 828 | token_ = scheduler_->InvalidToken(); |
| 829 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 830 | if (msgs_.size() != 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 831 | event_.set_event_time(msgs_.front()->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 832 | simulated_event_loop_->AddEvent(&event_); |
| 833 | |
| 834 | DoSchedule(event_.event_time()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) { |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 839 | CHECK(token_ == scheduler_->InvalidToken()) |
| 840 | << ": May not schedule multiple times"; |
| 841 | token_ = scheduler_->Schedule( |
| 842 | event_time + simulated_event_loop_->send_delay(), [this]() { |
| 843 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 844 | token_ = scheduler_->InvalidToken(); |
| 845 | simulated_event_loop_->HandleEvent(); |
| 846 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 850 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 851 | watcher->SetSimulatedChannel(this); |
| 852 | watchers_.emplace_back(watcher); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | ::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender( |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 856 | SimulatedEventLoop *event_loop) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 857 | return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
| 858 | } |
| 859 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 860 | ::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher( |
| 861 | EventLoop *event_loop) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 862 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 863 | ::std::unique_ptr<SimulatedFetcher> fetcher( |
| 864 | new SimulatedFetcher(event_loop, this)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 865 | fetchers_.push_back(fetcher.get()); |
| 866 | return ::std::move(fetcher); |
| 867 | } |
| 868 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 869 | uint32_t SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) { |
| 870 | const uint32_t queue_index = next_queue_index_.index(); |
| 871 | message->context.queue_index = queue_index; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 872 | message->context.data = message->data(channel()->max_size()) + |
| 873 | channel()->max_size() - message->context.size; |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 874 | |
| 875 | DCHECK(channel()->has_schema()) |
| 876 | << ": Missing schema for channel " |
| 877 | << configuration::StrippedChannelToString(channel()); |
| 878 | DCHECK(flatbuffers::Verify( |
| 879 | *channel()->schema(), *channel()->schema()->root_table(), |
| 880 | static_cast<const uint8_t *>(message->context.data), |
| 881 | message->context.size)) |
| 882 | << ": Corrupted flatbuffer on " << channel()->name()->c_str() << " " |
| 883 | << channel()->type()->c_str(); |
| 884 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 885 | next_queue_index_ = next_queue_index_.Increment(); |
| 886 | |
| 887 | latest_message_ = message; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 888 | for (SimulatedWatcher *watcher : watchers_) { |
| 889 | if (watcher->has_run()) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 890 | watcher->Schedule(message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | for (auto &fetcher : fetchers_) { |
| 894 | fetcher->Enqueue(message); |
| 895 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 896 | |
| 897 | return queue_index; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) { |
| 901 | fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 902 | } |
| 903 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 904 | SimulatedSender::SimulatedSender(SimulatedChannel *simulated_channel, |
| 905 | SimulatedEventLoop *event_loop) |
| 906 | : RawSender(event_loop, simulated_channel->channel()), |
| 907 | simulated_channel_(simulated_channel), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 908 | simulated_event_loop_(event_loop) { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 909 | simulated_channel_->CountSenderCreated(); |
| 910 | } |
| 911 | |
| 912 | SimulatedSender::~SimulatedSender() { |
| 913 | simulated_channel_->CountSenderDestroyed(); |
| 914 | } |
| 915 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 916 | bool SimulatedSender::DoSend(size_t length, |
| 917 | monotonic_clock::time_point monotonic_remote_time, |
| 918 | realtime_clock::time_point realtime_remote_time, |
| 919 | uint32_t remote_queue_index, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 920 | const UUID &source_boot_uuid) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 921 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 922 | << NodeName(simulated_event_loop_->node()) |
| 923 | << simulated_event_loop_->monotonic_now() << " " |
| 924 | << simulated_event_loop_->name() << " Send " |
| 925 | << configuration::StrippedChannelToString(channel()); |
| 926 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 927 | // The allocations in here are due to infrastructure and don't count in the |
| 928 | // no mallocs in RT code. |
| 929 | ScopedNotRealtime nrt; |
| 930 | CHECK_LE(length, size()) << ": Attempting to send too big a message."; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 931 | message_->context.monotonic_event_time = |
| 932 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 933 | message_->context.monotonic_remote_time = monotonic_remote_time; |
| 934 | message_->context.remote_queue_index = remote_queue_index; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 935 | message_->context.realtime_event_time = simulated_event_loop_->realtime_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 936 | message_->context.realtime_remote_time = realtime_remote_time; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 937 | message_->context.source_boot_uuid = source_boot_uuid; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 938 | CHECK_LE(length, message_->context.size); |
| 939 | message_->context.size = length; |
| 940 | |
| 941 | // TODO(austin): Track sending too fast. |
| 942 | sent_queue_index_ = simulated_channel_->Send(message_); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 943 | monotonic_sent_time_ = simulated_event_loop_->monotonic_now(); |
| 944 | realtime_sent_time_ = simulated_event_loop_->realtime_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 945 | |
| 946 | // Drop the reference to the message so that we allocate a new message for |
| 947 | // next time. Otherwise we will continue to reuse the same memory for all |
| 948 | // messages and corrupt it. |
| 949 | message_.reset(); |
| 950 | return true; |
| 951 | } |
| 952 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 953 | bool SimulatedSender::DoSend(const void *msg, size_t size, |
| 954 | monotonic_clock::time_point monotonic_remote_time, |
| 955 | realtime_clock::time_point realtime_remote_time, |
| 956 | uint32_t remote_queue_index, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 957 | const UUID &source_boot_uuid) { |
Austin Schuh | 102667e | 2020-12-11 20:13:28 -0800 | [diff] [blame] | 958 | CHECK_LE(size, this->size()) |
| 959 | << ": Attempting to send too big a message on " |
| 960 | << configuration::CleanedChannelToString(simulated_channel_->channel()); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 961 | |
| 962 | // This is wasteful, but since flatbuffers fill from the back end of the |
| 963 | // queue, we need it to be full sized. |
| 964 | message_ = SimulatedMessage::Make(simulated_channel_); |
| 965 | |
| 966 | // Now fill in the message. size is already populated above, and |
| 967 | // queue_index will be populated in simulated_channel_. Put this at the |
| 968 | // back of the data segment. |
| 969 | memcpy(message_->data(simulated_channel_->max_size()) + |
| 970 | simulated_channel_->max_size() - size, |
| 971 | msg, size); |
| 972 | |
| 973 | return DoSend(size, monotonic_remote_time, realtime_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 974 | remote_queue_index, source_boot_uuid); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 975 | } |
| 976 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 977 | SimulatedTimerHandler::SimulatedTimerHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 978 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 979 | ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 980 | : TimerHandler(simulated_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 981 | simulated_event_loop_(simulated_event_loop), |
| 982 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 983 | scheduler_(scheduler), |
| 984 | token_(scheduler_->InvalidToken()) {} |
| 985 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 986 | void SimulatedTimerHandler::Setup(monotonic_clock::time_point base, |
| 987 | monotonic_clock::duration repeat_offset) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 988 | // The allocations in here are due to infrastructure and don't count in the no |
| 989 | // mallocs in RT code. |
| 990 | ScopedNotRealtime nrt; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 991 | Disable(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 992 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 993 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 994 | base_ = base; |
| 995 | repeat_offset_ = repeat_offset; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 996 | token_ = scheduler_->Schedule(std::max(base, monotonic_now), [this]() { |
| 997 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 998 | token_ = scheduler_->InvalidToken(); |
| 999 | simulated_event_loop_->HandleEvent(); |
| 1000 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1001 | event_.set_event_time(base_); |
| 1002 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | void SimulatedTimerHandler::HandleEvent() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1006 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 1007 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1008 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 1009 | << NodeName(simulated_event_loop_->node()) << monotonic_now << " " |
| 1010 | << simulated_event_loop_->name() << " Timer '" << name() << "'"; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1011 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1012 | if (simulated_event_loop_->log_impl_) { |
| 1013 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1014 | } |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1015 | if (token_ != scheduler_->InvalidToken()) { |
| 1016 | scheduler_->Deschedule(token_); |
| 1017 | token_ = scheduler_->InvalidToken(); |
| 1018 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1019 | if (repeat_offset_ != monotonic_clock::zero()) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1020 | // Reschedule. |
| 1021 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1022 | token_ = scheduler_->Schedule(base_, [this]() { |
| 1023 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 1024 | token_ = scheduler_->InvalidToken(); |
| 1025 | simulated_event_loop_->HandleEvent(); |
| 1026 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1027 | event_.set_event_time(base_); |
| 1028 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1029 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1030 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 1031 | { |
| 1032 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 1033 | Call([monotonic_now]() { return monotonic_now; }, monotonic_now); |
| 1034 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1037 | void SimulatedTimerHandler::Disable() { |
| 1038 | simulated_event_loop_->RemoveEvent(&event_); |
| 1039 | if (token_ != scheduler_->InvalidToken()) { |
| 1040 | scheduler_->Deschedule(token_); |
| 1041 | token_ = scheduler_->InvalidToken(); |
| 1042 | } |
| 1043 | } |
| 1044 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1045 | SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1046 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 1047 | ::std::function<void(int)> fn, const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1048 | const monotonic_clock::duration offset) |
| 1049 | : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset), |
| 1050 | simulated_event_loop_(simulated_event_loop), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1051 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1052 | scheduler_(scheduler), |
| 1053 | token_(scheduler_->InvalidToken()) {} |
| 1054 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1055 | SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() { |
| 1056 | if (token_ != scheduler_->InvalidToken()) { |
| 1057 | scheduler_->Deschedule(token_); |
| 1058 | token_ = scheduler_->InvalidToken(); |
| 1059 | } |
| 1060 | simulated_event_loop_->RemoveEvent(&event_); |
| 1061 | } |
| 1062 | |
| 1063 | void SimulatedPhasedLoopHandler::HandleEvent() { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1064 | monotonic_clock::time_point monotonic_now = |
| 1065 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1066 | VLOG(1) << monotonic_now << " Phased loop " << simulated_event_loop_->name() |
| 1067 | << ", " << name(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1068 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1069 | if (simulated_event_loop_->log_impl_) { |
| 1070 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1071 | } |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 1072 | |
| 1073 | { |
| 1074 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 1075 | Call([monotonic_now]() { return monotonic_now; }, |
| 1076 | [this](monotonic_clock::time_point sleep_time) { |
| 1077 | Schedule(sleep_time); |
| 1078 | }); |
| 1079 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1080 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1081 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1082 | void SimulatedPhasedLoopHandler::Schedule( |
| 1083 | monotonic_clock::time_point sleep_time) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 1084 | // The allocations in here are due to infrastructure and don't count in the no |
| 1085 | // mallocs in RT code. |
| 1086 | ScopedNotRealtime nrt; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1087 | if (token_ != scheduler_->InvalidToken()) { |
| 1088 | scheduler_->Deschedule(token_); |
| 1089 | token_ = scheduler_->InvalidToken(); |
| 1090 | } |
| 1091 | token_ = scheduler_->Schedule(sleep_time, [this]() { |
| 1092 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 1093 | token_ = scheduler_->InvalidToken(); |
| 1094 | simulated_event_loop_->HandleEvent(); |
| 1095 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1096 | event_.set_event_time(sleep_time); |
| 1097 | simulated_event_loop_->AddEvent(&event_); |
| 1098 | } |
| 1099 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1100 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 1101 | const Configuration *configuration) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1102 | : configuration_(CHECK_NOTNULL(configuration)), |
| 1103 | nodes_(configuration::GetNodes(configuration_)) { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 1104 | CHECK(IsInitialized()) << ": Need to initialize AOS first."; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1105 | for (const Node *node : nodes_) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1106 | node_factories_.emplace_back( |
| 1107 | new NodeEventLoopFactory(&scheduler_scheduler_, this, node)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1108 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1109 | |
| 1110 | if (configuration::MultiNode(configuration)) { |
| 1111 | bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this); |
| 1112 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1115 | SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {} |
| 1116 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1117 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1118 | std::string_view node) { |
| 1119 | return GetNodeEventLoopFactory(configuration::GetNode(configuration(), node)); |
| 1120 | } |
| 1121 | |
| 1122 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1123 | const Node *node) { |
| 1124 | auto result = std::find_if( |
| 1125 | node_factories_.begin(), node_factories_.end(), |
| 1126 | [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) { |
| 1127 | return node_factory->node() == node; |
| 1128 | }); |
| 1129 | |
| 1130 | CHECK(result != node_factories_.end()) |
| 1131 | << ": Failed to find node " << FlatbufferToJson(node); |
| 1132 | |
| 1133 | return result->get(); |
| 1134 | } |
| 1135 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1136 | void SimulatedEventLoopFactory::SetTimeConverter( |
| 1137 | TimeConverter *time_converter) { |
| 1138 | for (std::unique_ptr<NodeEventLoopFactory> &factory : node_factories_) { |
| 1139 | factory->SetTimeConverter(time_converter); |
| 1140 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1141 | scheduler_scheduler_.SetTimeConverter(time_converter); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 1144 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1145 | std::string_view name, const Node *node) { |
| 1146 | if (node == nullptr) { |
| 1147 | CHECK(!configuration::MultiNode(configuration())) |
| 1148 | << ": Can't make a single node event loop in a multi-node world."; |
| 1149 | } else { |
| 1150 | CHECK(configuration::MultiNode(configuration())) |
| 1151 | << ": Can't make a multi-node event loop in a single-node world."; |
| 1152 | } |
| 1153 | return GetNodeEventLoopFactory(node)->MakeEventLoop(name); |
| 1154 | } |
| 1155 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1156 | NodeEventLoopFactory::NodeEventLoopFactory( |
| 1157 | EventSchedulerScheduler *scheduler_scheduler, |
| 1158 | SimulatedEventLoopFactory *factory, const Node *node) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1159 | : scheduler_(configuration::GetNodeIndex(factory->configuration(), node)), |
| 1160 | factory_(factory), |
| 1161 | node_(node) { |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1162 | scheduler_scheduler->AddEventScheduler(&scheduler_); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1163 | scheduler_.set_started([this]() { |
| 1164 | started_ = true; |
| 1165 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1166 | event_loop->SetIsRunning(true); |
| 1167 | } |
| 1168 | }); |
| 1169 | scheduler_.set_on_shutdown([this]() { |
| 1170 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node()) |
| 1171 | << monotonic_now() << " Shutting down node."; |
| 1172 | Shutdown(); |
| 1173 | ScheduleStartup(); |
| 1174 | }); |
| 1175 | ScheduleStartup(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | NodeEventLoopFactory::~NodeEventLoopFactory() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1179 | if (started_) { |
| 1180 | for (std::function<void()> &fn : on_shutdown_) { |
| 1181 | fn(); |
| 1182 | } |
| 1183 | |
| 1184 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1185 | << monotonic_now() << " Shutting down applications."; |
| 1186 | applications_.clear(); |
| 1187 | started_ = false; |
| 1188 | } |
| 1189 | |
| 1190 | if (event_loops_.size() != 0u) { |
| 1191 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1192 | LOG(ERROR) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1193 | << monotonic_now() << " Event loop '" << event_loop->name() |
| 1194 | << "' failed to shut down"; |
| 1195 | } |
| 1196 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1197 | CHECK_EQ(event_loops_.size(), 0u) << "Event loop didn't exit"; |
| 1198 | } |
| 1199 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1200 | void NodeEventLoopFactory::OnStartup(std::function<void()> &&fn) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1201 | CHECK(!scheduler_.is_running()) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1202 | << ": Can only register OnStartup handlers when not running."; |
| 1203 | on_startup_.emplace_back(std::move(fn)); |
| 1204 | if (started_) { |
| 1205 | size_t on_startup_index = on_startup_.size() - 1; |
| 1206 | scheduler_.ScheduleOnStartup( |
| 1207 | [this, on_startup_index]() { on_startup_[on_startup_index](); }); |
| 1208 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1211 | void NodeEventLoopFactory::OnShutdown(std::function<void()> &&fn) { |
| 1212 | on_shutdown_.emplace_back(std::move(fn)); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1213 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1214 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1215 | void NodeEventLoopFactory::ScheduleStartup() { |
| 1216 | scheduler_.ScheduleOnStartup([this]() { |
| 1217 | UUID next_uuid = scheduler_.boot_uuid(); |
| 1218 | if (boot_uuid_ != next_uuid) { |
| 1219 | CHECK_EQ(boot_uuid_, UUID::Zero()); |
| 1220 | boot_uuid_ = next_uuid; |
| 1221 | } |
| 1222 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node()) |
| 1223 | << monotonic_now() << " Starting up node on boot " << boot_uuid_; |
| 1224 | Startup(); |
| 1225 | }); |
| 1226 | } |
| 1227 | |
| 1228 | void NodeEventLoopFactory::Startup() { |
| 1229 | CHECK(!started_); |
| 1230 | for (size_t i = 0; i < on_startup_.size(); ++i) { |
| 1231 | on_startup_[i](); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | void NodeEventLoopFactory::Shutdown() { |
| 1236 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1237 | event_loop->SetIsRunning(false); |
| 1238 | } |
| 1239 | |
| 1240 | CHECK(started_); |
| 1241 | started_ = false; |
| 1242 | for (std::function<void()> &fn : on_shutdown_) { |
| 1243 | fn(); |
| 1244 | } |
| 1245 | |
| 1246 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1247 | << monotonic_now() << " Shutting down applications."; |
| 1248 | applications_.clear(); |
| 1249 | |
| 1250 | if (event_loops_.size() != 0u) { |
| 1251 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1252 | LOG(ERROR) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1253 | << monotonic_now() << " Event loop '" << event_loop->name() |
| 1254 | << "' failed to shut down"; |
| 1255 | } |
| 1256 | } |
| 1257 | CHECK_EQ(event_loops_.size(), 0u) << "Not all event loops shut down"; |
| 1258 | boot_uuid_ = UUID::Zero(); |
| 1259 | |
| 1260 | channels_.clear(); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1263 | void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1264 | // This sets running to true too. |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1265 | scheduler_scheduler_.RunOnStartup(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1266 | scheduler_scheduler_.RunFor(duration); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1267 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
| 1268 | if (node) { |
| 1269 | for (SimulatedEventLoop *loop : node->event_loops_) { |
| 1270 | loop->SetIsRunning(false); |
| 1271 | } |
| 1272 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | void SimulatedEventLoopFactory::Run() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1277 | // This sets running to true too. |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1278 | scheduler_scheduler_.RunOnStartup(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1279 | scheduler_scheduler_.Run(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1280 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
| 1281 | if (node) { |
| 1282 | for (SimulatedEventLoop *loop : node->event_loops_) { |
| 1283 | loop->SetIsRunning(false); |
| 1284 | } |
| 1285 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1289 | void SimulatedEventLoopFactory::Exit() { scheduler_scheduler_.Exit(); } |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1290 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1291 | void SimulatedEventLoopFactory::DisableForwarding(const Channel *channel) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1292 | CHECK(bridge_) << ": Can't disable forwarding without a message bridge."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1293 | bridge_->DisableForwarding(channel); |
| 1294 | } |
| 1295 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1296 | void SimulatedEventLoopFactory::DisableStatistics() { |
| 1297 | CHECK(bridge_) << ": Can't disable statistics without a message bridge."; |
| 1298 | bridge_->DisableStatistics(); |
| 1299 | } |
| 1300 | |
Austin Schuh | 2928ebe | 2021-02-07 22:10:27 -0800 | [diff] [blame] | 1301 | void SimulatedEventLoopFactory::SkipTimingReport() { |
| 1302 | CHECK(bridge_) << ": Can't skip timing reports without a message bridge."; |
| 1303 | bridge_->SkipTimingReport(); |
| 1304 | } |
| 1305 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame^] | 1306 | ::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop( |
| 1307 | std::string_view name) { |
| 1308 | CHECK(!scheduler_.is_running() || !started_) |
| 1309 | << ": Can't create an event loop while running"; |
| 1310 | |
| 1311 | pid_t tid = tid_; |
| 1312 | ++tid_; |
| 1313 | ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop( |
| 1314 | &scheduler_, this, &channels_, factory_->configuration(), &event_loops_, |
| 1315 | node_, tid)); |
| 1316 | result->set_name(name); |
| 1317 | result->set_send_delay(factory_->send_delay()); |
| 1318 | |
| 1319 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1320 | << monotonic_now() << " MakeEventLoop(\"" << result->name() << "\")"; |
| 1321 | return std::move(result); |
| 1322 | } |
| 1323 | |
| 1324 | void NodeEventLoopFactory::Disconnect(const Node *other) { |
| 1325 | factory_->bridge_->Disconnect(node_, other); |
| 1326 | } |
| 1327 | |
| 1328 | void NodeEventLoopFactory::Connect(const Node *other) { |
| 1329 | factory_->bridge_->Connect(node_, other); |
| 1330 | } |
| 1331 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1332 | } // namespace aos |