Logo AND Algorithmique Numérique Distribuée

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