Skip to content
Snippets Groups Projects
Commit c7be7c68 authored by Sree Kuchibhotla's avatar Sree Kuchibhotla
Browse files

Add an API at the core level to disable signals or use a different

signal number
parent 877dc0cc
No related branches found
No related tags found
No related merge requests found
...@@ -63,6 +63,14 @@ GRPCAPI void grpc_server_add_insecure_channel_from_fd(grpc_server *server, ...@@ -63,6 +63,14 @@ GRPCAPI void grpc_server_add_insecure_channel_from_fd(grpc_server *server,
grpc_completion_queue *cq, grpc_completion_queue *cq,
int fd); int fd);
/** GRPC Core POSIX library may internally use signals to optimize some work.
The library uses (SIGRTMIN + 2) signal by default. Use this API to instruct
the library to use a different signal i.e 'signum' instead.
Note:
- To prevent GRPC library from using any signals, pass a 'signum' of -1
- This API is optional but if called, it MUST be called before grpc_init() */
GRPCAPI void grpc_use_signal(int signum);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
* *
*/ */
#include <grpc/grpc_posix.h>
#include <grpc/support/port_platform.h> #include <grpc/support/port_platform.h>
#ifdef GPR_LINUX_EPOLL #ifdef GPR_LINUX_EPOLL
...@@ -58,9 +59,26 @@ ...@@ -58,9 +59,26 @@
#include "src/core/lib/profiling/timers.h" #include "src/core/lib/profiling/timers.h"
#include "src/core/lib/support/block_annotate.h" #include "src/core/lib/support/block_annotate.h"
struct polling_island; static int grpc_wakeup_signal = -1;
static bool is_grpc_wakeup_signal_initialized = false;
/* Implements the function defined in grpc_posix.h. This function might be
* called before even calling grpc_init() to set either a different signal to
* use. If signum == -1, then the use of signals is disabled */
void grpc_use_signal(int signum) {
grpc_wakeup_signal = signum;
is_grpc_wakeup_signal_initialized = true;
static int grpc_poller_kick_signum; if (grpc_wakeup_signal < 0) {
gpr_log(GPR_INFO,
"Use of signals is disabled. Epoll engine will not be used");
} else {
gpr_log(GPR_INFO, "epoll engine will be using signal: %d",
grpc_wakeup_signal);
}
}
struct polling_island;
/******************************************************************************* /*******************************************************************************
* Fd Declarations * Fd Declarations
...@@ -854,10 +872,7 @@ static void sig_handler(int sig_num) { ...@@ -854,10 +872,7 @@ static void sig_handler(int sig_num) {
#endif #endif
} }
static void poller_kick_init() { static void poller_kick_init() { signal(grpc_wakeup_signal, sig_handler); }
grpc_poller_kick_signum = SIGRTMIN + 2;
signal(grpc_poller_kick_signum, sig_handler);
}
/* Global state management */ /* Global state management */
static void pollset_global_init(void) { static void pollset_global_init(void) {
...@@ -874,7 +889,7 @@ static void pollset_global_shutdown(void) { ...@@ -874,7 +889,7 @@ static void pollset_global_shutdown(void) {
} }
static void pollset_worker_kick(grpc_pollset_worker *worker) { static void pollset_worker_kick(grpc_pollset_worker *worker) {
pthread_kill(worker->pt_id, grpc_poller_kick_signum); pthread_kill(worker->pt_id, grpc_wakeup_signal);
} }
/* Return 1 if the pollset has active threads in pollset_work (pollset must /* Return 1 if the pollset has active threads in pollset_work (pollset must
...@@ -1214,9 +1229,9 @@ static void pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, ...@@ -1214,9 +1229,9 @@ static void pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
pollset->kicked_without_pollers = 0; pollset->kicked_without_pollers = 0;
} else if (!pollset->shutting_down) { } else if (!pollset->shutting_down) {
sigemptyset(&new_mask); sigemptyset(&new_mask);
sigaddset(&new_mask, grpc_poller_kick_signum); sigaddset(&new_mask, grpc_wakeup_signal);
pthread_sigmask(SIG_BLOCK, &new_mask, &orig_mask); pthread_sigmask(SIG_BLOCK, &new_mask, &orig_mask);
sigdelset(&orig_mask, grpc_poller_kick_signum); sigdelset(&orig_mask, grpc_wakeup_signal);
push_front_worker(pollset, &worker); push_front_worker(pollset, &worker);
...@@ -1497,19 +1512,29 @@ static bool is_epoll_available() { ...@@ -1497,19 +1512,29 @@ static bool is_epoll_available() {
} }
const grpc_event_engine_vtable *grpc_init_epoll_linux(void) { const grpc_event_engine_vtable *grpc_init_epoll_linux(void) {
/* If use of signals is disabled, we cannot use epoll engine*/
if (is_grpc_wakeup_signal_initialized && grpc_wakeup_signal < 0) {
return NULL;
}
if (!is_epoll_available()) { if (!is_epoll_available()) {
return NULL; return NULL;
} }
if (!is_grpc_wakeup_signal_initialized) {
grpc_use_signal(SIGRTMIN + 2);
}
fd_global_init(); fd_global_init();
pollset_global_init(); pollset_global_init();
polling_island_global_init(); polling_island_global_init();
return &vtable; return &vtable;
} }
#else /* defined(GPR_LINUX_EPOLL) */ #else /* defined(GPR_LINUX_EPOLL) */
/* If GPR_LINUX_EPOLL is not defined, it means epoll is not available. Return /* If GPR_LINUX_EPOLL is not defined, it means epoll is not available. Return
* NULL */ * NULL */
const grpc_event_engine_vtable *grpc_init_epoll_linux(void) { return NULL; } const grpc_event_engine_vtable *grpc_init_epoll_linux(void) { return NULL; }
void grpc_use_signal(int signum) {}
#endif /* !defined(GPR_LINUX_EPOLL) */ #endif /* !defined(GPR_LINUX_EPOLL) */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment