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