| 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 Timestamp::Now() |
| 32 |
// whenever possible. |
| 33 |
|
| 34 |
#if GPR_CYCLE_COUNTER_CUSTOM |
| 35 |
typedef int64_t gpr_cycle_counter; |
| 36 |
gpr_cycle_counter gpr_get_cycle_counter(); |
| 37 |
#elif GPR_CYCLE_COUNTER_RDTSC_32 |
| 38 |
typedef int64_t gpr_cycle_counter; |
| 39 |
inline gpr_cycle_counter gpr_get_cycle_counter() { |
| 40 |
int64_t ret; |
| 41 |
__asm__ volatile("rdtsc" : "=A"(ret)); |
| 42 |
return ret; |
| 43 |
} |
| 44 |
#elif GPR_CYCLE_COUNTER_RDTSC_64 |
| 45 |
typedef int64_t gpr_cycle_counter; |
| 46 |
inline gpr_cycle_counter gpr_get_cycle_counter() { |
| 47 |
uint64_t low, high; |
| 48 |
__asm__ volatile("rdtsc" : "=a"(low), "=d"(high)); |
| 49 |
return (high << 32) | low; |
| 50 |
} |
| 51 |
#elif GPR_CYCLE_COUNTER_FALLBACK |
| 52 |
// TODO(soheil): add support for mrs on Arm. |
| 53 |
|
| 54 |
// Real time in micros. |
| 55 |
typedef double gpr_cycle_counter; |
| 56 |
gpr_cycle_counter gpr_get_cycle_counter(); |
| 57 |
#else |
| 58 |
#error Must define exactly one of \ |
| 59 |
GPR_CYCLE_COUNTER_RDTSC_32, \ |
| 60 |
GPR_CYCLE_COUNTER_RDTSC_64, \ |
| 61 |
GPR_CYCLE_COUNTER_CUSTOM, or \ |
| 62 |
GPR_CYCLE_COUNTER_FALLBACK |
| 63 |
#endif |
| 64 |
|
| 65 |
void gpr_precise_clock_init(void); |
| 66 |
void gpr_precise_clock_now(gpr_timespec* clk); |
| 67 |
gpr_timespec gpr_cycle_counter_to_time(gpr_cycle_counter cycles); |
| 68 |
gpr_timespec gpr_cycle_counter_sub(gpr_cycle_counter a, gpr_cycle_counter b); |
| 69 |
|
| 70 |
#endif /* GRPC_CORE_LIB_GPR_TIME_PRECISE_H */ |