Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move surf::As to s4u::As
[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 "src/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 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
375   THROW_UNIMPLEMENTED;
376 #  else
377   HANDLE h = GetCurrentThread();
378   FILETIME creationTime, exitTime, kernelTime, userTime;
379   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
380   unsigned __int64 ktm, utm;
381   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
382   ktm |= kernelTime.dwLowDateTime;
383   ktm /= 10;
384   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
385   utm |= userTime.dwLowDateTime;
386   utm /= 10;
387   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
388   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
389 #  endif                        /* windows version checker */
390 #endif
391 }
392
393 void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
394 {
395 #ifdef HAVE_POSIX_GETTIME
396   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
397   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
398   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
399 #elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
400   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
401   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
402   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
403   thread_basic_info_data_t thi_data;
404   thread_basic_info_t thi = &thi_data;
405   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
406   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
407   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
408 #elif defined(HAVE_GETTIMEOFDAY)
409   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
410   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
411   gettimeofday(&(timer->start), NULL);
412 #elif defined(_XBT_WIN32)
413   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
414   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
415 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
416   THROW_UNIMPLEMENTED;
417 #  else
418   HANDLE h = GetCurrentThread();
419   FILETIME creationTime, exitTime, kernelTime, userTime;
420   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
421   unsigned __int64 ktm, utm;
422   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
423   ktm |= kernelTime.dwLowDateTime;
424   ktm /= 10;
425   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
426   utm |= userTime.dwLowDateTime;
427   utm /= 10;
428   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
429   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
430 #  endif                        /* windows version checker */
431 #endif
432 }
433
434 void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
435 {
436 #ifdef HAVE_POSIX_GETTIME
437   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
438 #elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
439   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
440   thread_basic_info_data_t thi_data;
441   thread_basic_info_t thi = &thi_data;
442   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
443   timer->stop.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
444   timer->stop.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
445 #elif defined(HAVE_GETTIMEOFDAY)//if nothing else is available, return just time
446   gettimeofday(&(timer->stop), NULL);
447 #elif defined(_XBT_WIN32)
448 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
449   THROW_UNIMPLEMENTED;
450 #  else
451   HANDLE h = GetCurrentThread();
452   FILETIME creationTime, exitTime, kernelTime, userTime;
453   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
454   unsigned __int64 ktm, utm;
455   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
456   ktm |= kernelTime.dwLowDateTime;
457   ktm /= 10;
458   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
459   utm |= userTime.dwLowDateTime;
460   utm /= 10;
461   timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
462   timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
463 #  endif                        /* windows version checker */
464 #endif
465 }