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