Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix warning in xbt_os_time.c on Mach
[simgrid.git] / src / xbt / xbt_os_time.c
index 4e0f331..2819255 100644 (file)
@@ -1,6 +1,6 @@
 /* xbt_os_time.c -- portable interface to time-related functions            */
 
-/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2007-2010, 2012-2014. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
 #endif
 
 //Freebsd doesn't provide this clock_gettime flag yet, because it was added too recently (after 1993)
-#ifdef __FreeBSD__
-#define CLOCK_PROCESS_CPUTTIME_ID CLOCK_PROF
+#if defined (CLOCK_PROF) && ! defined (CLOCK_PROCESS_CPUTIME_ID)
+#define CLOCK_PROCESS_CPUTIME_ID CLOCK_PROF
+#endif
+
+#if defined(__APPLE__) && defined(__MACH__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <mach/mach_init.h>
+#include <mach/mach_host.h>
+#include <mach/mach_port.h>
+#include <mach/mach_traps.h>
+#include <mach/task_info.h>
+#include <mach/thread_info.h>
+#include <mach/thread_act.h>
+#include <mach/vm_region.h>
+#include <mach/vm_map.h>
+#include <mach/task.h>
 #endif
 
 double xbt_os_time(void)
@@ -92,13 +107,17 @@ void xbt_os_sleep(double sec)
 struct s_xbt_os_timer {
 #ifdef HAVE_POSIX_GETTIME
   struct timespec start, stop, elapse;
-#elif defined(HAVE_GETTIMEOFDAY)
+#elif defined(HAVE_GETTIMEOFDAY) || defined(_XBT_WIN32)
   struct timeval start, stop, elapse;
 #else
   unsigned long int start, stop, elapse;
 #endif
 };
 
+size_t xbt_os_timer_size(void){
+  return sizeof(struct s_xbt_os_timer);
+}
+
 xbt_os_timer_t xbt_os_timer_new(void)
 {
   return xbt_new0(struct s_xbt_os_timer, 1);
@@ -247,9 +266,13 @@ void xbt_os_cputimer_start(xbt_os_timer_t timer)
   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
+  timer->elapse.tv_sec = 0;
+  timer->elapse.tv_usec = 0;
+  gettimeofday(&(timer->start), NULL);
 #elif defined(_XBT_WIN32)
   timer->elapse.tv_sec = 0;
-  timer->elapse.tv_nsec = 0;
+  timer->elapse.tv_usec = 0;
 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
   THROW_UNIMPLEMENTED;
 #  else
@@ -275,9 +298,13 @@ void xbt_os_cputimer_resume(xbt_os_timer_t timer)
   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)
+  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);
 #elif defined(_XBT_WIN32)
   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
-  timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
+  timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
   THROW_UNIMPLEMENTED;
 #  else
@@ -302,6 +329,8 @@ void xbt_os_cputimer_stop(xbt_os_timer_t timer)
 {
 #ifdef HAVE_POSIX_GETTIME
   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(timer->stop));
+#elif defined(HAVE_GETTIMEOFDAY)
+  gettimeofday(&(timer->stop), NULL);
 #elif defined(_XBT_WIN32)
 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
   THROW_UNIMPLEMENTED;
@@ -328,6 +357,19 @@ void xbt_os_threadtimer_start(xbt_os_timer_t timer)
   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__)//attempt for timing of the thread on OSX
+  timer->elapse.tv_sec = 0;
+  timer->elapse.tv_usec = 0;
+  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->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
+  timer->elapse.tv_sec = 0;
+  timer->elapse.tv_usec = 0;
+  gettimeofday(&(timer->start), NULL);
 #elif defined(_XBT_WIN32)
   struct timeval tv;
 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
@@ -355,9 +397,22 @@ void xbt_os_threadtimer_resume(xbt_os_timer_t timer)
   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__)
+  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_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->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)
+  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);
 #elif defined(_XBT_WIN32)
   timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
-  timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
+  timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
   THROW_UNIMPLEMENTED;
 #  else
@@ -381,6 +436,15 @@ void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
 {
 #ifdef HAVE_POSIX_GETTIME
   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
+#elif defined(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
+  gettimeofday(&(timer->stop), NULL);
 #elif defined(_XBT_WIN32)
 #  if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
   THROW_UNIMPLEMENTED;