Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
distraction
[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-2007 The SimGrid team. 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 #endif
22
23 double xbt_os_time(void) {
24 #ifdef HAVE_GETTIMEOFDAY
25   struct timeval tv;
26
27   gettimeofday(&tv, NULL);
28
29   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
30 #elif defined(WIN32)
31
32 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
33    struct _timeb tm;
34    
35    _ftime (&tm);
36         
37    tv->tv_sec = tm.time;
38    tv->tv_usec = tm.millitm * 1000;
39    
40 #  else
41    FILETIME  ft;
42    unsigned __int64 tm;
43    
44    GetSystemTimeAsFileTime (&ft);
45    tm = (unsigned __int64)ft.dwHighDateTime << 32;
46    tm |= ft.dwLowDateTime;
47    tm /=10;
48    tm -= 11644473600000000ULL;
49    
50    tv->tv_sec  = (long) (tm / 1000000L);
51    tv->tv_usec = (long) (tm % 1000000L);
52 #endif /* windows version checker */
53                 
54    
55 #else  /* not windows, no gettimeofday => poor resolution */
56    
57   return (double)(time(NULL));
58 #endif /* HAVE_GETTIMEOFDAY? */         
59 }
60
61 void xbt_os_sleep(double sec) {
62 #ifdef HAVE_USLEEP
63   sleep(sec);
64   (void)usleep( (sec - floor(sec)) * 1000000);
65
66 #elif _WIN32
67
68      Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000));
69
70         
71 #else /* don't have usleep. Use select to sleep less than one second */
72   struct timeval timeout;
73
74   
75   timeout.tv_sec =  (unsigned long)(sec);
76   timeout.tv_usec = (sec - floor(sec)) * 1000000;
77               
78   select(0, NULL, NULL, NULL, &timeout);
79 #endif
80 }
81
82 /** @brief like free 
83     @hideinitializer */
84 XBT_PUBLIC(void) xbt_free_f(void* p)
85 {
86         free(p);
87 }
88
89
90 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus 
91    disabled in SDL source code */ 
92
93
94 /* \defgroup XBT_sysdep All system dependency
95  * \brief This section describes many macros/functions that can serve as
96  *  an OS abstraction.
97  */
98
99 /*
100 double xbt_os_time(void) {
101 #ifdef HAVE_GETTIMEOFDAY
102   struct timeval tv;
103
104   gettimeofday(&tv, NULL);
105
106   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
107 #else*/
108   /* Poor resolution */
109 /*  return (double)(time(NULL));*/
110 //#endif /* HAVE_GETTIMEOFDAY? */       
111 //}
112
113 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
114
115
116 struct s_xbt_os_timer {
117 #ifdef HAVE_GETTIMEOFDAY
118    struct timeval start,stop;
119 #else
120    unsigned long int start,stop;
121 #endif
122 };
123
124 xbt_os_timer_t xbt_os_timer_new(void) {
125    return xbt_new0(struct s_xbt_os_timer,1);
126 }
127 void xbt_os_timer_free(xbt_os_timer_t timer) {
128    free (timer);
129 }
130 void xbt_os_timer_start(xbt_os_timer_t timer) {
131 #ifdef HAVE_GETTIMEOFDAY
132   gettimeofday(&(timer->start), NULL);
133 #else 
134   timer->start = (unsigned long int)(time(NULL));
135 #endif
136 }
137 void xbt_os_timer_stop(xbt_os_timer_t timer) {
138 #ifdef HAVE_GETTIMEOFDAY
139   gettimeofday(&(timer->stop), NULL);
140 #else 
141   timer->stop = (unsigned long int)(time(NULL));
142 #endif
143 }
144 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
145 #ifdef HAVE_GETTIMEOFDAY
146    return  ((double)timer->stop.tv_sec)  - ((double)timer->start.tv_sec) +
147          ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
148 #else 
149    return  (double)timer->stop  - (double)timer->start;
150 #endif
151 }
152