blob: 68cc923847232e282fa1eabb72f748fadfcbd319 [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() {
205 // We need to do a fancy dance here to get all the accounting to work right.
206 // We want to copy the memory here, but then send after resetting. Otherwise
207 // the send for the timing report won't be counted in the timing report.
208 //
209 // Also, flatbuffers build from the back end. So place this at the back end
210 // of the buffer. We only have to care because we are using this in a very
211 // raw fashion.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800212 CHECK_LE(timing_report_.span().size(), timing_report_sender_->size())
Austin Schuh39788ff2019-12-01 18:22:57 -0800213 << ": Timing report bigger than the sender size.";
Austin Schuhadd6eb32020-11-09 21:24:26 -0800214 std::copy(timing_report_.span().data(),
215 timing_report_.span().data() + timing_report_.span().size(),
Austin Schuh39788ff2019-12-01 18:22:57 -0800216 reinterpret_cast<uint8_t *>(timing_report_sender_->data()) +
Austin Schuhadd6eb32020-11-09 21:24:26 -0800217 timing_report_sender_->size() - timing_report_.span().size());
Austin Schuh39788ff2019-12-01 18:22:57 -0800218
219 for (const std::unique_ptr<TimerHandler> &timer : timers_) {
220 timer->timing_.ResetTimingReport();
221 }
222 for (const std::unique_ptr<WatcherState> &watcher : watchers_) {
223 watcher->ResetReport();
224 }
225 for (const std::unique_ptr<PhasedLoopHandler> &phased_loop : phased_loops_) {
226 phased_loop->timing_.ResetTimingReport();
227 }
228 for (RawSender *sender : senders_) {
229 sender->timing_.ResetTimingReport();
230 }
231 for (RawFetcher *fetcher : fetchers_) {
232 fetcher->timing_.ResetTimingReport();
233 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800234 timing_report_sender_->Send(timing_report_.span().size());
Austin Schuh39788ff2019-12-01 18:22:57 -0800235}
236
237void EventLoop::UpdateTimingReport() {
238 // We need to support senders and fetchers changing while we are setting up
239 // the event loop. Otherwise we can't fetch or send until the loop runs. This
240 // means that on each change, we need to redo all this work. This makes setup
241 // more expensive, but not by all that much on a modern processor.
242
243 // Now, build up a report with everything pre-filled out.
244 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800245 fbb.ForceDefaults(true);
Austin Schuh39788ff2019-12-01 18:22:57 -0800246
247 // Pre-fill in the defaults for timers.
248 std::vector<flatbuffers::Offset<timing::Timer>> timer_offsets;
249 for (const std::unique_ptr<TimerHandler> &timer : timers_) {
250 flatbuffers::Offset<timing::Statistic> wakeup_latency_offset =
251 timing::CreateStatistic(fbb);
252 flatbuffers::Offset<timing::Statistic> handler_time_offset =
253 timing::CreateStatistic(fbb);
254 flatbuffers::Offset<flatbuffers::String> name_offset;
255 if (timer->name().size() != 0) {
256 name_offset = fbb.CreateString(timer->name());
257 }
258
259 timing::Timer::Builder timer_builder(fbb);
260
261 if (timer->name().size() != 0) {
262 timer_builder.add_name(name_offset);
263 }
264 timer_builder.add_wakeup_latency(wakeup_latency_offset);
265 timer_builder.add_handler_time(handler_time_offset);
266 timer_builder.add_count(0);
267 timer_offsets.emplace_back(timer_builder.Finish());
268 }
269
270 // Pre-fill in the defaults for phased_loops.
271 std::vector<flatbuffers::Offset<timing::Timer>> phased_loop_offsets;
272 for (const std::unique_ptr<PhasedLoopHandler> &phased_loop : phased_loops_) {
273 flatbuffers::Offset<timing::Statistic> wakeup_latency_offset =
274 timing::CreateStatistic(fbb);
275 flatbuffers::Offset<timing::Statistic> handler_time_offset =
276 timing::CreateStatistic(fbb);
277 flatbuffers::Offset<flatbuffers::String> name_offset;
278 if (phased_loop->name().size() != 0) {
279 name_offset = fbb.CreateString(phased_loop->name());
280 }
281
282 timing::Timer::Builder timer_builder(fbb);
283
284 if (phased_loop->name().size() != 0) {
285 timer_builder.add_name(name_offset);
286 }
287 timer_builder.add_wakeup_latency(wakeup_latency_offset);
288 timer_builder.add_handler_time(handler_time_offset);
289 timer_builder.add_count(0);
290 phased_loop_offsets.emplace_back(timer_builder.Finish());
291 }
292
293 // Pre-fill in the defaults for watchers.
294 std::vector<flatbuffers::Offset<timing::Watcher>> watcher_offsets;
295 for (const std::unique_ptr<WatcherState> &watcher : watchers_) {
296 flatbuffers::Offset<timing::Statistic> wakeup_latency_offset =
297 timing::CreateStatistic(fbb);
298 flatbuffers::Offset<timing::Statistic> handler_time_offset =
299 timing::CreateStatistic(fbb);
300
301 timing::Watcher::Builder watcher_builder(fbb);
302
303 watcher_builder.add_channel_index(watcher->channel_index());
304 watcher_builder.add_wakeup_latency(wakeup_latency_offset);
305 watcher_builder.add_handler_time(handler_time_offset);
306 watcher_builder.add_count(0);
307 watcher_offsets.emplace_back(watcher_builder.Finish());
308 }
309
310 // Pre-fill in the defaults for senders.
311 std::vector<flatbuffers::Offset<timing::Sender>> sender_offsets;
312 for (const RawSender *sender : senders_) {
313 flatbuffers::Offset<timing::Statistic> size_offset =
314 timing::CreateStatistic(fbb);
315
316 timing::Sender::Builder sender_builder(fbb);
317
318 sender_builder.add_channel_index(sender->timing_.channel_index);
319 sender_builder.add_size(size_offset);
320 sender_builder.add_count(0);
321 sender_offsets.emplace_back(sender_builder.Finish());
322 }
323
324 // Pre-fill in the defaults for fetchers.
325 std::vector<flatbuffers::Offset<timing::Fetcher>> fetcher_offsets;
326 for (RawFetcher *fetcher : fetchers_) {
327 flatbuffers::Offset<timing::Statistic> latency_offset =
328 timing::CreateStatistic(fbb);
329
330 timing::Fetcher::Builder fetcher_builder(fbb);
331
332 fetcher_builder.add_channel_index(fetcher->timing_.channel_index);
333 fetcher_builder.add_count(0);
334 fetcher_builder.add_latency(latency_offset);
335 fetcher_offsets.emplace_back(fetcher_builder.Finish());
336 }
337
338 // Then build the final report.
339 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Timer>>>
340 timers_offset;
341 if (timer_offsets.size() > 0) {
342 timers_offset = fbb.CreateVector(timer_offsets);
343 }
344
345 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Timer>>>
346 phased_loops_offset;
347 if (phased_loop_offsets.size() > 0) {
348 phased_loops_offset = fbb.CreateVector(phased_loop_offsets);
349 }
350
351 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Watcher>>>
352 watchers_offset;
353 if (watcher_offsets.size() > 0) {
354 watchers_offset = fbb.CreateVector(watcher_offsets);
355 }
356
357 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Sender>>>
358 senders_offset;
359 if (sender_offsets.size() > 0) {
360 senders_offset = fbb.CreateVector(sender_offsets);
361 }
362
363 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Fetcher>>>
364 fetchers_offset;
365 if (fetcher_offsets.size() > 0) {
366 fetchers_offset = fbb.CreateVector(fetcher_offsets);
367 }
368
369 flatbuffers::Offset<flatbuffers::String> name_offset =
370 fbb.CreateString(name());
371
372 timing::Report::Builder report_builder(fbb);
373 report_builder.add_name(name_offset);
374 report_builder.add_pid(GetTid());
375 if (timer_offsets.size() > 0) {
376 report_builder.add_timers(timers_offset);
377 }
378 if (phased_loop_offsets.size() > 0) {
379 report_builder.add_phased_loops(phased_loops_offset);
380 }
381 if (watcher_offsets.size() > 0) {
382 report_builder.add_watchers(watchers_offset);
383 }
384 if (sender_offsets.size() > 0) {
385 report_builder.add_senders(senders_offset);
386 }
387 if (fetcher_offsets.size() > 0) {
388 report_builder.add_fetchers(fetchers_offset);
389 }
390 fbb.Finish(report_builder.Finish());
391
392 timing_report_ = FlatbufferDetachedBuffer<timing::Report>(fbb.Release());
393
394 // Now that the pointers are stable, pass them to the timers and watchers to
395 // be updated.
396 for (size_t i = 0; i < timers_.size(); ++i) {
397 timers_[i]->timing_.set_timing_report(
398 timing_report_.mutable_message()->mutable_timers()->GetMutableObject(
399 i));
400 }
401
402 for (size_t i = 0; i < phased_loops_.size(); ++i) {
403 phased_loops_[i]->timing_.set_timing_report(
404 timing_report_.mutable_message()
405 ->mutable_phased_loops()
406 ->GetMutableObject(i));
407 }
408
409 for (size_t i = 0; i < watchers_.size(); ++i) {
410 watchers_[i]->set_timing_report(
411 timing_report_.mutable_message()->mutable_watchers()->GetMutableObject(
412 i));
413 }
414
415 for (size_t i = 0; i < senders_.size(); ++i) {
416 senders_[i]->timing_.set_timing_report(
417 timing_report_.mutable_message()->mutable_senders()->GetMutableObject(
418 i));
419 }
420
421 for (size_t i = 0; i < fetchers_.size(); ++i) {
422 fetchers_[i]->timing_.set_timing_report(
423 timing_report_.mutable_message()->mutable_fetchers()->GetMutableObject(
424 i));
425 }
426}
427
428void EventLoop::MaybeScheduleTimingReports() {
429 if (FLAGS_timing_reports && !skip_timing_report_) {
430 CHECK(!timing_report_sender_) << ": Timing reports already scheduled.";
431 // Make a raw sender for the report.
432 const Channel *channel = configuration::GetChannel(
433 configuration(), "/aos", timing::Report::GetFullyQualifiedName(),
Austin Schuhbca6cf02019-12-22 17:28:34 -0800434 name(), node());
Austin Schuh196a4452020-03-15 23:12:03 -0700435 CHECK(channel != nullptr) << ": Failed to look up {\"name\": \"/aos\", "
436 "\"type\": \"aos.timing.Report\"} on node "
437 << FlatbufferToJson(node());
Austin Schuhbca6cf02019-12-22 17:28:34 -0800438
439 // Since we are using a RawSender, validity isn't checked. So check it
440 // ourselves.
Austin Schuhca4828c2019-12-28 14:21:35 -0800441 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
442 LOG(FATAL) << "Channel { \"name\": \"/aos"
443 << channel->name()->string_view() << "\", \"type\": \""
444 << channel->type()->string_view()
445 << "\" } is not able to be sent on this node. Check your "
446 "configuration.";
Austin Schuhbca6cf02019-12-22 17:28:34 -0800447 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800448 CHECK(channel != nullptr) << ": Channel { \"name\": \"/aos\", \"type\": \""
449 << timing::Report::GetFullyQualifiedName()
450 << "\" } not found in config.";
451 timing_report_sender_ = MakeRawSender(channel);
452
453 // Register a handler which sends the report out by copying the raw data
454 // from the prebuilt and subsequently modified report.
455 TimerHandler *timing_reports_timer =
456 AddTimer([this]() { SendTimingReport(); });
457
458 // Set it up to send once per second.
459 timing_reports_timer->set_name("timing_reports");
460 OnRun([this, timing_reports_timer]() {
461 timing_reports_timer->Setup(
462 monotonic_now() + std::chrono::milliseconds(FLAGS_timing_report_ms),
463 std::chrono::milliseconds(FLAGS_timing_report_ms));
464 });
465
466 UpdateTimingReport();
467 }
468}
469
Austin Schuh7d87b672019-12-01 20:23:49 -0800470void EventLoop::ReserveEvents() {
471 events_.reserve(timers_.size() + phased_loops_.size() + watchers_.size());
472}
473
474namespace {
475bool CompareEvents(const EventLoopEvent *first, const EventLoopEvent *second) {
Brian Silvermanbd405c02020-06-23 16:25:23 -0700476 if (first->event_time() > second->event_time()) {
477 return true;
478 }
479 if (first->event_time() < second->event_time()) {
480 return false;
481 }
482 return first->generation() > second->generation();
Austin Schuh7d87b672019-12-01 20:23:49 -0800483}
484} // namespace
485
486void EventLoop::AddEvent(EventLoopEvent *event) {
487 DCHECK(std::find(events_.begin(), events_.end(), event) == events_.end());
Brian Silvermanbd405c02020-06-23 16:25:23 -0700488 DCHECK(event->generation() == 0);
489 event->set_generation(++event_generation_);
Austin Schuh7d87b672019-12-01 20:23:49 -0800490 events_.push_back(event);
491 std::push_heap(events_.begin(), events_.end(), CompareEvents);
492}
493
494void EventLoop::RemoveEvent(EventLoopEvent *event) {
495 auto e = std::find(events_.begin(), events_.end(), event);
496 if (e != events_.end()) {
Brian Silvermanbd405c02020-06-23 16:25:23 -0700497 DCHECK(event->generation() != 0);
Austin Schuh7d87b672019-12-01 20:23:49 -0800498 events_.erase(e);
499 std::make_heap(events_.begin(), events_.end(), CompareEvents);
500 event->Invalidate();
501 }
502}
503
504EventLoopEvent *EventLoop::PopEvent() {
505 EventLoopEvent *result = events_.front();
506 std::pop_heap(events_.begin(), events_.end(), CompareEvents);
507 events_.pop_back();
508 result->Invalidate();
509 return result;
510}
511
Austin Schuha9012be2021-07-21 15:19:11 -0700512void EventLoop::SetTimerContext(
513 monotonic_clock::time_point monotonic_event_time) {
514 context_.monotonic_event_time = monotonic_event_time;
515 context_.monotonic_remote_time = monotonic_clock::min_time;
516 context_.realtime_event_time = realtime_clock::min_time;
517 context_.realtime_remote_time = realtime_clock::min_time;
518 context_.queue_index = 0xffffffffu;
519 context_.size = 0u;
520 context_.data = nullptr;
521 context_.buffer_index = -1;
522 context_.source_boot_uuid = boot_uuid();
523}
524
Austin Schuh39788ff2019-12-01 18:22:57 -0800525void WatcherState::set_timing_report(timing::Watcher *watcher) {
526 CHECK_NOTNULL(watcher);
527 watcher_ = watcher;
528 wakeup_latency_.set_statistic(watcher->mutable_wakeup_latency());
529 handler_time_.set_statistic(watcher->mutable_handler_time());
530}
531
532void WatcherState::ResetReport() {
533 wakeup_latency_.Reset();
534 handler_time_.Reset();
535 watcher_->mutate_count(0);
Austin Schuh54cf95f2019-11-29 13:14:18 -0800536}
537
538} // namespace aos