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 //Freebsd doesn't provide this clock_gettime flag yet, because it was added too recently (after 1993)
22 #define CLOCK_PROCESS_CPUTTIME_ID CLOCK_PROF
25 double xbt_os_time(void)
27 #ifdef HAVE_GETTIMEOFDAY
29 gettimeofday(&tv, NULL);
30 #elif defined(_XBT_WIN32)
32 # if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
38 tv.tv_usec = tm.millitm * 1000;
44 GetSystemTimeAsFileTime(&ft);
45 tm = (unsigned __int64) ft.dwHighDateTime << 32;
46 tm |= ft.dwLowDateTime;
48 tm -= 11644473600000000ULL;
50 tv.tv_sec = (long) (tm / 1000000L);
51 tv.tv_usec = (long) (tm % 1000000L);
52 # endif /* windows version checker */
54 #else /* not windows, no gettimeofday => poor resolution */
55 return (double) (time(NULL));
56 #endif /* HAVE_GETTIMEOFDAY? */
58 return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
61 void xbt_os_sleep(double sec)
65 Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
70 ts.tv_nsec = (sec - floor(sec)) * 1e9;
71 nanosleep (&ts, NULL);
72 #else /* don't have nanosleep. Use select to sleep less than one second */
73 struct timeval timeout;
75 timeout.tv_sec = (unsigned long) (sec);
76 timeout.tv_usec = (sec - floor(sec)) * 1000000;
78 select(0, NULL, NULL, NULL, &timeout);
82 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
83 disabled in SDL source code */
86 /* \defgroup XBT_sysdep All system dependency
87 * \brief This section describes many macros/functions that can serve as
92 struct s_xbt_os_timer {
93 #ifdef HAVE_POSIX_GETTIME
94 struct timespec start, stop, elapse;
95 #elif defined(HAVE_GETTIMEOFDAY)
96 struct timeval start, stop, elapse;
98 unsigned long int start, stop, elapse;
102 xbt_os_timer_t xbt_os_timer_new(void)
104 return xbt_new0(struct s_xbt_os_timer, 1);
107 void xbt_os_timer_free(xbt_os_timer_t timer)
112 void xbt_os_timer_resume(xbt_os_timer_t timer)
114 #ifdef HAVE_POSIX_GETTIME
115 timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
117 timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
118 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
119 #elif defined(HAVE_GETTIMEOFDAY)
120 timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
122 timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
123 gettimeofday(&(timer->start), NULL);
125 timer->elapse = timer->stop - timer->start;
126 timer->start = (unsigned long int) (time(NULL));
130 void xbt_os_timer_start(xbt_os_timer_t timer)
132 #ifdef HAVE_POSIX_GETTIME
133 timer->elapse.tv_sec = 0;
134 timer->elapse.tv_nsec = 0;
135 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
136 #elif defined(HAVE_GETTIMEOFDAY)
137 timer->elapse.tv_sec = 0;
138 timer->elapse.tv_usec = 0;
139 gettimeofday(&(timer->start), NULL);
142 timer->start = (unsigned long int) (time(NULL));
146 void xbt_os_timer_stop(xbt_os_timer_t timer)
148 #ifdef HAVE_POSIX_GETTIME
149 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
150 #elif defined(HAVE_GETTIMEOFDAY)
151 gettimeofday(&(timer->stop), NULL);
153 timer->stop = (unsigned long int) (time(NULL));
157 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
159 #ifdef HAVE_POSIX_GETTIME
160 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
161 ((double) timer->elapse.tv_sec ) +
162 ((((double) timer->stop.tv_nsec) -
163 ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
164 #elif defined(HAVE_GETTIMEOFDAY)
165 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec)
166 + ((double) timer->elapse.tv_sec ) +
167 ((((double) timer->stop.tv_usec) -
168 ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) / 1000000.0);
170 return (double) timer->stop - (double) timer->start + (double)