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