1 /* xbt_os_time.c -- portable interface to time-related functions */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/sysdep.h"
10 #include "xbt/xbt_os_time.h" /* this module */
13 #include <math.h> /* floor */
16 #include <sys/timeb.h>
20 double xbt_os_time(void)
22 #ifdef HAVE_GETTIMEOFDAY
24 gettimeofday(&tv, NULL);
25 #elif defined(_XBT_WIN32)
27 # if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
33 tv.tv_usec = tm.millitm * 1000;
39 GetSystemTimeAsFileTime(&ft);
40 tm = (unsigned __int64) ft.dwHighDateTime << 32;
41 tm |= ft.dwLowDateTime;
43 tm -= 11644473600000000ULL;
45 tv.tv_sec = (long) (tm / 1000000L);
46 tv.tv_usec = (long) (tm % 1000000L);
47 # endif /* windows version checker */
49 #else /* not windows, no gettimeofday => poor resolution */
50 return (double) (time(NULL));
51 #endif /* HAVE_GETTIMEOFDAY? */
53 return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
56 void xbt_os_sleep(double sec)
60 Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
65 ts.tv_nsec = (sec - floor(sec)) * 1e9;
66 nanosleep (&ts, NULL);
67 #else /* don't have nanosleep. Use select to sleep less than one second */
68 struct timeval timeout;
70 timeout.tv_sec = (unsigned long) (sec);
71 timeout.tv_usec = (sec - floor(sec)) * 1000000;
73 select(0, NULL, NULL, NULL, &timeout);
77 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
78 disabled in SDL source code */
81 /* \defgroup XBT_sysdep All system dependency
82 * \brief This section describes many macros/functions that can serve as
87 struct s_xbt_os_timer {
88 #ifdef HAVE_POSIX_GETTIME
89 struct timespec start, stop, elapse;
90 #elif defined(HAVE_GETTIMEOFDAY)
91 struct timeval start, stop, elapse;
93 unsigned long int start, stop, elapse;
97 xbt_os_timer_t xbt_os_timer_new(void)
99 return xbt_new0(struct s_xbt_os_timer, 1);
102 void xbt_os_timer_free(xbt_os_timer_t timer)
107 void xbt_os_timer_resume(xbt_os_timer_t timer)
109 #ifdef HAVE_POSIX_GETTIME
110 timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
112 timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
113 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
114 #elif defined(HAVE_GETTIMEOFDAY)
115 timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
117 timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
118 gettimeofday(&(timer->start), NULL);
120 timer->elapse = timer->stop - timer->start;
121 timer->start = (unsigned long int) (time(NULL));
125 void xbt_os_timer_start(xbt_os_timer_t timer)
127 #ifdef HAVE_POSIX_GETTIME
128 timer->elapse.tv_sec = 0;
129 timer->elapse.tv_nsec = 0;
130 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
131 #elif defined(HAVE_GETTIMEOFDAY)
132 timer->elapse.tv_sec = 0;
133 timer->elapse.tv_usec = 0;
134 gettimeofday(&(timer->start), NULL);
137 timer->start = (unsigned long int) (time(NULL));
141 void xbt_os_timer_stop(xbt_os_timer_t timer)
143 #ifdef HAVE_POSIX_GETTIME
144 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
145 #elif defined(HAVE_GETTIMEOFDAY)
146 gettimeofday(&(timer->stop), NULL);
148 timer->stop = (unsigned long int) (time(NULL));
152 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
154 #ifdef HAVE_POSIX_GETTIME
155 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
156 ((double) timer->elapse.tv_sec ) +
157 ((((double) timer->stop.tv_nsec) -
158 ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
159 #elif defined(HAVE_GETTIMEOFDAY)
160 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec)
161 + ((double) timer->elapse.tv_sec ) +
162 ((((double) timer->stop.tv_usec) -
163 ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) / 1000000.0);
165 return (double) timer->stop - (double) timer->start + (double)