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