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