stats.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* stats.h -*-C++-*-
  2. *
  3. *************************************************************************
  4. *
  5. * @copyright
  6. * Copyright (C) 2009-2013, Intel Corporation
  7. * All rights reserved.
  8. *
  9. * @copyright
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. * * Neither the name of Intel Corporation nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * @copyright
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  35. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. **************************************************************************/
  38. /**
  39. * @file stats.h
  40. *
  41. * @brief Support for gathering and reporting statistics on Cilk applications.
  42. *
  43. * Note that stats are normally NOT compiled in because it increases the
  44. * overhead of stealing. To compile in profiling support, define CILK_PROFILE.
  45. */
  46. #ifndef INCLUDED_STATS_DOT_H
  47. #define INCLUDED_STATS_DOT_H
  48. /* #define CILK_PROFILE 1 */
  49. // @note The CILK_PROFILE flag and intervals is known to be broken
  50. // in at least programs with Windows exceptions.
  51. // Enable this flag at your own peril. :)
  52. #include <cilk/common.h>
  53. #include "rts-common.h"
  54. #include "internal/abi.h"
  55. #ifdef CILK_PROFILE
  56. #include <stdio.h> // Define FILE *
  57. #endif
  58. __CILKRTS_BEGIN_EXTERN_C
  59. /** @brief Events that we measure. */
  60. enum interval
  61. {
  62. INTERVAL_IN_SCHEDULER, ///< Time threads spend "bound" to Cilk
  63. INTERVAL_WORKING, ///< Time spent working
  64. INTERVAL_IN_RUNTIME, ///< Time spent executing runtime scheduling loop
  65. INTERVAL_STEALING, ///< Time spent stealing work
  66. INTERVAL_STEAL_SUCCESS, ///< Time to do a successful steal
  67. INTERVAL_STEAL_FAIL_EMPTYQ, ///< Count of steal failures due to lack of stealable work
  68. INTERVAL_STEAL_FAIL_LOCK, ///< Count of steal failures due to failure to lock worker
  69. INTERVAL_STEAL_FAIL_USER_WORKER, ///< Count of steal failures by user workers which attempt to steal from another team
  70. INTERVAL_STEAL_FAIL_DEKKER, ///< Count of steal failures due to Dekker protocol failure
  71. INTERVAL_SYNC_CHECK, ///< Time spent processing syncs
  72. INTERVAL_THE_EXCEPTION_CHECK, ///< Time spent performing THE exception checks
  73. INTERVAL_THE_EXCEPTION_CHECK_USELESS, ///< Count of useless THE exception checks
  74. INTERVAL_RETURNING, ///< Time spent returning from calls
  75. INTERVAL_FINALIZE_CHILD, ///< Time spent in finalize_child
  76. INTERVAL_PROVABLY_GOOD_STEAL, ///< Time spent in provably_good_steal
  77. INTERVAL_UNCONDITIONAL_STEAL, ///< Time spent in unconditional_steal
  78. INTERVAL_ALLOC_FULL_FRAME, ///< Time spent in __cilkrts_make_full_frame
  79. INTERVAL_FRAME_ALLOC_LARGE, ///< Count of calls to __cilkrts_frame_malloc for buffers bigger than FRAME_MALLOC_MAX_SIZE or with a NULL worker
  80. INTERVAL_FRAME_ALLOC, ///< Time spent allocating memory from worker buckets
  81. INTERVAL_FRAME_ALLOC_GLOBAL, ///< Time spent calling memory allocator when buckets are empty
  82. INTERVAL_FRAME_FREE_LARGE, ///< Count of calls to __cilkrts_frame_malloc for buffers bigger than FRAME_MALLOC_MAX_SIZE or with a NULL worker
  83. INTERVAL_FRAME_FREE, ///< Time spent freeing memory to worker buckets
  84. INTERVAL_FRAME_FREE_GLOBAL, ///< Time spent calling memory deallocator when buckets are full
  85. INTERVAL_MUTEX_LOCK, ///< Count of calls to __cilkrts_mutex_lock for a worker
  86. INTERVAL_MUTEX_LOCK_SPINNING, ///< Time spent spinning in __cilkrts_mutex_lock for a worker
  87. INTERVAL_MUTEX_LOCK_YIELDING, ///< Time spent yielding in __cilkrts_mutex_lock for a worker
  88. INTERVAL_MUTEX_TRYLOCK, ///< Count of calls to __cilkrts_mutex_trylock
  89. INTERVAL_FIBER_ALLOCATE, ///< Time spent calling cilk_fiber_allocate
  90. INTERVAL_FIBER_DEALLOCATE, ///< Time spent calling cilk_fiber_deallocate (not from thread)
  91. INTERVAL_FIBER_ALLOCATE_FROM_THREAD, ///< Time spent calling cilk_fiber_allocate_from_thread
  92. INTERVAL_FIBER_DEALLOCATE_FROM_THREAD, ///< Time spent calling cilk_fiber_deallocate (from thread)
  93. INTERVAL_SUSPEND_RESUME_OTHER, ///< Count of fiber suspend_self_and_resume_other
  94. INTERVAL_DEALLOCATE_RESUME_OTHER, ///< Count of fiber deallocate_self_and_resume_other
  95. INTERVAL_N ///< Number of intervals, must be last
  96. };
  97. /**
  98. * @brief Struct that collects of all runtime statistics.
  99. *
  100. * There is an instance of this structure in each worker's
  101. * local_state, as well as one in the @c global_state_t which will be
  102. * used to accumulate the per-worker stats.
  103. */
  104. typedef struct statistics
  105. {
  106. /** Number of times each interval is entered */
  107. unsigned long long count[INTERVAL_N];
  108. /**
  109. * Time when the system entered each interval, in system-dependent
  110. * "ticks"
  111. */
  112. unsigned long long start[INTERVAL_N];
  113. /** Total time spent in each interval, in system-dependent "ticks" */
  114. unsigned long long accum[INTERVAL_N];
  115. /**
  116. * Largest global number of stacks seen by this worker.
  117. * The true maximum at end of execution is the max of the
  118. * worker maxima.
  119. */
  120. long stack_hwm;
  121. } statistics;
  122. /**
  123. * Initializes a statistics structure
  124. *
  125. * @param s The statistics structure to be initialized.
  126. */
  127. COMMON_PORTABLE void __cilkrts_init_stats(statistics *s);
  128. /**
  129. * @brief Sums statistics from worker to the global struct
  130. *
  131. * @param to The statistics structure that will accumulate the information.
  132. * This structure is usually @c g->stats.
  133. * @param from The statistics structure that will be accumulated.
  134. * This structure is usually statistics kept per worker.
  135. */
  136. COMMON_PORTABLE
  137. void __cilkrts_accum_stats(statistics *to, statistics *from);
  138. /**
  139. * @brief Mark the start of an interval by saving the current tick count.
  140. *
  141. * @pre Start time == INVALID_START
  142. *
  143. * @param w The worker we're accumulating stats for.
  144. * @param i The interval we're accumulating stats for.
  145. */
  146. COMMON_PORTABLE
  147. void __cilkrts_start_interval(__cilkrts_worker *w, enum interval i);
  148. /**
  149. * @brief Mark the end of an interval by adding the ticks since the
  150. * start to the accumulated time.
  151. *
  152. * @pre Start time != INVALID_START
  153. *
  154. * @param w The worker we're accumulating stats for.
  155. * @param i The interval we're accumulating stats for.
  156. */
  157. COMMON_PORTABLE
  158. void __cilkrts_stop_interval(__cilkrts_worker *w, enum interval i);
  159. /**
  160. * @brief Start and stop interval I, charging zero time against it
  161. *
  162. * Precondition:
  163. * - Start time == INVALID_START
  164. *
  165. * @param w The worker we're accumulating stats for.
  166. * @param i The interval we're accumulating stats for.
  167. */
  168. COMMON_PORTABLE
  169. void __cilkrts_note_interval(__cilkrts_worker *w, enum interval i);
  170. #ifdef CILK_PROFILE
  171. COMMON_PORTABLE
  172. void dump_stats_to_file(FILE *stat_file, statistics *s);
  173. #endif
  174. #ifdef CILK_PROFILE
  175. # define START_INTERVAL(w, i) __cilkrts_start_interval(w, i);
  176. # define STOP_INTERVAL(w, i) __cilkrts_stop_interval(w, i);
  177. # define NOTE_INTERVAL(w, i) __cilkrts_note_interval(w, i);
  178. #else
  179. /** Start an interval. No effect unless CILK_PROFILE is defined. */
  180. # define START_INTERVAL(w, i)
  181. /** End an interval. No effect unless CILK_PROFILE is defined. */
  182. # define STOP_INTERVAL(w, i)
  183. /** Increment a counter. No effect unless CILK_PROFILE is defined. */
  184. # define NOTE_INTERVAL(w, i)
  185. #endif
  186. __CILKRTS_END_EXTERN_C
  187. #endif // ! defined(INCLUDED_STATS_DOT_H)