#include "bench.h" /* * There _must_ be something better than rusage.ru_utime with a higher * resolution ... * * Linux isn't using rusage.ru_nvcsw and rusage.ru_nivcsw, which might * have the highest resolution we could get for this ... */ #include #include #include #define TIMER_DEBUG 0 long timer_buffer[CONTEXT_NR]; long timer_value[CONTEXT_NR]; long timer_current() { struct rusage u1, u2; getrusage(RUSAGE_SELF, &u1); getrusage(RUSAGE_CHILDREN, &u2); #if TIMER_DEBUG fprintf(stderr, "\nTIMER: utime1 = (%ld, %ld) -> (%ld)", u1.ru_utime.tv_sec, u1.ru_utime.tv_usec, (u1.ru_utime.tv_sec * 100) + (u1.ru_utime.tv_usec / 10000)); fprintf(stderr, "\nTIMER: utime2 = (%ld, %ld) -> (%ld)", u2.ru_utime.tv_sec, u2.ru_utime.tv_usec, (u2.ru_utime.tv_sec * 100) + (u2.ru_utime.tv_usec / 10000)); #endif return ((u1.ru_utime.tv_sec + u2.ru_utime.tv_sec) * 100) + ((u1.ru_utime.tv_usec + u2.ru_utime.tv_usec) / 10000); } void timer_start(int context) { timer_buffer[context] = timer_current(); } void timer_stop(int context) { timer_value[context] = timer_current() - timer_buffer[context]; } long timer_reset(int context) { #if TIMER_DEBUG fprintf(stderr, "\nTIMER: result = %-32ld", timer_value[context]); #endif timer_buffer[context] = timer_value[context]; timer_value[context] = 0; return timer_buffer[context]; }