Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
At least. ignore ignorable
[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 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
76    disabled in SDL source code */
77
78
79 /* \defgroup XBT_sysdep All system dependency
80  * \brief This section describes many macros/functions that can serve as
81  *  an OS abstraction.
82  */
83
84
85 struct s_xbt_os_timer {
86 #ifdef HAVE_GETTIMEOFDAY
87   struct timeval start,stop;
88 #else
89   unsigned long int start,stop;
90 #endif
91 };
92
93 xbt_os_timer_t xbt_os_timer_new(void) {
94   return xbt_new0(struct s_xbt_os_timer,1);
95 }
96 void xbt_os_timer_free(xbt_os_timer_t timer) {
97   free (timer);
98 }
99 void xbt_os_timer_start(xbt_os_timer_t timer) {
100 #ifdef HAVE_GETTIMEOFDAY
101   gettimeofday(&(timer->start), NULL);
102 #else
103   timer->start = (unsigned long int)(time(NULL));
104 #endif
105 }
106 void xbt_os_timer_stop(xbt_os_timer_t timer) {
107 #ifdef HAVE_GETTIMEOFDAY
108   gettimeofday(&(timer->stop), NULL);
109 #else
110   timer->stop = (unsigned long int)(time(NULL));
111 #endif
112 }
113 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
114 #ifdef HAVE_GETTIMEOFDAY
115   return  ((double)timer->stop.tv_sec)  - ((double)timer->start.tv_sec) +
116   ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
117 #else
118   return  (double)timer->stop  - (double)timer->start;
119 #endif
120 }
121