X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fcommon%2Ftiming.c;h=15d0b551f58ecb2982cb104c2d866f2dd847408e;hb=3ea349549afe95a9bc04cff82edf10c23ab0ba48;hp=6f6d9b691108a1718183e1ca03f8d1021c6d2bbd;hpb=6dec394d82d2c8bb07b31dbb79373b1a957979a1;p=libfirm diff --git a/ir/common/timing.c b/ir/common/timing.c index 6f6d9b691..15d0b551f 100644 --- a/ir/common/timing.c +++ b/ir/common/timing.c @@ -22,22 +22,13 @@ * @brief platform neutral timing utilities * @version $Id: debug.c 17143 2008-01-02 20:56:33Z beck $ */ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif - -#include "firm_config.h" #include #include -#ifndef _WIN32 -#include -#endif - #include "timing.h" -#include "set.h" -#include "hashptr.h" +#include "xmalloc.h" #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN @@ -52,6 +43,7 @@ typedef union { #else +#include #define HAVE_GETTIMEOFDAY /* @@ -70,100 +62,76 @@ typedef struct timeval ir_timer_val_t; #include -static INLINE void _time_get(ir_timer_val_t *val); -static INLINE void _time_reset(ir_timer_val_t *val); -static INLINE unsigned long _time_to_msec(const ir_timer_val_t *val); -static INLINE ir_timer_val_t *_time_add(ir_timer_val_t *res, +static inline void _time_get(ir_timer_val_t *val); +static inline void _time_reset(ir_timer_val_t *val); +static inline unsigned long _time_to_msec(const ir_timer_val_t *val); +static inline ir_timer_val_t *_time_add(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs); -static INLINE ir_timer_val_t *_time_sub(ir_timer_val_t *res, +static inline ir_timer_val_t *_time_sub(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs); /** * A timer. */ -struct _ir_timer_t { +struct ir_timer_t { ir_timer_val_t elapsed; /**< the elapsed time so far */ ir_timer_val_t start; /**< the start value of the timer */ ir_timer_t *link; /**< link to the next entry in the timer stack */ - const char *name; /**< the name of the timer used for identification */ - const char *desc; /**< a description if the timer */ unsigned running : 1; /**< set if this timer is running */ }; -/** - * Compare two timers. - */ -static int ir_timer_cmp(const void *a, const void *b, size_t size) -{ - const ir_timer_t *t1 = a; - const ir_timer_t *t2 = b; - (void) size; - return strcmp(t1->name, t2->name); -} - -/** The set containing all currently registered timers. */ -static set *timers = NULL; - /** The top of the timer stack */ static ir_timer_t *timer_stack; -/** Initialize the timer module. */ -static void timing_init(void) +ir_timer_t *ir_timer_new(void) { - timers = new_set(ir_timer_cmp, 16); - timer_stack = NULL; -} + ir_timer_t *timer = XMALLOCZ(ir_timer_t); -ir_timer_t *ir_timer_register(const char *name, const char *desc) -{ - unsigned hash = HASH_STR(name, strlen(name)); - ir_timer_t timer; - - _time_reset(&timer.elapsed); - _time_reset(&timer.start); - timer.link = NULL; - timer.name = name; - timer.desc = desc; - timer.running = 0; + _time_reset(&timer->elapsed); + _time_reset(&timer->start); + timer->link = NULL; + timer->running = 0; - if (!timers) - timing_init(); + return timer; +} - return set_insert(timers, &timer, sizeof(timer), hash); +void ir_timer_free(ir_timer_t *timer) +{ + xfree(timer); } #ifdef HAVE_GETTIMEOFDAY -static INLINE void _time_get(ir_timer_val_t *val) +static inline void _time_get(ir_timer_val_t *val) { gettimeofday(val, NULL); } -static INLINE void _time_reset(ir_timer_val_t *val) +static inline void _time_reset(ir_timer_val_t *val) { timerclear(val); } -static INLINE unsigned long _time_to_msec(const ir_timer_val_t *elapsed) +static inline unsigned long _time_to_msec(const ir_timer_val_t *elapsed) { return (unsigned long) elapsed->tv_sec * 1000UL + (unsigned long) elapsed->tv_usec / 1000UL; } -static INLINE unsigned long _time_to_usec(const ir_timer_val_t *elapsed) +static inline unsigned long _time_to_usec(const ir_timer_val_t *elapsed) { return (unsigned long) elapsed->tv_sec * 1000000UL + (unsigned long) elapsed->tv_usec; } -static INLINE ir_timer_val_t *_time_add(ir_timer_val_t *res, +static inline ir_timer_val_t *_time_add(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs) { timeradd(lhs, rhs, res); return res; } -static INLINE ir_timer_val_t *_time_sub(ir_timer_val_t *res, +static inline ir_timer_val_t *_time_sub(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs) { timersub(lhs, rhs, res); @@ -172,41 +140,41 @@ static INLINE ir_timer_val_t *_time_sub(ir_timer_val_t *res, #elif defined(_WIN32) -static INLINE void _time_get(ir_timer_val_t *val) +static inline void _time_get(ir_timer_val_t *val) { - if(!QueryPerformanceCounter(&val->hi_prec)) + if (!QueryPerformanceCounter(&val->hi_prec)) val->lo_prec = timeGetTime(); } -static INLINE void _time_reset(ir_timer_val_t *val) +static inline void _time_reset(ir_timer_val_t *val) { memset(val, 0, sizeof(val[0])); } -static INLINE unsigned long _time_to_msec(const ir_timer_val_t *elapsed) +static inline unsigned long _time_to_msec(const ir_timer_val_t *elapsed) { LARGE_INTEGER freq; - if(!QueryPerformanceFrequency(&freq)) + if (!QueryPerformanceFrequency(&freq)) return (unsigned long) elapsed->lo_prec; return (unsigned long) ((elapsed->hi_prec.QuadPart * 1000) / freq.QuadPart); } -static INLINE unsigned long _time_to_usec(const ir_timer_val_t *elapsed) +static inline unsigned long _time_to_usec(const ir_timer_val_t *elapsed) { LARGE_INTEGER freq; - if(!QueryPerformanceFrequency(&freq)) + if (!QueryPerformanceFrequency(&freq)) return (unsigned long) elapsed->lo_prec; return (unsigned long) ((elapsed->hi_prec.QuadPart * 1000000) / freq.QuadPart); } -static INLINE ir_timer_val_t *_time_add(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs) +static inline ir_timer_val_t *_time_add(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs) { LARGE_INTEGER dummy; - if(QueryPerformanceFrequency(&dummy)) + if (QueryPerformanceFrequency(&dummy)) res->hi_prec.QuadPart = lhs->hi_prec.QuadPart + rhs->hi_prec.QuadPart; else res->lo_prec = lhs->lo_prec + rhs->lo_prec; @@ -214,10 +182,10 @@ static INLINE ir_timer_val_t *_time_add(ir_timer_val_t *res, const ir_timer_val_ return res; } -static INLINE ir_timer_val_t *_time_sub(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs) +static inline ir_timer_val_t *_time_sub(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs) { LARGE_INTEGER dummy; - if(QueryPerformanceFrequency(&dummy)) + if (QueryPerformanceFrequency(&dummy)) res->hi_prec.QuadPart = lhs->hi_prec.QuadPart - rhs->hi_prec.QuadPart; else res->lo_prec = lhs->lo_prec - rhs->lo_prec; @@ -227,7 +195,7 @@ static INLINE ir_timer_val_t *_time_sub(ir_timer_val_t *res, const ir_timer_val_ #endif /* _WIN32 */ -#if _XOPEN_REALTIME != -1 +#if defined(_XOPEN_REALTIME) && _XOPEN_REALTIME != -1 #include #include @@ -242,7 +210,7 @@ int ir_timer_enter_high_priority(void) struct sched_param p; int res, max, algo; - if(!std_sched_param_init) { + if (!std_sched_param_init) { res = sched_getparam(pid, &std_sched_param); std_sched_param_init = 1; } @@ -262,7 +230,7 @@ int ir_timer_leave_high_priority(void) int res = 0; pid_t pid = getpid(); - if(std_sched_param_init) + if (std_sched_param_init) res = sched_setparam(pid, &std_sched_param); return res; @@ -275,7 +243,7 @@ static int initial_priority = THREAD_PRIORITY_NORMAL; int ir_timer_leave_high_priority(void) { int res = 0; - if(!SetThreadPriority(GetCurrentThread(), initial_priority)) { + if (!SetThreadPriority(GetCurrentThread(), initial_priority)) { fprintf(stderr, "Failed to leave high priority (%d)\n", GetLastError()); res = GetLastError(); } @@ -287,7 +255,7 @@ int ir_timer_enter_high_priority(void) { int res = 0; initial_priority = GetThreadPriority(GetCurrentThread()); - if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)) { + if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)) { fprintf(stderr, "Failed to enter high priority (%d)\n", GetLastError()); res = GetLastError(); } @@ -331,7 +299,7 @@ size_t ir_get_heap_used_bytes(void) int heapstatus; size_t res = 0; hinfo._pentry = NULL; - while((heapstatus = _heapwalk(&hinfo)) == _HEAPOK) + while ((heapstatus = _heapwalk(&hinfo)) == _HEAPOK) res += hinfo._useflag == _USEDENTRY ? hinfo._size : 0; return res; } @@ -373,7 +341,7 @@ void ir_timer_stop(ir_timer_t *timer) { /* If the timer is running stop, measure the time and add it to the * elapsed time. */ - if(timer->running) { + if (timer->running) { ir_timer_val_t val; ir_timer_val_t tgt; @@ -416,7 +384,7 @@ unsigned long ir_timer_elapsed_msec(const ir_timer_t *timer) ir_timer_val_t v; const ir_timer_val_t *elapsed = &timer->elapsed; - if(timer->running) { + if (timer->running) { elapsed = &v; _time_get(&v); _time_add(&v, &timer->elapsed, _time_sub(&v, &v, &timer->start)); @@ -429,18 +397,10 @@ unsigned long ir_timer_elapsed_usec(const ir_timer_t *timer) ir_timer_val_t v; const ir_timer_val_t *elapsed = &timer->elapsed; - if(timer->running) { + if (timer->running) { elapsed = &v; _time_get(&v); _time_add(&v, &timer->elapsed, _time_sub(&v, &v, &timer->start)); } return _time_to_usec(elapsed); } - -const char *ir_timer_get_name(const ir_timer_t *timer) { - return timer->name; -} - -const char *ir_timer_get_description(const ir_timer_t *timer) { - return timer->desc; -}