Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add some tests for homogeneous dicts (global free functions).
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* xbt_os_time.c -- portable interface to time-related functions            */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/xbt_os_time.h"    /* this module */
11 #include "xbt/log.h"
12 #include "portable.h"
13 #include <math.h>               /* floor */
14
15 #ifdef _XBT_WIN32
16 #include <sys/timeb.h>
17 #include <windows.h>
18 #endif
19
20 double xbt_os_time(void)
21 {
22 #ifdef HAVE_GETTIMEOFDAY
23   struct timeval tv;
24   gettimeofday(&tv, NULL);
25 #elif defined(_XBT_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 {
58 #ifdef HAVE_USLEEP
59   sleep(sec);
60   (void) usleep((sec - floor(sec)) * 1000000);
61
62 #elif _XBT_WIN32
63   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
64
65 #else                           /* don't have usleep. Use select to sleep less than one second */
66   struct timeval timeout;
67
68
69   timeout.tv_sec = (unsigned long) (sec);
70   timeout.tv_usec = (sec - floor(sec)) * 1000000;
71
72   select(0, NULL, NULL, NULL, &timeout);
73 #endif
74 }
75
76 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
77    disabled in SDL source code */
78
79
80 /* \defgroup XBT_sysdep All system dependency
81  * \brief This section describes many macros/functions that can serve as
82  *  an OS abstraction.
83  */
84
85
86 struct s_xbt_os_timer {
87 #ifdef HAVE_POSIX_GETTIME
88   struct timespec start, stop;
89 #elif defined(HAVE_GETTIMEOFDAY)
90   struct timeval start, stop;
91 #else
92   unsigned long int start, stop;
93 #endif
94 };
95
96 xbt_os_timer_t xbt_os_timer_new(void)
97 {
98   return xbt_new0(struct s_xbt_os_timer, 1);
99 }
100
101 void xbt_os_timer_free(xbt_os_timer_t timer)
102 {
103   free(timer);
104 }
105
106 void xbt_os_timer_start(xbt_os_timer_t timer)
107 {
108 #ifdef HAVE_POSIX_GETTIME
109   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
110 #elif defined(HAVE_GETTIMEOFDAY)
111   gettimeofday(&(timer->start), NULL);
112 #else
113   timer->start = (unsigned long int) (time(NULL));
114 #endif
115 }
116
117 void xbt_os_timer_stop(xbt_os_timer_t timer)
118 {
119 #ifdef HAVE_POSIX_GETTIME
120   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
121 #elif defined(HAVE_GETTIMEOFDAY)
122   gettimeofday(&(timer->stop), NULL);
123 #else
124   timer->stop = (unsigned long int) (time(NULL));
125 #endif
126 }
127
128 double xbt_os_timer_elapsed(xbt_os_timer_t timer)
129 {
130 #ifdef HAVE_POSIX_GETTIME
131   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
132       ((((double) timer->stop.tv_nsec) -
133         ((double) timer->start.tv_nsec)) / 1e9);
134 #elif defined(HAVE_GETTIMEOFDAY)
135   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
136       ((((double) timer->stop.tv_usec) -
137         ((double) timer->start.tv_usec)) / 1000000.0);
138 #else
139   return (double) timer->stop - (double) timer->start;
140 #endif
141 }