15 #define ERROR_TIMER_NOT_AVAILABLE() do { \
16 fprintf(stderr, "Trying to use timer function %s, but it is not available on this platform\n",__FUNCTION__); \
21 #ifdef CLOCK_GETTIME_AVAIL
22 #ifdef CLOCK_MONOTONIC_RAW
23 #define TIMER_DEFAULT litl_get_time_monotonic_raw
25 #define TIMER_DEFAULT litl_get_time_monotonic
26 #endif // CLOCK_MONOTONIC_RAW
27 #else // CLOCK_GETTIME_AVAIL
28 #define TIMER_DEFAULT litl_get_time_ticks
29 #endif // CLOCK_GETTIME_AVAIL
41 unsigned threshold = 100000;
47 }
while (t2 - t1 < threshold);
55 static void __litl_time_benchmark() {
56 unsigned best_score = 0;
59 #define RUN_BENCHMARK(_func_) do { \
60 cur_score = __litl_time_benchmark_generic(_func_); \
61 if(cur_score > best_score) { \
62 best_score = cur_score; \
63 litl_set_timing_method(_func_); \
67 #ifdef CLOCK_GETTIME_AVAIL
69 #ifdef CLOCK_MONOTONIC_RAW
73 #ifdef CLOCK_MONOTONIC
81 #ifdef CLOCK_PROCESS_CPUTIME_ID
85 #ifdef CLOCK_THREAD_CPUTIME_ID
91 #if defined(__x86_64__) || defined(__i386)
100 char* time_str = getenv(
"LITL_TIMING_METHOD");
102 if (strcmp(time_str,
"monotonic_raw") == 0) {
103 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_MONOTONIC_RAW))
108 }
else if (strcmp(time_str,
"monotonic") == 0) {
109 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_MONOTONIC))
114 }
else if (strcmp(time_str,
"realtime") == 0) {
115 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_REALTIME))
120 }
else if (strcmp(time_str,
"process_cputime") == 0) {
121 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_PROCESS_CPUTIME_ID))
126 }
else if (strcmp(time_str,
"thread_cputime") == 0) {
127 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_THREAD_CPUTIME_ID))
132 }
else if (strcmp(time_str,
"ticks") == 0) {
133 #if defined(__x86_64__) || defined(__i386)
138 }
else if (strcmp(time_str,
"best") == 0) {
139 __litl_time_benchmark();
141 fprintf(stderr,
"Unknown timining method: '%s'\n", time_str);
146 not_available: __attribute__ ((__unused__)) fprintf(stderr,
147 "Timing function '%s' not available on this system\n", time_str);
162 static inline litl_time_t __litl_get_time_generic(clockid_t clk_id) {
165 clock_gettime(clk_id, &tp);
166 time = 1000000000 * tp.tv_sec + tp.tv_nsec;
174 #if(defined(CLOCK_GETTIME_AVAIL) && defined( CLOCK_MONOTONIC_RAW))
175 return __litl_get_time_generic(CLOCK_MONOTONIC_RAW);
187 #ifdef CLOCK_GETTIME_AVAIL
188 return __litl_get_time_generic(CLOCK_MONOTONIC);
200 #if (defined(CLOCK_GETTIME_AVAIL) && defined (CLOCK_REALTIME))
201 return __litl_get_time_generic(CLOCK_REALTIME);
213 #if (defined(CLOCK_GETTIME_AVAIL) && defined (CLOCK_PROCESS_CPUTIME_ID))
214 return __litl_get_time_generic(CLOCK_PROCESS_CPUTIME_ID);
226 #if (defined(CLOCK_GETTIME_AVAIL) && defined(CLOCK_THREAD_CPUTIME_ID))
227 return __litl_get_time_generic(CLOCK_THREAD_CPUTIME_ID);
241 #define ticks(val) do { \
243 asm volatile("rdtsc" : "=a" (__a), "=d" (__d)); \
244 (val) = ((litl_time_t)__a) | (((litl_time_t)__d)<<32); \
247 #elif defined(__i386)
250 __asm__ volatile("rdtsc" : "=A" (val))
254 #define ticks(val) (val) = -1
257 static int ticks_initialized = 0;
259 if (!ticks_initialized) {
265 __ticks_per_sec = init_end - init_start;
266 ticks_initialized = 1;
272 return time * 1e9 / __ticks_per_sec;
litl_time_t litl_get_time_process_cputime()
Uses clock_gettime(CLOCK_PROCESS_CPUTIME)
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_timing_method_t)()
A callback function that returns the current time in ns. It can be either a pointer to one of the tim...
litl_time_t litl_get_time_monotonic_raw()
Uses clock_gettime(CLOCK_MONOTONIC_RAW)
#define ERROR_TIMER_NOT_AVAILABLE()
#define RUN_BENCHMARK(_func_)
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)
void litl_time_initialize()
Initializes the timing mechanism.
int litl_set_timing_method(litl_timing_method_t callback)
Selects the timing function to use.
uint64_t litl_time_t
A data type for storing time stamps.
litl_timer Provides a set of functions for measuring time