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