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> |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 5 | #include <optional> |
| 6 | #include <queue> |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 7 | #include <string_view> |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 8 | #include <vector> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | |
| 10 | #include "absl/container/btree_map.h" |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 11 | #include "aos/events/aos_logging.h" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 12 | #include "aos/events/simulated_network_bridge.h" |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 13 | #include "aos/init.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 15 | #include "aos/realtime.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | #include "aos/util/phased_loop.h" |
| 17 | |
| 18 | namespace aos { |
| 19 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 20 | class SimulatedEventLoop; |
| 21 | class SimulatedFetcher; |
| 22 | class SimulatedChannel; |
| 23 | |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 24 | using CheckSentTooFast = NodeEventLoopFactory::CheckSentTooFast; |
| 25 | using ExclusiveSenders = NodeEventLoopFactory::ExclusiveSenders; |
| 26 | using EventLoopOptions = NodeEventLoopFactory::EventLoopOptions; |
| 27 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 30 | std::string NodeName(const Node *node) { |
| 31 | if (node == nullptr) { |
| 32 | return ""; |
| 33 | } |
| 34 | |
| 35 | return absl::StrCat(node->name()->string_view(), " "); |
| 36 | } |
| 37 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 38 | class ScopedMarkRealtimeRestorer { |
| 39 | public: |
| 40 | ScopedMarkRealtimeRestorer(bool rt) : rt_(rt), prior_(MarkRealtime(rt)) {} |
| 41 | ~ScopedMarkRealtimeRestorer() { CHECK_EQ(rt_, MarkRealtime(prior_)); } |
| 42 | |
| 43 | private: |
| 44 | const bool rt_; |
| 45 | const bool prior_; |
| 46 | }; |
| 47 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 48 | // Holds storage for a span object and the data referenced by that span for |
| 49 | // compatibility with RawSender::SharedSpan users. If constructed with |
| 50 | // MakeSharedSpan, span points to only the aligned segment of the entire data. |
| 51 | struct AlignedOwningSpan { |
| 52 | AlignedOwningSpan(const AlignedOwningSpan &) = delete; |
| 53 | AlignedOwningSpan &operator=(const AlignedOwningSpan &) = delete; |
| 54 | absl::Span<const uint8_t> span; |
| 55 | char data[]; |
| 56 | }; |
| 57 | |
| 58 | // Constructs a span which owns its data through a shared_ptr. The owning span |
| 59 | // points to a const view of the data; also returns a temporary mutable span |
| 60 | // which is only valid while the const shared span is kept alive. |
| 61 | std::pair<RawSender::SharedSpan, absl::Span<uint8_t>> MakeSharedSpan( |
| 62 | size_t size) { |
| 63 | AlignedOwningSpan *const span = reinterpret_cast<AlignedOwningSpan *>( |
| 64 | malloc(sizeof(AlignedOwningSpan) + size + kChannelDataAlignment - 1)); |
| 65 | |
| 66 | absl::Span mutable_span( |
| 67 | reinterpret_cast<uint8_t *>(RoundChannelData(&span->data[0], size)), |
| 68 | size); |
| 69 | new (span) AlignedOwningSpan{.span = mutable_span}; |
| 70 | |
| 71 | return std::make_pair( |
| 72 | RawSender::SharedSpan( |
| 73 | std::shared_ptr<AlignedOwningSpan>(span, |
| 74 | [](AlignedOwningSpan *s) { |
| 75 | s->~AlignedOwningSpan(); |
| 76 | free(s); |
| 77 | }), |
| 78 | &span->span), |
| 79 | mutable_span); |
| 80 | } |
| 81 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 82 | // Container for both a message, and the context for it for simulation. This |
| 83 | // makes tracking the timestamps associated with the data easy. |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 84 | struct SimulatedMessage final { |
| 85 | SimulatedMessage(const SimulatedMessage &) = delete; |
| 86 | SimulatedMessage &operator=(const SimulatedMessage &) = delete; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 87 | ~SimulatedMessage(); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 88 | |
| 89 | // Creates a SimulatedMessage with size bytes of storage. |
| 90 | // This is a shared_ptr so we don't have to implement refcounting or copying. |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 91 | static std::shared_ptr<SimulatedMessage> Make( |
| 92 | SimulatedChannel *channel, const RawSender::SharedSpan data); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 93 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | // Context for the data. |
| 95 | Context context; |
| 96 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 97 | SimulatedChannel *const channel = nullptr; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 98 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 99 | // Owning span to this message's data. Depending on the sender may either |
| 100 | // represent the data of just the flatbuffer, or max channel size. |
| 101 | RawSender::SharedSpan data; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 102 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 103 | // Mutable view of above data. If empty, this message is not mutable. |
| 104 | absl::Span<uint8_t> mutable_data; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 105 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 106 | // Determines whether this message is mutable. Used for Send where the user |
| 107 | // fills out a message stored internally then gives us the size of data used. |
| 108 | bool is_mutable() const { return data->size() == mutable_data.size(); } |
| 109 | |
| 110 | // Note: this should be private but make_shared requires it to be public. Use |
| 111 | // Make() above to construct. |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 112 | SimulatedMessage(SimulatedChannel *channel_in); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 115 | } // namespace |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 116 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 117 | // TODO(Brian): This should be in the anonymous namespace, but that annoys GCC |
| 118 | // for some reason... |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 119 | class SimulatedWatcher : public WatcherState, public EventScheduler::Event { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 120 | public: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 121 | SimulatedWatcher( |
| 122 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
| 123 | const Channel *channel, |
| 124 | std::function<void(const Context &context, const void *message)> fn); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 125 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 126 | ~SimulatedWatcher() override; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 127 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 128 | bool has_run() const; |
| 129 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 130 | void Handle() noexcept override; |
| 131 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 132 | void Startup(EventLoop * /*event_loop*/) override {} |
| 133 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 134 | void Schedule(std::shared_ptr<SimulatedMessage> message); |
| 135 | |
Austin Schuh | f4b09c7 | 2021-12-08 12:04:37 -0800 | [diff] [blame] | 136 | void HandleEvent() noexcept; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 137 | |
| 138 | void SetSimulatedChannel(SimulatedChannel *channel) { |
| 139 | simulated_channel_ = channel; |
| 140 | } |
| 141 | |
| 142 | private: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 143 | void DoSchedule(monotonic_clock::time_point event_time); |
| 144 | |
| 145 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
| 146 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 147 | SimulatedEventLoop *const simulated_event_loop_; |
| 148 | const Channel *const channel_; |
| 149 | EventScheduler *const scheduler_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 150 | EventHandler<SimulatedWatcher> event_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 151 | EventScheduler::Token token_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 152 | SimulatedChannel *simulated_channel_ = nullptr; |
| 153 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | |
| 155 | class SimulatedChannel { |
| 156 | public: |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 157 | explicit SimulatedChannel(const Channel *channel, |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 158 | std::chrono::nanoseconds channel_storage_duration, |
| 159 | const EventScheduler *scheduler) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 160 | : channel_(channel), |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 161 | channel_storage_duration_(channel_storage_duration), |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 162 | next_queue_index_(ipc_lib::QueueIndex::Zero(number_buffers())), |
| 163 | scheduler_(scheduler) { |
Brian Silverman | bc596c6 | 2021-10-15 14:04:54 -0700 | [diff] [blame] | 164 | available_buffer_indices_.resize(number_buffers()); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 165 | for (int i = 0; i < number_buffers(); ++i) { |
Brian Silverman | bc596c6 | 2021-10-15 14:04:54 -0700 | [diff] [blame] | 166 | available_buffer_indices_[i] = i; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 169 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 170 | ~SimulatedChannel() { |
| 171 | latest_message_.reset(); |
| 172 | CHECK_EQ(static_cast<size_t>(number_buffers()), |
| 173 | available_buffer_indices_.size()); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 174 | CHECK_EQ(0u, fetchers_.size()) |
| 175 | << configuration::StrippedChannelToString(channel()); |
| 176 | CHECK_EQ(0u, watchers_.size()) |
| 177 | << configuration::StrippedChannelToString(channel()); |
| 178 | CHECK_EQ(0, sender_count_) |
| 179 | << configuration::StrippedChannelToString(channel()); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // The number of messages we pretend to have in the queue. |
| 183 | int queue_size() const { |
| 184 | return channel()->frequency() * |
| 185 | std::chrono::duration_cast<std::chrono::duration<double>>( |
| 186 | channel_storage_duration_) |
| 187 | .count(); |
| 188 | } |
| 189 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 190 | std::chrono::nanoseconds channel_storage_duration() const { |
| 191 | return channel_storage_duration_; |
| 192 | } |
| 193 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 194 | // The number of extra buffers (beyond the queue) we pretend to have. |
| 195 | int number_scratch_buffers() const { |
| 196 | // We need to start creating messages before we know how many |
| 197 | // senders+readers we'll have, so we need to just pick something which is |
| 198 | // always big enough. |
| 199 | return 50; |
| 200 | } |
| 201 | |
| 202 | int number_buffers() const { return queue_size() + number_scratch_buffers(); } |
| 203 | |
| 204 | int GetBufferIndex() { |
| 205 | CHECK(!available_buffer_indices_.empty()) << ": This should be impossible"; |
| 206 | const int result = available_buffer_indices_.back(); |
| 207 | available_buffer_indices_.pop_back(); |
| 208 | return result; |
| 209 | } |
| 210 | |
| 211 | void FreeBufferIndex(int i) { |
Austin Schuh | c5047ea | 2021-03-20 22:00:21 -0700 | [diff] [blame] | 212 | // This extra checking has a large performance hit with sanitizers that |
| 213 | // track memory accesses, so just skip it. |
| 214 | #if !__has_feature(memory_sanitizer) && !__has_feature(address_sanitizer) |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 215 | DCHECK(std::find(available_buffer_indices_.begin(), |
| 216 | available_buffer_indices_.end(), |
| 217 | i) == available_buffer_indices_.end()) |
| 218 | << ": Buffer is not in use: " << i; |
Brian Silverman | f3e6df2 | 2021-01-19 15:02:21 -0800 | [diff] [blame] | 219 | #endif |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 220 | available_buffer_indices_.push_back(i); |
| 221 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 222 | |
| 223 | // Makes a connected raw sender which calls Send below. |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 224 | ::std::unique_ptr<RawSender> MakeRawSender(SimulatedEventLoop *event_loop); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 225 | |
| 226 | // Makes a connected raw fetcher. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 227 | ::std::unique_ptr<RawFetcher> MakeRawFetcher(EventLoop *event_loop); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 228 | |
| 229 | // Registers a watcher for the queue. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 230 | void MakeRawWatcher(SimulatedWatcher *watcher); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 231 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 232 | void RemoveWatcher(SimulatedWatcher *watcher) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 233 | watchers_.erase(std::find(watchers_.begin(), watchers_.end(), watcher)); |
| 234 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 235 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 236 | // Sends the message to all the connected receivers and fetchers. Returns the |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 237 | // sent queue index, or std::nullopt if messages were sent too fast. |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 238 | std::optional<uint32_t> Send(std::shared_ptr<SimulatedMessage> message, |
| 239 | CheckSentTooFast check_sent_too_fast); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 240 | |
| 241 | // Unregisters a fetcher. |
| 242 | void UnregisterFetcher(SimulatedFetcher *fetcher); |
| 243 | |
| 244 | std::shared_ptr<SimulatedMessage> latest_message() { return latest_message_; } |
| 245 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 246 | size_t max_size() const { return channel()->max_size(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 247 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 248 | const std::string_view name() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 249 | return channel()->name()->string_view(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 252 | const Channel *channel() const { return channel_; } |
| 253 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 254 | void CountSenderCreated() { |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 255 | CheckBufferCount(); |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 256 | if (sender_count_ >= channel()->num_senders()) { |
| 257 | LOG(FATAL) << "Failed to create sender on " |
| 258 | << configuration::CleanedChannelToString(channel()) |
| 259 | << ", too many senders."; |
| 260 | } |
| 261 | ++sender_count_; |
| 262 | } |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 263 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 264 | void CountSenderDestroyed() { |
| 265 | --sender_count_; |
| 266 | CHECK_GE(sender_count_, 0); |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 267 | if (sender_count_ == 0) { |
| 268 | allow_new_senders_ = true; |
| 269 | } |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 272 | private: |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 273 | void CheckBufferCount() { |
| 274 | int reader_count = 0; |
| 275 | if (channel()->read_method() == ReadMethod::PIN) { |
| 276 | reader_count = watchers_.size() + fetchers_.size(); |
| 277 | } |
| 278 | CHECK_LT(reader_count + sender_count_, number_scratch_buffers()); |
| 279 | } |
| 280 | |
| 281 | void CheckReaderCount() { |
| 282 | if (channel()->read_method() != ReadMethod::PIN) { |
| 283 | return; |
| 284 | } |
| 285 | CheckBufferCount(); |
| 286 | const int reader_count = watchers_.size() + fetchers_.size(); |
| 287 | if (reader_count >= channel()->num_readers()) { |
| 288 | LOG(FATAL) << "Failed to create reader on " |
| 289 | << configuration::CleanedChannelToString(channel()) |
| 290 | << ", too many readers."; |
| 291 | } |
| 292 | } |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 293 | |
| 294 | const Channel *const channel_; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 295 | const std::chrono::nanoseconds channel_storage_duration_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 296 | |
| 297 | // List of all watchers. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 298 | ::std::vector<SimulatedWatcher *> watchers_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 299 | |
| 300 | // List of all fetchers. |
| 301 | ::std::vector<SimulatedFetcher *> fetchers_; |
| 302 | std::shared_ptr<SimulatedMessage> latest_message_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 303 | |
| 304 | ipc_lib::QueueIndex next_queue_index_; |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 305 | |
| 306 | int sender_count_ = 0; |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 307 | // Used to track when an exclusive sender has been created (e.g., for log |
| 308 | // replay) and we want to prevent new senders from being accidentally created. |
| 309 | bool allow_new_senders_ = true; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 310 | |
| 311 | std::vector<uint16_t> available_buffer_indices_; |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 312 | |
| 313 | const EventScheduler *scheduler_; |
| 314 | |
| 315 | // Queue of all the message send times in the last channel_storage_duration_ |
| 316 | std::queue<monotonic_clock::time_point> last_times_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | namespace { |
| 320 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 321 | std::shared_ptr<SimulatedMessage> SimulatedMessage::Make( |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 322 | SimulatedChannel *channel, RawSender::SharedSpan data) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 323 | // The allocations in here are due to infrastructure and don't count in the no |
| 324 | // mallocs in RT code. |
| 325 | ScopedNotRealtime nrt; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 326 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 327 | auto message = std::make_shared<SimulatedMessage>(channel); |
| 328 | message->context.size = data->size(); |
| 329 | message->context.data = data->data(); |
| 330 | message->data = std::move(data); |
| 331 | |
| 332 | return message; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | SimulatedMessage::SimulatedMessage(SimulatedChannel *channel_in) |
| 336 | : channel(channel_in) { |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 337 | context.buffer_index = channel->GetBufferIndex(); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | SimulatedMessage::~SimulatedMessage() { |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 341 | channel->FreeBufferIndex(context.buffer_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | class SimulatedSender : public RawSender { |
| 345 | public: |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 346 | SimulatedSender(SimulatedChannel *simulated_channel, |
| 347 | SimulatedEventLoop *event_loop); |
| 348 | ~SimulatedSender() override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 349 | |
| 350 | void *data() override { |
| 351 | if (!message_) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 352 | auto [span, mutable_span] = |
| 353 | MakeSharedSpan(simulated_channel_->max_size()); |
| 354 | message_ = SimulatedMessage::Make(simulated_channel_, span); |
| 355 | message_->mutable_data = mutable_span; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 356 | } |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 357 | CHECK(message_->is_mutable()); |
| 358 | return message_->mutable_data.data(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | size_t size() override { return simulated_channel_->max_size(); } |
| 362 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 363 | Error DoSend(size_t length, monotonic_clock::time_point monotonic_remote_time, |
| 364 | realtime_clock::time_point realtime_remote_time, |
| 365 | uint32_t remote_queue_index, |
| 366 | const UUID &source_boot_uuid) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 367 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 368 | Error DoSend(const void *msg, size_t size, |
| 369 | monotonic_clock::time_point monotonic_remote_time, |
| 370 | realtime_clock::time_point realtime_remote_time, |
| 371 | uint32_t remote_queue_index, |
| 372 | const UUID &source_boot_uuid) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 373 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 374 | Error DoSend(const SharedSpan data, |
| 375 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 376 | aos::realtime_clock::time_point realtime_remote_time, |
| 377 | uint32_t remote_queue_index, |
| 378 | const UUID &source_boot_uuid) override; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 379 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 380 | int buffer_index() override { |
| 381 | // First, ensure message_ is allocated. |
| 382 | data(); |
| 383 | return message_->context.buffer_index; |
| 384 | } |
| 385 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 386 | private: |
| 387 | SimulatedChannel *simulated_channel_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 388 | SimulatedEventLoop *simulated_event_loop_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 389 | |
| 390 | std::shared_ptr<SimulatedMessage> message_; |
| 391 | }; |
| 392 | } // namespace |
| 393 | |
| 394 | class SimulatedFetcher : public RawFetcher { |
| 395 | public: |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 396 | explicit SimulatedFetcher(EventLoop *event_loop, |
| 397 | SimulatedChannel *simulated_channel) |
| 398 | : RawFetcher(event_loop, simulated_channel->channel()), |
| 399 | simulated_channel_(simulated_channel) {} |
| 400 | ~SimulatedFetcher() { simulated_channel_->UnregisterFetcher(this); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 401 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 402 | std::pair<bool, monotonic_clock::time_point> DoFetchNext() override { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 403 | // The allocations in here are due to infrastructure and don't count in the |
| 404 | // no mallocs in RT code. |
| 405 | ScopedNotRealtime nrt; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 406 | if (msgs_.size() == 0) { |
| 407 | return std::make_pair(false, monotonic_clock::min_time); |
| 408 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 409 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 410 | CHECK(!fell_behind_) << ": Got behind on " |
| 411 | << configuration::StrippedChannelToString( |
| 412 | simulated_channel_->channel()); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 413 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 414 | SetMsg(msgs_.front()); |
| 415 | msgs_.pop_front(); |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 416 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 419 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 420 | // The allocations in here are due to infrastructure and don't count in the |
| 421 | // no mallocs in RT code. |
| 422 | ScopedNotRealtime nrt; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 423 | if (msgs_.size() == 0) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 424 | // TODO(austin): Can we just do this logic unconditionally? It is a lot |
| 425 | // simpler. And call clear, obviously. |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 426 | if (!msg_ && simulated_channel_->latest_message()) { |
| 427 | SetMsg(simulated_channel_->latest_message()); |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 428 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 429 | } else { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 430 | return std::make_pair(false, monotonic_clock::min_time); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | // We've had a message enqueued, so we don't need to go looking for the |
| 435 | // latest message from before we started. |
| 436 | SetMsg(msgs_.back()); |
| 437 | msgs_.clear(); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 438 | fell_behind_ = false; |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 439 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | private: |
| 443 | friend class SimulatedChannel; |
| 444 | |
| 445 | // Updates the state inside RawFetcher to point to the data in msg_. |
| 446 | void SetMsg(std::shared_ptr<SimulatedMessage> msg) { |
Austin Schuh | e6f4c8d | 2021-12-11 12:36:06 -0800 | [diff] [blame] | 447 | msg_ = std::move(msg); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 448 | context_ = msg_->context; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 449 | if (channel()->read_method() != ReadMethod::PIN) { |
| 450 | context_.buffer_index = -1; |
| 451 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 452 | if (context_.remote_queue_index == 0xffffffffu) { |
| 453 | context_.remote_queue_index = context_.queue_index; |
| 454 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 455 | if (context_.monotonic_remote_time == monotonic_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 456 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 457 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 458 | if (context_.realtime_remote_time == realtime_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 459 | context_.realtime_remote_time = context_.realtime_event_time; |
| 460 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // Internal method for Simulation to add a message to the buffer. |
| 464 | void Enqueue(std::shared_ptr<SimulatedMessage> buffer) { |
Austin Schuh | e6f4c8d | 2021-12-11 12:36:06 -0800 | [diff] [blame] | 465 | msgs_.emplace_back(std::move(buffer)); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 466 | if (fell_behind_ || |
| 467 | msgs_.size() > static_cast<size_t>(simulated_channel_->queue_size())) { |
| 468 | fell_behind_ = true; |
| 469 | // Might as well empty out all the intermediate messages now. |
| 470 | while (msgs_.size() > 1) { |
| 471 | msgs_.pop_front(); |
| 472 | } |
| 473 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 476 | SimulatedChannel *simulated_channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 477 | std::shared_ptr<SimulatedMessage> msg_; |
| 478 | |
| 479 | // Messages queued up but not in use. |
| 480 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 481 | |
| 482 | // Whether we're currently "behind", which means a FetchNext call will fail. |
| 483 | bool fell_behind_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 484 | }; |
| 485 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 486 | class SimulatedTimerHandler : public TimerHandler, |
| 487 | public EventScheduler::Event { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 488 | public: |
| 489 | explicit SimulatedTimerHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 490 | SimulatedEventLoop *simulated_event_loop, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 491 | ::std::function<void()> fn); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 492 | ~SimulatedTimerHandler() { Disable(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 493 | |
| 494 | void Setup(monotonic_clock::time_point base, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 495 | monotonic_clock::duration repeat_offset) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 496 | |
Austin Schuh | f4b09c7 | 2021-12-08 12:04:37 -0800 | [diff] [blame] | 497 | void HandleEvent() noexcept; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 498 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 499 | void Handle() noexcept override; |
| 500 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 501 | void Disable() override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 502 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 503 | private: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 504 | SimulatedEventLoop *simulated_event_loop_; |
| 505 | EventHandler<SimulatedTimerHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 506 | EventScheduler *scheduler_; |
| 507 | EventScheduler::Token token_; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 508 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 509 | monotonic_clock::time_point base_; |
| 510 | monotonic_clock::duration repeat_offset_; |
| 511 | }; |
| 512 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 513 | class SimulatedPhasedLoopHandler : public PhasedLoopHandler, |
| 514 | public EventScheduler::Event { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 515 | public: |
| 516 | SimulatedPhasedLoopHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 517 | SimulatedEventLoop *simulated_event_loop, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 518 | ::std::function<void(int)> fn, |
| 519 | const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 520 | const monotonic_clock::duration offset); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 521 | ~SimulatedPhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 522 | |
Austin Schuh | f4b09c7 | 2021-12-08 12:04:37 -0800 | [diff] [blame] | 523 | void HandleEvent() noexcept; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 524 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 525 | void Schedule(monotonic_clock::time_point sleep_time) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 526 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 527 | void Handle() noexcept override; |
| 528 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 529 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 530 | SimulatedEventLoop *simulated_event_loop_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 531 | EventHandler<SimulatedPhasedLoopHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 532 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 533 | EventScheduler *scheduler_; |
| 534 | EventScheduler::Token token_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 535 | }; |
| 536 | |
| 537 | class SimulatedEventLoop : public EventLoop { |
| 538 | public: |
| 539 | explicit SimulatedEventLoop( |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 540 | EventScheduler *scheduler, NodeEventLoopFactory *node_event_loop_factory, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 541 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> |
| 542 | *channels, |
| 543 | const Configuration *configuration, |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 544 | std::vector<SimulatedEventLoop *> *event_loops_, const Node *node, |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 545 | pid_t tid, EventLoopOptions options) |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 546 | : EventLoop(CHECK_NOTNULL(configuration)), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 547 | scheduler_(scheduler), |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 548 | node_event_loop_factory_(node_event_loop_factory), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 549 | channels_(channels), |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 550 | event_loops_(event_loops_), |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 551 | node_(node), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 552 | tid_(tid), |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 553 | startup_tracker_(std::make_shared<StartupTracker>()), |
| 554 | options_(options) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 555 | startup_tracker_->loop = this; |
| 556 | scheduler_->ScheduleOnStartup([startup_tracker = startup_tracker_]() { |
| 557 | if (startup_tracker->loop) { |
| 558 | startup_tracker->loop->Setup(); |
| 559 | startup_tracker->has_setup = true; |
| 560 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 561 | }); |
| 562 | |
| 563 | event_loops_->push_back(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 564 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 565 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 566 | ~SimulatedEventLoop() override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 567 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 568 | // the event loop so the book keeping matches. |
| 569 | timing_report_sender_.reset(); |
| 570 | |
| 571 | // Force everything with a registered fd with epoll to be destroyed now. |
| 572 | timers_.clear(); |
| 573 | phased_loops_.clear(); |
| 574 | watchers_.clear(); |
| 575 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 576 | for (auto it = event_loops_->begin(); it != event_loops_->end(); ++it) { |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 577 | if (*it == this) { |
| 578 | event_loops_->erase(it); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 579 | break; |
| 580 | } |
| 581 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 582 | VLOG(1) << scheduler_->distributed_now() << " " << NodeName(node()) |
| 583 | << monotonic_now() << " ~SimulatedEventLoop(\"" << name_ << "\")"; |
| 584 | startup_tracker_->loop = nullptr; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 587 | void SetIsRunning(bool running) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 588 | VLOG(1) << scheduler_->distributed_now() << " " << NodeName(node()) |
| 589 | << monotonic_now() << " " << name_ << " set_is_running(" << running |
| 590 | << ")"; |
| 591 | CHECK(startup_tracker_->has_setup); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 592 | |
| 593 | set_is_running(running); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 594 | if (running) { |
| 595 | has_run_ = true; |
| 596 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 599 | bool has_run() const { return has_run_; } |
| 600 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 601 | std::chrono::nanoseconds send_delay() const { return send_delay_; } |
| 602 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
| 603 | send_delay_ = send_delay; |
| 604 | } |
| 605 | |
Stephan Pleines | 559fa6c | 2022-01-06 17:23:51 -0800 | [diff] [blame] | 606 | monotonic_clock::time_point monotonic_now() const override { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 607 | return node_event_loop_factory_->monotonic_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Stephan Pleines | 559fa6c | 2022-01-06 17:23:51 -0800 | [diff] [blame] | 610 | realtime_clock::time_point realtime_now() const override { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 611 | return node_event_loop_factory_->realtime_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 614 | distributed_clock::time_point distributed_now() { |
| 615 | return scheduler_->distributed_now(); |
| 616 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 617 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 618 | std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override; |
| 619 | |
| 620 | std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 621 | |
| 622 | void MakeRawWatcher( |
| 623 | const Channel *channel, |
| 624 | ::std::function<void(const Context &context, const void *message)> |
| 625 | watcher) override; |
| 626 | |
| 627 | TimerHandler *AddTimer(::std::function<void()> callback) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 628 | CHECK(!is_running()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 629 | return NewTimer(::std::unique_ptr<TimerHandler>( |
| 630 | new SimulatedTimerHandler(scheduler_, this, callback))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback, |
| 634 | const monotonic_clock::duration interval, |
| 635 | const monotonic_clock::duration offset = |
| 636 | ::std::chrono::seconds(0)) override { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 637 | return NewPhasedLoop( |
| 638 | ::std::unique_ptr<PhasedLoopHandler>(new SimulatedPhasedLoopHandler( |
| 639 | scheduler_, this, callback, interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | void OnRun(::std::function<void()> on_run) override { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 643 | CHECK(!is_running()) << ": Cannot register OnRun callback while running."; |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 644 | scheduler_->ScheduleOnRun([this, on_run = std::move(on_run)]() { |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 645 | logging::ScopedLogRestorer prev_logger; |
| 646 | if (log_impl_) { |
| 647 | prev_logger.Swap(log_impl_); |
| 648 | } |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 649 | ScopedMarkRealtimeRestorer rt(priority() > 0); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 650 | SetTimerContext(monotonic_now()); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 651 | on_run(); |
| 652 | }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 655 | const Node *node() const override { return node_; } |
| 656 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 657 | void set_name(const std::string_view name) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 658 | name_ = std::string(name); |
| 659 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 660 | const std::string_view name() const override { return name_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 661 | |
| 662 | SimulatedChannel *GetSimulatedChannel(const Channel *channel); |
| 663 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 664 | void SetRuntimeRealtimePriority(int priority) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 665 | CHECK(!is_running()) << ": Cannot set realtime priority while running."; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 666 | priority_ = priority; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 669 | int priority() const override { return priority_; } |
| 670 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 671 | void SetRuntimeAffinity(const cpu_set_t & /*cpuset*/) override { |
| 672 | CHECK(!is_running()) << ": Cannot set affinity while running."; |
| 673 | } |
| 674 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 675 | void Setup() { |
| 676 | MaybeScheduleTimingReports(); |
| 677 | if (!skip_logger_) { |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 678 | log_sender_.Initialize(&name_, |
| 679 | MakeSender<logging::LogMessageFbs>("/aos")); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 680 | log_impl_ = log_sender_.implementation(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 681 | } |
| 682 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 683 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 684 | int NumberBuffers(const Channel *channel) override; |
| 685 | |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 686 | const UUID &boot_uuid() const override { |
| 687 | return node_event_loop_factory_->boot_uuid(); |
| 688 | } |
| 689 | |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 690 | const EventLoopOptions &options() const { return options_; } |
| 691 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 692 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 693 | friend class SimulatedTimerHandler; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 694 | friend class SimulatedPhasedLoopHandler; |
| 695 | friend class SimulatedWatcher; |
| 696 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 697 | // We have a condition where we register a startup handler, but then get shut |
| 698 | // down before it runs. This results in a segfault if we are lucky, and |
| 699 | // corruption otherwise. To handle that, allocate a small object which points |
| 700 | // back to us and can be freed when the function is freed. That object can |
| 701 | // then be updated when we get destroyed so setup is not called. |
| 702 | struct StartupTracker { |
| 703 | SimulatedEventLoop *loop = nullptr; |
| 704 | bool has_setup = false; |
| 705 | }; |
| 706 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 707 | void HandleEvent() { |
| 708 | while (true) { |
| 709 | if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) { |
| 710 | break; |
| 711 | } |
| 712 | |
| 713 | EventLoopEvent *event = PopEvent(); |
| 714 | event->HandleEvent(); |
| 715 | } |
| 716 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 717 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 718 | pid_t GetTid() override { return tid_; } |
| 719 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 720 | EventScheduler *scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 721 | NodeEventLoopFactory *node_event_loop_factory_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 722 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_; |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 723 | std::vector<SimulatedEventLoop *> *event_loops_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 724 | |
| 725 | ::std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 726 | |
| 727 | int priority_ = 0; |
| 728 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 729 | std::chrono::nanoseconds send_delay_; |
| 730 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 731 | const Node *const node_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 732 | const pid_t tid_; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 733 | |
| 734 | AosLogToFbs log_sender_; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 735 | std::shared_ptr<logging::LogImplementation> log_impl_ = nullptr; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 736 | |
| 737 | bool has_run_ = false; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 738 | |
| 739 | std::shared_ptr<StartupTracker> startup_tracker_; |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 740 | |
| 741 | EventLoopOptions options_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 742 | }; |
| 743 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 744 | void SimulatedEventLoopFactory::set_send_delay( |
| 745 | std::chrono::nanoseconds send_delay) { |
| 746 | send_delay_ = send_delay; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 747 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 748 | if (node) { |
| 749 | for (SimulatedEventLoop *loop : node->event_loops_) { |
| 750 | loop->set_send_delay(send_delay_); |
| 751 | } |
| 752 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 756 | void SimulatedEventLoop::MakeRawWatcher( |
| 757 | const Channel *channel, |
| 758 | std::function<void(const Context &channel, const void *message)> watcher) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 759 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 760 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 761 | std::unique_ptr<SimulatedWatcher> shm_watcher = |
| 762 | std::make_unique<SimulatedWatcher>(this, scheduler_, channel, |
| 763 | std::move(watcher)); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 764 | |
| 765 | GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get()); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 766 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 767 | NewWatcher(std::move(shm_watcher)); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 768 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 769 | << " " << name() << " MakeRawWatcher(\"" |
| 770 | << configuration::StrippedChannelToString(channel) << "\")"; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 771 | |
| 772 | // Order of operations gets kinda wonky if we let people make watchers after |
| 773 | // running once. If someone has a valid use case, we can reconsider. |
| 774 | CHECK(!has_run()) << ": Can't add a watcher after running."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 778 | const Channel *channel) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 779 | TakeSender(channel); |
| 780 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 781 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 782 | << " " << name() << " MakeRawSender(\"" |
| 783 | << configuration::StrippedChannelToString(channel) << "\")"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 784 | return GetSimulatedChannel(channel)->MakeRawSender(this); |
| 785 | } |
| 786 | |
| 787 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 788 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 789 | ChannelIndex(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 790 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 791 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 792 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 793 | << "\", \"type\": \"" << channel->type()->string_view() |
| 794 | << "\" } is not able to be fetched on this node. Check your " |
| 795 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 798 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 799 | << " " << name() << " MakeRawFetcher(\"" |
| 800 | << configuration::StrippedChannelToString(channel) << "\")"; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 801 | return GetSimulatedChannel(channel)->MakeRawFetcher(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel( |
| 805 | const Channel *channel) { |
| 806 | auto it = channels_->find(SimpleChannel(channel)); |
| 807 | if (it == channels_->end()) { |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 808 | it = channels_ |
| 809 | ->emplace(SimpleChannel(channel), |
| 810 | std::unique_ptr<SimulatedChannel>(new SimulatedChannel( |
| 811 | channel, |
| 812 | std::chrono::nanoseconds( |
| 813 | configuration()->channel_storage_duration()), |
| 814 | scheduler_))) |
| 815 | .first; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 816 | } |
| 817 | return it->second.get(); |
| 818 | } |
| 819 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 820 | int SimulatedEventLoop::NumberBuffers(const Channel *channel) { |
| 821 | return GetSimulatedChannel(channel)->number_buffers(); |
| 822 | } |
| 823 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 824 | SimulatedWatcher::SimulatedWatcher( |
| 825 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 826 | const Channel *channel, |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 827 | std::function<void(const Context &context, const void *message)> fn) |
| 828 | : WatcherState(simulated_event_loop, channel, std::move(fn)), |
| 829 | simulated_event_loop_(simulated_event_loop), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 830 | channel_(channel), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 831 | scheduler_(scheduler), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 832 | event_(this), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 833 | token_(scheduler_->InvalidToken()) { |
| 834 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 835 | << NodeName(simulated_event_loop_->node()) |
| 836 | << simulated_event_loop_->monotonic_now() << " " |
| 837 | << simulated_event_loop_->name() << " Watching " |
| 838 | << configuration::StrippedChannelToString(channel_); |
| 839 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 840 | |
| 841 | SimulatedWatcher::~SimulatedWatcher() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 842 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 843 | << NodeName(simulated_event_loop_->node()) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 844 | << simulated_event_loop_->monotonic_now() << " " |
| 845 | << simulated_event_loop_->name() << " ~Watching " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 846 | << configuration::StrippedChannelToString(channel_); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 847 | simulated_event_loop_->RemoveEvent(&event_); |
| 848 | if (token_ != scheduler_->InvalidToken()) { |
| 849 | scheduler_->Deschedule(token_); |
| 850 | } |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 851 | CHECK_NOTNULL(simulated_channel_)->RemoveWatcher(this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 854 | bool SimulatedWatcher::has_run() const { |
| 855 | return simulated_event_loop_->has_run(); |
| 856 | } |
| 857 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 858 | void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) { |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 859 | monotonic_clock::time_point event_time = |
| 860 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 861 | |
| 862 | // Messages are queued in order. If we are the first, add ourselves. |
| 863 | // Otherwise, don't. |
| 864 | if (msgs_.size() == 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 865 | event_.set_event_time(message->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 866 | simulated_event_loop_->AddEvent(&event_); |
| 867 | |
| 868 | DoSchedule(event_time); |
| 869 | } |
| 870 | |
Austin Schuh | e6f4c8d | 2021-12-11 12:36:06 -0800 | [diff] [blame] | 871 | msgs_.emplace_back(std::move(message)); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Austin Schuh | f4b09c7 | 2021-12-08 12:04:37 -0800 | [diff] [blame] | 874 | void SimulatedWatcher::HandleEvent() noexcept { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 875 | const monotonic_clock::time_point monotonic_now = |
| 876 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 877 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 878 | << NodeName(simulated_event_loop_->node()) |
| 879 | << simulated_event_loop_->monotonic_now() << " " |
| 880 | << simulated_event_loop_->name() << " Watcher " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 881 | << configuration::StrippedChannelToString(channel_); |
| 882 | CHECK_NE(msgs_.size(), 0u) << ": No events to handle."; |
| 883 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 884 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 885 | if (simulated_event_loop_->log_impl_) { |
| 886 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 887 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 888 | Context context = msgs_.front()->context; |
| 889 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 890 | if (channel_->read_method() != ReadMethod::PIN) { |
| 891 | context.buffer_index = -1; |
| 892 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 893 | if (context.remote_queue_index == 0xffffffffu) { |
| 894 | context.remote_queue_index = context.queue_index; |
| 895 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 896 | if (context.monotonic_remote_time == monotonic_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 897 | context.monotonic_remote_time = context.monotonic_event_time; |
| 898 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 899 | if (context.realtime_remote_time == realtime_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 900 | context.realtime_remote_time = context.realtime_event_time; |
| 901 | } |
| 902 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 903 | { |
| 904 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 905 | DoCallCallback([monotonic_now]() { return monotonic_now; }, context); |
| 906 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 907 | |
| 908 | msgs_.pop_front(); |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 909 | if (token_ != scheduler_->InvalidToken()) { |
| 910 | scheduler_->Deschedule(token_); |
| 911 | token_ = scheduler_->InvalidToken(); |
| 912 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 913 | if (msgs_.size() != 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 914 | event_.set_event_time(msgs_.front()->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 915 | simulated_event_loop_->AddEvent(&event_); |
| 916 | |
| 917 | DoSchedule(event_.event_time()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 921 | void SimulatedWatcher::Handle() noexcept { |
| 922 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 923 | token_ = scheduler_->InvalidToken(); |
| 924 | simulated_event_loop_->HandleEvent(); |
| 925 | } |
| 926 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 927 | void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) { |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 928 | CHECK(token_ == scheduler_->InvalidToken()) |
| 929 | << ": May not schedule multiple times"; |
| 930 | token_ = scheduler_->Schedule( |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 931 | event_time + simulated_event_loop_->send_delay(), this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 935 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 936 | watcher->SetSimulatedChannel(this); |
| 937 | watchers_.emplace_back(watcher); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | ::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender( |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 941 | SimulatedEventLoop *event_loop) { |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 942 | CHECK(allow_new_senders_) |
| 943 | << ": Attempted to create a new sender on exclusive channel " |
| 944 | << configuration::StrippedChannelToString(channel_); |
| 945 | if (event_loop->options().exclusive_senders == ExclusiveSenders::kYes) { |
| 946 | CHECK_EQ(0, sender_count_) |
| 947 | << ": Attempted to add an exclusive sender on a channel with existing " |
| 948 | "senders: " |
| 949 | << configuration::StrippedChannelToString(channel_); |
| 950 | allow_new_senders_ = false; |
| 951 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 952 | return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
| 953 | } |
| 954 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 955 | ::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher( |
| 956 | EventLoop *event_loop) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 957 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 958 | ::std::unique_ptr<SimulatedFetcher> fetcher( |
| 959 | new SimulatedFetcher(event_loop, this)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 960 | fetchers_.push_back(fetcher.get()); |
| 961 | return ::std::move(fetcher); |
| 962 | } |
| 963 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 964 | std::optional<uint32_t> SimulatedChannel::Send( |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 965 | std::shared_ptr<SimulatedMessage> message, |
| 966 | CheckSentTooFast check_sent_too_fast) { |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 967 | const auto now = scheduler_->monotonic_now(); |
| 968 | // Remove times that are greater than or equal to a channel_storage_duration_ |
| 969 | // ago |
| 970 | while (!last_times_.empty() && |
| 971 | (now - last_times_.front() >= channel_storage_duration_)) { |
| 972 | last_times_.pop(); |
| 973 | } |
| 974 | |
| 975 | // Check that we are not sending messages too fast |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 976 | if (check_sent_too_fast == CheckSentTooFast::kYes && |
| 977 | static_cast<int>(last_times_.size()) >= queue_size()) { |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 978 | return std::nullopt; |
| 979 | } |
| 980 | |
| 981 | const std::optional<uint32_t> queue_index = {next_queue_index_.index()}; |
| 982 | last_times_.push(now); |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 983 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 984 | message->context.queue_index = *queue_index; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 985 | // Points to the actual data depending on the size set in context. Data may |
| 986 | // allocate more than the actual size of the message, so offset from the back |
| 987 | // of that to get the actual start of the data. |
| 988 | message->context.data = |
| 989 | message->data->data() + message->data->size() - message->context.size; |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 990 | |
| 991 | DCHECK(channel()->has_schema()) |
| 992 | << ": Missing schema for channel " |
| 993 | << configuration::StrippedChannelToString(channel()); |
| 994 | DCHECK(flatbuffers::Verify( |
| 995 | *channel()->schema(), *channel()->schema()->root_table(), |
| 996 | static_cast<const uint8_t *>(message->context.data), |
| 997 | message->context.size)) |
| 998 | << ": Corrupted flatbuffer on " << channel()->name()->c_str() << " " |
| 999 | << channel()->type()->c_str(); |
| 1000 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1001 | next_queue_index_ = next_queue_index_.Increment(); |
| 1002 | |
Austin Schuh | e6f4c8d | 2021-12-11 12:36:06 -0800 | [diff] [blame] | 1003 | latest_message_ = std::move(message); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1004 | for (SimulatedWatcher *watcher : watchers_) { |
| 1005 | if (watcher->has_run()) { |
Austin Schuh | e6f4c8d | 2021-12-11 12:36:06 -0800 | [diff] [blame] | 1006 | watcher->Schedule(latest_message_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | for (auto &fetcher : fetchers_) { |
Austin Schuh | e6f4c8d | 2021-12-11 12:36:06 -0800 | [diff] [blame] | 1010 | fetcher->Enqueue(latest_message_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1011 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1012 | return queue_index; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) { |
| 1016 | fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 1017 | } |
| 1018 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1019 | SimulatedSender::SimulatedSender(SimulatedChannel *simulated_channel, |
| 1020 | SimulatedEventLoop *event_loop) |
| 1021 | : RawSender(event_loop, simulated_channel->channel()), |
| 1022 | simulated_channel_(simulated_channel), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1023 | simulated_event_loop_(event_loop) { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1024 | simulated_channel_->CountSenderCreated(); |
| 1025 | } |
| 1026 | |
| 1027 | SimulatedSender::~SimulatedSender() { |
| 1028 | simulated_channel_->CountSenderDestroyed(); |
| 1029 | } |
| 1030 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1031 | RawSender::Error SimulatedSender::DoSend( |
| 1032 | size_t length, monotonic_clock::time_point monotonic_remote_time, |
| 1033 | realtime_clock::time_point realtime_remote_time, |
| 1034 | uint32_t remote_queue_index, const UUID &source_boot_uuid) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1035 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 1036 | << NodeName(simulated_event_loop_->node()) |
| 1037 | << simulated_event_loop_->monotonic_now() << " " |
| 1038 | << simulated_event_loop_->name() << " Send " |
| 1039 | << configuration::StrippedChannelToString(channel()); |
| 1040 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1041 | // The allocations in here are due to infrastructure and don't count in the |
| 1042 | // no mallocs in RT code. |
| 1043 | ScopedNotRealtime nrt; |
| 1044 | CHECK_LE(length, size()) << ": Attempting to send too big a message."; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1045 | message_->context.monotonic_event_time = |
| 1046 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1047 | message_->context.monotonic_remote_time = monotonic_remote_time; |
| 1048 | message_->context.remote_queue_index = remote_queue_index; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1049 | message_->context.realtime_event_time = simulated_event_loop_->realtime_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1050 | message_->context.realtime_remote_time = realtime_remote_time; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1051 | message_->context.source_boot_uuid = source_boot_uuid; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1052 | CHECK_LE(length, message_->context.size); |
| 1053 | message_->context.size = length; |
| 1054 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1055 | const std::optional<uint32_t> optional_queue_index = simulated_channel_->Send( |
| 1056 | message_, simulated_event_loop_->options().check_sent_too_fast); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1057 | |
| 1058 | // Check that we are not sending messages too fast |
| 1059 | if (!optional_queue_index) { |
| 1060 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 1061 | << NodeName(simulated_event_loop_->node()) |
| 1062 | << simulated_event_loop_->monotonic_now() << " " |
| 1063 | << simulated_event_loop_->name() |
| 1064 | << "\nMessages were sent too fast:\n" |
| 1065 | << "For channel: " |
| 1066 | << configuration::CleanedChannelToString( |
| 1067 | simulated_channel_->channel()) |
| 1068 | << '\n' |
| 1069 | << "Tried to send more than " << simulated_channel_->queue_size() |
| 1070 | << " (queue size) messages in the last " |
| 1071 | << std::chrono::duration<double>( |
| 1072 | simulated_channel_->channel_storage_duration()) |
| 1073 | .count() |
| 1074 | << " seconds (channel storage duration)" |
| 1075 | << "\n\n"; |
| 1076 | return Error::kMessagesSentTooFast; |
| 1077 | } |
| 1078 | |
| 1079 | sent_queue_index_ = *optional_queue_index; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1080 | monotonic_sent_time_ = simulated_event_loop_->monotonic_now(); |
| 1081 | realtime_sent_time_ = simulated_event_loop_->realtime_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1082 | |
| 1083 | // Drop the reference to the message so that we allocate a new message for |
| 1084 | // next time. Otherwise we will continue to reuse the same memory for all |
| 1085 | // messages and corrupt it. |
| 1086 | message_.reset(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1087 | return Error::kOk; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1090 | RawSender::Error SimulatedSender::DoSend( |
| 1091 | const void *msg, size_t size, |
| 1092 | monotonic_clock::time_point monotonic_remote_time, |
| 1093 | realtime_clock::time_point realtime_remote_time, |
| 1094 | uint32_t remote_queue_index, const UUID &source_boot_uuid) { |
Austin Schuh | 102667e | 2020-12-11 20:13:28 -0800 | [diff] [blame] | 1095 | CHECK_LE(size, this->size()) |
| 1096 | << ": Attempting to send too big a message on " |
| 1097 | << configuration::CleanedChannelToString(simulated_channel_->channel()); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1098 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1099 | // Allocates an aligned buffer in which to copy unaligned msg. |
| 1100 | auto [span, mutable_span] = MakeSharedSpan(size); |
| 1101 | message_ = SimulatedMessage::Make(simulated_channel_, span); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1102 | |
| 1103 | // Now fill in the message. size is already populated above, and |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1104 | // queue_index will be populated in simulated_channel_. |
| 1105 | memcpy(mutable_span.data(), msg, size); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1106 | |
| 1107 | return DoSend(size, monotonic_remote_time, realtime_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1108 | remote_queue_index, source_boot_uuid); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1111 | RawSender::Error SimulatedSender::DoSend( |
| 1112 | const RawSender::SharedSpan data, |
| 1113 | monotonic_clock::time_point monotonic_remote_time, |
| 1114 | realtime_clock::time_point realtime_remote_time, |
| 1115 | uint32_t remote_queue_index, const UUID &source_boot_uuid) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1116 | CHECK_LE(data->size(), this->size()) |
| 1117 | << ": Attempting to send too big a message on " |
| 1118 | << configuration::CleanedChannelToString(simulated_channel_->channel()); |
| 1119 | |
| 1120 | // Constructs a message sharing the already allocated and aligned message |
| 1121 | // data. |
| 1122 | message_ = SimulatedMessage::Make(simulated_channel_, data); |
| 1123 | |
| 1124 | return DoSend(data->size(), monotonic_remote_time, realtime_remote_time, |
| 1125 | remote_queue_index, source_boot_uuid); |
| 1126 | } |
| 1127 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1128 | SimulatedTimerHandler::SimulatedTimerHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1129 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 1130 | ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1131 | : TimerHandler(simulated_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1132 | simulated_event_loop_(simulated_event_loop), |
| 1133 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1134 | scheduler_(scheduler), |
| 1135 | token_(scheduler_->InvalidToken()) {} |
| 1136 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1137 | void SimulatedTimerHandler::Setup(monotonic_clock::time_point base, |
| 1138 | monotonic_clock::duration repeat_offset) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 1139 | // The allocations in here are due to infrastructure and don't count in the no |
| 1140 | // mallocs in RT code. |
| 1141 | ScopedNotRealtime nrt; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1142 | Disable(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1143 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 1144 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1145 | base_ = base; |
| 1146 | repeat_offset_ = repeat_offset; |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 1147 | token_ = scheduler_->Schedule(std::max(base, monotonic_now), this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1148 | event_.set_event_time(base_); |
| 1149 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 1152 | void SimulatedTimerHandler::Handle() noexcept { |
| 1153 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 1154 | token_ = scheduler_->InvalidToken(); |
| 1155 | simulated_event_loop_->HandleEvent(); |
| 1156 | } |
| 1157 | |
Austin Schuh | f4b09c7 | 2021-12-08 12:04:37 -0800 | [diff] [blame] | 1158 | void SimulatedTimerHandler::HandleEvent() noexcept { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1159 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 1160 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1161 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 1162 | << NodeName(simulated_event_loop_->node()) << monotonic_now << " " |
| 1163 | << simulated_event_loop_->name() << " Timer '" << name() << "'"; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1164 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1165 | if (simulated_event_loop_->log_impl_) { |
| 1166 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1167 | } |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1168 | if (token_ != scheduler_->InvalidToken()) { |
| 1169 | scheduler_->Deschedule(token_); |
| 1170 | token_ = scheduler_->InvalidToken(); |
| 1171 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1172 | if (repeat_offset_ != monotonic_clock::zero()) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1173 | // Reschedule. |
| 1174 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 1175 | token_ = scheduler_->Schedule(base_, this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1176 | event_.set_event_time(base_); |
| 1177 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1178 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1179 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 1180 | { |
| 1181 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 1182 | Call([monotonic_now]() { return monotonic_now; }, monotonic_now); |
| 1183 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1186 | void SimulatedTimerHandler::Disable() { |
| 1187 | simulated_event_loop_->RemoveEvent(&event_); |
| 1188 | if (token_ != scheduler_->InvalidToken()) { |
| 1189 | scheduler_->Deschedule(token_); |
| 1190 | token_ = scheduler_->InvalidToken(); |
| 1191 | } |
| 1192 | } |
| 1193 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1194 | SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1195 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 1196 | ::std::function<void(int)> fn, const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1197 | const monotonic_clock::duration offset) |
| 1198 | : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset), |
| 1199 | simulated_event_loop_(simulated_event_loop), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1200 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1201 | scheduler_(scheduler), |
| 1202 | token_(scheduler_->InvalidToken()) {} |
| 1203 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1204 | SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() { |
| 1205 | if (token_ != scheduler_->InvalidToken()) { |
| 1206 | scheduler_->Deschedule(token_); |
| 1207 | token_ = scheduler_->InvalidToken(); |
| 1208 | } |
| 1209 | simulated_event_loop_->RemoveEvent(&event_); |
| 1210 | } |
| 1211 | |
Austin Schuh | f4b09c7 | 2021-12-08 12:04:37 -0800 | [diff] [blame] | 1212 | void SimulatedPhasedLoopHandler::HandleEvent() noexcept { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1213 | monotonic_clock::time_point monotonic_now = |
| 1214 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1215 | VLOG(1) << monotonic_now << " Phased loop " << simulated_event_loop_->name() |
| 1216 | << ", " << name(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1217 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1218 | if (simulated_event_loop_->log_impl_) { |
| 1219 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1220 | } |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 1221 | |
| 1222 | { |
| 1223 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 1224 | Call([monotonic_now]() { return monotonic_now; }, |
| 1225 | [this](monotonic_clock::time_point sleep_time) { |
| 1226 | Schedule(sleep_time); |
| 1227 | }); |
| 1228 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1229 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1230 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 1231 | void SimulatedPhasedLoopHandler::Handle() noexcept { |
| 1232 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 1233 | token_ = scheduler_->InvalidToken(); |
| 1234 | simulated_event_loop_->HandleEvent(); |
| 1235 | } |
| 1236 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1237 | void SimulatedPhasedLoopHandler::Schedule( |
| 1238 | monotonic_clock::time_point sleep_time) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 1239 | // The allocations in here are due to infrastructure and don't count in the no |
| 1240 | // mallocs in RT code. |
| 1241 | ScopedNotRealtime nrt; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1242 | if (token_ != scheduler_->InvalidToken()) { |
| 1243 | scheduler_->Deschedule(token_); |
| 1244 | token_ = scheduler_->InvalidToken(); |
| 1245 | } |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 1246 | token_ = scheduler_->Schedule(sleep_time, this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1247 | event_.set_event_time(sleep_time); |
| 1248 | simulated_event_loop_->AddEvent(&event_); |
| 1249 | } |
| 1250 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1251 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 1252 | const Configuration *configuration) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1253 | : configuration_(CHECK_NOTNULL(configuration)), |
| 1254 | nodes_(configuration::GetNodes(configuration_)) { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 1255 | CHECK(IsInitialized()) << ": Need to initialize AOS first."; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1256 | for (const Node *node : nodes_) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1257 | node_factories_.emplace_back( |
| 1258 | new NodeEventLoopFactory(&scheduler_scheduler_, this, node)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1259 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1260 | |
| 1261 | if (configuration::MultiNode(configuration)) { |
| 1262 | bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this); |
| 1263 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1266 | SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {} |
| 1267 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1268 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1269 | std::string_view node) { |
| 1270 | return GetNodeEventLoopFactory(configuration::GetNode(configuration(), node)); |
| 1271 | } |
| 1272 | |
| 1273 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1274 | const Node *node) { |
| 1275 | auto result = std::find_if( |
| 1276 | node_factories_.begin(), node_factories_.end(), |
| 1277 | [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) { |
| 1278 | return node_factory->node() == node; |
| 1279 | }); |
| 1280 | |
| 1281 | CHECK(result != node_factories_.end()) |
| 1282 | << ": Failed to find node " << FlatbufferToJson(node); |
| 1283 | |
| 1284 | return result->get(); |
| 1285 | } |
| 1286 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1287 | void SimulatedEventLoopFactory::SetTimeConverter( |
| 1288 | TimeConverter *time_converter) { |
| 1289 | for (std::unique_ptr<NodeEventLoopFactory> &factory : node_factories_) { |
| 1290 | factory->SetTimeConverter(time_converter); |
| 1291 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1292 | scheduler_scheduler_.SetTimeConverter(time_converter); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 1295 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1296 | std::string_view name, const Node *node) { |
| 1297 | if (node == nullptr) { |
| 1298 | CHECK(!configuration::MultiNode(configuration())) |
| 1299 | << ": Can't make a single node event loop in a multi-node world."; |
| 1300 | } else { |
| 1301 | CHECK(configuration::MultiNode(configuration())) |
| 1302 | << ": Can't make a multi-node event loop in a single-node world."; |
| 1303 | } |
| 1304 | return GetNodeEventLoopFactory(node)->MakeEventLoop(name); |
| 1305 | } |
| 1306 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1307 | NodeEventLoopFactory::NodeEventLoopFactory( |
| 1308 | EventSchedulerScheduler *scheduler_scheduler, |
| 1309 | SimulatedEventLoopFactory *factory, const Node *node) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1310 | : scheduler_(configuration::GetNodeIndex(factory->configuration(), node)), |
| 1311 | factory_(factory), |
| 1312 | node_(node) { |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1313 | scheduler_scheduler->AddEventScheduler(&scheduler_); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1314 | scheduler_.set_started([this]() { |
| 1315 | started_ = true; |
| 1316 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1317 | event_loop->SetIsRunning(true); |
| 1318 | } |
| 1319 | }); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1320 | scheduler_.set_stopped([this]() { |
| 1321 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1322 | event_loop->SetIsRunning(false); |
| 1323 | } |
| 1324 | }); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1325 | scheduler_.set_on_shutdown([this]() { |
| 1326 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node()) |
| 1327 | << monotonic_now() << " Shutting down node."; |
| 1328 | Shutdown(); |
| 1329 | ScheduleStartup(); |
| 1330 | }); |
| 1331 | ScheduleStartup(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | NodeEventLoopFactory::~NodeEventLoopFactory() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1335 | if (started_) { |
| 1336 | for (std::function<void()> &fn : on_shutdown_) { |
| 1337 | fn(); |
| 1338 | } |
| 1339 | |
| 1340 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1341 | << monotonic_now() << " Shutting down applications."; |
| 1342 | applications_.clear(); |
| 1343 | started_ = false; |
| 1344 | } |
| 1345 | |
| 1346 | if (event_loops_.size() != 0u) { |
| 1347 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1348 | LOG(ERROR) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1349 | << monotonic_now() << " Event loop '" << event_loop->name() |
| 1350 | << "' failed to shut down"; |
| 1351 | } |
| 1352 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1353 | CHECK_EQ(event_loops_.size(), 0u) << "Event loop didn't exit"; |
| 1354 | } |
| 1355 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1356 | void NodeEventLoopFactory::OnStartup(std::function<void()> &&fn) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1357 | CHECK(!scheduler_.is_running()) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1358 | << ": Can only register OnStartup handlers when not running."; |
| 1359 | on_startup_.emplace_back(std::move(fn)); |
| 1360 | if (started_) { |
| 1361 | size_t on_startup_index = on_startup_.size() - 1; |
| 1362 | scheduler_.ScheduleOnStartup( |
| 1363 | [this, on_startup_index]() { on_startup_[on_startup_index](); }); |
| 1364 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1367 | void NodeEventLoopFactory::OnShutdown(std::function<void()> &&fn) { |
| 1368 | on_shutdown_.emplace_back(std::move(fn)); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1369 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1370 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1371 | void NodeEventLoopFactory::ScheduleStartup() { |
| 1372 | scheduler_.ScheduleOnStartup([this]() { |
| 1373 | UUID next_uuid = scheduler_.boot_uuid(); |
| 1374 | if (boot_uuid_ != next_uuid) { |
Austin Schuh | 188a2f6 | 2021-11-08 10:45:54 -0800 | [diff] [blame] | 1375 | CHECK_EQ(boot_uuid_, UUID::Zero()) |
| 1376 | << ": Boot UUID changed without restarting. Did TimeConverter " |
| 1377 | "change the boot UUID without signaling a restart, or did you " |
| 1378 | "change TimeConverter?"; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1379 | boot_uuid_ = next_uuid; |
| 1380 | } |
| 1381 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node()) |
| 1382 | << monotonic_now() << " Starting up node on boot " << boot_uuid_; |
| 1383 | Startup(); |
| 1384 | }); |
| 1385 | } |
| 1386 | |
| 1387 | void NodeEventLoopFactory::Startup() { |
| 1388 | CHECK(!started_); |
| 1389 | for (size_t i = 0; i < on_startup_.size(); ++i) { |
| 1390 | on_startup_[i](); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | void NodeEventLoopFactory::Shutdown() { |
| 1395 | for (SimulatedEventLoop *event_loop : event_loops_) { |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1396 | CHECK(!event_loop->is_running()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | CHECK(started_); |
| 1400 | started_ = false; |
| 1401 | for (std::function<void()> &fn : on_shutdown_) { |
| 1402 | fn(); |
| 1403 | } |
| 1404 | |
| 1405 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1406 | << monotonic_now() << " Shutting down applications."; |
| 1407 | applications_.clear(); |
| 1408 | |
| 1409 | if (event_loops_.size() != 0u) { |
| 1410 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1411 | LOG(ERROR) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1412 | << monotonic_now() << " Event loop '" << event_loop->name() |
| 1413 | << "' failed to shut down"; |
| 1414 | } |
| 1415 | } |
| 1416 | CHECK_EQ(event_loops_.size(), 0u) << "Not all event loops shut down"; |
| 1417 | boot_uuid_ = UUID::Zero(); |
| 1418 | |
| 1419 | channels_.clear(); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1422 | void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1423 | // This sets running to true too. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1424 | scheduler_scheduler_.RunFor(duration); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1425 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
| 1426 | if (node) { |
| 1427 | for (SimulatedEventLoop *loop : node->event_loops_) { |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1428 | CHECK(!loop->is_running()); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1429 | } |
| 1430 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | void SimulatedEventLoopFactory::Run() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1435 | // This sets running to true too. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1436 | scheduler_scheduler_.Run(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1437 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
| 1438 | if (node) { |
| 1439 | for (SimulatedEventLoop *loop : node->event_loops_) { |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1440 | CHECK(!loop->is_running()); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1441 | } |
| 1442 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1446 | void SimulatedEventLoopFactory::Exit() { scheduler_scheduler_.Exit(); } |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1447 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1448 | void SimulatedEventLoopFactory::DisableForwarding(const Channel *channel) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1449 | CHECK(bridge_) << ": Can't disable forwarding without a message bridge."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1450 | bridge_->DisableForwarding(channel); |
| 1451 | } |
| 1452 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1453 | void SimulatedEventLoopFactory::DisableStatistics() { |
| 1454 | CHECK(bridge_) << ": Can't disable statistics without a message bridge."; |
| 1455 | bridge_->DisableStatistics(); |
| 1456 | } |
| 1457 | |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 1458 | void SimulatedEventLoopFactory::EnableStatistics() { |
| 1459 | CHECK(bridge_) << ": Can't enable statistics without a message bridge."; |
| 1460 | bridge_->EnableStatistics(); |
| 1461 | } |
| 1462 | |
Austin Schuh | 2928ebe | 2021-02-07 22:10:27 -0800 | [diff] [blame] | 1463 | void SimulatedEventLoopFactory::SkipTimingReport() { |
| 1464 | CHECK(bridge_) << ": Can't skip timing reports without a message bridge."; |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 1465 | |
| 1466 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
| 1467 | if (node) { |
| 1468 | node->SkipTimingReport(); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | void NodeEventLoopFactory::SkipTimingReport() { |
| 1474 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1475 | event_loop->SkipTimingReport(); |
| 1476 | } |
| 1477 | skip_timing_report_ = true; |
| 1478 | } |
| 1479 | |
| 1480 | void NodeEventLoopFactory::EnableStatistics() { |
| 1481 | CHECK(factory_->bridge_) |
| 1482 | << ": Can't enable statistics without a message bridge."; |
| 1483 | factory_->bridge_->EnableStatistics(node_); |
| 1484 | } |
| 1485 | |
| 1486 | void NodeEventLoopFactory::DisableStatistics() { |
| 1487 | CHECK(factory_->bridge_) |
| 1488 | << ": Can't disable statistics without a message bridge."; |
| 1489 | factory_->bridge_->DisableStatistics(node_); |
Austin Schuh | 2928ebe | 2021-02-07 22:10:27 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1492 | ::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop( |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 1493 | std::string_view name, EventLoopOptions options) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1494 | CHECK(!scheduler_.is_running() || !started_) |
| 1495 | << ": Can't create an event loop while running"; |
| 1496 | |
| 1497 | pid_t tid = tid_; |
| 1498 | ++tid_; |
| 1499 | ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop( |
| 1500 | &scheduler_, this, &channels_, factory_->configuration(), &event_loops_, |
James Kuszmaul | 890c249 | 2022-04-06 14:59:31 -0700 | [diff] [blame] | 1501 | node_, tid, options)); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1502 | result->set_name(name); |
| 1503 | result->set_send_delay(factory_->send_delay()); |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 1504 | if (skip_timing_report_) { |
| 1505 | result->SkipTimingReport(); |
| 1506 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1507 | |
| 1508 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1509 | << monotonic_now() << " MakeEventLoop(\"" << result->name() << "\")"; |
| 1510 | return std::move(result); |
| 1511 | } |
| 1512 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 1513 | void SimulatedEventLoopFactory::AllowApplicationCreationDuring( |
| 1514 | std::function<void()> fn) { |
| 1515 | scheduler_scheduler_.TemporarilyStopAndRun(std::move(fn)); |
| 1516 | } |
| 1517 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1518 | void NodeEventLoopFactory::Disconnect(const Node *other) { |
| 1519 | factory_->bridge_->Disconnect(node_, other); |
| 1520 | } |
| 1521 | |
| 1522 | void NodeEventLoopFactory::Connect(const Node *other) { |
| 1523 | factory_->bridge_->Connect(node_, other); |
| 1524 | } |
| 1525 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1526 | } // namespace aos |