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