Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/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-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, stop, elapse;
96 #elif HAVE_GETTIMEOFDAY || defined(_WIN32)
97   struct timeval start, stop, elapse;
98 #else
99   unsigned long int start, stop, elapse;
100 #endif
101 };
102
103 size_t xbt_os_timer_size(void){
104   return sizeof(struct s_xbt_os_timer);
105 }
106
107 xbt_os_timer_t xbt_os_timer_new(void)
108 {
109   return xbt_new0(struct s_xbt_os_timer, 1);
110 }
111
112 void xbt_os_timer_free(xbt_os_timer_t timer)
113 {
114   free(timer);
115 }
116
117 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
118 {
119 #if HAVE_POSIX_GETTIME
120   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + ((double) timer->elapse.tv_sec ) +
121       ((((double) timer->stop.tv_nsec) - ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
122 #elif HAVE_GETTIMEOFDAY || defined(_WIN32)
123   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + ((double) timer->elapse.tv_sec ) +
124       ((((double) timer->stop.tv_usec) - ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) /
125          1000000.0);
126 #else
127   return (double) timer->stop - (double) timer->start + (double) timer->elapse;
128 #endif
129 }
130
131 void xbt_os_walltimer_start(xbt_os_timer_t timer)
132 {
133 #if HAVE_POSIX_GETTIME
134   timer->elapse.tv_sec = 0;
135   timer->elapse.tv_nsec = 0;
136   clock_gettime(CLOCK_REALTIME, &(timer->start));
137 #elif HAVE_GETTIMEOFDAY
138   timer->elapse.tv_sec = 0;
139   timer->elapse.tv_usec = 0;
140   gettimeofday(&(timer->start), NULL);
141 #elif defined(_WIN32)
142   timer->elapse.tv_sec = 0;
143   timer->elapse.tv_usec = 0;
144   FILETIME ft;
145   unsigned __int64 tm;
146
147   GetSystemTimeAsFileTime(&ft);
148   tm = (unsigned __int64) ft.dwHighDateTime << 32;
149   tm |= ft.dwLowDateTime;
150   tm /= 10;
151   tm -= 11644473600000000ULL;
152
153   timer->start.tv_sec = (long) (tm / 1000000L);
154   timer->start.tv_usec = (long) (tm % 1000000L);
155 #else
156   timer->elapse = 0;
157   timer->start = (unsigned long int) (time(NULL));
158 #endif
159 }
160
161 void xbt_os_walltimer_resume(xbt_os_timer_t timer)
162 {
163 #if HAVE_POSIX_GETTIME
164   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
165
166    timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
167   clock_gettime(CLOCK_REALTIME, &(timer->start));
168 #elif HAVE_GETTIMEOFDAY
169   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
170   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
171   gettimeofday(&(timer->start), NULL);
172 #elif defined(_WIN32)
173   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
174   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
175   FILETIME ft;
176   unsigned __int64 tm;
177
178   GetSystemTimeAsFileTime(&ft);
179   tm = (unsigned __int64) ft.dwHighDateTime << 32;
180   tm |= ft.dwLowDateTime;
181   tm /= 10;
182   tm -= 11644473600000000ULL;
183
184   timer->start.tv_sec = (long) (tm / 1000000L);
185   timer->start.tv_usec = (long) (tm % 1000000L);
186 #else
187   timer->elapse = timer->stop - timer->start;
188   timer->start = (unsigned long int) (time(NULL));
189 #endif
190 }
191
192 void xbt_os_walltimer_stop(xbt_os_timer_t timer)
193 {
194 #if HAVE_POSIX_GETTIME
195   clock_gettime(CLOCK_REALTIME, &(timer->stop));
196 #elif HAVE_GETTIMEOFDAY
197   gettimeofday(&(timer->stop), NULL);
198 #elif defined(_WIN32)
199   FILETIME ft;
200   unsigned __int64 tm;
201
202   GetSystemTimeAsFileTime(&ft);
203   tm = (unsigned __int64) ft.dwHighDateTime << 32;
204   tm |= ft.dwLowDateTime;
205   tm /= 10;
206   tm -= 11644473600000000ULL;
207
208   timer->stop.tv_sec = (long) (tm / 1000000L);
209   timer->stop.tv_usec = (long) (tm % 1000000L);
210 #else
211   timer->stop = (unsigned long int) (time(NULL));
212 #endif
213 }
214
215 void xbt_os_cputimer_start(xbt_os_timer_t timer)
216 {
217 #if HAVE_POSIX_GETTIME
218   timer->elapse.tv_sec = 0;
219   timer->elapse.tv_nsec = 0;
220   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
221 #elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
222   timer->elapse.tv_sec = 0;
223   timer->elapse.tv_usec = 0;
224   gettimeofday(&(timer->start), NULL);
225 #elif defined(_WIN32)
226   timer->elapse.tv_sec = 0;
227   timer->elapse.tv_usec = 0;
228   HANDLE h = GetCurrentProcess();
229   FILETIME creationTime, exitTime, kernelTime, userTime;
230   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
231   unsigned __int64 ktm, utm;
232   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
233   ktm |= kernelTime.dwLowDateTime;
234   ktm /= 10;
235   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
236   utm |= userTime.dwLowDateTime;
237   utm /= 10;
238   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
239   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
240 #endif
241 }
242
243 void xbt_os_cputimer_resume(xbt_os_timer_t timer)
244 {
245 #if HAVE_POSIX_GETTIME
246   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
247   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
248   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
249 #elif HAVE_GETTIMEOFDAY
250   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
251   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
252   gettimeofday(&(timer->start), NULL);
253 #elif defined(_WIN32)
254   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
255   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
256   HANDLE h = GetCurrentProcess();
257   FILETIME creationTime, exitTime, kernelTime, userTime;
258   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
259   unsigned __int64 ktm, utm;
260   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
261   ktm |= kernelTime.dwLowDateTime;
262   ktm /= 10;
263   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
264   utm |= userTime.dwLowDateTime;
265   utm /= 10;
266   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
267   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
268 #endif
269 }
270
271 void xbt_os_cputimer_stop(xbt_os_timer_t timer)
272 {
273 #if HAVE_POSIX_GETTIME
274   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
275 #elif HAVE_GETTIMEOFDAY
276   gettimeofday(&(timer->stop), NULL);
277 #elif defined(_WIN32)
278   HANDLE h = GetCurrentProcess();
279   FILETIME creationTime, exitTime, kernelTime, userTime;
280   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
281   unsigned __int64 ktm, utm;
282   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
283   ktm |= kernelTime.dwLowDateTime;
284   ktm /= 10;
285   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
286   utm |= userTime.dwLowDateTime;
287   utm /= 10;
288   timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
289   timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
290 #endif
291 }
292
293 void xbt_os_threadtimer_start(xbt_os_timer_t timer)
294 {
295 #if HAVE_POSIX_GETTIME
296   timer->elapse.tv_sec = 0;
297   timer->elapse.tv_nsec = 0;
298   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
299 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
300   timer->elapse.tv_sec = 0;
301   timer->elapse.tv_usec = 0;
302   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
303   thread_basic_info_data_t thi_data;
304   thread_basic_info_t thi = &thi_data;
305   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
306   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
307   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
308 #elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
309   timer->elapse.tv_sec = 0;
310   timer->elapse.tv_usec = 0;
311   gettimeofday(&(timer->start), NULL);
312 #elif defined(_WIN32)
313   HANDLE h = GetCurrentThread();
314   FILETIME creationTime, exitTime, kernelTime, userTime;
315   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
316   unsigned __int64 ktm, utm;
317   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
318   ktm |= kernelTime.dwLowDateTime;
319   ktm /= 10;
320   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
321   utm |= userTime.dwLowDateTime;
322   utm /= 10;
323   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
324   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
325 #endif
326 }
327
328 void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
329 {
330 #if HAVE_POSIX_GETTIME
331   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
332   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
333   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
334 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
335   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
336   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
337   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
338   thread_basic_info_data_t thi_data;
339   thread_basic_info_t thi = &thi_data;
340   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
341   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
342   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
343 #elif HAVE_GETTIMEOFDAY
344   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
345   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
346   gettimeofday(&(timer->start), NULL);
347 #elif defined(_WIN32)
348   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
349   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
350   HANDLE h = GetCurrentThread();
351   FILETIME creationTime, exitTime, kernelTime, userTime;
352   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
353   unsigned __int64 ktm, utm;
354   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
355   ktm |= kernelTime.dwLowDateTime;
356   ktm /= 10;
357   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
358   utm |= userTime.dwLowDateTime;
359   utm /= 10;
360   timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
361   timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
362 #endif
363 }
364
365 void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
366 {
367 #if HAVE_POSIX_GETTIME
368   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
369 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
370   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
371   thread_basic_info_data_t thi_data;
372   thread_basic_info_t thi = &thi_data;
373   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
374   timer->stop.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
375   timer->stop.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
376 #elif HAVE_GETTIMEOFDAY //if nothing else is available, return just time
377   gettimeofday(&(timer->stop), NULL);
378 #elif defined(_WIN32)
379   HANDLE h = GetCurrentThread();
380   FILETIME creationTime, exitTime, kernelTime, userTime;
381   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
382   unsigned __int64 ktm, utm;
383   ktm = (unsigned __int64) kernelTime.dwHighDateTime << 32;
384   ktm |= kernelTime.dwLowDateTime;
385   ktm /= 10;
386   utm = (unsigned __int64) userTime.dwHighDateTime << 32;
387   utm |= userTime.dwLowDateTime;
388   utm /= 10;
389   timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
390   timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
391 #endif
392 }