Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
de79d93f504ae6d735319f534e8c00fb4aa6778c
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* $Id$ */
2
3 /* xbt_os_time.c -- portable interface to time-related functions            */
4
5 /* Copyright (c) 2004-2008 The SimGrid team. All rights reserved.           */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/sysdep.h"
11 #include "xbt/xbt_os_time.h" /* this module */
12 #include "xbt/log.h"
13 #include "portable.h"
14 #include <math.h> /* floor */
15
16
17 #ifdef WIN32
18 #include <sys/timeb.h>
19 #endif
20
21 double xbt_os_time(void) {
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 #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 /** @brief like free 
76     @hideinitializer */
77 XBT_PUBLIC(void) xbt_free_f(void* p) {
78    free(p);
79 }
80
81
82 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus 
83    disabled in SDL source code */ 
84
85
86 /* \defgroup XBT_sysdep All system dependency
87  * \brief This section describes many macros/functions that can serve as
88  *  an OS abstraction.
89  */
90
91
92 struct s_xbt_os_timer {
93 #ifdef HAVE_GETTIMEOFDAY
94    struct timeval start,stop;
95 #else
96    unsigned long int start,stop;
97 #endif
98 };
99
100 xbt_os_timer_t xbt_os_timer_new(void) {
101    return xbt_new0(struct s_xbt_os_timer,1);
102 }
103 void xbt_os_timer_free(xbt_os_timer_t timer) {
104    free (timer);
105 }
106 void xbt_os_timer_start(xbt_os_timer_t timer) {
107 #ifdef HAVE_GETTIMEOFDAY
108   gettimeofday(&(timer->start), NULL);
109 #else 
110   timer->start = (unsigned long int)(time(NULL));
111 #endif
112 }
113 void xbt_os_timer_stop(xbt_os_timer_t timer) {
114 #ifdef HAVE_GETTIMEOFDAY
115   gettimeofday(&(timer->stop), NULL);
116 #else 
117   timer->stop = (unsigned long int)(time(NULL));
118 #endif
119 }
120 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
121 #ifdef HAVE_GETTIMEOFDAY
122    return  ((double)timer->stop.tv_sec)  - ((double)timer->start.tv_sec) +
123          ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
124 #else 
125    return  (double)timer->stop  - (double)timer->start;
126 #endif
127 }
128