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