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