• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c
2 **
3 ** Copyright 2006, Brian Swetland <swetland@frotz.net>
4 **
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
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
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
15 ** limitations under the License.
16 */
17 
18 #define TRACE_TAG FDEVENT
19 
20 #include "sysdeps.h"
21 #include "fdevent.h"
22 
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include <atomic>
31 #include <deque>
32 #include <functional>
33 #include <list>
34 #include <mutex>
35 #include <optional>
36 #include <unordered_map>
37 #include <utility>
38 #include <variant>
39 #include <vector>
40 
41 #include <android-base/chrono_utils.h>
42 #include <android-base/file.h>
43 #include <android-base/logging.h>
44 #include <android-base/stringprintf.h>
45 #include <android-base/thread_annotations.h>
46 #include <android-base/threads.h>
47 
48 #include "adb_io.h"
49 #include "adb_trace.h"
50 #include "adb_unique_fd.h"
51 #include "adb_utils.h"
52 #include "sysdeps/chrono.h"
53 
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 
61 struct PollNode {
62   fdevent* fde;
63   adb_pollfd pollfd;
64 
PollNodePollNode65   explicit PollNode(fdevent* fde) : fde(fde) {
66       memset(&pollfd, 0, sizeof(pollfd));
67       pollfd.fd = fde->fd.get();
68 
69 #if defined(__linux__)
70       // Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
71       // Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
72       pollfd.events = POLLRDHUP;
73 #endif
74   }
75 };
76 
77 // All operations to fdevent should happen only in the main thread.
78 // That's why we don't need a lock for fdevent.
79 static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
80 static auto& g_pending_list = *new std::list<fdevent*>();
81 static std::atomic<bool> terminate_loop(false);
82 static bool main_thread_valid;
83 static uint64_t main_thread_id;
84 
85 static uint64_t fdevent_id;
86 
87 static bool run_needs_flush = false;
88 static auto& run_queue_notify_fd = *new unique_fd();
89 static auto& run_queue_mutex = *new std::mutex();
90 static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>();
91 
check_main_thread()92 void check_main_thread() {
93     if (main_thread_valid) {
94         CHECK_EQ(main_thread_id, android::base::GetThreadId());
95     }
96 }
97 
set_main_thread()98 void set_main_thread() {
99     main_thread_valid = true;
100     main_thread_id = android::base::GetThreadId();
101 }
102 
dump_fde(const fdevent * fde)103 static std::string dump_fde(const fdevent* fde) {
104     std::string state;
105     if (fde->state & FDE_ACTIVE) {
106         state += "A";
107     }
108     if (fde->state & FDE_PENDING) {
109         state += "P";
110     }
111     if (fde->state & FDE_CREATED) {
112         state += "C";
113     }
114     if (fde->state & FDE_READ) {
115         state += "R";
116     }
117     if (fde->state & FDE_WRITE) {
118         state += "W";
119     }
120     if (fde->state & FDE_ERROR) {
121         state += "E";
122     }
123     return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(),
124                                        state.c_str());
125 }
126 
127 template <typename F>
fdevent_create_impl(int fd,F func,void * arg)128 static fdevent* fdevent_create_impl(int fd, F func, void* arg) {
129     check_main_thread();
130     CHECK_GE(fd, 0);
131 
132     fdevent* fde = new fdevent();
133     fde->id = fdevent_id++;
134     fde->state = FDE_ACTIVE;
135     fde->fd.reset(fd);
136     fde->func = func;
137     fde->arg = arg;
138     if (!set_file_block_mode(fd, false)) {
139         // Here is not proper to handle the error. If it fails here, some error is
140         // likely to be detected by poll(), then we can let the callback function
141         // to handle it.
142         LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
143     }
144     auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde));
145     CHECK(pair.second) << "install existing fd " << fd;
146 
147     fde->state |= FDE_CREATED;
148     return fde;
149 }
150 
fdevent_create(int fd,fd_func func,void * arg)151 fdevent* fdevent_create(int fd, fd_func func, void* arg) {
152     return fdevent_create_impl(fd, func, arg);
153 }
154 
fdevent_create(int fd,fd_func2 func,void * arg)155 fdevent* fdevent_create(int fd, fd_func2 func, void* arg) {
156     return fdevent_create_impl(fd, func, arg);
157 }
158 
fdevent_release(fdevent * fde)159 unique_fd fdevent_release(fdevent* fde) {
160     check_main_thread();
161     if (!fde) {
162         return {};
163     }
164 
165     if (!(fde->state & FDE_CREATED)) {
166         LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
167     }
168 
169     unique_fd result = std::move(fde->fd);
170     if (fde->state & FDE_ACTIVE) {
171         g_poll_node_map.erase(result.get());
172 
173         if (fde->state & FDE_PENDING) {
174             g_pending_list.remove(fde);
175         }
176         fde->state = 0;
177         fde->events = 0;
178     }
179 
180     delete fde;
181     return result;
182 }
183 
fdevent_destroy(fdevent * fde)184 void fdevent_destroy(fdevent* fde) {
185     // Release, and then let unique_fd's destructor cleanup.
186     fdevent_release(fde);
187 }
188 
fdevent_update(fdevent * fde,unsigned events)189 static void fdevent_update(fdevent* fde, unsigned events) {
190     auto it = g_poll_node_map.find(fde->fd.get());
191     CHECK(it != g_poll_node_map.end());
192     PollNode& node = it->second;
193     if (events & FDE_READ) {
194         node.pollfd.events |= POLLIN;
195     } else {
196         node.pollfd.events &= ~POLLIN;
197     }
198 
199     if (events & FDE_WRITE) {
200         node.pollfd.events |= POLLOUT;
201     } else {
202         node.pollfd.events &= ~POLLOUT;
203     }
204     fde->state = (fde->state & FDE_STATEMASK) | events;
205 }
206 
fdevent_set(fdevent * fde,unsigned events)207 void fdevent_set(fdevent* fde, unsigned events) {
208     check_main_thread();
209     events &= FDE_EVENTMASK;
210     if ((fde->state & FDE_EVENTMASK) == events) {
211         return;
212     }
213     CHECK(fde->state & FDE_ACTIVE);
214     fdevent_update(fde, events);
215     D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
216 
217     if (fde->state & FDE_PENDING) {
218         // If we are pending, make sure we don't signal an event that is no longer wanted.
219         fde->events &= events;
220         if (fde->events == 0) {
221             g_pending_list.remove(fde);
222             fde->state &= ~FDE_PENDING;
223         }
224     }
225 }
226 
fdevent_add(fdevent * fde,unsigned events)227 void fdevent_add(fdevent* fde, unsigned events) {
228     check_main_thread();
229     CHECK(!(events & FDE_TIMEOUT));
230     fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
231 }
232 
fdevent_del(fdevent * fde,unsigned events)233 void fdevent_del(fdevent* fde, unsigned events) {
234     check_main_thread();
235     CHECK(!(events & FDE_TIMEOUT));
236     fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
237 }
238 
fdevent_set_timeout(fdevent * fde,std::optional<std::chrono::milliseconds> timeout)239 void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) {
240     check_main_thread();
241     fde->timeout = timeout;
242     fde->last_active = std::chrono::steady_clock::now();
243 }
244 
dump_pollfds(const std::vector<adb_pollfd> & pollfds)245 static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
246     std::string result;
247     for (const auto& pollfd : pollfds) {
248         std::string op;
249         if (pollfd.events & POLLIN) {
250             op += "R";
251         }
252         if (pollfd.events & POLLOUT) {
253             op += "W";
254         }
255         android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
256     }
257     return result;
258 }
259 
calculate_timeout()260 static std::optional<std::chrono::milliseconds> calculate_timeout() {
261     std::optional<std::chrono::milliseconds> result = std::nullopt;
262     auto now = std::chrono::steady_clock::now();
263     check_main_thread();
264 
265     for (const auto& [fd, pollnode] : g_poll_node_map) {
266         UNUSED(fd);
267         auto timeout_opt = pollnode.fde->timeout;
268         if (timeout_opt) {
269             auto deadline = pollnode.fde->last_active + *timeout_opt;
270             auto time_left = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now);
271             if (time_left < std::chrono::milliseconds::zero()) {
272                 time_left = std::chrono::milliseconds::zero();
273             }
274 
275             if (!result) {
276                 result = time_left;
277             } else {
278                 result = std::min(*result, time_left);
279             }
280         }
281     }
282 
283     return result;
284 }
285 
fdevent_process()286 static void fdevent_process() {
287     std::vector<adb_pollfd> pollfds;
288     for (const auto& pair : g_poll_node_map) {
289         pollfds.push_back(pair.second.pollfd);
290     }
291     CHECK_GT(pollfds.size(), 0u);
292     D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
293 
294     auto timeout = calculate_timeout();
295     int timeout_ms;
296     if (!timeout) {
297         timeout_ms = -1;
298     } else {
299         timeout_ms = timeout->count();
300     }
301 
302     int ret = adb_poll(&pollfds[0], pollfds.size(), timeout_ms);
303     if (ret == -1) {
304         PLOG(ERROR) << "poll(), ret = " << ret;
305         return;
306     }
307 
308     auto post_poll = std::chrono::steady_clock::now();
309 
310     for (const auto& pollfd : pollfds) {
311         if (pollfd.revents != 0) {
312             D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
313         }
314         unsigned events = 0;
315         if (pollfd.revents & POLLIN) {
316             events |= FDE_READ;
317         }
318         if (pollfd.revents & POLLOUT) {
319             events |= FDE_WRITE;
320         }
321         if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
322             // We fake a read, as the rest of the code assumes that errors will
323             // be detected at that point.
324             events |= FDE_READ | FDE_ERROR;
325         }
326 #if defined(__linux__)
327         if (pollfd.revents & POLLRDHUP) {
328             events |= FDE_READ | FDE_ERROR;
329         }
330 #endif
331         auto it = g_poll_node_map.find(pollfd.fd);
332         CHECK(it != g_poll_node_map.end());
333         fdevent* fde = it->second.fde;
334 
335         if (events == 0) {
336             // Check for timeout.
337             if (fde->timeout) {
338                 auto deadline = fde->last_active + *fde->timeout;
339                 if (deadline < post_poll) {
340                     events |= FDE_TIMEOUT;
341                 }
342             }
343         }
344 
345         if (events != 0) {
346             CHECK_EQ(fde->fd.get(), pollfd.fd);
347             fde->events |= events;
348             fde->last_active = post_poll;
349             D("%s got events %x", dump_fde(fde).c_str(), events);
350             fde->state |= FDE_PENDING;
351             g_pending_list.push_back(fde);
352         }
353     }
354 }
355 
356 template <class T>
357 struct always_false : std::false_type {};
358 
fdevent_call_fdfunc(fdevent * fde)359 static void fdevent_call_fdfunc(fdevent* fde) {
360     unsigned events = fde->events;
361     fde->events = 0;
362     CHECK(fde->state & FDE_PENDING);
363     fde->state &= (~FDE_PENDING);
364     D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
365     std::visit(
366             [&](auto&& f) {
367                 using F = std::decay_t<decltype(f)>;
368                 if constexpr (std::is_same_v<fd_func, F>) {
369                     f(fde->fd.get(), events, fde->arg);
370                 } else if constexpr (std::is_same_v<fd_func2, F>) {
371                     f(fde, events, fde->arg);
372                 } else {
373                     static_assert(always_false<F>::value, "non-exhaustive visitor");
374                 }
375             },
376             fde->func);
377 }
378 
fdevent_run_flush()379 static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
380     // We need to be careful around reentrancy here, since a function we call can queue up another
381     // function.
382     while (true) {
383         std::function<void()> fn;
384         {
385             std::lock_guard<std::mutex> lock(run_queue_mutex);
386             if (run_queue.empty()) {
387                 break;
388             }
389             fn = run_queue.front();
390             run_queue.pop_front();
391         }
392         fn();
393     }
394 }
395 
fdevent_run_func(int fd,unsigned ev,void *)396 static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) {
397     CHECK_GE(fd, 0);
398     CHECK(ev & FDE_READ);
399 
400     char buf[1024];
401 
402     // Empty the fd.
403     if (adb_read(fd, buf, sizeof(buf)) == -1) {
404         PLOG(FATAL) << "failed to empty run queue notify fd";
405     }
406 
407     // Mark that we need to flush, and then run it at the end of fdevent_loop.
408     run_needs_flush = true;
409 }
410 
fdevent_run_setup()411 static void fdevent_run_setup() {
412     {
413         std::lock_guard<std::mutex> lock(run_queue_mutex);
414         CHECK(run_queue_notify_fd.get() == -1);
415         int s[2];
416         if (adb_socketpair(s) != 0) {
417             PLOG(FATAL) << "failed to create run queue notify socketpair";
418         }
419 
420         if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) {
421             PLOG(FATAL) << "failed to make run queue notify socket nonblocking";
422         }
423 
424         run_queue_notify_fd.reset(s[0]);
425         fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
426         CHECK(fde != nullptr);
427         fdevent_add(fde, FDE_READ);
428     }
429 
430     fdevent_run_flush();
431 }
432 
fdevent_run_on_main_thread(std::function<void ()> fn)433 void fdevent_run_on_main_thread(std::function<void()> fn) {
434     std::lock_guard<std::mutex> lock(run_queue_mutex);
435     run_queue.push_back(std::move(fn));
436 
437     // run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
438     // In that case, rely on the setup code to flush the queue without a notification being needed.
439     if (run_queue_notify_fd != -1) {
440         int rc = adb_write(run_queue_notify_fd.get(), "", 1);
441 
442         // It's possible that we get EAGAIN here, if lots of notifications came in while handling.
443         if (rc == 0) {
444             PLOG(FATAL) << "run queue notify fd was closed?";
445         } else if (rc == -1 && errno != EAGAIN) {
446             PLOG(FATAL) << "failed to write to run queue notify fd";
447         }
448     }
449 }
450 
fdevent_check_spin(uint64_t cycle)451 static void fdevent_check_spin(uint64_t cycle) {
452     // Check to see if we're spinning because we forgot about an fdevent
453     // by keeping track of how long fdevents have been continuously pending.
454     struct SpinCheck {
455         fdevent* fde;
456         android::base::boot_clock::time_point timestamp;
457         uint64_t cycle;
458     };
459     static auto& g_continuously_pending = *new std::unordered_map<uint64_t, SpinCheck>();
460     static auto last_cycle = android::base::boot_clock::now();
461 
462     auto now = android::base::boot_clock::now();
463     if (now - last_cycle > 10ms) {
464         // We're not spinning.
465         g_continuously_pending.clear();
466         last_cycle = now;
467         return;
468     }
469     last_cycle = now;
470 
471     for (auto* fde : g_pending_list) {
472         auto it = g_continuously_pending.find(fde->id);
473         if (it == g_continuously_pending.end()) {
474             g_continuously_pending[fde->id] =
475                     SpinCheck{.fde = fde, .timestamp = now, .cycle = cycle};
476         } else {
477             it->second.cycle = cycle;
478         }
479     }
480 
481     for (auto it = g_continuously_pending.begin(); it != g_continuously_pending.end();) {
482         if (it->second.cycle != cycle) {
483             it = g_continuously_pending.erase(it);
484         } else {
485             // Use an absurdly long window, since all we really care about is
486             // getting a bugreport eventually.
487             if (now - it->second.timestamp > 300s) {
488                 LOG(FATAL_WITHOUT_ABORT)
489                         << "detected spin in fdevent: " << dump_fde(it->second.fde);
490 #if defined(__linux__)
491                 int fd = it->second.fde->fd.get();
492                 std::string fd_path = android::base::StringPrintf("/proc/self/fd/%d", fd);
493                 std::string path;
494                 if (!android::base::Readlink(fd_path, &path)) {
495                     PLOG(FATAL_WITHOUT_ABORT) << "readlink of fd " << fd << " failed";
496                 }
497                 LOG(FATAL_WITHOUT_ABORT) << "fd " << fd << " = " << path;
498 #endif
499                 abort();
500             }
501             ++it;
502         }
503     }
504 }
505 
fdevent_loop()506 void fdevent_loop() {
507     set_main_thread();
508     fdevent_run_setup();
509 
510     uint64_t cycle = 0;
511     while (true) {
512         if (terminate_loop) {
513             return;
514         }
515 
516         D("--- --- waiting for events");
517 
518         fdevent_process();
519 
520         fdevent_check_spin(cycle++);
521 
522         while (!g_pending_list.empty()) {
523             fdevent* fde = g_pending_list.front();
524             g_pending_list.pop_front();
525             fdevent_call_fdfunc(fde);
526         }
527 
528         if (run_needs_flush) {
529             fdevent_run_flush();
530             run_needs_flush = false;
531         }
532     }
533 }
534 
fdevent_terminate_loop()535 void fdevent_terminate_loop() {
536     terminate_loop = true;
537 }
538 
fdevent_installed_count()539 size_t fdevent_installed_count() {
540     return g_poll_node_map.size();
541 }
542 
fdevent_reset()543 void fdevent_reset() {
544     g_poll_node_map.clear();
545     g_pending_list.clear();
546 
547     std::lock_guard<std::mutex> lock(run_queue_mutex);
548     run_queue_notify_fd.reset();
549     run_queue.clear();
550 
551     main_thread_valid = false;
552     terminate_loop = false;
553 }
554