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