17 static void __litl_time_ticks_initialize();
19 #define ERROR_TIMER_NOT_AVAILABLE() do { \
20 fprintf(stderr, "Trying to use timer function %s, but it is not available on this platform\n",__FUNCTION__); \
25 #if CLOCK_GETTIME_AVAIL
26 #ifdef CLOCK_MONOTONIC_RAW
27 #define TIMER_DEFAULT litl_get_time_monotonic_raw
29 #define TIMER_DEFAULT litl_get_time_monotonic
32 #define TIMER_DEFAULT litl_get_time_ticks
45 unsigned threshold = 100000;
51 }
while (t2 - t1 < threshold);
59 static void __litl_time_benchmark() {
60 unsigned best_score = 0;
63 #define RUN_BENCHMARK(_func_) do { \
64 cur_score = __litl_time_benchmark_generic(_func_); \
65 if(cur_score > best_score) { \
66 best_score = cur_score; \
67 litl_set_timing_method(_func_); \
71 #if CLOCK_GETTIME_AVAIL
73 #ifdef CLOCK_MONOTONIC_RAW
77 #ifdef CLOCK_MONOTONIC
85 #ifdef CLOCK_PROCESS_CPUTIME_ID
89 #ifdef CLOCK_THREAD_CPUTIME_ID
95 #if defined(__x86_64__) || defined(__i386)
96 __litl_time_ticks_initialize();
100 printf(
"[LiTL] selected timing method:");
101 #if CLOCK_GETTIME_AVAIL
102 #ifdef CLOCK_MONOTONIC_RAW
104 printf(
"monotonic_raw\n");
107 #ifdef CLOCK_MONOTONIC
109 printf(
"monotonic\n");
112 #ifdef CLOCK_REALTIME
114 printf(
"realtime\n");
117 #ifdef CLOCK_PROCESS_CPUTIME_ID
119 printf(
"process_cputime\n");
122 #ifdef CLOCK_THREAD_CPUTIME_ID
124 printf(
"thread_cputime\n");
129 #if defined(__x86_64__) || defined(__i386)
139 char* time_str = getenv(
"LITL_TIMING_METHOD");
141 if (strcmp(time_str,
"monotonic_raw") == 0) {
142 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_MONOTONIC_RAW))
147 }
else if (strcmp(time_str,
"monotonic") == 0) {
148 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_MONOTONIC))
153 }
else if (strcmp(time_str,
"realtime") == 0) {
154 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_REALTIME))
159 }
else if (strcmp(time_str,
"process_cputime") == 0) {
160 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_PROCESS_CPUTIME_ID))
165 }
else if (strcmp(time_str,
"thread_cputime") == 0) {
166 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_THREAD_CPUTIME_ID))
171 }
else if (strcmp(time_str,
"ticks") == 0) {
172 #if defined(__x86_64__) || defined(__i386)
179 }
else if (strcmp(time_str,
"none") == 0) {
181 }
else if (strcmp(time_str,
"best") == 0) {
182 __litl_time_benchmark();
184 fprintf(stderr,
"Unknown timining method: '%s'\n", time_str);
189 not_available: __attribute__ ((__unused__)) fprintf(stderr,
190 "Timing function '%s' not available on this system\n", time_str);
204 __litl_time_ticks_initialize();
210 #if CLOCK_GETTIME_AVAIL
211 static inline litl_time_t __litl_get_time_generic(clockid_t clk_id) {
214 clock_gettime(clk_id, &tp);
215 time = 1000000000 * tp.tv_sec + tp.tv_nsec;
224 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_MONOTONIC_RAW))
225 return __litl_get_time_generic(CLOCK_MONOTONIC_RAW);
237 #if CLOCK_GETTIME_AVAIL
238 return __litl_get_time_generic(CLOCK_MONOTONIC);
250 #if (defined(CLOCK_GETTIME_AVAIL) && defined (CLOCK_REALTIME))
251 return __litl_get_time_generic(CLOCK_REALTIME);
263 #if (defined(CLOCK_GETTIME_AVAIL) && defined (CLOCK_PROCESS_CPUTIME_ID))
264 return __litl_get_time_generic(CLOCK_PROCESS_CPUTIME_ID);
276 #if (defined(CLOCK_GETTIME_AVAIL) && defined(CLOCK_THREAD_CPUTIME_ID))
277 return __litl_get_time_generic(CLOCK_THREAD_CPUTIME_ID);
289 static int ticks_initialized = 0;
298 #define ticks(val) do { \
300 asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \
301 (val) = ((litl_time_t)__a) | (((litl_time_t)__d)<<32); \
304 #elif defined(__i386)
307 __asm__ volatile("rdtsc" : "=A" (val))
311 #define ticks(val) (val) = -1
318 return time * 1e9 / __ticks_per_sec;
322 static void __litl_time_ticks_initialize() {
323 if (!ticks_initialized) {
333 __ticks_per_sec = init_end - init_start;
334 ticks_initialized = 1;
void litl_time_initialize()
Initializes the timing mechanism.
litl_time_t(* litl_timing_method_t)()
A callback function that returns the current time in ns. It can be either a pointer to one of the tim...
int litl_set_timing_method(litl_timing_method_t callback)
Selects the timing function to use.
litl_time_t litl_get_time_none()
Ultra-fast measurement function.
litl_timing_method_t litl_get_time
Calls the selected timing method and get the current time in ns.
litl_time_t litl_get_time_ticks()
Uses CPU-specific register (for instance, rdtsc for X86* processors)
litl_time_t litl_get_time_monotonic()
Uses clock_gettime(CLOCK_MONOTONIC)
litl_time_t litl_get_time_monotonic_raw()
Uses clock_gettime(CLOCK_MONOTONIC_RAW)
litl_time_t litl_get_time_realtime()
Uses clock_gettime(CLOCK_REALTIME)
litl_time_t litl_get_time_thread_cputime()
Uses clock_gettime(CLOCK_THREAD_CPUTIME)
litl_time_t litl_get_time_process_cputime()
Uses clock_gettime(CLOCK_PROCESS_CPUTIME)
uint64_t litl_time_t
A data type for storing time stamps.
#define RUN_BENCHMARK(_func_)
#define ERROR_TIMER_NOT_AVAILABLE()
litl_timer Provides a set of functions for measuring time