blob: d2c9112b8a35dd0e2dff19f326310f994f38054a [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());
Austin Schuh3328d132020-02-28 13:54:57 -080063 return ShmFolder(channel) + channel->type()->str() + ".v2";
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();
Austin Schuhaa79e4e2019-12-29 20:43:32 -080099 config_.queue_size =
100 channel_storage_duration.count() * channel->frequency();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 config_.message_data_size = channel->max_size();
102
103 size_ = ipc_lib::LocklessQueueMemorySize(config_);
104
Austin Schuhfccb2d02020-01-26 16:11:19 -0800105 util::MkdirP(path, FLAGS_permissions);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106
107 // There are 2 cases. Either the file already exists, or it does not
108 // already exist and we need to create it. Start by trying to create it. If
109 // that fails, the file has already been created and we can open it
110 // normally.. Once the file has been created it wil never be deleted.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800111 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_EXCL,
Brian Silverman148d43d2020-06-07 18:19:22 -0500112 O_CLOEXEC | FLAGS_permissions);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800113 if (fd == -1 && errno == EEXIST) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700114 VLOG(1) << path << " already created.";
115 // File already exists.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800116 fd = open(path.c_str(), O_RDWR, O_CLOEXEC);
117 PCHECK(fd != -1) << ": Failed to open " << path;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700118 while (true) {
119 struct stat st;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800120 PCHECK(fstat(fd, &st) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700121 if (st.st_size != 0) {
122 CHECK_EQ(static_cast<size_t>(st.st_size), size_)
123 << ": Size of " << path
124 << " doesn't match expected size of backing queue file. Did the "
125 "queue definition change?";
126 break;
127 } else {
128 // The creating process didn't get around to it yet. Give it a bit.
129 std::this_thread::sleep_for(std::chrono::milliseconds(10));
130 VLOG(1) << path << " is zero size, waiting";
131 }
132 }
133 } else {
134 VLOG(1) << "Created " << path;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800135 PCHECK(fd != -1) << ": Failed to open " << path;
136 PCHECK(ftruncate(fd, size_) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700137 }
138
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800139 data_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 PCHECK(data_ != MAP_FAILED);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800141 PCHECK(close(fd) == 0);
Brian Silverman3b0cdaf2020-04-28 16:51:51 -0700142 PageFaultData(static_cast<char *>(data_), size_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143
144 ipc_lib::InitializeLocklessQueueMemory(memory(), config_);
145 }
146
Brian Silverman148d43d2020-06-07 18:19:22 -0500147 ~MMapedQueue() { PCHECK(munmap(data_, size_) == 0); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148
149 ipc_lib::LocklessQueueMemory *memory() const {
150 return reinterpret_cast<ipc_lib::LocklessQueueMemory *>(data_);
151 }
152
Austin Schuh39788ff2019-12-01 18:22:57 -0800153 const ipc_lib::LocklessQueueConfiguration &config() const { return config_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700154
Brian Silverman5120afb2020-01-31 17:44:35 -0800155 absl::Span<char> GetSharedMemory() const {
156 return absl::Span<char>(static_cast<char *>(data_), size_);
157 }
158
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160 ipc_lib::LocklessQueueConfiguration config_;
161
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 size_t size_;
163 void *data_;
164};
165
Austin Schuh217a9782019-12-21 23:02:50 -0800166namespace {
167
Austin Schuh217a9782019-12-21 23:02:50 -0800168const Node *MaybeMyNode(const Configuration *configuration) {
169 if (!configuration->has_nodes()) {
170 return nullptr;
171 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700172
Austin Schuh217a9782019-12-21 23:02:50 -0800173 return configuration::GetMyNode(configuration);
174}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175
176namespace chrono = ::std::chrono;
177
Austin Schuh39788ff2019-12-01 18:22:57 -0800178} // namespace
179
Austin Schuh217a9782019-12-21 23:02:50 -0800180ShmEventLoop::ShmEventLoop(const Configuration *configuration)
181 : EventLoop(configuration),
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800182 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -0800183 node_(MaybeMyNode(configuration)) {
184 if (configuration->has_nodes()) {
185 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
186 }
187}
Austin Schuh217a9782019-12-21 23:02:50 -0800188
Brian Silverman148d43d2020-06-07 18:19:22 -0500189namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -0800190
191class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192 public:
Brian Silverman3bca5322020-08-12 19:35:29 -0700193 explicit SimpleShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700194 : event_loop_(event_loop),
195 channel_(channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800196 lockless_queue_memory_(
197 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800198 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800199 event_loop->configuration()->channel_storage_duration()))),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200 lockless_queue_(lockless_queue_memory_.memory(),
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800201 lockless_queue_memory_.config()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700202 context_.data = nullptr;
203 // Point the queue index at the next index to read starting now. This
204 // makes it such that FetchNext will read the next message sent after
205 // the fetcher is created.
206 PointAtNextQueueIndex();
207 }
208
Austin Schuh39788ff2019-12-01 18:22:57 -0800209 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210
Brian Silverman3bca5322020-08-12 19:35:29 -0700211 // Sets this object to copy data out of the shared memory into a private
212 // buffer when fetching.
213 void CopyDataOnFetch() {
214 data_storage_.reset(static_cast<char *>(
215 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
216 }
217
Alex Perrycb7da4b2019-08-28 19:35:56 -0700218 // Points the next message to fetch at the queue index which will be
219 // populated next.
220 void PointAtNextQueueIndex() {
221 actual_queue_index_ = lockless_queue_.LatestQueueIndex();
222 if (!actual_queue_index_.valid()) {
223 // Nothing in the queue. The next element will show up at the 0th
224 // index in the queue.
225 actual_queue_index_ =
226 ipc_lib::QueueIndex::Zero(lockless_queue_.queue_size());
227 } else {
228 actual_queue_index_ = actual_queue_index_.Increment();
229 }
230 }
231
Austin Schuh39788ff2019-12-01 18:22:57 -0800232 bool FetchNext() {
Brian Silverman3bca5322020-08-12 19:35:29 -0700233 const ipc_lib::LocklessQueue::ReadResult read_result =
234 DoFetch(actual_queue_index_);
Austin Schuh432784f2020-06-23 17:27:35 -0700235
Alex Perrycb7da4b2019-08-28 19:35:56 -0700236 return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD;
237 }
238
Austin Schuh39788ff2019-12-01 18:22:57 -0800239 bool Fetch() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700240 const ipc_lib::QueueIndex queue_index = lockless_queue_.LatestQueueIndex();
241 // actual_queue_index_ is only meaningful if it was set by Fetch or
242 // FetchNext. This happens when valid_data_ has been set. So, only
243 // skip checking if valid_data_ is true.
244 //
245 // Also, if the latest queue index is invalid, we are empty. So there
246 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800247 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 queue_index == actual_queue_index_.DecrementBy(1u)) ||
249 !queue_index.valid()) {
250 return false;
251 }
252
Brian Silverman3bca5322020-08-12 19:35:29 -0700253 const ipc_lib::LocklessQueue::ReadResult read_result = DoFetch(queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700254
255 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800256 << ": Queue index went backwards. This should never happen. "
257 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700258
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259 return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD;
260 }
261
Austin Schuh39788ff2019-12-01 18:22:57 -0800262 Context context() const { return context_; }
263
Alex Perrycb7da4b2019-08-28 19:35:56 -0700264 bool RegisterWakeup(int priority) {
265 return lockless_queue_.RegisterWakeup(priority);
266 }
267
268 void UnregisterWakeup() { lockless_queue_.UnregisterWakeup(); }
269
Brian Silverman5120afb2020-01-31 17:44:35 -0800270 absl::Span<char> GetSharedMemory() const {
271 return lockless_queue_memory_.GetSharedMemory();
272 }
273
Brian Silverman6d2b3592020-06-18 14:40:15 -0700274 absl::Span<char> GetPrivateMemory() const {
Brian Silverman3bca5322020-08-12 19:35:29 -0700275 // Can't usefully expose this for pinning, because the buffer changes
276 // address for each message. Callers who want to work with that should just
277 // grab the whole shared memory buffer instead.
Brian Silverman6d2b3592020-06-18 14:40:15 -0700278 return absl::Span<char>(
279 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
280 lockless_queue_.message_data_size());
281 }
282
Alex Perrycb7da4b2019-08-28 19:35:56 -0700283 private:
Brian Silverman3bca5322020-08-12 19:35:29 -0700284 ipc_lib::LocklessQueue::ReadResult DoFetch(ipc_lib::QueueIndex queue_index) {
285 // TODO(austin): Get behind and make sure it dies.
286 char *copy_buffer = nullptr;
287 if (copy_data()) {
288 copy_buffer = data_storage_start();
289 }
290 ipc_lib::LocklessQueue::ReadResult read_result = lockless_queue_.Read(
291 queue_index.index(), &context_.monotonic_event_time,
292 &context_.realtime_event_time, &context_.monotonic_remote_time,
293 &context_.realtime_remote_time, &context_.remote_queue_index,
294 &context_.size, copy_buffer);
295
296 if (read_result == ipc_lib::LocklessQueue::ReadResult::GOOD) {
297 context_.queue_index = queue_index.index();
298 if (context_.remote_queue_index == 0xffffffffu) {
299 context_.remote_queue_index = context_.queue_index;
300 }
301 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
302 context_.monotonic_remote_time = context_.monotonic_event_time;
303 }
304 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
305 context_.realtime_remote_time = context_.realtime_event_time;
306 }
307 const char *const data = DataBuffer();
308 if (data) {
309 context_.data =
310 data + lockless_queue_.message_data_size() - context_.size;
311 } else {
312 context_.data = nullptr;
313 }
314 actual_queue_index_ = queue_index.Increment();
315 }
316
317 // Make sure the data wasn't modified while we were reading it. This
318 // can only happen if you are reading the last message *while* it is
319 // being written to, which means you are pretty far behind.
320 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::OVERWROTE)
321 << ": Got behind while reading and the last message was modified "
322 "out from under us while we were reading it. Don't get so far "
323 "behind on: "
324 << configuration::CleanedChannelToString(channel_);
325
326 // We fell behind between when we read the index and read the value.
327 // This isn't worth recovering from since this means we went to sleep
328 // for a long time in the middle of this function.
329 if (read_result == ipc_lib::LocklessQueue::ReadResult::TOO_OLD) {
330 event_loop_->SendTimingReport();
331 LOG(FATAL) << "The next message is no longer available. "
332 << configuration::CleanedChannelToString(channel_);
333 }
334
335 return read_result;
336 }
337
338 char *data_storage_start() const {
339 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800340 return RoundChannelData(data_storage_.get(), channel_->max_size());
341 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700342
343 // Note that for some modes the return value will change as new messages are
344 // read.
345 const char *DataBuffer() const {
346 if (copy_data()) {
347 return data_storage_start();
348 }
349 return nullptr;
350 }
351
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800352 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800353
Austin Schuh432784f2020-06-23 17:27:35 -0700354 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800355 const Channel *const channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700356 MMapedQueue lockless_queue_memory_;
357 ipc_lib::LocklessQueue lockless_queue_;
358
359 ipc_lib::QueueIndex actual_queue_index_ =
360 ipc_lib::LocklessQueue::empty_queue_index();
361
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800362 // This being empty indicates we're not going to copy data.
363 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800364
365 Context context_;
366};
367
368class ShmFetcher : public RawFetcher {
369 public:
Austin Schuh432784f2020-06-23 17:27:35 -0700370 explicit ShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800371 : RawFetcher(event_loop, channel),
Brian Silverman3bca5322020-08-12 19:35:29 -0700372 simple_shm_fetcher_(event_loop, channel) {
373 simple_shm_fetcher_.CopyDataOnFetch();
374 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800375
376 ~ShmFetcher() { context_.data = nullptr; }
377
378 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
379 if (simple_shm_fetcher_.FetchNext()) {
380 context_ = simple_shm_fetcher_.context();
381 return std::make_pair(true, monotonic_clock::now());
382 }
383 return std::make_pair(false, monotonic_clock::min_time);
384 }
385
386 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
387 if (simple_shm_fetcher_.Fetch()) {
388 context_ = simple_shm_fetcher_.context();
389 return std::make_pair(true, monotonic_clock::now());
390 }
391 return std::make_pair(false, monotonic_clock::min_time);
392 }
393
Brian Silverman6d2b3592020-06-18 14:40:15 -0700394 absl::Span<char> GetPrivateMemory() const {
395 return simple_shm_fetcher_.GetPrivateMemory();
396 }
397
Austin Schuh39788ff2019-12-01 18:22:57 -0800398 private:
399 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700400};
401
402class ShmSender : public RawSender {
403 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800404 explicit ShmSender(EventLoop *event_loop, const Channel *channel)
405 : RawSender(event_loop, channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800406 lockless_queue_memory_(
407 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800408 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800409 event_loop->configuration()->channel_storage_duration()))),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700410 lockless_queue_(lockless_queue_memory_.memory(),
411 lockless_queue_memory_.config()),
Austin Schuhe516ab02020-05-06 21:37:04 -0700412 lockless_queue_sender_(
413 VerifySender(lockless_queue_.MakeSender(), channel)) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700414
Austin Schuh39788ff2019-12-01 18:22:57 -0800415 ~ShmSender() override {}
416
Austin Schuhe516ab02020-05-06 21:37:04 -0700417 static ipc_lib::LocklessQueue::Sender VerifySender(
418 std::optional<ipc_lib::LocklessQueue::Sender> &&sender,
419 const Channel *channel) {
420 if (sender) {
421 return std::move(sender.value());
422 }
423 LOG(FATAL) << "Failed to create sender on "
424 << configuration::CleanedChannelToString(channel)
425 << ", too many senders.";
426 }
427
Alex Perrycb7da4b2019-08-28 19:35:56 -0700428 void *data() override { return lockless_queue_sender_.Data(); }
429 size_t size() override { return lockless_queue_sender_.size(); }
Austin Schuhad154822019-12-27 15:45:13 -0800430 bool DoSend(size_t length,
431 aos::monotonic_clock::time_point monotonic_remote_time,
432 aos::realtime_clock::time_point realtime_remote_time,
433 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700434 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
435 << ": Sent too big a message on "
436 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800437 lockless_queue_sender_.Send(
438 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
439 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800440 lockless_queue_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700441 return true;
442 }
443
Austin Schuhad154822019-12-27 15:45:13 -0800444 bool DoSend(const void *msg, size_t length,
445 aos::monotonic_clock::time_point monotonic_remote_time,
446 aos::realtime_clock::time_point realtime_remote_time,
447 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700448 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
449 << ": Sent too big a message on "
450 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800451 lockless_queue_sender_.Send(reinterpret_cast<const char *>(msg), length,
452 monotonic_remote_time, realtime_remote_time,
453 remote_queue_index, &monotonic_sent_time_,
454 &realtime_sent_time_, &sent_queue_index_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800455 lockless_queue_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700456 // TODO(austin): Return an error if we send too fast.
457 return true;
458 }
459
Brian Silverman5120afb2020-01-31 17:44:35 -0800460 absl::Span<char> GetSharedMemory() const {
461 return lockless_queue_memory_.GetSharedMemory();
462 }
463
Alex Perrycb7da4b2019-08-28 19:35:56 -0700464 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700465 MMapedQueue lockless_queue_memory_;
466 ipc_lib::LocklessQueue lockless_queue_;
467 ipc_lib::LocklessQueue::Sender lockless_queue_sender_;
468};
469
Alex Perrycb7da4b2019-08-28 19:35:56 -0700470// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500471class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700472 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500473 ShmWatcherState(
Austin Schuh7d87b672019-12-01 20:23:49 -0800474 ShmEventLoop *event_loop, const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800475 std::function<void(const Context &context, const void *message)> fn,
476 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500477 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800478 event_loop_(event_loop),
479 event_(this),
Brian Silverman3bca5322020-08-12 19:35:29 -0700480 simple_shm_fetcher_(event_loop, channel) {
481 if (copy_data) {
482 simple_shm_fetcher_.CopyDataOnFetch();
483 }
484 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700485
Brian Silverman148d43d2020-06-07 18:19:22 -0500486 ~ShmWatcherState() override { event_loop_->RemoveEvent(&event_); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800487
488 void Startup(EventLoop *event_loop) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800489 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800490 CHECK(RegisterWakeup(event_loop->priority()));
491 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700492
Alex Perrycb7da4b2019-08-28 19:35:56 -0700493 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800494 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700495 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800496 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800497
498 if (has_new_data_) {
499 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800500 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800501 event_loop_->AddEvent(&event_);
502 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700503 }
504
505 return has_new_data_;
506 }
507
Alex Perrycb7da4b2019-08-28 19:35:56 -0700508 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800509 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700510 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800511 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700512 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800513 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700514 }
515
Austin Schuh39788ff2019-12-01 18:22:57 -0800516 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700517 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800518 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700519 }
520
Austin Schuh39788ff2019-12-01 18:22:57 -0800521 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700522
Brian Silverman5120afb2020-01-31 17:44:35 -0800523 absl::Span<char> GetSharedMemory() const {
524 return simple_shm_fetcher_.GetSharedMemory();
525 }
526
Alex Perrycb7da4b2019-08-28 19:35:56 -0700527 private:
528 bool has_new_data_ = false;
529
Austin Schuh7d87b672019-12-01 20:23:49 -0800530 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500531 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800532 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700533};
534
535// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500536class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700537 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500538 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800539 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800540 shm_event_loop_(shm_event_loop),
541 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800542 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
543 // The timer may fire spurriously. HandleEvent on the event loop will
544 // call the callback if it is needed. It may also have called it when
545 // processing some other event, and the kernel decided to deliver this
546 // wakeup anyways.
547 timerfd_.Read();
548 shm_event_loop_->HandleEvent();
549 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700550 }
551
Brian Silverman148d43d2020-06-07 18:19:22 -0500552 ~ShmTimerHandler() {
Austin Schuh7d87b672019-12-01 20:23:49 -0800553 Disable();
554 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
555 }
556
557 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800558 CHECK(!event_.valid());
559 const auto monotonic_now = Call(monotonic_clock::now, base_);
560 if (event_.valid()) {
561 // If someone called Setup inside Call, rescheduling is already taken care
562 // of. Bail.
563 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800564 }
565
Austin Schuhcde39fd2020-02-22 20:58:24 -0800566 if (repeat_offset_ == chrono::seconds(0)) {
567 timerfd_.Disable();
568 } else {
569 // Compute how many cycles have elapsed and schedule the next iteration
570 // for the next iteration in the future.
571 const int elapsed_cycles =
572 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
573 std::chrono::nanoseconds(1)) /
574 repeat_offset_);
575 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800576
Austin Schuhcde39fd2020-02-22 20:58:24 -0800577 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800578 event_.set_event_time(base_);
579 shm_event_loop_->AddEvent(&event_);
Austin Schuhcde39fd2020-02-22 20:58:24 -0800580 timerfd_.SetTime(base_, chrono::seconds(0));
Austin Schuh7d87b672019-12-01 20:23:49 -0800581 }
582 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700583
584 void Setup(monotonic_clock::time_point base,
585 monotonic_clock::duration repeat_offset) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800586 if (event_.valid()) {
587 shm_event_loop_->RemoveEvent(&event_);
588 }
589
Alex Perrycb7da4b2019-08-28 19:35:56 -0700590 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800591 base_ = base;
592 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800593 event_.set_event_time(base_);
594 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700595 }
596
Austin Schuh7d87b672019-12-01 20:23:49 -0800597 void Disable() override {
598 shm_event_loop_->RemoveEvent(&event_);
599 timerfd_.Disable();
600 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700601
602 private:
603 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500604 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700605
Brian Silverman148d43d2020-06-07 18:19:22 -0500606 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700607
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800608 monotonic_clock::time_point base_;
609 monotonic_clock::duration repeat_offset_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700610};
611
612// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500613class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700614 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500615 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
616 ::std::function<void(int)> fn,
617 const monotonic_clock::duration interval,
618 const monotonic_clock::duration offset)
619 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800620 shm_event_loop_(shm_event_loop),
621 event_(this) {
622 shm_event_loop_->epoll_.OnReadable(
623 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
624 }
625
626 void HandleEvent() {
627 // The return value for read is the number of cycles that have elapsed.
628 // Because we check to see when this event *should* have happened, there are
629 // cases where Read() will return 0, when 1 cycle has actually happened.
630 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
631 // ignore it. Call handles rescheduling and calculating elapsed cycles
632 // without any extra help.
633 timerfd_.Read();
634 event_.Invalidate();
635
636 Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) {
637 Schedule(sleep_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700638 });
639 }
640
Brian Silverman148d43d2020-06-07 18:19:22 -0500641 ~ShmPhasedLoopHandler() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800642 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800643 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700644 }
645
646 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800647 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800648 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800649 if (event_.valid()) {
650 shm_event_loop_->RemoveEvent(&event_);
651 }
652
Austin Schuh39788ff2019-12-01 18:22:57 -0800653 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800654 event_.set_event_time(sleep_time);
655 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700656 }
657
658 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500659 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700660
Brian Silverman148d43d2020-06-07 18:19:22 -0500661 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700662};
Brian Silverman148d43d2020-06-07 18:19:22 -0500663
664} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700665
666::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
667 const Channel *channel) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800668 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
669 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
670 << "\", \"type\": \"" << channel->type()->string_view()
671 << "\" } is not able to be fetched on this node. Check your "
672 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800673 }
674
Brian Silverman148d43d2020-06-07 18:19:22 -0500675 return ::std::unique_ptr<RawFetcher>(new ShmFetcher(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700676}
677
678::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
679 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800680 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800681
Brian Silverman148d43d2020-06-07 18:19:22 -0500682 return ::std::unique_ptr<RawSender>(new ShmSender(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700683}
684
685void ShmEventLoop::MakeRawWatcher(
686 const Channel *channel,
687 std::function<void(const Context &context, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800688 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800689
Austin Schuh39788ff2019-12-01 18:22:57 -0800690 NewWatcher(::std::unique_ptr<WatcherState>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500691 new ShmWatcherState(this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800692}
693
694void ShmEventLoop::MakeRawNoArgWatcher(
695 const Channel *channel,
696 std::function<void(const Context &context)> watcher) {
697 TakeWatcher(channel);
698
Brian Silverman148d43d2020-06-07 18:19:22 -0500699 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800700 this, channel,
701 [watcher](const Context &context, const void *) { watcher(context); },
702 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700703}
704
705TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800706 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500707 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700708}
709
710PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
711 ::std::function<void(int)> callback,
712 const monotonic_clock::duration interval,
713 const monotonic_clock::duration offset) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500714 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
715 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700716}
717
718void ShmEventLoop::OnRun(::std::function<void()> on_run) {
719 on_run_.push_back(::std::move(on_run));
720}
721
Austin Schuh7d87b672019-12-01 20:23:49 -0800722void ShmEventLoop::HandleEvent() {
723 // Update all the times for handlers.
724 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500725 ShmWatcherState *watcher =
726 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Austin Schuh7d87b672019-12-01 20:23:49 -0800727
728 watcher->CheckForNewData();
729 }
730
Austin Schuh39788ff2019-12-01 18:22:57 -0800731 while (true) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800732 if (EventCount() == 0 ||
733 PeekEvent()->event_time() > monotonic_clock::now()) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800734 break;
735 }
736
Austin Schuh7d87b672019-12-01 20:23:49 -0800737 EventLoopEvent *event = PopEvent();
738 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800739 }
740}
741
Austin Schuh32fd5a72019-12-01 22:20:26 -0800742// RAII class to mask signals.
743class ScopedSignalMask {
744 public:
745 ScopedSignalMask(std::initializer_list<int> signals) {
746 sigset_t sigset;
747 PCHECK(sigemptyset(&sigset) == 0);
748 for (int signal : signals) {
749 PCHECK(sigaddset(&sigset, signal) == 0);
750 }
751
752 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
753 }
754
755 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
756
757 private:
758 sigset_t old_;
759};
760
761// Class to manage the static state associated with killing multiple event
762// loops.
763class SignalHandler {
764 public:
765 // Gets the singleton.
766 static SignalHandler *global() {
767 static SignalHandler loop;
768 return &loop;
769 }
770
771 // Handles the signal with the singleton.
772 static void HandleSignal(int) { global()->DoHandleSignal(); }
773
774 // Registers an event loop to receive Exit() calls.
775 void Register(ShmEventLoop *event_loop) {
776 // Block signals while we have the mutex so we never race with the signal
777 // handler.
778 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
779 std::unique_lock<stl_mutex> locker(mutex_);
780 if (event_loops_.size() == 0) {
781 // The first caller registers the signal handler.
782 struct sigaction new_action;
783 sigemptyset(&new_action.sa_mask);
784 // This makes it so that 2 control c's to a stuck process will kill it by
785 // restoring the original signal handler.
786 new_action.sa_flags = SA_RESETHAND;
787 new_action.sa_handler = &HandleSignal;
788
789 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
790 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
791 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
792 }
793
794 event_loops_.push_back(event_loop);
795 }
796
797 // Unregisters an event loop to receive Exit() calls.
798 void Unregister(ShmEventLoop *event_loop) {
799 // Block signals while we have the mutex so we never race with the signal
800 // handler.
801 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
802 std::unique_lock<stl_mutex> locker(mutex_);
803
Brian Silverman5120afb2020-01-31 17:44:35 -0800804 event_loops_.erase(
805 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800806
807 if (event_loops_.size() == 0u) {
808 // The last caller restores the original signal handlers.
809 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
810 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
811 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
812 }
813 }
814
815 private:
816 void DoHandleSignal() {
817 // We block signals while grabbing the lock, so there should never be a
818 // race. Confirm that this is true using trylock.
819 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
820 "modifing the event loop list.";
821 for (ShmEventLoop *event_loop : event_loops_) {
822 event_loop->Exit();
823 }
824 mutex_.unlock();
825 }
826
827 // Mutex to protect all state.
828 stl_mutex mutex_;
829 std::vector<ShmEventLoop *> event_loops_;
830 struct sigaction old_action_int_;
831 struct sigaction old_action_hup_;
832 struct sigaction old_action_term_;
833};
834
Alex Perrycb7da4b2019-08-28 19:35:56 -0700835void ShmEventLoop::Run() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800836 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -0800837
Alex Perrycb7da4b2019-08-28 19:35:56 -0700838 std::unique_ptr<ipc_lib::SignalFd> signalfd;
839
840 if (watchers_.size() > 0) {
841 signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
842
843 epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() {
844 signalfd_siginfo result = signalfd_ptr->Read();
845 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
846
847 // TODO(austin): We should really be checking *everything*, not just
848 // watchers, and calling the oldest thing first. That will improve
849 // determinism a lot.
850
Austin Schuh7d87b672019-12-01 20:23:49 -0800851 HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700852 });
853 }
854
Austin Schuh39788ff2019-12-01 18:22:57 -0800855 MaybeScheduleTimingReports();
856
Austin Schuh7d87b672019-12-01 20:23:49 -0800857 ReserveEvents();
858
Tyler Chatow67ddb032020-01-12 14:30:04 -0800859 {
860 AosLogToFbs aos_logger;
861 if (!skip_logger_) {
862 aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
863 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700864
Tyler Chatow67ddb032020-01-12 14:30:04 -0800865 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -0700866 const cpu_set_t default_affinity = DefaultAffinity();
867 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
868 ::aos::SetCurrentThreadAffinity(affinity_);
869 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800870 // Now, all the callbacks are setup. Lock everything into memory and go RT.
871 if (priority_ != 0) {
872 ::aos::InitRT();
873
874 LOG(INFO) << "Setting priority to " << priority_;
875 ::aos::SetCurrentThreadRealtimePriority(priority_);
876 }
877
878 set_is_running(true);
879
880 // Now that we are realtime (but before the OnRun handlers run), snap the
881 // queue index.
882 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
883 watcher->Startup(this);
884 }
885
886 // Now that we are RT, run all the OnRun handlers.
887 for (const auto &run : on_run_) {
888 run();
889 }
890
891 // And start our main event loop which runs all the timers and handles Quit.
892 epoll_.Run();
893
894 // Once epoll exits, there is no useful nonrt work left to do.
895 set_is_running(false);
896
897 // Nothing time or synchronization critical needs to happen after this
898 // point. Drop RT priority.
899 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700900 }
901
Austin Schuh39788ff2019-12-01 18:22:57 -0800902 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500903 ShmWatcherState *watcher =
904 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700905 watcher->UnregisterWakeup();
906 }
907
908 if (watchers_.size() > 0) {
909 epoll_.DeleteFd(signalfd->fd());
910 signalfd.reset();
911 }
Austin Schuh32fd5a72019-12-01 22:20:26 -0800912
913 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800914
915 // Trigger any remaining senders or fetchers to be cleared before destroying
916 // the event loop so the book keeping matches. Do this in the thread that
917 // created the timing reporter.
918 timing_report_sender_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700919}
920
921void ShmEventLoop::Exit() { epoll_.Quit(); }
922
923ShmEventLoop::~ShmEventLoop() {
Austin Schuh39788ff2019-12-01 18:22:57 -0800924 // Force everything with a registered fd with epoll to be destroyed now.
925 timers_.clear();
926 phased_loops_.clear();
927 watchers_.clear();
928
Alex Perrycb7da4b2019-08-28 19:35:56 -0700929 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
930}
931
Alex Perrycb7da4b2019-08-28 19:35:56 -0700932void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
933 if (is_running()) {
934 LOG(FATAL) << "Cannot set realtime priority while running.";
935 }
936 priority_ = priority;
937}
938
Brian Silverman6a54ff32020-04-28 16:41:39 -0700939void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
940 if (is_running()) {
941 LOG(FATAL) << "Cannot set affinity while running.";
942 }
943 affinity_ = cpuset;
944}
945
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800946void ShmEventLoop::set_name(const std::string_view name) {
947 name_ = std::string(name);
948 UpdateTimingReport();
949}
950
Brian Silverman5120afb2020-01-31 17:44:35 -0800951absl::Span<char> ShmEventLoop::GetWatcherSharedMemory(const Channel *channel) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500952 ShmWatcherState *const watcher_state =
953 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -0800954 return watcher_state->GetSharedMemory();
955}
956
957absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
958 const aos::RawSender *sender) const {
Brian Silverman148d43d2020-06-07 18:19:22 -0500959 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800960}
961
Brian Silverman6d2b3592020-06-18 14:40:15 -0700962absl::Span<char> ShmEventLoop::GetShmFetcherPrivateMemory(
963 const aos::RawFetcher *fetcher) const {
964 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
965}
966
Austin Schuh39788ff2019-12-01 18:22:57 -0800967pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
968
Alex Perrycb7da4b2019-08-28 19:35:56 -0700969} // namespace aos