Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
As Arnaud said, one function to rule them all
[simgrid.git] / src / xbt / xbt_os_time.c
1 /* $Id$ */
2
3 /* sysdep.c -- all system dependency                                        */
4 /*  no system header should be loaded out of this file so that we have only */
5 /*  one file to check when porting to another OS                            */
6
7 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #include "xbt/sysdep.h"
13 #include "xbt/xbt_os_time.h" /* this module */
14 #include "xbt/log.h"
15 #include "portable.h"
16 #include <math.h> /* floor */
17
18 double xbt_os_time(void) {
19 #ifdef HAVE_GETTIMEOFDAY
20   struct timeval tv;
21
22   gettimeofday(&tv, NULL);
23
24   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
25 #else
26   /* Poor resolution */
27   return (double)(time(NULL));
28 #endif /* HAVE_GETTIMEOFDAY? */         
29 }
30
31 void xbt_os_sleep(double sec) {
32 #ifdef HAVE_USLEEP
33   sleep(sec);
34   (void)usleep( (sec - floor(sec)) * 1000000);
35
36 #elif _WIN32
37
38      Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000));
39
40         
41 #else /* don't have usleep. Use select to sleep less than one second */
42   struct timeval timeout;
43
44   
45   timeout.tv_sec =  (unsigned long)(sec);
46   timeout.tv_usec = (sec - floor(sec)) * 1000000;
47               
48   select(0, NULL, NULL, NULL, &timeout);
49 #endif
50 }
51
52 /** @brief like free 
53     @hideinitializer */
54 XBT_PUBLIC(void) xbt_free_f(void* p)
55 {
56         free(p);
57 }
58
59
60 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus 
61    disabled in SDL source code */ 
62
63
64 /* \defgroup XBT_sysdep All system dependency
65  * \brief This section describes many macros/functions that can serve as
66  *  an OS abstraction.
67  */
68
69 /*
70 double xbt_os_time(void) {
71 #ifdef HAVE_GETTIMEOFDAY
72   struct timeval tv;
73
74   gettimeofday(&tv, NULL);
75
76   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
77 #else*/
78   /* Poor resolution */
79 /*  return (double)(time(NULL));*/
80 //#endif /* HAVE_GETTIMEOFDAY? */       
81 //}
82
83 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
84
85
86 struct s_xbt_os_timer {
87 #ifdef HAVE_GETTIMEOFDAY
88    struct timeval start,stop;
89 #else
90    unsigned long int start,stop;
91 #endif
92 };
93
94 xbt_os_timer_t xbt_os_timer_new(void) {
95    return xbt_new0(struct s_xbt_os_timer,1);
96 }
97 void xbt_os_timer_free(xbt_os_timer_t timer) {
98    free (timer);
99 }
100 void xbt_os_timer_start(xbt_os_timer_t timer) {
101 #ifdef HAVE_GETTIMEOFDAY
102   gettimeofday(&(timer->start), NULL);
103 #else 
104   timer->start = (unsigned long int)(time(NULL));
105 #endif
106 }
107 void xbt_os_timer_stop(xbt_os_timer_t timer) {
108 #ifdef HAVE_GETTIMEOFDAY
109   gettimeofday(&(timer->stop), NULL);
110 #else 
111   timer->stop = (unsigned long int)(time(NULL));
112 #endif
113 }
114 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
115 #ifdef HAVE_GETTIMEOFDAY
116    return  ((double)timer->stop.tv_sec)  - ((double)timer->start.tv_sec) +
117          ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
118 #else 
119    return  (double)timer->stop  - (double)timer->start;
120 #endif
121 }
122