blob: b4578cbb5f071c856e47da0821a7bbfb910448cc [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>
8#include <algorithm>
9#include <atomic>
10#include <chrono>
Austin Schuh39788ff2019-12-01 18:22:57 -080011#include <iterator>
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include <stdexcept>
13
14#include "aos/events/epoll.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080015#include "aos/events/event_loop_generated.h"
16#include "aos/events/timing_statistics.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "aos/ipc_lib/lockless_queue.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080018#include "aos/ipc_lib/signalfd.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "aos/realtime.h"
Austin Schuh32fd5a72019-12-01 22:20:26 -080020#include "aos/stl_mutex/stl_mutex.h"
Austin Schuhfccb2d02020-01-26 16:11:19 -080021#include "aos/util/file.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070022#include "aos/util/phased_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080023#include "glog/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070024
Austin Schuhe84c3ed2019-12-14 15:29:48 -080025namespace {
26
27// Returns the portion of the path after the last /. This very much assumes
28// that the application name is null terminated.
29const char *Filename(const char *path) {
30 const std::string_view path_string_view = path;
31 auto last_slash_pos = path_string_view.find_last_of("/");
32
33 return last_slash_pos == std::string_view::npos ? path
34 : path + last_slash_pos + 1;
35}
36
37} // namespace
38
Alex Perrycb7da4b2019-08-28 19:35:56 -070039DEFINE_string(shm_base, "/dev/shm/aos",
40 "Directory to place queue backing mmaped files in.");
41DEFINE_uint32(permissions, 0770,
42 "Permissions to make shared memory files and folders.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080043DEFINE_string(application_name, Filename(program_invocation_name),
44 "The application name");
Alex Perrycb7da4b2019-08-28 19:35:56 -070045
46namespace aos {
47
Austin Schuhcdab6192019-12-29 17:47:46 -080048void SetShmBase(const std::string_view base) {
49 FLAGS_shm_base = std::string(base) + "/dev/shm/aos";
50}
51
Alex Perrycb7da4b2019-08-28 19:35:56 -070052std::string ShmFolder(const Channel *channel) {
53 CHECK(channel->has_name());
54 CHECK_EQ(channel->name()->string_view()[0], '/');
55 return FLAGS_shm_base + channel->name()->str() + "/";
56}
57std::string ShmPath(const Channel *channel) {
58 CHECK(channel->has_type());
Austin Schuhad154822019-12-27 15:45:13 -080059 return ShmFolder(channel) + channel->type()->str() + ".v1";
Alex Perrycb7da4b2019-08-28 19:35:56 -070060}
61
62class MMapedQueue {
63 public:
Austin Schuhaa79e4e2019-12-29 20:43:32 -080064 MMapedQueue(const Channel *channel,
65 const std::chrono::seconds channel_storage_duration) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070066 std::string path = ShmPath(channel);
67
Austin Schuh80c7fce2019-12-05 20:48:43 -080068 config_.num_watchers = channel->num_watchers();
69 config_.num_senders = channel->num_senders();
Austin Schuhaa79e4e2019-12-29 20:43:32 -080070 config_.queue_size =
71 channel_storage_duration.count() * channel->frequency();
Alex Perrycb7da4b2019-08-28 19:35:56 -070072 config_.message_data_size = channel->max_size();
73
74 size_ = ipc_lib::LocklessQueueMemorySize(config_);
75
Austin Schuhfccb2d02020-01-26 16:11:19 -080076 util::MkdirP(path, FLAGS_permissions);
Alex Perrycb7da4b2019-08-28 19:35:56 -070077
78 // There are 2 cases. Either the file already exists, or it does not
79 // already exist and we need to create it. Start by trying to create it. If
80 // that fails, the file has already been created and we can open it
81 // normally.. Once the file has been created it wil never be deleted.
82 fd_ = open(path.c_str(), O_RDWR | O_CREAT | O_EXCL,
83 O_CLOEXEC | FLAGS_permissions);
84 if (fd_ == -1 && errno == EEXIST) {
85 VLOG(1) << path << " already created.";
86 // File already exists.
87 fd_ = open(path.c_str(), O_RDWR, O_CLOEXEC);
88 PCHECK(fd_ != -1) << ": Failed to open " << path;
89 while (true) {
90 struct stat st;
91 PCHECK(fstat(fd_, &st) == 0);
92 if (st.st_size != 0) {
93 CHECK_EQ(static_cast<size_t>(st.st_size), size_)
94 << ": Size of " << path
95 << " doesn't match expected size of backing queue file. Did the "
96 "queue definition change?";
97 break;
98 } else {
99 // The creating process didn't get around to it yet. Give it a bit.
100 std::this_thread::sleep_for(std::chrono::milliseconds(10));
101 VLOG(1) << path << " is zero size, waiting";
102 }
103 }
104 } else {
105 VLOG(1) << "Created " << path;
106 PCHECK(fd_ != -1) << ": Failed to open " << path;
107 PCHECK(ftruncate(fd_, size_) == 0);
108 }
109
110 data_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
111 PCHECK(data_ != MAP_FAILED);
112
113 ipc_lib::InitializeLocklessQueueMemory(memory(), config_);
114 }
115
116 ~MMapedQueue() {
117 PCHECK(munmap(data_, size_) == 0);
118 PCHECK(close(fd_) == 0);
119 }
120
121 ipc_lib::LocklessQueueMemory *memory() const {
122 return reinterpret_cast<ipc_lib::LocklessQueueMemory *>(data_);
123 }
124
Austin Schuh39788ff2019-12-01 18:22:57 -0800125 const ipc_lib::LocklessQueueConfiguration &config() const { return config_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126
Brian Silverman5120afb2020-01-31 17:44:35 -0800127 absl::Span<char> GetSharedMemory() const {
128 return absl::Span<char>(static_cast<char *>(data_), size_);
129 }
130
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 ipc_lib::LocklessQueueConfiguration config_;
133
134 int fd_;
135
136 size_t size_;
137 void *data_;
138};
139
Austin Schuh217a9782019-12-21 23:02:50 -0800140namespace {
141
Austin Schuh217a9782019-12-21 23:02:50 -0800142const Node *MaybeMyNode(const Configuration *configuration) {
143 if (!configuration->has_nodes()) {
144 return nullptr;
145 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700146
Austin Schuh217a9782019-12-21 23:02:50 -0800147 return configuration::GetMyNode(configuration);
148}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700149
150namespace chrono = ::std::chrono;
151
Austin Schuh39788ff2019-12-01 18:22:57 -0800152} // namespace
153
Austin Schuh217a9782019-12-21 23:02:50 -0800154ShmEventLoop::ShmEventLoop(const Configuration *configuration)
155 : EventLoop(configuration),
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800156 name_(FLAGS_application_name),
Austin Schuh15649d62019-12-28 16:36:38 -0800157 node_(MaybeMyNode(configuration)) {
158 if (configuration->has_nodes()) {
159 CHECK(node_ != nullptr) << ": Couldn't find node in config.";
160 }
161}
Austin Schuh217a9782019-12-21 23:02:50 -0800162
Austin Schuh39788ff2019-12-01 18:22:57 -0800163namespace internal {
164
165class SimpleShmFetcher {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166 public:
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800167 explicit SimpleShmFetcher(EventLoop *event_loop, const Channel *channel)
Austin Schuhf5652592019-12-29 16:26:15 -0800168 : channel_(channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800169 lockless_queue_memory_(
170 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800171 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800172 event_loop->configuration()->channel_storage_duration()))),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 lockless_queue_(lockless_queue_memory_.memory(),
174 lockless_queue_memory_.config()),
Brian Silvermana1652f32020-01-29 20:41:44 -0800175 data_storage_(static_cast<char *>(malloc(channel->max_size() +
176 kChannelDataAlignment - 1)),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700177 &free) {
178 context_.data = nullptr;
179 // Point the queue index at the next index to read starting now. This
180 // makes it such that FetchNext will read the next message sent after
181 // the fetcher is created.
182 PointAtNextQueueIndex();
183 }
184
Austin Schuh39788ff2019-12-01 18:22:57 -0800185 ~SimpleShmFetcher() {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186
187 // Points the next message to fetch at the queue index which will be
188 // populated next.
189 void PointAtNextQueueIndex() {
190 actual_queue_index_ = lockless_queue_.LatestQueueIndex();
191 if (!actual_queue_index_.valid()) {
192 // Nothing in the queue. The next element will show up at the 0th
193 // index in the queue.
194 actual_queue_index_ =
195 ipc_lib::QueueIndex::Zero(lockless_queue_.queue_size());
196 } else {
197 actual_queue_index_ = actual_queue_index_.Increment();
198 }
199 }
200
Austin Schuh39788ff2019-12-01 18:22:57 -0800201 bool FetchNext() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700202 // TODO(austin): Get behind and make sure it dies both here and with
203 // Fetch.
204 ipc_lib::LocklessQueue::ReadResult read_result = lockless_queue_.Read(
Austin Schuhad154822019-12-27 15:45:13 -0800205 actual_queue_index_.index(), &context_.monotonic_event_time,
206 &context_.realtime_event_time, &context_.monotonic_remote_time,
207 &context_.realtime_remote_time, &context_.remote_queue_index,
Brian Silvermana1652f32020-01-29 20:41:44 -0800208 &context_.size, data_storage_start());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209 if (read_result == ipc_lib::LocklessQueue::ReadResult::GOOD) {
210 context_.queue_index = actual_queue_index_.index();
Austin Schuhad154822019-12-27 15:45:13 -0800211 if (context_.remote_queue_index == 0xffffffffu) {
212 context_.remote_queue_index = context_.queue_index;
213 }
214 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
215 context_.monotonic_remote_time = context_.monotonic_event_time;
216 }
217 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
218 context_.realtime_remote_time = context_.realtime_event_time;
219 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800220 context_.data = data_storage_start() +
Austin Schuh39788ff2019-12-01 18:22:57 -0800221 lockless_queue_.message_data_size() - context_.size;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700222 actual_queue_index_ = actual_queue_index_.Increment();
223 }
224
225 // Make sure the data wasn't modified while we were reading it. This
226 // can only happen if you are reading the last message *while* it is
227 // being written to, which means you are pretty far behind.
228 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::OVERWROTE)
229 << ": Got behind while reading and the last message was modified "
Austin Schuhf5652592019-12-29 16:26:15 -0800230 "out from under us while we were reading it. Don't get so far "
231 "behind. "
232 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700233
234 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::TOO_OLD)
Austin Schuhf5652592019-12-29 16:26:15 -0800235 << ": The next message is no longer available. "
236 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700237 return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD;
238 }
239
Austin Schuh39788ff2019-12-01 18:22:57 -0800240 bool Fetch() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700241 const ipc_lib::QueueIndex queue_index = lockless_queue_.LatestQueueIndex();
242 // actual_queue_index_ is only meaningful if it was set by Fetch or
243 // FetchNext. This happens when valid_data_ has been set. So, only
244 // skip checking if valid_data_ is true.
245 //
246 // Also, if the latest queue index is invalid, we are empty. So there
247 // is nothing to fetch.
Austin Schuh39788ff2019-12-01 18:22:57 -0800248 if ((context_.data != nullptr &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 queue_index == actual_queue_index_.DecrementBy(1u)) ||
250 !queue_index.valid()) {
251 return false;
252 }
253
Austin Schuhad154822019-12-27 15:45:13 -0800254 ipc_lib::LocklessQueue::ReadResult read_result = lockless_queue_.Read(
255 queue_index.index(), &context_.monotonic_event_time,
256 &context_.realtime_event_time, &context_.monotonic_remote_time,
257 &context_.realtime_remote_time, &context_.remote_queue_index,
Brian Silvermana1652f32020-01-29 20:41:44 -0800258 &context_.size, data_storage_start());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259 if (read_result == ipc_lib::LocklessQueue::ReadResult::GOOD) {
260 context_.queue_index = queue_index.index();
Austin Schuhad154822019-12-27 15:45:13 -0800261 if (context_.remote_queue_index == 0xffffffffu) {
262 context_.remote_queue_index = context_.queue_index;
263 }
264 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
265 context_.monotonic_remote_time = context_.monotonic_event_time;
266 }
267 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
268 context_.realtime_remote_time = context_.realtime_event_time;
269 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800270 context_.data = data_storage_start() +
Austin Schuh39788ff2019-12-01 18:22:57 -0800271 lockless_queue_.message_data_size() - context_.size;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700272 actual_queue_index_ = queue_index.Increment();
273 }
274
275 // Make sure the data wasn't modified while we were reading it. This
276 // can only happen if you are reading the last message *while* it is
277 // being written to, which means you are pretty far behind.
278 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::OVERWROTE)
279 << ": Got behind while reading and the last message was modified "
Austin Schuhf5652592019-12-29 16:26:15 -0800280 "out from under us while we were reading it. Don't get so far "
281 "behind."
282 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700283
284 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::NOTHING_NEW)
Austin Schuhf5652592019-12-29 16:26:15 -0800285 << ": Queue index went backwards. This should never happen. "
286 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700287
288 // We fell behind between when we read the index and read the value.
289 // This isn't worth recovering from since this means we went to sleep
290 // for a long time in the middle of this function.
291 CHECK(read_result != ipc_lib::LocklessQueue::ReadResult::TOO_OLD)
Austin Schuhf5652592019-12-29 16:26:15 -0800292 << ": The next message is no longer available. "
293 << configuration::CleanedChannelToString(channel_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700294 return read_result == ipc_lib::LocklessQueue::ReadResult::GOOD;
295 }
296
Austin Schuh39788ff2019-12-01 18:22:57 -0800297 Context context() const { return context_; }
298
Alex Perrycb7da4b2019-08-28 19:35:56 -0700299 bool RegisterWakeup(int priority) {
300 return lockless_queue_.RegisterWakeup(priority);
301 }
302
303 void UnregisterWakeup() { lockless_queue_.UnregisterWakeup(); }
304
Brian Silverman5120afb2020-01-31 17:44:35 -0800305 absl::Span<char> GetSharedMemory() const {
306 return lockless_queue_memory_.GetSharedMemory();
307 }
308
Alex Perrycb7da4b2019-08-28 19:35:56 -0700309 private:
Brian Silvermana1652f32020-01-29 20:41:44 -0800310 char *data_storage_start() {
311 return RoundChannelData(data_storage_.get(), channel_->max_size());
312 }
313
Austin Schuhf5652592019-12-29 16:26:15 -0800314 const Channel *const channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700315 MMapedQueue lockless_queue_memory_;
316 ipc_lib::LocklessQueue lockless_queue_;
317
318 ipc_lib::QueueIndex actual_queue_index_ =
319 ipc_lib::LocklessQueue::empty_queue_index();
320
Brian Silvermana1652f32020-01-29 20:41:44 -0800321 std::unique_ptr<char, decltype(&free)> data_storage_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800322
323 Context context_;
324};
325
326class ShmFetcher : public RawFetcher {
327 public:
328 explicit ShmFetcher(EventLoop *event_loop, const Channel *channel)
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800329 : RawFetcher(event_loop, channel),
330 simple_shm_fetcher_(event_loop, channel) {}
Austin Schuh39788ff2019-12-01 18:22:57 -0800331
332 ~ShmFetcher() { context_.data = nullptr; }
333
334 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
335 if (simple_shm_fetcher_.FetchNext()) {
336 context_ = simple_shm_fetcher_.context();
337 return std::make_pair(true, monotonic_clock::now());
338 }
339 return std::make_pair(false, monotonic_clock::min_time);
340 }
341
342 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
343 if (simple_shm_fetcher_.Fetch()) {
344 context_ = simple_shm_fetcher_.context();
345 return std::make_pair(true, monotonic_clock::now());
346 }
347 return std::make_pair(false, monotonic_clock::min_time);
348 }
349
350 private:
351 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700352};
353
354class ShmSender : public RawSender {
355 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800356 explicit ShmSender(EventLoop *event_loop, const Channel *channel)
357 : RawSender(event_loop, channel),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800358 lockless_queue_memory_(
359 channel,
Brian Silverman587da252020-01-01 17:00:47 -0800360 chrono::ceil<chrono::seconds>(chrono::nanoseconds(
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800361 event_loop->configuration()->channel_storage_duration()))),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700362 lockless_queue_(lockless_queue_memory_.memory(),
363 lockless_queue_memory_.config()),
364 lockless_queue_sender_(lockless_queue_.MakeSender()) {}
365
Austin Schuh39788ff2019-12-01 18:22:57 -0800366 ~ShmSender() override {}
367
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 void *data() override { return lockless_queue_sender_.Data(); }
369 size_t size() override { return lockless_queue_sender_.size(); }
Austin Schuhad154822019-12-27 15:45:13 -0800370 bool DoSend(size_t length,
371 aos::monotonic_clock::time_point monotonic_remote_time,
372 aos::realtime_clock::time_point realtime_remote_time,
373 uint32_t remote_queue_index) override {
374 lockless_queue_sender_.Send(
375 length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
376 &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800377 lockless_queue_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700378 return true;
379 }
380
Austin Schuhad154822019-12-27 15:45:13 -0800381 bool DoSend(const void *msg, size_t length,
382 aos::monotonic_clock::time_point monotonic_remote_time,
383 aos::realtime_clock::time_point realtime_remote_time,
384 uint32_t remote_queue_index) override {
385 lockless_queue_sender_.Send(reinterpret_cast<const char *>(msg), length,
386 monotonic_remote_time, realtime_remote_time,
387 remote_queue_index, &monotonic_sent_time_,
388 &realtime_sent_time_, &sent_queue_index_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800389 lockless_queue_.Wakeup(event_loop()->priority());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700390 // TODO(austin): Return an error if we send too fast.
391 return true;
392 }
393
Brian Silverman5120afb2020-01-31 17:44:35 -0800394 absl::Span<char> GetSharedMemory() const {
395 return lockless_queue_memory_.GetSharedMemory();
396 }
397
Alex Perrycb7da4b2019-08-28 19:35:56 -0700398 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700399 MMapedQueue lockless_queue_memory_;
400 ipc_lib::LocklessQueue lockless_queue_;
401 ipc_lib::LocklessQueue::Sender lockless_queue_sender_;
402};
403
Alex Perrycb7da4b2019-08-28 19:35:56 -0700404// Class to manage the state for a Watcher.
Austin Schuh39788ff2019-12-01 18:22:57 -0800405class WatcherState : public aos::WatcherState {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700406 public:
407 WatcherState(
Austin Schuh7d87b672019-12-01 20:23:49 -0800408 ShmEventLoop *event_loop, const Channel *channel,
Austin Schuh39788ff2019-12-01 18:22:57 -0800409 std::function<void(const Context &context, const void *message)> fn)
410 : aos::WatcherState(event_loop, channel, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800411 event_loop_(event_loop),
412 event_(this),
Austin Schuhaa79e4e2019-12-29 20:43:32 -0800413 simple_shm_fetcher_(event_loop, channel) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700414
Austin Schuh7d87b672019-12-01 20:23:49 -0800415 ~WatcherState() override { event_loop_->RemoveEvent(&event_); }
Austin Schuh39788ff2019-12-01 18:22:57 -0800416
417 void Startup(EventLoop *event_loop) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800418 simple_shm_fetcher_.PointAtNextQueueIndex();
Austin Schuh39788ff2019-12-01 18:22:57 -0800419 CHECK(RegisterWakeup(event_loop->priority()));
420 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700421
Alex Perrycb7da4b2019-08-28 19:35:56 -0700422 // Returns true if there is new data available.
Austin Schuh7d87b672019-12-01 20:23:49 -0800423 bool CheckForNewData() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700424 if (!has_new_data_) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800425 has_new_data_ = simple_shm_fetcher_.FetchNext();
Austin Schuh7d87b672019-12-01 20:23:49 -0800426
427 if (has_new_data_) {
428 event_.set_event_time(
Austin Schuhad154822019-12-27 15:45:13 -0800429 simple_shm_fetcher_.context().monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800430 event_loop_->AddEvent(&event_);
431 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700432 }
433
434 return has_new_data_;
435 }
436
Alex Perrycb7da4b2019-08-28 19:35:56 -0700437 // Consumes the data by calling the callback.
Austin Schuh7d87b672019-12-01 20:23:49 -0800438 void HandleEvent() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700439 CHECK(has_new_data_);
Austin Schuh39788ff2019-12-01 18:22:57 -0800440 DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700441 has_new_data_ = false;
Austin Schuh7d87b672019-12-01 20:23:49 -0800442 CheckForNewData();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700443 }
444
Austin Schuh39788ff2019-12-01 18:22:57 -0800445 // Registers us to receive a signal on event reception.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700446 bool RegisterWakeup(int priority) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800447 return simple_shm_fetcher_.RegisterWakeup(priority);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700448 }
449
Austin Schuh39788ff2019-12-01 18:22:57 -0800450 void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451
Brian Silverman5120afb2020-01-31 17:44:35 -0800452 absl::Span<char> GetSharedMemory() const {
453 return simple_shm_fetcher_.GetSharedMemory();
454 }
455
Alex Perrycb7da4b2019-08-28 19:35:56 -0700456 private:
457 bool has_new_data_ = false;
458
Austin Schuh7d87b672019-12-01 20:23:49 -0800459 ShmEventLoop *event_loop_;
460 EventHandler<WatcherState> event_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800461 SimpleShmFetcher simple_shm_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700462};
463
464// Adapter class to adapt a timerfd to a TimerHandler.
Austin Schuh7d87b672019-12-01 20:23:49 -0800465class TimerHandlerState final : public TimerHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 public:
467 TimerHandlerState(ShmEventLoop *shm_event_loop, ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800468 : TimerHandler(shm_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800469 shm_event_loop_(shm_event_loop),
470 event_(this) {
471 shm_event_loop_->epoll_.OnReadable(
472 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
Alex Perrycb7da4b2019-08-28 19:35:56 -0700473 }
474
Austin Schuh7d87b672019-12-01 20:23:49 -0800475 ~TimerHandlerState() {
476 Disable();
477 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
478 }
479
480 void HandleEvent() {
481 uint64_t elapsed_cycles = timerfd_.Read();
482 if (elapsed_cycles == 0u) {
483 // We got called before the timer interrupt could happen, but because we
484 // are checking the time, we got called on time. Push the timer out by 1
485 // cycle.
486 elapsed_cycles = 1u;
487 timerfd_.SetTime(base_ + repeat_offset_, repeat_offset_);
488 }
489
490 Call(monotonic_clock::now, base_);
491
492 base_ += repeat_offset_ * elapsed_cycles;
493
494 if (repeat_offset_ != chrono::seconds(0)) {
495 event_.set_event_time(base_);
496 shm_event_loop_->AddEvent(&event_);
497 }
498 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700499
500 void Setup(monotonic_clock::time_point base,
501 monotonic_clock::duration repeat_offset) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800502 if (event_.valid()) {
503 shm_event_loop_->RemoveEvent(&event_);
504 }
505
Alex Perrycb7da4b2019-08-28 19:35:56 -0700506 timerfd_.SetTime(base, repeat_offset);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800507 base_ = base;
508 repeat_offset_ = repeat_offset;
Austin Schuh7d87b672019-12-01 20:23:49 -0800509 event_.set_event_time(base_);
510 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700511 }
512
Austin Schuh7d87b672019-12-01 20:23:49 -0800513 void Disable() override {
514 shm_event_loop_->RemoveEvent(&event_);
515 timerfd_.Disable();
516 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700517
518 private:
519 ShmEventLoop *shm_event_loop_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800520 EventHandler<TimerHandlerState> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700521
522 TimerFd timerfd_;
523
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800524 monotonic_clock::time_point base_;
525 monotonic_clock::duration repeat_offset_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700526};
527
528// Adapter class to the timerfd and PhasedLoop.
Austin Schuh7d87b672019-12-01 20:23:49 -0800529class PhasedLoopHandler final : public ::aos::PhasedLoopHandler {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700530 public:
531 PhasedLoopHandler(ShmEventLoop *shm_event_loop, ::std::function<void(int)> fn,
532 const monotonic_clock::duration interval,
533 const monotonic_clock::duration offset)
Austin Schuh39788ff2019-12-01 18:22:57 -0800534 : aos::PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset),
Austin Schuh7d87b672019-12-01 20:23:49 -0800535 shm_event_loop_(shm_event_loop),
536 event_(this) {
537 shm_event_loop_->epoll_.OnReadable(
538 timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); });
539 }
540
541 void HandleEvent() {
542 // The return value for read is the number of cycles that have elapsed.
543 // Because we check to see when this event *should* have happened, there are
544 // cases where Read() will return 0, when 1 cycle has actually happened.
545 // This occurs when the timer interrupt hasn't triggered yet. Therefore,
546 // ignore it. Call handles rescheduling and calculating elapsed cycles
547 // without any extra help.
548 timerfd_.Read();
549 event_.Invalidate();
550
551 Call(monotonic_clock::now, [this](monotonic_clock::time_point sleep_time) {
552 Schedule(sleep_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700553 });
554 }
555
Austin Schuh39788ff2019-12-01 18:22:57 -0800556 ~PhasedLoopHandler() override {
557 shm_event_loop_->epoll_.DeleteFd(timerfd_.fd());
Austin Schuh7d87b672019-12-01 20:23:49 -0800558 shm_event_loop_->RemoveEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700559 }
560
561 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800562 // Reschedules the timer.
Austin Schuh39788ff2019-12-01 18:22:57 -0800563 void Schedule(monotonic_clock::time_point sleep_time) override {
Austin Schuh7d87b672019-12-01 20:23:49 -0800564 if (event_.valid()) {
565 shm_event_loop_->RemoveEvent(&event_);
566 }
567
Austin Schuh39788ff2019-12-01 18:22:57 -0800568 timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero());
Austin Schuh7d87b672019-12-01 20:23:49 -0800569 event_.set_event_time(sleep_time);
570 shm_event_loop_->AddEvent(&event_);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700571 }
572
573 ShmEventLoop *shm_event_loop_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800574 EventHandler<PhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700575
576 TimerFd timerfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700577};
578} // namespace internal
579
580::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher(
581 const Channel *channel) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800582 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
583 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
584 << "\", \"type\": \"" << channel->type()->string_view()
585 << "\" } is not able to be fetched on this node. Check your "
586 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800587 }
588
Austin Schuh39788ff2019-12-01 18:22:57 -0800589 return ::std::unique_ptr<RawFetcher>(new internal::ShmFetcher(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700590}
591
592::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender(
593 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800594 TakeSender(channel);
Austin Schuh39788ff2019-12-01 18:22:57 -0800595
596 return ::std::unique_ptr<RawSender>(new internal::ShmSender(this, channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700597}
598
599void ShmEventLoop::MakeRawWatcher(
600 const Channel *channel,
601 std::function<void(const Context &context, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800602 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800603
Austin Schuh39788ff2019-12-01 18:22:57 -0800604 NewWatcher(::std::unique_ptr<WatcherState>(
605 new internal::WatcherState(this, channel, std::move(watcher))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700606}
607
608TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800609 return NewTimer(::std::unique_ptr<TimerHandler>(
610 new internal::TimerHandlerState(this, ::std::move(callback))));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700611}
612
613PhasedLoopHandler *ShmEventLoop::AddPhasedLoop(
614 ::std::function<void(int)> callback,
615 const monotonic_clock::duration interval,
616 const monotonic_clock::duration offset) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800617 return NewPhasedLoop(
618 ::std::unique_ptr<PhasedLoopHandler>(new internal::PhasedLoopHandler(
619 this, ::std::move(callback), interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700620}
621
622void ShmEventLoop::OnRun(::std::function<void()> on_run) {
623 on_run_.push_back(::std::move(on_run));
624}
625
Austin Schuh7d87b672019-12-01 20:23:49 -0800626void ShmEventLoop::HandleEvent() {
627 // Update all the times for handlers.
628 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
629 internal::WatcherState *watcher =
630 reinterpret_cast<internal::WatcherState *>(base_watcher.get());
631
632 watcher->CheckForNewData();
633 }
634
Austin Schuh39788ff2019-12-01 18:22:57 -0800635 while (true) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800636 if (EventCount() == 0 ||
637 PeekEvent()->event_time() > monotonic_clock::now()) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800638 break;
639 }
640
Austin Schuh7d87b672019-12-01 20:23:49 -0800641 EventLoopEvent *event = PopEvent();
642 event->HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -0800643 }
644}
645
Austin Schuh32fd5a72019-12-01 22:20:26 -0800646// RAII class to mask signals.
647class ScopedSignalMask {
648 public:
649 ScopedSignalMask(std::initializer_list<int> signals) {
650 sigset_t sigset;
651 PCHECK(sigemptyset(&sigset) == 0);
652 for (int signal : signals) {
653 PCHECK(sigaddset(&sigset, signal) == 0);
654 }
655
656 PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0);
657 }
658
659 ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); }
660
661 private:
662 sigset_t old_;
663};
664
665// Class to manage the static state associated with killing multiple event
666// loops.
667class SignalHandler {
668 public:
669 // Gets the singleton.
670 static SignalHandler *global() {
671 static SignalHandler loop;
672 return &loop;
673 }
674
675 // Handles the signal with the singleton.
676 static void HandleSignal(int) { global()->DoHandleSignal(); }
677
678 // Registers an event loop to receive Exit() calls.
679 void Register(ShmEventLoop *event_loop) {
680 // Block signals while we have the mutex so we never race with the signal
681 // handler.
682 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
683 std::unique_lock<stl_mutex> locker(mutex_);
684 if (event_loops_.size() == 0) {
685 // The first caller registers the signal handler.
686 struct sigaction new_action;
687 sigemptyset(&new_action.sa_mask);
688 // This makes it so that 2 control c's to a stuck process will kill it by
689 // restoring the original signal handler.
690 new_action.sa_flags = SA_RESETHAND;
691 new_action.sa_handler = &HandleSignal;
692
693 PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0);
694 PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0);
695 PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0);
696 }
697
698 event_loops_.push_back(event_loop);
699 }
700
701 // Unregisters an event loop to receive Exit() calls.
702 void Unregister(ShmEventLoop *event_loop) {
703 // Block signals while we have the mutex so we never race with the signal
704 // handler.
705 ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM});
706 std::unique_lock<stl_mutex> locker(mutex_);
707
Brian Silverman5120afb2020-01-31 17:44:35 -0800708 event_loops_.erase(
709 std::find(event_loops_.begin(), event_loops_.end(), event_loop));
Austin Schuh32fd5a72019-12-01 22:20:26 -0800710
711 if (event_loops_.size() == 0u) {
712 // The last caller restores the original signal handlers.
713 PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0);
714 PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0);
715 PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0);
716 }
717 }
718
719 private:
720 void DoHandleSignal() {
721 // We block signals while grabbing the lock, so there should never be a
722 // race. Confirm that this is true using trylock.
723 CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while "
724 "modifing the event loop list.";
725 for (ShmEventLoop *event_loop : event_loops_) {
726 event_loop->Exit();
727 }
728 mutex_.unlock();
729 }
730
731 // Mutex to protect all state.
732 stl_mutex mutex_;
733 std::vector<ShmEventLoop *> event_loops_;
734 struct sigaction old_action_int_;
735 struct sigaction old_action_hup_;
736 struct sigaction old_action_term_;
737};
738
Alex Perrycb7da4b2019-08-28 19:35:56 -0700739void ShmEventLoop::Run() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800740 SignalHandler::global()->Register(this);
Austin Schuh39788ff2019-12-01 18:22:57 -0800741
Alex Perrycb7da4b2019-08-28 19:35:56 -0700742 std::unique_ptr<ipc_lib::SignalFd> signalfd;
743
744 if (watchers_.size() > 0) {
745 signalfd.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal}));
746
747 epoll_.OnReadable(signalfd->fd(), [signalfd_ptr = signalfd.get(), this]() {
748 signalfd_siginfo result = signalfd_ptr->Read();
749 CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal);
750
751 // TODO(austin): We should really be checking *everything*, not just
752 // watchers, and calling the oldest thing first. That will improve
753 // determinism a lot.
754
Austin Schuh7d87b672019-12-01 20:23:49 -0800755 HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700756 });
757 }
758
Austin Schuh39788ff2019-12-01 18:22:57 -0800759 MaybeScheduleTimingReports();
760
Austin Schuh7d87b672019-12-01 20:23:49 -0800761 ReserveEvents();
762
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800763 aos::SetCurrentThreadName(name_.substr(0, 16));
Austin Schuh39788ff2019-12-01 18:22:57 -0800764 // Now, all the callbacks are setup. Lock everything into memory and go RT.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700765 if (priority_ != 0) {
766 ::aos::InitRT();
767
768 LOG(INFO) << "Setting priority to " << priority_;
769 ::aos::SetCurrentThreadRealtimePriority(priority_);
770 }
771
772 set_is_running(true);
773
774 // Now that we are realtime (but before the OnRun handlers run), snap the
775 // queue index.
Austin Schuh39788ff2019-12-01 18:22:57 -0800776 for (::std::unique_ptr<WatcherState> &watcher : watchers_) {
777 watcher->Startup(this);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700778 }
779
780 // Now that we are RT, run all the OnRun handlers.
781 for (const auto &run : on_run_) {
782 run();
783 }
784
Alex Perrycb7da4b2019-08-28 19:35:56 -0700785 // And start our main event loop which runs all the timers and handles Quit.
786 epoll_.Run();
787
788 // Once epoll exits, there is no useful nonrt work left to do.
789 set_is_running(false);
790
791 // Nothing time or synchronization critical needs to happen after this point.
792 // Drop RT priority.
793 ::aos::UnsetCurrentThreadRealtimePriority();
794
Austin Schuh39788ff2019-12-01 18:22:57 -0800795 for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) {
796 internal::WatcherState *watcher =
797 reinterpret_cast<internal::WatcherState *>(base_watcher.get());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700798 watcher->UnregisterWakeup();
799 }
800
801 if (watchers_.size() > 0) {
802 epoll_.DeleteFd(signalfd->fd());
803 signalfd.reset();
804 }
Austin Schuh32fd5a72019-12-01 22:20:26 -0800805
806 SignalHandler::global()->Unregister(this);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800807
808 // Trigger any remaining senders or fetchers to be cleared before destroying
809 // the event loop so the book keeping matches. Do this in the thread that
810 // created the timing reporter.
811 timing_report_sender_.reset();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700812}
813
814void ShmEventLoop::Exit() { epoll_.Quit(); }
815
816ShmEventLoop::~ShmEventLoop() {
Austin Schuh39788ff2019-12-01 18:22:57 -0800817 // Force everything with a registered fd with epoll to be destroyed now.
818 timers_.clear();
819 phased_loops_.clear();
820 watchers_.clear();
821
Alex Perrycb7da4b2019-08-28 19:35:56 -0700822 CHECK(!is_running()) << ": ShmEventLoop destroyed while running";
823}
824
Alex Perrycb7da4b2019-08-28 19:35:56 -0700825void ShmEventLoop::SetRuntimeRealtimePriority(int priority) {
826 if (is_running()) {
827 LOG(FATAL) << "Cannot set realtime priority while running.";
828 }
829 priority_ = priority;
830}
831
James Kuszmaul57c2baa2020-01-19 14:52:52 -0800832void ShmEventLoop::set_name(const std::string_view name) {
833 name_ = std::string(name);
834 UpdateTimingReport();
835}
836
Brian Silverman5120afb2020-01-31 17:44:35 -0800837absl::Span<char> ShmEventLoop::GetWatcherSharedMemory(const Channel *channel) {
838 internal::WatcherState *const watcher_state =
839 static_cast<internal::WatcherState *>(GetWatcherState(channel));
840 return watcher_state->GetSharedMemory();
841}
842
843absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory(
844 const aos::RawSender *sender) const {
845 return static_cast<const internal::ShmSender *>(sender)->GetSharedMemory();
846}
847
Austin Schuh39788ff2019-12-01 18:22:57 -0800848pid_t ShmEventLoop::GetTid() { return syscall(SYS_gettid); }
849
Alex Perrycb7da4b2019-08-28 19:35:56 -0700850} // namespace aos