Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_EVENT_LOOP_TMPL_H_ |
| 2 | #define AOS_EVENTS_EVENT_LOOP_TMPL_H_ |
| 3 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 4 | #include <inttypes.h> |
| 5 | #include <stdint.h> |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include <type_traits> |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 8 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include "aos/events/event_loop.h" |
| 10 | #include "glog/logging.h" |
| 11 | |
| 12 | namespace aos { |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 13 | namespace event_loop_internal { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 15 | // From a watch functor, specializations of this will extract the message type |
| 16 | // of the template argument. If T is not a valid message type, there will be no |
| 17 | // matching specialization. |
| 18 | // |
| 19 | // This is just the forward declaration, which will be used by one of the |
| 20 | // following specializations to match valid argument types. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | template <class T> |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 22 | struct watch_message_type_trait; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | |
| 24 | // From a watch functor, this will extract the message type of the argument. |
| 25 | // This is the template specialization. |
| 26 | template <class ClassType, class ReturnType, class A1> |
| 27 | struct watch_message_type_trait<ReturnType (ClassType::*)(A1) const> { |
| 28 | using message_type = typename std::decay<A1>::type; |
| 29 | }; |
| 30 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 31 | } // namespace event_loop_internal |
| 32 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | template <typename T> |
| 34 | typename Sender<T>::Builder Sender<T>::MakeBuilder() { |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 35 | return Builder(sender_.get(), sender_->fbb_allocator()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | template <typename Watch> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 39 | void EventLoop::MakeWatcher(const std::string_view channel_name, Watch &&w) { |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 40 | using MessageType = |
| 41 | typename event_loop_internal::watch_message_type_trait<decltype( |
| 42 | &Watch::operator())>::message_type; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 43 | const Channel *channel = configuration::GetChannel( |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 44 | configuration_, channel_name, MessageType::GetFullyQualifiedName(), |
| 45 | name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | |
| 47 | CHECK(channel != nullptr) |
| 48 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
Austin Schuh | c59a1de | 2021-02-20 14:47:39 -0800 | [diff] [blame] | 49 | << MessageType::GetFullyQualifiedName() |
| 50 | << "\" } not found in config for application " << name() << "."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 51 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 52 | MakeRawWatcher(channel, |
| 53 | [this, w](const Context &context, const void *message) { |
| 54 | context_ = context; |
| 55 | w(*flatbuffers::GetRoot<MessageType>( |
| 56 | reinterpret_cast<const char *>(message))); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | template <typename MessageType> |
| 61 | void EventLoop::MakeNoArgWatcher(const std::string_view channel_name, |
| 62 | std::function<void()> w) { |
| 63 | const Channel *channel = configuration::GetChannel( |
| 64 | configuration_, channel_name, MessageType::GetFullyQualifiedName(), |
| 65 | name(), node()); |
| 66 | CHECK(channel != nullptr) |
| 67 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
Austin Schuh | c59a1de | 2021-02-20 14:47:39 -0800 | [diff] [blame] | 68 | << MessageType::GetFullyQualifiedName() |
| 69 | << "\" } not found in config for application " << name() << "."; |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 70 | MakeRawNoArgWatcher(channel, [this, w](const Context &context) { |
| 71 | context_ = context; |
| 72 | w(); |
| 73 | }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 76 | inline bool RawFetcher::FetchNext() { |
| 77 | const auto result = DoFetchNext(); |
| 78 | if (result.first) { |
| 79 | timing_.fetcher->mutate_count(timing_.fetcher->count() + 1); |
| 80 | const monotonic_clock::time_point monotonic_time = result.second; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 81 | ftrace_.FormatMessage( |
| 82 | "%.*s: fetch next: now=%" PRId64 " event=%" PRId64 " queue=%" PRIu32, |
| 83 | static_cast<int>(ftrace_prefix_.size()), ftrace_prefix_.data(), |
| 84 | static_cast<int64_t>(monotonic_time.time_since_epoch().count()), |
| 85 | static_cast<int64_t>( |
| 86 | context_.monotonic_event_time.time_since_epoch().count()), |
| 87 | context_.queue_index); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 88 | const float latency = |
| 89 | std::chrono::duration_cast<std::chrono::duration<float>>( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 90 | monotonic_time - context_.monotonic_event_time) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 91 | .count(); |
| 92 | timing_.latency.Add(latency); |
| 93 | return true; |
| 94 | } |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 95 | ftrace_.FormatMessage( |
| 96 | "%.*s: fetch next: still event=%" PRId64 " queue=%" PRIu32, |
| 97 | static_cast<int>(ftrace_prefix_.size()), ftrace_prefix_.data(), |
| 98 | static_cast<int64_t>( |
| 99 | context_.monotonic_event_time.time_since_epoch().count()), |
| 100 | context_.queue_index); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | |
| 104 | inline bool RawFetcher::Fetch() { |
| 105 | const auto result = DoFetch(); |
| 106 | if (result.first) { |
| 107 | timing_.fetcher->mutate_count(timing_.fetcher->count() + 1); |
| 108 | const monotonic_clock::time_point monotonic_time = result.second; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 109 | ftrace_.FormatMessage( |
| 110 | "%.*s: fetch latest: now=%" PRId64 " event=%" PRId64 " queue=%" PRIu32, |
| 111 | static_cast<int>(ftrace_prefix_.size()), ftrace_prefix_.data(), |
| 112 | static_cast<int64_t>(monotonic_time.time_since_epoch().count()), |
| 113 | static_cast<int64_t>( |
| 114 | context_.monotonic_event_time.time_since_epoch().count()), |
| 115 | context_.queue_index); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 116 | const float latency = |
| 117 | std::chrono::duration_cast<std::chrono::duration<float>>( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 118 | monotonic_time - context_.monotonic_event_time) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 119 | .count(); |
| 120 | timing_.latency.Add(latency); |
| 121 | return true; |
| 122 | } |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 123 | ftrace_.FormatMessage( |
| 124 | "%.*s: fetch latest: still event=%" PRId64 " queue=%" PRIu32, |
| 125 | static_cast<int>(ftrace_prefix_.size()), ftrace_prefix_.data(), |
| 126 | static_cast<int64_t>( |
| 127 | context_.monotonic_event_time.time_since_epoch().count()), |
| 128 | context_.queue_index); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 132 | inline bool RawSender::Send(size_t size) { |
| 133 | return Send(size, monotonic_clock::min_time, realtime_clock::min_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 134 | 0xffffffffu, event_loop_->boot_uuid()); |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 137 | inline bool RawSender::Send( |
| 138 | size_t size, aos::monotonic_clock::time_point monotonic_remote_time, |
| 139 | aos::realtime_clock::time_point realtime_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 140 | uint32_t remote_queue_index, const UUID &uuid) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 141 | if (DoSend(size, monotonic_remote_time, realtime_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 142 | remote_queue_index, uuid)) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 143 | timing_.size.Add(size); |
| 144 | timing_.sender->mutate_count(timing_.sender->count() + 1); |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 145 | ftrace_.FormatMessage( |
| 146 | "%.*s: sent internal: event=%" PRId64 " queue=%" PRIu32, |
| 147 | static_cast<int>(ftrace_prefix_.size()), ftrace_prefix_.data(), |
| 148 | static_cast<int64_t>(monotonic_sent_time().time_since_epoch().count()), |
| 149 | sent_queue_index()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 155 | inline bool RawSender::Send(const void *data, size_t size) { |
| 156 | return Send(data, size, monotonic_clock::min_time, realtime_clock::min_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 157 | 0xffffffffu, event_loop_->boot_uuid()); |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 160 | inline bool RawSender::Send( |
| 161 | const void *data, size_t size, |
| 162 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 163 | aos::realtime_clock::time_point realtime_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 164 | uint32_t remote_queue_index, const UUID &uuid) { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 165 | if (DoSend(data, size, monotonic_remote_time, realtime_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 166 | remote_queue_index, uuid)) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 167 | timing_.size.Add(size); |
| 168 | timing_.sender->mutate_count(timing_.sender->count() + 1); |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 169 | ftrace_.FormatMessage( |
| 170 | "%.*s: sent external: event=%" PRId64 " queue=%" PRIu32, |
| 171 | static_cast<int>(ftrace_prefix_.size()), ftrace_prefix_.data(), |
| 172 | static_cast<int64_t>(monotonic_sent_time().time_since_epoch().count()), |
| 173 | sent_queue_index()); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 179 | inline monotonic_clock::time_point TimerHandler::Call( |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 180 | std::function<monotonic_clock::time_point()> get_time, |
| 181 | monotonic_clock::time_point event_time) { |
| 182 | CHECK_NOTNULL(timing_.timer); |
| 183 | const monotonic_clock::time_point monotonic_start_time = get_time(); |
| 184 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 185 | event_loop_->context_.monotonic_event_time = event_time; |
| 186 | event_loop_->context_.monotonic_remote_time = monotonic_clock::min_time; |
| 187 | event_loop_->context_.realtime_remote_time = |
| 188 | event_loop_->context_.realtime_event_time = realtime_clock::min_time; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 189 | event_loop_->context_.queue_index = 0xffffffffu; |
| 190 | event_loop_->context_.size = 0; |
| 191 | event_loop_->context_.data = nullptr; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 192 | event_loop_->context_.buffer_index = -1; |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 193 | event_loop_->context_.remote_boot_uuid = event_loop_->boot_uuid(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 194 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 195 | ftrace_.FormatMessage( |
| 196 | "timer: %.*s: start now=%" PRId64 " event=%" PRId64, |
| 197 | static_cast<int>(name_.size()), name_.data(), |
| 198 | static_cast<int64_t>(monotonic_start_time.time_since_epoch().count()), |
| 199 | static_cast<int64_t>(event_time.time_since_epoch().count())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 200 | { |
| 201 | const float start_latency = |
| 202 | std::chrono::duration_cast<std::chrono::duration<float>>( |
| 203 | monotonic_start_time - event_time) |
| 204 | .count(); |
| 205 | timing_.wakeup_latency.Add(start_latency); |
| 206 | } |
| 207 | timing_.timer->mutate_count(timing_.timer->count() + 1); |
| 208 | fn_(); |
| 209 | |
| 210 | const monotonic_clock::time_point monotonic_end_time = get_time(); |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 211 | ftrace_.FormatMessage( |
| 212 | "timer: %.*s: end now=%" PRId64, static_cast<int>(name_.size()), |
| 213 | name_.data(), |
| 214 | static_cast<int64_t>(monotonic_end_time.time_since_epoch().count())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 215 | |
| 216 | const float handler_latency = |
| 217 | std::chrono::duration_cast<std::chrono::duration<float>>( |
| 218 | monotonic_end_time - monotonic_start_time) |
| 219 | .count(); |
| 220 | timing_.handler_time.Add(handler_latency); |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 221 | return monotonic_start_time; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | inline void PhasedLoopHandler::Call( |
| 225 | std::function<monotonic_clock::time_point()> get_time, |
| 226 | std::function<void(monotonic_clock::time_point)> schedule) { |
| 227 | // Read time directly to save a vtable indirection... |
| 228 | const monotonic_clock::time_point monotonic_start_time = get_time(); |
| 229 | |
| 230 | // Update the context to hold the desired wakeup time. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 231 | event_loop_->context_.monotonic_event_time = phased_loop_.sleep_time(); |
| 232 | event_loop_->context_.monotonic_remote_time = monotonic_clock::min_time; |
| 233 | event_loop_->context_.realtime_remote_time = |
| 234 | event_loop_->context_.realtime_event_time = realtime_clock::min_time; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 235 | event_loop_->context_.queue_index = 0xffffffffu; |
| 236 | event_loop_->context_.size = 0; |
| 237 | event_loop_->context_.data = nullptr; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 238 | event_loop_->context_.buffer_index = -1; |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 239 | event_loop_->context_.remote_boot_uuid = event_loop_->boot_uuid(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 240 | |
| 241 | // Compute how many cycles elapsed and schedule the next wakeup. |
| 242 | Reschedule(schedule, monotonic_start_time); |
| 243 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 244 | ftrace_.FormatMessage( |
| 245 | "phased: %.*s: start now=%" PRId64 " event=%" PRId64 " cycles=%d", |
| 246 | static_cast<int>(name_.size()), name_.data(), |
| 247 | static_cast<int64_t>(monotonic_start_time.time_since_epoch().count()), |
| 248 | static_cast<int64_t>( |
| 249 | phased_loop_.sleep_time().time_since_epoch().count()), |
| 250 | cycles_elapsed_); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 251 | { |
| 252 | const float start_latency = |
| 253 | std::chrono::duration_cast<std::chrono::duration<float>>( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 254 | monotonic_start_time - event_loop_->context_.monotonic_event_time) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 255 | .count(); |
| 256 | timing_.wakeup_latency.Add(start_latency); |
| 257 | } |
| 258 | timing_.timer->mutate_count(timing_.timer->count() + 1); |
| 259 | |
| 260 | // Call the function with the elapsed cycles. |
| 261 | fn_(cycles_elapsed_); |
| 262 | cycles_elapsed_ = 0; |
| 263 | |
| 264 | const monotonic_clock::time_point monotonic_end_time = get_time(); |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 265 | ftrace_.FormatMessage( |
| 266 | "phased: %.*s: end now=%" PRId64, static_cast<int>(name_.size()), |
| 267 | name_.data(), |
| 268 | static_cast<int64_t>(monotonic_end_time.time_since_epoch().count())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 269 | |
| 270 | const float handler_latency = |
| 271 | std::chrono::duration_cast<std::chrono::duration<float>>( |
| 272 | monotonic_end_time - monotonic_start_time) |
| 273 | .count(); |
| 274 | timing_.handler_time.Add(handler_latency); |
| 275 | |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 276 | // If the handler took too long so we blew by the previous deadline, we |
Austin Schuh | 91ba639 | 2020-10-03 13:27:47 -0700 | [diff] [blame] | 277 | // want to just try for the next deadline. Reschedule. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 278 | if (monotonic_end_time > phased_loop_.sleep_time()) { |
| 279 | Reschedule(schedule, monotonic_end_time); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Class to automate the timing report generation for watchers. |
| 284 | class WatcherState { |
| 285 | public: |
| 286 | WatcherState( |
| 287 | EventLoop *event_loop, const Channel *channel, |
| 288 | std::function<void(const Context &context, const void *message)> fn) |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 289 | : channel_index_(event_loop->ChannelIndex(channel)), |
| 290 | ftrace_prefix_(configuration::StrippedChannelToString(channel)), |
| 291 | fn_(std::move(fn)) {} |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 292 | |
| 293 | virtual ~WatcherState() {} |
| 294 | |
| 295 | // Calls the callback, measuring time with get_time, with the provided |
| 296 | // context. |
| 297 | void DoCallCallback(std::function<monotonic_clock::time_point()> get_time, |
Austin Schuh | c5dc98f | 2021-06-16 14:52:46 -0700 | [diff] [blame^] | 298 | Context context) noexcept { |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 299 | if (context.data) { |
| 300 | CheckChannelDataAlignment(context.data, context.size); |
| 301 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 302 | const monotonic_clock::time_point monotonic_start_time = get_time(); |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 303 | ftrace_.FormatMessage( |
| 304 | "%.*s: watcher start: now=%" PRId64 " event=%" PRId64 " queue=%" PRIu32, |
| 305 | static_cast<int>(ftrace_prefix_.size()), ftrace_prefix_.data(), |
| 306 | static_cast<int64_t>(monotonic_start_time.time_since_epoch().count()), |
| 307 | static_cast<int64_t>( |
| 308 | context.monotonic_event_time.time_since_epoch().count()), |
| 309 | context.queue_index); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 310 | { |
| 311 | const float start_latency = |
| 312 | std::chrono::duration_cast<std::chrono::duration<float>>( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 313 | monotonic_start_time - context.monotonic_event_time) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 314 | .count(); |
| 315 | wakeup_latency_.Add(start_latency); |
| 316 | } |
| 317 | watcher_->mutate_count(watcher_->count() + 1); |
| 318 | fn_(context, context.data); |
| 319 | |
| 320 | const monotonic_clock::time_point monotonic_end_time = get_time(); |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 321 | ftrace_.FormatMessage( |
| 322 | "%.*s: watcher end: now=%" PRId64, |
| 323 | static_cast<int>(ftrace_prefix_.size()), ftrace_prefix_.data(), |
| 324 | static_cast<int64_t>(monotonic_end_time.time_since_epoch().count())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 325 | |
| 326 | const float handler_latency = |
| 327 | std::chrono::duration_cast<std::chrono::duration<float>>( |
| 328 | monotonic_end_time - monotonic_start_time) |
| 329 | .count(); |
| 330 | handler_time_.Add(handler_latency); |
| 331 | } |
| 332 | |
| 333 | int channel_index() const { return channel_index_; } |
| 334 | |
| 335 | void set_timing_report(timing::Watcher *watcher); |
| 336 | void ResetReport(); |
| 337 | |
| 338 | virtual void Startup(EventLoop *event_loop) = 0; |
| 339 | |
| 340 | protected: |
| 341 | const int channel_index_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 342 | const std::string ftrace_prefix_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 343 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 344 | const std::function<void(const Context &context, const void *message)> fn_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 345 | |
| 346 | internal::TimingStatistic wakeup_latency_; |
| 347 | internal::TimingStatistic handler_time_; |
| 348 | timing::Watcher *watcher_ = nullptr; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 349 | |
| 350 | Ftrace ftrace_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 351 | }; |
| 352 | |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 353 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 354 | bool Sender<T>::Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer) { |
| 355 | return sender_->Send(flatbuffer.span().data(), flatbuffer.span().size()); |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 358 | template <typename T> |
| 359 | bool Sender<T>::SendDetached(FlatbufferDetachedBuffer<T> detached) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 360 | CHECK_EQ(static_cast<void *>(detached.span().data() + detached.span().size() - |
| 361 | sender_->size()), |
| 362 | sender_->data()) |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 363 | << ": May only send the buffer detached from this Sender"; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 364 | return sender_->Send(detached.span().size()); |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 367 | } // namespace aos |
| 368 | |
| 369 | #endif // AOS_EVENTS_EVENT_LOOP_TMPL_H |