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