Microsecond Accurate (Or Better) Process Timing in Linux

Microsecond accurate (or better) process timing in Linux

If you are looking for this level of timing resolution, you are probably trying to do some micro-optimization. If that's the case, you should look at PAPI. Not only does it provide both wall-clock and virtual (process only) timing information, it also provides access to CPU event counters, which can be indispensable when you are trying to improve performance.

http://icl.cs.utk.edu/papi/

Linux' hrtimer - microsecond precision?

You can do what you want from user space

  1. use clock_gettime() with CLOCK_REALTIME to get the time-of-day with nano-second resolution
  2. use nanosleep() to yield the CPU until you are close to the time you need to execute your task (it is at least milli-second resolution).
  3. use a spin loop with clock_gettime() until you reach the desired time
  4. execute your task

The clock_gettime() function is implemented as a VDSO in recent kernels and modern x86 processors - it takes 20-30 nanoseconds to get the time-of-day with nano-second resolution - you should be able to call clock_gettime() over 30 times per micro-second. Using this method your task should dispatch within 1/30th of a micro-second of the intended time.

Getting millisecond or microsecond-accurate boot time in Linux (C/C++)

Use clock_gettime(CLOCK_BOOTTIME, &ts).

Is gettimeofday() guaranteed to be of microsecond resolution?

Maybe. But you have bigger problems. gettimeofday() can result in incorrect timings if there are processes on your system that change the timer (ie, ntpd). On a "normal" linux, though, I believe the resolution of gettimeofday() is 10us. It can jump forward and backward and time, consequently, based on the processes running on your system. This effectively makes the answer to your question no.

You should look into clock_gettime(CLOCK_MONOTONIC) for timing intervals. It suffers from several less issues due to things like multi-core systems and external clock settings.

Also, look into the clock_getres() function.

Getting an accurate execution time in C++ (micro seconds)

If you are using c++11 or later you could use std::chrono::high_resolution_clock.

A simple use case :

auto start = std::chrono::high_resolution_clock::now();
...
auto elapsed = std::chrono::high_resolution_clock::now() - start;

long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(
elapsed).count();

This solution has the advantage of being portable.


Beware that micro-benchmarking is hard. It's very easy to measure the wrong thing (like your benchmark optimizing away), or to include page-faults in your timed region, or fail to account for CPU frequency idle vs. turbo.

See Idiomatic way of performance evaluation? for some general tips, e.g. sanity check by testing the other one first and see if that changes which one appears faster.

High precision timing in userspace in Linux

clock_gettime allows you to get a nanosecond-precise time from the thread start, process start or epoch.



Related Topics



Leave a reply



Submit