timing.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <sys/time.h>
  18. #include <starpu.h>
  19. #include <common/config.h>
  20. #include <profiling/profiling.h>
  21. #include <common/timing.h>
  22. #ifdef HAVE_CLOCK_GETTIME
  23. #include <time.h>
  24. #ifndef _POSIX_C_SOURCE
  25. /* for clock_gettime */
  26. #define _POSIX_C_SOURCE 199309L
  27. #endif
  28. #ifdef __linux__
  29. #ifndef CLOCK_MONOTONIC_RAW
  30. #define CLOCK_MONOTONIC_RAW 4
  31. #endif
  32. #endif
  33. static struct timespec reference_start_time_ts;
  34. /* Modern CPUs' clocks are usually not synchronized so we use a monotonic clock
  35. * to have consistent timing measurements. The CLOCK_MONOTONIC_RAW clock is not
  36. * subject to NTP adjustments, but is not available on all systems (in that
  37. * case we use the CLOCK_MONOTONIC clock instead). */
  38. static void _starpu_clock_gettime(struct timespec *ts) {
  39. #ifdef CLOCK_MONOTONIC_RAW
  40. static int raw_supported = 0;
  41. switch (raw_supported) {
  42. case -1:
  43. break;
  44. case 1:
  45. clock_gettime(CLOCK_MONOTONIC_RAW, ts);
  46. return;
  47. case 0:
  48. if (clock_gettime(CLOCK_MONOTONIC_RAW, ts)) {
  49. raw_supported = -1;
  50. break;
  51. } else {
  52. raw_supported = 1;
  53. return;
  54. }
  55. }
  56. #endif
  57. clock_gettime(CLOCK_MONOTONIC, ts);
  58. }
  59. void _starpu_timing_init(void)
  60. {
  61. _starpu_clock_gettime(&reference_start_time_ts);
  62. }
  63. void starpu_clock_gettime(struct timespec *ts)
  64. {
  65. struct timespec absolute_ts;
  66. /* Read the current time */
  67. _starpu_clock_gettime(&absolute_ts);
  68. /* Compute the relative time since initialization */
  69. starpu_timespec_sub(&absolute_ts, &reference_start_time_ts, ts);
  70. }
  71. #else // !HAVE_CLOCK_GETTIME
  72. #if defined(__i386__) || defined(__pentium__) || defined(__pentiumpro__) || defined(__i586__) || defined(__i686__) || defined(__k6__) || defined(__k7__) || defined(__x86_64__)
  73. typedef union starpu_u_tick
  74. {
  75. uint64_t tick;
  76. struct
  77. {
  78. uint32_t low;
  79. uint32_t high;
  80. }
  81. sub;
  82. } starpu_tick_t;
  83. #define STARPU_GET_TICK(t) __asm__ volatile("rdtsc" : "=a" ((t).sub.low), "=d" ((t).sub.high))
  84. #define TICK_RAW_DIFF(t1, t2) ((t2).tick - (t1).tick)
  85. #define TICK_DIFF(t1, t2) (TICK_RAW_DIFF(t1, t2) - residual)
  86. static starpu_tick_t reference_start_tick;
  87. static double scale = 0.0;
  88. static unsigned long long residual = 0;
  89. static int inited = 0;
  90. void _starpu_timing_init(void)
  91. {
  92. static starpu_tick_t t1, t2;
  93. int i;
  94. if (inited) return;
  95. residual = (unsigned long long)1 << 63;
  96. for(i = 0; i < 20; i++)
  97. {
  98. STARPU_GET_TICK(t1);
  99. STARPU_GET_TICK(t2);
  100. residual = STARPU_MIN(residual, TICK_RAW_DIFF(t1, t2));
  101. }
  102. {
  103. struct timeval tv1,tv2;
  104. STARPU_GET_TICK(t1);
  105. gettimeofday(&tv1,0);
  106. usleep(500000);
  107. STARPU_GET_TICK(t2);
  108. gettimeofday(&tv2,0);
  109. scale = ((tv2.tv_sec*1e6 + tv2.tv_usec) -
  110. (tv1.tv_sec*1e6 + tv1.tv_usec)) /
  111. (double)(TICK_DIFF(t1, t2));
  112. }
  113. STARPU_GET_TICK(reference_start_tick);
  114. inited = 1;
  115. }
  116. void starpu_clock_gettime(struct timespec *ts)
  117. {
  118. starpu_tick_t tick_now;
  119. STARPU_GET_TICK(tick_now);
  120. uint64_t elapsed_ticks = TICK_DIFF(reference_start_tick, tick_now);
  121. /* We convert this number into nano-seconds so that we can fill the
  122. * timespec structure. */
  123. uint64_t elapsed_ns = (uint64_t)(((double)elapsed_ticks)*(scale*1000.0));
  124. long tv_nsec = (elapsed_ns % 1000000000);
  125. time_t tv_sec = (elapsed_ns / 1000000000);
  126. ts->tv_sec = tv_sec;
  127. ts->tv_nsec = tv_nsec;
  128. }
  129. #else // !HAVE_CLOCK_GETTIME & no rdtsc
  130. #warning StarPU could not find a timer, clock will always return 0
  131. void _starpu_timing_init(void)
  132. {
  133. }
  134. void starpu_clock_gettime(struct timespec *ts)
  135. {
  136. timerclear(ts);
  137. }
  138. #endif
  139. #endif // HAVE_CLOCK_GETTIME
  140. /* Returns the time elapsed between start and end in microseconds */
  141. double starpu_timing_timespec_delay_us(struct timespec *start, struct timespec *end)
  142. {
  143. struct timespec diff;
  144. starpu_timespec_sub(end, start, &diff);
  145. double us = (diff.tv_sec*1e6) + (diff.tv_nsec*1e-3);
  146. return us;
  147. }
  148. double starpu_timing_timespec_to_us(struct timespec *ts)
  149. {
  150. return (1000000.0*ts->tv_sec) + (0.001*ts->tv_nsec);
  151. }
  152. double starpu_timing_now(void)
  153. {
  154. struct timespec now;
  155. starpu_clock_gettime(&now);
  156. return starpu_timing_timespec_to_us(&now);
  157. }