Skip to content

Commit 15ff8f3

Browse files
committed
Merge github.com:grpc/grpc into lfe3
2 parents 6def710 + 6d472b3 commit 15ff8f3

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ load(
3333
"grpc_generate_one_off_targets",
3434
)
3535

36+
config_setting(
37+
name = "grpc_no_ares",
38+
values = {"define": "grpc_no_ares=true"},
39+
)
40+
3641
# This should be updated along with build.yaml
3742
g_stands_for = "generous"
3843

bazel/grpc_build_system.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def grpc_cc_library(name, srcs = [], public_hdrs = [], hdrs = [],
3333
native.cc_library(
3434
name = name,
3535
srcs = srcs,
36+
defines = select({
37+
"//:grpc_no_ares": ["GRPC_ARES=0"],
38+
"//conditions:default": [],
39+
}),
3640
hdrs = hdrs + public_hdrs,
3741
deps = deps + ["//external:" + dep for dep in external_deps],
3842
copts = copts,

src/core/lib/iomgr/exec_ctx.cc

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "src/core/lib/iomgr/combiner.h"
2626
#include "src/core/lib/profiling/timers.h"
2727

28+
#define GRPC_START_TIME_UPDATE_INTERVAL 10000
29+
extern "C" grpc_tracer_flag grpc_timer_check_trace;
30+
2831
bool grpc_exec_ctx_ready_to_finish(grpc_exec_ctx* exec_ctx) {
2932
if ((exec_ctx->flags & GRPC_EXEC_CTX_FLAG_IS_FINISHED) == 0) {
3033
if (exec_ctx->check_ready_to_finish(exec_ctx,
@@ -104,23 +107,49 @@ static void exec_ctx_sched(grpc_exec_ctx* exec_ctx, grpc_closure* closure,
104107
grpc_closure_list_append(&exec_ctx->closure_list, closure, error);
105108
}
106109

107-
static gpr_timespec
110+
/* This time pair is not entirely thread-safe as store/load of tv_sec and
111+
* tv_nsec are performed separately. However g_start_time do not need to have
112+
* sub-second precision, so it is ok if the value of tv_nsec is off in this
113+
* case. */
114+
typedef struct time_atm_pair {
115+
gpr_atm tv_sec;
116+
gpr_atm tv_nsec;
117+
} time_atm_pair;
118+
119+
static time_atm_pair
108120
g_start_time[GPR_TIMESPAN + 1]; // assumes GPR_TIMESPAN is the
109121
// last enum value in
110122
// gpr_clock_type
123+
static grpc_millis g_last_start_time_update;
124+
125+
static gpr_timespec timespec_from_time_atm_pair(const time_atm_pair* src,
126+
gpr_clock_type clock_type) {
127+
gpr_timespec time;
128+
time.tv_nsec = (int32_t)gpr_atm_no_barrier_load(&src->tv_nsec);
129+
time.tv_sec = (int64_t)gpr_atm_no_barrier_load(&src->tv_sec);
130+
time.clock_type = clock_type;
131+
return time;
132+
}
133+
134+
static void time_atm_pair_store(time_atm_pair* dst, const gpr_timespec src) {
135+
gpr_atm_no_barrier_store(&dst->tv_sec, src.tv_sec);
136+
gpr_atm_no_barrier_store(&dst->tv_nsec, src.tv_nsec);
137+
}
111138

112139
void grpc_exec_ctx_global_init(void) {
113140
for (int i = 0; i < GPR_TIMESPAN; i++) {
114-
g_start_time[i] = gpr_now((gpr_clock_type)i);
141+
time_atm_pair_store(&g_start_time[i], gpr_now((gpr_clock_type)i));
115142
}
116143
// allows uniform treatment in conversion functions
117-
g_start_time[GPR_TIMESPAN] = gpr_time_0(GPR_TIMESPAN);
144+
time_atm_pair_store(&g_start_time[GPR_TIMESPAN], gpr_time_0(GPR_TIMESPAN));
118145
}
119146

120147
void grpc_exec_ctx_global_shutdown(void) {}
121148

122149
static gpr_atm timespec_to_atm_round_down(gpr_timespec ts) {
123-
ts = gpr_time_sub(ts, g_start_time[ts.clock_type]);
150+
gpr_timespec start_time =
151+
timespec_from_time_atm_pair(&g_start_time[ts.clock_type], ts.clock_type);
152+
ts = gpr_time_sub(ts, start_time);
124153
double x =
125154
GPR_MS_PER_SEC * (double)ts.tv_sec + (double)ts.tv_nsec / GPR_NS_PER_MS;
126155
if (x < 0) return 0;
@@ -129,7 +158,9 @@ static gpr_atm timespec_to_atm_round_down(gpr_timespec ts) {
129158
}
130159

131160
static gpr_atm timespec_to_atm_round_up(gpr_timespec ts) {
132-
ts = gpr_time_sub(ts, g_start_time[ts.clock_type]);
161+
gpr_timespec start_time =
162+
timespec_from_time_atm_pair(&g_start_time[ts.clock_type], ts.clock_type);
163+
ts = gpr_time_sub(ts, start_time);
133164
double x = GPR_MS_PER_SEC * (double)ts.tv_sec +
134165
(double)ts.tv_nsec / GPR_NS_PER_MS +
135166
(double)(GPR_NS_PER_SEC - 1) / (double)GPR_NS_PER_SEC;
@@ -164,8 +195,9 @@ gpr_timespec grpc_millis_to_timespec(grpc_millis millis,
164195
if (clock_type == GPR_TIMESPAN) {
165196
return gpr_time_from_millis(millis, GPR_TIMESPAN);
166197
}
167-
return gpr_time_add(g_start_time[clock_type],
168-
gpr_time_from_millis(millis, GPR_TIMESPAN));
198+
gpr_timespec start_time =
199+
timespec_from_time_atm_pair(&g_start_time[clock_type], clock_type);
200+
return gpr_time_add(start_time, gpr_time_from_millis(millis, GPR_TIMESPAN));
169201
}
170202

171203
grpc_millis grpc_timespec_to_millis_round_down(gpr_timespec ts) {
@@ -176,6 +208,30 @@ grpc_millis grpc_timespec_to_millis_round_up(gpr_timespec ts) {
176208
return timespec_to_atm_round_up(ts);
177209
}
178210

211+
void grpc_exec_ctx_maybe_update_start_time(grpc_exec_ctx* exec_ctx) {
212+
grpc_millis now = grpc_exec_ctx_now(exec_ctx);
213+
grpc_millis last_start_time_update =
214+
gpr_atm_no_barrier_load(&g_last_start_time_update);
215+
216+
if (now > last_start_time_update &&
217+
now - last_start_time_update > GRPC_START_TIME_UPDATE_INTERVAL) {
218+
/* Get the current system time and subtract \a now from it, where \a now is
219+
* the relative time from grpc_init() from monotonic clock. This calibrates
220+
* the time when grpc_exec_ctx_global_init was called based on current
221+
* system clock. */
222+
gpr_atm_no_barrier_store(&g_last_start_time_update, now);
223+
gpr_timespec real_now = gpr_now(GPR_CLOCK_REALTIME);
224+
gpr_timespec real_start_time =
225+
gpr_time_sub(real_now, gpr_time_from_millis(now, GPR_TIMESPAN));
226+
time_atm_pair_store(&g_start_time[GPR_CLOCK_REALTIME], real_start_time);
227+
228+
if (GRPC_TRACER_ON(grpc_timer_check_trace)) {
229+
gpr_log(GPR_DEBUG, "Update realtime clock start time: %" PRId64 "s %dns",
230+
real_start_time.tv_sec, real_start_time.tv_nsec);
231+
}
232+
}
233+
}
234+
179235
static const grpc_closure_scheduler_vtable exec_ctx_scheduler_vtable = {
180236
exec_ctx_run, exec_ctx_sched, "exec_ctx"};
181237
static grpc_closure_scheduler exec_ctx_scheduler = {&exec_ctx_scheduler_vtable};

src/core/lib/iomgr/exec_ctx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ gpr_timespec grpc_millis_to_timespec(grpc_millis millis, gpr_clock_type clock);
124124
grpc_millis grpc_timespec_to_millis_round_down(gpr_timespec timespec);
125125
grpc_millis grpc_timespec_to_millis_round_up(gpr_timespec timespec);
126126

127+
void grpc_exec_ctx_maybe_update_start_time(grpc_exec_ctx* exec_ctx);
128+
127129
#ifdef __cplusplus
128130
}
129131
#endif

src/core/lib/iomgr/timer_manager.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ static void timer_main_loop(grpc_exec_ctx* exec_ctx) {
225225
for (;;) {
226226
grpc_millis next = GRPC_MILLIS_INF_FUTURE;
227227
grpc_exec_ctx_invalidate_now(exec_ctx);
228+
229+
/* Calibrate g_start_time in exec_ctx.cc with a regular interval in case the
230+
* system clock has changed */
231+
grpc_exec_ctx_maybe_update_start_time(exec_ctx);
232+
228233
// check timer state, updates next to the next time to run a check
229234
switch (grpc_timer_check(exec_ctx, &next)) {
230235
case GRPC_TIMERS_FIRED:

0 commit comments

Comments
 (0)