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