Shared Persistent Heap Data Environment Manual  1.1.0
Macros | Typedefs | Functions | Variables
sphtimer.h File Reference

Functions to access the Time Base register (PPC) or clock_gettime(CLOCK_MONOTONIC) measure time at high resolution. The POWER timebase is safe to use because it is; separate from CPU clock, invariant, and synchronized across cores and sockets. More...

#include <time.h>

Go to the source code of this file.

Macros

#define __C__
 ignore this macro behind the curtain.
 
#define __TIME_BASE(__time_v)
 Read the Time Base value. More...
 

Typedefs

typedef unsigned long long int sphtimer_t
 Value from TB/TSC register (64-bits on all platforms).
 

Functions

static sphtimer_t sphgettimer (void)
 Read and return the Timebase value. More...
 
__C__ sphtimer_t sphgetcpufreq (void)
 Return the Timebase update frequency. More...
 
static sphtimer_t sphfastcpufreq (void)
 Return the Timebase update frequency (fast version). More...
 

Variables

sphtimer_t sph_cpu_frequency
 Frequency which Timebase is updated by system. More...
 

Detailed Description

Functions to access the Time Base register (PPC) or clock_gettime(CLOCK_MONOTONIC) measure time at high resolution. The POWER timebase is safe to use because it is; separate from CPU clock, invariant, and synchronized across cores and sockets.

The Intel (x86/x86_64) Time Stamp Counter is not invariant except on the latest processors. As we want to support earlier processors back to the initial Core2 design, we use clock_gettime (CLOCK_MONOTONIC) by default for Intel. On modern Linux kernels, clock_gettime is implemented as a VDSO function which provides reasonable performance.

Where applications know they will be running on Intel processors with invariant TSC they can enable inlined rdtsc timers by defining __x86_64_INVARIANT_TSC and/or __x86_INVARIANT_TSC and rebuilding libsphde.

The TB/CLOCK_MONOTONIC value can be obtained using the function sphgettimer and the update frequency by using the functions sphgetcpufreq or preferably sphfastcpufreq.

For instance, to measure the time spend in some computation:

sphtimer_t before, after;
sphtimer_t timer_freg;
double seconds;
timer_freg = sphfastcpufreq ();
before = sphgettimer ();
// computation
after = sphgettimer ();
seconds = (double) (after - before) / (double) timer_freg;
printf ("sec spent: %lf\n", seconds);

Macro Definition Documentation

#define __TIME_BASE (   __time_v)
Value:
do { \
struct timespec __ts; \
clock_gettime (CLOCK_MONOTONIC, &__ts); \
__t = ((sphtimer_t)__ts.tv_sec * 1000000000L) \
+ (sphtimer_t)__ts.tv_nsec; \
__time_v = __t; \
} while (0)
unsigned long long int sphtimer_t
Value from TB/TSC register (64-bits on all platforms).
Definition: sphtimer.h:66

Read the Time Base value.

Function Documentation

static sphtimer_t sphfastcpufreq ( void  )
inlinestatic

Return the Timebase update frequency (fast version).

Will not read the Timebase frequency if the value was already cached (the function sphgetcpufreq forces an read/update of sph_cpu_frequency).

Returns
The Timebase frequency value.
__C__ sphtimer_t sphgetcpufreq ( void  )

Return the Timebase update frequency.

Reads the Timebase frequency and updates sph_cpu_frequency.

Returns
The Timebase update frequency update.
static sphtimer_t sphgettimer ( void  )
inlinestatic

Read and return the Timebase value.

Returns
The Timebase value.

Variable Documentation

sphtimer_t sph_cpu_frequency

Frequency which Timebase is updated by system.

Warning
This variable should not be used directly, the function sphfastcpufreq is fastest since it uses the cached value after the first call.