| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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/grpc.h> |
| 22 |
|
| 23 |
#include "src/core/lib/iomgr/port.h" |
| 24 |
|
| 25 |
#ifdef GRPC_POSIX_SOCKET_EV |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include <grpc/support/alloc.h> |
| 30 |
#include <grpc/support/log.h> |
| 31 |
#include <grpc/support/string_util.h> |
| 32 |
|
| 33 |
#include "src/core/lib/debug/trace.h" |
| 34 |
#include "src/core/lib/gpr/useful.h" |
| 35 |
#include "src/core/lib/gprpp/global_config.h" |
| 36 |
#include "src/core/lib/iomgr/ev_epoll1_linux.h" |
| 37 |
#include "src/core/lib/iomgr/ev_poll_posix.h" |
| 38 |
#include "src/core/lib/iomgr/ev_posix.h" |
| 39 |
#include "src/core/lib/iomgr/internal_errqueue.h" |
| 40 |
|
| 41 |
GPR_GLOBAL_CONFIG_DEFINE_STRING( |
| 42 |
grpc_poll_strategy, "all", |
| 43 |
"Declares which polling engines to try when starting gRPC. " |
| 44 |
"This is a comma-separated list of engines, which are tried in priority " |
| 45 |
"order first -> last.") |
| 46 |
|
| 47 |
grpc_core::DebugOnlyTraceFlag grpc_polling_trace( |
| 48 |
false, "polling"); /* Disabled by default */ |
| 49 |
|
| 50 |
/* Traces fd create/close operations */ |
| 51 |
grpc_core::DebugOnlyTraceFlag grpc_fd_trace(false, "fd_trace"); |
| 52 |
grpc_core::DebugOnlyTraceFlag grpc_trace_fd_refcount(false, "fd_refcount"); |
| 53 |
grpc_core::DebugOnlyTraceFlag grpc_polling_api_trace(false, "polling_api"); |
| 54 |
|
| 55 |
// Polling API trace only enabled in debug builds |
| 56 |
#ifndef NDEBUG |
| 57 |
#define GRPC_POLLING_API_TRACE(format, ...) \ |
| 58 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_polling_api_trace)) { \ |
| 59 |
gpr_log(GPR_INFO, "(polling-api) " format, __VA_ARGS__); \ |
| 60 |
} |
| 61 |
#else |
| 62 |
#define GRPC_POLLING_API_TRACE(...) |
| 63 |
#endif // NDEBUG |
| 64 |
|
| 65 |
/** Default poll() function - a pointer so that it can be overridden by some |
| 66 |
* tests */ |
| 67 |
#ifndef GPR_AIX |
| 68 |
grpc_poll_function_type grpc_poll_function = poll; |
| 69 |
#else |
| 70 |
int aix_poll(struct pollfd fds[], nfds_t nfds, int timeout) { |
| 71 |
return poll(fds, nfds, timeout); |
| 72 |
} |
| 73 |
grpc_poll_function_type grpc_poll_function = aix_poll; |
| 74 |
#endif // GPR_AIX |
| 75 |
|
| 76 |
grpc_wakeup_fd grpc_global_wakeup_fd; |
| 77 |
|
| 78 |
static const grpc_event_engine_vtable* g_event_engine = nullptr; |
| 79 |
static gpr_once g_choose_engine = GPR_ONCE_INIT; |
| 80 |
|
| 81 |
// The global array of event-engine factories. Each entry is a pair with a name |
| 82 |
// and an event-engine generator function (nullptr if there is no generator |
| 83 |
// registered for this name). The middle entries are the engines predefined by |
| 84 |
// open-source gRPC. The head entries represent an opportunity for specific |
| 85 |
// high-priority custom pollers to be added by the initializer plugins of |
| 86 |
// custom-built gRPC libraries. The tail entries represent the same, but for |
| 87 |
// low-priority custom pollers. The actual poller selected is either the first |
| 88 |
// available one in the list if no specific poller is requested, or the first |
| 89 |
// specific poller that is requested by name in the GRPC_POLL_STRATEGY |
| 90 |
// environment variable if that variable is set (which should be a |
| 91 |
// comma-separated list of one or more event engine names) |
| 92 |
static const grpc_event_engine_vtable* g_vtables[] = { |
| 93 |
nullptr, |
| 94 |
nullptr, |
| 95 |
nullptr, |
| 96 |
nullptr, |
| 97 |
&grpc_ev_epoll1_posix, |
| 98 |
&grpc_ev_poll_posix, |
| 99 |
&grpc_ev_none_posix, |
| 100 |
nullptr, |
| 101 |
nullptr, |
| 102 |
nullptr, |
| 103 |
nullptr, |
| 104 |
}; |
| 105 |
|
| 106 |
static void add(const char* beg, const char* end, char*** ss, size_t* ns) { |
| 107 |
size_t n = *ns; |
| 108 |
size_t np = n + 1; |
| 109 |
char* s; |
| 110 |
size_t len; |
| 111 |
GPR_ASSERT(end >= beg); |
| 112 |
len = static_cast<size_t>(end - beg); |
| 113 |
s = static_cast<char*>(gpr_malloc(len + 1)); |
| 114 |
memcpy(s, beg, len); |
| 115 |
s[len] = 0; |
| 116 |
*ss = static_cast<char**>(gpr_realloc(*ss, sizeof(char**) * np)); |
| 117 |
(*ss)[n] = s; |
| 118 |
*ns = np; |
| 119 |
} |
| 120 |
|
| 121 |
static void split(const char* s, char*** ss, size_t* ns) { |
| 122 |
const char* c = strchr(s, ','); |
| 123 |
if (c == nullptr) { |
| 124 |
add(s, s + strlen(s), ss, ns); |
| 125 |
} else { |
| 126 |
add(s, c, ss, ns); |
| 127 |
split(c + 1, ss, ns); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
static bool is(const char* want, const char* have) { |
| 132 |
return 0 == strcmp(want, "all") || 0 == strcmp(want, have); |
| 133 |
} |
| 134 |
|
| 135 |
static void try_engine(const char* engine) { |
| 136 |
for (size_t i = 0; i < GPR_ARRAY_SIZE(g_vtables); i++) { |
| 137 |
if (g_vtables[i] != nullptr && is(engine, g_vtables[i]->name) && |
| 138 |
g_vtables[i]->check_engine_available( |
| 139 |
0 == strcmp(engine, g_vtables[i]->name))) { |
| 140 |
g_event_engine = g_vtables[i]; |
| 141 |
gpr_log(GPR_DEBUG, "Using polling engine: %s", g_event_engine->name); |
| 142 |
return; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/* Call this before calling grpc_event_engine_init() */ |
| 148 |
void grpc_register_event_engine_factory(const grpc_event_engine_vtable* vtable, |
| 149 |
bool add_at_head) { |
| 150 |
const grpc_event_engine_vtable** first_null = nullptr; |
| 151 |
const grpc_event_engine_vtable** last_null = nullptr; |
| 152 |
|
| 153 |
// Overwrite an existing registration if already registered |
| 154 |
for (size_t i = 0; i < GPR_ARRAY_SIZE(g_vtables); i++) { |
| 155 |
if (g_vtables[i] == nullptr) { |
| 156 |
if (first_null == nullptr) first_null = &g_vtables[i]; |
| 157 |
last_null = &g_vtables[i]; |
| 158 |
} else if (0 == strcmp(g_vtables[i]->name, vtable->name)) { |
| 159 |
g_vtables[i] = vtable; |
| 160 |
return; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
*(add_at_head ? first_null : last_null) = vtable; |
| 165 |
} |
| 166 |
|
| 167 |
/*If grpc_event_engine_init() has been called, returns the poll_strategy_name. |
| 168 |
* Otherwise, returns nullptr. */ |
| 169 |
const char* grpc_get_poll_strategy_name() { return g_event_engine->name; } |
| 170 |
|
| 171 |
void grpc_event_engine_init(void) { |
| 172 |
gpr_once_init(&g_choose_engine, []() { |
| 173 |
grpc_core::UniquePtr<char> value = |
| 174 |
GPR_GLOBAL_CONFIG_GET(grpc_poll_strategy); |
| 175 |
|
| 176 |
char** strings = nullptr; |
| 177 |
size_t nstrings = 0; |
| 178 |
split(value.get(), &strings, &nstrings); |
| 179 |
|
| 180 |
for (size_t i = 0; g_event_engine == nullptr && i < nstrings; i++) { |
| 181 |
try_engine(strings[i]); |
| 182 |
} |
| 183 |
|
| 184 |
for (size_t i = 0; i < nstrings; i++) { |
| 185 |
gpr_free(strings[i]); |
| 186 |
} |
| 187 |
gpr_free(strings); |
| 188 |
|
| 189 |
if (g_event_engine == nullptr) { |
| 190 |
gpr_log(GPR_ERROR, "No event engine could be initialized from %s", |
| 191 |
value.get()); |
| 192 |
abort(); |
| 193 |
} |
| 194 |
}); |
| 195 |
g_event_engine->init_engine(); |
| 196 |
} |
| 197 |
|
| 198 |
void grpc_event_engine_shutdown(void) { g_event_engine->shutdown_engine(); } |
| 199 |
|
| 200 |
bool grpc_event_engine_can_track_errors(void) { |
| 201 |
/* Only track errors if platform supports errqueue. */ |
| 202 |
return grpc_core::KernelSupportsErrqueue() && g_event_engine->can_track_err; |
| 203 |
} |
| 204 |
|
| 205 |
bool grpc_event_engine_run_in_background(void) { |
| 206 |
// g_event_engine is nullptr when using a custom iomgr. |
| 207 |
return g_event_engine != nullptr && g_event_engine->run_in_background; |
| 208 |
} |
| 209 |
|
| 210 |
grpc_fd* grpc_fd_create(int fd, const char* name, bool track_err) { |
| 211 |
GRPC_POLLING_API_TRACE("fd_create(%d, %s, %d)", fd, name, track_err); |
| 212 |
GRPC_FD_TRACE("fd_create(%d, %s, %d)", fd, name, track_err); |
| 213 |
return g_event_engine->fd_create( |
| 214 |
fd, name, track_err && grpc_event_engine_can_track_errors()); |
| 215 |
} |
| 216 |
|
| 217 |
int grpc_fd_wrapped_fd(grpc_fd* fd) { |
| 218 |
return g_event_engine->fd_wrapped_fd(fd); |
| 219 |
} |
| 220 |
|
| 221 |
void grpc_fd_orphan(grpc_fd* fd, grpc_closure* on_done, int* release_fd, |
| 222 |
const char* reason) { |
| 223 |
GRPC_POLLING_API_TRACE("fd_orphan(%d, %p, %p, %s)", grpc_fd_wrapped_fd(fd), |
| 224 |
on_done, release_fd, reason); |
| 225 |
GRPC_FD_TRACE("grpc_fd_orphan, fd:%d closed", grpc_fd_wrapped_fd(fd)); |
| 226 |
|
| 227 |
g_event_engine->fd_orphan(fd, on_done, release_fd, reason); |
| 228 |
} |
| 229 |
|
| 230 |
void grpc_fd_shutdown(grpc_fd* fd, grpc_error_handle why) { |
| 231 |
GRPC_POLLING_API_TRACE("fd_shutdown(%d)", grpc_fd_wrapped_fd(fd)); |
| 232 |
GRPC_FD_TRACE("fd_shutdown(%d)", grpc_fd_wrapped_fd(fd)); |
| 233 |
g_event_engine->fd_shutdown(fd, why); |
| 234 |
} |
| 235 |
|
| 236 |
bool grpc_fd_is_shutdown(grpc_fd* fd) { |
| 237 |
return g_event_engine->fd_is_shutdown(fd); |
| 238 |
} |
| 239 |
|
| 240 |
void grpc_fd_notify_on_read(grpc_fd* fd, grpc_closure* closure) { |
| 241 |
g_event_engine->fd_notify_on_read(fd, closure); |
| 242 |
} |
| 243 |
|
| 244 |
void grpc_fd_notify_on_write(grpc_fd* fd, grpc_closure* closure) { |
| 245 |
g_event_engine->fd_notify_on_write(fd, closure); |
| 246 |
} |
| 247 |
|
| 248 |
void grpc_fd_notify_on_error(grpc_fd* fd, grpc_closure* closure) { |
| 249 |
g_event_engine->fd_notify_on_error(fd, closure); |
| 250 |
} |
| 251 |
|
| 252 |
void grpc_fd_set_readable(grpc_fd* fd) { g_event_engine->fd_set_readable(fd); } |
| 253 |
|
| 254 |
void grpc_fd_set_writable(grpc_fd* fd) { g_event_engine->fd_set_writable(fd); } |
| 255 |
|
| 256 |
void grpc_fd_set_error(grpc_fd* fd) { g_event_engine->fd_set_error(fd); } |
| 257 |
|
| 258 |
static size_t pollset_size(void) { return g_event_engine->pollset_size; } |
| 259 |
|
| 260 |
static void pollset_init(grpc_pollset* pollset, gpr_mu** mu) { |
| 261 |
GRPC_POLLING_API_TRACE("pollset_init(%p)", pollset); |
| 262 |
g_event_engine->pollset_init(pollset, mu); |
| 263 |
} |
| 264 |
|
| 265 |
static void pollset_shutdown(grpc_pollset* pollset, grpc_closure* closure) { |
| 266 |
GRPC_POLLING_API_TRACE("pollset_shutdown(%p)", pollset); |
| 267 |
g_event_engine->pollset_shutdown(pollset, closure); |
| 268 |
} |
| 269 |
|
| 270 |
static void pollset_destroy(grpc_pollset* pollset) { |
| 271 |
GRPC_POLLING_API_TRACE("pollset_destroy(%p)", pollset); |
| 272 |
g_event_engine->pollset_destroy(pollset); |
| 273 |
} |
| 274 |
|
| 275 |
static grpc_error_handle pollset_work(grpc_pollset* pollset, |
| 276 |
grpc_pollset_worker** worker, |
| 277 |
grpc_core::Timestamp deadline) { |
| 278 |
GRPC_POLLING_API_TRACE("pollset_work(%p, %" PRId64 ") begin", pollset, |
| 279 |
deadline.milliseconds_after_process_epoch()); |
| 280 |
grpc_error_handle err = |
| 281 |
g_event_engine->pollset_work(pollset, worker, deadline); |
| 282 |
GRPC_POLLING_API_TRACE("pollset_work(%p, %" PRId64 ") end", pollset, |
| 283 |
deadline.milliseconds_after_process_epoch()); |
| 284 |
return err; |
| 285 |
} |
| 286 |
|
| 287 |
static grpc_error_handle pollset_kick(grpc_pollset* pollset, |
| 288 |
grpc_pollset_worker* specific_worker) { |
| 289 |
GRPC_POLLING_API_TRACE("pollset_kick(%p, %p)", pollset, specific_worker); |
| 290 |
return g_event_engine->pollset_kick(pollset, specific_worker); |
| 291 |
} |
| 292 |
|
| 293 |
void grpc_pollset_add_fd(grpc_pollset* pollset, struct grpc_fd* fd) { |
| 294 |
GRPC_POLLING_API_TRACE("pollset_add_fd(%p, %d)", pollset, |
| 295 |
grpc_fd_wrapped_fd(fd)); |
| 296 |
g_event_engine->pollset_add_fd(pollset, fd); |
| 297 |
} |
| 298 |
|
| 299 |
void pollset_global_init() {} |
| 300 |
void pollset_global_shutdown() {} |
| 301 |
|
| 302 |
grpc_pollset_vtable grpc_posix_pollset_vtable = { |
| 303 |
pollset_global_init, pollset_global_shutdown, |
| 304 |
pollset_init, pollset_shutdown, |
| 305 |
pollset_destroy, pollset_work, |
| 306 |
pollset_kick, pollset_size}; |
| 307 |
|
| 308 |
static grpc_pollset_set* pollset_set_create(void) { |
| 309 |
grpc_pollset_set* pss = g_event_engine->pollset_set_create(); |
| 310 |
GRPC_POLLING_API_TRACE("pollset_set_create(%p)", pss); |
| 311 |
return pss; |
| 312 |
} |
| 313 |
|
| 314 |
static void pollset_set_destroy(grpc_pollset_set* pollset_set) { |
| 315 |
GRPC_POLLING_API_TRACE("pollset_set_destroy(%p)", pollset_set); |
| 316 |
g_event_engine->pollset_set_destroy(pollset_set); |
| 317 |
} |
| 318 |
|
| 319 |
static void pollset_set_add_pollset(grpc_pollset_set* pollset_set, |
| 320 |
grpc_pollset* pollset) { |
| 321 |
GRPC_POLLING_API_TRACE("pollset_set_add_pollset(%p, %p)", pollset_set, |
| 322 |
pollset); |
| 323 |
g_event_engine->pollset_set_add_pollset(pollset_set, pollset); |
| 324 |
} |
| 325 |
|
| 326 |
static void pollset_set_del_pollset(grpc_pollset_set* pollset_set, |
| 327 |
grpc_pollset* pollset) { |
| 328 |
GRPC_POLLING_API_TRACE("pollset_set_del_pollset(%p, %p)", pollset_set, |
| 329 |
pollset); |
| 330 |
g_event_engine->pollset_set_del_pollset(pollset_set, pollset); |
| 331 |
} |
| 332 |
|
| 333 |
static void pollset_set_add_pollset_set(grpc_pollset_set* bag, |
| 334 |
grpc_pollset_set* item) { |
| 335 |
GRPC_POLLING_API_TRACE("pollset_set_add_pollset_set(%p, %p)", bag, item); |
| 336 |
g_event_engine->pollset_set_add_pollset_set(bag, item); |
| 337 |
} |
| 338 |
|
| 339 |
static void pollset_set_del_pollset_set(grpc_pollset_set* bag, |
| 340 |
grpc_pollset_set* item) { |
| 341 |
GRPC_POLLING_API_TRACE("pollset_set_del_pollset_set(%p, %p)", bag, item); |
| 342 |
g_event_engine->pollset_set_del_pollset_set(bag, item); |
| 343 |
} |
| 344 |
|
| 345 |
grpc_pollset_set_vtable grpc_posix_pollset_set_vtable = { |
| 346 |
pollset_set_create, pollset_set_destroy, |
| 347 |
pollset_set_add_pollset, pollset_set_del_pollset, |
| 348 |
pollset_set_add_pollset_set, pollset_set_del_pollset_set}; |
| 349 |
|
| 350 |
void grpc_pollset_set_add_fd(grpc_pollset_set* pollset_set, grpc_fd* fd) { |
| 351 |
GRPC_POLLING_API_TRACE("pollset_set_add_fd(%p, %d)", pollset_set, |
| 352 |
grpc_fd_wrapped_fd(fd)); |
| 353 |
g_event_engine->pollset_set_add_fd(pollset_set, fd); |
| 354 |
} |
| 355 |
|
| 356 |
void grpc_pollset_set_del_fd(grpc_pollset_set* pollset_set, grpc_fd* fd) { |
| 357 |
GRPC_POLLING_API_TRACE("pollset_set_del_fd(%p, %d)", pollset_set, |
| 358 |
grpc_fd_wrapped_fd(fd)); |
| 359 |
g_event_engine->pollset_set_del_fd(pollset_set, fd); |
| 360 |
} |
| 361 |
|
| 362 |
bool grpc_is_any_background_poller_thread(void) { |
| 363 |
return g_event_engine->is_any_background_poller_thread(); |
| 364 |
} |
| 365 |
|
| 366 |
bool grpc_add_closure_to_background_poller(grpc_closure* closure, |
| 367 |
grpc_error_handle error) { |
| 368 |
return g_event_engine->add_closure_to_background_poller(closure, error); |
| 369 |
} |
| 370 |
|
| 371 |
void grpc_shutdown_background_closure(void) { |
| 372 |
g_event_engine->shutdown_background_closure(); |
| 373 |
} |
| 374 |
|
| 375 |
#endif // GRPC_POSIX_SOCKET_EV |