blob: 6e152cc20bb273915d6629161066a6b273ad88a7 [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
Austin Schuhef323c02020-09-01 14:55:28 -070015#include "absl/strings/str_cat.h"
Tyler Chatow67ddb032020-01-12 14:30:04 -080016#include "aos/events/aos_logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "aos/events/epoll.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080018#include "aos/events/event_loop_generated.h"
19#include "aos/events/timing_statistics.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070020#include "aos/ipc_lib/lockless_queue.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080021#include "aos/ipc_lib/signalfd.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070022#include "aos/realtime.h"
Austin Schuh32fd5a72019-12-01 22:20:26 -080023#include "aos/stl_mutex/stl_mutex.h"
Austin Schuhfccb2d02020-01-26 16:11:19 -080024#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070025#include "aos/util/phased_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080026#include "glog/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070027
Austin Schuhe84c3ed2019-12-14 15:29:48 -080028namespace {
29
30// Returns the portion of the path after the last /. This very much assumes
31// that the application name is null terminated.
32const char *Filename(const char *path) {
33 const std::string_view path_string_view = path;
34 auto last_slash_pos = path_string_view.find_last_of("/");
35
36 return last_slash_pos == std::string_view::npos ? path
37 : path + last_slash_pos + 1;
38}
39
40} // namespace
41
Alex Perrycb7da4b2019-08-28 19:35:56 -070042DEFINE_string(shm_base, "/dev/shm/aos",
43 "Directory to place queue backing mmaped files in.");
44DEFINE_uint32(permissions, 0770,
45 "Permissions to make shared memory files and folders.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080046DEFINE_string(application_name, Filename(program_invocation_name),
47 "The application name");
Alex Perrycb7da4b2019-08-28 19:35:56 -070048
49namespace aos {
50
Brian Silverman148d43d2020-06-07 18:19:22 -050051using namespace shm_event_loop_internal;
52
Austin Schuhcdab6192019-12-29 17:47:46 -080053void SetShmBase(const std::string_view base) {
Austin Schuhef323c02020-09-01 14:55:28 -070054 FLAGS_shm_base = std::string(base) + "/aos";
Austin Schuhcdab6192019-12-29 17:47:46 -080055}
56
Brian Silverman4f4e0612020-08-12 19:54:41 -070057namespace {
58
Austin Schuhef323c02020-09-01 14:55:28 -070059std::string ShmFolder(std::string_view shm_base, const Channel *channel) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070060 CHECK(channel->has_name());
61 CHECK_EQ(channel->name()->string_view()[0], '/');
Austin Schuhef323c02020-09-01 14:55:28 -070062 return absl::StrCat(shm_base, channel->name()->string_view(), "/");
Alex Perrycb7da4b2019-08-28 19:35:56 -070063}
Austin Schuhef323c02020-09-01 14:55:28 -070064std::string ShmPath(std::string_view shm_base, const Channel *channel) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070065 CHECK(channel->has_type());
Austin Schuhef323c02020-09-01 14:55:28 -070066 return ShmFolder(shm_base, channel) + channel->type()->str() + ".v3";
Alex Perrycb7da4b2019-08-28 19:35:56 -070067}
68
Brian Silvermana5450a92020-08-12 19:59:57 -070069void PageFaultDataWrite(char *data, size_t size) {
Brian Silverman3b0cdaf2020-04-28 16:51:51 -070070 // This just has to divide the actual page size. Being smaller will make this
71 // a bit slower than necessary, but not much. 1024 is a pretty conservative
72 // choice (most pages are probably 4096).
73 static constexpr size_t kPageSize = 1024;
74 const size_t pages = (size + kPageSize - 1) / kPageSize;
75 for (size_t i = 0; i < pages; ++i) {
76 char zero = 0;
77 // We need to ensure there's a writable pagetable entry, but avoid modifying
78 // the data.
79 //
80 // Even if you lock the data into memory, some kernels still seem to lazily
81 // create the actual pagetable entries. This means we need to somehow
82 // "write" to the page.
83 //
84 // Also, this takes place while other processes may be concurrently
85 // opening/initializing the memory, so we need to avoid corrupting that.
86 //
87 // This is the simplest operation I could think of which achieves that:
88 // "store 0 if it's already 0".
89 __atomic_compare_exchange_n(&data[i * kPageSize], &zero, 0, true,
90 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
91 }
92}
93
Brian Silvermana5450a92020-08-12 19:59:57 -070094void PageFaultDataRead(const char *data, size_t size) {
95 // This just has to divide the actual page size. Being smaller will make this
96 // a bit slower than necessary, but not much. 1024 is a pretty conservative
97 // choice (most pages are probably 4096).
98 static constexpr size_t kPageSize = 1024;
99 const size_t pages = (size + kPageSize - 1) / kPageSize;
100 for (size_t i = 0; i < pages; ++i) {
101 // We need to ensure there's a readable pagetable entry.
102 __atomic_load_n(&data[i * kPageSize], __ATOMIC_RELAXED);
103 }
104}
105
Brian Silverman4f4e0612020-08-12 19:54:41 -0700106ipc_lib::LocklessQueueConfiguration MakeQueueConfiguration(
107 const Channel *channel, std::chrono::seconds channel_storage_duration) {
108 ipc_lib::LocklessQueueConfiguration config;
109
110 config.num_watchers = channel->num_watchers();
111 config.num_senders = channel->num_senders();
112 // The value in the channel will default to 0 if readers are configured to
113 // copy.
114 config.num_pinners = channel->num_readers();
115 config.queue_size = channel_storage_duration.count() * channel->frequency();
116 config.message_data_size = channel->max_size();
117
118 return config;
119}
120
Austin Schuh2f8fd752020-09-01 22:38:28 -0700121class MMappedQueue {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700122 public:
Austin Schuh2f8fd752020-09-01 22:38:28 -0700123 MMappedQueue(std::string_view shm_base, const Channel *channel,
124 std::chrono::seconds channel_storage_duration)
Brian Silverman4f4e0612020-08-12 19:54:41 -0700125 : config_(MakeQueueConfiguration(channel, channel_storage_duration)) {
Austin Schuhef323c02020-09-01 14:55:28 -0700126 std::string path = ShmPath(shm_base, channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700127
Alex Perrycb7da4b2019-08-28 19:35:56 -0700128 size_ = ipc_lib::LocklessQueueMemorySize(config_);
129
Austin Schuhfccb2d02020-01-26 16:11:19 -0800130 util::MkdirP(path, FLAGS_permissions);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131
132 // There are 2 cases. Either the file already exists, or it does not
133 // already exist and we need to create it. Start by trying to create it. If
134 // that fails, the file has already been created and we can open it
Brian Silverman4f4e0612020-08-12 19:54:41 -0700135 // normally.. Once the file has been created it will never be deleted.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800136 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_EXCL,
Brian Silverman148d43d2020-06-07 18:19:22 -0500137 O_CLOEXEC | FLAGS_permissions);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800138 if (fd == -1 && errno == EEXIST) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 VLOG(1) << path << " already created.";
140 // File already exists.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800141 fd = open(path.c_str(), O_RDWR, O_CLOEXEC);
142 PCHECK(fd != -1) << ": Failed to open " << path;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 while (true) {
144 struct stat st;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800145 PCHECK(fstat(fd, &st) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700146 if (st.st_size != 0) {
147 CHECK_EQ(static_cast<size_t>(st.st_size), size_)
148 << ": Size of " << path
149 << " doesn't match expected size of backing queue file. Did the "
150 "queue definition change?";
151 break;
152 } else {
153 // The creating process didn't get around to it yet. Give it a bit.
154 std::this_thread::sleep_for(std::chrono::milliseconds(10));
155 VLOG(1) << path << " is zero size, waiting";
156 }
157 }
158 } else {
159 VLOG(1) << "Created " << path;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800160 PCHECK(fd != -1) << ": Failed to open " << path;
161 PCHECK(ftruncate(fd, size_) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 }
163
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800164 data_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165 PCHECK(data_ != MAP_FAILED);
Brian Silvermana5450a92020-08-12 19:59:57 -0700166 const_data_ = mmap(NULL, size_, PROT_READ, MAP_SHARED, fd, 0);
167 PCHECK(const_data_ != MAP_FAILED);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800168 PCHECK(close(fd) == 0);
Brian Silvermana5450a92020-08-12 19:59:57 -0700169 PageFaultDataWrite(static_cast<char *>(data_), size_);
170 PageFaultDataRead(static_cast<const char *>(const_data_), size_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700171
172 ipc_lib::InitializeLocklessQueueMemory(memory(), config_);
173 }
174
Austin Schuh2f8fd752020-09-01 22:38:28 -0700175 ~MMappedQueue() {
Brian Silvermana5450a92020-08-12 19:59:57 -0700176 PCHECK(munmap(data_, size_) == 0);
177 PCHECK(munmap(const_cast<void *>(const_data_), size_) == 0);
178 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700179
180 ipc_lib::LocklessQueueMemory *memory() const {
181 return reinterpret_cast<ipc_lib::LocklessQueueMemory *>(data_);
182 }
183
Brian Silvermana5450a92020-08-12 19:59:57 -0700184 const ipc_lib::LocklessQueueMemory *const_memory() const {
185 return reinterpret_cast<const ipc_lib::LocklessQueueMemory *>(const_data_);
186 }
187
Austin Schuh39788ff2019-12-01 18:22:57 -0800188 const ipc_lib::LocklessQueueConfiguration &config() const { return config_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700190 ipc_lib::LocklessQueue queue() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700191 return ipc_lib::LocklessQueue(const_memory(), memory(), config());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700192 }
193
Brian Silvermana5450a92020-08-12 19:59:57 -0700194 absl::Span<char> GetMutableSharedMemory() const {
Brian Silverman5120afb2020-01-31 17:44:35 -0800195 return absl::Span<char>(static_cast<char *>(data_), size_);
196 }
197
Brian Silvermana5450a92020-08-12 19:59:57 -0700198 absl::Span<const char> GetConstSharedMemory() const {
199 return absl::Span<const char>(static_cast<const char *>(const_data_),
200 size_);
201 }
202
Alex Perrycb7da4b2019-08-28 19:35:56 -0700203 private:
Brian Silverman4f4e0612020-08-12 19:54:41 -0700204 const ipc_lib::LocklessQueueConfiguration config_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700205
Alex Perrycb7da4b2019-08-28 19:35:56 -0700206 size_t size_;
207 void *data_;
Brian Silvermana5450a92020-08-12 19:59:57 -0700208 const void *const_data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209};
210
Austin Schuh217a9782019-12-21 23:02:50 -0800211const Node *MaybeMyNode(const Configuration *configuration) {
212 if (!configuration->has_nodes()) {
213 return nullptr;
214 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700215
Austin Schuh217a9782019-12-21 23:02:50 -0800216 return configuration::GetMyNode(configuration);
217}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700218
219namespace chrono = ::std::chrono;
220
Austin Schuh39788ff2019-12-01 18:22:57 -0800221} // namespace
222
Austin Schuh217a9782019-12-21 23:02:50 -0800223ShmEventLoop::ShmEventLoop(const Configuration *configuration)
224 : EventLoop(configuration),
Austin Schuhef323c02020-09-01 14:55:28 -0700225 shm_base_(FLAGS_shm_base),
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800226 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -0800227 node_(MaybeMyNode(configuration)) {
228 if (configuration->has_nodes()) {
229 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
230 }
231}
Austin Schuh217a9782019-12-21 23:02:50 -0800232
Brian Silverman148d43d2020-06-07 18:19:22 -0500233namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -0800234
235class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700236 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700237 explicit SimpleShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
238 const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700239 : event_loop_(event_loop),
240 channel_(channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800241 lockless_queue_memory_(
Austin Schuhef323c02020-09-01 14:55:28 -0700242 shm_base, channel,
Brian Silverman587da252020-01-01 17:00:47 -0800243 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800244 event_loop->configuration()->channel_storage_duration()))),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700245 reader_(lockless_queue_memory_.queue()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700246 context_.data = nullptr;
247 // Point the queue index at the next index to read starting now. This
248 // makes it such that FetchNext will read the next message sent after
249 // the fetcher is created.
250 PointAtNextQueueIndex();
251 }
252
Austin Schuh39788ff2019-12-01 18:22:57 -0800253 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700254
Brian Silverman77162972020-08-12 19:52:40 -0700255 // Sets this object to pin or copy data, as configured in the channel.
256 void RetrieveData() {
257 if (channel_->read_method() == ReadMethod::PIN) {
258 PinDataOnFetch();
259 } else {
260 CopyDataOnFetch();
261 }
262 }
263
Brian Silverman3bca5322020-08-12 19:35:29 -0700264 // Sets this object to copy data out of the shared memory into a private
265 // buffer when fetching.
266 void CopyDataOnFetch() {
Brian Silverman77162972020-08-12 19:52:40 -0700267 CHECK(!pin_data());
Brian Silverman3bca5322020-08-12 19:35:29 -0700268 data_storage_.reset(static_cast<char *>(
269 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
270 }
271
Brian Silverman77162972020-08-12 19:52:40 -0700272 // Sets this object to pin data in shared memory when fetching.
273 void PinDataOnFetch() {
274 CHECK(!copy_data());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700275 auto maybe_pinner =
276 ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue());
Brian Silverman77162972020-08-12 19:52:40 -0700277 if (!maybe_pinner) {
278 LOG(FATAL) << "Failed to create reader on "
279 << configuration::CleanedChannelToString(channel_)
280 << ", too many readers.";
281 }
282 pinner_ = std::move(maybe_pinner.value());
283 }
284
Alex Perrycb7da4b2019-08-28 19:35:56 -0700285 // Points the next message to fetch at the queue index which will be
286 // populated next.
287 void PointAtNextQueueIndex() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700288 actual_queue_index_ = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700289 if (!actual_queue_index_.valid()) {
290 // Nothing in the queue. The next element will show up at the 0th
291 // index in the queue.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700292 actual_queue_index_ = ipc_lib::QueueIndex::Zero(
293 LocklessQueueSize(lockless_queue_memory_.memory()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700294 } else {
295 actual_queue_index_ = actual_queue_index_.Increment();
296 }
297 }
298
Austin Schuh39788ff2019-12-01 18:22:57 -0800299 bool FetchNext() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700300 const ipc_lib::LocklessQueueReader::Result read_result =
Brian Silverman3bca5322020-08-12 19:35:29 -0700301 DoFetch(actual_queue_index_);
Austin Schuh432784f2020-06-23 17:27:35 -0700302
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700303 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304 }
305
Austin Schuh39788ff2019-12-01 18:22:57 -0800306 bool Fetch() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700307 const ipc_lib::QueueIndex queue_index = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700308 // actual_queue_index_ is only meaningful if it was set by Fetch or
309 // FetchNext. This happens when valid_data_ has been set. So, only
310 // skip checking if valid_data_ is true.
311 //
312 // Also, if the latest queue index is invalid, we are empty. So there
313 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800314 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700315 queue_index == actual_queue_index_.DecrementBy(1u)) ||
316 !queue_index.valid()) {
317 return false;
318 }
319
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700320 const ipc_lib::LocklessQueueReader::Result read_result =
321 DoFetch(queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700322
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700323 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800324 << ": Queue index went backwards. This should never happen. "
325 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700326
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700327 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700328 }
329
Austin Schuh39788ff2019-12-01 18:22:57 -0800330 Context context() const { return context_; }
331
Alex Perrycb7da4b2019-08-28 19:35:56 -0700332 bool RegisterWakeup(int priority) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700333 CHECK(!watcher_);
334 watcher_ = ipc_lib::LocklessQueueWatcher::Make(
335 lockless_queue_memory_.queue(), priority);
336 return static_cast<bool>(watcher_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 }
338
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700339 void UnregisterWakeup() {
340 CHECK(watcher_);
341 watcher_ = std::nullopt;
342 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700343
Brian Silvermana5450a92020-08-12 19:59:57 -0700344 absl::Span<char> GetMutableSharedMemory() {
345 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800346 }
347
Brian Silvermana5450a92020-08-12 19:59:57 -0700348 absl::Span<const char> GetConstSharedMemory() const {
349 return lockless_queue_memory_.GetConstSharedMemory();
350 }
351
352 absl::Span<const char> GetPrivateMemory() const {
353 if (pin_data()) {
354 return lockless_queue_memory_.GetConstSharedMemory();
355 }
Brian Silverman6d2b3592020-06-18 14:40:15 -0700356 return absl::Span<char>(
357 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700358 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()));
Brian Silverman6d2b3592020-06-18 14:40:15 -0700359 }
360
Alex Perrycb7da4b2019-08-28 19:35:56 -0700361 private:
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700362 ipc_lib::LocklessQueueReader::Result DoFetch(
363 ipc_lib::QueueIndex queue_index) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700364 // TODO(austin): Get behind and make sure it dies.
365 char *copy_buffer = nullptr;
366 if (copy_data()) {
367 copy_buffer = data_storage_start();
368 }
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700369 ipc_lib::LocklessQueueReader::Result read_result = reader_.Read(
Brian Silverman3bca5322020-08-12 19:35:29 -0700370 queue_index.index(), &context_.monotonic_event_time,
371 &context_.realtime_event_time, &context_.monotonic_remote_time,
372 &context_.realtime_remote_time, &context_.remote_queue_index,
373 &context_.size, copy_buffer);
374
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700375 if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) {
Brian Silverman77162972020-08-12 19:52:40 -0700376 if (pin_data()) {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700377 const int pin_result = pinner_->PinIndex(queue_index.index());
378 CHECK(pin_result >= 0)
Brian Silverman77162972020-08-12 19:52:40 -0700379 << ": Got behind while reading and the last message was modified "
380 "out from under us while we tried to pin it. Don't get so far "
381 "behind on: "
382 << configuration::CleanedChannelToString(channel_);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700383 context_.buffer_index = pin_result;
384 } else {
385 context_.buffer_index = -1;
Brian Silverman77162972020-08-12 19:52:40 -0700386 }
387
Brian Silverman3bca5322020-08-12 19:35:29 -0700388 context_.queue_index = queue_index.index();
389 if (context_.remote_queue_index == 0xffffffffu) {
390 context_.remote_queue_index = context_.queue_index;
391 }
392 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
393 context_.monotonic_remote_time = context_.monotonic_event_time;
394 }
395 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
396 context_.realtime_remote_time = context_.realtime_event_time;
397 }
398 const char *const data = DataBuffer();
399 if (data) {
400 context_.data =
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700401 data +
402 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) -
403 context_.size;
Brian Silverman3bca5322020-08-12 19:35:29 -0700404 } else {
405 context_.data = nullptr;
406 }
407 actual_queue_index_ = queue_index.Increment();
408 }
409
410 // Make sure the data wasn't modified while we were reading it. This
411 // can only happen if you are reading the last message *while* it is
412 // being written to, which means you are pretty far behind.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700413 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE)
Brian Silverman3bca5322020-08-12 19:35:29 -0700414 << ": Got behind while reading and the last message was modified "
415 "out from under us while we were reading it. Don't get so far "
416 "behind on: "
417 << configuration::CleanedChannelToString(channel_);
418
419 // We fell behind between when we read the index and read the value.
420 // This isn't worth recovering from since this means we went to sleep
421 // for a long time in the middle of this function.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700422 if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700423 event_loop_->SendTimingReport();
424 LOG(FATAL) << "The next message is no longer available. "
425 << configuration::CleanedChannelToString(channel_);
426 }
427
428 return read_result;
429 }
430
431 char *data_storage_start() const {
432 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800433 return RoundChannelData(data_storage_.get(), channel_->max_size());
434 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700435
436 // Note that for some modes the return value will change as new messages are
437 // read.
438 const char *DataBuffer() const {
439 if (copy_data()) {
440 return data_storage_start();
441 }
Brian Silverman77162972020-08-12 19:52:40 -0700442 if (pin_data()) {
443 return static_cast<const char *>(pinner_->Data());
444 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700445 return nullptr;
446 }
447
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800448 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silverman77162972020-08-12 19:52:40 -0700449 bool pin_data() const { return static_cast<bool>(pinner_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800450
Austin Schuh432784f2020-06-23 17:27:35 -0700451 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800452 const Channel *const channel_;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700453 MMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700454 ipc_lib::LocklessQueueReader reader_;
455 // This being nullopt indicates we're not looking for wakeups right now.
456 std::optional<ipc_lib::LocklessQueueWatcher> watcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700457
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700458 ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700459
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800460 // This being empty indicates we're not going to copy data.
461 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800462
Brian Silverman77162972020-08-12 19:52:40 -0700463 // This being nullopt indicates we're not going to pin messages.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700464 std::optional<ipc_lib::LocklessQueuePinner> pinner_;
Brian Silverman77162972020-08-12 19:52:40 -0700465
Austin Schuh39788ff2019-12-01 18:22:57 -0800466 Context context_;
467};
468
469class ShmFetcher : public RawFetcher {
470 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700471 explicit ShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
472 const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800473 : RawFetcher(event_loop, channel),
Austin Schuhef323c02020-09-01 14:55:28 -0700474 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman77162972020-08-12 19:52:40 -0700475 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700476 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800477
478 ~ShmFetcher() { context_.data = nullptr; }
479
480 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
481 if (simple_shm_fetcher_.FetchNext()) {
482 context_ = simple_shm_fetcher_.context();
483 return std::make_pair(true, monotonic_clock::now());
484 }
485 return std::make_pair(false, monotonic_clock::min_time);
486 }
487
488 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
489 if (simple_shm_fetcher_.Fetch()) {
490 context_ = simple_shm_fetcher_.context();
491 return std::make_pair(true, monotonic_clock::now());
492 }
493 return std::make_pair(false, monotonic_clock::min_time);
494 }
495
Brian Silvermana5450a92020-08-12 19:59:57 -0700496 absl::Span<const char> GetPrivateMemory() const {
Brian Silverman6d2b3592020-06-18 14:40:15 -0700497 return simple_shm_fetcher_.GetPrivateMemory();
498 }
499
Austin Schuh39788ff2019-12-01 18:22:57 -0800500 private:
501 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700502};
503
504class ShmSender : public RawSender {
505 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700506 explicit ShmSender(std::string_view shm_base, EventLoop *event_loop,
507 const Channel *channel)
Austin Schuh39788ff2019-12-01 18:22:57 -0800508 : RawSender(event_loop, channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800509 lockless_queue_memory_(
Austin Schuhef323c02020-09-01 14:55:28 -0700510 shm_base, channel,
Brian Silverman587da252020-01-01 17:00:47 -0800511 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800512 event_loop->configuration()->channel_storage_duration()))),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700513 lockless_queue_sender_(VerifySender(
514 ipc_lib::LocklessQueueSender::Make(lockless_queue_memory_.queue()),
515 channel)),
516 wake_upper_(lockless_queue_memory_.queue()) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700517
Austin Schuh39788ff2019-12-01 18:22:57 -0800518 ~ShmSender() override {}
519
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700520 static ipc_lib::LocklessQueueSender VerifySender(
521 std::optional<ipc_lib::LocklessQueueSender> sender,
Austin Schuhe516ab02020-05-06 21:37:04 -0700522 const Channel *channel) {
523 if (sender) {
524 return std::move(sender.value());
525 }
526 LOG(FATAL) << "Failed to create sender on "
527 << configuration::CleanedChannelToString(channel)
528 << ", too many senders.";
529 }
530
Alex Perrycb7da4b2019-08-28 19:35:56 -0700531 void *data() override { return lockless_queue_sender_.Data(); }
532 size_t size() override { return lockless_queue_sender_.size(); }
Austin Schuhad154822019-12-27 15:45:13 -0800533 bool DoSend(size_t length,
534 aos::monotonic_clock::time_point monotonic_remote_time,
535 aos::realtime_clock::time_point realtime_remote_time,
536 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700537 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
538 << ": Sent too big a message on "
539 << configuration::CleanedChannelToString(channel());
Austin Schuh91ba6392020-10-03 13:27:47 -0700540 CHECK(lockless_queue_sender_.Send(
Austin Schuhad154822019-12-27 15:45:13 -0800541 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
Austin Schuh91ba6392020-10-03 13:27:47 -0700542 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_))
543 << ": Somebody wrote outside the buffer of their message on channel "
544 << configuration::CleanedChannelToString(channel());
545
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700546 wake_upper_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700547 return true;
548 }
549
Austin Schuhad154822019-12-27 15:45:13 -0800550 bool DoSend(const void *msg, size_t length,
551 aos::monotonic_clock::time_point monotonic_remote_time,
552 aos::realtime_clock::time_point realtime_remote_time,
553 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700554 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
555 << ": Sent too big a message on "
556 << configuration::CleanedChannelToString(channel());
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700557 CHECK(lockless_queue_sender_.Send(
558 reinterpret_cast<const char *>(msg), length, monotonic_remote_time,
559 realtime_remote_time, remote_queue_index, &monotonic_sent_time_,
560 &realtime_sent_time_, &sent_queue_index_))
Austin Schuh91ba6392020-10-03 13:27:47 -0700561 << ": Somebody wrote outside the buffer of their message on channel "
562 << configuration::CleanedChannelToString(channel());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700563 wake_upper_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700564 // TODO(austin): Return an error if we send too fast.
565 return true;
566 }
567
Brian Silverman5120afb2020-01-31 17:44:35 -0800568 absl::Span<char> GetSharedMemory() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700569 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800570 }
571
Brian Silverman4f4e0612020-08-12 19:54:41 -0700572 int buffer_index() override { return lockless_queue_sender_.buffer_index(); }
573
Alex Perrycb7da4b2019-08-28 19:35:56 -0700574 private:
Austin Schuh2f8fd752020-09-01 22:38:28 -0700575 MMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700576 ipc_lib::LocklessQueueSender lockless_queue_sender_;
577 ipc_lib::LocklessQueueWakeUpper wake_upper_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700578};
579
Alex Perrycb7da4b2019-08-28 19:35:56 -0700580// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500581class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700582 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500583 ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700584 std::string_view shm_base, ShmEventLoop *event_loop,
585 const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800586 std::function<void(const Context &context, const void *message)> fn,
587 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500588 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800589 event_loop_(event_loop),
590 event_(this),
Austin Schuhef323c02020-09-01 14:55:28 -0700591 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700592 if (copy_data) {
Brian Silverman77162972020-08-12 19:52:40 -0700593 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700594 }
595 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700596
Brian Silverman148d43d2020-06-07 18:19:22 -0500597 ~ShmWatcherState() override { event_loop_->RemoveEvent(&event_); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800598
599 void Startup(EventLoop *event_loop) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800600 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800601 CHECK(RegisterWakeup(event_loop->priority()));
602 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700603
Alex Perrycb7da4b2019-08-28 19:35:56 -0700604 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800605 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700606 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800607 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800608
609 if (has_new_data_) {
610 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800611 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800612 event_loop_->AddEvent(&event_);
613 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700614 }
615
616 return has_new_data_;
617 }
618
Alex Perrycb7da4b2019-08-28 19:35:56 -0700619 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800620 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700621 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800622 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700623 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800624 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700625 }
626
Austin Schuh39788ff2019-12-01 18:22:57 -0800627 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700628 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800629 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700630 }
631
Austin Schuh39788ff2019-12-01 18:22:57 -0800632 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700633
Brian Silvermana5450a92020-08-12 19:59:57 -0700634 absl::Span<const char> GetSharedMemory() const {
635 return simple_shm_fetcher_.GetConstSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800636 }
637
Alex Perrycb7da4b2019-08-28 19:35:56 -0700638 private:
639 bool has_new_data_ = false;
640
Austin Schuh7d87b672019-12-01 20:23:49 -0800641 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500642 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800643 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700644};
645
646// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500647class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700648 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500649 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800650 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800651 shm_event_loop_(shm_event_loop),
652 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800653 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
654 // The timer may fire spurriously. HandleEvent on the event loop will
655 // call the callback if it is needed. It may also have called it when
656 // processing some other event, and the kernel decided to deliver this
657 // wakeup anyways.
658 timerfd_.Read();
659 shm_event_loop_->HandleEvent();
660 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700661 }
662
Brian Silverman148d43d2020-06-07 18:19:22 -0500663 ~ShmTimerHandler() {
Austin Schuh7d87b672019-12-01 20:23:49 -0800664 Disable();
665 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
666 }
667
668 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800669 CHECK(!event_.valid());
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700670 disabled_ = false;
Austin Schuhcde39fd2020-02-22 20:58:24 -0800671 const auto monotonic_now = Call(monotonic_clock::now, base_);
672 if (event_.valid()) {
673 // If someone called Setup inside Call, rescheduling is already taken care
674 // of. Bail.
675 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800676 }
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700677 if (disabled_) {
678 // Somebody called Disable inside Call, so we don't want to reschedule.
679 // Bail.
680 return;
681 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800682
Austin Schuhcde39fd2020-02-22 20:58:24 -0800683 if (repeat_offset_ == chrono::seconds(0)) {
684 timerfd_.Disable();
685 } else {
686 // Compute how many cycles have elapsed and schedule the next iteration
687 // for the next iteration in the future.
688 const int elapsed_cycles =
689 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
690 std::chrono::nanoseconds(1)) /
691 repeat_offset_);
692 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800693
Austin Schuhcde39fd2020-02-22 20:58:24 -0800694 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800695 event_.set_event_time(base_);
696 shm_event_loop_->AddEvent(&event_);
Austin Schuhcde39fd2020-02-22 20:58:24 -0800697 timerfd_.SetTime(base_, chrono::seconds(0));
Austin Schuh7d87b672019-12-01 20:23:49 -0800698 }
699 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700700
701 void Setup(monotonic_clock::time_point base,
702 monotonic_clock::duration repeat_offset) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800703 if (event_.valid()) {
704 shm_event_loop_->RemoveEvent(&event_);
705 }
706
Alex Perrycb7da4b2019-08-28 19:35:56 -0700707 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800708 base_ = base;
709 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800710 event_.set_event_time(base_);
711 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700712 }
713
Austin Schuh7d87b672019-12-01 20:23:49 -0800714 void Disable() override {
715 shm_event_loop_->RemoveEvent(&event_);
716 timerfd_.Disable();
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700717 disabled_ = true;
Austin Schuh7d87b672019-12-01 20:23:49 -0800718 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700719
720 private:
721 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500722 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700723
Brian Silverman148d43d2020-06-07 18:19:22 -0500724 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700725
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800726 monotonic_clock::time_point base_;
727 monotonic_clock::duration repeat_offset_;
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700728
729 // Used to track if Disable() was called during the callback, so we know not
730 // to reschedule.
731 bool disabled_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700732};
733
734// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500735class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700736 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500737 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
738 ::std::function<void(int)> fn,
739 const monotonic_clock::duration interval,
740 const monotonic_clock::duration offset)
741 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800742 shm_event_loop_(shm_event_loop),
743 event_(this) {
744 shm_event_loop_->epoll_.OnReadable(
745 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
746 }
747
748 void HandleEvent() {
749 // The return value for read is the number of cycles that have elapsed.
750 // Because we check to see when this event *should* have happened, there are
751 // cases where Read() will return 0, when 1 cycle has actually happened.
752 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
753 // ignore it. Call handles rescheduling and calculating elapsed cycles
754 // without any extra help.
755 timerfd_.Read();
756 event_.Invalidate();
757
758 Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) {
759 Schedule(sleep_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700760 });
761 }
762
Brian Silverman148d43d2020-06-07 18:19:22 -0500763 ~ShmPhasedLoopHandler() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800764 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800765 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700766 }
767
768 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800769 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800770 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800771 if (event_.valid()) {
772 shm_event_loop_->RemoveEvent(&event_);
773 }
774
Austin Schuh39788ff2019-12-01 18:22:57 -0800775 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800776 event_.set_event_time(sleep_time);
777 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700778 }
779
780 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500781 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700782
Brian Silverman148d43d2020-06-07 18:19:22 -0500783 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700784};
Brian Silverman148d43d2020-06-07 18:19:22 -0500785
786} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700787
788::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
789 const Channel *channel) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800790 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
791 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
792 << "\", \"type\": \"" << channel->type()->string_view()
793 << "\" } is not able to be fetched on this node. Check your "
794 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800795 }
796
Austin Schuhef323c02020-09-01 14:55:28 -0700797 return ::std::unique_ptr<RawFetcher>(
798 new ShmFetcher(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700799}
800
801::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
802 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800803 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800804
Austin Schuhef323c02020-09-01 14:55:28 -0700805 return ::std::unique_ptr<RawSender>(new ShmSender(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700806}
807
808void ShmEventLoop::MakeRawWatcher(
809 const Channel *channel,
810 std::function<void(const Context &context, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800811 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800812
Austin Schuh39788ff2019-12-01 18:22:57 -0800813 NewWatcher(::std::unique_ptr<WatcherState>(
Austin Schuhef323c02020-09-01 14:55:28 -0700814 new ShmWatcherState(shm_base_, this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800815}
816
817void ShmEventLoop::MakeRawNoArgWatcher(
818 const Channel *channel,
819 std::function<void(const Context &context)> watcher) {
820 TakeWatcher(channel);
821
Brian Silverman148d43d2020-06-07 18:19:22 -0500822 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700823 shm_base_, this, channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800824 [watcher](const Context &context, const void *) { watcher(context); },
825 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700826}
827
828TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800829 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500830 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700831}
832
833PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
834 ::std::function<void(int)> callback,
835 const monotonic_clock::duration interval,
836 const monotonic_clock::duration offset) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500837 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
838 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700839}
840
841void ShmEventLoop::OnRun(::std::function<void()> on_run) {
842 on_run_.push_back(::std::move(on_run));
843}
844
Austin Schuh7d87b672019-12-01 20:23:49 -0800845void ShmEventLoop::HandleEvent() {
846 // Update all the times for handlers.
847 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500848 ShmWatcherState *watcher =
849 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Austin Schuh7d87b672019-12-01 20:23:49 -0800850
851 watcher->CheckForNewData();
852 }
853
Austin Schuh39788ff2019-12-01 18:22:57 -0800854 while (true) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800855 if (EventCount() == 0 ||
856 PeekEvent()->event_time() > monotonic_clock::now()) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800857 break;
858 }
859
Austin Schuh7d87b672019-12-01 20:23:49 -0800860 EventLoopEvent *event = PopEvent();
861 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800862 }
863}
864
Austin Schuh32fd5a72019-12-01 22:20:26 -0800865// RAII class to mask signals.
866class ScopedSignalMask {
867 public:
868 ScopedSignalMask(std::initializer_list<int> signals) {
869 sigset_t sigset;
870 PCHECK(sigemptyset(&sigset) == 0);
871 for (int signal : signals) {
872 PCHECK(sigaddset(&sigset, signal) == 0);
873 }
874
875 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
876 }
877
878 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
879
880 private:
881 sigset_t old_;
882};
883
884// Class to manage the static state associated with killing multiple event
885// loops.
886class SignalHandler {
887 public:
888 // Gets the singleton.
889 static SignalHandler *global() {
890 static SignalHandler loop;
891 return &loop;
892 }
893
894 // Handles the signal with the singleton.
895 static void HandleSignal(int) { global()->DoHandleSignal(); }
896
897 // Registers an event loop to receive Exit() calls.
898 void Register(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 if (event_loops_.size() == 0) {
904 // The first caller registers the signal handler.
905 struct sigaction new_action;
906 sigemptyset(&new_action.sa_mask);
907 // This makes it so that 2 control c's to a stuck process will kill it by
908 // restoring the original signal handler.
909 new_action.sa_flags = SA_RESETHAND;
910 new_action.sa_handler = &HandleSignal;
911
912 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
913 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
914 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
915 }
916
917 event_loops_.push_back(event_loop);
918 }
919
920 // Unregisters an event loop to receive Exit() calls.
921 void Unregister(ShmEventLoop *event_loop) {
922 // Block signals while we have the mutex so we never race with the signal
923 // handler.
924 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
925 std::unique_lock<stl_mutex> locker(mutex_);
926
Brian Silverman5120afb2020-01-31 17:44:35 -0800927 event_loops_.erase(
928 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800929
930 if (event_loops_.size() == 0u) {
931 // The last caller restores the original signal handlers.
932 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
933 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
934 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
935 }
936 }
937
938 private:
939 void DoHandleSignal() {
940 // We block signals while grabbing the lock, so there should never be a
941 // race. Confirm that this is true using trylock.
942 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
943 "modifing the event loop list.";
944 for (ShmEventLoop *event_loop : event_loops_) {
945 event_loop->Exit();
946 }
947 mutex_.unlock();
948 }
949
950 // Mutex to protect all state.
951 stl_mutex mutex_;
952 std::vector<ShmEventLoop *> event_loops_;
953 struct sigaction old_action_int_;
954 struct sigaction old_action_hup_;
955 struct sigaction old_action_term_;
956};
957
Alex Perrycb7da4b2019-08-28 19:35:56 -0700958void ShmEventLoop::Run() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800959 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -0800960
Alex Perrycb7da4b2019-08-28 19:35:56 -0700961 std::unique_ptr<ipc_lib::SignalFd> signalfd;
962
963 if (watchers_.size() > 0) {
964 signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
965
966 epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() {
967 signalfd_siginfo result = signalfd_ptr->Read();
968 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
969
970 // TODO(austin): We should really be checking *everything*, not just
971 // watchers, and calling the oldest thing first. That will improve
972 // determinism a lot.
973
Austin Schuh7d87b672019-12-01 20:23:49 -0800974 HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700975 });
976 }
977
Austin Schuh39788ff2019-12-01 18:22:57 -0800978 MaybeScheduleTimingReports();
979
Austin Schuh7d87b672019-12-01 20:23:49 -0800980 ReserveEvents();
981
Tyler Chatow67ddb032020-01-12 14:30:04 -0800982 {
Austin Schuha0c41ba2020-09-10 22:59:14 -0700983 logging::ScopedLogRestorer prev_logger;
Tyler Chatow67ddb032020-01-12 14:30:04 -0800984 AosLogToFbs aos_logger;
985 if (!skip_logger_) {
986 aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
Austin Schuha0c41ba2020-09-10 22:59:14 -0700987 prev_logger.Swap(aos_logger.implementation());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800988 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700989
Tyler Chatow67ddb032020-01-12 14:30:04 -0800990 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -0700991 const cpu_set_t default_affinity = DefaultAffinity();
992 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
993 ::aos::SetCurrentThreadAffinity(affinity_);
994 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800995 // Now, all the callbacks are setup. Lock everything into memory and go RT.
996 if (priority_ != 0) {
997 ::aos::InitRT();
998
999 LOG(INFO) << "Setting priority to " << priority_;
1000 ::aos::SetCurrentThreadRealtimePriority(priority_);
1001 }
1002
1003 set_is_running(true);
1004
1005 // Now that we are realtime (but before the OnRun handlers run), snap the
1006 // queue index.
1007 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
1008 watcher->Startup(this);
1009 }
1010
1011 // Now that we are RT, run all the OnRun handlers.
1012 for (const auto &run : on_run_) {
1013 run();
1014 }
1015
1016 // And start our main event loop which runs all the timers and handles Quit.
1017 epoll_.Run();
1018
1019 // Once epoll exits, there is no useful nonrt work left to do.
1020 set_is_running(false);
1021
1022 // Nothing time or synchronization critical needs to happen after this
1023 // point. Drop RT priority.
1024 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001025 }
1026
Austin Schuh39788ff2019-12-01 18:22:57 -08001027 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001028 ShmWatcherState *watcher =
1029 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -07001030 watcher->UnregisterWakeup();
1031 }
1032
1033 if (watchers_.size() > 0) {
1034 epoll_.DeleteFd(signalfd->fd());
1035 signalfd.reset();
1036 }
Austin Schuh32fd5a72019-12-01 22:20:26 -08001037
1038 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001039
1040 // Trigger any remaining senders or fetchers to be cleared before destroying
1041 // the event loop so the book keeping matches. Do this in the thread that
1042 // created the timing reporter.
1043 timing_report_sender_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001044}
1045
1046void ShmEventLoop::Exit() { epoll_.Quit(); }
1047
1048ShmEventLoop::~ShmEventLoop() {
Austin Schuh39788ff2019-12-01 18:22:57 -08001049 // Force everything with a registered fd with epoll to be destroyed now.
1050 timers_.clear();
1051 phased_loops_.clear();
1052 watchers_.clear();
1053
Alex Perrycb7da4b2019-08-28 19:35:56 -07001054 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
1055}
1056
Alex Perrycb7da4b2019-08-28 19:35:56 -07001057void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
1058 if (is_running()) {
1059 LOG(FATAL) << "Cannot set realtime priority while running.";
1060 }
1061 priority_ = priority;
1062}
1063
Brian Silverman6a54ff32020-04-28 16:41:39 -07001064void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
1065 if (is_running()) {
1066 LOG(FATAL) << "Cannot set affinity while running.";
1067 }
1068 affinity_ = cpuset;
1069}
1070
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001071void ShmEventLoop::set_name(const std::string_view name) {
1072 name_ = std::string(name);
1073 UpdateTimingReport();
1074}
1075
Brian Silvermana5450a92020-08-12 19:59:57 -07001076absl::Span<const char> ShmEventLoop::GetWatcherSharedMemory(
1077 const Channel *channel) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001078 ShmWatcherState *const watcher_state =
1079 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -08001080 return watcher_state->GetSharedMemory();
1081}
1082
Brian Silverman4f4e0612020-08-12 19:54:41 -07001083int ShmEventLoop::NumberBuffers(const Channel *channel) {
1084 return MakeQueueConfiguration(
1085 channel, chrono::ceil<chrono::seconds>(chrono::nanoseconds(
1086 configuration()->channel_storage_duration())))
1087 .num_messages();
1088}
1089
Brian Silverman5120afb2020-01-31 17:44:35 -08001090absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
1091 const aos::RawSender *sender) const {
Brian Silverman148d43d2020-06-07 18:19:22 -05001092 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -08001093}
1094
Brian Silvermana5450a92020-08-12 19:59:57 -07001095absl::Span<const char> ShmEventLoop::GetShmFetcherPrivateMemory(
Brian Silverman6d2b3592020-06-18 14:40:15 -07001096 const aos::RawFetcher *fetcher) const {
1097 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
1098}
1099
Austin Schuh39788ff2019-12-01 18:22:57 -08001100pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
1101
Alex Perrycb7da4b2019-08-28 19:35:56 -07001102} // namespace aos