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 */
7 /* Copyright (c) 2004-2007 The SimGrid team. All rights reserved. */
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. */
12 #include "xbt/sysdep.h"
13 #include "xbt/xbt_os_time.h" /* this module */
16 #include <math.h> /* floor */
20 #include <sys\timeb.h>
23 double xbt_os_time(void) {
24 #ifdef HAVE_GETTIMEOFDAY
27 gettimeofday(&tv, NULL);
29 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
32 # if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
38 tv->tv_usec = tm.millitm * 1000;
44 GetSystemTimeAsFileTime (&ft);
45 tm = (unsigned __int64)ft.dwHighDateTime << 32;
46 tm |= ft.dwLowDateTime;
48 tm -= 11644473600000000ULL;
50 tv->tv_sec = (long) (tm / 1000000L);
51 tv->tv_usec = (long) (tm % 1000000L);
52 #endif /* windows version checker */
55 #else /* not windows, no gettimeofday => poor resolution */
57 return (double)(time(NULL));
58 #endif /* HAVE_GETTIMEOFDAY? */
61 void xbt_os_sleep(double sec) {
64 (void)usleep( (sec - floor(sec)) * 1000000);
68 Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000));
71 #else /* don't have usleep. Use select to sleep less than one second */
72 struct timeval timeout;
75 timeout.tv_sec = (unsigned long)(sec);
76 timeout.tv_usec = (sec - floor(sec)) * 1000000;
78 select(0, NULL, NULL, NULL, &timeout);
84 XBT_PUBLIC(void) xbt_free_f(void* p)
90 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
91 disabled in SDL source code */
94 /* \defgroup XBT_sysdep All system dependency
95 * \brief This section describes many macros/functions that can serve as
100 double xbt_os_time(void) {
101 #ifdef HAVE_GETTIMEOFDAY
104 gettimeofday(&tv, NULL);
106 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
108 /* Poor resolution */
109 /* return (double)(time(NULL));*/
110 //#endif /* HAVE_GETTIMEOFDAY? */
113 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
116 struct s_xbt_os_timer {
117 #ifdef HAVE_GETTIMEOFDAY
118 struct timeval start,stop;
120 unsigned long int start,stop;
124 xbt_os_timer_t xbt_os_timer_new(void) {
125 return xbt_new0(struct s_xbt_os_timer,1);
127 void xbt_os_timer_free(xbt_os_timer_t timer) {
130 void xbt_os_timer_start(xbt_os_timer_t timer) {
131 #ifdef HAVE_GETTIMEOFDAY
132 gettimeofday(&(timer->start), NULL);
134 timer->start = (unsigned long int)(time(NULL));
137 void xbt_os_timer_stop(xbt_os_timer_t timer) {
138 #ifdef HAVE_GETTIMEOFDAY
139 gettimeofday(&(timer->stop), NULL);
141 timer->stop = (unsigned long int)(time(NULL));
144 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
145 #ifdef HAVE_GETTIMEOFDAY
146 return ((double)timer->stop.tv_sec) - ((double)timer->start.tv_sec) +
147 ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
149 return (double)timer->stop - (double)timer->start;