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