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