55 f64 time_now, time_start, time_limit, freq;
59 time_limit = time_now + sample_time;
61 while (time_now < time_limit)
65 freq = (t[1] - t[0]) / (time_now - time_start);
81 #if defined(__aarch64__)
86 fd = open (
"/proc/cpuinfo", 0);
95 if (
unformat (&input,
"cpu MHz : %f", &cpu_freq))
97 else if (
unformat (&input,
"timebase : %f", &ppc_timebase))
108 if (ppc_timebase != 0)
109 cpu_freq = ppc_timebase;
125 fd = open (
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", 0);
130 (void)
unformat (&input,
"%f", &cpu_freq);
141 #if defined (__aarch64__)
147 asm volatile (
"mrs %0, cntfrq_el0":
"=r" (hz));
153 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
168 return (
u64) ecx *ebx / eax;
170 if (max_leaf >= 0x16)
179 return 1e6 * (eax & 0xffff);
185 if (clib_cpu_supports_invariant_tsc ())
221 if (
c->clocks_per_second == 0.0)
223 clib_warning (
"os_cpu_clock_frequency() returned 0.0, use 2e9...");
224 c->clocks_per_second = 2e9;
227 c->seconds_per_clock = 1 /
c->clocks_per_second;
231 c->log2_clocks_per_frequency_verify =
c->log2_clocks_per_second + 4;
234 c->init_reference_time =
c->last_verify_reference_time;
236 c->init_cpu_time =
c->last_verify_cpu_time =
c->last_cpu_time;
237 c->total_cpu_time = 0ULL;
247 c->damping_constant =
exp (-1.0 / 3.75);
253 f64 now_reference, delta_reference, delta_reference_max;
254 f64 delta_clock_in_seconds;
255 u64 now_clock, delta_clock;
256 f64 new_clocks_per_second, delta;
263 delta_reference = now_reference -
c->last_verify_reference_time;
266 delta_clock_in_seconds = (
f64) (now_clock -
c->last_verify_cpu_time) *
267 c->seconds_per_clock;
273 c->init_reference_time += (delta_reference - delta_clock_in_seconds);
274 c->total_cpu_time = (now_reference -
c->init_reference_time)
275 *
c->clocks_per_second;
277 c->last_cpu_time = now_clock;
280 delta_clock =
c->last_cpu_time -
c->last_verify_cpu_time;
282 c->last_verify_cpu_time =
c->last_cpu_time;
283 c->last_verify_reference_time = now_reference;
290 delta_reference_max = (
f64) (2ULL <<
c->log2_clocks_per_frequency_verify) /
291 (
f64) (1ULL <<
c->log2_clocks_per_second);
292 delta_reference_max = delta_reference_max > 8.0 ? delta_reference_max : 8.0;
295 if (delta_reference <= 0.0 || delta_reference > delta_reference_max)
302 new_clocks_per_second = ((
f64) delta_clock) / delta_reference;
305 delta = new_clocks_per_second -
c->clocks_per_second;
312 clib_warning (
"Rejecting large frequency change of %.2f%%",
313 (delta /
c->clocks_per_second) * 100.0);
318 c->clocks_per_second =
c->clocks_per_second *
c->damping_constant +
319 (1.0 -
c->damping_constant) * new_clocks_per_second;
320 c->seconds_per_clock = 1.0 /
c->clocks_per_second;
327 (now_reference -
c->init_reference_time) *
c->clocks_per_second;
335 int verbose = va_arg (*args,
int);
347 delta_reftime_in_seconds = reftime -
c->init_reference_time;
349 error =
now - delta_reftime_in_seconds;
351 s =
format (s,
", reftime %.6f, error %.6f, clocks/sec %.6f",
352 delta_reftime_in_seconds,
error,
c->clocks_per_second);