The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c |
| 2 | ** |
| 3 | ** Copyright 2006, Brian Swetland <swetland@frotz.net> |
| 4 | ** |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 18 | #define TRACE_TAG FDEVENT |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 19 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | #include "sysdeps.h" |
| 21 | #include "fdevent.h" |
| 22 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <fcntl.h> |
Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 30 | #include <atomic> |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 31 | #include <deque> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 32 | #include <functional> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 33 | #include <list> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 34 | #include <mutex> |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 35 | #include <optional> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 36 | #include <unordered_map> |
Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 37 | #include <utility> |
| 38 | #include <variant> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 39 | #include <vector> |
| 40 | |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 41 | #include <android-base/chrono_utils.h> |
| 42 | #include <android-base/file.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/logging.h> |
| 44 | #include <android-base/stringprintf.h> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 45 | #include <android-base/thread_annotations.h> |
Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 46 | #include <android-base/threads.h> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 47 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 48 | #include "adb_io.h" |
leozwang | d3fc15f | 2014-07-29 12:50:02 -0700 | [diff] [blame] | 49 | #include "adb_trace.h" |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 50 | #include "adb_unique_fd.h" |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 51 | #include "adb_utils.h" |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 52 | #include "sysdeps/chrono.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | #define FDE_EVENTMASK 0x00ff |
| 55 | #define FDE_STATEMASK 0xff00 |
| 56 | |
| 57 | #define FDE_ACTIVE 0x0100 |
| 58 | #define FDE_PENDING 0x0200 |
| 59 | #define FDE_CREATED 0x0400 |
| 60 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 61 | static std::string dump_fde(const fdevent* fde) { |
| 62 | std::string state; |
| 63 | if (fde->state & FDE_ACTIVE) { |
| 64 | state += "A"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 66 | if (fde->state & FDE_PENDING) { |
| 67 | state += "P"; |
| 68 | } |
| 69 | if (fde->state & FDE_CREATED) { |
| 70 | state += "C"; |
| 71 | } |
| 72 | if (fde->state & FDE_READ) { |
| 73 | state += "R"; |
| 74 | } |
| 75 | if (fde->state & FDE_WRITE) { |
| 76 | state += "W"; |
| 77 | } |
| 78 | if (fde->state & FDE_ERROR) { |
| 79 | state += "E"; |
| 80 | } |
Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 81 | return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(), |
| 82 | state.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 85 | struct PollNode { |
| 86 | fdevent* fde; |
| 87 | adb_pollfd pollfd; |
| 88 | |
| 89 | explicit PollNode(fdevent* fde) : fde(fde) { |
| 90 | memset(&pollfd, 0, sizeof(pollfd)); |
| 91 | pollfd.fd = fde->fd.get(); |
| 92 | |
| 93 | #if defined(__linux__) |
| 94 | // Always enable POLLRDHUP, so the host server can take action when some clients disconnect. |
| 95 | // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034. |
| 96 | pollfd.events = POLLRDHUP; |
| 97 | #endif |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | struct fdevent_context_poll : public fdevent_context { |
| 102 | virtual ~fdevent_context_poll() = default; |
| 103 | |
| 104 | virtual fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg) final; |
| 105 | virtual unique_fd Destroy(fdevent* fde) final; |
| 106 | |
| 107 | virtual void Set(fdevent* fde, unsigned events) final; |
| 108 | virtual void Add(fdevent* fde, unsigned events) final; |
| 109 | virtual void Del(fdevent* fde, unsigned events) final; |
| 110 | virtual void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) final; |
| 111 | |
| 112 | virtual void Loop() final; |
| 113 | |
| 114 | virtual void CheckMainThread() final; |
| 115 | |
| 116 | virtual void Run(std::function<void()> fn) final; |
| 117 | |
| 118 | virtual void TerminateLoop() final; |
| 119 | virtual size_t InstalledCount() final; |
| 120 | virtual void Reset() final; |
| 121 | |
| 122 | // All operations to fdevent should happen only in the main thread. |
| 123 | // That's why we don't need a lock for fdevent. |
| 124 | std::unordered_map<int, PollNode> poll_node_map_; |
| 125 | std::list<fdevent*> pending_list_; |
| 126 | bool main_thread_valid_ = false; |
| 127 | uint64_t main_thread_id_ = 0; |
| 128 | uint64_t fdevent_id_ = 0; |
| 129 | |
| 130 | bool run_needs_flush_ = false; |
| 131 | unique_fd run_queue_notify_fd_; |
| 132 | std::mutex run_queue_mutex_; |
| 133 | std::deque<std::function<void()>> run_queue_ GUARDED_BY(run_queue_mutex_); |
| 134 | |
| 135 | std::atomic<bool> terminate_loop_ = false; |
| 136 | }; |
| 137 | |
| 138 | static fdevent_context* g_ambient_fdevent_context = new fdevent_context_poll(); |
| 139 | |
| 140 | static fdevent_context* fdevent_get_ambient() { |
| 141 | return g_ambient_fdevent_context; |
| 142 | } |
| 143 | |
| 144 | void fdevent_context_poll::CheckMainThread() { |
| 145 | if (main_thread_valid_) { |
| 146 | CHECK_EQ(main_thread_id_, android::base::GetThreadId()); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | fdevent* fdevent_context_poll::Create(unique_fd fd, std::variant<fd_func, fd_func2> func, |
| 151 | void* arg) { |
| 152 | CheckMainThread(); |
| 153 | CHECK_GE(fd.get(), 0); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 154 | |
| 155 | fdevent* fde = new fdevent(); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 156 | fde->id = fdevent_id_++; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | fde->state = FDE_ACTIVE; |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 158 | fde->fd = std::move(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | fde->func = func; |
| 160 | fde->arg = arg; |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 161 | if (!set_file_block_mode(fde->fd, false)) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 162 | // Here is not proper to handle the error. If it fails here, some error is |
| 163 | // likely to be detected by poll(), then we can let the callback function |
| 164 | // to handle it. |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 165 | LOG(ERROR) << "failed to set non-blocking mode for fd " << fde->fd.get(); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 166 | } |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 167 | auto pair = poll_node_map_.emplace(fde->fd.get(), PollNode(fde)); |
| 168 | CHECK(pair.second) << "install existing fd " << fde->fd.get(); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 169 | |
| 170 | fde->state |= FDE_CREATED; |
| 171 | return fde; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 174 | unique_fd fdevent_context_poll::Destroy(fdevent* fde) { |
| 175 | CheckMainThread(); |
Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 176 | if (!fde) { |
| 177 | return {}; |
| 178 | } |
| 179 | |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 180 | if (!(fde->state & FDE_CREATED)) { |
| 181 | LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde); |
| 182 | } |
| 183 | |
Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 184 | unique_fd result = std::move(fde->fd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 185 | if (fde->state & FDE_ACTIVE) { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 186 | poll_node_map_.erase(result.get()); |
Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 187 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 188 | if (fde->state & FDE_PENDING) { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 189 | pending_list_.remove(fde); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 190 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 191 | fde->state = 0; |
| 192 | fde->events = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | } |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 194 | |
| 195 | delete fde; |
Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 196 | return result; |
| 197 | } |
| 198 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 199 | void fdevent_context_poll::Set(fdevent* fde, unsigned events) { |
| 200 | CheckMainThread(); |
| 201 | events &= FDE_EVENTMASK; |
| 202 | if ((fde->state & FDE_EVENTMASK) == events) { |
| 203 | return; |
| 204 | } |
| 205 | CHECK(fde->state & FDE_ACTIVE); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 207 | auto it = poll_node_map_.find(fde->fd.get()); |
| 208 | CHECK(it != poll_node_map_.end()); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 209 | PollNode& node = it->second; |
| 210 | if (events & FDE_READ) { |
| 211 | node.pollfd.events |= POLLIN; |
| 212 | } else { |
| 213 | node.pollfd.events &= ~POLLIN; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 216 | if (events & FDE_WRITE) { |
| 217 | node.pollfd.events |= POLLOUT; |
| 218 | } else { |
| 219 | node.pollfd.events &= ~POLLOUT; |
| 220 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | fde->state = (fde->state & FDE_STATEMASK) | events; |
| 222 | |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 223 | D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 224 | |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 225 | if (fde->state & FDE_PENDING) { |
| 226 | // If we are pending, make sure we don't signal an event that is no longer wanted. |
| 227 | fde->events &= events; |
| 228 | if (fde->events == 0) { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 229 | pending_list_.remove(fde); |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 230 | fde->state &= ~FDE_PENDING; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 235 | void fdevent_context_poll::Add(fdevent* fde, unsigned events) { |
| 236 | Set(fde, (fde->state & FDE_EVENTMASK) | events); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 239 | void fdevent_context_poll::Del(fdevent* fde, unsigned events) { |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 240 | CHECK(!(events & FDE_TIMEOUT)); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 241 | Set(fde, (fde->state & FDE_EVENTMASK) & ~events); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 244 | void fdevent_context_poll::SetTimeout(fdevent* fde, |
| 245 | std::optional<std::chrono::milliseconds> timeout) { |
| 246 | CheckMainThread(); |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 247 | fde->timeout = timeout; |
| 248 | fde->last_active = std::chrono::steady_clock::now(); |
| 249 | } |
| 250 | |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 251 | static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 252 | std::string result; |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 253 | for (const auto& pollfd : pollfds) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 254 | std::string op; |
| 255 | if (pollfd.events & POLLIN) { |
| 256 | op += "R"; |
| 257 | } |
| 258 | if (pollfd.events & POLLOUT) { |
| 259 | op += "W"; |
| 260 | } |
| 261 | android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str()); |
| 262 | } |
| 263 | return result; |
| 264 | } |
| 265 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 266 | static std::optional<std::chrono::milliseconds> calculate_timeout(fdevent_context_poll* ctx) { |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 267 | std::optional<std::chrono::milliseconds> result = std::nullopt; |
| 268 | auto now = std::chrono::steady_clock::now(); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 269 | ctx->CheckMainThread(); |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 270 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 271 | for (const auto& [fd, pollnode] : ctx->poll_node_map_) { |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 272 | UNUSED(fd); |
| 273 | auto timeout_opt = pollnode.fde->timeout; |
| 274 | if (timeout_opt) { |
| 275 | auto deadline = pollnode.fde->last_active + *timeout_opt; |
| 276 | auto time_left = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now); |
| 277 | if (time_left < std::chrono::milliseconds::zero()) { |
| 278 | time_left = std::chrono::milliseconds::zero(); |
| 279 | } |
| 280 | |
| 281 | if (!result) { |
| 282 | result = time_left; |
| 283 | } else { |
| 284 | result = std::min(*result, time_left); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return result; |
| 290 | } |
| 291 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 292 | static void fdevent_process(fdevent_context_poll* ctx) { |
Josh Gao | 3777d2e | 2016-02-16 17:34:53 -0800 | [diff] [blame] | 293 | std::vector<adb_pollfd> pollfds; |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 294 | for (const auto& pair : ctx->poll_node_map_) { |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 295 | pollfds.push_back(pair.second.pollfd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 296 | } |
| 297 | CHECK_GT(pollfds.size(), 0u); |
| 298 | D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str()); |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 299 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 300 | auto timeout = calculate_timeout(ctx); |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 301 | int timeout_ms; |
| 302 | if (!timeout) { |
| 303 | timeout_ms = -1; |
| 304 | } else { |
| 305 | timeout_ms = timeout->count(); |
| 306 | } |
| 307 | |
| 308 | int ret = adb_poll(&pollfds[0], pollfds.size(), timeout_ms); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 309 | if (ret == -1) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 310 | PLOG(ERROR) << "poll(), ret = " << ret; |
| 311 | return; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 312 | } |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 313 | |
| 314 | auto post_poll = std::chrono::steady_clock::now(); |
| 315 | |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 316 | for (const auto& pollfd : pollfds) { |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 317 | if (pollfd.revents != 0) { |
| 318 | D("for fd %d, revents = %x", pollfd.fd, pollfd.revents); |
| 319 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 320 | unsigned events = 0; |
| 321 | if (pollfd.revents & POLLIN) { |
| 322 | events |= FDE_READ; |
| 323 | } |
| 324 | if (pollfd.revents & POLLOUT) { |
| 325 | events |= FDE_WRITE; |
| 326 | } |
| 327 | if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
| 328 | // We fake a read, as the rest of the code assumes that errors will |
| 329 | // be detected at that point. |
| 330 | events |= FDE_READ | FDE_ERROR; |
| 331 | } |
Yabin Cui | aa77e22 | 2015-09-29 12:25:33 -0700 | [diff] [blame] | 332 | #if defined(__linux__) |
| 333 | if (pollfd.revents & POLLRDHUP) { |
| 334 | events |= FDE_READ | FDE_ERROR; |
| 335 | } |
| 336 | #endif |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 337 | auto it = ctx->poll_node_map_.find(pollfd.fd); |
| 338 | CHECK(it != ctx->poll_node_map_.end()); |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 339 | fdevent* fde = it->second.fde; |
| 340 | |
| 341 | if (events == 0) { |
| 342 | // Check for timeout. |
| 343 | if (fde->timeout) { |
| 344 | auto deadline = fde->last_active + *fde->timeout; |
| 345 | if (deadline < post_poll) { |
| 346 | events |= FDE_TIMEOUT; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 351 | if (events != 0) { |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 352 | CHECK_EQ(fde->fd.get(), pollfd.fd); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 353 | fde->events |= events; |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 354 | fde->last_active = post_poll; |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 355 | D("%s got events %x", dump_fde(fde).c_str(), events); |
| 356 | fde->state |= FDE_PENDING; |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 357 | ctx->pending_list_.push_back(fde); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 362 | template <class T> |
| 363 | struct always_false : std::false_type {}; |
| 364 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 365 | static void fdevent_call_fdfunc(fdevent* fde) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 366 | unsigned events = fde->events; |
| 367 | fde->events = 0; |
Spencer Low | 888a748 | 2015-09-29 18:33:38 -0700 | [diff] [blame] | 368 | CHECK(fde->state & FDE_PENDING); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 369 | fde->state &= (~FDE_PENDING); |
| 370 | D("fdevent_call_fdfunc %s", dump_fde(fde).c_str()); |
Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 371 | std::visit( |
| 372 | [&](auto&& f) { |
| 373 | using F = std::decay_t<decltype(f)>; |
| 374 | if constexpr (std::is_same_v<fd_func, F>) { |
| 375 | f(fde->fd.get(), events, fde->arg); |
| 376 | } else if constexpr (std::is_same_v<fd_func2, F>) { |
| 377 | f(fde, events, fde->arg); |
| 378 | } else { |
| 379 | static_assert(always_false<F>::value, "non-exhaustive visitor"); |
| 380 | } |
| 381 | }, |
| 382 | fde->func); |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 385 | static void fdevent_run_flush(fdevent_context_poll* ctx) EXCLUDES(ctx->run_queue_mutex_) { |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 386 | // We need to be careful around reentrancy here, since a function we call can queue up another |
| 387 | // function. |
| 388 | while (true) { |
| 389 | std::function<void()> fn; |
| 390 | { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 391 | std::lock_guard<std::mutex> lock(ctx->run_queue_mutex_); |
| 392 | if (ctx->run_queue_.empty()) { |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 393 | break; |
| 394 | } |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 395 | fn = ctx->run_queue_.front(); |
| 396 | ctx->run_queue_.pop_front(); |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 397 | } |
| 398 | fn(); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 399 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 402 | static void fdevent_run_func(int fd, unsigned ev, void* data) { |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 403 | CHECK_GE(fd, 0); |
| 404 | CHECK(ev & FDE_READ); |
| 405 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 406 | bool* run_needs_flush = static_cast<bool*>(data); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 407 | char buf[1024]; |
| 408 | |
| 409 | // Empty the fd. |
| 410 | if (adb_read(fd, buf, sizeof(buf)) == -1) { |
| 411 | PLOG(FATAL) << "failed to empty run queue notify fd"; |
| 412 | } |
| 413 | |
Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 414 | // Mark that we need to flush, and then run it at the end of fdevent_loop. |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 415 | *run_needs_flush = true; |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 418 | static void fdevent_run_setup(fdevent_context_poll* ctx) { |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 419 | { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 420 | std::lock_guard<std::mutex> lock(ctx->run_queue_mutex_); |
| 421 | CHECK(ctx->run_queue_notify_fd_.get() == -1); |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 422 | int s[2]; |
| 423 | if (adb_socketpair(s) != 0) { |
| 424 | PLOG(FATAL) << "failed to create run queue notify socketpair"; |
| 425 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 426 | |
Josh Gao | 1222abc | 2018-03-19 15:19:45 -0700 | [diff] [blame] | 427 | if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) { |
| 428 | PLOG(FATAL) << "failed to make run queue notify socket nonblocking"; |
| 429 | } |
| 430 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 431 | ctx->run_queue_notify_fd_.reset(s[0]); |
| 432 | fdevent* fde = ctx->Create(unique_fd(s[1]), fdevent_run_func, &ctx->run_needs_flush_); |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 433 | CHECK(fde != nullptr); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 434 | ctx->Add(fde, FDE_READ); |
Josh Gao | e39ccd3 | 2018-02-23 14:37:07 -0800 | [diff] [blame] | 435 | } |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 436 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 437 | fdevent_run_flush(ctx); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 440 | void fdevent_context_poll::Run(std::function<void()> fn) { |
| 441 | std::lock_guard<std::mutex> lock(run_queue_mutex_); |
| 442 | run_queue_.push_back(std::move(fn)); |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 443 | |
| 444 | // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up. |
| 445 | // In that case, rely on the setup code to flush the queue without a notification being needed. |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 446 | if (run_queue_notify_fd_ != -1) { |
| 447 | int rc = adb_write(run_queue_notify_fd_.get(), "", 1); |
Josh Gao | 1222abc | 2018-03-19 15:19:45 -0700 | [diff] [blame] | 448 | |
| 449 | // It's possible that we get EAGAIN here, if lots of notifications came in while handling. |
| 450 | if (rc == 0) { |
| 451 | PLOG(FATAL) << "run queue notify fd was closed?"; |
| 452 | } else if (rc == -1 && errno != EAGAIN) { |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 453 | PLOG(FATAL) << "failed to write to run queue notify fd"; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 458 | static void fdevent_check_spin(fdevent_context_poll* ctx, uint64_t cycle) { |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 459 | // Check to see if we're spinning because we forgot about an fdevent |
| 460 | // by keeping track of how long fdevents have been continuously pending. |
| 461 | struct SpinCheck { |
| 462 | fdevent* fde; |
| 463 | android::base::boot_clock::time_point timestamp; |
| 464 | uint64_t cycle; |
| 465 | }; |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 466 | |
| 467 | // TODO: Move this into the base fdevent_context. |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 468 | static auto& g_continuously_pending = *new std::unordered_map<uint64_t, SpinCheck>(); |
| 469 | static auto last_cycle = android::base::boot_clock::now(); |
| 470 | |
| 471 | auto now = android::base::boot_clock::now(); |
| 472 | if (now - last_cycle > 10ms) { |
| 473 | // We're not spinning. |
| 474 | g_continuously_pending.clear(); |
| 475 | last_cycle = now; |
| 476 | return; |
| 477 | } |
| 478 | last_cycle = now; |
| 479 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 480 | for (auto* fde : ctx->pending_list_) { |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 481 | auto it = g_continuously_pending.find(fde->id); |
| 482 | if (it == g_continuously_pending.end()) { |
| 483 | g_continuously_pending[fde->id] = |
| 484 | SpinCheck{.fde = fde, .timestamp = now, .cycle = cycle}; |
| 485 | } else { |
| 486 | it->second.cycle = cycle; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | for (auto it = g_continuously_pending.begin(); it != g_continuously_pending.end();) { |
| 491 | if (it->second.cycle != cycle) { |
| 492 | it = g_continuously_pending.erase(it); |
| 493 | } else { |
| 494 | // Use an absurdly long window, since all we really care about is |
| 495 | // getting a bugreport eventually. |
| 496 | if (now - it->second.timestamp > 300s) { |
| 497 | LOG(FATAL_WITHOUT_ABORT) |
| 498 | << "detected spin in fdevent: " << dump_fde(it->second.fde); |
| 499 | #if defined(__linux__) |
| 500 | int fd = it->second.fde->fd.get(); |
| 501 | std::string fd_path = android::base::StringPrintf("/proc/self/fd/%d", fd); |
| 502 | std::string path; |
| 503 | if (!android::base::Readlink(fd_path, &path)) { |
| 504 | PLOG(FATAL_WITHOUT_ABORT) << "readlink of fd " << fd << " failed"; |
| 505 | } |
| 506 | LOG(FATAL_WITHOUT_ABORT) << "fd " << fd << " = " << path; |
| 507 | #endif |
| 508 | abort(); |
| 509 | } |
| 510 | ++it; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 515 | void fdevent_context_poll::Loop() { |
| 516 | this->main_thread_id_ = android::base::GetThreadId(); |
| 517 | this->main_thread_valid_ = true; |
| 518 | fdevent_run_setup(this); |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 519 | |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 520 | uint64_t cycle = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 521 | while (true) { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 522 | if (terminate_loop_) { |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 526 | D("--- --- waiting for events"); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 527 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 528 | fdevent_process(this); |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 529 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 530 | fdevent_check_spin(this, cycle++); |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 531 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 532 | while (!pending_list_.empty()) { |
| 533 | fdevent* fde = pending_list_.front(); |
| 534 | pending_list_.pop_front(); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 535 | fdevent_call_fdfunc(fde); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 536 | } |
Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 537 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 538 | if (run_needs_flush_) { |
| 539 | fdevent_run_flush(this); |
| 540 | run_needs_flush_ = false; |
Josh Gao | fa30bf3 | 2018-03-29 16:23:43 -0700 | [diff] [blame] | 541 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 544 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 545 | void fdevent_context_poll::TerminateLoop() { |
| 546 | terminate_loop_ = true; |
| 547 | } |
| 548 | |
| 549 | size_t fdevent_context_poll::InstalledCount() { |
| 550 | return poll_node_map_.size(); |
| 551 | } |
| 552 | |
| 553 | void fdevent_context_poll::Reset() { |
| 554 | poll_node_map_.clear(); |
| 555 | pending_list_.clear(); |
| 556 | |
| 557 | std::lock_guard<std::mutex> lock(run_queue_mutex_); |
| 558 | run_queue_notify_fd_.reset(); |
| 559 | run_queue_.clear(); |
| 560 | |
| 561 | main_thread_valid_ = false; |
| 562 | terminate_loop_ = false; |
| 563 | } |
| 564 | |
| 565 | fdevent* fdevent_create(int fd, fd_func func, void* arg) { |
| 566 | unique_fd ufd(fd); |
| 567 | return fdevent_get_ambient()->Create(std::move(ufd), func, arg); |
| 568 | } |
| 569 | |
| 570 | fdevent* fdevent_create(int fd, fd_func2 func, void* arg) { |
| 571 | unique_fd ufd(fd); |
| 572 | return fdevent_get_ambient()->Create(std::move(ufd), func, arg); |
| 573 | } |
| 574 | |
| 575 | unique_fd fdevent_release(fdevent* fde) { |
| 576 | return fdevent_get_ambient()->Destroy(fde); |
| 577 | } |
| 578 | |
| 579 | void fdevent_destroy(fdevent* fde) { |
| 580 | fdevent_get_ambient()->Destroy(fde); |
| 581 | } |
| 582 | |
| 583 | void fdevent_set(fdevent* fde, unsigned events) { |
| 584 | fdevent_get_ambient()->Set(fde, events); |
| 585 | } |
| 586 | |
| 587 | void fdevent_add(fdevent* fde, unsigned events) { |
| 588 | fdevent_get_ambient()->Add(fde, events); |
| 589 | } |
| 590 | |
| 591 | void fdevent_del(fdevent* fde, unsigned events) { |
| 592 | fdevent_get_ambient()->Del(fde, events); |
| 593 | } |
| 594 | |
| 595 | void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) { |
| 596 | fdevent_get_ambient()->SetTimeout(fde, timeout); |
| 597 | } |
| 598 | |
| 599 | void fdevent_run_on_main_thread(std::function<void()> fn) { |
| 600 | fdevent_get_ambient()->Run(std::move(fn)); |
| 601 | } |
| 602 | |
| 603 | void fdevent_loop() { |
| 604 | fdevent_get_ambient()->Loop(); |
| 605 | } |
| 606 | |
| 607 | void check_main_thread() { |
| 608 | fdevent_get_ambient()->CheckMainThread(); |
| 609 | } |
| 610 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 611 | void fdevent_terminate_loop() { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 612 | fdevent_get_ambient()->TerminateLoop(); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 615 | size_t fdevent_installed_count() { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 616 | return fdevent_get_ambient()->InstalledCount(); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | void fdevent_reset() { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame^] | 620 | return fdevent_get_ambient()->Reset(); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 621 | } |