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)]() { |
| 612 | ScopedMarkRealtimeRestorer rt(priority() > 0); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 613 | SetTimerContext(monotonic_now()); |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 614 | on_run(); |
| 615 | }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 618 | const Node *node() const override { return node_; } |
| 619 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 620 | void set_name(const std::string_view name) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 621 | name_ = std::string(name); |
| 622 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 623 | const std::string_view name() const override { return name_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 624 | |
| 625 | SimulatedChannel *GetSimulatedChannel(const Channel *channel); |
| 626 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 627 | void SetRuntimeRealtimePriority(int priority) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 628 | CHECK(!is_running()) << ": Cannot set realtime priority while running."; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 629 | priority_ = priority; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 632 | int priority() const override { return priority_; } |
| 633 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 634 | void SetRuntimeAffinity(const cpu_set_t & /*cpuset*/) override { |
| 635 | CHECK(!is_running()) << ": Cannot set affinity while running."; |
| 636 | } |
| 637 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 638 | void Setup() { |
| 639 | MaybeScheduleTimingReports(); |
| 640 | if (!skip_logger_) { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 641 | log_sender_.Initialize(MakeSender<logging::LogMessageFbs>("/aos")); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 642 | log_impl_ = log_sender_.implementation(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 643 | } |
| 644 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 645 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 646 | int NumberBuffers(const Channel *channel) override; |
| 647 | |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 648 | const UUID &boot_uuid() const override { |
| 649 | return node_event_loop_factory_->boot_uuid(); |
| 650 | } |
| 651 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 652 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 653 | friend class SimulatedTimerHandler; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 654 | friend class SimulatedPhasedLoopHandler; |
| 655 | friend class SimulatedWatcher; |
| 656 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 657 | // We have a condition where we register a startup handler, but then get shut |
| 658 | // down before it runs. This results in a segfault if we are lucky, and |
| 659 | // corruption otherwise. To handle that, allocate a small object which points |
| 660 | // back to us and can be freed when the function is freed. That object can |
| 661 | // then be updated when we get destroyed so setup is not called. |
| 662 | struct StartupTracker { |
| 663 | SimulatedEventLoop *loop = nullptr; |
| 664 | bool has_setup = false; |
| 665 | }; |
| 666 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 667 | void HandleEvent() { |
| 668 | while (true) { |
| 669 | if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) { |
| 670 | break; |
| 671 | } |
| 672 | |
| 673 | EventLoopEvent *event = PopEvent(); |
| 674 | event->HandleEvent(); |
| 675 | } |
| 676 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 677 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 678 | pid_t GetTid() override { return tid_; } |
| 679 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 680 | EventScheduler *scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 681 | NodeEventLoopFactory *node_event_loop_factory_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 682 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_; |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 683 | std::vector<SimulatedEventLoop *> *event_loops_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 684 | |
| 685 | ::std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 686 | |
| 687 | int priority_ = 0; |
| 688 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 689 | std::chrono::nanoseconds send_delay_; |
| 690 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 691 | const Node *const node_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 692 | const pid_t tid_; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 693 | |
| 694 | AosLogToFbs log_sender_; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 695 | std::shared_ptr<logging::LogImplementation> log_impl_ = nullptr; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 696 | |
| 697 | bool has_run_ = false; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 698 | |
| 699 | std::shared_ptr<StartupTracker> startup_tracker_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 700 | }; |
| 701 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 702 | void SimulatedEventLoopFactory::set_send_delay( |
| 703 | std::chrono::nanoseconds send_delay) { |
| 704 | send_delay_ = send_delay; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 705 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 706 | if (node) { |
| 707 | for (SimulatedEventLoop *loop : node->event_loops_) { |
| 708 | loop->set_send_delay(send_delay_); |
| 709 | } |
| 710 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 714 | void SimulatedEventLoop::MakeRawWatcher( |
| 715 | const Channel *channel, |
| 716 | std::function<void(const Context &channel, const void *message)> watcher) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 717 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 718 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 719 | std::unique_ptr<SimulatedWatcher> shm_watcher = |
| 720 | std::make_unique<SimulatedWatcher>(this, scheduler_, channel, |
| 721 | std::move(watcher)); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 722 | |
| 723 | GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get()); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 724 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 725 | NewWatcher(std::move(shm_watcher)); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 726 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 727 | << " " << name() << " MakeRawWatcher(\"" |
| 728 | << configuration::StrippedChannelToString(channel) << "\")"; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 729 | |
| 730 | // Order of operations gets kinda wonky if we let people make watchers after |
| 731 | // running once. If someone has a valid use case, we can reconsider. |
| 732 | CHECK(!has_run()) << ": Can't add a watcher after running."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 736 | const Channel *channel) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 737 | TakeSender(channel); |
| 738 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 739 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 740 | << " " << name() << " MakeRawSender(\"" |
| 741 | << configuration::StrippedChannelToString(channel) << "\")"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 742 | return GetSimulatedChannel(channel)->MakeRawSender(this); |
| 743 | } |
| 744 | |
| 745 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 746 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 747 | ChannelIndex(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 748 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 749 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 750 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 751 | << "\", \"type\": \"" << channel->type()->string_view() |
| 752 | << "\" } is not able to be fetched on this node. Check your " |
| 753 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 756 | VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now() |
| 757 | << " " << name() << " MakeRawFetcher(\"" |
| 758 | << configuration::StrippedChannelToString(channel) << "\")"; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 759 | return GetSimulatedChannel(channel)->MakeRawFetcher(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel( |
| 763 | const Channel *channel) { |
| 764 | auto it = channels_->find(SimpleChannel(channel)); |
| 765 | if (it == channels_->end()) { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 766 | it = |
| 767 | channels_ |
| 768 | ->emplace( |
| 769 | SimpleChannel(channel), |
| 770 | std::unique_ptr<SimulatedChannel>(new SimulatedChannel( |
| 771 | channel, std::chrono::nanoseconds( |
| 772 | configuration()->channel_storage_duration())))) |
| 773 | .first; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 774 | } |
| 775 | return it->second.get(); |
| 776 | } |
| 777 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 778 | int SimulatedEventLoop::NumberBuffers(const Channel *channel) { |
| 779 | return GetSimulatedChannel(channel)->number_buffers(); |
| 780 | } |
| 781 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 782 | SimulatedWatcher::SimulatedWatcher( |
| 783 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 784 | const Channel *channel, |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 785 | std::function<void(const Context &context, const void *message)> fn) |
| 786 | : WatcherState(simulated_event_loop, channel, std::move(fn)), |
| 787 | simulated_event_loop_(simulated_event_loop), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 788 | channel_(channel), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 789 | scheduler_(scheduler), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 790 | event_(this), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 791 | token_(scheduler_->InvalidToken()) { |
| 792 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 793 | << NodeName(simulated_event_loop_->node()) |
| 794 | << simulated_event_loop_->monotonic_now() << " " |
| 795 | << simulated_event_loop_->name() << " Watching " |
| 796 | << configuration::StrippedChannelToString(channel_); |
| 797 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 798 | |
| 799 | SimulatedWatcher::~SimulatedWatcher() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 800 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 801 | << NodeName(simulated_event_loop_->node()) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 802 | << simulated_event_loop_->monotonic_now() << " " |
| 803 | << simulated_event_loop_->name() << " ~Watching " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 804 | << configuration::StrippedChannelToString(channel_); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 805 | simulated_event_loop_->RemoveEvent(&event_); |
| 806 | if (token_ != scheduler_->InvalidToken()) { |
| 807 | scheduler_->Deschedule(token_); |
| 808 | } |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 809 | CHECK_NOTNULL(simulated_channel_)->RemoveWatcher(this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 812 | bool SimulatedWatcher::has_run() const { |
| 813 | return simulated_event_loop_->has_run(); |
| 814 | } |
| 815 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 816 | void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) { |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 817 | monotonic_clock::time_point event_time = |
| 818 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 819 | |
| 820 | // Messages are queued in order. If we are the first, add ourselves. |
| 821 | // Otherwise, don't. |
| 822 | if (msgs_.size() == 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 823 | event_.set_event_time(message->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 824 | simulated_event_loop_->AddEvent(&event_); |
| 825 | |
| 826 | DoSchedule(event_time); |
| 827 | } |
| 828 | |
| 829 | msgs_.emplace_back(message); |
| 830 | } |
| 831 | |
| 832 | void SimulatedWatcher::HandleEvent() { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 833 | const monotonic_clock::time_point monotonic_now = |
| 834 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 835 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 836 | << NodeName(simulated_event_loop_->node()) |
| 837 | << simulated_event_loop_->monotonic_now() << " " |
| 838 | << simulated_event_loop_->name() << " Watcher " |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 839 | << configuration::StrippedChannelToString(channel_); |
| 840 | CHECK_NE(msgs_.size(), 0u) << ": No events to handle."; |
| 841 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 842 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 843 | if (simulated_event_loop_->log_impl_) { |
| 844 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 845 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 846 | Context context = msgs_.front()->context; |
| 847 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 848 | if (channel_->read_method() != ReadMethod::PIN) { |
| 849 | context.buffer_index = -1; |
| 850 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 851 | if (context.remote_queue_index == 0xffffffffu) { |
| 852 | context.remote_queue_index = context.queue_index; |
| 853 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 854 | if (context.monotonic_remote_time == monotonic_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 855 | context.monotonic_remote_time = context.monotonic_event_time; |
| 856 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 857 | if (context.realtime_remote_time == realtime_clock::min_time) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 858 | context.realtime_remote_time = context.realtime_event_time; |
| 859 | } |
| 860 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 861 | { |
| 862 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 863 | DoCallCallback([monotonic_now]() { return monotonic_now; }, context); |
| 864 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 865 | |
| 866 | msgs_.pop_front(); |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 867 | if (token_ != scheduler_->InvalidToken()) { |
| 868 | scheduler_->Deschedule(token_); |
| 869 | token_ = scheduler_->InvalidToken(); |
| 870 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 871 | if (msgs_.size() != 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 872 | event_.set_event_time(msgs_.front()->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 873 | simulated_event_loop_->AddEvent(&event_); |
| 874 | |
| 875 | DoSchedule(event_.event_time()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
| 879 | void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) { |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 880 | CHECK(token_ == scheduler_->InvalidToken()) |
| 881 | << ": May not schedule multiple times"; |
| 882 | token_ = scheduler_->Schedule( |
| 883 | event_time + simulated_event_loop_->send_delay(), [this]() { |
| 884 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 885 | token_ = scheduler_->InvalidToken(); |
| 886 | simulated_event_loop_->HandleEvent(); |
| 887 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 891 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 892 | watcher->SetSimulatedChannel(this); |
| 893 | watchers_.emplace_back(watcher); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | ::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender( |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 897 | SimulatedEventLoop *event_loop) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 898 | return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
| 899 | } |
| 900 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 901 | ::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher( |
| 902 | EventLoop *event_loop) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 903 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 904 | ::std::unique_ptr<SimulatedFetcher> fetcher( |
| 905 | new SimulatedFetcher(event_loop, this)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 906 | fetchers_.push_back(fetcher.get()); |
| 907 | return ::std::move(fetcher); |
| 908 | } |
| 909 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 910 | uint32_t SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) { |
| 911 | const uint32_t queue_index = next_queue_index_.index(); |
| 912 | message->context.queue_index = queue_index; |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 913 | |
| 914 | // Points to the actual data depending on the size set in context. Data may |
| 915 | // allocate more than the actual size of the message, so offset from the back |
| 916 | // of that to get the actual start of the data. |
| 917 | message->context.data = |
| 918 | message->data->data() + message->data->size() - message->context.size; |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 919 | |
| 920 | DCHECK(channel()->has_schema()) |
| 921 | << ": Missing schema for channel " |
| 922 | << configuration::StrippedChannelToString(channel()); |
| 923 | DCHECK(flatbuffers::Verify( |
| 924 | *channel()->schema(), *channel()->schema()->root_table(), |
| 925 | static_cast<const uint8_t *>(message->context.data), |
| 926 | message->context.size)) |
| 927 | << ": Corrupted flatbuffer on " << channel()->name()->c_str() << " " |
| 928 | << channel()->type()->c_str(); |
| 929 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 930 | next_queue_index_ = next_queue_index_.Increment(); |
| 931 | |
| 932 | latest_message_ = message; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 933 | for (SimulatedWatcher *watcher : watchers_) { |
| 934 | if (watcher->has_run()) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 935 | watcher->Schedule(message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | for (auto &fetcher : fetchers_) { |
| 939 | fetcher->Enqueue(message); |
| 940 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 941 | |
| 942 | return queue_index; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) { |
| 946 | fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 947 | } |
| 948 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 949 | SimulatedSender::SimulatedSender(SimulatedChannel *simulated_channel, |
| 950 | SimulatedEventLoop *event_loop) |
| 951 | : RawSender(event_loop, simulated_channel->channel()), |
| 952 | simulated_channel_(simulated_channel), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 953 | simulated_event_loop_(event_loop) { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 954 | simulated_channel_->CountSenderCreated(); |
| 955 | } |
| 956 | |
| 957 | SimulatedSender::~SimulatedSender() { |
| 958 | simulated_channel_->CountSenderDestroyed(); |
| 959 | } |
| 960 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 961 | bool SimulatedSender::DoSend(size_t length, |
| 962 | monotonic_clock::time_point monotonic_remote_time, |
| 963 | realtime_clock::time_point realtime_remote_time, |
| 964 | uint32_t remote_queue_index, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 965 | const UUID &source_boot_uuid) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 966 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 967 | << NodeName(simulated_event_loop_->node()) |
| 968 | << simulated_event_loop_->monotonic_now() << " " |
| 969 | << simulated_event_loop_->name() << " Send " |
| 970 | << configuration::StrippedChannelToString(channel()); |
| 971 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 972 | // The allocations in here are due to infrastructure and don't count in the |
| 973 | // no mallocs in RT code. |
| 974 | ScopedNotRealtime nrt; |
| 975 | CHECK_LE(length, size()) << ": Attempting to send too big a message."; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 976 | message_->context.monotonic_event_time = |
| 977 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 978 | message_->context.monotonic_remote_time = monotonic_remote_time; |
| 979 | message_->context.remote_queue_index = remote_queue_index; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 980 | message_->context.realtime_event_time = simulated_event_loop_->realtime_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 981 | message_->context.realtime_remote_time = realtime_remote_time; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 982 | message_->context.source_boot_uuid = source_boot_uuid; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 983 | CHECK_LE(length, message_->context.size); |
| 984 | message_->context.size = length; |
| 985 | |
| 986 | // TODO(austin): Track sending too fast. |
| 987 | sent_queue_index_ = simulated_channel_->Send(message_); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 988 | monotonic_sent_time_ = simulated_event_loop_->monotonic_now(); |
| 989 | realtime_sent_time_ = simulated_event_loop_->realtime_now(); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 990 | |
| 991 | // Drop the reference to the message so that we allocate a new message for |
| 992 | // next time. Otherwise we will continue to reuse the same memory for all |
| 993 | // messages and corrupt it. |
| 994 | message_.reset(); |
| 995 | return true; |
| 996 | } |
| 997 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 998 | bool SimulatedSender::DoSend(const void *msg, size_t size, |
| 999 | monotonic_clock::time_point monotonic_remote_time, |
| 1000 | realtime_clock::time_point realtime_remote_time, |
| 1001 | uint32_t remote_queue_index, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1002 | const UUID &source_boot_uuid) { |
Austin Schuh | 102667e | 2020-12-11 20:13:28 -0800 | [diff] [blame] | 1003 | CHECK_LE(size, this->size()) |
| 1004 | << ": Attempting to send too big a message on " |
| 1005 | << configuration::CleanedChannelToString(simulated_channel_->channel()); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1006 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1007 | // Allocates an aligned buffer in which to copy unaligned msg. |
| 1008 | auto [span, mutable_span] = MakeSharedSpan(size); |
| 1009 | message_ = SimulatedMessage::Make(simulated_channel_, span); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1010 | |
| 1011 | // Now fill in the message. size is already populated above, and |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1012 | // queue_index will be populated in simulated_channel_. |
| 1013 | memcpy(mutable_span.data(), msg, size); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1014 | |
| 1015 | return DoSend(size, monotonic_remote_time, realtime_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1016 | remote_queue_index, source_boot_uuid); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1019 | bool SimulatedSender::DoSend(const RawSender::SharedSpan data, |
| 1020 | monotonic_clock::time_point monotonic_remote_time, |
| 1021 | realtime_clock::time_point realtime_remote_time, |
| 1022 | uint32_t remote_queue_index, |
| 1023 | const UUID &source_boot_uuid) { |
| 1024 | CHECK_LE(data->size(), this->size()) |
| 1025 | << ": Attempting to send too big a message on " |
| 1026 | << configuration::CleanedChannelToString(simulated_channel_->channel()); |
| 1027 | |
| 1028 | // Constructs a message sharing the already allocated and aligned message |
| 1029 | // data. |
| 1030 | message_ = SimulatedMessage::Make(simulated_channel_, data); |
| 1031 | |
| 1032 | return DoSend(data->size(), monotonic_remote_time, realtime_remote_time, |
| 1033 | remote_queue_index, source_boot_uuid); |
| 1034 | } |
| 1035 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1036 | SimulatedTimerHandler::SimulatedTimerHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1037 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 1038 | ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1039 | : TimerHandler(simulated_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1040 | simulated_event_loop_(simulated_event_loop), |
| 1041 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1042 | scheduler_(scheduler), |
| 1043 | token_(scheduler_->InvalidToken()) {} |
| 1044 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1045 | void SimulatedTimerHandler::Setup(monotonic_clock::time_point base, |
| 1046 | monotonic_clock::duration repeat_offset) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 1047 | // The allocations in here are due to infrastructure and don't count in the no |
| 1048 | // mallocs in RT code. |
| 1049 | ScopedNotRealtime nrt; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1050 | Disable(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1051 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 1052 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1053 | base_ = base; |
| 1054 | repeat_offset_ = repeat_offset; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1055 | token_ = scheduler_->Schedule(std::max(base, monotonic_now), [this]() { |
| 1056 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 1057 | token_ = scheduler_->InvalidToken(); |
| 1058 | simulated_event_loop_->HandleEvent(); |
| 1059 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1060 | event_.set_event_time(base_); |
| 1061 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | void SimulatedTimerHandler::HandleEvent() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1065 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 1066 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1067 | VLOG(1) << simulated_event_loop_->distributed_now() << " " |
| 1068 | << NodeName(simulated_event_loop_->node()) << monotonic_now << " " |
| 1069 | << simulated_event_loop_->name() << " Timer '" << name() << "'"; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1070 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1071 | if (simulated_event_loop_->log_impl_) { |
| 1072 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1073 | } |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1074 | if (token_ != scheduler_->InvalidToken()) { |
| 1075 | scheduler_->Deschedule(token_); |
| 1076 | token_ = scheduler_->InvalidToken(); |
| 1077 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1078 | if (repeat_offset_ != monotonic_clock::zero()) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1079 | // Reschedule. |
| 1080 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1081 | token_ = scheduler_->Schedule(base_, [this]() { |
| 1082 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 1083 | token_ = scheduler_->InvalidToken(); |
| 1084 | simulated_event_loop_->HandleEvent(); |
| 1085 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1086 | event_.set_event_time(base_); |
| 1087 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1088 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1089 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 1090 | { |
| 1091 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 1092 | Call([monotonic_now]() { return monotonic_now; }, monotonic_now); |
| 1093 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1096 | void SimulatedTimerHandler::Disable() { |
| 1097 | simulated_event_loop_->RemoveEvent(&event_); |
| 1098 | if (token_ != scheduler_->InvalidToken()) { |
| 1099 | scheduler_->Deschedule(token_); |
| 1100 | token_ = scheduler_->InvalidToken(); |
| 1101 | } |
| 1102 | } |
| 1103 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1104 | SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1105 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 1106 | ::std::function<void(int)> fn, const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1107 | const monotonic_clock::duration offset) |
| 1108 | : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset), |
| 1109 | simulated_event_loop_(simulated_event_loop), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1110 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1111 | scheduler_(scheduler), |
| 1112 | token_(scheduler_->InvalidToken()) {} |
| 1113 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1114 | SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() { |
| 1115 | if (token_ != scheduler_->InvalidToken()) { |
| 1116 | scheduler_->Deschedule(token_); |
| 1117 | token_ = scheduler_->InvalidToken(); |
| 1118 | } |
| 1119 | simulated_event_loop_->RemoveEvent(&event_); |
| 1120 | } |
| 1121 | |
| 1122 | void SimulatedPhasedLoopHandler::HandleEvent() { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1123 | monotonic_clock::time_point monotonic_now = |
| 1124 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1125 | VLOG(1) << monotonic_now << " Phased loop " << simulated_event_loop_->name() |
| 1126 | << ", " << name(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1127 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1128 | if (simulated_event_loop_->log_impl_) { |
| 1129 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1130 | } |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 1131 | |
| 1132 | { |
| 1133 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 1134 | Call([monotonic_now]() { return monotonic_now; }, |
| 1135 | [this](monotonic_clock::time_point sleep_time) { |
| 1136 | Schedule(sleep_time); |
| 1137 | }); |
| 1138 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1139 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 1140 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1141 | void SimulatedPhasedLoopHandler::Schedule( |
| 1142 | monotonic_clock::time_point sleep_time) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 1143 | // The allocations in here are due to infrastructure and don't count in the no |
| 1144 | // mallocs in RT code. |
| 1145 | ScopedNotRealtime nrt; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 1146 | if (token_ != scheduler_->InvalidToken()) { |
| 1147 | scheduler_->Deschedule(token_); |
| 1148 | token_ = scheduler_->InvalidToken(); |
| 1149 | } |
| 1150 | token_ = scheduler_->Schedule(sleep_time, [this]() { |
| 1151 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 1152 | token_ = scheduler_->InvalidToken(); |
| 1153 | simulated_event_loop_->HandleEvent(); |
| 1154 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1155 | event_.set_event_time(sleep_time); |
| 1156 | simulated_event_loop_->AddEvent(&event_); |
| 1157 | } |
| 1158 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1159 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 1160 | const Configuration *configuration) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1161 | : configuration_(CHECK_NOTNULL(configuration)), |
| 1162 | nodes_(configuration::GetNodes(configuration_)) { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 1163 | CHECK(IsInitialized()) << ": Need to initialize AOS first."; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1164 | for (const Node *node : nodes_) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1165 | node_factories_.emplace_back( |
| 1166 | new NodeEventLoopFactory(&scheduler_scheduler_, this, node)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1167 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1168 | |
| 1169 | if (configuration::MultiNode(configuration)) { |
| 1170 | bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this); |
| 1171 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1174 | SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {} |
| 1175 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1176 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1177 | std::string_view node) { |
| 1178 | return GetNodeEventLoopFactory(configuration::GetNode(configuration(), node)); |
| 1179 | } |
| 1180 | |
| 1181 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1182 | const Node *node) { |
| 1183 | auto result = std::find_if( |
| 1184 | node_factories_.begin(), node_factories_.end(), |
| 1185 | [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) { |
| 1186 | return node_factory->node() == node; |
| 1187 | }); |
| 1188 | |
| 1189 | CHECK(result != node_factories_.end()) |
| 1190 | << ": Failed to find node " << FlatbufferToJson(node); |
| 1191 | |
| 1192 | return result->get(); |
| 1193 | } |
| 1194 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1195 | void SimulatedEventLoopFactory::SetTimeConverter( |
| 1196 | TimeConverter *time_converter) { |
| 1197 | for (std::unique_ptr<NodeEventLoopFactory> &factory : node_factories_) { |
| 1198 | factory->SetTimeConverter(time_converter); |
| 1199 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1200 | scheduler_scheduler_.SetTimeConverter(time_converter); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 1203 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1204 | std::string_view name, const Node *node) { |
| 1205 | if (node == nullptr) { |
| 1206 | CHECK(!configuration::MultiNode(configuration())) |
| 1207 | << ": Can't make a single node event loop in a multi-node world."; |
| 1208 | } else { |
| 1209 | CHECK(configuration::MultiNode(configuration())) |
| 1210 | << ": Can't make a multi-node event loop in a single-node world."; |
| 1211 | } |
| 1212 | return GetNodeEventLoopFactory(node)->MakeEventLoop(name); |
| 1213 | } |
| 1214 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1215 | NodeEventLoopFactory::NodeEventLoopFactory( |
| 1216 | EventSchedulerScheduler *scheduler_scheduler, |
| 1217 | SimulatedEventLoopFactory *factory, const Node *node) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1218 | : scheduler_(configuration::GetNodeIndex(factory->configuration(), node)), |
| 1219 | factory_(factory), |
| 1220 | node_(node) { |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1221 | scheduler_scheduler->AddEventScheduler(&scheduler_); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1222 | scheduler_.set_started([this]() { |
| 1223 | started_ = true; |
| 1224 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1225 | event_loop->SetIsRunning(true); |
| 1226 | } |
| 1227 | }); |
| 1228 | scheduler_.set_on_shutdown([this]() { |
| 1229 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node()) |
| 1230 | << monotonic_now() << " Shutting down node."; |
| 1231 | Shutdown(); |
| 1232 | ScheduleStartup(); |
| 1233 | }); |
| 1234 | ScheduleStartup(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | NodeEventLoopFactory::~NodeEventLoopFactory() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1238 | if (started_) { |
| 1239 | for (std::function<void()> &fn : on_shutdown_) { |
| 1240 | fn(); |
| 1241 | } |
| 1242 | |
| 1243 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1244 | << monotonic_now() << " Shutting down applications."; |
| 1245 | applications_.clear(); |
| 1246 | started_ = false; |
| 1247 | } |
| 1248 | |
| 1249 | if (event_loops_.size() != 0u) { |
| 1250 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1251 | LOG(ERROR) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1252 | << monotonic_now() << " Event loop '" << event_loop->name() |
| 1253 | << "' failed to shut down"; |
| 1254 | } |
| 1255 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1256 | CHECK_EQ(event_loops_.size(), 0u) << "Event loop didn't exit"; |
| 1257 | } |
| 1258 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1259 | void NodeEventLoopFactory::OnStartup(std::function<void()> &&fn) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1260 | CHECK(!scheduler_.is_running()) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1261 | << ": Can only register OnStartup handlers when not running."; |
| 1262 | on_startup_.emplace_back(std::move(fn)); |
| 1263 | if (started_) { |
| 1264 | size_t on_startup_index = on_startup_.size() - 1; |
| 1265 | scheduler_.ScheduleOnStartup( |
| 1266 | [this, on_startup_index]() { on_startup_[on_startup_index](); }); |
| 1267 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1270 | void NodeEventLoopFactory::OnShutdown(std::function<void()> &&fn) { |
| 1271 | on_shutdown_.emplace_back(std::move(fn)); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1272 | } |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1273 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1274 | void NodeEventLoopFactory::ScheduleStartup() { |
| 1275 | scheduler_.ScheduleOnStartup([this]() { |
| 1276 | UUID next_uuid = scheduler_.boot_uuid(); |
| 1277 | if (boot_uuid_ != next_uuid) { |
| 1278 | CHECK_EQ(boot_uuid_, UUID::Zero()); |
| 1279 | boot_uuid_ = next_uuid; |
| 1280 | } |
| 1281 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node()) |
| 1282 | << monotonic_now() << " Starting up node on boot " << boot_uuid_; |
| 1283 | Startup(); |
| 1284 | }); |
| 1285 | } |
| 1286 | |
| 1287 | void NodeEventLoopFactory::Startup() { |
| 1288 | CHECK(!started_); |
| 1289 | for (size_t i = 0; i < on_startup_.size(); ++i) { |
| 1290 | on_startup_[i](); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | void NodeEventLoopFactory::Shutdown() { |
| 1295 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1296 | event_loop->SetIsRunning(false); |
| 1297 | } |
| 1298 | |
| 1299 | CHECK(started_); |
| 1300 | started_ = false; |
| 1301 | for (std::function<void()> &fn : on_shutdown_) { |
| 1302 | fn(); |
| 1303 | } |
| 1304 | |
| 1305 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1306 | << monotonic_now() << " Shutting down applications."; |
| 1307 | applications_.clear(); |
| 1308 | |
| 1309 | if (event_loops_.size() != 0u) { |
| 1310 | for (SimulatedEventLoop *event_loop : event_loops_) { |
| 1311 | LOG(ERROR) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1312 | << monotonic_now() << " Event loop '" << event_loop->name() |
| 1313 | << "' failed to shut down"; |
| 1314 | } |
| 1315 | } |
| 1316 | CHECK_EQ(event_loops_.size(), 0u) << "Not all event loops shut down"; |
| 1317 | boot_uuid_ = UUID::Zero(); |
| 1318 | |
| 1319 | channels_.clear(); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1322 | void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1323 | // This sets running to true too. |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1324 | scheduler_scheduler_.RunOnStartup(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1325 | scheduler_scheduler_.RunFor(duration); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1326 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
| 1327 | if (node) { |
| 1328 | for (SimulatedEventLoop *loop : node->event_loops_) { |
| 1329 | loop->SetIsRunning(false); |
| 1330 | } |
| 1331 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | void SimulatedEventLoopFactory::Run() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1336 | // This sets running to true too. |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1337 | scheduler_scheduler_.RunOnStartup(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1338 | scheduler_scheduler_.Run(); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 1339 | for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) { |
| 1340 | if (node) { |
| 1341 | for (SimulatedEventLoop *loop : node->event_loops_) { |
| 1342 | loop->SetIsRunning(false); |
| 1343 | } |
| 1344 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1348 | void SimulatedEventLoopFactory::Exit() { scheduler_scheduler_.Exit(); } |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1349 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1350 | void SimulatedEventLoopFactory::DisableForwarding(const Channel *channel) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1351 | CHECK(bridge_) << ": Can't disable forwarding without a message bridge."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1352 | bridge_->DisableForwarding(channel); |
| 1353 | } |
| 1354 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1355 | void SimulatedEventLoopFactory::DisableStatistics() { |
| 1356 | CHECK(bridge_) << ": Can't disable statistics without a message bridge."; |
| 1357 | bridge_->DisableStatistics(); |
| 1358 | } |
| 1359 | |
Austin Schuh | 2928ebe | 2021-02-07 22:10:27 -0800 | [diff] [blame] | 1360 | void SimulatedEventLoopFactory::SkipTimingReport() { |
| 1361 | CHECK(bridge_) << ": Can't skip timing reports without a message bridge."; |
Austin Schuh | 2928ebe | 2021-02-07 22:10:27 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1364 | ::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop( |
| 1365 | std::string_view name) { |
| 1366 | CHECK(!scheduler_.is_running() || !started_) |
| 1367 | << ": Can't create an event loop while running"; |
| 1368 | |
| 1369 | pid_t tid = tid_; |
| 1370 | ++tid_; |
| 1371 | ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop( |
| 1372 | &scheduler_, this, &channels_, factory_->configuration(), &event_loops_, |
| 1373 | node_, tid)); |
| 1374 | result->set_name(name); |
| 1375 | result->set_send_delay(factory_->send_delay()); |
| 1376 | |
| 1377 | VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node()) |
| 1378 | << monotonic_now() << " MakeEventLoop(\"" << result->name() << "\")"; |
| 1379 | return std::move(result); |
| 1380 | } |
| 1381 | |
| 1382 | void NodeEventLoopFactory::Disconnect(const Node *other) { |
| 1383 | factory_->bridge_->Disconnect(node_, other); |
| 1384 | } |
| 1385 | |
| 1386 | void NodeEventLoopFactory::Connect(const Node *other) { |
| 1387 | factory_->bridge_->Connect(node_, other); |
| 1388 | } |
| 1389 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1390 | } // namespace aos |