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