Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ebd1f8bff719f4b20f18c29d7ebc29212f253d4b
[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 {
23 #ifdef HAVE_GETTIMEOFDAY
24   struct timeval tv;
25   gettimeofday(&tv, NULL);
26 #elif defined(WIN32)
27   struct timeval tv;
28 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
29   struct _timeb tm;
30
31   _ftime(&tm);
32
33   tv.tv_sec = tm.time;
34   tv.tv_usec = tm.millitm * 1000;
35
36 #  else
37   FILETIME ft;
38   unsigned __int64 tm;
39
40   GetSystemTimeAsFileTime(&ft);
41   tm = (unsigned __int64) ft.dwHighDateTime << 32;
42   tm |= ft.dwLowDateTime;
43   tm /= 10;
44   tm -= 11644473600000000ULL;
45
46   tv.tv_sec = (long) (tm / 1000000L);
47   tv.tv_usec = (long) (tm % 1000000L);
48 #  endif /* windows version checker */
49
50 #else /* not windows, no gettimeofday => poor resolution */
51   return (double) (time(NULL));
52 #endif /* HAVE_GETTIMEOFDAY? */
53
54   return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);
55 }
56
57 void xbt_os_sleep(double sec)
58 {
59 #ifdef HAVE_USLEEP
60   sleep(sec);
61   (void) usleep((sec - floor(sec)) * 1000000);
62
63 #elif WIN32
64   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
65
66 #else /* don't have usleep. Use select to sleep less than one second */
67   struct timeval timeout;
68
69
70   timeout.tv_sec = (unsigned long) (sec);
71   timeout.tv_usec = (sec - floor(sec)) * 1000000;
72
73   select(0, NULL, NULL, NULL, &timeout);
74 #endif
75 }
76
77 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
78    disabled in SDL source code */
79
80
81 /* \defgroup XBT_sysdep All system dependency
82  * \brief This section describes many macros/functions that can serve as
83  *  an OS abstraction.
84  */
85
86
87 struct s_xbt_os_timer {
88 #ifdef 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 {
97   return xbt_new0(struct s_xbt_os_timer, 1);
98 }
99
100 void xbt_os_timer_free(xbt_os_timer_t timer)
101 {
102   free(timer);
103 }
104
105 void xbt_os_timer_start(xbt_os_timer_t timer)
106 {
107 #ifdef 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 {
116 #ifdef 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_GETTIMEOFDAY
126   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
127     ((((double) timer->stop.tv_usec) -
128       ((double) timer->start.tv_usec)) / 1000000.0);
129 #else
130   return (double) timer->stop - (double) timer->start;
131 #endif
132 }