blob: 1ac86560e48e191bed62aafdb01b9e705c69a41b [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/shm_event_loop.h"
2
3#include <sys/mman.h>
4#include <sys/stat.h>
Austin Schuh39788ff2019-12-01 18:22:57 -08005#include <sys/syscall.h>
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include <sys/types.h>
7#include <unistd.h>
Tyler Chatow67ddb032020-01-12 14:30:04 -08008
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include <algorithm>
10#include <atomic>
11#include <chrono>
Austin Schuh39788ff2019-12-01 18:22:57 -080012#include <iterator>
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include <stdexcept>
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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "aos/ipc_lib/lockless_queue.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080020#include "aos/ipc_lib/signalfd.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070021#include "aos/realtime.h"
Austin Schuh32fd5a72019-12-01 22:20:26 -080022#include "aos/stl_mutex/stl_mutex.h"
Austin Schuhfccb2d02020-01-26 16:11:19 -080023#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070024#include "aos/util/phased_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080025#include "glog/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070026
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
Alex Perrycb7da4b2019-08-28 19:35:56 -070041DEFINE_string(shm_base, "/dev/shm/aos",
42 "Directory to place queue backing mmaped files in.");
43DEFINE_uint32(permissions, 0770,
44 "Permissions to make shared memory files and folders.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080045DEFINE_string(application_name, Filename(program_invocation_name),
46 "The application name");
Alex Perrycb7da4b2019-08-28 19:35:56 -070047
48namespace aos {
49
Brian Silverman148d43d2020-06-07 18:19:22 -050050using namespace shm_event_loop_internal;
51
Austin Schuhcdab6192019-12-29 17:47:46 -080052void SetShmBase(const std::string_view base) {
53 FLAGS_shm_base = std::string(base) + "/dev/shm/aos";
54}
55
Brian Silverman4f4e0612020-08-12 19:54:41 -070056namespace {
57
Alex Perrycb7da4b2019-08-28 19:35:56 -070058std::string ShmFolder(const Channel *channel) {
59 CHECK(channel->has_name());
60 CHECK_EQ(channel->name()->string_view()[0], '/');
61 return FLAGS_shm_base + channel->name()->str() + "/";
62}
63std::string ShmPath(const Channel *channel) {
64 CHECK(channel->has_type());
Brian Silverman177567e2020-08-12 19:51:33 -070065 return ShmFolder(channel) + channel->type()->str() + ".v3";
Alex Perrycb7da4b2019-08-28 19:35:56 -070066}
67
Brian Silverman3b0cdaf2020-04-28 16:51:51 -070068void PageFaultData(char *data, size_t size) {
69 // This just has to divide the actual page size. Being smaller will make this
70 // a bit slower than necessary, but not much. 1024 is a pretty conservative
71 // choice (most pages are probably 4096).
72 static constexpr size_t kPageSize = 1024;
73 const size_t pages = (size + kPageSize - 1) / kPageSize;
74 for (size_t i = 0; i < pages; ++i) {
75 char zero = 0;
76 // We need to ensure there's a writable pagetable entry, but avoid modifying
77 // the data.
78 //
79 // Even if you lock the data into memory, some kernels still seem to lazily
80 // create the actual pagetable entries. This means we need to somehow
81 // "write" to the page.
82 //
83 // Also, this takes place while other processes may be concurrently
84 // opening/initializing the memory, so we need to avoid corrupting that.
85 //
86 // This is the simplest operation I could think of which achieves that:
87 // "store 0 if it's already 0".
88 __atomic_compare_exchange_n(&data[i * kPageSize], &zero, 0, true,
89 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
90 }
91}
92
Brian Silverman4f4e0612020-08-12 19:54:41 -070093ipc_lib::LocklessQueueConfiguration MakeQueueConfiguration(
94 const Channel *channel, std::chrono::seconds channel_storage_duration) {
95 ipc_lib::LocklessQueueConfiguration config;
96
97 config.num_watchers = channel->num_watchers();
98 config.num_senders = channel->num_senders();
99 // The value in the channel will default to 0 if readers are configured to
100 // copy.
101 config.num_pinners = channel->num_readers();
102 config.queue_size = channel_storage_duration.count() * channel->frequency();
103 config.message_data_size = channel->max_size();
104
105 return config;
106}
107
Alex Perrycb7da4b2019-08-28 19:35:56 -0700108class MMapedQueue {
109 public:
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800110 MMapedQueue(const Channel *channel,
Brian Silverman4f4e0612020-08-12 19:54:41 -0700111 std::chrono::seconds channel_storage_duration)
112 : config_(MakeQueueConfiguration(channel, channel_storage_duration)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700113 std::string path = ShmPath(channel);
114
Alex Perrycb7da4b2019-08-28 19:35:56 -0700115 size_ = ipc_lib::LocklessQueueMemorySize(config_);
116
Austin Schuhfccb2d02020-01-26 16:11:19 -0800117 util::MkdirP(path, FLAGS_permissions);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700118
119 // There are 2 cases. Either the file already exists, or it does not
120 // already exist and we need to create it. Start by trying to create it. If
121 // that fails, the file has already been created and we can open it
Brian Silverman4f4e0612020-08-12 19:54:41 -0700122 // normally.. Once the file has been created it will never be deleted.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800123 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_EXCL,
Brian Silverman148d43d2020-06-07 18:19:22 -0500124 O_CLOEXEC | FLAGS_permissions);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800125 if (fd == -1 && errno == EEXIST) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126 VLOG(1) << path << " already created.";
127 // File already exists.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800128 fd = open(path.c_str(), O_RDWR, O_CLOEXEC);
129 PCHECK(fd != -1) << ": Failed to open " << path;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 while (true) {
131 struct stat st;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800132 PCHECK(fstat(fd, &st) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700133 if (st.st_size != 0) {
134 CHECK_EQ(static_cast<size_t>(st.st_size), size_)
135 << ": Size of " << path
136 << " doesn't match expected size of backing queue file. Did the "
137 "queue definition change?";
138 break;
139 } else {
140 // The creating process didn't get around to it yet. Give it a bit.
141 std::this_thread::sleep_for(std::chrono::milliseconds(10));
142 VLOG(1) << path << " is zero size, waiting";
143 }
144 }
145 } else {
146 VLOG(1) << "Created " << path;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800147 PCHECK(fd != -1) << ": Failed to open " << path;
148 PCHECK(ftruncate(fd, size_) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700149 }
150
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800151 data_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700152 PCHECK(data_ != MAP_FAILED);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800153 PCHECK(close(fd) == 0);
Brian Silverman3b0cdaf2020-04-28 16:51:51 -0700154 PageFaultData(static_cast<char *>(data_), size_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155
156 ipc_lib::InitializeLocklessQueueMemory(memory(), config_);
157 }
158
Brian Silverman148d43d2020-06-07 18:19:22 -0500159 ~MMapedQueue() { PCHECK(munmap(data_, size_) == 0); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160
161 ipc_lib::LocklessQueueMemory *memory() const {
162 return reinterpret_cast<ipc_lib::LocklessQueueMemory *>(data_);
163 }
164
Austin Schuh39788ff2019-12-01 18:22:57 -0800165 const ipc_lib::LocklessQueueConfiguration &config() const { return config_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700167 ipc_lib::LocklessQueue queue() const {
168 return ipc_lib::LocklessQueue(memory(), memory(), config());
169 }
170
Brian Silverman5120afb2020-01-31 17:44:35 -0800171 absl::Span<char> GetSharedMemory() const {
172 return absl::Span<char>(static_cast<char *>(data_), size_);
173 }
174
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175 private:
Brian Silverman4f4e0612020-08-12 19:54:41 -0700176 const ipc_lib::LocklessQueueConfiguration config_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700177
Alex Perrycb7da4b2019-08-28 19:35:56 -0700178 size_t size_;
179 void *data_;
180};
181
Austin Schuh217a9782019-12-21 23:02:50 -0800182const Node *MaybeMyNode(const Configuration *configuration) {
183 if (!configuration->has_nodes()) {
184 return nullptr;
185 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186
Austin Schuh217a9782019-12-21 23:02:50 -0800187 return configuration::GetMyNode(configuration);
188}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189
190namespace chrono = ::std::chrono;
191
Austin Schuh39788ff2019-12-01 18:22:57 -0800192} // namespace
193
Austin Schuh217a9782019-12-21 23:02:50 -0800194ShmEventLoop::ShmEventLoop(const Configuration *configuration)
195 : EventLoop(configuration),
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800196 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -0800197 node_(MaybeMyNode(configuration)) {
198 if (configuration->has_nodes()) {
199 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
200 }
201}
Austin Schuh217a9782019-12-21 23:02:50 -0800202
Brian Silverman148d43d2020-06-07 18:19:22 -0500203namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -0800204
205class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700206 public:
Brian Silverman3bca5322020-08-12 19:35:29 -0700207 explicit SimpleShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700208 : event_loop_(event_loop),
209 channel_(channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800210 lockless_queue_memory_(
211 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800212 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800213 event_loop->configuration()->channel_storage_duration()))),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700214 reader_(lockless_queue_memory_.queue()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700215 context_.data = nullptr;
216 // Point the queue index at the next index to read starting now. This
217 // makes it such that FetchNext will read the next message sent after
218 // the fetcher is created.
219 PointAtNextQueueIndex();
220 }
221
Austin Schuh39788ff2019-12-01 18:22:57 -0800222 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700223
Brian Silverman77162972020-08-12 19:52:40 -0700224 // Sets this object to pin or copy data, as configured in the channel.
225 void RetrieveData() {
226 if (channel_->read_method() == ReadMethod::PIN) {
227 PinDataOnFetch();
228 } else {
229 CopyDataOnFetch();
230 }
231 }
232
Brian Silverman3bca5322020-08-12 19:35:29 -0700233 // Sets this object to copy data out of the shared memory into a private
234 // buffer when fetching.
235 void CopyDataOnFetch() {
Brian Silverman77162972020-08-12 19:52:40 -0700236 CHECK(!pin_data());
Brian Silverman3bca5322020-08-12 19:35:29 -0700237 data_storage_.reset(static_cast<char *>(
238 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
239 }
240
Brian Silverman77162972020-08-12 19:52:40 -0700241 // Sets this object to pin data in shared memory when fetching.
242 void PinDataOnFetch() {
243 CHECK(!copy_data());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700244 auto maybe_pinner =
245 ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue());
Brian Silverman77162972020-08-12 19:52:40 -0700246 if (!maybe_pinner) {
247 LOG(FATAL) << "Failed to create reader on "
248 << configuration::CleanedChannelToString(channel_)
249 << ", too many readers.";
250 }
251 pinner_ = std::move(maybe_pinner.value());
252 }
253
Alex Perrycb7da4b2019-08-28 19:35:56 -0700254 // Points the next message to fetch at the queue index which will be
255 // populated next.
256 void PointAtNextQueueIndex() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700257 actual_queue_index_ = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700258 if (!actual_queue_index_.valid()) {
259 // Nothing in the queue. The next element will show up at the 0th
260 // index in the queue.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700261 actual_queue_index_ = ipc_lib::QueueIndex::Zero(
262 LocklessQueueSize(lockless_queue_memory_.memory()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700263 } else {
264 actual_queue_index_ = actual_queue_index_.Increment();
265 }
266 }
267
Austin Schuh39788ff2019-12-01 18:22:57 -0800268 bool FetchNext() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700269 const ipc_lib::LocklessQueueReader::Result read_result =
Brian Silverman3bca5322020-08-12 19:35:29 -0700270 DoFetch(actual_queue_index_);
Austin Schuh432784f2020-06-23 17:27:35 -0700271
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700272 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 }
274
Austin Schuh39788ff2019-12-01 18:22:57 -0800275 bool Fetch() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700276 const ipc_lib::QueueIndex queue_index = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277 // actual_queue_index_ is only meaningful if it was set by Fetch or
278 // FetchNext. This happens when valid_data_ has been set. So, only
279 // skip checking if valid_data_ is true.
280 //
281 // Also, if the latest queue index is invalid, we are empty. So there
282 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800283 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 queue_index == actual_queue_index_.DecrementBy(1u)) ||
285 !queue_index.valid()) {
286 return false;
287 }
288
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700289 const ipc_lib::LocklessQueueReader::Result read_result =
290 DoFetch(queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700291
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700292 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800293 << ": Queue index went backwards. This should never happen. "
294 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700296 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297 }
298
Austin Schuh39788ff2019-12-01 18:22:57 -0800299 Context context() const { return context_; }
300
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 bool RegisterWakeup(int priority) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700302 CHECK(!watcher_);
303 watcher_ = ipc_lib::LocklessQueueWatcher::Make(
304 lockless_queue_memory_.queue(), priority);
305 return static_cast<bool>(watcher_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700306 }
307
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700308 void UnregisterWakeup() {
309 CHECK(watcher_);
310 watcher_ = std::nullopt;
311 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700312
Brian Silverman5120afb2020-01-31 17:44:35 -0800313 absl::Span<char> GetSharedMemory() const {
314 return lockless_queue_memory_.GetSharedMemory();
315 }
316
Brian Silverman6d2b3592020-06-18 14:40:15 -0700317 absl::Span<char> GetPrivateMemory() const {
Brian Silverman3bca5322020-08-12 19:35:29 -0700318 // Can't usefully expose this for pinning, because the buffer changes
319 // address for each message. Callers who want to work with that should just
320 // grab the whole shared memory buffer instead.
Brian Silverman6d2b3592020-06-18 14:40:15 -0700321 return absl::Span<char>(
322 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700323 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()));
Brian Silverman6d2b3592020-06-18 14:40:15 -0700324 }
325
Alex Perrycb7da4b2019-08-28 19:35:56 -0700326 private:
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700327 ipc_lib::LocklessQueueReader::Result DoFetch(
328 ipc_lib::QueueIndex queue_index) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700329 // TODO(austin): Get behind and make sure it dies.
330 char *copy_buffer = nullptr;
331 if (copy_data()) {
332 copy_buffer = data_storage_start();
333 }
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700334 ipc_lib::LocklessQueueReader::Result read_result = reader_.Read(
Brian Silverman3bca5322020-08-12 19:35:29 -0700335 queue_index.index(), &context_.monotonic_event_time,
336 &context_.realtime_event_time, &context_.monotonic_remote_time,
337 &context_.realtime_remote_time, &context_.remote_queue_index,
338 &context_.size, copy_buffer);
339
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700340 if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) {
Brian Silverman77162972020-08-12 19:52:40 -0700341 if (pin_data()) {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700342 const int pin_result = pinner_->PinIndex(queue_index.index());
343 CHECK(pin_result >= 0)
Brian Silverman77162972020-08-12 19:52:40 -0700344 << ": Got behind while reading and the last message was modified "
345 "out from under us while we tried to pin it. Don't get so far "
346 "behind on: "
347 << configuration::CleanedChannelToString(channel_);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700348 context_.buffer_index = pin_result;
349 } else {
350 context_.buffer_index = -1;
Brian Silverman77162972020-08-12 19:52:40 -0700351 }
352
Brian Silverman3bca5322020-08-12 19:35:29 -0700353 context_.queue_index = queue_index.index();
354 if (context_.remote_queue_index == 0xffffffffu) {
355 context_.remote_queue_index = context_.queue_index;
356 }
357 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
358 context_.monotonic_remote_time = context_.monotonic_event_time;
359 }
360 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
361 context_.realtime_remote_time = context_.realtime_event_time;
362 }
363 const char *const data = DataBuffer();
364 if (data) {
365 context_.data =
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700366 data +
367 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) -
368 context_.size;
Brian Silverman3bca5322020-08-12 19:35:29 -0700369 } else {
370 context_.data = nullptr;
371 }
372 actual_queue_index_ = queue_index.Increment();
373 }
374
375 // Make sure the data wasn't modified while we were reading it. This
376 // can only happen if you are reading the last message *while* it is
377 // being written to, which means you are pretty far behind.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700378 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE)
Brian Silverman3bca5322020-08-12 19:35:29 -0700379 << ": Got behind while reading and the last message was modified "
380 "out from under us while we were reading it. Don't get so far "
381 "behind on: "
382 << configuration::CleanedChannelToString(channel_);
383
384 // We fell behind between when we read the index and read the value.
385 // This isn't worth recovering from since this means we went to sleep
386 // for a long time in the middle of this function.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700387 if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700388 event_loop_->SendTimingReport();
389 LOG(FATAL) << "The next message is no longer available. "
390 << configuration::CleanedChannelToString(channel_);
391 }
392
393 return read_result;
394 }
395
396 char *data_storage_start() const {
397 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800398 return RoundChannelData(data_storage_.get(), channel_->max_size());
399 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700400
401 // Note that for some modes the return value will change as new messages are
402 // read.
403 const char *DataBuffer() const {
404 if (copy_data()) {
405 return data_storage_start();
406 }
Brian Silverman77162972020-08-12 19:52:40 -0700407 if (pin_data()) {
408 return static_cast<const char *>(pinner_->Data());
409 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700410 return nullptr;
411 }
412
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800413 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silverman77162972020-08-12 19:52:40 -0700414 bool pin_data() const { return static_cast<bool>(pinner_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800415
Austin Schuh432784f2020-06-23 17:27:35 -0700416 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800417 const Channel *const channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700418 MMapedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700419 ipc_lib::LocklessQueueReader reader_;
420 // This being nullopt indicates we're not looking for wakeups right now.
421 std::optional<ipc_lib::LocklessQueueWatcher> watcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700422
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700423 ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700424
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800425 // This being empty indicates we're not going to copy data.
426 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800427
Brian Silverman77162972020-08-12 19:52:40 -0700428 // This being nullopt indicates we're not going to pin messages.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700429 std::optional<ipc_lib::LocklessQueuePinner> pinner_;
Brian Silverman77162972020-08-12 19:52:40 -0700430
Austin Schuh39788ff2019-12-01 18:22:57 -0800431 Context context_;
432};
433
434class ShmFetcher : public RawFetcher {
435 public:
Austin Schuh432784f2020-06-23 17:27:35 -0700436 explicit ShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800437 : RawFetcher(event_loop, channel),
Brian Silverman3bca5322020-08-12 19:35:29 -0700438 simple_shm_fetcher_(event_loop, channel) {
Brian Silverman77162972020-08-12 19:52:40 -0700439 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700440 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800441
442 ~ShmFetcher() { context_.data = nullptr; }
443
444 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
445 if (simple_shm_fetcher_.FetchNext()) {
446 context_ = simple_shm_fetcher_.context();
447 return std::make_pair(true, monotonic_clock::now());
448 }
449 return std::make_pair(false, monotonic_clock::min_time);
450 }
451
452 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
453 if (simple_shm_fetcher_.Fetch()) {
454 context_ = simple_shm_fetcher_.context();
455 return std::make_pair(true, monotonic_clock::now());
456 }
457 return std::make_pair(false, monotonic_clock::min_time);
458 }
459
Brian Silverman6d2b3592020-06-18 14:40:15 -0700460 absl::Span<char> GetPrivateMemory() const {
461 return simple_shm_fetcher_.GetPrivateMemory();
462 }
463
Austin Schuh39788ff2019-12-01 18:22:57 -0800464 private:
465 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466};
467
468class ShmSender : public RawSender {
469 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800470 explicit ShmSender(EventLoop *event_loop, const Channel *channel)
471 : RawSender(event_loop, channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800472 lockless_queue_memory_(
473 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800474 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800475 event_loop->configuration()->channel_storage_duration()))),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700476 lockless_queue_sender_(VerifySender(
477 ipc_lib::LocklessQueueSender::Make(lockless_queue_memory_.queue()),
478 channel)),
479 wake_upper_(lockless_queue_memory_.queue()) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480
Austin Schuh39788ff2019-12-01 18:22:57 -0800481 ~ShmSender() override {}
482
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700483 static ipc_lib::LocklessQueueSender VerifySender(
484 std::optional<ipc_lib::LocklessQueueSender> sender,
Austin Schuhe516ab02020-05-06 21:37:04 -0700485 const Channel *channel) {
486 if (sender) {
487 return std::move(sender.value());
488 }
489 LOG(FATAL) << "Failed to create sender on "
490 << configuration::CleanedChannelToString(channel)
491 << ", too many senders.";
492 }
493
Alex Perrycb7da4b2019-08-28 19:35:56 -0700494 void *data() override { return lockless_queue_sender_.Data(); }
495 size_t size() override { return lockless_queue_sender_.size(); }
Austin Schuhad154822019-12-27 15:45:13 -0800496 bool DoSend(size_t length,
497 aos::monotonic_clock::time_point monotonic_remote_time,
498 aos::realtime_clock::time_point realtime_remote_time,
499 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700500 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
501 << ": Sent too big a message on "
502 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800503 lockless_queue_sender_.Send(
504 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
505 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700506 wake_upper_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700507 return true;
508 }
509
Austin Schuhad154822019-12-27 15:45:13 -0800510 bool DoSend(const void *msg, size_t length,
511 aos::monotonic_clock::time_point monotonic_remote_time,
512 aos::realtime_clock::time_point realtime_remote_time,
513 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700514 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
515 << ": Sent too big a message on "
516 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800517 lockless_queue_sender_.Send(reinterpret_cast<const char *>(msg), length,
518 monotonic_remote_time, realtime_remote_time,
519 remote_queue_index, &monotonic_sent_time_,
520 &realtime_sent_time_, &sent_queue_index_);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700521 wake_upper_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700522 // TODO(austin): Return an error if we send too fast.
523 return true;
524 }
525
Brian Silverman5120afb2020-01-31 17:44:35 -0800526 absl::Span<char> GetSharedMemory() const {
527 return lockless_queue_memory_.GetSharedMemory();
528 }
529
Brian Silverman4f4e0612020-08-12 19:54:41 -0700530 int buffer_index() override { return lockless_queue_sender_.buffer_index(); }
531
Alex Perrycb7da4b2019-08-28 19:35:56 -0700532 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700533 MMapedQueue 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 Schuh7d87b672019-12-01 20:23:49 -0800542 ShmEventLoop *event_loop, const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800543 std::function<void(const Context &context, const void *message)> fn,
544 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500545 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800546 event_loop_(event_loop),
547 event_(this),
Brian Silverman3bca5322020-08-12 19:35:29 -0700548 simple_shm_fetcher_(event_loop, channel) {
549 if (copy_data) {
Brian Silverman77162972020-08-12 19:52:40 -0700550 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700551 }
552 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700553
Brian Silverman148d43d2020-06-07 18:19:22 -0500554 ~ShmWatcherState() override { event_loop_->RemoveEvent(&event_); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800555
556 void Startup(EventLoop *event_loop) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800557 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800558 CHECK(RegisterWakeup(event_loop->priority()));
559 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700560
Alex Perrycb7da4b2019-08-28 19:35:56 -0700561 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800562 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700563 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800564 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800565
566 if (has_new_data_) {
567 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800568 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800569 event_loop_->AddEvent(&event_);
570 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700571 }
572
573 return has_new_data_;
574 }
575
Alex Perrycb7da4b2019-08-28 19:35:56 -0700576 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800577 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700578 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800579 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700580 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800581 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700582 }
583
Austin Schuh39788ff2019-12-01 18:22:57 -0800584 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700585 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800586 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700587 }
588
Austin Schuh39788ff2019-12-01 18:22:57 -0800589 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700590
Brian Silverman5120afb2020-01-31 17:44:35 -0800591 absl::Span<char> GetSharedMemory() const {
592 return simple_shm_fetcher_.GetSharedMemory();
593 }
594
Alex Perrycb7da4b2019-08-28 19:35:56 -0700595 private:
596 bool has_new_data_ = false;
597
Austin Schuh7d87b672019-12-01 20:23:49 -0800598 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500599 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800600 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700601};
602
603// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500604class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700605 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500606 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800607 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800608 shm_event_loop_(shm_event_loop),
609 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800610 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
611 // The timer may fire spurriously. HandleEvent on the event loop will
612 // call the callback if it is needed. It may also have called it when
613 // processing some other event, and the kernel decided to deliver this
614 // wakeup anyways.
615 timerfd_.Read();
616 shm_event_loop_->HandleEvent();
617 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700618 }
619
Brian Silverman148d43d2020-06-07 18:19:22 -0500620 ~ShmTimerHandler() {
Austin Schuh7d87b672019-12-01 20:23:49 -0800621 Disable();
622 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
623 }
624
625 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800626 CHECK(!event_.valid());
627 const auto monotonic_now = Call(monotonic_clock::now, base_);
628 if (event_.valid()) {
629 // If someone called Setup inside Call, rescheduling is already taken care
630 // of. Bail.
631 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800632 }
633
Austin Schuhcde39fd2020-02-22 20:58:24 -0800634 if (repeat_offset_ == chrono::seconds(0)) {
635 timerfd_.Disable();
636 } else {
637 // Compute how many cycles have elapsed and schedule the next iteration
638 // for the next iteration in the future.
639 const int elapsed_cycles =
640 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
641 std::chrono::nanoseconds(1)) /
642 repeat_offset_);
643 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800644
Austin Schuhcde39fd2020-02-22 20:58:24 -0800645 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800646 event_.set_event_time(base_);
647 shm_event_loop_->AddEvent(&event_);
Austin Schuhcde39fd2020-02-22 20:58:24 -0800648 timerfd_.SetTime(base_, chrono::seconds(0));
Austin Schuh7d87b672019-12-01 20:23:49 -0800649 }
650 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700651
652 void Setup(monotonic_clock::time_point base,
653 monotonic_clock::duration repeat_offset) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800654 if (event_.valid()) {
655 shm_event_loop_->RemoveEvent(&event_);
656 }
657
Alex Perrycb7da4b2019-08-28 19:35:56 -0700658 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800659 base_ = base;
660 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800661 event_.set_event_time(base_);
662 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700663 }
664
Austin Schuh7d87b672019-12-01 20:23:49 -0800665 void Disable() override {
666 shm_event_loop_->RemoveEvent(&event_);
667 timerfd_.Disable();
668 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700669
670 private:
671 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500672 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700673
Brian Silverman148d43d2020-06-07 18:19:22 -0500674 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700675
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800676 monotonic_clock::time_point base_;
677 monotonic_clock::duration repeat_offset_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700678};
679
680// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500681class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700682 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500683 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
684 ::std::function<void(int)> fn,
685 const monotonic_clock::duration interval,
686 const monotonic_clock::duration offset)
687 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800688 shm_event_loop_(shm_event_loop),
689 event_(this) {
690 shm_event_loop_->epoll_.OnReadable(
691 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
692 }
693
694 void HandleEvent() {
695 // The return value for read is the number of cycles that have elapsed.
696 // Because we check to see when this event *should* have happened, there are
697 // cases where Read() will return 0, when 1 cycle has actually happened.
698 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
699 // ignore it. Call handles rescheduling and calculating elapsed cycles
700 // without any extra help.
701 timerfd_.Read();
702 event_.Invalidate();
703
704 Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) {
705 Schedule(sleep_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700706 });
707 }
708
Brian Silverman148d43d2020-06-07 18:19:22 -0500709 ~ShmPhasedLoopHandler() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800710 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800711 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700712 }
713
714 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800715 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800716 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800717 if (event_.valid()) {
718 shm_event_loop_->RemoveEvent(&event_);
719 }
720
Austin Schuh39788ff2019-12-01 18:22:57 -0800721 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800722 event_.set_event_time(sleep_time);
723 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700724 }
725
726 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500727 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700728
Brian Silverman148d43d2020-06-07 18:19:22 -0500729 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700730};
Brian Silverman148d43d2020-06-07 18:19:22 -0500731
732} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700733
734::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
735 const Channel *channel) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800736 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
737 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
738 << "\", \"type\": \"" << channel->type()->string_view()
739 << "\" } is not able to be fetched on this node. Check your "
740 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800741 }
742
Brian Silverman148d43d2020-06-07 18:19:22 -0500743 return ::std::unique_ptr<RawFetcher>(new ShmFetcher(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700744}
745
746::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
747 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800748 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800749
Brian Silverman148d43d2020-06-07 18:19:22 -0500750 return ::std::unique_ptr<RawSender>(new ShmSender(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700751}
752
753void ShmEventLoop::MakeRawWatcher(
754 const Channel *channel,
755 std::function<void(const Context &context, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800756 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800757
Austin Schuh39788ff2019-12-01 18:22:57 -0800758 NewWatcher(::std::unique_ptr<WatcherState>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500759 new ShmWatcherState(this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800760}
761
762void ShmEventLoop::MakeRawNoArgWatcher(
763 const Channel *channel,
764 std::function<void(const Context &context)> watcher) {
765 TakeWatcher(channel);
766
Brian Silverman148d43d2020-06-07 18:19:22 -0500767 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800768 this, channel,
769 [watcher](const Context &context, const void *) { watcher(context); },
770 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700771}
772
773TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800774 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500775 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700776}
777
778PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
779 ::std::function<void(int)> callback,
780 const monotonic_clock::duration interval,
781 const monotonic_clock::duration offset) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500782 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
783 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700784}
785
786void ShmEventLoop::OnRun(::std::function<void()> on_run) {
787 on_run_.push_back(::std::move(on_run));
788}
789
Austin Schuh7d87b672019-12-01 20:23:49 -0800790void ShmEventLoop::HandleEvent() {
791 // Update all the times for handlers.
792 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500793 ShmWatcherState *watcher =
794 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Austin Schuh7d87b672019-12-01 20:23:49 -0800795
796 watcher->CheckForNewData();
797 }
798
Austin Schuh39788ff2019-12-01 18:22:57 -0800799 while (true) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800800 if (EventCount() == 0 ||
801 PeekEvent()->event_time() > monotonic_clock::now()) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800802 break;
803 }
804
Austin Schuh7d87b672019-12-01 20:23:49 -0800805 EventLoopEvent *event = PopEvent();
806 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800807 }
808}
809
Austin Schuh32fd5a72019-12-01 22:20:26 -0800810// RAII class to mask signals.
811class ScopedSignalMask {
812 public:
813 ScopedSignalMask(std::initializer_list<int> signals) {
814 sigset_t sigset;
815 PCHECK(sigemptyset(&sigset) == 0);
816 for (int signal : signals) {
817 PCHECK(sigaddset(&sigset, signal) == 0);
818 }
819
820 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
821 }
822
823 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
824
825 private:
826 sigset_t old_;
827};
828
829// Class to manage the static state associated with killing multiple event
830// loops.
831class SignalHandler {
832 public:
833 // Gets the singleton.
834 static SignalHandler *global() {
835 static SignalHandler loop;
836 return &loop;
837 }
838
839 // Handles the signal with the singleton.
840 static void HandleSignal(int) { global()->DoHandleSignal(); }
841
842 // Registers an event loop to receive Exit() calls.
843 void Register(ShmEventLoop *event_loop) {
844 // Block signals while we have the mutex so we never race with the signal
845 // handler.
846 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
847 std::unique_lock<stl_mutex> locker(mutex_);
848 if (event_loops_.size() == 0) {
849 // The first caller registers the signal handler.
850 struct sigaction new_action;
851 sigemptyset(&new_action.sa_mask);
852 // This makes it so that 2 control c's to a stuck process will kill it by
853 // restoring the original signal handler.
854 new_action.sa_flags = SA_RESETHAND;
855 new_action.sa_handler = &HandleSignal;
856
857 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
858 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
859 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
860 }
861
862 event_loops_.push_back(event_loop);
863 }
864
865 // Unregisters an event loop to receive Exit() calls.
866 void Unregister(ShmEventLoop *event_loop) {
867 // Block signals while we have the mutex so we never race with the signal
868 // handler.
869 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
870 std::unique_lock<stl_mutex> locker(mutex_);
871
Brian Silverman5120afb2020-01-31 17:44:35 -0800872 event_loops_.erase(
873 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800874
875 if (event_loops_.size() == 0u) {
876 // The last caller restores the original signal handlers.
877 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
878 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
879 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
880 }
881 }
882
883 private:
884 void DoHandleSignal() {
885 // We block signals while grabbing the lock, so there should never be a
886 // race. Confirm that this is true using trylock.
887 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
888 "modifing the event loop list.";
889 for (ShmEventLoop *event_loop : event_loops_) {
890 event_loop->Exit();
891 }
892 mutex_.unlock();
893 }
894
895 // Mutex to protect all state.
896 stl_mutex mutex_;
897 std::vector<ShmEventLoop *> event_loops_;
898 struct sigaction old_action_int_;
899 struct sigaction old_action_hup_;
900 struct sigaction old_action_term_;
901};
902
Alex Perrycb7da4b2019-08-28 19:35:56 -0700903void ShmEventLoop::Run() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800904 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -0800905
Alex Perrycb7da4b2019-08-28 19:35:56 -0700906 std::unique_ptr<ipc_lib::SignalFd> signalfd;
907
908 if (watchers_.size() > 0) {
909 signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
910
911 epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() {
912 signalfd_siginfo result = signalfd_ptr->Read();
913 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
914
915 // TODO(austin): We should really be checking *everything*, not just
916 // watchers, and calling the oldest thing first. That will improve
917 // determinism a lot.
918
Austin Schuh7d87b672019-12-01 20:23:49 -0800919 HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700920 });
921 }
922
Austin Schuh39788ff2019-12-01 18:22:57 -0800923 MaybeScheduleTimingReports();
924
Austin Schuh7d87b672019-12-01 20:23:49 -0800925 ReserveEvents();
926
Tyler Chatow67ddb032020-01-12 14:30:04 -0800927 {
928 AosLogToFbs aos_logger;
929 if (!skip_logger_) {
930 aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
931 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700932
Tyler Chatow67ddb032020-01-12 14:30:04 -0800933 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -0700934 const cpu_set_t default_affinity = DefaultAffinity();
935 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
936 ::aos::SetCurrentThreadAffinity(affinity_);
937 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800938 // Now, all the callbacks are setup. Lock everything into memory and go RT.
939 if (priority_ != 0) {
940 ::aos::InitRT();
941
942 LOG(INFO) << "Setting priority to " << priority_;
943 ::aos::SetCurrentThreadRealtimePriority(priority_);
944 }
945
946 set_is_running(true);
947
948 // Now that we are realtime (but before the OnRun handlers run), snap the
949 // queue index.
950 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
951 watcher->Startup(this);
952 }
953
954 // Now that we are RT, run all the OnRun handlers.
955 for (const auto &run : on_run_) {
956 run();
957 }
958
959 // And start our main event loop which runs all the timers and handles Quit.
960 epoll_.Run();
961
962 // Once epoll exits, there is no useful nonrt work left to do.
963 set_is_running(false);
964
965 // Nothing time or synchronization critical needs to happen after this
966 // point. Drop RT priority.
967 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700968 }
969
Austin Schuh39788ff2019-12-01 18:22:57 -0800970 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500971 ShmWatcherState *watcher =
972 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700973 watcher->UnregisterWakeup();
974 }
975
976 if (watchers_.size() > 0) {
977 epoll_.DeleteFd(signalfd->fd());
978 signalfd.reset();
979 }
Austin Schuh32fd5a72019-12-01 22:20:26 -0800980
981 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800982
983 // Trigger any remaining senders or fetchers to be cleared before destroying
984 // the event loop so the book keeping matches. Do this in the thread that
985 // created the timing reporter.
986 timing_report_sender_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700987}
988
989void ShmEventLoop::Exit() { epoll_.Quit(); }
990
991ShmEventLoop::~ShmEventLoop() {
Austin Schuh39788ff2019-12-01 18:22:57 -0800992 // Force everything with a registered fd with epoll to be destroyed now.
993 timers_.clear();
994 phased_loops_.clear();
995 watchers_.clear();
996
Alex Perrycb7da4b2019-08-28 19:35:56 -0700997 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
998}
999
Alex Perrycb7da4b2019-08-28 19:35:56 -07001000void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
1001 if (is_running()) {
1002 LOG(FATAL) << "Cannot set realtime priority while running.";
1003 }
1004 priority_ = priority;
1005}
1006
Brian Silverman6a54ff32020-04-28 16:41:39 -07001007void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
1008 if (is_running()) {
1009 LOG(FATAL) << "Cannot set affinity while running.";
1010 }
1011 affinity_ = cpuset;
1012}
1013
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001014void ShmEventLoop::set_name(const std::string_view name) {
1015 name_ = std::string(name);
1016 UpdateTimingReport();
1017}
1018
Brian Silverman5120afb2020-01-31 17:44:35 -08001019absl::Span<char> ShmEventLoop::GetWatcherSharedMemory(const Channel *channel) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001020 ShmWatcherState *const watcher_state =
1021 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -08001022 return watcher_state->GetSharedMemory();
1023}
1024
Brian Silverman4f4e0612020-08-12 19:54:41 -07001025int ShmEventLoop::NumberBuffers(const Channel *channel) {
1026 return MakeQueueConfiguration(
1027 channel, chrono::ceil<chrono::seconds>(chrono::nanoseconds(
1028 configuration()->channel_storage_duration())))
1029 .num_messages();
1030}
1031
Brian Silverman5120afb2020-01-31 17:44:35 -08001032absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
1033 const aos::RawSender *sender) const {
Brian Silverman148d43d2020-06-07 18:19:22 -05001034 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -08001035}
1036
Brian Silverman6d2b3592020-06-18 14:40:15 -07001037absl::Span<char> ShmEventLoop::GetShmFetcherPrivateMemory(
1038 const aos::RawFetcher *fetcher) const {
1039 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
1040}
1041
Austin Schuh39788ff2019-12-01 18:22:57 -08001042pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
1043
Alex Perrycb7da4b2019-08-28 19:35:56 -07001044} // namespace aos