blob: 01574fa44d180bb2599ae8f5191ecd9b104b9b59 [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>
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08005#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07006
7#include "absl/container/btree_map.h"
Austin Schuh898f4972020-01-11 17:21:25 -08008#include "aos/events/simulated_network_bridge.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "aos/json_to_flatbuffer.h"
10#include "aos/util/phased_loop.h"
Tyler Chatow67ddb032020-01-12 14:30:04 -080011#include "aos/events/aos_logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012
13namespace aos {
14
15// Container for both a message, and the context for it for simulation. This
16// makes tracking the timestamps associated with the data easy.
17struct SimulatedMessage {
Alex Perrycb7da4b2019-08-28 19:35:56 -070018 // Context for the data.
19 Context context;
20
21 // The data.
Brian Silvermana1652f32020-01-29 20:41:44 -080022 char *data(size_t buffer_size) {
23 return RoundChannelData(&actual_data[0], buffer_size);
24 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070025
Brian Silvermana1652f32020-01-29 20:41:44 -080026 // Then the data, including padding on the end so we can align the buffer we
27 // actually return from data().
28 char actual_data[];
Alex Perrycb7da4b2019-08-28 19:35:56 -070029};
30
Austin Schuh7d87b672019-12-01 20:23:49 -080031class SimulatedEventLoop;
Alex Perrycb7da4b2019-08-28 19:35:56 -070032class SimulatedFetcher;
Austin Schuh39788ff2019-12-01 18:22:57 -080033class SimulatedChannel;
34
Austin Schuh7d87b672019-12-01 20:23:49 -080035class SimulatedWatcher : public WatcherState {
Austin Schuh39788ff2019-12-01 18:22:57 -080036 public:
Austin Schuh7d87b672019-12-01 20:23:49 -080037 SimulatedWatcher(
38 SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler,
Austin Schuhac0771c2020-01-07 18:36:30 -080039 NodeEventLoopFactory *node_event_loop_factory,
Austin Schuh7d87b672019-12-01 20:23:49 -080040 const Channel *channel,
41 std::function<void(const Context &context, const void *message)> fn);
Austin Schuh39788ff2019-12-01 18:22:57 -080042
Austin Schuh7d87b672019-12-01 20:23:49 -080043 ~SimulatedWatcher() override;
Austin Schuh39788ff2019-12-01 18:22:57 -080044
45 void Startup(EventLoop * /*event_loop*/) override {}
46
Austin Schuh7d87b672019-12-01 20:23:49 -080047 void Schedule(std::shared_ptr<SimulatedMessage> message);
48
49 void HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -080050
51 void SetSimulatedChannel(SimulatedChannel *channel) {
52 simulated_channel_ = channel;
53 }
54
55 private:
Austin Schuh7d87b672019-12-01 20:23:49 -080056 void DoSchedule(monotonic_clock::time_point event_time);
57
58 ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_;
59
60 SimulatedEventLoop *simulated_event_loop_;
61 EventHandler<SimulatedWatcher> event_;
62 EventScheduler *scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -080063 NodeEventLoopFactory *node_event_loop_factory_;
Austin Schuh7d87b672019-12-01 20:23:49 -080064 EventScheduler::Token token_;
Austin Schuh39788ff2019-12-01 18:22:57 -080065 SimulatedChannel *simulated_channel_ = nullptr;
66};
Alex Perrycb7da4b2019-08-28 19:35:56 -070067
68class SimulatedChannel {
69 public:
70 explicit SimulatedChannel(const Channel *channel, EventScheduler *scheduler)
Austin Schuh39788ff2019-12-01 18:22:57 -080071 : channel_(channel),
Alex Perrycb7da4b2019-08-28 19:35:56 -070072 scheduler_(scheduler),
73 next_queue_index_(ipc_lib::QueueIndex::Zero(channel->max_size())) {}
74
75 ~SimulatedChannel() { CHECK_EQ(0u, fetchers_.size()); }
76
77 // Makes a connected raw sender which calls Send below.
78 ::std::unique_ptr<RawSender> MakeRawSender(EventLoop *event_loop);
79
80 // Makes a connected raw fetcher.
Austin Schuh39788ff2019-12-01 18:22:57 -080081 ::std::unique_ptr<RawFetcher> MakeRawFetcher(EventLoop *event_loop);
Alex Perrycb7da4b2019-08-28 19:35:56 -070082
83 // Registers a watcher for the queue.
Austin Schuh7d87b672019-12-01 20:23:49 -080084 void MakeRawWatcher(SimulatedWatcher *watcher);
Austin Schuh39788ff2019-12-01 18:22:57 -080085
Austin Schuh7d87b672019-12-01 20:23:49 -080086 void RemoveWatcher(SimulatedWatcher *watcher) {
Austin Schuh39788ff2019-12-01 18:22:57 -080087 watchers_.erase(std::find(watchers_.begin(), watchers_.end(), watcher));
88 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070089
Austin Schuhad154822019-12-27 15:45:13 -080090 // Sends the message to all the connected receivers and fetchers. Returns the
91 // sent queue index.
92 uint32_t Send(std::shared_ptr<SimulatedMessage> message);
Alex Perrycb7da4b2019-08-28 19:35:56 -070093
94 // Unregisters a fetcher.
95 void UnregisterFetcher(SimulatedFetcher *fetcher);
96
97 std::shared_ptr<SimulatedMessage> latest_message() { return latest_message_; }
98
Austin Schuh39788ff2019-12-01 18:22:57 -080099 size_t max_size() const { return channel()->max_size(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700100
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800101 const std::string_view name() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800102 return channel()->name()->string_view();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700103 }
104
Austin Schuh39788ff2019-12-01 18:22:57 -0800105 const Channel *channel() const { return channel_; }
106
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800108 const Channel *channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700109
110 // List of all watchers.
Austin Schuh7d87b672019-12-01 20:23:49 -0800111 ::std::vector<SimulatedWatcher *> watchers_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112
113 // List of all fetchers.
114 ::std::vector<SimulatedFetcher *> fetchers_;
115 std::shared_ptr<SimulatedMessage> latest_message_;
116 EventScheduler *scheduler_;
117
118 ipc_lib::QueueIndex next_queue_index_;
119};
120
121namespace {
122
123// Creates a SimulatedMessage with size bytes of storage.
124// This is a shared_ptr so we don't have to implement refcounting or copying.
125std::shared_ptr<SimulatedMessage> MakeSimulatedMessage(size_t size) {
126 SimulatedMessage *message = reinterpret_cast<SimulatedMessage *>(
Brian Silvermana1652f32020-01-29 20:41:44 -0800127 malloc(sizeof(SimulatedMessage) + size + kChannelDataAlignment - 1));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700128 message->context.size = size;
Brian Silvermana1652f32020-01-29 20:41:44 -0800129 message->context.data = message->data(size);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130
131 return std::shared_ptr<SimulatedMessage>(message, free);
132}
133
134class SimulatedSender : public RawSender {
135 public:
136 SimulatedSender(SimulatedChannel *simulated_channel, EventLoop *event_loop)
Austin Schuh39788ff2019-12-01 18:22:57 -0800137 : RawSender(event_loop, simulated_channel->channel()),
Austin Schuh54cf95f2019-11-29 13:14:18 -0800138 simulated_channel_(simulated_channel),
139 event_loop_(event_loop) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 ~SimulatedSender() {}
141
142 void *data() override {
143 if (!message_) {
144 message_ = MakeSimulatedMessage(simulated_channel_->max_size());
145 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800146 return message_->data(simulated_channel_->max_size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700147 }
148
149 size_t size() override { return simulated_channel_->max_size(); }
150
Austin Schuhad154822019-12-27 15:45:13 -0800151 bool DoSend(size_t length,
152 aos::monotonic_clock::time_point monotonic_remote_time,
153 aos::realtime_clock::time_point realtime_remote_time,
154 uint32_t remote_queue_index) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155 CHECK_LE(length, size()) << ": Attempting to send too big a message.";
Austin Schuhad154822019-12-27 15:45:13 -0800156 message_->context.monotonic_event_time = event_loop_->monotonic_now();
157 message_->context.monotonic_remote_time = monotonic_remote_time;
158 message_->context.remote_queue_index = remote_queue_index;
159 message_->context.realtime_event_time = event_loop_->realtime_now();
160 message_->context.realtime_remote_time = realtime_remote_time;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 CHECK_LE(length, message_->context.size);
162 message_->context.size = length;
163
164 // TODO(austin): Track sending too fast.
Austin Schuhad154822019-12-27 15:45:13 -0800165 sent_queue_index_ = simulated_channel_->Send(message_);
166 monotonic_sent_time_ = event_loop_->monotonic_now();
167 realtime_sent_time_ = event_loop_->realtime_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168
169 // Drop the reference to the message so that we allocate a new message for
170 // next time. Otherwise we will continue to reuse the same memory for all
171 // messages and corrupt it.
172 message_.reset();
173 return true;
174 }
175
Austin Schuhad154822019-12-27 15:45:13 -0800176 bool DoSend(const void *msg, size_t size,
177 aos::monotonic_clock::time_point monotonic_remote_time,
178 aos::realtime_clock::time_point realtime_remote_time,
179 uint32_t remote_queue_index) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700180 CHECK_LE(size, this->size()) << ": Attempting to send too big a message.";
181
182 // This is wasteful, but since flatbuffers fill from the back end of the
183 // queue, we need it to be full sized.
184 message_ = MakeSimulatedMessage(simulated_channel_->max_size());
185
186 // Now fill in the message. size is already populated above, and
Austin Schuhac0771c2020-01-07 18:36:30 -0800187 // queue_index will be populated in simulated_channel_. Put this at the
188 // back of the data segment.
Brian Silvermana1652f32020-01-29 20:41:44 -0800189 memcpy(message_->data(simulated_channel_->max_size()) +
190 simulated_channel_->max_size() - size,
191 msg, size);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192
Austin Schuhac0771c2020-01-07 18:36:30 -0800193 return DoSend(size, monotonic_remote_time, realtime_remote_time,
194 remote_queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700195 }
196
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 private:
198 SimulatedChannel *simulated_channel_;
199 EventLoop *event_loop_;
200
201 std::shared_ptr<SimulatedMessage> message_;
202};
203} // namespace
204
205class SimulatedFetcher : public RawFetcher {
206 public:
Austin Schuhac0771c2020-01-07 18:36:30 -0800207 explicit SimulatedFetcher(EventLoop *event_loop,
208 SimulatedChannel *simulated_channel)
209 : RawFetcher(event_loop, simulated_channel->channel()),
210 simulated_channel_(simulated_channel) {}
211 ~SimulatedFetcher() { simulated_channel_->UnregisterFetcher(this); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700212
Austin Schuh39788ff2019-12-01 18:22:57 -0800213 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
214 if (msgs_.size() == 0) {
215 return std::make_pair(false, monotonic_clock::min_time);
216 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700217
218 SetMsg(msgs_.front());
219 msgs_.pop_front();
Austin Schuha5e14192020-01-06 18:02:41 -0800220 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700221 }
222
Austin Schuh39788ff2019-12-01 18:22:57 -0800223 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700224 if (msgs_.size() == 0) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800225 // TODO(austin): Can we just do this logic unconditionally? It is a lot
226 // simpler. And call clear, obviously.
Austin Schuhac0771c2020-01-07 18:36:30 -0800227 if (!msg_ && simulated_channel_->latest_message()) {
228 SetMsg(simulated_channel_->latest_message());
Austin Schuha5e14192020-01-06 18:02:41 -0800229 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700230 } else {
Austin Schuh39788ff2019-12-01 18:22:57 -0800231 return std::make_pair(false, monotonic_clock::min_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700232 }
233 }
234
235 // We've had a message enqueued, so we don't need to go looking for the
236 // latest message from before we started.
237 SetMsg(msgs_.back());
238 msgs_.clear();
Austin Schuha5e14192020-01-06 18:02:41 -0800239 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700240 }
241
242 private:
243 friend class SimulatedChannel;
244
245 // Updates the state inside RawFetcher to point to the data in msg_.
246 void SetMsg(std::shared_ptr<SimulatedMessage> msg) {
247 msg_ = msg;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 context_ = msg_->context;
Austin Schuhad154822019-12-27 15:45:13 -0800249 if (context_.remote_queue_index == 0xffffffffu) {
250 context_.remote_queue_index = context_.queue_index;
251 }
252 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
253 context_.monotonic_remote_time = context_.monotonic_event_time;
254 }
255 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
256 context_.realtime_remote_time = context_.realtime_event_time;
257 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700258 }
259
260 // Internal method for Simulation to add a message to the buffer.
261 void Enqueue(std::shared_ptr<SimulatedMessage> buffer) {
262 msgs_.emplace_back(buffer);
263 }
264
Austin Schuhac0771c2020-01-07 18:36:30 -0800265 SimulatedChannel *simulated_channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700266 std::shared_ptr<SimulatedMessage> msg_;
267
268 // Messages queued up but not in use.
269 ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_;
270};
271
272class SimulatedTimerHandler : public TimerHandler {
273 public:
274 explicit SimulatedTimerHandler(EventScheduler *scheduler,
Austin Schuhac0771c2020-01-07 18:36:30 -0800275 NodeEventLoopFactory *node_event_loop_factory,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800276 SimulatedEventLoop *simulated_event_loop,
Austin Schuh39788ff2019-12-01 18:22:57 -0800277 ::std::function<void()> fn);
Austin Schuh7d87b672019-12-01 20:23:49 -0800278 ~SimulatedTimerHandler() { Disable(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700279
280 void Setup(monotonic_clock::time_point base,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800281 monotonic_clock::duration repeat_offset) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700282
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800283 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284
Austin Schuh7d87b672019-12-01 20:23:49 -0800285 void Disable() override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286
Alex Perrycb7da4b2019-08-28 19:35:56 -0700287 private:
Austin Schuh7d87b672019-12-01 20:23:49 -0800288 SimulatedEventLoop *simulated_event_loop_;
289 EventHandler<SimulatedTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700290 EventScheduler *scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800291 NodeEventLoopFactory *node_event_loop_factory_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700292 EventScheduler::Token token_;
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800293
Alex Perrycb7da4b2019-08-28 19:35:56 -0700294 monotonic_clock::time_point base_;
295 monotonic_clock::duration repeat_offset_;
296};
297
298class SimulatedPhasedLoopHandler : public PhasedLoopHandler {
299 public:
300 SimulatedPhasedLoopHandler(EventScheduler *scheduler,
Austin Schuhac0771c2020-01-07 18:36:30 -0800301 NodeEventLoopFactory *node_event_loop_factory,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800302 SimulatedEventLoop *simulated_event_loop,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700303 ::std::function<void(int)> fn,
304 const monotonic_clock::duration interval,
Austin Schuh39788ff2019-12-01 18:22:57 -0800305 const monotonic_clock::duration offset);
Austin Schuh7d87b672019-12-01 20:23:49 -0800306 ~SimulatedPhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700307
Austin Schuh7d87b672019-12-01 20:23:49 -0800308 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700309
Austin Schuh7d87b672019-12-01 20:23:49 -0800310 void Schedule(monotonic_clock::time_point sleep_time) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700311
312 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800313 SimulatedEventLoop *simulated_event_loop_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800314 EventHandler<SimulatedPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700315
Austin Schuh39788ff2019-12-01 18:22:57 -0800316 EventScheduler *scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800317 NodeEventLoopFactory *node_event_loop_factory_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800318 EventScheduler::Token token_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700319};
320
321class SimulatedEventLoop : public EventLoop {
322 public:
323 explicit SimulatedEventLoop(
324 EventScheduler *scheduler,
Austin Schuhac0771c2020-01-07 18:36:30 -0800325 NodeEventLoopFactory *node_event_loop_factory,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700326 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>>
327 *channels,
328 const Configuration *configuration,
329 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
Austin Schuh39788ff2019-12-01 18:22:57 -0800330 *raw_event_loops,
Austin Schuh217a9782019-12-21 23:02:50 -0800331 const Node *node, pid_t tid)
Austin Schuh39788ff2019-12-01 18:22:57 -0800332 : EventLoop(CHECK_NOTNULL(configuration)),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700333 scheduler_(scheduler),
Austin Schuhac0771c2020-01-07 18:36:30 -0800334 node_event_loop_factory_(node_event_loop_factory),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700335 channels_(channels),
Austin Schuh39788ff2019-12-01 18:22:57 -0800336 raw_event_loops_(raw_event_loops),
Austin Schuh217a9782019-12-21 23:02:50 -0800337 node_(node),
Austin Schuh39788ff2019-12-01 18:22:57 -0800338 tid_(tid) {
339 raw_event_loops_->push_back(std::make_pair(this, [this](bool value) {
340 if (!has_setup_) {
341 Setup();
342 has_setup_ = true;
343 }
344 set_is_running(value);
345 }));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346 }
347 ~SimulatedEventLoop() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800348 // Trigger any remaining senders or fetchers to be cleared before destroying
349 // the event loop so the book keeping matches.
350 timing_report_sender_.reset();
351
352 // Force everything with a registered fd with epoll to be destroyed now.
353 timers_.clear();
354 phased_loops_.clear();
355 watchers_.clear();
356
Alex Perrycb7da4b2019-08-28 19:35:56 -0700357 for (auto it = raw_event_loops_->begin(); it != raw_event_loops_->end();
358 ++it) {
359 if (it->first == this) {
360 raw_event_loops_->erase(it);
361 break;
362 }
363 }
364 }
365
Austin Schuh7d87b672019-12-01 20:23:49 -0800366 std::chrono::nanoseconds send_delay() const { return send_delay_; }
367 void set_send_delay(std::chrono::nanoseconds send_delay) {
368 send_delay_ = send_delay;
369 }
370
Alex Perrycb7da4b2019-08-28 19:35:56 -0700371 ::aos::monotonic_clock::time_point monotonic_now() override {
Austin Schuhac0771c2020-01-07 18:36:30 -0800372 return node_event_loop_factory_->monotonic_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700373 }
374
375 ::aos::realtime_clock::time_point realtime_now() override {
Austin Schuhac0771c2020-01-07 18:36:30 -0800376 return node_event_loop_factory_->realtime_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700377 }
378
379 ::std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
380
381 ::std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
382
383 void MakeRawWatcher(
384 const Channel *channel,
385 ::std::function<void(const Context &context, const void *message)>
386 watcher) override;
387
388 TimerHandler *AddTimer(::std::function<void()> callback) override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800389 CHECK(!is_running());
Austin Schuhac0771c2020-01-07 18:36:30 -0800390 return NewTimer(::std::unique_ptr<TimerHandler>(new SimulatedTimerHandler(
391 scheduler_, node_event_loop_factory_, this, callback)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700392 }
393
394 PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback,
395 const monotonic_clock::duration interval,
396 const monotonic_clock::duration offset =
397 ::std::chrono::seconds(0)) override {
Austin Schuhac0771c2020-01-07 18:36:30 -0800398 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
399 new SimulatedPhasedLoopHandler(scheduler_, node_event_loop_factory_,
400 this, callback, interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700401 }
402
403 void OnRun(::std::function<void()> on_run) override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800404 scheduler_->ScheduleOnRun(on_run);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700405 }
406
Austin Schuh217a9782019-12-21 23:02:50 -0800407 const Node *node() const override { return node_; }
408
James Kuszmaul3ae42262019-11-08 12:33:41 -0800409 void set_name(const std::string_view name) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700410 name_ = std::string(name);
411 }
James Kuszmaul3ae42262019-11-08 12:33:41 -0800412 const std::string_view name() const override { return name_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700413
414 SimulatedChannel *GetSimulatedChannel(const Channel *channel);
415
Austin Schuh39788ff2019-12-01 18:22:57 -0800416 void SetRuntimeRealtimePriority(int priority) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 CHECK(!is_running()) << ": Cannot set realtime priority while running.";
Austin Schuh39788ff2019-12-01 18:22:57 -0800418 priority_ = priority;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700419 }
420
Austin Schuh39788ff2019-12-01 18:22:57 -0800421 int priority() const override { return priority_; }
422
Tyler Chatow67ddb032020-01-12 14:30:04 -0800423 void Setup() {
424 MaybeScheduleTimingReports();
425 if (!skip_logger_) {
426 logging::ScopedLogRestorer prev_logger;
427 log_sender_.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
428 log_impl_ = logging::GetImplementation();
429 }
430 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800431
Alex Perrycb7da4b2019-08-28 19:35:56 -0700432 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800433 friend class SimulatedTimerHandler;
Austin Schuh7d87b672019-12-01 20:23:49 -0800434 friend class SimulatedPhasedLoopHandler;
435 friend class SimulatedWatcher;
436
437 void HandleEvent() {
438 while (true) {
439 if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) {
440 break;
441 }
442
443 EventLoopEvent *event = PopEvent();
444 event->HandleEvent();
445 }
446 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800447
Austin Schuh39788ff2019-12-01 18:22:57 -0800448 pid_t GetTid() override { return tid_; }
449
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450 EventScheduler *scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800451 NodeEventLoopFactory *node_event_loop_factory_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700452 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_;
453 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
454 *raw_event_loops_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700455
456 ::std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800457
458 int priority_ = 0;
459
460 bool has_setup_ = false;
461
Austin Schuh7d87b672019-12-01 20:23:49 -0800462 std::chrono::nanoseconds send_delay_;
463
Austin Schuh217a9782019-12-21 23:02:50 -0800464 const Node *const node_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800465 const pid_t tid_;
Tyler Chatow67ddb032020-01-12 14:30:04 -0800466
467 AosLogToFbs log_sender_;
468 logging::LogImplementation *log_impl_ = nullptr;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700469};
470
Austin Schuh7d87b672019-12-01 20:23:49 -0800471void SimulatedEventLoopFactory::set_send_delay(
472 std::chrono::nanoseconds send_delay) {
473 send_delay_ = send_delay;
474 for (std::pair<EventLoop *, std::function<void(bool)>> &loop :
475 raw_event_loops_) {
476 reinterpret_cast<SimulatedEventLoop *>(loop.first)
477 ->set_send_delay(send_delay_);
478 }
479}
480
Alex Perrycb7da4b2019-08-28 19:35:56 -0700481void SimulatedEventLoop::MakeRawWatcher(
482 const Channel *channel,
483 std::function<void(const Context &channel, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800484 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800485
Austin Schuhac0771c2020-01-07 18:36:30 -0800486 std::unique_ptr<SimulatedWatcher> shm_watcher(new SimulatedWatcher(
487 this, scheduler_, node_event_loop_factory_, channel, std::move(watcher)));
Austin Schuh39788ff2019-12-01 18:22:57 -0800488
489 GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get());
490 NewWatcher(std::move(shm_watcher));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700491}
492
493std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender(
494 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800495 TakeSender(channel);
496
Alex Perrycb7da4b2019-08-28 19:35:56 -0700497 return GetSimulatedChannel(channel)->MakeRawSender(this);
498}
499
500std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher(
501 const Channel *channel) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800502 ChannelIndex(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800503
Austin Schuhca4828c2019-12-28 14:21:35 -0800504 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
505 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
506 << "\", \"type\": \"" << channel->type()->string_view()
507 << "\" } is not able to be fetched on this node. Check your "
508 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800509 }
510
Austin Schuh39788ff2019-12-01 18:22:57 -0800511 return GetSimulatedChannel(channel)->MakeRawFetcher(this);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700512}
513
514SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel(
515 const Channel *channel) {
516 auto it = channels_->find(SimpleChannel(channel));
517 if (it == channels_->end()) {
518 it = channels_
519 ->emplace(SimpleChannel(channel),
520 std::unique_ptr<SimulatedChannel>(
521 new SimulatedChannel(channel, scheduler_)))
522 .first;
523 }
524 return it->second.get();
525}
526
Austin Schuh7d87b672019-12-01 20:23:49 -0800527SimulatedWatcher::SimulatedWatcher(
528 SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler,
Austin Schuhac0771c2020-01-07 18:36:30 -0800529 NodeEventLoopFactory *node_event_loop_factory, const Channel *channel,
Austin Schuh7d87b672019-12-01 20:23:49 -0800530 std::function<void(const Context &context, const void *message)> fn)
531 : WatcherState(simulated_event_loop, channel, std::move(fn)),
532 simulated_event_loop_(simulated_event_loop),
533 event_(this),
534 scheduler_(scheduler),
Austin Schuhac0771c2020-01-07 18:36:30 -0800535 node_event_loop_factory_(node_event_loop_factory),
Austin Schuh7d87b672019-12-01 20:23:49 -0800536 token_(scheduler_->InvalidToken()) {}
537
538SimulatedWatcher::~SimulatedWatcher() {
539 simulated_event_loop_->RemoveEvent(&event_);
540 if (token_ != scheduler_->InvalidToken()) {
541 scheduler_->Deschedule(token_);
542 }
543 simulated_channel_->RemoveWatcher(this);
544}
545
546void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) {
Austin Schuha5e14192020-01-06 18:02:41 -0800547 monotonic_clock::time_point event_time =
548 simulated_event_loop_->monotonic_now();
Austin Schuh7d87b672019-12-01 20:23:49 -0800549
550 // Messages are queued in order. If we are the first, add ourselves.
551 // Otherwise, don't.
552 if (msgs_.size() == 0) {
Austin Schuhad154822019-12-27 15:45:13 -0800553 event_.set_event_time(message->context.monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800554 simulated_event_loop_->AddEvent(&event_);
555
556 DoSchedule(event_time);
557 }
558
559 msgs_.emplace_back(message);
560}
561
562void SimulatedWatcher::HandleEvent() {
563 CHECK_NE(msgs_.size(), 0u) << ": No events to handle.";
564
565 const monotonic_clock::time_point monotonic_now =
566 simulated_event_loop_->monotonic_now();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800567 logging::ScopedLogRestorer prev_logger;
568 if (simulated_event_loop_->log_impl_ != nullptr) {
569 logging::SetImplementation(simulated_event_loop_->log_impl_);
570 }
Austin Schuhad154822019-12-27 15:45:13 -0800571 Context context = msgs_.front()->context;
572
573 if (context.remote_queue_index == 0xffffffffu) {
574 context.remote_queue_index = context.queue_index;
575 }
576 if (context.monotonic_remote_time == aos::monotonic_clock::min_time) {
577 context.monotonic_remote_time = context.monotonic_event_time;
578 }
579 if (context.realtime_remote_time == aos::realtime_clock::min_time) {
580 context.realtime_remote_time = context.realtime_event_time;
581 }
582
583 DoCallCallback([monotonic_now]() { return monotonic_now; }, context);
Austin Schuh7d87b672019-12-01 20:23:49 -0800584
585 msgs_.pop_front();
586 if (msgs_.size() != 0) {
Austin Schuhad154822019-12-27 15:45:13 -0800587 event_.set_event_time(msgs_.front()->context.monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800588 simulated_event_loop_->AddEvent(&event_);
589
590 DoSchedule(event_.event_time());
591 } else {
592 token_ = scheduler_->InvalidToken();
593 }
594}
595
596void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) {
Austin Schuhac0771c2020-01-07 18:36:30 -0800597 token_ = scheduler_->Schedule(
598 node_event_loop_factory_->ToDistributedClock(
599 event_time + simulated_event_loop_->send_delay()),
600 [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800601}
602
603void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800604 watcher->SetSimulatedChannel(this);
605 watchers_.emplace_back(watcher);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700606}
607
608::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender(
609 EventLoop *event_loop) {
610 return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop));
611}
612
Austin Schuh39788ff2019-12-01 18:22:57 -0800613::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher(
614 EventLoop *event_loop) {
615 ::std::unique_ptr<SimulatedFetcher> fetcher(
616 new SimulatedFetcher(event_loop, this));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700617 fetchers_.push_back(fetcher.get());
618 return ::std::move(fetcher);
619}
620
Austin Schuhad154822019-12-27 15:45:13 -0800621uint32_t SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) {
622 const uint32_t queue_index = next_queue_index_.index();
623 message->context.queue_index = queue_index;
Brian Silvermana1652f32020-01-29 20:41:44 -0800624 message->context.data = message->data(channel()->max_size()) +
625 channel()->max_size() - message->context.size;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700626 next_queue_index_ = next_queue_index_.Increment();
627
628 latest_message_ = message;
629 if (scheduler_->is_running()) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800630 for (SimulatedWatcher *watcher : watchers_) {
631 watcher->Schedule(message);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700632 }
633 }
634 for (auto &fetcher : fetchers_) {
635 fetcher->Enqueue(message);
636 }
Austin Schuhad154822019-12-27 15:45:13 -0800637
638 return queue_index;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700639}
640
641void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) {
642 fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher));
643}
644
Austin Schuh39788ff2019-12-01 18:22:57 -0800645SimulatedTimerHandler::SimulatedTimerHandler(
Austin Schuhac0771c2020-01-07 18:36:30 -0800646 EventScheduler *scheduler, NodeEventLoopFactory *node_event_loop_factory,
647 SimulatedEventLoop *simulated_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800648 : TimerHandler(simulated_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800649 simulated_event_loop_(simulated_event_loop),
650 event_(this),
Austin Schuh39788ff2019-12-01 18:22:57 -0800651 scheduler_(scheduler),
Austin Schuhac0771c2020-01-07 18:36:30 -0800652 node_event_loop_factory_(node_event_loop_factory),
Austin Schuh39788ff2019-12-01 18:22:57 -0800653 token_(scheduler_->InvalidToken()) {}
654
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800655void SimulatedTimerHandler::Setup(monotonic_clock::time_point base,
656 monotonic_clock::duration repeat_offset) {
657 Disable();
658 const ::aos::monotonic_clock::time_point monotonic_now =
Austin Schuha5e14192020-01-06 18:02:41 -0800659 simulated_event_loop_->monotonic_now();
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800660 base_ = base;
661 repeat_offset_ = repeat_offset;
662 if (base < monotonic_now) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800663 token_ = scheduler_->Schedule(
Austin Schuhac0771c2020-01-07 18:36:30 -0800664 node_event_loop_factory_->ToDistributedClock(monotonic_now),
665 [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800666 } else {
Austin Schuh7d87b672019-12-01 20:23:49 -0800667 token_ = scheduler_->Schedule(
Austin Schuhac0771c2020-01-07 18:36:30 -0800668 node_event_loop_factory_->ToDistributedClock(base),
669 [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800670 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800671 event_.set_event_time(base_);
672 simulated_event_loop_->AddEvent(&event_);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800673}
674
675void SimulatedTimerHandler::HandleEvent() {
676 const ::aos::monotonic_clock::time_point monotonic_now =
Austin Schuha5e14192020-01-06 18:02:41 -0800677 simulated_event_loop_->monotonic_now();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800678 logging::ScopedLogRestorer prev_logger;
679 if (simulated_event_loop_->log_impl_ != nullptr) {
680 logging::SetImplementation(simulated_event_loop_->log_impl_);
681 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800682 if (repeat_offset_ != ::aos::monotonic_clock::zero()) {
683 // Reschedule.
684 while (base_ <= monotonic_now) base_ += repeat_offset_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800685 token_ = scheduler_->Schedule(
Austin Schuhac0771c2020-01-07 18:36:30 -0800686 node_event_loop_factory_->ToDistributedClock(base_),
687 [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800688 event_.set_event_time(base_);
689 simulated_event_loop_->AddEvent(&event_);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800690 } else {
691 token_ = scheduler_->InvalidToken();
692 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800693
Austin Schuh39788ff2019-12-01 18:22:57 -0800694 Call([monotonic_now]() { return monotonic_now; }, monotonic_now);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800695}
696
Austin Schuh7d87b672019-12-01 20:23:49 -0800697void SimulatedTimerHandler::Disable() {
698 simulated_event_loop_->RemoveEvent(&event_);
699 if (token_ != scheduler_->InvalidToken()) {
700 scheduler_->Deschedule(token_);
701 token_ = scheduler_->InvalidToken();
702 }
703}
704
Austin Schuh39788ff2019-12-01 18:22:57 -0800705SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler(
Austin Schuhac0771c2020-01-07 18:36:30 -0800706 EventScheduler *scheduler, NodeEventLoopFactory *node_event_loop_factory,
707 SimulatedEventLoop *simulated_event_loop, ::std::function<void(int)> fn,
708 const monotonic_clock::duration interval,
Austin Schuh39788ff2019-12-01 18:22:57 -0800709 const monotonic_clock::duration offset)
710 : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset),
711 simulated_event_loop_(simulated_event_loop),
Austin Schuh7d87b672019-12-01 20:23:49 -0800712 event_(this),
Austin Schuh39788ff2019-12-01 18:22:57 -0800713 scheduler_(scheduler),
Austin Schuhac0771c2020-01-07 18:36:30 -0800714 node_event_loop_factory_(node_event_loop_factory),
Austin Schuh39788ff2019-12-01 18:22:57 -0800715 token_(scheduler_->InvalidToken()) {}
716
Austin Schuh7d87b672019-12-01 20:23:49 -0800717SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() {
718 if (token_ != scheduler_->InvalidToken()) {
719 scheduler_->Deschedule(token_);
720 token_ = scheduler_->InvalidToken();
721 }
722 simulated_event_loop_->RemoveEvent(&event_);
723}
724
725void SimulatedPhasedLoopHandler::HandleEvent() {
Austin Schuh39788ff2019-12-01 18:22:57 -0800726 monotonic_clock::time_point monotonic_now =
727 simulated_event_loop_->monotonic_now();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800728 logging::ScopedLogRestorer prev_logger;
729 if (simulated_event_loop_->log_impl_ != nullptr) {
730 logging::SetImplementation(simulated_event_loop_->log_impl_);
731 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800732 Call(
733 [monotonic_now]() { return monotonic_now; },
734 [this](monotonic_clock::time_point sleep_time) { Schedule(sleep_time); });
735}
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800736
Austin Schuh7d87b672019-12-01 20:23:49 -0800737void SimulatedPhasedLoopHandler::Schedule(
738 monotonic_clock::time_point sleep_time) {
739 token_ = scheduler_->Schedule(
Austin Schuhac0771c2020-01-07 18:36:30 -0800740 node_event_loop_factory_->ToDistributedClock(sleep_time),
741 [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800742 event_.set_event_time(sleep_time);
743 simulated_event_loop_->AddEvent(&event_);
744}
745
Austin Schuhac0771c2020-01-07 18:36:30 -0800746NodeEventLoopFactory::NodeEventLoopFactory(
747 EventScheduler *scheduler, SimulatedEventLoopFactory *factory,
748 const Node *node,
749 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
750 *raw_event_loops)
751 : scheduler_(scheduler),
752 factory_(factory),
753 node_(node),
754 raw_event_loops_(raw_event_loops) {}
755
Alex Perrycb7da4b2019-08-28 19:35:56 -0700756SimulatedEventLoopFactory::SimulatedEventLoopFactory(
757 const Configuration *configuration)
Austin Schuhac0771c2020-01-07 18:36:30 -0800758 : configuration_(CHECK_NOTNULL(configuration)) {
759 if (configuration::MultiNode(configuration_)) {
760 for (const Node *node : *configuration->nodes()) {
761 nodes_.emplace_back(node);
Austin Schuh15649d62019-12-28 16:36:38 -0800762 }
Austin Schuhac0771c2020-01-07 18:36:30 -0800763 } else {
764 nodes_.emplace_back(nullptr);
765 }
766
767 for (const Node *node : nodes_) {
768 node_factories_.emplace_back(
769 new NodeEventLoopFactory(&scheduler_, this, node, &raw_event_loops_));
Austin Schuh15649d62019-12-28 16:36:38 -0800770 }
Austin Schuh898f4972020-01-11 17:21:25 -0800771
772 if (configuration::MultiNode(configuration)) {
773 bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this);
774 }
Austin Schuh15649d62019-12-28 16:36:38 -0800775}
776
Alex Perrycb7da4b2019-08-28 19:35:56 -0700777SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {}
778
Austin Schuhac0771c2020-01-07 18:36:30 -0800779NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory(
780 const Node *node) {
781 auto result = std::find_if(
782 node_factories_.begin(), node_factories_.end(),
783 [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) {
784 return node_factory->node() == node;
785 });
786
787 CHECK(result != node_factories_.end())
788 << ": Failed to find node " << FlatbufferToJson(node);
789
790 return result->get();
791}
792
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800793::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop(
Austin Schuhac0771c2020-01-07 18:36:30 -0800794 std::string_view name, const Node *node) {
795 if (node == nullptr) {
796 CHECK(!configuration::MultiNode(configuration()))
797 << ": Can't make a single node event loop in a multi-node world.";
798 } else {
799 CHECK(configuration::MultiNode(configuration()))
800 << ": Can't make a multi-node event loop in a single-node world.";
801 }
802 return GetNodeEventLoopFactory(node)->MakeEventLoop(name);
803}
804
805::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop(
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800806 std::string_view name) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800807 pid_t tid = tid_;
808 ++tid_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800809 ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop(
Austin Schuhac0771c2020-01-07 18:36:30 -0800810 scheduler_, this, &channels_, factory_->configuration(), raw_event_loops_,
811 node_, tid));
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800812 result->set_name(name);
Austin Schuhac0771c2020-01-07 18:36:30 -0800813 result->set_send_delay(factory_->send_delay());
Austin Schuh7d87b672019-12-01 20:23:49 -0800814 return std::move(result);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700815}
816
817void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) {
818 for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop :
819 raw_event_loops_) {
820 event_loop.second(true);
821 }
822 scheduler_.RunFor(duration);
Austin Schuh39788ff2019-12-01 18:22:57 -0800823 for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop :
824 raw_event_loops_) {
825 event_loop.second(false);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700826 }
827}
828
829void SimulatedEventLoopFactory::Run() {
830 for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop :
831 raw_event_loops_) {
832 event_loop.second(true);
833 }
834 scheduler_.Run();
Austin Schuh39788ff2019-12-01 18:22:57 -0800835 for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop :
836 raw_event_loops_) {
837 event_loop.second(false);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700838 }
839}
840
841} // namespace aos