Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
365d127b2cc97847492bccf2aa555b7a38b4a017
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* xbt_os_time.c -- portable interface to time-related functions            */\r
2 \r
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.\r
4  * All rights reserved.                                                     */\r
5 \r
6 /* This program is free software; you can redistribute it and/or modify it\r
7  * under the terms of the license (GNU LGPL) which comes with this package. */\r
8 \r
9 #include "xbt/sysdep.h"\r
10 #include "xbt/xbt_os_time.h"    /* this module */\r
11 #include "xbt/log.h"\r
12 #include "portable.h"\r
13 #include <math.h>               /* floor */\r
14 \r
15 #ifdef WIN32\r
16 #include <sys/timeb.h>\r
17 #endif\r
18 \r
19 double xbt_os_time(void)\r
20 {\r
21 #ifdef HAVE_GETTIMEOFDAY\r
22   struct timeval tv;\r
23   gettimeofday(&tv, NULL);\r
24 #elif defined(WIN32)\r
25   struct timeval tv;\r
26 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)\r
27   struct _timeb tm;\r
28 \r
29   _ftime(&tm);\r
30 \r
31   tv.tv_sec = tm.time;\r
32   tv.tv_usec = tm.millitm * 1000;\r
33 \r
34 #  else\r
35   FILETIME ft;\r
36   unsigned __int64 tm;\r
37 \r
38   GetSystemTimeAsFileTime(&ft);\r
39   tm = (unsigned __int64) ft.dwHighDateTime << 32;\r
40   tm |= ft.dwLowDateTime;\r
41   tm /= 10;\r
42   tm -= 11644473600000000ULL;\r
43 \r
44   tv.tv_sec = (long) (tm / 1000000L);\r
45   tv.tv_usec = (long) (tm % 1000000L);\r
46 #  endif /* windows version checker */\r
47 \r
48 #else /* not windows, no gettimeofday => poor resolution */\r
49   return (double) (time(NULL));\r
50 #endif /* HAVE_GETTIMEOFDAY? */\r
51 \r
52   return (double) (tv.tv_sec + tv.tv_usec / 1000000.0);\r
53 }\r
54 \r
55 void xbt_os_sleep(double sec)\r
56 {\r
57 #ifdef HAVE_USLEEP\r
58   sleep(sec);\r
59   (void) usleep((sec - floor(sec)) * 1000000);\r
60 \r
61 #elif WIN32\r
62   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));\r
63 \r
64 #else /* don't have usleep. Use select to sleep less than one second */\r
65   struct timeval timeout;\r
66 \r
67 \r
68   timeout.tv_sec = (unsigned long) (sec);\r
69   timeout.tv_usec = (sec - floor(sec)) * 1000000;\r
70 \r
71   select(0, NULL, NULL, NULL, &timeout);\r
72 #endif\r
73 }\r
74 \r
75 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus\r
76    disabled in SDL source code */\r
77 \r
78 \r
79 /* \defgroup XBT_sysdep All system dependency\r
80  * \brief This section describes many macros/functions that can serve as\r
81  *  an OS abstraction.\r
82  */\r
83 \r
84 \r
85 struct s_xbt_os_timer {\r
86 #ifdef HAVE_POSIX_GETTIME\r
87   struct timespec start, stop;\r
88 #elif defined(HAVE_GETTIMEOFDAY)\r
89   struct timeval start, stop;\r
90 #else\r
91   unsigned long int start, stop;\r
92 #endif\r
93 };\r
94 \r
95 xbt_os_timer_t xbt_os_timer_new(void) {\r
96   return xbt_new0(struct s_xbt_os_timer, 1);\r
97 }\r
98 \r
99 void xbt_os_timer_free(xbt_os_timer_t timer) {\r
100   free(timer);\r
101 }\r
102 \r
103 void xbt_os_timer_start(xbt_os_timer_t timer) {\r
104 #ifdef HAVE_POSIX_GETTIME\r
105   clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&(timer->start));\r
106 #elif defined(HAVE_GETTIMEOFDAY)\r
107   gettimeofday(&(timer->start), NULL);\r
108 #else\r
109   timer->start = (unsigned long int) (time(NULL));\r
110 #endif\r
111 }\r
112 \r
113 void xbt_os_timer_stop(xbt_os_timer_t timer) {\r
114 #ifdef HAVE_POSIX_GETTIME\r
115   clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&(timer->stop));\r
116 #elif defined(HAVE_GETTIMEOFDAY)\r
117   gettimeofday(&(timer->stop), NULL);\r
118 #else\r
119   timer->stop = (unsigned long int) (time(NULL));\r
120 #endif\r
121 }\r
122 \r
123 double xbt_os_timer_elapsed(xbt_os_timer_t timer)\r
124 {\r
125 #ifdef HAVE_POSIX_GETTIME\r
126   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +\r
127       ((((double) timer->stop.tv_nsec) -\r
128         ((double) timer->start.tv_nsec)) / 1e9);\r
129 #elif defined(HAVE_GETTIMEOFDAY)\r
130   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +\r
131     ((((double) timer->stop.tv_usec) -\r
132       ((double) timer->start.tv_usec)) / 1000000.0);\r
133 #else\r
134   return (double) timer->stop - (double) timer->start;\r
135 #endif\r
136 }\r