3 /* xbt_os_time.c -- portable interface to time-related functions */
5 /* Copyright (c) 2004-2008 The SimGrid team. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
11 #include "xbt/xbt_os_time.h" /* this module */
14 #include <math.h> /* floor */
18 #include <sys/timeb.h>
21 double xbt_os_time(void)
23 #ifdef HAVE_GETTIMEOFDAY
25 gettimeofday(&tv, NULL);
28 # if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
34 tv.tv_usec = tm.millitm * 1000;
40 GetSystemTimeAsFileTime(&ft);
41 tm = (unsigned __int64) ft.dwHighDateTime << 32;
42 tm |= ft.dwLowDateTime;
44 tm -= 11644473600000000ULL;
46 tv.tv_sec = (long) (tm / 1000000L);
47 tv.tv_usec = (long) (tm % 1000000L);
48 # endif /* windows version checker */
50 #else /* not windows, no gettimeofday => poor resolution */
51 return (double) (time(NULL));
52 #endif /* HAVE_GETTIMEOFDAY? */
54 return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
57 void xbt_os_sleep(double sec)
61 (void) usleep((sec - floor(sec)) * 1000000);
64 Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
66 #else /* don't have usleep. Use select to sleep less than one second */
67 struct timeval timeout;
70 timeout.tv_sec = (unsigned long) (sec);
71 timeout.tv_usec = (sec - floor(sec)) * 1000000;
73 select(0, NULL, NULL, NULL, &timeout);
77 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
78 disabled in SDL source code */
81 /* \defgroup XBT_sysdep All system dependency
82 * \brief This section describes many macros/functions that can serve as
87 struct s_xbt_os_timer {
88 #ifdef HAVE_POSIX_GETTIME
89 struct timespec start, stop;
90 #elif defined(HAVE_GETTIMEOFDAY)
91 struct timeval start, stop;
93 unsigned long int start, stop;
97 xbt_os_timer_t xbt_os_timer_new(void) {
98 return xbt_new0(struct s_xbt_os_timer, 1);
101 void xbt_os_timer_free(xbt_os_timer_t timer) {
105 void xbt_os_timer_start(xbt_os_timer_t timer) {
106 #ifdef HAVE_POSIX_GETTIME
107 clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&(timer->start));
108 #elif defined(HAVE_GETTIMEOFDAY)
109 gettimeofday(&(timer->start), NULL);
111 timer->start = (unsigned long int) (time(NULL));
115 void xbt_os_timer_stop(xbt_os_timer_t timer) {
116 #ifdef HAVE_POSIX_GETTIME
117 clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&(timer->stop));
118 #elif defined(HAVE_GETTIMEOFDAY)
119 gettimeofday(&(timer->stop), NULL);
121 timer->stop = (unsigned long int) (time(NULL));
125 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
127 #ifdef HAVE_POSIX_GETTIME
128 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
129 ((((double) timer->stop.tv_nsec) -
130 ((double) timer->start.tv_nsec)) / 1e9);
131 #elif defined(HAVE_GETTIMEOFDAY)
132 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
133 ((((double) timer->stop.tv_usec) -
134 ((double) timer->start.tv_usec)) / 1000000.0);
136 return (double) timer->stop - (double) timer->start;