1 /* xbt_os_time.c -- portable interface to time-related functions */
3 /* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved. */
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. */
8 #include "src/internal_config.h"
9 #include "xbt/sysdep.h"
11 #include "xbt/xbt_os_time.h" /* this module */
12 #include <math.h> /* floor */
17 #include <sys/timeb.h>
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
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>
41 double xbt_os_time(void)
45 gettimeofday(&tv, NULL);
51 GetSystemTimeAsFileTime(&ft);
52 tm = (unsigned __int64) ft.dwHighDateTime << 32;
53 tm |= ft.dwLowDateTime;
55 tm -= 11644473600000000ULL;
57 tv.tv_sec = (long) (tm / 1000000L);
58 tv.tv_usec = (long) (tm % 1000000L);
60 #else /* not windows, no gettimeofday => poor resolution */
61 return (double) (time(NULL));
62 #endif /* HAVE_GETTIMEOFDAY? */
64 return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
67 void xbt_os_sleep(double sec)
71 Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
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;
81 timeout.tv_sec = (unsigned long) (sec);
82 timeout.tv_usec = (sec - floor(sec)) * 1000000;
84 select(0, NULL, NULL, NULL, &timeout);
88 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus disabled in SDL source code */
90 /* \defgroup XBT_sysdep All system dependency
91 * \brief This section describes many macros/functions that can serve as an OS abstraction.
93 struct s_xbt_os_timer {
94 #if HAVE_POSIX_GETTIME
95 struct timespec start;
97 struct timespec elapse;
98 #elif HAVE_GETTIMEOFDAY || defined(_WIN32)
101 struct timeval elapse;
103 unsigned long int start;
104 unsigned long int stop;
105 unsigned long int elapse;
109 size_t xbt_os_timer_size(void)
111 return sizeof(struct s_xbt_os_timer);
114 xbt_os_timer_t xbt_os_timer_new(void)
116 return xbt_new0(struct s_xbt_os_timer, 1);
119 void xbt_os_timer_free(xbt_os_timer_t timer)
124 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
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 )) /
134 return (double) timer->stop - (double) timer->start + (double) timer->elapse;
138 void xbt_os_walltimer_start(xbt_os_timer_t timer)
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;
154 GetSystemTimeAsFileTime(&ft);
155 tm = (unsigned __int64) ft.dwHighDateTime << 32;
156 tm |= ft.dwLowDateTime;
158 tm -= 11644473600000000ULL;
160 timer->start.tv_sec = (long) (tm / 1000000L);
161 timer->start.tv_usec = (long) (tm % 1000000L);
164 timer->start = (unsigned long int) (time(NULL));
168 void xbt_os_walltimer_resume(xbt_os_timer_t timer)
170 #if HAVE_POSIX_GETTIME
171 timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
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;
185 GetSystemTimeAsFileTime(&ft);
186 tm = (unsigned __int64) ft.dwHighDateTime << 32;
187 tm |= ft.dwLowDateTime;
189 tm -= 11644473600000000ULL;
191 timer->start.tv_sec = (long) (tm / 1000000L);
192 timer->start.tv_usec = (long) (tm % 1000000L);
194 timer->elapse = timer->stop - timer->start;
195 timer->start = (unsigned long int) (time(NULL));
199 void xbt_os_walltimer_stop(xbt_os_timer_t timer)
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)
209 GetSystemTimeAsFileTime(&ft);
210 tm = (unsigned __int64) ft.dwHighDateTime << 32;
211 tm |= ft.dwLowDateTime;
213 tm -= 11644473600000000ULL;
215 timer->stop.tv_sec = (long) (tm / 1000000L);
216 timer->stop.tv_usec = (long) (tm % 1000000L);
218 timer->stop = (unsigned long int) (time(NULL));
222 void xbt_os_cputimer_start(xbt_os_timer_t timer)
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;
242 utm = (unsigned __int64) userTime.dwHighDateTime << 32;
243 utm |= userTime.dwLowDateTime;
245 timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
246 timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
250 void xbt_os_cputimer_resume(xbt_os_timer_t timer)
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;
270 utm = (unsigned __int64) userTime.dwHighDateTime << 32;
271 utm |= userTime.dwLowDateTime;
273 timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
274 timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
278 void xbt_os_cputimer_stop(xbt_os_timer_t timer)
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;
292 utm = (unsigned __int64) userTime.dwHighDateTime << 32;
293 utm |= userTime.dwLowDateTime;
295 timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
296 timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
300 void xbt_os_threadtimer_start(xbt_os_timer_t timer)
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;
327 utm = (unsigned __int64) userTime.dwHighDateTime << 32;
328 utm |= userTime.dwLowDateTime;
330 timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
331 timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
335 void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
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;
364 utm = (unsigned __int64) userTime.dwHighDateTime << 32;
365 utm |= userTime.dwLowDateTime;
367 timer->start.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
368 timer->start.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);
372 void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
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;
393 utm = (unsigned __int64) userTime.dwHighDateTime << 32;
394 utm |= userTime.dwLowDateTime;
396 timer->stop.tv_sec = (long) (ktm / 1000000L) + (long) (utm / 1000000L);
397 timer->stop.tv_usec = (long) (ktm % 1000000L) + (long) (utm % 1000000L);