From f3756c1e0d09a90173851a8e8ab4f6342ae8de45 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 1 Jul 2015 17:21:01 -0700 Subject: [PATCH] Introduce multiple clocks to GPR --- include/grpc/support/time.h | 16 ++++++- src/core/channel/census_filter.c | 6 +-- src/core/channel/client_setup.c | 6 +-- src/core/iomgr/iomgr.c | 12 ++--- src/core/iomgr/pollset_posix.c | 2 +- src/core/iomgr/pollset_windows.c | 2 +- src/core/iomgr/tcp_client_posix.c | 2 +- src/core/iomgr/tcp_client_windows.c | 2 +- src/core/profiling/timers_preciseclock.h | 2 +- src/core/security/credentials.c | 10 ++-- .../security/google_default_credentials.c | 2 +- src/core/security/json_token.c | 2 +- src/core/statistics/census_rpc_stats.c | 4 +- src/core/statistics/census_tracing.c | 6 +-- src/core/statistics/window_stats.h | 4 +- src/core/support/cancellable.c | 2 +- src/core/support/log_linux.c | 2 +- src/core/support/log_posix.c | 2 +- src/core/support/log_win32.c | 2 +- src/core/support/sync_win32.c | 2 +- src/core/support/time_posix.c | 47 ++++++++++++++++--- src/core/support/time_win32.c | 2 +- src/core/surface/call.c | 2 +- src/core/surface/completion_queue.c | 2 +- src/core/surface/init.c | 2 + src/core/transport/chttp2/parsing.c | 2 +- src/core/transport/chttp2/stream_encoder.c | 2 +- src/core/transport/metadata.c | 2 +- src/csharp/ext/grpc_csharp_ext.c | 2 +- src/php/ext/grpc/timeval.c | 2 +- src/ruby/ext/grpc/rb_completion_queue.c | 2 +- test/core/end2end/cq_verifier.c | 2 +- test/core/fling/client.c | 2 +- test/core/fling/server.c | 2 +- test/core/httpcli/httpcli_test.c | 2 +- test/core/iomgr/alarm_list_test.c | 2 +- test/core/iomgr/alarm_test.c | 4 +- test/core/iomgr/endpoint_tests.c | 6 +-- test/core/iomgr/tcp_client_posix_test.c | 6 +-- test/core/iomgr/tcp_server_posix_test.c | 2 +- .../network_benchmarks/low_level_ping_pong.c | 2 +- test/core/statistics/census_log_tests.c | 4 +- .../multiple_writers_circular_buffer_test.c | 2 +- test/core/statistics/multiple_writers_test.c | 2 +- test/core/statistics/performance_test.c | 2 +- test/core/statistics/quick_test.c | 2 +- test/core/statistics/small_log_test.c | 2 +- test/core/statistics/window_stats_test.c | 6 +-- test/core/support/cancellable_test.c | 26 +++++----- test/core/support/sync_test.c | 12 ++--- test/core/surface/completion_queue_test.c | 2 +- test/core/util/test_config.h | 4 +- 52 files changed, 148 insertions(+), 101 deletions(-) diff --git a/include/grpc/support/time.h b/include/grpc/support/time.h index 1fd3181859f..fb8371bb463 100644 --- a/include/grpc/support/time.h +++ b/include/grpc/support/time.h @@ -62,8 +62,20 @@ extern const gpr_timespec gpr_inf_past; /* The far past. */ #define GPR_NS_PER_US 1000 #define GPR_US_PER_MS 1000 -/* Return the current time measured from the system's default epoch. */ -gpr_timespec gpr_now(void); +/* The clocks we support. */ +typedef enum { + /* Monotonic clock. Epoch undefined. Always moves forwards. */ + GPR_CLOCK_MONOTONIC = 0, + /* Realtime clock. May jump forwards or backwards. Settable by + the system administrator. Has its epoch at 0:00:00 UTC 1 Jan 1970. */ + GPR_CLOCK_REALTIME +} gpr_clock_type; + +/* initialize time subsystem */ +void gpr_time_init(void); + +/* Return the current time measured from the given clocks epoch. */ +gpr_timespec gpr_now(gpr_clock_type clock); /* Return -ve, 0, or +ve according to whether a < b, a == b, or a > b respectively. */ diff --git a/src/core/channel/census_filter.c b/src/core/channel/census_filter.c index 7e393a01a6a..0aa3cc3710b 100644 --- a/src/core/channel/census_filter.c +++ b/src/core/channel/census_filter.c @@ -149,7 +149,7 @@ static void client_init_call_elem(grpc_call_element* elem, call_data* d = elem->call_data; GPR_ASSERT(d != NULL); init_rpc_stats(&d->stats); - d->start_ts = gpr_now(); + d->start_ts = gpr_now(GPR_CLOCK_REALTIME); d->op_id = census_tracing_start_op(); if (initial_op) client_mutate_op(elem, initial_op); } @@ -167,7 +167,7 @@ static void server_init_call_elem(grpc_call_element* elem, call_data* d = elem->call_data; GPR_ASSERT(d != NULL); init_rpc_stats(&d->stats); - d->start_ts = gpr_now(); + d->start_ts = gpr_now(GPR_CLOCK_REALTIME); d->op_id = census_tracing_start_op(); if (initial_op) server_mutate_op(elem, initial_op); } @@ -176,7 +176,7 @@ static void server_destroy_call_elem(grpc_call_element* elem) { call_data* d = elem->call_data; GPR_ASSERT(d != NULL); d->stats.elapsed_time_ms = - gpr_timespec_to_micros(gpr_time_sub(gpr_now(), d->start_ts)); + gpr_timespec_to_micros(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), d->start_ts)); census_record_rpc_server_stats(d->op_id, &d->stats); census_tracing_end_op(d->op_id); } diff --git a/src/core/channel/client_setup.c b/src/core/channel/client_setup.c index 5be8fa66e99..9844f4e43e1 100644 --- a/src/core/channel/client_setup.c +++ b/src/core/channel/client_setup.c @@ -94,7 +94,7 @@ static void setup_initiate(grpc_transport_setup *sp) { int in_alarm = 0; r->setup = s; - r->deadline = gpr_time_add(gpr_now(), gpr_time_from_seconds(60)); + r->deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(60)); gpr_mu_lock(&s->mu); GPR_ASSERT(s->refs > 0); @@ -221,7 +221,7 @@ void grpc_client_setup_create_and_attach( int grpc_client_setup_request_should_continue(grpc_client_setup_request *r, const char *reason) { int result; - if (gpr_time_cmp(gpr_now(), r->deadline) > 0) { + if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), r->deadline) > 0) { result = 0; } else { gpr_mu_lock(&r->setup->mu); @@ -275,7 +275,7 @@ void grpc_client_setup_request_finish(grpc_client_setup_request *r, /* TODO(klempner): Replace these values with further consideration. 2x is probably too aggressive of a backoff. */ gpr_timespec max_backoff = gpr_time_from_minutes(2); - gpr_timespec now = gpr_now(); + gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); gpr_timespec deadline = gpr_time_add(s->current_backoff_interval, now); GPR_ASSERT(!s->in_alarm); s->in_alarm = 1; diff --git a/src/core/iomgr/iomgr.c b/src/core/iomgr/iomgr.c index c47528aa94b..39ba4f52640 100644 --- a/src/core/iomgr/iomgr.c +++ b/src/core/iomgr/iomgr.c @@ -59,7 +59,7 @@ static void background_callback_executor(void *ignored) { while (!g_shutdown) { gpr_timespec deadline = gpr_inf_future; gpr_timespec short_deadline = - gpr_time_add(gpr_now(), gpr_time_from_millis(100)); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100)); if (g_cbs_head) { grpc_iomgr_closure *closure = g_cbs_head; g_cbs_head = closure->next; @@ -67,7 +67,7 @@ static void background_callback_executor(void *ignored) { gpr_mu_unlock(&g_mu); closure->cb(closure->cb_arg, closure->success); gpr_mu_lock(&g_mu); - } else if (grpc_alarm_check(&g_mu, gpr_now(), &deadline)) { + } else if (grpc_alarm_check(&g_mu, gpr_now(GPR_CLOCK_REALTIME), &deadline)) { } else { gpr_mu_unlock(&g_mu); gpr_sleep_until(gpr_time_min(short_deadline, deadline)); @@ -89,7 +89,7 @@ void grpc_iomgr_init(void) { gpr_thd_id id; gpr_mu_init(&g_mu); gpr_cv_init(&g_rcv); - grpc_alarm_list_init(gpr_now()); + grpc_alarm_list_init(gpr_now(GPR_CLOCK_REALTIME)); g_root_object.next = g_root_object.prev = &g_root_object; g_root_object.name = "root"; grpc_iomgr_platform_init(); @@ -110,7 +110,7 @@ void grpc_iomgr_shutdown(void) { grpc_iomgr_object *obj; grpc_iomgr_closure *closure; gpr_timespec shutdown_deadline = - gpr_time_add(gpr_now(), gpr_time_from_seconds(10)); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10)); gpr_mu_lock(&g_mu); g_shutdown = 1; @@ -145,9 +145,9 @@ void grpc_iomgr_shutdown(void) { if (g_root_object.next != &g_root_object) { int timeout = 0; gpr_timespec short_deadline = - gpr_time_add(gpr_now(), gpr_time_from_millis(100)); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100)); while (gpr_cv_wait(&g_rcv, &g_mu, short_deadline) && g_cbs_head == NULL) { - if (gpr_time_cmp(gpr_now(), shutdown_deadline) > 0) { + if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > 0) { timeout = 1; break; } diff --git a/src/core/iomgr/pollset_posix.c b/src/core/iomgr/pollset_posix.c index 15ed8e75e6c..852f2f4740d 100644 --- a/src/core/iomgr/pollset_posix.c +++ b/src/core/iomgr/pollset_posix.c @@ -122,7 +122,7 @@ static void finish_shutdown(grpc_pollset *pollset) { int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { /* pollset->mu already held */ - gpr_timespec now = gpr_now(); + gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); if (gpr_time_cmp(now, deadline) > 0) { return 0; } diff --git a/src/core/iomgr/pollset_windows.c b/src/core/iomgr/pollset_windows.c index 8d6bc79c96c..c0573030e03 100644 --- a/src/core/iomgr/pollset_windows.c +++ b/src/core/iomgr/pollset_windows.c @@ -70,7 +70,7 @@ void grpc_pollset_destroy(grpc_pollset *pollset) { int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { gpr_timespec now; - now = gpr_now(); + now = gpr_now(GPR_CLOCK_REALTIME); if (gpr_time_cmp(now, deadline) > 0) { return 0 /* GPR_FALSE */; } diff --git a/src/core/iomgr/tcp_client_posix.c b/src/core/iomgr/tcp_client_posix.c index bbf7711588f..e0b0fd6e53e 100644 --- a/src/core/iomgr/tcp_client_posix.c +++ b/src/core/iomgr/tcp_client_posix.c @@ -246,7 +246,7 @@ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *ep), ac->write_closure.cb_arg = ac; gpr_mu_lock(&ac->mu); - grpc_alarm_init(&ac->alarm, deadline, on_alarm, ac, gpr_now()); + grpc_alarm_init(&ac->alarm, deadline, on_alarm, ac, gpr_now(GPR_CLOCK_REALTIME)); grpc_fd_notify_on_write(ac->fd, &ac->write_closure); gpr_mu_unlock(&ac->mu); diff --git a/src/core/iomgr/tcp_client_windows.c b/src/core/iomgr/tcp_client_windows.c index b1a169b5192..2ae08f24353 100644 --- a/src/core/iomgr/tcp_client_windows.c +++ b/src/core/iomgr/tcp_client_windows.c @@ -215,7 +215,7 @@ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *tcp), ac->refs = 2; ac->aborted = 0; - grpc_alarm_init(&ac->alarm, deadline, on_alarm, ac, gpr_now()); + grpc_alarm_init(&ac->alarm, deadline, on_alarm, ac, gpr_now(GPR_CLOCK_REALTIME)); socket->write_info.outstanding = 1; grpc_socket_notify_on_write(socket, on_connect, ac); return; diff --git a/src/core/profiling/timers_preciseclock.h b/src/core/profiling/timers_preciseclock.h index 163d52b7974..5c251b47e6f 100644 --- a/src/core/profiling/timers_preciseclock.h +++ b/src/core/profiling/timers_preciseclock.h @@ -82,7 +82,7 @@ struct grpc_precise_clock { gpr_timespec clock; }; static void grpc_precise_clock_now(grpc_precise_clock* clk) { - clk->clock = gpr_now(); + clk->clock = gpr_now(GPR_CLOCK_REALTIME); } #define GRPC_PRECISE_CLOCK_FORMAT "%ld.%09d" #define GRPC_PRECISE_CLOCK_PRINTF_ARGS(clk) \ diff --git a/src/core/security/credentials.c b/src/core/security/credentials.c index cf663faf2d0..4481554d746 100644 --- a/src/core/security/credentials.c +++ b/src/core/security/credentials.c @@ -384,7 +384,7 @@ static void jwt_get_request_metadata(grpc_credentials *creds, if (c->cached.service_url != NULL && strcmp(c->cached.service_url, service_url) == 0 && c->cached.jwt_md != NULL && - (gpr_time_cmp(gpr_time_sub(c->cached.jwt_expiration, gpr_now()), + (gpr_time_cmp(gpr_time_sub(c->cached.jwt_expiration, gpr_now(GPR_CLOCK_REALTIME)), refresh_threshold) > 0)) { jwt_md = grpc_credentials_md_store_ref(c->cached.jwt_md); } @@ -401,7 +401,7 @@ static void jwt_get_request_metadata(grpc_credentials *creds, char *md_value; gpr_asprintf(&md_value, "Bearer %s", jwt); gpr_free(jwt); - c->cached.jwt_expiration = gpr_time_add(gpr_now(), c->jwt_lifetime); + c->cached.jwt_expiration = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), c->jwt_lifetime); c->cached.service_url = gpr_strdup(service_url); c->cached.jwt_md = grpc_credentials_md_store_create(1); grpc_credentials_md_store_add_cstrings( @@ -586,7 +586,7 @@ static void on_oauth2_token_fetcher_http_response( status = grpc_oauth2_token_fetcher_credentials_parse_server_response( response, &c->access_token_md, &token_lifetime); if (status == GRPC_CREDENTIALS_OK) { - c->token_expiration = gpr_time_add(gpr_now(), token_lifetime); + c->token_expiration = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), token_lifetime); r->cb(r->user_data, c->access_token_md->entries, c->access_token_md->num_entries, status); } else { @@ -608,7 +608,7 @@ static void oauth2_token_fetcher_get_request_metadata( { gpr_mu_lock(&c->mu); if (c->access_token_md != NULL && - (gpr_time_cmp(gpr_time_sub(c->token_expiration, gpr_now()), + (gpr_time_cmp(gpr_time_sub(c->token_expiration, gpr_now(GPR_CLOCK_REALTIME)), refresh_threshold) > 0)) { cached_access_token_md = grpc_credentials_md_store_ref(c->access_token_md); @@ -623,7 +623,7 @@ static void oauth2_token_fetcher_get_request_metadata( c->fetch_func( grpc_credentials_metadata_request_create(creds, cb, user_data), &c->httpcli_context, pollset, on_oauth2_token_fetcher_http_response, - gpr_time_add(gpr_now(), refresh_threshold)); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), refresh_threshold)); } } diff --git a/src/core/security/google_default_credentials.c b/src/core/security/google_default_credentials.c index 5822ce63374..93e609456ec 100644 --- a/src/core/security/google_default_credentials.c +++ b/src/core/security/google_default_credentials.c @@ -105,7 +105,7 @@ static int is_stack_running_on_compute_engine(void) { grpc_httpcli_context_init(&context); grpc_httpcli_get(&context, &detector.pollset, &request, - gpr_time_add(gpr_now(), max_detection_delay), + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), max_detection_delay), on_compute_engine_detection_http_response, &detector); /* Block until we get the response. This is not ideal but this should only be diff --git a/src/core/security/json_token.c b/src/core/security/json_token.c index 6116f1d767a..3c411d76cfc 100644 --- a/src/core/security/json_token.c +++ b/src/core/security/json_token.c @@ -207,7 +207,7 @@ static char *encoded_jwt_claim(const grpc_auth_json_key *json_key, grpc_json *child = NULL; char *json_str = NULL; char *result = NULL; - gpr_timespec now = gpr_now(); + gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); gpr_timespec expiration = gpr_time_add(now, token_lifetime); char now_str[GPR_LTOA_MIN_BUFSIZE]; char expiration_str[GPR_LTOA_MIN_BUFSIZE]; diff --git a/src/core/statistics/census_rpc_stats.c b/src/core/statistics/census_rpc_stats.c index 0491c919479..3e571b1143e 100644 --- a/src/core/statistics/census_rpc_stats.c +++ b/src/core/statistics/census_rpc_stats.c @@ -157,7 +157,7 @@ static void record_stats(census_ht* store, census_op_id op_id, key.ptr = gpr_strdup(key.ptr); census_ht_insert(store, key, (void*)window_stats); } - census_window_stats_add(window_stats, gpr_now(), stats); + census_window_stats_add(window_stats, gpr_now(GPR_CLOCK_REALTIME), stats); } else { census_internal_unlock_trace_store(); } @@ -185,7 +185,7 @@ static void get_stats(census_ht* store, census_aggregated_rpc_stats* data) { if (store != NULL) { size_t n; unsigned i, j; - gpr_timespec now = gpr_now(); + gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); census_ht_kv* kv = census_ht_get_all_elements(store, &n); if (kv != NULL) { data->num_entries = n; diff --git a/src/core/statistics/census_tracing.c b/src/core/statistics/census_tracing.c index 05e72b99c0b..3b31a02cb37 100644 --- a/src/core/statistics/census_tracing.c +++ b/src/core/statistics/census_tracing.c @@ -94,7 +94,7 @@ census_op_id census_tracing_start_op(void) { g_id++; memcpy(&ret->id, &g_id, sizeof(census_op_id)); ret->rpc_stats.cnt = 1; - ret->ts = gpr_now(); + ret->ts = gpr_now(GPR_CLOCK_REALTIME); census_ht_insert(g_trace_store, op_id_as_key(&ret->id), (void*)ret); gpr_log(GPR_DEBUG, "Start tracing for id %lu", g_id); gpr_mu_unlock(&g_mu); @@ -122,7 +122,7 @@ void census_tracing_print(census_op_id op_id, const char* anno_txt) { trace = census_ht_find(g_trace_store, op_id_as_key(&op_id)); if (trace != NULL) { census_trace_annotation* anno = gpr_malloc(sizeof(census_trace_annotation)); - anno->ts = gpr_now(); + anno->ts = gpr_now(GPR_CLOCK_REALTIME); { char* d = anno->txt; const char* s = anno_txt; @@ -144,7 +144,7 @@ void census_tracing_end_op(census_op_id op_id) { trace = census_ht_find(g_trace_store, op_id_as_key(&op_id)); if (trace != NULL) { trace->rpc_stats.elapsed_time_ms = - gpr_timespec_to_micros(gpr_time_sub(gpr_now(), trace->ts)); + gpr_timespec_to_micros(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), trace->ts)); gpr_log(GPR_DEBUG, "End tracing for id %lu, method %s, latency %f us", op_id_2_uint64(&op_id), trace->method, trace->rpc_stats.elapsed_time_ms); diff --git a/src/core/statistics/window_stats.h b/src/core/statistics/window_stats.h index d733d8d247a..9651198c71d 100644 --- a/src/core/statistics/window_stats.h +++ b/src/core/statistics/window_stats.h @@ -90,11 +90,11 @@ // Record a new event, taking 15.3ms, transferring 1784 bytes. stat.latency = 0.153; stat.bytes = 1784; - census_window_stats_add(stats, gpr_now(), &stat); + census_window_stats_add(stats, gpr_now(GPR_CLOCK_REALTIME), &stat); // Get sums and print them out result[kMinInterval].statistic = &sums[kMinInterval]; result[kHourInterval].statistic = &sums[kHourInterval]; - census_window_stats_get_sums(stats, gpr_now(), result); + census_window_stats_get_sums(stats, gpr_now(GPR_CLOCK_REALTIME), result); printf("%d events/min, average time %gs, average bytes %g\n", result[kMinInterval].count, (my_stat*)result[kMinInterval].statistic->latency / diff --git a/src/core/support/cancellable.c b/src/core/support/cancellable.c index 5a4d488dd3c..4f8f237f40e 100644 --- a/src/core/support/cancellable.c +++ b/src/core/support/cancellable.c @@ -122,7 +122,7 @@ void gpr_cancellable_cancel(gpr_cancellable *c) { gpr_event ev; gpr_event_init(&ev); gpr_event_wait(&ev, - gpr_time_add(gpr_now(), gpr_time_from_micros(1000))); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000))); } } } while (failures != 0); diff --git a/src/core/support/log_linux.c b/src/core/support/log_linux.c index 48349d2c831..26f1feb5b39 100644 --- a/src/core/support/log_linux.c +++ b/src/core/support/log_linux.c @@ -73,7 +73,7 @@ void gpr_default_log(gpr_log_func_args *args) { char *final_slash; const char *display_file; char time_buffer[64]; - gpr_timespec now = gpr_now(); + gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); struct tm tm; final_slash = strrchr(args->file, '/'); diff --git a/src/core/support/log_posix.c b/src/core/support/log_posix.c index afca792c404..940ee20f151 100644 --- a/src/core/support/log_posix.c +++ b/src/core/support/log_posix.c @@ -75,7 +75,7 @@ void gpr_default_log(gpr_log_func_args *args) { char *final_slash; const char *display_file; char time_buffer[64]; - gpr_timespec now = gpr_now(); + gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); struct tm tm; final_slash = strrchr(args->file, '/'); diff --git a/src/core/support/log_win32.c b/src/core/support/log_win32.c index d249be7d2ec..629781da8a2 100644 --- a/src/core/support/log_win32.c +++ b/src/core/support/log_win32.c @@ -82,7 +82,7 @@ void gpr_log(const char *file, int line, gpr_log_severity severity, /* Simple starter implementation */ void gpr_default_log(gpr_log_func_args *args) { char time_buffer[64]; - gpr_timespec now = gpr_now(); + gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); struct tm tm; if (localtime_s(&tm, &now.tv_sec)) { diff --git a/src/core/support/sync_win32.c b/src/core/support/sync_win32.c index cc31d9b052d..22387103fe2 100644 --- a/src/core/support/sync_win32.c +++ b/src/core/support/sync_win32.c @@ -89,7 +89,7 @@ int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) { if (gpr_time_cmp(abs_deadline, gpr_inf_future) == 0) { SleepConditionVariableCS(cv, &mu->cs, INFINITE); } else { - gpr_timespec now = gpr_now(); + gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); gpr_int64 now_ms = now.tv_sec * 1000 + now.tv_nsec / 1000000; gpr_int64 deadline_ms = abs_deadline.tv_sec * 1000 + abs_deadline.tv_nsec / 1000000; diff --git a/src/core/support/time_posix.c b/src/core/support/time_posix.c index afb58ef2313..d5797e0db2d 100644 --- a/src/core/support/time_posix.c +++ b/src/core/support/time_posix.c @@ -55,22 +55,55 @@ static gpr_timespec gpr_from_timespec(struct timespec ts) { return rv; } -gpr_timespec gpr_now(void) { +/** maps gpr_clock_type --> clockid_t for clock_gettime */ +static clockid_t clockid_for_gpr_clock[] = { + CLOCK_MONOTONIC, + CLOCK_REALTIME +}; + +void gpr_time_init(void) {} + +gpr_timespec gpr_now(gpr_clock_type clock) { struct timespec now; - clock_gettime(CLOCK_REALTIME, &now); + clock_gettime(clockid_for_gpr_clock[clock], &now); return gpr_from_timespec(now); } #else /* For some reason Apple's OSes haven't implemented clock_gettime. */ #include +#include +#include + +static double g_time_scale; +static uint64_t g_time_start; + +void gpr_time_init(void) { + mach_timebase_info_data_t tb = { 0, 1 }; + mach_timebase_info(&tb); + g_time_scale = tb.numer; + g_time_scale /= tb.denom; + g_time_start = mach_absolute_time(); +} -gpr_timespec gpr_now(void) { +gpr_timespec gpr_now(gpr_clock_type clock) { gpr_timespec now; struct timeval now_tv; - gettimeofday(&now_tv, NULL); - now.tv_sec = now_tv.tv_sec; - now.tv_nsec = now_tv.tv_usec * 1000; + double now_dbl; + + switch (clock) { + case GPR_CLOCK_REALTIME: + gettimeofday(&now_tv, NULL); + now.tv_sec = now_tv.tv_sec; + now.tv_nsec = now_tv.tv_usec * 1000; + break; + case GPR_CLOCK_MONOTONIC: + now_dbl = (mach_absolute_time() - g_time_start) * g_time_scale; + now.tv_sec = now_dbl * 1e-9; + now.tv_nsec = now_dbl - now.tv_sec * 1e9; + break; + } + return now; } #endif @@ -83,7 +116,7 @@ void gpr_sleep_until(gpr_timespec until) { for (;;) { /* We could simplify by using clock_nanosleep instead, but it might be * slightly less portable. */ - now = gpr_now(); + now = gpr_now(GPR_CLOCK_REALTIME); if (gpr_time_cmp(until, now) <= 0) { return; } diff --git a/src/core/support/time_win32.c b/src/core/support/time_win32.c index f4443b5c2d7..827abc741de 100644 --- a/src/core/support/time_win32.c +++ b/src/core/support/time_win32.c @@ -58,7 +58,7 @@ void gpr_sleep_until(gpr_timespec until) { for (;;) { /* We could simplify by using clock_nanosleep instead, but it might be * slightly less portable. */ - now = gpr_now(); + now = gpr_now(GPR_CLOCK_REALTIME); if (gpr_time_cmp(until, now) <= 0) { return; } diff --git a/src/core/surface/call.c b/src/core/surface/call.c index 181617fff80..82bd1b2de27 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -1190,7 +1190,7 @@ static void set_deadline_alarm(grpc_call *call, gpr_timespec deadline) { } GRPC_CALL_INTERNAL_REF(call, "alarm"); call->have_alarm = 1; - grpc_alarm_init(&call->alarm, deadline, call_alarm, call, gpr_now()); + grpc_alarm_init(&call->alarm, deadline, call_alarm, call, gpr_now(GPR_CLOCK_REALTIME)); } /* we offset status by a small amount when storing it into transport metadata diff --git a/src/core/surface/completion_queue.c b/src/core/surface/completion_queue.c index 030a8b4e6fc..3c074da652a 100644 --- a/src/core/surface/completion_queue.c +++ b/src/core/surface/completion_queue.c @@ -325,7 +325,7 @@ void grpc_cq_hack_spin_pollset(grpc_completion_queue *cc) { gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset)); grpc_pollset_kick(&cc->pollset); grpc_pollset_work(&cc->pollset, - gpr_time_add(gpr_now(), gpr_time_from_millis(100))); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100))); gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); } diff --git a/src/core/surface/init.c b/src/core/surface/init.c index ca61a38a351..db896934a38 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -33,6 +33,7 @@ #include #include +#include #include "src/core/channel/channel_stack.h" #include "src/core/debug/trace.h" #include "src/core/iomgr/iomgr.h" @@ -56,6 +57,7 @@ void grpc_init(void) { gpr_mu_lock(&g_init_mu); if (++g_initializations == 1) { + gpr_time_init(); grpc_register_tracer("channel", &grpc_trace_channel); grpc_register_tracer("surface", &grpc_surface_trace); grpc_register_tracer("http", &grpc_http_trace); diff --git a/src/core/transport/chttp2/parsing.c b/src/core/transport/chttp2/parsing.c index 4664a0895c2..a11b7e6d369 100644 --- a/src/core/transport/chttp2/parsing.c +++ b/src/core/transport/chttp2/parsing.c @@ -599,7 +599,7 @@ static void on_header(void *tp, grpc_mdelem *md) { } grpc_chttp2_incoming_metadata_buffer_set_deadline( &stream_parsing->incoming_metadata, - gpr_time_add(gpr_now(), *cached_timeout)); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), *cached_timeout)); grpc_mdelem_unref(md); } else { grpc_chttp2_incoming_metadata_buffer_add(&stream_parsing->incoming_metadata, diff --git a/src/core/transport/chttp2/stream_encoder.c b/src/core/transport/chttp2/stream_encoder.c index cf78ac50cc5..db642961ec3 100644 --- a/src/core/transport/chttp2/stream_encoder.c +++ b/src/core/transport/chttp2/stream_encoder.c @@ -437,7 +437,7 @@ static void deadline_enc(grpc_chttp2_hpack_compressor *c, gpr_timespec deadline, framer_state *st) { char timeout_str[GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE]; grpc_mdelem *mdelem; - grpc_chttp2_encode_timeout(gpr_time_sub(deadline, gpr_now()), timeout_str); + grpc_chttp2_encode_timeout(gpr_time_sub(deadline, gpr_now(GPR_CLOCK_REALTIME)), timeout_str); mdelem = grpc_mdelem_from_metadata_strings( c->mdctx, grpc_mdstr_ref(c->timeout_key_str), grpc_mdstr_from_string(c->mdctx, timeout_str)); diff --git a/src/core/transport/metadata.c b/src/core/transport/metadata.c index c80d67823f5..4d0d83e7b7b 100644 --- a/src/core/transport/metadata.c +++ b/src/core/transport/metadata.c @@ -161,7 +161,7 @@ grpc_mdctx *grpc_mdctx_create(void) { /* This seed is used to prevent remote connections from controlling hash table * collisions. It needs to be somewhat unpredictable to a remote connection. */ - return grpc_mdctx_create_with_seed(gpr_now().tv_nsec); + return grpc_mdctx_create_with_seed(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); } static void discard_metadata(grpc_mdctx *ctx) { diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index ec125db78bf..a55cc9e247f 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -379,7 +379,7 @@ grpcsharp_channel_args_destroy(grpc_channel_args *args) { /* Timespec */ -GPR_EXPORT gpr_timespec GPR_CALLTYPE gprsharp_now(void) { return gpr_now(); } +GPR_EXPORT gpr_timespec GPR_CALLTYPE gprsharp_now(void) { return gpr_now(GPR_CLOCK_REALTIME); } GPR_EXPORT gpr_timespec GPR_CALLTYPE gprsharp_inf_future(void) { return gpr_inf_future; diff --git a/src/php/ext/grpc/timeval.c b/src/php/ext/grpc/timeval.c index 8a278d67607..ccf7f0f81af 100644 --- a/src/php/ext/grpc/timeval.c +++ b/src/php/ext/grpc/timeval.c @@ -208,7 +208,7 @@ PHP_METHOD(Timeval, similar) { * @return Timeval The current time */ PHP_METHOD(Timeval, now) { - zval *now = grpc_php_wrap_timeval(gpr_now()); + zval *now = grpc_php_wrap_timeval(gpr_now(GPR_CLOCK_REALTIME)); RETURN_DESTROY_ZVAL(now); } diff --git a/src/ruby/ext/grpc/rb_completion_queue.c b/src/ruby/ext/grpc/rb_completion_queue.c index 8fb3949b3dc..2d52d96dc85 100644 --- a/src/ruby/ext/grpc/rb_completion_queue.c +++ b/src/ruby/ext/grpc/rb_completion_queue.c @@ -91,7 +91,7 @@ static void grpc_rb_completion_queue_shutdown_drain(grpc_completion_queue *cq) { * - investigate further, this is probably another example of C-level cleanup * not working consistently in all cases. */ - next_call.timeout = gpr_time_add(gpr_now(), gpr_time_from_micros(5e3)); + next_call.timeout = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(5e3)); do { rb_thread_call_without_gvl(grpc_rb_completion_queue_next_no_gil, (void *)&next_call, NULL, NULL); diff --git a/test/core/end2end/cq_verifier.c b/test/core/end2end/cq_verifier.c index 33f7c02b612..c290cfdbadc 100644 --- a/test/core/end2end/cq_verifier.c +++ b/test/core/end2end/cq_verifier.c @@ -248,7 +248,7 @@ void cq_verify(cq_verifier *v) { } void cq_verify_empty(cq_verifier *v) { - gpr_timespec deadline = gpr_time_add(gpr_now(), gpr_time_from_seconds(1)); + gpr_timespec deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(1)); grpc_event ev; GPR_ASSERT(v->expect.next == &v->expect && "expectation queue must be empty"); diff --git a/test/core/fling/client.c b/test/core/fling/client.c index ee5e390c396..6741a9dec84 100644 --- a/test/core/fling/client.c +++ b/test/core/fling/client.c @@ -124,7 +124,7 @@ static void step_ping_pong_stream(void) { } static double now(void) { - gpr_timespec tv = gpr_now(); + gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME); return 1e9 * tv.tv_sec + tv.tv_nsec; } diff --git a/test/core/fling/server.c b/test/core/fling/server.c index 9542e15ad06..ac024070609 100644 --- a/test/core/fling/server.c +++ b/test/core/fling/server.c @@ -241,7 +241,7 @@ int main(int argc, char **argv) { shutdown_started = 1; } ev = grpc_completion_queue_next( - cq, gpr_time_add(gpr_now(), gpr_time_from_micros(1000000))); + cq, gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000000))); s = ev.tag; switch (ev.type) { case GRPC_OP_COMPLETE: diff --git a/test/core/httpcli/httpcli_test.c b/test/core/httpcli/httpcli_test.c index 6e579bc0450..9e76a766667 100644 --- a/test/core/httpcli/httpcli_test.c +++ b/test/core/httpcli/httpcli_test.c @@ -145,7 +145,7 @@ int main(int argc, char **argv) { gpr_free(args[0]); gpr_free(args[2]); - gpr_sleep_until(gpr_time_add(gpr_now(), gpr_time_from_seconds(5))); + gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(5))); grpc_test_init(argc, argv); grpc_init(); diff --git a/test/core/iomgr/alarm_list_test.c b/test/core/iomgr/alarm_list_test.c index 684e3f579a4..461c268a23a 100644 --- a/test/core/iomgr/alarm_list_test.c +++ b/test/core/iomgr/alarm_list_test.c @@ -51,7 +51,7 @@ static void cb(void *arg, int success) { } static void add_test(void) { - gpr_timespec start = gpr_now(); + gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME); int i; grpc_alarm alarms[20]; diff --git a/test/core/iomgr/alarm_test.c b/test/core/iomgr/alarm_test.c index 0ccec435e61..362eb5fe634 100644 --- a/test/core/iomgr/alarm_test.c +++ b/test/core/iomgr/alarm_test.c @@ -113,7 +113,7 @@ static void test_grpc_alarm(void) { gpr_event_init(&arg.fcb_arg); grpc_alarm_init(&alarm, GRPC_TIMEOUT_MILLIS_TO_DEADLINE(100), alarm_cb, &arg, - gpr_now()); + gpr_now(GPR_CLOCK_REALTIME)); alarm_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1); gpr_mu_lock(&arg.mu); @@ -165,7 +165,7 @@ static void test_grpc_alarm(void) { gpr_event_init(&arg2.fcb_arg); grpc_alarm_init(&alarm_to_cancel, GRPC_TIMEOUT_MILLIS_TO_DEADLINE(100), - alarm_cb, &arg2, gpr_now()); + alarm_cb, &arg2, gpr_now(GPR_CLOCK_REALTIME)); grpc_alarm_cancel(&alarm_to_cancel); alarm_deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1); diff --git a/test/core/iomgr/endpoint_tests.c b/test/core/iomgr/endpoint_tests.c index 8198c247525..0cfba5fac81 100644 --- a/test/core/iomgr/endpoint_tests.c +++ b/test/core/iomgr/endpoint_tests.c @@ -254,7 +254,7 @@ static void read_and_write_test(grpc_endpoint_test_config config, gpr_mu_lock(GRPC_POLLSET_MU(g_pollset)); while (!state.read_done || !state.write_done) { - GPR_ASSERT(gpr_time_cmp(gpr_now(), deadline) < 0); + GPR_ASSERT(gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0); grpc_pollset_work(g_pollset, deadline); } gpr_mu_unlock(GRPC_POLLSET_MU(g_pollset)); @@ -350,14 +350,14 @@ static void shutdown_during_write_test(grpc_endpoint_test_config config, deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); gpr_mu_lock(GRPC_POLLSET_MU(g_pollset)); while (!write_st.done) { - GPR_ASSERT(gpr_time_cmp(gpr_now(), deadline) < 0); + GPR_ASSERT(gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0); grpc_pollset_work(g_pollset, deadline); } gpr_mu_unlock(GRPC_POLLSET_MU(g_pollset)); grpc_endpoint_destroy(write_st.ep); gpr_mu_lock(GRPC_POLLSET_MU(g_pollset)); while (!read_st.done) { - GPR_ASSERT(gpr_time_cmp(gpr_now(), deadline) < 0); + GPR_ASSERT(gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0); grpc_pollset_work(g_pollset, deadline); } gpr_mu_unlock(GRPC_POLLSET_MU(g_pollset)); diff --git a/test/core/iomgr/tcp_client_posix_test.c b/test/core/iomgr/tcp_client_posix_test.c index b673c032b27..38f043339c3 100644 --- a/test/core/iomgr/tcp_client_posix_test.c +++ b/test/core/iomgr/tcp_client_posix_test.c @@ -187,11 +187,11 @@ void test_times_out(void) { /* Make sure the event doesn't trigger early */ gpr_mu_lock(GRPC_POLLSET_MU(&g_pollset)); while (gpr_time_cmp(gpr_time_add(connect_deadline, gpr_time_from_seconds(2)), - gpr_now()) > 0) { - int is_after_deadline = gpr_time_cmp(connect_deadline, gpr_now()) <= 0; + gpr_now(GPR_CLOCK_REALTIME)) > 0) { + int is_after_deadline = gpr_time_cmp(connect_deadline, gpr_now(GPR_CLOCK_REALTIME)) <= 0; if (is_after_deadline && gpr_time_cmp(gpr_time_add(connect_deadline, gpr_time_from_seconds(1)), - gpr_now()) > 0) { + gpr_now(GPR_CLOCK_REALTIME)) > 0) { /* allow some slack before insisting that things be done */ } else { GPR_ASSERT(g_connections_complete == diff --git a/test/core/iomgr/tcp_server_posix_test.c b/test/core/iomgr/tcp_server_posix_test.c index fb262711c0c..110a2eb1d90 100644 --- a/test/core/iomgr/tcp_server_posix_test.c +++ b/test/core/iomgr/tcp_server_posix_test.c @@ -135,7 +135,7 @@ static void test_connect(int n) { gpr_log(GPR_DEBUG, "wait"); while (g_nconnects == nconnects_before && - gpr_time_cmp(deadline, gpr_now()) > 0) { + gpr_time_cmp(deadline, gpr_now(GPR_CLOCK_REALTIME)) > 0) { grpc_pollset_work(&g_pollset, deadline); } gpr_log(GPR_DEBUG, "wait done"); diff --git a/test/core/network_benchmarks/low_level_ping_pong.c b/test/core/network_benchmarks/low_level_ping_pong.c index 78a0eef1a2e..0ce4bd4b250 100644 --- a/test/core/network_benchmarks/low_level_ping_pong.c +++ b/test/core/network_benchmarks/low_level_ping_pong.c @@ -296,7 +296,7 @@ static void print_histogram(gpr_histogram *histogram) { } static double now(void) { - gpr_timespec tv = gpr_now(); + gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME); return 1e9 * tv.tv_sec + tv.tv_nsec; } diff --git a/test/core/statistics/census_log_tests.c b/test/core/statistics/census_log_tests.c index 241ec1ce4f4..a34dcf07c4d 100644 --- a/test/core/statistics/census_log_tests.c +++ b/test/core/statistics/census_log_tests.c @@ -568,7 +568,7 @@ void test_performance(void) { double write_time_micro = 0.0; int nrecords = 0; setup_test(0); - start_time = gpr_now(); + start_time = gpr_now(GPR_CLOCK_REALTIME); while (1) { void* record = census_log_start_write(write_size); if (record == NULL) { @@ -577,7 +577,7 @@ void test_performance(void) { census_log_end_write(record, write_size); nrecords++; } - write_time = gpr_time_sub(gpr_now(), start_time); + write_time = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start_time); write_time_micro = write_time.tv_sec * 1000000 + write_time.tv_nsec / 1000; census_log_shutdown(); printf( diff --git a/test/core/statistics/multiple_writers_circular_buffer_test.c b/test/core/statistics/multiple_writers_circular_buffer_test.c index a645e15918a..56ada893efd 100644 --- a/test/core/statistics/multiple_writers_circular_buffer_test.c +++ b/test/core/statistics/multiple_writers_circular_buffer_test.c @@ -40,7 +40,7 @@ int main(int argc, char **argv) { grpc_test_init(argc, argv); - srand(gpr_now().tv_nsec); + srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); test_multiple_writers_circular_log(); return 0; } diff --git a/test/core/statistics/multiple_writers_test.c b/test/core/statistics/multiple_writers_test.c index 84aef15c1a9..e524927da62 100644 --- a/test/core/statistics/multiple_writers_test.c +++ b/test/core/statistics/multiple_writers_test.c @@ -40,7 +40,7 @@ int main(int argc, char **argv) { grpc_test_init(argc, argv); - srand(gpr_now().tv_nsec); + srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); test_multiple_writers(); return 0; } diff --git a/test/core/statistics/performance_test.c b/test/core/statistics/performance_test.c index 3c1e28241ec..3f0e0800933 100644 --- a/test/core/statistics/performance_test.c +++ b/test/core/statistics/performance_test.c @@ -40,7 +40,7 @@ int main(int argc, char **argv) { grpc_test_init(argc, argv); - srand(gpr_now().tv_nsec); + srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); test_performance(); return 0; } diff --git a/test/core/statistics/quick_test.c b/test/core/statistics/quick_test.c index 0e432314bba..c72ae77b98e 100644 --- a/test/core/statistics/quick_test.c +++ b/test/core/statistics/quick_test.c @@ -40,7 +40,7 @@ int main(int argc, char **argv) { grpc_test_init(argc, argv); - srand(gpr_now().tv_nsec); + srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); test_invalid_record_size(); test_end_write_with_different_size(); test_read_pending_record(); diff --git a/test/core/statistics/small_log_test.c b/test/core/statistics/small_log_test.c index c151b77f639..b26b95f639c 100644 --- a/test/core/statistics/small_log_test.c +++ b/test/core/statistics/small_log_test.c @@ -40,7 +40,7 @@ int main(int argc, char **argv) { grpc_test_init(argc, argv); - srand(gpr_now().tv_nsec); + srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); test_small_log(); return 0; } diff --git a/test/core/statistics/window_stats_test.c b/test/core/statistics/window_stats_test.c index d893f7f792b..1c66a874070 100644 --- a/test/core/statistics/window_stats_test.c +++ b/test/core/statistics/window_stats_test.c @@ -83,7 +83,7 @@ void empty_test(void) { result.statistic = ∑ census_window_stats_get_sums(stats, zero, &result); GPR_ASSERT(result.count == 0 && sum.value1 == 0 && sum.value2 == 0); - census_window_stats_get_sums(stats, gpr_now(), &result); + census_window_stats_get_sums(stats, gpr_now(GPR_CLOCK_REALTIME), &result); GPR_ASSERT(result.count == 0 && sum.value1 == 0 && sum.value2 == 0); census_window_stats_destroy(stats); } @@ -268,7 +268,7 @@ void rolling_time_test(void) { struct census_window_stats* stats = census_window_stats_create(1, &kMinInterval, 7, &kMyStatInfo); GPR_ASSERT(stats != NULL); - srand(gpr_now().tv_nsec); + srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); for (i = 0; i < 100000; i++) { increment.tv_nsec = rand() % 100000000; /* up to 1/10th second */ when = gpr_time_add(when, increment); @@ -292,7 +292,7 @@ void infinite_interval_test(void) { gpr_timespec increment = {0, 0}; struct census_window_stats* stats = census_window_stats_create(1, &gpr_inf_future, 10, &kMyStatInfo); - srand(gpr_now().tv_nsec); + srand(gpr_now(GPR_CLOCK_REALTIME).tv_nsec); for (i = 0; i < count; i++) { increment.tv_sec = rand() % 21600; /* 6 hours */ when = gpr_time_add(when, increment); diff --git a/test/core/support/cancellable_test.c b/test/core/support/cancellable_test.c index b2db1afc761..e5ed77c7ca5 100644 --- a/test/core/support/cancellable_test.c +++ b/test/core/support/cancellable_test.c @@ -81,22 +81,22 @@ static void test(void) { GPR_ASSERT(!gpr_cancellable_is_cancelled(&t.cancel)); /* Test timeout on event wait for uncancelled gpr_cancellable */ - interval = gpr_now(); + interval = gpr_now(GPR_CLOCK_REALTIME); gpr_event_cancellable_wait( - &t.ev, gpr_time_add(gpr_now(), gpr_time_from_micros(1000000)), &t.cancel); - interval = gpr_time_sub(gpr_now(), interval); + &t.ev, gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000000)), &t.cancel); + interval = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), interval); GPR_ASSERT(gpr_time_cmp(interval, gpr_time_from_micros(500000)) >= 0); GPR_ASSERT(gpr_time_cmp(gpr_time_from_micros(2000000), interval) >= 0); /* Test timeout on cv wait for uncancelled gpr_cancellable */ gpr_mu_lock(&t.mu); - interval = gpr_now(); + interval = gpr_now(GPR_CLOCK_REALTIME); while (!gpr_cv_cancellable_wait( &t.cv, &t.mu, - gpr_time_add(gpr_now(), gpr_time_from_micros(1000000)), + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000000)), &t.cancel)) { } - interval = gpr_time_sub(gpr_now(), interval); + interval = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), interval); GPR_ASSERT(gpr_time_cmp(interval, gpr_time_from_micros(500000)) >= 0); GPR_ASSERT(gpr_time_cmp(gpr_time_from_micros(2000000), interval) >= 0); gpr_mu_unlock(&t.mu); @@ -113,7 +113,7 @@ static void test(void) { /* Wait a second, and check that no threads have finished waiting. */ gpr_mu_lock(&t.mu); gpr_cv_wait(&t.cv, &t.mu, - gpr_time_add(gpr_now(), gpr_time_from_micros(1000000))); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000000))); GPR_ASSERT(t.n == n); gpr_mu_unlock(&t.mu); @@ -129,21 +129,21 @@ static void test(void) { /* Test timeout on cv wait for cancelled gpr_cancellable */ gpr_mu_lock(&t.mu); - interval = gpr_now(); + interval = gpr_now(GPR_CLOCK_REALTIME); while (!gpr_cv_cancellable_wait( &t.cv, &t.mu, - gpr_time_add(gpr_now(), gpr_time_from_micros(1000000)), + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000000)), &t.cancel)) { } - interval = gpr_time_sub(gpr_now(), interval); + interval = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), interval); GPR_ASSERT(gpr_time_cmp(gpr_time_from_micros(100000), interval) >= 0); gpr_mu_unlock(&t.mu); /* Test timeout on event wait for cancelled gpr_cancellable */ - interval = gpr_now(); + interval = gpr_now(GPR_CLOCK_REALTIME); gpr_event_cancellable_wait( - &t.ev, gpr_time_add(gpr_now(), gpr_time_from_micros(1000000)), &t.cancel); - interval = gpr_time_sub(gpr_now(), interval); + &t.ev, gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000000)), &t.cancel); + interval = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), interval); GPR_ASSERT(gpr_time_cmp(gpr_time_from_micros(100000), interval) >= 0); gpr_mu_destroy(&t.mu); diff --git a/test/core/support/sync_test.c b/test/core/support/sync_test.c index 44bc6ba4711..bf5829c4159 100644 --- a/test/core/support/sync_test.c +++ b/test/core/support/sync_test.c @@ -242,12 +242,12 @@ static void test(const char *name, void (*body)(void *m), void (*extra)(void *m), int timeout_s) { gpr_int64 iterations = 1024; struct test *m; - gpr_timespec start = gpr_now(); + gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME); gpr_timespec time_taken; gpr_timespec deadline = gpr_time_add(start, gpr_time_from_micros(timeout_s * 1000000)); fprintf(stderr, "%s:", name); - while (gpr_time_cmp(gpr_now(), deadline) < 0) { + while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0) { iterations <<= 1; fprintf(stderr, " %ld", (long)iterations); m = test_new(10, iterations); @@ -265,7 +265,7 @@ static void test(const char *name, void (*body)(void *m), } test_destroy(m); } - time_taken = gpr_time_sub(gpr_now(), start); + time_taken = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start); fprintf(stderr, " done %ld.%09d s\n", (long)time_taken.tv_sec, (int)time_taken.tv_nsec); } @@ -323,7 +323,7 @@ static void inc_with_1ms_delay(void *v /*=m*/) { for (i = 0; i != m->iterations; i++) { gpr_timespec deadline; gpr_mu_lock(&m->mu); - deadline = gpr_time_add(gpr_now(), gpr_time_from_micros(1000)); + deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000)); while (!gpr_cv_wait(&m->cv, &m->mu, deadline)) { } m->counter++; @@ -339,7 +339,7 @@ static void inc_with_1ms_delay_event(void *v /*=m*/) { gpr_int64 i; for (i = 0; i != m->iterations; i++) { gpr_timespec deadline; - deadline = gpr_time_add(gpr_now(), gpr_time_from_micros(1000)); + deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000)); GPR_ASSERT(gpr_event_wait(&m->event, deadline) == NULL); gpr_mu_lock(&m->mu); m->counter++; @@ -384,7 +384,7 @@ static void consumer(void *v /*=m*/) { gpr_mu_unlock(&m->mu); GPR_ASSERT( !queue_remove(&m->q, &value, - gpr_time_add(gpr_now(), gpr_time_from_micros(1000000)))); + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(1000000)))); mark_thread_done(m); } diff --git a/test/core/surface/completion_queue_test.c b/test/core/surface/completion_queue_test.c index eba24f5c6e6..66136cfe5ec 100644 --- a/test/core/surface/completion_queue_test.c +++ b/test/core/surface/completion_queue_test.c @@ -69,7 +69,7 @@ static void test_wait_empty(void) { LOG_TEST("test_wait_empty"); cc = grpc_completion_queue_create(); - GPR_ASSERT(grpc_completion_queue_next(cc, gpr_now()).type == + GPR_ASSERT(grpc_completion_queue_next(cc, gpr_now(GPR_CLOCK_REALTIME)).type == GRPC_QUEUE_TIMEOUT); shutdown_and_destroy(cc); } diff --git a/test/core/util/test_config.h b/test/core/util/test_config.h index 0b3c54373d9..3db13f5739f 100644 --- a/test/core/util/test_config.h +++ b/test/core/util/test_config.h @@ -52,11 +52,11 @@ extern "C" { (GRPC_TEST_SLOWDOWN_BUILD_FACTOR * GRPC_TEST_SLOWDOWN_MACHINE_FACTOR) #define GRPC_TIMEOUT_SECONDS_TO_DEADLINE(x) \ - gpr_time_add(gpr_now(), \ + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), \ gpr_time_from_micros(GRPC_TEST_SLOWDOWN_FACTOR * 1e6 * (x))) #define GRPC_TIMEOUT_MILLIS_TO_DEADLINE(x) \ - gpr_time_add(gpr_now(), \ + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), \ gpr_time_from_micros(GRPC_TEST_SLOWDOWN_FACTOR * 1e3 * (x))) #ifndef GRPC_TEST_CUSTOM_PICK_PORT