• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/lib/iomgr/port.h"
22 
23 #ifdef GRPC_POSIX_SOCKET_EV
24 
25 #include "src/core/lib/iomgr/ev_posix.h"
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_epollex_linux.h"
38 #include "src/core/lib/iomgr/ev_poll_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
aix_poll(struct pollfd fds[],nfds_t nfds,int timeout)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 const char* g_poll_strategy_name = nullptr;
80 
81 typedef const grpc_event_engine_vtable* (*event_engine_factory_fn)(
82     bool explicit_request);
83 
84 struct event_engine_factory {
85   const char* name;
86   event_engine_factory_fn factory;
87 };
88 namespace {
89 
90 grpc_poll_function_type real_poll_function;
91 
dummy_poll(struct pollfd fds[],nfds_t nfds,int timeout)92 int dummy_poll(struct pollfd fds[], nfds_t nfds, int timeout) {
93   if (timeout == 0) {
94     return real_poll_function(fds, nfds, 0);
95   } else {
96     gpr_log(GPR_ERROR, "Attempted a blocking poll when declared non-polling.");
97     GPR_ASSERT(false);
98     return -1;
99   }
100 }
101 
init_non_polling(bool explicit_request)102 const grpc_event_engine_vtable* init_non_polling(bool explicit_request) {
103   if (!explicit_request) {
104     return nullptr;
105   }
106   // return the simplest engine as a dummy but also override the poller
107   auto ret = grpc_init_poll_posix(explicit_request);
108   real_poll_function = grpc_poll_function;
109   grpc_poll_function = dummy_poll;
110 
111   return ret;
112 }
113 }  // namespace
114 
115 #define ENGINE_HEAD_CUSTOM "head_custom"
116 #define ENGINE_TAIL_CUSTOM "tail_custom"
117 
118 // The global array of event-engine factories. Each entry is a pair with a name
119 // and an event-engine generator function (nullptr if there is no generator
120 // registered for this name). The middle entries are the engines predefined by
121 // open-source gRPC. The head entries represent an opportunity for specific
122 // high-priority custom pollers to be added by the initializer plugins of
123 // custom-built gRPC libraries. The tail entries represent the same, but for
124 // low-priority custom pollers. The actual poller selected is either the first
125 // available one in the list if no specific poller is requested, or the first
126 // specific poller that is requested by name in the GRPC_POLL_STRATEGY
127 // environment variable if that variable is set (which should be a
128 // comma-separated list of one or more event engine names)
129 static event_engine_factory g_factories[] = {
130     {ENGINE_HEAD_CUSTOM, nullptr},        {ENGINE_HEAD_CUSTOM, nullptr},
131     {ENGINE_HEAD_CUSTOM, nullptr},        {ENGINE_HEAD_CUSTOM, nullptr},
132     {"epollex", grpc_init_epollex_linux}, {"epoll1", grpc_init_epoll1_linux},
133     {"poll", grpc_init_poll_posix},       {"none", init_non_polling},
134     {ENGINE_TAIL_CUSTOM, nullptr},        {ENGINE_TAIL_CUSTOM, nullptr},
135     {ENGINE_TAIL_CUSTOM, nullptr},        {ENGINE_TAIL_CUSTOM, nullptr},
136 };
137 
add(const char * beg,const char * end,char *** ss,size_t * ns)138 static void add(const char* beg, const char* end, char*** ss, size_t* ns) {
139   size_t n = *ns;
140   size_t np = n + 1;
141   char* s;
142   size_t len;
143   GPR_ASSERT(end >= beg);
144   len = static_cast<size_t>(end - beg);
145   s = static_cast<char*>(gpr_malloc(len + 1));
146   memcpy(s, beg, len);
147   s[len] = 0;
148   *ss = static_cast<char**>(gpr_realloc(*ss, sizeof(char**) * np));
149   (*ss)[n] = s;
150   *ns = np;
151 }
152 
split(const char * s,char *** ss,size_t * ns)153 static void split(const char* s, char*** ss, size_t* ns) {
154   const char* c = strchr(s, ',');
155   if (c == nullptr) {
156     add(s, s + strlen(s), ss, ns);
157   } else {
158     add(s, c, ss, ns);
159     split(c + 1, ss, ns);
160   }
161 }
162 
is(const char * want,const char * have)163 static bool is(const char* want, const char* have) {
164   return 0 == strcmp(want, "all") || 0 == strcmp(want, have);
165 }
166 
try_engine(const char * engine)167 static void try_engine(const char* engine) {
168   for (size_t i = 0; i < GPR_ARRAY_SIZE(g_factories); i++) {
169     if (g_factories[i].factory != nullptr && is(engine, g_factories[i].name)) {
170       if ((g_event_engine = g_factories[i].factory(
171                0 == strcmp(engine, g_factories[i].name)))) {
172         g_poll_strategy_name = g_factories[i].name;
173         gpr_log(GPR_DEBUG, "Using polling engine: %s", g_factories[i].name);
174         return;
175       }
176     }
177   }
178 }
179 
180 /* Call this before calling grpc_event_engine_init() */
grpc_register_event_engine_factory(const char * name,event_engine_factory_fn factory,bool add_at_head)181 void grpc_register_event_engine_factory(const char* name,
182                                         event_engine_factory_fn factory,
183                                         bool add_at_head) {
184   const char* custom_match =
185       add_at_head ? ENGINE_HEAD_CUSTOM : ENGINE_TAIL_CUSTOM;
186 
187   // Overwrite an existing registration if already registered
188   for (size_t i = 0; i < GPR_ARRAY_SIZE(g_factories); i++) {
189     if (0 == strcmp(name, g_factories[i].name)) {
190       g_factories[i].factory = factory;
191       return;
192     }
193   }
194 
195   // Otherwise fill in an available custom slot
196   for (size_t i = 0; i < GPR_ARRAY_SIZE(g_factories); i++) {
197     if (0 == strcmp(g_factories[i].name, custom_match)) {
198       g_factories[i].name = name;
199       g_factories[i].factory = factory;
200       return;
201     }
202   }
203 
204   // Otherwise fail
205   GPR_ASSERT(false);
206 }
207 
208 /*If grpc_event_engine_init() has been called, returns the poll_strategy_name.
209  * Otherwise, returns nullptr. */
grpc_get_poll_strategy_name()210 const char* grpc_get_poll_strategy_name() { return g_poll_strategy_name; }
211 
grpc_event_engine_init(void)212 void grpc_event_engine_init(void) {
213   grpc_core::UniquePtr<char> value = GPR_GLOBAL_CONFIG_GET(grpc_poll_strategy);
214 
215   char** strings = nullptr;
216   size_t nstrings = 0;
217   split(value.get(), &strings, &nstrings);
218 
219   for (size_t i = 0; g_event_engine == nullptr && i < nstrings; i++) {
220     try_engine(strings[i]);
221   }
222 
223   for (size_t i = 0; i < nstrings; i++) {
224     gpr_free(strings[i]);
225   }
226   gpr_free(strings);
227 
228   if (g_event_engine == nullptr) {
229     gpr_log(GPR_ERROR, "No event engine could be initialized from %s",
230             value.get());
231     abort();
232   }
233 }
234 
grpc_event_engine_shutdown(void)235 void grpc_event_engine_shutdown(void) {
236   g_event_engine->shutdown_engine();
237   g_event_engine = nullptr;
238 }
239 
grpc_event_engine_can_track_errors(void)240 bool grpc_event_engine_can_track_errors(void) {
241   /* Only track errors if platform supports errqueue. */
242   if (grpc_core::kernel_supports_errqueue()) {
243     return g_event_engine->can_track_err;
244   }
245   return false;
246 }
247 
grpc_event_engine_run_in_background(void)248 bool grpc_event_engine_run_in_background(void) {
249   // g_event_engine is nullptr when using a custom iomgr.
250   return g_event_engine != nullptr && g_event_engine->run_in_background;
251 }
252 
grpc_fd_create(int fd,const char * name,bool track_err)253 grpc_fd* grpc_fd_create(int fd, const char* name, bool track_err) {
254   GRPC_POLLING_API_TRACE("fd_create(%d, %s, %d)", fd, name, track_err);
255   GRPC_FD_TRACE("fd_create(%d, %s, %d)", fd, name, track_err);
256   return g_event_engine->fd_create(
257       fd, name, track_err && grpc_event_engine_can_track_errors());
258 }
259 
grpc_fd_wrapped_fd(grpc_fd * fd)260 int grpc_fd_wrapped_fd(grpc_fd* fd) {
261   return g_event_engine->fd_wrapped_fd(fd);
262 }
263 
grpc_fd_orphan(grpc_fd * fd,grpc_closure * on_done,int * release_fd,const char * reason)264 void grpc_fd_orphan(grpc_fd* fd, grpc_closure* on_done, int* release_fd,
265                     const char* reason) {
266   GRPC_POLLING_API_TRACE("fd_orphan(%d, %p, %p, %s)", grpc_fd_wrapped_fd(fd),
267                          on_done, release_fd, reason);
268   GRPC_FD_TRACE("grpc_fd_orphan, fd:%d closed", grpc_fd_wrapped_fd(fd));
269 
270   g_event_engine->fd_orphan(fd, on_done, release_fd, reason);
271 }
272 
grpc_fd_shutdown(grpc_fd * fd,grpc_error * why)273 void grpc_fd_shutdown(grpc_fd* fd, grpc_error* why) {
274   GRPC_POLLING_API_TRACE("fd_shutdown(%d)", grpc_fd_wrapped_fd(fd));
275   GRPC_FD_TRACE("fd_shutdown(%d)", grpc_fd_wrapped_fd(fd));
276   g_event_engine->fd_shutdown(fd, why);
277 }
278 
grpc_fd_is_shutdown(grpc_fd * fd)279 bool grpc_fd_is_shutdown(grpc_fd* fd) {
280   return g_event_engine->fd_is_shutdown(fd);
281 }
282 
grpc_fd_notify_on_read(grpc_fd * fd,grpc_closure * closure)283 void grpc_fd_notify_on_read(grpc_fd* fd, grpc_closure* closure) {
284   g_event_engine->fd_notify_on_read(fd, closure);
285 }
286 
grpc_fd_notify_on_write(grpc_fd * fd,grpc_closure * closure)287 void grpc_fd_notify_on_write(grpc_fd* fd, grpc_closure* closure) {
288   g_event_engine->fd_notify_on_write(fd, closure);
289 }
290 
grpc_fd_notify_on_error(grpc_fd * fd,grpc_closure * closure)291 void grpc_fd_notify_on_error(grpc_fd* fd, grpc_closure* closure) {
292   g_event_engine->fd_notify_on_error(fd, closure);
293 }
294 
grpc_fd_set_readable(grpc_fd * fd)295 void grpc_fd_set_readable(grpc_fd* fd) { g_event_engine->fd_set_readable(fd); }
296 
grpc_fd_set_writable(grpc_fd * fd)297 void grpc_fd_set_writable(grpc_fd* fd) { g_event_engine->fd_set_writable(fd); }
298 
grpc_fd_set_error(grpc_fd * fd)299 void grpc_fd_set_error(grpc_fd* fd) { g_event_engine->fd_set_error(fd); }
300 
pollset_size(void)301 static size_t pollset_size(void) { return g_event_engine->pollset_size; }
302 
pollset_init(grpc_pollset * pollset,gpr_mu ** mu)303 static void pollset_init(grpc_pollset* pollset, gpr_mu** mu) {
304   GRPC_POLLING_API_TRACE("pollset_init(%p)", pollset);
305   g_event_engine->pollset_init(pollset, mu);
306 }
307 
pollset_shutdown(grpc_pollset * pollset,grpc_closure * closure)308 static void pollset_shutdown(grpc_pollset* pollset, grpc_closure* closure) {
309   GRPC_POLLING_API_TRACE("pollset_shutdown(%p)", pollset);
310   g_event_engine->pollset_shutdown(pollset, closure);
311 }
312 
pollset_destroy(grpc_pollset * pollset)313 static void pollset_destroy(grpc_pollset* pollset) {
314   GRPC_POLLING_API_TRACE("pollset_destroy(%p)", pollset);
315   g_event_engine->pollset_destroy(pollset);
316 }
317 
pollset_work(grpc_pollset * pollset,grpc_pollset_worker ** worker,grpc_millis deadline)318 static grpc_error* pollset_work(grpc_pollset* pollset,
319                                 grpc_pollset_worker** worker,
320                                 grpc_millis deadline) {
321   GRPC_POLLING_API_TRACE("pollset_work(%p, %" PRId64 ") begin", pollset,
322                          deadline);
323   grpc_error* err = g_event_engine->pollset_work(pollset, worker, deadline);
324   GRPC_POLLING_API_TRACE("pollset_work(%p, %" PRId64 ") end", pollset,
325                          deadline);
326   return err;
327 }
328 
pollset_kick(grpc_pollset * pollset,grpc_pollset_worker * specific_worker)329 static grpc_error* pollset_kick(grpc_pollset* pollset,
330                                 grpc_pollset_worker* specific_worker) {
331   GRPC_POLLING_API_TRACE("pollset_kick(%p, %p)", pollset, specific_worker);
332   return g_event_engine->pollset_kick(pollset, specific_worker);
333 }
334 
grpc_pollset_add_fd(grpc_pollset * pollset,struct grpc_fd * fd)335 void grpc_pollset_add_fd(grpc_pollset* pollset, struct grpc_fd* fd) {
336   GRPC_POLLING_API_TRACE("pollset_add_fd(%p, %d)", pollset,
337                          grpc_fd_wrapped_fd(fd));
338   g_event_engine->pollset_add_fd(pollset, fd);
339 }
340 
pollset_global_init()341 void pollset_global_init() {}
pollset_global_shutdown()342 void pollset_global_shutdown() {}
343 
344 grpc_pollset_vtable grpc_posix_pollset_vtable = {
345     pollset_global_init, pollset_global_shutdown,
346     pollset_init,        pollset_shutdown,
347     pollset_destroy,     pollset_work,
348     pollset_kick,        pollset_size};
349 
pollset_set_create(void)350 static grpc_pollset_set* pollset_set_create(void) {
351   grpc_pollset_set* pss = g_event_engine->pollset_set_create();
352   GRPC_POLLING_API_TRACE("pollset_set_create(%p)", pss);
353   return pss;
354 }
355 
pollset_set_destroy(grpc_pollset_set * pollset_set)356 static void pollset_set_destroy(grpc_pollset_set* pollset_set) {
357   GRPC_POLLING_API_TRACE("pollset_set_destroy(%p)", pollset_set);
358   g_event_engine->pollset_set_destroy(pollset_set);
359 }
360 
pollset_set_add_pollset(grpc_pollset_set * pollset_set,grpc_pollset * pollset)361 static void pollset_set_add_pollset(grpc_pollset_set* pollset_set,
362                                     grpc_pollset* pollset) {
363   GRPC_POLLING_API_TRACE("pollset_set_add_pollset(%p, %p)", pollset_set,
364                          pollset);
365   g_event_engine->pollset_set_add_pollset(pollset_set, pollset);
366 }
367 
pollset_set_del_pollset(grpc_pollset_set * pollset_set,grpc_pollset * pollset)368 static void pollset_set_del_pollset(grpc_pollset_set* pollset_set,
369                                     grpc_pollset* pollset) {
370   GRPC_POLLING_API_TRACE("pollset_set_del_pollset(%p, %p)", pollset_set,
371                          pollset);
372   g_event_engine->pollset_set_del_pollset(pollset_set, pollset);
373 }
374 
pollset_set_add_pollset_set(grpc_pollset_set * bag,grpc_pollset_set * item)375 static void pollset_set_add_pollset_set(grpc_pollset_set* bag,
376                                         grpc_pollset_set* item) {
377   GRPC_POLLING_API_TRACE("pollset_set_add_pollset_set(%p, %p)", bag, item);
378   g_event_engine->pollset_set_add_pollset_set(bag, item);
379 }
380 
pollset_set_del_pollset_set(grpc_pollset_set * bag,grpc_pollset_set * item)381 static void pollset_set_del_pollset_set(grpc_pollset_set* bag,
382                                         grpc_pollset_set* item) {
383   GRPC_POLLING_API_TRACE("pollset_set_del_pollset_set(%p, %p)", bag, item);
384   g_event_engine->pollset_set_del_pollset_set(bag, item);
385 }
386 
387 grpc_pollset_set_vtable grpc_posix_pollset_set_vtable = {
388     pollset_set_create,          pollset_set_destroy,
389     pollset_set_add_pollset,     pollset_set_del_pollset,
390     pollset_set_add_pollset_set, pollset_set_del_pollset_set};
391 
grpc_pollset_set_add_fd(grpc_pollset_set * pollset_set,grpc_fd * fd)392 void grpc_pollset_set_add_fd(grpc_pollset_set* pollset_set, grpc_fd* fd) {
393   GRPC_POLLING_API_TRACE("pollset_set_add_fd(%p, %d)", pollset_set,
394                          grpc_fd_wrapped_fd(fd));
395   g_event_engine->pollset_set_add_fd(pollset_set, fd);
396 }
397 
grpc_pollset_set_del_fd(grpc_pollset_set * pollset_set,grpc_fd * fd)398 void grpc_pollset_set_del_fd(grpc_pollset_set* pollset_set, grpc_fd* fd) {
399   GRPC_POLLING_API_TRACE("pollset_set_del_fd(%p, %d)", pollset_set,
400                          grpc_fd_wrapped_fd(fd));
401   g_event_engine->pollset_set_del_fd(pollset_set, fd);
402 }
403 
grpc_is_any_background_poller_thread(void)404 bool grpc_is_any_background_poller_thread(void) {
405   return g_event_engine->is_any_background_poller_thread();
406 }
407 
grpc_add_closure_to_background_poller(grpc_closure * closure,grpc_error * error)408 bool grpc_add_closure_to_background_poller(grpc_closure* closure,
409                                            grpc_error* error) {
410   return g_event_engine->add_closure_to_background_poller(closure, error);
411 }
412 
grpc_shutdown_background_closure(void)413 void grpc_shutdown_background_closure(void) {
414   g_event_engine->shutdown_background_closure();
415 }
416 
417 #endif  // GRPC_POSIX_SOCKET_EV
418