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