Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* xbt_os_time.c -- portable interface to time-related functions            */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/xbt_os_time.h"    /* this module */
11 #include "xbt/log.h"
12 #include "portable.h"
13 #include <math.h>               /* floor */
14
15 #ifdef _XBT_WIN32
16 #include <sys/timeb.h>
17 #include <windows.h>
18 #endif
19
20 //Freebsd doesn't provide this clock_gettime flag yet, because it was added too recently (after 1993)
21 #ifdef __FreeBSD__
22 #define CLOCK_PROCESS_CPUTTIME_ID CLOCK_PROF
23 #endif
24
25 double xbt_os_time(void)
26 {
27 #ifdef HAVE_GETTIMEOFDAY
28   struct timeval tv;
29   gettimeofday(&tv, NULL);
30 #elif defined(_XBT_WIN32)
31   struct timeval tv;
32 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
33   struct _timeb tm;
34
35   _ftime(&tm);
36
37   tv.tv_sec = tm.time;
38   tv.tv_usec = tm.millitm * 1000;
39
40 #  else
41   FILETIME ft;
42   unsigned __int64 tm;
43
44   GetSystemTimeAsFileTime(&ft);
45   tm = (unsigned __int64) ft.dwHighDateTime << 32;
46   tm |= ft.dwLowDateTime;
47   tm /= 10;
48   tm -= 11644473600000000ULL;
49
50   tv.tv_sec = (long) (tm / 1000000L);
51   tv.tv_usec = (long) (tm % 1000000L);
52 #  endif                        /* windows version checker */
53
54 #else                           /* not windows, no gettimeofday => poor resolution */
55   return (double) (time(NULL));
56 #endif                          /* HAVE_GETTIMEOFDAY? */
57
58   return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
59 }
60
61 void xbt_os_sleep(double sec)
62 {
63
64 #ifdef _XBT_WIN32
65   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
66
67 #elif HAVE_NANOSLEEP
68   struct timespec ts;
69   ts.tv_sec = sec;
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;
74
75   timeout.tv_sec = (unsigned long) (sec);
76   timeout.tv_usec = (sec - floor(sec)) * 1000000;
77
78   select(0, NULL, NULL, NULL, &timeout);
79 #endif
80 }
81
82 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
83    disabled in SDL source code */
84
85
86 /* \defgroup XBT_sysdep All system dependency
87  * \brief This section describes many macros/functions that can serve as
88  *  an OS abstraction.
89  */
90
91
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;
97 #else
98   unsigned long int start, stop, elapse;
99 #endif
100 };
101
102 xbt_os_timer_t xbt_os_timer_new(void)
103 {
104   return xbt_new0(struct s_xbt_os_timer, 1);
105 }
106
107 void xbt_os_timer_free(xbt_os_timer_t timer)
108 {
109   free(timer);
110 }
111
112 void xbt_os_timer_resume(xbt_os_timer_t timer)
113 {
114 #ifdef HAVE_POSIX_GETTIME
115   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
116
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;
121
122    timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
123   gettimeofday(&(timer->start), NULL);
124 #else
125   timer->elapse = timer->stop - timer->start;
126   timer->start = (unsigned long int) (time(NULL));
127 #endif
128 }
129
130 void xbt_os_timer_start(xbt_os_timer_t timer)
131 {
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);
140 #else
141   timer->elapse = 0;
142   timer->start = (unsigned long int) (time(NULL));
143 #endif
144 }
145
146 void xbt_os_timer_stop(xbt_os_timer_t timer)
147 {
148 #ifdef HAVE_POSIX_GETTIME
149   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
150 #elif defined(HAVE_GETTIMEOFDAY)
151   gettimeofday(&(timer->stop), NULL);
152 #else
153   timer->stop = (unsigned long int) (time(NULL));
154 #endif
155 }
156
157 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
158 {
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);
169 #else
170   return (double) timer->stop - (double) timer->start + (double)
171     timer->elapse;
172 #endif
173 }