Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 1 | #include "aos/events/epoll.h" |
| 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <sys/epoll.h> |
Kiran Mohan | fed1a7e | 2023-10-19 11:05:27 -0700 | [diff] [blame] | 5 | #include <sys/socket.h> |
| 6 | #include <sys/stat.h> |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 7 | #include <sys/timerfd.h> |
| 8 | #include <unistd.h> |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 9 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 10 | #include <atomic> |
Kiran Mohan | fed1a7e | 2023-10-19 11:05:27 -0700 | [diff] [blame] | 11 | #include <cstring> |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 14 | #include "glog/logging.h" |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 15 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 16 | #include "aos/time/time.h" |
| 17 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 18 | namespace aos { |
| 19 | namespace internal { |
| 20 | |
| 21 | TimerFd::TimerFd() |
| 22 | : fd_(timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK)) { |
| 23 | PCHECK(fd_ != -1); |
| 24 | Disable(); |
| 25 | } |
| 26 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 27 | TimerFd::~TimerFd() { PCHECK(close(fd_) == 0); } |
| 28 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 29 | void TimerFd::SetTime(monotonic_clock::time_point start, |
| 30 | monotonic_clock::duration interval) { |
James Kuszmaul | 8866e64 | 2022-06-10 16:00:36 -0700 | [diff] [blame] | 31 | CHECK_GE(start, monotonic_clock::epoch()); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 32 | struct itimerspec new_value; |
| 33 | new_value.it_interval = ::aos::time::to_timespec(interval); |
| 34 | new_value.it_value = ::aos::time::to_timespec(start); |
| 35 | PCHECK(timerfd_settime(fd_, TFD_TIMER_ABSTIME, &new_value, nullptr) == 0); |
| 36 | } |
| 37 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 38 | uint64_t TimerFd::Read() { |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 39 | uint64_t buf; |
| 40 | ssize_t result = read(fd_, &buf, sizeof(buf)); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 41 | if (result == -1) { |
| 42 | if (errno == EAGAIN) { |
| 43 | return 0; |
| 44 | } |
| 45 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 46 | PCHECK(result != -1); |
| 47 | CHECK_EQ(result, static_cast<int>(sizeof(buf))); |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 48 | |
| 49 | return buf; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | EPoll::EPoll() : epoll_fd_(epoll_create1(EPOLL_CLOEXEC)) { |
| 53 | PCHECK(epoll_fd_ > 0); |
| 54 | |
| 55 | // Create a pipe for the Quit function. We want to use a pipe to be async |
| 56 | // safe so this can be called from signal handlers. |
| 57 | int pipefd[2]; |
| 58 | PCHECK(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) == 0); |
| 59 | quit_epoll_fd_ = pipefd[0]; |
| 60 | quit_signal_fd_ = pipefd[1]; |
| 61 | // Read the fd when data is sent and set run_ to false. |
| 62 | OnReadable(quit_epoll_fd_, [this]() { |
| 63 | run_ = false; |
| 64 | char buf[1]; |
| 65 | PCHECK(read(quit_epoll_fd_, &buf[0], 1) == 1); |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | EPoll::~EPoll() { |
| 70 | // Clean up the quit pipe and epoll fd. |
| 71 | DeleteFd(quit_epoll_fd_); |
| 72 | close(quit_signal_fd_); |
| 73 | close(quit_epoll_fd_); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 74 | CHECK_EQ(fns_.size(), 0u) |
| 75 | << ": Not all file descriptors were unregistered before shutting down."; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 76 | close(epoll_fd_); |
| 77 | } |
| 78 | |
Austin Schuh | 219b778 | 2020-12-21 12:01:40 -0800 | [diff] [blame] | 79 | void EPoll::BeforeWait(std::function<void()> function) { |
| 80 | before_epoll_wait_functions_.emplace_back(std::move(function)); |
| 81 | } |
| 82 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 83 | void EPoll::Run() { |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 84 | run_ = true; |
James Kuszmaul | c8e657e | 2020-12-14 23:49:45 -0800 | [diff] [blame] | 85 | // As long as run_ is true or we still have events to process, keep polling. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 86 | while (true) { |
James Kuszmaul | c8e657e | 2020-12-14 23:49:45 -0800 | [diff] [blame] | 87 | // If we ran out of events and Quit() was called, quit |
| 88 | if (!Poll(run_)) { |
| 89 | if (!run_) { |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
James Kuszmaul | c8e657e | 2020-12-14 23:49:45 -0800 | [diff] [blame] | 96 | bool EPoll::Poll(bool block) { |
Austin Schuh | 219b778 | 2020-12-21 12:01:40 -0800 | [diff] [blame] | 97 | for (const std::function<void()> &function : before_epoll_wait_functions_) { |
| 98 | function(); |
| 99 | } |
James Kuszmaul | c8e657e | 2020-12-14 23:49:45 -0800 | [diff] [blame] | 100 | // Pull a single event out. Infinite timeout if we are supposed to be |
| 101 | // running, and 0 length timeout otherwise. This lets us flush the event |
| 102 | // queue before quitting. |
| 103 | struct epoll_event event; |
| 104 | int num_events = epoll_wait(epoll_fd_, &event, 1, block ? -1 : 0); |
| 105 | // Retry on EINTR and nothing else. |
| 106 | if (num_events == -1) { |
| 107 | if (errno == EINTR) { |
| 108 | return false; |
| 109 | } |
| 110 | PCHECK(num_events != -1); |
| 111 | } |
| 112 | |
| 113 | if (num_events == 0) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | EventData *const event_data = static_cast<struct EventData *>(event.data.ptr); |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 118 | event_data->DoCallbacks(event.events); |
James Kuszmaul | c8e657e | 2020-12-14 23:49:45 -0800 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
Austin Schuh | f74daa6 | 2021-07-21 15:29:17 -0700 | [diff] [blame] | 122 | void EPoll::Quit() { |
| 123 | // Shortcut to break us out of infinite loops. We might write more than once |
| 124 | // to the pipe, but we'll stop once the first is read on the other end. |
| 125 | if (!run_) { |
| 126 | return; |
| 127 | } |
| 128 | PCHECK(write(quit_signal_fd_, "q", 1) == 1); |
| 129 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 130 | |
| 131 | void EPoll::OnReadable(int fd, ::std::function<void()> function) { |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 132 | EventData *event_data = GetEventData(fd); |
| 133 | if (event_data == nullptr) { |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 134 | fns_.emplace_back(std::make_unique<InOutEventData>(fd)); |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 135 | event_data = fns_.back().get(); |
| 136 | } else { |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 137 | CHECK(!static_cast<InOutEventData *>(event_data)->in_fn) |
| 138 | << ": Duplicate in functions for " << fd; |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 139 | } |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 140 | static_cast<InOutEventData *>(event_data)->in_fn = ::std::move(function); |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 141 | DoEpollCtl(event_data, event_data->events | kInEvents); |
| 142 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 143 | |
Austin Schuh | 603419f | 2021-04-24 18:15:20 -0700 | [diff] [blame] | 144 | void EPoll::OnError(int fd, ::std::function<void()> function) { |
| 145 | EventData *event_data = GetEventData(fd); |
| 146 | if (event_data == nullptr) { |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 147 | fns_.emplace_back(std::make_unique<InOutEventData>(fd)); |
Austin Schuh | 603419f | 2021-04-24 18:15:20 -0700 | [diff] [blame] | 148 | event_data = fns_.back().get(); |
| 149 | } else { |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 150 | CHECK(!static_cast<InOutEventData *>(event_data)->err_fn) |
| 151 | << ": Duplicate error functions for " << fd; |
Austin Schuh | 603419f | 2021-04-24 18:15:20 -0700 | [diff] [blame] | 152 | } |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 153 | static_cast<InOutEventData *>(event_data)->err_fn = ::std::move(function); |
Austin Schuh | 603419f | 2021-04-24 18:15:20 -0700 | [diff] [blame] | 154 | DoEpollCtl(event_data, event_data->events | kErrorEvents); |
| 155 | } |
| 156 | |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 157 | void EPoll::OnWriteable(int fd, ::std::function<void()> function) { |
| 158 | EventData *event_data = GetEventData(fd); |
| 159 | if (event_data == nullptr) { |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 160 | fns_.emplace_back(std::make_unique<InOutEventData>(fd)); |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 161 | event_data = fns_.back().get(); |
| 162 | } else { |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 163 | CHECK(!static_cast<InOutEventData *>(event_data)->out_fn) |
| 164 | << ": Duplicate out functions for " << fd; |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 165 | } |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 166 | static_cast<InOutEventData *>(event_data)->out_fn = ::std::move(function); |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 167 | DoEpollCtl(event_data, event_data->events | kOutEvents); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 170 | void EPoll::OnEvents(int fd, ::std::function<void(uint32_t)> function) { |
| 171 | if (GetEventData(fd) != nullptr) { |
| 172 | LOG(FATAL) << "May not replace OnEvents handlers"; |
| 173 | } |
| 174 | fns_.emplace_back(std::make_unique<SingleEventData>(fd)); |
| 175 | static_cast<SingleEventData *>(fns_.back().get())->fn = std::move(function); |
| 176 | } |
| 177 | |
Austin Schuh | 219b778 | 2020-12-21 12:01:40 -0800 | [diff] [blame] | 178 | void EPoll::ForgetClosedFd(int fd) { |
| 179 | auto element = fns_.begin(); |
| 180 | while (fns_.end() != element) { |
| 181 | if (element->get()->fd == fd) { |
| 182 | fns_.erase(element); |
| 183 | return; |
| 184 | } |
| 185 | ++element; |
| 186 | } |
| 187 | LOG(FATAL) << "fd " << fd << " not found"; |
| 188 | } |
| 189 | |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 190 | void EPoll::SetEvents(int fd, uint32_t events) { |
| 191 | DoEpollCtl(CHECK_NOTNULL(GetEventData(fd)), events); |
| 192 | } |
| 193 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 194 | // Removes fd from the event loop. |
| 195 | void EPoll::DeleteFd(int fd) { |
| 196 | auto element = fns_.begin(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 197 | while (fns_.end() != element) { |
| 198 | if (element->get()->fd == fd) { |
| 199 | fns_.erase(element); |
Austin Schuh | 219b778 | 2020-12-21 12:01:40 -0800 | [diff] [blame] | 200 | PCHECK(epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr) == 0) |
| 201 | << "Failed to delete fd " << fd; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | ++element; |
| 205 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 206 | LOG(FATAL) << "fd " << fd << " not found"; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Kiran Mohan | fed1a7e | 2023-10-19 11:05:27 -0700 | [diff] [blame] | 209 | namespace { |
| 210 | bool IsSocket(int fd) { |
| 211 | struct stat st; |
| 212 | if (fstat(fd, &st) == -1) { |
| 213 | return false; |
| 214 | } |
| 215 | return static_cast<bool>(S_ISSOCK(st.st_mode)); |
| 216 | } |
| 217 | |
| 218 | ::std::string GetSocketErrorStr(int fd) { |
| 219 | ::std::string error_str; |
| 220 | if (IsSocket(fd)) { |
| 221 | int error = 0; |
| 222 | socklen_t errlen = sizeof(error); |
| 223 | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == 0) { |
| 224 | if (error) { |
| 225 | error_str = "Socket error: " + ::std::string(strerror(error)); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | return error_str; |
| 230 | } |
| 231 | } // namespace |
| 232 | |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 233 | void EPoll::InOutEventData::DoCallbacks(uint32_t events) { |
| 234 | if (events & kInEvents) { |
Kiran Mohan | fed1a7e | 2023-10-19 11:05:27 -0700 | [diff] [blame] | 235 | CHECK(in_fn) << ": No handler registered for input events on descriptor " |
| 236 | << fd << ". Received events = 0x" << std::hex << events |
| 237 | << std::dec; |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 238 | in_fn(); |
| 239 | } |
| 240 | if (events & kOutEvents) { |
Kiran Mohan | fed1a7e | 2023-10-19 11:05:27 -0700 | [diff] [blame] | 241 | CHECK(out_fn) << ": No handler registered for output events on descriptor " |
| 242 | << fd << ". Received events = 0x" << std::hex << events |
| 243 | << std::dec; |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 244 | out_fn(); |
| 245 | } |
| 246 | if (events & kErrorEvents) { |
Kiran Mohan | fed1a7e | 2023-10-19 11:05:27 -0700 | [diff] [blame] | 247 | CHECK(err_fn) << ": No handler registered for error events on descriptor " |
| 248 | << fd << ". Received events = 0x" << std::hex << events |
| 249 | << std::dec << ". " << GetSocketErrorStr(fd); |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 250 | err_fn(); |
| 251 | } |
| 252 | } |
| 253 | |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 254 | void EPoll::EnableEvents(int fd, uint32_t events) { |
| 255 | EventData *const event_data = CHECK_NOTNULL(GetEventData(fd)); |
| 256 | DoEpollCtl(event_data, event_data->events | events); |
| 257 | } |
| 258 | |
| 259 | void EPoll::DisableEvents(int fd, uint32_t events) { |
| 260 | EventData *const event_data = CHECK_NOTNULL(GetEventData(fd)); |
| 261 | DoEpollCtl(event_data, event_data->events & ~events); |
| 262 | } |
| 263 | |
| 264 | EPoll::EventData *EPoll::GetEventData(int fd) { |
| 265 | const auto iterator = std::find_if( |
| 266 | fns_.begin(), fns_.end(), |
| 267 | [fd](const std::unique_ptr<EventData> &data) { return data->fd == fd; }); |
| 268 | if (iterator == fns_.end()) { |
| 269 | return nullptr; |
| 270 | } |
| 271 | return iterator->get(); |
| 272 | } |
| 273 | |
| 274 | void EPoll::DoEpollCtl(EventData *event_data, const uint32_t new_events) { |
| 275 | const uint32_t old_events = event_data->events; |
Austin Schuh | 4c83968 | 2021-07-21 15:30:44 -0700 | [diff] [blame] | 276 | if (old_events == new_events) { |
| 277 | // Shortcut without calling into the kernel. This happens often with |
| 278 | // external event loop integrations that are emulating poll, so make it |
| 279 | // fast. |
| 280 | return; |
| 281 | } |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 282 | event_data->events = new_events; |
| 283 | if (new_events == 0) { |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 284 | // It was added, but should now be removed. |
| 285 | PCHECK(epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, event_data->fd, nullptr) == 0); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | int operation = EPOLL_CTL_MOD; |
| 290 | if (old_events == 0) { |
| 291 | // If it wasn't added before, then this is the first time it's being added. |
| 292 | operation = EPOLL_CTL_ADD; |
| 293 | } |
| 294 | struct epoll_event event; |
| 295 | event.events = event_data->events; |
| 296 | event.data.ptr = event_data; |
| 297 | PCHECK(epoll_ctl(epoll_fd_, operation, event_data->fd, &event) == 0) |
| 298 | << ": Failed to " << operation << " epoll fd: " << event_data->fd; |
| 299 | } |
| 300 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 301 | } // namespace internal |
| 302 | } // namespace aos |