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