|
|
@ -1196,30 +1196,16 @@ static struct timespec millis_to_timespec(int millis) { |
|
|
|
return linux_ts; |
|
|
|
return linux_ts; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Convert a timespec to milliseconds:
|
|
|
|
static int poll_deadline_to_millis_timeout(grpc_exec_ctx *exec_ctx, |
|
|
|
- Very small or negative poll times are clamped to zero to do a non-blocking |
|
|
|
grpc_millis millis) { |
|
|
|
poll (which becomes spin polling) |
|
|
|
if (millis == GRPC_MILLIS_INF_FUTURE) return -1; |
|
|
|
- Other small values are rounded up to one millisecond |
|
|
|
grpc_millis delta = millis - grpc_exec_ctx_now(exec_ctx); |
|
|
|
- Longer than a millisecond polls are rounded up to the next nearest |
|
|
|
if (delta > INT_MAX) |
|
|
|
millisecond to avoid spinning |
|
|
|
return INT_MAX; |
|
|
|
- Infinite timeouts are converted to -1 */ |
|
|
|
else if (delta < 0) |
|
|
|
static int poll_deadline_to_millis_timeout(gpr_timespec deadline, |
|
|
|
|
|
|
|
gpr_timespec now) { |
|
|
|
|
|
|
|
gpr_timespec timeout; |
|
|
|
|
|
|
|
static const int64_t max_spin_polling_us = 10; |
|
|
|
|
|
|
|
if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) { |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros( |
|
|
|
|
|
|
|
max_spin_polling_us, |
|
|
|
|
|
|
|
GPR_TIMESPAN))) <= 0) { |
|
|
|
|
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
else |
|
|
|
timeout = gpr_time_sub(deadline, now); |
|
|
|
return (int)delta; |
|
|
|
int millis = gpr_time_to_millis(gpr_time_add( |
|
|
|
|
|
|
|
timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN))); |
|
|
|
|
|
|
|
return millis >= 1 ? millis : 1; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
|
|
|
static void fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd, |
|
|
@ -1287,10 +1273,9 @@ static void pollset_destroy(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) { |
|
|
|
gpr_mu_destroy(&pollset->po.mu); |
|
|
|
gpr_mu_destroy(&pollset->po.mu); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* NOTE: This function may modify 'now' */ |
|
|
|
static bool acquire_polling_lease(grpc_exec_ctx *exec_ctx, |
|
|
|
static bool acquire_polling_lease(grpc_pollset_worker *worker, |
|
|
|
grpc_pollset_worker *worker, |
|
|
|
polling_island *pi, gpr_timespec deadline, |
|
|
|
polling_island *pi, grpc_millis *deadline) { |
|
|
|
gpr_timespec *now) { |
|
|
|
|
|
|
|
bool is_lease_acquired = false; |
|
|
|
bool is_lease_acquired = false; |
|
|
|
|
|
|
|
|
|
|
|
gpr_mu_lock(&pi->worker_list_mu); // LOCK
|
|
|
|
gpr_mu_lock(&pi->worker_list_mu); // LOCK
|
|
|
@ -1302,7 +1287,7 @@ static bool acquire_polling_lease(grpc_pollset_worker *worker, |
|
|
|
|
|
|
|
|
|
|
|
bool is_timeout = false; |
|
|
|
bool is_timeout = false; |
|
|
|
int ret; |
|
|
|
int ret; |
|
|
|
int timeout_ms = poll_deadline_to_millis_timeout(deadline, *now); |
|
|
|
int timeout_ms = poll_deadline_to_millis_timeout(exec_ctx, *deadline); |
|
|
|
if (timeout_ms == -1) { |
|
|
|
if (timeout_ms == -1) { |
|
|
|
ret = sigwaitinfo(&g_wakeup_sig_set, NULL); |
|
|
|
ret = sigwaitinfo(&g_wakeup_sig_set, NULL); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -1325,18 +1310,13 @@ static bool acquire_polling_lease(grpc_pollset_worker *worker, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Did the worker come out of sigtimedwait due to a thread that just
|
|
|
|
|
|
|
|
exited epoll and kicking it (in release_polling_lease function). */ |
|
|
|
|
|
|
|
bool is_polling_turn = gpr_atm_acq_load(&worker->is_polling_turn); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Did the worker come out of sigtimedwait due to a thread alerting it that
|
|
|
|
/* Did the worker come out of sigtimedwait due to a thread alerting it that
|
|
|
|
some completion event was (likely) available in the completion queue */ |
|
|
|
some completion event was (likely) available in the completion queue */ |
|
|
|
bool is_kicked = gpr_atm_no_barrier_load(&worker->is_kicked); |
|
|
|
bool is_kicked = gpr_atm_no_barrier_load(&worker->is_kicked); |
|
|
|
|
|
|
|
|
|
|
|
if (is_kicked || is_timeout) { |
|
|
|
if (is_kicked || is_timeout) { |
|
|
|
*now = deadline; /* Essentially make the epoll timeout = 0 */ |
|
|
|
*deadline = grpc_exec_ctx_now( |
|
|
|
} else if (is_polling_turn) { |
|
|
|
exec_ctx); /* Essentially make the epoll timeout = 0 */ |
|
|
|
*now = gpr_now(GPR_CLOCK_MONOTONIC); /* Reduce the epoll timeout */ |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
gpr_mu_lock(&pi->worker_list_mu); // LOCK
|
|
|
|
gpr_mu_lock(&pi->worker_list_mu); // LOCK
|
|
|
@ -1376,11 +1356,11 @@ static void release_polling_lease(polling_island *pi, grpc_error **error) { |
|
|
|
static void pollset_do_epoll_pwait(grpc_exec_ctx *exec_ctx, int epoll_fd, |
|
|
|
static void pollset_do_epoll_pwait(grpc_exec_ctx *exec_ctx, int epoll_fd, |
|
|
|
grpc_pollset *pollset, polling_island *pi, |
|
|
|
grpc_pollset *pollset, polling_island *pi, |
|
|
|
grpc_pollset_worker *worker, |
|
|
|
grpc_pollset_worker *worker, |
|
|
|
gpr_timespec now, gpr_timespec deadline, |
|
|
|
grpc_millis deadline, sigset_t *sig_mask, |
|
|
|
sigset_t *sig_mask, grpc_error **error) { |
|
|
|
grpc_error **error) { |
|
|
|
/* Only g_max_pollers_per_pi threads can be doing polling in parallel.
|
|
|
|
/* Only g_max_pollers_per_pi threads can be doing polling in parallel.
|
|
|
|
If we cannot get a lease, we cannot continue to do epoll_pwait() */ |
|
|
|
If we cannot get a lease, we cannot continue to do epoll_pwait() */ |
|
|
|
if (!acquire_polling_lease(worker, pi, deadline, &now)) { |
|
|
|
if (!acquire_polling_lease(exec_ctx, worker, pi, &deadline)) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1390,12 +1370,12 @@ static void pollset_do_epoll_pwait(grpc_exec_ctx *exec_ctx, int epoll_fd, |
|
|
|
const char *err_desc = "pollset_work_and_unlock"; |
|
|
|
const char *err_desc = "pollset_work_and_unlock"; |
|
|
|
|
|
|
|
|
|
|
|
/* timeout_ms is the time between 'now' and 'deadline' */ |
|
|
|
/* timeout_ms is the time between 'now' and 'deadline' */ |
|
|
|
int timeout_ms = poll_deadline_to_millis_timeout(deadline, now); |
|
|
|
int timeout_ms = poll_deadline_to_millis_timeout(exec_ctx, deadline); |
|
|
|
|
|
|
|
|
|
|
|
GRPC_SCHEDULING_START_BLOCKING_REGION; |
|
|
|
GRPC_SCHEDULING_START_BLOCKING_REGION; |
|
|
|
ep_rv = |
|
|
|
ep_rv = |
|
|
|
epoll_pwait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, timeout_ms, sig_mask); |
|
|
|
epoll_pwait(epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, timeout_ms, sig_mask); |
|
|
|
GRPC_SCHEDULING_END_BLOCKING_REGION; |
|
|
|
GRPC_SCHEDULING_END_BLOCKING_REGION_WITH_EXEC_CTX(exec_ctx); |
|
|
|
|
|
|
|
|
|
|
|
/* Give back the lease right away so that some other thread can enter */ |
|
|
|
/* Give back the lease right away so that some other thread can enter */ |
|
|
|
release_polling_lease(pi, error); |
|
|
|
release_polling_lease(pi, error); |
|
|
@ -1450,8 +1430,8 @@ static void pollset_do_epoll_pwait(grpc_exec_ctx *exec_ctx, int epoll_fd, |
|
|
|
static void pollset_work_and_unlock(grpc_exec_ctx *exec_ctx, |
|
|
|
static void pollset_work_and_unlock(grpc_exec_ctx *exec_ctx, |
|
|
|
grpc_pollset *pollset, |
|
|
|
grpc_pollset *pollset, |
|
|
|
grpc_pollset_worker *worker, |
|
|
|
grpc_pollset_worker *worker, |
|
|
|
gpr_timespec now, gpr_timespec deadline, |
|
|
|
grpc_millis deadline, sigset_t *sig_mask, |
|
|
|
sigset_t *sig_mask, grpc_error **error) { |
|
|
|
grpc_error **error) { |
|
|
|
int epoll_fd = -1; |
|
|
|
int epoll_fd = -1; |
|
|
|
polling_island *pi = NULL; |
|
|
|
polling_island *pi = NULL; |
|
|
|
GPR_TIMER_BEGIN("pollset_work_and_unlock", 0); |
|
|
|
GPR_TIMER_BEGIN("pollset_work_and_unlock", 0); |
|
|
@ -1499,7 +1479,7 @@ static void pollset_work_and_unlock(grpc_exec_ctx *exec_ctx, |
|
|
|
gpr_mu_unlock(&pollset->po.mu); |
|
|
|
gpr_mu_unlock(&pollset->po.mu); |
|
|
|
|
|
|
|
|
|
|
|
g_current_thread_polling_island = pi; |
|
|
|
g_current_thread_polling_island = pi; |
|
|
|
pollset_do_epoll_pwait(exec_ctx, epoll_fd, pollset, pi, worker, now, deadline, |
|
|
|
pollset_do_epoll_pwait(exec_ctx, epoll_fd, pollset, pi, worker, deadline, |
|
|
|
sig_mask, error); |
|
|
|
sig_mask, error); |
|
|
|
g_current_thread_polling_island = NULL; |
|
|
|
g_current_thread_polling_island = NULL; |
|
|
|
|
|
|
|
|
|
|
@ -1521,7 +1501,7 @@ static void pollset_work_and_unlock(grpc_exec_ctx *exec_ctx, |
|
|
|
ensure that it is held by the time the function returns */ |
|
|
|
ensure that it is held by the time the function returns */ |
|
|
|
static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
|
|
|
static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
|
|
|
grpc_pollset_worker **worker_hdl, |
|
|
|
grpc_pollset_worker **worker_hdl, |
|
|
|
gpr_timespec now, gpr_timespec deadline) { |
|
|
|
grpc_millis deadline) { |
|
|
|
GPR_TIMER_BEGIN("pollset_work", 0); |
|
|
|
GPR_TIMER_BEGIN("pollset_work", 0); |
|
|
|
grpc_error *error = GRPC_ERROR_NONE; |
|
|
|
grpc_error *error = GRPC_ERROR_NONE; |
|
|
|
|
|
|
|
|
|
|
@ -1577,7 +1557,7 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
|
|
|
|
|
|
|
|
|
|
|
push_front_worker(pollset, &worker); /* Add worker to pollset */ |
|
|
|
push_front_worker(pollset, &worker); /* Add worker to pollset */ |
|
|
|
|
|
|
|
|
|
|
|
pollset_work_and_unlock(exec_ctx, pollset, &worker, now, deadline, |
|
|
|
pollset_work_and_unlock(exec_ctx, pollset, &worker, deadline, |
|
|
|
&g_orig_sigmask, &error); |
|
|
|
&g_orig_sigmask, &error); |
|
|
|
grpc_exec_ctx_flush(exec_ctx); |
|
|
|
grpc_exec_ctx_flush(exec_ctx); |
|
|
|
|
|
|
|
|
|
|
|