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