Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MAIS C'ETAIT SUR, EN FAIT ! C'ETAIT SUR !
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* xbt_os_time.c -- portable interface to time-related functions            */
2
3 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "src/internal_config.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/xbt_os_time.h"    /* this module */
12 #include <math.h>               /* floor */
13 #include <sys/time.h>
14 #include <time.h>
15
16 #ifdef _WIN32
17 #include <sys/timeb.h>
18 #include <windows.h>
19 #else
20 #include <unistd.h>
21 #endif
22
23 //Freebsd doesn't provide this clock_gettime flag yet, because it was added too recently (after 1993)
24 #if defined (CLOCK_PROF) && ! defined (CLOCK_PROCESS_CPUTIME_ID)
25 #define CLOCK_PROCESS_CPUTIME_ID CLOCK_PROF
26 #endif
27
28 #if defined(__APPLE__) && defined(__MACH__)
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31 #include <mach/mach_init.h>
32 #include <mach/mach_host.h>
33 #include <mach/mach_port.h>
34 #include <mach/mach_traps.h>
35 #include <mach/task_info.h>
36 #include <mach/thread_info.h>
37 #include <mach/thread_act.h>
38 #include <mach/vm_region.h>
39 #include <mach/vm_map.h>
40 #include <mach/task.h>
41 #endif
42
43 #ifdef _WIN32
44 static void w32_time_to_timeval(struct timeval* tv, const FILETIME* ft)
45 {
46   unsigned __int64 tm;
47   tm = (unsigned __int64)ft->dwHighDateTime << 32;
48   tm |= ft->dwLowDateTime;
49   tm /= 10;
50   tm -= 11644473600000000ULL;
51   tv->tv_sec  = (long)(tm / 1000000L);
52   tv->tv_usec = (long)(tm % 1000000L);
53 }
54
55 static void w32_times_to_timeval(struct timeval* tv, const FILETIME* kernel_time, const FILETIME* user_time)
56 {
57   unsigned __int64 ktm, utm;
58   ktm = (unsigned __int64)kernel_time->dwHighDateTime << 32;
59   ktm |= kernel_time->dwLowDateTime;
60   ktm /= 10;
61   utm = (unsigned __int64)user_time->dwHighDateTime << 32;
62   utm |= user_time->dwLowDateTime;
63   utm /= 10;
64   tv->tv_sec  = (long)(ktm / 1000000L) + (long)(utm / 1000000L);
65   tv->tv_usec = (long)(ktm % 1000000L) + (long)(utm % 1000000L);
66 }
67 #endif
68
69 double xbt_os_time(void)
70 {
71 #if HAVE_GETTIMEOFDAY
72   struct timeval tv;
73   gettimeofday(&tv, NULL);
74 #elif defined(_WIN32)
75   struct timeval tv;
76   FILETIME ft;
77   GetSystemTimeAsFileTime(&ft);
78   w32_time_to_timeval(&tv, &ft);
79 #else                           /* not windows, no gettimeofday => poor resolution */
80   return (double) (time(NULL));
81 #endif                          /* HAVE_GETTIMEOFDAY? */
82
83   return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
84 }
85
86 void xbt_os_sleep(double sec)
87 {
88
89 #ifdef _WIN32
90   Sleep((floor(sec) * 1000) + ((sec - floor(sec)) * 1000));
91
92 #elif HAVE_NANOSLEEP
93   struct timespec ts;
94   ts.tv_sec  = (time_t)sec;
95   ts.tv_nsec = (long)((sec - floor(sec)) * 1e9);
96   nanosleep (&ts, NULL);
97 #else                           /* don't have nanosleep. Use select to sleep less than one second */
98   struct timeval timeout;
99
100   timeout.tv_sec  = (long)sec;
101   timeout.tv_usec = (long)(sec - floor(sec)) * 1e6);
102
103   select(0, NULL, NULL, NULL, &timeout);
104 #endif
105 }
106
107 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus  disabled in SDL source code */
108
109 /* @defgroup XBT_sysdep All system dependency
110  * @brief This section describes many macros/functions that can serve as  an OS abstraction.
111  */
112 struct s_xbt_os_timer {
113 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
114   struct timespec start;
115   struct timespec stop;
116   struct timespec elapse;
117 #elif HAVE_GETTIMEOFDAY || defined(_WIN32)
118   struct timeval start;
119   struct timeval stop;
120   struct timeval elapse;
121 #else
122   unsigned long int start;
123   unsigned long int stop;
124   unsigned long int elapse;
125 #endif
126 };
127
128 size_t xbt_os_timer_size(void)
129 {
130   return sizeof(struct s_xbt_os_timer);
131 }
132
133 xbt_os_timer_t xbt_os_timer_new(void)
134 {
135   return xbt_new0(struct s_xbt_os_timer, 1);
136 }
137
138 void xbt_os_timer_free(xbt_os_timer_t timer)
139 {
140   free(timer);
141 }
142
143 double xbt_os_timer_elapsed(const_xbt_os_timer_t timer)
144 {
145 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
146   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + ((double) timer->elapse.tv_sec ) +
147       ((((double) timer->stop.tv_nsec) - ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
148 #elif HAVE_GETTIMEOFDAY || defined(_WIN32)
149   return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + ((double) timer->elapse.tv_sec ) +
150       ((((double) timer->stop.tv_usec) - ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) /
151          1000000.0);
152 #else
153   return (double) timer->stop - (double) timer->start + (double) timer->elapse;
154 #endif
155 }
156
157 void xbt_os_walltimer_start(xbt_os_timer_t timer)
158 {
159 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
160   timer->elapse.tv_sec = 0;
161   timer->elapse.tv_nsec = 0;
162   clock_gettime(CLOCK_REALTIME, &(timer->start));
163 #elif HAVE_GETTIMEOFDAY
164   timer->elapse.tv_sec = 0;
165   timer->elapse.tv_usec = 0;
166   gettimeofday(&(timer->start), NULL);
167 #elif defined(_WIN32)
168   timer->elapse.tv_sec = 0;
169   timer->elapse.tv_usec = 0;
170   FILETIME ft;
171   GetSystemTimeAsFileTime(&ft);
172   w32_time_to_timeval(&timer->start, &ft);
173 #else
174   timer->elapse = 0;
175   timer->start = (unsigned long int) (time(NULL));
176 #endif
177 }
178
179 void xbt_os_walltimer_resume(xbt_os_timer_t timer)
180 {
181 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
182   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
183
184    timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
185   clock_gettime(CLOCK_REALTIME, &(timer->start));
186 #elif HAVE_GETTIMEOFDAY
187   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
188   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
189   gettimeofday(&(timer->start), NULL);
190 #elif defined(_WIN32)
191   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
192   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
193   FILETIME ft;
194   GetSystemTimeAsFileTime(&ft);
195   w32_time_to_timeval(&timer->start, &ft);
196 #else
197   timer->elapse = timer->stop - timer->start;
198   timer->start = (unsigned long int) (time(NULL));
199 #endif
200 }
201
202 void xbt_os_walltimer_stop(xbt_os_timer_t timer)
203 {
204 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
205   clock_gettime(CLOCK_REALTIME, &(timer->stop));
206 #elif HAVE_GETTIMEOFDAY
207   gettimeofday(&(timer->stop), NULL);
208 #elif defined(_WIN32)
209   FILETIME ft;
210   GetSystemTimeAsFileTime(&ft);
211   w32_time_to_timeval(&timer->stop, &ft);
212 #else
213   timer->stop = (unsigned long int) (time(NULL));
214 #endif
215 }
216
217 void xbt_os_cputimer_start(xbt_os_timer_t timer)
218 {
219 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
220   timer->elapse.tv_sec = 0;
221   timer->elapse.tv_nsec = 0;
222   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
223 #elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
224   timer->elapse.tv_sec = 0;
225   timer->elapse.tv_usec = 0;
226   gettimeofday(&(timer->start), NULL);
227 #elif defined(_WIN32)
228   timer->elapse.tv_sec = 0;
229   timer->elapse.tv_usec = 0;
230   HANDLE h = GetCurrentProcess();
231   FILETIME creationTime, exitTime, kernelTime, userTime;
232   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
233   w32_times_to_timeval(&timer->start, &kernelTime, &userTime);
234 #else
235 # error The cpu timers of SimGrid do not seem to work on your platform.
236 #endif
237 }
238
239 void xbt_os_cputimer_resume(xbt_os_timer_t timer)
240 {
241 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
242   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
243   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
244   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
245 #elif HAVE_GETTIMEOFDAY
246   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
247   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
248   gettimeofday(&(timer->start), NULL);
249 #elif defined(_WIN32)
250   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
251   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
252   HANDLE h = GetCurrentProcess();
253   FILETIME creationTime, exitTime, kernelTime, userTime;
254   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
255   w32_times_to_timeval(&timer->start, &kernelTime, &userTime);
256 #else
257 # error The cpu timers of SimGrid do not seem to work on your platform.
258 #endif
259 }
260
261 void xbt_os_cputimer_stop(xbt_os_timer_t timer)
262 {
263 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
264   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
265 #elif HAVE_GETTIMEOFDAY
266   gettimeofday(&(timer->stop), NULL);
267 #elif defined(_WIN32)
268   HANDLE h = GetCurrentProcess();
269   FILETIME creationTime, exitTime, kernelTime, userTime;
270   GetProcessTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
271   w32_times_to_timeval(&timer->stop, &kernelTime, &userTime);
272 #else
273 # error The cpu timers of SimGrid do not seem to work on your platform.
274 #endif
275 }
276
277 void xbt_os_threadtimer_start(xbt_os_timer_t timer)
278 {
279 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
280   timer->elapse.tv_sec = 0;
281   timer->elapse.tv_nsec = 0;
282   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
283 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
284   timer->elapse.tv_sec = 0;
285   timer->elapse.tv_usec = 0;
286   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
287   thread_basic_info_data_t thi_data;
288   thread_basic_info_t thi = &thi_data;
289   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
290   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
291   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
292 #elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
293   timer->elapse.tv_sec = 0;
294   timer->elapse.tv_usec = 0;
295   gettimeofday(&(timer->start), NULL);
296 #elif defined(_WIN32)
297   HANDLE h = GetCurrentThread();
298   FILETIME creationTime, exitTime, kernelTime, userTime;
299   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
300   w32_times_to_timeval(&timer->start, &kernelTime, &userTime);
301 #else
302 # error The thread timers of SimGrid do not seem to work on your platform.
303 #endif
304 }
305
306 void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
307 {
308 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
309   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
310   timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
311   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
312 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
313   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
314   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
315   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
316   thread_basic_info_data_t thi_data;
317   thread_basic_info_t thi = &thi_data;
318   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
319   timer->start.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
320   timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
321 #elif HAVE_GETTIMEOFDAY
322   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
323   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
324   gettimeofday(&(timer->start), NULL);
325 #elif defined(_WIN32)
326   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
327   timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
328   HANDLE h = GetCurrentThread();
329   FILETIME creationTime, exitTime, kernelTime, userTime;
330   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
331   w32_times_to_timeval(&timer->start, &kernelTime, &userTime);
332 #else
333 # error The thread timers of SimGrid do not seem to work on your platform.
334 #endif
335 }
336
337 void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
338 {
339 #if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
340   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
341 #elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
342   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
343   thread_basic_info_data_t thi_data;
344   thread_basic_info_t thi = &thi_data;
345   thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
346   timer->stop.tv_usec =  thi->system_time.microseconds + thi->user_time.microseconds;
347   timer->stop.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
348 #elif HAVE_GETTIMEOFDAY //if nothing else is available, return just time
349   gettimeofday(&(timer->stop), NULL);
350 #elif defined(_WIN32)
351   HANDLE h = GetCurrentThread();
352   FILETIME creationTime, exitTime, kernelTime, userTime;
353   GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
354   w32_times_to_timeval(&timer->stop, &kernelTime, &userTime);
355 #else
356 # error The thread timers of SimGrid do not seem to work on your platform.
357 #endif
358 }