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