Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function: xbt_os_timer_resume() to restart a timer w/o resetting it.
authorMartin Quinson <martin.quinson@loria.fr>
Sun, 22 Jul 2012 09:33:07 +0000 (11:33 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Sun, 22 Jul 2012 09:33:07 +0000 (11:33 +0200)
Patch from Jean-Noel Quintin, thanks for that dude.

ChangeLog
src/include/xbt/xbt_os_time.h
src/xbt/xbt_os_time.c

index 579b0d8..3ccbce5 100644 (file)
--- 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().
  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 <simgrid-devel@lists.gforge.inria.fr>
 
 
  -- $date Da SimGrid team <simgrid-devel@lists.gforge.inria.fr>
 
index ab5d51f..3498cec 100644 (file)
@@ -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(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);
 
 XBT_PUBLIC(void) xbt_os_timer_stop(xbt_os_timer_t timer);
 XBT_PUBLIC(double) xbt_os_timer_elapsed(xbt_os_timer_t timer);
 
index dc72c91..0f17fcc 100644 (file)
@@ -86,11 +86,11 @@ void xbt_os_sleep(double sec)
 
 struct s_xbt_os_timer {
 #ifdef HAVE_POSIX_GETTIME
 
 struct s_xbt_os_timer {
 #ifdef HAVE_POSIX_GETTIME
-  struct timespec start, stop;
+  struct timespec start, stop, elapse;
 #elif defined(HAVE_GETTIMEOFDAY)
 #elif defined(HAVE_GETTIMEOFDAY)
-  struct timeval start, stop;
+  struct timeval start, stop, elapse;
 #else
 #else
-  unsigned long int start, stop;
+  unsigned long int start, stop, elapse;
 #endif
 };
 
 #endif
 };
 
@@ -104,13 +104,36 @@ void xbt_os_timer_free(xbt_os_timer_t timer)
   free(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
 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)
   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
   gettimeofday(&(timer->start), NULL);
 #else
+  timer->elapse = 0;
   timer->start = (unsigned long int) (time(NULL));
 #endif
 }
   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) +
 {
 #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->stop.tv_nsec) -
-        ((double) timer->start.tv_nsec)) / 1e9);
+        ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
 #elif defined(HAVE_GETTIMEOFDAY)
 #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->stop.tv_usec) -
-        ((double) timer->start.tv_usec)) / 1000000.0);
+        ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) / 1000000.0);
 #else
 #else
-  return (double) timer->stop - (double) timer->start;
+  return (double) timer->stop - (double) timer->start + (double)
+    timer->elapse;
 #endif
 }
 #endif
 }