timevar.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Timing variables for measuring compiler performance.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. Contributed by Alex Samuel <samuel@codesourcery.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_TIMEVAR_H
  17. #define GCC_TIMEVAR_H
  18. /* Timing variables are used to measure elapsed time in various
  19. portions of the compiler. Each measures elapsed user, system, and
  20. wall-clock time, as appropriate to and supported by the host
  21. system.
  22. Timing variables are defined using the DEFTIMEVAR macro in
  23. timevar.def. Each has an enumeral identifier, used when referring
  24. to the timing variable in code, and a character string name.
  25. Timing variables can be used in two ways:
  26. - On the timing stack, using timevar_push and timevar_pop.
  27. Timing variables may be pushed onto the stack; elapsed time is
  28. attributed to the topmost timing variable on the stack. When
  29. another variable is pushed on, the previous topmost variable is
  30. `paused' until the pushed variable is popped back off.
  31. - As a standalone timer, using timevar_start and timevar_stop.
  32. All time elapsed between the two calls is attributed to the
  33. variable.
  34. */
  35. /* This structure stores the various varieties of time that can be
  36. measured. Times are stored in seconds. The time may be an
  37. absolute time or a time difference; in the former case, the time
  38. base is undefined, except that the difference between two times
  39. produces a valid time difference. */
  40. struct timevar_time_def
  41. {
  42. /* User time in this process. */
  43. double user;
  44. /* System time (if applicable for this host platform) in this
  45. process. */
  46. double sys;
  47. /* Wall clock time. */
  48. double wall;
  49. /* Garbage collector memory. */
  50. size_t ggc_mem;
  51. };
  52. /* An enumeration of timing variable identifiers. Constructed from
  53. the contents of timevar.def. */
  54. #define DEFTIMEVAR(identifier__, name__) \
  55. identifier__,
  56. typedef enum
  57. {
  58. TV_NONE,
  59. #include "timevar.def"
  60. TIMEVAR_LAST
  61. }
  62. timevar_id_t;
  63. #undef DEFTIMEVAR
  64. /* A class to hold all state relating to timing. */
  65. class timer;
  66. /* The singleton instance of timing state.
  67. This is non-NULL if timevars should be used. In GCC, this happens with
  68. the -ftime-report flag. Hence this is NULL for the common,
  69. needs-to-be-fast case, with an early reject happening for this being
  70. NULL. */
  71. extern timer *g_timer;
  72. /* Total amount of memory allocated by garbage collector. */
  73. extern size_t timevar_ggc_mem_total;
  74. extern void timevar_init (void);
  75. extern void timevar_start (timevar_id_t);
  76. extern void timevar_stop (timevar_id_t);
  77. extern bool timevar_cond_start (timevar_id_t);
  78. extern void timevar_cond_stop (timevar_id_t, bool);
  79. /* The public (within GCC) interface for timing. */
  80. class timer
  81. {
  82. public:
  83. timer ();
  84. ~timer ();
  85. void start (timevar_id_t tv);
  86. void stop (timevar_id_t tv);
  87. void push (timevar_id_t tv);
  88. void pop (timevar_id_t tv);
  89. bool cond_start (timevar_id_t tv);
  90. void cond_stop (timevar_id_t tv);
  91. void push_client_item (const char *item_name);
  92. void pop_client_item ();
  93. void print (FILE *fp);
  94. const char *get_topmost_item_name () const;
  95. private:
  96. /* Private member functions. */
  97. void validate_phases (FILE *fp) const;
  98. struct timevar_def;
  99. void push_internal (struct timevar_def *tv);
  100. void pop_internal ();
  101. static void print_row (FILE *fp,
  102. const timevar_time_def *total,
  103. const char *name, const timevar_time_def &elapsed);
  104. static bool all_zero (const timevar_time_def &elapsed);
  105. private:
  106. typedef hash_map<timevar_def *, timevar_time_def> child_map_t;
  107. /* Private type: a timing variable. */
  108. struct timevar_def
  109. {
  110. /* Elapsed time for this variable. */
  111. struct timevar_time_def elapsed;
  112. /* If this variable is timed independently of the timing stack,
  113. using timevar_start, this contains the start time. */
  114. struct timevar_time_def start_time;
  115. /* The name of this timing variable. */
  116. const char *name;
  117. /* Nonzero if this timing variable is running as a standalone
  118. timer. */
  119. unsigned standalone : 1;
  120. /* Nonzero if this timing variable was ever started or pushed onto
  121. the timing stack. */
  122. unsigned used : 1;
  123. child_map_t *children;
  124. };
  125. /* Private type: an element on the timing stack
  126. Elapsed time is attributed to the topmost timing variable on the
  127. stack. */
  128. struct timevar_stack_def
  129. {
  130. /* The timing variable at this stack level. */
  131. struct timevar_def *timevar;
  132. /* The next lower timing variable context in the stack. */
  133. struct timevar_stack_def *next;
  134. };
  135. /* A class for managing a collection of named timing items, for use
  136. e.g. by libgccjit for timing client code. This class is declared
  137. inside timevar.c to avoid everything using timevar.h
  138. from needing vec and hash_map. */
  139. class named_items;
  140. private:
  141. /* Data members (all private). */
  142. /* Declared timing variables. Constructed from the contents of
  143. timevar.def. */
  144. timevar_def m_timevars[TIMEVAR_LAST];
  145. /* The top of the timing stack. */
  146. timevar_stack_def *m_stack;
  147. /* A list of unused (i.e. allocated and subsequently popped)
  148. timevar_stack_def instances. */
  149. timevar_stack_def *m_unused_stack_instances;
  150. /* The time at which the topmost element on the timing stack was
  151. pushed. Time elapsed since then is attributed to the topmost
  152. element. */
  153. timevar_time_def m_start_time;
  154. /* If non-NULL, for use when timing libgccjit's client code. */
  155. named_items *m_jit_client_items;
  156. friend class named_items;
  157. };
  158. /* Provided for backward compatibility. */
  159. static inline void
  160. timevar_push (timevar_id_t tv)
  161. {
  162. if (g_timer)
  163. g_timer->push (tv);
  164. }
  165. static inline void
  166. timevar_pop (timevar_id_t tv)
  167. {
  168. if (g_timer)
  169. g_timer->pop (tv);
  170. }
  171. // This is a simple timevar wrapper class that pushes a timevar in its
  172. // constructor and pops the timevar in its destructor.
  173. class auto_timevar
  174. {
  175. public:
  176. auto_timevar (timer *t, timevar_id_t tv)
  177. : m_timer (t),
  178. m_tv (tv)
  179. {
  180. if (m_timer)
  181. m_timer->push (m_tv);
  182. }
  183. explicit auto_timevar (timevar_id_t tv)
  184. : m_timer (g_timer)
  185. , m_tv (tv)
  186. {
  187. if (m_timer)
  188. m_timer->push (m_tv);
  189. }
  190. ~auto_timevar ()
  191. {
  192. if (m_timer)
  193. m_timer->pop (m_tv);
  194. }
  195. private:
  196. // Private to disallow copies.
  197. auto_timevar (const auto_timevar &);
  198. timer *m_timer;
  199. timevar_id_t m_tv;
  200. };
  201. extern void print_time (const char *, long);
  202. #endif /* ! GCC_TIMEVAR_H */