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