blob: 1515e40293c998483080f1f450a0856971a13893 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/simulated_event_loop.h"
2
3#include <algorithm>
4#include <deque>
milind1f1dca32021-07-03 13:50:07 -07005#include <optional>
6#include <queue>
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08007#include <string_view>
Brian Silverman661eb8d2020-08-12 19:41:01 -07008#include <vector>
Alex Perrycb7da4b2019-08-28 19:35:56 -07009
10#include "absl/container/btree_map.h"
Brian Silverman661eb8d2020-08-12 19:41:01 -070011#include "aos/events/aos_logging.h"
Austin Schuh898f4972020-01-11 17:21:25 -080012#include "aos/events/simulated_network_bridge.h"
Austin Schuh094d09b2020-11-20 23:26:52 -080013#include "aos/init.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "aos/json_to_flatbuffer.h"
Austin Schuhcc6070c2020-10-10 20:25:56 -070015#include "aos/realtime.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "aos/util/phased_loop.h"
17
Austin Schuh9b1d6282022-06-10 17:03:21 -070018// TODO(austin): If someone runs a SimulatedEventLoop on a RT thread with
19// die_on_malloc set, it won't die. Really, we need to go RT, or fall back to
20// the base thread's original RT state to be actually accurate.
21
Alex Perrycb7da4b2019-08-28 19:35:56 -070022namespace aos {
23
Brian Silverman661eb8d2020-08-12 19:41:01 -070024class SimulatedEventLoop;
25class SimulatedFetcher;
26class SimulatedChannel;
27
James Kuszmaul890c2492022-04-06 14:59:31 -070028using CheckSentTooFast = NodeEventLoopFactory::CheckSentTooFast;
29using ExclusiveSenders = NodeEventLoopFactory::ExclusiveSenders;
30using EventLoopOptions = NodeEventLoopFactory::EventLoopOptions;
31
Brian Silverman661eb8d2020-08-12 19:41:01 -070032namespace {
33
Austin Schuh057d29f2021-08-21 23:05:15 -070034std::string NodeName(const Node *node) {
35 if (node == nullptr) {
36 return "";
37 }
38
39 return absl::StrCat(node->name()->string_view(), " ");
40}
41
Austin Schuhcc6070c2020-10-10 20:25:56 -070042class ScopedMarkRealtimeRestorer {
43 public:
44 ScopedMarkRealtimeRestorer(bool rt) : rt_(rt), prior_(MarkRealtime(rt)) {}
45 ~ScopedMarkRealtimeRestorer() { CHECK_EQ(rt_, MarkRealtime(prior_)); }
46
47 private:
48 const bool rt_;
49 const bool prior_;
50};
51
Tyler Chatowb7c6eba2021-07-28 14:43:23 -070052// Holds storage for a span object and the data referenced by that span for
53// compatibility with RawSender::SharedSpan users. If constructed with
54// MakeSharedSpan, span points to only the aligned segment of the entire data.
55struct AlignedOwningSpan {
56 AlignedOwningSpan(const AlignedOwningSpan &) = delete;
57 AlignedOwningSpan &operator=(const AlignedOwningSpan &) = delete;
58 absl::Span<const uint8_t> span;
59 char data[];
60};
61
62// Constructs a span which owns its data through a shared_ptr. The owning span
63// points to a const view of the data; also returns a temporary mutable span
64// which is only valid while the const shared span is kept alive.
65std::pair<RawSender::SharedSpan, absl::Span<uint8_t>> MakeSharedSpan(
66 size_t size) {
67 AlignedOwningSpan *const span = reinterpret_cast<AlignedOwningSpan *>(
68 malloc(sizeof(AlignedOwningSpan) + size + kChannelDataAlignment - 1));
69
70 absl::Span mutable_span(
71 reinterpret_cast<uint8_t *>(RoundChannelData(&span->data[0], size)),
72 size);
73 new (span) AlignedOwningSpan{.span = mutable_span};
74
75 return std::make_pair(
76 RawSender::SharedSpan(
77 std::shared_ptr<AlignedOwningSpan>(span,
78 [](AlignedOwningSpan *s) {
79 s->~AlignedOwningSpan();
80 free(s);
81 }),
82 &span->span),
83 mutable_span);
84}
85
Alex Perrycb7da4b2019-08-28 19:35:56 -070086// Container for both a message, and the context for it for simulation. This
87// makes tracking the timestamps associated with the data easy.
Brian Silverman661eb8d2020-08-12 19:41:01 -070088struct SimulatedMessage final {
89 SimulatedMessage(const SimulatedMessage &) = delete;
90 SimulatedMessage &operator=(const SimulatedMessage &) = delete;
Tyler Chatowb7c6eba2021-07-28 14:43:23 -070091 ~SimulatedMessage();
Brian Silverman661eb8d2020-08-12 19:41:01 -070092
93 // Creates a SimulatedMessage with size bytes of storage.
94 // This is a shared_ptr so we don't have to implement refcounting or copying.
Tyler Chatowb7c6eba2021-07-28 14:43:23 -070095 static std::shared_ptr<SimulatedMessage> Make(
96 SimulatedChannel *channel, const RawSender::SharedSpan data);
Brian Silverman661eb8d2020-08-12 19:41:01 -070097
Alex Perrycb7da4b2019-08-28 19:35:56 -070098 // Context for the data.
99 Context context;
100
Brian Silverman661eb8d2020-08-12 19:41:01 -0700101 SimulatedChannel *const channel = nullptr;
Brian Silverman661eb8d2020-08-12 19:41:01 -0700102
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700103 // Owning span to this message's data. Depending on the sender may either
104 // represent the data of just the flatbuffer, or max channel size.
105 RawSender::SharedSpan data;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700107 // Mutable view of above data. If empty, this message is not mutable.
108 absl::Span<uint8_t> mutable_data;
Brian Silverman661eb8d2020-08-12 19:41:01 -0700109
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700110 // Determines whether this message is mutable. Used for Send where the user
111 // fills out a message stored internally then gives us the size of data used.
112 bool is_mutable() const { return data->size() == mutable_data.size(); }
113
114 // Note: this should be private but make_shared requires it to be public. Use
115 // Make() above to construct.
Brian Silverman661eb8d2020-08-12 19:41:01 -0700116 SimulatedMessage(SimulatedChannel *channel_in);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117};
118
Brian Silverman661eb8d2020-08-12 19:41:01 -0700119} // namespace
Austin Schuh39788ff2019-12-01 18:22:57 -0800120
Brian Silverman661eb8d2020-08-12 19:41:01 -0700121// TODO(Brian): This should be in the anonymous namespace, but that annoys GCC
122// for some reason...
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800123class SimulatedWatcher : public WatcherState, public EventScheduler::Event {
Austin Schuh39788ff2019-12-01 18:22:57 -0800124 public:
Austin Schuh7d87b672019-12-01 20:23:49 -0800125 SimulatedWatcher(
126 SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler,
127 const Channel *channel,
128 std::function<void(const Context &context, const void *message)> fn);
Austin Schuh39788ff2019-12-01 18:22:57 -0800129
Austin Schuh7d87b672019-12-01 20:23:49 -0800130 ~SimulatedWatcher() override;
Austin Schuh39788ff2019-12-01 18:22:57 -0800131
Austin Schuh8fb315a2020-11-19 22:33:58 -0800132 bool has_run() const;
133
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800134 void Handle() noexcept override;
135
Austin Schuh39788ff2019-12-01 18:22:57 -0800136 void Startup(EventLoop * /*event_loop*/) override {}
137
Austin Schuh7d87b672019-12-01 20:23:49 -0800138 void Schedule(std::shared_ptr<SimulatedMessage> message);
139
Austin Schuhf4b09c72021-12-08 12:04:37 -0800140 void HandleEvent() noexcept;
Austin Schuh39788ff2019-12-01 18:22:57 -0800141
142 void SetSimulatedChannel(SimulatedChannel *channel) {
143 simulated_channel_ = channel;
144 }
145
146 private:
Austin Schuh7d87b672019-12-01 20:23:49 -0800147 void DoSchedule(monotonic_clock::time_point event_time);
148
149 ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_;
150
Brian Silverman4f4e0612020-08-12 19:54:41 -0700151 SimulatedEventLoop *const simulated_event_loop_;
152 const Channel *const channel_;
153 EventScheduler *const scheduler_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800154 EventHandler<SimulatedWatcher> event_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800155 EventScheduler::Token token_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800156 SimulatedChannel *simulated_channel_ = nullptr;
157};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700158
159class SimulatedChannel {
160 public:
Austin Schuh8fb315a2020-11-19 22:33:58 -0800161 explicit SimulatedChannel(const Channel *channel,
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700162 std::chrono::nanoseconds channel_storage_duration,
163 const EventScheduler *scheduler)
Austin Schuh39788ff2019-12-01 18:22:57 -0800164 : channel_(channel),
Brian Silverman661eb8d2020-08-12 19:41:01 -0700165 channel_storage_duration_(channel_storage_duration),
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700166 next_queue_index_(ipc_lib::QueueIndex::Zero(number_buffers())),
167 scheduler_(scheduler) {
Brian Silvermanbc596c62021-10-15 14:04:54 -0700168 available_buffer_indices_.resize(number_buffers());
Brian Silverman661eb8d2020-08-12 19:41:01 -0700169 for (int i = 0; i < number_buffers(); ++i) {
Brian Silvermanbc596c62021-10-15 14:04:54 -0700170 available_buffer_indices_[i] = i;
Brian Silverman661eb8d2020-08-12 19:41:01 -0700171 }
172 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173
Brian Silverman661eb8d2020-08-12 19:41:01 -0700174 ~SimulatedChannel() {
175 latest_message_.reset();
176 CHECK_EQ(static_cast<size_t>(number_buffers()),
177 available_buffer_indices_.size());
James Kuszmaul4f106fb2021-01-05 20:53:02 -0800178 CHECK_EQ(0u, fetchers_.size())
179 << configuration::StrippedChannelToString(channel());
180 CHECK_EQ(0u, watchers_.size())
181 << configuration::StrippedChannelToString(channel());
182 CHECK_EQ(0, sender_count_)
183 << configuration::StrippedChannelToString(channel());
Brian Silverman661eb8d2020-08-12 19:41:01 -0700184 }
185
186 // The number of messages we pretend to have in the queue.
187 int queue_size() const {
Austin Schuhfb37c612022-08-11 15:24:51 -0700188 return configuration::QueueSize(channel()->frequency(),
189 channel_storage_duration_);
Brian Silverman661eb8d2020-08-12 19:41:01 -0700190 }
191
milind1f1dca32021-07-03 13:50:07 -0700192 std::chrono::nanoseconds channel_storage_duration() const {
193 return channel_storage_duration_;
194 }
195
Brian Silverman661eb8d2020-08-12 19:41:01 -0700196 // The number of extra buffers (beyond the queue) we pretend to have.
197 int number_scratch_buffers() const {
Austin Schuhfb37c612022-08-11 15:24:51 -0700198 return configuration::QueueScratchBufferSize(channel());
Brian Silverman661eb8d2020-08-12 19:41:01 -0700199 }
200
201 int number_buffers() const { return queue_size() + number_scratch_buffers(); }
202
203 int GetBufferIndex() {
204 CHECK(!available_buffer_indices_.empty()) << ": This should be impossible";
205 const int result = available_buffer_indices_.back();
206 available_buffer_indices_.pop_back();
207 return result;
208 }
209
210 void FreeBufferIndex(int i) {
Austin Schuhc5047ea2021-03-20 22:00:21 -0700211 // This extra checking has a large performance hit with sanitizers that
212 // track memory accesses, so just skip it.
213#if !__has_feature(memory_sanitizer) && !__has_feature(address_sanitizer)
Brian Silverman661eb8d2020-08-12 19:41:01 -0700214 DCHECK(std::find(available_buffer_indices_.begin(),
215 available_buffer_indices_.end(),
216 i) == available_buffer_indices_.end())
217 << ": Buffer is not in use: " << i;
Brian Silvermanf3e6df22021-01-19 15:02:21 -0800218#endif
Brian Silverman661eb8d2020-08-12 19:41:01 -0700219 available_buffer_indices_.push_back(i);
220 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700221
222 // Makes a connected raw sender which calls Send below.
Austin Schuh8fb315a2020-11-19 22:33:58 -0800223 ::std::unique_ptr<RawSender> MakeRawSender(SimulatedEventLoop *event_loop);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700224
225 // Makes a connected raw fetcher.
Austin Schuh39788ff2019-12-01 18:22:57 -0800226 ::std::unique_ptr<RawFetcher> MakeRawFetcher(EventLoop *event_loop);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700227
228 // Registers a watcher for the queue.
Austin Schuh7d87b672019-12-01 20:23:49 -0800229 void MakeRawWatcher(SimulatedWatcher *watcher);
Austin Schuh39788ff2019-12-01 18:22:57 -0800230
Austin Schuh7d87b672019-12-01 20:23:49 -0800231 void RemoveWatcher(SimulatedWatcher *watcher) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800232 watchers_.erase(std::find(watchers_.begin(), watchers_.end(), watcher));
233 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700234
Austin Schuhad154822019-12-27 15:45:13 -0800235 // Sends the message to all the connected receivers and fetchers. Returns the
milind1f1dca32021-07-03 13:50:07 -0700236 // sent queue index, or std::nullopt if messages were sent too fast.
James Kuszmaul890c2492022-04-06 14:59:31 -0700237 std::optional<uint32_t> Send(std::shared_ptr<SimulatedMessage> message,
238 CheckSentTooFast check_sent_too_fast);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700239
240 // Unregisters a fetcher.
241 void UnregisterFetcher(SimulatedFetcher *fetcher);
242
243 std::shared_ptr<SimulatedMessage> latest_message() { return latest_message_; }
244
Austin Schuh39788ff2019-12-01 18:22:57 -0800245 size_t max_size() const { return channel()->max_size(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700246
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800247 const std::string_view name() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800248 return channel()->name()->string_view();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 }
250
Austin Schuh39788ff2019-12-01 18:22:57 -0800251 const Channel *channel() const { return channel_; }
252
Austin Schuhe516ab02020-05-06 21:37:04 -0700253 void CountSenderCreated() {
254 if (sender_count_ >= channel()->num_senders()) {
255 LOG(FATAL) << "Failed to create sender on "
256 << configuration::CleanedChannelToString(channel())
257 << ", too many senders.";
258 }
Austin Schuhfb37c612022-08-11 15:24:51 -0700259 CheckBufferCount();
Austin Schuhe516ab02020-05-06 21:37:04 -0700260 ++sender_count_;
261 }
Brian Silverman77162972020-08-12 19:52:40 -0700262
Austin Schuhe516ab02020-05-06 21:37:04 -0700263 void CountSenderDestroyed() {
264 --sender_count_;
265 CHECK_GE(sender_count_, 0);
James Kuszmaul890c2492022-04-06 14:59:31 -0700266 if (sender_count_ == 0) {
267 allow_new_senders_ = true;
268 }
Austin Schuhe516ab02020-05-06 21:37:04 -0700269 }
270
Alex Perrycb7da4b2019-08-28 19:35:56 -0700271 private:
Brian Silverman77162972020-08-12 19:52:40 -0700272 void CheckBufferCount() {
273 int reader_count = 0;
274 if (channel()->read_method() == ReadMethod::PIN) {
275 reader_count = watchers_.size() + fetchers_.size();
276 }
277 CHECK_LT(reader_count + sender_count_, number_scratch_buffers());
278 }
279
280 void CheckReaderCount() {
281 if (channel()->read_method() != ReadMethod::PIN) {
282 return;
283 }
284 CheckBufferCount();
285 const int reader_count = watchers_.size() + fetchers_.size();
286 if (reader_count >= channel()->num_readers()) {
287 LOG(FATAL) << "Failed to create reader on "
288 << configuration::CleanedChannelToString(channel())
289 << ", too many readers.";
290 }
291 }
Brian Silverman661eb8d2020-08-12 19:41:01 -0700292
293 const Channel *const channel_;
Brian Silverman661eb8d2020-08-12 19:41:01 -0700294 const std::chrono::nanoseconds channel_storage_duration_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295
296 // List of all watchers.
Austin Schuh7d87b672019-12-01 20:23:49 -0800297 ::std::vector<SimulatedWatcher *> watchers_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298
299 // List of all fetchers.
300 ::std::vector<SimulatedFetcher *> fetchers_;
301 std::shared_ptr<SimulatedMessage> latest_message_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700302
303 ipc_lib::QueueIndex next_queue_index_;
Austin Schuhe516ab02020-05-06 21:37:04 -0700304
305 int sender_count_ = 0;
James Kuszmaul890c2492022-04-06 14:59:31 -0700306 // Used to track when an exclusive sender has been created (e.g., for log
307 // replay) and we want to prevent new senders from being accidentally created.
308 bool allow_new_senders_ = true;
Brian Silverman661eb8d2020-08-12 19:41:01 -0700309
310 std::vector<uint16_t> available_buffer_indices_;
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700311
312 const EventScheduler *scheduler_;
313
314 // Queue of all the message send times in the last channel_storage_duration_
315 std::queue<monotonic_clock::time_point> last_times_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700316};
317
318namespace {
319
Brian Silverman661eb8d2020-08-12 19:41:01 -0700320std::shared_ptr<SimulatedMessage> SimulatedMessage::Make(
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700321 SimulatedChannel *channel, RawSender::SharedSpan data) {
Austin Schuh62288252020-11-18 23:26:04 -0800322 // The allocations in here are due to infrastructure and don't count in the no
323 // mallocs in RT code.
324 ScopedNotRealtime nrt;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700326 auto message = std::make_shared<SimulatedMessage>(channel);
327 message->context.size = data->size();
328 message->context.data = data->data();
329 message->data = std::move(data);
330
331 return message;
Brian Silverman661eb8d2020-08-12 19:41:01 -0700332}
333
334SimulatedMessage::SimulatedMessage(SimulatedChannel *channel_in)
335 : channel(channel_in) {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700336 context.buffer_index = channel->GetBufferIndex();
Brian Silverman661eb8d2020-08-12 19:41:01 -0700337}
338
339SimulatedMessage::~SimulatedMessage() {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700340 channel->FreeBufferIndex(context.buffer_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700341}
342
343class SimulatedSender : public RawSender {
344 public:
Austin Schuh8fb315a2020-11-19 22:33:58 -0800345 SimulatedSender(SimulatedChannel *simulated_channel,
346 SimulatedEventLoop *event_loop);
347 ~SimulatedSender() override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700348
349 void *data() override {
350 if (!message_) {
Austin Schuh9b1d6282022-06-10 17:03:21 -0700351 // This API is safe to use in a RT context on a RT system. So annotate it
352 // accordingly.
353 ScopedNotRealtime nrt;
354
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700355 auto [span, mutable_span] =
356 MakeSharedSpan(simulated_channel_->max_size());
357 message_ = SimulatedMessage::Make(simulated_channel_, span);
358 message_->mutable_data = mutable_span;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700359 }
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700360 CHECK(message_->is_mutable());
361 return message_->mutable_data.data();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700362 }
363
364 size_t size() override { return simulated_channel_->max_size(); }
365
milind1f1dca32021-07-03 13:50:07 -0700366 Error DoSend(size_t length, monotonic_clock::time_point monotonic_remote_time,
367 realtime_clock::time_point realtime_remote_time,
368 uint32_t remote_queue_index,
369 const UUID &source_boot_uuid) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700370
milind1f1dca32021-07-03 13:50:07 -0700371 Error DoSend(const void *msg, size_t size,
372 monotonic_clock::time_point monotonic_remote_time,
373 realtime_clock::time_point realtime_remote_time,
374 uint32_t remote_queue_index,
375 const UUID &source_boot_uuid) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700376
milind1f1dca32021-07-03 13:50:07 -0700377 Error DoSend(const SharedSpan data,
378 aos::monotonic_clock::time_point monotonic_remote_time,
379 aos::realtime_clock::time_point realtime_remote_time,
380 uint32_t remote_queue_index,
381 const UUID &source_boot_uuid) override;
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700382
Brian Silverman4f4e0612020-08-12 19:54:41 -0700383 int buffer_index() override {
384 // First, ensure message_ is allocated.
385 data();
386 return message_->context.buffer_index;
387 }
388
Alex Perrycb7da4b2019-08-28 19:35:56 -0700389 private:
390 SimulatedChannel *simulated_channel_;
Austin Schuh58646e22021-08-23 23:51:46 -0700391 SimulatedEventLoop *simulated_event_loop_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700392
393 std::shared_ptr<SimulatedMessage> message_;
394};
395} // namespace
396
397class SimulatedFetcher : public RawFetcher {
398 public:
Austin Schuhac0771c2020-01-07 18:36:30 -0800399 explicit SimulatedFetcher(EventLoop *event_loop,
400 SimulatedChannel *simulated_channel)
401 : RawFetcher(event_loop, simulated_channel->channel()),
402 simulated_channel_(simulated_channel) {}
403 ~SimulatedFetcher() { simulated_channel_->UnregisterFetcher(this); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700404
Austin Schuh39788ff2019-12-01 18:22:57 -0800405 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
Austin Schuh62288252020-11-18 23:26:04 -0800406 // The allocations in here are due to infrastructure and don't count in the
407 // no mallocs in RT code.
408 ScopedNotRealtime nrt;
Austin Schuh39788ff2019-12-01 18:22:57 -0800409 if (msgs_.size() == 0) {
410 return std::make_pair(false, monotonic_clock::min_time);
411 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700412
James Kuszmaulbcd96fc2020-10-12 20:29:32 -0700413 CHECK(!fell_behind_) << ": Got behind on "
414 << configuration::StrippedChannelToString(
415 simulated_channel_->channel());
Brian Silverman661eb8d2020-08-12 19:41:01 -0700416
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 SetMsg(msgs_.front());
418 msgs_.pop_front();
Austin Schuha5e14192020-01-06 18:02:41 -0800419 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700420 }
421
Austin Schuh39788ff2019-12-01 18:22:57 -0800422 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
Austin Schuh62288252020-11-18 23:26:04 -0800423 // The allocations in here are due to infrastructure and don't count in the
424 // no mallocs in RT code.
425 ScopedNotRealtime nrt;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700426 if (msgs_.size() == 0) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800427 // TODO(austin): Can we just do this logic unconditionally? It is a lot
428 // simpler. And call clear, obviously.
Austin Schuhac0771c2020-01-07 18:36:30 -0800429 if (!msg_ && simulated_channel_->latest_message()) {
430 SetMsg(simulated_channel_->latest_message());
Austin Schuha5e14192020-01-06 18:02:41 -0800431 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700432 } else {
Austin Schuh39788ff2019-12-01 18:22:57 -0800433 return std::make_pair(false, monotonic_clock::min_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700434 }
435 }
436
437 // We've had a message enqueued, so we don't need to go looking for the
438 // latest message from before we started.
439 SetMsg(msgs_.back());
440 msgs_.clear();
Brian Silverman661eb8d2020-08-12 19:41:01 -0700441 fell_behind_ = false;
Austin Schuha5e14192020-01-06 18:02:41 -0800442 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700443 }
444
445 private:
446 friend class SimulatedChannel;
447
448 // Updates the state inside RawFetcher to point to the data in msg_.
449 void SetMsg(std::shared_ptr<SimulatedMessage> msg) {
Austin Schuhe6f4c8d2021-12-11 12:36:06 -0800450 msg_ = std::move(msg);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451 context_ = msg_->context;
Brian Silverman4f4e0612020-08-12 19:54:41 -0700452 if (channel()->read_method() != ReadMethod::PIN) {
453 context_.buffer_index = -1;
454 }
Austin Schuhad154822019-12-27 15:45:13 -0800455 if (context_.remote_queue_index == 0xffffffffu) {
456 context_.remote_queue_index = context_.queue_index;
457 }
Austin Schuh58646e22021-08-23 23:51:46 -0700458 if (context_.monotonic_remote_time == monotonic_clock::min_time) {
Austin Schuhad154822019-12-27 15:45:13 -0800459 context_.monotonic_remote_time = context_.monotonic_event_time;
460 }
Austin Schuh58646e22021-08-23 23:51:46 -0700461 if (context_.realtime_remote_time == realtime_clock::min_time) {
Austin Schuhad154822019-12-27 15:45:13 -0800462 context_.realtime_remote_time = context_.realtime_event_time;
463 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700464 }
465
466 // Internal method for Simulation to add a message to the buffer.
467 void Enqueue(std::shared_ptr<SimulatedMessage> buffer) {
Austin Schuhe6f4c8d2021-12-11 12:36:06 -0800468 msgs_.emplace_back(std::move(buffer));
Brian Silverman661eb8d2020-08-12 19:41:01 -0700469 if (fell_behind_ ||
470 msgs_.size() > static_cast<size_t>(simulated_channel_->queue_size())) {
471 fell_behind_ = true;
472 // Might as well empty out all the intermediate messages now.
473 while (msgs_.size() > 1) {
474 msgs_.pop_front();
475 }
476 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700477 }
478
Austin Schuhac0771c2020-01-07 18:36:30 -0800479 SimulatedChannel *simulated_channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480 std::shared_ptr<SimulatedMessage> msg_;
481
482 // Messages queued up but not in use.
483 ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_;
Brian Silverman661eb8d2020-08-12 19:41:01 -0700484
485 // Whether we're currently "behind", which means a FetchNext call will fail.
486 bool fell_behind_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700487};
488
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800489class SimulatedTimerHandler : public TimerHandler,
490 public EventScheduler::Event {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700491 public:
492 explicit SimulatedTimerHandler(EventScheduler *scheduler,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800493 SimulatedEventLoop *simulated_event_loop,
Austin Schuh39788ff2019-12-01 18:22:57 -0800494 ::std::function<void()> fn);
Austin Schuh7d87b672019-12-01 20:23:49 -0800495 ~SimulatedTimerHandler() { Disable(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700496
497 void Setup(monotonic_clock::time_point base,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800498 monotonic_clock::duration repeat_offset) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700499
Austin Schuhf4b09c72021-12-08 12:04:37 -0800500 void HandleEvent() noexcept;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700501
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800502 void Handle() noexcept override;
503
Austin Schuh7d87b672019-12-01 20:23:49 -0800504 void Disable() override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700505
Alex Perrycb7da4b2019-08-28 19:35:56 -0700506 private:
Austin Schuh7d87b672019-12-01 20:23:49 -0800507 SimulatedEventLoop *simulated_event_loop_;
508 EventHandler<SimulatedTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700509 EventScheduler *scheduler_;
510 EventScheduler::Token token_;
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800511
Alex Perrycb7da4b2019-08-28 19:35:56 -0700512 monotonic_clock::time_point base_;
513 monotonic_clock::duration repeat_offset_;
514};
515
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800516class SimulatedPhasedLoopHandler : public PhasedLoopHandler,
517 public EventScheduler::Event {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700518 public:
519 SimulatedPhasedLoopHandler(EventScheduler *scheduler,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800520 SimulatedEventLoop *simulated_event_loop,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700521 ::std::function<void(int)> fn,
522 const monotonic_clock::duration interval,
Austin Schuh39788ff2019-12-01 18:22:57 -0800523 const monotonic_clock::duration offset);
Austin Schuh7d87b672019-12-01 20:23:49 -0800524 ~SimulatedPhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700525
Austin Schuhf4b09c72021-12-08 12:04:37 -0800526 void HandleEvent() noexcept;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700527
Austin Schuh7d87b672019-12-01 20:23:49 -0800528 void Schedule(monotonic_clock::time_point sleep_time) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700529
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800530 void Handle() noexcept override;
531
Alex Perrycb7da4b2019-08-28 19:35:56 -0700532 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800533 SimulatedEventLoop *simulated_event_loop_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800534 EventHandler<SimulatedPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700535
Austin Schuh39788ff2019-12-01 18:22:57 -0800536 EventScheduler *scheduler_;
537 EventScheduler::Token token_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700538};
539
540class SimulatedEventLoop : public EventLoop {
541 public:
542 explicit SimulatedEventLoop(
Brian Silverman661eb8d2020-08-12 19:41:01 -0700543 EventScheduler *scheduler, NodeEventLoopFactory *node_event_loop_factory,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700544 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>>
545 *channels,
546 const Configuration *configuration,
Austin Schuh057d29f2021-08-21 23:05:15 -0700547 std::vector<SimulatedEventLoop *> *event_loops_, const Node *node,
James Kuszmaul890c2492022-04-06 14:59:31 -0700548 pid_t tid, EventLoopOptions options)
Austin Schuh83c7f702021-01-19 22:36:29 -0800549 : EventLoop(CHECK_NOTNULL(configuration)),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700550 scheduler_(scheduler),
Austin Schuhac0771c2020-01-07 18:36:30 -0800551 node_event_loop_factory_(node_event_loop_factory),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700552 channels_(channels),
Austin Schuh057d29f2021-08-21 23:05:15 -0700553 event_loops_(event_loops_),
Austin Schuh217a9782019-12-21 23:02:50 -0800554 node_(node),
Austin Schuh58646e22021-08-23 23:51:46 -0700555 tid_(tid),
James Kuszmaul890c2492022-04-06 14:59:31 -0700556 startup_tracker_(std::make_shared<StartupTracker>()),
557 options_(options) {
Austin Schuh58646e22021-08-23 23:51:46 -0700558 startup_tracker_->loop = this;
559 scheduler_->ScheduleOnStartup([startup_tracker = startup_tracker_]() {
560 if (startup_tracker->loop) {
561 startup_tracker->loop->Setup();
562 startup_tracker->has_setup = true;
563 }
Austin Schuh057d29f2021-08-21 23:05:15 -0700564 });
565
566 event_loops_->push_back(this);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700567 }
Austin Schuh58646e22021-08-23 23:51:46 -0700568
Alex Perrycb7da4b2019-08-28 19:35:56 -0700569 ~SimulatedEventLoop() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800570 // Trigger any remaining senders or fetchers to be cleared before destroying
571 // the event loop so the book keeping matches.
572 timing_report_sender_.reset();
573
574 // Force everything with a registered fd with epoll to be destroyed now.
575 timers_.clear();
576 phased_loops_.clear();
577 watchers_.clear();
578
Austin Schuh58646e22021-08-23 23:51:46 -0700579 for (auto it = event_loops_->begin(); it != event_loops_->end(); ++it) {
Austin Schuh057d29f2021-08-21 23:05:15 -0700580 if (*it == this) {
581 event_loops_->erase(it);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700582 break;
583 }
584 }
Austin Schuh58646e22021-08-23 23:51:46 -0700585 VLOG(1) << scheduler_->distributed_now() << " " << NodeName(node())
586 << monotonic_now() << " ~SimulatedEventLoop(\"" << name_ << "\")";
587 startup_tracker_->loop = nullptr;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700588 }
589
Austin Schuh057d29f2021-08-21 23:05:15 -0700590 void SetIsRunning(bool running) {
Austin Schuh58646e22021-08-23 23:51:46 -0700591 VLOG(1) << scheduler_->distributed_now() << " " << NodeName(node())
592 << monotonic_now() << " " << name_ << " set_is_running(" << running
593 << ")";
594 CHECK(startup_tracker_->has_setup);
Austin Schuh057d29f2021-08-21 23:05:15 -0700595
596 set_is_running(running);
Austin Schuh58646e22021-08-23 23:51:46 -0700597 if (running) {
598 has_run_ = true;
599 }
Austin Schuh057d29f2021-08-21 23:05:15 -0700600 }
601
Austin Schuh8fb315a2020-11-19 22:33:58 -0800602 bool has_run() const { return has_run_; }
603
Austin Schuh7d87b672019-12-01 20:23:49 -0800604 std::chrono::nanoseconds send_delay() const { return send_delay_; }
605 void set_send_delay(std::chrono::nanoseconds send_delay) {
606 send_delay_ = send_delay;
607 }
608
Stephan Pleines559fa6c2022-01-06 17:23:51 -0800609 monotonic_clock::time_point monotonic_now() const override {
Austin Schuhac0771c2020-01-07 18:36:30 -0800610 return node_event_loop_factory_->monotonic_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700611 }
612
Stephan Pleines559fa6c2022-01-06 17:23:51 -0800613 realtime_clock::time_point realtime_now() const override {
Austin Schuhac0771c2020-01-07 18:36:30 -0800614 return node_event_loop_factory_->realtime_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700615 }
616
Austin Schuh58646e22021-08-23 23:51:46 -0700617 distributed_clock::time_point distributed_now() {
618 return scheduler_->distributed_now();
619 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700620
Austin Schuh58646e22021-08-23 23:51:46 -0700621 std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
622
623 std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700624
625 void MakeRawWatcher(
626 const Channel *channel,
627 ::std::function<void(const Context &context, const void *message)>
628 watcher) override;
629
630 TimerHandler *AddTimer(::std::function<void()> callback) override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800631 CHECK(!is_running());
Austin Schuh8bd96322020-02-13 21:18:22 -0800632 return NewTimer(::std::unique_ptr<TimerHandler>(
633 new SimulatedTimerHandler(scheduler_, this, callback)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700634 }
635
636 PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback,
637 const monotonic_clock::duration interval,
638 const monotonic_clock::duration offset =
639 ::std::chrono::seconds(0)) override {
Austin Schuh8bd96322020-02-13 21:18:22 -0800640 return NewPhasedLoop(
641 ::std::unique_ptr<PhasedLoopHandler>(new SimulatedPhasedLoopHandler(
642 scheduler_, this, callback, interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700643 }
644
645 void OnRun(::std::function<void()> on_run) override {
Austin Schuh8fb315a2020-11-19 22:33:58 -0800646 CHECK(!is_running()) << ": Cannot register OnRun callback while running.";
Austin Schuhcc6070c2020-10-10 20:25:56 -0700647 scheduler_->ScheduleOnRun([this, on_run = std::move(on_run)]() {
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800648 logging::ScopedLogRestorer prev_logger;
649 if (log_impl_) {
650 prev_logger.Swap(log_impl_);
651 }
Austin Schuh65493d62022-08-17 15:10:37 -0700652 ScopedMarkRealtimeRestorer rt(runtime_realtime_priority() > 0);
Austin Schuha9012be2021-07-21 15:19:11 -0700653 SetTimerContext(monotonic_now());
Austin Schuhcc6070c2020-10-10 20:25:56 -0700654 on_run();
655 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700656 }
657
Austin Schuh217a9782019-12-21 23:02:50 -0800658 const Node *node() const override { return node_; }
659
James Kuszmaul3ae42262019-11-08 12:33:41 -0800660 void set_name(const std::string_view name) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700661 name_ = std::string(name);
662 }
James Kuszmaul3ae42262019-11-08 12:33:41 -0800663 const std::string_view name() const override { return name_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700664
665 SimulatedChannel *GetSimulatedChannel(const Channel *channel);
666
Austin Schuh39788ff2019-12-01 18:22:57 -0800667 void SetRuntimeRealtimePriority(int priority) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700668 CHECK(!is_running()) << ": Cannot set realtime priority while running.";
Austin Schuh39788ff2019-12-01 18:22:57 -0800669 priority_ = priority;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700670 }
671
Austin Schuh65493d62022-08-17 15:10:37 -0700672 int runtime_realtime_priority() const override { return priority_; }
673 const cpu_set_t &runtime_affinity() const override { return affinity_; }
Austin Schuh39788ff2019-12-01 18:22:57 -0800674
Austin Schuh65493d62022-08-17 15:10:37 -0700675 void SetRuntimeAffinity(const cpu_set_t &affinity) override {
Brian Silverman6a54ff32020-04-28 16:41:39 -0700676 CHECK(!is_running()) << ": Cannot set affinity while running.";
Austin Schuh65493d62022-08-17 15:10:37 -0700677 affinity_ = affinity;
Brian Silverman6a54ff32020-04-28 16:41:39 -0700678 }
679
Tyler Chatow67ddb032020-01-12 14:30:04 -0800680 void Setup() {
681 MaybeScheduleTimingReports();
682 if (!skip_logger_) {
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800683 log_sender_.Initialize(&name_,
684 MakeSender<logging::LogMessageFbs>("/aos"));
Austin Schuha0c41ba2020-09-10 22:59:14 -0700685 log_impl_ = log_sender_.implementation();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800686 }
687 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800688
Brian Silverman4f4e0612020-08-12 19:54:41 -0700689 int NumberBuffers(const Channel *channel) override;
690
Austin Schuh83c7f702021-01-19 22:36:29 -0800691 const UUID &boot_uuid() const override {
692 return node_event_loop_factory_->boot_uuid();
693 }
694
James Kuszmaul890c2492022-04-06 14:59:31 -0700695 const EventLoopOptions &options() const { return options_; }
696
Alex Perrycb7da4b2019-08-28 19:35:56 -0700697 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800698 friend class SimulatedTimerHandler;
Austin Schuh7d87b672019-12-01 20:23:49 -0800699 friend class SimulatedPhasedLoopHandler;
700 friend class SimulatedWatcher;
701
Austin Schuh58646e22021-08-23 23:51:46 -0700702 // We have a condition where we register a startup handler, but then get shut
703 // down before it runs. This results in a segfault if we are lucky, and
704 // corruption otherwise. To handle that, allocate a small object which points
705 // back to us and can be freed when the function is freed. That object can
706 // then be updated when we get destroyed so setup is not called.
707 struct StartupTracker {
708 SimulatedEventLoop *loop = nullptr;
709 bool has_setup = false;
710 };
711
Austin Schuh7d87b672019-12-01 20:23:49 -0800712 void HandleEvent() {
713 while (true) {
714 if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) {
715 break;
716 }
717
718 EventLoopEvent *event = PopEvent();
719 event->HandleEvent();
720 }
721 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800722
Austin Schuh39788ff2019-12-01 18:22:57 -0800723 pid_t GetTid() override { return tid_; }
724
Alex Perrycb7da4b2019-08-28 19:35:56 -0700725 EventScheduler *scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800726 NodeEventLoopFactory *node_event_loop_factory_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700727 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_;
Austin Schuh057d29f2021-08-21 23:05:15 -0700728 std::vector<SimulatedEventLoop *> *event_loops_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700729
730 ::std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800731
732 int priority_ = 0;
Austin Schuh65493d62022-08-17 15:10:37 -0700733 cpu_set_t affinity_ = DefaultAffinity();
Austin Schuh39788ff2019-12-01 18:22:57 -0800734
Austin Schuh7d87b672019-12-01 20:23:49 -0800735 std::chrono::nanoseconds send_delay_;
736
Austin Schuh217a9782019-12-21 23:02:50 -0800737 const Node *const node_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800738 const pid_t tid_;
Tyler Chatow67ddb032020-01-12 14:30:04 -0800739
740 AosLogToFbs log_sender_;
Austin Schuha0c41ba2020-09-10 22:59:14 -0700741 std::shared_ptr<logging::LogImplementation> log_impl_ = nullptr;
Austin Schuh8fb315a2020-11-19 22:33:58 -0800742
743 bool has_run_ = false;
Austin Schuh58646e22021-08-23 23:51:46 -0700744
745 std::shared_ptr<StartupTracker> startup_tracker_;
James Kuszmaul890c2492022-04-06 14:59:31 -0700746
747 EventLoopOptions options_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700748};
749
Austin Schuh7d87b672019-12-01 20:23:49 -0800750void SimulatedEventLoopFactory::set_send_delay(
751 std::chrono::nanoseconds send_delay) {
752 send_delay_ = send_delay;
Austin Schuh58646e22021-08-23 23:51:46 -0700753 for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) {
Austin Schuh057d29f2021-08-21 23:05:15 -0700754 if (node) {
755 for (SimulatedEventLoop *loop : node->event_loops_) {
756 loop->set_send_delay(send_delay_);
757 }
758 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800759 }
760}
761
James Kuszmaulb67409b2022-06-20 16:25:03 -0700762void SimulatedEventLoopFactory::SetRealtimeReplayRate(double replay_rate) {
763 scheduler_scheduler_.SetReplayRate(replay_rate);
764}
765
Alex Perrycb7da4b2019-08-28 19:35:56 -0700766void SimulatedEventLoop::MakeRawWatcher(
767 const Channel *channel,
768 std::function<void(const Context &channel, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800769 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800770
Austin Schuh057d29f2021-08-21 23:05:15 -0700771 std::unique_ptr<SimulatedWatcher> shm_watcher =
772 std::make_unique<SimulatedWatcher>(this, scheduler_, channel,
773 std::move(watcher));
Austin Schuh39788ff2019-12-01 18:22:57 -0800774
775 GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get());
Austin Schuh057d29f2021-08-21 23:05:15 -0700776
Austin Schuh39788ff2019-12-01 18:22:57 -0800777 NewWatcher(std::move(shm_watcher));
Austin Schuh58646e22021-08-23 23:51:46 -0700778 VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now()
779 << " " << name() << " MakeRawWatcher(\""
780 << configuration::StrippedChannelToString(channel) << "\")";
Austin Schuh8fb315a2020-11-19 22:33:58 -0800781
782 // Order of operations gets kinda wonky if we let people make watchers after
783 // running once. If someone has a valid use case, we can reconsider.
784 CHECK(!has_run()) << ": Can't add a watcher after running.";
Alex Perrycb7da4b2019-08-28 19:35:56 -0700785}
786
787std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender(
788 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800789 TakeSender(channel);
790
Austin Schuh58646e22021-08-23 23:51:46 -0700791 VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now()
792 << " " << name() << " MakeRawSender(\""
793 << configuration::StrippedChannelToString(channel) << "\")";
Alex Perrycb7da4b2019-08-28 19:35:56 -0700794 return GetSimulatedChannel(channel)->MakeRawSender(this);
795}
796
797std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher(
798 const Channel *channel) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800799 ChannelIndex(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800800
Austin Schuhca4828c2019-12-28 14:21:35 -0800801 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
802 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
803 << "\", \"type\": \"" << channel->type()->string_view()
804 << "\" } is not able to be fetched on this node. Check your "
805 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800806 }
807
Austin Schuh58646e22021-08-23 23:51:46 -0700808 VLOG(1) << distributed_now() << " " << NodeName(node()) << monotonic_now()
809 << " " << name() << " MakeRawFetcher(\""
810 << configuration::StrippedChannelToString(channel) << "\")";
Austin Schuh39788ff2019-12-01 18:22:57 -0800811 return GetSimulatedChannel(channel)->MakeRawFetcher(this);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700812}
813
814SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel(
815 const Channel *channel) {
816 auto it = channels_->find(SimpleChannel(channel));
817 if (it == channels_->end()) {
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700818 it = channels_
819 ->emplace(SimpleChannel(channel),
820 std::unique_ptr<SimulatedChannel>(new SimulatedChannel(
821 channel,
822 std::chrono::nanoseconds(
823 configuration()->channel_storage_duration()),
824 scheduler_)))
825 .first;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700826 }
827 return it->second.get();
828}
829
Brian Silverman4f4e0612020-08-12 19:54:41 -0700830int SimulatedEventLoop::NumberBuffers(const Channel *channel) {
831 return GetSimulatedChannel(channel)->number_buffers();
832}
833
Austin Schuh7d87b672019-12-01 20:23:49 -0800834SimulatedWatcher::SimulatedWatcher(
835 SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler,
Austin Schuh8bd96322020-02-13 21:18:22 -0800836 const Channel *channel,
Austin Schuh7d87b672019-12-01 20:23:49 -0800837 std::function<void(const Context &context, const void *message)> fn)
838 : WatcherState(simulated_event_loop, channel, std::move(fn)),
839 simulated_event_loop_(simulated_event_loop),
Brian Silverman4f4e0612020-08-12 19:54:41 -0700840 channel_(channel),
Austin Schuh7d87b672019-12-01 20:23:49 -0800841 scheduler_(scheduler),
Brian Silverman4f4e0612020-08-12 19:54:41 -0700842 event_(this),
Austin Schuh58646e22021-08-23 23:51:46 -0700843 token_(scheduler_->InvalidToken()) {
844 VLOG(1) << simulated_event_loop_->distributed_now() << " "
845 << NodeName(simulated_event_loop_->node())
846 << simulated_event_loop_->monotonic_now() << " "
847 << simulated_event_loop_->name() << " Watching "
848 << configuration::StrippedChannelToString(channel_);
849}
Austin Schuh7d87b672019-12-01 20:23:49 -0800850
851SimulatedWatcher::~SimulatedWatcher() {
Austin Schuh58646e22021-08-23 23:51:46 -0700852 VLOG(1) << simulated_event_loop_->distributed_now() << " "
Austin Schuh057d29f2021-08-21 23:05:15 -0700853 << NodeName(simulated_event_loop_->node())
Austin Schuh58646e22021-08-23 23:51:46 -0700854 << simulated_event_loop_->monotonic_now() << " "
855 << simulated_event_loop_->name() << " ~Watching "
Austin Schuh057d29f2021-08-21 23:05:15 -0700856 << configuration::StrippedChannelToString(channel_);
Austin Schuh7d87b672019-12-01 20:23:49 -0800857 simulated_event_loop_->RemoveEvent(&event_);
858 if (token_ != scheduler_->InvalidToken()) {
859 scheduler_->Deschedule(token_);
860 }
Brian Silverman4f4e0612020-08-12 19:54:41 -0700861 CHECK_NOTNULL(simulated_channel_)->RemoveWatcher(this);
Austin Schuh7d87b672019-12-01 20:23:49 -0800862}
863
Austin Schuh8fb315a2020-11-19 22:33:58 -0800864bool SimulatedWatcher::has_run() const {
865 return simulated_event_loop_->has_run();
866}
867
Austin Schuh7d87b672019-12-01 20:23:49 -0800868void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) {
Austin Schuha5e14192020-01-06 18:02:41 -0800869 monotonic_clock::time_point event_time =
870 simulated_event_loop_->monotonic_now();
Austin Schuh7d87b672019-12-01 20:23:49 -0800871
872 // Messages are queued in order. If we are the first, add ourselves.
873 // Otherwise, don't.
874 if (msgs_.size() == 0) {
Austin Schuhad154822019-12-27 15:45:13 -0800875 event_.set_event_time(message->context.monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800876 simulated_event_loop_->AddEvent(&event_);
877
878 DoSchedule(event_time);
879 }
880
Austin Schuhe6f4c8d2021-12-11 12:36:06 -0800881 msgs_.emplace_back(std::move(message));
Austin Schuh7d87b672019-12-01 20:23:49 -0800882}
883
Austin Schuhf4b09c72021-12-08 12:04:37 -0800884void SimulatedWatcher::HandleEvent() noexcept {
Austin Schuh7d87b672019-12-01 20:23:49 -0800885 const monotonic_clock::time_point monotonic_now =
886 simulated_event_loop_->monotonic_now();
Austin Schuh58646e22021-08-23 23:51:46 -0700887 VLOG(1) << simulated_event_loop_->distributed_now() << " "
888 << NodeName(simulated_event_loop_->node())
889 << simulated_event_loop_->monotonic_now() << " "
890 << simulated_event_loop_->name() << " Watcher "
Austin Schuh057d29f2021-08-21 23:05:15 -0700891 << configuration::StrippedChannelToString(channel_);
892 CHECK_NE(msgs_.size(), 0u) << ": No events to handle.";
893
Tyler Chatow67ddb032020-01-12 14:30:04 -0800894 logging::ScopedLogRestorer prev_logger;
Austin Schuha0c41ba2020-09-10 22:59:14 -0700895 if (simulated_event_loop_->log_impl_) {
896 prev_logger.Swap(simulated_event_loop_->log_impl_);
Tyler Chatow67ddb032020-01-12 14:30:04 -0800897 }
Austin Schuhad154822019-12-27 15:45:13 -0800898 Context context = msgs_.front()->context;
899
Brian Silverman4f4e0612020-08-12 19:54:41 -0700900 if (channel_->read_method() != ReadMethod::PIN) {
901 context.buffer_index = -1;
902 }
Austin Schuhad154822019-12-27 15:45:13 -0800903 if (context.remote_queue_index == 0xffffffffu) {
904 context.remote_queue_index = context.queue_index;
905 }
Austin Schuh58646e22021-08-23 23:51:46 -0700906 if (context.monotonic_remote_time == monotonic_clock::min_time) {
Austin Schuhad154822019-12-27 15:45:13 -0800907 context.monotonic_remote_time = context.monotonic_event_time;
908 }
Austin Schuh58646e22021-08-23 23:51:46 -0700909 if (context.realtime_remote_time == realtime_clock::min_time) {
Austin Schuhad154822019-12-27 15:45:13 -0800910 context.realtime_remote_time = context.realtime_event_time;
911 }
912
Austin Schuhcc6070c2020-10-10 20:25:56 -0700913 {
Austin Schuh65493d62022-08-17 15:10:37 -0700914 ScopedMarkRealtimeRestorer rt(
915 simulated_event_loop_->runtime_realtime_priority() > 0);
Austin Schuhcc6070c2020-10-10 20:25:56 -0700916 DoCallCallback([monotonic_now]() { return monotonic_now; }, context);
917 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800918
919 msgs_.pop_front();
Austin Schuheb4e4ce2020-09-10 23:04:18 -0700920 if (token_ != scheduler_->InvalidToken()) {
921 scheduler_->Deschedule(token_);
922 token_ = scheduler_->InvalidToken();
923 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800924 if (msgs_.size() != 0) {
Austin Schuhad154822019-12-27 15:45:13 -0800925 event_.set_event_time(msgs_.front()->context.monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800926 simulated_event_loop_->AddEvent(&event_);
927
928 DoSchedule(event_.event_time());
Austin Schuh7d87b672019-12-01 20:23:49 -0800929 }
930}
931
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800932void SimulatedWatcher::Handle() noexcept {
933 DCHECK(token_ != scheduler_->InvalidToken());
934 token_ = scheduler_->InvalidToken();
935 simulated_event_loop_->HandleEvent();
936}
937
Austin Schuh7d87b672019-12-01 20:23:49 -0800938void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) {
Austin Schuheb4e4ce2020-09-10 23:04:18 -0700939 CHECK(token_ == scheduler_->InvalidToken())
940 << ": May not schedule multiple times";
941 token_ = scheduler_->Schedule(
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800942 event_time + simulated_event_loop_->send_delay(), this);
Austin Schuh7d87b672019-12-01 20:23:49 -0800943}
944
945void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) {
Brian Silverman77162972020-08-12 19:52:40 -0700946 CheckReaderCount();
Austin Schuh39788ff2019-12-01 18:22:57 -0800947 watcher->SetSimulatedChannel(this);
948 watchers_.emplace_back(watcher);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700949}
950
951::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender(
Austin Schuh8fb315a2020-11-19 22:33:58 -0800952 SimulatedEventLoop *event_loop) {
James Kuszmaul890c2492022-04-06 14:59:31 -0700953 CHECK(allow_new_senders_)
954 << ": Attempted to create a new sender on exclusive channel "
955 << configuration::StrippedChannelToString(channel_);
James Kuszmaul94ca5132022-07-19 09:11:08 -0700956 std::optional<ExclusiveSenders> per_channel_option;
957 for (const std::pair<const aos::Channel *, ExclusiveSenders> &per_channel :
958 event_loop->options().per_channel_exclusivity) {
959 if (per_channel.first->name()->string_view() ==
960 channel_->name()->string_view() &&
961 per_channel.first->type()->string_view() ==
962 channel_->type()->string_view()) {
963 CHECK(!per_channel_option.has_value())
964 << ": Channel " << configuration::StrippedChannelToString(channel_)
965 << " listed twice in per-channel list.";
966 per_channel_option = per_channel.second;
967 }
968 }
969 if (!per_channel_option.has_value()) {
970 // This could just as easily be implemented by setting
971 // per_channel_option to the global setting when we initialize it, but
972 // then we'd lose track of whether a given channel appears twice in
973 // the list.
974 per_channel_option = event_loop->options().exclusive_senders;
975 }
976 if (per_channel_option.value() == ExclusiveSenders::kYes) {
James Kuszmaul890c2492022-04-06 14:59:31 -0700977 CHECK_EQ(0, sender_count_)
978 << ": Attempted to add an exclusive sender on a channel with existing "
979 "senders: "
980 << configuration::StrippedChannelToString(channel_);
981 allow_new_senders_ = false;
982 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700983 return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop));
984}
985
Austin Schuh39788ff2019-12-01 18:22:57 -0800986::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher(
987 EventLoop *event_loop) {
Brian Silverman77162972020-08-12 19:52:40 -0700988 CheckReaderCount();
Austin Schuh39788ff2019-12-01 18:22:57 -0800989 ::std::unique_ptr<SimulatedFetcher> fetcher(
990 new SimulatedFetcher(event_loop, this));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700991 fetchers_.push_back(fetcher.get());
992 return ::std::move(fetcher);
993}
994
milind1f1dca32021-07-03 13:50:07 -0700995std::optional<uint32_t> SimulatedChannel::Send(
Austin Schuh60e77942022-05-16 17:48:24 -0700996 std::shared_ptr<SimulatedMessage> message,
997 CheckSentTooFast check_sent_too_fast) {
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700998 const auto now = scheduler_->monotonic_now();
999 // Remove times that are greater than or equal to a channel_storage_duration_
1000 // ago
1001 while (!last_times_.empty() &&
1002 (now - last_times_.front() >= channel_storage_duration_)) {
1003 last_times_.pop();
1004 }
1005
1006 // Check that we are not sending messages too fast
James Kuszmaul890c2492022-04-06 14:59:31 -07001007 if (check_sent_too_fast == CheckSentTooFast::kYes &&
1008 static_cast<int>(last_times_.size()) >= queue_size()) {
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001009 return std::nullopt;
1010 }
1011
1012 const std::optional<uint32_t> queue_index = {next_queue_index_.index()};
1013 last_times_.push(now);
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001014
milind1f1dca32021-07-03 13:50:07 -07001015 message->context.queue_index = *queue_index;
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001016 // Points to the actual data depending on the size set in context. Data may
1017 // allocate more than the actual size of the message, so offset from the back
1018 // of that to get the actual start of the data.
1019 message->context.data =
1020 message->data->data() + message->data->size() - message->context.size;
Austin Schuha9df9ad2021-06-16 14:49:39 -07001021
1022 DCHECK(channel()->has_schema())
1023 << ": Missing schema for channel "
1024 << configuration::StrippedChannelToString(channel());
1025 DCHECK(flatbuffers::Verify(
1026 *channel()->schema(), *channel()->schema()->root_table(),
1027 static_cast<const uint8_t *>(message->context.data),
1028 message->context.size))
1029 << ": Corrupted flatbuffer on " << channel()->name()->c_str() << " "
1030 << channel()->type()->c_str();
1031
Alex Perrycb7da4b2019-08-28 19:35:56 -07001032 next_queue_index_ = next_queue_index_.Increment();
1033
Austin Schuhe6f4c8d2021-12-11 12:36:06 -08001034 latest_message_ = std::move(message);
Austin Schuh8fb315a2020-11-19 22:33:58 -08001035 for (SimulatedWatcher *watcher : watchers_) {
1036 if (watcher->has_run()) {
Austin Schuhe6f4c8d2021-12-11 12:36:06 -08001037 watcher->Schedule(latest_message_);
Alex Perrycb7da4b2019-08-28 19:35:56 -07001038 }
1039 }
1040 for (auto &fetcher : fetchers_) {
Austin Schuhe6f4c8d2021-12-11 12:36:06 -08001041 fetcher->Enqueue(latest_message_);
Alex Perrycb7da4b2019-08-28 19:35:56 -07001042 }
Austin Schuhad154822019-12-27 15:45:13 -08001043 return queue_index;
Alex Perrycb7da4b2019-08-28 19:35:56 -07001044}
1045
1046void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) {
1047 fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher));
1048}
1049
Austin Schuh8fb315a2020-11-19 22:33:58 -08001050SimulatedSender::SimulatedSender(SimulatedChannel *simulated_channel,
1051 SimulatedEventLoop *event_loop)
1052 : RawSender(event_loop, simulated_channel->channel()),
1053 simulated_channel_(simulated_channel),
Austin Schuh58646e22021-08-23 23:51:46 -07001054 simulated_event_loop_(event_loop) {
Austin Schuh8fb315a2020-11-19 22:33:58 -08001055 simulated_channel_->CountSenderCreated();
1056}
1057
1058SimulatedSender::~SimulatedSender() {
1059 simulated_channel_->CountSenderDestroyed();
1060}
1061
milind1f1dca32021-07-03 13:50:07 -07001062RawSender::Error SimulatedSender::DoSend(
1063 size_t length, monotonic_clock::time_point monotonic_remote_time,
1064 realtime_clock::time_point realtime_remote_time,
1065 uint32_t remote_queue_index, const UUID &source_boot_uuid) {
Austin Schuh9b1d6282022-06-10 17:03:21 -07001066 // The allocations in here are due to infrastructure and don't count in the
1067 // no mallocs in RT code.
1068 ScopedNotRealtime nrt;
1069
Austin Schuh58646e22021-08-23 23:51:46 -07001070 VLOG(1) << simulated_event_loop_->distributed_now() << " "
1071 << NodeName(simulated_event_loop_->node())
1072 << simulated_event_loop_->monotonic_now() << " "
1073 << simulated_event_loop_->name() << " Send "
1074 << configuration::StrippedChannelToString(channel());
1075
Austin Schuh8fb315a2020-11-19 22:33:58 -08001076 CHECK_LE(length, size()) << ": Attempting to send too big a message.";
Austin Schuh58646e22021-08-23 23:51:46 -07001077 message_->context.monotonic_event_time =
1078 simulated_event_loop_->monotonic_now();
Austin Schuh8fb315a2020-11-19 22:33:58 -08001079 message_->context.monotonic_remote_time = monotonic_remote_time;
1080 message_->context.remote_queue_index = remote_queue_index;
Austin Schuh58646e22021-08-23 23:51:46 -07001081 message_->context.realtime_event_time = simulated_event_loop_->realtime_now();
Austin Schuh8fb315a2020-11-19 22:33:58 -08001082 message_->context.realtime_remote_time = realtime_remote_time;
Austin Schuha9012be2021-07-21 15:19:11 -07001083 message_->context.source_boot_uuid = source_boot_uuid;
Austin Schuh8fb315a2020-11-19 22:33:58 -08001084 CHECK_LE(length, message_->context.size);
1085 message_->context.size = length;
1086
Austin Schuh60e77942022-05-16 17:48:24 -07001087 const std::optional<uint32_t> optional_queue_index = simulated_channel_->Send(
1088 message_, simulated_event_loop_->options().check_sent_too_fast);
milind1f1dca32021-07-03 13:50:07 -07001089
1090 // Check that we are not sending messages too fast
1091 if (!optional_queue_index) {
1092 VLOG(1) << simulated_event_loop_->distributed_now() << " "
1093 << NodeName(simulated_event_loop_->node())
1094 << simulated_event_loop_->monotonic_now() << " "
1095 << simulated_event_loop_->name()
1096 << "\nMessages were sent too fast:\n"
1097 << "For channel: "
1098 << configuration::CleanedChannelToString(
1099 simulated_channel_->channel())
1100 << '\n'
1101 << "Tried to send more than " << simulated_channel_->queue_size()
1102 << " (queue size) messages in the last "
1103 << std::chrono::duration<double>(
1104 simulated_channel_->channel_storage_duration())
1105 .count()
1106 << " seconds (channel storage duration)"
1107 << "\n\n";
1108 return Error::kMessagesSentTooFast;
1109 }
1110
1111 sent_queue_index_ = *optional_queue_index;
Austin Schuh58646e22021-08-23 23:51:46 -07001112 monotonic_sent_time_ = simulated_event_loop_->monotonic_now();
1113 realtime_sent_time_ = simulated_event_loop_->realtime_now();
Austin Schuh8fb315a2020-11-19 22:33:58 -08001114
1115 // Drop the reference to the message so that we allocate a new message for
1116 // next time. Otherwise we will continue to reuse the same memory for all
1117 // messages and corrupt it.
1118 message_.reset();
milind1f1dca32021-07-03 13:50:07 -07001119 return Error::kOk;
Austin Schuh8fb315a2020-11-19 22:33:58 -08001120}
1121
milind1f1dca32021-07-03 13:50:07 -07001122RawSender::Error SimulatedSender::DoSend(
1123 const void *msg, size_t size,
1124 monotonic_clock::time_point monotonic_remote_time,
1125 realtime_clock::time_point realtime_remote_time,
1126 uint32_t remote_queue_index, const UUID &source_boot_uuid) {
Austin Schuh102667e2020-12-11 20:13:28 -08001127 CHECK_LE(size, this->size())
1128 << ": Attempting to send too big a message on "
1129 << configuration::CleanedChannelToString(simulated_channel_->channel());
Austin Schuh8fb315a2020-11-19 22:33:58 -08001130
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001131 // Allocates an aligned buffer in which to copy unaligned msg.
1132 auto [span, mutable_span] = MakeSharedSpan(size);
1133 message_ = SimulatedMessage::Make(simulated_channel_, span);
Austin Schuh8fb315a2020-11-19 22:33:58 -08001134
1135 // Now fill in the message. size is already populated above, and
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001136 // queue_index will be populated in simulated_channel_.
1137 memcpy(mutable_span.data(), msg, size);
Austin Schuh8fb315a2020-11-19 22:33:58 -08001138
1139 return DoSend(size, monotonic_remote_time, realtime_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -07001140 remote_queue_index, source_boot_uuid);
Austin Schuh8fb315a2020-11-19 22:33:58 -08001141}
1142
milind1f1dca32021-07-03 13:50:07 -07001143RawSender::Error SimulatedSender::DoSend(
1144 const RawSender::SharedSpan data,
1145 monotonic_clock::time_point monotonic_remote_time,
1146 realtime_clock::time_point realtime_remote_time,
1147 uint32_t remote_queue_index, const UUID &source_boot_uuid) {
Tyler Chatowb7c6eba2021-07-28 14:43:23 -07001148 CHECK_LE(data->size(), this->size())
1149 << ": Attempting to send too big a message on "
1150 << configuration::CleanedChannelToString(simulated_channel_->channel());
1151
1152 // Constructs a message sharing the already allocated and aligned message
1153 // data.
1154 message_ = SimulatedMessage::Make(simulated_channel_, data);
1155
1156 return DoSend(data->size(), monotonic_remote_time, realtime_remote_time,
1157 remote_queue_index, source_boot_uuid);
1158}
1159
Austin Schuh39788ff2019-12-01 18:22:57 -08001160SimulatedTimerHandler::SimulatedTimerHandler(
Austin Schuh8bd96322020-02-13 21:18:22 -08001161 EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop,
1162 ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -08001163 : TimerHandler(simulated_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -08001164 simulated_event_loop_(simulated_event_loop),
1165 event_(this),
Austin Schuh39788ff2019-12-01 18:22:57 -08001166 scheduler_(scheduler),
1167 token_(scheduler_->InvalidToken()) {}
1168
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001169void SimulatedTimerHandler::Setup(monotonic_clock::time_point base,
1170 monotonic_clock::duration repeat_offset) {
James Kuszmaul86e86c32022-07-21 17:39:47 -07001171 CHECK_GE(base, monotonic_clock::epoch());
Austin Schuh62288252020-11-18 23:26:04 -08001172 // The allocations in here are due to infrastructure and don't count in the no
1173 // mallocs in RT code.
1174 ScopedNotRealtime nrt;
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001175 Disable();
Austin Schuh58646e22021-08-23 23:51:46 -07001176 const monotonic_clock::time_point monotonic_now =
Austin Schuha5e14192020-01-06 18:02:41 -08001177 simulated_event_loop_->monotonic_now();
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001178 base_ = base;
1179 repeat_offset_ = repeat_offset;
Austin Schuhef8f1ae2021-12-11 12:35:05 -08001180 token_ = scheduler_->Schedule(std::max(base, monotonic_now), this);
Austin Schuh7d87b672019-12-01 20:23:49 -08001181 event_.set_event_time(base_);
1182 simulated_event_loop_->AddEvent(&event_);
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001183}
1184
Austin Schuhef8f1ae2021-12-11 12:35:05 -08001185void SimulatedTimerHandler::Handle() noexcept {
1186 DCHECK(token_ != scheduler_->InvalidToken());
1187 token_ = scheduler_->InvalidToken();
1188 simulated_event_loop_->HandleEvent();
1189}
1190
Austin Schuhf4b09c72021-12-08 12:04:37 -08001191void SimulatedTimerHandler::HandleEvent() noexcept {
Austin Schuh58646e22021-08-23 23:51:46 -07001192 const monotonic_clock::time_point monotonic_now =
Austin Schuha5e14192020-01-06 18:02:41 -08001193 simulated_event_loop_->monotonic_now();
Austin Schuh58646e22021-08-23 23:51:46 -07001194 VLOG(1) << simulated_event_loop_->distributed_now() << " "
1195 << NodeName(simulated_event_loop_->node()) << monotonic_now << " "
1196 << simulated_event_loop_->name() << " Timer '" << name() << "'";
Tyler Chatow67ddb032020-01-12 14:30:04 -08001197 logging::ScopedLogRestorer prev_logger;
Austin Schuha0c41ba2020-09-10 22:59:14 -07001198 if (simulated_event_loop_->log_impl_) {
1199 prev_logger.Swap(simulated_event_loop_->log_impl_);
Tyler Chatow67ddb032020-01-12 14:30:04 -08001200 }
Austin Schuheb4e4ce2020-09-10 23:04:18 -07001201 if (token_ != scheduler_->InvalidToken()) {
Austin Schuh9b1d6282022-06-10 17:03:21 -07001202 {
1203 ScopedNotRealtime nrt;
1204 scheduler_->Deschedule(token_);
1205 }
Austin Schuheb4e4ce2020-09-10 23:04:18 -07001206 token_ = scheduler_->InvalidToken();
1207 }
Austin Schuh58646e22021-08-23 23:51:46 -07001208 if (repeat_offset_ != monotonic_clock::zero()) {
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001209 // Reschedule.
1210 while (base_ <= monotonic_now) base_ += repeat_offset_;
Austin Schuhef8f1ae2021-12-11 12:35:05 -08001211 token_ = scheduler_->Schedule(base_, this);
Austin Schuh7d87b672019-12-01 20:23:49 -08001212 event_.set_event_time(base_);
1213 simulated_event_loop_->AddEvent(&event_);
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001214 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001215
Austin Schuhcc6070c2020-10-10 20:25:56 -07001216 {
Austin Schuh65493d62022-08-17 15:10:37 -07001217 ScopedMarkRealtimeRestorer rt(
1218 simulated_event_loop_->runtime_realtime_priority() > 0);
Austin Schuhcc6070c2020-10-10 20:25:56 -07001219 Call([monotonic_now]() { return monotonic_now; }, monotonic_now);
1220 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001221}
1222
Austin Schuh7d87b672019-12-01 20:23:49 -08001223void SimulatedTimerHandler::Disable() {
1224 simulated_event_loop_->RemoveEvent(&event_);
1225 if (token_ != scheduler_->InvalidToken()) {
Austin Schuh9b1d6282022-06-10 17:03:21 -07001226 {
1227 ScopedNotRealtime nrt;
1228 scheduler_->Deschedule(token_);
1229 }
Austin Schuh7d87b672019-12-01 20:23:49 -08001230 token_ = scheduler_->InvalidToken();
1231 }
1232}
1233
Austin Schuh39788ff2019-12-01 18:22:57 -08001234SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler(
Austin Schuh8bd96322020-02-13 21:18:22 -08001235 EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop,
1236 ::std::function<void(int)> fn, const monotonic_clock::duration interval,
Austin Schuh39788ff2019-12-01 18:22:57 -08001237 const monotonic_clock::duration offset)
1238 : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset),
1239 simulated_event_loop_(simulated_event_loop),
Austin Schuh7d87b672019-12-01 20:23:49 -08001240 event_(this),
Austin Schuh39788ff2019-12-01 18:22:57 -08001241 scheduler_(scheduler),
1242 token_(scheduler_->InvalidToken()) {}
1243
Austin Schuh7d87b672019-12-01 20:23:49 -08001244SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() {
1245 if (token_ != scheduler_->InvalidToken()) {
1246 scheduler_->Deschedule(token_);
1247 token_ = scheduler_->InvalidToken();
1248 }
1249 simulated_event_loop_->RemoveEvent(&event_);
1250}
1251
Austin Schuhf4b09c72021-12-08 12:04:37 -08001252void SimulatedPhasedLoopHandler::HandleEvent() noexcept {
Austin Schuh39788ff2019-12-01 18:22:57 -08001253 monotonic_clock::time_point monotonic_now =
1254 simulated_event_loop_->monotonic_now();
Austin Schuh057d29f2021-08-21 23:05:15 -07001255 VLOG(1) << monotonic_now << " Phased loop " << simulated_event_loop_->name()
1256 << ", " << name();
Tyler Chatow67ddb032020-01-12 14:30:04 -08001257 logging::ScopedLogRestorer prev_logger;
Austin Schuha0c41ba2020-09-10 22:59:14 -07001258 if (simulated_event_loop_->log_impl_) {
1259 prev_logger.Swap(simulated_event_loop_->log_impl_);
Tyler Chatow67ddb032020-01-12 14:30:04 -08001260 }
Austin Schuhcc6070c2020-10-10 20:25:56 -07001261
1262 {
Austin Schuh65493d62022-08-17 15:10:37 -07001263 ScopedMarkRealtimeRestorer rt(
1264 simulated_event_loop_->runtime_realtime_priority() > 0);
Austin Schuhcc6070c2020-10-10 20:25:56 -07001265 Call([monotonic_now]() { return monotonic_now; },
1266 [this](monotonic_clock::time_point sleep_time) {
1267 Schedule(sleep_time);
1268 });
1269 }
Austin Schuh39788ff2019-12-01 18:22:57 -08001270}
Austin Schuhde8a8ff2019-11-30 15:25:36 -08001271
Austin Schuhef8f1ae2021-12-11 12:35:05 -08001272void SimulatedPhasedLoopHandler::Handle() noexcept {
1273 DCHECK(token_ != scheduler_->InvalidToken());
1274 token_ = scheduler_->InvalidToken();
1275 simulated_event_loop_->HandleEvent();
1276}
1277
Austin Schuh7d87b672019-12-01 20:23:49 -08001278void SimulatedPhasedLoopHandler::Schedule(
1279 monotonic_clock::time_point sleep_time) {
Austin Schuh62288252020-11-18 23:26:04 -08001280 // The allocations in here are due to infrastructure and don't count in the no
1281 // mallocs in RT code.
1282 ScopedNotRealtime nrt;
Austin Schuheb4e4ce2020-09-10 23:04:18 -07001283 if (token_ != scheduler_->InvalidToken()) {
1284 scheduler_->Deschedule(token_);
1285 token_ = scheduler_->InvalidToken();
1286 }
Austin Schuhef8f1ae2021-12-11 12:35:05 -08001287 token_ = scheduler_->Schedule(sleep_time, this);
Austin Schuh7d87b672019-12-01 20:23:49 -08001288 event_.set_event_time(sleep_time);
1289 simulated_event_loop_->AddEvent(&event_);
1290}
1291
Alex Perrycb7da4b2019-08-28 19:35:56 -07001292SimulatedEventLoopFactory::SimulatedEventLoopFactory(
1293 const Configuration *configuration)
Austin Schuh6f3babe2020-01-26 20:34:50 -08001294 : configuration_(CHECK_NOTNULL(configuration)),
1295 nodes_(configuration::GetNodes(configuration_)) {
Austin Schuh094d09b2020-11-20 23:26:52 -08001296 CHECK(IsInitialized()) << ": Need to initialize AOS first.";
Austin Schuhac0771c2020-01-07 18:36:30 -08001297 for (const Node *node : nodes_) {
Austin Schuh58646e22021-08-23 23:51:46 -07001298 node_factories_.emplace_back(
1299 new NodeEventLoopFactory(&scheduler_scheduler_, this, node));
Austin Schuh15649d62019-12-28 16:36:38 -08001300 }
Austin Schuh898f4972020-01-11 17:21:25 -08001301
1302 if (configuration::MultiNode(configuration)) {
1303 bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this);
1304 }
Austin Schuh15649d62019-12-28 16:36:38 -08001305}
1306
Alex Perrycb7da4b2019-08-28 19:35:56 -07001307SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {}
1308
Austin Schuhac0771c2020-01-07 18:36:30 -08001309NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory(
Austin Schuh057d29f2021-08-21 23:05:15 -07001310 std::string_view node) {
1311 return GetNodeEventLoopFactory(configuration::GetNode(configuration(), node));
1312}
1313
1314NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory(
Austin Schuhac0771c2020-01-07 18:36:30 -08001315 const Node *node) {
1316 auto result = std::find_if(
1317 node_factories_.begin(), node_factories_.end(),
1318 [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) {
1319 return node_factory->node() == node;
1320 });
1321
1322 CHECK(result != node_factories_.end())
1323 << ": Failed to find node " << FlatbufferToJson(node);
1324
1325 return result->get();
1326}
1327
Austin Schuh87dd3832021-01-01 23:07:31 -08001328void SimulatedEventLoopFactory::SetTimeConverter(
1329 TimeConverter *time_converter) {
1330 for (std::unique_ptr<NodeEventLoopFactory> &factory : node_factories_) {
1331 factory->SetTimeConverter(time_converter);
1332 }
Austin Schuh58646e22021-08-23 23:51:46 -07001333 scheduler_scheduler_.SetTimeConverter(time_converter);
Austin Schuh87dd3832021-01-01 23:07:31 -08001334}
1335
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08001336::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop(
Austin Schuhac0771c2020-01-07 18:36:30 -08001337 std::string_view name, const Node *node) {
1338 if (node == nullptr) {
1339 CHECK(!configuration::MultiNode(configuration()))
1340 << ": Can't make a single node event loop in a multi-node world.";
1341 } else {
1342 CHECK(configuration::MultiNode(configuration()))
1343 << ": Can't make a multi-node event loop in a single-node world.";
1344 }
1345 return GetNodeEventLoopFactory(node)->MakeEventLoop(name);
1346}
1347
Austin Schuh057d29f2021-08-21 23:05:15 -07001348NodeEventLoopFactory::NodeEventLoopFactory(
1349 EventSchedulerScheduler *scheduler_scheduler,
1350 SimulatedEventLoopFactory *factory, const Node *node)
Austin Schuh58646e22021-08-23 23:51:46 -07001351 : scheduler_(configuration::GetNodeIndex(factory->configuration(), node)),
1352 factory_(factory),
1353 node_(node) {
Austin Schuh057d29f2021-08-21 23:05:15 -07001354 scheduler_scheduler->AddEventScheduler(&scheduler_);
Austin Schuh58646e22021-08-23 23:51:46 -07001355 scheduler_.set_started([this]() {
1356 started_ = true;
1357 for (SimulatedEventLoop *event_loop : event_loops_) {
1358 event_loop->SetIsRunning(true);
1359 }
1360 });
Austin Schuhe33c08d2022-02-03 18:15:21 -08001361 scheduler_.set_stopped([this]() {
1362 for (SimulatedEventLoop *event_loop : event_loops_) {
1363 event_loop->SetIsRunning(false);
1364 }
1365 });
Austin Schuh58646e22021-08-23 23:51:46 -07001366 scheduler_.set_on_shutdown([this]() {
1367 VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node())
1368 << monotonic_now() << " Shutting down node.";
1369 Shutdown();
1370 ScheduleStartup();
1371 });
1372 ScheduleStartup();
Austin Schuh057d29f2021-08-21 23:05:15 -07001373}
1374
1375NodeEventLoopFactory::~NodeEventLoopFactory() {
Austin Schuh58646e22021-08-23 23:51:46 -07001376 if (started_) {
1377 for (std::function<void()> &fn : on_shutdown_) {
1378 fn();
1379 }
1380
1381 VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node())
1382 << monotonic_now() << " Shutting down applications.";
1383 applications_.clear();
1384 started_ = false;
1385 }
1386
1387 if (event_loops_.size() != 0u) {
1388 for (SimulatedEventLoop *event_loop : event_loops_) {
1389 LOG(ERROR) << scheduler_.distributed_now() << " " << NodeName(node())
1390 << monotonic_now() << " Event loop '" << event_loop->name()
1391 << "' failed to shut down";
1392 }
1393 }
Austin Schuh057d29f2021-08-21 23:05:15 -07001394 CHECK_EQ(event_loops_.size(), 0u) << "Event loop didn't exit";
1395}
1396
Austin Schuh58646e22021-08-23 23:51:46 -07001397void NodeEventLoopFactory::OnStartup(std::function<void()> &&fn) {
Austin Schuh8bd96322020-02-13 21:18:22 -08001398 CHECK(!scheduler_.is_running())
Austin Schuh58646e22021-08-23 23:51:46 -07001399 << ": Can only register OnStartup handlers when not running.";
1400 on_startup_.emplace_back(std::move(fn));
1401 if (started_) {
1402 size_t on_startup_index = on_startup_.size() - 1;
1403 scheduler_.ScheduleOnStartup(
1404 [this, on_startup_index]() { on_startup_[on_startup_index](); });
1405 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001406}
1407
Austin Schuh58646e22021-08-23 23:51:46 -07001408void NodeEventLoopFactory::OnShutdown(std::function<void()> &&fn) {
1409 on_shutdown_.emplace_back(std::move(fn));
Austin Schuhc0b0f722020-12-12 18:36:06 -08001410}
Austin Schuh057d29f2021-08-21 23:05:15 -07001411
Austin Schuh58646e22021-08-23 23:51:46 -07001412void NodeEventLoopFactory::ScheduleStartup() {
1413 scheduler_.ScheduleOnStartup([this]() {
1414 UUID next_uuid = scheduler_.boot_uuid();
1415 if (boot_uuid_ != next_uuid) {
Austin Schuh188a2f62021-11-08 10:45:54 -08001416 CHECK_EQ(boot_uuid_, UUID::Zero())
1417 << ": Boot UUID changed without restarting. Did TimeConverter "
1418 "change the boot UUID without signaling a restart, or did you "
1419 "change TimeConverter?";
Austin Schuh58646e22021-08-23 23:51:46 -07001420 boot_uuid_ = next_uuid;
1421 }
1422 VLOG(1) << scheduler_.distributed_now() << " " << NodeName(this->node())
1423 << monotonic_now() << " Starting up node on boot " << boot_uuid_;
1424 Startup();
1425 });
1426}
1427
1428void NodeEventLoopFactory::Startup() {
1429 CHECK(!started_);
1430 for (size_t i = 0; i < on_startup_.size(); ++i) {
1431 on_startup_[i]();
1432 }
1433}
1434
1435void NodeEventLoopFactory::Shutdown() {
1436 for (SimulatedEventLoop *event_loop : event_loops_) {
Austin Schuhe33c08d2022-02-03 18:15:21 -08001437 CHECK(!event_loop->is_running());
Austin Schuh58646e22021-08-23 23:51:46 -07001438 }
1439
1440 CHECK(started_);
1441 started_ = false;
1442 for (std::function<void()> &fn : on_shutdown_) {
1443 fn();
1444 }
1445
1446 VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node())
1447 << monotonic_now() << " Shutting down applications.";
1448 applications_.clear();
1449
1450 if (event_loops_.size() != 0u) {
1451 for (SimulatedEventLoop *event_loop : event_loops_) {
1452 LOG(ERROR) << scheduler_.distributed_now() << " " << NodeName(node())
1453 << monotonic_now() << " Event loop '" << event_loop->name()
1454 << "' failed to shut down";
1455 }
1456 }
1457 CHECK_EQ(event_loops_.size(), 0u) << "Not all event loops shut down";
1458 boot_uuid_ = UUID::Zero();
1459
1460 channels_.clear();
Austin Schuhc0b0f722020-12-12 18:36:06 -08001461}
1462
Alex Perrycb7da4b2019-08-28 19:35:56 -07001463void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) {
Austin Schuh58646e22021-08-23 23:51:46 -07001464 // This sets running to true too.
Austin Schuh8bd96322020-02-13 21:18:22 -08001465 scheduler_scheduler_.RunFor(duration);
Austin Schuh057d29f2021-08-21 23:05:15 -07001466 for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) {
1467 if (node) {
1468 for (SimulatedEventLoop *loop : node->event_loops_) {
Austin Schuhe33c08d2022-02-03 18:15:21 -08001469 CHECK(!loop->is_running());
Austin Schuh057d29f2021-08-21 23:05:15 -07001470 }
1471 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001472 }
1473}
1474
1475void SimulatedEventLoopFactory::Run() {
Austin Schuh58646e22021-08-23 23:51:46 -07001476 // This sets running to true too.
Austin Schuh8bd96322020-02-13 21:18:22 -08001477 scheduler_scheduler_.Run();
Austin Schuh057d29f2021-08-21 23:05:15 -07001478 for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) {
1479 if (node) {
1480 for (SimulatedEventLoop *loop : node->event_loops_) {
Austin Schuhe33c08d2022-02-03 18:15:21 -08001481 CHECK(!loop->is_running());
Austin Schuh057d29f2021-08-21 23:05:15 -07001482 }
1483 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001484 }
1485}
1486
Austin Schuh87dd3832021-01-01 23:07:31 -08001487void SimulatedEventLoopFactory::Exit() { scheduler_scheduler_.Exit(); }
Austin Schuh8fb315a2020-11-19 22:33:58 -08001488
Austin Schuh6f3babe2020-01-26 20:34:50 -08001489void SimulatedEventLoopFactory::DisableForwarding(const Channel *channel) {
Austin Schuh4c3b9702020-08-30 11:34:55 -07001490 CHECK(bridge_) << ": Can't disable forwarding without a message bridge.";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001491 bridge_->DisableForwarding(channel);
1492}
1493
Austin Schuh4c3b9702020-08-30 11:34:55 -07001494void SimulatedEventLoopFactory::DisableStatistics() {
1495 CHECK(bridge_) << ": Can't disable statistics without a message bridge.";
James Kuszmaul94ca5132022-07-19 09:11:08 -07001496 bridge_->DisableStatistics(
1497 message_bridge::SimulatedMessageBridge::DestroySenders::kNo);
1498}
1499
1500void SimulatedEventLoopFactory::PermanentlyDisableStatistics() {
1501 CHECK(bridge_) << ": Can't disable statistics without a message bridge.";
1502 bridge_->DisableStatistics(
1503 message_bridge::SimulatedMessageBridge::DestroySenders::kYes);
Austin Schuh4c3b9702020-08-30 11:34:55 -07001504}
1505
Austin Schuh48205e62021-11-12 14:13:18 -08001506void SimulatedEventLoopFactory::EnableStatistics() {
1507 CHECK(bridge_) << ": Can't enable statistics without a message bridge.";
1508 bridge_->EnableStatistics();
1509}
1510
Austin Schuh2928ebe2021-02-07 22:10:27 -08001511void SimulatedEventLoopFactory::SkipTimingReport() {
1512 CHECK(bridge_) << ": Can't skip timing reports without a message bridge.";
Austin Schuh48205e62021-11-12 14:13:18 -08001513
1514 for (std::unique_ptr<NodeEventLoopFactory> &node : node_factories_) {
1515 if (node) {
1516 node->SkipTimingReport();
1517 }
1518 }
1519}
1520
1521void NodeEventLoopFactory::SkipTimingReport() {
1522 for (SimulatedEventLoop *event_loop : event_loops_) {
1523 event_loop->SkipTimingReport();
1524 }
1525 skip_timing_report_ = true;
1526}
1527
1528void NodeEventLoopFactory::EnableStatistics() {
1529 CHECK(factory_->bridge_)
1530 << ": Can't enable statistics without a message bridge.";
1531 factory_->bridge_->EnableStatistics(node_);
1532}
1533
1534void NodeEventLoopFactory::DisableStatistics() {
1535 CHECK(factory_->bridge_)
1536 << ": Can't disable statistics without a message bridge.";
1537 factory_->bridge_->DisableStatistics(node_);
Austin Schuh2928ebe2021-02-07 22:10:27 -08001538}
1539
Austin Schuh58646e22021-08-23 23:51:46 -07001540::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop(
James Kuszmaul890c2492022-04-06 14:59:31 -07001541 std::string_view name, EventLoopOptions options) {
Austin Schuh58646e22021-08-23 23:51:46 -07001542 CHECK(!scheduler_.is_running() || !started_)
1543 << ": Can't create an event loop while running";
1544
1545 pid_t tid = tid_;
1546 ++tid_;
1547 ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop(
1548 &scheduler_, this, &channels_, factory_->configuration(), &event_loops_,
James Kuszmaul890c2492022-04-06 14:59:31 -07001549 node_, tid, options));
Austin Schuh58646e22021-08-23 23:51:46 -07001550 result->set_name(name);
1551 result->set_send_delay(factory_->send_delay());
Austin Schuh48205e62021-11-12 14:13:18 -08001552 if (skip_timing_report_) {
1553 result->SkipTimingReport();
1554 }
Austin Schuh58646e22021-08-23 23:51:46 -07001555
1556 VLOG(1) << scheduler_.distributed_now() << " " << NodeName(node())
1557 << monotonic_now() << " MakeEventLoop(\"" << result->name() << "\")";
1558 return std::move(result);
1559}
1560
Austin Schuhe33c08d2022-02-03 18:15:21 -08001561void SimulatedEventLoopFactory::AllowApplicationCreationDuring(
1562 std::function<void()> fn) {
1563 scheduler_scheduler_.TemporarilyStopAndRun(std::move(fn));
1564}
1565
Austin Schuh58646e22021-08-23 23:51:46 -07001566void NodeEventLoopFactory::Disconnect(const Node *other) {
1567 factory_->bridge_->Disconnect(node_, other);
1568}
1569
1570void NodeEventLoopFactory::Connect(const Node *other) {
1571 factory_->bridge_->Connect(node_, other);
1572}
1573
Alex Perrycb7da4b2019-08-28 19:35:56 -07001574} // namespace aos