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 | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 24 | class ScopedMarkRealtimeRestorer { |
| 25 | public: |
| 26 | ScopedMarkRealtimeRestorer(bool rt) : rt_(rt), prior_(MarkRealtime(rt)) {} |
| 27 | ~ScopedMarkRealtimeRestorer() { CHECK_EQ(rt_, MarkRealtime(prior_)); } |
| 28 | |
| 29 | private: |
| 30 | const bool rt_; |
| 31 | const bool prior_; |
| 32 | }; |
| 33 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | // Container for both a message, and the context for it for simulation. This |
| 35 | // makes tracking the timestamps associated with the data easy. |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 36 | struct SimulatedMessage final { |
| 37 | SimulatedMessage(const SimulatedMessage &) = delete; |
| 38 | SimulatedMessage &operator=(const SimulatedMessage &) = delete; |
| 39 | |
| 40 | // Creates a SimulatedMessage with size bytes of storage. |
| 41 | // This is a shared_ptr so we don't have to implement refcounting or copying. |
| 42 | static std::shared_ptr<SimulatedMessage> Make(SimulatedChannel *channel); |
| 43 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | // Context for the data. |
| 45 | Context context; |
| 46 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 47 | SimulatedChannel *const channel = nullptr; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 48 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 49 | // The data. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 50 | char *data(size_t buffer_size) { |
| 51 | return RoundChannelData(&actual_data[0], buffer_size); |
| 52 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 53 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 54 | // Then the data, including padding on the end so we can align the buffer we |
| 55 | // actually return from data(). |
| 56 | char actual_data[]; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | SimulatedMessage(SimulatedChannel *channel_in); |
| 60 | ~SimulatedMessage(); |
| 61 | |
| 62 | static void DestroyAndFree(SimulatedMessage *p) { |
| 63 | p->~SimulatedMessage(); |
| 64 | free(p); |
| 65 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 68 | } // namespace |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 69 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 70 | // TODO(Brian): This should be in the anonymous namespace, but that annoys GCC |
| 71 | // for some reason... |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 72 | class SimulatedWatcher : public WatcherState { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 73 | public: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 74 | SimulatedWatcher( |
| 75 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
| 76 | const Channel *channel, |
| 77 | std::function<void(const Context &context, const void *message)> fn); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 78 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 79 | ~SimulatedWatcher() override; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 80 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 81 | bool has_run() const; |
| 82 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 83 | void Startup(EventLoop * /*event_loop*/) override {} |
| 84 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 85 | void Schedule(std::shared_ptr<SimulatedMessage> message); |
| 86 | |
| 87 | void HandleEvent(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 88 | |
| 89 | void SetSimulatedChannel(SimulatedChannel *channel) { |
| 90 | simulated_channel_ = channel; |
| 91 | } |
| 92 | |
| 93 | private: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 94 | void DoSchedule(monotonic_clock::time_point event_time); |
| 95 | |
| 96 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
| 97 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 98 | SimulatedEventLoop *const simulated_event_loop_; |
| 99 | const Channel *const channel_; |
| 100 | EventScheduler *const scheduler_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 101 | EventHandler<SimulatedWatcher> event_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 102 | EventScheduler::Token token_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 103 | SimulatedChannel *simulated_channel_ = nullptr; |
| 104 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 105 | |
| 106 | class SimulatedChannel { |
| 107 | public: |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 108 | explicit SimulatedChannel(const Channel *channel, |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 109 | std::chrono::nanoseconds channel_storage_duration) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 110 | : channel_(channel), |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 111 | channel_storage_duration_(channel_storage_duration), |
| 112 | next_queue_index_(ipc_lib::QueueIndex::Zero(number_buffers())) { |
| 113 | available_buffer_indices_.reserve(number_buffers()); |
| 114 | for (int i = 0; i < number_buffers(); ++i) { |
| 115 | available_buffer_indices_.push_back(i); |
| 116 | } |
| 117 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 118 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 119 | ~SimulatedChannel() { |
| 120 | latest_message_.reset(); |
| 121 | CHECK_EQ(static_cast<size_t>(number_buffers()), |
| 122 | available_buffer_indices_.size()); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 123 | CHECK_EQ(0u, fetchers_.size()) |
| 124 | << configuration::StrippedChannelToString(channel()); |
| 125 | CHECK_EQ(0u, watchers_.size()) |
| 126 | << configuration::StrippedChannelToString(channel()); |
| 127 | CHECK_EQ(0, sender_count_) |
| 128 | << configuration::StrippedChannelToString(channel()); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // The number of messages we pretend to have in the queue. |
| 132 | int queue_size() const { |
| 133 | return channel()->frequency() * |
| 134 | std::chrono::duration_cast<std::chrono::duration<double>>( |
| 135 | channel_storage_duration_) |
| 136 | .count(); |
| 137 | } |
| 138 | |
| 139 | // The number of extra buffers (beyond the queue) we pretend to have. |
| 140 | int number_scratch_buffers() const { |
| 141 | // We need to start creating messages before we know how many |
| 142 | // senders+readers we'll have, so we need to just pick something which is |
| 143 | // always big enough. |
| 144 | return 50; |
| 145 | } |
| 146 | |
| 147 | int number_buffers() const { return queue_size() + number_scratch_buffers(); } |
| 148 | |
| 149 | int GetBufferIndex() { |
| 150 | CHECK(!available_buffer_indices_.empty()) << ": This should be impossible"; |
| 151 | const int result = available_buffer_indices_.back(); |
| 152 | available_buffer_indices_.pop_back(); |
| 153 | return result; |
| 154 | } |
| 155 | |
| 156 | void FreeBufferIndex(int i) { |
| 157 | DCHECK(std::find(available_buffer_indices_.begin(), |
| 158 | available_buffer_indices_.end(), |
| 159 | i) == available_buffer_indices_.end()) |
| 160 | << ": Buffer is not in use: " << i; |
| 161 | available_buffer_indices_.push_back(i); |
| 162 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | |
| 164 | // Makes a connected raw sender which calls Send below. |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 165 | ::std::unique_ptr<RawSender> MakeRawSender(SimulatedEventLoop *event_loop); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 166 | |
| 167 | // Makes a connected raw fetcher. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 168 | ::std::unique_ptr<RawFetcher> MakeRawFetcher(EventLoop *event_loop); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 169 | |
| 170 | // Registers a watcher for the queue. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 171 | void MakeRawWatcher(SimulatedWatcher *watcher); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 172 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 173 | void RemoveWatcher(SimulatedWatcher *watcher) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 174 | watchers_.erase(std::find(watchers_.begin(), watchers_.end(), watcher)); |
| 175 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 177 | // Sends the message to all the connected receivers and fetchers. Returns the |
| 178 | // sent queue index. |
| 179 | uint32_t Send(std::shared_ptr<SimulatedMessage> message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 180 | |
| 181 | // Unregisters a fetcher. |
| 182 | void UnregisterFetcher(SimulatedFetcher *fetcher); |
| 183 | |
| 184 | std::shared_ptr<SimulatedMessage> latest_message() { return latest_message_; } |
| 185 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 186 | size_t max_size() const { return channel()->max_size(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 187 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 188 | const std::string_view name() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 189 | return channel()->name()->string_view(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 192 | const Channel *channel() const { return channel_; } |
| 193 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 194 | void CountSenderCreated() { |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 195 | CheckBufferCount(); |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 196 | if (sender_count_ >= channel()->num_senders()) { |
| 197 | LOG(FATAL) << "Failed to create sender on " |
| 198 | << configuration::CleanedChannelToString(channel()) |
| 199 | << ", too many senders."; |
| 200 | } |
| 201 | ++sender_count_; |
| 202 | } |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 203 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 204 | void CountSenderDestroyed() { |
| 205 | --sender_count_; |
| 206 | CHECK_GE(sender_count_, 0); |
| 207 | } |
| 208 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 209 | private: |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 210 | void CheckBufferCount() { |
| 211 | int reader_count = 0; |
| 212 | if (channel()->read_method() == ReadMethod::PIN) { |
| 213 | reader_count = watchers_.size() + fetchers_.size(); |
| 214 | } |
| 215 | CHECK_LT(reader_count + sender_count_, number_scratch_buffers()); |
| 216 | } |
| 217 | |
| 218 | void CheckReaderCount() { |
| 219 | if (channel()->read_method() != ReadMethod::PIN) { |
| 220 | return; |
| 221 | } |
| 222 | CheckBufferCount(); |
| 223 | const int reader_count = watchers_.size() + fetchers_.size(); |
| 224 | if (reader_count >= channel()->num_readers()) { |
| 225 | LOG(FATAL) << "Failed to create reader on " |
| 226 | << configuration::CleanedChannelToString(channel()) |
| 227 | << ", too many readers."; |
| 228 | } |
| 229 | } |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 230 | |
| 231 | const Channel *const channel_; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 232 | const std::chrono::nanoseconds channel_storage_duration_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 233 | |
| 234 | // List of all watchers. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 235 | ::std::vector<SimulatedWatcher *> watchers_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 236 | |
| 237 | // List of all fetchers. |
| 238 | ::std::vector<SimulatedFetcher *> fetchers_; |
| 239 | std::shared_ptr<SimulatedMessage> latest_message_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 240 | |
| 241 | ipc_lib::QueueIndex next_queue_index_; |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 242 | |
| 243 | int sender_count_ = 0; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 244 | |
| 245 | std::vector<uint16_t> available_buffer_indices_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | namespace { |
| 249 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 250 | std::shared_ptr<SimulatedMessage> SimulatedMessage::Make( |
| 251 | SimulatedChannel *channel) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 252 | // The allocations in here are due to infrastructure and don't count in the no |
| 253 | // mallocs in RT code. |
| 254 | ScopedNotRealtime nrt; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 255 | const size_t size = channel->max_size(); |
| 256 | SimulatedMessage *const message = reinterpret_cast<SimulatedMessage *>( |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 257 | malloc(sizeof(SimulatedMessage) + size + kChannelDataAlignment - 1)); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 258 | new (message) SimulatedMessage(channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 259 | message->context.size = size; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 260 | message->context.data = message->data(size); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 261 | |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 262 | return std::shared_ptr<SimulatedMessage>(message, |
| 263 | &SimulatedMessage::DestroyAndFree); |
| 264 | } |
| 265 | |
| 266 | SimulatedMessage::SimulatedMessage(SimulatedChannel *channel_in) |
| 267 | : channel(channel_in) { |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 268 | context.buffer_index = channel->GetBufferIndex(); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | SimulatedMessage::~SimulatedMessage() { |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 272 | channel->FreeBufferIndex(context.buffer_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | class SimulatedSender : public RawSender { |
| 276 | public: |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 277 | SimulatedSender(SimulatedChannel *simulated_channel, |
| 278 | SimulatedEventLoop *event_loop); |
| 279 | ~SimulatedSender() override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 280 | |
| 281 | void *data() override { |
| 282 | if (!message_) { |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 283 | message_ = SimulatedMessage::Make(simulated_channel_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 284 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 285 | return message_->data(simulated_channel_->max_size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | size_t size() override { return simulated_channel_->max_size(); } |
| 289 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 290 | bool DoSend(size_t length, |
| 291 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 292 | aos::realtime_clock::time_point realtime_remote_time, |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 293 | uint32_t remote_queue_index) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 294 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 295 | bool DoSend(const void *msg, size_t size, |
| 296 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 297 | aos::realtime_clock::time_point realtime_remote_time, |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 298 | uint32_t remote_queue_index) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 299 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 300 | int buffer_index() override { |
| 301 | // First, ensure message_ is allocated. |
| 302 | data(); |
| 303 | return message_->context.buffer_index; |
| 304 | } |
| 305 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 306 | private: |
| 307 | SimulatedChannel *simulated_channel_; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 308 | SimulatedEventLoop *event_loop_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 309 | |
| 310 | std::shared_ptr<SimulatedMessage> message_; |
| 311 | }; |
| 312 | } // namespace |
| 313 | |
| 314 | class SimulatedFetcher : public RawFetcher { |
| 315 | public: |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 316 | explicit SimulatedFetcher(EventLoop *event_loop, |
| 317 | SimulatedChannel *simulated_channel) |
| 318 | : RawFetcher(event_loop, simulated_channel->channel()), |
| 319 | simulated_channel_(simulated_channel) {} |
| 320 | ~SimulatedFetcher() { simulated_channel_->UnregisterFetcher(this); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 321 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 322 | std::pair<bool, monotonic_clock::time_point> DoFetchNext() override { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 323 | // The allocations in here are due to infrastructure and don't count in the |
| 324 | // no mallocs in RT code. |
| 325 | ScopedNotRealtime nrt; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 326 | if (msgs_.size() == 0) { |
| 327 | return std::make_pair(false, monotonic_clock::min_time); |
| 328 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 329 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 330 | CHECK(!fell_behind_) << ": Got behind on " |
| 331 | << configuration::StrippedChannelToString( |
| 332 | simulated_channel_->channel()); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 333 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 334 | SetMsg(msgs_.front()); |
| 335 | msgs_.pop_front(); |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 336 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 339 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 340 | // The allocations in here are due to infrastructure and don't count in the |
| 341 | // no mallocs in RT code. |
| 342 | ScopedNotRealtime nrt; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 343 | if (msgs_.size() == 0) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 344 | // TODO(austin): Can we just do this logic unconditionally? It is a lot |
| 345 | // simpler. And call clear, obviously. |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 346 | if (!msg_ && simulated_channel_->latest_message()) { |
| 347 | SetMsg(simulated_channel_->latest_message()); |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 348 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 349 | } else { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 350 | return std::make_pair(false, monotonic_clock::min_time); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | // We've had a message enqueued, so we don't need to go looking for the |
| 355 | // latest message from before we started. |
| 356 | SetMsg(msgs_.back()); |
| 357 | msgs_.clear(); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 358 | fell_behind_ = false; |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 359 | return std::make_pair(true, event_loop()->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | private: |
| 363 | friend class SimulatedChannel; |
| 364 | |
| 365 | // Updates the state inside RawFetcher to point to the data in msg_. |
| 366 | void SetMsg(std::shared_ptr<SimulatedMessage> msg) { |
| 367 | msg_ = msg; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 368 | context_ = msg_->context; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 369 | if (channel()->read_method() != ReadMethod::PIN) { |
| 370 | context_.buffer_index = -1; |
| 371 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 372 | if (context_.remote_queue_index == 0xffffffffu) { |
| 373 | context_.remote_queue_index = context_.queue_index; |
| 374 | } |
| 375 | if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 376 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 377 | } |
| 378 | if (context_.realtime_remote_time == aos::realtime_clock::min_time) { |
| 379 | context_.realtime_remote_time = context_.realtime_event_time; |
| 380 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // Internal method for Simulation to add a message to the buffer. |
| 384 | void Enqueue(std::shared_ptr<SimulatedMessage> buffer) { |
| 385 | msgs_.emplace_back(buffer); |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 386 | if (fell_behind_ || |
| 387 | msgs_.size() > static_cast<size_t>(simulated_channel_->queue_size())) { |
| 388 | fell_behind_ = true; |
| 389 | // Might as well empty out all the intermediate messages now. |
| 390 | while (msgs_.size() > 1) { |
| 391 | msgs_.pop_front(); |
| 392 | } |
| 393 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 396 | SimulatedChannel *simulated_channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 397 | std::shared_ptr<SimulatedMessage> msg_; |
| 398 | |
| 399 | // Messages queued up but not in use. |
| 400 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 401 | |
| 402 | // Whether we're currently "behind", which means a FetchNext call will fail. |
| 403 | bool fell_behind_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | class SimulatedTimerHandler : public TimerHandler { |
| 407 | public: |
| 408 | explicit SimulatedTimerHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 409 | SimulatedEventLoop *simulated_event_loop, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 410 | ::std::function<void()> fn); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 411 | ~SimulatedTimerHandler() { Disable(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 412 | |
| 413 | void Setup(monotonic_clock::time_point base, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 414 | monotonic_clock::duration repeat_offset) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 415 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 416 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 417 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 418 | void Disable() override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 419 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 420 | private: |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 421 | SimulatedEventLoop *simulated_event_loop_; |
| 422 | EventHandler<SimulatedTimerHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 423 | EventScheduler *scheduler_; |
| 424 | EventScheduler::Token token_; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 425 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 426 | monotonic_clock::time_point base_; |
| 427 | monotonic_clock::duration repeat_offset_; |
| 428 | }; |
| 429 | |
| 430 | class SimulatedPhasedLoopHandler : public PhasedLoopHandler { |
| 431 | public: |
| 432 | SimulatedPhasedLoopHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 433 | SimulatedEventLoop *simulated_event_loop, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 434 | ::std::function<void(int)> fn, |
| 435 | const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 436 | const monotonic_clock::duration offset); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 437 | ~SimulatedPhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 438 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 439 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 440 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 441 | void Schedule(monotonic_clock::time_point sleep_time) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 442 | |
| 443 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 444 | SimulatedEventLoop *simulated_event_loop_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 445 | EventHandler<SimulatedPhasedLoopHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 446 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 447 | EventScheduler *scheduler_; |
| 448 | EventScheduler::Token token_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 449 | }; |
| 450 | |
| 451 | class SimulatedEventLoop : public EventLoop { |
| 452 | public: |
| 453 | explicit SimulatedEventLoop( |
Brian Silverman | 661eb8d | 2020-08-12 19:41:01 -0700 | [diff] [blame] | 454 | EventScheduler *scheduler, NodeEventLoopFactory *node_event_loop_factory, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 455 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> |
| 456 | *channels, |
| 457 | const Configuration *configuration, |
| 458 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 459 | *raw_event_loops, |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 460 | const Node *node, pid_t tid) |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 461 | : EventLoop(CHECK_NOTNULL(configuration), |
| 462 | node_event_loop_factory->boot_uuid()), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 463 | scheduler_(scheduler), |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 464 | node_event_loop_factory_(node_event_loop_factory), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 465 | channels_(channels), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 466 | raw_event_loops_(raw_event_loops), |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 467 | node_(node), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 468 | tid_(tid) { |
| 469 | raw_event_loops_->push_back(std::make_pair(this, [this](bool value) { |
| 470 | if (!has_setup_) { |
| 471 | Setup(); |
| 472 | has_setup_ = true; |
| 473 | } |
| 474 | set_is_running(value); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 475 | has_run_ = true; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 476 | })); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 477 | } |
| 478 | ~SimulatedEventLoop() override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 479 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 480 | // the event loop so the book keeping matches. |
| 481 | timing_report_sender_.reset(); |
| 482 | |
| 483 | // Force everything with a registered fd with epoll to be destroyed now. |
| 484 | timers_.clear(); |
| 485 | phased_loops_.clear(); |
| 486 | watchers_.clear(); |
| 487 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 488 | for (auto it = raw_event_loops_->begin(); it != raw_event_loops_->end(); |
| 489 | ++it) { |
| 490 | if (it->first == this) { |
| 491 | raw_event_loops_->erase(it); |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 497 | bool has_run() const { return has_run_; } |
| 498 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 499 | std::chrono::nanoseconds send_delay() const { return send_delay_; } |
| 500 | void set_send_delay(std::chrono::nanoseconds send_delay) { |
| 501 | send_delay_ = send_delay; |
| 502 | } |
| 503 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 504 | ::aos::monotonic_clock::time_point monotonic_now() override { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 505 | return node_event_loop_factory_->monotonic_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | ::aos::realtime_clock::time_point realtime_now() override { |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 509 | return node_event_loop_factory_->realtime_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | ::std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override; |
| 513 | |
| 514 | ::std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override; |
| 515 | |
| 516 | void MakeRawWatcher( |
| 517 | const Channel *channel, |
| 518 | ::std::function<void(const Context &context, const void *message)> |
| 519 | watcher) override; |
| 520 | |
| 521 | TimerHandler *AddTimer(::std::function<void()> callback) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 522 | CHECK(!is_running()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 523 | return NewTimer(::std::unique_ptr<TimerHandler>( |
| 524 | new SimulatedTimerHandler(scheduler_, this, callback))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback, |
| 528 | const monotonic_clock::duration interval, |
| 529 | const monotonic_clock::duration offset = |
| 530 | ::std::chrono::seconds(0)) override { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 531 | return NewPhasedLoop( |
| 532 | ::std::unique_ptr<PhasedLoopHandler>(new SimulatedPhasedLoopHandler( |
| 533 | scheduler_, this, callback, interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void OnRun(::std::function<void()> on_run) override { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 537 | CHECK(!is_running()) << ": Cannot register OnRun callback while running."; |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 538 | scheduler_->ScheduleOnRun([this, on_run = std::move(on_run)]() { |
| 539 | ScopedMarkRealtimeRestorer rt(priority() > 0); |
| 540 | on_run(); |
| 541 | }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 544 | const Node *node() const override { return node_; } |
| 545 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 546 | void set_name(const std::string_view name) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 547 | name_ = std::string(name); |
| 548 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 549 | const std::string_view name() const override { return name_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 550 | |
| 551 | SimulatedChannel *GetSimulatedChannel(const Channel *channel); |
| 552 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 553 | void SetRuntimeRealtimePriority(int priority) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 554 | CHECK(!is_running()) << ": Cannot set realtime priority while running."; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 555 | priority_ = priority; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 558 | int priority() const override { return priority_; } |
| 559 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 560 | void SetRuntimeAffinity(const cpu_set_t & /*cpuset*/) override { |
| 561 | CHECK(!is_running()) << ": Cannot set affinity while running."; |
| 562 | } |
| 563 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 564 | void Setup() { |
| 565 | MaybeScheduleTimingReports(); |
| 566 | if (!skip_logger_) { |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 567 | log_sender_.Initialize(MakeSender<logging::LogMessageFbs>("/aos")); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 568 | log_impl_ = log_sender_.implementation(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 569 | } |
| 570 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 571 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 572 | int NumberBuffers(const Channel *channel) override; |
| 573 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 574 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 575 | friend class SimulatedTimerHandler; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 576 | friend class SimulatedPhasedLoopHandler; |
| 577 | friend class SimulatedWatcher; |
| 578 | |
| 579 | void HandleEvent() { |
| 580 | while (true) { |
| 581 | if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) { |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | EventLoopEvent *event = PopEvent(); |
| 586 | event->HandleEvent(); |
| 587 | } |
| 588 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 589 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 590 | pid_t GetTid() override { return tid_; } |
| 591 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 592 | EventScheduler *scheduler_; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 593 | NodeEventLoopFactory *node_event_loop_factory_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 594 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_; |
| 595 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 596 | *raw_event_loops_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 597 | |
| 598 | ::std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 599 | |
| 600 | int priority_ = 0; |
| 601 | |
| 602 | bool has_setup_ = false; |
| 603 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 604 | std::chrono::nanoseconds send_delay_; |
| 605 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 606 | const Node *const node_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 607 | const pid_t tid_; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 608 | |
| 609 | AosLogToFbs log_sender_; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 610 | std::shared_ptr<logging::LogImplementation> log_impl_ = nullptr; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 611 | |
| 612 | bool has_run_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 613 | }; |
| 614 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 615 | void SimulatedEventLoopFactory::set_send_delay( |
| 616 | std::chrono::nanoseconds send_delay) { |
| 617 | send_delay_ = send_delay; |
| 618 | for (std::pair<EventLoop *, std::function<void(bool)>> &loop : |
| 619 | raw_event_loops_) { |
| 620 | reinterpret_cast<SimulatedEventLoop *>(loop.first) |
| 621 | ->set_send_delay(send_delay_); |
| 622 | } |
| 623 | } |
| 624 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 625 | void SimulatedEventLoop::MakeRawWatcher( |
| 626 | const Channel *channel, |
| 627 | std::function<void(const Context &channel, const void *message)> watcher) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 628 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 629 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 630 | std::unique_ptr<SimulatedWatcher> shm_watcher( |
| 631 | new SimulatedWatcher(this, scheduler_, channel, std::move(watcher))); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 632 | |
| 633 | GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get()); |
| 634 | NewWatcher(std::move(shm_watcher)); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 635 | |
| 636 | // Order of operations gets kinda wonky if we let people make watchers after |
| 637 | // running once. If someone has a valid use case, we can reconsider. |
| 638 | CHECK(!has_run()) << ": Can't add a watcher after running."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 642 | const Channel *channel) { |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 643 | TakeSender(channel); |
| 644 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 645 | return GetSimulatedChannel(channel)->MakeRawSender(this); |
| 646 | } |
| 647 | |
| 648 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 649 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 650 | ChannelIndex(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 651 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 652 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 653 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 654 | << "\", \"type\": \"" << channel->type()->string_view() |
| 655 | << "\" } is not able to be fetched on this node. Check your " |
| 656 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 659 | return GetSimulatedChannel(channel)->MakeRawFetcher(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel( |
| 663 | const Channel *channel) { |
| 664 | auto it = channels_->find(SimpleChannel(channel)); |
| 665 | if (it == channels_->end()) { |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 666 | it = |
| 667 | channels_ |
| 668 | ->emplace( |
| 669 | SimpleChannel(channel), |
| 670 | std::unique_ptr<SimulatedChannel>(new SimulatedChannel( |
| 671 | channel, std::chrono::nanoseconds( |
| 672 | configuration()->channel_storage_duration())))) |
| 673 | .first; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 674 | } |
| 675 | return it->second.get(); |
| 676 | } |
| 677 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 678 | int SimulatedEventLoop::NumberBuffers(const Channel *channel) { |
| 679 | return GetSimulatedChannel(channel)->number_buffers(); |
| 680 | } |
| 681 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 682 | SimulatedWatcher::SimulatedWatcher( |
| 683 | SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler, |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 684 | const Channel *channel, |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 685 | std::function<void(const Context &context, const void *message)> fn) |
| 686 | : WatcherState(simulated_event_loop, channel, std::move(fn)), |
| 687 | simulated_event_loop_(simulated_event_loop), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 688 | channel_(channel), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 689 | scheduler_(scheduler), |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 690 | event_(this), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 691 | token_(scheduler_->InvalidToken()) {} |
| 692 | |
| 693 | SimulatedWatcher::~SimulatedWatcher() { |
| 694 | simulated_event_loop_->RemoveEvent(&event_); |
| 695 | if (token_ != scheduler_->InvalidToken()) { |
| 696 | scheduler_->Deschedule(token_); |
| 697 | } |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 698 | CHECK_NOTNULL(simulated_channel_)->RemoveWatcher(this); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 701 | bool SimulatedWatcher::has_run() const { |
| 702 | return simulated_event_loop_->has_run(); |
| 703 | } |
| 704 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 705 | void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) { |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 706 | monotonic_clock::time_point event_time = |
| 707 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 708 | |
| 709 | // Messages are queued in order. If we are the first, add ourselves. |
| 710 | // Otherwise, don't. |
| 711 | if (msgs_.size() == 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 712 | event_.set_event_time(message->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 713 | simulated_event_loop_->AddEvent(&event_); |
| 714 | |
| 715 | DoSchedule(event_time); |
| 716 | } |
| 717 | |
| 718 | msgs_.emplace_back(message); |
| 719 | } |
| 720 | |
| 721 | void SimulatedWatcher::HandleEvent() { |
| 722 | CHECK_NE(msgs_.size(), 0u) << ": No events to handle."; |
| 723 | |
| 724 | const monotonic_clock::time_point monotonic_now = |
| 725 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 726 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 727 | if (simulated_event_loop_->log_impl_) { |
| 728 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 729 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 730 | Context context = msgs_.front()->context; |
| 731 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 732 | if (channel_->read_method() != ReadMethod::PIN) { |
| 733 | context.buffer_index = -1; |
| 734 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 735 | if (context.remote_queue_index == 0xffffffffu) { |
| 736 | context.remote_queue_index = context.queue_index; |
| 737 | } |
| 738 | if (context.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 739 | context.monotonic_remote_time = context.monotonic_event_time; |
| 740 | } |
| 741 | if (context.realtime_remote_time == aos::realtime_clock::min_time) { |
| 742 | context.realtime_remote_time = context.realtime_event_time; |
| 743 | } |
| 744 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 745 | { |
| 746 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 747 | DoCallCallback([monotonic_now]() { return monotonic_now; }, context); |
| 748 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 749 | |
| 750 | msgs_.pop_front(); |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 751 | if (token_ != scheduler_->InvalidToken()) { |
| 752 | scheduler_->Deschedule(token_); |
| 753 | token_ = scheduler_->InvalidToken(); |
| 754 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 755 | if (msgs_.size() != 0) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 756 | event_.set_event_time(msgs_.front()->context.monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 757 | simulated_event_loop_->AddEvent(&event_); |
| 758 | |
| 759 | DoSchedule(event_.event_time()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) { |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 764 | CHECK(token_ == scheduler_->InvalidToken()) |
| 765 | << ": May not schedule multiple times"; |
| 766 | token_ = scheduler_->Schedule( |
| 767 | event_time + simulated_event_loop_->send_delay(), [this]() { |
| 768 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 769 | token_ = scheduler_->InvalidToken(); |
| 770 | simulated_event_loop_->HandleEvent(); |
| 771 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 775 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 776 | watcher->SetSimulatedChannel(this); |
| 777 | watchers_.emplace_back(watcher); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | ::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender( |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 781 | SimulatedEventLoop *event_loop) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 782 | return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
| 783 | } |
| 784 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 785 | ::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher( |
| 786 | EventLoop *event_loop) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 787 | CheckReaderCount(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 788 | ::std::unique_ptr<SimulatedFetcher> fetcher( |
| 789 | new SimulatedFetcher(event_loop, this)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 790 | fetchers_.push_back(fetcher.get()); |
| 791 | return ::std::move(fetcher); |
| 792 | } |
| 793 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 794 | uint32_t SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) { |
| 795 | const uint32_t queue_index = next_queue_index_.index(); |
| 796 | message->context.queue_index = queue_index; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 797 | message->context.data = message->data(channel()->max_size()) + |
| 798 | channel()->max_size() - message->context.size; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 799 | next_queue_index_ = next_queue_index_.Increment(); |
| 800 | |
| 801 | latest_message_ = message; |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 802 | for (SimulatedWatcher *watcher : watchers_) { |
| 803 | if (watcher->has_run()) { |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 804 | watcher->Schedule(message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | for (auto &fetcher : fetchers_) { |
| 808 | fetcher->Enqueue(message); |
| 809 | } |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 810 | |
| 811 | return queue_index; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) { |
| 815 | fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 816 | } |
| 817 | |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 818 | SimulatedSender::SimulatedSender(SimulatedChannel *simulated_channel, |
| 819 | SimulatedEventLoop *event_loop) |
| 820 | : RawSender(event_loop, simulated_channel->channel()), |
| 821 | simulated_channel_(simulated_channel), |
| 822 | event_loop_(event_loop) { |
| 823 | simulated_channel_->CountSenderCreated(); |
| 824 | } |
| 825 | |
| 826 | SimulatedSender::~SimulatedSender() { |
| 827 | simulated_channel_->CountSenderDestroyed(); |
| 828 | } |
| 829 | |
| 830 | bool SimulatedSender::DoSend( |
| 831 | size_t length, aos::monotonic_clock::time_point monotonic_remote_time, |
| 832 | aos::realtime_clock::time_point realtime_remote_time, |
| 833 | uint32_t remote_queue_index) { |
| 834 | // The allocations in here are due to infrastructure and don't count in the |
| 835 | // no mallocs in RT code. |
| 836 | ScopedNotRealtime nrt; |
| 837 | CHECK_LE(length, size()) << ": Attempting to send too big a message."; |
| 838 | message_->context.monotonic_event_time = event_loop_->monotonic_now(); |
| 839 | message_->context.monotonic_remote_time = monotonic_remote_time; |
| 840 | message_->context.remote_queue_index = remote_queue_index; |
| 841 | message_->context.realtime_event_time = event_loop_->realtime_now(); |
| 842 | message_->context.realtime_remote_time = realtime_remote_time; |
| 843 | CHECK_LE(length, message_->context.size); |
| 844 | message_->context.size = length; |
| 845 | |
| 846 | // TODO(austin): Track sending too fast. |
| 847 | sent_queue_index_ = simulated_channel_->Send(message_); |
| 848 | monotonic_sent_time_ = event_loop_->monotonic_now(); |
| 849 | realtime_sent_time_ = event_loop_->realtime_now(); |
| 850 | |
| 851 | // Drop the reference to the message so that we allocate a new message for |
| 852 | // next time. Otherwise we will continue to reuse the same memory for all |
| 853 | // messages and corrupt it. |
| 854 | message_.reset(); |
| 855 | return true; |
| 856 | } |
| 857 | |
| 858 | bool SimulatedSender::DoSend( |
| 859 | const void *msg, size_t size, |
| 860 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 861 | aos::realtime_clock::time_point realtime_remote_time, |
| 862 | uint32_t remote_queue_index) { |
Austin Schuh | 102667e | 2020-12-11 20:13:28 -0800 | [diff] [blame] | 863 | CHECK_LE(size, this->size()) |
| 864 | << ": Attempting to send too big a message on " |
| 865 | << configuration::CleanedChannelToString(simulated_channel_->channel()); |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 866 | |
| 867 | // This is wasteful, but since flatbuffers fill from the back end of the |
| 868 | // queue, we need it to be full sized. |
| 869 | message_ = SimulatedMessage::Make(simulated_channel_); |
| 870 | |
| 871 | // Now fill in the message. size is already populated above, and |
| 872 | // queue_index will be populated in simulated_channel_. Put this at the |
| 873 | // back of the data segment. |
| 874 | memcpy(message_->data(simulated_channel_->max_size()) + |
| 875 | simulated_channel_->max_size() - size, |
| 876 | msg, size); |
| 877 | |
| 878 | return DoSend(size, monotonic_remote_time, realtime_remote_time, |
| 879 | remote_queue_index); |
| 880 | } |
| 881 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 882 | SimulatedTimerHandler::SimulatedTimerHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 883 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 884 | ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 885 | : TimerHandler(simulated_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 886 | simulated_event_loop_(simulated_event_loop), |
| 887 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 888 | scheduler_(scheduler), |
| 889 | token_(scheduler_->InvalidToken()) {} |
| 890 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 891 | void SimulatedTimerHandler::Setup(monotonic_clock::time_point base, |
| 892 | monotonic_clock::duration repeat_offset) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 893 | // The allocations in here are due to infrastructure and don't count in the no |
| 894 | // mallocs in RT code. |
| 895 | ScopedNotRealtime nrt; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 896 | Disable(); |
| 897 | const ::aos::monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 898 | simulated_event_loop_->monotonic_now(); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 899 | base_ = base; |
| 900 | repeat_offset_ = repeat_offset; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 901 | token_ = scheduler_->Schedule(std::max(base, monotonic_now), [this]() { |
| 902 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 903 | token_ = scheduler_->InvalidToken(); |
| 904 | simulated_event_loop_->HandleEvent(); |
| 905 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 906 | event_.set_event_time(base_); |
| 907 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | void SimulatedTimerHandler::HandleEvent() { |
| 911 | const ::aos::monotonic_clock::time_point monotonic_now = |
Austin Schuh | a5e1419 | 2020-01-06 18:02:41 -0800 | [diff] [blame] | 912 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 913 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 914 | if (simulated_event_loop_->log_impl_) { |
| 915 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 916 | } |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 917 | if (token_ != scheduler_->InvalidToken()) { |
| 918 | scheduler_->Deschedule(token_); |
| 919 | token_ = scheduler_->InvalidToken(); |
| 920 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 921 | if (repeat_offset_ != ::aos::monotonic_clock::zero()) { |
| 922 | // Reschedule. |
| 923 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 924 | token_ = scheduler_->Schedule(base_, [this]() { |
| 925 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 926 | token_ = scheduler_->InvalidToken(); |
| 927 | simulated_event_loop_->HandleEvent(); |
| 928 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 929 | event_.set_event_time(base_); |
| 930 | simulated_event_loop_->AddEvent(&event_); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 931 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 932 | |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 933 | { |
| 934 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 935 | Call([monotonic_now]() { return monotonic_now; }, monotonic_now); |
| 936 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 939 | void SimulatedTimerHandler::Disable() { |
| 940 | simulated_event_loop_->RemoveEvent(&event_); |
| 941 | if (token_ != scheduler_->InvalidToken()) { |
| 942 | scheduler_->Deschedule(token_); |
| 943 | token_ = scheduler_->InvalidToken(); |
| 944 | } |
| 945 | } |
| 946 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 947 | SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 948 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 949 | ::std::function<void(int)> fn, const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 950 | const monotonic_clock::duration offset) |
| 951 | : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset), |
| 952 | simulated_event_loop_(simulated_event_loop), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 953 | event_(this), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 954 | scheduler_(scheduler), |
| 955 | token_(scheduler_->InvalidToken()) {} |
| 956 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 957 | SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() { |
| 958 | if (token_ != scheduler_->InvalidToken()) { |
| 959 | scheduler_->Deschedule(token_); |
| 960 | token_ = scheduler_->InvalidToken(); |
| 961 | } |
| 962 | simulated_event_loop_->RemoveEvent(&event_); |
| 963 | } |
| 964 | |
| 965 | void SimulatedPhasedLoopHandler::HandleEvent() { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 966 | monotonic_clock::time_point monotonic_now = |
| 967 | simulated_event_loop_->monotonic_now(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 968 | logging::ScopedLogRestorer prev_logger; |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 969 | if (simulated_event_loop_->log_impl_) { |
| 970 | prev_logger.Swap(simulated_event_loop_->log_impl_); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 971 | } |
Austin Schuh | cc6070c | 2020-10-10 20:25:56 -0700 | [diff] [blame] | 972 | |
| 973 | { |
| 974 | ScopedMarkRealtimeRestorer rt(simulated_event_loop_->priority() > 0); |
| 975 | Call([monotonic_now]() { return monotonic_now; }, |
| 976 | [this](monotonic_clock::time_point sleep_time) { |
| 977 | Schedule(sleep_time); |
| 978 | }); |
| 979 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 980 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 981 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 982 | void SimulatedPhasedLoopHandler::Schedule( |
| 983 | monotonic_clock::time_point sleep_time) { |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 984 | // The allocations in here are due to infrastructure and don't count in the no |
| 985 | // mallocs in RT code. |
| 986 | ScopedNotRealtime nrt; |
Austin Schuh | eb4e4ce | 2020-09-10 23:04:18 -0700 | [diff] [blame] | 987 | if (token_ != scheduler_->InvalidToken()) { |
| 988 | scheduler_->Deschedule(token_); |
| 989 | token_ = scheduler_->InvalidToken(); |
| 990 | } |
| 991 | token_ = scheduler_->Schedule(sleep_time, [this]() { |
| 992 | DCHECK(token_ != scheduler_->InvalidToken()); |
| 993 | token_ = scheduler_->InvalidToken(); |
| 994 | simulated_event_loop_->HandleEvent(); |
| 995 | }); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 996 | event_.set_event_time(sleep_time); |
| 997 | simulated_event_loop_->AddEvent(&event_); |
| 998 | } |
| 999 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1000 | NodeEventLoopFactory::NodeEventLoopFactory( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1001 | EventSchedulerScheduler *scheduler_scheduler, |
| 1002 | SimulatedEventLoopFactory *factory, const Node *node, |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1003 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 1004 | *raw_event_loops) |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1005 | : factory_(factory), node_(node), raw_event_loops_(raw_event_loops) { |
| 1006 | scheduler_scheduler->AddEventScheduler(&scheduler_); |
| 1007 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1008 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1009 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 1010 | const Configuration *configuration) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1011 | : configuration_(CHECK_NOTNULL(configuration)), |
| 1012 | nodes_(configuration::GetNodes(configuration_)) { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 1013 | CHECK(IsInitialized()) << ": Need to initialize AOS first."; |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1014 | for (const Node *node : nodes_) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1015 | node_factories_.emplace_back(new NodeEventLoopFactory( |
| 1016 | &scheduler_scheduler_, this, node, &raw_event_loops_)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1017 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1018 | |
| 1019 | if (configuration::MultiNode(configuration)) { |
| 1020 | bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this); |
| 1021 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1024 | SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {} |
| 1025 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1026 | NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory( |
| 1027 | const Node *node) { |
| 1028 | auto result = std::find_if( |
| 1029 | node_factories_.begin(), node_factories_.end(), |
| 1030 | [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) { |
| 1031 | return node_factory->node() == node; |
| 1032 | }); |
| 1033 | |
| 1034 | CHECK(result != node_factories_.end()) |
| 1035 | << ": Failed to find node " << FlatbufferToJson(node); |
| 1036 | |
| 1037 | return result->get(); |
| 1038 | } |
| 1039 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1040 | void SimulatedEventLoopFactory::SetTimeConverter( |
| 1041 | TimeConverter *time_converter) { |
| 1042 | for (std::unique_ptr<NodeEventLoopFactory> &factory : node_factories_) { |
| 1043 | factory->SetTimeConverter(time_converter); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 1047 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop( |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1048 | std::string_view name, const Node *node) { |
| 1049 | if (node == nullptr) { |
| 1050 | CHECK(!configuration::MultiNode(configuration())) |
| 1051 | << ": Can't make a single node event loop in a multi-node world."; |
| 1052 | } else { |
| 1053 | CHECK(configuration::MultiNode(configuration())) |
| 1054 | << ": Can't make a multi-node event loop in a single-node world."; |
| 1055 | } |
| 1056 | return GetNodeEventLoopFactory(node)->MakeEventLoop(name); |
| 1057 | } |
| 1058 | |
| 1059 | ::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop( |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 1060 | std::string_view name) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1061 | CHECK(!scheduler_.is_running()) |
| 1062 | << ": Can't create an event loop while running"; |
| 1063 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1064 | pid_t tid = tid_; |
| 1065 | ++tid_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1066 | ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1067 | &scheduler_, this, &channels_, factory_->configuration(), |
| 1068 | raw_event_loops_, node_, tid)); |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 1069 | result->set_name(name); |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1070 | result->set_send_delay(factory_->send_delay()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1071 | return std::move(result); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 1074 | void NodeEventLoopFactory::Disconnect(const Node *other) { |
| 1075 | factory_->bridge_->Disconnect(node_, other); |
| 1076 | } |
| 1077 | void NodeEventLoopFactory::Connect(const Node *other) { |
| 1078 | factory_->bridge_->Connect(node_, other); |
| 1079 | } |
| 1080 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1081 | void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) { |
| 1082 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 1083 | raw_event_loops_) { |
| 1084 | event_loop.second(true); |
| 1085 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1086 | scheduler_scheduler_.RunFor(duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1087 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 1088 | raw_event_loops_) { |
| 1089 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | void SimulatedEventLoopFactory::Run() { |
| 1094 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 1095 | raw_event_loops_) { |
| 1096 | event_loop.second(true); |
| 1097 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1098 | scheduler_scheduler_.Run(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1099 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 1100 | raw_event_loops_) { |
| 1101 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 1105 | void SimulatedEventLoopFactory::Exit() { scheduler_scheduler_.Exit(); } |
Austin Schuh | 8fb315a | 2020-11-19 22:33:58 -0800 | [diff] [blame] | 1106 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1107 | void SimulatedEventLoopFactory::DisableForwarding(const Channel *channel) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1108 | CHECK(bridge_) << ": Can't disable forwarding without a message bridge."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1109 | bridge_->DisableForwarding(channel); |
| 1110 | } |
| 1111 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 1112 | void SimulatedEventLoopFactory::DisableStatistics() { |
| 1113 | CHECK(bridge_) << ": Can't disable statistics without a message bridge."; |
| 1114 | bridge_->DisableStatistics(); |
| 1115 | } |
| 1116 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1117 | } // namespace aos |