blob: 0b485c6a3819204ac5f2f584694a13575c34adf3 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#ifndef AOS_EVENTS_EVENT_LOOP_TMPL_H_
2#define AOS_EVENTS_EVENT_LOOP_TMPL_H_
3
4#include <type_traits>
5#include "aos/events/event_loop.h"
6#include "glog/logging.h"
7
8namespace aos {
9
10// From a watch functor, this will extract the message type of the argument.
11// This is the template forward declaration, and it extracts the call operator
12// as a PTMF to be used by the following specialization.
13template <class T>
14struct watch_message_type_trait
15 : watch_message_type_trait<decltype(&T::operator())> {};
16
17// From a watch functor, this will extract the message type of the argument.
18// This is the template specialization.
19template <class ClassType, class ReturnType, class A1>
20struct watch_message_type_trait<ReturnType (ClassType::*)(A1) const> {
21 using message_type = typename std::decay<A1>::type;
22};
23
24template <typename T>
25typename Sender<T>::Builder Sender<T>::MakeBuilder() {
26 return Builder(sender_.get(), sender_->data(), sender_->size());
27}
28
29template <typename Watch>
James Kuszmaul3ae42262019-11-08 12:33:41 -080030void EventLoop::MakeWatcher(const std::string_view channel_name, Watch &&w) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 using T = typename watch_message_type_trait<Watch>::message_type;
32 const Channel *channel = configuration::GetChannel(
Austin Schuhbca6cf02019-12-22 17:28:34 -080033 configuration_, channel_name, T::GetFullyQualifiedName(), name(), node());
Alex Perrycb7da4b2019-08-28 19:35:56 -070034
35 CHECK(channel != nullptr)
36 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
37 << T::GetFullyQualifiedName() << "\" } not found in config.";
38
Austin Schuh217a9782019-12-21 23:02:50 -080039 if (node() != nullptr) {
40 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
41 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
42 << "\", \"type\": \"" << T::GetFullyQualifiedName()
43 << "\" } is not able to be watched on this node. Check your "
44 "configuration.";
45 }
46 }
47
Alex Perrycb7da4b2019-08-28 19:35:56 -070048 return MakeRawWatcher(
49 channel, [this, w](const Context &context, const void *message) {
50 context_ = context;
51 w(*flatbuffers::GetRoot<T>(reinterpret_cast<const char *>(message)));
52 });
53}
54
Austin Schuh39788ff2019-12-01 18:22:57 -080055inline bool RawFetcher::FetchNext() {
56 const auto result = DoFetchNext();
57 if (result.first) {
58 timing_.fetcher->mutate_count(timing_.fetcher->count() + 1);
59 const monotonic_clock::time_point monotonic_time = result.second;
60 const float latency =
61 std::chrono::duration_cast<std::chrono::duration<float>>(
62 monotonic_time - context_.monotonic_sent_time)
63 .count();
64 timing_.latency.Add(latency);
65 return true;
66 }
67 return false;
68}
69
70inline bool RawFetcher::Fetch() {
71 const auto result = DoFetch();
72 if (result.first) {
73 timing_.fetcher->mutate_count(timing_.fetcher->count() + 1);
74 const monotonic_clock::time_point monotonic_time = result.second;
75 const float latency =
76 std::chrono::duration_cast<std::chrono::duration<float>>(
77 monotonic_time - context_.monotonic_sent_time)
78 .count();
79 timing_.latency.Add(latency);
80 return true;
81 }
82 return false;
83}
84
85inline bool RawSender::Send(size_t size) {
86 if (DoSend(size)) {
87 timing_.size.Add(size);
88 timing_.sender->mutate_count(timing_.sender->count() + 1);
89 return true;
90 }
91 return false;
92}
93
94inline bool RawSender::Send(const void *data, size_t size) {
95 if (DoSend(data, size)) {
96 timing_.size.Add(size);
97 timing_.sender->mutate_count(timing_.sender->count() + 1);
98 return true;
99 }
100 return false;
101}
102
103inline void TimerHandler::Call(
104 std::function<monotonic_clock::time_point()> get_time,
105 monotonic_clock::time_point event_time) {
106 CHECK_NOTNULL(timing_.timer);
107 const monotonic_clock::time_point monotonic_start_time = get_time();
108
109 event_loop_->context_.monotonic_sent_time = event_time;
110 event_loop_->context_.realtime_sent_time = realtime_clock::min_time;
111 event_loop_->context_.queue_index = 0xffffffffu;
112 event_loop_->context_.size = 0;
113 event_loop_->context_.data = nullptr;
114
115 {
116 const float start_latency =
117 std::chrono::duration_cast<std::chrono::duration<float>>(
118 monotonic_start_time - event_time)
119 .count();
120 timing_.wakeup_latency.Add(start_latency);
121 }
122 timing_.timer->mutate_count(timing_.timer->count() + 1);
123 fn_();
124
125 const monotonic_clock::time_point monotonic_end_time = get_time();
126
127 const float handler_latency =
128 std::chrono::duration_cast<std::chrono::duration<float>>(
129 monotonic_end_time - monotonic_start_time)
130 .count();
131 timing_.handler_time.Add(handler_latency);
132}
133
134inline void PhasedLoopHandler::Call(
135 std::function<monotonic_clock::time_point()> get_time,
136 std::function<void(monotonic_clock::time_point)> schedule) {
137 // Read time directly to save a vtable indirection...
138 const monotonic_clock::time_point monotonic_start_time = get_time();
139
140 // Update the context to hold the desired wakeup time.
141 event_loop_->context_.monotonic_sent_time = phased_loop_.sleep_time();
142 event_loop_->context_.realtime_sent_time = realtime_clock::min_time;
143 event_loop_->context_.queue_index = 0xffffffffu;
144 event_loop_->context_.size = 0;
145 event_loop_->context_.data = nullptr;
146
147 // Compute how many cycles elapsed and schedule the next wakeup.
148 Reschedule(schedule, monotonic_start_time);
149
150 {
151 const float start_latency =
152 std::chrono::duration_cast<std::chrono::duration<float>>(
153 monotonic_start_time - event_loop_->context_.monotonic_sent_time)
154 .count();
155 timing_.wakeup_latency.Add(start_latency);
156 }
157 timing_.timer->mutate_count(timing_.timer->count() + 1);
158
159 // Call the function with the elapsed cycles.
160 fn_(cycles_elapsed_);
161 cycles_elapsed_ = 0;
162
163 const monotonic_clock::time_point monotonic_end_time = get_time();
164
165 const float handler_latency =
166 std::chrono::duration_cast<std::chrono::duration<float>>(
167 monotonic_end_time - monotonic_start_time)
168 .count();
169 timing_.handler_time.Add(handler_latency);
170
171 // If the handler too too long so we blew by the previous deadline, we
172 // want to just try for the next deadline. Rescuedule.
173 if (monotonic_end_time > phased_loop_.sleep_time()) {
174 Reschedule(schedule, monotonic_end_time);
175 }
176}
177
178// Class to automate the timing report generation for watchers.
179class WatcherState {
180 public:
181 WatcherState(
182 EventLoop *event_loop, const Channel *channel,
183 std::function<void(const Context &context, const void *message)> fn)
184 : channel_index_(event_loop->ChannelIndex(channel)), fn_(std::move(fn)) {}
185
186 virtual ~WatcherState() {}
187
188 // Calls the callback, measuring time with get_time, with the provided
189 // context.
190 void DoCallCallback(std::function<monotonic_clock::time_point()> get_time,
191 Context context) {
192 const monotonic_clock::time_point monotonic_start_time = get_time();
193 {
194 const float start_latency =
195 std::chrono::duration_cast<std::chrono::duration<float>>(
196 monotonic_start_time - context.monotonic_sent_time)
197 .count();
198 wakeup_latency_.Add(start_latency);
199 }
200 watcher_->mutate_count(watcher_->count() + 1);
201 fn_(context, context.data);
202
203 const monotonic_clock::time_point monotonic_end_time = get_time();
204
205 const float handler_latency =
206 std::chrono::duration_cast<std::chrono::duration<float>>(
207 monotonic_end_time - monotonic_start_time)
208 .count();
209 handler_time_.Add(handler_latency);
210 }
211
212 int channel_index() const { return channel_index_; }
213
214 void set_timing_report(timing::Watcher *watcher);
215 void ResetReport();
216
217 virtual void Startup(EventLoop *event_loop) = 0;
218
219 protected:
220 const int channel_index_;
221
222 std::function<void(const Context &context, const void *message)> fn_;
223
224 internal::TimingStatistic wakeup_latency_;
225 internal::TimingStatistic handler_time_;
226 timing::Watcher *watcher_ = nullptr;
227};
228
Alex Perrycb7da4b2019-08-28 19:35:56 -0700229} // namespace aos
230
231#endif // AOS_EVENTS_EVENT_LOOP_TMPL_H