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