1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_CORE_LIB_GPR_TIME_PRECISE_H
20 #define GRPC_CORE_LIB_GPR_TIME_PRECISE_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <grpc/impl/codegen/gpr_types.h>
25 #include <grpc/support/time.h>
26
27 // Depending on the platform gpr_get_cycle_counter() can have a resolution as
28 // low as a usec. Use other clock sources or gpr_precise_clock_now(),
29 // where you need high resolution clocks.
30 //
31 // Using gpr_get_cycle_counter() is preferred to using ExecCtx::Get()->Now()
32 // whenever possible.
33
34 #if GPR_CYCLE_COUNTER_RDTSC_32
35 typedef int64_t gpr_cycle_counter;
gpr_get_cycle_counter()36 inline gpr_cycle_counter gpr_get_cycle_counter() {
37 int64_t ret;
38 __asm__ volatile("rdtsc" : "=A"(ret));
39 return ret;
40 }
41 #elif GPR_CYCLE_COUNTER_RDTSC_64
42 typedef int64_t gpr_cycle_counter;
gpr_get_cycle_counter()43 inline gpr_cycle_counter gpr_get_cycle_counter() {
44 uint64_t low, high;
45 __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
46 return (high << 32) | low;
47 }
48 #elif GPR_CYCLE_COUNTER_FALLBACK
49 // TODO(soheil): add support for mrs on Arm.
50
51 // Real time in micros.
52 typedef double gpr_cycle_counter;
53 gpr_cycle_counter gpr_get_cycle_counter();
54 #else
55 #error Must define exactly one of \
56 GPR_CYCLE_COUNTER_RDTSC_32, \
57 GPR_CYCLE_COUNTER_RDTSC_64, or \
58 GPR_CYCLE_COUNTER_FALLBACK
59 #endif
60
61 void gpr_precise_clock_init(void);
62 void gpr_precise_clock_now(gpr_timespec* clk);
63 gpr_timespec gpr_cycle_counter_to_time(gpr_cycle_counter cycles);
64 gpr_timespec gpr_cycle_counter_sub(gpr_cycle_counter a, gpr_cycle_counter b);
65
66 #endif /* GRPC_CORE_LIB_GPR_TIME_PRECISE_H */
67