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