blob: 238305064a11f658fe7f02e24b6c2f4e9162609c [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:
Austin Schuh432784f2020-06-23 17:27:35 -0700193 explicit SimpleShmFetcher(ShmEventLoop *event_loop, const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800194 bool copy_data)
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()) {
203 if (copy_data) {
204 data_storage_.reset(static_cast<char *>(
205 malloc(channel->max_size() + kChannelDataAlignment - 1)));
206 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207 context_.data = nullptr;
208 // Point the queue index at the next index to read starting now. This
209 // makes it such that FetchNext will read the next message sent after
210 // the fetcher is created.
211 PointAtNextQueueIndex();
212 }
213
Austin Schuh39788ff2019-12-01 18:22:57 -0800214 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700215
216 // Points the next message to fetch at the queue index which will be
217 // populated next.
218 void PointAtNextQueueIndex() {
219 actual_queue_index_ = lockless_queue_.LatestQueueIndex();
220 if (!actual_queue_index_.valid()) {
221 // Nothing in the queue. The next element will show up at the 0th
222 // index in the queue.
223 actual_queue_index_ =
224 ipc_lib::QueueIndex::Zero(lockless_queue_.queue_size());
225 } else {
226 actual_queue_index_ = actual_queue_index_.Increment();
227 }
228 }
229
Austin Schuh39788ff2019-12-01 18:22:57 -0800230 bool FetchNext() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700231 // TODO(austin): Get behind and make sure it dies both here and with
232 // Fetch.
233 ipc_lib::LocklessQueue::ReadResult read_result = lockless_queue_.Read(
Austin Schuhad154822019-12-27 15:45:13 -0800234 actual_queue_index_.index(), &context_.monotonic_event_time,
235 &context_.realtime_event_time, &context_.monotonic_remote_time,
236 &context_.realtime_remote_time, &context_.remote_queue_index,
Brian Silvermana1652f32020-01-29 20:41:44 -0800237 &context_.size, data_storage_start());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700238 if (read_result == ipc_lib::LocklessQueue::ReadResult::GOOD) {
239 context_.queue_index = actual_queue_index_.index();
Austin Schuhad154822019-12-27 15:45:13 -0800240 if (context_.remote_queue_index == 0xffffffffu) {
241 context_.remote_queue_index = context_.queue_index;
242 }
243 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
244 context_.monotonic_remote_time = context_.monotonic_event_time;
245 }
246 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
247 context_.realtime_remote_time = context_.realtime_event_time;
248 }
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800249 if (copy_data()) {
250 context_.data = data_storage_start() +
251 lockless_queue_.message_data_size() - context_.size;
252 } else {
253 context_.data = nullptr;
254 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700255 actual_queue_index_ = actual_queue_index_.Increment();
256 }
257
258 // Make sure the data wasn't modified while we were reading it. This
259 // can only happen if you are reading the last message *while* it is
260 // being written to, which means you are pretty far behind.
261 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::OVERWROTE)
262 << ": Got behind while reading and the last message was modified "
Austin Schuhf5652592019-12-29 16:26:15 -0800263 "out from under us while we were reading it. Don't get so far "
264 "behind. "
265 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700266
Austin Schuh432784f2020-06-23 17:27:35 -0700267 if (read_result == ipc_lib::LocklessQueue::ReadResult::TOO_OLD) {
268 event_loop_->SendTimingReport();
269 LOG(FATAL) << "The next message is no longer available. "
270 << configuration::CleanedChannelToString(channel_);
271 }
272
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD;
274 }
275
Austin Schuh39788ff2019-12-01 18:22:57 -0800276 bool Fetch() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277 const ipc_lib::QueueIndex queue_index = lockless_queue_.LatestQueueIndex();
278 // actual_queue_index_ is only meaningful if it was set by Fetch or
279 // FetchNext. This happens when valid_data_ has been set. So, only
280 // skip checking if valid_data_ is true.
281 //
282 // Also, if the latest queue index is invalid, we are empty. So there
283 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800284 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700285 queue_index == actual_queue_index_.DecrementBy(1u)) ||
286 !queue_index.valid()) {
287 return false;
288 }
289
Austin Schuhad154822019-12-27 15:45:13 -0800290 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,
Brian Silvermana1652f32020-01-29 20:41:44 -0800294 &context_.size, data_storage_start());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295 if (read_result == ipc_lib::LocklessQueue::ReadResult::GOOD) {
296 context_.queue_index = queue_index.index();
Austin Schuhad154822019-12-27 15:45:13 -0800297 if (context_.remote_queue_index == 0xffffffffu) {
298 context_.remote_queue_index = context_.queue_index;
299 }
300 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
301 context_.monotonic_remote_time = context_.monotonic_event_time;
302 }
303 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
304 context_.realtime_remote_time = context_.realtime_event_time;
305 }
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800306 if (copy_data()) {
307 context_.data = data_storage_start() +
308 lockless_queue_.message_data_size() - context_.size;
309 } else {
310 context_.data = nullptr;
311 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700312 actual_queue_index_ = queue_index.Increment();
313 }
314
315 // Make sure the data wasn't modified while we were reading it. This
316 // can only happen if you are reading the last message *while* it is
317 // being written to, which means you are pretty far behind.
318 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::OVERWROTE)
319 << ": Got behind while reading and the last message was modified "
Austin Schuhf5652592019-12-29 16:26:15 -0800320 "out from under us while we were reading it. Don't get so far "
321 "behind."
322 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700323
324 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800325 << ": Queue index went backwards. This should never happen. "
326 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700327
328 // We fell behind between when we read the index and read the value.
329 // This isn't worth recovering from since this means we went to sleep
330 // for a long time in the middle of this function.
Austin Schuh432784f2020-06-23 17:27:35 -0700331 if (read_result == ipc_lib::LocklessQueue::ReadResult::TOO_OLD) {
332 event_loop_->SendTimingReport();
333 LOG(FATAL) << "The next message is no longer available. "
334 << configuration::CleanedChannelToString(channel_);
335 }
336
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD;
338 }
339
Austin Schuh39788ff2019-12-01 18:22:57 -0800340 Context context() const { return context_; }
341
Alex Perrycb7da4b2019-08-28 19:35:56 -0700342 bool RegisterWakeup(int priority) {
343 return lockless_queue_.RegisterWakeup(priority);
344 }
345
346 void UnregisterWakeup() { lockless_queue_.UnregisterWakeup(); }
347
Brian Silverman5120afb2020-01-31 17:44:35 -0800348 absl::Span<char> GetSharedMemory() const {
349 return lockless_queue_memory_.GetSharedMemory();
350 }
351
Brian Silverman6d2b3592020-06-18 14:40:15 -0700352 absl::Span<char> GetPrivateMemory() const {
353 CHECK(copy_data());
354 return absl::Span<char>(
355 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
356 lockless_queue_.message_data_size());
357 }
358
Alex Perrycb7da4b2019-08-28 19:35:56 -0700359 private:
Brian Silvermana1652f32020-01-29 20:41:44 -0800360 char *data_storage_start() {
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800361 if (!copy_data()) return nullptr;
Brian Silvermana1652f32020-01-29 20:41:44 -0800362 return RoundChannelData(data_storage_.get(), channel_->max_size());
363 }
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800364 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800365
Austin Schuh432784f2020-06-23 17:27:35 -0700366 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800367 const Channel *const channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 MMapedQueue lockless_queue_memory_;
369 ipc_lib::LocklessQueue lockless_queue_;
370
371 ipc_lib::QueueIndex actual_queue_index_ =
372 ipc_lib::LocklessQueue::empty_queue_index();
373
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800374 // This being empty indicates we're not going to copy data.
375 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800376
377 Context context_;
378};
379
380class ShmFetcher : public RawFetcher {
381 public:
Austin Schuh432784f2020-06-23 17:27:35 -0700382 explicit ShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800383 : RawFetcher(event_loop, channel),
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800384 simple_shm_fetcher_(event_loop, channel, true) {}
Austin Schuh39788ff2019-12-01 18:22:57 -0800385
386 ~ShmFetcher() { context_.data = nullptr; }
387
388 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
389 if (simple_shm_fetcher_.FetchNext()) {
390 context_ = simple_shm_fetcher_.context();
391 return std::make_pair(true, monotonic_clock::now());
392 }
393 return std::make_pair(false, monotonic_clock::min_time);
394 }
395
396 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
397 if (simple_shm_fetcher_.Fetch()) {
398 context_ = simple_shm_fetcher_.context();
399 return std::make_pair(true, monotonic_clock::now());
400 }
401 return std::make_pair(false, monotonic_clock::min_time);
402 }
403
Brian Silverman6d2b3592020-06-18 14:40:15 -0700404 absl::Span<char> GetPrivateMemory() const {
405 return simple_shm_fetcher_.GetPrivateMemory();
406 }
407
Austin Schuh39788ff2019-12-01 18:22:57 -0800408 private:
409 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700410};
411
412class ShmSender : public RawSender {
413 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800414 explicit ShmSender(EventLoop *event_loop, const Channel *channel)
415 : RawSender(event_loop, channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800416 lockless_queue_memory_(
417 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800418 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800419 event_loop->configuration()->channel_storage_duration()))),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700420 lockless_queue_(lockless_queue_memory_.memory(),
421 lockless_queue_memory_.config()),
Austin Schuhe516ab02020-05-06 21:37:04 -0700422 lockless_queue_sender_(
423 VerifySender(lockless_queue_.MakeSender(), channel)) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700424
Austin Schuh39788ff2019-12-01 18:22:57 -0800425 ~ShmSender() override {}
426
Austin Schuhe516ab02020-05-06 21:37:04 -0700427 static ipc_lib::LocklessQueue::Sender VerifySender(
428 std::optional<ipc_lib::LocklessQueue::Sender> &&sender,
429 const Channel *channel) {
430 if (sender) {
431 return std::move(sender.value());
432 }
433 LOG(FATAL) << "Failed to create sender on "
434 << configuration::CleanedChannelToString(channel)
435 << ", too many senders.";
436 }
437
Alex Perrycb7da4b2019-08-28 19:35:56 -0700438 void *data() override { return lockless_queue_sender_.Data(); }
439 size_t size() override { return lockless_queue_sender_.size(); }
Austin Schuhad154822019-12-27 15:45:13 -0800440 bool DoSend(size_t length,
441 aos::monotonic_clock::time_point monotonic_remote_time,
442 aos::realtime_clock::time_point realtime_remote_time,
443 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700444 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
445 << ": Sent too big a message on "
446 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800447 lockless_queue_sender_.Send(
448 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
449 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800450 lockless_queue_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451 return true;
452 }
453
Austin Schuhad154822019-12-27 15:45:13 -0800454 bool DoSend(const void *msg, size_t length,
455 aos::monotonic_clock::time_point monotonic_remote_time,
456 aos::realtime_clock::time_point realtime_remote_time,
457 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700458 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
459 << ": Sent too big a message on "
460 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800461 lockless_queue_sender_.Send(reinterpret_cast<const char *>(msg), length,
462 monotonic_remote_time, realtime_remote_time,
463 remote_queue_index, &monotonic_sent_time_,
464 &realtime_sent_time_, &sent_queue_index_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800465 lockless_queue_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 // TODO(austin): Return an error if we send too fast.
467 return true;
468 }
469
Brian Silverman5120afb2020-01-31 17:44:35 -0800470 absl::Span<char> GetSharedMemory() const {
471 return lockless_queue_memory_.GetSharedMemory();
472 }
473
Alex Perrycb7da4b2019-08-28 19:35:56 -0700474 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700475 MMapedQueue lockless_queue_memory_;
476 ipc_lib::LocklessQueue lockless_queue_;
477 ipc_lib::LocklessQueue::Sender lockless_queue_sender_;
478};
479
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500481class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700482 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500483 ShmWatcherState(
Austin Schuh7d87b672019-12-01 20:23:49 -0800484 ShmEventLoop *event_loop, const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800485 std::function<void(const Context &context, const void *message)> fn,
486 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500487 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800488 event_loop_(event_loop),
489 event_(this),
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800490 simple_shm_fetcher_(event_loop, channel, copy_data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700491
Brian Silverman148d43d2020-06-07 18:19:22 -0500492 ~ShmWatcherState() override { event_loop_->RemoveEvent(&event_); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800493
494 void Startup(EventLoop *event_loop) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800495 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800496 CHECK(RegisterWakeup(event_loop->priority()));
497 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700498
Alex Perrycb7da4b2019-08-28 19:35:56 -0700499 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800500 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700501 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800502 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800503
504 if (has_new_data_) {
505 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800506 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800507 event_loop_->AddEvent(&event_);
508 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700509 }
510
511 return has_new_data_;
512 }
513
Alex Perrycb7da4b2019-08-28 19:35:56 -0700514 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800515 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700516 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800517 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700518 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800519 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700520 }
521
Austin Schuh39788ff2019-12-01 18:22:57 -0800522 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700523 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800524 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700525 }
526
Austin Schuh39788ff2019-12-01 18:22:57 -0800527 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700528
Brian Silverman5120afb2020-01-31 17:44:35 -0800529 absl::Span<char> GetSharedMemory() const {
530 return simple_shm_fetcher_.GetSharedMemory();
531 }
532
Alex Perrycb7da4b2019-08-28 19:35:56 -0700533 private:
534 bool has_new_data_ = false;
535
Austin Schuh7d87b672019-12-01 20:23:49 -0800536 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500537 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800538 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700539};
540
541// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500542class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700543 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500544 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800545 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800546 shm_event_loop_(shm_event_loop),
547 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800548 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
549 // The timer may fire spurriously. HandleEvent on the event loop will
550 // call the callback if it is needed. It may also have called it when
551 // processing some other event, and the kernel decided to deliver this
552 // wakeup anyways.
553 timerfd_.Read();
554 shm_event_loop_->HandleEvent();
555 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700556 }
557
Brian Silverman148d43d2020-06-07 18:19:22 -0500558 ~ShmTimerHandler() {
Austin Schuh7d87b672019-12-01 20:23:49 -0800559 Disable();
560 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
561 }
562
563 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800564 CHECK(!event_.valid());
565 const auto monotonic_now = Call(monotonic_clock::now, base_);
566 if (event_.valid()) {
567 // If someone called Setup inside Call, rescheduling is already taken care
568 // of. Bail.
569 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800570 }
571
Austin Schuhcde39fd2020-02-22 20:58:24 -0800572 if (repeat_offset_ == chrono::seconds(0)) {
573 timerfd_.Disable();
574 } else {
575 // Compute how many cycles have elapsed and schedule the next iteration
576 // for the next iteration in the future.
577 const int elapsed_cycles =
578 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
579 std::chrono::nanoseconds(1)) /
580 repeat_offset_);
581 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800582
Austin Schuhcde39fd2020-02-22 20:58:24 -0800583 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800584 event_.set_event_time(base_);
585 shm_event_loop_->AddEvent(&event_);
Austin Schuhcde39fd2020-02-22 20:58:24 -0800586 timerfd_.SetTime(base_, chrono::seconds(0));
Austin Schuh7d87b672019-12-01 20:23:49 -0800587 }
588 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700589
590 void Setup(monotonic_clock::time_point base,
591 monotonic_clock::duration repeat_offset) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800592 if (event_.valid()) {
593 shm_event_loop_->RemoveEvent(&event_);
594 }
595
Alex Perrycb7da4b2019-08-28 19:35:56 -0700596 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800597 base_ = base;
598 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800599 event_.set_event_time(base_);
600 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700601 }
602
Austin Schuh7d87b672019-12-01 20:23:49 -0800603 void Disable() override {
604 shm_event_loop_->RemoveEvent(&event_);
605 timerfd_.Disable();
606 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700607
608 private:
609 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500610 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700611
Brian Silverman148d43d2020-06-07 18:19:22 -0500612 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700613
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800614 monotonic_clock::time_point base_;
615 monotonic_clock::duration repeat_offset_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700616};
617
618// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500619class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700620 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500621 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
622 ::std::function<void(int)> fn,
623 const monotonic_clock::duration interval,
624 const monotonic_clock::duration offset)
625 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800626 shm_event_loop_(shm_event_loop),
627 event_(this) {
628 shm_event_loop_->epoll_.OnReadable(
629 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
630 }
631
632 void HandleEvent() {
633 // The return value for read is the number of cycles that have elapsed.
634 // Because we check to see when this event *should* have happened, there are
635 // cases where Read() will return 0, when 1 cycle has actually happened.
636 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
637 // ignore it. Call handles rescheduling and calculating elapsed cycles
638 // without any extra help.
639 timerfd_.Read();
640 event_.Invalidate();
641
642 Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) {
643 Schedule(sleep_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700644 });
645 }
646
Brian Silverman148d43d2020-06-07 18:19:22 -0500647 ~ShmPhasedLoopHandler() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800648 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800649 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700650 }
651
652 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800653 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800654 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800655 if (event_.valid()) {
656 shm_event_loop_->RemoveEvent(&event_);
657 }
658
Austin Schuh39788ff2019-12-01 18:22:57 -0800659 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800660 event_.set_event_time(sleep_time);
661 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700662 }
663
664 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500665 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700666
Brian Silverman148d43d2020-06-07 18:19:22 -0500667 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700668};
Brian Silverman148d43d2020-06-07 18:19:22 -0500669
670} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700671
672::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
673 const Channel *channel) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800674 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
675 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
676 << "\", \"type\": \"" << channel->type()->string_view()
677 << "\" } is not able to be fetched on this node. Check your "
678 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800679 }
680
Brian Silverman148d43d2020-06-07 18:19:22 -0500681 return ::std::unique_ptr<RawFetcher>(new ShmFetcher(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700682}
683
684::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
685 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800686 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800687
Brian Silverman148d43d2020-06-07 18:19:22 -0500688 return ::std::unique_ptr<RawSender>(new ShmSender(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700689}
690
691void ShmEventLoop::MakeRawWatcher(
692 const Channel *channel,
693 std::function<void(const Context &context, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800694 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800695
Austin Schuh39788ff2019-12-01 18:22:57 -0800696 NewWatcher(::std::unique_ptr<WatcherState>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500697 new ShmWatcherState(this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800698}
699
700void ShmEventLoop::MakeRawNoArgWatcher(
701 const Channel *channel,
702 std::function<void(const Context &context)> watcher) {
703 TakeWatcher(channel);
704
Brian Silverman148d43d2020-06-07 18:19:22 -0500705 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800706 this, channel,
707 [watcher](const Context &context, const void *) { watcher(context); },
708 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700709}
710
711TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800712 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500713 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700714}
715
716PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
717 ::std::function<void(int)> callback,
718 const monotonic_clock::duration interval,
719 const monotonic_clock::duration offset) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500720 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
721 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700722}
723
724void ShmEventLoop::OnRun(::std::function<void()> on_run) {
725 on_run_.push_back(::std::move(on_run));
726}
727
Austin Schuh7d87b672019-12-01 20:23:49 -0800728void ShmEventLoop::HandleEvent() {
729 // Update all the times for handlers.
730 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500731 ShmWatcherState *watcher =
732 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Austin Schuh7d87b672019-12-01 20:23:49 -0800733
734 watcher->CheckForNewData();
735 }
736
Austin Schuh39788ff2019-12-01 18:22:57 -0800737 while (true) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800738 if (EventCount() == 0 ||
739 PeekEvent()->event_time() > monotonic_clock::now()) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800740 break;
741 }
742
Austin Schuh7d87b672019-12-01 20:23:49 -0800743 EventLoopEvent *event = PopEvent();
744 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800745 }
746}
747
Austin Schuh32fd5a72019-12-01 22:20:26 -0800748// RAII class to mask signals.
749class ScopedSignalMask {
750 public:
751 ScopedSignalMask(std::initializer_list<int> signals) {
752 sigset_t sigset;
753 PCHECK(sigemptyset(&sigset) == 0);
754 for (int signal : signals) {
755 PCHECK(sigaddset(&sigset, signal) == 0);
756 }
757
758 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
759 }
760
761 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
762
763 private:
764 sigset_t old_;
765};
766
767// Class to manage the static state associated with killing multiple event
768// loops.
769class SignalHandler {
770 public:
771 // Gets the singleton.
772 static SignalHandler *global() {
773 static SignalHandler loop;
774 return &loop;
775 }
776
777 // Handles the signal with the singleton.
778 static void HandleSignal(int) { global()->DoHandleSignal(); }
779
780 // Registers an event loop to receive Exit() calls.
781 void Register(ShmEventLoop *event_loop) {
782 // Block signals while we have the mutex so we never race with the signal
783 // handler.
784 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
785 std::unique_lock<stl_mutex> locker(mutex_);
786 if (event_loops_.size() == 0) {
787 // The first caller registers the signal handler.
788 struct sigaction new_action;
789 sigemptyset(&new_action.sa_mask);
790 // This makes it so that 2 control c's to a stuck process will kill it by
791 // restoring the original signal handler.
792 new_action.sa_flags = SA_RESETHAND;
793 new_action.sa_handler = &HandleSignal;
794
795 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
796 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
797 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
798 }
799
800 event_loops_.push_back(event_loop);
801 }
802
803 // Unregisters an event loop to receive Exit() calls.
804 void Unregister(ShmEventLoop *event_loop) {
805 // Block signals while we have the mutex so we never race with the signal
806 // handler.
807 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
808 std::unique_lock<stl_mutex> locker(mutex_);
809
Brian Silverman5120afb2020-01-31 17:44:35 -0800810 event_loops_.erase(
811 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800812
813 if (event_loops_.size() == 0u) {
814 // The last caller restores the original signal handlers.
815 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
816 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
817 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
818 }
819 }
820
821 private:
822 void DoHandleSignal() {
823 // We block signals while grabbing the lock, so there should never be a
824 // race. Confirm that this is true using trylock.
825 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
826 "modifing the event loop list.";
827 for (ShmEventLoop *event_loop : event_loops_) {
828 event_loop->Exit();
829 }
830 mutex_.unlock();
831 }
832
833 // Mutex to protect all state.
834 stl_mutex mutex_;
835 std::vector<ShmEventLoop *> event_loops_;
836 struct sigaction old_action_int_;
837 struct sigaction old_action_hup_;
838 struct sigaction old_action_term_;
839};
840
Alex Perrycb7da4b2019-08-28 19:35:56 -0700841void ShmEventLoop::Run() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800842 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -0800843
Alex Perrycb7da4b2019-08-28 19:35:56 -0700844 std::unique_ptr<ipc_lib::SignalFd> signalfd;
845
846 if (watchers_.size() > 0) {
847 signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
848
849 epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() {
850 signalfd_siginfo result = signalfd_ptr->Read();
851 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
852
853 // TODO(austin): We should really be checking *everything*, not just
854 // watchers, and calling the oldest thing first. That will improve
855 // determinism a lot.
856
Austin Schuh7d87b672019-12-01 20:23:49 -0800857 HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700858 });
859 }
860
Austin Schuh39788ff2019-12-01 18:22:57 -0800861 MaybeScheduleTimingReports();
862
Austin Schuh7d87b672019-12-01 20:23:49 -0800863 ReserveEvents();
864
Tyler Chatow67ddb032020-01-12 14:30:04 -0800865 {
866 AosLogToFbs aos_logger;
867 if (!skip_logger_) {
868 aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
869 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700870
Tyler Chatow67ddb032020-01-12 14:30:04 -0800871 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -0700872 const cpu_set_t default_affinity = DefaultAffinity();
873 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
874 ::aos::SetCurrentThreadAffinity(affinity_);
875 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800876 // Now, all the callbacks are setup. Lock everything into memory and go RT.
877 if (priority_ != 0) {
878 ::aos::InitRT();
879
880 LOG(INFO) << "Setting priority to " << priority_;
881 ::aos::SetCurrentThreadRealtimePriority(priority_);
882 }
883
884 set_is_running(true);
885
886 // Now that we are realtime (but before the OnRun handlers run), snap the
887 // queue index.
888 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
889 watcher->Startup(this);
890 }
891
892 // Now that we are RT, run all the OnRun handlers.
893 for (const auto &run : on_run_) {
894 run();
895 }
896
897 // And start our main event loop which runs all the timers and handles Quit.
898 epoll_.Run();
899
900 // Once epoll exits, there is no useful nonrt work left to do.
901 set_is_running(false);
902
903 // Nothing time or synchronization critical needs to happen after this
904 // point. Drop RT priority.
905 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700906 }
907
Austin Schuh39788ff2019-12-01 18:22:57 -0800908 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500909 ShmWatcherState *watcher =
910 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700911 watcher->UnregisterWakeup();
912 }
913
914 if (watchers_.size() > 0) {
915 epoll_.DeleteFd(signalfd->fd());
916 signalfd.reset();
917 }
Austin Schuh32fd5a72019-12-01 22:20:26 -0800918
919 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800920
921 // Trigger any remaining senders or fetchers to be cleared before destroying
922 // the event loop so the book keeping matches. Do this in the thread that
923 // created the timing reporter.
924 timing_report_sender_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700925}
926
927void ShmEventLoop::Exit() { epoll_.Quit(); }
928
929ShmEventLoop::~ShmEventLoop() {
Austin Schuh39788ff2019-12-01 18:22:57 -0800930 // Force everything with a registered fd with epoll to be destroyed now.
931 timers_.clear();
932 phased_loops_.clear();
933 watchers_.clear();
934
Alex Perrycb7da4b2019-08-28 19:35:56 -0700935 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
936}
937
Alex Perrycb7da4b2019-08-28 19:35:56 -0700938void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
939 if (is_running()) {
940 LOG(FATAL) << "Cannot set realtime priority while running.";
941 }
942 priority_ = priority;
943}
944
Brian Silverman6a54ff32020-04-28 16:41:39 -0700945void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
946 if (is_running()) {
947 LOG(FATAL) << "Cannot set affinity while running.";
948 }
949 affinity_ = cpuset;
950}
951
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800952void ShmEventLoop::set_name(const std::string_view name) {
953 name_ = std::string(name);
954 UpdateTimingReport();
955}
956
Brian Silverman5120afb2020-01-31 17:44:35 -0800957absl::Span<char> ShmEventLoop::GetWatcherSharedMemory(const Channel *channel) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500958 ShmWatcherState *const watcher_state =
959 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -0800960 return watcher_state->GetSharedMemory();
961}
962
963absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
964 const aos::RawSender *sender) const {
Brian Silverman148d43d2020-06-07 18:19:22 -0500965 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800966}
967
Brian Silverman6d2b3592020-06-18 14:40:15 -0700968absl::Span<char> ShmEventLoop::GetShmFetcherPrivateMemory(
969 const aos::RawFetcher *fetcher) const {
970 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
971}
972
Austin Schuh39788ff2019-12-01 18:22:57 -0800973pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
974
Alex Perrycb7da4b2019-08-28 19:35:56 -0700975} // namespace aos