jdmccalpin / low-overhead-timers

Very low-overhead timer/counter interfaces for C on Intel 64 processors.
BSD 3-Clause "New" or "Revised" License
116 stars 16 forks source link

This project contains a set of very low-overhead timers/counters for inline use in C code running on Intel64 processors.

These interfaces use the lowest-level hardware instructions to read the Time Stamp Counter (TSC), the core fixed-function performance counters, the core programmable performance counters, and a few auxiliary bits of information.

These are intended for use in highly controlled tests. The hardware performance counters are private to each logical processor, so differences only make sense between values that are guaranteed to have come from the same logical processor.

These timers/counters employ user-space instructions only, and require no elevated privileges. Elevated privileges are required to program to counters, but not to read them. The sample scripts SetupPowerLevelCounters.sh and SetupUserKernelCounters.sh require the msrtools binaries to be installed and root permission to execute. They set up the first four hardware performance counters on each logical processor to measure events that are useful for determining whether the output of the test_timer_overhead program is valid.

The interfaces provided include:

Auxiliary routines include:

The source code "low_overhead_counters.c" can be used in one of two ways:

  1. include "low_overhead_counters.c" directly into your code, or

  2. include "low_overhead_counters.h" in your code and put

    "low_overhead_counters.c" as a separate input on the compile line, or compile it separately and include "low_overhead_counters.o" on the compile/link line.

The second approach may add a few instructions and/or cycles to the overhead of the counters unless inter-procedural optimizations are applied.

A driver code and scripts for testing these interfaces are in the LowOverheadTimersTests subdirectory.