From 7b84f81a8ca570c3d1e74bbb98d8d38de2bce3c7 Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Tue, 13 Aug 2019 15:04:03 -0400 Subject: [PATCH] Add grpc_cycle_counter_to_millis(). --- .../filters/client_channel/client_channel.cc | 3 +-- src/core/lib/iomgr/exec_ctx.cc | 17 +++++++++++++++++ src/core/lib/iomgr/exec_ctx.h | 3 +++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/core/ext/filters/client_channel/client_channel.cc b/src/core/ext/filters/client_channel/client_channel.cc index 51210107872..0a9b5ac43fb 100644 --- a/src/core/ext/filters/client_channel/client_channel.cc +++ b/src/core/ext/filters/client_channel/client_channel.cc @@ -3730,8 +3730,7 @@ void CallData::ApplyServiceConfigToCallLocked(grpc_call_element* elem) { // from the client API, reset the deadline timer. if (chand->deadline_checking_enabled() && method_params_->timeout() != 0) { const grpc_millis per_method_deadline = - grpc_timespec_to_millis_round_up( - gpr_cycle_counter_to_time(call_start_time_)) + + grpc_cycle_counter_to_millis_round_up(call_start_time_) + method_params_->timeout(); if (per_method_deadline < deadline_) { deadline_ = per_method_deadline; diff --git a/src/core/lib/iomgr/exec_ctx.cc b/src/core/lib/iomgr/exec_ctx.cc index a847b4dcefc..5cb778de98e 100644 --- a/src/core/lib/iomgr/exec_ctx.cc +++ b/src/core/lib/iomgr/exec_ctx.cc @@ -52,6 +52,7 @@ static void exec_ctx_sched(grpc_closure* closure, grpc_error* error) { } static gpr_timespec g_start_time; +static gpr_cycle_counter g_start_cycle; static grpc_millis timespec_to_millis_round_down(gpr_timespec ts) { ts = gpr_time_sub(ts, g_start_time); @@ -101,6 +102,16 @@ grpc_millis grpc_timespec_to_millis_round_up(gpr_timespec ts) { gpr_convert_clock_type(ts, g_start_time.clock_type)); } +grpc_millis grpc_cycle_counter_to_millis_round_down(gpr_cycle_counter cycles) { + return timespec_to_millis_round_down( + gpr_time_add(g_start_time, gpr_cycle_counter_sub(cycles, g_start_cycle))); +} + +grpc_millis grpc_cycle_counter_to_millis_round_up(gpr_cycle_counter cycles) { + return timespec_to_millis_round_up( + gpr_time_add(g_start_time, gpr_cycle_counter_sub(cycles, g_start_cycle))); +} + 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}; @@ -117,7 +128,13 @@ void ExecCtx::TestOnlyGlobalInit(gpr_timespec new_val) { } void ExecCtx::GlobalInit(void) { + // gpr_now(GPR_CLOCK_MONOTONIC) incurs a syscall. We don't actually know the + // exact cycle the time was captured, so we use the average of cycles before + // and after the syscall as the starting cycle. + const gpr_cycle_counter cycle_before = gpr_get_cycle_counter(); g_start_time = gpr_now(GPR_CLOCK_MONOTONIC); + const gpr_cycle_counter cycle_after = gpr_get_cycle_counter(); + g_start_cycle = (cycle_before + cycle_after) / 2; gpr_tls_init(&exec_ctx_); } diff --git a/src/core/lib/iomgr/exec_ctx.h b/src/core/lib/iomgr/exec_ctx.h index daf019c41ee..0ccf2a878bf 100644 --- a/src/core/lib/iomgr/exec_ctx.h +++ b/src/core/lib/iomgr/exec_ctx.h @@ -26,6 +26,7 @@ #include #include +#include "src/core/lib/gpr/time_precise.h" #include "src/core/lib/gpr/tls.h" #include "src/core/lib/gprpp/fork.h" #include "src/core/lib/iomgr/closure.h" @@ -58,6 +59,8 @@ extern grpc_closure_scheduler* grpc_schedule_on_exec_ctx; gpr_timespec grpc_millis_to_timespec(grpc_millis millis, gpr_clock_type clock); grpc_millis grpc_timespec_to_millis_round_down(gpr_timespec timespec); grpc_millis grpc_timespec_to_millis_round_up(gpr_timespec timespec); +grpc_millis grpc_cycle_counter_to_millis_round_down(gpr_cycle_counter cycles); +grpc_millis grpc_cycle_counter_to_millis_round_up(gpr_cycle_counter cycles); namespace grpc_core { /** Execution context.