34 #ifndef GPR_CYCLE_COUNTER_CUSTOM
35 #if GPR_CYCLE_COUNTER_RDTSC_32 || GPR_CYCLE_COUNTER_RDTSC_64
37 static bool read_freq_from_kernel(
double* freq) {
39 int fd =
open(
"/sys/devices/system/cpu/cpu0/tsc_freq_khz", O_RDONLY);
48 const long val = strtol(
line, &
err, 10);
49 if (
line[0] !=
'\0' && (*
err ==
'\n' || *
err ==
'\0')) {
59 static double cycles_per_second = 0;
60 static gpr_cycle_counter start_cycle;
62 static bool is_fake_clock() {
65 for (
int i = 0;
i < 8; ++
i) {
78 if (read_freq_from_kernel(&cycles_per_second)) {
79 start_cycle = gpr_get_cycle_counter();
84 if (is_fake_clock()) {
85 cycles_per_second = 1;
92 double last_freq = -1;
93 bool converged =
false;
94 for (
int i = 0;
i < 8 && !converged; ++
i, measurement_ns *= 2) {
95 start_cycle = gpr_get_cycle_counter();
103 }
while (loop_ns < measurement_ns);
104 gpr_cycle_counter end_cycle = gpr_get_cycle_counter();
107 static_cast<double>(end_cycle - start_cycle) / loop_ns *
GPR_NS_PER_SEC;
109 last_freq != -1 && (freq * 0.99 < last_freq && last_freq < freq * 1.01);
112 cycles_per_second = last_freq;
118 static_cast<double>(cycles - start_cycle) / cycles_per_second;
122 (secs -
static_cast<double>(ts.
tv_sec)));
128 const double secs =
static_cast<double>(
a -
b) / cycles_per_second;
132 (secs -
static_cast<double>(ts.
tv_sec)));
141 #elif GPR_CYCLE_COUNTER_FALLBACK
144 gpr_cycle_counter gpr_get_cycle_counter() {