/* xbt_os_time.c -- portable interface to time-related functions */
-/* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
+#include "src/internal_config.h"
#include "xbt/sysdep.h"
-#include "xbt/xbt_os_time.h" /* this module */
#include "xbt/log.h"
-#include "src/portable.h"
+#include "xbt/xbt_os_time.h" /* this module */
#include <math.h> /* floor */
+#include <sys/time.h>
+#include <time.h>
#ifdef _WIN32
#include <sys/timeb.h>
double xbt_os_time(void)
{
-#ifdef HAVE_GETTIMEOFDAY
+#if HAVE_GETTIMEOFDAY
struct timeval tv;
gettimeofday(&tv, NULL);
#elif defined(_WIN32)
#endif
}
-/* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
- disabled in SDL source code */
-
+/* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus disabled in SDL source code */
/* \defgroup XBT_sysdep All system dependency
- * \brief This section describes many macros/functions that can serve as
- * an OS abstraction.
+ * \brief This section describes many macros/functions that can serve as an OS abstraction.
*/
-
-
struct s_xbt_os_timer {
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
struct timespec start, stop, elapse;
-#elif defined(HAVE_GETTIMEOFDAY) || defined(_WIN32)
+#elif HAVE_GETTIMEOFDAY || defined(_WIN32)
struct timeval start, stop, elapse;
#else
unsigned long int start, stop, elapse;
double xbt_os_timer_elapsed(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
- return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
- ((double) timer->elapse.tv_sec ) +
- ((((double) timer->stop.tv_nsec) -
- ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
-#elif defined(HAVE_GETTIMEOFDAY) || defined(_WIN32)
- return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec)
- + ((double) timer->elapse.tv_sec ) +
- ((((double) timer->stop.tv_usec) -
- ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) / 1000000.0);
+#if HAVE_POSIX_GETTIME
+ return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + ((double) timer->elapse.tv_sec ) +
+ ((((double) timer->stop.tv_nsec) - ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
+#elif HAVE_GETTIMEOFDAY || defined(_WIN32)
+ return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) + ((double) timer->elapse.tv_sec ) +
+ ((((double) timer->stop.tv_usec) - ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) /
+ 1000000.0);
#else
- return (double) timer->stop - (double) timer->start + (double)
- timer->elapse;
+ return (double) timer->stop - (double) timer->start + (double) timer->elapse;
#endif
}
void xbt_os_walltimer_start(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
timer->elapse.tv_sec = 0;
timer->elapse.tv_nsec = 0;
clock_gettime(CLOCK_REALTIME, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
timer->elapse.tv_sec = 0;
timer->elapse.tv_usec = 0;
gettimeofday(&(timer->start), NULL);
void xbt_os_walltimer_resume(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
clock_gettime(CLOCK_REALTIME, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
gettimeofday(&(timer->start), NULL);
void xbt_os_walltimer_stop(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
clock_gettime(CLOCK_REALTIME, &(timer->stop));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
gettimeofday(&(timer->stop), NULL);
#elif defined(_WIN32)
FILETIME ft;
void xbt_os_cputimer_start(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
timer->elapse.tv_sec = 0;
timer->elapse.tv_nsec = 0;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY)//return time and not cputime in this case
+#elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
timer->elapse.tv_sec = 0;
timer->elapse.tv_usec = 0;
gettimeofday(&(timer->start), NULL);
void xbt_os_cputimer_resume(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
gettimeofday(&(timer->start), NULL);
void xbt_os_cputimer_stop(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
gettimeofday(&(timer->stop), NULL);
#elif defined(_WIN32)
HANDLE h = GetCurrentProcess();
void xbt_os_threadtimer_start(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
timer->elapse.tv_sec = 0;
timer->elapse.tv_nsec = 0;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
+#elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
timer->elapse.tv_sec = 0;
timer->elapse.tv_usec = 0;
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
timer->start.tv_usec = thi->system_time.microseconds + thi->user_time.microseconds;
timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
-#elif defined(HAVE_GETTIMEOFDAY)//return time and not cputime in this case
+#elif HAVE_GETTIMEOFDAY //return time and not cputime in this case
timer->elapse.tv_sec = 0;
timer->elapse.tv_usec = 0;
gettimeofday(&(timer->start), NULL);
void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
-#elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
+#elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
timer->start.tv_usec = thi->system_time.microseconds + thi->user_time.microseconds;
timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif HAVE_GETTIMEOFDAY
timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
gettimeofday(&(timer->start), NULL);
void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
{
-#ifdef HAVE_POSIX_GETTIME
+#if HAVE_POSIX_GETTIME
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
-#elif defined(HAVE_GETTIMEOFDAY) && defined(__MACH__) && defined(__APPLE__)
+#elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
thread_basic_info_data_t thi_data;
thread_basic_info_t thi = &thi_data;
thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
timer->stop.tv_usec = thi->system_time.microseconds + thi->user_time.microseconds;
timer->stop.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
-#elif defined(HAVE_GETTIMEOFDAY)//if nothing else is available, return just time
+#elif HAVE_GETTIMEOFDAY //if nothing else is available, return just time
gettimeofday(&(timer->stop), NULL);
#elif defined(_WIN32)
HANDLE h = GetCurrentThread();