19

Is there an equivalent C function in linux for reading the CPU counter and its frequency?

I am looking for something similair to QueryPerformanceCounter function that reads the 64bit counter in modern CPU's

myforwik
  • 191
  • 1
  • 1
  • 4

3 Answers3

15

clock_gettime(2), with an argument of CLOCK_REALTIME_HR.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
4

clock_gettime() with CLOCK_MONOTONIC_RAW

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41
1

The HAVEGE library uses the CPU counter. Check out its source code here. The relevant file is src/hardtick.h.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365