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_SRC_CORE_LIB_GPR_TIME_PRECISE_H
20 #define GRPC_SRC_CORE_LIB_GPR_TIME_PRECISE_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <grpc/support/time.h>
25
26 // Depending on the platform gpr_get_cycle_counter() can have a resolution as
27 // low as a usec. Use other clock sources or gpr_precise_clock_now(),
28 // where you need high resolution clocks.
29 //
30 // Using gpr_get_cycle_counter() is preferred to using Timestamp::Now()
31 // whenever possible.
32
33 #if GPR_CYCLE_COUNTER_CUSTOM
34 typedef int64_t gpr_cycle_counter;
35 gpr_cycle_counter gpr_get_cycle_counter();
36 #elif GPR_CYCLE_COUNTER_RDTSC_32
37 typedef int64_t gpr_cycle_counter;
gpr_get_cycle_counter()38 inline gpr_cycle_counter gpr_get_cycle_counter() {
39 int64_t ret;
40 __asm__ volatile("rdtsc" : "=A"(ret));
41 return ret;
42 }
43 #elif GPR_CYCLE_COUNTER_RDTSC_64
44 typedef int64_t gpr_cycle_counter;
gpr_get_cycle_counter()45 inline gpr_cycle_counter gpr_get_cycle_counter() {
46 uint64_t low, high;
47 __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
48 return (high << 32) | low;
49 }
50 #elif GPR_CYCLE_COUNTER_FALLBACK
51 // TODO(soheil): add support for mrs on Arm.
52
53 // Real time in micros.
54 typedef double gpr_cycle_counter;
55 gpr_cycle_counter gpr_get_cycle_counter();
56 #else
57 #error Must define exactly one of \
58 GPR_CYCLE_COUNTER_RDTSC_32, \
59 GPR_CYCLE_COUNTER_RDTSC_64, \
60 GPR_CYCLE_COUNTER_CUSTOM, or \
61 GPR_CYCLE_COUNTER_FALLBACK
62 #endif
63
64 void gpr_precise_clock_init(void);
65 void gpr_precise_clock_now(gpr_timespec* clk);
66 gpr_timespec gpr_cycle_counter_to_time(gpr_cycle_counter cycles);
67 gpr_timespec gpr_cycle_counter_sub(gpr_cycle_counter a, gpr_cycle_counter b);
68
69 #endif // GRPC_SRC_CORE_LIB_GPR_TIME_PRECISE_H
70