Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2017 gRPC authors. |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "src/core/lib/iomgr/port.h" |
| 20 | |
| 21 | /* This polling engine is only relevant on linux kernels supporting epoll() */ |
| 22 | #ifdef GRPC_LINUX_EPOLL |
| 23 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 24 | #include "src/core/lib/iomgr/ev_epoll1_linux.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 25 | |
| 26 | #include <assert.h> |
| 27 | #include <errno.h> |
| 28 | #include <poll.h> |
| 29 | #include <pthread.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/epoll.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | #include <grpc/support/alloc.h> |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 36 | #include <grpc/support/cpu.h> |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 37 | #include <grpc/support/log.h> |
| 38 | #include <grpc/support/string_util.h> |
| 39 | #include <grpc/support/tls.h> |
| 40 | #include <grpc/support/useful.h> |
| 41 | |
| 42 | #include "src/core/lib/iomgr/ev_posix.h" |
| 43 | #include "src/core/lib/iomgr/iomgr_internal.h" |
| 44 | #include "src/core/lib/iomgr/lockfree_event.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 45 | #include "src/core/lib/iomgr/wakeup_fd_posix.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 46 | #include "src/core/lib/profiling/timers.h" |
| 47 | #include "src/core/lib/support/block_annotate.h" |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 48 | #include "src/core/lib/support/string.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 49 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 50 | static grpc_wakeup_fd global_wakeup_fd; |
| 51 | static int g_epfd; |
| 52 | |
| 53 | /******************************************************************************* |
| 54 | * Fd Declarations |
| 55 | */ |
| 56 | |
| 57 | struct grpc_fd { |
| 58 | int fd; |
| 59 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 60 | gpr_atm read_closure; |
| 61 | gpr_atm write_closure; |
| 62 | |
| 63 | struct grpc_fd *freelist_next; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 64 | |
| 65 | /* The pollset that last noticed that the fd is readable. The actual type |
| 66 | * stored in this is (grpc_pollset *) */ |
| 67 | gpr_atm read_notifier_pollset; |
| 68 | |
| 69 | grpc_iomgr_object iomgr_object; |
| 70 | }; |
| 71 | |
| 72 | static void fd_global_init(void); |
| 73 | static void fd_global_shutdown(void); |
| 74 | |
| 75 | /******************************************************************************* |
| 76 | * Pollset Declarations |
| 77 | */ |
| 78 | |
Craig Tiller | 43bf259 | 2017-04-28 23:21:01 +0000 | [diff] [blame] | 79 | typedef enum { UNKICKED, KICKED, DESIGNATED_POLLER } kick_state; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 80 | |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 81 | static const char *kick_state_string(kick_state st) { |
| 82 | switch (st) { |
| 83 | case UNKICKED: |
| 84 | return "UNKICKED"; |
| 85 | case KICKED: |
| 86 | return "KICKED"; |
| 87 | case DESIGNATED_POLLER: |
| 88 | return "DESIGNATED_POLLER"; |
| 89 | } |
| 90 | GPR_UNREACHABLE_CODE(return "UNKNOWN"); |
| 91 | } |
| 92 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 93 | struct grpc_pollset_worker { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 94 | kick_state kick_state; |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 95 | int kick_state_mutator; // which line of code last changed kick state |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 96 | bool initialized_cv; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 97 | grpc_pollset_worker *next; |
| 98 | grpc_pollset_worker *prev; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 99 | gpr_cv cv; |
Craig Tiller | 50da5ec | 2017-05-01 13:51:14 -0700 | [diff] [blame] | 100 | grpc_closure_list schedule_on_end_work; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 103 | #define SET_KICK_STATE(worker, state) \ |
| 104 | do { \ |
| 105 | (worker)->kick_state = (state); \ |
| 106 | (worker)->kick_state_mutator = __LINE__; \ |
| 107 | } while (false) |
| 108 | |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 109 | #define MAX_NEIGHBOURHOODS 1024 |
| 110 | |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 111 | typedef struct pollset_neighbourhood { |
| 112 | gpr_mu mu; |
| 113 | grpc_pollset *active_root; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 114 | char pad[GPR_CACHELINE_SIZE]; |
| 115 | } pollset_neighbourhood; |
| 116 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 117 | struct grpc_pollset { |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 118 | gpr_mu mu; |
| 119 | pollset_neighbourhood *neighbourhood; |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 120 | bool reassigning_neighbourhood; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 121 | grpc_pollset_worker *root_worker; |
| 122 | bool kicked_without_poller; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 123 | bool seen_inactive; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 124 | bool shutting_down; /* Is the pollset shutting down ? */ |
| 125 | bool finish_shutdown_called; /* Is the 'finish_shutdown_locked()' called ? */ |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 126 | grpc_closure *shutdown_closure; /* Called after after shutdown is complete */ |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 127 | int begin_refs; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 128 | |
| 129 | grpc_pollset *next; |
| 130 | grpc_pollset *prev; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | /******************************************************************************* |
| 134 | * Pollset-set Declarations |
| 135 | */ |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 136 | |
Craig Tiller | 61f96c1 | 2017-05-12 13:36:39 -0700 | [diff] [blame] | 137 | struct grpc_pollset_set { |
| 138 | char unused; |
| 139 | }; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 140 | |
| 141 | /******************************************************************************* |
| 142 | * Common helpers |
| 143 | */ |
| 144 | |
| 145 | static bool append_error(grpc_error **composite, grpc_error *error, |
| 146 | const char *desc) { |
| 147 | if (error == GRPC_ERROR_NONE) return true; |
| 148 | if (*composite == GRPC_ERROR_NONE) { |
| 149 | *composite = GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc); |
| 150 | } |
| 151 | *composite = grpc_error_add_child(*composite, error); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | /******************************************************************************* |
| 156 | * Fd Definitions |
| 157 | */ |
| 158 | |
| 159 | /* We need to keep a freelist not because of any concerns of malloc performance |
| 160 | * but instead so that implementations with multiple threads in (for example) |
| 161 | * epoll_wait deal with the race between pollset removal and incoming poll |
| 162 | * notifications. |
| 163 | * |
| 164 | * The problem is that the poller ultimately holds a reference to this |
| 165 | * object, so it is very difficult to know when is safe to free it, at least |
| 166 | * without some expensive synchronization. |
| 167 | * |
| 168 | * If we keep the object freelisted, in the worst case losing this race just |
| 169 | * becomes a spurious read notification on a reused fd. |
| 170 | */ |
| 171 | |
| 172 | /* The alarm system needs to be able to wakeup 'some poller' sometimes |
| 173 | * (specifically when a new alarm needs to be triggered earlier than the next |
| 174 | * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a |
| 175 | * case occurs. */ |
| 176 | |
| 177 | static grpc_fd *fd_freelist = NULL; |
| 178 | static gpr_mu fd_freelist_mu; |
| 179 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 180 | static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); } |
| 181 | |
| 182 | static void fd_global_shutdown(void) { |
| 183 | gpr_mu_lock(&fd_freelist_mu); |
| 184 | gpr_mu_unlock(&fd_freelist_mu); |
| 185 | while (fd_freelist != NULL) { |
| 186 | grpc_fd *fd = fd_freelist; |
| 187 | fd_freelist = fd_freelist->freelist_next; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 188 | gpr_free(fd); |
| 189 | } |
| 190 | gpr_mu_destroy(&fd_freelist_mu); |
| 191 | } |
| 192 | |
| 193 | static grpc_fd *fd_create(int fd, const char *name) { |
| 194 | grpc_fd *new_fd = NULL; |
| 195 | |
| 196 | gpr_mu_lock(&fd_freelist_mu); |
| 197 | if (fd_freelist != NULL) { |
| 198 | new_fd = fd_freelist; |
| 199 | fd_freelist = fd_freelist->freelist_next; |
| 200 | } |
| 201 | gpr_mu_unlock(&fd_freelist_mu); |
| 202 | |
| 203 | if (new_fd == NULL) { |
| 204 | new_fd = gpr_malloc(sizeof(grpc_fd)); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 207 | new_fd->fd = fd; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 208 | grpc_lfev_init(&new_fd->read_closure); |
| 209 | grpc_lfev_init(&new_fd->write_closure); |
| 210 | gpr_atm_no_barrier_store(&new_fd->read_notifier_pollset, (gpr_atm)NULL); |
| 211 | |
| 212 | new_fd->freelist_next = NULL; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 213 | |
| 214 | char *fd_name; |
| 215 | gpr_asprintf(&fd_name, "%s fd=%d", name, fd); |
| 216 | grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name); |
Noah Eisen | 264879f | 2017-06-20 17:14:47 -0700 | [diff] [blame] | 217 | #ifndef NDEBUG |
| 218 | if (GRPC_TRACER_ON(grpc_trace_fd_refcount)) { |
| 219 | gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, new_fd, fd_name); |
| 220 | } |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 221 | #endif |
| 222 | gpr_free(fd_name); |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 223 | |
| 224 | struct epoll_event ev = {.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET), |
| 225 | .data.ptr = new_fd}; |
| 226 | if (epoll_ctl(g_epfd, EPOLL_CTL_ADD, fd, &ev) != 0) { |
| 227 | gpr_log(GPR_ERROR, "epoll_ctl failed: %s", strerror(errno)); |
| 228 | } |
| 229 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 230 | return new_fd; |
| 231 | } |
| 232 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 233 | static int fd_wrapped_fd(grpc_fd *fd) { return fd->fd; } |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 234 | |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 235 | /* Might be called multiple times */ |
| 236 | static void fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_error *why) { |
| 237 | if (grpc_lfev_set_shutdown(exec_ctx, &fd->read_closure, |
| 238 | GRPC_ERROR_REF(why))) { |
| 239 | shutdown(fd->fd, SHUT_RDWR); |
| 240 | grpc_lfev_set_shutdown(exec_ctx, &fd->write_closure, GRPC_ERROR_REF(why)); |
| 241 | } |
| 242 | GRPC_ERROR_UNREF(why); |
| 243 | } |
| 244 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 245 | static void fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 246 | grpc_closure *on_done, int *release_fd, |
| 247 | const char *reason) { |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 248 | grpc_error *error = GRPC_ERROR_NONE; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 249 | |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 250 | if (!grpc_lfev_is_shutdown(&fd->read_closure)) { |
| 251 | fd_shutdown(exec_ctx, fd, GRPC_ERROR_CREATE_FROM_COPIED_STRING(reason)); |
| 252 | } |
| 253 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 254 | /* If release_fd is not NULL, we should be relinquishing control of the file |
| 255 | descriptor fd->fd (but we still own the grpc_fd structure). */ |
| 256 | if (release_fd != NULL) { |
| 257 | *release_fd = fd->fd; |
| 258 | } else { |
| 259 | close(fd->fd); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 260 | } |
| 261 | |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 262 | GRPC_CLOSURE_SCHED(exec_ctx, on_done, GRPC_ERROR_REF(error)); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 263 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 264 | grpc_iomgr_unregister_object(&fd->iomgr_object); |
| 265 | grpc_lfev_destroy(&fd->read_closure); |
| 266 | grpc_lfev_destroy(&fd->write_closure); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 267 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 268 | gpr_mu_lock(&fd_freelist_mu); |
| 269 | fd->freelist_next = fd_freelist; |
| 270 | fd_freelist = fd; |
| 271 | gpr_mu_unlock(&fd_freelist_mu); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static grpc_pollset *fd_get_read_notifier_pollset(grpc_exec_ctx *exec_ctx, |
| 275 | grpc_fd *fd) { |
| 276 | gpr_atm notifier = gpr_atm_acq_load(&fd->read_notifier_pollset); |
| 277 | return (grpc_pollset *)notifier; |
| 278 | } |
| 279 | |
| 280 | static bool fd_is_shutdown(grpc_fd *fd) { |
| 281 | return grpc_lfev_is_shutdown(&fd->read_closure); |
| 282 | } |
| 283 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 284 | static void fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 285 | grpc_closure *closure) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 286 | grpc_lfev_notify_on(exec_ctx, &fd->read_closure, closure, "read"); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static void fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 290 | grpc_closure *closure) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 291 | grpc_lfev_notify_on(exec_ctx, &fd->write_closure, closure, "write"); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 294 | static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
| 295 | grpc_pollset *notifier) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 296 | grpc_lfev_set_ready(exec_ctx, &fd->read_closure, "read"); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 297 | |
| 298 | /* Note, it is possible that fd_become_readable might be called twice with |
| 299 | different 'notifier's when an fd becomes readable and it is in two epoll |
| 300 | sets (This can happen briefly during polling island merges). In such cases |
| 301 | it does not really matter which notifer is set as the read_notifier_pollset |
| 302 | (They would both point to the same polling island anyway) */ |
| 303 | /* Use release store to match with acquire load in fd_get_read_notifier */ |
| 304 | gpr_atm_rel_store(&fd->read_notifier_pollset, (gpr_atm)notifier); |
| 305 | } |
| 306 | |
| 307 | static void fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 308 | grpc_lfev_set_ready(exec_ctx, &fd->write_closure, "write"); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /******************************************************************************* |
| 312 | * Pollset Definitions |
| 313 | */ |
| 314 | |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 315 | GPR_TLS_DECL(g_current_thread_pollset); |
| 316 | GPR_TLS_DECL(g_current_thread_worker); |
| 317 | static gpr_atm g_active_poller; |
| 318 | static pollset_neighbourhood *g_neighbourhoods; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 319 | static size_t g_num_neighbourhoods; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 320 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 321 | /* Return true if first in list */ |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 322 | static bool worker_insert(grpc_pollset *pollset, grpc_pollset_worker *worker) { |
| 323 | if (pollset->root_worker == NULL) { |
| 324 | pollset->root_worker = worker; |
| 325 | worker->next = worker->prev = worker; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 326 | return true; |
| 327 | } else { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 328 | worker->next = pollset->root_worker; |
| 329 | worker->prev = worker->next->prev; |
| 330 | worker->next->prev = worker; |
| 331 | worker->prev->next = worker; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 332 | return false; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* Return true if last in list */ |
| 337 | typedef enum { EMPTIED, NEW_ROOT, REMOVED } worker_remove_result; |
| 338 | |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 339 | static worker_remove_result worker_remove(grpc_pollset *pollset, |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 340 | grpc_pollset_worker *worker) { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 341 | if (worker == pollset->root_worker) { |
| 342 | if (worker == worker->next) { |
| 343 | pollset->root_worker = NULL; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 344 | return EMPTIED; |
| 345 | } else { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 346 | pollset->root_worker = worker->next; |
| 347 | worker->prev->next = worker->next; |
| 348 | worker->next->prev = worker->prev; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 349 | return NEW_ROOT; |
| 350 | } |
| 351 | } else { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 352 | worker->prev->next = worker->next; |
| 353 | worker->next->prev = worker->prev; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 354 | return REMOVED; |
| 355 | } |
| 356 | } |
| 357 | |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 358 | static size_t choose_neighbourhood(void) { |
| 359 | return (size_t)gpr_cpu_current_cpu() % g_num_neighbourhoods; |
| 360 | } |
| 361 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 362 | static grpc_error *pollset_global_init(void) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 363 | gpr_tls_init(&g_current_thread_pollset); |
| 364 | gpr_tls_init(&g_current_thread_worker); |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 365 | gpr_atm_no_barrier_store(&g_active_poller, 0); |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 366 | global_wakeup_fd.read_fd = -1; |
| 367 | grpc_error *err = grpc_wakeup_fd_init(&global_wakeup_fd); |
| 368 | if (err != GRPC_ERROR_NONE) return err; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 369 | struct epoll_event ev = {.events = (uint32_t)(EPOLLIN | EPOLLET), |
| 370 | .data.ptr = &global_wakeup_fd}; |
| 371 | if (epoll_ctl(g_epfd, EPOLL_CTL_ADD, global_wakeup_fd.read_fd, &ev) != 0) { |
| 372 | return GRPC_OS_ERROR(errno, "epoll_ctl"); |
| 373 | } |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 374 | g_num_neighbourhoods = GPR_CLAMP(gpr_cpu_num_cores(), 1, MAX_NEIGHBOURHOODS); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 375 | g_neighbourhoods = |
| 376 | gpr_zalloc(sizeof(*g_neighbourhoods) * g_num_neighbourhoods); |
| 377 | for (size_t i = 0; i < g_num_neighbourhoods; i++) { |
| 378 | gpr_mu_init(&g_neighbourhoods[i].mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 379 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 380 | return GRPC_ERROR_NONE; |
| 381 | } |
| 382 | |
| 383 | static void pollset_global_shutdown(void) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 384 | gpr_tls_destroy(&g_current_thread_pollset); |
| 385 | gpr_tls_destroy(&g_current_thread_worker); |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 386 | if (global_wakeup_fd.read_fd != -1) grpc_wakeup_fd_destroy(&global_wakeup_fd); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 387 | for (size_t i = 0; i < g_num_neighbourhoods; i++) { |
| 388 | gpr_mu_destroy(&g_neighbourhoods[i].mu); |
| 389 | } |
| 390 | gpr_free(g_neighbourhoods); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static void pollset_init(grpc_pollset *pollset, gpr_mu **mu) { |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 394 | gpr_mu_init(&pollset->mu); |
| 395 | *mu = &pollset->mu; |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 396 | pollset->neighbourhood = &g_neighbourhoods[choose_neighbourhood()]; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 397 | pollset->seen_inactive = true; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Craig Tiller | c610985 | 2017-05-01 14:26:49 -0700 | [diff] [blame] | 400 | static void pollset_destroy(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) { |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 401 | gpr_mu_lock(&pollset->mu); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 402 | if (!pollset->seen_inactive) { |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 403 | pollset_neighbourhood *neighbourhood = pollset->neighbourhood; |
| 404 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | a95bacf | 2017-05-01 12:51:24 -0700 | [diff] [blame] | 405 | retry_lock_neighbourhood: |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 406 | gpr_mu_lock(&neighbourhood->mu); |
| 407 | gpr_mu_lock(&pollset->mu); |
| 408 | if (!pollset->seen_inactive) { |
| 409 | if (pollset->neighbourhood != neighbourhood) { |
| 410 | gpr_mu_unlock(&neighbourhood->mu); |
| 411 | neighbourhood = pollset->neighbourhood; |
| 412 | gpr_mu_unlock(&pollset->mu); |
| 413 | goto retry_lock_neighbourhood; |
| 414 | } |
| 415 | pollset->prev->next = pollset->next; |
| 416 | pollset->next->prev = pollset->prev; |
| 417 | if (pollset == pollset->neighbourhood->active_root) { |
| 418 | pollset->neighbourhood->active_root = |
| 419 | pollset->next == pollset ? NULL : pollset->next; |
| 420 | } |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 421 | } |
| 422 | gpr_mu_unlock(&pollset->neighbourhood->mu); |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 423 | } |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 424 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 425 | gpr_mu_destroy(&pollset->mu); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static grpc_error *pollset_kick_all(grpc_pollset *pollset) { |
| 429 | grpc_error *error = GRPC_ERROR_NONE; |
| 430 | if (pollset->root_worker != NULL) { |
| 431 | grpc_pollset_worker *worker = pollset->root_worker; |
| 432 | do { |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 433 | switch (worker->kick_state) { |
| 434 | case KICKED: |
| 435 | break; |
| 436 | case UNKICKED: |
| 437 | SET_KICK_STATE(worker, KICKED); |
| 438 | if (worker->initialized_cv) { |
| 439 | gpr_cv_signal(&worker->cv); |
| 440 | } |
| 441 | break; |
| 442 | case DESIGNATED_POLLER: |
| 443 | SET_KICK_STATE(worker, KICKED); |
| 444 | append_error(&error, grpc_wakeup_fd_wakeup(&global_wakeup_fd), |
| 445 | "pollset_shutdown"); |
| 446 | break; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 449 | worker = worker->next; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 450 | } while (worker != pollset->root_worker); |
| 451 | } |
| 452 | return error; |
| 453 | } |
| 454 | |
| 455 | static void pollset_maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, |
| 456 | grpc_pollset *pollset) { |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 457 | if (pollset->shutdown_closure != NULL && pollset->root_worker == NULL && |
| 458 | pollset->begin_refs == 0) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 459 | GRPC_CLOSURE_SCHED(exec_ctx, pollset->shutdown_closure, GRPC_ERROR_NONE); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 460 | pollset->shutdown_closure = NULL; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | static void pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 465 | grpc_closure *closure) { |
| 466 | GPR_ASSERT(pollset->shutdown_closure == NULL); |
Craig Tiller | c81512a | 2017-05-26 09:53:58 -0700 | [diff] [blame] | 467 | GPR_ASSERT(!pollset->shutting_down); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 468 | pollset->shutdown_closure = closure; |
Craig Tiller | c81512a | 2017-05-26 09:53:58 -0700 | [diff] [blame] | 469 | pollset->shutting_down = true; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 470 | GRPC_LOG_IF_ERROR("pollset_shutdown", pollset_kick_all(pollset)); |
| 471 | pollset_maybe_finish_shutdown(exec_ctx, pollset); |
| 472 | } |
| 473 | |
Craig Tiller | a95bacf | 2017-05-01 12:51:24 -0700 | [diff] [blame] | 474 | #define MAX_EPOLL_EVENTS 100 |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 475 | |
| 476 | static int poll_deadline_to_millis_timeout(gpr_timespec deadline, |
| 477 | gpr_timespec now) { |
| 478 | gpr_timespec timeout; |
| 479 | if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) { |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | if (gpr_time_cmp(deadline, now) <= 0) { |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static const gpr_timespec round_up = { |
| 488 | .clock_type = GPR_TIMESPAN, .tv_sec = 0, .tv_nsec = GPR_NS_PER_MS - 1}; |
| 489 | timeout = gpr_time_sub(deadline, now); |
| 490 | int millis = gpr_time_to_millis(gpr_time_add(timeout, round_up)); |
| 491 | return millis >= 1 ? millis : 1; |
| 492 | } |
| 493 | |
| 494 | static grpc_error *pollset_epoll(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 495 | gpr_timespec now, gpr_timespec deadline) { |
| 496 | struct epoll_event events[MAX_EPOLL_EVENTS]; |
| 497 | static const char *err_desc = "pollset_poll"; |
| 498 | |
| 499 | int timeout = poll_deadline_to_millis_timeout(deadline, now); |
| 500 | |
| 501 | if (timeout != 0) { |
| 502 | GRPC_SCHEDULING_START_BLOCKING_REGION; |
| 503 | } |
| 504 | int r; |
| 505 | do { |
| 506 | r = epoll_wait(g_epfd, events, MAX_EPOLL_EVENTS, timeout); |
| 507 | } while (r < 0 && errno == EINTR); |
| 508 | if (timeout != 0) { |
| 509 | GRPC_SCHEDULING_END_BLOCKING_REGION; |
| 510 | } |
| 511 | |
| 512 | if (r < 0) return GRPC_OS_ERROR(errno, "epoll_wait"); |
| 513 | |
| 514 | grpc_error *error = GRPC_ERROR_NONE; |
| 515 | for (int i = 0; i < r; i++) { |
| 516 | void *data_ptr = events[i].data.ptr; |
| 517 | if (data_ptr == &global_wakeup_fd) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 518 | append_error(&error, grpc_wakeup_fd_consume_wakeup(&global_wakeup_fd), |
| 519 | err_desc); |
| 520 | } else { |
| 521 | grpc_fd *fd = (grpc_fd *)(data_ptr); |
| 522 | bool cancel = (events[i].events & (EPOLLERR | EPOLLHUP)) != 0; |
| 523 | bool read_ev = (events[i].events & (EPOLLIN | EPOLLPRI)) != 0; |
| 524 | bool write_ev = (events[i].events & EPOLLOUT) != 0; |
| 525 | if (read_ev || cancel) { |
| 526 | fd_become_readable(exec_ctx, fd, pollset); |
| 527 | } |
| 528 | if (write_ev || cancel) { |
| 529 | fd_become_writable(exec_ctx, fd); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return error; |
| 535 | } |
| 536 | |
| 537 | static bool begin_worker(grpc_pollset *pollset, grpc_pollset_worker *worker, |
| 538 | grpc_pollset_worker **worker_hdl, gpr_timespec *now, |
| 539 | gpr_timespec deadline) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 540 | if (worker_hdl != NULL) *worker_hdl = worker; |
| 541 | worker->initialized_cv = false; |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 542 | SET_KICK_STATE(worker, UNKICKED); |
Craig Tiller | 50da5ec | 2017-05-01 13:51:14 -0700 | [diff] [blame] | 543 | worker->schedule_on_end_work = (grpc_closure_list)GRPC_CLOSURE_LIST_INIT; |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 544 | pollset->begin_refs++; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 545 | |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 546 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 547 | gpr_log(GPR_ERROR, "PS:%p BEGIN_STARTS:%p", pollset, worker); |
| 548 | } |
| 549 | |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 550 | if (pollset->seen_inactive) { |
| 551 | // pollset has been observed to be inactive, we need to move back to the |
| 552 | // active list |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 553 | bool is_reassigning = false; |
| 554 | if (!pollset->reassigning_neighbourhood) { |
| 555 | is_reassigning = true; |
| 556 | pollset->reassigning_neighbourhood = true; |
| 557 | pollset->neighbourhood = &g_neighbourhoods[choose_neighbourhood()]; |
| 558 | } |
| 559 | pollset_neighbourhood *neighbourhood = pollset->neighbourhood; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 560 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 561 | // pollset unlocked: state may change (even worker->kick_state) |
| 562 | retry_lock_neighbourhood: |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 563 | gpr_mu_lock(&neighbourhood->mu); |
| 564 | gpr_mu_lock(&pollset->mu); |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 565 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 566 | gpr_log(GPR_ERROR, "PS:%p BEGIN_REORG:%p kick_state=%s is_reassigning=%d", |
| 567 | pollset, worker, kick_state_string(worker->kick_state), |
| 568 | is_reassigning); |
| 569 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 570 | if (pollset->seen_inactive) { |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 571 | if (neighbourhood != pollset->neighbourhood) { |
| 572 | gpr_mu_unlock(&neighbourhood->mu); |
| 573 | neighbourhood = pollset->neighbourhood; |
| 574 | gpr_mu_unlock(&pollset->mu); |
| 575 | goto retry_lock_neighbourhood; |
| 576 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 577 | pollset->seen_inactive = false; |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 578 | if (neighbourhood->active_root == NULL) { |
| 579 | neighbourhood->active_root = pollset->next = pollset->prev = pollset; |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 580 | if (worker->kick_state == UNKICKED && |
| 581 | gpr_atm_no_barrier_cas(&g_active_poller, 0, (gpr_atm)worker)) { |
| 582 | SET_KICK_STATE(worker, DESIGNATED_POLLER); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 583 | } |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 584 | } else { |
| 585 | pollset->next = neighbourhood->active_root; |
| 586 | pollset->prev = pollset->next->prev; |
| 587 | pollset->next->prev = pollset->prev->next = pollset; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 590 | if (is_reassigning) { |
| 591 | GPR_ASSERT(pollset->reassigning_neighbourhood); |
| 592 | pollset->reassigning_neighbourhood = false; |
| 593 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 594 | gpr_mu_unlock(&neighbourhood->mu); |
| 595 | } |
| 596 | worker_insert(pollset, worker); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 597 | pollset->begin_refs--; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 598 | if (worker->kick_state == UNKICKED) { |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 599 | GPR_ASSERT(gpr_atm_no_barrier_load(&g_active_poller) != (gpr_atm)worker); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 600 | worker->initialized_cv = true; |
| 601 | gpr_cv_init(&worker->cv); |
Craig Tiller | c81512a | 2017-05-26 09:53:58 -0700 | [diff] [blame] | 602 | while (worker->kick_state == UNKICKED && !pollset->shutting_down) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 603 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 604 | gpr_log(GPR_ERROR, "PS:%p BEGIN_WAIT:%p kick_state=%s shutdown=%d", |
| 605 | pollset, worker, kick_state_string(worker->kick_state), |
| 606 | pollset->shutting_down); |
| 607 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 608 | if (gpr_cv_wait(&worker->cv, &pollset->mu, deadline) && |
| 609 | worker->kick_state == UNKICKED) { |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 610 | SET_KICK_STATE(worker, KICKED); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 611 | } |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 612 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 613 | *now = gpr_now(now->clock_type); |
| 614 | } |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 615 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 616 | gpr_log(GPR_ERROR, "PS:%p BEGIN_DONE:%p kick_state=%s shutdown=%d", pollset, |
| 617 | worker, kick_state_string(worker->kick_state), |
| 618 | pollset->shutting_down); |
| 619 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 620 | |
Craig Tiller | c81512a | 2017-05-26 09:53:58 -0700 | [diff] [blame] | 621 | return worker->kick_state == DESIGNATED_POLLER && !pollset->shutting_down; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 624 | static bool check_neighbourhood_for_available_poller( |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 625 | pollset_neighbourhood *neighbourhood) { |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 626 | bool found_worker = false; |
| 627 | do { |
| 628 | grpc_pollset *inspect = neighbourhood->active_root; |
| 629 | if (inspect == NULL) { |
| 630 | break; |
| 631 | } |
| 632 | gpr_mu_lock(&inspect->mu); |
| 633 | GPR_ASSERT(!inspect->seen_inactive); |
| 634 | grpc_pollset_worker *inspect_worker = inspect->root_worker; |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 635 | if (inspect_worker != NULL) { |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 636 | do { |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 637 | switch (inspect_worker->kick_state) { |
| 638 | case UNKICKED: |
| 639 | if (gpr_atm_no_barrier_cas(&g_active_poller, 0, |
| 640 | (gpr_atm)inspect_worker)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 641 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 642 | gpr_log(GPR_DEBUG, " .. choose next poller to be %p", |
| 643 | inspect_worker); |
| 644 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 645 | SET_KICK_STATE(inspect_worker, DESIGNATED_POLLER); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 646 | if (inspect_worker->initialized_cv) { |
| 647 | gpr_cv_signal(&inspect_worker->cv); |
| 648 | } |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 649 | } else { |
| 650 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 651 | gpr_log(GPR_DEBUG, " .. beaten to choose next poller"); |
| 652 | } |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 653 | } |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 654 | // even if we didn't win the cas, there's a worker, we can stop |
| 655 | found_worker = true; |
| 656 | break; |
| 657 | case KICKED: |
| 658 | break; |
| 659 | case DESIGNATED_POLLER: |
| 660 | found_worker = true; // ok, so someone else found the worker, but |
| 661 | // we'll accept that |
| 662 | break; |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 663 | } |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 664 | inspect_worker = inspect_worker->next; |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 665 | } while (!found_worker && inspect_worker != inspect->root_worker); |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 666 | } |
| 667 | if (!found_worker) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 668 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 669 | gpr_log(GPR_DEBUG, " .. mark pollset %p inactive", inspect); |
| 670 | } |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 671 | inspect->seen_inactive = true; |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 672 | if (inspect == neighbourhood->active_root) { |
Craig Tiller | a95bacf | 2017-05-01 12:51:24 -0700 | [diff] [blame] | 673 | neighbourhood->active_root = |
| 674 | inspect->next == inspect ? NULL : inspect->next; |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 675 | } |
| 676 | inspect->next->prev = inspect->prev; |
| 677 | inspect->prev->next = inspect->next; |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 678 | inspect->next = inspect->prev = NULL; |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 679 | } |
| 680 | gpr_mu_unlock(&inspect->mu); |
| 681 | } while (!found_worker); |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 682 | return found_worker; |
| 683 | } |
| 684 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 685 | static void end_worker(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 686 | grpc_pollset_worker *worker, |
| 687 | grpc_pollset_worker **worker_hdl) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 688 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 689 | gpr_log(GPR_DEBUG, "PS:%p END_WORKER:%p", pollset, worker); |
| 690 | } |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 691 | if (worker_hdl != NULL) *worker_hdl = NULL; |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 692 | /* Make sure we appear kicked */ |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 693 | SET_KICK_STATE(worker, KICKED); |
Craig Tiller | 50da5ec | 2017-05-01 13:51:14 -0700 | [diff] [blame] | 694 | grpc_closure_list_move(&worker->schedule_on_end_work, |
| 695 | &exec_ctx->closure_list); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 696 | if (gpr_atm_no_barrier_load(&g_active_poller) == (gpr_atm)worker) { |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 697 | if (worker->next != worker && worker->next->kick_state == UNKICKED) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 698 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 699 | gpr_log(GPR_DEBUG, " .. choose next poller to be peer %p", worker); |
| 700 | } |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 701 | GPR_ASSERT(worker->next->initialized_cv); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 702 | gpr_atm_no_barrier_store(&g_active_poller, (gpr_atm)worker->next); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 703 | SET_KICK_STATE(worker->next, DESIGNATED_POLLER); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 704 | gpr_cv_signal(&worker->next->cv); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 705 | if (grpc_exec_ctx_has_work(exec_ctx)) { |
| 706 | gpr_mu_unlock(&pollset->mu); |
| 707 | grpc_exec_ctx_flush(exec_ctx); |
| 708 | gpr_mu_lock(&pollset->mu); |
| 709 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 710 | } else { |
| 711 | gpr_atm_no_barrier_store(&g_active_poller, 0); |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 712 | size_t poller_neighbourhood_idx = |
| 713 | (size_t)(pollset->neighbourhood - g_neighbourhoods); |
Craig Tiller | bb74267 | 2017-05-17 22:19:05 +0000 | [diff] [blame] | 714 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 715 | bool found_worker = false; |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 716 | bool scan_state[MAX_NEIGHBOURHOODS]; |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 717 | for (size_t i = 0; !found_worker && i < g_num_neighbourhoods; i++) { |
| 718 | pollset_neighbourhood *neighbourhood = |
| 719 | &g_neighbourhoods[(poller_neighbourhood_idx + i) % |
| 720 | g_num_neighbourhoods]; |
| 721 | if (gpr_mu_trylock(&neighbourhood->mu)) { |
| 722 | found_worker = |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 723 | check_neighbourhood_for_available_poller(neighbourhood); |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 724 | gpr_mu_unlock(&neighbourhood->mu); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 725 | scan_state[i] = true; |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 726 | } else { |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 727 | scan_state[i] = false; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 728 | } |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 729 | } |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 730 | for (size_t i = 0; !found_worker && i < g_num_neighbourhoods; i++) { |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 731 | if (scan_state[i]) continue; |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 732 | pollset_neighbourhood *neighbourhood = |
| 733 | &g_neighbourhoods[(poller_neighbourhood_idx + i) % |
| 734 | g_num_neighbourhoods]; |
| 735 | gpr_mu_lock(&neighbourhood->mu); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 736 | found_worker = check_neighbourhood_for_available_poller(neighbourhood); |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 737 | gpr_mu_unlock(&neighbourhood->mu); |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 738 | } |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 739 | grpc_exec_ctx_flush(exec_ctx); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 740 | gpr_mu_lock(&pollset->mu); |
| 741 | } |
Craig Tiller | 50da5ec | 2017-05-01 13:51:14 -0700 | [diff] [blame] | 742 | } else if (grpc_exec_ctx_has_work(exec_ctx)) { |
| 743 | gpr_mu_unlock(&pollset->mu); |
| 744 | grpc_exec_ctx_flush(exec_ctx); |
| 745 | gpr_mu_lock(&pollset->mu); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 746 | } |
| 747 | if (worker->initialized_cv) { |
| 748 | gpr_cv_destroy(&worker->cv); |
| 749 | } |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 750 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 751 | gpr_log(GPR_DEBUG, " .. remove worker"); |
| 752 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 753 | if (EMPTIED == worker_remove(pollset, worker)) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 754 | pollset_maybe_finish_shutdown(exec_ctx, pollset); |
| 755 | } |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 756 | GPR_ASSERT(gpr_atm_no_barrier_load(&g_active_poller) != (gpr_atm)worker); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* pollset->po.mu lock must be held by the caller before calling this. |
| 760 | The function pollset_work() may temporarily release the lock (pollset->po.mu) |
| 761 | during the course of its execution but it will always re-acquire the lock and |
| 762 | ensure that it is held by the time the function returns */ |
| 763 | static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 764 | grpc_pollset_worker **worker_hdl, |
| 765 | gpr_timespec now, gpr_timespec deadline) { |
| 766 | grpc_pollset_worker worker; |
| 767 | grpc_error *error = GRPC_ERROR_NONE; |
| 768 | static const char *err_desc = "pollset_work"; |
| 769 | if (pollset->kicked_without_poller) { |
| 770 | pollset->kicked_without_poller = false; |
| 771 | return GRPC_ERROR_NONE; |
| 772 | } |
| 773 | if (begin_worker(pollset, &worker, worker_hdl, &now, deadline)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 774 | gpr_tls_set(&g_current_thread_pollset, (intptr_t)pollset); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 775 | gpr_tls_set(&g_current_thread_worker, (intptr_t)&worker); |
Craig Tiller | c81512a | 2017-05-26 09:53:58 -0700 | [diff] [blame] | 776 | GPR_ASSERT(!pollset->shutting_down); |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 777 | GPR_ASSERT(!pollset->seen_inactive); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 778 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 779 | append_error(&error, pollset_epoll(exec_ctx, pollset, now, deadline), |
| 780 | err_desc); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 781 | gpr_mu_lock(&pollset->mu); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 782 | gpr_tls_set(&g_current_thread_worker, 0); |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 783 | } else { |
| 784 | gpr_tls_set(&g_current_thread_pollset, (intptr_t)pollset); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 785 | } |
| 786 | end_worker(exec_ctx, pollset, &worker, worker_hdl); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 787 | gpr_tls_set(&g_current_thread_pollset, 0); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 788 | return error; |
| 789 | } |
| 790 | |
| 791 | static grpc_error *pollset_kick(grpc_pollset *pollset, |
| 792 | grpc_pollset_worker *specific_worker) { |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 793 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 794 | gpr_strvec log; |
| 795 | gpr_strvec_init(&log); |
| 796 | char *tmp; |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 797 | gpr_asprintf( |
| 798 | &tmp, "PS:%p KICK:%p curps=%p curworker=%p root=%p", pollset, |
| 799 | specific_worker, (void *)gpr_tls_get(&g_current_thread_pollset), |
| 800 | (void *)gpr_tls_get(&g_current_thread_worker), pollset->root_worker); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 801 | gpr_strvec_add(&log, tmp); |
| 802 | if (pollset->root_worker != NULL) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 803 | gpr_asprintf(&tmp, " {kick_state=%s next=%p {kick_state=%s}}", |
| 804 | kick_state_string(pollset->root_worker->kick_state), |
| 805 | pollset->root_worker->next, |
| 806 | kick_state_string(pollset->root_worker->next->kick_state)); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 807 | gpr_strvec_add(&log, tmp); |
| 808 | } |
| 809 | if (specific_worker != NULL) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 810 | gpr_asprintf(&tmp, " worker_kick_state=%s", |
| 811 | kick_state_string(specific_worker->kick_state)); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 812 | gpr_strvec_add(&log, tmp); |
| 813 | } |
| 814 | tmp = gpr_strvec_flatten(&log, NULL); |
| 815 | gpr_strvec_destroy(&log); |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 816 | gpr_log(GPR_ERROR, "%s", tmp); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 817 | gpr_free(tmp); |
| 818 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 819 | if (specific_worker == NULL) { |
| 820 | if (gpr_tls_get(&g_current_thread_pollset) != (intptr_t)pollset) { |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 821 | grpc_pollset_worker *root_worker = pollset->root_worker; |
| 822 | if (root_worker == NULL) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 823 | pollset->kicked_without_poller = true; |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 824 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 825 | gpr_log(GPR_ERROR, " .. kicked_without_poller"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 826 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 827 | return GRPC_ERROR_NONE; |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 828 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 829 | grpc_pollset_worker *next_worker = root_worker->next; |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 830 | if (root_worker->kick_state == KICKED) { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 831 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 832 | gpr_log(GPR_ERROR, " .. already kicked %p", root_worker); |
| 833 | } |
| 834 | SET_KICK_STATE(root_worker, KICKED); |
| 835 | return GRPC_ERROR_NONE; |
| 836 | } else if (next_worker->kick_state == KICKED) { |
| 837 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 838 | gpr_log(GPR_ERROR, " .. already kicked %p", next_worker); |
| 839 | } |
| 840 | SET_KICK_STATE(next_worker, KICKED); |
| 841 | return GRPC_ERROR_NONE; |
| 842 | } else if (root_worker == |
| 843 | next_worker && // only try and wake up a poller if |
| 844 | // there is no next worker |
| 845 | root_worker == (grpc_pollset_worker *)gpr_atm_no_barrier_load( |
| 846 | &g_active_poller)) { |
| 847 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 848 | gpr_log(GPR_ERROR, " .. kicked %p", root_worker); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 849 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 850 | SET_KICK_STATE(root_worker, KICKED); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 851 | return grpc_wakeup_fd_wakeup(&global_wakeup_fd); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 852 | } else if (next_worker->kick_state == UNKICKED) { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 853 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 854 | gpr_log(GPR_ERROR, " .. kicked %p", next_worker); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 855 | } |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 856 | GPR_ASSERT(next_worker->initialized_cv); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 857 | SET_KICK_STATE(next_worker, KICKED); |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 858 | gpr_cv_signal(&next_worker->cv); |
| 859 | return GRPC_ERROR_NONE; |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 860 | } else if (next_worker->kick_state == DESIGNATED_POLLER) { |
| 861 | if (root_worker->kick_state != DESIGNATED_POLLER) { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 862 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 863 | gpr_log( |
| 864 | GPR_ERROR, |
| 865 | " .. kicked root non-poller %p (initialized_cv=%d) (poller=%p)", |
| 866 | root_worker, root_worker->initialized_cv, next_worker); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 867 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 868 | SET_KICK_STATE(root_worker, KICKED); |
| 869 | if (root_worker->initialized_cv) { |
| 870 | gpr_cv_signal(&root_worker->cv); |
| 871 | } |
| 872 | return GRPC_ERROR_NONE; |
| 873 | } else { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 874 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 875 | gpr_log(GPR_ERROR, " .. non-root poller %p (root=%p)", next_worker, |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 876 | root_worker); |
| 877 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 878 | SET_KICK_STATE(next_worker, KICKED); |
| 879 | return grpc_wakeup_fd_wakeup(&global_wakeup_fd); |
| 880 | } |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 881 | } else { |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 882 | GPR_ASSERT(next_worker->kick_state == KICKED); |
| 883 | SET_KICK_STATE(next_worker, KICKED); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 884 | return GRPC_ERROR_NONE; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 885 | } |
| 886 | } else { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 887 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
| 888 | gpr_log(GPR_ERROR, " .. kicked while waking up"); |
| 889 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 890 | return GRPC_ERROR_NONE; |
| 891 | } |
Craig Tiller | 43bf259 | 2017-04-28 23:21:01 +0000 | [diff] [blame] | 892 | } else if (specific_worker->kick_state == KICKED) { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 893 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 894 | gpr_log(GPR_ERROR, " .. specific worker already kicked"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 895 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 896 | return GRPC_ERROR_NONE; |
| 897 | } else if (gpr_tls_get(&g_current_thread_worker) == |
| 898 | (intptr_t)specific_worker) { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 899 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 900 | gpr_log(GPR_ERROR, " .. mark %p kicked", specific_worker); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 901 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 902 | SET_KICK_STATE(specific_worker, KICKED); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 903 | return GRPC_ERROR_NONE; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 904 | } else if (specific_worker == |
| 905 | (grpc_pollset_worker *)gpr_atm_no_barrier_load(&g_active_poller)) { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 906 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 907 | gpr_log(GPR_ERROR, " .. kick active poller"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 908 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 909 | SET_KICK_STATE(specific_worker, KICKED); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 910 | return grpc_wakeup_fd_wakeup(&global_wakeup_fd); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 911 | } else if (specific_worker->initialized_cv) { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 912 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 913 | gpr_log(GPR_ERROR, " .. kick waiting worker"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 914 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 915 | SET_KICK_STATE(specific_worker, KICKED); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 916 | gpr_cv_signal(&specific_worker->cv); |
| 917 | return GRPC_ERROR_NONE; |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 918 | } else { |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 919 | if (GRPC_TRACER_ON(grpc_polling_trace)) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 920 | gpr_log(GPR_ERROR, " .. kick non-waiting worker"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 921 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 922 | SET_KICK_STATE(specific_worker, KICKED); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 923 | return GRPC_ERROR_NONE; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | static void pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 928 | grpc_fd *fd) {} |
| 929 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 930 | /******************************************************************************* |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 931 | * Pollset-set Definitions |
| 932 | */ |
| 933 | |
| 934 | static grpc_pollset_set *pollset_set_create(void) { |
| 935 | return (grpc_pollset_set *)((intptr_t)0xdeafbeef); |
| 936 | } |
| 937 | |
| 938 | static void pollset_set_destroy(grpc_exec_ctx *exec_ctx, |
| 939 | grpc_pollset_set *pss) {} |
| 940 | |
| 941 | static void pollset_set_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset_set *pss, |
| 942 | grpc_fd *fd) {} |
| 943 | |
| 944 | static void pollset_set_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset_set *pss, |
| 945 | grpc_fd *fd) {} |
| 946 | |
| 947 | static void pollset_set_add_pollset(grpc_exec_ctx *exec_ctx, |
| 948 | grpc_pollset_set *pss, grpc_pollset *ps) {} |
| 949 | |
| 950 | static void pollset_set_del_pollset(grpc_exec_ctx *exec_ctx, |
| 951 | grpc_pollset_set *pss, grpc_pollset *ps) {} |
| 952 | |
| 953 | static void pollset_set_add_pollset_set(grpc_exec_ctx *exec_ctx, |
| 954 | grpc_pollset_set *bag, |
| 955 | grpc_pollset_set *item) {} |
| 956 | |
| 957 | static void pollset_set_del_pollset_set(grpc_exec_ctx *exec_ctx, |
| 958 | grpc_pollset_set *bag, |
| 959 | grpc_pollset_set *item) {} |
| 960 | |
| 961 | /******************************************************************************* |
| 962 | * Event engine binding |
| 963 | */ |
| 964 | |
| 965 | static void shutdown_engine(void) { |
| 966 | fd_global_shutdown(); |
| 967 | pollset_global_shutdown(); |
Sree Kuchibhotla | 54c31c7 | 2017-07-17 14:57:27 -0700 | [diff] [blame] | 968 | close(g_epfd); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static const grpc_event_engine_vtable vtable = { |
| 972 | .pollset_size = sizeof(grpc_pollset), |
| 973 | |
| 974 | .fd_create = fd_create, |
| 975 | .fd_wrapped_fd = fd_wrapped_fd, |
| 976 | .fd_orphan = fd_orphan, |
| 977 | .fd_shutdown = fd_shutdown, |
| 978 | .fd_is_shutdown = fd_is_shutdown, |
| 979 | .fd_notify_on_read = fd_notify_on_read, |
| 980 | .fd_notify_on_write = fd_notify_on_write, |
| 981 | .fd_get_read_notifier_pollset = fd_get_read_notifier_pollset, |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 982 | |
| 983 | .pollset_init = pollset_init, |
| 984 | .pollset_shutdown = pollset_shutdown, |
| 985 | .pollset_destroy = pollset_destroy, |
| 986 | .pollset_work = pollset_work, |
| 987 | .pollset_kick = pollset_kick, |
| 988 | .pollset_add_fd = pollset_add_fd, |
| 989 | |
| 990 | .pollset_set_create = pollset_set_create, |
| 991 | .pollset_set_destroy = pollset_set_destroy, |
| 992 | .pollset_set_add_pollset = pollset_set_add_pollset, |
| 993 | .pollset_set_del_pollset = pollset_set_del_pollset, |
| 994 | .pollset_set_add_pollset_set = pollset_set_add_pollset_set, |
| 995 | .pollset_set_del_pollset_set = pollset_set_del_pollset_set, |
| 996 | .pollset_set_add_fd = pollset_set_add_fd, |
| 997 | .pollset_set_del_fd = pollset_set_del_fd, |
| 998 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 999 | .shutdown_engine = shutdown_engine, |
| 1000 | }; |
| 1001 | |
| 1002 | /* It is possible that GLIBC has epoll but the underlying kernel doesn't. |
| 1003 | * Create a dummy epoll_fd to make sure epoll support is available */ |
Craig Tiller | 6f0af49 | 2017-04-27 19:26:16 +0000 | [diff] [blame] | 1004 | const grpc_event_engine_vtable *grpc_init_epoll1_linux(bool explicit_request) { |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1005 | if (!grpc_has_wakeup_fd()) { |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1009 | g_epfd = epoll_create1(EPOLL_CLOEXEC); |
| 1010 | if (g_epfd < 0) { |
| 1011 | gpr_log(GPR_ERROR, "epoll unavailable"); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1012 | return NULL; |
| 1013 | } |
| 1014 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1015 | fd_global_init(); |
| 1016 | |
| 1017 | if (!GRPC_LOG_IF_ERROR("pollset_global_init", pollset_global_init())) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1018 | close(g_epfd); |
| 1019 | fd_global_shutdown(); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1020 | return NULL; |
| 1021 | } |
| 1022 | |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1023 | gpr_log(GPR_ERROR, "grpc epoll fd: %d", g_epfd); |
| 1024 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1025 | return &vtable; |
| 1026 | } |
| 1027 | |
| 1028 | #else /* defined(GRPC_LINUX_EPOLL) */ |
| 1029 | #if defined(GRPC_POSIX_SOCKET) |
| 1030 | #include "src/core/lib/iomgr/ev_posix.h" |
| 1031 | /* If GRPC_LINUX_EPOLL is not defined, it means epoll is not available. Return |
| 1032 | * NULL */ |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 1033 | const grpc_event_engine_vtable *grpc_init_epoll1_linux(bool explicit_request) { |
| 1034 | return NULL; |
| 1035 | } |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1036 | #endif /* defined(GRPC_POSIX_SOCKET) */ |
| 1037 | #endif /* !defined(GRPC_LINUX_EPOLL) */ |