Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1027ecf3401271263b119aeb07bdfc4818fa3e12
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* xbt_os_time.c -- portable interface to time-related functions            */
2
3 /* Copyright (c) 2007-2010, 2012-2015. 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 #if defined (CLOCK_PROF) && ! defined (CLOCK_PROCESS_CPUTIME_ID)
22 #define CLOCK_PROCESS_CPUTIME_ID CLOCK_PROF
23 #endif
24
25 #if defined(__APPLE__) && defined(__MACH__)
26 #include <sys/types.h>
27 #include <sys/sysctl.h>
28 #include <mach/mach_init.h>
29 #include <mach/mach_host.h>
30 #include <mach/mach_port.h>
31 #include <mach/mach_traps.h>
32 #include <mach/task_info.h>
33 #include <mach/thread_info.h>
34 #include <mach/thread_act.h>
35 #include <mach/vm_region.h>
36 #include <mach/vm_map.h>
37 #include <mach/task.h>
38 #endif
39
40 double xbt_os_time(void)
41 {
42 #ifdef HAVE_GETTIMEOFDAY
43   struct timeval tv;
44   gettimeofday(&tv, NULL);
45 #elif defined(_XBT_WIN32)
46   struct timeval tv;
47 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
48   struct _timeb tm;
49
50   _ftime(&tm);
51
52   tv.tv_sec = tm.time;
53   tv.tv_usec = tm.millitm * 1000;
54
55 #  else
56   FILETIME ft;
57   unsigned __int64 tm;
58
59   GetSystemTimeAsFileTime(&ft);
60   tm = (unsigned __int64) ft.dwHighDateTime << 32;
61   tm |= ft.dwLowDateTime;
62   tm /= 10;
63   tm -= 11644473600000000ULL;
64
65   tv.tv_sec = (long) (tm / 1000000L);
66   tv.tv_usec = (long) (tm % 1000000L);
67 #  endif                        /* windows version checker */
68
69 #else                           /* not windows, no gettimeofday => poor resolution */
70   return (double) (time(NULL));
71 #endif                          /* HAVE_GETTIMEOFDAY? */
72
73   return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
74 }
75
76 void xbt_os_sleep(double sec)
77 {
78
79 #ifdef _XBT_WIN32
80   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
81
82 #elif HAVE_NANOSLEEP
83   struct timespec ts;
84   ts.tv_sec = sec;
85   ts.tv_nsec = (sec - floor(sec)) * 1e9;
86   nanosleep (&ts, NULL);
87 #else                           /* don't have nanosleep. Use select to sleep less than one second */
88   struct timeval timeout;
89
90   timeout.tv_sec = (unsigned long) (sec);
91   timeout.tv_usec = (sec - floor(sec)) * 1000000;
92
93   select(0, NULL, NULL, NULL, &timeout);
94 #endif
95 }
96
97 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
98    disabled in SDL source code */
99
100
101 /* \defgroup XBT_sysdep All system dependency
102  * \brief This section describes many macros/functions that can serve as
103  *  an OS abstraction.
104  */
105
106
107 struct s_xbt_os_timer {
108 #ifdef HAVE_POSIX_GETTIME
109   struct timespec start, stop, elapse;
110 #elif defined(HAVE_GETTIMEOFDAY) || defined(_XBT_WIN32)
111   struct timeval start, stop, elapse;
112 #else
113   unsigned long int start, stop, elapse;
114 #endif
115 };
116
117 size_t xbt_os_timer_size(void){
118   return sizeof(struct s_xbt_os_timer);
119 }
120
121 xbt_os_timer_t xbt_os_timer_new(void)
122 {
123   return xbt_new0(struct s_xbt_os_timer, 1);
124 }
125
126 void xbt_os_timer_free(xbt_os_timer_t timer)
127 {
128   free(timer);
129 }
130
131 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
132 {
133 #ifdef HAVE_POSIX_GETTIME
134   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
135                                           ((double) timer->elapse.tv_sec ) +
136       ((((double) timer->stop.tv_nsec) -
137         ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
138 #elif defined(HAVE_GETTIMEOFDAY) || defined(_XBT_WIN32)
139   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec)
140     + ((double) timer->elapse.tv_sec ) +
141       ((((double) timer->stop.tv_usec) -
142         ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) / 1000000.0);
143 #else
144   return (double) timer->stop - (double) timer->start + (double)
145     timer->elapse;
146 #endif
147 }
148
149 void xbt_os_walltimer_start(xbt_os_timer_t timer)
150 {
151 #ifdef HAVE_POSIX_GETTIME
152   timer->elapse.tv_sec = 0;
153   timer->elapse.tv_nsec = 0;
154   clock_gettime(CLOCK_REALTIME, &(timer->start));
155 #elif defined(HAVE_GETTIMEOFDAY)
156   timer->elapse.tv_sec = 0;
157   timer->elapse.tv_usec = 0;
158   gettimeofday(&(timer->start), NULL);
159 #elif defined(_XBT_WIN32)
160   timer->elapse.tv_sec = 0;
161   timer->elapse.tv_usec = 0;
162 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
163   struct _timeb tm;
164
165   _ftime(&tm);
166
167   timer->start.tv_sec = tm.time;
168   timer->start.tv_usec = tm.millitm * 1000;
169
170 #  else
171   FILETIME ft;
172   unsigned __int64 tm;
173
174   GetSystemTimeAsFileTime(&ft);
175   tm = (unsigned __int64) ft.dwHighDateTime << 32;
176   tm |= ft.dwLowDateTime;
177   tm /= 10;
178   tm -= 11644473600000000ULL;
179
180   timer->start.tv_sec = (long) (tm / 1000000L);
181   timer->start.tv_usec = (long) (tm % 1000000L);
182 #  endif                        /* windows version checker */
183 #else
184   timer->elapse = 0;
185   timer->start = (unsigned long int) (time(NULL));
186 #endif
187 }
188
189 void xbt_os_walltimer_resume(xbt_os_timer_t timer)
190 {
191 #ifdef HAVE_POSIX_GETTIME
192   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
193
194    timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
195   clock_gettime(CLOCK_REALTIME, &(timer->start));
196 #elif defined(HAVE_GETTIMEOFDAY)
197   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
198   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
199   gettimeofday(&(timer->start), NULL);
200 #elif defined(_XBT_WIN32)
201   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
202   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
203 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
204   struct _timeb tm;
205
206   _ftime(&tm);
207
208   timer->start.tv_sec = tm.time;
209   timer->start.tv_usec = tm.millitm * 1000;
210
211 #  else
212   FILETIME ft;
213   unsigned __int64 tm;
214
215   GetSystemTimeAsFileTime(&ft);
216   tm = (unsigned __int64) ft.dwHighDateTime << 32;
217   tm |= ft.dwLowDateTime;
218   tm /= 10;
219   tm -= 11644473600000000ULL;
220
221   timer->start.tv_sec = (long) (tm / 1000000L);
222   timer->start.tv_usec = (long) (tm % 1000000L);
223 #  endif
224 #else
225   timer->elapse = timer->stop - timer->start;
226   timer->start = (unsigned long int) (time(NULL));
227 #endif
228 }
229
230 void xbt_os_walltimer_stop(xbt_os_timer_t timer)
231 {
232 #ifdef HAVE_POSIX_GETTIME
233   clock_gettime(CLOCK_REALTIME, &(timer->stop));
234 #elif defined(HAVE_GETTIMEOFDAY)
235   gettimeofday(&(timer->stop), NULL);
236 #elif defined(_XBT_WIN32)
237 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
238   struct _timeb tm;
239
240   _ftime(&tm);
241
242   timer->stop.tv_sec = tm.time;
243   timer->stop.tv_usec = tm.millitm * 1000;
244
245 #  else
246   FILETIME ft;
247   unsigned __int64 tm;
248
249   GetSystemTimeAsFileTime(&ft);
250   tm = (unsigned __int64) ft.dwHighDateTime << 32;
251   tm |= ft.dwLowDateTime;
252   tm /= 10;
253   tm -= 11644473600000000ULL;
254
255   timer->stop.tv_sec = (long) (tm / 1000000L);
256   timer->stop.tv_usec = (long) (tm % 1000000L);
257 #  endif
258 #else
259   timer->stop = (unsigned long int) (time(NULL));
260 #endif
261 }
262
263 void xbt_os_cputimer_start(xbt_os_timer_t timer)
264 {
265 #ifdef HAVE_POSIX_GETTIME
266   timer->elapse.tv_sec = 0;
267   timer->elapse.tv_nsec = 0;
268   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
269 #elif defined(HAVE_GETTIMEOFDAY)//return time and not cputime in this case
270   timer->elapse.tv_sec = 0;
271   timer->elapse.tv_usec = 0;
272   gettimeofday(&(timer->start), NULL);
273 #elif defined(_XBT_WIN32)
274   timer->elapse.tv_sec = 0;
275   timer->elapse.tv_usec = 0;
276 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
277   THROW_UNIMPLEMENTED;
278 #  else
279   HANDLE h = GetCurrentProcess();
280   FILETIME creationTime, exitTime, kernelTime, userTime;
281   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
282   unsigned __int64 ktm, utm;
283   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
284   ktm |= kernelTime.dwLowDateTime;
285   ktm /= 10;
286   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
287   utm |= userTime.dwLowDateTime;
288   utm /= 10;
289   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
290   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
291 #  endif                        /* windows version checker */
292 #endif
293 }
294
295 void xbt_os_cputimer_resume(xbt_os_timer_t timer)
296 {
297 #ifdef HAVE_POSIX_GETTIME
298   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
299   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
300   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
301 #elif defined(HAVE_GETTIMEOFDAY)
302   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
303   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
304   gettimeofday(&(timer->start), NULL);
305 #elif defined(_XBT_WIN32)
306   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
307   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
308 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
309   THROW_UNIMPLEMENTED;
310 #  else
311   HANDLE h = GetCurrentProcess();
312   FILETIME creationTime, exitTime, kernelTime, userTime;
313   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
314   unsigned __int64 ktm, utm;
315   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
316   ktm |= kernelTime.dwLowDateTime;
317   ktm /= 10;
318   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
319   utm |= userTime.dwLowDateTime;
320   utm /= 10;
321   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
322   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
323 #  endif                        /* windows version checker */
324
325 #endif
326 }
327
328 void xbt_os_cputimer_stop(xbt_os_timer_t timer)
329 {
330 #ifdef HAVE_POSIX_GETTIME
331   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
332 #elif defined(HAVE_GETTIMEOFDAY)
333   gettimeofday(&(timer->stop), NULL);
334 #elif defined(_XBT_WIN32)
335 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
336   THROW_UNIMPLEMENTED;
337 #  else
338   HANDLE h = GetCurrentProcess();
339   FILETIME creationTime, exitTime, kernelTime, userTime;
340   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
341   unsigned __int64 ktm, utm;
342   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
343   ktm |= kernelTime.dwLowDateTime;
344   ktm /= 10;
345   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
346   utm |= userTime.dwLowDateTime;
347   utm /= 10;
348   timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
349   timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
350 #  endif                        /* windows version checker */
351 #endif
352 }
353
354 void xbt_os_threadtimer_start(xbt_os_timer_t timer)
355 {
356 #ifdef HAVE_POSIX_GETTIME
357   timer->elapse.tv_sec = 0;
358   timer->elapse.tv_nsec = 0;
359   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
360 #elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)//attempt for timing of the thread on OSX
361   timer->elapse.tv_sec = 0;
362   timer->elapse.tv_usec = 0;
363   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
364   thread_basic_info_data_t thi_data;
365   thread_basic_info_t thi = &thi_data;
366   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
367   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
368   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
369 #elif defined(HAVE_GETTIMEOFDAY)//return time and not cputime in this case
370   timer->elapse.tv_sec = 0;
371   timer->elapse.tv_usec = 0;
372   gettimeofday(&(timer->start), NULL);
373 #elif defined(_XBT_WIN32)
374   struct timeval tv;
375 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
376   THROW_UNIMPLEMENTED;
377 #  else
378   HANDLE h = GetCurrentThread();
379   FILETIME creationTime, exitTime, kernelTime, userTime;
380   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
381   unsigned __int64 ktm, utm;
382   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
383   ktm |= kernelTime.dwLowDateTime;
384   ktm /= 10;
385   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
386   utm |= userTime.dwLowDateTime;
387   utm /= 10;
388   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
389   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
390 #  endif                        /* windows version checker */
391 #endif
392 }
393
394 void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
395 {
396 #ifdef HAVE_POSIX_GETTIME
397   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
398   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
399   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
400 #elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
401   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
402   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
403   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
404   thread_basic_info_data_t thi_data;
405   thread_basic_info_t thi = &thi_data;
406   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
407   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
408   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
409 #elif defined(HAVE_GETTIMEOFDAY)
410   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
411   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
412   gettimeofday(&(timer->start), NULL);
413 #elif defined(_XBT_WIN32)
414   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
415   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
416 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
417   THROW_UNIMPLEMENTED;
418 #  else
419   HANDLE h = GetCurrentThread();
420   FILETIME creationTime, exitTime, kernelTime, userTime;
421   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
422   unsigned __int64 ktm, utm;
423   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
424   ktm |= kernelTime.dwLowDateTime;
425   ktm /= 10;
426   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
427   utm |= userTime.dwLowDateTime;
428   utm /= 10;
429   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
430   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
431 #  endif                        /* windows version checker */
432 #endif
433 }
434
435 void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
436 {
437 #ifdef HAVE_POSIX_GETTIME
438   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
439 #elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
440   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
441   thread_basic_info_data_t thi_data;
442   thread_basic_info_t thi = &thi_data;
443   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
444   timer->stop.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
445   timer->stop.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
446 #elif defined(HAVE_GETTIMEOFDAY)//if nothing else is available, return just time
447   gettimeofday(&(timer->stop), NULL);
448 #elif defined(_XBT_WIN32)
449 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
450   THROW_UNIMPLEMENTED;
451 #  else
452   HANDLE h = GetCurrentThread();
453   FILETIME creationTime, exitTime, kernelTime, userTime;
454   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
455   unsigned __int64 ktm, utm;
456   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
457   ktm |= kernelTime.dwLowDateTime;
458   ktm /= 10;
459   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
460   utm |= userTime.dwLowDateTime;
461   utm /= 10;
462   timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
463   timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
464 #  endif                        /* windows version checker */
465 #endif
466 }