Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stringify a bit
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* xbt_os_time.c -- portable interface to time-related functions            */
2
3 /* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "src/internal_config.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/xbt_os_time.h"    /* this module */
12 #include <math.h>               /* floor */
13 #include <sys/time.h>
14 #include <time.h>
15
16 #ifdef _WIN32
17 #include <sys/timeb.h>
18 #include <windows.h>
19 #endif
20
21 //Freebsd doesn't provide this clock_gettime flag yet, because it was added too recently (after 1993)
22 #if defined (CLOCK_PROF) && ! defined (CLOCK_PROCESS_CPUTIME_ID)
23 #define CLOCK_PROCESS_CPUTIME_ID CLOCK_PROF
24 #endif
25
26 #if defined(__APPLE__) && defined(__MACH__)
27 #include <sys/types.h>
28 #include <sys/sysctl.h>
29 #include <mach/mach_init.h>
30 #include <mach/mach_host.h>
31 #include <mach/mach_port.h>
32 #include <mach/mach_traps.h>
33 #include <mach/task_info.h>
34 #include <mach/thread_info.h>
35 #include <mach/thread_act.h>
36 #include <mach/vm_region.h>
37 #include <mach/vm_map.h>
38 #include <mach/task.h>
39 #endif
40
41 double xbt_os_time(void)
42 {
43 #if HAVE_GETTIMEOFDAY
44   struct timeval tv;
45   gettimeofday(&tv, NULL);
46 #elif defined(_WIN32)
47   struct timeval tv;
48   FILETIME ft;
49   unsigned __int64 tm;
50
51   GetSystemTimeAsFileTime(&ft);
52   tm = (unsigned __int64) ft.dwHighDateTime << 32;
53   tm |= ft.dwLowDateTime;
54   tm /= 10;
55   tm -= 11644473600000000ULL;
56
57   tv.tv_sec = (long) (tm / 1000000L);
58   tv.tv_usec = (long) (tm % 1000000L);
59
60 #else                           /* not windows, no gettimeofday => poor resolution */
61   return (double) (time(NULL));
62 #endif                          /* HAVE_GETTIMEOFDAY? */
63
64   return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
65 }
66
67 void xbt_os_sleep(double sec)
68 {
69
70 #ifdef _WIN32
71   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
72
73 #elif HAVE_NANOSLEEP
74   struct timespec ts;
75   ts.tv_sec = sec;
76   ts.tv_nsec = (sec - floor(sec)) * 1e9;
77   nanosleep (&ts, NULL);
78 #else                           /* don't have nanosleep. Use select to sleep less than one second */
79   struct timeval timeout;
80
81   timeout.tv_sec = (unsigned long) (sec);
82   timeout.tv_usec = (sec - floor(sec)) * 1000000;
83
84   select(0, NULL, NULL, NULL, &timeout);
85 #endif
86 }
87
88 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus  disabled in SDL source code */
89
90 /* \defgroup XBT_sysdep All system dependency
91  * \brief This section describes many macros/functions that can serve as  an OS abstraction.
92  */
93 struct s_xbt_os_timer {
94 #if HAVE_POSIX_GETTIME
95   struct timespec start;
96   struct timespec stop;
97   struct timespec elapse;
98 #elif HAVE_GETTIMEOFDAY || defined(_WIN32)
99   struct timeval start;
100   struct timeval stop;
101   struct timeval elapse;
102 #else
103   unsigned long int start;
104   unsigned long int stop;
105   unsigned long int elapse;
106 #endif
107 };
108
109 size_t xbt_os_timer_size(void)
110 {
111   return sizeof(struct s_xbt_os_timer);
112 }
113
114 xbt_os_timer_t xbt_os_timer_new(void)
115 {
116   return xbt_new0(struct s_xbt_os_timer, 1);
117 }
118
119 void xbt_os_timer_free(xbt_os_timer_t timer)
120 {
121   free(timer);
122 }
123
124 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
125 {
126 #if HAVE_POSIX_GETTIME
127   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + ((double) timer->elapse.tv_sec ) +
128       ((((double) timer->stop.tv_nsec) - ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
129 #elif HAVE_GETTIMEOFDAY || defined(_WIN32)
130   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + ((double) timer->elapse.tv_sec ) +
131       ((((double) timer->stop.tv_usec) - ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) /
132          1000000.0);
133 #else
134   return (double) timer->stop - (double) timer->start + (double) timer->elapse;
135 #endif
136 }
137
138 void xbt_os_walltimer_start(xbt_os_timer_t timer)
139 {
140 #if HAVE_POSIX_GETTIME
141   timer->elapse.tv_sec = 0;
142   timer->elapse.tv_nsec = 0;
143   clock_gettime(CLOCK_REALTIME, &(timer->start));
144 #elif HAVE_GETTIMEOFDAY
145   timer->elapse.tv_sec = 0;
146   timer->elapse.tv_usec = 0;
147   gettimeofday(&(timer->start), NULL);
148 #elif defined(_WIN32)
149   timer->elapse.tv_sec = 0;
150   timer->elapse.tv_usec = 0;
151   FILETIME ft;
152   unsigned __int64 tm;
153
154   GetSystemTimeAsFileTime(&ft);
155   tm = (unsigned __int64) ft.dwHighDateTime << 32;
156   tm |= ft.dwLowDateTime;
157   tm /= 10;
158   tm -= 11644473600000000ULL;
159
160   timer->start.tv_sec = (long) (tm / 1000000L);
161   timer->start.tv_usec = (long) (tm % 1000000L);
162 #else
163   timer->elapse = 0;
164   timer->start = (unsigned long int) (time(NULL));
165 #endif
166 }
167
168 void xbt_os_walltimer_resume(xbt_os_timer_t timer)
169 {
170 #if HAVE_POSIX_GETTIME
171   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
172
173    timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
174   clock_gettime(CLOCK_REALTIME, &(timer->start));
175 #elif HAVE_GETTIMEOFDAY
176   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
177   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
178   gettimeofday(&(timer->start), NULL);
179 #elif defined(_WIN32)
180   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
181   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
182   FILETIME ft;
183   unsigned __int64 tm;
184
185   GetSystemTimeAsFileTime(&ft);
186   tm = (unsigned __int64) ft.dwHighDateTime << 32;
187   tm |= ft.dwLowDateTime;
188   tm /= 10;
189   tm -= 11644473600000000ULL;
190
191   timer->start.tv_sec = (long) (tm / 1000000L);
192   timer->start.tv_usec = (long) (tm % 1000000L);
193 #else
194   timer->elapse = timer->stop - timer->start;
195   timer->start = (unsigned long int) (time(NULL));
196 #endif
197 }
198
199 void xbt_os_walltimer_stop(xbt_os_timer_t timer)
200 {
201 #if HAVE_POSIX_GETTIME
202   clock_gettime(CLOCK_REALTIME, &(timer->stop));
203 #elif HAVE_GETTIMEOFDAY
204   gettimeofday(&(timer->stop), NULL);
205 #elif defined(_WIN32)
206   FILETIME ft;
207   unsigned __int64 tm;
208
209   GetSystemTimeAsFileTime(&ft);
210   tm = (unsigned __int64) ft.dwHighDateTime << 32;
211   tm |= ft.dwLowDateTime;
212   tm /= 10;
213   tm -= 11644473600000000ULL;
214
215   timer->stop.tv_sec = (long) (tm / 1000000L);
216   timer->stop.tv_usec = (long) (tm % 1000000L);
217 #else
218   timer->stop = (unsigned long int) (time(NULL));
219 #endif
220 }
221
222 void xbt_os_cputimer_start(xbt_os_timer_t timer)
223 {
224 #if HAVE_POSIX_GETTIME
225   timer->elapse.tv_sec = 0;
226   timer->elapse.tv_nsec = 0;
227   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
228 #elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
229   timer->elapse.tv_sec = 0;
230   timer->elapse.tv_usec = 0;
231   gettimeofday(&(timer->start), NULL);
232 #elif defined(_WIN32)
233   timer->elapse.tv_sec = 0;
234   timer->elapse.tv_usec = 0;
235   HANDLE h = GetCurrentProcess();
236   FILETIME creationTime, exitTime, kernelTime, userTime;
237   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
238   unsigned __int64 ktm, utm;
239   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
240   ktm |= kernelTime.dwLowDateTime;
241   ktm /= 10;
242   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
243   utm |= userTime.dwLowDateTime;
244   utm /= 10;
245   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
246   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
247 #endif
248 }
249
250 void xbt_os_cputimer_resume(xbt_os_timer_t timer)
251 {
252 #if HAVE_POSIX_GETTIME
253   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
254   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
255   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
256 #elif HAVE_GETTIMEOFDAY
257   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
258   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
259   gettimeofday(&(timer->start), NULL);
260 #elif defined(_WIN32)
261   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
262   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
263   HANDLE h = GetCurrentProcess();
264   FILETIME creationTime, exitTime, kernelTime, userTime;
265   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
266   unsigned __int64 ktm, utm;
267   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
268   ktm |= kernelTime.dwLowDateTime;
269   ktm /= 10;
270   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
271   utm |= userTime.dwLowDateTime;
272   utm /= 10;
273   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
274   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
275 #endif
276 }
277
278 void xbt_os_cputimer_stop(xbt_os_timer_t timer)
279 {
280 #if HAVE_POSIX_GETTIME
281   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
282 #elif HAVE_GETTIMEOFDAY
283   gettimeofday(&(timer->stop), NULL);
284 #elif defined(_WIN32)
285   HANDLE h = GetCurrentProcess();
286   FILETIME creationTime, exitTime, kernelTime, userTime;
287   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
288   unsigned __int64 ktm, utm;
289   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
290   ktm |= kernelTime.dwLowDateTime;
291   ktm /= 10;
292   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
293   utm |= userTime.dwLowDateTime;
294   utm /= 10;
295   timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
296   timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
297 #endif
298 }
299
300 void xbt_os_threadtimer_start(xbt_os_timer_t timer)
301 {
302 #if HAVE_POSIX_GETTIME
303   timer->elapse.tv_sec = 0;
304   timer->elapse.tv_nsec = 0;
305   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
306 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
307   timer->elapse.tv_sec = 0;
308   timer->elapse.tv_usec = 0;
309   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
310   thread_basic_info_data_t thi_data;
311   thread_basic_info_t thi = &thi_data;
312   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
313   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
314   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
315 #elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
316   timer->elapse.tv_sec = 0;
317   timer->elapse.tv_usec = 0;
318   gettimeofday(&(timer->start), NULL);
319 #elif defined(_WIN32)
320   HANDLE h = GetCurrentThread();
321   FILETIME creationTime, exitTime, kernelTime, userTime;
322   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
323   unsigned __int64 ktm, utm;
324   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
325   ktm |= kernelTime.dwLowDateTime;
326   ktm /= 10;
327   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
328   utm |= userTime.dwLowDateTime;
329   utm /= 10;
330   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
331   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
332 #endif
333 }
334
335 void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
336 {
337 #if HAVE_POSIX_GETTIME
338   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
339   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
340   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
341 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
342   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
343   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
344   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
345   thread_basic_info_data_t thi_data;
346   thread_basic_info_t thi = &thi_data;
347   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
348   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
349   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
350 #elif HAVE_GETTIMEOFDAY
351   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
352   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
353   gettimeofday(&(timer->start), NULL);
354 #elif defined(_WIN32)
355   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
356   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
357   HANDLE h = GetCurrentThread();
358   FILETIME creationTime, exitTime, kernelTime, userTime;
359   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
360   unsigned __int64 ktm, utm;
361   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
362   ktm |= kernelTime.dwLowDateTime;
363   ktm /= 10;
364   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
365   utm |= userTime.dwLowDateTime;
366   utm /= 10;
367   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
368   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
369 #endif
370 }
371
372 void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
373 {
374 #if HAVE_POSIX_GETTIME
375   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
376 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
377   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
378   thread_basic_info_data_t thi_data;
379   thread_basic_info_t thi = &thi_data;
380   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
381   timer->stop.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
382   timer->stop.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
383 #elif HAVE_GETTIMEOFDAY //if nothing else is available, return just time
384   gettimeofday(&(timer->stop), NULL);
385 #elif defined(_WIN32)
386   HANDLE h = GetCurrentThread();
387   FILETIME creationTime, exitTime, kernelTime, userTime;
388   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
389   unsigned __int64 ktm, utm;
390   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
391   ktm |= kernelTime.dwLowDateTime;
392   ktm /= 10;
393   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
394   utm |= userTime.dwLowDateTime;
395   utm /= 10;
396   timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
397   timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
398 #endif
399 }