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 | 070019a | 2022-12-20 22:23:09 -0800 | [diff] [blame^] | 6 | #include "aos/realtime.h" |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 7 | #include "glog/logging.h" |
| 8 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 9 | DEFINE_bool(timing_reports, true, "Publish timing reports."); |
| 10 | DEFINE_int32(timing_report_ms, 1000, |
| 11 | "Period in milliseconds to publish timing reports at."); |
| 12 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 13 | namespace aos { |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 14 | namespace { |
| 15 | void CheckAlignment(const Channel *channel) { |
| 16 | if (channel->max_size() % alignof(flatbuffers::largest_scalar_t) != 0) { |
| 17 | LOG(FATAL) << "max_size() (" << channel->max_size() |
| 18 | << ") is not a multiple of alignment (" |
| 19 | << alignof(flatbuffers::largest_scalar_t) << ") for channel " |
| 20 | << configuration::CleanedChannelToString(channel) << "."; |
| 21 | } |
| 22 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 23 | |
| 24 | std::string_view ErrorToString(const RawSender::Error err) { |
| 25 | switch (err) { |
| 26 | case RawSender::Error::kOk: |
| 27 | return "RawSender::Error::kOk"; |
| 28 | case RawSender::Error::kMessagesSentTooFast: |
| 29 | return "RawSender::Error::kMessagesSentTooFast"; |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 30 | case RawSender::Error::kInvalidRedzone: |
| 31 | return "RawSender::Error::kInvalidRedzone"; |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 32 | } |
| 33 | LOG(FATAL) << "Unknown error given with code " << static_cast<int>(err); |
| 34 | } |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 35 | } // namespace |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 36 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 37 | std::ostream &operator<<(std::ostream &os, const RawSender::Error err) { |
| 38 | os << ErrorToString(err); |
| 39 | return os; |
| 40 | } |
| 41 | |
| 42 | void RawSender::CheckOk(const RawSender::Error err) { |
| 43 | CHECK_EQ(err, Error::kOk) << "Messages were sent too fast on channel: " |
| 44 | << configuration::CleanedChannelToString(channel_); |
| 45 | } |
| 46 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 47 | RawSender::RawSender(EventLoop *event_loop, const Channel *channel) |
| 48 | : event_loop_(event_loop), |
| 49 | channel_(channel), |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 50 | ftrace_prefix_(configuration::StrippedChannelToString(channel)), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 51 | timing_(event_loop_->ChannelIndex(channel)) { |
| 52 | event_loop_->NewSender(this); |
| 53 | } |
| 54 | |
| 55 | RawSender::~RawSender() { event_loop_->DeleteSender(this); } |
| 56 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 57 | RawSender::Error RawSender::DoSend( |
| 58 | const SharedSpan data, monotonic_clock::time_point monotonic_remote_time, |
| 59 | realtime_clock::time_point realtime_remote_time, |
| 60 | uint32_t remote_queue_index, const UUID &source_boot_uuid) { |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 61 | return DoSend(data->data(), data->size(), monotonic_remote_time, |
| 62 | realtime_remote_time, remote_queue_index, source_boot_uuid); |
| 63 | } |
| 64 | |
James Kuszmaul | 93abac1 | 2022-04-14 15:05:10 -0700 | [diff] [blame] | 65 | void RawSender::RecordSendResult(const Error error, size_t message_size) { |
| 66 | switch (error) { |
| 67 | case Error::kOk: { |
| 68 | if (timing_.sender) { |
| 69 | timing_.size.Add(message_size); |
| 70 | timing_.sender->mutate_count(timing_.sender->count() + 1); |
| 71 | } |
| 72 | break; |
| 73 | } |
| 74 | case Error::kMessagesSentTooFast: |
| 75 | timing_.IncrementError(timing::SendError::MESSAGE_SENT_TOO_FAST); |
| 76 | break; |
| 77 | case Error::kInvalidRedzone: |
| 78 | timing_.IncrementError(timing::SendError::INVALID_REDZONE); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 83 | RawFetcher::RawFetcher(EventLoop *event_loop, const Channel *channel) |
| 84 | : event_loop_(event_loop), |
| 85 | channel_(channel), |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 86 | ftrace_prefix_(configuration::StrippedChannelToString(channel)), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 87 | timing_(event_loop_->ChannelIndex(channel)) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 88 | context_.monotonic_event_time = monotonic_clock::min_time; |
| 89 | context_.monotonic_remote_time = monotonic_clock::min_time; |
| 90 | context_.realtime_event_time = realtime_clock::min_time; |
| 91 | context_.realtime_remote_time = realtime_clock::min_time; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 92 | context_.queue_index = 0xffffffff; |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 93 | context_.remote_queue_index = 0xffffffffu; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 94 | context_.size = 0; |
| 95 | context_.data = nullptr; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 96 | context_.buffer_index = -1; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 97 | event_loop_->NewFetcher(this); |
| 98 | } |
| 99 | |
| 100 | RawFetcher::~RawFetcher() { event_loop_->DeleteFetcher(this); } |
| 101 | |
| 102 | TimerHandler::TimerHandler(EventLoop *event_loop, std::function<void()> fn) |
| 103 | : event_loop_(event_loop), fn_(std::move(fn)) {} |
| 104 | |
| 105 | TimerHandler::~TimerHandler() {} |
| 106 | |
| 107 | PhasedLoopHandler::PhasedLoopHandler(EventLoop *event_loop, |
| 108 | std::function<void(int)> fn, |
| 109 | const monotonic_clock::duration interval, |
| 110 | const monotonic_clock::duration offset) |
| 111 | : event_loop_(event_loop), |
| 112 | fn_(std::move(fn)), |
| 113 | phased_loop_(interval, event_loop_->monotonic_now(), offset) { |
| 114 | event_loop_->OnRun([this]() { |
| 115 | const monotonic_clock::time_point monotonic_now = |
| 116 | event_loop_->monotonic_now(); |
| 117 | phased_loop_.Reset(monotonic_now); |
| 118 | Reschedule( |
| 119 | [this](monotonic_clock::time_point sleep_time) { |
| 120 | Schedule(sleep_time); |
| 121 | }, |
| 122 | monotonic_now); |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 123 | // Reschedule here will count cycles elapsed before now, and then the |
| 124 | // reschedule before running the handler will count the time that elapsed |
| 125 | // then. So clear the count here. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 126 | cycles_elapsed_ = 0; |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | PhasedLoopHandler::~PhasedLoopHandler() {} |
| 131 | |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 132 | EventLoop::EventLoop(const Configuration *configuration) |
| 133 | : timing_report_(flatbuffers::DetachedBuffer()), |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 134 | configuration_(configuration) {} |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 135 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 136 | EventLoop::~EventLoop() { |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 137 | if (!senders_.empty()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 138 | for (const RawSender *sender : senders_) { |
| 139 | LOG(ERROR) << " Sender " |
| 140 | << configuration::StrippedChannelToString(sender->channel()) |
| 141 | << " still open"; |
| 142 | } |
| 143 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 144 | CHECK_EQ(senders_.size(), 0u) << ": Not all senders destroyed"; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 145 | CHECK_EQ(events_.size(), 0u) << ": Not all events unregistered"; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 148 | void EventLoop::SkipTimingReport() { |
| 149 | skip_timing_report_ = true; |
| 150 | timing_report_ = flatbuffers::DetachedBuffer(); |
| 151 | |
| 152 | for (size_t i = 0; i < timers_.size(); ++i) { |
| 153 | timers_[i]->timing_.set_timing_report(nullptr); |
| 154 | } |
| 155 | |
| 156 | for (size_t i = 0; i < phased_loops_.size(); ++i) { |
| 157 | phased_loops_[i]->timing_.set_timing_report(nullptr); |
| 158 | } |
| 159 | |
| 160 | for (size_t i = 0; i < watchers_.size(); ++i) { |
| 161 | watchers_[i]->set_timing_report(nullptr); |
| 162 | } |
| 163 | |
| 164 | for (size_t i = 0; i < senders_.size(); ++i) { |
| 165 | senders_[i]->timing_.set_timing_report(nullptr); |
| 166 | } |
| 167 | |
| 168 | for (size_t i = 0; i < fetchers_.size(); ++i) { |
| 169 | fetchers_[i]->timing_.set_timing_report(nullptr); |
| 170 | } |
| 171 | } |
| 172 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 173 | int EventLoop::ChannelIndex(const Channel *channel) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 174 | return configuration::ChannelIndex(configuration_, channel); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 177 | WatcherState *EventLoop::GetWatcherState(const Channel *channel) { |
| 178 | const int channel_index = ChannelIndex(channel); |
| 179 | for (const std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 180 | if (watcher->channel_index() == channel_index) { |
| 181 | return watcher.get(); |
| 182 | } |
| 183 | } |
| 184 | LOG(FATAL) << "No watcher found for channel"; |
| 185 | } |
| 186 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 187 | void EventLoop::NewSender(RawSender *sender) { |
| 188 | senders_.emplace_back(sender); |
| 189 | UpdateTimingReport(); |
| 190 | } |
| 191 | void EventLoop::DeleteSender(RawSender *sender) { |
| 192 | CHECK(!is_running()); |
| 193 | auto s = std::find(senders_.begin(), senders_.end(), sender); |
| 194 | CHECK(s != senders_.end()) << ": Sender not in senders list"; |
| 195 | senders_.erase(s); |
| 196 | UpdateTimingReport(); |
| 197 | } |
| 198 | |
| 199 | TimerHandler *EventLoop::NewTimer(std::unique_ptr<TimerHandler> timer) { |
| 200 | timers_.emplace_back(std::move(timer)); |
| 201 | UpdateTimingReport(); |
| 202 | return timers_.back().get(); |
| 203 | } |
| 204 | |
| 205 | PhasedLoopHandler *EventLoop::NewPhasedLoop( |
| 206 | std::unique_ptr<PhasedLoopHandler> phased_loop) { |
| 207 | phased_loops_.emplace_back(std::move(phased_loop)); |
| 208 | UpdateTimingReport(); |
| 209 | return phased_loops_.back().get(); |
| 210 | } |
| 211 | |
| 212 | void EventLoop::NewFetcher(RawFetcher *fetcher) { |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 213 | CheckAlignment(fetcher->channel()); |
| 214 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 215 | fetchers_.emplace_back(fetcher); |
| 216 | UpdateTimingReport(); |
| 217 | } |
| 218 | |
| 219 | void EventLoop::DeleteFetcher(RawFetcher *fetcher) { |
| 220 | CHECK(!is_running()); |
| 221 | auto f = std::find(fetchers_.begin(), fetchers_.end(), fetcher); |
| 222 | CHECK(f != fetchers_.end()) << ": Fetcher not in fetchers list"; |
| 223 | fetchers_.erase(f); |
| 224 | UpdateTimingReport(); |
| 225 | } |
| 226 | |
| 227 | WatcherState *EventLoop::NewWatcher(std::unique_ptr<WatcherState> watcher) { |
| 228 | watchers_.emplace_back(std::move(watcher)); |
| 229 | |
| 230 | UpdateTimingReport(); |
| 231 | |
| 232 | return watchers_.back().get(); |
| 233 | } |
| 234 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 235 | void EventLoop::TakeWatcher(const Channel *channel) { |
| 236 | CHECK(!is_running()) << ": Cannot add new objects while running."; |
| 237 | ChannelIndex(channel); |
| 238 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 239 | CheckAlignment(channel); |
| 240 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 241 | CHECK(taken_senders_.find(channel) == taken_senders_.end()) |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 242 | << ": " << configuration::CleanedChannelToString(channel) |
| 243 | << " is already being used."; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 244 | |
| 245 | auto result = taken_watchers_.insert(channel); |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 246 | CHECK(result.second) << ": " << configuration::CleanedChannelToString(channel) |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 247 | << " is already being used."; |
| 248 | |
| 249 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 250 | LOG(FATAL) << ": " << configuration::CleanedChannelToString(channel) |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 251 | << " is not able to be watched on this node. Check your " |
| 252 | "configuration."; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void EventLoop::TakeSender(const Channel *channel) { |
| 257 | CHECK(!is_running()) << ": Cannot add new objects while running."; |
| 258 | ChannelIndex(channel); |
| 259 | |
Austin Schuh | d54780b | 2020-10-03 16:26:02 -0700 | [diff] [blame] | 260 | CheckAlignment(channel); |
| 261 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 262 | CHECK(taken_watchers_.find(channel) == taken_watchers_.end()) |
Austin Schuh | 8072f92 | 2020-02-16 21:51:47 -0800 | [diff] [blame] | 263 | << ": Channel " << configuration::CleanedChannelToString(channel) |
| 264 | << " is already being used."; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 265 | |
| 266 | // We don't care if this is a duplicate. |
| 267 | taken_senders_.insert(channel); |
| 268 | } |
| 269 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 270 | void EventLoop::SendTimingReport() { |
Brian Silverman | ce418d0 | 2021-11-03 11:25:52 -0700 | [diff] [blame] | 271 | if (!timing_report_sender_) { |
| 272 | // Timing reports are disabled, so nothing for us to do. |
| 273 | return; |
| 274 | } |
| 275 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 276 | // We need to do a fancy dance here to get all the accounting to work right. |
| 277 | // We want to copy the memory here, but then send after resetting. Otherwise |
| 278 | // the send for the timing report won't be counted in the timing report. |
| 279 | // |
| 280 | // Also, flatbuffers build from the back end. So place this at the back end |
| 281 | // of the buffer. We only have to care because we are using this in a very |
| 282 | // raw fashion. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 283 | CHECK_LE(timing_report_.span().size(), timing_report_sender_->size()) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 284 | << ": Timing report bigger than the sender size."; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 285 | std::copy(timing_report_.span().data(), |
| 286 | timing_report_.span().data() + timing_report_.span().size(), |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 287 | reinterpret_cast<uint8_t *>(timing_report_sender_->data()) + |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 288 | timing_report_sender_->size() - timing_report_.span().size()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 289 | |
| 290 | for (const std::unique_ptr<TimerHandler> &timer : timers_) { |
| 291 | timer->timing_.ResetTimingReport(); |
| 292 | } |
| 293 | for (const std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 294 | watcher->ResetReport(); |
| 295 | } |
| 296 | for (const std::unique_ptr<PhasedLoopHandler> &phased_loop : phased_loops_) { |
| 297 | phased_loop->timing_.ResetTimingReport(); |
| 298 | } |
| 299 | for (RawSender *sender : senders_) { |
| 300 | sender->timing_.ResetTimingReport(); |
| 301 | } |
| 302 | for (RawFetcher *fetcher : fetchers_) { |
| 303 | fetcher->timing_.ResetTimingReport(); |
| 304 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 305 | // TODO(milind): If we fail to send, we don't want to reset the timing report. |
| 306 | // We would need to move the reset after the send, and then find the correct |
| 307 | // timing report and set the reports with it instead of letting the sender do |
| 308 | // this. If we failed to send, we wouldn't reset or set the reports, so they |
| 309 | // can accumalate until the next send. |
| 310 | timing_report_failure_counter_.Count( |
| 311 | timing_report_sender_->Send(timing_report_.span().size())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void EventLoop::UpdateTimingReport() { |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 315 | if (skip_timing_report_) { |
| 316 | return; |
| 317 | } |
| 318 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 319 | // We need to support senders and fetchers changing while we are setting up |
| 320 | // the event loop. Otherwise we can't fetch or send until the loop runs. This |
| 321 | // means that on each change, we need to redo all this work. This makes setup |
| 322 | // more expensive, but not by all that much on a modern processor. |
| 323 | |
| 324 | // Now, build up a report with everything pre-filled out. |
| 325 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 326 | fbb.ForceDefaults(true); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 327 | |
| 328 | // Pre-fill in the defaults for timers. |
| 329 | std::vector<flatbuffers::Offset<timing::Timer>> timer_offsets; |
| 330 | for (const std::unique_ptr<TimerHandler> &timer : timers_) { |
| 331 | flatbuffers::Offset<timing::Statistic> wakeup_latency_offset = |
| 332 | timing::CreateStatistic(fbb); |
| 333 | flatbuffers::Offset<timing::Statistic> handler_time_offset = |
| 334 | timing::CreateStatistic(fbb); |
| 335 | flatbuffers::Offset<flatbuffers::String> name_offset; |
| 336 | if (timer->name().size() != 0) { |
| 337 | name_offset = fbb.CreateString(timer->name()); |
| 338 | } |
| 339 | |
| 340 | timing::Timer::Builder timer_builder(fbb); |
| 341 | |
| 342 | if (timer->name().size() != 0) { |
| 343 | timer_builder.add_name(name_offset); |
| 344 | } |
| 345 | timer_builder.add_wakeup_latency(wakeup_latency_offset); |
| 346 | timer_builder.add_handler_time(handler_time_offset); |
| 347 | timer_builder.add_count(0); |
| 348 | timer_offsets.emplace_back(timer_builder.Finish()); |
| 349 | } |
| 350 | |
| 351 | // Pre-fill in the defaults for phased_loops. |
| 352 | std::vector<flatbuffers::Offset<timing::Timer>> phased_loop_offsets; |
| 353 | for (const std::unique_ptr<PhasedLoopHandler> &phased_loop : phased_loops_) { |
| 354 | flatbuffers::Offset<timing::Statistic> wakeup_latency_offset = |
| 355 | timing::CreateStatistic(fbb); |
| 356 | flatbuffers::Offset<timing::Statistic> handler_time_offset = |
| 357 | timing::CreateStatistic(fbb); |
| 358 | flatbuffers::Offset<flatbuffers::String> name_offset; |
| 359 | if (phased_loop->name().size() != 0) { |
| 360 | name_offset = fbb.CreateString(phased_loop->name()); |
| 361 | } |
| 362 | |
| 363 | timing::Timer::Builder timer_builder(fbb); |
| 364 | |
| 365 | if (phased_loop->name().size() != 0) { |
| 366 | timer_builder.add_name(name_offset); |
| 367 | } |
| 368 | timer_builder.add_wakeup_latency(wakeup_latency_offset); |
| 369 | timer_builder.add_handler_time(handler_time_offset); |
| 370 | timer_builder.add_count(0); |
| 371 | phased_loop_offsets.emplace_back(timer_builder.Finish()); |
| 372 | } |
| 373 | |
| 374 | // Pre-fill in the defaults for watchers. |
| 375 | std::vector<flatbuffers::Offset<timing::Watcher>> watcher_offsets; |
| 376 | for (const std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 377 | flatbuffers::Offset<timing::Statistic> wakeup_latency_offset = |
| 378 | timing::CreateStatistic(fbb); |
| 379 | flatbuffers::Offset<timing::Statistic> handler_time_offset = |
| 380 | timing::CreateStatistic(fbb); |
| 381 | |
| 382 | timing::Watcher::Builder watcher_builder(fbb); |
| 383 | |
| 384 | watcher_builder.add_channel_index(watcher->channel_index()); |
| 385 | watcher_builder.add_wakeup_latency(wakeup_latency_offset); |
| 386 | watcher_builder.add_handler_time(handler_time_offset); |
| 387 | watcher_builder.add_count(0); |
| 388 | watcher_offsets.emplace_back(watcher_builder.Finish()); |
| 389 | } |
| 390 | |
| 391 | // Pre-fill in the defaults for senders. |
| 392 | std::vector<flatbuffers::Offset<timing::Sender>> sender_offsets; |
James Kuszmaul | cc94ed4 | 2022-08-24 11:36:17 -0700 | [diff] [blame] | 393 | for (RawSender *sender : senders_) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 394 | flatbuffers::Offset<timing::Statistic> size_offset = |
| 395 | timing::CreateStatistic(fbb); |
| 396 | |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame] | 397 | const flatbuffers::Offset< |
| 398 | flatbuffers::Vector<flatbuffers::Offset<timing::SendErrorCount>>> |
James Kuszmaul | cc94ed4 | 2022-08-24 11:36:17 -0700 | [diff] [blame] | 399 | error_counts_offset = sender->timing_.error_counter.Initialize(&fbb); |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame] | 400 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 401 | timing::Sender::Builder sender_builder(fbb); |
| 402 | |
| 403 | sender_builder.add_channel_index(sender->timing_.channel_index); |
| 404 | sender_builder.add_size(size_offset); |
James Kuszmaul | 7851433 | 2022-04-06 15:08:34 -0700 | [diff] [blame] | 405 | sender_builder.add_error_counts(error_counts_offset); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 406 | sender_builder.add_count(0); |
| 407 | sender_offsets.emplace_back(sender_builder.Finish()); |
| 408 | } |
| 409 | |
| 410 | // Pre-fill in the defaults for fetchers. |
| 411 | std::vector<flatbuffers::Offset<timing::Fetcher>> fetcher_offsets; |
| 412 | for (RawFetcher *fetcher : fetchers_) { |
| 413 | flatbuffers::Offset<timing::Statistic> latency_offset = |
| 414 | timing::CreateStatistic(fbb); |
| 415 | |
| 416 | timing::Fetcher::Builder fetcher_builder(fbb); |
| 417 | |
| 418 | fetcher_builder.add_channel_index(fetcher->timing_.channel_index); |
| 419 | fetcher_builder.add_count(0); |
| 420 | fetcher_builder.add_latency(latency_offset); |
| 421 | fetcher_offsets.emplace_back(fetcher_builder.Finish()); |
| 422 | } |
| 423 | |
| 424 | // Then build the final report. |
| 425 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Timer>>> |
| 426 | timers_offset; |
| 427 | if (timer_offsets.size() > 0) { |
| 428 | timers_offset = fbb.CreateVector(timer_offsets); |
| 429 | } |
| 430 | |
| 431 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Timer>>> |
| 432 | phased_loops_offset; |
| 433 | if (phased_loop_offsets.size() > 0) { |
| 434 | phased_loops_offset = fbb.CreateVector(phased_loop_offsets); |
| 435 | } |
| 436 | |
| 437 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Watcher>>> |
| 438 | watchers_offset; |
| 439 | if (watcher_offsets.size() > 0) { |
| 440 | watchers_offset = fbb.CreateVector(watcher_offsets); |
| 441 | } |
| 442 | |
| 443 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Sender>>> |
| 444 | senders_offset; |
| 445 | if (sender_offsets.size() > 0) { |
| 446 | senders_offset = fbb.CreateVector(sender_offsets); |
| 447 | } |
| 448 | |
| 449 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<timing::Fetcher>>> |
| 450 | fetchers_offset; |
| 451 | if (fetcher_offsets.size() > 0) { |
| 452 | fetchers_offset = fbb.CreateVector(fetcher_offsets); |
| 453 | } |
| 454 | |
| 455 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 456 | fbb.CreateString(name()); |
| 457 | |
| 458 | timing::Report::Builder report_builder(fbb); |
| 459 | report_builder.add_name(name_offset); |
| 460 | report_builder.add_pid(GetTid()); |
| 461 | if (timer_offsets.size() > 0) { |
| 462 | report_builder.add_timers(timers_offset); |
| 463 | } |
| 464 | if (phased_loop_offsets.size() > 0) { |
| 465 | report_builder.add_phased_loops(phased_loops_offset); |
| 466 | } |
| 467 | if (watcher_offsets.size() > 0) { |
| 468 | report_builder.add_watchers(watchers_offset); |
| 469 | } |
| 470 | if (sender_offsets.size() > 0) { |
| 471 | report_builder.add_senders(senders_offset); |
| 472 | } |
| 473 | if (fetcher_offsets.size() > 0) { |
| 474 | report_builder.add_fetchers(fetchers_offset); |
| 475 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 476 | report_builder.add_send_failures(timing_report_failure_counter_.failures()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 477 | fbb.Finish(report_builder.Finish()); |
| 478 | |
| 479 | timing_report_ = FlatbufferDetachedBuffer<timing::Report>(fbb.Release()); |
| 480 | |
| 481 | // Now that the pointers are stable, pass them to the timers and watchers to |
| 482 | // be updated. |
| 483 | for (size_t i = 0; i < timers_.size(); ++i) { |
| 484 | timers_[i]->timing_.set_timing_report( |
| 485 | timing_report_.mutable_message()->mutable_timers()->GetMutableObject( |
| 486 | i)); |
| 487 | } |
| 488 | |
| 489 | for (size_t i = 0; i < phased_loops_.size(); ++i) { |
| 490 | phased_loops_[i]->timing_.set_timing_report( |
| 491 | timing_report_.mutable_message() |
| 492 | ->mutable_phased_loops() |
| 493 | ->GetMutableObject(i)); |
| 494 | } |
| 495 | |
| 496 | for (size_t i = 0; i < watchers_.size(); ++i) { |
| 497 | watchers_[i]->set_timing_report( |
| 498 | timing_report_.mutable_message()->mutable_watchers()->GetMutableObject( |
| 499 | i)); |
| 500 | } |
| 501 | |
| 502 | for (size_t i = 0; i < senders_.size(); ++i) { |
| 503 | senders_[i]->timing_.set_timing_report( |
| 504 | timing_report_.mutable_message()->mutable_senders()->GetMutableObject( |
| 505 | i)); |
| 506 | } |
| 507 | |
| 508 | for (size_t i = 0; i < fetchers_.size(); ++i) { |
| 509 | fetchers_[i]->timing_.set_timing_report( |
| 510 | timing_report_.mutable_message()->mutable_fetchers()->GetMutableObject( |
| 511 | i)); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void EventLoop::MaybeScheduleTimingReports() { |
| 516 | if (FLAGS_timing_reports && !skip_timing_report_) { |
| 517 | CHECK(!timing_report_sender_) << ": Timing reports already scheduled."; |
| 518 | // Make a raw sender for the report. |
| 519 | const Channel *channel = configuration::GetChannel( |
| 520 | configuration(), "/aos", timing::Report::GetFullyQualifiedName(), |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 521 | name(), node()); |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 522 | CHECK(channel != nullptr) << ": Failed to look up {\"name\": \"/aos\", " |
| 523 | "\"type\": \"aos.timing.Report\"} on node " |
| 524 | << FlatbufferToJson(node()); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 525 | |
| 526 | // Since we are using a RawSender, validity isn't checked. So check it |
| 527 | // ourselves. |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 528 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 529 | LOG(FATAL) << "Channel { \"name\": \"/aos" |
| 530 | << channel->name()->string_view() << "\", \"type\": \"" |
| 531 | << channel->type()->string_view() |
| 532 | << "\" } is not able to be sent on this node. Check your " |
| 533 | "configuration."; |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 534 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 535 | CHECK(channel != nullptr) << ": Channel { \"name\": \"/aos\", \"type\": \"" |
| 536 | << timing::Report::GetFullyQualifiedName() |
| 537 | << "\" } not found in config."; |
| 538 | timing_report_sender_ = MakeRawSender(channel); |
| 539 | |
| 540 | // Register a handler which sends the report out by copying the raw data |
| 541 | // from the prebuilt and subsequently modified report. |
| 542 | TimerHandler *timing_reports_timer = |
| 543 | AddTimer([this]() { SendTimingReport(); }); |
| 544 | |
| 545 | // Set it up to send once per second. |
| 546 | timing_reports_timer->set_name("timing_reports"); |
| 547 | OnRun([this, timing_reports_timer]() { |
| 548 | timing_reports_timer->Setup( |
| 549 | monotonic_now() + std::chrono::milliseconds(FLAGS_timing_report_ms), |
| 550 | std::chrono::milliseconds(FLAGS_timing_report_ms)); |
| 551 | }); |
| 552 | |
| 553 | UpdateTimingReport(); |
| 554 | } |
| 555 | } |
| 556 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 557 | void EventLoop::ReserveEvents() { |
| 558 | events_.reserve(timers_.size() + phased_loops_.size() + watchers_.size()); |
| 559 | } |
| 560 | |
| 561 | namespace { |
| 562 | bool CompareEvents(const EventLoopEvent *first, const EventLoopEvent *second) { |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 563 | if (first->event_time() > second->event_time()) { |
| 564 | return true; |
| 565 | } |
| 566 | if (first->event_time() < second->event_time()) { |
| 567 | return false; |
| 568 | } |
| 569 | return first->generation() > second->generation(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 570 | } |
| 571 | } // namespace |
| 572 | |
| 573 | void EventLoop::AddEvent(EventLoopEvent *event) { |
| 574 | DCHECK(std::find(events_.begin(), events_.end(), event) == events_.end()); |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 575 | DCHECK(event->generation() == 0); |
| 576 | event->set_generation(++event_generation_); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 577 | events_.push_back(event); |
| 578 | std::push_heap(events_.begin(), events_.end(), CompareEvents); |
| 579 | } |
| 580 | |
| 581 | void EventLoop::RemoveEvent(EventLoopEvent *event) { |
| 582 | auto e = std::find(events_.begin(), events_.end(), event); |
| 583 | if (e != events_.end()) { |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 584 | DCHECK(event->generation() != 0); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 585 | events_.erase(e); |
| 586 | std::make_heap(events_.begin(), events_.end(), CompareEvents); |
| 587 | event->Invalidate(); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | EventLoopEvent *EventLoop::PopEvent() { |
| 592 | EventLoopEvent *result = events_.front(); |
| 593 | std::pop_heap(events_.begin(), events_.end(), CompareEvents); |
| 594 | events_.pop_back(); |
| 595 | result->Invalidate(); |
| 596 | return result; |
| 597 | } |
| 598 | |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 599 | void EventLoop::ClearContext() { |
| 600 | context_.monotonic_event_time = monotonic_clock::min_time; |
| 601 | context_.monotonic_remote_time = monotonic_clock::min_time; |
| 602 | context_.realtime_event_time = realtime_clock::min_time; |
| 603 | context_.realtime_remote_time = realtime_clock::min_time; |
| 604 | context_.queue_index = 0xffffffffu; |
| 605 | context_.remote_queue_index = 0xffffffffu; |
| 606 | context_.size = 0u; |
| 607 | context_.data = nullptr; |
| 608 | context_.buffer_index = -1; |
| 609 | context_.source_boot_uuid = boot_uuid(); |
| 610 | } |
| 611 | |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 612 | void EventLoop::SetTimerContext( |
| 613 | monotonic_clock::time_point monotonic_event_time) { |
| 614 | context_.monotonic_event_time = monotonic_event_time; |
| 615 | context_.monotonic_remote_time = monotonic_clock::min_time; |
| 616 | context_.realtime_event_time = realtime_clock::min_time; |
| 617 | context_.realtime_remote_time = realtime_clock::min_time; |
| 618 | context_.queue_index = 0xffffffffu; |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 619 | context_.remote_queue_index = 0xffffffffu; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 620 | context_.size = 0u; |
| 621 | context_.data = nullptr; |
| 622 | context_.buffer_index = -1; |
| 623 | context_.source_boot_uuid = boot_uuid(); |
| 624 | } |
| 625 | |
Austin Schuh | 070019a | 2022-12-20 22:23:09 -0800 | [diff] [blame^] | 626 | cpu_set_t EventLoop::DefaultAffinity() { return aos::DefaultAffinity(); } |
| 627 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 628 | void WatcherState::set_timing_report(timing::Watcher *watcher) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 629 | watcher_ = watcher; |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 630 | if (!watcher) { |
| 631 | wakeup_latency_.set_statistic(nullptr); |
| 632 | handler_time_.set_statistic(nullptr); |
| 633 | } else { |
| 634 | wakeup_latency_.set_statistic(watcher->mutable_wakeup_latency()); |
| 635 | handler_time_.set_statistic(watcher->mutable_handler_time()); |
| 636 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void WatcherState::ResetReport() { |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 640 | if (!watcher_) { |
| 641 | return; |
| 642 | } |
| 643 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 644 | wakeup_latency_.Reset(); |
| 645 | handler_time_.Reset(); |
| 646 | watcher_->mutate_count(0); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | } // namespace aos |