blob: 0aa82a1d5b70c95c4902c3bf719c50fc93c8e129 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -070056std::string ShmFolder(const Channel *channel) {
57 CHECK(channel->has_name());
58 CHECK_EQ(channel->name()->string_view()[0], '/');
59 return FLAGS_shm_base + channel->name()->str() + "/";
60}
61std::string ShmPath(const Channel *channel) {
62 CHECK(channel->has_type());
Brian Silverman177567e2020-08-12 19:51:33 -070063 return ShmFolder(channel) + channel->type()->str() + ".v3";
Alex Perrycb7da4b2019-08-28 19:35:56 -070064}
65
Brian Silverman3b0cdaf2020-04-28 16:51:51 -070066void PageFaultData(char *data, size_t size) {
67 // This just has to divide the actual page size. Being smaller will make this
68 // a bit slower than necessary, but not much. 1024 is a pretty conservative
69 // choice (most pages are probably 4096).
70 static constexpr size_t kPageSize = 1024;
71 const size_t pages = (size + kPageSize - 1) / kPageSize;
72 for (size_t i = 0; i < pages; ++i) {
73 char zero = 0;
74 // We need to ensure there's a writable pagetable entry, but avoid modifying
75 // the data.
76 //
77 // Even if you lock the data into memory, some kernels still seem to lazily
78 // create the actual pagetable entries. This means we need to somehow
79 // "write" to the page.
80 //
81 // Also, this takes place while other processes may be concurrently
82 // opening/initializing the memory, so we need to avoid corrupting that.
83 //
84 // This is the simplest operation I could think of which achieves that:
85 // "store 0 if it's already 0".
86 __atomic_compare_exchange_n(&data[i * kPageSize], &zero, 0, true,
87 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
88 }
89}
90
Alex Perrycb7da4b2019-08-28 19:35:56 -070091class MMapedQueue {
92 public:
Austin Schuhaa79e4e2019-12-29 20:43:32 -080093 MMapedQueue(const Channel *channel,
94 const std::chrono::seconds channel_storage_duration) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 std::string path = ShmPath(channel);
96
Austin Schuh80c7fce2019-12-05 20:48:43 -080097 config_.num_watchers = channel->num_watchers();
98 config_.num_senders = channel->num_senders();
Brian Silverman177567e2020-08-12 19:51:33 -070099 config_.num_pinners = 0;
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800100 config_.queue_size =
101 channel_storage_duration.count() * channel->frequency();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102 config_.message_data_size = channel->max_size();
103
104 size_ = ipc_lib::LocklessQueueMemorySize(config_);
105
Austin Schuhfccb2d02020-01-26 16:11:19 -0800106 util::MkdirP(path, FLAGS_permissions);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107
108 // There are 2 cases. Either the file already exists, or it does not
109 // already exist and we need to create it. Start by trying to create it. If
110 // that fails, the file has already been created and we can open it
111 // normally.. Once the file has been created it wil never be deleted.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800112 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_EXCL,
Brian Silverman148d43d2020-06-07 18:19:22 -0500113 O_CLOEXEC | FLAGS_permissions);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800114 if (fd == -1 && errno == EEXIST) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700115 VLOG(1) << path << " already created.";
116 // File already exists.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800117 fd = open(path.c_str(), O_RDWR, O_CLOEXEC);
118 PCHECK(fd != -1) << ": Failed to open " << path;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700119 while (true) {
120 struct stat st;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800121 PCHECK(fstat(fd, &st) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700122 if (st.st_size != 0) {
123 CHECK_EQ(static_cast<size_t>(st.st_size), size_)
124 << ": Size of " << path
125 << " doesn't match expected size of backing queue file. Did the "
126 "queue definition change?";
127 break;
128 } else {
129 // The creating process didn't get around to it yet. Give it a bit.
130 std::this_thread::sleep_for(std::chrono::milliseconds(10));
131 VLOG(1) << path << " is zero size, waiting";
132 }
133 }
134 } else {
135 VLOG(1) << "Created " << path;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800136 PCHECK(fd != -1) << ": Failed to open " << path;
137 PCHECK(ftruncate(fd, size_) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 }
139
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800140 data_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 PCHECK(data_ != MAP_FAILED);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800142 PCHECK(close(fd) == 0);
Brian Silverman3b0cdaf2020-04-28 16:51:51 -0700143 PageFaultData(static_cast<char *>(data_), size_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700144
145 ipc_lib::InitializeLocklessQueueMemory(memory(), config_);
146 }
147
Brian Silverman148d43d2020-06-07 18:19:22 -0500148 ~MMapedQueue() { PCHECK(munmap(data_, size_) == 0); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700149
150 ipc_lib::LocklessQueueMemory *memory() const {
151 return reinterpret_cast<ipc_lib::LocklessQueueMemory *>(data_);
152 }
153
Austin Schuh39788ff2019-12-01 18:22:57 -0800154 const ipc_lib::LocklessQueueConfiguration &config() const { return config_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155
Brian Silverman5120afb2020-01-31 17:44:35 -0800156 absl::Span<char> GetSharedMemory() const {
157 return absl::Span<char>(static_cast<char *>(data_), size_);
158 }
159
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 ipc_lib::LocklessQueueConfiguration config_;
162
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 size_t size_;
164 void *data_;
165};
166
Austin Schuh217a9782019-12-21 23:02:50 -0800167namespace {
168
Austin Schuh217a9782019-12-21 23:02:50 -0800169const Node *MaybeMyNode(const Configuration *configuration) {
170 if (!configuration->has_nodes()) {
171 return nullptr;
172 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173
Austin Schuh217a9782019-12-21 23:02:50 -0800174 return configuration::GetMyNode(configuration);
175}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176
177namespace chrono = ::std::chrono;
178
Austin Schuh39788ff2019-12-01 18:22:57 -0800179} // namespace
180
Austin Schuh217a9782019-12-21 23:02:50 -0800181ShmEventLoop::ShmEventLoop(const Configuration *configuration)
182 : EventLoop(configuration),
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800183 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -0800184 node_(MaybeMyNode(configuration)) {
185 if (configuration->has_nodes()) {
186 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
187 }
188}
Austin Schuh217a9782019-12-21 23:02:50 -0800189
Brian Silverman148d43d2020-06-07 18:19:22 -0500190namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -0800191
192class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 public:
Brian Silverman3bca5322020-08-12 19:35:29 -0700194 explicit SimpleShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700195 : event_loop_(event_loop),
196 channel_(channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800197 lockless_queue_memory_(
198 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800199 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800200 event_loop->configuration()->channel_storage_duration()))),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700201 lockless_queue_(lockless_queue_memory_.memory(),
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800202 lockless_queue_memory_.config()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700203 context_.data = nullptr;
204 // Point the queue index at the next index to read starting now. This
205 // makes it such that FetchNext will read the next message sent after
206 // the fetcher is created.
207 PointAtNextQueueIndex();
208 }
209
Austin Schuh39788ff2019-12-01 18:22:57 -0800210 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700211
Brian Silverman3bca5322020-08-12 19:35:29 -0700212 // Sets this object to copy data out of the shared memory into a private
213 // buffer when fetching.
214 void CopyDataOnFetch() {
215 data_storage_.reset(static_cast<char *>(
216 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
217 }
218
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219 // Points the next message to fetch at the queue index which will be
220 // populated next.
221 void PointAtNextQueueIndex() {
222 actual_queue_index_ = lockless_queue_.LatestQueueIndex();
223 if (!actual_queue_index_.valid()) {
224 // Nothing in the queue. The next element will show up at the 0th
225 // index in the queue.
226 actual_queue_index_ =
227 ipc_lib::QueueIndex::Zero(lockless_queue_.queue_size());
228 } else {
229 actual_queue_index_ = actual_queue_index_.Increment();
230 }
231 }
232
Austin Schuh39788ff2019-12-01 18:22:57 -0800233 bool FetchNext() {
Brian Silverman3bca5322020-08-12 19:35:29 -0700234 const ipc_lib::LocklessQueue::ReadResult read_result =
235 DoFetch(actual_queue_index_);
Austin Schuh432784f2020-06-23 17:27:35 -0700236
Alex Perrycb7da4b2019-08-28 19:35:56 -0700237 return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD;
238 }
239
Austin Schuh39788ff2019-12-01 18:22:57 -0800240 bool Fetch() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700241 const ipc_lib::QueueIndex queue_index = lockless_queue_.LatestQueueIndex();
242 // actual_queue_index_ is only meaningful if it was set by Fetch or
243 // FetchNext. This happens when valid_data_ has been set. So, only
244 // skip checking if valid_data_ is true.
245 //
246 // Also, if the latest queue index is invalid, we are empty. So there
247 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800248 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 queue_index == actual_queue_index_.DecrementBy(1u)) ||
250 !queue_index.valid()) {
251 return false;
252 }
253
Brian Silverman3bca5322020-08-12 19:35:29 -0700254 const ipc_lib::LocklessQueue::ReadResult read_result = DoFetch(queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700255
256 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800257 << ": Queue index went backwards. This should never happen. "
258 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260 return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD;
261 }
262
Austin Schuh39788ff2019-12-01 18:22:57 -0800263 Context context() const { return context_; }
264
Alex Perrycb7da4b2019-08-28 19:35:56 -0700265 bool RegisterWakeup(int priority) {
266 return lockless_queue_.RegisterWakeup(priority);
267 }
268
269 void UnregisterWakeup() { lockless_queue_.UnregisterWakeup(); }
270
Brian Silverman5120afb2020-01-31 17:44:35 -0800271 absl::Span<char> GetSharedMemory() const {
272 return lockless_queue_memory_.GetSharedMemory();
273 }
274
Brian Silverman6d2b3592020-06-18 14:40:15 -0700275 absl::Span<char> GetPrivateMemory() const {
Brian Silverman3bca5322020-08-12 19:35:29 -0700276 // Can't usefully expose this for pinning, because the buffer changes
277 // address for each message. Callers who want to work with that should just
278 // grab the whole shared memory buffer instead.
Brian Silverman6d2b3592020-06-18 14:40:15 -0700279 return absl::Span<char>(
280 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
281 lockless_queue_.message_data_size());
282 }
283
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 private:
Brian Silverman3bca5322020-08-12 19:35:29 -0700285 ipc_lib::LocklessQueue::ReadResult DoFetch(ipc_lib::QueueIndex queue_index) {
286 // TODO(austin): Get behind and make sure it dies.
287 char *copy_buffer = nullptr;
288 if (copy_data()) {
289 copy_buffer = data_storage_start();
290 }
291 ipc_lib::LocklessQueue::ReadResult read_result = lockless_queue_.Read(
292 queue_index.index(), &context_.monotonic_event_time,
293 &context_.realtime_event_time, &context_.monotonic_remote_time,
294 &context_.realtime_remote_time, &context_.remote_queue_index,
295 &context_.size, copy_buffer);
296
297 if (read_result == ipc_lib::LocklessQueue::ReadResult::GOOD) {
298 context_.queue_index = queue_index.index();
299 if (context_.remote_queue_index == 0xffffffffu) {
300 context_.remote_queue_index = context_.queue_index;
301 }
302 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
303 context_.monotonic_remote_time = context_.monotonic_event_time;
304 }
305 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
306 context_.realtime_remote_time = context_.realtime_event_time;
307 }
308 const char *const data = DataBuffer();
309 if (data) {
310 context_.data =
311 data + lockless_queue_.message_data_size() - context_.size;
312 } else {
313 context_.data = nullptr;
314 }
315 actual_queue_index_ = queue_index.Increment();
316 }
317
318 // Make sure the data wasn't modified while we were reading it. This
319 // can only happen if you are reading the last message *while* it is
320 // being written to, which means you are pretty far behind.
321 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::OVERWROTE)
322 << ": Got behind while reading and the last message was modified "
323 "out from under us while we were reading it. Don't get so far "
324 "behind on: "
325 << configuration::CleanedChannelToString(channel_);
326
327 // We fell behind between when we read the index and read the value.
328 // This isn't worth recovering from since this means we went to sleep
329 // for a long time in the middle of this function.
330 if (read_result == ipc_lib::LocklessQueue::ReadResult::TOO_OLD) {
331 event_loop_->SendTimingReport();
332 LOG(FATAL) << "The next message is no longer available. "
333 << configuration::CleanedChannelToString(channel_);
334 }
335
336 return read_result;
337 }
338
339 char *data_storage_start() const {
340 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800341 return RoundChannelData(data_storage_.get(), channel_->max_size());
342 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700343
344 // Note that for some modes the return value will change as new messages are
345 // read.
346 const char *DataBuffer() const {
347 if (copy_data()) {
348 return data_storage_start();
349 }
350 return nullptr;
351 }
352
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800353 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800354
Austin Schuh432784f2020-06-23 17:27:35 -0700355 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800356 const Channel *const channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700357 MMapedQueue lockless_queue_memory_;
358 ipc_lib::LocklessQueue lockless_queue_;
359
360 ipc_lib::QueueIndex actual_queue_index_ =
361 ipc_lib::LocklessQueue::empty_queue_index();
362
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800363 // This being empty indicates we're not going to copy data.
364 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800365
366 Context context_;
367};
368
369class ShmFetcher : public RawFetcher {
370 public:
Austin Schuh432784f2020-06-23 17:27:35 -0700371 explicit ShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800372 : RawFetcher(event_loop, channel),
Brian Silverman3bca5322020-08-12 19:35:29 -0700373 simple_shm_fetcher_(event_loop, channel) {
374 simple_shm_fetcher_.CopyDataOnFetch();
375 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800376
377 ~ShmFetcher() { context_.data = nullptr; }
378
379 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
380 if (simple_shm_fetcher_.FetchNext()) {
381 context_ = simple_shm_fetcher_.context();
382 return std::make_pair(true, monotonic_clock::now());
383 }
384 return std::make_pair(false, monotonic_clock::min_time);
385 }
386
387 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
388 if (simple_shm_fetcher_.Fetch()) {
389 context_ = simple_shm_fetcher_.context();
390 return std::make_pair(true, monotonic_clock::now());
391 }
392 return std::make_pair(false, monotonic_clock::min_time);
393 }
394
Brian Silverman6d2b3592020-06-18 14:40:15 -0700395 absl::Span<char> GetPrivateMemory() const {
396 return simple_shm_fetcher_.GetPrivateMemory();
397 }
398
Austin Schuh39788ff2019-12-01 18:22:57 -0800399 private:
400 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700401};
402
403class ShmSender : public RawSender {
404 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800405 explicit ShmSender(EventLoop *event_loop, const Channel *channel)
406 : RawSender(event_loop, channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800407 lockless_queue_memory_(
408 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800409 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800410 event_loop->configuration()->channel_storage_duration()))),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700411 lockless_queue_(lockless_queue_memory_.memory(),
412 lockless_queue_memory_.config()),
Austin Schuhe516ab02020-05-06 21:37:04 -0700413 lockless_queue_sender_(
414 VerifySender(lockless_queue_.MakeSender(), channel)) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700415
Austin Schuh39788ff2019-12-01 18:22:57 -0800416 ~ShmSender() override {}
417
Austin Schuhe516ab02020-05-06 21:37:04 -0700418 static ipc_lib::LocklessQueue::Sender VerifySender(
419 std::optional<ipc_lib::LocklessQueue::Sender> &&sender,
420 const Channel *channel) {
421 if (sender) {
422 return std::move(sender.value());
423 }
424 LOG(FATAL) << "Failed to create sender on "
425 << configuration::CleanedChannelToString(channel)
426 << ", too many senders.";
427 }
428
Alex Perrycb7da4b2019-08-28 19:35:56 -0700429 void *data() override { return lockless_queue_sender_.Data(); }
430 size_t size() override { return lockless_queue_sender_.size(); }
Austin Schuhad154822019-12-27 15:45:13 -0800431 bool DoSend(size_t length,
432 aos::monotonic_clock::time_point monotonic_remote_time,
433 aos::realtime_clock::time_point realtime_remote_time,
434 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700435 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
436 << ": Sent too big a message on "
437 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800438 lockless_queue_sender_.Send(
439 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
440 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800441 lockless_queue_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700442 return true;
443 }
444
Austin Schuhad154822019-12-27 15:45:13 -0800445 bool DoSend(const void *msg, size_t length,
446 aos::monotonic_clock::time_point monotonic_remote_time,
447 aos::realtime_clock::time_point realtime_remote_time,
448 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700449 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
450 << ": Sent too big a message on "
451 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800452 lockless_queue_sender_.Send(reinterpret_cast<const char *>(msg), length,
453 monotonic_remote_time, realtime_remote_time,
454 remote_queue_index, &monotonic_sent_time_,
455 &realtime_sent_time_, &sent_queue_index_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800456 lockless_queue_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700457 // TODO(austin): Return an error if we send too fast.
458 return true;
459 }
460
Brian Silverman5120afb2020-01-31 17:44:35 -0800461 absl::Span<char> GetSharedMemory() const {
462 return lockless_queue_memory_.GetSharedMemory();
463 }
464
Alex Perrycb7da4b2019-08-28 19:35:56 -0700465 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 MMapedQueue lockless_queue_memory_;
467 ipc_lib::LocklessQueue lockless_queue_;
468 ipc_lib::LocklessQueue::Sender lockless_queue_sender_;
469};
470
Alex Perrycb7da4b2019-08-28 19:35:56 -0700471// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500472class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700473 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500474 ShmWatcherState(
Austin Schuh7d87b672019-12-01 20:23:49 -0800475 ShmEventLoop *event_loop, const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800476 std::function<void(const Context &context, const void *message)> fn,
477 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500478 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800479 event_loop_(event_loop),
480 event_(this),
Brian Silverman3bca5322020-08-12 19:35:29 -0700481 simple_shm_fetcher_(event_loop, channel) {
482 if (copy_data) {
483 simple_shm_fetcher_.CopyDataOnFetch();
484 }
485 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700486
Brian Silverman148d43d2020-06-07 18:19:22 -0500487 ~ShmWatcherState() override { event_loop_->RemoveEvent(&event_); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800488
489 void Startup(EventLoop *event_loop) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800490 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800491 CHECK(RegisterWakeup(event_loop->priority()));
492 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700493
Alex Perrycb7da4b2019-08-28 19:35:56 -0700494 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800495 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700496 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800497 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800498
499 if (has_new_data_) {
500 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800501 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800502 event_loop_->AddEvent(&event_);
503 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700504 }
505
506 return has_new_data_;
507 }
508
Alex Perrycb7da4b2019-08-28 19:35:56 -0700509 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800510 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700511 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800512 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700513 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800514 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700515 }
516
Austin Schuh39788ff2019-12-01 18:22:57 -0800517 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700518 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800519 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700520 }
521
Austin Schuh39788ff2019-12-01 18:22:57 -0800522 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700523
Brian Silverman5120afb2020-01-31 17:44:35 -0800524 absl::Span<char> GetSharedMemory() const {
525 return simple_shm_fetcher_.GetSharedMemory();
526 }
527
Alex Perrycb7da4b2019-08-28 19:35:56 -0700528 private:
529 bool has_new_data_ = false;
530
Austin Schuh7d87b672019-12-01 20:23:49 -0800531 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500532 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800533 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700534};
535
536// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500537class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700538 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500539 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800540 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800541 shm_event_loop_(shm_event_loop),
542 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800543 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
544 // The timer may fire spurriously. HandleEvent on the event loop will
545 // call the callback if it is needed. It may also have called it when
546 // processing some other event, and the kernel decided to deliver this
547 // wakeup anyways.
548 timerfd_.Read();
549 shm_event_loop_->HandleEvent();
550 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700551 }
552
Brian Silverman148d43d2020-06-07 18:19:22 -0500553 ~ShmTimerHandler() {
Austin Schuh7d87b672019-12-01 20:23:49 -0800554 Disable();
555 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
556 }
557
558 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800559 CHECK(!event_.valid());
560 const auto monotonic_now = Call(monotonic_clock::now, base_);
561 if (event_.valid()) {
562 // If someone called Setup inside Call, rescheduling is already taken care
563 // of. Bail.
564 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800565 }
566
Austin Schuhcde39fd2020-02-22 20:58:24 -0800567 if (repeat_offset_ == chrono::seconds(0)) {
568 timerfd_.Disable();
569 } else {
570 // Compute how many cycles have elapsed and schedule the next iteration
571 // for the next iteration in the future.
572 const int elapsed_cycles =
573 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
574 std::chrono::nanoseconds(1)) /
575 repeat_offset_);
576 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800577
Austin Schuhcde39fd2020-02-22 20:58:24 -0800578 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800579 event_.set_event_time(base_);
580 shm_event_loop_->AddEvent(&event_);
Austin Schuhcde39fd2020-02-22 20:58:24 -0800581 timerfd_.SetTime(base_, chrono::seconds(0));
Austin Schuh7d87b672019-12-01 20:23:49 -0800582 }
583 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700584
585 void Setup(monotonic_clock::time_point base,
586 monotonic_clock::duration repeat_offset) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800587 if (event_.valid()) {
588 shm_event_loop_->RemoveEvent(&event_);
589 }
590
Alex Perrycb7da4b2019-08-28 19:35:56 -0700591 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800592 base_ = base;
593 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800594 event_.set_event_time(base_);
595 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700596 }
597
Austin Schuh7d87b672019-12-01 20:23:49 -0800598 void Disable() override {
599 shm_event_loop_->RemoveEvent(&event_);
600 timerfd_.Disable();
601 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700602
603 private:
604 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500605 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700606
Brian Silverman148d43d2020-06-07 18:19:22 -0500607 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700608
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800609 monotonic_clock::time_point base_;
610 monotonic_clock::duration repeat_offset_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700611};
612
613// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500614class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700615 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500616 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
617 ::std::function<void(int)> fn,
618 const monotonic_clock::duration interval,
619 const monotonic_clock::duration offset)
620 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800621 shm_event_loop_(shm_event_loop),
622 event_(this) {
623 shm_event_loop_->epoll_.OnReadable(
624 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
625 }
626
627 void HandleEvent() {
628 // The return value for read is the number of cycles that have elapsed.
629 // Because we check to see when this event *should* have happened, there are
630 // cases where Read() will return 0, when 1 cycle has actually happened.
631 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
632 // ignore it. Call handles rescheduling and calculating elapsed cycles
633 // without any extra help.
634 timerfd_.Read();
635 event_.Invalidate();
636
637 Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) {
638 Schedule(sleep_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700639 });
640 }
641
Brian Silverman148d43d2020-06-07 18:19:22 -0500642 ~ShmPhasedLoopHandler() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800643 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800644 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700645 }
646
647 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800648 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800649 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800650 if (event_.valid()) {
651 shm_event_loop_->RemoveEvent(&event_);
652 }
653
Austin Schuh39788ff2019-12-01 18:22:57 -0800654 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800655 event_.set_event_time(sleep_time);
656 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700657 }
658
659 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500660 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700661
Brian Silverman148d43d2020-06-07 18:19:22 -0500662 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700663};
Brian Silverman148d43d2020-06-07 18:19:22 -0500664
665} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700666
667::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
668 const Channel *channel) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800669 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
670 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
671 << "\", \"type\": \"" << channel->type()->string_view()
672 << "\" } is not able to be fetched on this node. Check your "
673 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800674 }
675
Brian Silverman148d43d2020-06-07 18:19:22 -0500676 return ::std::unique_ptr<RawFetcher>(new ShmFetcher(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700677}
678
679::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
680 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800681 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800682
Brian Silverman148d43d2020-06-07 18:19:22 -0500683 return ::std::unique_ptr<RawSender>(new ShmSender(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700684}
685
686void ShmEventLoop::MakeRawWatcher(
687 const Channel *channel,
688 std::function<void(const Context &context, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800689 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800690
Austin Schuh39788ff2019-12-01 18:22:57 -0800691 NewWatcher(::std::unique_ptr<WatcherState>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500692 new ShmWatcherState(this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800693}
694
695void ShmEventLoop::MakeRawNoArgWatcher(
696 const Channel *channel,
697 std::function<void(const Context &context)> watcher) {
698 TakeWatcher(channel);
699
Brian Silverman148d43d2020-06-07 18:19:22 -0500700 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800701 this, channel,
702 [watcher](const Context &context, const void *) { watcher(context); },
703 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700704}
705
706TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800707 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500708 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700709}
710
711PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
712 ::std::function<void(int)> callback,
713 const monotonic_clock::duration interval,
714 const monotonic_clock::duration offset) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500715 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
716 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700717}
718
719void ShmEventLoop::OnRun(::std::function<void()> on_run) {
720 on_run_.push_back(::std::move(on_run));
721}
722
Austin Schuh7d87b672019-12-01 20:23:49 -0800723void ShmEventLoop::HandleEvent() {
724 // Update all the times for handlers.
725 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500726 ShmWatcherState *watcher =
727 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Austin Schuh7d87b672019-12-01 20:23:49 -0800728
729 watcher->CheckForNewData();
730 }
731
Austin Schuh39788ff2019-12-01 18:22:57 -0800732 while (true) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800733 if (EventCount() == 0 ||
734 PeekEvent()->event_time() > monotonic_clock::now()) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800735 break;
736 }
737
Austin Schuh7d87b672019-12-01 20:23:49 -0800738 EventLoopEvent *event = PopEvent();
739 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800740 }
741}
742
Austin Schuh32fd5a72019-12-01 22:20:26 -0800743// RAII class to mask signals.
744class ScopedSignalMask {
745 public:
746 ScopedSignalMask(std::initializer_list<int> signals) {
747 sigset_t sigset;
748 PCHECK(sigemptyset(&sigset) == 0);
749 for (int signal : signals) {
750 PCHECK(sigaddset(&sigset, signal) == 0);
751 }
752
753 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
754 }
755
756 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
757
758 private:
759 sigset_t old_;
760};
761
762// Class to manage the static state associated with killing multiple event
763// loops.
764class SignalHandler {
765 public:
766 // Gets the singleton.
767 static SignalHandler *global() {
768 static SignalHandler loop;
769 return &loop;
770 }
771
772 // Handles the signal with the singleton.
773 static void HandleSignal(int) { global()->DoHandleSignal(); }
774
775 // Registers an event loop to receive Exit() calls.
776 void Register(ShmEventLoop *event_loop) {
777 // Block signals while we have the mutex so we never race with the signal
778 // handler.
779 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
780 std::unique_lock<stl_mutex> locker(mutex_);
781 if (event_loops_.size() == 0) {
782 // The first caller registers the signal handler.
783 struct sigaction new_action;
784 sigemptyset(&new_action.sa_mask);
785 // This makes it so that 2 control c's to a stuck process will kill it by
786 // restoring the original signal handler.
787 new_action.sa_flags = SA_RESETHAND;
788 new_action.sa_handler = &HandleSignal;
789
790 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
791 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
792 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
793 }
794
795 event_loops_.push_back(event_loop);
796 }
797
798 // Unregisters an event loop to receive Exit() calls.
799 void Unregister(ShmEventLoop *event_loop) {
800 // Block signals while we have the mutex so we never race with the signal
801 // handler.
802 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
803 std::unique_lock<stl_mutex> locker(mutex_);
804
Brian Silverman5120afb2020-01-31 17:44:35 -0800805 event_loops_.erase(
806 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800807
808 if (event_loops_.size() == 0u) {
809 // The last caller restores the original signal handlers.
810 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
811 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
812 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
813 }
814 }
815
816 private:
817 void DoHandleSignal() {
818 // We block signals while grabbing the lock, so there should never be a
819 // race. Confirm that this is true using trylock.
820 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
821 "modifing the event loop list.";
822 for (ShmEventLoop *event_loop : event_loops_) {
823 event_loop->Exit();
824 }
825 mutex_.unlock();
826 }
827
828 // Mutex to protect all state.
829 stl_mutex mutex_;
830 std::vector<ShmEventLoop *> event_loops_;
831 struct sigaction old_action_int_;
832 struct sigaction old_action_hup_;
833 struct sigaction old_action_term_;
834};
835
Alex Perrycb7da4b2019-08-28 19:35:56 -0700836void ShmEventLoop::Run() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800837 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -0800838
Alex Perrycb7da4b2019-08-28 19:35:56 -0700839 std::unique_ptr<ipc_lib::SignalFd> signalfd;
840
841 if (watchers_.size() > 0) {
842 signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
843
844 epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() {
845 signalfd_siginfo result = signalfd_ptr->Read();
846 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
847
848 // TODO(austin): We should really be checking *everything*, not just
849 // watchers, and calling the oldest thing first. That will improve
850 // determinism a lot.
851
Austin Schuh7d87b672019-12-01 20:23:49 -0800852 HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700853 });
854 }
855
Austin Schuh39788ff2019-12-01 18:22:57 -0800856 MaybeScheduleTimingReports();
857
Austin Schuh7d87b672019-12-01 20:23:49 -0800858 ReserveEvents();
859
Tyler Chatow67ddb032020-01-12 14:30:04 -0800860 {
861 AosLogToFbs aos_logger;
862 if (!skip_logger_) {
863 aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
864 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700865
Tyler Chatow67ddb032020-01-12 14:30:04 -0800866 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -0700867 const cpu_set_t default_affinity = DefaultAffinity();
868 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
869 ::aos::SetCurrentThreadAffinity(affinity_);
870 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800871 // Now, all the callbacks are setup. Lock everything into memory and go RT.
872 if (priority_ != 0) {
873 ::aos::InitRT();
874
875 LOG(INFO) << "Setting priority to " << priority_;
876 ::aos::SetCurrentThreadRealtimePriority(priority_);
877 }
878
879 set_is_running(true);
880
881 // Now that we are realtime (but before the OnRun handlers run), snap the
882 // queue index.
883 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
884 watcher->Startup(this);
885 }
886
887 // Now that we are RT, run all the OnRun handlers.
888 for (const auto &run : on_run_) {
889 run();
890 }
891
892 // And start our main event loop which runs all the timers and handles Quit.
893 epoll_.Run();
894
895 // Once epoll exits, there is no useful nonrt work left to do.
896 set_is_running(false);
897
898 // Nothing time or synchronization critical needs to happen after this
899 // point. Drop RT priority.
900 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700901 }
902
Austin Schuh39788ff2019-12-01 18:22:57 -0800903 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500904 ShmWatcherState *watcher =
905 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700906 watcher->UnregisterWakeup();
907 }
908
909 if (watchers_.size() > 0) {
910 epoll_.DeleteFd(signalfd->fd());
911 signalfd.reset();
912 }
Austin Schuh32fd5a72019-12-01 22:20:26 -0800913
914 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800915
916 // Trigger any remaining senders or fetchers to be cleared before destroying
917 // the event loop so the book keeping matches. Do this in the thread that
918 // created the timing reporter.
919 timing_report_sender_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700920}
921
922void ShmEventLoop::Exit() { epoll_.Quit(); }
923
924ShmEventLoop::~ShmEventLoop() {
Austin Schuh39788ff2019-12-01 18:22:57 -0800925 // Force everything with a registered fd with epoll to be destroyed now.
926 timers_.clear();
927 phased_loops_.clear();
928 watchers_.clear();
929
Alex Perrycb7da4b2019-08-28 19:35:56 -0700930 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
931}
932
Alex Perrycb7da4b2019-08-28 19:35:56 -0700933void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
934 if (is_running()) {
935 LOG(FATAL) << "Cannot set realtime priority while running.";
936 }
937 priority_ = priority;
938}
939
Brian Silverman6a54ff32020-04-28 16:41:39 -0700940void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
941 if (is_running()) {
942 LOG(FATAL) << "Cannot set affinity while running.";
943 }
944 affinity_ = cpuset;
945}
946
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800947void ShmEventLoop::set_name(const std::string_view name) {
948 name_ = std::string(name);
949 UpdateTimingReport();
950}
951
Brian Silverman5120afb2020-01-31 17:44:35 -0800952absl::Span<char> ShmEventLoop::GetWatcherSharedMemory(const Channel *channel) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500953 ShmWatcherState *const watcher_state =
954 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -0800955 return watcher_state->GetSharedMemory();
956}
957
958absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
959 const aos::RawSender *sender) const {
Brian Silverman148d43d2020-06-07 18:19:22 -0500960 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800961}
962
Brian Silverman6d2b3592020-06-18 14:40:15 -0700963absl::Span<char> ShmEventLoop::GetShmFetcherPrivateMemory(
964 const aos::RawFetcher *fetcher) const {
965 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
966}
967
Austin Schuh39788ff2019-12-01 18:22:57 -0800968pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
969
Alex Perrycb7da4b2019-08-28 19:35:56 -0700970} // namespace aos