blob: 512905d683f9eba7b65347c0ba9309c1a8bf17da [file] [log] [blame]
Craig Tillerc67cc992017-04-27 10:15:51 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2017 gRPC authors.
Craig Tillerc67cc992017-04-27 10:15:51 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tillerc67cc992017-04-27 10:15:51 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tillerc67cc992017-04-27 10:15:51 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tillerc67cc992017-04-27 10:15:51 -070016 *
17 */
18
19#include "src/core/lib/iomgr/port.h"
20
yang-gceb24752017-11-07 12:06:37 -080021#include <grpc/support/log.h>
22
Mehrdad Afshari8b0e9fb2018-01-17 13:42:26 -080023/* This polling engine is only relevant on linux kernels supporting epoll
24 epoll_create() or epoll_create1() */
Mehrdad Afsharifb669002018-01-17 15:37:56 -080025#ifdef GRPC_LINUX_EPOLL
Craig Tiller4509c472017-04-27 19:05:13 +000026#include "src/core/lib/iomgr/ev_epoll1_linux.h"
Craig Tillerc67cc992017-04-27 10:15:51 -070027
28#include <assert.h>
29#include <errno.h>
Mehrdad Afshari1957fd02018-01-16 17:22:01 -080030#include <fcntl.h>
Craig Tiller20397792017-07-18 11:35:27 -070031#include <limits.h>
Craig Tillerc67cc992017-04-27 10:15:51 -070032#include <poll.h>
33#include <pthread.h>
34#include <string.h>
35#include <sys/epoll.h>
36#include <sys/socket.h>
37#include <unistd.h>
38
39#include <grpc/support/alloc.h>
Craig Tiller6de05932017-04-28 09:17:38 -070040#include <grpc/support/cpu.h>
Craig Tillerc67cc992017-04-27 10:15:51 -070041#include <grpc/support/string_util.h>
42#include <grpc/support/tls.h>
Craig Tillerc67cc992017-04-27 10:15:51 -070043
Craig Tillerb4bb1cd2017-07-20 14:18:17 -070044#include "src/core/lib/debug/stats.h"
Mark D. Rothdbdf4952018-01-18 11:21:12 -080045#include "src/core/lib/gpr/string.h"
Vijay Paid4d0a302018-01-25 13:24:03 -080046#include "src/core/lib/gpr/useful.h"
Mark D. Roth4f2b0fd2018-01-19 12:12:23 -080047#include "src/core/lib/gprpp/manual_constructor.h"
Craig Tiller6b7c1fb2017-07-19 15:45:03 -070048#include "src/core/lib/iomgr/block_annotate.h"
Craig Tillerc67cc992017-04-27 10:15:51 -070049#include "src/core/lib/iomgr/ev_posix.h"
50#include "src/core/lib/iomgr/iomgr_internal.h"
51#include "src/core/lib/iomgr/lockfree_event.h"
Craig Tillerc67cc992017-04-27 10:15:51 -070052#include "src/core/lib/iomgr/wakeup_fd_posix.h"
Craig Tillerc67cc992017-04-27 10:15:51 -070053#include "src/core/lib/profiling/timers.h"
Craig Tillerc67cc992017-04-27 10:15:51 -070054
Craig Tillerc67cc992017-04-27 10:15:51 -070055static grpc_wakeup_fd global_wakeup_fd;
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -070056
57/*******************************************************************************
58 * Singleton epoll set related fields
59 */
60
61#define MAX_EPOLL_EVENTS 100
Sree Kuchibhotla19614522017-08-25 17:10:10 -070062#define MAX_EPOLL_EVENTS_HANDLED_PER_ITERATION 1
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -070063
Sree Kuchibhotlae01940f2017-08-27 18:10:12 -070064/* NOTE ON SYNCHRONIZATION:
65 * - Fields in this struct are only modified by the designated poller. Hence
66 * there is no need for any locks to protect the struct.
67 * - num_events and cursor fields have to be of atomic type to provide memory
68 * visibility guarantees only. i.e In case of multiple pollers, the designated
69 * polling thread keeps changing; the thread that wrote these values may be
70 * different from the thread reading the values
71 */
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -070072typedef struct epoll_set {
73 int epfd;
74
75 /* The epoll_events after the last call to epoll_wait() */
76 struct epoll_event events[MAX_EPOLL_EVENTS];
77
78 /* The number of epoll_events after the last call to epoll_wait() */
Sree Kuchibhotlaa92a9cc2017-08-27 14:02:15 -070079 gpr_atm num_events;
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -070080
81 /* Index of the first event in epoll_events that has to be processed. This
82 * field is only valid if num_events > 0 */
Sree Kuchibhotlaa92a9cc2017-08-27 14:02:15 -070083 gpr_atm cursor;
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -070084} epoll_set;
85
86/* The global singleton epoll set */
87static epoll_set g_epoll_set;
88
Mehrdad Afshari8b0e9fb2018-01-17 13:42:26 -080089static int epoll_create_and_cloexec() {
Mehrdad Afshari1957fd02018-01-16 17:22:01 -080090#ifdef GRPC_LINUX_EPOLL_CREATE1
91 int fd = epoll_create1(EPOLL_CLOEXEC);
Mehrdad Afshari8b0e9fb2018-01-17 13:42:26 -080092 if (fd < 0) {
93 gpr_log(GPR_ERROR, "epoll_create1 unavailable");
Mehrdad Afshari1957fd02018-01-16 17:22:01 -080094 }
Mehrdad Afshari1957fd02018-01-16 17:22:01 -080095#else
96 int fd = epoll_create(MAX_EPOLL_EVENTS);
97 if (fd < 0) {
98 gpr_log(GPR_ERROR, "epoll_create unavailable");
Mehrdad Afshari8b0e9fb2018-01-17 13:42:26 -080099 } else if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
100 gpr_log(GPR_ERROR, "fcntl following epoll_create failed");
Mehrdad Afshari1957fd02018-01-16 17:22:01 -0800101 return -1;
102 }
Mehrdad Afshari1957fd02018-01-16 17:22:01 -0800103#endif
Mehrdad Afshari8b0e9fb2018-01-17 13:42:26 -0800104 return fd;
Mehrdad Afshari1957fd02018-01-16 17:22:01 -0800105}
106
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700107/* Must be called *only* once */
108static bool epoll_set_init() {
Mehrdad Afshari8b0e9fb2018-01-17 13:42:26 -0800109 g_epoll_set.epfd = epoll_create_and_cloexec();
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700110 if (g_epoll_set.epfd < 0) {
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700111 return false;
112 }
113
Sree Kuchibhotlaa92a9cc2017-08-27 14:02:15 -0700114 gpr_log(GPR_INFO, "grpc epoll fd: %d", g_epoll_set.epfd);
115 gpr_atm_no_barrier_store(&g_epoll_set.num_events, 0);
116 gpr_atm_no_barrier_store(&g_epoll_set.cursor, 0);
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700117 return true;
118}
119
120/* epoll_set_init() MUST be called before calling this. */
121static void epoll_set_shutdown() {
122 if (g_epoll_set.epfd >= 0) {
123 close(g_epoll_set.epfd);
124 g_epoll_set.epfd = -1;
125 }
126}
Craig Tillerc67cc992017-04-27 10:15:51 -0700127
128/*******************************************************************************
129 * Fd Declarations
130 */
131
132struct grpc_fd {
133 int fd;
134
Craig Tillerfbf61bb2017-11-08 11:50:14 -0800135 grpc_core::ManualConstructor<grpc_core::LockfreeEvent> read_closure;
136 grpc_core::ManualConstructor<grpc_core::LockfreeEvent> write_closure;
Craig Tillerc67cc992017-04-27 10:15:51 -0700137
Craig Tillerbaa14a92017-11-03 09:09:36 -0700138 struct grpc_fd* freelist_next;
Craig Tillerc67cc992017-04-27 10:15:51 -0700139
140 /* The pollset that last noticed that the fd is readable. The actual type
141 * stored in this is (grpc_pollset *) */
142 gpr_atm read_notifier_pollset;
143
144 grpc_iomgr_object iomgr_object;
145};
146
147static void fd_global_init(void);
148static void fd_global_shutdown(void);
149
150/*******************************************************************************
151 * Pollset Declarations
152 */
153
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700154typedef enum { UNKICKED, KICKED, DESIGNATED_POLLER } kick_state;
Craig Tillerc67cc992017-04-27 10:15:51 -0700155
Craig Tillerbaa14a92017-11-03 09:09:36 -0700156static const char* kick_state_string(kick_state st) {
Craig Tiller830e82a2017-05-31 16:26:27 -0700157 switch (st) {
158 case UNKICKED:
159 return "UNKICKED";
160 case KICKED:
161 return "KICKED";
162 case DESIGNATED_POLLER:
163 return "DESIGNATED_POLLER";
164 }
165 GPR_UNREACHABLE_CODE(return "UNKNOWN");
166}
167
Craig Tillerc67cc992017-04-27 10:15:51 -0700168struct grpc_pollset_worker {
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700169 kick_state state;
Craig Tiller55624a32017-05-26 08:14:44 -0700170 int kick_state_mutator; // which line of code last changed kick state
Craig Tillerc67cc992017-04-27 10:15:51 -0700171 bool initialized_cv;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700172 grpc_pollset_worker* next;
173 grpc_pollset_worker* prev;
Craig Tillerc67cc992017-04-27 10:15:51 -0700174 gpr_cv cv;
Craig Tiller50da5ec2017-05-01 13:51:14 -0700175 grpc_closure_list schedule_on_end_work;
Craig Tillerc67cc992017-04-27 10:15:51 -0700176};
177
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700178#define SET_KICK_STATE(worker, kick_state) \
Craig Tiller55624a32017-05-26 08:14:44 -0700179 do { \
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700180 (worker)->state = (kick_state); \
Craig Tiller55624a32017-05-26 08:14:44 -0700181 (worker)->kick_state_mutator = __LINE__; \
182 } while (false)
183
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700184#define MAX_NEIGHBORHOODS 1024
Craig Tillerba550da2017-05-01 14:26:31 +0000185
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700186typedef struct pollset_neighborhood {
Craig Tiller6de05932017-04-28 09:17:38 -0700187 gpr_mu mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700188 grpc_pollset* active_root;
Craig Tiller6de05932017-04-28 09:17:38 -0700189 char pad[GPR_CACHELINE_SIZE];
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700190} pollset_neighborhood;
Craig Tiller6de05932017-04-28 09:17:38 -0700191
Craig Tillerc67cc992017-04-27 10:15:51 -0700192struct grpc_pollset {
Craig Tiller6de05932017-04-28 09:17:38 -0700193 gpr_mu mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700194 pollset_neighborhood* neighborhood;
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700195 bool reassigning_neighborhood;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700196 grpc_pollset_worker* root_worker;
Craig Tiller4509c472017-04-27 19:05:13 +0000197 bool kicked_without_poller;
Sree Kuchibhotla0d8431a2017-07-18 16:21:54 -0700198
199 /* Set to true if the pollset is observed to have no workers available to
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700200 poll */
Craig Tiller6de05932017-04-28 09:17:38 -0700201 bool seen_inactive;
Sree Kuchibhotla0d8431a2017-07-18 16:21:54 -0700202 bool shutting_down; /* Is the pollset shutting down ? */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700203 grpc_closure* shutdown_closure; /* Called after after shutdown is complete */
Sree Kuchibhotla0d8431a2017-07-18 16:21:54 -0700204
205 /* Number of workers who are *about-to* attach themselves to the pollset
206 * worker list */
Craig Tillerba550da2017-05-01 14:26:31 +0000207 int begin_refs;
Craig Tiller6de05932017-04-28 09:17:38 -0700208
Craig Tillerbaa14a92017-11-03 09:09:36 -0700209 grpc_pollset* next;
210 grpc_pollset* prev;
Craig Tillerc67cc992017-04-27 10:15:51 -0700211};
212
213/*******************************************************************************
214 * Pollset-set Declarations
215 */
Craig Tiller6de05932017-04-28 09:17:38 -0700216
Craig Tiller61f96c12017-05-12 13:36:39 -0700217struct grpc_pollset_set {
218 char unused;
219};
Craig Tillerc67cc992017-04-27 10:15:51 -0700220
221/*******************************************************************************
222 * Common helpers
223 */
224
Craig Tillerbaa14a92017-11-03 09:09:36 -0700225static bool append_error(grpc_error** composite, grpc_error* error,
226 const char* desc) {
Craig Tillerc67cc992017-04-27 10:15:51 -0700227 if (error == GRPC_ERROR_NONE) return true;
228 if (*composite == GRPC_ERROR_NONE) {
229 *composite = GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc);
230 }
231 *composite = grpc_error_add_child(*composite, error);
232 return false;
233}
234
235/*******************************************************************************
236 * Fd Definitions
237 */
238
239/* We need to keep a freelist not because of any concerns of malloc performance
240 * but instead so that implementations with multiple threads in (for example)
241 * epoll_wait deal with the race between pollset removal and incoming poll
242 * notifications.
243 *
244 * The problem is that the poller ultimately holds a reference to this
245 * object, so it is very difficult to know when is safe to free it, at least
246 * without some expensive synchronization.
247 *
248 * If we keep the object freelisted, in the worst case losing this race just
249 * becomes a spurious read notification on a reused fd.
250 */
251
252/* The alarm system needs to be able to wakeup 'some poller' sometimes
253 * (specifically when a new alarm needs to be triggered earlier than the next
254 * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a
255 * case occurs. */
256
Craig Tiller4782d922017-11-10 09:53:21 -0800257static grpc_fd* fd_freelist = nullptr;
Craig Tillerc67cc992017-04-27 10:15:51 -0700258static gpr_mu fd_freelist_mu;
259
Craig Tillerc67cc992017-04-27 10:15:51 -0700260static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); }
261
262static void fd_global_shutdown(void) {
263 gpr_mu_lock(&fd_freelist_mu);
264 gpr_mu_unlock(&fd_freelist_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800265 while (fd_freelist != nullptr) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700266 grpc_fd* fd = fd_freelist;
Craig Tillerc67cc992017-04-27 10:15:51 -0700267 fd_freelist = fd_freelist->freelist_next;
Craig Tillerc67cc992017-04-27 10:15:51 -0700268 gpr_free(fd);
269 }
270 gpr_mu_destroy(&fd_freelist_mu);
271}
272
Craig Tillerbaa14a92017-11-03 09:09:36 -0700273static grpc_fd* fd_create(int fd, const char* name) {
Craig Tiller4782d922017-11-10 09:53:21 -0800274 grpc_fd* new_fd = nullptr;
Craig Tillerc67cc992017-04-27 10:15:51 -0700275
276 gpr_mu_lock(&fd_freelist_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800277 if (fd_freelist != nullptr) {
Craig Tillerc67cc992017-04-27 10:15:51 -0700278 new_fd = fd_freelist;
279 fd_freelist = fd_freelist->freelist_next;
280 }
281 gpr_mu_unlock(&fd_freelist_mu);
282
Craig Tiller4782d922017-11-10 09:53:21 -0800283 if (new_fd == nullptr) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700284 new_fd = (grpc_fd*)gpr_malloc(sizeof(grpc_fd));
yang-g26521b32017-11-17 17:15:37 -0800285 new_fd->read_closure.Init();
286 new_fd->write_closure.Init();
Craig Tillerc67cc992017-04-27 10:15:51 -0700287 }
288
Craig Tillerc67cc992017-04-27 10:15:51 -0700289 new_fd->fd = fd;
yang-ged49fe52017-11-20 13:49:54 -0800290 new_fd->read_closure->InitEvent();
291 new_fd->write_closure->InitEvent();
Craig Tillerc67cc992017-04-27 10:15:51 -0700292 gpr_atm_no_barrier_store(&new_fd->read_notifier_pollset, (gpr_atm)NULL);
293
Craig Tiller4782d922017-11-10 09:53:21 -0800294 new_fd->freelist_next = nullptr;
Craig Tillerc67cc992017-04-27 10:15:51 -0700295
Craig Tillerbaa14a92017-11-03 09:09:36 -0700296 char* fd_name;
Craig Tillerc67cc992017-04-27 10:15:51 -0700297 gpr_asprintf(&fd_name, "%s fd=%d", name, fd);
298 grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name);
Noah Eisen264879f2017-06-20 17:14:47 -0700299#ifndef NDEBUG
ncteisen3cffe1f2017-11-10 13:56:23 -0800300 if (grpc_trace_fd_refcount.enabled()) {
Noah Eisen264879f2017-06-20 17:14:47 -0700301 gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, new_fd, fd_name);
302 }
Craig Tillerc67cc992017-04-27 10:15:51 -0700303#endif
304 gpr_free(fd_name);
Craig Tiller9ddb3152017-04-27 21:32:56 +0000305
Yash Tibrewal533d1182017-09-18 10:48:22 -0700306 struct epoll_event ev;
307 ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET);
308 ev.data.ptr = new_fd;
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700309 if (epoll_ctl(g_epoll_set.epfd, EPOLL_CTL_ADD, fd, &ev) != 0) {
Craig Tiller9ddb3152017-04-27 21:32:56 +0000310 gpr_log(GPR_ERROR, "epoll_ctl failed: %s", strerror(errno));
311 }
312
Craig Tillerc67cc992017-04-27 10:15:51 -0700313 return new_fd;
314}
315
Craig Tillerbaa14a92017-11-03 09:09:36 -0700316static int fd_wrapped_fd(grpc_fd* fd) { return fd->fd; }
Craig Tillerc67cc992017-04-27 10:15:51 -0700317
Sree Kuchibhotlaf2641472017-08-02 23:46:40 -0700318/* if 'releasing_fd' is true, it means that we are going to detach the internal
319 * fd from grpc_fd structure (i.e which means we should not be calling
320 * shutdown() syscall on that fd) */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800321static void fd_shutdown_internal(grpc_fd* fd, grpc_error* why,
322 bool releasing_fd) {
323 if (fd->read_closure->SetShutdown(GRPC_ERROR_REF(why))) {
Sree Kuchibhotlaf2641472017-08-02 23:46:40 -0700324 if (!releasing_fd) {
325 shutdown(fd->fd, SHUT_RDWR);
326 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800327 fd->write_closure->SetShutdown(GRPC_ERROR_REF(why));
Craig Tiller9ddb3152017-04-27 21:32:56 +0000328 }
329 GRPC_ERROR_UNREF(why);
330}
331
Sree Kuchibhotlaf2641472017-08-02 23:46:40 -0700332/* Might be called multiple times */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800333static void fd_shutdown(grpc_fd* fd, grpc_error* why) {
334 fd_shutdown_internal(fd, why, false);
Sree Kuchibhotlaf2641472017-08-02 23:46:40 -0700335}
336
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800337static void fd_orphan(grpc_fd* fd, grpc_closure* on_done, int* release_fd,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700338 bool already_closed, const char* reason) {
339 grpc_error* error = GRPC_ERROR_NONE;
Craig Tiller4782d922017-11-10 09:53:21 -0800340 bool is_release_fd = (release_fd != nullptr);
Craig Tillerc67cc992017-04-27 10:15:51 -0700341
Craig Tillerfbf61bb2017-11-08 11:50:14 -0800342 if (!fd->read_closure->IsShutdown()) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800343 fd_shutdown_internal(fd, GRPC_ERROR_CREATE_FROM_COPIED_STRING(reason),
Sree Kuchibhotlaf2641472017-08-02 23:46:40 -0700344 is_release_fd);
Craig Tiller9ddb3152017-04-27 21:32:56 +0000345 }
346
Craig Tillerc67cc992017-04-27 10:15:51 -0700347 /* If release_fd is not NULL, we should be relinquishing control of the file
348 descriptor fd->fd (but we still own the grpc_fd structure). */
Sree Kuchibhotlaf2641472017-08-02 23:46:40 -0700349 if (is_release_fd) {
Craig Tillerc67cc992017-04-27 10:15:51 -0700350 *release_fd = fd->fd;
Yuchen Zengd40a7ae2017-07-12 15:59:56 -0700351 } else if (!already_closed) {
Craig Tillerc67cc992017-04-27 10:15:51 -0700352 close(fd->fd);
Craig Tillerc67cc992017-04-27 10:15:51 -0700353 }
354
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800355 GRPC_CLOSURE_SCHED(on_done, GRPC_ERROR_REF(error));
Craig Tillerc67cc992017-04-27 10:15:51 -0700356
Craig Tiller4509c472017-04-27 19:05:13 +0000357 grpc_iomgr_unregister_object(&fd->iomgr_object);
yang-ged49fe52017-11-20 13:49:54 -0800358 fd->read_closure->DestroyEvent();
359 fd->write_closure->DestroyEvent();
Craig Tillerc67cc992017-04-27 10:15:51 -0700360
Craig Tiller4509c472017-04-27 19:05:13 +0000361 gpr_mu_lock(&fd_freelist_mu);
362 fd->freelist_next = fd_freelist;
363 fd_freelist = fd;
364 gpr_mu_unlock(&fd_freelist_mu);
Craig Tillerc67cc992017-04-27 10:15:51 -0700365}
366
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800367static grpc_pollset* fd_get_read_notifier_pollset(grpc_fd* fd) {
Craig Tillerc67cc992017-04-27 10:15:51 -0700368 gpr_atm notifier = gpr_atm_acq_load(&fd->read_notifier_pollset);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700369 return (grpc_pollset*)notifier;
Craig Tillerc67cc992017-04-27 10:15:51 -0700370}
371
Craig Tillerbaa14a92017-11-03 09:09:36 -0700372static bool fd_is_shutdown(grpc_fd* fd) {
Craig Tillerfbf61bb2017-11-08 11:50:14 -0800373 return fd->read_closure->IsShutdown();
Craig Tillerc67cc992017-04-27 10:15:51 -0700374}
375
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800376static void fd_notify_on_read(grpc_fd* fd, grpc_closure* closure) {
377 fd->read_closure->NotifyOn(closure);
Craig Tillerc67cc992017-04-27 10:15:51 -0700378}
379
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800380static void fd_notify_on_write(grpc_fd* fd, grpc_closure* closure) {
381 fd->write_closure->NotifyOn(closure);
Craig Tillerc67cc992017-04-27 10:15:51 -0700382}
383
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800384static void fd_become_readable(grpc_fd* fd, grpc_pollset* notifier) {
385 fd->read_closure->SetReady();
Craig Tiller4509c472017-04-27 19:05:13 +0000386 /* Use release store to match with acquire load in fd_get_read_notifier */
387 gpr_atm_rel_store(&fd->read_notifier_pollset, (gpr_atm)notifier);
388}
389
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800390static void fd_become_writable(grpc_fd* fd) { fd->write_closure->SetReady(); }
Craig Tillerc67cc992017-04-27 10:15:51 -0700391
392/*******************************************************************************
393 * Pollset Definitions
394 */
395
Craig Tiller6de05932017-04-28 09:17:38 -0700396GPR_TLS_DECL(g_current_thread_pollset);
397GPR_TLS_DECL(g_current_thread_worker);
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700398
399/* The designated poller */
Craig Tiller6de05932017-04-28 09:17:38 -0700400static gpr_atm g_active_poller;
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700401
Craig Tillerbaa14a92017-11-03 09:09:36 -0700402static pollset_neighborhood* g_neighborhoods;
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700403static size_t g_num_neighborhoods;
Craig Tiller6de05932017-04-28 09:17:38 -0700404
Craig Tillerc67cc992017-04-27 10:15:51 -0700405/* Return true if first in list */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700406static bool worker_insert(grpc_pollset* pollset, grpc_pollset_worker* worker) {
Craig Tiller4782d922017-11-10 09:53:21 -0800407 if (pollset->root_worker == nullptr) {
Craig Tiller32f90ee2017-04-28 12:46:41 -0700408 pollset->root_worker = worker;
409 worker->next = worker->prev = worker;
Craig Tillerc67cc992017-04-27 10:15:51 -0700410 return true;
411 } else {
Craig Tiller32f90ee2017-04-28 12:46:41 -0700412 worker->next = pollset->root_worker;
413 worker->prev = worker->next->prev;
414 worker->next->prev = worker;
415 worker->prev->next = worker;
Craig Tillerc67cc992017-04-27 10:15:51 -0700416 return false;
417 }
418}
419
420/* Return true if last in list */
421typedef enum { EMPTIED, NEW_ROOT, REMOVED } worker_remove_result;
422
Craig Tillerbaa14a92017-11-03 09:09:36 -0700423static worker_remove_result worker_remove(grpc_pollset* pollset,
424 grpc_pollset_worker* worker) {
Craig Tiller32f90ee2017-04-28 12:46:41 -0700425 if (worker == pollset->root_worker) {
426 if (worker == worker->next) {
Craig Tiller4782d922017-11-10 09:53:21 -0800427 pollset->root_worker = nullptr;
Craig Tillerc67cc992017-04-27 10:15:51 -0700428 return EMPTIED;
429 } else {
Craig Tiller32f90ee2017-04-28 12:46:41 -0700430 pollset->root_worker = worker->next;
431 worker->prev->next = worker->next;
432 worker->next->prev = worker->prev;
Craig Tillerc67cc992017-04-27 10:15:51 -0700433 return NEW_ROOT;
434 }
435 } else {
Craig Tiller32f90ee2017-04-28 12:46:41 -0700436 worker->prev->next = worker->next;
437 worker->next->prev = worker->prev;
Craig Tillerc67cc992017-04-27 10:15:51 -0700438 return REMOVED;
439 }
440}
441
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700442static size_t choose_neighborhood(void) {
443 return (size_t)gpr_cpu_current_cpu() % g_num_neighborhoods;
Craig Tillerba550da2017-05-01 14:26:31 +0000444}
445
Craig Tillerbaa14a92017-11-03 09:09:36 -0700446static grpc_error* pollset_global_init(void) {
Craig Tiller4509c472017-04-27 19:05:13 +0000447 gpr_tls_init(&g_current_thread_pollset);
448 gpr_tls_init(&g_current_thread_worker);
Craig Tiller6de05932017-04-28 09:17:38 -0700449 gpr_atm_no_barrier_store(&g_active_poller, 0);
Craig Tiller375eb252017-04-27 23:29:12 +0000450 global_wakeup_fd.read_fd = -1;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700451 grpc_error* err = grpc_wakeup_fd_init(&global_wakeup_fd);
Craig Tiller375eb252017-04-27 23:29:12 +0000452 if (err != GRPC_ERROR_NONE) return err;
Yash Tibrewal533d1182017-09-18 10:48:22 -0700453 struct epoll_event ev;
454 ev.events = (uint32_t)(EPOLLIN | EPOLLET);
455 ev.data.ptr = &global_wakeup_fd;
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700456 if (epoll_ctl(g_epoll_set.epfd, EPOLL_CTL_ADD, global_wakeup_fd.read_fd,
457 &ev) != 0) {
Craig Tiller4509c472017-04-27 19:05:13 +0000458 return GRPC_OS_ERROR(errno, "epoll_ctl");
459 }
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700460 g_num_neighborhoods = GPR_CLAMP(gpr_cpu_num_cores(), 1, MAX_NEIGHBORHOODS);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700461 g_neighborhoods = (pollset_neighborhood*)gpr_zalloc(sizeof(*g_neighborhoods) *
462 g_num_neighborhoods);
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700463 for (size_t i = 0; i < g_num_neighborhoods; i++) {
464 gpr_mu_init(&g_neighborhoods[i].mu);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700465 }
Craig Tiller4509c472017-04-27 19:05:13 +0000466 return GRPC_ERROR_NONE;
467}
468
469static void pollset_global_shutdown(void) {
Craig Tiller4509c472017-04-27 19:05:13 +0000470 gpr_tls_destroy(&g_current_thread_pollset);
471 gpr_tls_destroy(&g_current_thread_worker);
Craig Tiller375eb252017-04-27 23:29:12 +0000472 if (global_wakeup_fd.read_fd != -1) grpc_wakeup_fd_destroy(&global_wakeup_fd);
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700473 for (size_t i = 0; i < g_num_neighborhoods; i++) {
474 gpr_mu_destroy(&g_neighborhoods[i].mu);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700475 }
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700476 gpr_free(g_neighborhoods);
Craig Tiller4509c472017-04-27 19:05:13 +0000477}
478
Craig Tillerbaa14a92017-11-03 09:09:36 -0700479static void pollset_init(grpc_pollset* pollset, gpr_mu** mu) {
Craig Tiller6de05932017-04-28 09:17:38 -0700480 gpr_mu_init(&pollset->mu);
481 *mu = &pollset->mu;
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700482 pollset->neighborhood = &g_neighborhoods[choose_neighborhood()];
483 pollset->reassigning_neighborhood = false;
Craig Tiller4782d922017-11-10 09:53:21 -0800484 pollset->root_worker = nullptr;
Sree Kuchibhotla30882302017-08-16 13:46:52 -0700485 pollset->kicked_without_poller = false;
Craig Tiller6de05932017-04-28 09:17:38 -0700486 pollset->seen_inactive = true;
Sree Kuchibhotla30882302017-08-16 13:46:52 -0700487 pollset->shutting_down = false;
Craig Tiller4782d922017-11-10 09:53:21 -0800488 pollset->shutdown_closure = nullptr;
Sree Kuchibhotla30882302017-08-16 13:46:52 -0700489 pollset->begin_refs = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800490 pollset->next = pollset->prev = nullptr;
Craig Tiller6de05932017-04-28 09:17:38 -0700491}
492
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800493static void pollset_destroy(grpc_pollset* pollset) {
Craig Tillere00d7332017-05-01 15:43:51 +0000494 gpr_mu_lock(&pollset->mu);
Craig Tillerba550da2017-05-01 14:26:31 +0000495 if (!pollset->seen_inactive) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700496 pollset_neighborhood* neighborhood = pollset->neighborhood;
Craig Tillere00d7332017-05-01 15:43:51 +0000497 gpr_mu_unlock(&pollset->mu);
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700498 retry_lock_neighborhood:
499 gpr_mu_lock(&neighborhood->mu);
Craig Tillere00d7332017-05-01 15:43:51 +0000500 gpr_mu_lock(&pollset->mu);
501 if (!pollset->seen_inactive) {
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700502 if (pollset->neighborhood != neighborhood) {
503 gpr_mu_unlock(&neighborhood->mu);
504 neighborhood = pollset->neighborhood;
Craig Tillere00d7332017-05-01 15:43:51 +0000505 gpr_mu_unlock(&pollset->mu);
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700506 goto retry_lock_neighborhood;
Craig Tillere00d7332017-05-01 15:43:51 +0000507 }
508 pollset->prev->next = pollset->next;
509 pollset->next->prev = pollset->prev;
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700510 if (pollset == pollset->neighborhood->active_root) {
511 pollset->neighborhood->active_root =
Craig Tiller4782d922017-11-10 09:53:21 -0800512 pollset->next == pollset ? nullptr : pollset->next;
Craig Tillere00d7332017-05-01 15:43:51 +0000513 }
Craig Tillerba550da2017-05-01 14:26:31 +0000514 }
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700515 gpr_mu_unlock(&pollset->neighborhood->mu);
Craig Tiller6de05932017-04-28 09:17:38 -0700516 }
Craig Tillere00d7332017-05-01 15:43:51 +0000517 gpr_mu_unlock(&pollset->mu);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700518 gpr_mu_destroy(&pollset->mu);
Craig Tiller4509c472017-04-27 19:05:13 +0000519}
520
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800521static grpc_error* pollset_kick_all(grpc_pollset* pollset) {
yang-gce1cfea2018-01-31 15:59:50 -0800522 GPR_TIMER_SCOPE("pollset_kick_all", 0);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700523 grpc_error* error = GRPC_ERROR_NONE;
Craig Tiller4782d922017-11-10 09:53:21 -0800524 if (pollset->root_worker != nullptr) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700525 grpc_pollset_worker* worker = pollset->root_worker;
Craig Tiller4509c472017-04-27 19:05:13 +0000526 do {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800527 GRPC_STATS_INC_POLLSET_KICK();
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700528 switch (worker->state) {
Craig Tiller55624a32017-05-26 08:14:44 -0700529 case KICKED:
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800530 GRPC_STATS_INC_POLLSET_KICKED_AGAIN();
Craig Tiller55624a32017-05-26 08:14:44 -0700531 break;
532 case UNKICKED:
533 SET_KICK_STATE(worker, KICKED);
534 if (worker->initialized_cv) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800535 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV();
Craig Tiller55624a32017-05-26 08:14:44 -0700536 gpr_cv_signal(&worker->cv);
537 }
538 break;
539 case DESIGNATED_POLLER:
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800540 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD();
Craig Tiller55624a32017-05-26 08:14:44 -0700541 SET_KICK_STATE(worker, KICKED);
542 append_error(&error, grpc_wakeup_fd_wakeup(&global_wakeup_fd),
Sree Kuchibhotla0d8431a2017-07-18 16:21:54 -0700543 "pollset_kick_all");
Craig Tiller55624a32017-05-26 08:14:44 -0700544 break;
Craig Tiller4509c472017-04-27 19:05:13 +0000545 }
546
Craig Tiller32f90ee2017-04-28 12:46:41 -0700547 worker = worker->next;
Craig Tiller4509c472017-04-27 19:05:13 +0000548 } while (worker != pollset->root_worker);
549 }
Sree Kuchibhotla0d8431a2017-07-18 16:21:54 -0700550 // TODO: sreek. Check if we need to set 'kicked_without_poller' to true here
551 // in the else case
Craig Tiller4509c472017-04-27 19:05:13 +0000552 return error;
553}
554
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800555static void pollset_maybe_finish_shutdown(grpc_pollset* pollset) {
Craig Tiller4782d922017-11-10 09:53:21 -0800556 if (pollset->shutdown_closure != nullptr && pollset->root_worker == nullptr &&
Craig Tillerba550da2017-05-01 14:26:31 +0000557 pollset->begin_refs == 0) {
yang-gdf92a642017-08-21 22:38:45 -0700558 GPR_TIMER_MARK("pollset_finish_shutdown", 0);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800559 GRPC_CLOSURE_SCHED(pollset->shutdown_closure, GRPC_ERROR_NONE);
Craig Tiller4782d922017-11-10 09:53:21 -0800560 pollset->shutdown_closure = nullptr;
Craig Tiller4509c472017-04-27 19:05:13 +0000561 }
562}
563
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800564static void pollset_shutdown(grpc_pollset* pollset, grpc_closure* closure) {
yang-gce1cfea2018-01-31 15:59:50 -0800565 GPR_TIMER_SCOPE("pollset_shutdown", 0);
Craig Tiller4782d922017-11-10 09:53:21 -0800566 GPR_ASSERT(pollset->shutdown_closure == nullptr);
Craig Tillerc81512a2017-05-26 09:53:58 -0700567 GPR_ASSERT(!pollset->shutting_down);
Craig Tiller4509c472017-04-27 19:05:13 +0000568 pollset->shutdown_closure = closure;
Craig Tillerc81512a2017-05-26 09:53:58 -0700569 pollset->shutting_down = true;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800570 GRPC_LOG_IF_ERROR("pollset_shutdown", pollset_kick_all(pollset));
571 pollset_maybe_finish_shutdown(pollset);
Craig Tiller4509c472017-04-27 19:05:13 +0000572}
573
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800574static int poll_deadline_to_millis_timeout(grpc_millis millis) {
Craig Tiller20397792017-07-18 11:35:27 -0700575 if (millis == GRPC_MILLIS_INF_FUTURE) return -1;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800576 grpc_millis delta = millis - grpc_core::ExecCtx::Get()->Now();
Craig Tillerd9b82bd2017-08-29 12:16:56 -0700577 if (delta > INT_MAX) {
Craig Tiller20397792017-07-18 11:35:27 -0700578 return INT_MAX;
Craig Tillerd9b82bd2017-08-29 12:16:56 -0700579 } else if (delta < 0) {
Craig Tiller4509c472017-04-27 19:05:13 +0000580 return 0;
Craig Tillerd9b82bd2017-08-29 12:16:56 -0700581 } else {
Craig Tiller20397792017-07-18 11:35:27 -0700582 return (int)delta;
Craig Tiller4509c472017-04-27 19:05:13 +0000583 }
Craig Tiller4509c472017-04-27 19:05:13 +0000584}
585
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700586/* Process the epoll events found by do_epoll_wait() function.
587 - g_epoll_set.cursor points to the index of the first event to be processed
588 - This function then processes up-to MAX_EPOLL_EVENTS_PER_ITERATION and
589 updates the g_epoll_set.cursor
Craig Tiller4509c472017-04-27 19:05:13 +0000590
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700591 NOTE ON SYNCRHONIZATION: Similar to do_epoll_wait(), this function is only
592 called by g_active_poller thread. So there is no need for synchronization
593 when accessing fields in g_epoll_set */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800594static grpc_error* process_epoll_events(grpc_pollset* pollset) {
yang-gce1cfea2018-01-31 15:59:50 -0800595 GPR_TIMER_SCOPE("process_epoll_events", 0);
596
Craig Tillerbaa14a92017-11-03 09:09:36 -0700597 static const char* err_desc = "process_events";
598 grpc_error* error = GRPC_ERROR_NONE;
Sree Kuchibhotlaa92a9cc2017-08-27 14:02:15 -0700599 long num_events = gpr_atm_acq_load(&g_epoll_set.num_events);
600 long cursor = gpr_atm_acq_load(&g_epoll_set.cursor);
601 for (int idx = 0;
602 (idx < MAX_EPOLL_EVENTS_HANDLED_PER_ITERATION) && cursor != num_events;
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700603 idx++) {
Sree Kuchibhotlaa92a9cc2017-08-27 14:02:15 -0700604 long c = cursor++;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700605 struct epoll_event* ev = &g_epoll_set.events[c];
606 void* data_ptr = ev->data.ptr;
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700607
Craig Tiller4509c472017-04-27 19:05:13 +0000608 if (data_ptr == &global_wakeup_fd) {
Craig Tiller4509c472017-04-27 19:05:13 +0000609 append_error(&error, grpc_wakeup_fd_consume_wakeup(&global_wakeup_fd),
610 err_desc);
611 } else {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700612 grpc_fd* fd = (grpc_fd*)(data_ptr);
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700613 bool cancel = (ev->events & (EPOLLERR | EPOLLHUP)) != 0;
614 bool read_ev = (ev->events & (EPOLLIN | EPOLLPRI)) != 0;
615 bool write_ev = (ev->events & EPOLLOUT) != 0;
616
Craig Tiller4509c472017-04-27 19:05:13 +0000617 if (read_ev || cancel) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800618 fd_become_readable(fd, pollset);
Craig Tiller4509c472017-04-27 19:05:13 +0000619 }
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700620
Craig Tiller4509c472017-04-27 19:05:13 +0000621 if (write_ev || cancel) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800622 fd_become_writable(fd);
Craig Tiller4509c472017-04-27 19:05:13 +0000623 }
624 }
625 }
Sree Kuchibhotlaa92a9cc2017-08-27 14:02:15 -0700626 gpr_atm_rel_store(&g_epoll_set.cursor, cursor);
Craig Tiller4509c472017-04-27 19:05:13 +0000627 return error;
628}
629
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700630/* Do epoll_wait and store the events in g_epoll_set.events field. This does not
631 "process" any of the events yet; that is done in process_epoll_events().
632 *See process_epoll_events() function for more details.
633
634 NOTE ON SYNCHRONIZATION: At any point of time, only the g_active_poller
635 (i.e the designated poller thread) will be calling this function. So there is
636 no need for any synchronization when accesing fields in g_epoll_set */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800637static grpc_error* do_epoll_wait(grpc_pollset* ps, grpc_millis deadline) {
yang-gce1cfea2018-01-31 15:59:50 -0800638 GPR_TIMER_SCOPE("do_epoll_wait", 0);
Craig Tiller4509c472017-04-27 19:05:13 +0000639
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700640 int r;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800641 int timeout = poll_deadline_to_millis_timeout(deadline);
Craig Tiller4509c472017-04-27 19:05:13 +0000642 if (timeout != 0) {
643 GRPC_SCHEDULING_START_BLOCKING_REGION;
644 }
Craig Tiller4509c472017-04-27 19:05:13 +0000645 do {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800646 GRPC_STATS_INC_SYSCALL_POLL();
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700647 r = epoll_wait(g_epoll_set.epfd, g_epoll_set.events, MAX_EPOLL_EVENTS,
648 timeout);
Craig Tiller4509c472017-04-27 19:05:13 +0000649 } while (r < 0 && errno == EINTR);
650 if (timeout != 0) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800651 GRPC_SCHEDULING_END_BLOCKING_REGION;
Craig Tiller4509c472017-04-27 19:05:13 +0000652 }
653
654 if (r < 0) return GRPC_OS_ERROR(errno, "epoll_wait");
655
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800656 GRPC_STATS_INC_POLL_EVENTS_RETURNED(r);
Craig Tiller0ff222a2017-09-01 09:41:43 -0700657
ncteisen3cffe1f2017-11-10 13:56:23 -0800658 if (grpc_polling_trace.enabled()) {
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700659 gpr_log(GPR_DEBUG, "ps: %p poll got %d events", ps, r);
Craig Tiller4509c472017-04-27 19:05:13 +0000660 }
661
Sree Kuchibhotlaa92a9cc2017-08-27 14:02:15 -0700662 gpr_atm_rel_store(&g_epoll_set.num_events, r);
663 gpr_atm_rel_store(&g_epoll_set.cursor, 0);
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700664
665 return GRPC_ERROR_NONE;
Craig Tiller4509c472017-04-27 19:05:13 +0000666}
667
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800668static bool begin_worker(grpc_pollset* pollset, grpc_pollset_worker* worker,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700669 grpc_pollset_worker** worker_hdl,
Craig Tiller20397792017-07-18 11:35:27 -0700670 grpc_millis deadline) {
yang-gce1cfea2018-01-31 15:59:50 -0800671 GPR_TIMER_SCOPE("begin_worker", 0);
Craig Tiller4782d922017-11-10 09:53:21 -0800672 if (worker_hdl != nullptr) *worker_hdl = worker;
Craig Tiller4509c472017-04-27 19:05:13 +0000673 worker->initialized_cv = false;
Craig Tiller55624a32017-05-26 08:14:44 -0700674 SET_KICK_STATE(worker, UNKICKED);
Craig Tiller50da5ec2017-05-01 13:51:14 -0700675 worker->schedule_on_end_work = (grpc_closure_list)GRPC_CLOSURE_LIST_INIT;
Craig Tillerba550da2017-05-01 14:26:31 +0000676 pollset->begin_refs++;
Craig Tiller4509c472017-04-27 19:05:13 +0000677
ncteisen3cffe1f2017-11-10 13:56:23 -0800678 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -0800679 gpr_log(GPR_DEBUG, "PS:%p BEGIN_STARTS:%p", pollset, worker);
Craig Tiller830e82a2017-05-31 16:26:27 -0700680 }
681
Craig Tiller32f90ee2017-04-28 12:46:41 -0700682 if (pollset->seen_inactive) {
683 // pollset has been observed to be inactive, we need to move back to the
684 // active list
Craig Tillere00d7332017-05-01 15:43:51 +0000685 bool is_reassigning = false;
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700686 if (!pollset->reassigning_neighborhood) {
Craig Tillere00d7332017-05-01 15:43:51 +0000687 is_reassigning = true;
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700688 pollset->reassigning_neighborhood = true;
689 pollset->neighborhood = &g_neighborhoods[choose_neighborhood()];
Craig Tillere00d7332017-05-01 15:43:51 +0000690 }
Craig Tillerbaa14a92017-11-03 09:09:36 -0700691 pollset_neighborhood* neighborhood = pollset->neighborhood;
Craig Tiller32f90ee2017-04-28 12:46:41 -0700692 gpr_mu_unlock(&pollset->mu);
Craig Tillerba550da2017-05-01 14:26:31 +0000693 // pollset unlocked: state may change (even worker->kick_state)
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700694 retry_lock_neighborhood:
695 gpr_mu_lock(&neighborhood->mu);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700696 gpr_mu_lock(&pollset->mu);
ncteisen3cffe1f2017-11-10 13:56:23 -0800697 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -0800698 gpr_log(GPR_DEBUG, "PS:%p BEGIN_REORG:%p kick_state=%s is_reassigning=%d",
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700699 pollset, worker, kick_state_string(worker->state),
Craig Tiller830e82a2017-05-31 16:26:27 -0700700 is_reassigning);
701 }
Craig Tiller32f90ee2017-04-28 12:46:41 -0700702 if (pollset->seen_inactive) {
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700703 if (neighborhood != pollset->neighborhood) {
704 gpr_mu_unlock(&neighborhood->mu);
705 neighborhood = pollset->neighborhood;
Craig Tiller2acab6e2017-04-30 23:06:33 +0000706 gpr_mu_unlock(&pollset->mu);
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700707 goto retry_lock_neighborhood;
Craig Tiller2acab6e2017-04-30 23:06:33 +0000708 }
Sree Kuchibhotla0d8431a2017-07-18 16:21:54 -0700709
Sree Kuchibhotlafb349402017-09-06 10:58:06 -0700710 /* In the brief time we released the pollset locks above, the worker MAY
711 have been kicked. In this case, the worker should get out of this
712 pollset ASAP and hence this should neither add the pollset to
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700713 neighborhood nor mark the pollset as active.
Sree Kuchibhotlafb349402017-09-06 10:58:06 -0700714
715 On a side note, the only way a worker's kick state could have changed
716 at this point is if it were "kicked specifically". Since the worker has
717 not added itself to the pollset yet (by calling worker_insert()), it is
718 not visible in the "kick any" path yet */
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700719 if (worker->state == UNKICKED) {
Sree Kuchibhotlafb349402017-09-06 10:58:06 -0700720 pollset->seen_inactive = false;
Craig Tiller4782d922017-11-10 09:53:21 -0800721 if (neighborhood->active_root == nullptr) {
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700722 neighborhood->active_root = pollset->next = pollset->prev = pollset;
Sree Kuchibhotlafb349402017-09-06 10:58:06 -0700723 /* Make this the designated poller if there isn't one already */
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700724 if (worker->state == UNKICKED &&
Sree Kuchibhotlafb349402017-09-06 10:58:06 -0700725 gpr_atm_no_barrier_cas(&g_active_poller, 0, (gpr_atm)worker)) {
726 SET_KICK_STATE(worker, DESIGNATED_POLLER);
727 }
728 } else {
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700729 pollset->next = neighborhood->active_root;
Sree Kuchibhotlafb349402017-09-06 10:58:06 -0700730 pollset->prev = pollset->next->prev;
731 pollset->next->prev = pollset->prev->next = pollset;
Craig Tiller32f90ee2017-04-28 12:46:41 -0700732 }
Craig Tiller4509c472017-04-27 19:05:13 +0000733 }
734 }
Craig Tillere00d7332017-05-01 15:43:51 +0000735 if (is_reassigning) {
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700736 GPR_ASSERT(pollset->reassigning_neighborhood);
737 pollset->reassigning_neighborhood = false;
Craig Tillere00d7332017-05-01 15:43:51 +0000738 }
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700739 gpr_mu_unlock(&neighborhood->mu);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700740 }
Sree Kuchibhotlae6506bc2017-07-18 21:43:45 -0700741
Craig Tiller32f90ee2017-04-28 12:46:41 -0700742 worker_insert(pollset, worker);
Craig Tillerba550da2017-05-01 14:26:31 +0000743 pollset->begin_refs--;
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700744 if (worker->state == UNKICKED && !pollset->kicked_without_poller) {
Craig Tillera4b8eb02017-04-29 00:13:52 +0000745 GPR_ASSERT(gpr_atm_no_barrier_load(&g_active_poller) != (gpr_atm)worker);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700746 worker->initialized_cv = true;
747 gpr_cv_init(&worker->cv);
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700748 while (worker->state == UNKICKED && !pollset->shutting_down) {
ncteisen3cffe1f2017-11-10 13:56:23 -0800749 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -0800750 gpr_log(GPR_DEBUG, "PS:%p BEGIN_WAIT:%p kick_state=%s shutdown=%d",
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700751 pollset, worker, kick_state_string(worker->state),
Craig Tiller830e82a2017-05-31 16:26:27 -0700752 pollset->shutting_down);
753 }
Sree Kuchibhotla0d8431a2017-07-18 16:21:54 -0700754
Craig Tiller20397792017-07-18 11:35:27 -0700755 if (gpr_cv_wait(&worker->cv, &pollset->mu,
Sree Kuchibhotla54961bb2017-12-04 12:50:27 -0800756 grpc_millis_to_timespec(deadline, GPR_CLOCK_MONOTONIC)) &&
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700757 worker->state == UNKICKED) {
Sree Kuchibhotla0d8431a2017-07-18 16:21:54 -0700758 /* If gpr_cv_wait returns true (i.e a timeout), pretend that the worker
759 received a kick */
Craig Tiller55624a32017-05-26 08:14:44 -0700760 SET_KICK_STATE(worker, KICKED);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700761 }
Craig Tillerba550da2017-05-01 14:26:31 +0000762 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800763 grpc_core::ExecCtx::Get()->InvalidateNow();
Craig Tiller4509c472017-04-27 19:05:13 +0000764 }
765
ncteisen3cffe1f2017-11-10 13:56:23 -0800766 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -0800767 gpr_log(GPR_DEBUG,
Sree Kuchibhotla949d0752017-07-20 23:49:15 -0700768 "PS:%p BEGIN_DONE:%p kick_state=%s shutdown=%d "
769 "kicked_without_poller: %d",
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700770 pollset, worker, kick_state_string(worker->state),
Sree Kuchibhotla949d0752017-07-20 23:49:15 -0700771 pollset->shutting_down, pollset->kicked_without_poller);
Craig Tiller830e82a2017-05-31 16:26:27 -0700772 }
Craig Tiller4509c472017-04-27 19:05:13 +0000773
Sree Kuchibhotlae6506bc2017-07-18 21:43:45 -0700774 /* We release pollset lock in this function at a couple of places:
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700775 * 1. Briefly when assigning pollset to a neighborhood
Sree Kuchibhotlae6506bc2017-07-18 21:43:45 -0700776 * 2. When doing gpr_cv_wait()
777 * It is possible that 'kicked_without_poller' was set to true during (1) and
778 * 'shutting_down' is set to true during (1) or (2). If either of them is
779 * true, this worker cannot do polling */
Sree Kuchibhotlae6506bc2017-07-18 21:43:45 -0700780 /* TODO(sreek): Perhaps there is a better way to handle kicked_without_poller
781 * case; especially when the worker is the DESIGNATED_POLLER */
782
Sree Kuchibhotlaa0616ef2017-07-18 23:49:49 -0700783 if (pollset->kicked_without_poller) {
784 pollset->kicked_without_poller = false;
785 return false;
786 }
787
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700788 return worker->state == DESIGNATED_POLLER && !pollset->shutting_down;
Craig Tiller4509c472017-04-27 19:05:13 +0000789}
790
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700791static bool check_neighborhood_for_available_poller(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800792 pollset_neighborhood* neighborhood) {
yang-gce1cfea2018-01-31 15:59:50 -0800793 GPR_TIMER_SCOPE("check_neighborhood_for_available_poller", 0);
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700794 bool found_worker = false;
795 do {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700796 grpc_pollset* inspect = neighborhood->active_root;
Craig Tiller4782d922017-11-10 09:53:21 -0800797 if (inspect == nullptr) {
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700798 break;
799 }
800 gpr_mu_lock(&inspect->mu);
801 GPR_ASSERT(!inspect->seen_inactive);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700802 grpc_pollset_worker* inspect_worker = inspect->root_worker;
Craig Tiller4782d922017-11-10 09:53:21 -0800803 if (inspect_worker != nullptr) {
Craig Tillera4b8eb02017-04-29 00:13:52 +0000804 do {
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700805 switch (inspect_worker->state) {
Craig Tillerba550da2017-05-01 14:26:31 +0000806 case UNKICKED:
807 if (gpr_atm_no_barrier_cas(&g_active_poller, 0,
808 (gpr_atm)inspect_worker)) {
ncteisen3cffe1f2017-11-10 13:56:23 -0800809 if (grpc_polling_trace.enabled()) {
Craig Tiller830e82a2017-05-31 16:26:27 -0700810 gpr_log(GPR_DEBUG, " .. choose next poller to be %p",
811 inspect_worker);
812 }
Craig Tiller55624a32017-05-26 08:14:44 -0700813 SET_KICK_STATE(inspect_worker, DESIGNATED_POLLER);
Craig Tillerba550da2017-05-01 14:26:31 +0000814 if (inspect_worker->initialized_cv) {
yang-gdf92a642017-08-21 22:38:45 -0700815 GPR_TIMER_MARK("signal worker", 0);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800816 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV();
Craig Tillerba550da2017-05-01 14:26:31 +0000817 gpr_cv_signal(&inspect_worker->cv);
818 }
Craig Tiller830e82a2017-05-31 16:26:27 -0700819 } else {
ncteisen3cffe1f2017-11-10 13:56:23 -0800820 if (grpc_polling_trace.enabled()) {
Craig Tiller830e82a2017-05-31 16:26:27 -0700821 gpr_log(GPR_DEBUG, " .. beaten to choose next poller");
822 }
Craig Tillera4b8eb02017-04-29 00:13:52 +0000823 }
Craig Tillerba550da2017-05-01 14:26:31 +0000824 // even if we didn't win the cas, there's a worker, we can stop
825 found_worker = true;
826 break;
827 case KICKED:
828 break;
829 case DESIGNATED_POLLER:
830 found_worker = true; // ok, so someone else found the worker, but
831 // we'll accept that
832 break;
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700833 }
Craig Tillera4b8eb02017-04-29 00:13:52 +0000834 inspect_worker = inspect_worker->next;
Craig Tiller830e82a2017-05-31 16:26:27 -0700835 } while (!found_worker && inspect_worker != inspect->root_worker);
Craig Tillera4b8eb02017-04-29 00:13:52 +0000836 }
837 if (!found_worker) {
ncteisen3cffe1f2017-11-10 13:56:23 -0800838 if (grpc_polling_trace.enabled()) {
Craig Tiller830e82a2017-05-31 16:26:27 -0700839 gpr_log(GPR_DEBUG, " .. mark pollset %p inactive", inspect);
840 }
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700841 inspect->seen_inactive = true;
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700842 if (inspect == neighborhood->active_root) {
843 neighborhood->active_root =
Craig Tiller4782d922017-11-10 09:53:21 -0800844 inspect->next == inspect ? nullptr : inspect->next;
Craig Tiller2acab6e2017-04-30 23:06:33 +0000845 }
846 inspect->next->prev = inspect->prev;
847 inspect->prev->next = inspect->next;
Craig Tiller4782d922017-11-10 09:53:21 -0800848 inspect->next = inspect->prev = nullptr;
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700849 }
850 gpr_mu_unlock(&inspect->mu);
851 } while (!found_worker);
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700852 return found_worker;
853}
854
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800855static void end_worker(grpc_pollset* pollset, grpc_pollset_worker* worker,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700856 grpc_pollset_worker** worker_hdl) {
yang-gce1cfea2018-01-31 15:59:50 -0800857 GPR_TIMER_SCOPE("end_worker", 0);
ncteisen3cffe1f2017-11-10 13:56:23 -0800858 if (grpc_polling_trace.enabled()) {
Craig Tiller830e82a2017-05-31 16:26:27 -0700859 gpr_log(GPR_DEBUG, "PS:%p END_WORKER:%p", pollset, worker);
860 }
Craig Tiller4782d922017-11-10 09:53:21 -0800861 if (worker_hdl != nullptr) *worker_hdl = nullptr;
Craig Tiller830e82a2017-05-31 16:26:27 -0700862 /* Make sure we appear kicked */
Craig Tiller55624a32017-05-26 08:14:44 -0700863 SET_KICK_STATE(worker, KICKED);
Craig Tiller50da5ec2017-05-01 13:51:14 -0700864 grpc_closure_list_move(&worker->schedule_on_end_work,
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800865 grpc_core::ExecCtx::Get()->closure_list());
Craig Tiller8502ecb2017-04-28 14:22:01 -0700866 if (gpr_atm_no_barrier_load(&g_active_poller) == (gpr_atm)worker) {
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700867 if (worker->next != worker && worker->next->state == UNKICKED) {
ncteisen3cffe1f2017-11-10 13:56:23 -0800868 if (grpc_polling_trace.enabled()) {
Craig Tiller830e82a2017-05-31 16:26:27 -0700869 gpr_log(GPR_DEBUG, " .. choose next poller to be peer %p", worker);
870 }
Craig Tiller2acab6e2017-04-30 23:06:33 +0000871 GPR_ASSERT(worker->next->initialized_cv);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700872 gpr_atm_no_barrier_store(&g_active_poller, (gpr_atm)worker->next);
Craig Tiller55624a32017-05-26 08:14:44 -0700873 SET_KICK_STATE(worker->next, DESIGNATED_POLLER);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800874 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV();
Craig Tiller32f90ee2017-04-28 12:46:41 -0700875 gpr_cv_signal(&worker->next->cv);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800876 if (grpc_core::ExecCtx::Get()->HasWork()) {
Craig Tiller8502ecb2017-04-28 14:22:01 -0700877 gpr_mu_unlock(&pollset->mu);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800878 grpc_core::ExecCtx::Get()->Flush();
Craig Tiller8502ecb2017-04-28 14:22:01 -0700879 gpr_mu_lock(&pollset->mu);
880 }
Craig Tiller32f90ee2017-04-28 12:46:41 -0700881 } else {
882 gpr_atm_no_barrier_store(&g_active_poller, 0);
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700883 size_t poller_neighborhood_idx =
884 (size_t)(pollset->neighborhood - g_neighborhoods);
Craig Tillerbb742672017-05-17 22:19:05 +0000885 gpr_mu_unlock(&pollset->mu);
Craig Tiller32f90ee2017-04-28 12:46:41 -0700886 bool found_worker = false;
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700887 bool scan_state[MAX_NEIGHBORHOODS];
888 for (size_t i = 0; !found_worker && i < g_num_neighborhoods; i++) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700889 pollset_neighborhood* neighborhood =
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700890 &g_neighborhoods[(poller_neighborhood_idx + i) %
891 g_num_neighborhoods];
892 if (gpr_mu_trylock(&neighborhood->mu)) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800893 found_worker = check_neighborhood_for_available_poller(neighborhood);
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700894 gpr_mu_unlock(&neighborhood->mu);
Craig Tillerba550da2017-05-01 14:26:31 +0000895 scan_state[i] = true;
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700896 } else {
Craig Tillerba550da2017-05-01 14:26:31 +0000897 scan_state[i] = false;
Craig Tiller32f90ee2017-04-28 12:46:41 -0700898 }
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700899 }
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700900 for (size_t i = 0; !found_worker && i < g_num_neighborhoods; i++) {
Craig Tillerba550da2017-05-01 14:26:31 +0000901 if (scan_state[i]) continue;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700902 pollset_neighborhood* neighborhood =
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700903 &g_neighborhoods[(poller_neighborhood_idx + i) %
904 g_num_neighborhoods];
905 gpr_mu_lock(&neighborhood->mu);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800906 found_worker = check_neighborhood_for_available_poller(neighborhood);
Vijay Pai4b7ef4d2017-09-11 23:09:22 -0700907 gpr_mu_unlock(&neighborhood->mu);
Craig Tillerbbf4c7a2017-04-28 15:12:10 -0700908 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800909 grpc_core::ExecCtx::Get()->Flush();
Craig Tiller32f90ee2017-04-28 12:46:41 -0700910 gpr_mu_lock(&pollset->mu);
911 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800912 } else if (grpc_core::ExecCtx::Get()->HasWork()) {
Craig Tiller50da5ec2017-05-01 13:51:14 -0700913 gpr_mu_unlock(&pollset->mu);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800914 grpc_core::ExecCtx::Get()->Flush();
Craig Tiller50da5ec2017-05-01 13:51:14 -0700915 gpr_mu_lock(&pollset->mu);
Craig Tiller4509c472017-04-27 19:05:13 +0000916 }
917 if (worker->initialized_cv) {
918 gpr_cv_destroy(&worker->cv);
919 }
ncteisen3cffe1f2017-11-10 13:56:23 -0800920 if (grpc_polling_trace.enabled()) {
Craig Tiller830e82a2017-05-31 16:26:27 -0700921 gpr_log(GPR_DEBUG, " .. remove worker");
922 }
Craig Tiller32f90ee2017-04-28 12:46:41 -0700923 if (EMPTIED == worker_remove(pollset, worker)) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800924 pollset_maybe_finish_shutdown(pollset);
Craig Tiller4509c472017-04-27 19:05:13 +0000925 }
Craig Tillera4b8eb02017-04-29 00:13:52 +0000926 GPR_ASSERT(gpr_atm_no_barrier_load(&g_active_poller) != (gpr_atm)worker);
Craig Tiller4509c472017-04-27 19:05:13 +0000927}
928
929/* pollset->po.mu lock must be held by the caller before calling this.
930 The function pollset_work() may temporarily release the lock (pollset->po.mu)
931 during the course of its execution but it will always re-acquire the lock and
932 ensure that it is held by the time the function returns */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800933static grpc_error* pollset_work(grpc_pollset* ps,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700934 grpc_pollset_worker** worker_hdl,
Craig Tiller20397792017-07-18 11:35:27 -0700935 grpc_millis deadline) {
yang-gce1cfea2018-01-31 15:59:50 -0800936 GPR_TIMER_SCOPE("pollset_work", 0);
Craig Tiller4509c472017-04-27 19:05:13 +0000937 grpc_pollset_worker worker;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700938 grpc_error* error = GRPC_ERROR_NONE;
939 static const char* err_desc = "pollset_work";
Sree Kuchibhotlab154cd12017-08-25 10:33:41 -0700940 if (ps->kicked_without_poller) {
941 ps->kicked_without_poller = false;
Craig Tiller4509c472017-04-27 19:05:13 +0000942 return GRPC_ERROR_NONE;
943 }
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700944
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800945 if (begin_worker(ps, &worker, worker_hdl, deadline)) {
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700946 gpr_tls_set(&g_current_thread_pollset, (intptr_t)ps);
Craig Tiller4509c472017-04-27 19:05:13 +0000947 gpr_tls_set(&g_current_thread_worker, (intptr_t)&worker);
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700948 GPR_ASSERT(!ps->shutting_down);
949 GPR_ASSERT(!ps->seen_inactive);
950
951 gpr_mu_unlock(&ps->mu); /* unlock */
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700952 /* This is the designated polling thread at this point and should ideally do
953 polling. However, if there are unprocessed events left from a previous
954 call to do_epoll_wait(), skip calling epoll_wait() in this iteration and
955 process the pending epoll events.
956
957 The reason for decoupling do_epoll_wait and process_epoll_events is to
958 better distrubute the work (i.e handling epoll events) across multiple
959 threads
960
961 process_epoll_events() returns very quickly: It just queues the work on
962 exec_ctx but does not execute it (the actual exectution or more
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800963 accurately grpc_core::ExecCtx::Get()->Flush() happens in end_worker()
964 AFTER selecting a designated poller). So we are not waiting long periods
965 without a designated poller */
Sree Kuchibhotlaa92a9cc2017-08-27 14:02:15 -0700966 if (gpr_atm_acq_load(&g_epoll_set.cursor) ==
967 gpr_atm_acq_load(&g_epoll_set.num_events)) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800968 append_error(&error, do_epoll_wait(ps, deadline), err_desc);
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700969 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800970 append_error(&error, process_epoll_events(ps), err_desc);
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700971
972 gpr_mu_lock(&ps->mu); /* lock */
973
Craig Tiller4509c472017-04-27 19:05:13 +0000974 gpr_tls_set(&g_current_thread_worker, 0);
Craig Tiller830e82a2017-05-31 16:26:27 -0700975 } else {
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700976 gpr_tls_set(&g_current_thread_pollset, (intptr_t)ps);
Craig Tiller4509c472017-04-27 19:05:13 +0000977 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800978 end_worker(ps, &worker, worker_hdl);
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -0700979
Craig Tiller8502ecb2017-04-28 14:22:01 -0700980 gpr_tls_set(&g_current_thread_pollset, 0);
Craig Tiller4509c472017-04-27 19:05:13 +0000981 return error;
982}
983
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800984static grpc_error* pollset_kick(grpc_pollset* pollset,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700985 grpc_pollset_worker* specific_worker) {
yang-gce1cfea2018-01-31 15:59:50 -0800986 GPR_TIMER_SCOPE("pollset_kick", 0);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800987 GRPC_STATS_INC_POLLSET_KICK();
Craig Tillerbaa14a92017-11-03 09:09:36 -0700988 grpc_error* ret_err = GRPC_ERROR_NONE;
ncteisen3cffe1f2017-11-10 13:56:23 -0800989 if (grpc_polling_trace.enabled()) {
Craig Tillerb89bac02017-05-26 15:20:32 +0000990 gpr_strvec log;
991 gpr_strvec_init(&log);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700992 char* tmp;
993 gpr_asprintf(&tmp, "PS:%p KICK:%p curps=%p curworker=%p root=%p", pollset,
994 specific_worker, (void*)gpr_tls_get(&g_current_thread_pollset),
995 (void*)gpr_tls_get(&g_current_thread_worker),
996 pollset->root_worker);
Craig Tillerb89bac02017-05-26 15:20:32 +0000997 gpr_strvec_add(&log, tmp);
Craig Tiller4782d922017-11-10 09:53:21 -0800998 if (pollset->root_worker != nullptr) {
Craig Tiller830e82a2017-05-31 16:26:27 -0700999 gpr_asprintf(&tmp, " {kick_state=%s next=%p {kick_state=%s}}",
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001000 kick_state_string(pollset->root_worker->state),
Craig Tiller830e82a2017-05-31 16:26:27 -07001001 pollset->root_worker->next,
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001002 kick_state_string(pollset->root_worker->next->state));
Craig Tillerb89bac02017-05-26 15:20:32 +00001003 gpr_strvec_add(&log, tmp);
1004 }
Craig Tiller4782d922017-11-10 09:53:21 -08001005 if (specific_worker != nullptr) {
Craig Tiller830e82a2017-05-31 16:26:27 -07001006 gpr_asprintf(&tmp, " worker_kick_state=%s",
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001007 kick_state_string(specific_worker->state));
Craig Tillerb89bac02017-05-26 15:20:32 +00001008 gpr_strvec_add(&log, tmp);
1009 }
Craig Tiller4782d922017-11-10 09:53:21 -08001010 tmp = gpr_strvec_flatten(&log, nullptr);
Craig Tillerb89bac02017-05-26 15:20:32 +00001011 gpr_strvec_destroy(&log);
yang-g69b4e4c2018-01-24 14:36:20 -08001012 gpr_log(GPR_DEBUG, "%s", tmp);
Craig Tillerb89bac02017-05-26 15:20:32 +00001013 gpr_free(tmp);
1014 }
Sree Kuchibhotlafb349402017-09-06 10:58:06 -07001015
Craig Tiller4782d922017-11-10 09:53:21 -08001016 if (specific_worker == nullptr) {
Craig Tiller4509c472017-04-27 19:05:13 +00001017 if (gpr_tls_get(&g_current_thread_pollset) != (intptr_t)pollset) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001018 grpc_pollset_worker* root_worker = pollset->root_worker;
Craig Tiller4782d922017-11-10 09:53:21 -08001019 if (root_worker == nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001020 GRPC_STATS_INC_POLLSET_KICKED_WITHOUT_POLLER();
Craig Tiller4509c472017-04-27 19:05:13 +00001021 pollset->kicked_without_poller = true;
ncteisen3cffe1f2017-11-10 13:56:23 -08001022 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001023 gpr_log(GPR_DEBUG, " .. kicked_without_poller");
Craig Tiller75aef7f2017-05-26 08:26:08 -07001024 }
yang-gdf92a642017-08-21 22:38:45 -07001025 goto done;
Craig Tiller375eb252017-04-27 23:29:12 +00001026 }
Craig Tillerbaa14a92017-11-03 09:09:36 -07001027 grpc_pollset_worker* next_worker = root_worker->next;
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001028 if (root_worker->state == KICKED) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001029 GRPC_STATS_INC_POLLSET_KICKED_AGAIN();
ncteisen3cffe1f2017-11-10 13:56:23 -08001030 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001031 gpr_log(GPR_DEBUG, " .. already kicked %p", root_worker);
Craig Tiller830e82a2017-05-31 16:26:27 -07001032 }
1033 SET_KICK_STATE(root_worker, KICKED);
yang-gdf92a642017-08-21 22:38:45 -07001034 goto done;
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001035 } else if (next_worker->state == KICKED) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001036 GRPC_STATS_INC_POLLSET_KICKED_AGAIN();
ncteisen3cffe1f2017-11-10 13:56:23 -08001037 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001038 gpr_log(GPR_DEBUG, " .. already kicked %p", next_worker);
Craig Tiller830e82a2017-05-31 16:26:27 -07001039 }
1040 SET_KICK_STATE(next_worker, KICKED);
yang-gdf92a642017-08-21 22:38:45 -07001041 goto done;
Craig Tiller830e82a2017-05-31 16:26:27 -07001042 } else if (root_worker ==
1043 next_worker && // only try and wake up a poller if
1044 // there is no next worker
Craig Tillerbaa14a92017-11-03 09:09:36 -07001045 root_worker == (grpc_pollset_worker*)gpr_atm_no_barrier_load(
Craig Tiller830e82a2017-05-31 16:26:27 -07001046 &g_active_poller)) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001047 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD();
ncteisen3cffe1f2017-11-10 13:56:23 -08001048 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001049 gpr_log(GPR_DEBUG, " .. kicked %p", root_worker);
Craig Tiller75aef7f2017-05-26 08:26:08 -07001050 }
Craig Tiller55624a32017-05-26 08:14:44 -07001051 SET_KICK_STATE(root_worker, KICKED);
yang-gdf92a642017-08-21 22:38:45 -07001052 ret_err = grpc_wakeup_fd_wakeup(&global_wakeup_fd);
1053 goto done;
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001054 } else if (next_worker->state == UNKICKED) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001055 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV();
ncteisen3cffe1f2017-11-10 13:56:23 -08001056 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001057 gpr_log(GPR_DEBUG, " .. kicked %p", next_worker);
Craig Tiller75aef7f2017-05-26 08:26:08 -07001058 }
Craig Tiller8502ecb2017-04-28 14:22:01 -07001059 GPR_ASSERT(next_worker->initialized_cv);
Craig Tiller55624a32017-05-26 08:14:44 -07001060 SET_KICK_STATE(next_worker, KICKED);
Craig Tiller375eb252017-04-27 23:29:12 +00001061 gpr_cv_signal(&next_worker->cv);
yang-gdf92a642017-08-21 22:38:45 -07001062 goto done;
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001063 } else if (next_worker->state == DESIGNATED_POLLER) {
1064 if (root_worker->state != DESIGNATED_POLLER) {
ncteisen3cffe1f2017-11-10 13:56:23 -08001065 if (grpc_polling_trace.enabled()) {
Craig Tiller830e82a2017-05-31 16:26:27 -07001066 gpr_log(
yang-g69b4e4c2018-01-24 14:36:20 -08001067 GPR_DEBUG,
Craig Tiller830e82a2017-05-31 16:26:27 -07001068 " .. kicked root non-poller %p (initialized_cv=%d) (poller=%p)",
1069 root_worker, root_worker->initialized_cv, next_worker);
Craig Tiller75aef7f2017-05-26 08:26:08 -07001070 }
Craig Tiller55624a32017-05-26 08:14:44 -07001071 SET_KICK_STATE(root_worker, KICKED);
1072 if (root_worker->initialized_cv) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001073 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV();
Craig Tiller55624a32017-05-26 08:14:44 -07001074 gpr_cv_signal(&root_worker->cv);
1075 }
yang-gdf92a642017-08-21 22:38:45 -07001076 goto done;
Craig Tiller55624a32017-05-26 08:14:44 -07001077 } else {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001078 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD();
ncteisen3cffe1f2017-11-10 13:56:23 -08001079 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001080 gpr_log(GPR_DEBUG, " .. non-root poller %p (root=%p)", next_worker,
Craig Tiller75aef7f2017-05-26 08:26:08 -07001081 root_worker);
1082 }
Craig Tiller55624a32017-05-26 08:14:44 -07001083 SET_KICK_STATE(next_worker, KICKED);
yang-gdf92a642017-08-21 22:38:45 -07001084 ret_err = grpc_wakeup_fd_wakeup(&global_wakeup_fd);
1085 goto done;
Craig Tiller55624a32017-05-26 08:14:44 -07001086 }
Craig Tiller8502ecb2017-04-28 14:22:01 -07001087 } else {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001088 GRPC_STATS_INC_POLLSET_KICKED_AGAIN();
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001089 GPR_ASSERT(next_worker->state == KICKED);
Craig Tiller55624a32017-05-26 08:14:44 -07001090 SET_KICK_STATE(next_worker, KICKED);
yang-gdf92a642017-08-21 22:38:45 -07001091 goto done;
Craig Tiller4509c472017-04-27 19:05:13 +00001092 }
1093 } else {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001094 GRPC_STATS_INC_POLLSET_KICK_OWN_THREAD();
ncteisen3cffe1f2017-11-10 13:56:23 -08001095 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001096 gpr_log(GPR_DEBUG, " .. kicked while waking up");
Craig Tiller830e82a2017-05-31 16:26:27 -07001097 }
yang-gdf92a642017-08-21 22:38:45 -07001098 goto done;
Craig Tiller4509c472017-04-27 19:05:13 +00001099 }
Sree Kuchibhotlafb349402017-09-06 10:58:06 -07001100
1101 GPR_UNREACHABLE_CODE(goto done);
1102 }
1103
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -07001104 if (specific_worker->state == KICKED) {
ncteisen3cffe1f2017-11-10 13:56:23 -08001105 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001106 gpr_log(GPR_DEBUG, " .. specific worker already kicked");
Craig Tiller75aef7f2017-05-26 08:26:08 -07001107 }
yang-gdf92a642017-08-21 22:38:45 -07001108 goto done;
Craig Tiller4509c472017-04-27 19:05:13 +00001109 } else if (gpr_tls_get(&g_current_thread_worker) ==
1110 (intptr_t)specific_worker) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001111 GRPC_STATS_INC_POLLSET_KICK_OWN_THREAD();
ncteisen3cffe1f2017-11-10 13:56:23 -08001112 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001113 gpr_log(GPR_DEBUG, " .. mark %p kicked", specific_worker);
Craig Tiller75aef7f2017-05-26 08:26:08 -07001114 }
Craig Tiller55624a32017-05-26 08:14:44 -07001115 SET_KICK_STATE(specific_worker, KICKED);
yang-gdf92a642017-08-21 22:38:45 -07001116 goto done;
Craig Tiller32f90ee2017-04-28 12:46:41 -07001117 } else if (specific_worker ==
Craig Tillerbaa14a92017-11-03 09:09:36 -07001118 (grpc_pollset_worker*)gpr_atm_no_barrier_load(&g_active_poller)) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001119 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD();
ncteisen3cffe1f2017-11-10 13:56:23 -08001120 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001121 gpr_log(GPR_DEBUG, " .. kick active poller");
Craig Tiller75aef7f2017-05-26 08:26:08 -07001122 }
Craig Tiller55624a32017-05-26 08:14:44 -07001123 SET_KICK_STATE(specific_worker, KICKED);
yang-gdf92a642017-08-21 22:38:45 -07001124 ret_err = grpc_wakeup_fd_wakeup(&global_wakeup_fd);
1125 goto done;
Craig Tiller8502ecb2017-04-28 14:22:01 -07001126 } else if (specific_worker->initialized_cv) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001127 GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV();
ncteisen3cffe1f2017-11-10 13:56:23 -08001128 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001129 gpr_log(GPR_DEBUG, " .. kick waiting worker");
Craig Tiller75aef7f2017-05-26 08:26:08 -07001130 }
Craig Tiller55624a32017-05-26 08:14:44 -07001131 SET_KICK_STATE(specific_worker, KICKED);
Craig Tiller4509c472017-04-27 19:05:13 +00001132 gpr_cv_signal(&specific_worker->cv);
yang-gdf92a642017-08-21 22:38:45 -07001133 goto done;
Craig Tiller8502ecb2017-04-28 14:22:01 -07001134 } else {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001135 GRPC_STATS_INC_POLLSET_KICKED_AGAIN();
ncteisen3cffe1f2017-11-10 13:56:23 -08001136 if (grpc_polling_trace.enabled()) {
yang-g69b4e4c2018-01-24 14:36:20 -08001137 gpr_log(GPR_DEBUG, " .. kick non-waiting worker");
Craig Tiller75aef7f2017-05-26 08:26:08 -07001138 }
Craig Tiller55624a32017-05-26 08:14:44 -07001139 SET_KICK_STATE(specific_worker, KICKED);
yang-gdf92a642017-08-21 22:38:45 -07001140 goto done;
Craig Tiller4509c472017-04-27 19:05:13 +00001141 }
yang-gdf92a642017-08-21 22:38:45 -07001142done:
yang-gdf92a642017-08-21 22:38:45 -07001143 return ret_err;
Craig Tiller4509c472017-04-27 19:05:13 +00001144}
1145
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001146static void pollset_add_fd(grpc_pollset* pollset, grpc_fd* fd) {}
Craig Tiller4509c472017-04-27 19:05:13 +00001147
Craig Tiller4509c472017-04-27 19:05:13 +00001148/*******************************************************************************
Craig Tillerc67cc992017-04-27 10:15:51 -07001149 * Pollset-set Definitions
1150 */
1151
Craig Tillerbaa14a92017-11-03 09:09:36 -07001152static grpc_pollset_set* pollset_set_create(void) {
1153 return (grpc_pollset_set*)((intptr_t)0xdeafbeef);
Craig Tillerc67cc992017-04-27 10:15:51 -07001154}
1155
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001156static void pollset_set_destroy(grpc_pollset_set* pss) {}
Craig Tillerc67cc992017-04-27 10:15:51 -07001157
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001158static void pollset_set_add_fd(grpc_pollset_set* pss, grpc_fd* fd) {}
Craig Tillerc67cc992017-04-27 10:15:51 -07001159
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001160static void pollset_set_del_fd(grpc_pollset_set* pss, grpc_fd* fd) {}
Craig Tillerc67cc992017-04-27 10:15:51 -07001161
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001162static void pollset_set_add_pollset(grpc_pollset_set* pss, grpc_pollset* ps) {}
Craig Tillerc67cc992017-04-27 10:15:51 -07001163
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001164static void pollset_set_del_pollset(grpc_pollset_set* pss, grpc_pollset* ps) {}
Craig Tillerc67cc992017-04-27 10:15:51 -07001165
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001166static void pollset_set_add_pollset_set(grpc_pollset_set* bag,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001167 grpc_pollset_set* item) {}
Craig Tillerc67cc992017-04-27 10:15:51 -07001168
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001169static void pollset_set_del_pollset_set(grpc_pollset_set* bag,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001170 grpc_pollset_set* item) {}
Craig Tillerc67cc992017-04-27 10:15:51 -07001171
1172/*******************************************************************************
1173 * Event engine binding
1174 */
1175
1176static void shutdown_engine(void) {
1177 fd_global_shutdown();
1178 pollset_global_shutdown();
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -07001179 epoll_set_shutdown();
Craig Tillerc67cc992017-04-27 10:15:51 -07001180}
1181
1182static const grpc_event_engine_vtable vtable = {
Yash Tibrewal533d1182017-09-18 10:48:22 -07001183 sizeof(grpc_pollset),
Craig Tillerc67cc992017-04-27 10:15:51 -07001184
Yash Tibrewal533d1182017-09-18 10:48:22 -07001185 fd_create,
1186 fd_wrapped_fd,
1187 fd_orphan,
1188 fd_shutdown,
1189 fd_notify_on_read,
1190 fd_notify_on_write,
1191 fd_is_shutdown,
1192 fd_get_read_notifier_pollset,
Craig Tillerc67cc992017-04-27 10:15:51 -07001193
Yash Tibrewal533d1182017-09-18 10:48:22 -07001194 pollset_init,
1195 pollset_shutdown,
1196 pollset_destroy,
1197 pollset_work,
1198 pollset_kick,
1199 pollset_add_fd,
Craig Tillerc67cc992017-04-27 10:15:51 -07001200
Yash Tibrewal533d1182017-09-18 10:48:22 -07001201 pollset_set_create,
1202 pollset_set_destroy,
1203 pollset_set_add_pollset,
1204 pollset_set_del_pollset,
1205 pollset_set_add_pollset_set,
1206 pollset_set_del_pollset_set,
1207 pollset_set_add_fd,
1208 pollset_set_del_fd,
Craig Tillerc67cc992017-04-27 10:15:51 -07001209
Yash Tibrewal533d1182017-09-18 10:48:22 -07001210 shutdown_engine,
Craig Tillerc67cc992017-04-27 10:15:51 -07001211};
1212
1213/* It is possible that GLIBC has epoll but the underlying kernel doesn't.
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -07001214 * Create epoll_fd (epoll_set_init() takes care of that) to make sure epoll
1215 * support is available */
Craig Tillerbaa14a92017-11-03 09:09:36 -07001216const grpc_event_engine_vtable* grpc_init_epoll1_linux(bool explicit_request) {
Craig Tillerc67cc992017-04-27 10:15:51 -07001217 if (!grpc_has_wakeup_fd()) {
yang-g30101b02017-11-06 14:35:30 -08001218 gpr_log(GPR_ERROR, "Skipping epoll1 because of no wakeup fd.");
Craig Tiller4782d922017-11-10 09:53:21 -08001219 return nullptr;
Craig Tillerc67cc992017-04-27 10:15:51 -07001220 }
1221
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -07001222 if (!epoll_set_init()) {
Craig Tiller4782d922017-11-10 09:53:21 -08001223 return nullptr;
Craig Tillerc67cc992017-04-27 10:15:51 -07001224 }
1225
Craig Tillerc67cc992017-04-27 10:15:51 -07001226 fd_global_init();
1227
1228 if (!GRPC_LOG_IF_ERROR("pollset_global_init", pollset_global_init())) {
Craig Tiller4509c472017-04-27 19:05:13 +00001229 fd_global_shutdown();
Sree Kuchibhotla5efc9132017-08-17 14:10:38 -07001230 epoll_set_shutdown();
Craig Tiller4782d922017-11-10 09:53:21 -08001231 return nullptr;
Craig Tillerc67cc992017-04-27 10:15:51 -07001232 }
1233
1234 return &vtable;
1235}
1236
Mehrdad Afsharifb669002018-01-17 15:37:56 -08001237#else /* defined(GRPC_LINUX_EPOLL) */
Craig Tillerc67cc992017-04-27 10:15:51 -07001238#if defined(GRPC_POSIX_SOCKET)
Yash Tibrewal1cac2232017-09-26 11:31:11 -07001239#include "src/core/lib/iomgr/ev_epoll1_linux.h"
Craig Tillerc67cc992017-04-27 10:15:51 -07001240/* If GRPC_LINUX_EPOLL is not defined, it means epoll is not available. Return
1241 * NULL */
Craig Tillerbaa14a92017-11-03 09:09:36 -07001242const grpc_event_engine_vtable* grpc_init_epoll1_linux(bool explicit_request) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001243 return nullptr;
Craig Tiller9ddb3152017-04-27 21:32:56 +00001244}
Craig Tillerc67cc992017-04-27 10:15:51 -07001245#endif /* defined(GRPC_POSIX_SOCKET) */
Mehrdad Afsharifb669002018-01-17 15:37:56 -08001246#endif /* !defined(GRPC_LINUX_EPOLL) */