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 | |
yang-g | ceb2475 | 2017-11-07 12:06:37 -0800 | [diff] [blame] | 21 | #include <grpc/support/log.h> |
| 22 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 23 | /* This polling engine is only relevant on linux kernels supporting epoll() */ |
| 24 | #ifdef GRPC_LINUX_EPOLL |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 25 | #include "src/core/lib/iomgr/ev_epoll1_linux.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 26 | |
| 27 | #include <assert.h> |
| 28 | #include <errno.h> |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 29 | #include <limits.h> |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 30 | #include <poll.h> |
| 31 | #include <pthread.h> |
| 32 | #include <string.h> |
| 33 | #include <sys/epoll.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include <grpc/support/alloc.h> |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 38 | #include <grpc/support/cpu.h> |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 39 | #include <grpc/support/string_util.h> |
| 40 | #include <grpc/support/tls.h> |
| 41 | #include <grpc/support/useful.h> |
| 42 | |
Craig Tiller | b4bb1cd | 2017-07-20 14:18:17 -0700 | [diff] [blame] | 43 | #include "src/core/lib/debug/stats.h" |
Craig Tiller | 6b7c1fb | 2017-07-19 15:45:03 -0700 | [diff] [blame] | 44 | #include "src/core/lib/iomgr/block_annotate.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 45 | #include "src/core/lib/iomgr/ev_posix.h" |
| 46 | #include "src/core/lib/iomgr/iomgr_internal.h" |
| 47 | #include "src/core/lib/iomgr/lockfree_event.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 48 | #include "src/core/lib/iomgr/wakeup_fd_posix.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 49 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 50 | #include "src/core/lib/support/manual_constructor.h" |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 51 | #include "src/core/lib/support/string.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 52 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 53 | static grpc_wakeup_fd global_wakeup_fd; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 54 | |
| 55 | /******************************************************************************* |
| 56 | * Singleton epoll set related fields |
| 57 | */ |
| 58 | |
| 59 | #define MAX_EPOLL_EVENTS 100 |
Sree Kuchibhotla | 1961452 | 2017-08-25 17:10:10 -0700 | [diff] [blame] | 60 | #define MAX_EPOLL_EVENTS_HANDLED_PER_ITERATION 1 |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 61 | |
Sree Kuchibhotla | e01940f | 2017-08-27 18:10:12 -0700 | [diff] [blame] | 62 | /* NOTE ON SYNCHRONIZATION: |
| 63 | * - Fields in this struct are only modified by the designated poller. Hence |
| 64 | * there is no need for any locks to protect the struct. |
| 65 | * - num_events and cursor fields have to be of atomic type to provide memory |
| 66 | * visibility guarantees only. i.e In case of multiple pollers, the designated |
| 67 | * polling thread keeps changing; the thread that wrote these values may be |
| 68 | * different from the thread reading the values |
| 69 | */ |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 70 | typedef struct epoll_set { |
| 71 | int epfd; |
| 72 | |
| 73 | /* The epoll_events after the last call to epoll_wait() */ |
| 74 | struct epoll_event events[MAX_EPOLL_EVENTS]; |
| 75 | |
| 76 | /* The number of epoll_events after the last call to epoll_wait() */ |
Sree Kuchibhotla | a92a9cc | 2017-08-27 14:02:15 -0700 | [diff] [blame] | 77 | gpr_atm num_events; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 78 | |
| 79 | /* Index of the first event in epoll_events that has to be processed. This |
| 80 | * field is only valid if num_events > 0 */ |
Sree Kuchibhotla | a92a9cc | 2017-08-27 14:02:15 -0700 | [diff] [blame] | 81 | gpr_atm cursor; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 82 | } epoll_set; |
| 83 | |
| 84 | /* The global singleton epoll set */ |
| 85 | static epoll_set g_epoll_set; |
| 86 | |
| 87 | /* Must be called *only* once */ |
| 88 | static bool epoll_set_init() { |
| 89 | g_epoll_set.epfd = epoll_create1(EPOLL_CLOEXEC); |
| 90 | if (g_epoll_set.epfd < 0) { |
| 91 | gpr_log(GPR_ERROR, "epoll unavailable"); |
| 92 | return false; |
| 93 | } |
| 94 | |
Sree Kuchibhotla | a92a9cc | 2017-08-27 14:02:15 -0700 | [diff] [blame] | 95 | gpr_log(GPR_INFO, "grpc epoll fd: %d", g_epoll_set.epfd); |
| 96 | gpr_atm_no_barrier_store(&g_epoll_set.num_events, 0); |
| 97 | gpr_atm_no_barrier_store(&g_epoll_set.cursor, 0); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
| 101 | /* epoll_set_init() MUST be called before calling this. */ |
| 102 | static void epoll_set_shutdown() { |
| 103 | if (g_epoll_set.epfd >= 0) { |
| 104 | close(g_epoll_set.epfd); |
| 105 | g_epoll_set.epfd = -1; |
| 106 | } |
| 107 | } |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 108 | |
| 109 | /******************************************************************************* |
| 110 | * Fd Declarations |
| 111 | */ |
| 112 | |
| 113 | struct grpc_fd { |
| 114 | int fd; |
| 115 | |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 116 | grpc_core::ManualConstructor<grpc_core::LockfreeEvent> read_closure; |
| 117 | grpc_core::ManualConstructor<grpc_core::LockfreeEvent> write_closure; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 118 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 119 | struct grpc_fd* freelist_next; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 120 | |
| 121 | /* The pollset that last noticed that the fd is readable. The actual type |
| 122 | * stored in this is (grpc_pollset *) */ |
| 123 | gpr_atm read_notifier_pollset; |
| 124 | |
| 125 | grpc_iomgr_object iomgr_object; |
| 126 | }; |
| 127 | |
| 128 | static void fd_global_init(void); |
| 129 | static void fd_global_shutdown(void); |
| 130 | |
| 131 | /******************************************************************************* |
| 132 | * Pollset Declarations |
| 133 | */ |
| 134 | |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 135 | typedef enum { UNKICKED, KICKED, DESIGNATED_POLLER } kick_state; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 136 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 137 | static const char* kick_state_string(kick_state st) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 138 | switch (st) { |
| 139 | case UNKICKED: |
| 140 | return "UNKICKED"; |
| 141 | case KICKED: |
| 142 | return "KICKED"; |
| 143 | case DESIGNATED_POLLER: |
| 144 | return "DESIGNATED_POLLER"; |
| 145 | } |
| 146 | GPR_UNREACHABLE_CODE(return "UNKNOWN"); |
| 147 | } |
| 148 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 149 | struct grpc_pollset_worker { |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 150 | kick_state state; |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 151 | int kick_state_mutator; // which line of code last changed kick state |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 152 | bool initialized_cv; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 153 | grpc_pollset_worker* next; |
| 154 | grpc_pollset_worker* prev; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 155 | gpr_cv cv; |
Craig Tiller | 50da5ec | 2017-05-01 13:51:14 -0700 | [diff] [blame] | 156 | grpc_closure_list schedule_on_end_work; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 159 | #define SET_KICK_STATE(worker, kick_state) \ |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 160 | do { \ |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 161 | (worker)->state = (kick_state); \ |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 162 | (worker)->kick_state_mutator = __LINE__; \ |
| 163 | } while (false) |
| 164 | |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 165 | #define MAX_NEIGHBORHOODS 1024 |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 166 | |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 167 | typedef struct pollset_neighborhood { |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 168 | gpr_mu mu; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 169 | grpc_pollset* active_root; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 170 | char pad[GPR_CACHELINE_SIZE]; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 171 | } pollset_neighborhood; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 172 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 173 | struct grpc_pollset { |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 174 | gpr_mu mu; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 175 | pollset_neighborhood* neighborhood; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 176 | bool reassigning_neighborhood; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 177 | grpc_pollset_worker* root_worker; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 178 | bool kicked_without_poller; |
Sree Kuchibhotla | 0d8431a | 2017-07-18 16:21:54 -0700 | [diff] [blame] | 179 | |
| 180 | /* Set to true if the pollset is observed to have no workers available to |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 181 | poll */ |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 182 | bool seen_inactive; |
Sree Kuchibhotla | 0d8431a | 2017-07-18 16:21:54 -0700 | [diff] [blame] | 183 | bool shutting_down; /* Is the pollset shutting down ? */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 184 | grpc_closure* shutdown_closure; /* Called after after shutdown is complete */ |
Sree Kuchibhotla | 0d8431a | 2017-07-18 16:21:54 -0700 | [diff] [blame] | 185 | |
| 186 | /* Number of workers who are *about-to* attach themselves to the pollset |
| 187 | * worker list */ |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 188 | int begin_refs; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 189 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 190 | grpc_pollset* next; |
| 191 | grpc_pollset* prev; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | /******************************************************************************* |
| 195 | * Pollset-set Declarations |
| 196 | */ |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 197 | |
Craig Tiller | 61f96c1 | 2017-05-12 13:36:39 -0700 | [diff] [blame] | 198 | struct grpc_pollset_set { |
| 199 | char unused; |
| 200 | }; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 201 | |
| 202 | /******************************************************************************* |
| 203 | * Common helpers |
| 204 | */ |
| 205 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 206 | static bool append_error(grpc_error** composite, grpc_error* error, |
| 207 | const char* desc) { |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 208 | if (error == GRPC_ERROR_NONE) return true; |
| 209 | if (*composite == GRPC_ERROR_NONE) { |
| 210 | *composite = GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc); |
| 211 | } |
| 212 | *composite = grpc_error_add_child(*composite, error); |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | /******************************************************************************* |
| 217 | * Fd Definitions |
| 218 | */ |
| 219 | |
| 220 | /* We need to keep a freelist not because of any concerns of malloc performance |
| 221 | * but instead so that implementations with multiple threads in (for example) |
| 222 | * epoll_wait deal with the race between pollset removal and incoming poll |
| 223 | * notifications. |
| 224 | * |
| 225 | * The problem is that the poller ultimately holds a reference to this |
| 226 | * object, so it is very difficult to know when is safe to free it, at least |
| 227 | * without some expensive synchronization. |
| 228 | * |
| 229 | * If we keep the object freelisted, in the worst case losing this race just |
| 230 | * becomes a spurious read notification on a reused fd. |
| 231 | */ |
| 232 | |
| 233 | /* The alarm system needs to be able to wakeup 'some poller' sometimes |
| 234 | * (specifically when a new alarm needs to be triggered earlier than the next |
| 235 | * alarm 'epoch'). This wakeup_fd gives us something to alert on when such a |
| 236 | * case occurs. */ |
| 237 | |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 238 | static grpc_fd* fd_freelist = nullptr; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 239 | static gpr_mu fd_freelist_mu; |
| 240 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 241 | static void fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); } |
| 242 | |
| 243 | static void fd_global_shutdown(void) { |
| 244 | gpr_mu_lock(&fd_freelist_mu); |
| 245 | gpr_mu_unlock(&fd_freelist_mu); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 246 | while (fd_freelist != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 247 | grpc_fd* fd = fd_freelist; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 248 | fd_freelist = fd_freelist->freelist_next; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 249 | gpr_free(fd); |
| 250 | } |
| 251 | gpr_mu_destroy(&fd_freelist_mu); |
| 252 | } |
| 253 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 254 | static grpc_fd* fd_create(int fd, const char* name) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 255 | grpc_fd* new_fd = nullptr; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 256 | |
| 257 | gpr_mu_lock(&fd_freelist_mu); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 258 | if (fd_freelist != nullptr) { |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 259 | new_fd = fd_freelist; |
| 260 | fd_freelist = fd_freelist->freelist_next; |
| 261 | } |
| 262 | gpr_mu_unlock(&fd_freelist_mu); |
| 263 | |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 264 | if (new_fd == nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 265 | new_fd = (grpc_fd*)gpr_malloc(sizeof(grpc_fd)); |
yang-g | 26521b3 | 2017-11-17 17:15:37 -0800 | [diff] [blame] | 266 | new_fd->read_closure.Init(); |
| 267 | new_fd->write_closure.Init(); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 270 | new_fd->fd = fd; |
yang-g | ed49fe5 | 2017-11-20 13:49:54 -0800 | [diff] [blame] | 271 | new_fd->read_closure->InitEvent(); |
| 272 | new_fd->write_closure->InitEvent(); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 273 | gpr_atm_no_barrier_store(&new_fd->read_notifier_pollset, (gpr_atm)NULL); |
| 274 | |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 275 | new_fd->freelist_next = nullptr; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 276 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 277 | char* fd_name; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 278 | gpr_asprintf(&fd_name, "%s fd=%d", name, fd); |
| 279 | grpc_iomgr_register_object(&new_fd->iomgr_object, fd_name); |
Noah Eisen | 264879f | 2017-06-20 17:14:47 -0700 | [diff] [blame] | 280 | #ifndef NDEBUG |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 281 | if (grpc_trace_fd_refcount.enabled()) { |
Noah Eisen | 264879f | 2017-06-20 17:14:47 -0700 | [diff] [blame] | 282 | gpr_log(GPR_DEBUG, "FD %d %p create %s", fd, new_fd, fd_name); |
| 283 | } |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 284 | #endif |
| 285 | gpr_free(fd_name); |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 286 | |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame] | 287 | struct epoll_event ev; |
| 288 | ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET); |
| 289 | ev.data.ptr = new_fd; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 290 | if (epoll_ctl(g_epoll_set.epfd, EPOLL_CTL_ADD, fd, &ev) != 0) { |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 291 | gpr_log(GPR_ERROR, "epoll_ctl failed: %s", strerror(errno)); |
| 292 | } |
| 293 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 294 | return new_fd; |
| 295 | } |
| 296 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 297 | static int fd_wrapped_fd(grpc_fd* fd) { return fd->fd; } |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 298 | |
Sree Kuchibhotla | f264147 | 2017-08-02 23:46:40 -0700 | [diff] [blame] | 299 | /* if 'releasing_fd' is true, it means that we are going to detach the internal |
| 300 | * fd from grpc_fd structure (i.e which means we should not be calling |
| 301 | * shutdown() syscall on that fd) */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 302 | static void fd_shutdown_internal(grpc_exec_ctx* exec_ctx, grpc_fd* fd, |
| 303 | grpc_error* why, bool releasing_fd) { |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 304 | if (fd->read_closure->SetShutdown(exec_ctx, GRPC_ERROR_REF(why))) { |
Sree Kuchibhotla | f264147 | 2017-08-02 23:46:40 -0700 | [diff] [blame] | 305 | if (!releasing_fd) { |
| 306 | shutdown(fd->fd, SHUT_RDWR); |
| 307 | } |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 308 | fd->write_closure->SetShutdown(exec_ctx, GRPC_ERROR_REF(why)); |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 309 | } |
| 310 | GRPC_ERROR_UNREF(why); |
| 311 | } |
| 312 | |
Sree Kuchibhotla | f264147 | 2017-08-02 23:46:40 -0700 | [diff] [blame] | 313 | /* Might be called multiple times */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 314 | static void fd_shutdown(grpc_exec_ctx* exec_ctx, grpc_fd* fd, grpc_error* why) { |
Sree Kuchibhotla | f264147 | 2017-08-02 23:46:40 -0700 | [diff] [blame] | 315 | fd_shutdown_internal(exec_ctx, fd, why, false); |
| 316 | } |
| 317 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 318 | static void fd_orphan(grpc_exec_ctx* exec_ctx, grpc_fd* fd, |
| 319 | grpc_closure* on_done, int* release_fd, |
| 320 | bool already_closed, const char* reason) { |
| 321 | grpc_error* error = GRPC_ERROR_NONE; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 322 | bool is_release_fd = (release_fd != nullptr); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 323 | |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 324 | if (!fd->read_closure->IsShutdown()) { |
Sree Kuchibhotla | f264147 | 2017-08-02 23:46:40 -0700 | [diff] [blame] | 325 | fd_shutdown_internal(exec_ctx, fd, |
| 326 | GRPC_ERROR_CREATE_FROM_COPIED_STRING(reason), |
| 327 | is_release_fd); |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 330 | /* If release_fd is not NULL, we should be relinquishing control of the file |
| 331 | descriptor fd->fd (but we still own the grpc_fd structure). */ |
Sree Kuchibhotla | f264147 | 2017-08-02 23:46:40 -0700 | [diff] [blame] | 332 | if (is_release_fd) { |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 333 | *release_fd = fd->fd; |
Yuchen Zeng | d40a7ae | 2017-07-12 15:59:56 -0700 | [diff] [blame] | 334 | } else if (!already_closed) { |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 335 | close(fd->fd); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 336 | } |
| 337 | |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 338 | GRPC_CLOSURE_SCHED(exec_ctx, on_done, GRPC_ERROR_REF(error)); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 339 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 340 | grpc_iomgr_unregister_object(&fd->iomgr_object); |
yang-g | ed49fe5 | 2017-11-20 13:49:54 -0800 | [diff] [blame] | 341 | fd->read_closure->DestroyEvent(); |
| 342 | fd->write_closure->DestroyEvent(); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 343 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 344 | gpr_mu_lock(&fd_freelist_mu); |
| 345 | fd->freelist_next = fd_freelist; |
| 346 | fd_freelist = fd; |
| 347 | gpr_mu_unlock(&fd_freelist_mu); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 350 | static grpc_pollset* fd_get_read_notifier_pollset(grpc_exec_ctx* exec_ctx, |
| 351 | grpc_fd* fd) { |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 352 | gpr_atm notifier = gpr_atm_acq_load(&fd->read_notifier_pollset); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 353 | return (grpc_pollset*)notifier; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 356 | static bool fd_is_shutdown(grpc_fd* fd) { |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 357 | return fd->read_closure->IsShutdown(); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 360 | static void fd_notify_on_read(grpc_exec_ctx* exec_ctx, grpc_fd* fd, |
| 361 | grpc_closure* closure) { |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 362 | fd->read_closure->NotifyOn(exec_ctx, closure); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 365 | static void fd_notify_on_write(grpc_exec_ctx* exec_ctx, grpc_fd* fd, |
| 366 | grpc_closure* closure) { |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 367 | fd->write_closure->NotifyOn(exec_ctx, closure); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 370 | static void fd_become_readable(grpc_exec_ctx* exec_ctx, grpc_fd* fd, |
| 371 | grpc_pollset* notifier) { |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 372 | fd->read_closure->SetReady(exec_ctx); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 373 | /* Use release store to match with acquire load in fd_get_read_notifier */ |
| 374 | gpr_atm_rel_store(&fd->read_notifier_pollset, (gpr_atm)notifier); |
| 375 | } |
| 376 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 377 | static void fd_become_writable(grpc_exec_ctx* exec_ctx, grpc_fd* fd) { |
Craig Tiller | fbf61bb | 2017-11-08 11:50:14 -0800 | [diff] [blame] | 378 | fd->write_closure->SetReady(exec_ctx); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /******************************************************************************* |
| 382 | * Pollset Definitions |
| 383 | */ |
| 384 | |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 385 | GPR_TLS_DECL(g_current_thread_pollset); |
| 386 | GPR_TLS_DECL(g_current_thread_worker); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 387 | |
| 388 | /* The designated poller */ |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 389 | static gpr_atm g_active_poller; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 390 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 391 | static pollset_neighborhood* g_neighborhoods; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 392 | static size_t g_num_neighborhoods; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 393 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 394 | /* Return true if first in list */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 395 | static bool worker_insert(grpc_pollset* pollset, grpc_pollset_worker* worker) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 396 | if (pollset->root_worker == nullptr) { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 397 | pollset->root_worker = worker; |
| 398 | worker->next = worker->prev = worker; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 399 | return true; |
| 400 | } else { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 401 | worker->next = pollset->root_worker; |
| 402 | worker->prev = worker->next->prev; |
| 403 | worker->next->prev = worker; |
| 404 | worker->prev->next = worker; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 405 | return false; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /* Return true if last in list */ |
| 410 | typedef enum { EMPTIED, NEW_ROOT, REMOVED } worker_remove_result; |
| 411 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 412 | static worker_remove_result worker_remove(grpc_pollset* pollset, |
| 413 | grpc_pollset_worker* worker) { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 414 | if (worker == pollset->root_worker) { |
| 415 | if (worker == worker->next) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 416 | pollset->root_worker = nullptr; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 417 | return EMPTIED; |
| 418 | } else { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 419 | pollset->root_worker = worker->next; |
| 420 | worker->prev->next = worker->next; |
| 421 | worker->next->prev = worker->prev; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 422 | return NEW_ROOT; |
| 423 | } |
| 424 | } else { |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 425 | worker->prev->next = worker->next; |
| 426 | worker->next->prev = worker->prev; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 427 | return REMOVED; |
| 428 | } |
| 429 | } |
| 430 | |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 431 | static size_t choose_neighborhood(void) { |
| 432 | return (size_t)gpr_cpu_current_cpu() % g_num_neighborhoods; |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 435 | static grpc_error* pollset_global_init(void) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 436 | gpr_tls_init(&g_current_thread_pollset); |
| 437 | gpr_tls_init(&g_current_thread_worker); |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 438 | gpr_atm_no_barrier_store(&g_active_poller, 0); |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 439 | global_wakeup_fd.read_fd = -1; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 440 | grpc_error* err = grpc_wakeup_fd_init(&global_wakeup_fd); |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 441 | if (err != GRPC_ERROR_NONE) return err; |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame] | 442 | struct epoll_event ev; |
| 443 | ev.events = (uint32_t)(EPOLLIN | EPOLLET); |
| 444 | ev.data.ptr = &global_wakeup_fd; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 445 | if (epoll_ctl(g_epoll_set.epfd, EPOLL_CTL_ADD, global_wakeup_fd.read_fd, |
| 446 | &ev) != 0) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 447 | return GRPC_OS_ERROR(errno, "epoll_ctl"); |
| 448 | } |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 449 | g_num_neighborhoods = GPR_CLAMP(gpr_cpu_num_cores(), 1, MAX_NEIGHBORHOODS); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 450 | g_neighborhoods = (pollset_neighborhood*)gpr_zalloc(sizeof(*g_neighborhoods) * |
| 451 | g_num_neighborhoods); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 452 | for (size_t i = 0; i < g_num_neighborhoods; i++) { |
| 453 | gpr_mu_init(&g_neighborhoods[i].mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 454 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 455 | return GRPC_ERROR_NONE; |
| 456 | } |
| 457 | |
| 458 | static void pollset_global_shutdown(void) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 459 | gpr_tls_destroy(&g_current_thread_pollset); |
| 460 | gpr_tls_destroy(&g_current_thread_worker); |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 461 | if (global_wakeup_fd.read_fd != -1) grpc_wakeup_fd_destroy(&global_wakeup_fd); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 462 | for (size_t i = 0; i < g_num_neighborhoods; i++) { |
| 463 | gpr_mu_destroy(&g_neighborhoods[i].mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 464 | } |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 465 | gpr_free(g_neighborhoods); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 468 | static void pollset_init(grpc_pollset* pollset, gpr_mu** mu) { |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 469 | gpr_mu_init(&pollset->mu); |
| 470 | *mu = &pollset->mu; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 471 | pollset->neighborhood = &g_neighborhoods[choose_neighborhood()]; |
| 472 | pollset->reassigning_neighborhood = false; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 473 | pollset->root_worker = nullptr; |
Sree Kuchibhotla | 3088230 | 2017-08-16 13:46:52 -0700 | [diff] [blame] | 474 | pollset->kicked_without_poller = false; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 475 | pollset->seen_inactive = true; |
Sree Kuchibhotla | 3088230 | 2017-08-16 13:46:52 -0700 | [diff] [blame] | 476 | pollset->shutting_down = false; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 477 | pollset->shutdown_closure = nullptr; |
Sree Kuchibhotla | 3088230 | 2017-08-16 13:46:52 -0700 | [diff] [blame] | 478 | pollset->begin_refs = 0; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 479 | pollset->next = pollset->prev = nullptr; |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 482 | static void pollset_destroy(grpc_exec_ctx* exec_ctx, grpc_pollset* pollset) { |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 483 | gpr_mu_lock(&pollset->mu); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 484 | if (!pollset->seen_inactive) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 485 | pollset_neighborhood* neighborhood = pollset->neighborhood; |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 486 | gpr_mu_unlock(&pollset->mu); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 487 | retry_lock_neighborhood: |
| 488 | gpr_mu_lock(&neighborhood->mu); |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 489 | gpr_mu_lock(&pollset->mu); |
| 490 | if (!pollset->seen_inactive) { |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 491 | if (pollset->neighborhood != neighborhood) { |
| 492 | gpr_mu_unlock(&neighborhood->mu); |
| 493 | neighborhood = pollset->neighborhood; |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 494 | gpr_mu_unlock(&pollset->mu); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 495 | goto retry_lock_neighborhood; |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 496 | } |
| 497 | pollset->prev->next = pollset->next; |
| 498 | pollset->next->prev = pollset->prev; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 499 | if (pollset == pollset->neighborhood->active_root) { |
| 500 | pollset->neighborhood->active_root = |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 501 | pollset->next == pollset ? nullptr : pollset->next; |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 502 | } |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 503 | } |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 504 | gpr_mu_unlock(&pollset->neighborhood->mu); |
Craig Tiller | 6de0593 | 2017-04-28 09:17:38 -0700 | [diff] [blame] | 505 | } |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 506 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 507 | gpr_mu_destroy(&pollset->mu); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 510 | static grpc_error* pollset_kick_all(grpc_exec_ctx* exec_ctx, |
| 511 | grpc_pollset* pollset) { |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 512 | GPR_TIMER_BEGIN("pollset_kick_all", 0); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 513 | grpc_error* error = GRPC_ERROR_NONE; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 514 | if (pollset->root_worker != nullptr) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 515 | grpc_pollset_worker* worker = pollset->root_worker; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 516 | do { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 517 | GRPC_STATS_INC_POLLSET_KICK(exec_ctx); |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 518 | switch (worker->state) { |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 519 | case KICKED: |
Craig Tiller | 480f5d8 | 2017-09-13 09:36:07 -0700 | [diff] [blame] | 520 | GRPC_STATS_INC_POLLSET_KICKED_AGAIN(exec_ctx); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 521 | break; |
| 522 | case UNKICKED: |
| 523 | SET_KICK_STATE(worker, KICKED); |
| 524 | if (worker->initialized_cv) { |
Craig Tiller | ebacb2f | 2017-09-13 12:32:33 -0700 | [diff] [blame] | 525 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV(exec_ctx); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 526 | gpr_cv_signal(&worker->cv); |
| 527 | } |
| 528 | break; |
| 529 | case DESIGNATED_POLLER: |
Craig Tiller | 480f5d8 | 2017-09-13 09:36:07 -0700 | [diff] [blame] | 530 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD(exec_ctx); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 531 | SET_KICK_STATE(worker, KICKED); |
| 532 | append_error(&error, grpc_wakeup_fd_wakeup(&global_wakeup_fd), |
Sree Kuchibhotla | 0d8431a | 2017-07-18 16:21:54 -0700 | [diff] [blame] | 533 | "pollset_kick_all"); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 534 | break; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 537 | worker = worker->next; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 538 | } while (worker != pollset->root_worker); |
| 539 | } |
Sree Kuchibhotla | 0d8431a | 2017-07-18 16:21:54 -0700 | [diff] [blame] | 540 | // TODO: sreek. Check if we need to set 'kicked_without_poller' to true here |
| 541 | // in the else case |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 542 | GPR_TIMER_END("pollset_kick_all", 0); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 543 | return error; |
| 544 | } |
| 545 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 546 | static void pollset_maybe_finish_shutdown(grpc_exec_ctx* exec_ctx, |
| 547 | grpc_pollset* pollset) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 548 | if (pollset->shutdown_closure != nullptr && pollset->root_worker == nullptr && |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 549 | pollset->begin_refs == 0) { |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 550 | GPR_TIMER_MARK("pollset_finish_shutdown", 0); |
ncteisen | 969b46e | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 551 | GRPC_CLOSURE_SCHED(exec_ctx, pollset->shutdown_closure, GRPC_ERROR_NONE); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 552 | pollset->shutdown_closure = nullptr; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 556 | static void pollset_shutdown(grpc_exec_ctx* exec_ctx, grpc_pollset* pollset, |
| 557 | grpc_closure* closure) { |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 558 | GPR_TIMER_BEGIN("pollset_shutdown", 0); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 559 | GPR_ASSERT(pollset->shutdown_closure == nullptr); |
Craig Tiller | c81512a | 2017-05-26 09:53:58 -0700 | [diff] [blame] | 560 | GPR_ASSERT(!pollset->shutting_down); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 561 | pollset->shutdown_closure = closure; |
Craig Tiller | c81512a | 2017-05-26 09:53:58 -0700 | [diff] [blame] | 562 | pollset->shutting_down = true; |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 563 | GRPC_LOG_IF_ERROR("pollset_shutdown", pollset_kick_all(exec_ctx, pollset)); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 564 | pollset_maybe_finish_shutdown(exec_ctx, pollset); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 565 | GPR_TIMER_END("pollset_shutdown", 0); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 568 | static int poll_deadline_to_millis_timeout(grpc_exec_ctx* exec_ctx, |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 569 | grpc_millis millis) { |
| 570 | if (millis == GRPC_MILLIS_INF_FUTURE) return -1; |
| 571 | grpc_millis delta = millis - grpc_exec_ctx_now(exec_ctx); |
Craig Tiller | d9b82bd | 2017-08-29 12:16:56 -0700 | [diff] [blame] | 572 | if (delta > INT_MAX) { |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 573 | return INT_MAX; |
Craig Tiller | d9b82bd | 2017-08-29 12:16:56 -0700 | [diff] [blame] | 574 | } else if (delta < 0) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 575 | return 0; |
Craig Tiller | d9b82bd | 2017-08-29 12:16:56 -0700 | [diff] [blame] | 576 | } else { |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 577 | return (int)delta; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 578 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 581 | /* Process the epoll events found by do_epoll_wait() function. |
| 582 | - g_epoll_set.cursor points to the index of the first event to be processed |
| 583 | - This function then processes up-to MAX_EPOLL_EVENTS_PER_ITERATION and |
| 584 | updates the g_epoll_set.cursor |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 585 | |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 586 | NOTE ON SYNCRHONIZATION: Similar to do_epoll_wait(), this function is only |
| 587 | called by g_active_poller thread. So there is no need for synchronization |
| 588 | when accessing fields in g_epoll_set */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 589 | static grpc_error* process_epoll_events(grpc_exec_ctx* exec_ctx, |
| 590 | grpc_pollset* pollset) { |
| 591 | static const char* err_desc = "process_events"; |
| 592 | grpc_error* error = GRPC_ERROR_NONE; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 593 | |
Sree Kuchibhotla | 3d609f1 | 2017-08-25 10:00:18 -0700 | [diff] [blame] | 594 | GPR_TIMER_BEGIN("process_epoll_events", 0); |
Sree Kuchibhotla | a92a9cc | 2017-08-27 14:02:15 -0700 | [diff] [blame] | 595 | long num_events = gpr_atm_acq_load(&g_epoll_set.num_events); |
| 596 | long cursor = gpr_atm_acq_load(&g_epoll_set.cursor); |
| 597 | for (int idx = 0; |
| 598 | (idx < MAX_EPOLL_EVENTS_HANDLED_PER_ITERATION) && cursor != num_events; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 599 | idx++) { |
Sree Kuchibhotla | a92a9cc | 2017-08-27 14:02:15 -0700 | [diff] [blame] | 600 | long c = cursor++; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 601 | struct epoll_event* ev = &g_epoll_set.events[c]; |
| 602 | void* data_ptr = ev->data.ptr; |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 603 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 604 | if (data_ptr == &global_wakeup_fd) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 605 | append_error(&error, grpc_wakeup_fd_consume_wakeup(&global_wakeup_fd), |
| 606 | err_desc); |
| 607 | } else { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 608 | grpc_fd* fd = (grpc_fd*)(data_ptr); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 609 | bool cancel = (ev->events & (EPOLLERR | EPOLLHUP)) != 0; |
| 610 | bool read_ev = (ev->events & (EPOLLIN | EPOLLPRI)) != 0; |
| 611 | bool write_ev = (ev->events & EPOLLOUT) != 0; |
| 612 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 613 | if (read_ev || cancel) { |
| 614 | fd_become_readable(exec_ctx, fd, pollset); |
| 615 | } |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 616 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 617 | if (write_ev || cancel) { |
| 618 | fd_become_writable(exec_ctx, fd); |
| 619 | } |
| 620 | } |
| 621 | } |
Sree Kuchibhotla | a92a9cc | 2017-08-27 14:02:15 -0700 | [diff] [blame] | 622 | gpr_atm_rel_store(&g_epoll_set.cursor, cursor); |
Sree Kuchibhotla | 3d609f1 | 2017-08-25 10:00:18 -0700 | [diff] [blame] | 623 | GPR_TIMER_END("process_epoll_events", 0); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 624 | return error; |
| 625 | } |
| 626 | |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 627 | /* Do epoll_wait and store the events in g_epoll_set.events field. This does not |
| 628 | "process" any of the events yet; that is done in process_epoll_events(). |
| 629 | *See process_epoll_events() function for more details. |
| 630 | |
| 631 | NOTE ON SYNCHRONIZATION: At any point of time, only the g_active_poller |
| 632 | (i.e the designated poller thread) will be calling this function. So there is |
| 633 | no need for any synchronization when accesing fields in g_epoll_set */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 634 | static grpc_error* do_epoll_wait(grpc_exec_ctx* exec_ctx, grpc_pollset* ps, |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 635 | grpc_millis deadline) { |
Sree Kuchibhotla | 3d609f1 | 2017-08-25 10:00:18 -0700 | [diff] [blame] | 636 | GPR_TIMER_BEGIN("do_epoll_wait", 0); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 637 | |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 638 | int r; |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 639 | int timeout = poll_deadline_to_millis_timeout(exec_ctx, deadline); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 640 | if (timeout != 0) { |
| 641 | GRPC_SCHEDULING_START_BLOCKING_REGION; |
| 642 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 643 | do { |
Craig Tiller | b4bb1cd | 2017-07-20 14:18:17 -0700 | [diff] [blame] | 644 | GRPC_STATS_INC_SYSCALL_POLL(exec_ctx); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 645 | r = epoll_wait(g_epoll_set.epfd, g_epoll_set.events, MAX_EPOLL_EVENTS, |
| 646 | timeout); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 647 | } while (r < 0 && errno == EINTR); |
| 648 | if (timeout != 0) { |
Craig Tiller | 781e91a | 2017-07-17 16:21:00 -0700 | [diff] [blame] | 649 | GRPC_SCHEDULING_END_BLOCKING_REGION_WITH_EXEC_CTX(exec_ctx); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if (r < 0) return GRPC_OS_ERROR(errno, "epoll_wait"); |
| 653 | |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 654 | GRPC_STATS_INC_POLL_EVENTS_RETURNED(exec_ctx, r); |
| 655 | |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 656 | if (grpc_polling_trace.enabled()) { |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 657 | gpr_log(GPR_DEBUG, "ps: %p poll got %d events", ps, r); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Sree Kuchibhotla | a92a9cc | 2017-08-27 14:02:15 -0700 | [diff] [blame] | 660 | gpr_atm_rel_store(&g_epoll_set.num_events, r); |
| 661 | gpr_atm_rel_store(&g_epoll_set.cursor, 0); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 662 | |
Sree Kuchibhotla | 3d609f1 | 2017-08-25 10:00:18 -0700 | [diff] [blame] | 663 | GPR_TIMER_END("do_epoll_wait", 0); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 664 | return GRPC_ERROR_NONE; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 667 | static bool begin_worker(grpc_exec_ctx* exec_ctx, grpc_pollset* pollset, |
| 668 | grpc_pollset_worker* worker, |
| 669 | grpc_pollset_worker** worker_hdl, |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 670 | grpc_millis deadline) { |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 671 | GPR_TIMER_BEGIN("begin_worker", 0); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 672 | if (worker_hdl != nullptr) *worker_hdl = worker; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 673 | worker->initialized_cv = false; |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 674 | SET_KICK_STATE(worker, UNKICKED); |
Craig Tiller | 50da5ec | 2017-05-01 13:51:14 -0700 | [diff] [blame] | 675 | worker->schedule_on_end_work = (grpc_closure_list)GRPC_CLOSURE_LIST_INIT; |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 676 | pollset->begin_refs++; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 677 | |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 678 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 679 | gpr_log(GPR_ERROR, "PS:%p BEGIN_STARTS:%p", pollset, worker); |
| 680 | } |
| 681 | |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 682 | if (pollset->seen_inactive) { |
| 683 | // pollset has been observed to be inactive, we need to move back to the |
| 684 | // active list |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 685 | bool is_reassigning = false; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 686 | if (!pollset->reassigning_neighborhood) { |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 687 | is_reassigning = true; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 688 | pollset->reassigning_neighborhood = true; |
| 689 | pollset->neighborhood = &g_neighborhoods[choose_neighborhood()]; |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 690 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 691 | pollset_neighborhood* neighborhood = pollset->neighborhood; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 692 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 693 | // pollset unlocked: state may change (even worker->kick_state) |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 694 | retry_lock_neighborhood: |
| 695 | gpr_mu_lock(&neighborhood->mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 696 | gpr_mu_lock(&pollset->mu); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 697 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 698 | gpr_log(GPR_ERROR, "PS:%p BEGIN_REORG:%p kick_state=%s is_reassigning=%d", |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 699 | pollset, worker, kick_state_string(worker->state), |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 700 | is_reassigning); |
| 701 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 702 | if (pollset->seen_inactive) { |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 703 | if (neighborhood != pollset->neighborhood) { |
| 704 | gpr_mu_unlock(&neighborhood->mu); |
| 705 | neighborhood = pollset->neighborhood; |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 706 | gpr_mu_unlock(&pollset->mu); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 707 | goto retry_lock_neighborhood; |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 708 | } |
Sree Kuchibhotla | 0d8431a | 2017-07-18 16:21:54 -0700 | [diff] [blame] | 709 | |
Sree Kuchibhotla | fb34940 | 2017-09-06 10:58:06 -0700 | [diff] [blame] | 710 | /* 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 Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 713 | neighborhood nor mark the pollset as active. |
Sree Kuchibhotla | fb34940 | 2017-09-06 10:58:06 -0700 | [diff] [blame] | 714 | |
| 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 Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 719 | if (worker->state == UNKICKED) { |
Sree Kuchibhotla | fb34940 | 2017-09-06 10:58:06 -0700 | [diff] [blame] | 720 | pollset->seen_inactive = false; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 721 | if (neighborhood->active_root == nullptr) { |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 722 | neighborhood->active_root = pollset->next = pollset->prev = pollset; |
Sree Kuchibhotla | fb34940 | 2017-09-06 10:58:06 -0700 | [diff] [blame] | 723 | /* Make this the designated poller if there isn't one already */ |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 724 | if (worker->state == UNKICKED && |
Sree Kuchibhotla | fb34940 | 2017-09-06 10:58:06 -0700 | [diff] [blame] | 725 | gpr_atm_no_barrier_cas(&g_active_poller, 0, (gpr_atm)worker)) { |
| 726 | SET_KICK_STATE(worker, DESIGNATED_POLLER); |
| 727 | } |
| 728 | } else { |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 729 | pollset->next = neighborhood->active_root; |
Sree Kuchibhotla | fb34940 | 2017-09-06 10:58:06 -0700 | [diff] [blame] | 730 | pollset->prev = pollset->next->prev; |
| 731 | pollset->next->prev = pollset->prev->next = pollset; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 732 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 735 | if (is_reassigning) { |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 736 | GPR_ASSERT(pollset->reassigning_neighborhood); |
| 737 | pollset->reassigning_neighborhood = false; |
Craig Tiller | e00d733 | 2017-05-01 15:43:51 +0000 | [diff] [blame] | 738 | } |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 739 | gpr_mu_unlock(&neighborhood->mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 740 | } |
Sree Kuchibhotla | e6506bc | 2017-07-18 21:43:45 -0700 | [diff] [blame] | 741 | |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 742 | worker_insert(pollset, worker); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 743 | pollset->begin_refs--; |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 744 | if (worker->state == UNKICKED && !pollset->kicked_without_poller) { |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 745 | 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] | 746 | worker->initialized_cv = true; |
| 747 | gpr_cv_init(&worker->cv); |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 748 | while (worker->state == UNKICKED && !pollset->shutting_down) { |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 749 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 750 | gpr_log(GPR_ERROR, "PS:%p BEGIN_WAIT:%p kick_state=%s shutdown=%d", |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 751 | pollset, worker, kick_state_string(worker->state), |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 752 | pollset->shutting_down); |
| 753 | } |
Sree Kuchibhotla | 0d8431a | 2017-07-18 16:21:54 -0700 | [diff] [blame] | 754 | |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 755 | if (gpr_cv_wait(&worker->cv, &pollset->mu, |
| 756 | grpc_millis_to_timespec(deadline, GPR_CLOCK_REALTIME)) && |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 757 | worker->state == UNKICKED) { |
Sree Kuchibhotla | 0d8431a | 2017-07-18 16:21:54 -0700 | [diff] [blame] | 758 | /* If gpr_cv_wait returns true (i.e a timeout), pretend that the worker |
| 759 | received a kick */ |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 760 | SET_KICK_STATE(worker, KICKED); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 761 | } |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 762 | } |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 763 | grpc_exec_ctx_invalidate_now(exec_ctx); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 764 | } |
| 765 | |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 766 | if (grpc_polling_trace.enabled()) { |
Sree Kuchibhotla | 949d075 | 2017-07-20 23:49:15 -0700 | [diff] [blame] | 767 | gpr_log(GPR_ERROR, |
| 768 | "PS:%p BEGIN_DONE:%p kick_state=%s shutdown=%d " |
| 769 | "kicked_without_poller: %d", |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 770 | pollset, worker, kick_state_string(worker->state), |
Sree Kuchibhotla | 949d075 | 2017-07-20 23:49:15 -0700 | [diff] [blame] | 771 | pollset->shutting_down, pollset->kicked_without_poller); |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 772 | } |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 773 | |
Sree Kuchibhotla | e6506bc | 2017-07-18 21:43:45 -0700 | [diff] [blame] | 774 | /* We release pollset lock in this function at a couple of places: |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 775 | * 1. Briefly when assigning pollset to a neighborhood |
Sree Kuchibhotla | e6506bc | 2017-07-18 21:43:45 -0700 | [diff] [blame] | 776 | * 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 Kuchibhotla | e6506bc | 2017-07-18 21:43:45 -0700 | [diff] [blame] | 780 | /* 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 Kuchibhotla | a0616ef | 2017-07-18 23:49:49 -0700 | [diff] [blame] | 783 | if (pollset->kicked_without_poller) { |
| 784 | pollset->kicked_without_poller = false; |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 785 | GPR_TIMER_END("begin_worker", 0); |
Sree Kuchibhotla | a0616ef | 2017-07-18 23:49:49 -0700 | [diff] [blame] | 786 | return false; |
| 787 | } |
| 788 | |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 789 | GPR_TIMER_END("begin_worker", 0); |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 790 | return worker->state == DESIGNATED_POLLER && !pollset->shutting_down; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 793 | static bool check_neighborhood_for_available_poller( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 794 | grpc_exec_ctx* exec_ctx, pollset_neighborhood* neighborhood) { |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 795 | GPR_TIMER_BEGIN("check_neighborhood_for_available_poller", 0); |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 796 | bool found_worker = false; |
| 797 | do { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 798 | grpc_pollset* inspect = neighborhood->active_root; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 799 | if (inspect == nullptr) { |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 800 | break; |
| 801 | } |
| 802 | gpr_mu_lock(&inspect->mu); |
| 803 | GPR_ASSERT(!inspect->seen_inactive); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 804 | grpc_pollset_worker* inspect_worker = inspect->root_worker; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 805 | if (inspect_worker != nullptr) { |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 806 | do { |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 807 | switch (inspect_worker->state) { |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 808 | case UNKICKED: |
| 809 | if (gpr_atm_no_barrier_cas(&g_active_poller, 0, |
| 810 | (gpr_atm)inspect_worker)) { |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 811 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 812 | gpr_log(GPR_DEBUG, " .. choose next poller to be %p", |
| 813 | inspect_worker); |
| 814 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 815 | SET_KICK_STATE(inspect_worker, DESIGNATED_POLLER); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 816 | if (inspect_worker->initialized_cv) { |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 817 | GPR_TIMER_MARK("signal worker", 0); |
Craig Tiller | cf34fa5 | 2017-09-13 12:37:01 -0700 | [diff] [blame] | 818 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV(exec_ctx); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 819 | gpr_cv_signal(&inspect_worker->cv); |
| 820 | } |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 821 | } else { |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 822 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 823 | gpr_log(GPR_DEBUG, " .. beaten to choose next poller"); |
| 824 | } |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 825 | } |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 826 | // even if we didn't win the cas, there's a worker, we can stop |
| 827 | found_worker = true; |
| 828 | break; |
| 829 | case KICKED: |
| 830 | break; |
| 831 | case DESIGNATED_POLLER: |
| 832 | found_worker = true; // ok, so someone else found the worker, but |
| 833 | // we'll accept that |
| 834 | break; |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 835 | } |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 836 | inspect_worker = inspect_worker->next; |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 837 | } while (!found_worker && inspect_worker != inspect->root_worker); |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 838 | } |
| 839 | if (!found_worker) { |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 840 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 841 | gpr_log(GPR_DEBUG, " .. mark pollset %p inactive", inspect); |
| 842 | } |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 843 | inspect->seen_inactive = true; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 844 | if (inspect == neighborhood->active_root) { |
| 845 | neighborhood->active_root = |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 846 | inspect->next == inspect ? nullptr : inspect->next; |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 847 | } |
| 848 | inspect->next->prev = inspect->prev; |
| 849 | inspect->prev->next = inspect->next; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 850 | inspect->next = inspect->prev = nullptr; |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 851 | } |
| 852 | gpr_mu_unlock(&inspect->mu); |
| 853 | } while (!found_worker); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 854 | GPR_TIMER_END("check_neighborhood_for_available_poller", 0); |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 855 | return found_worker; |
| 856 | } |
| 857 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 858 | static void end_worker(grpc_exec_ctx* exec_ctx, grpc_pollset* pollset, |
| 859 | grpc_pollset_worker* worker, |
| 860 | grpc_pollset_worker** worker_hdl) { |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 861 | GPR_TIMER_BEGIN("end_worker", 0); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 862 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 863 | gpr_log(GPR_DEBUG, "PS:%p END_WORKER:%p", pollset, worker); |
| 864 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 865 | if (worker_hdl != nullptr) *worker_hdl = nullptr; |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 866 | /* Make sure we appear kicked */ |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 867 | SET_KICK_STATE(worker, KICKED); |
Craig Tiller | 50da5ec | 2017-05-01 13:51:14 -0700 | [diff] [blame] | 868 | grpc_closure_list_move(&worker->schedule_on_end_work, |
| 869 | &exec_ctx->closure_list); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 870 | if (gpr_atm_no_barrier_load(&g_active_poller) == (gpr_atm)worker) { |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 871 | if (worker->next != worker && worker->next->state == UNKICKED) { |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 872 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 873 | gpr_log(GPR_DEBUG, " .. choose next poller to be peer %p", worker); |
| 874 | } |
Craig Tiller | 2acab6e | 2017-04-30 23:06:33 +0000 | [diff] [blame] | 875 | GPR_ASSERT(worker->next->initialized_cv); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 876 | gpr_atm_no_barrier_store(&g_active_poller, (gpr_atm)worker->next); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 877 | SET_KICK_STATE(worker->next, DESIGNATED_POLLER); |
Craig Tiller | 1a012bb | 2017-09-13 14:29:00 -0700 | [diff] [blame] | 878 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV(exec_ctx); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 879 | gpr_cv_signal(&worker->next->cv); |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 880 | if (grpc_exec_ctx_has_work(exec_ctx)) { |
| 881 | gpr_mu_unlock(&pollset->mu); |
| 882 | grpc_exec_ctx_flush(exec_ctx); |
| 883 | gpr_mu_lock(&pollset->mu); |
| 884 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 885 | } else { |
| 886 | gpr_atm_no_barrier_store(&g_active_poller, 0); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 887 | size_t poller_neighborhood_idx = |
| 888 | (size_t)(pollset->neighborhood - g_neighborhoods); |
Craig Tiller | bb74267 | 2017-05-17 22:19:05 +0000 | [diff] [blame] | 889 | gpr_mu_unlock(&pollset->mu); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 890 | bool found_worker = false; |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 891 | bool scan_state[MAX_NEIGHBORHOODS]; |
| 892 | for (size_t i = 0; !found_worker && i < g_num_neighborhoods; i++) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 893 | pollset_neighborhood* neighborhood = |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 894 | &g_neighborhoods[(poller_neighborhood_idx + i) % |
| 895 | g_num_neighborhoods]; |
| 896 | if (gpr_mu_trylock(&neighborhood->mu)) { |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 897 | found_worker = |
Craig Tiller | 64f8b12 | 2017-09-13 12:39:21 -0700 | [diff] [blame] | 898 | check_neighborhood_for_available_poller(exec_ctx, neighborhood); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 899 | gpr_mu_unlock(&neighborhood->mu); |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 900 | scan_state[i] = true; |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 901 | } else { |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 902 | scan_state[i] = false; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 903 | } |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 904 | } |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 905 | for (size_t i = 0; !found_worker && i < g_num_neighborhoods; i++) { |
Craig Tiller | ba550da | 2017-05-01 14:26:31 +0000 | [diff] [blame] | 906 | if (scan_state[i]) continue; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 907 | pollset_neighborhood* neighborhood = |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 908 | &g_neighborhoods[(poller_neighborhood_idx + i) % |
| 909 | g_num_neighborhoods]; |
| 910 | gpr_mu_lock(&neighborhood->mu); |
Craig Tiller | cf34fa5 | 2017-09-13 12:37:01 -0700 | [diff] [blame] | 911 | found_worker = |
Craig Tiller | 64f8b12 | 2017-09-13 12:39:21 -0700 | [diff] [blame] | 912 | check_neighborhood_for_available_poller(exec_ctx, neighborhood); |
Vijay Pai | 4b7ef4d | 2017-09-11 23:09:22 -0700 | [diff] [blame] | 913 | gpr_mu_unlock(&neighborhood->mu); |
Craig Tiller | bbf4c7a | 2017-04-28 15:12:10 -0700 | [diff] [blame] | 914 | } |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 915 | grpc_exec_ctx_flush(exec_ctx); |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 916 | gpr_mu_lock(&pollset->mu); |
| 917 | } |
Craig Tiller | 50da5ec | 2017-05-01 13:51:14 -0700 | [diff] [blame] | 918 | } else if (grpc_exec_ctx_has_work(exec_ctx)) { |
| 919 | gpr_mu_unlock(&pollset->mu); |
| 920 | grpc_exec_ctx_flush(exec_ctx); |
| 921 | gpr_mu_lock(&pollset->mu); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 922 | } |
| 923 | if (worker->initialized_cv) { |
| 924 | gpr_cv_destroy(&worker->cv); |
| 925 | } |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 926 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 927 | gpr_log(GPR_DEBUG, " .. remove worker"); |
| 928 | } |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 929 | if (EMPTIED == worker_remove(pollset, worker)) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 930 | pollset_maybe_finish_shutdown(exec_ctx, pollset); |
| 931 | } |
Craig Tiller | a4b8eb0 | 2017-04-29 00:13:52 +0000 | [diff] [blame] | 932 | GPR_ASSERT(gpr_atm_no_barrier_load(&g_active_poller) != (gpr_atm)worker); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 933 | GPR_TIMER_END("end_worker", 0); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /* pollset->po.mu lock must be held by the caller before calling this. |
| 937 | The function pollset_work() may temporarily release the lock (pollset->po.mu) |
| 938 | during the course of its execution but it will always re-acquire the lock and |
| 939 | ensure that it is held by the time the function returns */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 940 | static grpc_error* pollset_work(grpc_exec_ctx* exec_ctx, grpc_pollset* ps, |
| 941 | grpc_pollset_worker** worker_hdl, |
Craig Tiller | 2039779 | 2017-07-18 11:35:27 -0700 | [diff] [blame] | 942 | grpc_millis deadline) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 943 | grpc_pollset_worker worker; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 944 | grpc_error* error = GRPC_ERROR_NONE; |
| 945 | static const char* err_desc = "pollset_work"; |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 946 | GPR_TIMER_BEGIN("pollset_work", 0); |
Sree Kuchibhotla | b154cd1 | 2017-08-25 10:33:41 -0700 | [diff] [blame] | 947 | if (ps->kicked_without_poller) { |
| 948 | ps->kicked_without_poller = false; |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 949 | GPR_TIMER_END("pollset_work", 0); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 950 | return GRPC_ERROR_NONE; |
| 951 | } |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 952 | |
Craig Tiller | d9b82bd | 2017-08-29 12:16:56 -0700 | [diff] [blame] | 953 | if (begin_worker(exec_ctx, ps, &worker, worker_hdl, deadline)) { |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 954 | gpr_tls_set(&g_current_thread_pollset, (intptr_t)ps); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 955 | gpr_tls_set(&g_current_thread_worker, (intptr_t)&worker); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 956 | GPR_ASSERT(!ps->shutting_down); |
| 957 | GPR_ASSERT(!ps->seen_inactive); |
| 958 | |
| 959 | gpr_mu_unlock(&ps->mu); /* unlock */ |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 960 | /* This is the designated polling thread at this point and should ideally do |
| 961 | polling. However, if there are unprocessed events left from a previous |
| 962 | call to do_epoll_wait(), skip calling epoll_wait() in this iteration and |
| 963 | process the pending epoll events. |
| 964 | |
| 965 | The reason for decoupling do_epoll_wait and process_epoll_events is to |
| 966 | better distrubute the work (i.e handling epoll events) across multiple |
| 967 | threads |
| 968 | |
| 969 | process_epoll_events() returns very quickly: It just queues the work on |
| 970 | exec_ctx but does not execute it (the actual exectution or more |
| 971 | accurately grpc_exec_ctx_flush() happens in end_worker() AFTER selecting |
| 972 | a designated poller). So we are not waiting long periods without a |
| 973 | designated poller */ |
Sree Kuchibhotla | a92a9cc | 2017-08-27 14:02:15 -0700 | [diff] [blame] | 974 | if (gpr_atm_acq_load(&g_epoll_set.cursor) == |
| 975 | gpr_atm_acq_load(&g_epoll_set.num_events)) { |
Craig Tiller | d9b82bd | 2017-08-29 12:16:56 -0700 | [diff] [blame] | 976 | append_error(&error, do_epoll_wait(exec_ctx, ps, deadline), err_desc); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 977 | } |
| 978 | append_error(&error, process_epoll_events(exec_ctx, ps), err_desc); |
| 979 | |
| 980 | gpr_mu_lock(&ps->mu); /* lock */ |
| 981 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 982 | gpr_tls_set(&g_current_thread_worker, 0); |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 983 | } else { |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 984 | gpr_tls_set(&g_current_thread_pollset, (intptr_t)ps); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 985 | } |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 986 | end_worker(exec_ctx, ps, &worker, worker_hdl); |
| 987 | |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 988 | gpr_tls_set(&g_current_thread_pollset, 0); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 989 | GPR_TIMER_END("pollset_work", 0); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 990 | return error; |
| 991 | } |
| 992 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 993 | static grpc_error* pollset_kick(grpc_exec_ctx* exec_ctx, grpc_pollset* pollset, |
| 994 | grpc_pollset_worker* specific_worker) { |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 995 | GPR_TIMER_BEGIN("pollset_kick", 0); |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 996 | GRPC_STATS_INC_POLLSET_KICK(exec_ctx); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 997 | grpc_error* ret_err = GRPC_ERROR_NONE; |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 998 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 999 | gpr_strvec log; |
| 1000 | gpr_strvec_init(&log); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1001 | char* tmp; |
| 1002 | gpr_asprintf(&tmp, "PS:%p KICK:%p curps=%p curworker=%p root=%p", pollset, |
| 1003 | specific_worker, (void*)gpr_tls_get(&g_current_thread_pollset), |
| 1004 | (void*)gpr_tls_get(&g_current_thread_worker), |
| 1005 | pollset->root_worker); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 1006 | gpr_strvec_add(&log, tmp); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1007 | if (pollset->root_worker != nullptr) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1008 | gpr_asprintf(&tmp, " {kick_state=%s next=%p {kick_state=%s}}", |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1009 | kick_state_string(pollset->root_worker->state), |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1010 | pollset->root_worker->next, |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1011 | kick_state_string(pollset->root_worker->next->state)); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 1012 | gpr_strvec_add(&log, tmp); |
| 1013 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1014 | if (specific_worker != nullptr) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1015 | gpr_asprintf(&tmp, " worker_kick_state=%s", |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1016 | kick_state_string(specific_worker->state)); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 1017 | gpr_strvec_add(&log, tmp); |
| 1018 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1019 | tmp = gpr_strvec_flatten(&log, nullptr); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 1020 | gpr_strvec_destroy(&log); |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1021 | gpr_log(GPR_ERROR, "%s", tmp); |
Craig Tiller | b89bac0 | 2017-05-26 15:20:32 +0000 | [diff] [blame] | 1022 | gpr_free(tmp); |
| 1023 | } |
Sree Kuchibhotla | fb34940 | 2017-09-06 10:58:06 -0700 | [diff] [blame] | 1024 | |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1025 | if (specific_worker == nullptr) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1026 | if (gpr_tls_get(&g_current_thread_pollset) != (intptr_t)pollset) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1027 | grpc_pollset_worker* root_worker = pollset->root_worker; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1028 | if (root_worker == nullptr) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1029 | GRPC_STATS_INC_POLLSET_KICKED_WITHOUT_POLLER(exec_ctx); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1030 | pollset->kicked_without_poller = true; |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1031 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1032 | gpr_log(GPR_ERROR, " .. kicked_without_poller"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1033 | } |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1034 | goto done; |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 1035 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1036 | grpc_pollset_worker* next_worker = root_worker->next; |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1037 | if (root_worker->state == KICKED) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1038 | GRPC_STATS_INC_POLLSET_KICKED_AGAIN(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1039 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1040 | gpr_log(GPR_ERROR, " .. already kicked %p", root_worker); |
| 1041 | } |
| 1042 | SET_KICK_STATE(root_worker, KICKED); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1043 | goto done; |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1044 | } else if (next_worker->state == KICKED) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1045 | GRPC_STATS_INC_POLLSET_KICKED_AGAIN(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1046 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1047 | gpr_log(GPR_ERROR, " .. already kicked %p", next_worker); |
| 1048 | } |
| 1049 | SET_KICK_STATE(next_worker, KICKED); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1050 | goto done; |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1051 | } else if (root_worker == |
| 1052 | next_worker && // only try and wake up a poller if |
| 1053 | // there is no next worker |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1054 | root_worker == (grpc_pollset_worker*)gpr_atm_no_barrier_load( |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1055 | &g_active_poller)) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1056 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1057 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1058 | gpr_log(GPR_ERROR, " .. kicked %p", root_worker); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1059 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1060 | SET_KICK_STATE(root_worker, KICKED); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1061 | ret_err = grpc_wakeup_fd_wakeup(&global_wakeup_fd); |
| 1062 | goto done; |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1063 | } else if (next_worker->state == UNKICKED) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1064 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1065 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1066 | gpr_log(GPR_ERROR, " .. kicked %p", next_worker); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1067 | } |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 1068 | GPR_ASSERT(next_worker->initialized_cv); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1069 | SET_KICK_STATE(next_worker, KICKED); |
Craig Tiller | 375eb25 | 2017-04-27 23:29:12 +0000 | [diff] [blame] | 1070 | gpr_cv_signal(&next_worker->cv); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1071 | goto done; |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1072 | } else if (next_worker->state == DESIGNATED_POLLER) { |
| 1073 | if (root_worker->state != DESIGNATED_POLLER) { |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1074 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1075 | gpr_log( |
| 1076 | GPR_ERROR, |
| 1077 | " .. kicked root non-poller %p (initialized_cv=%d) (poller=%p)", |
| 1078 | root_worker, root_worker->initialized_cv, next_worker); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1079 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1080 | SET_KICK_STATE(root_worker, KICKED); |
| 1081 | if (root_worker->initialized_cv) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1082 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV(exec_ctx); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1083 | gpr_cv_signal(&root_worker->cv); |
| 1084 | } |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1085 | goto done; |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1086 | } else { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1087 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1088 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1089 | gpr_log(GPR_ERROR, " .. non-root poller %p (root=%p)", next_worker, |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1090 | root_worker); |
| 1091 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1092 | SET_KICK_STATE(next_worker, KICKED); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1093 | ret_err = grpc_wakeup_fd_wakeup(&global_wakeup_fd); |
| 1094 | goto done; |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1095 | } |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 1096 | } else { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1097 | GRPC_STATS_INC_POLLSET_KICKED_AGAIN(exec_ctx); |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1098 | GPR_ASSERT(next_worker->state == KICKED); |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1099 | SET_KICK_STATE(next_worker, KICKED); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1100 | goto done; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1101 | } |
| 1102 | } else { |
Craig Tiller | 1a012bb | 2017-09-13 14:29:00 -0700 | [diff] [blame] | 1103 | GRPC_STATS_INC_POLLSET_KICK_OWN_THREAD(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1104 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1105 | gpr_log(GPR_ERROR, " .. kicked while waking up"); |
| 1106 | } |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1107 | goto done; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1108 | } |
Sree Kuchibhotla | fb34940 | 2017-09-06 10:58:06 -0700 | [diff] [blame] | 1109 | |
| 1110 | GPR_UNREACHABLE_CODE(goto done); |
| 1111 | } |
| 1112 | |
Yash Tibrewal | b2a54ac | 2017-09-13 10:18:07 -0700 | [diff] [blame] | 1113 | if (specific_worker->state == KICKED) { |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1114 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1115 | gpr_log(GPR_ERROR, " .. specific worker already kicked"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1116 | } |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1117 | goto done; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1118 | } else if (gpr_tls_get(&g_current_thread_worker) == |
| 1119 | (intptr_t)specific_worker) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1120 | GRPC_STATS_INC_POLLSET_KICK_OWN_THREAD(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1121 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1122 | gpr_log(GPR_ERROR, " .. mark %p kicked", specific_worker); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1123 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1124 | SET_KICK_STATE(specific_worker, KICKED); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1125 | goto done; |
Craig Tiller | 32f90ee | 2017-04-28 12:46:41 -0700 | [diff] [blame] | 1126 | } else if (specific_worker == |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1127 | (grpc_pollset_worker*)gpr_atm_no_barrier_load(&g_active_poller)) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1128 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_FD(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1129 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1130 | gpr_log(GPR_ERROR, " .. kick active poller"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1131 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1132 | SET_KICK_STATE(specific_worker, KICKED); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1133 | ret_err = grpc_wakeup_fd_wakeup(&global_wakeup_fd); |
| 1134 | goto done; |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 1135 | } else if (specific_worker->initialized_cv) { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1136 | GRPC_STATS_INC_POLLSET_KICK_WAKEUP_CV(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1137 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1138 | gpr_log(GPR_ERROR, " .. kick waiting worker"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1139 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1140 | SET_KICK_STATE(specific_worker, KICKED); |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1141 | gpr_cv_signal(&specific_worker->cv); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1142 | goto done; |
Craig Tiller | 8502ecb | 2017-04-28 14:22:01 -0700 | [diff] [blame] | 1143 | } else { |
Craig Tiller | 0ff222a | 2017-09-01 09:41:43 -0700 | [diff] [blame] | 1144 | GRPC_STATS_INC_POLLSET_KICKED_AGAIN(exec_ctx); |
ncteisen | 3cffe1f | 2017-11-10 13:56:23 -0800 | [diff] [blame] | 1145 | if (grpc_polling_trace.enabled()) { |
Craig Tiller | 830e82a | 2017-05-31 16:26:27 -0700 | [diff] [blame] | 1146 | gpr_log(GPR_ERROR, " .. kick non-waiting worker"); |
Craig Tiller | 75aef7f | 2017-05-26 08:26:08 -0700 | [diff] [blame] | 1147 | } |
Craig Tiller | 55624a3 | 2017-05-26 08:14:44 -0700 | [diff] [blame] | 1148 | SET_KICK_STATE(specific_worker, KICKED); |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1149 | goto done; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1150 | } |
yang-g | df92a64 | 2017-08-21 22:38:45 -0700 | [diff] [blame] | 1151 | done: |
| 1152 | GPR_TIMER_END("pollset_kick", 0); |
| 1153 | return ret_err; |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1156 | static void pollset_add_fd(grpc_exec_ctx* exec_ctx, grpc_pollset* pollset, |
| 1157 | grpc_fd* fd) {} |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1158 | |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1159 | /******************************************************************************* |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1160 | * Pollset-set Definitions |
| 1161 | */ |
| 1162 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1163 | static grpc_pollset_set* pollset_set_create(void) { |
| 1164 | return (grpc_pollset_set*)((intptr_t)0xdeafbeef); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1167 | static void pollset_set_destroy(grpc_exec_ctx* exec_ctx, |
| 1168 | grpc_pollset_set* pss) {} |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1169 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1170 | static void pollset_set_add_fd(grpc_exec_ctx* exec_ctx, grpc_pollset_set* pss, |
| 1171 | grpc_fd* fd) {} |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1172 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1173 | static void pollset_set_del_fd(grpc_exec_ctx* exec_ctx, grpc_pollset_set* pss, |
| 1174 | grpc_fd* fd) {} |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1175 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1176 | static void pollset_set_add_pollset(grpc_exec_ctx* exec_ctx, |
| 1177 | grpc_pollset_set* pss, grpc_pollset* ps) {} |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1178 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1179 | static void pollset_set_del_pollset(grpc_exec_ctx* exec_ctx, |
| 1180 | grpc_pollset_set* pss, grpc_pollset* ps) {} |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1181 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1182 | static void pollset_set_add_pollset_set(grpc_exec_ctx* exec_ctx, |
| 1183 | grpc_pollset_set* bag, |
| 1184 | grpc_pollset_set* item) {} |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1185 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1186 | static void pollset_set_del_pollset_set(grpc_exec_ctx* exec_ctx, |
| 1187 | grpc_pollset_set* bag, |
| 1188 | grpc_pollset_set* item) {} |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1189 | |
| 1190 | /******************************************************************************* |
| 1191 | * Event engine binding |
| 1192 | */ |
| 1193 | |
| 1194 | static void shutdown_engine(void) { |
| 1195 | fd_global_shutdown(); |
| 1196 | pollset_global_shutdown(); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 1197 | epoll_set_shutdown(); |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | static const grpc_event_engine_vtable vtable = { |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame] | 1201 | sizeof(grpc_pollset), |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1202 | |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame] | 1203 | fd_create, |
| 1204 | fd_wrapped_fd, |
| 1205 | fd_orphan, |
| 1206 | fd_shutdown, |
| 1207 | fd_notify_on_read, |
| 1208 | fd_notify_on_write, |
| 1209 | fd_is_shutdown, |
| 1210 | fd_get_read_notifier_pollset, |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1211 | |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame] | 1212 | pollset_init, |
| 1213 | pollset_shutdown, |
| 1214 | pollset_destroy, |
| 1215 | pollset_work, |
| 1216 | pollset_kick, |
| 1217 | pollset_add_fd, |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1218 | |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame] | 1219 | pollset_set_create, |
| 1220 | pollset_set_destroy, |
| 1221 | pollset_set_add_pollset, |
| 1222 | pollset_set_del_pollset, |
| 1223 | pollset_set_add_pollset_set, |
| 1224 | pollset_set_del_pollset_set, |
| 1225 | pollset_set_add_fd, |
| 1226 | pollset_set_del_fd, |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1227 | |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame] | 1228 | shutdown_engine, |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1229 | }; |
| 1230 | |
| 1231 | /* It is possible that GLIBC has epoll but the underlying kernel doesn't. |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 1232 | * Create epoll_fd (epoll_set_init() takes care of that) to make sure epoll |
| 1233 | * support is available */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1234 | const grpc_event_engine_vtable* grpc_init_epoll1_linux(bool explicit_request) { |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1235 | if (!grpc_has_wakeup_fd()) { |
yang-g | 30101b0 | 2017-11-06 14:35:30 -0800 | [diff] [blame] | 1236 | gpr_log(GPR_ERROR, "Skipping epoll1 because of no wakeup fd."); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1237 | return nullptr; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 1240 | if (!epoll_set_init()) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1241 | return nullptr; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1244 | fd_global_init(); |
| 1245 | |
| 1246 | if (!GRPC_LOG_IF_ERROR("pollset_global_init", pollset_global_init())) { |
Craig Tiller | 4509c47 | 2017-04-27 19:05:13 +0000 | [diff] [blame] | 1247 | fd_global_shutdown(); |
Sree Kuchibhotla | 5efc913 | 2017-08-17 14:10:38 -0700 | [diff] [blame] | 1248 | epoll_set_shutdown(); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 1249 | return nullptr; |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | return &vtable; |
| 1253 | } |
| 1254 | |
| 1255 | #else /* defined(GRPC_LINUX_EPOLL) */ |
| 1256 | #if defined(GRPC_POSIX_SOCKET) |
Yash Tibrewal | 1cac223 | 2017-09-26 11:31:11 -0700 | [diff] [blame] | 1257 | #include "src/core/lib/iomgr/ev_epoll1_linux.h" |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1258 | /* If GRPC_LINUX_EPOLL is not defined, it means epoll is not available. Return |
| 1259 | * NULL */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1260 | const grpc_event_engine_vtable* grpc_init_epoll1_linux(bool explicit_request) { |
Craig Tiller | 9ddb315 | 2017-04-27 21:32:56 +0000 | [diff] [blame] | 1261 | return NULL; |
| 1262 | } |
Craig Tiller | c67cc99 | 2017-04-27 10:15:51 -0700 | [diff] [blame] | 1263 | #endif /* defined(GRPC_POSIX_SOCKET) */ |
| 1264 | #endif /* !defined(GRPC_LINUX_EPOLL) */ |