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