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