Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This implement the function gettimeofday() for win32 (Visual C++ does not provides...
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* $Id$ */
2
3 /* sysdep.c -- all system dependency                                        */
4 /*  no system header should be loaded out of this file so that we have only */
5 /*  one file to check when porting to another OS                            */
6
7 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #include "xbt/sysdep.h"
13 #include "xbt/xbt_os_time.h" /* this module */
14 #include "xbt/log.h"
15 #include "portable.h"
16 #include <math.h> /* floor */
17
18
19 #ifdef WIN32
20 #include <sys\timeb.h>
21 #include <errno.h>
22
23 int 
24 gettimeofday(struct timeval *tv, struct timezone *tz)
25 {
26         
27         #if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
28         struct _timeb tm;
29         #else
30         FILETIME  ft;
31         unsigned __int64 tm;
32         #endif
33         
34         if (!tv) 
35         {
36                 errno = EINVAL;
37                 return -1;
38         }
39         
40         #if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
41         _ftime (&tm);
42         
43     tv->tv_sec = tm.time;
44     tv->tv_usec = tm.millitm * 1000;
45         #else
46         GetSystemTimeAsFileTime (&ft);
47         tm = (unsigned __int64)ft.dwHighDateTime << 32;
48         tm |= ft.dwLowDateTime;
49         tm /=10;
50         tm -= 11644473600000000ULL;
51         
52         tv->tv_sec  = (long) (tm / 1000000L);
53         tv->tv_usec = (long) (tm % 1000000L);
54         #endif
55         
56         
57         
58         return 0;
59 }
60 #endif
61
62 double xbt_os_time(void) {
63 #ifdef HAVE_GETTIMEOFDAY
64   struct timeval tv;
65
66   gettimeofday(&tv, NULL);
67
68   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
69 #else
70   /* Poor resolution */
71   return (double)(time(NULL));
72 #endif /* HAVE_GETTIMEOFDAY? */         
73 }
74
75 void xbt_os_sleep(double sec) {
76 #ifdef HAVE_USLEEP
77   sleep(sec);
78   (void)usleep( (sec - floor(sec)) * 1000000);
79
80 #elif _WIN32
81
82      Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000));
83
84         
85 #else /* don't have usleep. Use select to sleep less than one second */
86   struct timeval timeout;
87
88   
89   timeout.tv_sec =  (unsigned long)(sec);
90   timeout.tv_usec = (sec - floor(sec)) * 1000000;
91               
92   select(0, NULL, NULL, NULL, &timeout);
93 #endif
94 }
95
96 /** @brief like free 
97     @hideinitializer */
98 XBT_PUBLIC(void) xbt_free_f(void* p)
99 {
100         free(p);
101 }
102
103
104 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus 
105    disabled in SDL source code */ 
106
107
108 /* \defgroup XBT_sysdep All system dependency
109  * \brief This section describes many macros/functions that can serve as
110  *  an OS abstraction.
111  */
112
113 /*
114 double xbt_os_time(void) {
115 #ifdef HAVE_GETTIMEOFDAY
116   struct timeval tv;
117
118   gettimeofday(&tv, NULL);
119
120   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
121 #else*/
122   /* Poor resolution */
123 /*  return (double)(time(NULL));*/
124 //#endif /* HAVE_GETTIMEOFDAY? */       
125 //}
126
127 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
128
129
130 struct s_xbt_os_timer {
131 #ifdef HAVE_GETTIMEOFDAY
132    struct timeval start,stop;
133 #else
134    unsigned long int start,stop;
135 #endif
136 };
137
138 xbt_os_timer_t xbt_os_timer_new(void) {
139    return xbt_new0(struct s_xbt_os_timer,1);
140 }
141 void xbt_os_timer_free(xbt_os_timer_t timer) {
142    free (timer);
143 }
144 void xbt_os_timer_start(xbt_os_timer_t timer) {
145 #ifdef HAVE_GETTIMEOFDAY
146   gettimeofday(&(timer->start), NULL);
147 #else 
148   timer->start = (unsigned long int)(time(NULL));
149 #endif
150 }
151 void xbt_os_timer_stop(xbt_os_timer_t timer) {
152 #ifdef HAVE_GETTIMEOFDAY
153   gettimeofday(&(timer->stop), NULL);
154 #else 
155   timer->stop = (unsigned long int)(time(NULL));
156 #endif
157 }
158 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
159 #ifdef HAVE_GETTIMEOFDAY
160    return  ((double)timer->stop.tv_sec)  - ((double)timer->start.tv_sec) +
161          ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
162 #else 
163    return  (double)timer->stop  - (double)timer->start;
164 #endif
165 }
166