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 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 34 | bool 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 Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 43 | RawFetcher::RawFetcher(EventLoop *event_loop, const Channel *channel) |
| 44 | : event_loop_(event_loop), |
| 45 | channel_(channel), |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 46 | ftrace_prefix_(configuration::StrippedChannelToString(channel)), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 47 | timing_(event_loop_->ChannelIndex(channel)) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 48 | 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 Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 52 | context_.queue_index = 0xffffffff; |
| 53 | context_.size = 0; |
| 54 | context_.data = nullptr; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 55 | context_.buffer_index = -1; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 56 | event_loop_->NewFetcher(this); |
| 57 | } |
| 58 | |
| 59 | RawFetcher::~RawFetcher() { event_loop_->DeleteFetcher(this); } |
| 60 | |
| 61 | TimerHandler::TimerHandler(EventLoop *event_loop, std::function<void()> fn) |
| 62 | : event_loop_(event_loop), fn_(std::move(fn)) {} |
| 63 | |
| 64 | TimerHandler::~TimerHandler() {} |
| 65 | |
| 66 | PhasedLoopHandler::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 Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 82 | // 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 Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 85 | cycles_elapsed_ = 0; |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | PhasedLoopHandler::~PhasedLoopHandler() {} |
| 90 | |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 91 | EventLoop::EventLoop(const Configuration *configuration) |
| 92 | : timing_report_(flatbuffers::DetachedBuffer()), |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 93 | configuration_(configuration) {} |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 94 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 95 | EventLoop::~EventLoop() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 96 | if(!senders_.empty()) { |
| 97 | for (const RawSender *sender : senders_) { |
| 98 | LOG(ERROR) << " Sender " |
| 99 | << configuration::StrippedChannelToString(sender->channel()) |
| 100 | << " still open"; |
| 101 | } |
| 102 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 103 | CHECK_EQ(senders_.size(), 0u) << ": Not all senders destroyed"; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 104 | CHECK_EQ(events_.size(), 0u) << ": Not all events unregistered"; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int EventLoop::ChannelIndex(const Channel *channel) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 108 | return configuration::ChannelIndex(configuration_, channel); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 111 | WatcherState *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 Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 121 | void EventLoop::NewSender(RawSender *sender) { |
| 122 | senders_.emplace_back(sender); |
| 123 | UpdateTimingReport(); |
| 124 | } |
| 125 | void 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 | |
| 133 | TimerHandler *EventLoop::NewTimer(std::unique_ptr<TimerHandler> timer) { |
| 134 | timers_.emplace_back(std::move(timer)); |
| 135 | UpdateTimingReport(); |
| 136 | return timers_.back().get(); |
| 137 | } |
| 138 | |
| 139 | PhasedLoopHandler *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 | |
| 146 | void EventLoop::NewFetcher(RawFetcher *fetcher) { |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 147 | CheckAlignment(fetcher->channel()); |
| 148 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 149 | fetchers_.emplace_back(fetcher); |
| 150 | UpdateTimingReport(); |
| 151 | } |
| 152 | |
| 153 | void 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 | |
| 161 | WatcherState *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 Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 169 | void EventLoop::TakeWatcher(const Channel *channel) { |
| 170 | CHECK(!is_running()) << ": Cannot add new objects while running."; |
| 171 | ChannelIndex(channel); |
| 172 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 173 | CheckAlignment(channel); |
| 174 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 175 | CHECK(taken_senders_.find(channel) == taken_senders_.end()) |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 176 | << ": " << configuration::CleanedChannelToString(channel) |
| 177 | << " is already being used."; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 178 | |
| 179 | auto result = taken_watchers_.insert(channel); |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 180 | CHECK(result.second) << ": " << configuration::CleanedChannelToString(channel) |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 181 | << " is already being used."; |
| 182 | |
| 183 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 184 | LOG(FATAL) << ": " << configuration::CleanedChannelToString(channel) |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 185 | << " is not able to be watched on this node. Check your " |
| 186 | "configuration."; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void EventLoop::TakeSender(const Channel *channel) { |
| 191 | CHECK(!is_running()) << ": Cannot add new objects while running."; |
| 192 | ChannelIndex(channel); |
| 193 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 194 | CheckAlignment(channel); |
| 195 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 196 | CHECK(taken_watchers_.find(channel) == taken_watchers_.end()) |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 197 | << ": Channel " << configuration::CleanedChannelToString(channel) |
| 198 | << " is already being used."; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 199 | |
| 200 | // We don't care if this is a duplicate. |
| 201 | taken_senders_.insert(channel); |
| 202 | } |
| 203 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 204 | void EventLoop::SendTimingReport() { |
Brian Silverman | ce418d0 | 2021-11-03 11:25:52 -0700 | [diff] [blame] | 205 | if (!timing_report_sender_) { |
| 206 | // Timing reports are disabled, so nothing for us to do. |
| 207 | return; |
| 208 | } |
| 209 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 210 | // 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 217 | CHECK_LE(timing_report_.span().size(), timing_report_sender_->size()) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 218 | << ": Timing report bigger than the sender size."; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 219 | std::copy(timing_report_.span().data(), |
| 220 | timing_report_.span().data() + timing_report_.span().size(), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 221 | reinterpret_cast<uint8_t *>(timing_report_sender_->data()) + |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 222 | timing_report_sender_->size() - timing_report_.span().size()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 223 | |
| 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 239 | timing_report_sender_->Send(timing_report_.span().size()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void 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 Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 250 | fbb.ForceDefaults(true); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 251 | |
| 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 | |
| 433 | void 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 Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 439 | name(), node()); |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 440 | CHECK(channel != nullptr) << ": Failed to look up {\"name\": \"/aos\", " |
| 441 | "\"type\": \"aos.timing.Report\"} on node " |
| 442 | << FlatbufferToJson(node()); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 443 | |
| 444 | // Since we are using a RawSender, validity isn't checked. So check it |
| 445 | // ourselves. |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 446 | 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 Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 452 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 453 | 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 Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 475 | void EventLoop::ReserveEvents() { |
| 476 | events_.reserve(timers_.size() + phased_loops_.size() + watchers_.size()); |
| 477 | } |
| 478 | |
| 479 | namespace { |
| 480 | bool CompareEvents(const EventLoopEvent *first, const EventLoopEvent *second) { |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 481 | 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 Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 488 | } |
| 489 | } // namespace |
| 490 | |
| 491 | void EventLoop::AddEvent(EventLoopEvent *event) { |
| 492 | DCHECK(std::find(events_.begin(), events_.end(), event) == events_.end()); |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 493 | DCHECK(event->generation() == 0); |
| 494 | event->set_generation(++event_generation_); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 495 | events_.push_back(event); |
| 496 | std::push_heap(events_.begin(), events_.end(), CompareEvents); |
| 497 | } |
| 498 | |
| 499 | void EventLoop::RemoveEvent(EventLoopEvent *event) { |
| 500 | auto e = std::find(events_.begin(), events_.end(), event); |
| 501 | if (e != events_.end()) { |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 502 | DCHECK(event->generation() != 0); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 503 | events_.erase(e); |
| 504 | std::make_heap(events_.begin(), events_.end(), CompareEvents); |
| 505 | event->Invalidate(); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | EventLoopEvent *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 Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 517 | void 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 Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 530 | void 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 | |
| 537 | void WatcherState::ResetReport() { |
| 538 | wakeup_latency_.Reset(); |
| 539 | handler_time_.Reset(); |
| 540 | watcher_->mutate_count(0); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | } // namespace aos |