1 /* xbt_os_time.c -- portable interface to time-related functions */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "xbt/sysdep.h"
10 #include "xbt/xbt_os_time.h" /* this module */
13 #include <math.h> /* floor */
17 #include <sys/timeb.h>
20 double xbt_os_time(void)
22 #ifdef HAVE_GETTIMEOFDAY
24 gettimeofday(&tv, NULL);
25 #elif defined(_XBT_WIN32)
27 # if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
33 tv.tv_usec = tm.millitm * 1000;
39 GetSystemTimeAsFileTime(&ft);
40 tm = (unsigned __int64) ft.dwHighDateTime << 32;
41 tm |= ft.dwLowDateTime;
43 tm -= 11644473600000000ULL;
45 tv.tv_sec = (long) (tm / 1000000L);
46 tv.tv_usec = (long) (tm % 1000000L);
47 # endif /* windows version checker */
49 #else /* not windows, no gettimeofday => poor resolution */
50 return (double) (time(NULL));
51 #endif /* HAVE_GETTIMEOFDAY? */
53 return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
56 void xbt_os_sleep(double sec)
60 (void) usleep((sec - floor(sec)) * 1000000);
63 Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
65 #else /* don't have usleep. Use select to sleep less than one second */
66 struct timeval timeout;
69 timeout.tv_sec = (unsigned long) (sec);
70 timeout.tv_usec = (sec - floor(sec)) * 1000000;
72 select(0, NULL, NULL, NULL, &timeout);
76 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
77 disabled in SDL source code */
80 /* \defgroup XBT_sysdep All system dependency
81 * \brief This section describes many macros/functions that can serve as
86 struct s_xbt_os_timer {
87 #ifdef HAVE_POSIX_GETTIME
88 struct timespec start, stop;
89 #elif defined(HAVE_GETTIMEOFDAY)
90 struct timeval start, stop;
92 unsigned long int start, stop;
96 xbt_os_timer_t xbt_os_timer_new(void) {
97 return xbt_new0(struct s_xbt_os_timer, 1);
100 void xbt_os_timer_free(xbt_os_timer_t timer) {
104 void xbt_os_timer_start(xbt_os_timer_t timer) {
105 #ifdef HAVE_POSIX_GETTIME
106 clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&(timer->start));
107 #elif defined(HAVE_GETTIMEOFDAY)
108 gettimeofday(&(timer->start), NULL);
110 timer->start = (unsigned long int) (time(NULL));
114 void xbt_os_timer_stop(xbt_os_timer_t timer) {
115 #ifdef HAVE_POSIX_GETTIME
116 clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&(timer->stop));
117 #elif defined(HAVE_GETTIMEOFDAY)
118 gettimeofday(&(timer->stop), NULL);
120 timer->stop = (unsigned long int) (time(NULL));
124 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
126 #ifdef HAVE_POSIX_GETTIME
127 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
128 ((((double) timer->stop.tv_nsec) -
129 ((double) timer->start.tv_nsec)) / 1e9);
130 #elif defined(HAVE_GETTIMEOFDAY)
131 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
132 ((((double) timer->stop.tv_usec) -
133 ((double) timer->start.tv_usec)) / 1000000.0);
135 return (double) timer->stop - (double) timer->start;