Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/shm_event_loop.h" |
| 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #include <sys/stat.h> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 4 | #include <sys/syscall.h> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include <sys/types.h> |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <atomic> |
| 9 | #include <chrono> |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 10 | #include <iterator> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include <stdexcept> |
| 12 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 13 | #include "glog/logging.h" |
| 14 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 15 | #include "aos/events/aos_logging.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | #include "aos/events/epoll.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 17 | #include "aos/events/event_loop_generated.h" |
| 18 | #include "aos/events/timing_statistics.h" |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 19 | #include "aos/init.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | #include "aos/ipc_lib/lockless_queue.h" |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 21 | #include "aos/ipc_lib/memory_mapped_queue.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 22 | #include "aos/realtime.h" |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 23 | #include "aos/stl_mutex/stl_mutex.h" |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 24 | #include "aos/util/file.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | #include "aos/util/phased_loop.h" |
| 26 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | // Returns the portion of the path after the last /. This very much assumes |
| 30 | // that the application name is null terminated. |
| 31 | const char *Filename(const char *path) { |
| 32 | const std::string_view path_string_view = path; |
| 33 | auto last_slash_pos = path_string_view.find_last_of("/"); |
| 34 | |
| 35 | return last_slash_pos == std::string_view::npos ? path |
| 36 | : path + last_slash_pos + 1; |
| 37 | } |
| 38 | |
| 39 | } // namespace |
| 40 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | DEFINE_string(shm_base, "/dev/shm/aos", |
| 42 | "Directory to place queue backing mmaped files in."); |
Brennan Coslett | 6fd3c00 | 2023-07-11 17:41:09 -0500 | [diff] [blame] | 43 | // This value is affected by the umask of the process which is calling it |
| 44 | // and is set to the user's value by default (check yours running `umask` on |
| 45 | // the command line). |
| 46 | // Any file mode requested is transformed using: mode & ~umask and the default |
| 47 | // umask is 0022 (allow any permissions for the user, dont allow writes for |
| 48 | // groups or others). |
| 49 | // See https://man7.org/linux/man-pages/man2/umask.2.html for more details. |
| 50 | // WITH THE DEFAULT UMASK YOU WONT ACTUALLY GET THESE PERMISSIONS :) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 51 | DEFINE_uint32(permissions, 0770, |
Brennan Coslett | 6fd3c00 | 2023-07-11 17:41:09 -0500 | [diff] [blame] | 52 | "Permissions to make shared memory files and folders, " |
Brennan Coslett | d5077bc | 2023-07-13 08:49:35 -0500 | [diff] [blame] | 53 | "affected by the process's umask. " |
Brennan Coslett | 6fd3c00 | 2023-07-11 17:41:09 -0500 | [diff] [blame] | 54 | "See shm_event_loop.cc for more details."); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 55 | DEFINE_string(application_name, Filename(program_invocation_name), |
| 56 | "The application name"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 57 | |
| 58 | namespace aos { |
| 59 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 60 | using namespace shm_event_loop_internal; |
| 61 | |
Austin Schuh | cdab619 | 2019-12-29 17:47:46 -0800 | [diff] [blame] | 62 | void SetShmBase(const std::string_view base) { |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 63 | FLAGS_shm_base = std::string(base) + "/aos"; |
Austin Schuh | cdab619 | 2019-12-29 17:47:46 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 66 | namespace { |
| 67 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 68 | const Node *MaybeMyNode(const Configuration *configuration) { |
| 69 | if (!configuration->has_nodes()) { |
| 70 | return nullptr; |
| 71 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 72 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 73 | return configuration::GetMyNode(configuration); |
| 74 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 75 | |
Philipp Schrader | a873466 | 2023-08-06 14:49:39 -0700 | [diff] [blame] | 76 | void IgnoreWakeupSignal() { |
| 77 | struct sigaction action; |
| 78 | action.sa_handler = SIG_IGN; |
| 79 | PCHECK(sigemptyset(&action.sa_mask) == 0); |
| 80 | action.sa_flags = 0; |
| 81 | PCHECK(sigaction(ipc_lib::kWakeupSignal, &action, nullptr) == 0); |
| 82 | } |
| 83 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 84 | } // namespace |
| 85 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 86 | ShmEventLoop::ShmEventLoop(const Configuration *configuration) |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 87 | : EventLoop(configuration), |
| 88 | boot_uuid_(UUID::BootUUID()), |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 89 | shm_base_(FLAGS_shm_base), |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 90 | name_(FLAGS_application_name), |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 91 | node_(MaybeMyNode(configuration)) { |
Philipp Schrader | a873466 | 2023-08-06 14:49:39 -0700 | [diff] [blame] | 92 | // Ignore the wakeup signal by default. Otherwise, we have race conditions on |
| 93 | // shutdown where a wakeup signal will uncleanly terminate the process. |
| 94 | // See LocklessQueueWakeUpper::Wakeup() for some more information. |
| 95 | IgnoreWakeupSignal(); |
| 96 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 97 | CHECK(IsInitialized()) << ": Need to initialize AOS first."; |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 98 | ClearContext(); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 99 | if (configuration->has_nodes()) { |
| 100 | CHECK(node_ != nullptr) << ": Couldn't find node in config."; |
| 101 | } |
| 102 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 103 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 104 | namespace shm_event_loop_internal { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 105 | |
| 106 | class SimpleShmFetcher { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 107 | public: |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 108 | explicit SimpleShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop, |
| 109 | const Channel *channel) |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 110 | : event_loop_(event_loop), |
| 111 | channel_(channel), |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 112 | lockless_queue_memory_(shm_base, FLAGS_permissions, |
| 113 | event_loop->configuration(), channel), |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 114 | reader_(lockless_queue_memory_.queue()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 115 | context_.data = nullptr; |
| 116 | // Point the queue index at the next index to read starting now. This |
| 117 | // makes it such that FetchNext will read the next message sent after |
| 118 | // the fetcher is created. |
| 119 | PointAtNextQueueIndex(); |
| 120 | } |
| 121 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 122 | ~SimpleShmFetcher() {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 123 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 124 | // Sets this object to pin or copy data, as configured in the channel. |
| 125 | void RetrieveData() { |
| 126 | if (channel_->read_method() == ReadMethod::PIN) { |
| 127 | PinDataOnFetch(); |
| 128 | } else { |
| 129 | CopyDataOnFetch(); |
| 130 | } |
| 131 | } |
| 132 | |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 133 | // Sets this object to copy data out of the shared memory into a private |
| 134 | // buffer when fetching. |
| 135 | void CopyDataOnFetch() { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 136 | CHECK(!pin_data()); |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 137 | data_storage_.reset(static_cast<char *>( |
| 138 | malloc(channel_->max_size() + kChannelDataAlignment - 1))); |
| 139 | } |
| 140 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 141 | // Sets this object to pin data in shared memory when fetching. |
| 142 | void PinDataOnFetch() { |
| 143 | CHECK(!copy_data()); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 144 | auto maybe_pinner = |
| 145 | ipc_lib::LocklessQueuePinner::Make(lockless_queue_memory_.queue()); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 146 | if (!maybe_pinner) { |
| 147 | LOG(FATAL) << "Failed to create reader on " |
| 148 | << configuration::CleanedChannelToString(channel_) |
| 149 | << ", too many readers."; |
| 150 | } |
| 151 | pinner_ = std::move(maybe_pinner.value()); |
| 152 | } |
| 153 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | // Points the next message to fetch at the queue index which will be |
| 155 | // populated next. |
| 156 | void PointAtNextQueueIndex() { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 157 | actual_queue_index_ = reader_.LatestIndex(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 158 | if (!actual_queue_index_.valid()) { |
| 159 | // Nothing in the queue. The next element will show up at the 0th |
| 160 | // index in the queue. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 161 | actual_queue_index_ = ipc_lib::QueueIndex::Zero( |
| 162 | LocklessQueueSize(lockless_queue_memory_.memory())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | } else { |
| 164 | actual_queue_index_ = actual_queue_index_.Increment(); |
| 165 | } |
| 166 | } |
| 167 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 168 | bool FetchNext() { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 169 | const ipc_lib::LocklessQueueReader::Result read_result = |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 170 | DoFetch(actual_queue_index_); |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 171 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 172 | return read_result == ipc_lib::LocklessQueueReader::Result::GOOD; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 175 | bool Fetch() { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 176 | const ipc_lib::QueueIndex queue_index = reader_.LatestIndex(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 177 | // actual_queue_index_ is only meaningful if it was set by Fetch or |
| 178 | // FetchNext. This happens when valid_data_ has been set. So, only |
| 179 | // skip checking if valid_data_ is true. |
| 180 | // |
| 181 | // Also, if the latest queue index is invalid, we are empty. So there |
| 182 | // is nothing to fetch. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 183 | if ((context_.data != nullptr && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 184 | queue_index == actual_queue_index_.DecrementBy(1u)) || |
| 185 | !queue_index.valid()) { |
| 186 | return false; |
| 187 | } |
| 188 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 189 | const ipc_lib::LocklessQueueReader::Result read_result = |
| 190 | DoFetch(queue_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 192 | CHECK(read_result != ipc_lib::LocklessQueueReader::Result::NOTHING_NEW) |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 193 | << ": Queue index went backwards. This should never happen. " |
| 194 | << configuration::CleanedChannelToString(channel_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 195 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 196 | return read_result == ipc_lib::LocklessQueueReader::Result::GOOD; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 199 | Context context() const { return context_; } |
| 200 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 201 | bool RegisterWakeup(int priority) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 202 | CHECK(!watcher_); |
| 203 | watcher_ = ipc_lib::LocklessQueueWatcher::Make( |
| 204 | lockless_queue_memory_.queue(), priority); |
| 205 | return static_cast<bool>(watcher_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 208 | void UnregisterWakeup() { |
| 209 | CHECK(watcher_); |
| 210 | watcher_ = std::nullopt; |
| 211 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 212 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 213 | absl::Span<char> GetMutableSharedMemory() { |
| 214 | return lockless_queue_memory_.GetMutableSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 217 | absl::Span<const char> GetConstSharedMemory() const { |
| 218 | return lockless_queue_memory_.GetConstSharedMemory(); |
| 219 | } |
| 220 | |
| 221 | absl::Span<const char> GetPrivateMemory() const { |
| 222 | if (pin_data()) { |
| 223 | return lockless_queue_memory_.GetConstSharedMemory(); |
| 224 | } |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 225 | return absl::Span<char>( |
| 226 | const_cast<SimpleShmFetcher *>(this)->data_storage_start(), |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 227 | LocklessQueueMessageDataSize(lockless_queue_memory_.memory())); |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 230 | private: |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 231 | ipc_lib::LocklessQueueReader::Result DoFetch( |
| 232 | ipc_lib::QueueIndex queue_index) { |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 233 | // TODO(austin): Get behind and make sure it dies. |
| 234 | char *copy_buffer = nullptr; |
| 235 | if (copy_data()) { |
| 236 | copy_buffer = data_storage_start(); |
| 237 | } |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 238 | ipc_lib::LocklessQueueReader::Result read_result = reader_.Read( |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 239 | queue_index.index(), &context_.monotonic_event_time, |
| 240 | &context_.realtime_event_time, &context_.monotonic_remote_time, |
| 241 | &context_.realtime_remote_time, &context_.remote_queue_index, |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 242 | &context_.source_boot_uuid, &context_.size, copy_buffer, |
| 243 | std::ref(should_fetch_)); |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 244 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 245 | if (read_result == ipc_lib::LocklessQueueReader::Result::GOOD) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 246 | if (pin_data()) { |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 247 | const int pin_result = pinner_->PinIndex(queue_index.index()); |
| 248 | CHECK(pin_result >= 0) |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 249 | << ": Got behind while reading and the last message was modified " |
| 250 | "out from under us while we tried to pin it. Don't get so far " |
| 251 | "behind on: " |
| 252 | << configuration::CleanedChannelToString(channel_); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 253 | context_.buffer_index = pin_result; |
| 254 | } else { |
| 255 | context_.buffer_index = -1; |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 258 | context_.queue_index = queue_index.index(); |
| 259 | if (context_.remote_queue_index == 0xffffffffu) { |
| 260 | context_.remote_queue_index = context_.queue_index; |
| 261 | } |
| 262 | if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) { |
| 263 | context_.monotonic_remote_time = context_.monotonic_event_time; |
| 264 | } |
| 265 | if (context_.realtime_remote_time == aos::realtime_clock::min_time) { |
| 266 | context_.realtime_remote_time = context_.realtime_event_time; |
| 267 | } |
| 268 | const char *const data = DataBuffer(); |
| 269 | if (data) { |
| 270 | context_.data = |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 271 | data + |
| 272 | LocklessQueueMessageDataSize(lockless_queue_memory_.memory()) - |
| 273 | context_.size; |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 274 | } else { |
| 275 | context_.data = nullptr; |
| 276 | } |
| 277 | actual_queue_index_ = queue_index.Increment(); |
| 278 | } |
| 279 | |
| 280 | // Make sure the data wasn't modified while we were reading it. This |
| 281 | // can only happen if you are reading the last message *while* it is |
| 282 | // being written to, which means you are pretty far behind. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 283 | CHECK(read_result != ipc_lib::LocklessQueueReader::Result::OVERWROTE) |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 284 | << ": Got behind while reading and the last message was modified " |
| 285 | "out from under us while we were reading it. Don't get so far " |
| 286 | "behind on: " |
| 287 | << configuration::CleanedChannelToString(channel_); |
| 288 | |
| 289 | // We fell behind between when we read the index and read the value. |
| 290 | // This isn't worth recovering from since this means we went to sleep |
| 291 | // for a long time in the middle of this function. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 292 | if (read_result == ipc_lib::LocklessQueueReader::Result::TOO_OLD) { |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 293 | event_loop_->SendTimingReport(); |
| 294 | LOG(FATAL) << "The next message is no longer available. " |
| 295 | << configuration::CleanedChannelToString(channel_); |
| 296 | } |
| 297 | |
| 298 | return read_result; |
| 299 | } |
| 300 | |
| 301 | char *data_storage_start() const { |
| 302 | CHECK(copy_data()); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 303 | return RoundChannelData(data_storage_.get(), channel_->max_size()); |
| 304 | } |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 305 | |
| 306 | // Note that for some modes the return value will change as new messages are |
| 307 | // read. |
| 308 | const char *DataBuffer() const { |
| 309 | if (copy_data()) { |
| 310 | return data_storage_start(); |
| 311 | } |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 312 | if (pin_data()) { |
| 313 | return static_cast<const char *>(pinner_->Data()); |
| 314 | } |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 315 | return nullptr; |
| 316 | } |
| 317 | |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 318 | bool copy_data() const { return static_cast<bool>(data_storage_); } |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 319 | bool pin_data() const { return static_cast<bool>(pinner_); } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 320 | |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 321 | aos::ShmEventLoop *event_loop_; |
Austin Schuh | f565259 | 2019-12-29 16:26:15 -0800 | [diff] [blame] | 322 | const Channel *const channel_; |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 323 | ipc_lib::MemoryMappedQueue lockless_queue_memory_; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 324 | ipc_lib::LocklessQueueReader reader_; |
| 325 | // This being nullopt indicates we're not looking for wakeups right now. |
| 326 | std::optional<ipc_lib::LocklessQueueWatcher> watcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 327 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 328 | ipc_lib::QueueIndex actual_queue_index_ = ipc_lib::QueueIndex::Invalid(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 329 | |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 330 | // This being empty indicates we're not going to copy data. |
| 331 | std::unique_ptr<char, decltype(&free)> data_storage_{nullptr, &free}; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 332 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 333 | // This being nullopt indicates we're not going to pin messages. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 334 | std::optional<ipc_lib::LocklessQueuePinner> pinner_; |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 335 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 336 | Context context_; |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 337 | |
| 338 | // Pre-allocated should_fetch function so we don't allocate. |
| 339 | std::function<bool(const Context &)> should_fetch_ = [](const Context &) { |
| 340 | return true; |
| 341 | }; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | class ShmFetcher : public RawFetcher { |
| 345 | public: |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 346 | explicit ShmFetcher(std::string_view shm_base, ShmEventLoop *event_loop, |
| 347 | const Channel *channel) |
Austin Schuh | aa79e4e | 2019-12-29 20:43:32 -0800 | [diff] [blame] | 348 | : RawFetcher(event_loop, channel), |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 349 | simple_shm_fetcher_(shm_base, event_loop, channel) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 350 | simple_shm_fetcher_.RetrieveData(); |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 351 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 352 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 353 | ~ShmFetcher() override { |
| 354 | shm_event_loop()->CheckCurrentThread(); |
| 355 | context_.data = nullptr; |
| 356 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 357 | |
| 358 | std::pair<bool, monotonic_clock::time_point> DoFetchNext() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 359 | shm_event_loop()->CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 360 | if (simple_shm_fetcher_.FetchNext()) { |
| 361 | context_ = simple_shm_fetcher_.context(); |
| 362 | return std::make_pair(true, monotonic_clock::now()); |
| 363 | } |
| 364 | return std::make_pair(false, monotonic_clock::min_time); |
| 365 | } |
| 366 | |
| 367 | std::pair<bool, monotonic_clock::time_point> DoFetch() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 368 | shm_event_loop()->CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 369 | if (simple_shm_fetcher_.Fetch()) { |
| 370 | context_ = simple_shm_fetcher_.context(); |
| 371 | return std::make_pair(true, monotonic_clock::now()); |
| 372 | } |
| 373 | return std::make_pair(false, monotonic_clock::min_time); |
| 374 | } |
| 375 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 376 | absl::Span<const char> GetPrivateMemory() const { |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 377 | return simple_shm_fetcher_.GetPrivateMemory(); |
| 378 | } |
| 379 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 380 | private: |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 381 | const ShmEventLoop *shm_event_loop() const { |
| 382 | return static_cast<const ShmEventLoop *>(event_loop()); |
| 383 | } |
| 384 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 385 | SimpleShmFetcher simple_shm_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 388 | class ShmExitHandle : public ExitHandle { |
| 389 | public: |
| 390 | ShmExitHandle(ShmEventLoop *event_loop) : event_loop_(event_loop) { |
| 391 | ++event_loop_->exit_handle_count_; |
| 392 | } |
| 393 | ~ShmExitHandle() override { |
| 394 | CHECK_GT(event_loop_->exit_handle_count_, 0); |
| 395 | --event_loop_->exit_handle_count_; |
| 396 | } |
| 397 | |
| 398 | void Exit() override { event_loop_->Exit(); } |
| 399 | |
| 400 | private: |
| 401 | ShmEventLoop *const event_loop_; |
| 402 | }; |
| 403 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 404 | class ShmSender : public RawSender { |
| 405 | public: |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 406 | explicit ShmSender(std::string_view shm_base, EventLoop *event_loop, |
| 407 | const Channel *channel) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 408 | : RawSender(event_loop, channel), |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 409 | lockless_queue_memory_(shm_base, FLAGS_permissions, |
| 410 | event_loop->configuration(), channel), |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 411 | lockless_queue_sender_( |
| 412 | VerifySender(ipc_lib::LocklessQueueSender::Make( |
| 413 | lockless_queue_memory_.queue(), |
| 414 | configuration::ChannelStorageDuration( |
| 415 | event_loop->configuration(), channel)), |
| 416 | channel)), |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 417 | wake_upper_(lockless_queue_memory_.queue()) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 418 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 419 | ~ShmSender() override { shm_event_loop()->CheckCurrentThread(); } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 420 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 421 | static ipc_lib::LocklessQueueSender VerifySender( |
| 422 | std::optional<ipc_lib::LocklessQueueSender> sender, |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 423 | const Channel *channel) { |
| 424 | if (sender) { |
| 425 | return std::move(sender.value()); |
| 426 | } |
| 427 | LOG(FATAL) << "Failed to create sender on " |
| 428 | << configuration::CleanedChannelToString(channel) |
| 429 | << ", too many senders."; |
| 430 | } |
| 431 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 432 | void *data() override { |
| 433 | shm_event_loop()->CheckCurrentThread(); |
| 434 | return lockless_queue_sender_.Data(); |
| 435 | } |
| 436 | size_t size() override { |
| 437 | shm_event_loop()->CheckCurrentThread(); |
| 438 | return lockless_queue_sender_.size(); |
| 439 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 440 | |
| 441 | Error DoSend(size_t length, |
| 442 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 443 | aos::realtime_clock::time_point realtime_remote_time, |
| 444 | uint32_t remote_queue_index, |
| 445 | const UUID &source_boot_uuid) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 446 | shm_event_loop()->CheckCurrentThread(); |
Austin Schuh | 0f7ed46 | 2020-03-28 20:38:34 -0700 | [diff] [blame] | 447 | CHECK_LE(length, static_cast<size_t>(channel()->max_size())) |
| 448 | << ": Sent too big a message on " |
| 449 | << configuration::CleanedChannelToString(channel()); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 450 | const auto result = lockless_queue_sender_.Send( |
| 451 | length, monotonic_remote_time, realtime_remote_time, remote_queue_index, |
| 452 | source_boot_uuid, &monotonic_sent_time_, &realtime_sent_time_, |
| 453 | &sent_queue_index_); |
| 454 | CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE) |
Austin Schuh | 91ba639 | 2020-10-03 13:27:47 -0700 | [diff] [blame] | 455 | << ": Somebody wrote outside the buffer of their message on channel " |
| 456 | << configuration::CleanedChannelToString(channel()); |
| 457 | |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 458 | wake_upper_.Wakeup(event_loop()->is_running() |
| 459 | ? event_loop()->runtime_realtime_priority() |
| 460 | : 0); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 461 | return CheckLocklessQueueResult(result); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 462 | } |
| 463 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 464 | Error DoSend(const void *msg, size_t length, |
| 465 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 466 | aos::realtime_clock::time_point realtime_remote_time, |
| 467 | uint32_t remote_queue_index, |
| 468 | const UUID &source_boot_uuid) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 469 | shm_event_loop()->CheckCurrentThread(); |
Austin Schuh | 0f7ed46 | 2020-03-28 20:38:34 -0700 | [diff] [blame] | 470 | CHECK_LE(length, static_cast<size_t>(channel()->max_size())) |
| 471 | << ": Sent too big a message on " |
| 472 | << configuration::CleanedChannelToString(channel()); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 473 | const auto result = lockless_queue_sender_.Send( |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 474 | reinterpret_cast<const char *>(msg), length, monotonic_remote_time, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 475 | realtime_remote_time, remote_queue_index, source_boot_uuid, |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 476 | &monotonic_sent_time_, &realtime_sent_time_, &sent_queue_index_); |
| 477 | |
| 478 | CHECK_NE(result, ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE) |
| 479 | << ": Somebody wrote outside the buffer of their message on " |
| 480 | "channel " |
Austin Schuh | 91ba639 | 2020-10-03 13:27:47 -0700 | [diff] [blame] | 481 | << configuration::CleanedChannelToString(channel()); |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 482 | wake_upper_.Wakeup(event_loop()->is_running() |
| 483 | ? event_loop()->runtime_realtime_priority() |
| 484 | : 0); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 485 | |
| 486 | return CheckLocklessQueueResult(result); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 489 | absl::Span<char> GetSharedMemory() const { |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 490 | return lockless_queue_memory_.GetMutableSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 493 | int buffer_index() override { |
| 494 | shm_event_loop()->CheckCurrentThread(); |
| 495 | return lockless_queue_sender_.buffer_index(); |
| 496 | } |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 497 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 498 | private: |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 499 | const ShmEventLoop *shm_event_loop() const { |
| 500 | return static_cast<const ShmEventLoop *>(event_loop()); |
| 501 | } |
| 502 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 503 | RawSender::Error CheckLocklessQueueResult( |
| 504 | const ipc_lib::LocklessQueueSender::Result &result) { |
| 505 | switch (result) { |
| 506 | case ipc_lib::LocklessQueueSender::Result::GOOD: |
| 507 | return Error::kOk; |
| 508 | case ipc_lib::LocklessQueueSender::Result::MESSAGES_SENT_TOO_FAST: |
| 509 | return Error::kMessagesSentTooFast; |
| 510 | case ipc_lib::LocklessQueueSender::Result::INVALID_REDZONE: |
| 511 | return Error::kInvalidRedzone; |
| 512 | } |
| 513 | LOG(FATAL) << "Unknown lockless queue sender result" |
| 514 | << static_cast<int>(result); |
| 515 | } |
| 516 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 517 | ipc_lib::MemoryMappedQueue lockless_queue_memory_; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 518 | ipc_lib::LocklessQueueSender lockless_queue_sender_; |
| 519 | ipc_lib::LocklessQueueWakeUpper wake_upper_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 520 | }; |
| 521 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 522 | // Class to manage the state for a Watcher. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 523 | class ShmWatcherState : public WatcherState { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 524 | public: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 525 | ShmWatcherState( |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 526 | std::string_view shm_base, ShmEventLoop *event_loop, |
| 527 | const Channel *channel, |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 528 | std::function<void(const Context &context, const void *message)> fn, |
| 529 | bool copy_data) |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 530 | : WatcherState(event_loop, channel, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 531 | event_loop_(event_loop), |
| 532 | event_(this), |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 533 | simple_shm_fetcher_(shm_base, event_loop, channel) { |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 534 | if (copy_data) { |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 535 | simple_shm_fetcher_.RetrieveData(); |
Brian Silverman | 3bca532 | 2020-08-12 19:35:29 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 538 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 539 | ~ShmWatcherState() override { |
| 540 | event_loop_->CheckCurrentThread(); |
| 541 | event_loop_->RemoveEvent(&event_); |
| 542 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 543 | |
| 544 | void Startup(EventLoop *event_loop) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 545 | event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 546 | simple_shm_fetcher_.PointAtNextQueueIndex(); |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 547 | CHECK(RegisterWakeup(event_loop->runtime_realtime_priority())); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 548 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 549 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 550 | // Returns true if there is new data available. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 551 | bool CheckForNewData() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 552 | if (!has_new_data_) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 553 | has_new_data_ = simple_shm_fetcher_.FetchNext(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 554 | |
| 555 | if (has_new_data_) { |
| 556 | event_.set_event_time( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 557 | simple_shm_fetcher_.context().monotonic_event_time); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 558 | event_loop_->AddEvent(&event_); |
| 559 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | return has_new_data_; |
| 563 | } |
| 564 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 565 | // Consumes the data by calling the callback. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 566 | void HandleEvent() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 567 | CHECK(has_new_data_); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 568 | DoCallCallback(monotonic_clock::now, simple_shm_fetcher_.context()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 569 | has_new_data_ = false; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 570 | CheckForNewData(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 573 | // Registers us to receive a signal on event reception. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 574 | bool RegisterWakeup(int priority) { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 575 | return simple_shm_fetcher_.RegisterWakeup(priority); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 578 | void UnregisterWakeup() { return simple_shm_fetcher_.UnregisterWakeup(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 579 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 580 | absl::Span<const char> GetSharedMemory() const { |
| 581 | return simple_shm_fetcher_.GetConstSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 584 | private: |
| 585 | bool has_new_data_ = false; |
| 586 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 587 | ShmEventLoop *event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 588 | EventHandler<ShmWatcherState> event_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 589 | SimpleShmFetcher simple_shm_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | // Adapter class to adapt a timerfd to a TimerHandler. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 593 | class ShmTimerHandler final : public TimerHandler { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 594 | public: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 595 | ShmTimerHandler(ShmEventLoop *shm_event_loop, ::std::function<void()> fn) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 596 | : TimerHandler(shm_event_loop, std::move(fn)), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 597 | shm_event_loop_(shm_event_loop), |
| 598 | event_(this) { |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 599 | shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 600 | // The timer may fire spuriously. HandleEvent on the event loop will |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 601 | // call the callback if it is needed. It may also have called it when |
| 602 | // processing some other event, and the kernel decided to deliver this |
| 603 | // wakeup anyways. |
| 604 | timerfd_.Read(); |
| 605 | shm_event_loop_->HandleEvent(); |
| 606 | }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 609 | ~ShmTimerHandler() { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 610 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 611 | Disable(); |
| 612 | shm_event_loop_->epoll_.DeleteFd(timerfd_.fd()); |
| 613 | } |
| 614 | |
| 615 | void HandleEvent() { |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 616 | CHECK(!event_.valid()); |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 617 | disabled_ = false; |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 618 | const auto monotonic_now = Call(monotonic_clock::now, base_); |
| 619 | if (event_.valid()) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 620 | // If someone called Schedule inside Call, rescheduling is already taken |
| 621 | // care of. Bail. |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 622 | return; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 623 | } |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 624 | if (disabled_) { |
| 625 | // Somebody called Disable inside Call, so we don't want to reschedule. |
| 626 | // Bail. |
| 627 | return; |
| 628 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 629 | |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 630 | if (repeat_offset_ == std::chrono::seconds(0)) { |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 631 | timerfd_.Disable(); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 632 | disabled_ = true; |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 633 | } else { |
| 634 | // Compute how many cycles have elapsed and schedule the next iteration |
| 635 | // for the next iteration in the future. |
| 636 | const int elapsed_cycles = |
| 637 | std::max<int>(0, (monotonic_now - base_ + repeat_offset_ - |
| 638 | std::chrono::nanoseconds(1)) / |
| 639 | repeat_offset_); |
| 640 | base_ += repeat_offset_ * elapsed_cycles; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 641 | |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 642 | // Update the heap and schedule the timerfd wakeup. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 643 | event_.set_event_time(base_); |
| 644 | shm_event_loop_->AddEvent(&event_); |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 645 | timerfd_.SetTime(base_, std::chrono::seconds(0)); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 646 | disabled_ = false; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 649 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 650 | void Schedule(monotonic_clock::time_point base, |
| 651 | monotonic_clock::duration repeat_offset) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 652 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 653 | if (event_.valid()) { |
| 654 | shm_event_loop_->RemoveEvent(&event_); |
| 655 | } |
| 656 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 657 | timerfd_.SetTime(base, repeat_offset); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 658 | base_ = base; |
| 659 | repeat_offset_ = repeat_offset; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 660 | event_.set_event_time(base_); |
| 661 | shm_event_loop_->AddEvent(&event_); |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 662 | disabled_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 665 | void Disable() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 666 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 667 | shm_event_loop_->RemoveEvent(&event_); |
| 668 | timerfd_.Disable(); |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 669 | disabled_ = true; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 670 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 671 | |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 672 | bool IsDisabled() override { return disabled_; } |
| 673 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 674 | private: |
| 675 | ShmEventLoop *shm_event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 676 | EventHandler<ShmTimerHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 677 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 678 | internal::TimerFd timerfd_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 679 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 680 | monotonic_clock::time_point base_; |
| 681 | monotonic_clock::duration repeat_offset_; |
Brian Silverman | af9a4d8 | 2020-10-06 15:10:58 -0700 | [diff] [blame] | 682 | |
| 683 | // Used to track if Disable() was called during the callback, so we know not |
| 684 | // to reschedule. |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 685 | bool disabled_ = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 686 | }; |
| 687 | |
| 688 | // Adapter class to the timerfd and PhasedLoop. |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 689 | class ShmPhasedLoopHandler final : public PhasedLoopHandler { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 690 | public: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 691 | ShmPhasedLoopHandler(ShmEventLoop *shm_event_loop, |
| 692 | ::std::function<void(int)> fn, |
| 693 | const monotonic_clock::duration interval, |
| 694 | const monotonic_clock::duration offset) |
| 695 | : PhasedLoopHandler(shm_event_loop, std::move(fn), interval, offset), |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 696 | shm_event_loop_(shm_event_loop), |
| 697 | event_(this) { |
| 698 | shm_event_loop_->epoll_.OnReadable( |
| 699 | timerfd_.fd(), [this]() { shm_event_loop_->HandleEvent(); }); |
| 700 | } |
| 701 | |
| 702 | void HandleEvent() { |
| 703 | // The return value for read is the number of cycles that have elapsed. |
| 704 | // Because we check to see when this event *should* have happened, there are |
| 705 | // cases where Read() will return 0, when 1 cycle has actually happened. |
| 706 | // This occurs when the timer interrupt hasn't triggered yet. Therefore, |
| 707 | // ignore it. Call handles rescheduling and calculating elapsed cycles |
| 708 | // without any extra help. |
| 709 | timerfd_.Read(); |
| 710 | event_.Invalidate(); |
| 711 | |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 712 | Call(monotonic_clock::now); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 715 | ~ShmPhasedLoopHandler() override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 716 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 717 | shm_event_loop_->epoll_.DeleteFd(timerfd_.fd()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 718 | shm_event_loop_->RemoveEvent(&event_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | private: |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 722 | // Reschedules the timer. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 723 | void Schedule(monotonic_clock::time_point sleep_time) override { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 724 | shm_event_loop_->CheckCurrentThread(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 725 | if (event_.valid()) { |
| 726 | shm_event_loop_->RemoveEvent(&event_); |
| 727 | } |
| 728 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 729 | timerfd_.SetTime(sleep_time, ::aos::monotonic_clock::zero()); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 730 | event_.set_event_time(sleep_time); |
| 731 | shm_event_loop_->AddEvent(&event_); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | ShmEventLoop *shm_event_loop_; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 735 | EventHandler<ShmPhasedLoopHandler> event_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 736 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 737 | internal::TimerFd timerfd_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 738 | }; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 739 | |
| 740 | } // namespace shm_event_loop_internal |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 741 | |
| 742 | ::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher( |
| 743 | const Channel *channel) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 744 | CheckCurrentThread(); |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 745 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 746 | LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view() |
| 747 | << "\", \"type\": \"" << channel->type()->string_view() |
| 748 | << "\" } is not able to be fetched on this node. Check your " |
| 749 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 752 | return ::std::unique_ptr<RawFetcher>( |
| 753 | new ShmFetcher(shm_base_, this, channel)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | ::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender( |
| 757 | const Channel *channel) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 758 | CheckCurrentThread(); |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 759 | TakeSender(channel); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 760 | |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 761 | return ::std::unique_ptr<RawSender>(new ShmSender(shm_base_, this, channel)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void ShmEventLoop::MakeRawWatcher( |
| 765 | const Channel *channel, |
| 766 | std::function<void(const Context &context, const void *message)> watcher) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 767 | CheckCurrentThread(); |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 768 | TakeWatcher(channel); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 769 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 770 | NewWatcher(::std::unique_ptr<WatcherState>( |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 771 | new ShmWatcherState(shm_base_, this, channel, std::move(watcher), true))); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | void ShmEventLoop::MakeRawNoArgWatcher( |
| 775 | const Channel *channel, |
| 776 | std::function<void(const Context &context)> watcher) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 777 | CheckCurrentThread(); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 778 | TakeWatcher(channel); |
| 779 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 780 | NewWatcher(::std::unique_ptr<WatcherState>(new ShmWatcherState( |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 781 | shm_base_, this, channel, |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 782 | [watcher](const Context &context, const void *) { watcher(context); }, |
| 783 | false))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 787 | CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 788 | return NewTimer(::std::unique_ptr<TimerHandler>( |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 789 | new ShmTimerHandler(this, ::std::move(callback)))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | PhasedLoopHandler *ShmEventLoop::AddPhasedLoop( |
| 793 | ::std::function<void(int)> callback, |
| 794 | const monotonic_clock::duration interval, |
| 795 | const monotonic_clock::duration offset) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 796 | CheckCurrentThread(); |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 797 | return NewPhasedLoop(::std::unique_ptr<PhasedLoopHandler>( |
| 798 | new ShmPhasedLoopHandler(this, ::std::move(callback), interval, offset))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | void ShmEventLoop::OnRun(::std::function<void()> on_run) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 802 | CheckCurrentThread(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 803 | on_run_.push_back(::std::move(on_run)); |
| 804 | } |
| 805 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 806 | void ShmEventLoop::CheckCurrentThread() const { |
| 807 | if (__builtin_expect(check_mutex_ != nullptr, false)) { |
| 808 | CHECK(check_mutex_->is_locked()) |
| 809 | << ": The configured mutex is not locked while calling a " |
| 810 | "ShmEventLoop function"; |
| 811 | } |
| 812 | if (__builtin_expect(!!check_tid_, false)) { |
| 813 | CHECK_EQ(syscall(SYS_gettid), *check_tid_) |
| 814 | << ": Being called from the wrong thread"; |
| 815 | } |
| 816 | } |
| 817 | |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 818 | // This is a bit tricky because watchers can generate new events at any time (as |
| 819 | // long as it's in the past). We want to check the watchers at least once before |
| 820 | // declaring there are no events to handle, and we want to check them again if |
| 821 | // event processing takes long enough that we find an event after that point in |
| 822 | // time to handle. |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 823 | void ShmEventLoop::HandleEvent() { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 824 | // Time through which we've checked for new events in watchers. |
| 825 | monotonic_clock::time_point checked_until = monotonic_clock::min_time; |
| 826 | if (!signalfd_) { |
| 827 | // Nothing to check, so we can bail out immediately once we're out of |
| 828 | // events. |
| 829 | CHECK(watchers_.empty()); |
| 830 | checked_until = monotonic_clock::max_time; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 833 | // Loop until we run out of events to check. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 834 | while (true) { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 835 | // Time of the next event we know about. If this is before checked_until, we |
| 836 | // know there aren't any new events before the next one that we already know |
| 837 | // about, so no need to check the watchers. |
| 838 | monotonic_clock::time_point next_time = monotonic_clock::max_time; |
| 839 | |
| 840 | if (EventCount() == 0) { |
| 841 | if (checked_until != monotonic_clock::min_time) { |
| 842 | // No events, and we've already checked the watchers at least once, so |
| 843 | // we're all done. |
| 844 | // |
| 845 | // There's a small chance that a watcher has gotten another event in |
| 846 | // between checked_until and now. If so, then the signalfd will be |
| 847 | // triggered now and we'll re-enter HandleEvent immediately. This is |
| 848 | // unlikely though, so we don't want to spend time checking all the |
| 849 | // watchers unnecessarily. |
| 850 | break; |
| 851 | } |
| 852 | } else { |
| 853 | next_time = PeekEvent()->event_time(); |
| 854 | } |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 855 | monotonic_clock::time_point now; |
| 856 | bool new_data = false; |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 857 | |
| 858 | if (next_time > checked_until) { |
| 859 | // Read all of the signals, because there's no point in waking up again |
| 860 | // immediately to handle each one if we've fallen behind. |
| 861 | // |
| 862 | // This is safe before checking for new data on the watchers. If a signal |
| 863 | // is cleared here, the corresponding CheckForNewData() call below will |
| 864 | // pick it up. |
| 865 | while (true) { |
| 866 | const signalfd_siginfo result = signalfd_->Read(); |
| 867 | if (result.ssi_signo == 0) { |
| 868 | break; |
| 869 | } |
| 870 | CHECK_EQ(result.ssi_signo, ipc_lib::kWakeupSignal); |
| 871 | } |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 872 | // This is the last time we can guarantee that if a message is published |
| 873 | // before, we will notice it. |
| 874 | now = monotonic_clock::now(); |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 875 | |
| 876 | // Check all the watchers for new events. |
| 877 | for (std::unique_ptr<WatcherState> &base_watcher : watchers_) { |
| 878 | ShmWatcherState *const watcher = |
| 879 | reinterpret_cast<ShmWatcherState *>(base_watcher.get()); |
| 880 | |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 881 | // Track if we got a message. |
| 882 | if (watcher->CheckForNewData()) { |
| 883 | new_data = true; |
| 884 | } |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 885 | } |
| 886 | if (EventCount() == 0) { |
| 887 | // Still no events, all done now. |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | checked_until = now; |
| 892 | // Check for any new events we found. |
| 893 | next_time = PeekEvent()->event_time(); |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 894 | } else { |
| 895 | now = monotonic_clock::now(); |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | if (next_time > now) { |
Austin Schuh | 00cad2e | 2022-12-02 20:11:04 -0800 | [diff] [blame] | 899 | // Ok, we got a message with a timestamp *after* we wrote down time. We |
| 900 | // need to process it (otherwise we will go to sleep without processing |
| 901 | // it), but we also need to make sure no other messages have come in |
| 902 | // before it that we would process out of order. Just go around again to |
| 903 | // redo the checks. |
| 904 | if (new_data) { |
| 905 | continue; |
| 906 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 907 | break; |
| 908 | } |
| 909 | |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 910 | EventLoopEvent *const event = PopEvent(); |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 911 | event->HandleEvent(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 915 | // RAII class to mask signals. |
| 916 | class ScopedSignalMask { |
| 917 | public: |
| 918 | ScopedSignalMask(std::initializer_list<int> signals) { |
| 919 | sigset_t sigset; |
| 920 | PCHECK(sigemptyset(&sigset) == 0); |
| 921 | for (int signal : signals) { |
| 922 | PCHECK(sigaddset(&sigset, signal) == 0); |
| 923 | } |
| 924 | |
| 925 | PCHECK(sigprocmask(SIG_BLOCK, &sigset, &old_) == 0); |
| 926 | } |
| 927 | |
| 928 | ~ScopedSignalMask() { PCHECK(sigprocmask(SIG_SETMASK, &old_, nullptr) == 0); } |
| 929 | |
| 930 | private: |
| 931 | sigset_t old_; |
| 932 | }; |
| 933 | |
| 934 | // Class to manage the static state associated with killing multiple event |
| 935 | // loops. |
| 936 | class SignalHandler { |
| 937 | public: |
| 938 | // Gets the singleton. |
| 939 | static SignalHandler *global() { |
| 940 | static SignalHandler loop; |
| 941 | return &loop; |
| 942 | } |
| 943 | |
| 944 | // Handles the signal with the singleton. |
| 945 | static void HandleSignal(int) { global()->DoHandleSignal(); } |
| 946 | |
| 947 | // Registers an event loop to receive Exit() calls. |
| 948 | void Register(ShmEventLoop *event_loop) { |
| 949 | // Block signals while we have the mutex so we never race with the signal |
| 950 | // handler. |
| 951 | ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM}); |
| 952 | std::unique_lock<stl_mutex> locker(mutex_); |
| 953 | if (event_loops_.size() == 0) { |
| 954 | // The first caller registers the signal handler. |
| 955 | struct sigaction new_action; |
| 956 | sigemptyset(&new_action.sa_mask); |
| 957 | // This makes it so that 2 control c's to a stuck process will kill it by |
| 958 | // restoring the original signal handler. |
| 959 | new_action.sa_flags = SA_RESETHAND; |
| 960 | new_action.sa_handler = &HandleSignal; |
| 961 | |
| 962 | PCHECK(sigaction(SIGINT, &new_action, &old_action_int_) == 0); |
| 963 | PCHECK(sigaction(SIGHUP, &new_action, &old_action_hup_) == 0); |
| 964 | PCHECK(sigaction(SIGTERM, &new_action, &old_action_term_) == 0); |
| 965 | } |
| 966 | |
| 967 | event_loops_.push_back(event_loop); |
| 968 | } |
| 969 | |
| 970 | // Unregisters an event loop to receive Exit() calls. |
| 971 | void Unregister(ShmEventLoop *event_loop) { |
| 972 | // Block signals while we have the mutex so we never race with the signal |
| 973 | // handler. |
| 974 | ScopedSignalMask mask({SIGINT, SIGHUP, SIGTERM}); |
| 975 | std::unique_lock<stl_mutex> locker(mutex_); |
| 976 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 977 | event_loops_.erase( |
| 978 | std::find(event_loops_.begin(), event_loops_.end(), event_loop)); |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 979 | |
| 980 | if (event_loops_.size() == 0u) { |
| 981 | // The last caller restores the original signal handlers. |
| 982 | PCHECK(sigaction(SIGINT, &old_action_int_, nullptr) == 0); |
| 983 | PCHECK(sigaction(SIGHUP, &old_action_hup_, nullptr) == 0); |
| 984 | PCHECK(sigaction(SIGTERM, &old_action_term_, nullptr) == 0); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | private: |
| 989 | void DoHandleSignal() { |
| 990 | // We block signals while grabbing the lock, so there should never be a |
| 991 | // race. Confirm that this is true using trylock. |
| 992 | CHECK(mutex_.try_lock()) << ": sigprocmask failed to block signals while " |
| 993 | "modifing the event loop list."; |
| 994 | for (ShmEventLoop *event_loop : event_loops_) { |
| 995 | event_loop->Exit(); |
| 996 | } |
| 997 | mutex_.unlock(); |
| 998 | } |
| 999 | |
| 1000 | // Mutex to protect all state. |
| 1001 | stl_mutex mutex_; |
| 1002 | std::vector<ShmEventLoop *> event_loops_; |
| 1003 | struct sigaction old_action_int_; |
| 1004 | struct sigaction old_action_hup_; |
| 1005 | struct sigaction old_action_term_; |
| 1006 | }; |
| 1007 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1008 | void ShmEventLoop::Run() { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1009 | CheckCurrentThread(); |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 1010 | SignalHandler::global()->Register(this); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1011 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1012 | if (watchers_.size() > 0) { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 1013 | signalfd_.reset(new ipc_lib::SignalFd({ipc_lib::kWakeupSignal})); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1014 | |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 1015 | epoll_.OnReadable(signalfd_->fd(), [this]() { HandleEvent(); }); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1018 | MaybeScheduleTimingReports(); |
| 1019 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1020 | ReserveEvents(); |
| 1021 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1022 | { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1023 | logging::ScopedLogRestorer prev_logger; |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1024 | AosLogToFbs aos_logger; |
| 1025 | if (!skip_logger_) { |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 1026 | aos_logger.Initialize(&name_, MakeSender<logging::LogMessageFbs>("/aos")); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 1027 | prev_logger.Swap(aos_logger.implementation()); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1028 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1029 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1030 | aos::SetCurrentThreadName(name_.substr(0, 16)); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1031 | const cpu_set_t default_affinity = DefaultAffinity(); |
| 1032 | if (!CPU_EQUAL(&affinity_, &default_affinity)) { |
| 1033 | ::aos::SetCurrentThreadAffinity(affinity_); |
| 1034 | } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1035 | // Now, all the callbacks are setup. Lock everything into memory and go RT. |
| 1036 | if (priority_ != 0) { |
| 1037 | ::aos::InitRT(); |
| 1038 | |
| 1039 | LOG(INFO) << "Setting priority to " << priority_; |
| 1040 | ::aos::SetCurrentThreadRealtimePriority(priority_); |
| 1041 | } |
| 1042 | |
| 1043 | set_is_running(true); |
| 1044 | |
| 1045 | // Now that we are realtime (but before the OnRun handlers run), snap the |
| 1046 | // queue index. |
| 1047 | for (::std::unique_ptr<WatcherState> &watcher : watchers_) { |
| 1048 | watcher->Startup(this); |
| 1049 | } |
| 1050 | |
| 1051 | // Now that we are RT, run all the OnRun handlers. |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 1052 | SetTimerContext(monotonic_clock::now()); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 1053 | for (const auto &run : on_run_) { |
| 1054 | run(); |
| 1055 | } |
| 1056 | |
| 1057 | // And start our main event loop which runs all the timers and handles Quit. |
| 1058 | epoll_.Run(); |
| 1059 | |
| 1060 | // Once epoll exits, there is no useful nonrt work left to do. |
| 1061 | set_is_running(false); |
| 1062 | |
| 1063 | // Nothing time or synchronization critical needs to happen after this |
| 1064 | // point. Drop RT priority. |
| 1065 | ::aos::UnsetCurrentThreadRealtimePriority(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1068 | for (::std::unique_ptr<WatcherState> &base_watcher : watchers_) { |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 1069 | ShmWatcherState *watcher = |
| 1070 | reinterpret_cast<ShmWatcherState *>(base_watcher.get()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1071 | watcher->UnregisterWakeup(); |
| 1072 | } |
| 1073 | |
| 1074 | if (watchers_.size() > 0) { |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 1075 | epoll_.DeleteFd(signalfd_->fd()); |
| 1076 | signalfd_.reset(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1077 | } |
Austin Schuh | 32fd5a7 | 2019-12-01 22:20:26 -0800 | [diff] [blame] | 1078 | |
| 1079 | SignalHandler::global()->Unregister(this); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1080 | |
| 1081 | // Trigger any remaining senders or fetchers to be cleared before destroying |
| 1082 | // the event loop so the book keeping matches. Do this in the thread that |
| 1083 | // created the timing reporter. |
| 1084 | timing_report_sender_.reset(); |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 1085 | ClearContext(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | void ShmEventLoop::Exit() { epoll_.Quit(); } |
| 1089 | |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 1090 | std::unique_ptr<ExitHandle> ShmEventLoop::MakeExitHandle() { |
| 1091 | return std::make_unique<ShmExitHandle>(this); |
| 1092 | } |
| 1093 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1094 | ShmEventLoop::~ShmEventLoop() { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1095 | CheckCurrentThread(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1096 | // Force everything with a registered fd with epoll to be destroyed now. |
| 1097 | timers_.clear(); |
| 1098 | phased_loops_.clear(); |
| 1099 | watchers_.clear(); |
| 1100 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1101 | CHECK(!is_running()) << ": ShmEventLoop destroyed while running"; |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 1102 | CHECK_EQ(0, exit_handle_count_) |
| 1103 | << ": All ExitHandles must be destroyed before the ShmEventLoop"; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1106 | void ShmEventLoop::SetRuntimeRealtimePriority(int priority) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1107 | CheckCurrentThread(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1108 | if (is_running()) { |
| 1109 | LOG(FATAL) << "Cannot set realtime priority while running."; |
| 1110 | } |
| 1111 | priority_ = priority; |
| 1112 | } |
| 1113 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1114 | void ShmEventLoop::SetRuntimeAffinity(const cpu_set_t &cpuset) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1115 | CheckCurrentThread(); |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 1116 | if (is_running()) { |
| 1117 | LOG(FATAL) << "Cannot set affinity while running."; |
| 1118 | } |
| 1119 | affinity_ = cpuset; |
| 1120 | } |
| 1121 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 1122 | void ShmEventLoop::set_name(const std::string_view name) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1123 | CheckCurrentThread(); |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 1124 | name_ = std::string(name); |
| 1125 | UpdateTimingReport(); |
| 1126 | } |
| 1127 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 1128 | absl::Span<const char> ShmEventLoop::GetWatcherSharedMemory( |
| 1129 | const Channel *channel) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1130 | CheckCurrentThread(); |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 1131 | ShmWatcherState *const watcher_state = |
| 1132 | static_cast<ShmWatcherState *>(GetWatcherState(channel)); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 1133 | return watcher_state->GetSharedMemory(); |
| 1134 | } |
| 1135 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1136 | int ShmEventLoop::NumberBuffers(const Channel *channel) { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1137 | CheckCurrentThread(); |
Austin Schuh | 4d275fc | 2022-09-16 15:42:45 -0700 | [diff] [blame] | 1138 | return ipc_lib::MakeQueueConfiguration(configuration(), channel) |
| 1139 | .num_messages(); |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 1142 | absl::Span<char> ShmEventLoop::GetShmSenderSharedMemory( |
| 1143 | const aos::RawSender *sender) const { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1144 | CheckCurrentThread(); |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 1145 | return static_cast<const ShmSender *>(sender)->GetSharedMemory(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 1148 | absl::Span<const char> ShmEventLoop::GetShmFetcherPrivateMemory( |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 1149 | const aos::RawFetcher *fetcher) const { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1150 | CheckCurrentThread(); |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 1151 | return static_cast<const ShmFetcher *>(fetcher)->GetPrivateMemory(); |
| 1152 | } |
| 1153 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 1154 | pid_t ShmEventLoop::GetTid() { |
| 1155 | CheckCurrentThread(); |
| 1156 | return syscall(SYS_gettid); |
| 1157 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 1158 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1159 | } // namespace aos |