GRPC Core 0.10.0.0

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
timers_preciseclock.h
Go to the documentation of this file.
1 /*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #ifndef GRPC_CORE_PROFILING_TIMERS_PRECISECLOCK_H
35 #define GRPC_CORE_PROFILING_TIMERS_PRECISECLOCK_H
36
37 #include <grpc/support/sync.h>
38 #include <grpc/support/time.h>
39 #include <stdio.h>
40
41 #ifdef GRPC_TIMERS_RDTSC
42 typedef long long int grpc_precise_clock;
43 #if defined(__i386__)
44 static void grpc_precise_clock_now(grpc_precise_clock *clk) {
45 grpc_precise_clock ret;
46 __asm__ volatile("rdtsc" : "=A"(ret));
47 *clk = ret;
48 }
49
50 // ----------------------------------------------------------------
51 #elif defined(__x86_64__) || defined(__amd64__)
52 static void grpc_precise_clock_now(grpc_precise_clock *clk) {
53 unsigned long long low, high;
54 __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
55 *clk = (high << 32) | low;
56 }
57 #endif
58 static gpr_once precise_clock_init = GPR_ONCE_INIT;
59 static double cycles_per_second = 0.0;
60 static void grpc_precise_clock_init() {
61 time_t start = time(NULL);
62 grpc_precise_clock start_time;
63 grpc_precise_clock end_time;
64 while (time(NULL) == start)
65 ;
66 grpc_precise_clock_now(&start_time);
67 while (time(NULL) == start + 1)
68 ;
69 grpc_precise_clock_now(&end_time);
70 cycles_per_second = end_time - start_time;
71 }
72 static double grpc_precise_clock_scaling_factor() {
73 gpr_once_init(&precise_clock_init, grpc_precise_clock_init);
74 return 1e6 / cycles_per_second;
75 }
76 #define GRPC_PRECISE_CLOCK_FORMAT "%f"
77 #define GRPC_PRECISE_CLOCK_PRINTF_ARGS(clk) \
78 (*(clk)*grpc_precise_clock_scaling_factor())
79 #else
80 typedef struct grpc_precise_clock grpc_precise_clock;
81 struct grpc_precise_clock {
82 gpr_timespec clock;
83 };
84 static void grpc_precise_clock_now(grpc_precise_clock* clk) {
85 clk->clock = gpr_now(GPR_CLOCK_REALTIME);
86 }
87 #define GRPC_PRECISE_CLOCK_FORMAT "%ld.%09d"
88 #define GRPC_PRECISE_CLOCK_PRINTF_ARGS(clk) \
89 (clk)->clock.tv_sec, (clk)->clock.tv_nsec
90 static void grpc_precise_clock_print(const grpc_precise_clock* clk, FILE* fp) {
91 fprintf(fp, "%ld.%09d", clk->clock.tv_sec, clk->clock.tv_nsec);
92 }
93 #endif /* GRPC_TIMERS_RDTSC */
94
95 #endif /* GRPC_CORE_PROFILING_TIMERS_PRECISECLOCK_H */
gpr_timespec::tv_sec
time_t tv_sec
Definition: time.h:61
gpr_once_init
void gpr_once_init(gpr_once *once, void(*init_routine)(void))
GPR_CLOCK_REALTIME
Definition: time.h:54
time.h
grpc_precise_clock
struct grpc_precise_clock grpc_precise_clock
Definition: timers_preciseclock.h:80
gpr_once
pthread_once_t gpr_once
Definition: sync_posix.h:43
grpc_precise_clock::clock
gpr_timespec clock
Definition: timers_preciseclock.h:82
gpr_timespec::tv_nsec
int tv_nsec
Definition: time.h:62
GPR_ONCE_INIT
#define GPR_ONCE_INIT
Definition: sync_posix.h:45
sync.h
gpr_now
gpr_timespec gpr_now(gpr_clock_type clock)
gpr_timespec
Definition: time.h:60
grpc_precise_clock
Definition: timers_preciseclock.h:81