blob: 644d998f4f7f36ad1658e6b237590f058da9262e [file] [log] [blame]
Austin Schuh54cf95f2019-11-29 13:14:18 -08001#include "aos/events/event_loop.h"
2
3#include "aos/configuration.h"
4#include "aos/configuration_generated.h"
Tyler Chatow67ddb032020-01-12 14:30:04 -08005#include "aos/logging/implementations.h"
Austin Schuh54cf95f2019-11-29 13:14:18 -08006#include "glog/logging.h"
7
Austin Schuh39788ff2019-12-01 18:22:57 -08008DEFINE_bool(timing_reports, true, "Publish timing reports.");
9DEFINE_int32(timing_report_ms, 1000,
10 "Period in milliseconds to publish timing reports at.");
11
Austin Schuh54cf95f2019-11-29 13:14:18 -080012namespace aos {
Austin Schuhd54780b2020-10-03 16:26:02 -070013namespace {
14void CheckAlignment(const Channel *channel) {
15 if (channel->max_size() % alignof(flatbuffers::largest_scalar_t) != 0) {
16 LOG(FATAL) << "max_size() (" << channel->max_size()
17 << ") is not a multiple of alignment ("
18 << alignof(flatbuffers::largest_scalar_t) << ") for channel "
19 << configuration::CleanedChannelToString(channel) << ".";
20 }
21}
22} // namespace
Austin Schuh54cf95f2019-11-29 13:14:18 -080023
Austin Schuh39788ff2019-12-01 18:22:57 -080024RawSender::RawSender(EventLoop *event_loop, const Channel *channel)
25 : event_loop_(event_loop),
26 channel_(channel),
Brian Silverman79ec7fc2020-06-08 20:11:22 -050027 ftrace_prefix_(configuration::StrippedChannelToString(channel)),
Austin Schuh39788ff2019-12-01 18:22:57 -080028 timing_(event_loop_->ChannelIndex(channel)) {
29 event_loop_->NewSender(this);
30}
31
32RawSender::~RawSender() { event_loop_->DeleteSender(this); }
33
Tyler Chatowb7c6eba2021-07-28 14:43:23 -070034bool RawSender::DoSend(const SharedSpan data,
35 monotonic_clock::time_point monotonic_remote_time,
36 realtime_clock::time_point realtime_remote_time,
37 uint32_t remote_queue_index,
38 const UUID &source_boot_uuid) {
39 return DoSend(data->data(), data->size(), monotonic_remote_time,
40 realtime_remote_time, remote_queue_index, source_boot_uuid);
41}
42
Austin Schuh39788ff2019-12-01 18:22:57 -080043RawFetcher::RawFetcher(EventLoop *event_loop, const Channel *channel)
44 : event_loop_(event_loop),
45 channel_(channel),
Brian Silverman79ec7fc2020-06-08 20:11:22 -050046 ftrace_prefix_(configuration::StrippedChannelToString(channel)),
Austin Schuh39788ff2019-12-01 18:22:57 -080047 timing_(event_loop_->ChannelIndex(channel)) {
Austin Schuhad154822019-12-27 15:45:13 -080048 context_.monotonic_event_time = monotonic_clock::min_time;
49 context_.monotonic_remote_time = monotonic_clock::min_time;
50 context_.realtime_event_time = realtime_clock::min_time;
51 context_.realtime_remote_time = realtime_clock::min_time;
Austin Schuh39788ff2019-12-01 18:22:57 -080052 context_.queue_index = 0xffffffff;
53 context_.size = 0;
54 context_.data = nullptr;
Brian Silverman4f4e0612020-08-12 19:54:41 -070055 context_.buffer_index = -1;
Austin Schuh39788ff2019-12-01 18:22:57 -080056 event_loop_->NewFetcher(this);
57}
58
59RawFetcher::~RawFetcher() { event_loop_->DeleteFetcher(this); }
60
61TimerHandler::TimerHandler(EventLoop *event_loop, std::function<void()> fn)
62 : event_loop_(event_loop), fn_(std::move(fn)) {}
63
64TimerHandler::~TimerHandler() {}
65
66PhasedLoopHandler::PhasedLoopHandler(EventLoop *event_loop,
67 std::function<void(int)> fn,
68 const monotonic_clock::duration interval,
69 const monotonic_clock::duration offset)
70 : event_loop_(event_loop),
71 fn_(std::move(fn)),
72 phased_loop_(interval, event_loop_->monotonic_now(), offset) {
73 event_loop_->OnRun([this]() {
74 const monotonic_clock::time_point monotonic_now =
75 event_loop_->monotonic_now();
76 phased_loop_.Reset(monotonic_now);
77 Reschedule(
78 [this](monotonic_clock::time_point sleep_time) {
79 Schedule(sleep_time);
80 },
81 monotonic_now);
Milind Upadhyay42589bb2021-05-19 20:05:16 -070082 // Reschedule here will count cycles elapsed before now, and then the
83 // reschedule before running the handler will count the time that elapsed
84 // then. So clear the count here.
Austin Schuh39788ff2019-12-01 18:22:57 -080085 cycles_elapsed_ = 0;
86 });
87}
88
89PhasedLoopHandler::~PhasedLoopHandler() {}
90
Austin Schuh83c7f702021-01-19 22:36:29 -080091EventLoop::EventLoop(const Configuration *configuration)
92 : timing_report_(flatbuffers::DetachedBuffer()),
Austin Schuh56196432020-10-24 20:15:21 -070093 configuration_(configuration) {}
Tyler Chatow67ddb032020-01-12 14:30:04 -080094
Austin Schuh39788ff2019-12-01 18:22:57 -080095EventLoop::~EventLoop() {
Austin Schuh58646e22021-08-23 23:51:46 -070096 if(!senders_.empty()) {
97 for (const RawSender *sender : senders_) {
98 LOG(ERROR) << " Sender "
99 << configuration::StrippedChannelToString(sender->channel())
100 << " still open";
101 }
102 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800103 CHECK_EQ(senders_.size(), 0u) << ": Not all senders destroyed";
Austin Schuh7d87b672019-12-01 20:23:49 -0800104 CHECK_EQ(events_.size(), 0u) << ": Not all events unregistered";
Austin Schuh39788ff2019-12-01 18:22:57 -0800105}
106
107int EventLoop::ChannelIndex(const Channel *channel) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800108 return configuration::ChannelIndex(configuration_, channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800109}
110
Brian Silverman5120afb2020-01-31 17:44:35 -0800111WatcherState *EventLoop::GetWatcherState(const Channel *channel) {
112 const int channel_index = ChannelIndex(channel);
113 for (const std::unique_ptr<WatcherState> &watcher : watchers_) {
114 if (watcher->channel_index() == channel_index) {
115 return watcher.get();
116 }
117 }
118 LOG(FATAL) << "No watcher found for channel";
119}
120
Austin Schuh39788ff2019-12-01 18:22:57 -0800121void EventLoop::NewSender(RawSender *sender) {
122 senders_.emplace_back(sender);
123 UpdateTimingReport();
124}
125void EventLoop::DeleteSender(RawSender *sender) {
126 CHECK(!is_running());
127 auto s = std::find(senders_.begin(), senders_.end(), sender);
128 CHECK(s != senders_.end()) << ": Sender not in senders list";
129 senders_.erase(s);
130 UpdateTimingReport();
131}
132
133TimerHandler *EventLoop::NewTimer(std::unique_ptr<TimerHandler> timer) {
134 timers_.emplace_back(std::move(timer));
135 UpdateTimingReport();
136 return timers_.back().get();
137}
138
139PhasedLoopHandler *EventLoop::NewPhasedLoop(
140 std::unique_ptr<PhasedLoopHandler> phased_loop) {
141 phased_loops_.emplace_back(std::move(phased_loop));
142 UpdateTimingReport();
143 return phased_loops_.back().get();
144}
145
146void EventLoop::NewFetcher(RawFetcher *fetcher) {
Austin Schuhd54780b2020-10-03 16:26:02 -0700147 CheckAlignment(fetcher->channel());
148
Austin Schuh39788ff2019-12-01 18:22:57 -0800149 fetchers_.emplace_back(fetcher);
150 UpdateTimingReport();
151}
152
153void EventLoop::DeleteFetcher(RawFetcher *fetcher) {
154 CHECK(!is_running());
155 auto f = std::find(fetchers_.begin(), fetchers_.end(), fetcher);
156 CHECK(f != fetchers_.end()) << ": Fetcher not in fetchers list";
157 fetchers_.erase(f);
158 UpdateTimingReport();
159}
160
161WatcherState *EventLoop::NewWatcher(std::unique_ptr<WatcherState> watcher) {
162 watchers_.emplace_back(std::move(watcher));
163
164 UpdateTimingReport();
165
166 return watchers_.back().get();
167}
168
Brian Silverman0fc69932020-01-24 21:54:02 -0800169void EventLoop::TakeWatcher(const Channel *channel) {
170 CHECK(!is_running()) << ": Cannot add new objects while running.";
171 ChannelIndex(channel);
172
Austin Schuhd54780b2020-10-03 16:26:02 -0700173 CheckAlignment(channel);
174
Brian Silverman0fc69932020-01-24 21:54:02 -0800175 CHECK(taken_senders_.find(channel) == taken_senders_.end())
Austin Schuh8072f922020-02-16 21:51:47 -0800176 << ": " << configuration::CleanedChannelToString(channel)
177 << " is already being used.";
Brian Silverman0fc69932020-01-24 21:54:02 -0800178
179 auto result = taken_watchers_.insert(channel);
Austin Schuh8072f922020-02-16 21:51:47 -0800180 CHECK(result.second) << ": " << configuration::CleanedChannelToString(channel)
Brian Silverman0fc69932020-01-24 21:54:02 -0800181 << " is already being used.";
182
183 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
Austin Schuh8072f922020-02-16 21:51:47 -0800184 LOG(FATAL) << ": " << configuration::CleanedChannelToString(channel)
Brian Silverman0fc69932020-01-24 21:54:02 -0800185 << " is not able to be watched on this node. Check your "
186 "configuration.";
187 }
188}
189
190void EventLoop::TakeSender(const Channel *channel) {
191 CHECK(!is_running()) << ": Cannot add new objects while running.";
192 ChannelIndex(channel);
193
Austin Schuhd54780b2020-10-03 16:26:02 -0700194 CheckAlignment(channel);
195
Brian Silverman0fc69932020-01-24 21:54:02 -0800196 CHECK(taken_watchers_.find(channel) == taken_watchers_.end())
Austin Schuh8072f922020-02-16 21:51:47 -0800197 << ": Channel " << configuration::CleanedChannelToString(channel)
198 << " is already being used.";
Brian Silverman0fc69932020-01-24 21:54:02 -0800199
200 // We don't care if this is a duplicate.
201 taken_senders_.insert(channel);
202}
203
Austin Schuh39788ff2019-12-01 18:22:57 -0800204void EventLoop::SendTimingReport() {
Brian Silvermance418d02021-11-03 11:25:52 -0700205 if (!timing_report_sender_) {
206 // Timing reports are disabled, so nothing for us to do.
207 return;
208 }
209
Austin Schuh39788ff2019-12-01 18:22:57 -0800210 // We need to do a fancy dance here to get all the accounting to work right.
211 // We want to copy the memory here, but then send after resetting. Otherwise
212 // the send for the timing report won't be counted in the timing report.
213 //
214 // Also, flatbuffers build from the back end. So place this at the back end
215 // of the buffer. We only have to care because we are using this in a very
216 // raw fashion.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800217 CHECK_LE(timing_report_.span().size(), timing_report_sender_->size())
Austin Schuh39788ff2019-12-01 18:22:57 -0800218 << ": Timing report bigger than the sender size.";
Austin Schuhadd6eb32020-11-09 21:24:26 -0800219 std::copy(timing_report_.span().data(),
220 timing_report_.span().data() + timing_report_.span().size(),
Austin Schuh39788ff2019-12-01 18:22:57 -0800221 reinterpret_cast<uint8_t *>(timing_report_sender_->data()) +
Austin Schuhadd6eb32020-11-09 21:24:26 -0800222 timing_report_sender_->size() - timing_report_.span().size());
Austin Schuh39788ff2019-12-01 18:22:57 -0800223
224 for (const std::unique_ptr<TimerHandler> &timer : timers_) {
225 timer->timing_.ResetTimingReport();
226 }
227 for (const std::unique_ptr<WatcherState> &watcher : watchers_) {
228 watcher->ResetReport();
229 }
230 for (const std::unique_ptr<PhasedLoopHandler> &phased_loop : phased_loops_) {
231 phased_loop->timing_.ResetTimingReport();
232 }
233 for (RawSender *sender : senders_) {
234 sender->timing_.ResetTimingReport();
235 }
236 for (RawFetcher *fetcher : fetchers_) {
237 fetcher->timing_.ResetTimingReport();
238 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800239 timing_report_sender_->Send(timing_report_.span().size());
Austin Schuh39788ff2019-12-01 18:22:57 -0800240}
241
242void EventLoop::UpdateTimingReport() {
243 // We need to support senders and fetchers changing while we are setting up
244 // the event loop. Otherwise we can't fetch or send until the loop runs. This
245 // means that on each change, we need to redo all this work. This makes setup
246 // more expensive, but not by all that much on a modern processor.
247
248 // Now, build up a report with everything pre-filled out.
249 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800250 fbb.ForceDefaults(true);
Austin Schuh39788ff2019-12-01 18:22:57 -0800251
252 // Pre-fill in the defaults for timers.
253 std::vector<flatbuffers::Offset<timing::Timer>> timer_offsets;
254 for (const std::unique_ptr<TimerHandler> &timer : timers_) {
255 flatbuffers::Offset<timing::Statistic> wakeup_latency_offset =
256 timing::CreateStatistic(fbb);
257 flatbuffers::Offset<timing::Statistic> handler_time_offset =
258 timing::CreateStatistic(fbb);
259 flatbuffers::Offset<flatbuffers::String> name_offset;
260 if (timer->name().size() != 0) {
261 name_offset = fbb.CreateString(timer->name());
262 }
263
264 timing::Timer::Builder timer_builder(fbb);
265
266 if (timer->name().size() != 0) {
267 timer_builder.add_name(name_offset);
268 }
269 timer_builder.add_wakeup_latency(wakeup_latency_offset);
270 timer_builder.add_handler_time(handler_time_offset);
271 timer_builder.add_count(0);
272 timer_offsets.emplace_back(timer_builder.Finish());
273 }
274
275 // Pre-fill in the defaults for phased_loops.
276 std::vector<flatbuffers::Offset<timing::Timer>> phased_loop_offsets;
277 for (const std::unique_ptr<PhasedLoopHandler> &phased_loop : phased_loops_) {
278 flatbuffers::Offset<timing::Statistic> wakeup_latency_offset =
279 timing::CreateStatistic(fbb);
280 flatbuffers::Offset<timing::Statistic> handler_time_offset =
281 timing::CreateStatistic(fbb);
282 flatbuffers::Offset<flatbuffers::String> name_offset;
283 if (phased_loop->name().size() != 0) {
284 name_offset = fbb.CreateString(phased_loop->name());
285 }
286
287 timing::Timer::Builder timer_builder(fbb);
288
289 if (phased_loop->name().size() != 0) {
290 timer_builder.add_name(name_offset);
291 }
292 timer_builder.add_wakeup_latency(wakeup_latency_offset);
293 timer_builder.add_handler_time(handler_time_offset);
294 timer_builder.add_count(0);
295 phased_loop_offsets.emplace_back(timer_builder.Finish());
296 }
297
298 // Pre-fill in the defaults for watchers.
299 std::vector<flatbuffers::Offset<timing::Watcher>> watcher_offsets;
300 for (const std::unique_ptr<WatcherState> &watcher : watchers_) {
301 flatbuffers::Offset<timing::Statistic> wakeup_latency_offset =
302 timing::CreateStatistic(fbb);
303 flatbuffers::Offset<timing::Statistic> handler_time_offset =
304 timing::CreateStatistic(fbb);
305
306 timing::Watcher::Builder watcher_builder(fbb);
307
308 watcher_builder.add_channel_index(watcher->channel_index());
309 watcher_builder.add_wakeup_latency(wakeup_latency_offset);
310 watcher_builder.add_handler_time(handler_time_offset);
311 watcher_builder.add_count(0);
312 watcher_offsets.emplace_back(watcher_builder.Finish());
313 }
314
315 // Pre-fill in the defaults for senders.
316 std::vector<flatbuffers::Offset<timing::Sender>> sender_offsets;
317 for (const RawSender *sender : senders_) {
318 flatbuffers::Offset<timing::Statistic> size_offset =
319 timing::CreateStatistic(fbb);
320
321 timing::Sender::Builder sender_builder(fbb);
322
323 sender_builder.add_channel_index(sender->timing_.channel_index);
324 sender_builder.add_size(size_offset);
325 sender_builder.add_count(0);
326 sender_offsets.emplace_back(sender_builder.Finish());
327 }
328
329 // Pre-fill in the defaults for fetchers.
330 std::vector<flatbuffers::Offset<timing::Fetcher>> fetcher_offsets;
331 for (RawFetcher *fetcher : fetchers_) {
332 flatbuffers::Offset<timing::Statistic> latency_offset =
333 timing::CreateStatistic(fbb);
334
335 timing::Fetcher::Builder fetcher_builder(fbb);
336
337 fetcher_builder.add_channel_index(fetcher->timing_.channel_index);
338 fetcher_builder.add_count(0);
339 fetcher_builder.add_latency(latency_offset);
340 fetcher_offsets.emplace_back(fetcher_builder.Finish());
341 }
342
343 // Then build the final report.
344 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Timer>>>
345 timers_offset;
346 if (timer_offsets.size() > 0) {
347 timers_offset = fbb.CreateVector(timer_offsets);
348 }
349
350 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Timer>>>
351 phased_loops_offset;
352 if (phased_loop_offsets.size() > 0) {
353 phased_loops_offset = fbb.CreateVector(phased_loop_offsets);
354 }
355
356 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Watcher>>>
357 watchers_offset;
358 if (watcher_offsets.size() > 0) {
359 watchers_offset = fbb.CreateVector(watcher_offsets);
360 }
361
362 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Sender>>>
363 senders_offset;
364 if (sender_offsets.size() > 0) {
365 senders_offset = fbb.CreateVector(sender_offsets);
366 }
367
368 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Fetcher>>>
369 fetchers_offset;
370 if (fetcher_offsets.size() > 0) {
371 fetchers_offset = fbb.CreateVector(fetcher_offsets);
372 }
373
374 flatbuffers::Offset<flatbuffers::String> name_offset =
375 fbb.CreateString(name());
376
377 timing::Report::Builder report_builder(fbb);
378 report_builder.add_name(name_offset);
379 report_builder.add_pid(GetTid());
380 if (timer_offsets.size() > 0) {
381 report_builder.add_timers(timers_offset);
382 }
383 if (phased_loop_offsets.size() > 0) {
384 report_builder.add_phased_loops(phased_loops_offset);
385 }
386 if (watcher_offsets.size() > 0) {
387 report_builder.add_watchers(watchers_offset);
388 }
389 if (sender_offsets.size() > 0) {
390 report_builder.add_senders(senders_offset);
391 }
392 if (fetcher_offsets.size() > 0) {
393 report_builder.add_fetchers(fetchers_offset);
394 }
395 fbb.Finish(report_builder.Finish());
396
397 timing_report_ = FlatbufferDetachedBuffer<timing::Report>(fbb.Release());
398
399 // Now that the pointers are stable, pass them to the timers and watchers to
400 // be updated.
401 for (size_t i = 0; i < timers_.size(); ++i) {
402 timers_[i]->timing_.set_timing_report(
403 timing_report_.mutable_message()->mutable_timers()->GetMutableObject(
404 i));
405 }
406
407 for (size_t i = 0; i < phased_loops_.size(); ++i) {
408 phased_loops_[i]->timing_.set_timing_report(
409 timing_report_.mutable_message()
410 ->mutable_phased_loops()
411 ->GetMutableObject(i));
412 }
413
414 for (size_t i = 0; i < watchers_.size(); ++i) {
415 watchers_[i]->set_timing_report(
416 timing_report_.mutable_message()->mutable_watchers()->GetMutableObject(
417 i));
418 }
419
420 for (size_t i = 0; i < senders_.size(); ++i) {
421 senders_[i]->timing_.set_timing_report(
422 timing_report_.mutable_message()->mutable_senders()->GetMutableObject(
423 i));
424 }
425
426 for (size_t i = 0; i < fetchers_.size(); ++i) {
427 fetchers_[i]->timing_.set_timing_report(
428 timing_report_.mutable_message()->mutable_fetchers()->GetMutableObject(
429 i));
430 }
431}
432
433void EventLoop::MaybeScheduleTimingReports() {
434 if (FLAGS_timing_reports && !skip_timing_report_) {
435 CHECK(!timing_report_sender_) << ": Timing reports already scheduled.";
436 // Make a raw sender for the report.
437 const Channel *channel = configuration::GetChannel(
438 configuration(), "/aos", timing::Report::GetFullyQualifiedName(),
Austin Schuhbca6cf02019-12-22 17:28:34 -0800439 name(), node());
Austin Schuh196a4452020-03-15 23:12:03 -0700440 CHECK(channel != nullptr) << ": Failed to look up {\"name\": \"/aos\", "
441 "\"type\": \"aos.timing.Report\"} on node "
442 << FlatbufferToJson(node());
Austin Schuhbca6cf02019-12-22 17:28:34 -0800443
444 // Since we are using a RawSender, validity isn't checked. So check it
445 // ourselves.
Austin Schuhca4828c2019-12-28 14:21:35 -0800446 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
447 LOG(FATAL) << "Channel { \"name\": \"/aos"
448 << channel->name()->string_view() << "\", \"type\": \""
449 << channel->type()->string_view()
450 << "\" } is not able to be sent on this node. Check your "
451 "configuration.";
Austin Schuhbca6cf02019-12-22 17:28:34 -0800452 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800453 CHECK(channel != nullptr) << ": Channel { \"name\": \"/aos\", \"type\": \""
454 << timing::Report::GetFullyQualifiedName()
455 << "\" } not found in config.";
456 timing_report_sender_ = MakeRawSender(channel);
457
458 // Register a handler which sends the report out by copying the raw data
459 // from the prebuilt and subsequently modified report.
460 TimerHandler *timing_reports_timer =
461 AddTimer([this]() { SendTimingReport(); });
462
463 // Set it up to send once per second.
464 timing_reports_timer->set_name("timing_reports");
465 OnRun([this, timing_reports_timer]() {
466 timing_reports_timer->Setup(
467 monotonic_now() + std::chrono::milliseconds(FLAGS_timing_report_ms),
468 std::chrono::milliseconds(FLAGS_timing_report_ms));
469 });
470
471 UpdateTimingReport();
472 }
473}
474
Austin Schuh7d87b672019-12-01 20:23:49 -0800475void EventLoop::ReserveEvents() {
476 events_.reserve(timers_.size() + phased_loops_.size() + watchers_.size());
477}
478
479namespace {
480bool CompareEvents(const EventLoopEvent *first, const EventLoopEvent *second) {
Brian Silvermanbd405c02020-06-23 16:25:23 -0700481 if (first->event_time() > second->event_time()) {
482 return true;
483 }
484 if (first->event_time() < second->event_time()) {
485 return false;
486 }
487 return first->generation() > second->generation();
Austin Schuh7d87b672019-12-01 20:23:49 -0800488}
489} // namespace
490
491void EventLoop::AddEvent(EventLoopEvent *event) {
492 DCHECK(std::find(events_.begin(), events_.end(), event) == events_.end());
Brian Silvermanbd405c02020-06-23 16:25:23 -0700493 DCHECK(event->generation() == 0);
494 event->set_generation(++event_generation_);
Austin Schuh7d87b672019-12-01 20:23:49 -0800495 events_.push_back(event);
496 std::push_heap(events_.begin(), events_.end(), CompareEvents);
497}
498
499void EventLoop::RemoveEvent(EventLoopEvent *event) {
500 auto e = std::find(events_.begin(), events_.end(), event);
501 if (e != events_.end()) {
Brian Silvermanbd405c02020-06-23 16:25:23 -0700502 DCHECK(event->generation() != 0);
Austin Schuh7d87b672019-12-01 20:23:49 -0800503 events_.erase(e);
504 std::make_heap(events_.begin(), events_.end(), CompareEvents);
505 event->Invalidate();
506 }
507}
508
509EventLoopEvent *EventLoop::PopEvent() {
510 EventLoopEvent *result = events_.front();
511 std::pop_heap(events_.begin(), events_.end(), CompareEvents);
512 events_.pop_back();
513 result->Invalidate();
514 return result;
515}
516
Austin Schuha9012be2021-07-21 15:19:11 -0700517void EventLoop::SetTimerContext(
518 monotonic_clock::time_point monotonic_event_time) {
519 context_.monotonic_event_time = monotonic_event_time;
520 context_.monotonic_remote_time = monotonic_clock::min_time;
521 context_.realtime_event_time = realtime_clock::min_time;
522 context_.realtime_remote_time = realtime_clock::min_time;
523 context_.queue_index = 0xffffffffu;
524 context_.size = 0u;
525 context_.data = nullptr;
526 context_.buffer_index = -1;
527 context_.source_boot_uuid = boot_uuid();
528}
529
Austin Schuh39788ff2019-12-01 18:22:57 -0800530void WatcherState::set_timing_report(timing::Watcher *watcher) {
531 CHECK_NOTNULL(watcher);
532 watcher_ = watcher;
533 wakeup_latency_.set_statistic(watcher->mutable_wakeup_latency());
534 handler_time_.set_statistic(watcher->mutable_handler_time());
535}
536
537void WatcherState::ResetReport() {
538 wakeup_latency_.Reset();
539 handler_time_.Reset();
540 watcher_->mutate_count(0);
Austin Schuh54cf95f2019-11-29 13:14:18 -0800541}
542
543} // namespace aos