blob: 601370926b7020dceb7b9d40bc5c1953887e961a [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/shm_event_loop.h"
2
3#include <sys/mman.h>
4#include <sys/stat.h>
Austin Schuh39788ff2019-12-01 18:22:57 -08005#include <sys/syscall.h>
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include <sys/types.h>
7#include <unistd.h>
Tyler Chatow67ddb032020-01-12 14:30:04 -08008
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include <algorithm>
10#include <atomic>
11#include <chrono>
Austin Schuh39788ff2019-12-01 18:22:57 -080012#include <iterator>
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include <stdexcept>
14
Tyler Chatow67ddb032020-01-12 14:30:04 -080015#include "aos/events/aos_logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "aos/events/epoll.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080017#include "aos/events/event_loop_generated.h"
18#include "aos/events/timing_statistics.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "aos/ipc_lib/lockless_queue.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080020#include "aos/ipc_lib/signalfd.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070021#include "aos/realtime.h"
Austin Schuh32fd5a72019-12-01 22:20:26 -080022#include "aos/stl_mutex/stl_mutex.h"
Austin Schuhfccb2d02020-01-26 16:11:19 -080023#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070024#include "aos/util/phased_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080025#include "glog/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070026
Austin Schuhe84c3ed2019-12-14 15:29:48 -080027namespace {
28
29// Returns the portion of the path after the last /. This very much assumes
30// that the application name is null terminated.
31const char *Filename(const char *path) {
32 const std::string_view path_string_view = path;
33 auto last_slash_pos = path_string_view.find_last_of("/");
34
35 return last_slash_pos == std::string_view::npos ? path
36 : path + last_slash_pos + 1;
37}
38
39} // namespace
40
Alex Perrycb7da4b2019-08-28 19:35:56 -070041DEFINE_string(shm_base, "/dev/shm/aos",
42 "Directory to place queue backing mmaped files in.");
43DEFINE_uint32(permissions, 0770,
44 "Permissions to make shared memory files and folders.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080045DEFINE_string(application_name, Filename(program_invocation_name),
46 "The application name");
Alex Perrycb7da4b2019-08-28 19:35:56 -070047
48namespace aos {
49
Brian Silverman148d43d2020-06-07 18:19:22 -050050using namespace shm_event_loop_internal;
51
Austin Schuhcdab6192019-12-29 17:47:46 -080052void SetShmBase(const std::string_view base) {
53 FLAGS_shm_base = std::string(base) + "/dev/shm/aos";
54}
55
Brian Silverman4f4e0612020-08-12 19:54:41 -070056namespace {
57
Alex Perrycb7da4b2019-08-28 19:35:56 -070058std::string ShmFolder(const Channel *channel) {
59 CHECK(channel->has_name());
60 CHECK_EQ(channel->name()->string_view()[0], '/');
61 return FLAGS_shm_base + channel->name()->str() + "/";
62}
63std::string ShmPath(const Channel *channel) {
64 CHECK(channel->has_type());
Brian Silverman177567e2020-08-12 19:51:33 -070065 return ShmFolder(channel) + channel->type()->str() + ".v3";
Alex Perrycb7da4b2019-08-28 19:35:56 -070066}
67
Brian Silvermana5450a92020-08-12 19:59:57 -070068void PageFaultDataWrite(char *data, size_t size) {
Brian Silverman3b0cdaf2020-04-28 16:51:51 -070069 // This just has to divide the actual page size. Being smaller will make this
70 // a bit slower than necessary, but not much. 1024 is a pretty conservative
71 // choice (most pages are probably 4096).
72 static constexpr size_t kPageSize = 1024;
73 const size_t pages = (size + kPageSize - 1) / kPageSize;
74 for (size_t i = 0; i < pages; ++i) {
75 char zero = 0;
76 // We need to ensure there's a writable pagetable entry, but avoid modifying
77 // the data.
78 //
79 // Even if you lock the data into memory, some kernels still seem to lazily
80 // create the actual pagetable entries. This means we need to somehow
81 // "write" to the page.
82 //
83 // Also, this takes place while other processes may be concurrently
84 // opening/initializing the memory, so we need to avoid corrupting that.
85 //
86 // This is the simplest operation I could think of which achieves that:
87 // "store 0 if it's already 0".
88 __atomic_compare_exchange_n(&data[i * kPageSize], &zero, 0, true,
89 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
90 }
91}
92
Brian Silvermana5450a92020-08-12 19:59:57 -070093void PageFaultDataRead(const char *data, size_t size) {
94 // This just has to divide the actual page size. Being smaller will make this
95 // a bit slower than necessary, but not much. 1024 is a pretty conservative
96 // choice (most pages are probably 4096).
97 static constexpr size_t kPageSize = 1024;
98 const size_t pages = (size + kPageSize - 1) / kPageSize;
99 for (size_t i = 0; i < pages; ++i) {
100 // We need to ensure there's a readable pagetable entry.
101 __atomic_load_n(&data[i * kPageSize], __ATOMIC_RELAXED);
102 }
103}
104
Brian Silverman4f4e0612020-08-12 19:54:41 -0700105ipc_lib::LocklessQueueConfiguration MakeQueueConfiguration(
106 const Channel *channel, std::chrono::seconds channel_storage_duration) {
107 ipc_lib::LocklessQueueConfiguration config;
108
109 config.num_watchers = channel->num_watchers();
110 config.num_senders = channel->num_senders();
111 // The value in the channel will default to 0 if readers are configured to
112 // copy.
113 config.num_pinners = channel->num_readers();
114 config.queue_size = channel_storage_duration.count() * channel->frequency();
115 config.message_data_size = channel->max_size();
116
117 return config;
118}
119
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120class MMapedQueue {
121 public:
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800122 MMapedQueue(const Channel *channel,
Brian Silverman4f4e0612020-08-12 19:54:41 -0700123 std::chrono::seconds channel_storage_duration)
124 : config_(MakeQueueConfiguration(channel, channel_storage_duration)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700125 std::string path = ShmPath(channel);
126
Alex Perrycb7da4b2019-08-28 19:35:56 -0700127 size_ = ipc_lib::LocklessQueueMemorySize(config_);
128
Austin Schuhfccb2d02020-01-26 16:11:19 -0800129 util::MkdirP(path, FLAGS_permissions);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130
131 // There are 2 cases. Either the file already exists, or it does not
132 // already exist and we need to create it. Start by trying to create it. If
133 // that fails, the file has already been created and we can open it
Brian Silverman4f4e0612020-08-12 19:54:41 -0700134 // normally.. Once the file has been created it will never be deleted.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800135 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_EXCL,
Brian Silverman148d43d2020-06-07 18:19:22 -0500136 O_CLOEXEC | FLAGS_permissions);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800137 if (fd == -1 && errno == EEXIST) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 VLOG(1) << path << " already created.";
139 // File already exists.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800140 fd = open(path.c_str(), O_RDWR, O_CLOEXEC);
141 PCHECK(fd != -1) << ": Failed to open " << path;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700142 while (true) {
143 struct stat st;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800144 PCHECK(fstat(fd, &st) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700145 if (st.st_size != 0) {
146 CHECK_EQ(static_cast<size_t>(st.st_size), size_)
147 << ": Size of " << path
148 << " doesn't match expected size of backing queue file. Did the "
149 "queue definition change?";
150 break;
151 } else {
152 // The creating process didn't get around to it yet. Give it a bit.
153 std::this_thread::sleep_for(std::chrono::milliseconds(10));
154 VLOG(1) << path << " is zero size, waiting";
155 }
156 }
157 } else {
158 VLOG(1) << "Created " << path;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800159 PCHECK(fd != -1) << ": Failed to open " << path;
160 PCHECK(ftruncate(fd, size_) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 }
162
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800163 data_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 PCHECK(data_ != MAP_FAILED);
Brian Silvermana5450a92020-08-12 19:59:57 -0700165 const_data_ = mmap(NULL, size_, PROT_READ, MAP_SHARED, fd, 0);
166 PCHECK(const_data_ != MAP_FAILED);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800167 PCHECK(close(fd) == 0);
Brian Silvermana5450a92020-08-12 19:59:57 -0700168 PageFaultDataWrite(static_cast<char *>(data_), size_);
169 PageFaultDataRead(static_cast<const char *>(const_data_), size_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170
171 ipc_lib::InitializeLocklessQueueMemory(memory(), config_);
172 }
173
Brian Silvermana5450a92020-08-12 19:59:57 -0700174 ~MMapedQueue() {
175 PCHECK(munmap(data_, size_) == 0);
176 PCHECK(munmap(const_cast<void *>(const_data_), size_) == 0);
177 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700178
179 ipc_lib::LocklessQueueMemory *memory() const {
180 return reinterpret_cast<ipc_lib::LocklessQueueMemory *>(data_);
181 }
182
Brian Silvermana5450a92020-08-12 19:59:57 -0700183 const ipc_lib::LocklessQueueMemory *const_memory() const {
184 return reinterpret_cast<const ipc_lib::LocklessQueueMemory *>(const_data_);
185 }
186
Austin Schuh39788ff2019-12-01 18:22:57 -0800187 const ipc_lib::LocklessQueueConfiguration &config() const { return config_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700189 ipc_lib::LocklessQueue queue() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700190 return ipc_lib::LocklessQueue(const_memory(), memory(), config());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700191 }
192
Brian Silvermana5450a92020-08-12 19:59:57 -0700193 absl::Span<char> GetMutableSharedMemory() const {
Brian Silverman5120afb2020-01-31 17:44:35 -0800194 return absl::Span<char>(static_cast<char *>(data_), size_);
195 }
196
Brian Silvermana5450a92020-08-12 19:59:57 -0700197 absl::Span<const char> GetConstSharedMemory() const {
198 return absl::Span<const char>(static_cast<const char *>(const_data_),
199 size_);
200 }
201
Alex Perrycb7da4b2019-08-28 19:35:56 -0700202 private:
Brian Silverman4f4e0612020-08-12 19:54:41 -0700203 const ipc_lib::LocklessQueueConfiguration config_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204
Alex Perrycb7da4b2019-08-28 19:35:56 -0700205 size_t size_;
206 void *data_;
Brian Silvermana5450a92020-08-12 19:59:57 -0700207 const void *const_data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700208};
209
Austin Schuh217a9782019-12-21 23:02:50 -0800210const Node *MaybeMyNode(const Configuration *configuration) {
211 if (!configuration->has_nodes()) {
212 return nullptr;
213 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700214
Austin Schuh217a9782019-12-21 23:02:50 -0800215 return configuration::GetMyNode(configuration);
216}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700217
218namespace chrono = ::std::chrono;
219
Austin Schuh39788ff2019-12-01 18:22:57 -0800220} // namespace
221
Austin Schuh217a9782019-12-21 23:02:50 -0800222ShmEventLoop::ShmEventLoop(const Configuration *configuration)
223 : EventLoop(configuration),
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800224 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -0800225 node_(MaybeMyNode(configuration)) {
226 if (configuration->has_nodes()) {
227 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
228 }
229}
Austin Schuh217a9782019-12-21 23:02:50 -0800230
Brian Silverman148d43d2020-06-07 18:19:22 -0500231namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -0800232
233class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700234 public:
Brian Silverman3bca5322020-08-12 19:35:29 -0700235 explicit SimpleShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700236 : event_loop_(event_loop),
237 channel_(channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800238 lockless_queue_memory_(
239 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800240 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800241 event_loop->configuration()->channel_storage_duration()))),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700242 reader_(lockless_queue_memory_.queue()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700243 context_.data = nullptr;
244 // Point the queue index at the next index to read starting now. This
245 // makes it such that FetchNext will read the next message sent after
246 // the fetcher is created.
247 PointAtNextQueueIndex();
248 }
249
Austin Schuh39788ff2019-12-01 18:22:57 -0800250 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700251
Brian Silverman77162972020-08-12 19:52:40 -0700252 // Sets this object to pin or copy data, as configured in the channel.
253 void RetrieveData() {
254 if (channel_->read_method() == ReadMethod::PIN) {
255 PinDataOnFetch();
256 } else {
257 CopyDataOnFetch();
258 }
259 }
260
Brian Silverman3bca5322020-08-12 19:35:29 -0700261 // Sets this object to copy data out of the shared memory into a private
262 // buffer when fetching.
263 void CopyDataOnFetch() {
Brian Silverman77162972020-08-12 19:52:40 -0700264 CHECK(!pin_data());
Brian Silverman3bca5322020-08-12 19:35:29 -0700265 data_storage_.reset(static_cast<char *>(
266 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
267 }
268
Brian Silverman77162972020-08-12 19:52:40 -0700269 // Sets this object to pin data in shared memory when fetching.
270 void PinDataOnFetch() {
271 CHECK(!copy_data());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700272 auto maybe_pinner =
273 ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue());
Brian Silverman77162972020-08-12 19:52:40 -0700274 if (!maybe_pinner) {
275 LOG(FATAL) << "Failed to create reader on "
276 << configuration::CleanedChannelToString(channel_)
277 << ", too many readers.";
278 }
279 pinner_ = std::move(maybe_pinner.value());
280 }
281
Alex Perrycb7da4b2019-08-28 19:35:56 -0700282 // Points the next message to fetch at the queue index which will be
283 // populated next.
284 void PointAtNextQueueIndex() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700285 actual_queue_index_ = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700286 if (!actual_queue_index_.valid()) {
287 // Nothing in the queue. The next element will show up at the 0th
288 // index in the queue.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700289 actual_queue_index_ = ipc_lib::QueueIndex::Zero(
290 LocklessQueueSize(lockless_queue_memory_.memory()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700291 } else {
292 actual_queue_index_ = actual_queue_index_.Increment();
293 }
294 }
295
Austin Schuh39788ff2019-12-01 18:22:57 -0800296 bool FetchNext() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700297 const ipc_lib::LocklessQueueReader::Result read_result =
Brian Silverman3bca5322020-08-12 19:35:29 -0700298 DoFetch(actual_queue_index_);
Austin Schuh432784f2020-06-23 17:27:35 -0700299
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700300 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 }
302
Austin Schuh39788ff2019-12-01 18:22:57 -0800303 bool Fetch() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700304 const ipc_lib::QueueIndex queue_index = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700305 // actual_queue_index_ is only meaningful if it was set by Fetch or
306 // FetchNext. This happens when valid_data_ has been set. So, only
307 // skip checking if valid_data_ is true.
308 //
309 // Also, if the latest queue index is invalid, we are empty. So there
310 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800311 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700312 queue_index == actual_queue_index_.DecrementBy(1u)) ||
313 !queue_index.valid()) {
314 return false;
315 }
316
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700317 const ipc_lib::LocklessQueueReader::Result read_result =
318 DoFetch(queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700319
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700320 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800321 << ": Queue index went backwards. This should never happen. "
322 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700323
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700324 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325 }
326
Austin Schuh39788ff2019-12-01 18:22:57 -0800327 Context context() const { return context_; }
328
Alex Perrycb7da4b2019-08-28 19:35:56 -0700329 bool RegisterWakeup(int priority) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700330 CHECK(!watcher_);
331 watcher_ = ipc_lib::LocklessQueueWatcher::Make(
332 lockless_queue_memory_.queue(), priority);
333 return static_cast<bool>(watcher_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700334 }
335
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700336 void UnregisterWakeup() {
337 CHECK(watcher_);
338 watcher_ = std::nullopt;
339 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700340
Brian Silvermana5450a92020-08-12 19:59:57 -0700341 absl::Span<char> GetMutableSharedMemory() {
342 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800343 }
344
Brian Silvermana5450a92020-08-12 19:59:57 -0700345 absl::Span<const char> GetConstSharedMemory() const {
346 return lockless_queue_memory_.GetConstSharedMemory();
347 }
348
349 absl::Span<const char> GetPrivateMemory() const {
350 if (pin_data()) {
351 return lockless_queue_memory_.GetConstSharedMemory();
352 }
Brian Silverman6d2b3592020-06-18 14:40:15 -0700353 return absl::Span<char>(
354 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700355 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()));
Brian Silverman6d2b3592020-06-18 14:40:15 -0700356 }
357
Alex Perrycb7da4b2019-08-28 19:35:56 -0700358 private:
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700359 ipc_lib::LocklessQueueReader::Result DoFetch(
360 ipc_lib::QueueIndex queue_index) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700361 // TODO(austin): Get behind and make sure it dies.
362 char *copy_buffer = nullptr;
363 if (copy_data()) {
364 copy_buffer = data_storage_start();
365 }
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700366 ipc_lib::LocklessQueueReader::Result read_result = reader_.Read(
Brian Silverman3bca5322020-08-12 19:35:29 -0700367 queue_index.index(), &context_.monotonic_event_time,
368 &context_.realtime_event_time, &context_.monotonic_remote_time,
369 &context_.realtime_remote_time, &context_.remote_queue_index,
370 &context_.size, copy_buffer);
371
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700372 if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) {
Brian Silverman77162972020-08-12 19:52:40 -0700373 if (pin_data()) {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700374 const int pin_result = pinner_->PinIndex(queue_index.index());
375 CHECK(pin_result >= 0)
Brian Silverman77162972020-08-12 19:52:40 -0700376 << ": Got behind while reading and the last message was modified "
377 "out from under us while we tried to pin it. Don't get so far "
378 "behind on: "
379 << configuration::CleanedChannelToString(channel_);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700380 context_.buffer_index = pin_result;
381 } else {
382 context_.buffer_index = -1;
Brian Silverman77162972020-08-12 19:52:40 -0700383 }
384
Brian Silverman3bca5322020-08-12 19:35:29 -0700385 context_.queue_index = queue_index.index();
386 if (context_.remote_queue_index == 0xffffffffu) {
387 context_.remote_queue_index = context_.queue_index;
388 }
389 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
390 context_.monotonic_remote_time = context_.monotonic_event_time;
391 }
392 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
393 context_.realtime_remote_time = context_.realtime_event_time;
394 }
395 const char *const data = DataBuffer();
396 if (data) {
397 context_.data =
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700398 data +
399 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) -
400 context_.size;
Brian Silverman3bca5322020-08-12 19:35:29 -0700401 } else {
402 context_.data = nullptr;
403 }
404 actual_queue_index_ = queue_index.Increment();
405 }
406
407 // Make sure the data wasn't modified while we were reading it. This
408 // can only happen if you are reading the last message *while* it is
409 // being written to, which means you are pretty far behind.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700410 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE)
Brian Silverman3bca5322020-08-12 19:35:29 -0700411 << ": Got behind while reading and the last message was modified "
412 "out from under us while we were reading it. Don't get so far "
413 "behind on: "
414 << configuration::CleanedChannelToString(channel_);
415
416 // We fell behind between when we read the index and read the value.
417 // This isn't worth recovering from since this means we went to sleep
418 // for a long time in the middle of this function.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700419 if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700420 event_loop_->SendTimingReport();
421 LOG(FATAL) << "The next message is no longer available. "
422 << configuration::CleanedChannelToString(channel_);
423 }
424
425 return read_result;
426 }
427
428 char *data_storage_start() const {
429 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800430 return RoundChannelData(data_storage_.get(), channel_->max_size());
431 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700432
433 // Note that for some modes the return value will change as new messages are
434 // read.
435 const char *DataBuffer() const {
436 if (copy_data()) {
437 return data_storage_start();
438 }
Brian Silverman77162972020-08-12 19:52:40 -0700439 if (pin_data()) {
440 return static_cast<const char *>(pinner_->Data());
441 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700442 return nullptr;
443 }
444
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800445 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silverman77162972020-08-12 19:52:40 -0700446 bool pin_data() const { return static_cast<bool>(pinner_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800447
Austin Schuh432784f2020-06-23 17:27:35 -0700448 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800449 const Channel *const channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450 MMapedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700451 ipc_lib::LocklessQueueReader reader_;
452 // This being nullopt indicates we're not looking for wakeups right now.
453 std::optional<ipc_lib::LocklessQueueWatcher> watcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700454
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700455 ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700456
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800457 // This being empty indicates we're not going to copy data.
458 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800459
Brian Silverman77162972020-08-12 19:52:40 -0700460 // This being nullopt indicates we're not going to pin messages.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700461 std::optional<ipc_lib::LocklessQueuePinner> pinner_;
Brian Silverman77162972020-08-12 19:52:40 -0700462
Austin Schuh39788ff2019-12-01 18:22:57 -0800463 Context context_;
464};
465
466class ShmFetcher : public RawFetcher {
467 public:
Austin Schuh432784f2020-06-23 17:27:35 -0700468 explicit ShmFetcher(ShmEventLoop *event_loop, const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800469 : RawFetcher(event_loop, channel),
Brian Silverman3bca5322020-08-12 19:35:29 -0700470 simple_shm_fetcher_(event_loop, channel) {
Brian Silverman77162972020-08-12 19:52:40 -0700471 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700472 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800473
474 ~ShmFetcher() { context_.data = nullptr; }
475
476 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
477 if (simple_shm_fetcher_.FetchNext()) {
478 context_ = simple_shm_fetcher_.context();
479 return std::make_pair(true, monotonic_clock::now());
480 }
481 return std::make_pair(false, monotonic_clock::min_time);
482 }
483
484 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
485 if (simple_shm_fetcher_.Fetch()) {
486 context_ = simple_shm_fetcher_.context();
487 return std::make_pair(true, monotonic_clock::now());
488 }
489 return std::make_pair(false, monotonic_clock::min_time);
490 }
491
Brian Silvermana5450a92020-08-12 19:59:57 -0700492 absl::Span<const char> GetPrivateMemory() const {
Brian Silverman6d2b3592020-06-18 14:40:15 -0700493 return simple_shm_fetcher_.GetPrivateMemory();
494 }
495
Austin Schuh39788ff2019-12-01 18:22:57 -0800496 private:
497 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700498};
499
500class ShmSender : public RawSender {
501 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800502 explicit ShmSender(EventLoop *event_loop, const Channel *channel)
503 : RawSender(event_loop, channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800504 lockless_queue_memory_(
505 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800506 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800507 event_loop->configuration()->channel_storage_duration()))),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700508 lockless_queue_sender_(VerifySender(
509 ipc_lib::LocklessQueueSender::Make(lockless_queue_memory_.queue()),
510 channel)),
511 wake_upper_(lockless_queue_memory_.queue()) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700512
Austin Schuh39788ff2019-12-01 18:22:57 -0800513 ~ShmSender() override {}
514
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700515 static ipc_lib::LocklessQueueSender VerifySender(
516 std::optional<ipc_lib::LocklessQueueSender> sender,
Austin Schuhe516ab02020-05-06 21:37:04 -0700517 const Channel *channel) {
518 if (sender) {
519 return std::move(sender.value());
520 }
521 LOG(FATAL) << "Failed to create sender on "
522 << configuration::CleanedChannelToString(channel)
523 << ", too many senders.";
524 }
525
Alex Perrycb7da4b2019-08-28 19:35:56 -0700526 void *data() override { return lockless_queue_sender_.Data(); }
527 size_t size() override { return lockless_queue_sender_.size(); }
Austin Schuhad154822019-12-27 15:45:13 -0800528 bool DoSend(size_t length,
529 aos::monotonic_clock::time_point monotonic_remote_time,
530 aos::realtime_clock::time_point realtime_remote_time,
531 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700532 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
533 << ": Sent too big a message on "
534 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800535 lockless_queue_sender_.Send(
536 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
537 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700538 wake_upper_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700539 return true;
540 }
541
Austin Schuhad154822019-12-27 15:45:13 -0800542 bool DoSend(const void *msg, size_t length,
543 aos::monotonic_clock::time_point monotonic_remote_time,
544 aos::realtime_clock::time_point realtime_remote_time,
545 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700546 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
547 << ": Sent too big a message on "
548 << configuration::CleanedChannelToString(channel());
Austin Schuhad154822019-12-27 15:45:13 -0800549 lockless_queue_sender_.Send(reinterpret_cast<const char *>(msg), length,
550 monotonic_remote_time, realtime_remote_time,
551 remote_queue_index, &monotonic_sent_time_,
552 &realtime_sent_time_, &sent_queue_index_);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700553 wake_upper_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700554 // TODO(austin): Return an error if we send too fast.
555 return true;
556 }
557
Brian Silverman5120afb2020-01-31 17:44:35 -0800558 absl::Span<char> GetSharedMemory() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700559 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800560 }
561
Brian Silverman4f4e0612020-08-12 19:54:41 -0700562 int buffer_index() override { return lockless_queue_sender_.buffer_index(); }
563
Alex Perrycb7da4b2019-08-28 19:35:56 -0700564 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700565 MMapedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700566 ipc_lib::LocklessQueueSender lockless_queue_sender_;
567 ipc_lib::LocklessQueueWakeUpper wake_upper_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700568};
569
Alex Perrycb7da4b2019-08-28 19:35:56 -0700570// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500571class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700572 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500573 ShmWatcherState(
Austin Schuh7d87b672019-12-01 20:23:49 -0800574 ShmEventLoop *event_loop, const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800575 std::function<void(const Context &context, const void *message)> fn,
576 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500577 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800578 event_loop_(event_loop),
579 event_(this),
Brian Silverman3bca5322020-08-12 19:35:29 -0700580 simple_shm_fetcher_(event_loop, channel) {
581 if (copy_data) {
Brian Silverman77162972020-08-12 19:52:40 -0700582 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700583 }
584 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700585
Brian Silverman148d43d2020-06-07 18:19:22 -0500586 ~ShmWatcherState() override { event_loop_->RemoveEvent(&event_); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800587
588 void Startup(EventLoop *event_loop) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800589 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800590 CHECK(RegisterWakeup(event_loop->priority()));
591 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700592
Alex Perrycb7da4b2019-08-28 19:35:56 -0700593 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800594 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700595 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800596 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800597
598 if (has_new_data_) {
599 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800600 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800601 event_loop_->AddEvent(&event_);
602 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700603 }
604
605 return has_new_data_;
606 }
607
Alex Perrycb7da4b2019-08-28 19:35:56 -0700608 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800609 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700610 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800611 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700612 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800613 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700614 }
615
Austin Schuh39788ff2019-12-01 18:22:57 -0800616 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700617 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800618 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700619 }
620
Austin Schuh39788ff2019-12-01 18:22:57 -0800621 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700622
Brian Silvermana5450a92020-08-12 19:59:57 -0700623 absl::Span<const char> GetSharedMemory() const {
624 return simple_shm_fetcher_.GetConstSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800625 }
626
Alex Perrycb7da4b2019-08-28 19:35:56 -0700627 private:
628 bool has_new_data_ = false;
629
Austin Schuh7d87b672019-12-01 20:23:49 -0800630 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500631 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800632 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700633};
634
635// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500636class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700637 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500638 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800639 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800640 shm_event_loop_(shm_event_loop),
641 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800642 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
643 // The timer may fire spurriously. HandleEvent on the event loop will
644 // call the callback if it is needed. It may also have called it when
645 // processing some other event, and the kernel decided to deliver this
646 // wakeup anyways.
647 timerfd_.Read();
648 shm_event_loop_->HandleEvent();
649 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700650 }
651
Brian Silverman148d43d2020-06-07 18:19:22 -0500652 ~ShmTimerHandler() {
Austin Schuh7d87b672019-12-01 20:23:49 -0800653 Disable();
654 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
655 }
656
657 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800658 CHECK(!event_.valid());
659 const auto monotonic_now = Call(monotonic_clock::now, base_);
660 if (event_.valid()) {
661 // If someone called Setup inside Call, rescheduling is already taken care
662 // of. Bail.
663 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800664 }
665
Austin Schuhcde39fd2020-02-22 20:58:24 -0800666 if (repeat_offset_ == chrono::seconds(0)) {
667 timerfd_.Disable();
668 } else {
669 // Compute how many cycles have elapsed and schedule the next iteration
670 // for the next iteration in the future.
671 const int elapsed_cycles =
672 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
673 std::chrono::nanoseconds(1)) /
674 repeat_offset_);
675 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800676
Austin Schuhcde39fd2020-02-22 20:58:24 -0800677 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800678 event_.set_event_time(base_);
679 shm_event_loop_->AddEvent(&event_);
Austin Schuhcde39fd2020-02-22 20:58:24 -0800680 timerfd_.SetTime(base_, chrono::seconds(0));
Austin Schuh7d87b672019-12-01 20:23:49 -0800681 }
682 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700683
684 void Setup(monotonic_clock::time_point base,
685 monotonic_clock::duration repeat_offset) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800686 if (event_.valid()) {
687 shm_event_loop_->RemoveEvent(&event_);
688 }
689
Alex Perrycb7da4b2019-08-28 19:35:56 -0700690 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800691 base_ = base;
692 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800693 event_.set_event_time(base_);
694 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700695 }
696
Austin Schuh7d87b672019-12-01 20:23:49 -0800697 void Disable() override {
698 shm_event_loop_->RemoveEvent(&event_);
699 timerfd_.Disable();
700 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700701
702 private:
703 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500704 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700705
Brian Silverman148d43d2020-06-07 18:19:22 -0500706 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700707
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800708 monotonic_clock::time_point base_;
709 monotonic_clock::duration repeat_offset_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700710};
711
712// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500713class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700714 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500715 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
716 ::std::function<void(int)> fn,
717 const monotonic_clock::duration interval,
718 const monotonic_clock::duration offset)
719 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800720 shm_event_loop_(shm_event_loop),
721 event_(this) {
722 shm_event_loop_->epoll_.OnReadable(
723 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
724 }
725
726 void HandleEvent() {
727 // The return value for read is the number of cycles that have elapsed.
728 // Because we check to see when this event *should* have happened, there are
729 // cases where Read() will return 0, when 1 cycle has actually happened.
730 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
731 // ignore it. Call handles rescheduling and calculating elapsed cycles
732 // without any extra help.
733 timerfd_.Read();
734 event_.Invalidate();
735
736 Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) {
737 Schedule(sleep_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700738 });
739 }
740
Brian Silverman148d43d2020-06-07 18:19:22 -0500741 ~ShmPhasedLoopHandler() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800742 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800743 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700744 }
745
746 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800747 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800748 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800749 if (event_.valid()) {
750 shm_event_loop_->RemoveEvent(&event_);
751 }
752
Austin Schuh39788ff2019-12-01 18:22:57 -0800753 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800754 event_.set_event_time(sleep_time);
755 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700756 }
757
758 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500759 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700760
Brian Silverman148d43d2020-06-07 18:19:22 -0500761 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700762};
Brian Silverman148d43d2020-06-07 18:19:22 -0500763
764} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700765
766::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
767 const Channel *channel) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800768 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
769 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
770 << "\", \"type\": \"" << channel->type()->string_view()
771 << "\" } is not able to be fetched on this node. Check your "
772 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800773 }
774
Brian Silverman148d43d2020-06-07 18:19:22 -0500775 return ::std::unique_ptr<RawFetcher>(new ShmFetcher(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700776}
777
778::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
779 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800780 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800781
Brian Silverman148d43d2020-06-07 18:19:22 -0500782 return ::std::unique_ptr<RawSender>(new ShmSender(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700783}
784
785void ShmEventLoop::MakeRawWatcher(
786 const Channel *channel,
787 std::function<void(const Context &context, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800788 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800789
Austin Schuh39788ff2019-12-01 18:22:57 -0800790 NewWatcher(::std::unique_ptr<WatcherState>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500791 new ShmWatcherState(this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800792}
793
794void ShmEventLoop::MakeRawNoArgWatcher(
795 const Channel *channel,
796 std::function<void(const Context &context)> watcher) {
797 TakeWatcher(channel);
798
Brian Silverman148d43d2020-06-07 18:19:22 -0500799 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800800 this, channel,
801 [watcher](const Context &context, const void *) { watcher(context); },
802 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700803}
804
805TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800806 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500807 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700808}
809
810PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
811 ::std::function<void(int)> callback,
812 const monotonic_clock::duration interval,
813 const monotonic_clock::duration offset) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500814 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
815 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700816}
817
818void ShmEventLoop::OnRun(::std::function<void()> on_run) {
819 on_run_.push_back(::std::move(on_run));
820}
821
Austin Schuh7d87b672019-12-01 20:23:49 -0800822void ShmEventLoop::HandleEvent() {
823 // Update all the times for handlers.
824 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500825 ShmWatcherState *watcher =
826 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Austin Schuh7d87b672019-12-01 20:23:49 -0800827
828 watcher->CheckForNewData();
829 }
830
Austin Schuh39788ff2019-12-01 18:22:57 -0800831 while (true) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800832 if (EventCount() == 0 ||
833 PeekEvent()->event_time() > monotonic_clock::now()) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800834 break;
835 }
836
Austin Schuh7d87b672019-12-01 20:23:49 -0800837 EventLoopEvent *event = PopEvent();
838 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800839 }
840}
841
Austin Schuh32fd5a72019-12-01 22:20:26 -0800842// RAII class to mask signals.
843class ScopedSignalMask {
844 public:
845 ScopedSignalMask(std::initializer_list<int> signals) {
846 sigset_t sigset;
847 PCHECK(sigemptyset(&sigset) == 0);
848 for (int signal : signals) {
849 PCHECK(sigaddset(&sigset, signal) == 0);
850 }
851
852 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
853 }
854
855 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
856
857 private:
858 sigset_t old_;
859};
860
861// Class to manage the static state associated with killing multiple event
862// loops.
863class SignalHandler {
864 public:
865 // Gets the singleton.
866 static SignalHandler *global() {
867 static SignalHandler loop;
868 return &loop;
869 }
870
871 // Handles the signal with the singleton.
872 static void HandleSignal(int) { global()->DoHandleSignal(); }
873
874 // Registers an event loop to receive Exit() calls.
875 void Register(ShmEventLoop *event_loop) {
876 // Block signals while we have the mutex so we never race with the signal
877 // handler.
878 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
879 std::unique_lock<stl_mutex> locker(mutex_);
880 if (event_loops_.size() == 0) {
881 // The first caller registers the signal handler.
882 struct sigaction new_action;
883 sigemptyset(&new_action.sa_mask);
884 // This makes it so that 2 control c's to a stuck process will kill it by
885 // restoring the original signal handler.
886 new_action.sa_flags = SA_RESETHAND;
887 new_action.sa_handler = &HandleSignal;
888
889 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
890 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
891 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
892 }
893
894 event_loops_.push_back(event_loop);
895 }
896
897 // Unregisters an event loop to receive Exit() calls.
898 void Unregister(ShmEventLoop *event_loop) {
899 // Block signals while we have the mutex so we never race with the signal
900 // handler.
901 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
902 std::unique_lock<stl_mutex> locker(mutex_);
903
Brian Silverman5120afb2020-01-31 17:44:35 -0800904 event_loops_.erase(
905 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800906
907 if (event_loops_.size() == 0u) {
908 // The last caller restores the original signal handlers.
909 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
910 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
911 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
912 }
913 }
914
915 private:
916 void DoHandleSignal() {
917 // We block signals while grabbing the lock, so there should never be a
918 // race. Confirm that this is true using trylock.
919 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
920 "modifing the event loop list.";
921 for (ShmEventLoop *event_loop : event_loops_) {
922 event_loop->Exit();
923 }
924 mutex_.unlock();
925 }
926
927 // Mutex to protect all state.
928 stl_mutex mutex_;
929 std::vector<ShmEventLoop *> event_loops_;
930 struct sigaction old_action_int_;
931 struct sigaction old_action_hup_;
932 struct sigaction old_action_term_;
933};
934
Alex Perrycb7da4b2019-08-28 19:35:56 -0700935void ShmEventLoop::Run() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800936 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -0800937
Alex Perrycb7da4b2019-08-28 19:35:56 -0700938 std::unique_ptr<ipc_lib::SignalFd> signalfd;
939
940 if (watchers_.size() > 0) {
941 signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
942
943 epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() {
944 signalfd_siginfo result = signalfd_ptr->Read();
945 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
946
947 // TODO(austin): We should really be checking *everything*, not just
948 // watchers, and calling the oldest thing first. That will improve
949 // determinism a lot.
950
Austin Schuh7d87b672019-12-01 20:23:49 -0800951 HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700952 });
953 }
954
Austin Schuh39788ff2019-12-01 18:22:57 -0800955 MaybeScheduleTimingReports();
956
Austin Schuh7d87b672019-12-01 20:23:49 -0800957 ReserveEvents();
958
Tyler Chatow67ddb032020-01-12 14:30:04 -0800959 {
960 AosLogToFbs aos_logger;
961 if (!skip_logger_) {
962 aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
963 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700964
Tyler Chatow67ddb032020-01-12 14:30:04 -0800965 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -0700966 const cpu_set_t default_affinity = DefaultAffinity();
967 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
968 ::aos::SetCurrentThreadAffinity(affinity_);
969 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800970 // Now, all the callbacks are setup. Lock everything into memory and go RT.
971 if (priority_ != 0) {
972 ::aos::InitRT();
973
974 LOG(INFO) << "Setting priority to " << priority_;
975 ::aos::SetCurrentThreadRealtimePriority(priority_);
976 }
977
978 set_is_running(true);
979
980 // Now that we are realtime (but before the OnRun handlers run), snap the
981 // queue index.
982 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
983 watcher->Startup(this);
984 }
985
986 // Now that we are RT, run all the OnRun handlers.
987 for (const auto &run : on_run_) {
988 run();
989 }
990
991 // And start our main event loop which runs all the timers and handles Quit.
992 epoll_.Run();
993
994 // Once epoll exits, there is no useful nonrt work left to do.
995 set_is_running(false);
996
997 // Nothing time or synchronization critical needs to happen after this
998 // point. Drop RT priority.
999 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001000 }
1001
Austin Schuh39788ff2019-12-01 18:22:57 -08001002 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001003 ShmWatcherState *watcher =
1004 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -07001005 watcher->UnregisterWakeup();
1006 }
1007
1008 if (watchers_.size() > 0) {
1009 epoll_.DeleteFd(signalfd->fd());
1010 signalfd.reset();
1011 }
Austin Schuh32fd5a72019-12-01 22:20:26 -08001012
1013 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001014
1015 // Trigger any remaining senders or fetchers to be cleared before destroying
1016 // the event loop so the book keeping matches. Do this in the thread that
1017 // created the timing reporter.
1018 timing_report_sender_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001019}
1020
1021void ShmEventLoop::Exit() { epoll_.Quit(); }
1022
1023ShmEventLoop::~ShmEventLoop() {
Austin Schuh39788ff2019-12-01 18:22:57 -08001024 // Force everything with a registered fd with epoll to be destroyed now.
1025 timers_.clear();
1026 phased_loops_.clear();
1027 watchers_.clear();
1028
Alex Perrycb7da4b2019-08-28 19:35:56 -07001029 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
1030}
1031
Alex Perrycb7da4b2019-08-28 19:35:56 -07001032void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
1033 if (is_running()) {
1034 LOG(FATAL) << "Cannot set realtime priority while running.";
1035 }
1036 priority_ = priority;
1037}
1038
Brian Silverman6a54ff32020-04-28 16:41:39 -07001039void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
1040 if (is_running()) {
1041 LOG(FATAL) << "Cannot set affinity while running.";
1042 }
1043 affinity_ = cpuset;
1044}
1045
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001046void ShmEventLoop::set_name(const std::string_view name) {
1047 name_ = std::string(name);
1048 UpdateTimingReport();
1049}
1050
Brian Silvermana5450a92020-08-12 19:59:57 -07001051absl::Span<const char> ShmEventLoop::GetWatcherSharedMemory(
1052 const Channel *channel) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001053 ShmWatcherState *const watcher_state =
1054 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -08001055 return watcher_state->GetSharedMemory();
1056}
1057
Brian Silverman4f4e0612020-08-12 19:54:41 -07001058int ShmEventLoop::NumberBuffers(const Channel *channel) {
1059 return MakeQueueConfiguration(
1060 channel, chrono::ceil<chrono::seconds>(chrono::nanoseconds(
1061 configuration()->channel_storage_duration())))
1062 .num_messages();
1063}
1064
Brian Silverman5120afb2020-01-31 17:44:35 -08001065absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
1066 const aos::RawSender *sender) const {
Brian Silverman148d43d2020-06-07 18:19:22 -05001067 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -08001068}
1069
Brian Silvermana5450a92020-08-12 19:59:57 -07001070absl::Span<const char> ShmEventLoop::GetShmFetcherPrivateMemory(
Brian Silverman6d2b3592020-06-18 14:40:15 -07001071 const aos::RawFetcher *fetcher) const {
1072 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
1073}
1074
Austin Schuh39788ff2019-12-01 18:22:57 -08001075pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
1076
Alex Perrycb7da4b2019-08-28 19:35:56 -07001077} // namespace aos