Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Apparently, I'm not allowed to free the memory here
[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 double xbt_os_time(void)
21 {
22 #ifdef HAVE_GETTIMEOFDAY
23   struct timeval tv;
24   gettimeofday(&tv, NULL);
25 #elif defined(_XBT_WIN32)
26   struct timeval tv;
27 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
28   struct _timeb tm;
29
30   _ftime(&tm);
31
32   tv.tv_sec = tm.time;
33   tv.tv_usec = tm.millitm * 1000;
34
35 #  else
36   FILETIME ft;
37   unsigned __int64 tm;
38
39   GetSystemTimeAsFileTime(&ft);
40   tm = (unsigned __int64) ft.dwHighDateTime << 32;
41   tm |= ft.dwLowDateTime;
42   tm /= 10;
43   tm -= 11644473600000000ULL;
44
45   tv.tv_sec = (long) (tm / 1000000L);
46   tv.tv_usec = (long) (tm % 1000000L);
47 #  endif                        /* windows version checker */
48
49 #else                           /* not windows, no gettimeofday => poor resolution */
50   return (double) (time(NULL));
51 #endif                          /* HAVE_GETTIMEOFDAY? */
52
53   return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
54 }
55
56 void xbt_os_sleep(double sec)
57 {
58
59 #ifdef _XBT_WIN32
60   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
61
62 #elif HAVE_NANOSLEEP
63   struct timespec ts;
64   ts.tv_sec = sec;
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;
69
70   timeout.tv_sec = (unsigned long) (sec);
71   timeout.tv_usec = (sec - floor(sec)) * 1000000;
72
73   select(0, NULL, NULL, NULL, &timeout);
74 #endif
75 }
76
77 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
78    disabled in SDL source code */
79
80
81 /* \defgroup XBT_sysdep All system dependency
82  * \brief This section describes many macros/functions that can serve as
83  *  an OS abstraction.
84  */
85
86
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;
92 #else
93   unsigned long int start, stop, elapse;
94 #endif
95 };
96
97 xbt_os_timer_t xbt_os_timer_new(void)
98 {
99   return xbt_new0(struct s_xbt_os_timer, 1);
100 }
101
102 void xbt_os_timer_free(xbt_os_timer_t timer)
103 {
104   free(timer);
105 }
106
107 void xbt_os_timer_resume(xbt_os_timer_t timer)
108 {
109 #ifdef HAVE_POSIX_GETTIME
110   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
111
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;
116
117    timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
118   gettimeofday(&(timer->start), NULL);
119 #else
120   timer->elapse = timer->stop - timer->start;
121   timer->start = (unsigned long int) (time(NULL));
122 #endif
123 }
124
125 void xbt_os_timer_start(xbt_os_timer_t timer)
126 {
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);
135 #else
136   timer->elapse = 0;
137   timer->start = (unsigned long int) (time(NULL));
138 #endif
139 }
140
141 void xbt_os_timer_stop(xbt_os_timer_t timer)
142 {
143 #ifdef HAVE_POSIX_GETTIME
144   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
145 #elif defined(HAVE_GETTIMEOFDAY)
146   gettimeofday(&(timer->stop), NULL);
147 #else
148   timer->stop = (unsigned long int) (time(NULL));
149 #endif
150 }
151
152 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
153 {
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);
164 #else
165   return (double) timer->stop - (double) timer->start + (double)
166     timer->elapse;
167 #endif
168 }