From cb2925f9e27482e531d327f9e1e19550c5966457 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 22 Jul 2012 11:33:07 +0200 Subject: [PATCH] New function: xbt_os_timer_resume() to restart a timer w/o resetting it. Patch from Jean-Noel Quintin, thanks for that dude. --- ChangeLog | 1 + src/include/xbt/xbt_os_time.h | 1 + src/xbt/xbt_os_time.c | 40 +++++++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 579b0d8319..3ccbce5f43 100644 --- a/ChangeLog +++ b/ChangeLog @@ -60,6 +60,7 @@ SimGrid (3.8) NOT RELEASED; urgency=low XBT: * Functions xbt_dict_hash() and xbt_dict_hash_ext() are made public, and renamed to xbt_str_hash() and xbt_str_hash_ext(). + * New function: xbt_os_timer_resume() to restart a timer w/o resetting it. -- $date Da SimGrid team diff --git a/src/include/xbt/xbt_os_time.h b/src/include/xbt/xbt_os_time.h index ab5d51f56d..3498cecd35 100644 --- a/src/include/xbt/xbt_os_time.h +++ b/src/include/xbt/xbt_os_time.h @@ -24,6 +24,7 @@ typedef struct s_xbt_os_timer *xbt_os_timer_t; XBT_PUBLIC(xbt_os_timer_t) xbt_os_timer_new(void); XBT_PUBLIC(void) xbt_os_timer_free(xbt_os_timer_t timer); XBT_PUBLIC(void) xbt_os_timer_start(xbt_os_timer_t timer); +XBT_PUBLIC(void) xbt_os_timer_resume(xbt_os_timer_t timer); XBT_PUBLIC(void) xbt_os_timer_stop(xbt_os_timer_t timer); XBT_PUBLIC(double) xbt_os_timer_elapsed(xbt_os_timer_t timer); diff --git a/src/xbt/xbt_os_time.c b/src/xbt/xbt_os_time.c index dc72c91b13..0f17fcc9a6 100644 --- a/src/xbt/xbt_os_time.c +++ b/src/xbt/xbt_os_time.c @@ -86,11 +86,11 @@ void xbt_os_sleep(double sec) struct s_xbt_os_timer { #ifdef HAVE_POSIX_GETTIME - struct timespec start, stop; + struct timespec start, stop, elapse; #elif defined(HAVE_GETTIMEOFDAY) - struct timeval start, stop; + struct timeval start, stop, elapse; #else - unsigned long int start, stop; + unsigned long int start, stop, elapse; #endif }; @@ -104,13 +104,36 @@ void xbt_os_timer_free(xbt_os_timer_t timer) free(timer); } +void xbt_os_timer_resume(xbt_os_timer_t timer) +{ +#ifdef HAVE_POSIX_GETTIME + timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec; + + timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start)); +#elif defined(HAVE_GETTIMEOFDAY) + timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec; + + timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec; + gettimeofday(&(timer->start), NULL); +#else + timer->elapse = timer->stop - timer->start; + timer->start = (unsigned long int) (time(NULL)); +#endif +} + void xbt_os_timer_start(xbt_os_timer_t timer) { #ifdef HAVE_POSIX_GETTIME + timer->elapse.tv_sec = 0; + timer->elapse.tv_nsec = 0; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start)); #elif defined(HAVE_GETTIMEOFDAY) + timer->elapse.tv_sec = 0; + timer->elapse.tv_usec = 0; gettimeofday(&(timer->start), NULL); #else + timer->elapse = 0; timer->start = (unsigned long int) (time(NULL)); #endif } @@ -130,13 +153,16 @@ double xbt_os_timer_elapsed(xbt_os_timer_t timer) { #ifdef HAVE_POSIX_GETTIME return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + + ((double) timer->elapse.tv_sec ) + ((((double) timer->stop.tv_nsec) - - ((double) timer->start.tv_nsec)) / 1e9); + ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9); #elif defined(HAVE_GETTIMEOFDAY) - return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + + return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + + ((double) timer->elapse.tv_sec ) + ((((double) timer->stop.tv_usec) - - ((double) timer->start.tv_usec)) / 1000000.0); + ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) / 1000000.0); #else - return (double) timer->stop - (double) timer->start; + return (double) timer->stop - (double) timer->start + (double) + timer->elapse; #endif } -- 2.20.1