From c0df1c0f9a04d9b150af5ba710090f7f8c19f7d5 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 17 Jul 2017 16:12:33 -0700 Subject: Compiling again --- src/core/lib/backoff/backoff.c | 72 +++++++++++++++++++++++++++++++++ src/core/lib/backoff/backoff.h | 57 ++++++++++++++++++++++++++ src/core/lib/iomgr/exec_ctx.c | 8 +++- src/core/lib/iomgr/exec_ctx.h | 6 +-- src/core/lib/iomgr/timer_manager.c | 2 +- src/core/lib/support/backoff.c | 72 --------------------------------- src/core/lib/support/backoff.h | 56 ------------------------- src/core/lib/surface/alarm.c | 3 +- src/core/lib/surface/completion_queue.c | 14 +++---- 9 files changed, 144 insertions(+), 146 deletions(-) create mode 100644 src/core/lib/backoff/backoff.c create mode 100644 src/core/lib/backoff/backoff.h delete mode 100644 src/core/lib/support/backoff.c delete mode 100644 src/core/lib/support/backoff.h (limited to 'src/core/lib') diff --git a/src/core/lib/backoff/backoff.c b/src/core/lib/backoff/backoff.c new file mode 100644 index 0000000000..141b952134 --- /dev/null +++ b/src/core/lib/backoff/backoff.c @@ -0,0 +1,72 @@ +/* + * + * Copyright 2016 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/backoff/backoff.h" + +#include + +void grpc_backoff_init(grpc_backoff *backoff, int64_t initial_connect_timeout, + double multiplier, double jitter, + int64_t min_timeout_millis, int64_t max_timeout_millis) { + backoff->initial_connect_timeout = initial_connect_timeout; + backoff->multiplier = multiplier; + backoff->jitter = jitter; + backoff->min_timeout_millis = min_timeout_millis; + backoff->max_timeout_millis = max_timeout_millis; + backoff->rng_state = (uint32_t)gpr_now(GPR_CLOCK_REALTIME).tv_nsec; +} + +grpc_millis grpc_backoff_begin(grpc_exec_ctx *exec_ctx, grpc_backoff *backoff) { + backoff->current_timeout_millis = backoff->initial_connect_timeout; + const int64_t first_timeout = + GPR_MAX(backoff->current_timeout_millis, backoff->min_timeout_millis); + return grpc_exec_ctx_now(exec_ctx) + first_timeout; +} + +/* Generate a random number between 0 and 1. */ +static double generate_uniform_random_number(uint32_t *rng_state) { + *rng_state = (1103515245 * *rng_state + 12345) % ((uint32_t)1 << 31); + return *rng_state / (double)((uint32_t)1 << 31); +} + +grpc_millis grpc_backoff_step(grpc_exec_ctx *exec_ctx, grpc_backoff *backoff) { + const double new_timeout_millis = + backoff->multiplier * (double)backoff->current_timeout_millis; + backoff->current_timeout_millis = + GPR_MIN((int64_t)new_timeout_millis, backoff->max_timeout_millis); + + const double jitter_range_width = backoff->jitter * new_timeout_millis; + const double jitter = + (2 * generate_uniform_random_number(&backoff->rng_state) - 1) * + jitter_range_width; + + backoff->current_timeout_millis = + (int64_t)((double)(backoff->current_timeout_millis) + jitter); + + const grpc_millis current_deadline = + grpc_exec_ctx_now(exec_ctx) + backoff->current_timeout_millis; + + const grpc_millis min_deadline = + grpc_exec_ctx_now(exec_ctx) + backoff->min_timeout_millis; + + return GPR_MAX(current_deadline, min_deadline); +} + +void grpc_backoff_reset(grpc_backoff *backoff) { + backoff->current_timeout_millis = backoff->initial_connect_timeout; +} diff --git a/src/core/lib/backoff/backoff.h b/src/core/lib/backoff/backoff.h new file mode 100644 index 0000000000..f1258bcdc5 --- /dev/null +++ b/src/core/lib/backoff/backoff.h @@ -0,0 +1,57 @@ +/* + * + * Copyright 2016 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_SUPPORT_BACKOFF_H +#define GRPC_CORE_LIB_SUPPORT_BACKOFF_H + +#include "src/core/lib/iomgr/exec_ctx.h" + +typedef struct { + /// const: how long to wait after the first failure before retrying + int64_t initial_connect_timeout; + /// const: factor with which to multiply backoff after a failed retry + double multiplier; + /// const: amount to randomize backoffs + double jitter; + /// const: minimum time between retries in milliseconds + int64_t min_timeout_millis; + /// const: maximum time between retries in milliseconds + int64_t max_timeout_millis; + + /// random number generator + uint32_t rng_state; + + /// current retry timeout in milliseconds + int64_t current_timeout_millis; +} grpc_backoff; + +/// Initialize backoff machinery - does not need to be destroyed +void grpc_backoff_init(grpc_backoff *backoff, int64_t initial_connect_timeout, + double multiplier, double jitter, + int64_t min_timeout_millis, int64_t max_timeout_millis); + +/// Begin retry loop: returns a timespec for the NEXT retry +grpc_millis grpc_backoff_begin(grpc_exec_ctx *exec_ctx, grpc_backoff *backoff); +/// Step a retry loop: returns a timespec for the NEXT retry +grpc_millis grpc_backoff_step(grpc_exec_ctx *exec_ctx, grpc_backoff *backoff); +/// Reset the backoff, so the next grpc_backoff_step will be a +/// grpc_backoff_begin +/// instead +void grpc_backoff_reset(grpc_backoff *backoff); + +#endif /* GRPC_CORE_LIB_SUPPORT_BACKOFF_H */ diff --git a/src/core/lib/iomgr/exec_ctx.c b/src/core/lib/iomgr/exec_ctx.c index 5d2fc4af86..80aaaee0cb 100644 --- a/src/core/lib/iomgr/exec_ctx.c +++ b/src/core/lib/iomgr/exec_ctx.c @@ -137,13 +137,17 @@ void grpc_exec_ctx_invalidate_now(grpc_exec_ctx *exec_ctx) { exec_ctx->now_is_valid = false; } -gpr_timespec grpc_millis_to_timespec(grpc_exec_ctx *exec_ctx, - grpc_millis millis, +gpr_timespec grpc_millis_to_timespec(grpc_millis millis, gpr_clock_type clock_type) { return gpr_time_add(gpr_convert_clock_type(g_start_time, clock_type), gpr_time_from_millis(millis, GPR_TIMESPAN)); } +grpc_millis grpc_timespec_to_millis(gpr_timespec ts) { + return timespec_to_atm_round_down( + gpr_convert_clock_type(ts, g_start_time.clock_type)); +} + static const grpc_closure_scheduler_vtable exec_ctx_scheduler_vtable = { exec_ctx_run, exec_ctx_sched, "exec_ctx"}; static grpc_closure_scheduler exec_ctx_scheduler = {&exec_ctx_scheduler_vtable}; diff --git a/src/core/lib/iomgr/exec_ctx.h b/src/core/lib/iomgr/exec_ctx.h index 8e030bf741..87383809f3 100644 --- a/src/core/lib/iomgr/exec_ctx.h +++ b/src/core/lib/iomgr/exec_ctx.h @@ -113,9 +113,7 @@ void grpc_exec_ctx_global_shutdown(void); grpc_millis grpc_exec_ctx_now(grpc_exec_ctx *exec_ctx); void grpc_exec_ctx_invalidate_now(grpc_exec_ctx *exec_ctx); -gpr_timespec grpc_millis_to_timespec(grpc_exec_ctx *exec_ctx, - grpc_millis millis, gpr_clock_type clock); -grpc_millis grpc_timespec_to_millis(grpc_exec_ctx *exec_ctx, - gpr_timespec timespec); +gpr_timespec grpc_millis_to_timespec(grpc_millis millis, gpr_clock_type clock); +grpc_millis grpc_timespec_to_millis(gpr_timespec timespec); #endif /* GRPC_CORE_LIB_IOMGR_EXEC_CTX_H */ diff --git a/src/core/lib/iomgr/timer_manager.c b/src/core/lib/iomgr/timer_manager.c index 751314c23f..cd2a3a8c78 100644 --- a/src/core/lib/iomgr/timer_manager.c +++ b/src/core/lib/iomgr/timer_manager.c @@ -188,7 +188,7 @@ static bool wait_until(grpc_exec_ctx *exec_ctx, grpc_millis next) { } gpr_cv_wait(&g_cv_wait, &g_mu, - grpc_millis_to_timespec(exec_ctx, next, GPR_CLOCK_REALTIME)); + grpc_millis_to_timespec(next, GPR_CLOCK_REALTIME)); if (GRPC_TRACER_ON(grpc_timer_check_trace)) { gpr_log(GPR_DEBUG, "wait ended: was_timed:%d kicked:%d", diff --git a/src/core/lib/support/backoff.c b/src/core/lib/support/backoff.c deleted file mode 100644 index 6dc0df473b..0000000000 --- a/src/core/lib/support/backoff.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/support/backoff.h" - -#include - -void gpr_backoff_init(gpr_backoff *backoff, int64_t initial_connect_timeout, - double multiplier, double jitter, - int64_t min_timeout_millis, int64_t max_timeout_millis) { - backoff->initial_connect_timeout = initial_connect_timeout; - backoff->multiplier = multiplier; - backoff->jitter = jitter; - backoff->min_timeout_millis = min_timeout_millis; - backoff->max_timeout_millis = max_timeout_millis; - backoff->rng_state = (uint32_t)gpr_now(GPR_CLOCK_REALTIME).tv_nsec; -} - -gpr_timespec gpr_backoff_begin(gpr_backoff *backoff, gpr_timespec now) { - backoff->current_timeout_millis = backoff->initial_connect_timeout; - const int64_t first_timeout = - GPR_MAX(backoff->current_timeout_millis, backoff->min_timeout_millis); - return gpr_time_add(now, gpr_time_from_millis(first_timeout, GPR_TIMESPAN)); -} - -/* Generate a random number between 0 and 1. */ -static double generate_uniform_random_number(uint32_t *rng_state) { - *rng_state = (1103515245 * *rng_state + 12345) % ((uint32_t)1 << 31); - return *rng_state / (double)((uint32_t)1 << 31); -} - -gpr_timespec gpr_backoff_step(gpr_backoff *backoff, gpr_timespec now) { - const double new_timeout_millis = - backoff->multiplier * (double)backoff->current_timeout_millis; - backoff->current_timeout_millis = - GPR_MIN((int64_t)new_timeout_millis, backoff->max_timeout_millis); - - const double jitter_range_width = backoff->jitter * new_timeout_millis; - const double jitter = - (2 * generate_uniform_random_number(&backoff->rng_state) - 1) * - jitter_range_width; - - backoff->current_timeout_millis = - (int64_t)((double)(backoff->current_timeout_millis) + jitter); - - const gpr_timespec current_deadline = gpr_time_add( - now, gpr_time_from_millis(backoff->current_timeout_millis, GPR_TIMESPAN)); - - const gpr_timespec min_deadline = gpr_time_add( - now, gpr_time_from_millis(backoff->min_timeout_millis, GPR_TIMESPAN)); - - return gpr_time_max(current_deadline, min_deadline); -} - -void gpr_backoff_reset(gpr_backoff *backoff) { - backoff->current_timeout_millis = backoff->initial_connect_timeout; -} diff --git a/src/core/lib/support/backoff.h b/src/core/lib/support/backoff.h deleted file mode 100644 index 6e0cc3a4b6..0000000000 --- a/src/core/lib/support/backoff.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * - * Copyright 2016 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef GRPC_CORE_LIB_SUPPORT_BACKOFF_H -#define GRPC_CORE_LIB_SUPPORT_BACKOFF_H - -#include - -typedef struct { - /// const: how long to wait after the first failure before retrying - int64_t initial_connect_timeout; - /// const: factor with which to multiply backoff after a failed retry - double multiplier; - /// const: amount to randomize backoffs - double jitter; - /// const: minimum time between retries in milliseconds - int64_t min_timeout_millis; - /// const: maximum time between retries in milliseconds - int64_t max_timeout_millis; - - /// random number generator - uint32_t rng_state; - - /// current retry timeout in milliseconds - int64_t current_timeout_millis; -} gpr_backoff; - -/// Initialize backoff machinery - does not need to be destroyed -void gpr_backoff_init(gpr_backoff *backoff, int64_t initial_connect_timeout, - double multiplier, double jitter, - int64_t min_timeout_millis, int64_t max_timeout_millis); - -/// Begin retry loop: returns a timespec for the NEXT retry -gpr_timespec gpr_backoff_begin(gpr_backoff *backoff, gpr_timespec now); -/// Step a retry loop: returns a timespec for the NEXT retry -gpr_timespec gpr_backoff_step(gpr_backoff *backoff, gpr_timespec now); -/// Reset the backoff, so the next gpr_backoff_step will be a gpr_backoff_begin -/// instead -void gpr_backoff_reset(gpr_backoff *backoff); - -#endif /* GRPC_CORE_LIB_SUPPORT_BACKOFF_H */ diff --git a/src/core/lib/surface/alarm.c b/src/core/lib/surface/alarm.c index 911ac010e0..5140780bac 100644 --- a/src/core/lib/surface/alarm.c +++ b/src/core/lib/surface/alarm.c @@ -52,8 +52,7 @@ grpc_alarm *grpc_alarm_create(grpc_completion_queue *cq, gpr_timespec deadline, grpc_cq_begin_op(cq, tag); GRPC_CLOSURE_INIT(&alarm->on_alarm, alarm_cb, alarm, grpc_schedule_on_exec_ctx); - grpc_timer_init(&exec_ctx, &alarm->alarm, - grpc_timespec_to_millis(&exec_ctx, deadline), + grpc_timer_init(&exec_ctx, &alarm->alarm, grpc_timespec_to_millis(deadline), &alarm->on_alarm); grpc_exec_ctx_finish(&exec_ctx); return alarm; diff --git a/src/core/lib/surface/completion_queue.c b/src/core/lib/surface/completion_queue.c index cefa8a2a89..e054741520 100644 --- a/src/core/lib/surface/completion_queue.c +++ b/src/core/lib/surface/completion_queue.c @@ -110,7 +110,7 @@ static grpc_error *non_polling_poller_work(grpc_exec_ctx *exec_ctx, } w.kicked = false; gpr_timespec deadline_ts = - grpc_millis_to_timespec(exec_ctx, deadline, GPR_CLOCK_REALTIME); + grpc_millis_to_timespec(deadline, GPR_CLOCK_REALTIME); while (!npp->shutdown && !w.kicked && !gpr_cv_wait(&w.cv, &npp->mu, deadline_ts)) ; @@ -815,18 +815,17 @@ static grpc_event cq_next(grpc_completion_queue *cq, gpr_timespec deadline, GRPC_CQ_INTERNAL_REF(cq, "next"); + grpc_millis deadline_millis = grpc_timespec_to_millis(deadline); cq_is_finished_arg is_finished_arg = { .last_seen_things_queued_ever = gpr_atm_no_barrier_load(&cqd->things_queued_ever), .cq = cq, - .deadline = 0, + .deadline = deadline_millis, .stolen_completion = NULL, .tag = NULL, .first_loop = true}; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INITIALIZER(0, cq_is_next_finished, &is_finished_arg); - grpc_millis deadline_millis = is_finished_arg.deadline = - grpc_timespec_to_millis(&exec_ctx, deadline); for (;;) { grpc_millis iteration_deadline = deadline_millis; @@ -1047,22 +1046,19 @@ static grpc_event cq_pluck(grpc_completion_queue *cq, void *tag, dump_pending_tags(cq); - deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC); - GRPC_CQ_INTERNAL_REF(cq, "pluck"); gpr_mu_lock(cq->mu); + grpc_millis deadline_millis = grpc_timespec_to_millis(deadline); cq_is_finished_arg is_finished_arg = { .last_seen_things_queued_ever = gpr_atm_no_barrier_load(&cqd->things_queued_ever), .cq = cq, - .deadline = 0, + .deadline = deadline_millis, .stolen_completion = NULL, .tag = tag, .first_loop = true}; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INITIALIZER(0, cq_is_pluck_finished, &is_finished_arg); - grpc_millis deadline_millis = is_finished_arg.deadline = - grpc_timespec_to_millis(&exec_ctx, deadline); for (;;) { if (is_finished_arg.stolen_completion != NULL) { gpr_mu_unlock(cq->mu); -- cgit v1.2.3