blob: 2fbb44595d2188e0494f896c1c1c2d309e57897f [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/shm_event_loop.h"
2
Alex Perrycb7da4b2019-08-28 19:35:56 -07003#include <sys/stat.h>
Austin Schuh39788ff2019-12-01 18:22:57 -08004#include <sys/syscall.h>
Alex Perrycb7da4b2019-08-28 19:35:56 -07005#include <sys/types.h>
Tyler Chatow67ddb032020-01-12 14:30:04 -08006
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include <algorithm>
8#include <atomic>
9#include <chrono>
Austin Schuh39788ff2019-12-01 18:22:57 -080010#include <iterator>
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include <stdexcept>
12
Philipp Schrader790cb542023-07-05 21:06:52 -070013#include "glog/logging.h"
14
Tyler Chatow67ddb032020-01-12 14:30:04 -080015#include "aos/events/aos_logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "aos/events/epoll.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080017#include "aos/events/event_loop_generated.h"
18#include "aos/events/timing_statistics.h"
Austin Schuh094d09b2020-11-20 23:26:52 -080019#include "aos/init.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070020#include "aos/ipc_lib/lockless_queue.h"
Austin Schuh4d275fc2022-09-16 15:42:45 -070021#include "aos/ipc_lib/memory_mapped_queue.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070022#include "aos/realtime.h"
Austin Schuh32fd5a72019-12-01 22:20:26 -080023#include "aos/stl_mutex/stl_mutex.h"
Austin Schuhfccb2d02020-01-26 16:11:19 -080024#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070025#include "aos/util/phased_loop.h"
26
Austin Schuhe84c3ed2019-12-14 15:29:48 -080027namespace {
28
29// Returns the portion of the path after the last /. This very much assumes
30// that the application name is null terminated.
31const char *Filename(const char *path) {
32 const std::string_view path_string_view = path;
33 auto last_slash_pos = path_string_view.find_last_of("/");
34
35 return last_slash_pos == std::string_view::npos ? path
36 : path + last_slash_pos + 1;
37}
38
39} // namespace
40
Brennan Coslett6fd3c002023-07-11 17:41:09 -050041// This value is affected by the umask of the process which is calling it
42// and is set to the user's value by default (check yours running `umask` on
43// the command line).
44// Any file mode requested is transformed using: mode & ~umask and the default
45// umask is 0022 (allow any permissions for the user, dont allow writes for
46// groups or others).
47// See https://man7.org/linux/man-pages/man2/umask.2.html for more details.
48// WITH THE DEFAULT UMASK YOU WONT ACTUALLY GET THESE PERMISSIONS :)
Alex Perrycb7da4b2019-08-28 19:35:56 -070049DEFINE_uint32(permissions, 0770,
Brennan Coslett6fd3c002023-07-11 17:41:09 -050050 "Permissions to make shared memory files and folders, "
Brennan Coslettd5077bc2023-07-13 08:49:35 -050051 "affected by the process's umask. "
Brennan Coslett6fd3c002023-07-11 17:41:09 -050052 "See shm_event_loop.cc for more details.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080053DEFINE_string(application_name, Filename(program_invocation_name),
54 "The application name");
Alex Perrycb7da4b2019-08-28 19:35:56 -070055
56namespace aos {
57
Brian Silverman148d43d2020-06-07 18:19:22 -050058using namespace shm_event_loop_internal;
59
Brian Silverman4f4e0612020-08-12 19:54:41 -070060namespace {
61
Austin Schuh217a9782019-12-21 23:02:50 -080062const Node *MaybeMyNode(const Configuration *configuration) {
63 if (!configuration->has_nodes()) {
64 return nullptr;
65 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070066
Austin Schuh217a9782019-12-21 23:02:50 -080067 return configuration::GetMyNode(configuration);
68}
Alex Perrycb7da4b2019-08-28 19:35:56 -070069
Philipp Schradera8734662023-08-06 14:49:39 -070070void IgnoreWakeupSignal() {
71 struct sigaction action;
72 action.sa_handler = SIG_IGN;
73 PCHECK(sigemptyset(&action.sa_mask) == 0);
74 action.sa_flags = 0;
75 PCHECK(sigaction(ipc_lib::kWakeupSignal, &action, nullptr) == 0);
76}
77
Austin Schuh39788ff2019-12-01 18:22:57 -080078} // namespace
79
Austin Schuh217a9782019-12-21 23:02:50 -080080ShmEventLoop::ShmEventLoop(const Configuration *configuration)
Austin Schuh83c7f702021-01-19 22:36:29 -080081 : EventLoop(configuration),
82 boot_uuid_(UUID::BootUUID()),
Austin Schuhef323c02020-09-01 14:55:28 -070083 shm_base_(FLAGS_shm_base),
Austin Schuhe84c3ed2019-12-14 15:29:48 -080084 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -080085 node_(MaybeMyNode(configuration)) {
Philipp Schradera8734662023-08-06 14:49:39 -070086 // Ignore the wakeup signal by default. Otherwise, we have race conditions on
87 // shutdown where a wakeup signal will uncleanly terminate the process.
88 // See LocklessQueueWakeUpper::Wakeup() for some more information.
89 IgnoreWakeupSignal();
90
Austin Schuh094d09b2020-11-20 23:26:52 -080091 CHECK(IsInitialized()) << ": Need to initialize AOS first.";
Austin Schuh0debde12022-08-17 16:25:17 -070092 ClearContext();
Austin Schuh15649d62019-12-28 16:36:38 -080093 if (configuration->has_nodes()) {
94 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
95 }
96}
Austin Schuh217a9782019-12-21 23:02:50 -080097
Brian Silverman148d43d2020-06-07 18:19:22 -050098namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -080099
100class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700102 explicit SimpleShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
103 const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700104 : event_loop_(event_loop),
105 channel_(channel),
Austin Schuh4d275fc2022-09-16 15:42:45 -0700106 lockless_queue_memory_(shm_base, FLAGS_permissions,
107 event_loop->configuration(), channel),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700108 reader_(lockless_queue_memory_.queue()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700109 context_.data = nullptr;
110 // Point the queue index at the next index to read starting now. This
111 // makes it such that FetchNext will read the next message sent after
112 // the fetcher is created.
113 PointAtNextQueueIndex();
114 }
115
Austin Schuh39788ff2019-12-01 18:22:57 -0800116 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117
Brian Silverman77162972020-08-12 19:52:40 -0700118 // Sets this object to pin or copy data, as configured in the channel.
119 void RetrieveData() {
120 if (channel_->read_method() == ReadMethod::PIN) {
121 PinDataOnFetch();
122 } else {
123 CopyDataOnFetch();
124 }
125 }
126
Brian Silverman3bca5322020-08-12 19:35:29 -0700127 // Sets this object to copy data out of the shared memory into a private
128 // buffer when fetching.
129 void CopyDataOnFetch() {
Brian Silverman77162972020-08-12 19:52:40 -0700130 CHECK(!pin_data());
Brian Silverman3bca5322020-08-12 19:35:29 -0700131 data_storage_.reset(static_cast<char *>(
132 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
133 }
134
Brian Silverman77162972020-08-12 19:52:40 -0700135 // Sets this object to pin data in shared memory when fetching.
136 void PinDataOnFetch() {
137 CHECK(!copy_data());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700138 auto maybe_pinner =
139 ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue());
Brian Silverman77162972020-08-12 19:52:40 -0700140 if (!maybe_pinner) {
141 LOG(FATAL) << "Failed to create reader on "
142 << configuration::CleanedChannelToString(channel_)
143 << ", too many readers.";
144 }
145 pinner_ = std::move(maybe_pinner.value());
146 }
147
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 // Points the next message to fetch at the queue index which will be
149 // populated next.
150 void PointAtNextQueueIndex() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700151 actual_queue_index_ = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700152 if (!actual_queue_index_.valid()) {
153 // Nothing in the queue. The next element will show up at the 0th
154 // index in the queue.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700155 actual_queue_index_ = ipc_lib::QueueIndex::Zero(
156 LocklessQueueSize(lockless_queue_memory_.memory()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700157 } else {
158 actual_queue_index_ = actual_queue_index_.Increment();
159 }
160 }
161
Austin Schuh2b4661a2023-09-20 21:37:33 -0700162 bool FetchNext() { return FetchNextIf(should_fetch_); }
Austin Schuh98ed26f2023-07-19 14:12:28 -0700163
164 bool FetchNextIf(std::function<bool(const Context &)> fn) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700165 const ipc_lib::LocklessQueueReader::Result read_result =
Austin Schuh98ed26f2023-07-19 14:12:28 -0700166 DoFetch(actual_queue_index_, std::move(fn));
Austin Schuh432784f2020-06-23 17:27:35 -0700167
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700168 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 }
170
Austin Schuh98ed26f2023-07-19 14:12:28 -0700171 bool FetchIf(std::function<bool(const Context &)> fn) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700172 const ipc_lib::QueueIndex queue_index = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 // actual_queue_index_ is only meaningful if it was set by Fetch or
174 // FetchNext. This happens when valid_data_ has been set. So, only
175 // skip checking if valid_data_ is true.
176 //
177 // Also, if the latest queue index is invalid, we are empty. So there
178 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800179 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700180 queue_index == actual_queue_index_.DecrementBy(1u)) ||
181 !queue_index.valid()) {
182 return false;
183 }
184
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700185 const ipc_lib::LocklessQueueReader::Result read_result =
Austin Schuh98ed26f2023-07-19 14:12:28 -0700186 DoFetch(queue_index, std::move(fn));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700188 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800189 << ": Queue index went backwards. This should never happen. "
190 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700191
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700192 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 }
194
Austin Schuh2b4661a2023-09-20 21:37:33 -0700195 bool Fetch() { return FetchIf(should_fetch_); }
Austin Schuh98ed26f2023-07-19 14:12:28 -0700196
Austin Schuh39788ff2019-12-01 18:22:57 -0800197 Context context() const { return context_; }
198
Alex Perrycb7da4b2019-08-28 19:35:56 -0700199 bool RegisterWakeup(int priority) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700200 CHECK(!watcher_);
201 watcher_ = ipc_lib::LocklessQueueWatcher::Make(
202 lockless_queue_memory_.queue(), priority);
203 return static_cast<bool>(watcher_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 }
205
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700206 void UnregisterWakeup() {
207 CHECK(watcher_);
208 watcher_ = std::nullopt;
209 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210
Brian Silvermana5450a92020-08-12 19:59:57 -0700211 absl::Span<char> GetMutableSharedMemory() {
212 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800213 }
214
Brian Silvermana5450a92020-08-12 19:59:57 -0700215 absl::Span<const char> GetConstSharedMemory() const {
216 return lockless_queue_memory_.GetConstSharedMemory();
217 }
218
219 absl::Span<const char> GetPrivateMemory() const {
220 if (pin_data()) {
221 return lockless_queue_memory_.GetConstSharedMemory();
222 }
Brian Silverman6d2b3592020-06-18 14:40:15 -0700223 return absl::Span<char>(
224 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700225 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()));
Brian Silverman6d2b3592020-06-18 14:40:15 -0700226 }
227
Alex Perrycb7da4b2019-08-28 19:35:56 -0700228 private:
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700229 ipc_lib::LocklessQueueReader::Result DoFetch(
Austin Schuh98ed26f2023-07-19 14:12:28 -0700230 ipc_lib::QueueIndex queue_index,
231 std::function<bool(const Context &context)> fn) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700232 // TODO(austin): Get behind and make sure it dies.
233 char *copy_buffer = nullptr;
234 if (copy_data()) {
235 copy_buffer = data_storage_start();
236 }
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700237 ipc_lib::LocklessQueueReader::Result read_result = reader_.Read(
Brian Silverman3bca5322020-08-12 19:35:29 -0700238 queue_index.index(), &context_.monotonic_event_time,
239 &context_.realtime_event_time, &context_.monotonic_remote_time,
240 &context_.realtime_remote_time, &context_.remote_queue_index,
Austin Schuh98ed26f2023-07-19 14:12:28 -0700241 &context_.source_boot_uuid, &context_.size, copy_buffer, std::move(fn));
Brian Silverman3bca5322020-08-12 19:35:29 -0700242
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700243 if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) {
Brian Silverman77162972020-08-12 19:52:40 -0700244 if (pin_data()) {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700245 const int pin_result = pinner_->PinIndex(queue_index.index());
246 CHECK(pin_result >= 0)
Brian Silverman77162972020-08-12 19:52:40 -0700247 << ": Got behind while reading and the last message was modified "
248 "out from under us while we tried to pin it. Don't get so far "
249 "behind on: "
250 << configuration::CleanedChannelToString(channel_);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700251 context_.buffer_index = pin_result;
252 } else {
253 context_.buffer_index = -1;
Brian Silverman77162972020-08-12 19:52:40 -0700254 }
255
Brian Silverman3bca5322020-08-12 19:35:29 -0700256 context_.queue_index = queue_index.index();
257 if (context_.remote_queue_index == 0xffffffffu) {
258 context_.remote_queue_index = context_.queue_index;
259 }
260 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
261 context_.monotonic_remote_time = context_.monotonic_event_time;
262 }
263 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
264 context_.realtime_remote_time = context_.realtime_event_time;
265 }
266 const char *const data = DataBuffer();
267 if (data) {
268 context_.data =
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700269 data +
270 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) -
271 context_.size;
Brian Silverman3bca5322020-08-12 19:35:29 -0700272 } else {
273 context_.data = nullptr;
274 }
275 actual_queue_index_ = queue_index.Increment();
276 }
277
278 // Make sure the data wasn't modified while we were reading it. This
279 // can only happen if you are reading the last message *while* it is
280 // being written to, which means you are pretty far behind.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700281 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE)
Brian Silverman3bca5322020-08-12 19:35:29 -0700282 << ": Got behind while reading and the last message was modified "
283 "out from under us while we were reading it. Don't get so far "
284 "behind on: "
285 << configuration::CleanedChannelToString(channel_);
286
287 // We fell behind between when we read the index and read the value.
288 // This isn't worth recovering from since this means we went to sleep
289 // for a long time in the middle of this function.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700290 if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700291 event_loop_->SendTimingReport();
292 LOG(FATAL) << "The next message is no longer available. "
293 << configuration::CleanedChannelToString(channel_);
294 }
295
296 return read_result;
297 }
298
299 char *data_storage_start() const {
300 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800301 return RoundChannelData(data_storage_.get(), channel_->max_size());
302 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700303
304 // Note that for some modes the return value will change as new messages are
305 // read.
306 const char *DataBuffer() const {
307 if (copy_data()) {
308 return data_storage_start();
309 }
Brian Silverman77162972020-08-12 19:52:40 -0700310 if (pin_data()) {
311 return static_cast<const char *>(pinner_->Data());
312 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700313 return nullptr;
314 }
315
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800316 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silverman77162972020-08-12 19:52:40 -0700317 bool pin_data() const { return static_cast<bool>(pinner_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800318
Austin Schuh432784f2020-06-23 17:27:35 -0700319 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800320 const Channel *const channel_;
Austin Schuh4d275fc2022-09-16 15:42:45 -0700321 ipc_lib::MemoryMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700322 ipc_lib::LocklessQueueReader reader_;
323 // This being nullopt indicates we're not looking for wakeups right now.
324 std::optional<ipc_lib::LocklessQueueWatcher> watcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700326 ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700327
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800328 // This being empty indicates we're not going to copy data.
329 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800330
Brian Silverman77162972020-08-12 19:52:40 -0700331 // This being nullopt indicates we're not going to pin messages.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700332 std::optional<ipc_lib::LocklessQueuePinner> pinner_;
Brian Silverman77162972020-08-12 19:52:40 -0700333
Austin Schuh39788ff2019-12-01 18:22:57 -0800334 Context context_;
Austin Schuh82ea7382023-07-14 15:17:34 -0700335
336 // Pre-allocated should_fetch function so we don't allocate.
Austin Schuh98ed26f2023-07-19 14:12:28 -0700337 const std::function<bool(const Context &)> should_fetch_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800338};
339
340class ShmFetcher : public RawFetcher {
341 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700342 explicit ShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
343 const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800344 : RawFetcher(event_loop, channel),
Austin Schuhef323c02020-09-01 14:55:28 -0700345 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman77162972020-08-12 19:52:40 -0700346 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700347 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800348
Austin Schuh3054f5f2021-07-21 15:38:01 -0700349 ~ShmFetcher() override {
350 shm_event_loop()->CheckCurrentThread();
351 context_.data = nullptr;
352 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800353
354 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700355 shm_event_loop()->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800356 if (simple_shm_fetcher_.FetchNext()) {
357 context_ = simple_shm_fetcher_.context();
358 return std::make_pair(true, monotonic_clock::now());
359 }
360 return std::make_pair(false, monotonic_clock::min_time);
361 }
362
Austin Schuh98ed26f2023-07-19 14:12:28 -0700363 std::pair<bool, monotonic_clock::time_point> DoFetchNextIf(
364 std::function<bool(const Context &context)> fn) override {
365 shm_event_loop()->CheckCurrentThread();
366 if (simple_shm_fetcher_.FetchNextIf(std::move(fn))) {
367 context_ = simple_shm_fetcher_.context();
368 return std::make_pair(true, monotonic_clock::now());
369 }
370 return std::make_pair(false, monotonic_clock::min_time);
371 }
372
Austin Schuh39788ff2019-12-01 18:22:57 -0800373 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700374 shm_event_loop()->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800375 if (simple_shm_fetcher_.Fetch()) {
376 context_ = simple_shm_fetcher_.context();
377 return std::make_pair(true, monotonic_clock::now());
378 }
379 return std::make_pair(false, monotonic_clock::min_time);
380 }
381
Austin Schuh98ed26f2023-07-19 14:12:28 -0700382 std::pair<bool, monotonic_clock::time_point> DoFetchIf(
383 std::function<bool(const Context &context)> fn) override {
384 shm_event_loop()->CheckCurrentThread();
385 if (simple_shm_fetcher_.FetchIf(std::move(fn))) {
386 context_ = simple_shm_fetcher_.context();
387 return std::make_pair(true, monotonic_clock::now());
388 }
389 return std::make_pair(false, monotonic_clock::min_time);
390 }
391
Brian Silvermana5450a92020-08-12 19:59:57 -0700392 absl::Span<const char> GetPrivateMemory() const {
Brian Silverman6d2b3592020-06-18 14:40:15 -0700393 return simple_shm_fetcher_.GetPrivateMemory();
394 }
395
Austin Schuh39788ff2019-12-01 18:22:57 -0800396 private:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700397 const ShmEventLoop *shm_event_loop() const {
398 return static_cast<const ShmEventLoop *>(event_loop());
399 }
400
Austin Schuh39788ff2019-12-01 18:22:57 -0800401 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700402};
403
Brian Silvermane1fe2512022-08-14 23:18:50 -0700404class ShmExitHandle : public ExitHandle {
405 public:
406 ShmExitHandle(ShmEventLoop *event_loop) : event_loop_(event_loop) {
407 ++event_loop_->exit_handle_count_;
408 }
409 ~ShmExitHandle() override {
410 CHECK_GT(event_loop_->exit_handle_count_, 0);
411 --event_loop_->exit_handle_count_;
412 }
413
414 void Exit() override { event_loop_->Exit(); }
415
416 private:
417 ShmEventLoop *const event_loop_;
418};
419
Alex Perrycb7da4b2019-08-28 19:35:56 -0700420class ShmSender : public RawSender {
421 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700422 explicit ShmSender(std::string_view shm_base, EventLoop *event_loop,
423 const Channel *channel)
Austin Schuh39788ff2019-12-01 18:22:57 -0800424 : RawSender(event_loop, channel),
Austin Schuh4d275fc2022-09-16 15:42:45 -0700425 lockless_queue_memory_(shm_base, FLAGS_permissions,
426 event_loop->configuration(), channel),
Austin Schuhfff9c3a2023-06-16 18:48:23 -0700427 lockless_queue_sender_(
428 VerifySender(ipc_lib::LocklessQueueSender::Make(
429 lockless_queue_memory_.queue(),
430 configuration::ChannelStorageDuration(
431 event_loop->configuration(), channel)),
432 channel)),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700433 wake_upper_(lockless_queue_memory_.queue()) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700434
Austin Schuh3054f5f2021-07-21 15:38:01 -0700435 ~ShmSender() override { shm_event_loop()->CheckCurrentThread(); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800436
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700437 static ipc_lib::LocklessQueueSender VerifySender(
438 std::optional<ipc_lib::LocklessQueueSender> sender,
Austin Schuhe516ab02020-05-06 21:37:04 -0700439 const Channel *channel) {
440 if (sender) {
441 return std::move(sender.value());
442 }
443 LOG(FATAL) << "Failed to create sender on "
444 << configuration::CleanedChannelToString(channel)
445 << ", too many senders.";
446 }
447
Austin Schuh3054f5f2021-07-21 15:38:01 -0700448 void *data() override {
449 shm_event_loop()->CheckCurrentThread();
450 return lockless_queue_sender_.Data();
451 }
452 size_t size() override {
453 shm_event_loop()->CheckCurrentThread();
454 return lockless_queue_sender_.size();
455 }
milind1f1dca32021-07-03 13:50:07 -0700456
457 Error DoSend(size_t length,
458 aos::monotonic_clock::time_point monotonic_remote_time,
459 aos::realtime_clock::time_point realtime_remote_time,
460 uint32_t remote_queue_index,
461 const UUID &source_boot_uuid) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700462 shm_event_loop()->CheckCurrentThread();
Austin Schuh0f7ed462020-03-28 20:38:34 -0700463 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
464 << ": Sent too big a message on "
465 << configuration::CleanedChannelToString(channel());
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700466 const auto result = lockless_queue_sender_.Send(
467 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
468 source_boot_uuid, &monotonic_sent_time_, &realtime_sent_time_,
469 &sent_queue_index_);
470 CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE)
Austin Schuh91ba6392020-10-03 13:27:47 -0700471 << ": Somebody wrote outside the buffer of their message on channel "
472 << configuration::CleanedChannelToString(channel());
473
Austin Schuh65493d62022-08-17 15:10:37 -0700474 wake_upper_.Wakeup(event_loop()->is_running()
475 ? event_loop()->runtime_realtime_priority()
476 : 0);
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700477 return CheckLocklessQueueResult(result);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700478 }
479
milind1f1dca32021-07-03 13:50:07 -0700480 Error DoSend(const void *msg, size_t length,
481 aos::monotonic_clock::time_point monotonic_remote_time,
482 aos::realtime_clock::time_point realtime_remote_time,
483 uint32_t remote_queue_index,
484 const UUID &source_boot_uuid) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700485 shm_event_loop()->CheckCurrentThread();
Austin Schuh0f7ed462020-03-28 20:38:34 -0700486 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
487 << ": Sent too big a message on "
488 << configuration::CleanedChannelToString(channel());
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700489 const auto result = lockless_queue_sender_.Send(
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700490 reinterpret_cast<const char *>(msg), length, monotonic_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -0700491 realtime_remote_time, remote_queue_index, source_boot_uuid,
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700492 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
493
494 CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE)
495 << ": Somebody wrote outside the buffer of their message on "
496 "channel "
Austin Schuh91ba6392020-10-03 13:27:47 -0700497 << configuration::CleanedChannelToString(channel());
Austin Schuh65493d62022-08-17 15:10:37 -0700498 wake_upper_.Wakeup(event_loop()->is_running()
499 ? event_loop()->runtime_realtime_priority()
500 : 0);
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700501
502 return CheckLocklessQueueResult(result);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700503 }
504
Brian Silverman5120afb2020-01-31 17:44:35 -0800505 absl::Span<char> GetSharedMemory() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700506 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800507 }
508
Austin Schuh3054f5f2021-07-21 15:38:01 -0700509 int buffer_index() override {
510 shm_event_loop()->CheckCurrentThread();
511 return lockless_queue_sender_.buffer_index();
512 }
Brian Silverman4f4e0612020-08-12 19:54:41 -0700513
Alex Perrycb7da4b2019-08-28 19:35:56 -0700514 private:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700515 const ShmEventLoop *shm_event_loop() const {
516 return static_cast<const ShmEventLoop *>(event_loop());
517 }
518
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700519 RawSender::Error CheckLocklessQueueResult(
520 const ipc_lib::LocklessQueueSender::Result &result) {
521 switch (result) {
522 case ipc_lib::LocklessQueueSender::Result::GOOD:
523 return Error::kOk;
524 case ipc_lib::LocklessQueueSender::Result::MESSAGES_SENT_TOO_FAST:
525 return Error::kMessagesSentTooFast;
526 case ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE:
527 return Error::kInvalidRedzone;
528 }
529 LOG(FATAL) << "Unknown lockless queue sender result"
530 << static_cast<int>(result);
531 }
532
Austin Schuh4d275fc2022-09-16 15:42:45 -0700533 ipc_lib::MemoryMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700534 ipc_lib::LocklessQueueSender lockless_queue_sender_;
535 ipc_lib::LocklessQueueWakeUpper wake_upper_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700536};
537
Alex Perrycb7da4b2019-08-28 19:35:56 -0700538// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500539class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700540 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500541 ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700542 std::string_view shm_base, ShmEventLoop *event_loop,
543 const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800544 std::function<void(const Context &context, const void *message)> fn,
545 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500546 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800547 event_loop_(event_loop),
548 event_(this),
Austin Schuhef323c02020-09-01 14:55:28 -0700549 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700550 if (copy_data) {
Brian Silverman77162972020-08-12 19:52:40 -0700551 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700552 }
553 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700554
Austin Schuh3054f5f2021-07-21 15:38:01 -0700555 ~ShmWatcherState() override {
556 event_loop_->CheckCurrentThread();
557 event_loop_->RemoveEvent(&event_);
558 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800559
560 void Startup(EventLoop *event_loop) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700561 event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800562 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh65493d62022-08-17 15:10:37 -0700563 CHECK(RegisterWakeup(event_loop->runtime_realtime_priority()));
Austin Schuh39788ff2019-12-01 18:22:57 -0800564 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700565
Alex Perrycb7da4b2019-08-28 19:35:56 -0700566 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800567 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700568 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800569 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800570
571 if (has_new_data_) {
572 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800573 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800574 event_loop_->AddEvent(&event_);
575 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700576 }
577
578 return has_new_data_;
579 }
580
Alex Perrycb7da4b2019-08-28 19:35:56 -0700581 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800582 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700583 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800584 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700585 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800586 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700587 }
588
Austin Schuh39788ff2019-12-01 18:22:57 -0800589 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700590 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800591 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700592 }
593
Austin Schuh39788ff2019-12-01 18:22:57 -0800594 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700595
Brian Silvermana5450a92020-08-12 19:59:57 -0700596 absl::Span<const char> GetSharedMemory() const {
597 return simple_shm_fetcher_.GetConstSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800598 }
599
Alex Perrycb7da4b2019-08-28 19:35:56 -0700600 private:
601 bool has_new_data_ = false;
602
Austin Schuh7d87b672019-12-01 20:23:49 -0800603 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500604 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800605 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700606};
607
608// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500609class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700610 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500611 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800612 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800613 shm_event_loop_(shm_event_loop),
614 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800615 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
Austin Schuh5ca13112021-02-07 22:06:53 -0800616 // The timer may fire spuriously. HandleEvent on the event loop will
Austin Schuhcde39fd2020-02-22 20:58:24 -0800617 // call the callback if it is needed. It may also have called it when
618 // processing some other event, and the kernel decided to deliver this
619 // wakeup anyways.
620 timerfd_.Read();
621 shm_event_loop_->HandleEvent();
622 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700623 }
624
Brian Silverman148d43d2020-06-07 18:19:22 -0500625 ~ShmTimerHandler() {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700626 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800627 Disable();
628 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
629 }
630
631 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800632 CHECK(!event_.valid());
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700633 disabled_ = false;
Austin Schuhcde39fd2020-02-22 20:58:24 -0800634 const auto monotonic_now = Call(monotonic_clock::now, base_);
635 if (event_.valid()) {
Philipp Schradera6712522023-07-05 20:25:11 -0700636 // If someone called Schedule inside Call, rescheduling is already taken
637 // care of. Bail.
Austin Schuhcde39fd2020-02-22 20:58:24 -0800638 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800639 }
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700640 if (disabled_) {
641 // Somebody called Disable inside Call, so we don't want to reschedule.
642 // Bail.
643 return;
644 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800645
Austin Schuh4d275fc2022-09-16 15:42:45 -0700646 if (repeat_offset_ == std::chrono::seconds(0)) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800647 timerfd_.Disable();
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700648 disabled_ = true;
Austin Schuhcde39fd2020-02-22 20:58:24 -0800649 } else {
650 // Compute how many cycles have elapsed and schedule the next iteration
651 // for the next iteration in the future.
652 const int elapsed_cycles =
653 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
654 std::chrono::nanoseconds(1)) /
655 repeat_offset_);
656 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800657
Austin Schuhcde39fd2020-02-22 20:58:24 -0800658 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800659 event_.set_event_time(base_);
660 shm_event_loop_->AddEvent(&event_);
Austin Schuh4d275fc2022-09-16 15:42:45 -0700661 timerfd_.SetTime(base_, std::chrono::seconds(0));
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700662 disabled_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800663 }
664 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700665
Philipp Schradera6712522023-07-05 20:25:11 -0700666 void Schedule(monotonic_clock::time_point base,
667 monotonic_clock::duration repeat_offset) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700668 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800669 if (event_.valid()) {
670 shm_event_loop_->RemoveEvent(&event_);
671 }
672
Alex Perrycb7da4b2019-08-28 19:35:56 -0700673 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800674 base_ = base;
675 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800676 event_.set_event_time(base_);
677 shm_event_loop_->AddEvent(&event_);
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700678 disabled_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700679 }
680
Austin Schuh7d87b672019-12-01 20:23:49 -0800681 void Disable() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700682 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800683 shm_event_loop_->RemoveEvent(&event_);
684 timerfd_.Disable();
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700685 disabled_ = true;
Austin Schuh7d87b672019-12-01 20:23:49 -0800686 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700687
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700688 bool IsDisabled() override { return disabled_; }
689
Alex Perrycb7da4b2019-08-28 19:35:56 -0700690 private:
691 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500692 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700693
Brian Silverman148d43d2020-06-07 18:19:22 -0500694 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700695
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800696 monotonic_clock::time_point base_;
697 monotonic_clock::duration repeat_offset_;
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700698
699 // Used to track if Disable() was called during the callback, so we know not
700 // to reschedule.
Naman Gupta4d13b0a2022-10-19 16:41:24 -0700701 bool disabled_ = true;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700702};
703
704// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500705class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700706 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500707 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
708 ::std::function<void(int)> fn,
709 const monotonic_clock::duration interval,
710 const monotonic_clock::duration offset)
711 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800712 shm_event_loop_(shm_event_loop),
713 event_(this) {
714 shm_event_loop_->epoll_.OnReadable(
715 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
716 }
717
718 void HandleEvent() {
719 // The return value for read is the number of cycles that have elapsed.
720 // Because we check to see when this event *should* have happened, there are
721 // cases where Read() will return 0, when 1 cycle has actually happened.
722 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
723 // ignore it. Call handles rescheduling and calculating elapsed cycles
724 // without any extra help.
725 timerfd_.Read();
726 event_.Invalidate();
727
James Kuszmaul20dcc7c2023-01-20 11:06:31 -0800728 Call(monotonic_clock::now);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700729 }
730
Brian Silverman148d43d2020-06-07 18:19:22 -0500731 ~ShmPhasedLoopHandler() override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700732 shm_event_loop_->CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800733 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800734 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700735 }
736
737 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800738 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800739 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700740 shm_event_loop_->CheckCurrentThread();
Austin Schuh7d87b672019-12-01 20:23:49 -0800741 if (event_.valid()) {
742 shm_event_loop_->RemoveEvent(&event_);
743 }
744
Austin Schuh39788ff2019-12-01 18:22:57 -0800745 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800746 event_.set_event_time(sleep_time);
747 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700748 }
749
750 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500751 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700752
Brian Silverman148d43d2020-06-07 18:19:22 -0500753 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700754};
Brian Silverman148d43d2020-06-07 18:19:22 -0500755
756} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700757
758::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
759 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700760 CheckCurrentThread();
Austin Schuhca4828c2019-12-28 14:21:35 -0800761 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
762 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
763 << "\", \"type\": \"" << channel->type()->string_view()
764 << "\" } is not able to be fetched on this node. Check your "
765 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800766 }
767
Austin Schuhef323c02020-09-01 14:55:28 -0700768 return ::std::unique_ptr<RawFetcher>(
769 new ShmFetcher(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700770}
771
772::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
773 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700774 CheckCurrentThread();
Brian Silverman0fc69932020-01-24 21:54:02 -0800775 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800776
Austin Schuhef323c02020-09-01 14:55:28 -0700777 return ::std::unique_ptr<RawSender>(new ShmSender(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700778}
779
780void ShmEventLoop::MakeRawWatcher(
781 const Channel *channel,
782 std::function<void(const Context &context, const void *message)> watcher) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700783 CheckCurrentThread();
Brian Silverman0fc69932020-01-24 21:54:02 -0800784 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800785
Austin Schuh39788ff2019-12-01 18:22:57 -0800786 NewWatcher(::std::unique_ptr<WatcherState>(
Austin Schuhef323c02020-09-01 14:55:28 -0700787 new ShmWatcherState(shm_base_, this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800788}
789
790void ShmEventLoop::MakeRawNoArgWatcher(
791 const Channel *channel,
792 std::function<void(const Context &context)> watcher) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700793 CheckCurrentThread();
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800794 TakeWatcher(channel);
795
Brian Silverman148d43d2020-06-07 18:19:22 -0500796 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700797 shm_base_, this, channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800798 [watcher](const Context &context, const void *) { watcher(context); },
799 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700800}
801
802TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700803 CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -0800804 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500805 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700806}
807
808PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
809 ::std::function<void(int)> callback,
810 const monotonic_clock::duration interval,
811 const monotonic_clock::duration offset) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700812 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -0500813 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
814 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700815}
816
817void ShmEventLoop::OnRun(::std::function<void()> on_run) {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700818 CheckCurrentThread();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700819 on_run_.push_back(::std::move(on_run));
820}
821
Austin Schuh3054f5f2021-07-21 15:38:01 -0700822void ShmEventLoop::CheckCurrentThread() const {
823 if (__builtin_expect(check_mutex_ != nullptr, false)) {
824 CHECK(check_mutex_->is_locked())
825 << ": The configured mutex is not locked while calling a "
826 "ShmEventLoop function";
827 }
828 if (__builtin_expect(!!check_tid_, false)) {
829 CHECK_EQ(syscall(SYS_gettid), *check_tid_)
830 << ": Being called from the wrong thread";
831 }
832}
833
Austin Schuh5ca13112021-02-07 22:06:53 -0800834// This is a bit tricky because watchers can generate new events at any time (as
835// long as it's in the past). We want to check the watchers at least once before
836// declaring there are no events to handle, and we want to check them again if
837// event processing takes long enough that we find an event after that point in
838// time to handle.
Austin Schuh7d87b672019-12-01 20:23:49 -0800839void ShmEventLoop::HandleEvent() {
Austin Schuh5ca13112021-02-07 22:06:53 -0800840 // Time through which we've checked for new events in watchers.
841 monotonic_clock::time_point checked_until = monotonic_clock::min_time;
842 if (!signalfd_) {
843 // Nothing to check, so we can bail out immediately once we're out of
844 // events.
845 CHECK(watchers_.empty());
846 checked_until = monotonic_clock::max_time;
Austin Schuh7d87b672019-12-01 20:23:49 -0800847 }
848
Austin Schuh5ca13112021-02-07 22:06:53 -0800849 // Loop until we run out of events to check.
Austin Schuh39788ff2019-12-01 18:22:57 -0800850 while (true) {
Austin Schuh5ca13112021-02-07 22:06:53 -0800851 // Time of the next event we know about. If this is before checked_until, we
852 // know there aren't any new events before the next one that we already know
853 // about, so no need to check the watchers.
854 monotonic_clock::time_point next_time = monotonic_clock::max_time;
855
856 if (EventCount() == 0) {
857 if (checked_until != monotonic_clock::min_time) {
858 // No events, and we've already checked the watchers at least once, so
859 // we're all done.
860 //
861 // There's a small chance that a watcher has gotten another event in
862 // between checked_until and now. If so, then the signalfd will be
863 // triggered now and we'll re-enter HandleEvent immediately. This is
864 // unlikely though, so we don't want to spend time checking all the
865 // watchers unnecessarily.
866 break;
867 }
868 } else {
869 next_time = PeekEvent()->event_time();
870 }
Austin Schuh00cad2e2022-12-02 20:11:04 -0800871 monotonic_clock::time_point now;
872 bool new_data = false;
Austin Schuh5ca13112021-02-07 22:06:53 -0800873
874 if (next_time > checked_until) {
875 // Read all of the signals, because there's no point in waking up again
876 // immediately to handle each one if we've fallen behind.
877 //
878 // This is safe before checking for new data on the watchers. If a signal
879 // is cleared here, the corresponding CheckForNewData() call below will
880 // pick it up.
881 while (true) {
882 const signalfd_siginfo result = signalfd_->Read();
883 if (result.ssi_signo == 0) {
884 break;
885 }
886 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
887 }
Austin Schuh00cad2e2022-12-02 20:11:04 -0800888 // This is the last time we can guarantee that if a message is published
889 // before, we will notice it.
890 now = monotonic_clock::now();
Austin Schuh5ca13112021-02-07 22:06:53 -0800891
892 // Check all the watchers for new events.
893 for (std::unique_ptr<WatcherState> &base_watcher : watchers_) {
894 ShmWatcherState *const watcher =
895 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
896
Austin Schuh00cad2e2022-12-02 20:11:04 -0800897 // Track if we got a message.
898 if (watcher->CheckForNewData()) {
899 new_data = true;
900 }
Austin Schuh5ca13112021-02-07 22:06:53 -0800901 }
902 if (EventCount() == 0) {
903 // Still no events, all done now.
904 break;
905 }
906
907 checked_until = now;
908 // Check for any new events we found.
909 next_time = PeekEvent()->event_time();
Austin Schuh00cad2e2022-12-02 20:11:04 -0800910 } else {
911 now = monotonic_clock::now();
Austin Schuh5ca13112021-02-07 22:06:53 -0800912 }
913
914 if (next_time > now) {
Austin Schuh00cad2e2022-12-02 20:11:04 -0800915 // Ok, we got a message with a timestamp *after* we wrote down time. We
916 // need to process it (otherwise we will go to sleep without processing
917 // it), but we also need to make sure no other messages have come in
918 // before it that we would process out of order. Just go around again to
919 // redo the checks.
920 if (new_data) {
921 continue;
922 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800923 break;
924 }
925
Austin Schuh5ca13112021-02-07 22:06:53 -0800926 EventLoopEvent *const event = PopEvent();
Austin Schuh7d87b672019-12-01 20:23:49 -0800927 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800928 }
929}
930
Austin Schuh32fd5a72019-12-01 22:20:26 -0800931// RAII class to mask signals.
932class ScopedSignalMask {
933 public:
934 ScopedSignalMask(std::initializer_list<int> signals) {
935 sigset_t sigset;
936 PCHECK(sigemptyset(&sigset) == 0);
937 for (int signal : signals) {
938 PCHECK(sigaddset(&sigset, signal) == 0);
939 }
940
941 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
942 }
943
944 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
945
946 private:
947 sigset_t old_;
948};
949
950// Class to manage the static state associated with killing multiple event
951// loops.
952class SignalHandler {
953 public:
954 // Gets the singleton.
955 static SignalHandler *global() {
956 static SignalHandler loop;
957 return &loop;
958 }
959
960 // Handles the signal with the singleton.
961 static void HandleSignal(int) { global()->DoHandleSignal(); }
962
963 // Registers an event loop to receive Exit() calls.
964 void Register(ShmEventLoop *event_loop) {
965 // Block signals while we have the mutex so we never race with the signal
966 // handler.
967 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
968 std::unique_lock<stl_mutex> locker(mutex_);
969 if (event_loops_.size() == 0) {
970 // The first caller registers the signal handler.
971 struct sigaction new_action;
972 sigemptyset(&new_action.sa_mask);
973 // This makes it so that 2 control c's to a stuck process will kill it by
974 // restoring the original signal handler.
975 new_action.sa_flags = SA_RESETHAND;
976 new_action.sa_handler = &HandleSignal;
977
978 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
979 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
980 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
981 }
982
983 event_loops_.push_back(event_loop);
984 }
985
986 // Unregisters an event loop to receive Exit() calls.
987 void Unregister(ShmEventLoop *event_loop) {
988 // Block signals while we have the mutex so we never race with the signal
989 // handler.
990 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
991 std::unique_lock<stl_mutex> locker(mutex_);
992
Brian Silverman5120afb2020-01-31 17:44:35 -0800993 event_loops_.erase(
994 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800995
996 if (event_loops_.size() == 0u) {
997 // The last caller restores the original signal handlers.
998 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
999 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
1000 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
1001 }
1002 }
1003
1004 private:
1005 void DoHandleSignal() {
1006 // We block signals while grabbing the lock, so there should never be a
1007 // race. Confirm that this is true using trylock.
1008 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
1009 "modifing the event loop list.";
1010 for (ShmEventLoop *event_loop : event_loops_) {
1011 event_loop->Exit();
1012 }
1013 mutex_.unlock();
1014 }
1015
1016 // Mutex to protect all state.
1017 stl_mutex mutex_;
1018 std::vector<ShmEventLoop *> event_loops_;
1019 struct sigaction old_action_int_;
1020 struct sigaction old_action_hup_;
1021 struct sigaction old_action_term_;
1022};
1023
Alex Perrycb7da4b2019-08-28 19:35:56 -07001024void ShmEventLoop::Run() {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001025 CheckCurrentThread();
Austin Schuh32fd5a72019-12-01 22:20:26 -08001026 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -08001027
Alex Perrycb7da4b2019-08-28 19:35:56 -07001028 if (watchers_.size() > 0) {
Austin Schuh5ca13112021-02-07 22:06:53 -08001029 signalfd_.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
Alex Perrycb7da4b2019-08-28 19:35:56 -07001030
Austin Schuh5ca13112021-02-07 22:06:53 -08001031 epoll_.OnReadable(signalfd_->fd(), [this]() { HandleEvent(); });
Alex Perrycb7da4b2019-08-28 19:35:56 -07001032 }
1033
Austin Schuh39788ff2019-12-01 18:22:57 -08001034 MaybeScheduleTimingReports();
1035
Austin Schuh7d87b672019-12-01 20:23:49 -08001036 ReserveEvents();
1037
Tyler Chatow67ddb032020-01-12 14:30:04 -08001038 {
Austin Schuha0c41ba2020-09-10 22:59:14 -07001039 logging::ScopedLogRestorer prev_logger;
Tyler Chatow67ddb032020-01-12 14:30:04 -08001040 AosLogToFbs aos_logger;
1041 if (!skip_logger_) {
Austin Schuhad9e5eb2021-11-19 20:33:55 -08001042 aos_logger.Initialize(&name_, MakeSender<logging::LogMessageFbs>("/aos"));
Austin Schuha0c41ba2020-09-10 22:59:14 -07001043 prev_logger.Swap(aos_logger.implementation());
Tyler Chatow67ddb032020-01-12 14:30:04 -08001044 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001045
Tyler Chatow67ddb032020-01-12 14:30:04 -08001046 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -07001047 const cpu_set_t default_affinity = DefaultAffinity();
1048 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
1049 ::aos::SetCurrentThreadAffinity(affinity_);
1050 }
Tyler Chatow67ddb032020-01-12 14:30:04 -08001051 // Now, all the callbacks are setup. Lock everything into memory and go RT.
1052 if (priority_ != 0) {
1053 ::aos::InitRT();
1054
1055 LOG(INFO) << "Setting priority to " << priority_;
1056 ::aos::SetCurrentThreadRealtimePriority(priority_);
1057 }
1058
1059 set_is_running(true);
1060
1061 // Now that we are realtime (but before the OnRun handlers run), snap the
1062 // queue index.
1063 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
1064 watcher->Startup(this);
1065 }
1066
1067 // Now that we are RT, run all the OnRun handlers.
Austin Schuha9012be2021-07-21 15:19:11 -07001068 SetTimerContext(monotonic_clock::now());
Tyler Chatow67ddb032020-01-12 14:30:04 -08001069 for (const auto &run : on_run_) {
1070 run();
1071 }
1072
1073 // And start our main event loop which runs all the timers and handles Quit.
1074 epoll_.Run();
1075
1076 // Once epoll exits, there is no useful nonrt work left to do.
1077 set_is_running(false);
1078
1079 // Nothing time or synchronization critical needs to happen after this
1080 // point. Drop RT priority.
1081 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001082 }
1083
Austin Schuh39788ff2019-12-01 18:22:57 -08001084 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001085 ShmWatcherState *watcher =
1086 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -07001087 watcher->UnregisterWakeup();
1088 }
1089
1090 if (watchers_.size() > 0) {
Austin Schuh5ca13112021-02-07 22:06:53 -08001091 epoll_.DeleteFd(signalfd_->fd());
1092 signalfd_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001093 }
Austin Schuh32fd5a72019-12-01 22:20:26 -08001094
1095 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001096
1097 // Trigger any remaining senders or fetchers to be cleared before destroying
1098 // the event loop so the book keeping matches. Do this in the thread that
1099 // created the timing reporter.
1100 timing_report_sender_.reset();
Austin Schuh0debde12022-08-17 16:25:17 -07001101 ClearContext();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001102}
1103
1104void ShmEventLoop::Exit() { epoll_.Quit(); }
1105
Brian Silvermane1fe2512022-08-14 23:18:50 -07001106std::unique_ptr<ExitHandle> ShmEventLoop::MakeExitHandle() {
1107 return std::make_unique<ShmExitHandle>(this);
1108}
1109
Alex Perrycb7da4b2019-08-28 19:35:56 -07001110ShmEventLoop::~ShmEventLoop() {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001111 CheckCurrentThread();
Austin Schuh39788ff2019-12-01 18:22:57 -08001112 // Force everything with a registered fd with epoll to be destroyed now.
1113 timers_.clear();
1114 phased_loops_.clear();
1115 watchers_.clear();
1116
Alex Perrycb7da4b2019-08-28 19:35:56 -07001117 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
Brian Silvermane1fe2512022-08-14 23:18:50 -07001118 CHECK_EQ(0, exit_handle_count_)
1119 << ": All ExitHandles must be destroyed before the ShmEventLoop";
Alex Perrycb7da4b2019-08-28 19:35:56 -07001120}
1121
Alex Perrycb7da4b2019-08-28 19:35:56 -07001122void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001123 CheckCurrentThread();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001124 if (is_running()) {
1125 LOG(FATAL) << "Cannot set realtime priority while running.";
1126 }
1127 priority_ = priority;
1128}
1129
Brian Silverman6a54ff32020-04-28 16:41:39 -07001130void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001131 CheckCurrentThread();
Brian Silverman6a54ff32020-04-28 16:41:39 -07001132 if (is_running()) {
1133 LOG(FATAL) << "Cannot set affinity while running.";
1134 }
1135 affinity_ = cpuset;
1136}
1137
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001138void ShmEventLoop::set_name(const std::string_view name) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001139 CheckCurrentThread();
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001140 name_ = std::string(name);
1141 UpdateTimingReport();
1142}
1143
Brian Silvermana5450a92020-08-12 19:59:57 -07001144absl::Span<const char> ShmEventLoop::GetWatcherSharedMemory(
1145 const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001146 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -05001147 ShmWatcherState *const watcher_state =
1148 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -08001149 return watcher_state->GetSharedMemory();
1150}
1151
Brian Silverman4f4e0612020-08-12 19:54:41 -07001152int ShmEventLoop::NumberBuffers(const Channel *channel) {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001153 CheckCurrentThread();
Austin Schuh4d275fc2022-09-16 15:42:45 -07001154 return ipc_lib::MakeQueueConfiguration(configuration(), channel)
1155 .num_messages();
Brian Silverman4f4e0612020-08-12 19:54:41 -07001156}
1157
Brian Silverman5120afb2020-01-31 17:44:35 -08001158absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
1159 const aos::RawSender *sender) const {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001160 CheckCurrentThread();
Brian Silverman148d43d2020-06-07 18:19:22 -05001161 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -08001162}
1163
Brian Silvermana5450a92020-08-12 19:59:57 -07001164absl::Span<const char> ShmEventLoop::GetShmFetcherPrivateMemory(
Brian Silverman6d2b3592020-06-18 14:40:15 -07001165 const aos::RawFetcher *fetcher) const {
Austin Schuh3054f5f2021-07-21 15:38:01 -07001166 CheckCurrentThread();
Brian Silverman6d2b3592020-06-18 14:40:15 -07001167 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
1168}
1169
Austin Schuh3054f5f2021-07-21 15:38:01 -07001170pid_t ShmEventLoop::GetTid() {
1171 CheckCurrentThread();
1172 return syscall(SYS_gettid);
1173}
Austin Schuh39788ff2019-12-01 18:22:57 -08001174
Alex Perrycb7da4b2019-08-28 19:35:56 -07001175} // namespace aos