Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0345e0f5ebad8d0fe71ae34cc14d31b35003b1d4
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* xbt_os_time.c -- portable interface to time-related functions            */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/xbt_os_time.h"    /* this module */
11 #include "xbt/log.h"
12 #include "portable.h"
13 #include <math.h>               /* floor */
14
15
16 #ifdef WIN32
17 #include <sys/timeb.h>
18 #endif
19
20 double xbt_os_time(void)
21 {
22 #ifdef HAVE_GETTIMEOFDAY
23   struct timeval tv;
24   gettimeofday(&tv, NULL);
25 #elif defined(WIN32)
26   struct timeval tv;
27 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
28   struct _timeb tm;
29
30   _ftime(&tm);
31
32   tv.tv_sec = tm.time;
33   tv.tv_usec = tm.millitm * 1000;
34
35 #  else
36   FILETIME ft;
37   unsigned __int64 tm;
38
39   GetSystemTimeAsFileTime(&ft);
40   tm = (unsigned __int64) ft.dwHighDateTime << 32;
41   tm |= ft.dwLowDateTime;
42   tm /= 10;
43   tm -= 11644473600000000ULL;
44
45   tv.tv_sec = (long) (tm / 1000000L);
46   tv.tv_usec = (long) (tm % 1000000L);
47 #  endif /* windows version checker */
48
49 #else /* not windows, no gettimeofday => poor resolution */
50   return (double) (time(NULL));
51 #endif /* HAVE_GETTIMEOFDAY? */
52
53   return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
54 }
55
56 void xbt_os_sleep(double sec)
57 {
58 #ifdef HAVE_USLEEP
59   sleep(sec);
60   (void) usleep((sec - floor(sec)) * 1000000);
61
62 #elif WIN32
63   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
64
65 #else /* don't have usleep. Use select to sleep less than one second */
66   struct timeval timeout;
67
68
69   timeout.tv_sec = (unsigned long) (sec);
70   timeout.tv_usec = (sec - floor(sec)) * 1000000;
71
72   select(0, NULL, NULL, NULL, &timeout);
73 #endif
74 }
75
76 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
77    disabled in SDL source code */
78
79
80 /* \defgroup XBT_sysdep All system dependency
81  * \brief This section describes many macros/functions that can serve as
82  *  an OS abstraction.
83  */
84
85
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;
91 #else
92   unsigned long int start, stop;
93 #endif
94 };
95
96 xbt_os_timer_t xbt_os_timer_new(void) {
97   return xbt_new0(struct s_xbt_os_timer, 1);
98 }
99
100 void xbt_os_timer_free(xbt_os_timer_t timer) {
101   free(timer);
102 }
103
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);
109 #else
110   timer->start = (unsigned long int) (time(NULL));
111 #endif
112 }
113
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);
119 #else
120   timer->stop = (unsigned long int) (time(NULL));
121 #endif
122 }
123
124 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
125 {
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);
134 #else
135   return (double) timer->stop - (double) timer->start;
136 #endif
137 }