1 /* xbt_os_time.c -- portable interface to time-related functions */
\r
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
\r
4 * All rights reserved. */
\r
6 /* This program is free software; you can redistribute it and/or modify it
\r
7 * under the terms of the license (GNU LGPL) which comes with this package. */
\r
9 #include "xbt/sysdep.h"
\r
10 #include "xbt/xbt_os_time.h" /* this module */
\r
11 #include "xbt/log.h"
\r
12 #include "portable.h"
\r
13 #include <math.h> /* floor */
\r
16 #include <sys/timeb.h>
\r
19 double xbt_os_time(void)
\r
21 #ifdef HAVE_GETTIMEOFDAY
\r
23 gettimeofday(&tv, NULL);
\r
24 #elif defined(WIN32)
\r
26 # if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
\r
31 tv.tv_sec = tm.time;
\r
32 tv.tv_usec = tm.millitm * 1000;
\r
36 unsigned __int64 tm;
\r
38 GetSystemTimeAsFileTime(&ft);
\r
39 tm = (unsigned __int64) ft.dwHighDateTime << 32;
\r
40 tm |= ft.dwLowDateTime;
\r
42 tm -= 11644473600000000ULL;
\r
44 tv.tv_sec = (long) (tm / 1000000L);
\r
45 tv.tv_usec = (long) (tm % 1000000L);
\r
46 # endif /* windows version checker */
\r
48 #else /* not windows, no gettimeofday => poor resolution */
\r
49 return (double) (time(NULL));
\r
50 #endif /* HAVE_GETTIMEOFDAY? */
\r
52 return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
\r
55 void xbt_os_sleep(double sec)
\r
59 (void) usleep((sec - floor(sec)) * 1000000);
\r
62 Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
\r
64 #else /* don't have usleep. Use select to sleep less than one second */
\r
65 struct timeval timeout;
\r
68 timeout.tv_sec = (unsigned long) (sec);
\r
69 timeout.tv_usec = (sec - floor(sec)) * 1000000;
\r
71 select(0, NULL, NULL, NULL, &timeout);
\r
75 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
\r
76 disabled in SDL source code */
\r
79 /* \defgroup XBT_sysdep All system dependency
\r
80 * \brief This section describes many macros/functions that can serve as
\r
81 * an OS abstraction.
\r
85 struct s_xbt_os_timer {
\r
86 #ifdef HAVE_POSIX_GETTIME
\r
87 struct timespec start, stop;
\r
88 #elif defined(HAVE_GETTIMEOFDAY)
\r
89 struct timeval start, stop;
\r
91 unsigned long int start, stop;
\r
95 xbt_os_timer_t xbt_os_timer_new(void) {
\r
96 return xbt_new0(struct s_xbt_os_timer, 1);
\r
99 void xbt_os_timer_free(xbt_os_timer_t timer) {
\r
103 void xbt_os_timer_start(xbt_os_timer_t timer) {
\r
104 #ifdef HAVE_POSIX_GETTIME
\r
105 clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&(timer->start));
\r
106 #elif defined(HAVE_GETTIMEOFDAY)
\r
107 gettimeofday(&(timer->start), NULL);
\r
109 timer->start = (unsigned long int) (time(NULL));
\r
113 void xbt_os_timer_stop(xbt_os_timer_t timer) {
\r
114 #ifdef HAVE_POSIX_GETTIME
\r
115 clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&(timer->stop));
\r
116 #elif defined(HAVE_GETTIMEOFDAY)
\r
117 gettimeofday(&(timer->stop), NULL);
\r
119 timer->stop = (unsigned long int) (time(NULL));
\r
123 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
\r
125 #ifdef HAVE_POSIX_GETTIME
\r
126 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
\r
127 ((((double) timer->stop.tv_nsec) -
\r
128 ((double) timer->start.tv_nsec)) / 1e9);
\r
129 #elif defined(HAVE_GETTIMEOFDAY)
\r
130 return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
\r
131 ((((double) timer->stop.tv_usec) -
\r
132 ((double) timer->start.tv_usec)) / 1000000.0);
\r
134 return (double) timer->stop - (double) timer->start;
\r