Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 1 | #include "aos/events/event_loop.h" |
| 2 | |
| 3 | #include "aos/configuration.h" |
| 4 | #include "aos/configuration_generated.h" |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 5 | #include "aos/logging/implementations.h" |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 6 | #include "glog/logging.h" |
| 7 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 8 | DEFINE_bool(timing_reports, true, "Publish timing reports."); |
| 9 | DEFINE_int32(timing_report_ms, 1000, |
| 10 | "Period in milliseconds to publish timing reports at."); |
| 11 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 12 | namespace aos { |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 13 | namespace { |
| 14 | void 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 Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 24 | RawSender::RawSender(EventLoop *event_loop, const Channel *channel) |
| 25 | : event_loop_(event_loop), |
| 26 | channel_(channel), |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 27 | ftrace_prefix_(configuration::StrippedChannelToString(channel)), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 28 | timing_(event_loop_->ChannelIndex(channel)) { |
| 29 | event_loop_->NewSender(this); |
| 30 | } |
| 31 | |
| 32 | RawSender::~RawSender() { event_loop_->DeleteSender(this); } |
| 33 | |
| 34 | RawFetcher::RawFetcher(EventLoop *event_loop, const Channel *channel) |
| 35 | : event_loop_(event_loop), |
| 36 | channel_(channel), |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 37 | ftrace_prefix_(configuration::StrippedChannelToString(channel)), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 38 | timing_(event_loop_->ChannelIndex(channel)) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 39 | context_.monotonic_event_time = monotonic_clock::min_time; |
| 40 | context_.monotonic_remote_time = monotonic_clock::min_time; |
| 41 | context_.realtime_event_time = realtime_clock::min_time; |
| 42 | context_.realtime_remote_time = realtime_clock::min_time; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 43 | context_.queue_index = 0xffffffff; |
| 44 | context_.size = 0; |
| 45 | context_.data = nullptr; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 46 | context_.buffer_index = -1; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 47 | event_loop_->NewFetcher(this); |
| 48 | } |
| 49 | |
| 50 | RawFetcher::~RawFetcher() { event_loop_->DeleteFetcher(this); } |
| 51 | |
| 52 | TimerHandler::TimerHandler(EventLoop *event_loop, std::function<void()> fn) |
| 53 | : event_loop_(event_loop), fn_(std::move(fn)) {} |
| 54 | |
| 55 | TimerHandler::~TimerHandler() {} |
| 56 | |
| 57 | PhasedLoopHandler::PhasedLoopHandler(EventLoop *event_loop, |
| 58 | std::function<void(int)> fn, |
| 59 | const monotonic_clock::duration interval, |
| 60 | const monotonic_clock::duration offset) |
| 61 | : event_loop_(event_loop), |
| 62 | fn_(std::move(fn)), |
| 63 | phased_loop_(interval, event_loop_->monotonic_now(), offset) { |
| 64 | event_loop_->OnRun([this]() { |
| 65 | const monotonic_clock::time_point monotonic_now = |
| 66 | event_loop_->monotonic_now(); |
| 67 | phased_loop_.Reset(monotonic_now); |
| 68 | Reschedule( |
| 69 | [this](monotonic_clock::time_point sleep_time) { |
| 70 | Schedule(sleep_time); |
| 71 | }, |
| 72 | monotonic_now); |
| 73 | // The first time, we'll double count. Reschedule here will count cycles |
| 74 | // elapsed before now, and then the reschedule before runing the handler |
| 75 | // will count the time that elapsed then. So clear the count here. |
| 76 | cycles_elapsed_ = 0; |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | PhasedLoopHandler::~PhasedLoopHandler() {} |
| 81 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 82 | EventLoop::EventLoop(const Configuration *configuration) |
| 83 | : timing_report_(flatbuffers::DetachedBuffer()), |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 84 | configuration_(configuration) {} |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 85 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 86 | EventLoop::~EventLoop() { |
| 87 | CHECK_EQ(senders_.size(), 0u) << ": Not all senders destroyed"; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 88 | CHECK_EQ(events_.size(), 0u) << ": Not all events unregistered"; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int EventLoop::ChannelIndex(const Channel *channel) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 92 | return configuration::ChannelIndex(configuration_, channel); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 95 | WatcherState *EventLoop::GetWatcherState(const Channel *channel) { |
| 96 | const int channel_index = ChannelIndex(channel); |
| 97 | for (const std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 98 | if (watcher->channel_index() == channel_index) { |
| 99 | return watcher.get(); |
| 100 | } |
| 101 | } |
| 102 | LOG(FATAL) << "No watcher found for channel"; |
| 103 | } |
| 104 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 105 | void EventLoop::NewSender(RawSender *sender) { |
| 106 | senders_.emplace_back(sender); |
| 107 | UpdateTimingReport(); |
| 108 | } |
| 109 | void EventLoop::DeleteSender(RawSender *sender) { |
| 110 | CHECK(!is_running()); |
| 111 | auto s = std::find(senders_.begin(), senders_.end(), sender); |
| 112 | CHECK(s != senders_.end()) << ": Sender not in senders list"; |
| 113 | senders_.erase(s); |
| 114 | UpdateTimingReport(); |
| 115 | } |
| 116 | |
| 117 | TimerHandler *EventLoop::NewTimer(std::unique_ptr<TimerHandler> timer) { |
| 118 | timers_.emplace_back(std::move(timer)); |
| 119 | UpdateTimingReport(); |
| 120 | return timers_.back().get(); |
| 121 | } |
| 122 | |
| 123 | PhasedLoopHandler *EventLoop::NewPhasedLoop( |
| 124 | std::unique_ptr<PhasedLoopHandler> phased_loop) { |
| 125 | phased_loops_.emplace_back(std::move(phased_loop)); |
| 126 | UpdateTimingReport(); |
| 127 | return phased_loops_.back().get(); |
| 128 | } |
| 129 | |
| 130 | void EventLoop::NewFetcher(RawFetcher *fetcher) { |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 131 | CheckAlignment(fetcher->channel()); |
| 132 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 133 | fetchers_.emplace_back(fetcher); |
| 134 | UpdateTimingReport(); |
| 135 | } |
| 136 | |
| 137 | void EventLoop::DeleteFetcher(RawFetcher *fetcher) { |
| 138 | CHECK(!is_running()); |
| 139 | auto f = std::find(fetchers_.begin(), fetchers_.end(), fetcher); |
| 140 | CHECK(f != fetchers_.end()) << ": Fetcher not in fetchers list"; |
| 141 | fetchers_.erase(f); |
| 142 | UpdateTimingReport(); |
| 143 | } |
| 144 | |
| 145 | WatcherState *EventLoop::NewWatcher(std::unique_ptr<WatcherState> watcher) { |
| 146 | watchers_.emplace_back(std::move(watcher)); |
| 147 | |
| 148 | UpdateTimingReport(); |
| 149 | |
| 150 | return watchers_.back().get(); |
| 151 | } |
| 152 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 153 | void EventLoop::TakeWatcher(const Channel *channel) { |
| 154 | CHECK(!is_running()) << ": Cannot add new objects while running."; |
| 155 | ChannelIndex(channel); |
| 156 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 157 | CheckAlignment(channel); |
| 158 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 159 | CHECK(taken_senders_.find(channel) == taken_senders_.end()) |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 160 | << ": " << configuration::CleanedChannelToString(channel) |
| 161 | << " is already being used."; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 162 | |
| 163 | auto result = taken_watchers_.insert(channel); |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 164 | CHECK(result.second) << ": " << configuration::CleanedChannelToString(channel) |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 165 | << " is already being used."; |
| 166 | |
| 167 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 168 | LOG(FATAL) << ": " << configuration::CleanedChannelToString(channel) |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 169 | << " is not able to be watched on this node. Check your " |
| 170 | "configuration."; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void EventLoop::TakeSender(const Channel *channel) { |
| 175 | CHECK(!is_running()) << ": Cannot add new objects while running."; |
| 176 | ChannelIndex(channel); |
| 177 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 178 | CheckAlignment(channel); |
| 179 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 180 | CHECK(taken_watchers_.find(channel) == taken_watchers_.end()) |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 181 | << ": Channel " << configuration::CleanedChannelToString(channel) |
| 182 | << " is already being used."; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 183 | |
| 184 | // We don't care if this is a duplicate. |
| 185 | taken_senders_.insert(channel); |
| 186 | } |
| 187 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 188 | void EventLoop::SendTimingReport() { |
| 189 | // We need to do a fancy dance here to get all the accounting to work right. |
| 190 | // We want to copy the memory here, but then send after resetting. Otherwise |
| 191 | // the send for the timing report won't be counted in the timing report. |
| 192 | // |
| 193 | // Also, flatbuffers build from the back end. So place this at the back end |
| 194 | // of the buffer. We only have to care because we are using this in a very |
| 195 | // raw fashion. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 196 | CHECK_LE(timing_report_.span().size(), timing_report_sender_->size()) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 197 | << ": Timing report bigger than the sender size."; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 198 | std::copy(timing_report_.span().data(), |
| 199 | timing_report_.span().data() + timing_report_.span().size(), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 200 | reinterpret_cast<uint8_t *>(timing_report_sender_->data()) + |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 201 | timing_report_sender_->size() - timing_report_.span().size()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 202 | |
| 203 | for (const std::unique_ptr<TimerHandler> &timer : timers_) { |
| 204 | timer->timing_.ResetTimingReport(); |
| 205 | } |
| 206 | for (const std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 207 | watcher->ResetReport(); |
| 208 | } |
| 209 | for (const std::unique_ptr<PhasedLoopHandler> &phased_loop : phased_loops_) { |
| 210 | phased_loop->timing_.ResetTimingReport(); |
| 211 | } |
| 212 | for (RawSender *sender : senders_) { |
| 213 | sender->timing_.ResetTimingReport(); |
| 214 | } |
| 215 | for (RawFetcher *fetcher : fetchers_) { |
| 216 | fetcher->timing_.ResetTimingReport(); |
| 217 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame^] | 218 | timing_report_sender_->Send(timing_report_.span().size()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void EventLoop::UpdateTimingReport() { |
| 222 | // We need to support senders and fetchers changing while we are setting up |
| 223 | // the event loop. Otherwise we can't fetch or send until the loop runs. This |
| 224 | // means that on each change, we need to redo all this work. This makes setup |
| 225 | // more expensive, but not by all that much on a modern processor. |
| 226 | |
| 227 | // Now, build up a report with everything pre-filled out. |
| 228 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 229 | fbb.ForceDefaults(true); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 230 | |
| 231 | // Pre-fill in the defaults for timers. |
| 232 | std::vector<flatbuffers::Offset<timing::Timer>> timer_offsets; |
| 233 | for (const std::unique_ptr<TimerHandler> &timer : timers_) { |
| 234 | flatbuffers::Offset<timing::Statistic> wakeup_latency_offset = |
| 235 | timing::CreateStatistic(fbb); |
| 236 | flatbuffers::Offset<timing::Statistic> handler_time_offset = |
| 237 | timing::CreateStatistic(fbb); |
| 238 | flatbuffers::Offset<flatbuffers::String> name_offset; |
| 239 | if (timer->name().size() != 0) { |
| 240 | name_offset = fbb.CreateString(timer->name()); |
| 241 | } |
| 242 | |
| 243 | timing::Timer::Builder timer_builder(fbb); |
| 244 | |
| 245 | if (timer->name().size() != 0) { |
| 246 | timer_builder.add_name(name_offset); |
| 247 | } |
| 248 | timer_builder.add_wakeup_latency(wakeup_latency_offset); |
| 249 | timer_builder.add_handler_time(handler_time_offset); |
| 250 | timer_builder.add_count(0); |
| 251 | timer_offsets.emplace_back(timer_builder.Finish()); |
| 252 | } |
| 253 | |
| 254 | // Pre-fill in the defaults for phased_loops. |
| 255 | std::vector<flatbuffers::Offset<timing::Timer>> phased_loop_offsets; |
| 256 | for (const std::unique_ptr<PhasedLoopHandler> &phased_loop : phased_loops_) { |
| 257 | flatbuffers::Offset<timing::Statistic> wakeup_latency_offset = |
| 258 | timing::CreateStatistic(fbb); |
| 259 | flatbuffers::Offset<timing::Statistic> handler_time_offset = |
| 260 | timing::CreateStatistic(fbb); |
| 261 | flatbuffers::Offset<flatbuffers::String> name_offset; |
| 262 | if (phased_loop->name().size() != 0) { |
| 263 | name_offset = fbb.CreateString(phased_loop->name()); |
| 264 | } |
| 265 | |
| 266 | timing::Timer::Builder timer_builder(fbb); |
| 267 | |
| 268 | if (phased_loop->name().size() != 0) { |
| 269 | timer_builder.add_name(name_offset); |
| 270 | } |
| 271 | timer_builder.add_wakeup_latency(wakeup_latency_offset); |
| 272 | timer_builder.add_handler_time(handler_time_offset); |
| 273 | timer_builder.add_count(0); |
| 274 | phased_loop_offsets.emplace_back(timer_builder.Finish()); |
| 275 | } |
| 276 | |
| 277 | // Pre-fill in the defaults for watchers. |
| 278 | std::vector<flatbuffers::Offset<timing::Watcher>> watcher_offsets; |
| 279 | for (const std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 280 | flatbuffers::Offset<timing::Statistic> wakeup_latency_offset = |
| 281 | timing::CreateStatistic(fbb); |
| 282 | flatbuffers::Offset<timing::Statistic> handler_time_offset = |
| 283 | timing::CreateStatistic(fbb); |
| 284 | |
| 285 | timing::Watcher::Builder watcher_builder(fbb); |
| 286 | |
| 287 | watcher_builder.add_channel_index(watcher->channel_index()); |
| 288 | watcher_builder.add_wakeup_latency(wakeup_latency_offset); |
| 289 | watcher_builder.add_handler_time(handler_time_offset); |
| 290 | watcher_builder.add_count(0); |
| 291 | watcher_offsets.emplace_back(watcher_builder.Finish()); |
| 292 | } |
| 293 | |
| 294 | // Pre-fill in the defaults for senders. |
| 295 | std::vector<flatbuffers::Offset<timing::Sender>> sender_offsets; |
| 296 | for (const RawSender *sender : senders_) { |
| 297 | flatbuffers::Offset<timing::Statistic> size_offset = |
| 298 | timing::CreateStatistic(fbb); |
| 299 | |
| 300 | timing::Sender::Builder sender_builder(fbb); |
| 301 | |
| 302 | sender_builder.add_channel_index(sender->timing_.channel_index); |
| 303 | sender_builder.add_size(size_offset); |
| 304 | sender_builder.add_count(0); |
| 305 | sender_offsets.emplace_back(sender_builder.Finish()); |
| 306 | } |
| 307 | |
| 308 | // Pre-fill in the defaults for fetchers. |
| 309 | std::vector<flatbuffers::Offset<timing::Fetcher>> fetcher_offsets; |
| 310 | for (RawFetcher *fetcher : fetchers_) { |
| 311 | flatbuffers::Offset<timing::Statistic> latency_offset = |
| 312 | timing::CreateStatistic(fbb); |
| 313 | |
| 314 | timing::Fetcher::Builder fetcher_builder(fbb); |
| 315 | |
| 316 | fetcher_builder.add_channel_index(fetcher->timing_.channel_index); |
| 317 | fetcher_builder.add_count(0); |
| 318 | fetcher_builder.add_latency(latency_offset); |
| 319 | fetcher_offsets.emplace_back(fetcher_builder.Finish()); |
| 320 | } |
| 321 | |
| 322 | // Then build the final report. |
| 323 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Timer>>> |
| 324 | timers_offset; |
| 325 | if (timer_offsets.size() > 0) { |
| 326 | timers_offset = fbb.CreateVector(timer_offsets); |
| 327 | } |
| 328 | |
| 329 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Timer>>> |
| 330 | phased_loops_offset; |
| 331 | if (phased_loop_offsets.size() > 0) { |
| 332 | phased_loops_offset = fbb.CreateVector(phased_loop_offsets); |
| 333 | } |
| 334 | |
| 335 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Watcher>>> |
| 336 | watchers_offset; |
| 337 | if (watcher_offsets.size() > 0) { |
| 338 | watchers_offset = fbb.CreateVector(watcher_offsets); |
| 339 | } |
| 340 | |
| 341 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Sender>>> |
| 342 | senders_offset; |
| 343 | if (sender_offsets.size() > 0) { |
| 344 | senders_offset = fbb.CreateVector(sender_offsets); |
| 345 | } |
| 346 | |
| 347 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Fetcher>>> |
| 348 | fetchers_offset; |
| 349 | if (fetcher_offsets.size() > 0) { |
| 350 | fetchers_offset = fbb.CreateVector(fetcher_offsets); |
| 351 | } |
| 352 | |
| 353 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 354 | fbb.CreateString(name()); |
| 355 | |
| 356 | timing::Report::Builder report_builder(fbb); |
| 357 | report_builder.add_name(name_offset); |
| 358 | report_builder.add_pid(GetTid()); |
| 359 | if (timer_offsets.size() > 0) { |
| 360 | report_builder.add_timers(timers_offset); |
| 361 | } |
| 362 | if (phased_loop_offsets.size() > 0) { |
| 363 | report_builder.add_phased_loops(phased_loops_offset); |
| 364 | } |
| 365 | if (watcher_offsets.size() > 0) { |
| 366 | report_builder.add_watchers(watchers_offset); |
| 367 | } |
| 368 | if (sender_offsets.size() > 0) { |
| 369 | report_builder.add_senders(senders_offset); |
| 370 | } |
| 371 | if (fetcher_offsets.size() > 0) { |
| 372 | report_builder.add_fetchers(fetchers_offset); |
| 373 | } |
| 374 | fbb.Finish(report_builder.Finish()); |
| 375 | |
| 376 | timing_report_ = FlatbufferDetachedBuffer<timing::Report>(fbb.Release()); |
| 377 | |
| 378 | // Now that the pointers are stable, pass them to the timers and watchers to |
| 379 | // be updated. |
| 380 | for (size_t i = 0; i < timers_.size(); ++i) { |
| 381 | timers_[i]->timing_.set_timing_report( |
| 382 | timing_report_.mutable_message()->mutable_timers()->GetMutableObject( |
| 383 | i)); |
| 384 | } |
| 385 | |
| 386 | for (size_t i = 0; i < phased_loops_.size(); ++i) { |
| 387 | phased_loops_[i]->timing_.set_timing_report( |
| 388 | timing_report_.mutable_message() |
| 389 | ->mutable_phased_loops() |
| 390 | ->GetMutableObject(i)); |
| 391 | } |
| 392 | |
| 393 | for (size_t i = 0; i < watchers_.size(); ++i) { |
| 394 | watchers_[i]->set_timing_report( |
| 395 | timing_report_.mutable_message()->mutable_watchers()->GetMutableObject( |
| 396 | i)); |
| 397 | } |
| 398 | |
| 399 | for (size_t i = 0; i < senders_.size(); ++i) { |
| 400 | senders_[i]->timing_.set_timing_report( |
| 401 | timing_report_.mutable_message()->mutable_senders()->GetMutableObject( |
| 402 | i)); |
| 403 | } |
| 404 | |
| 405 | for (size_t i = 0; i < fetchers_.size(); ++i) { |
| 406 | fetchers_[i]->timing_.set_timing_report( |
| 407 | timing_report_.mutable_message()->mutable_fetchers()->GetMutableObject( |
| 408 | i)); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void EventLoop::MaybeScheduleTimingReports() { |
| 413 | if (FLAGS_timing_reports && !skip_timing_report_) { |
| 414 | CHECK(!timing_report_sender_) << ": Timing reports already scheduled."; |
| 415 | // Make a raw sender for the report. |
| 416 | const Channel *channel = configuration::GetChannel( |
| 417 | configuration(), "/aos", timing::Report::GetFullyQualifiedName(), |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 418 | name(), node()); |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 419 | CHECK(channel != nullptr) << ": Failed to look up {\"name\": \"/aos\", " |
| 420 | "\"type\": \"aos.timing.Report\"} on node " |
| 421 | << FlatbufferToJson(node()); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 422 | |
| 423 | // Since we are using a RawSender, validity isn't checked. So check it |
| 424 | // ourselves. |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 425 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 426 | LOG(FATAL) << "Channel { \"name\": \"/aos" |
| 427 | << channel->name()->string_view() << "\", \"type\": \"" |
| 428 | << channel->type()->string_view() |
| 429 | << "\" } is not able to be sent on this node. Check your " |
| 430 | "configuration."; |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 431 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 432 | CHECK(channel != nullptr) << ": Channel { \"name\": \"/aos\", \"type\": \"" |
| 433 | << timing::Report::GetFullyQualifiedName() |
| 434 | << "\" } not found in config."; |
| 435 | timing_report_sender_ = MakeRawSender(channel); |
| 436 | |
| 437 | // Register a handler which sends the report out by copying the raw data |
| 438 | // from the prebuilt and subsequently modified report. |
| 439 | TimerHandler *timing_reports_timer = |
| 440 | AddTimer([this]() { SendTimingReport(); }); |
| 441 | |
| 442 | // Set it up to send once per second. |
| 443 | timing_reports_timer->set_name("timing_reports"); |
| 444 | OnRun([this, timing_reports_timer]() { |
| 445 | timing_reports_timer->Setup( |
| 446 | monotonic_now() + std::chrono::milliseconds(FLAGS_timing_report_ms), |
| 447 | std::chrono::milliseconds(FLAGS_timing_report_ms)); |
| 448 | }); |
| 449 | |
| 450 | UpdateTimingReport(); |
| 451 | } |
| 452 | } |
| 453 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 454 | void EventLoop::ReserveEvents() { |
| 455 | events_.reserve(timers_.size() + phased_loops_.size() + watchers_.size()); |
| 456 | } |
| 457 | |
| 458 | namespace { |
| 459 | bool CompareEvents(const EventLoopEvent *first, const EventLoopEvent *second) { |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 460 | if (first->event_time() > second->event_time()) { |
| 461 | return true; |
| 462 | } |
| 463 | if (first->event_time() < second->event_time()) { |
| 464 | return false; |
| 465 | } |
| 466 | return first->generation() > second->generation(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 467 | } |
| 468 | } // namespace |
| 469 | |
| 470 | void EventLoop::AddEvent(EventLoopEvent *event) { |
| 471 | DCHECK(std::find(events_.begin(), events_.end(), event) == events_.end()); |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 472 | DCHECK(event->generation() == 0); |
| 473 | event->set_generation(++event_generation_); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 474 | events_.push_back(event); |
| 475 | std::push_heap(events_.begin(), events_.end(), CompareEvents); |
| 476 | } |
| 477 | |
| 478 | void EventLoop::RemoveEvent(EventLoopEvent *event) { |
| 479 | auto e = std::find(events_.begin(), events_.end(), event); |
| 480 | if (e != events_.end()) { |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 481 | DCHECK(event->generation() != 0); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 482 | events_.erase(e); |
| 483 | std::make_heap(events_.begin(), events_.end(), CompareEvents); |
| 484 | event->Invalidate(); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | EventLoopEvent *EventLoop::PopEvent() { |
| 489 | EventLoopEvent *result = events_.front(); |
| 490 | std::pop_heap(events_.begin(), events_.end(), CompareEvents); |
| 491 | events_.pop_back(); |
| 492 | result->Invalidate(); |
| 493 | return result; |
| 494 | } |
| 495 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 496 | void WatcherState::set_timing_report(timing::Watcher *watcher) { |
| 497 | CHECK_NOTNULL(watcher); |
| 498 | watcher_ = watcher; |
| 499 | wakeup_latency_.set_statistic(watcher->mutable_wakeup_latency()); |
| 500 | handler_time_.set_statistic(watcher->mutable_handler_time()); |
| 501 | } |
| 502 | |
| 503 | void WatcherState::ResetReport() { |
| 504 | wakeup_latency_.Reset(); |
| 505 | handler_time_.Reset(); |
| 506 | watcher_->mutate_count(0); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | } // namespace aos |