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> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | |
| 7 | #include "absl/container/btree_map.h" |
| 8 | #include "absl/container/btree_set.h" |
| 9 | #include "aos/json_to_flatbuffer.h" |
| 10 | #include "aos/util/phased_loop.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | // Container for both a message, and the context for it for simulation. This |
| 15 | // makes tracking the timestamps associated with the data easy. |
| 16 | struct SimulatedMessage { |
| 17 | // Struct to let us force data to be well aligned. |
| 18 | struct OveralignedChar { |
| 19 | char data alignas(32); |
| 20 | }; |
| 21 | |
| 22 | // Context for the data. |
| 23 | Context context; |
| 24 | |
| 25 | // The data. |
| 26 | char *data() { return reinterpret_cast<char *>(&actual_data[0]); } |
| 27 | |
| 28 | // Then the data. |
| 29 | OveralignedChar actual_data[]; |
| 30 | }; |
| 31 | |
| 32 | class SimulatedFetcher; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 33 | class SimulatedChannel; |
| 34 | |
| 35 | class ShmWatcher : public WatcherState { |
| 36 | public: |
| 37 | ShmWatcher( |
| 38 | EventLoop *event_loop, const Channel *channel, |
| 39 | std::function<void(const Context &context, const void *message)> fn) |
| 40 | : WatcherState(event_loop, channel, std::move(fn)), |
| 41 | event_loop_(event_loop) {} |
| 42 | |
| 43 | ~ShmWatcher() override; |
| 44 | |
| 45 | void Call(const Context &context) { |
| 46 | const monotonic_clock::time_point monotonic_now = |
| 47 | event_loop_->monotonic_now(); |
| 48 | DoCallCallback([monotonic_now]() { return monotonic_now; }, context); |
| 49 | } |
| 50 | |
| 51 | void Startup(EventLoop * /*event_loop*/) override {} |
| 52 | |
| 53 | void Schedule(EventScheduler *scheduler, |
| 54 | std::shared_ptr<SimulatedMessage> message) { |
| 55 | // TODO(austin): Track the token once we schedule in the future. |
| 56 | // TODO(austin): Schedule wakeup in the future so we don't have 0 latency. |
| 57 | scheduler->Schedule(scheduler->monotonic_now(), |
| 58 | [this, message]() { Call(message->context); }); |
| 59 | } |
| 60 | |
| 61 | void SetSimulatedChannel(SimulatedChannel *channel) { |
| 62 | simulated_channel_ = channel; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | EventLoop *event_loop_; |
| 67 | SimulatedChannel *simulated_channel_ = nullptr; |
| 68 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 69 | |
| 70 | class SimulatedChannel { |
| 71 | public: |
| 72 | explicit SimulatedChannel(const Channel *channel, EventScheduler *scheduler) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 73 | : channel_(channel), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | scheduler_(scheduler), |
| 75 | next_queue_index_(ipc_lib::QueueIndex::Zero(channel->max_size())) {} |
| 76 | |
| 77 | ~SimulatedChannel() { CHECK_EQ(0u, fetchers_.size()); } |
| 78 | |
| 79 | // Makes a connected raw sender which calls Send below. |
| 80 | ::std::unique_ptr<RawSender> MakeRawSender(EventLoop *event_loop); |
| 81 | |
| 82 | // Makes a connected raw fetcher. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 83 | ::std::unique_ptr<RawFetcher> MakeRawFetcher(EventLoop *event_loop); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | |
| 85 | // Registers a watcher for the queue. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 86 | void MakeRawWatcher(ShmWatcher *watcher); |
| 87 | |
| 88 | void RemoveWatcher(ShmWatcher *watcher) { |
| 89 | watchers_.erase(std::find(watchers_.begin(), watchers_.end(), watcher)); |
| 90 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | |
| 92 | // Sends the message to all the connected receivers and fetchers. |
| 93 | void Send(std::shared_ptr<SimulatedMessage> message); |
| 94 | |
| 95 | // Unregisters a fetcher. |
| 96 | void UnregisterFetcher(SimulatedFetcher *fetcher); |
| 97 | |
| 98 | std::shared_ptr<SimulatedMessage> latest_message() { return latest_message_; } |
| 99 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 100 | size_t max_size() const { return channel()->max_size(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 101 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 102 | const std::string_view name() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 103 | return channel()->name()->string_view(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 106 | const Channel *channel() const { return channel_; } |
| 107 | |
| 108 | ::aos::monotonic_clock::time_point monotonic_now() const { |
| 109 | return scheduler_->monotonic_now(); |
| 110 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 111 | |
| 112 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 113 | const Channel *channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 114 | |
| 115 | // List of all watchers. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 116 | ::std::vector<ShmWatcher *> watchers_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 117 | |
| 118 | // List of all fetchers. |
| 119 | ::std::vector<SimulatedFetcher *> fetchers_; |
| 120 | std::shared_ptr<SimulatedMessage> latest_message_; |
| 121 | EventScheduler *scheduler_; |
| 122 | |
| 123 | ipc_lib::QueueIndex next_queue_index_; |
| 124 | }; |
| 125 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 126 | ShmWatcher::~ShmWatcher() { simulated_channel_->RemoveWatcher(this); } |
| 127 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | namespace { |
| 129 | |
| 130 | // Creates a SimulatedMessage with size bytes of storage. |
| 131 | // This is a shared_ptr so we don't have to implement refcounting or copying. |
| 132 | std::shared_ptr<SimulatedMessage> MakeSimulatedMessage(size_t size) { |
| 133 | SimulatedMessage *message = reinterpret_cast<SimulatedMessage *>( |
| 134 | malloc(sizeof(SimulatedMessage) + size)); |
| 135 | message->context.size = size; |
| 136 | message->context.data = message->data(); |
| 137 | |
| 138 | return std::shared_ptr<SimulatedMessage>(message, free); |
| 139 | } |
| 140 | |
| 141 | class SimulatedSender : public RawSender { |
| 142 | public: |
| 143 | SimulatedSender(SimulatedChannel *simulated_channel, EventLoop *event_loop) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 144 | : RawSender(event_loop, simulated_channel->channel()), |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 145 | simulated_channel_(simulated_channel), |
| 146 | event_loop_(event_loop) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 147 | ~SimulatedSender() {} |
| 148 | |
| 149 | void *data() override { |
| 150 | if (!message_) { |
| 151 | message_ = MakeSimulatedMessage(simulated_channel_->max_size()); |
| 152 | } |
| 153 | return message_->data(); |
| 154 | } |
| 155 | |
| 156 | size_t size() override { return simulated_channel_->max_size(); } |
| 157 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 158 | bool DoSend(size_t length) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 159 | CHECK_LE(length, size()) << ": Attempting to send too big a message."; |
| 160 | message_->context.monotonic_sent_time = event_loop_->monotonic_now(); |
| 161 | message_->context.realtime_sent_time = event_loop_->realtime_now(); |
| 162 | CHECK_LE(length, message_->context.size); |
| 163 | message_->context.size = length; |
| 164 | |
| 165 | // TODO(austin): Track sending too fast. |
| 166 | simulated_channel_->Send(message_); |
| 167 | |
| 168 | // Drop the reference to the message so that we allocate a new message for |
| 169 | // next time. Otherwise we will continue to reuse the same memory for all |
| 170 | // messages and corrupt it. |
| 171 | message_.reset(); |
| 172 | return true; |
| 173 | } |
| 174 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 175 | bool DoSend(const void *msg, size_t size) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | CHECK_LE(size, this->size()) << ": Attempting to send too big a message."; |
| 177 | |
| 178 | // This is wasteful, but since flatbuffers fill from the back end of the |
| 179 | // queue, we need it to be full sized. |
| 180 | message_ = MakeSimulatedMessage(simulated_channel_->max_size()); |
| 181 | |
| 182 | // Now fill in the message. size is already populated above, and |
| 183 | // queue_index will be populated in queue_. Put this at the back of the |
| 184 | // data segment. |
| 185 | memcpy(message_->data() + simulated_channel_->max_size() - size, msg, size); |
| 186 | |
| 187 | return Send(size); |
| 188 | } |
| 189 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 190 | private: |
| 191 | SimulatedChannel *simulated_channel_; |
| 192 | EventLoop *event_loop_; |
| 193 | |
| 194 | std::shared_ptr<SimulatedMessage> message_; |
| 195 | }; |
| 196 | } // namespace |
| 197 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 198 | class SimulatedEventLoop; |
| 199 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | class SimulatedFetcher : public RawFetcher { |
| 201 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 202 | explicit SimulatedFetcher(EventLoop *event_loop, SimulatedChannel *queue) |
| 203 | : RawFetcher(event_loop, queue->channel()), queue_(queue) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 204 | ~SimulatedFetcher() { queue_->UnregisterFetcher(this); } |
| 205 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 206 | std::pair<bool, monotonic_clock::time_point> DoFetchNext() override { |
| 207 | if (msgs_.size() == 0) { |
| 208 | return std::make_pair(false, monotonic_clock::min_time); |
| 209 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 210 | |
| 211 | SetMsg(msgs_.front()); |
| 212 | msgs_.pop_front(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 213 | return std::make_pair(true, queue_->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 216 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 217 | if (msgs_.size() == 0) { |
| 218 | if (!msg_ && queue_->latest_message()) { |
| 219 | SetMsg(queue_->latest_message()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 220 | return std::make_pair(true, queue_->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 221 | } else { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 222 | return std::make_pair(false, monotonic_clock::min_time); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | // We've had a message enqueued, so we don't need to go looking for the |
| 227 | // latest message from before we started. |
| 228 | SetMsg(msgs_.back()); |
| 229 | msgs_.clear(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 230 | return std::make_pair(true, queue_->monotonic_now()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | private: |
| 234 | friend class SimulatedChannel; |
| 235 | |
| 236 | // Updates the state inside RawFetcher to point to the data in msg_. |
| 237 | void SetMsg(std::shared_ptr<SimulatedMessage> msg) { |
| 238 | msg_ = msg; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 239 | context_ = msg_->context; |
| 240 | } |
| 241 | |
| 242 | // Internal method for Simulation to add a message to the buffer. |
| 243 | void Enqueue(std::shared_ptr<SimulatedMessage> buffer) { |
| 244 | msgs_.emplace_back(buffer); |
| 245 | } |
| 246 | |
| 247 | SimulatedChannel *queue_; |
| 248 | std::shared_ptr<SimulatedMessage> msg_; |
| 249 | |
| 250 | // Messages queued up but not in use. |
| 251 | ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_; |
| 252 | }; |
| 253 | |
| 254 | class SimulatedTimerHandler : public TimerHandler { |
| 255 | public: |
| 256 | explicit SimulatedTimerHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 257 | SimulatedEventLoop *simulated_event_loop, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 258 | ::std::function<void()> fn); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 259 | ~SimulatedTimerHandler() {} |
| 260 | |
| 261 | void Setup(monotonic_clock::time_point base, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 262 | monotonic_clock::duration repeat_offset) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 263 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 264 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 265 | |
| 266 | void Disable() override { |
| 267 | if (token_ != scheduler_->InvalidToken()) { |
| 268 | scheduler_->Deschedule(token_); |
| 269 | token_ = scheduler_->InvalidToken(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | ::aos::monotonic_clock::time_point monotonic_now() const { |
| 274 | return scheduler_->monotonic_now(); |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | EventScheduler *scheduler_; |
| 279 | EventScheduler::Token token_; |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 280 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 281 | monotonic_clock::time_point base_; |
| 282 | monotonic_clock::duration repeat_offset_; |
| 283 | }; |
| 284 | |
| 285 | class SimulatedPhasedLoopHandler : public PhasedLoopHandler { |
| 286 | public: |
| 287 | SimulatedPhasedLoopHandler(EventScheduler *scheduler, |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 288 | SimulatedEventLoop *simulated_event_loop, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 289 | ::std::function<void(int)> fn, |
| 290 | const monotonic_clock::duration interval, |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 291 | const monotonic_clock::duration offset); |
| 292 | ~SimulatedPhasedLoopHandler() { |
| 293 | if (token_ != scheduler_->InvalidToken()) { |
| 294 | scheduler_->Deschedule(token_); |
| 295 | token_ = scheduler_->InvalidToken(); |
| 296 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 299 | void HandleTimerWakeup(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 300 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 301 | void Schedule(monotonic_clock::time_point sleep_time) override { |
| 302 | token_ = scheduler_->Schedule(sleep_time, [this]() { HandleTimerWakeup(); }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 306 | SimulatedEventLoop *simulated_event_loop_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 307 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 308 | EventScheduler *scheduler_; |
| 309 | EventScheduler::Token token_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | class SimulatedEventLoop : public EventLoop { |
| 313 | public: |
| 314 | explicit SimulatedEventLoop( |
| 315 | EventScheduler *scheduler, |
| 316 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> |
| 317 | *channels, |
| 318 | const Configuration *configuration, |
| 319 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 320 | *raw_event_loops, |
| 321 | pid_t tid) |
| 322 | : EventLoop(CHECK_NOTNULL(configuration)), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 323 | scheduler_(scheduler), |
| 324 | channels_(channels), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 325 | raw_event_loops_(raw_event_loops), |
| 326 | tid_(tid) { |
| 327 | raw_event_loops_->push_back(std::make_pair(this, [this](bool value) { |
| 328 | if (!has_setup_) { |
| 329 | Setup(); |
| 330 | has_setup_ = true; |
| 331 | } |
| 332 | set_is_running(value); |
| 333 | })); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 334 | } |
| 335 | ~SimulatedEventLoop() override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 336 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 337 | // the event loop so the book keeping matches. |
| 338 | timing_report_sender_.reset(); |
| 339 | |
| 340 | // Force everything with a registered fd with epoll to be destroyed now. |
| 341 | timers_.clear(); |
| 342 | phased_loops_.clear(); |
| 343 | watchers_.clear(); |
| 344 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 345 | for (auto it = raw_event_loops_->begin(); it != raw_event_loops_->end(); |
| 346 | ++it) { |
| 347 | if (it->first == this) { |
| 348 | raw_event_loops_->erase(it); |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | ::aos::monotonic_clock::time_point monotonic_now() override { |
| 355 | return scheduler_->monotonic_now(); |
| 356 | } |
| 357 | |
| 358 | ::aos::realtime_clock::time_point realtime_now() override { |
| 359 | return scheduler_->realtime_now(); |
| 360 | } |
| 361 | |
| 362 | ::std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override; |
| 363 | |
| 364 | ::std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override; |
| 365 | |
| 366 | void MakeRawWatcher( |
| 367 | const Channel *channel, |
| 368 | ::std::function<void(const Context &context, const void *message)> |
| 369 | watcher) override; |
| 370 | |
| 371 | TimerHandler *AddTimer(::std::function<void()> callback) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 372 | CHECK(!is_running()); |
| 373 | return NewTimer(::std::unique_ptr<TimerHandler>( |
| 374 | new SimulatedTimerHandler(scheduler_, this, callback))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback, |
| 378 | const monotonic_clock::duration interval, |
| 379 | const monotonic_clock::duration offset = |
| 380 | ::std::chrono::seconds(0)) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 381 | return NewPhasedLoop( |
| 382 | ::std::unique_ptr<PhasedLoopHandler>(new SimulatedPhasedLoopHandler( |
| 383 | scheduler_, this, callback, interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void OnRun(::std::function<void()> on_run) override { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 387 | scheduler_->ScheduleOnRun(on_run); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 388 | } |
| 389 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 390 | void set_name(const std::string_view name) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 391 | name_ = std::string(name); |
| 392 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 393 | const std::string_view name() const override { return name_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 394 | |
| 395 | SimulatedChannel *GetSimulatedChannel(const Channel *channel); |
| 396 | |
| 397 | void Take(const Channel *channel); |
| 398 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 399 | void SetRuntimeRealtimePriority(int priority) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 400 | CHECK(!is_running()) << ": Cannot set realtime priority while running."; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 401 | priority_ = priority; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 404 | int priority() const override { return priority_; } |
| 405 | |
| 406 | void Setup() { MaybeScheduleTimingReports(); } |
| 407 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 408 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 409 | friend class SimulatedTimerHandler; |
| 410 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 411 | pid_t GetTid() override { return tid_; } |
| 412 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 413 | EventScheduler *scheduler_; |
| 414 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_; |
| 415 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 416 | *raw_event_loops_; |
| 417 | absl::btree_set<SimpleChannel> taken_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 418 | |
| 419 | ::std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 420 | |
| 421 | int priority_ = 0; |
| 422 | |
| 423 | bool has_setup_ = false; |
| 424 | |
| 425 | const pid_t tid_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | void SimulatedEventLoop::MakeRawWatcher( |
| 429 | const Channel *channel, |
| 430 | std::function<void(const Context &channel, const void *message)> watcher) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 431 | ChannelIndex(channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 432 | Take(channel); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 433 | std::unique_ptr<ShmWatcher> shm_watcher( |
| 434 | new ShmWatcher(this, channel, std::move(watcher))); |
| 435 | |
| 436 | GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get()); |
| 437 | NewWatcher(std::move(shm_watcher)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender( |
| 441 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 442 | ChannelIndex(channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 443 | Take(channel); |
| 444 | return GetSimulatedChannel(channel)->MakeRawSender(this); |
| 445 | } |
| 446 | |
| 447 | std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher( |
| 448 | const Channel *channel) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 449 | ChannelIndex(channel); |
| 450 | return GetSimulatedChannel(channel)->MakeRawFetcher(this); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel( |
| 454 | const Channel *channel) { |
| 455 | auto it = channels_->find(SimpleChannel(channel)); |
| 456 | if (it == channels_->end()) { |
| 457 | it = channels_ |
| 458 | ->emplace(SimpleChannel(channel), |
| 459 | std::unique_ptr<SimulatedChannel>( |
| 460 | new SimulatedChannel(channel, scheduler_))) |
| 461 | .first; |
| 462 | } |
| 463 | return it->second.get(); |
| 464 | } |
| 465 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 466 | void SimulatedChannel::MakeRawWatcher(ShmWatcher *watcher) { |
| 467 | watcher->SetSimulatedChannel(this); |
| 468 | watchers_.emplace_back(watcher); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | ::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender( |
| 472 | EventLoop *event_loop) { |
| 473 | return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop)); |
| 474 | } |
| 475 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 476 | ::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher( |
| 477 | EventLoop *event_loop) { |
| 478 | ::std::unique_ptr<SimulatedFetcher> fetcher( |
| 479 | new SimulatedFetcher(event_loop, this)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 480 | fetchers_.push_back(fetcher.get()); |
| 481 | return ::std::move(fetcher); |
| 482 | } |
| 483 | |
| 484 | void SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) { |
| 485 | message->context.queue_index = next_queue_index_.index(); |
| 486 | message->context.data = |
| 487 | message->data() + channel()->max_size() - message->context.size; |
| 488 | next_queue_index_ = next_queue_index_.Increment(); |
| 489 | |
| 490 | latest_message_ = message; |
| 491 | if (scheduler_->is_running()) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 492 | for (ShmWatcher *watcher : watchers_) { |
| 493 | watcher->Schedule(scheduler_, message); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | for (auto &fetcher : fetchers_) { |
| 497 | fetcher->Enqueue(message); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) { |
| 502 | fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 503 | } |
| 504 | |
| 505 | SimpleChannel::SimpleChannel(const Channel *channel) |
| 506 | : name(CHECK_NOTNULL(CHECK_NOTNULL(channel)->name())->str()), |
| 507 | type(CHECK_NOTNULL(CHECK_NOTNULL(channel)->type())->str()) {} |
| 508 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 509 | SimulatedTimerHandler::SimulatedTimerHandler( |
| 510 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 511 | ::std::function<void()> fn) |
| 512 | : TimerHandler(simulated_event_loop, std::move(fn)), |
| 513 | scheduler_(scheduler), |
| 514 | token_(scheduler_->InvalidToken()) {} |
| 515 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 516 | void SimulatedTimerHandler::Setup(monotonic_clock::time_point base, |
| 517 | monotonic_clock::duration repeat_offset) { |
| 518 | Disable(); |
| 519 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 520 | scheduler_->monotonic_now(); |
| 521 | base_ = base; |
| 522 | repeat_offset_ = repeat_offset; |
| 523 | if (base < monotonic_now) { |
| 524 | token_ = scheduler_->Schedule(monotonic_now, [this]() { HandleEvent(); }); |
| 525 | } else { |
| 526 | token_ = scheduler_->Schedule(base, [this]() { HandleEvent(); }); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void SimulatedTimerHandler::HandleEvent() { |
| 531 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 532 | scheduler_->monotonic_now(); |
| 533 | if (repeat_offset_ != ::aos::monotonic_clock::zero()) { |
| 534 | // Reschedule. |
| 535 | while (base_ <= monotonic_now) base_ += repeat_offset_; |
| 536 | token_ = scheduler_->Schedule(base_, [this]() { HandleEvent(); }); |
| 537 | } else { |
| 538 | token_ = scheduler_->InvalidToken(); |
| 539 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 540 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 541 | Call([monotonic_now]() { return monotonic_now; }, monotonic_now); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 544 | SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler( |
| 545 | EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop, |
| 546 | ::std::function<void(int)> fn, const monotonic_clock::duration interval, |
| 547 | const monotonic_clock::duration offset) |
| 548 | : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset), |
| 549 | simulated_event_loop_(simulated_event_loop), |
| 550 | scheduler_(scheduler), |
| 551 | token_(scheduler_->InvalidToken()) {} |
| 552 | |
| 553 | void SimulatedPhasedLoopHandler::HandleTimerWakeup() { |
| 554 | monotonic_clock::time_point monotonic_now = |
| 555 | simulated_event_loop_->monotonic_now(); |
| 556 | Call( |
| 557 | [monotonic_now]() { return monotonic_now; }, |
| 558 | [this](monotonic_clock::time_point sleep_time) { Schedule(sleep_time); }); |
| 559 | } |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 560 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 561 | void SimulatedEventLoop::Take(const Channel *channel) { |
| 562 | CHECK(!is_running()) << ": Cannot add new objects while running."; |
| 563 | |
| 564 | auto result = taken_.insert(SimpleChannel(channel)); |
| 565 | CHECK(result.second) << ": " << FlatbufferToJson(channel) |
| 566 | << " is already being used."; |
| 567 | } |
| 568 | |
| 569 | SimulatedEventLoopFactory::SimulatedEventLoopFactory( |
| 570 | const Configuration *configuration) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 571 | : configuration_(CHECK_NOTNULL(configuration)) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 572 | SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {} |
| 573 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 574 | ::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop( |
| 575 | std::string_view name) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 576 | pid_t tid = tid_; |
| 577 | ++tid_; |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 578 | ::std::unique_ptr<EventLoop> result(new SimulatedEventLoop( |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 579 | &scheduler_, &channels_, configuration_, &raw_event_loops_, tid)); |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 580 | result->set_name(name); |
| 581 | return result; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) { |
| 585 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 586 | raw_event_loops_) { |
| 587 | event_loop.second(true); |
| 588 | } |
| 589 | scheduler_.RunFor(duration); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 590 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 591 | raw_event_loops_) { |
| 592 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | void SimulatedEventLoopFactory::Run() { |
| 597 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 598 | raw_event_loops_) { |
| 599 | event_loop.second(true); |
| 600 | } |
| 601 | scheduler_.Run(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame^] | 602 | for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop : |
| 603 | raw_event_loops_) { |
| 604 | event_loop.second(false); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | } // namespace aos |