blob: 39ce8dbab3aa9b7268d23fe2820120506d6aaa05 [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"
Austin Schuh094d09b2020-11-20 23:26:52 -080020#include "aos/init.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070021#include "aos/ipc_lib/lockless_queue.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080022#include "aos/ipc_lib/signalfd.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070023#include "aos/realtime.h"
Austin Schuh32fd5a72019-12-01 22:20:26 -080024#include "aos/stl_mutex/stl_mutex.h"
Austin Schuhfccb2d02020-01-26 16:11:19 -080025#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070026#include "aos/util/phased_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080027#include "glog/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070028
Austin Schuhe84c3ed2019-12-14 15:29:48 -080029namespace {
30
31// Returns the portion of the path after the last /. This very much assumes
32// that the application name is null terminated.
33const char *Filename(const char *path) {
34 const std::string_view path_string_view = path;
35 auto last_slash_pos = path_string_view.find_last_of("/");
36
37 return last_slash_pos == std::string_view::npos ? path
38 : path + last_slash_pos + 1;
39}
40
41} // namespace
42
Alex Perrycb7da4b2019-08-28 19:35:56 -070043DEFINE_string(shm_base, "/dev/shm/aos",
44 "Directory to place queue backing mmaped files in.");
45DEFINE_uint32(permissions, 0770,
46 "Permissions to make shared memory files and folders.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080047DEFINE_string(application_name, Filename(program_invocation_name),
48 "The application name");
Alex Perrycb7da4b2019-08-28 19:35:56 -070049
50namespace aos {
51
Brian Silverman148d43d2020-06-07 18:19:22 -050052using namespace shm_event_loop_internal;
53
Austin Schuhcdab6192019-12-29 17:47:46 -080054void SetShmBase(const std::string_view base) {
Austin Schuhef323c02020-09-01 14:55:28 -070055 FLAGS_shm_base = std::string(base) + "/aos";
Austin Schuhcdab6192019-12-29 17:47:46 -080056}
57
Brian Silverman4f4e0612020-08-12 19:54:41 -070058namespace {
59
Austin Schuhef323c02020-09-01 14:55:28 -070060std::string ShmFolder(std::string_view shm_base, const Channel *channel) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070061 CHECK(channel->has_name());
62 CHECK_EQ(channel->name()->string_view()[0], '/');
Austin Schuhef323c02020-09-01 14:55:28 -070063 return absl::StrCat(shm_base, channel->name()->string_view(), "/");
Alex Perrycb7da4b2019-08-28 19:35:56 -070064}
Austin Schuhef323c02020-09-01 14:55:28 -070065std::string ShmPath(std::string_view shm_base, const Channel *channel) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070066 CHECK(channel->has_type());
Austin Schuhef323c02020-09-01 14:55:28 -070067 return ShmFolder(shm_base, channel) + channel->type()->str() + ".v3";
Alex Perrycb7da4b2019-08-28 19:35:56 -070068}
69
Brian Silvermana5450a92020-08-12 19:59:57 -070070void PageFaultDataWrite(char *data, size_t size) {
Brian Silverman3b0cdaf2020-04-28 16:51:51 -070071 // This just has to divide the actual page size. Being smaller will make this
72 // a bit slower than necessary, but not much. 1024 is a pretty conservative
73 // choice (most pages are probably 4096).
74 static constexpr size_t kPageSize = 1024;
75 const size_t pages = (size + kPageSize - 1) / kPageSize;
76 for (size_t i = 0; i < pages; ++i) {
77 char zero = 0;
78 // We need to ensure there's a writable pagetable entry, but avoid modifying
79 // the data.
80 //
81 // Even if you lock the data into memory, some kernels still seem to lazily
82 // create the actual pagetable entries. This means we need to somehow
83 // "write" to the page.
84 //
85 // Also, this takes place while other processes may be concurrently
86 // opening/initializing the memory, so we need to avoid corrupting that.
87 //
88 // This is the simplest operation I could think of which achieves that:
89 // "store 0 if it's already 0".
90 __atomic_compare_exchange_n(&data[i * kPageSize], &zero, 0, true,
91 __ATOMIC_RELAXED, __ATOMIC_RELAXED);
92 }
93}
94
Brian Silvermana5450a92020-08-12 19:59:57 -070095void PageFaultDataRead(const char *data, size_t size) {
96 // This just has to divide the actual page size. Being smaller will make this
97 // a bit slower than necessary, but not much. 1024 is a pretty conservative
98 // choice (most pages are probably 4096).
99 static constexpr size_t kPageSize = 1024;
100 const size_t pages = (size + kPageSize - 1) / kPageSize;
101 for (size_t i = 0; i < pages; ++i) {
102 // We need to ensure there's a readable pagetable entry.
103 __atomic_load_n(&data[i * kPageSize], __ATOMIC_RELAXED);
104 }
105}
106
Brian Silverman4f4e0612020-08-12 19:54:41 -0700107ipc_lib::LocklessQueueConfiguration MakeQueueConfiguration(
108 const Channel *channel, std::chrono::seconds channel_storage_duration) {
109 ipc_lib::LocklessQueueConfiguration config;
110
111 config.num_watchers = channel->num_watchers();
112 config.num_senders = channel->num_senders();
113 // The value in the channel will default to 0 if readers are configured to
114 // copy.
115 config.num_pinners = channel->num_readers();
116 config.queue_size = channel_storage_duration.count() * channel->frequency();
117 config.message_data_size = channel->max_size();
118
119 return config;
120}
121
Austin Schuh2f8fd752020-09-01 22:38:28 -0700122class MMappedQueue {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123 public:
Austin Schuh2f8fd752020-09-01 22:38:28 -0700124 MMappedQueue(std::string_view shm_base, const Channel *channel,
125 std::chrono::seconds channel_storage_duration)
Brian Silverman4f4e0612020-08-12 19:54:41 -0700126 : config_(MakeQueueConfiguration(channel, channel_storage_duration)) {
Austin Schuhef323c02020-09-01 14:55:28 -0700127 std::string path = ShmPath(shm_base, channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700128
Alex Perrycb7da4b2019-08-28 19:35:56 -0700129 size_ = ipc_lib::LocklessQueueMemorySize(config_);
130
Austin Schuhfccb2d02020-01-26 16:11:19 -0800131 util::MkdirP(path, FLAGS_permissions);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132
133 // There are 2 cases. Either the file already exists, or it does not
134 // already exist and we need to create it. Start by trying to create it. If
135 // that fails, the file has already been created and we can open it
Brian Silverman4f4e0612020-08-12 19:54:41 -0700136 // normally.. Once the file has been created it will never be deleted.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800137 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_EXCL,
Brian Silverman148d43d2020-06-07 18:19:22 -0500138 O_CLOEXEC | FLAGS_permissions);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800139 if (fd == -1 && errno == EEXIST) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140 VLOG(1) << path << " already created.";
141 // File already exists.
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800142 fd = open(path.c_str(), O_RDWR, O_CLOEXEC);
143 PCHECK(fd != -1) << ": Failed to open " << path;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700144 while (true) {
145 struct stat st;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800146 PCHECK(fstat(fd, &st) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700147 if (st.st_size != 0) {
148 CHECK_EQ(static_cast<size_t>(st.st_size), size_)
149 << ": Size of " << path
150 << " doesn't match expected size of backing queue file. Did the "
151 "queue definition change?";
152 break;
153 } else {
154 // The creating process didn't get around to it yet. Give it a bit.
155 std::this_thread::sleep_for(std::chrono::milliseconds(10));
156 VLOG(1) << path << " is zero size, waiting";
157 }
158 }
159 } else {
160 VLOG(1) << "Created " << path;
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800161 PCHECK(fd != -1) << ": Failed to open " << path;
162 PCHECK(ftruncate(fd, size_) == 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 }
164
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800165 data_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166 PCHECK(data_ != MAP_FAILED);
Brian Silvermana5450a92020-08-12 19:59:57 -0700167 const_data_ = mmap(NULL, size_, PROT_READ, MAP_SHARED, fd, 0);
168 PCHECK(const_data_ != MAP_FAILED);
Brian Silvermanf9f30ea2020-03-04 23:18:54 -0800169 PCHECK(close(fd) == 0);
Brian Silvermana5450a92020-08-12 19:59:57 -0700170 PageFaultDataWrite(static_cast<char *>(data_), size_);
171 PageFaultDataRead(static_cast<const char *>(const_data_), size_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700172
173 ipc_lib::InitializeLocklessQueueMemory(memory(), config_);
174 }
175
Austin Schuh2f8fd752020-09-01 22:38:28 -0700176 ~MMappedQueue() {
Brian Silvermana5450a92020-08-12 19:59:57 -0700177 PCHECK(munmap(data_, size_) == 0);
178 PCHECK(munmap(const_cast<void *>(const_data_), size_) == 0);
179 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700180
181 ipc_lib::LocklessQueueMemory *memory() const {
182 return reinterpret_cast<ipc_lib::LocklessQueueMemory *>(data_);
183 }
184
Brian Silvermana5450a92020-08-12 19:59:57 -0700185 const ipc_lib::LocklessQueueMemory *const_memory() const {
186 return reinterpret_cast<const ipc_lib::LocklessQueueMemory *>(const_data_);
187 }
188
Austin Schuh39788ff2019-12-01 18:22:57 -0800189 const ipc_lib::LocklessQueueConfiguration &config() const { return config_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700190
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700191 ipc_lib::LocklessQueue queue() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700192 return ipc_lib::LocklessQueue(const_memory(), memory(), config());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700193 }
194
Brian Silvermana5450a92020-08-12 19:59:57 -0700195 absl::Span<char> GetMutableSharedMemory() const {
Brian Silverman5120afb2020-01-31 17:44:35 -0800196 return absl::Span<char>(static_cast<char *>(data_), size_);
197 }
198
Brian Silvermana5450a92020-08-12 19:59:57 -0700199 absl::Span<const char> GetConstSharedMemory() const {
200 return absl::Span<const char>(static_cast<const char *>(const_data_),
201 size_);
202 }
203
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 private:
Brian Silverman4f4e0612020-08-12 19:54:41 -0700205 const ipc_lib::LocklessQueueConfiguration config_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700206
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207 size_t size_;
208 void *data_;
Brian Silvermana5450a92020-08-12 19:59:57 -0700209 const void *const_data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210};
211
Austin Schuh217a9782019-12-21 23:02:50 -0800212const Node *MaybeMyNode(const Configuration *configuration) {
213 if (!configuration->has_nodes()) {
214 return nullptr;
215 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700216
Austin Schuh217a9782019-12-21 23:02:50 -0800217 return configuration::GetMyNode(configuration);
218}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219
220namespace chrono = ::std::chrono;
221
Austin Schuh39788ff2019-12-01 18:22:57 -0800222} // namespace
223
Austin Schuh217a9782019-12-21 23:02:50 -0800224ShmEventLoop::ShmEventLoop(const Configuration *configuration)
Austin Schuh83c7f702021-01-19 22:36:29 -0800225 : EventLoop(configuration),
226 boot_uuid_(UUID::BootUUID()),
Austin Schuhef323c02020-09-01 14:55:28 -0700227 shm_base_(FLAGS_shm_base),
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800228 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -0800229 node_(MaybeMyNode(configuration)) {
Austin Schuh094d09b2020-11-20 23:26:52 -0800230 CHECK(IsInitialized()) << ": Need to initialize AOS first.";
Austin Schuh15649d62019-12-28 16:36:38 -0800231 if (configuration->has_nodes()) {
232 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
233 }
234}
Austin Schuh217a9782019-12-21 23:02:50 -0800235
Brian Silverman148d43d2020-06-07 18:19:22 -0500236namespace shm_event_loop_internal {
Austin Schuh39788ff2019-12-01 18:22:57 -0800237
238class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700239 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700240 explicit SimpleShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
241 const Channel *channel)
Austin Schuh432784f2020-06-23 17:27:35 -0700242 : event_loop_(event_loop),
243 channel_(channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800244 lockless_queue_memory_(
Austin Schuhef323c02020-09-01 14:55:28 -0700245 shm_base, channel,
Brian Silverman587da252020-01-01 17:00:47 -0800246 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800247 event_loop->configuration()->channel_storage_duration()))),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700248 reader_(lockless_queue_memory_.queue()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 context_.data = nullptr;
250 // Point the queue index at the next index to read starting now. This
251 // makes it such that FetchNext will read the next message sent after
252 // the fetcher is created.
253 PointAtNextQueueIndex();
254 }
255
Austin Schuh39788ff2019-12-01 18:22:57 -0800256 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700257
Brian Silverman77162972020-08-12 19:52:40 -0700258 // Sets this object to pin or copy data, as configured in the channel.
259 void RetrieveData() {
260 if (channel_->read_method() == ReadMethod::PIN) {
261 PinDataOnFetch();
262 } else {
263 CopyDataOnFetch();
264 }
265 }
266
Brian Silverman3bca5322020-08-12 19:35:29 -0700267 // Sets this object to copy data out of the shared memory into a private
268 // buffer when fetching.
269 void CopyDataOnFetch() {
Brian Silverman77162972020-08-12 19:52:40 -0700270 CHECK(!pin_data());
Brian Silverman3bca5322020-08-12 19:35:29 -0700271 data_storage_.reset(static_cast<char *>(
272 malloc(channel_->max_size() + kChannelDataAlignment - 1)));
273 }
274
Brian Silverman77162972020-08-12 19:52:40 -0700275 // Sets this object to pin data in shared memory when fetching.
276 void PinDataOnFetch() {
277 CHECK(!copy_data());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700278 auto maybe_pinner =
279 ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue());
Brian Silverman77162972020-08-12 19:52:40 -0700280 if (!maybe_pinner) {
281 LOG(FATAL) << "Failed to create reader on "
282 << configuration::CleanedChannelToString(channel_)
283 << ", too many readers.";
284 }
285 pinner_ = std::move(maybe_pinner.value());
286 }
287
Alex Perrycb7da4b2019-08-28 19:35:56 -0700288 // Points the next message to fetch at the queue index which will be
289 // populated next.
290 void PointAtNextQueueIndex() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700291 actual_queue_index_ = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700292 if (!actual_queue_index_.valid()) {
293 // Nothing in the queue. The next element will show up at the 0th
294 // index in the queue.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700295 actual_queue_index_ = ipc_lib::QueueIndex::Zero(
296 LocklessQueueSize(lockless_queue_memory_.memory()));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297 } else {
298 actual_queue_index_ = actual_queue_index_.Increment();
299 }
300 }
301
Austin Schuh39788ff2019-12-01 18:22:57 -0800302 bool FetchNext() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700303 const ipc_lib::LocklessQueueReader::Result read_result =
Brian Silverman3bca5322020-08-12 19:35:29 -0700304 DoFetch(actual_queue_index_);
Austin Schuh432784f2020-06-23 17:27:35 -0700305
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700306 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700307 }
308
Austin Schuh39788ff2019-12-01 18:22:57 -0800309 bool Fetch() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700310 const ipc_lib::QueueIndex queue_index = reader_.LatestIndex();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700311 // actual_queue_index_ is only meaningful if it was set by Fetch or
312 // FetchNext. This happens when valid_data_ has been set. So, only
313 // skip checking if valid_data_ is true.
314 //
315 // Also, if the latest queue index is invalid, we are empty. So there
316 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800317 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700318 queue_index == actual_queue_index_.DecrementBy(1u)) ||
319 !queue_index.valid()) {
320 return false;
321 }
322
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700323 const ipc_lib::LocklessQueueReader::Result read_result =
324 DoFetch(queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700325
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700326 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800327 << ": Queue index went backwards. This should never happen. "
328 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700329
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700330 return read_result == ipc_lib::LocklessQueueReader::Result::GOOD;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700331 }
332
Austin Schuh39788ff2019-12-01 18:22:57 -0800333 Context context() const { return context_; }
334
Alex Perrycb7da4b2019-08-28 19:35:56 -0700335 bool RegisterWakeup(int priority) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700336 CHECK(!watcher_);
337 watcher_ = ipc_lib::LocklessQueueWatcher::Make(
338 lockless_queue_memory_.queue(), priority);
339 return static_cast<bool>(watcher_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700340 }
341
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700342 void UnregisterWakeup() {
343 CHECK(watcher_);
344 watcher_ = std::nullopt;
345 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346
Brian Silvermana5450a92020-08-12 19:59:57 -0700347 absl::Span<char> GetMutableSharedMemory() {
348 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800349 }
350
Brian Silvermana5450a92020-08-12 19:59:57 -0700351 absl::Span<const char> GetConstSharedMemory() const {
352 return lockless_queue_memory_.GetConstSharedMemory();
353 }
354
355 absl::Span<const char> GetPrivateMemory() const {
356 if (pin_data()) {
357 return lockless_queue_memory_.GetConstSharedMemory();
358 }
Brian Silverman6d2b3592020-06-18 14:40:15 -0700359 return absl::Span<char>(
360 const_cast<SimpleShmFetcher *>(this)->data_storage_start(),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700361 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()));
Brian Silverman6d2b3592020-06-18 14:40:15 -0700362 }
363
Alex Perrycb7da4b2019-08-28 19:35:56 -0700364 private:
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700365 ipc_lib::LocklessQueueReader::Result DoFetch(
366 ipc_lib::QueueIndex queue_index) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700367 // TODO(austin): Get behind and make sure it dies.
368 char *copy_buffer = nullptr;
369 if (copy_data()) {
370 copy_buffer = data_storage_start();
371 }
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700372 ipc_lib::LocklessQueueReader::Result read_result = reader_.Read(
Brian Silverman3bca5322020-08-12 19:35:29 -0700373 queue_index.index(), &context_.monotonic_event_time,
374 &context_.realtime_event_time, &context_.monotonic_remote_time,
375 &context_.realtime_remote_time, &context_.remote_queue_index,
376 &context_.size, copy_buffer);
377
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700378 if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) {
Brian Silverman77162972020-08-12 19:52:40 -0700379 if (pin_data()) {
Brian Silverman4f4e0612020-08-12 19:54:41 -0700380 const int pin_result = pinner_->PinIndex(queue_index.index());
381 CHECK(pin_result >= 0)
Brian Silverman77162972020-08-12 19:52:40 -0700382 << ": Got behind while reading and the last message was modified "
383 "out from under us while we tried to pin it. Don't get so far "
384 "behind on: "
385 << configuration::CleanedChannelToString(channel_);
Brian Silverman4f4e0612020-08-12 19:54:41 -0700386 context_.buffer_index = pin_result;
387 } else {
388 context_.buffer_index = -1;
Brian Silverman77162972020-08-12 19:52:40 -0700389 }
390
Brian Silverman3bca5322020-08-12 19:35:29 -0700391 context_.queue_index = queue_index.index();
392 if (context_.remote_queue_index == 0xffffffffu) {
393 context_.remote_queue_index = context_.queue_index;
394 }
395 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
396 context_.monotonic_remote_time = context_.monotonic_event_time;
397 }
398 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
399 context_.realtime_remote_time = context_.realtime_event_time;
400 }
401 const char *const data = DataBuffer();
402 if (data) {
403 context_.data =
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700404 data +
405 LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) -
406 context_.size;
Brian Silverman3bca5322020-08-12 19:35:29 -0700407 } else {
408 context_.data = nullptr;
409 }
410 actual_queue_index_ = queue_index.Increment();
411 }
412
413 // Make sure the data wasn't modified while we were reading it. This
414 // can only happen if you are reading the last message *while* it is
415 // being written to, which means you are pretty far behind.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700416 CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE)
Brian Silverman3bca5322020-08-12 19:35:29 -0700417 << ": Got behind while reading and the last message was modified "
418 "out from under us while we were reading it. Don't get so far "
419 "behind on: "
420 << configuration::CleanedChannelToString(channel_);
421
422 // We fell behind between when we read the index and read the value.
423 // This isn't worth recovering from since this means we went to sleep
424 // for a long time in the middle of this function.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700425 if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700426 event_loop_->SendTimingReport();
427 LOG(FATAL) << "The next message is no longer available. "
428 << configuration::CleanedChannelToString(channel_);
429 }
430
431 return read_result;
432 }
433
434 char *data_storage_start() const {
435 CHECK(copy_data());
Brian Silvermana1652f32020-01-29 20:41:44 -0800436 return RoundChannelData(data_storage_.get(), channel_->max_size());
437 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700438
439 // Note that for some modes the return value will change as new messages are
440 // read.
441 const char *DataBuffer() const {
442 if (copy_data()) {
443 return data_storage_start();
444 }
Brian Silverman77162972020-08-12 19:52:40 -0700445 if (pin_data()) {
446 return static_cast<const char *>(pinner_->Data());
447 }
Brian Silverman3bca5322020-08-12 19:35:29 -0700448 return nullptr;
449 }
450
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800451 bool copy_data() const { return static_cast<bool>(data_storage_); }
Brian Silverman77162972020-08-12 19:52:40 -0700452 bool pin_data() const { return static_cast<bool>(pinner_); }
Brian Silvermana1652f32020-01-29 20:41:44 -0800453
Austin Schuh432784f2020-06-23 17:27:35 -0700454 aos::ShmEventLoop *event_loop_;
Austin Schuhf5652592019-12-29 16:26:15 -0800455 const Channel *const channel_;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700456 MMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700457 ipc_lib::LocklessQueueReader reader_;
458 // This being nullopt indicates we're not looking for wakeups right now.
459 std::optional<ipc_lib::LocklessQueueWatcher> watcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700460
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700461 ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700462
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800463 // This being empty indicates we're not going to copy data.
464 std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free};
Austin Schuh39788ff2019-12-01 18:22:57 -0800465
Brian Silverman77162972020-08-12 19:52:40 -0700466 // This being nullopt indicates we're not going to pin messages.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700467 std::optional<ipc_lib::LocklessQueuePinner> pinner_;
Brian Silverman77162972020-08-12 19:52:40 -0700468
Austin Schuh39788ff2019-12-01 18:22:57 -0800469 Context context_;
470};
471
472class ShmFetcher : public RawFetcher {
473 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700474 explicit ShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop,
475 const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800476 : RawFetcher(event_loop, channel),
Austin Schuhef323c02020-09-01 14:55:28 -0700477 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman77162972020-08-12 19:52:40 -0700478 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700479 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800480
481 ~ShmFetcher() { context_.data = nullptr; }
482
483 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
484 if (simple_shm_fetcher_.FetchNext()) {
485 context_ = simple_shm_fetcher_.context();
486 return std::make_pair(true, monotonic_clock::now());
487 }
488 return std::make_pair(false, monotonic_clock::min_time);
489 }
490
491 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
492 if (simple_shm_fetcher_.Fetch()) {
493 context_ = simple_shm_fetcher_.context();
494 return std::make_pair(true, monotonic_clock::now());
495 }
496 return std::make_pair(false, monotonic_clock::min_time);
497 }
498
Brian Silvermana5450a92020-08-12 19:59:57 -0700499 absl::Span<const char> GetPrivateMemory() const {
Brian Silverman6d2b3592020-06-18 14:40:15 -0700500 return simple_shm_fetcher_.GetPrivateMemory();
501 }
502
Austin Schuh39788ff2019-12-01 18:22:57 -0800503 private:
504 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700505};
506
507class ShmSender : public RawSender {
508 public:
Austin Schuhef323c02020-09-01 14:55:28 -0700509 explicit ShmSender(std::string_view shm_base, EventLoop *event_loop,
510 const Channel *channel)
Austin Schuh39788ff2019-12-01 18:22:57 -0800511 : RawSender(event_loop, channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800512 lockless_queue_memory_(
Austin Schuhef323c02020-09-01 14:55:28 -0700513 shm_base, channel,
Brian Silverman587da252020-01-01 17:00:47 -0800514 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800515 event_loop->configuration()->channel_storage_duration()))),
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700516 lockless_queue_sender_(VerifySender(
517 ipc_lib::LocklessQueueSender::Make(lockless_queue_memory_.queue()),
518 channel)),
519 wake_upper_(lockless_queue_memory_.queue()) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700520
Austin Schuh39788ff2019-12-01 18:22:57 -0800521 ~ShmSender() override {}
522
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700523 static ipc_lib::LocklessQueueSender VerifySender(
524 std::optional<ipc_lib::LocklessQueueSender> sender,
Austin Schuhe516ab02020-05-06 21:37:04 -0700525 const Channel *channel) {
526 if (sender) {
527 return std::move(sender.value());
528 }
529 LOG(FATAL) << "Failed to create sender on "
530 << configuration::CleanedChannelToString(channel)
531 << ", too many senders.";
532 }
533
Alex Perrycb7da4b2019-08-28 19:35:56 -0700534 void *data() override { return lockless_queue_sender_.Data(); }
535 size_t size() override { return lockless_queue_sender_.size(); }
Austin Schuhad154822019-12-27 15:45:13 -0800536 bool DoSend(size_t length,
537 aos::monotonic_clock::time_point monotonic_remote_time,
538 aos::realtime_clock::time_point realtime_remote_time,
539 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700540 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
541 << ": Sent too big a message on "
542 << configuration::CleanedChannelToString(channel());
Austin Schuh91ba6392020-10-03 13:27:47 -0700543 CHECK(lockless_queue_sender_.Send(
Austin Schuhad154822019-12-27 15:45:13 -0800544 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
Austin Schuh91ba6392020-10-03 13:27:47 -0700545 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_))
546 << ": Somebody wrote outside the buffer of their message on channel "
547 << configuration::CleanedChannelToString(channel());
548
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700549 wake_upper_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700550 return true;
551 }
552
Austin Schuhad154822019-12-27 15:45:13 -0800553 bool DoSend(const void *msg, size_t length,
554 aos::monotonic_clock::time_point monotonic_remote_time,
555 aos::realtime_clock::time_point realtime_remote_time,
556 uint32_t remote_queue_index) override {
Austin Schuh0f7ed462020-03-28 20:38:34 -0700557 CHECK_LE(length, static_cast<size_t>(channel()->max_size()))
558 << ": Sent too big a message on "
559 << configuration::CleanedChannelToString(channel());
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700560 CHECK(lockless_queue_sender_.Send(
561 reinterpret_cast<const char *>(msg), length, monotonic_remote_time,
562 realtime_remote_time, remote_queue_index, &monotonic_sent_time_,
563 &realtime_sent_time_, &sent_queue_index_))
Austin Schuh91ba6392020-10-03 13:27:47 -0700564 << ": Somebody wrote outside the buffer of their message on channel "
565 << configuration::CleanedChannelToString(channel());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700566 wake_upper_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700567 // TODO(austin): Return an error if we send too fast.
568 return true;
569 }
570
Brian Silverman5120afb2020-01-31 17:44:35 -0800571 absl::Span<char> GetSharedMemory() const {
Brian Silvermana5450a92020-08-12 19:59:57 -0700572 return lockless_queue_memory_.GetMutableSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800573 }
574
Brian Silverman4f4e0612020-08-12 19:54:41 -0700575 int buffer_index() override { return lockless_queue_sender_.buffer_index(); }
576
Alex Perrycb7da4b2019-08-28 19:35:56 -0700577 private:
Austin Schuh2f8fd752020-09-01 22:38:28 -0700578 MMappedQueue lockless_queue_memory_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700579 ipc_lib::LocklessQueueSender lockless_queue_sender_;
580 ipc_lib::LocklessQueueWakeUpper wake_upper_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700581};
582
Alex Perrycb7da4b2019-08-28 19:35:56 -0700583// Class to manage the state for a Watcher.
Brian Silverman148d43d2020-06-07 18:19:22 -0500584class ShmWatcherState : public WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700585 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500586 ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700587 std::string_view shm_base, ShmEventLoop *event_loop,
588 const Channel *channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800589 std::function<void(const Context &context, const void *message)> fn,
590 bool copy_data)
Brian Silverman148d43d2020-06-07 18:19:22 -0500591 : WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800592 event_loop_(event_loop),
593 event_(this),
Austin Schuhef323c02020-09-01 14:55:28 -0700594 simple_shm_fetcher_(shm_base, event_loop, channel) {
Brian Silverman3bca5322020-08-12 19:35:29 -0700595 if (copy_data) {
Brian Silverman77162972020-08-12 19:52:40 -0700596 simple_shm_fetcher_.RetrieveData();
Brian Silverman3bca5322020-08-12 19:35:29 -0700597 }
598 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700599
Brian Silverman148d43d2020-06-07 18:19:22 -0500600 ~ShmWatcherState() override { event_loop_->RemoveEvent(&event_); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800601
602 void Startup(EventLoop *event_loop) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800603 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800604 CHECK(RegisterWakeup(event_loop->priority()));
605 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700606
Alex Perrycb7da4b2019-08-28 19:35:56 -0700607 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800608 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700609 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800610 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800611
612 if (has_new_data_) {
613 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800614 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800615 event_loop_->AddEvent(&event_);
616 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700617 }
618
619 return has_new_data_;
620 }
621
Alex Perrycb7da4b2019-08-28 19:35:56 -0700622 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800623 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700624 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800625 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700626 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800627 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700628 }
629
Austin Schuh39788ff2019-12-01 18:22:57 -0800630 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700631 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800632 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700633 }
634
Austin Schuh39788ff2019-12-01 18:22:57 -0800635 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700636
Brian Silvermana5450a92020-08-12 19:59:57 -0700637 absl::Span<const char> GetSharedMemory() const {
638 return simple_shm_fetcher_.GetConstSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -0800639 }
640
Alex Perrycb7da4b2019-08-28 19:35:56 -0700641 private:
642 bool has_new_data_ = false;
643
Austin Schuh7d87b672019-12-01 20:23:49 -0800644 ShmEventLoop *event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500645 EventHandler<ShmWatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800646 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700647};
648
649// Adapter class to adapt a timerfd to a TimerHandler.
Brian Silverman148d43d2020-06-07 18:19:22 -0500650class ShmTimerHandler final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700651 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500652 ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800653 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800654 shm_event_loop_(shm_event_loop),
655 event_(this) {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800656 shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() {
657 // The timer may fire spurriously. HandleEvent on the event loop will
658 // call the callback if it is needed. It may also have called it when
659 // processing some other event, and the kernel decided to deliver this
660 // wakeup anyways.
661 timerfd_.Read();
662 shm_event_loop_->HandleEvent();
663 });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700664 }
665
Brian Silverman148d43d2020-06-07 18:19:22 -0500666 ~ShmTimerHandler() {
Austin Schuh7d87b672019-12-01 20:23:49 -0800667 Disable();
668 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
669 }
670
671 void HandleEvent() {
Austin Schuhcde39fd2020-02-22 20:58:24 -0800672 CHECK(!event_.valid());
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700673 disabled_ = false;
Austin Schuhcde39fd2020-02-22 20:58:24 -0800674 const auto monotonic_now = Call(monotonic_clock::now, base_);
675 if (event_.valid()) {
676 // If someone called Setup inside Call, rescheduling is already taken care
677 // of. Bail.
678 return;
Austin Schuh7d87b672019-12-01 20:23:49 -0800679 }
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700680 if (disabled_) {
681 // Somebody called Disable inside Call, so we don't want to reschedule.
682 // Bail.
683 return;
684 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800685
Austin Schuhcde39fd2020-02-22 20:58:24 -0800686 if (repeat_offset_ == chrono::seconds(0)) {
687 timerfd_.Disable();
688 } else {
689 // Compute how many cycles have elapsed and schedule the next iteration
690 // for the next iteration in the future.
691 const int elapsed_cycles =
692 std::max<int>(0, (monotonic_now - base_ + repeat_offset_ -
693 std::chrono::nanoseconds(1)) /
694 repeat_offset_);
695 base_ += repeat_offset_ * elapsed_cycles;
Austin Schuh7d87b672019-12-01 20:23:49 -0800696
Austin Schuhcde39fd2020-02-22 20:58:24 -0800697 // Update the heap and schedule the timerfd wakeup.
Austin Schuh7d87b672019-12-01 20:23:49 -0800698 event_.set_event_time(base_);
699 shm_event_loop_->AddEvent(&event_);
Austin Schuhcde39fd2020-02-22 20:58:24 -0800700 timerfd_.SetTime(base_, chrono::seconds(0));
Austin Schuh7d87b672019-12-01 20:23:49 -0800701 }
702 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700703
704 void Setup(monotonic_clock::time_point base,
705 monotonic_clock::duration repeat_offset) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800706 if (event_.valid()) {
707 shm_event_loop_->RemoveEvent(&event_);
708 }
709
Alex Perrycb7da4b2019-08-28 19:35:56 -0700710 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800711 base_ = base;
712 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800713 event_.set_event_time(base_);
714 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700715 }
716
Austin Schuh7d87b672019-12-01 20:23:49 -0800717 void Disable() override {
718 shm_event_loop_->RemoveEvent(&event_);
719 timerfd_.Disable();
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700720 disabled_ = true;
Austin Schuh7d87b672019-12-01 20:23:49 -0800721 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700722
723 private:
724 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500725 EventHandler<ShmTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700726
Brian Silverman148d43d2020-06-07 18:19:22 -0500727 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700728
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800729 monotonic_clock::time_point base_;
730 monotonic_clock::duration repeat_offset_;
Brian Silvermanaf9a4d82020-10-06 15:10:58 -0700731
732 // Used to track if Disable() was called during the callback, so we know not
733 // to reschedule.
734 bool disabled_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700735};
736
737// Adapter class to the timerfd and PhasedLoop.
Brian Silverman148d43d2020-06-07 18:19:22 -0500738class ShmPhasedLoopHandler final : public PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700739 public:
Brian Silverman148d43d2020-06-07 18:19:22 -0500740 ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop,
741 ::std::function<void(int)> fn,
742 const monotonic_clock::duration interval,
743 const monotonic_clock::duration offset)
744 : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800745 shm_event_loop_(shm_event_loop),
746 event_(this) {
747 shm_event_loop_->epoll_.OnReadable(
748 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
749 }
750
751 void HandleEvent() {
752 // The return value for read is the number of cycles that have elapsed.
753 // Because we check to see when this event *should* have happened, there are
754 // cases where Read() will return 0, when 1 cycle has actually happened.
755 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
756 // ignore it. Call handles rescheduling and calculating elapsed cycles
757 // without any extra help.
758 timerfd_.Read();
759 event_.Invalidate();
760
761 Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) {
762 Schedule(sleep_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700763 });
764 }
765
Brian Silverman148d43d2020-06-07 18:19:22 -0500766 ~ShmPhasedLoopHandler() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800767 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800768 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700769 }
770
771 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800772 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800773 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800774 if (event_.valid()) {
775 shm_event_loop_->RemoveEvent(&event_);
776 }
777
Austin Schuh39788ff2019-12-01 18:22:57 -0800778 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800779 event_.set_event_time(sleep_time);
780 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700781 }
782
783 ShmEventLoop *shm_event_loop_;
Brian Silverman148d43d2020-06-07 18:19:22 -0500784 EventHandler<ShmPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700785
Brian Silverman148d43d2020-06-07 18:19:22 -0500786 internal::TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700787};
Brian Silverman148d43d2020-06-07 18:19:22 -0500788
789} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -0700790
791::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
792 const Channel *channel) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800793 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
794 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
795 << "\", \"type\": \"" << channel->type()->string_view()
796 << "\" } is not able to be fetched on this node. Check your "
797 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800798 }
799
Austin Schuhef323c02020-09-01 14:55:28 -0700800 return ::std::unique_ptr<RawFetcher>(
801 new ShmFetcher(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700802}
803
804::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
805 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800806 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800807
Austin Schuhef323c02020-09-01 14:55:28 -0700808 return ::std::unique_ptr<RawSender>(new ShmSender(shm_base_, this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700809}
810
811void ShmEventLoop::MakeRawWatcher(
812 const Channel *channel,
813 std::function<void(const Context &context, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800814 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800815
Austin Schuh39788ff2019-12-01 18:22:57 -0800816 NewWatcher(::std::unique_ptr<WatcherState>(
Austin Schuhef323c02020-09-01 14:55:28 -0700817 new ShmWatcherState(shm_base_, this, channel, std::move(watcher), true)));
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800818}
819
820void ShmEventLoop::MakeRawNoArgWatcher(
821 const Channel *channel,
822 std::function<void(const Context &context)> watcher) {
823 TakeWatcher(channel);
824
Brian Silverman148d43d2020-06-07 18:19:22 -0500825 NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState(
Austin Schuhef323c02020-09-01 14:55:28 -0700826 shm_base_, this, channel,
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800827 [watcher](const Context &context, const void *) { watcher(context); },
828 false)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700829}
830
831TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800832 return NewTimer(::std::unique_ptr<TimerHandler>(
Brian Silverman148d43d2020-06-07 18:19:22 -0500833 new ShmTimerHandler(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700834}
835
836PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
837 ::std::function<void(int)> callback,
838 const monotonic_clock::duration interval,
839 const monotonic_clock::duration offset) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500840 return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>(
841 new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700842}
843
844void ShmEventLoop::OnRun(::std::function<void()> on_run) {
845 on_run_.push_back(::std::move(on_run));
846}
847
Austin Schuh7d87b672019-12-01 20:23:49 -0800848void ShmEventLoop::HandleEvent() {
849 // Update all the times for handlers.
850 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -0500851 ShmWatcherState *watcher =
852 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Austin Schuh7d87b672019-12-01 20:23:49 -0800853
854 watcher->CheckForNewData();
855 }
856
Austin Schuh39788ff2019-12-01 18:22:57 -0800857 while (true) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800858 if (EventCount() == 0 ||
859 PeekEvent()->event_time() > monotonic_clock::now()) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800860 break;
861 }
862
Austin Schuh7d87b672019-12-01 20:23:49 -0800863 EventLoopEvent *event = PopEvent();
864 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800865 }
866}
867
Austin Schuh32fd5a72019-12-01 22:20:26 -0800868// RAII class to mask signals.
869class ScopedSignalMask {
870 public:
871 ScopedSignalMask(std::initializer_list<int> signals) {
872 sigset_t sigset;
873 PCHECK(sigemptyset(&sigset) == 0);
874 for (int signal : signals) {
875 PCHECK(sigaddset(&sigset, signal) == 0);
876 }
877
878 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
879 }
880
881 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
882
883 private:
884 sigset_t old_;
885};
886
887// Class to manage the static state associated with killing multiple event
888// loops.
889class SignalHandler {
890 public:
891 // Gets the singleton.
892 static SignalHandler *global() {
893 static SignalHandler loop;
894 return &loop;
895 }
896
897 // Handles the signal with the singleton.
898 static void HandleSignal(int) { global()->DoHandleSignal(); }
899
900 // Registers an event loop to receive Exit() calls.
901 void Register(ShmEventLoop *event_loop) {
902 // Block signals while we have the mutex so we never race with the signal
903 // handler.
904 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
905 std::unique_lock<stl_mutex> locker(mutex_);
906 if (event_loops_.size() == 0) {
907 // The first caller registers the signal handler.
908 struct sigaction new_action;
909 sigemptyset(&new_action.sa_mask);
910 // This makes it so that 2 control c's to a stuck process will kill it by
911 // restoring the original signal handler.
912 new_action.sa_flags = SA_RESETHAND;
913 new_action.sa_handler = &HandleSignal;
914
915 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
916 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
917 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
918 }
919
920 event_loops_.push_back(event_loop);
921 }
922
923 // Unregisters an event loop to receive Exit() calls.
924 void Unregister(ShmEventLoop *event_loop) {
925 // Block signals while we have the mutex so we never race with the signal
926 // handler.
927 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
928 std::unique_lock<stl_mutex> locker(mutex_);
929
Brian Silverman5120afb2020-01-31 17:44:35 -0800930 event_loops_.erase(
931 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800932
933 if (event_loops_.size() == 0u) {
934 // The last caller restores the original signal handlers.
935 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
936 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
937 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
938 }
939 }
940
941 private:
942 void DoHandleSignal() {
943 // We block signals while grabbing the lock, so there should never be a
944 // race. Confirm that this is true using trylock.
945 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
946 "modifing the event loop list.";
947 for (ShmEventLoop *event_loop : event_loops_) {
948 event_loop->Exit();
949 }
950 mutex_.unlock();
951 }
952
953 // Mutex to protect all state.
954 stl_mutex mutex_;
955 std::vector<ShmEventLoop *> event_loops_;
956 struct sigaction old_action_int_;
957 struct sigaction old_action_hup_;
958 struct sigaction old_action_term_;
959};
960
Alex Perrycb7da4b2019-08-28 19:35:56 -0700961void ShmEventLoop::Run() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800962 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -0800963
Alex Perrycb7da4b2019-08-28 19:35:56 -0700964 std::unique_ptr<ipc_lib::SignalFd> signalfd;
965
966 if (watchers_.size() > 0) {
967 signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
968
969 epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() {
970 signalfd_siginfo result = signalfd_ptr->Read();
971 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
972
973 // TODO(austin): We should really be checking *everything*, not just
974 // watchers, and calling the oldest thing first. That will improve
975 // determinism a lot.
976
Austin Schuh7d87b672019-12-01 20:23:49 -0800977 HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700978 });
979 }
980
Austin Schuh39788ff2019-12-01 18:22:57 -0800981 MaybeScheduleTimingReports();
982
Austin Schuh7d87b672019-12-01 20:23:49 -0800983 ReserveEvents();
984
Tyler Chatow67ddb032020-01-12 14:30:04 -0800985 {
Austin Schuha0c41ba2020-09-10 22:59:14 -0700986 logging::ScopedLogRestorer prev_logger;
Tyler Chatow67ddb032020-01-12 14:30:04 -0800987 AosLogToFbs aos_logger;
988 if (!skip_logger_) {
989 aos_logger.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
Austin Schuha0c41ba2020-09-10 22:59:14 -0700990 prev_logger.Swap(aos_logger.implementation());
Tyler Chatow67ddb032020-01-12 14:30:04 -0800991 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700992
Tyler Chatow67ddb032020-01-12 14:30:04 -0800993 aos::SetCurrentThreadName(name_.substr(0, 16));
Brian Silverman6a54ff32020-04-28 16:41:39 -0700994 const cpu_set_t default_affinity = DefaultAffinity();
995 if (!CPU_EQUAL(&affinity_, &default_affinity)) {
996 ::aos::SetCurrentThreadAffinity(affinity_);
997 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800998 // Now, all the callbacks are setup. Lock everything into memory and go RT.
999 if (priority_ != 0) {
1000 ::aos::InitRT();
1001
1002 LOG(INFO) << "Setting priority to " << priority_;
1003 ::aos::SetCurrentThreadRealtimePriority(priority_);
1004 }
1005
1006 set_is_running(true);
1007
1008 // Now that we are realtime (but before the OnRun handlers run), snap the
1009 // queue index.
1010 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
1011 watcher->Startup(this);
1012 }
1013
1014 // Now that we are RT, run all the OnRun handlers.
1015 for (const auto &run : on_run_) {
1016 run();
1017 }
1018
1019 // And start our main event loop which runs all the timers and handles Quit.
1020 epoll_.Run();
1021
1022 // Once epoll exits, there is no useful nonrt work left to do.
1023 set_is_running(false);
1024
1025 // Nothing time or synchronization critical needs to happen after this
1026 // point. Drop RT priority.
1027 ::aos::UnsetCurrentThreadRealtimePriority();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001028 }
1029
Austin Schuh39788ff2019-12-01 18:22:57 -08001030 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001031 ShmWatcherState *watcher =
1032 reinterpret_cast<ShmWatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -07001033 watcher->UnregisterWakeup();
1034 }
1035
1036 if (watchers_.size() > 0) {
1037 epoll_.DeleteFd(signalfd->fd());
1038 signalfd.reset();
1039 }
Austin Schuh32fd5a72019-12-01 22:20:26 -08001040
1041 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001042
1043 // Trigger any remaining senders or fetchers to be cleared before destroying
1044 // the event loop so the book keeping matches. Do this in the thread that
1045 // created the timing reporter.
1046 timing_report_sender_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001047}
1048
1049void ShmEventLoop::Exit() { epoll_.Quit(); }
1050
1051ShmEventLoop::~ShmEventLoop() {
Austin Schuh39788ff2019-12-01 18:22:57 -08001052 // Force everything with a registered fd with epoll to be destroyed now.
1053 timers_.clear();
1054 phased_loops_.clear();
1055 watchers_.clear();
1056
Alex Perrycb7da4b2019-08-28 19:35:56 -07001057 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
1058}
1059
Alex Perrycb7da4b2019-08-28 19:35:56 -07001060void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
1061 if (is_running()) {
1062 LOG(FATAL) << "Cannot set realtime priority while running.";
1063 }
1064 priority_ = priority;
1065}
1066
Brian Silverman6a54ff32020-04-28 16:41:39 -07001067void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) {
1068 if (is_running()) {
1069 LOG(FATAL) << "Cannot set affinity while running.";
1070 }
1071 affinity_ = cpuset;
1072}
1073
James Kuszmaul57c2baa2020-01-19 14:52:52 -08001074void ShmEventLoop::set_name(const std::string_view name) {
1075 name_ = std::string(name);
1076 UpdateTimingReport();
1077}
1078
Brian Silvermana5450a92020-08-12 19:59:57 -07001079absl::Span<const char> ShmEventLoop::GetWatcherSharedMemory(
1080 const Channel *channel) {
Brian Silverman148d43d2020-06-07 18:19:22 -05001081 ShmWatcherState *const watcher_state =
1082 static_cast<ShmWatcherState *>(GetWatcherState(channel));
Brian Silverman5120afb2020-01-31 17:44:35 -08001083 return watcher_state->GetSharedMemory();
1084}
1085
Brian Silverman4f4e0612020-08-12 19:54:41 -07001086int ShmEventLoop::NumberBuffers(const Channel *channel) {
1087 return MakeQueueConfiguration(
1088 channel, chrono::ceil<chrono::seconds>(chrono::nanoseconds(
1089 configuration()->channel_storage_duration())))
1090 .num_messages();
1091}
1092
Brian Silverman5120afb2020-01-31 17:44:35 -08001093absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
1094 const aos::RawSender *sender) const {
Brian Silverman148d43d2020-06-07 18:19:22 -05001095 return static_cast<const ShmSender *>(sender)->GetSharedMemory();
Brian Silverman5120afb2020-01-31 17:44:35 -08001096}
1097
Brian Silvermana5450a92020-08-12 19:59:57 -07001098absl::Span<const char> ShmEventLoop::GetShmFetcherPrivateMemory(
Brian Silverman6d2b3592020-06-18 14:40:15 -07001099 const aos::RawFetcher *fetcher) const {
1100 return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory();
1101}
1102
Austin Schuh39788ff2019-12-01 18:22:57 -08001103pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
1104
Alex Perrycb7da4b2019-08-28 19:35:56 -07001105} // namespace aos