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