Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compare file prefix only.
[simgrid.git] / src / xbt / xbt_os_time.c
index 836bd17..0e8951f 100644 (file)
@@ -1,16 +1,17 @@
 /* xbt_os_time.c -- portable interface to time-related functions            */
 
-/* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include "src/internal_config.h"
 #include "xbt/sysdep.h"
-#include "xbt/xbt_os_time.h"    /* this module */
 #include "xbt/log.h"
-#include "src/portable.h"
+#include "xbt/xbt_os_time.h"    /* this module */
 #include <math.h>               /* floor */
+#include <sys/time.h>
+#include <time.h>
 
 #ifdef _WIN32
 #include <sys/timeb.h>
@@ -39,7 +40,7 @@
 
 double xbt_os_time(void)
 {
-#ifdef HAVE_GETTIMEOFDAY
+#if HAVE_GETTIMEOFDAY
   struct timeval tv;
   gettimeofday(&tv, NULL);
 #elif defined(_WIN32)
@@ -84,27 +85,29 @@ void xbt_os_sleep(double sec)
 #endif
 }
 
-/* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
-   disabled in SDL source code */
-
+/* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus  disabled in SDL source code */
 
 /* \defgroup XBT_sysdep All system dependency
- * \brief This section describes many macros/functions that can serve as
- *  an OS abstraction.
+ * \brief This section describes many macros/functions that can serve as  an OS abstraction.
  */
-
-
 struct s_xbt_os_timer {
-#ifdef HAVE_POSIX_GETTIME
-  struct timespec start, stop, elapse;
-#elif defined(HAVE_GETTIMEOFDAY) || defined(_WIN32)
-  struct timeval start, stop, elapse;
+#if HAVE_POSIX_GETTIME
+  struct timespec start;
+  struct timespec stop;
+  struct timespec elapse;
+#elif HAVE_GETTIMEOFDAY || defined(_WIN32)
+  struct timeval start;
+  struct timeval stop;
+  struct timeval elapse;
 #else
-  unsigned long int start, stop, elapse;
+  unsigned long int start;
+  unsigned long int stop;
+  unsigned long int elapse;
 #endif
 };
 
-size_t xbt_os_timer_size(void){
+size_t xbt_os_timer_size(void)
+{
   return sizeof(struct s_xbt_os_timer);
 }
 
@@ -120,29 +123,25 @@ void xbt_os_timer_free(xbt_os_timer_t timer)
 
 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) + ((double) timer->elapse.tv_nsec )) / 1e9);
-#elif defined(HAVE_GETTIMEOFDAY) || defined(_WIN32)
-  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) + ((double) timer->elapse.tv_usec )) / 1000000.0);
+#if 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) + ((double) timer->elapse.tv_nsec )) / 1e9);
+#elif HAVE_GETTIMEOFDAY || defined(_WIN32)
+  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) + ((double) timer->elapse.tv_usec )) /
+         1000000.0);
 #else
-  return (double) timer->stop - (double) timer->start + (double)
-    timer->elapse;
+  return (double) timer->stop - (double) timer->start + (double) timer->elapse;
 #endif
 }
 
 void xbt_os_walltimer_start(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
   timer->elapse.tv_sec = 0;
   timer->elapse.tv_nsec = 0;
   clock_gettime(CLOCK_REALTIME, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
   timer->elapse.tv_sec = 0;
   timer->elapse.tv_usec = 0;
   gettimeofday(&(timer->start), NULL);
@@ -168,12 +167,12 @@ void xbt_os_walltimer_start(xbt_os_timer_t timer)
 
 void xbt_os_walltimer_resume(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if 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_REALTIME, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif 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);
@@ -199,9 +198,9 @@ void xbt_os_walltimer_resume(xbt_os_timer_t timer)
 
 void xbt_os_walltimer_stop(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
   clock_gettime(CLOCK_REALTIME, &(timer->stop));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
   gettimeofday(&(timer->stop), NULL);
 #elif defined(_WIN32)
   FILETIME ft;
@@ -222,11 +221,11 @@ void xbt_os_walltimer_stop(xbt_os_timer_t timer)
 
 void xbt_os_cputimer_start(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if 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)//return time and not cputime in this case
+#elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
   timer->elapse.tv_sec = 0;
   timer->elapse.tv_usec = 0;
   gettimeofday(&(timer->start), NULL);
@@ -250,11 +249,11 @@ void xbt_os_cputimer_start(xbt_os_timer_t timer)
 
 void xbt_os_cputimer_resume(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if 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)
+#elif 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);
@@ -278,9 +277,9 @@ void xbt_os_cputimer_resume(xbt_os_timer_t timer)
 
 void xbt_os_cputimer_stop(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
   gettimeofday(&(timer->stop), NULL);
 #elif defined(_WIN32)
   HANDLE h = GetCurrentProcess();
@@ -300,11 +299,11 @@ void xbt_os_cputimer_stop(xbt_os_timer_t timer)
 
 void xbt_os_threadtimer_start(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
   timer->elapse.tv_sec = 0;
   timer->elapse.tv_nsec = 0;
   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
+#elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
   timer->elapse.tv_sec = 0;
   timer->elapse.tv_usec = 0;
   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
@@ -313,7 +312,7 @@ void xbt_os_threadtimer_start(xbt_os_timer_t timer)
   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
-#elif defined(HAVE_GETTIMEOFDAY)//return time and not cputime in this case
+#elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
   timer->elapse.tv_sec = 0;
   timer->elapse.tv_usec = 0;
   gettimeofday(&(timer->start), NULL);
@@ -335,11 +334,11 @@ void xbt_os_threadtimer_start(xbt_os_timer_t timer)
 
 void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if 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_THREAD_CPUTIME_ID, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
+#elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
@@ -348,7 +347,7 @@ void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif 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);
@@ -372,16 +371,16 @@ void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
 
 void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
 {
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
-#elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
+#elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
   thread_basic_info_data_t thi_data;
   thread_basic_info_t thi = &thi_data;
   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
   timer->stop.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
   timer->stop.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
-#elif defined(HAVE_GETTIMEOFDAY)//if nothing else is available, return just time
+#elif HAVE_GETTIMEOFDAY //if nothing else is available, return just time
   gettimeofday(&(timer->stop), NULL);
 #elif defined(_WIN32)
   HANDLE h = GetCurrentThread();