From ff4eedb54fbd3d1f757a07681d1318be47512bb2 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 1 Mar 2012 09:44:06 +0100 Subject: [PATCH] Use clock_gettime for a better resolution in timer.h. --- Makefile | 2 +- timer.h | 82 +++++++++++++++++++++----------------------------------- 2 files changed, 31 insertions(+), 53 deletions(-) diff --git a/Makefile b/Makefile index 59afd30..7ec6c5d 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ LDFLAGS += -L $(SIMGRID_INSTALL_DIR)/lib LDFLAGS += -Wl,-rpath,$(SIMGRID_INSTALL_DIR)/lib LINK.o = $(CXX) $(CXXFLAGS) $(LDFLAGS) $(TARGET_ARCH) -LDLIBS := -lsimgrid +LDLIBS := -lsimgrid -lrt MAKEDEPEND.FLAGS = $(CPPFLAGS) -MM -MG -MF $@ $< MAKEDEPEND.C = $(CC) $(CFLAGS) $(MAKEDEPEND.FLAGS) diff --git a/timer.h b/timer.h index 7ddc3ee..52502a1 100644 --- a/timer.h +++ b/timer.h @@ -1,51 +1,31 @@ #ifndef TIMER_H #define TIMER_H -#include -#include - -#ifdef _BSD_SOURCE -# define HAVE_TIMERADD -# define HAVE_TIMERSUB -# define HAVE_TIMERCLEAR -#else -# warning _BSD_SOURCE not defined -# undef HAVE_TIMERADD -# undef HAVE_TIMERSUB -# undef HAVE_TIMERCLEAR -#endif +#include inline -struct timeval operator+(const struct timeval& a, const struct timeval& b) +struct timespec operator+(const struct timespec& a, const struct timespec& b) { - struct timeval result; -#ifdef HAVE_TIMERADD - timeradd(&a, &b, &result); -#else + struct timespec result; result.tv_sec = a.tv_sec + b.tv_sec; - result.tv_usec = a.tv_usec + b.tv_usec; - if (result.tv_usec >= 1000000) { + result.tv_nsec = a.tv_nsec + b.tv_nsec; + if (result.tv_nsec >= 1000000000L) { ++result.tv_sec; - result.tv_usec -= 1000000; + result.tv_nsec -= 1000000000L; } -#endif return result; } inline -struct timeval operator-(const struct timeval& a, const struct timeval& b) +struct timespec operator-(const struct timespec& a, const struct timespec& b) { - struct timeval result; -#ifdef HAVE_TIMERSUB - timersub(&a, &b, &result); -#else + struct timespec result; result.tv_sec = a.tv_sec - b.tv_sec; - result.tv_usec = a.tv_usec - b.tv_usec; - if (result.tv_usec < 0) { + result.tv_nsec = a.tv_nsec - b.tv_nsec; + if (result.tv_nsec < 0) { --result.tv_sec; - result.tv_usec += 1000000; + result.tv_nsec += 1000000000L; } -#endif return result; } @@ -60,19 +40,19 @@ public: void start(); void stop(); - struct timeval tv_duration() const; + struct timespec tv_duration() const; double duration() const; private: clock_type clk; - struct timeval before; - struct timeval after; - struct timeval difference; + struct timespec before; + struct timespec after; + struct timespec difference; - void get_time(struct timeval& tv); + void get_time(struct timespec& tv); - static void tv_clear(struct timeval& a); - static double timertod(const struct timeval& a); + static void tv_clear(struct timespec& a); + static double timertod(const struct timespec& a); }; inline @@ -108,7 +88,7 @@ void timestamp::stop() } inline -struct timeval timestamp::tv_duration() const +struct timespec timestamp::tv_duration() const { return difference; } @@ -120,36 +100,34 @@ double timestamp::duration() const } inline -void timestamp::get_time(struct timeval& tv) +void timestamp::get_time(struct timespec& tv) { switch (clk) { case cpu_time: { - struct rusage usage; - getrusage(RUSAGE_SELF, &usage); - tv = usage.ru_utime + usage.ru_stime; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tv); break; } case wallclock_time: - gettimeofday(&tv, NULL); +#ifdef CLOCK_MONOTONIC_RAW + clock_gettime(CLOCK_MONOTONIC_RAW, &tv); +#else + clock_gettime(CLOCK_MONOTONIC, &tv); +#endif break; } } inline -void timestamp::tv_clear(struct timeval& a) +void timestamp::tv_clear(struct timespec& a) { -#ifdef HAVE_TIMERCLEAR - timerclear(&a); -#else a.tv_sec = 0; - a.tv_usec = 0; -#endif + a.tv_nsec = 0; } inline -double timestamp::timertod(const struct timeval& a) +double timestamp::timertod(const struct timespec& a) { - return a.tv_sec + a.tv_usec / 1e6; + return a.tv_sec + a.tv_nsec / 1e9; } #endif // !TIMER_H -- 2.39.5