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