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
26 gettimeofday(&tv, NULL);
29 # if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
35 tv.tv_usec = tm.millitm * 1000;
41 GetSystemTimeAsFileTime (&ft);
42 tm = (unsigned __int64)ft.dwHighDateTime << 32;
43 tm |= ft.dwLowDateTime;
45 tm -= 11644473600000000ULL;
47 tv.tv_sec = (long) (tm / 1000000L);
48 tv.tv_usec = (long) (tm % 1000000L);
49 # endif /* windows version checker */
51 #else /* not windows, no gettimeofday => poor resolution */
52 return (double)(time(NULL));
53 #endif /* HAVE_GETTIMEOFDAY? */
55 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
58 void xbt_os_sleep(double sec) {
61 (void)usleep( (sec - floor(sec)) * 1000000);
65 Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000));
68 #else /* don't have usleep. Use select to sleep less than one second */
69 struct timeval timeout;
72 timeout.tv_sec = (unsigned long)(sec);
73 timeout.tv_usec = (sec - floor(sec)) * 1000000;
75 select(0, NULL, NULL, NULL, &timeout);
81 XBT_PUBLIC(void) xbt_free_f(void* p)
87 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
88 disabled in SDL source code */
91 /* \defgroup XBT_sysdep All system dependency
92 * \brief This section describes many macros/functions that can serve as
97 double xbt_os_time(void) {
98 #ifdef HAVE_GETTIMEOFDAY
101 gettimeofday(&tv, NULL);
103 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
105 /* Poor resolution */
106 /* return (double)(time(NULL));*/
107 //#endif /* HAVE_GETTIMEOFDAY? */
110 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
113 struct s_xbt_os_timer {
114 #ifdef HAVE_GETTIMEOFDAY
115 struct timeval start,stop;
117 unsigned long int start,stop;
121 xbt_os_timer_t xbt_os_timer_new(void) {
122 return xbt_new0(struct s_xbt_os_timer,1);
124 void xbt_os_timer_free(xbt_os_timer_t timer) {
127 void xbt_os_timer_start(xbt_os_timer_t timer) {
128 #ifdef HAVE_GETTIMEOFDAY
129 gettimeofday(&(timer->start), NULL);
131 timer->start = (unsigned long int)(time(NULL));
134 void xbt_os_timer_stop(xbt_os_timer_t timer) {
135 #ifdef HAVE_GETTIMEOFDAY
136 gettimeofday(&(timer->stop), NULL);
138 timer->stop = (unsigned long int)(time(NULL));
141 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
142 #ifdef HAVE_GETTIMEOFDAY
143 return ((double)timer->stop.tv_sec) - ((double)timer->start.tv_sec) +
144 ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
146 return (double)timer->stop - (double)timer->start;