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