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