timevar.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* Timing variables for measuring compiler performance.
  2. Copyright (C) 2000, 2002, 2004, 2006, 2009-2015, 2018-2020 Free Software
  3. Foundation, Inc.
  4. Contributed by Alex Samuel <samuel@codesourcery.com>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. /* Specification. */
  17. #include "timevar.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "gethrxtime.h"
  22. #include "gettext.h"
  23. #define _(msgid) gettext (msgid)
  24. #include "xalloc.h"
  25. /* See timevar.h for an explanation of timing variables. */
  26. int timevar_enabled = 0;
  27. /* A timing variable. */
  28. struct timevar_def
  29. {
  30. /* Elapsed time for this variable. */
  31. struct timevar_time_def elapsed;
  32. /* If this variable is timed independently of the timing stack,
  33. using timevar_start, this contains the start time. */
  34. struct timevar_time_def start_time;
  35. /* The name of this timing variable. */
  36. const char *name;
  37. /* Non-zero if this timing variable is running as a standalone
  38. timer. */
  39. unsigned standalone : 1;
  40. /* Non-zero if this timing variable was ever started or pushed onto
  41. the timing stack. */
  42. unsigned used : 1;
  43. };
  44. /* An element on the timing stack. Elapsed time is attributed to the
  45. topmost timing variable on the stack. */
  46. struct timevar_stack_def
  47. {
  48. /* The timing variable at this stack level. */
  49. struct timevar_def *timevar;
  50. /* The next lower timing variable context in the stack. */
  51. struct timevar_stack_def *next;
  52. };
  53. /* Declared timing variables. Constructed from the contents of
  54. timevar.def. */
  55. static struct timevar_def timevars[TIMEVAR_LAST];
  56. /* The top of the timing stack. */
  57. static struct timevar_stack_def *stack;
  58. /* A list of unused (i.e. allocated and subsequently popped)
  59. timevar_stack_def instances. */
  60. static struct timevar_stack_def *unused_stack_instances;
  61. /* The time at which the topmost element on the timing stack was
  62. pushed. Time elapsed since then is attributed to the topmost
  63. element. */
  64. static struct timevar_time_def start_time;
  65. /* Fill the current times into TIME. */
  66. static void
  67. set_to_current_time (struct timevar_time_def *now)
  68. {
  69. now->user = 0;
  70. now->sys = 0;
  71. now->wall = 0;
  72. if (!timevar_enabled)
  73. return;
  74. /*
  75. struct rusage self;
  76. getrusage (RUSAGE_SELF, &self);
  77. struct rusage chld;
  78. getrusage (RUSAGE_CHILDREN, &chld);
  79. now->user =
  80. xtime_make (self.ru_utime.tv_sec + chld.ru_utime.tv_sec,
  81. (self.ru_utime.tv_usec + chld.ru_utime.tv_usec) * 1000);
  82. now->sys =
  83. xtime_make (self.ru_stime.tv_sec + chld.ru_stime.tv_sec,
  84. (self.ru_stime.tv_usec + chld.ru_stime.tv_usec) * 1000);
  85. */
  86. now->wall = gethrxtime();
  87. }
  88. /* Return the current time. */
  89. static struct timevar_time_def
  90. get_current_time (void)
  91. {
  92. struct timevar_time_def now;
  93. set_to_current_time (&now);
  94. return now;
  95. }
  96. /* Add the difference between STOP and START to TIMER. */
  97. static void
  98. timevar_accumulate (struct timevar_time_def *timer,
  99. const struct timevar_time_def *start,
  100. const struct timevar_time_def *stop)
  101. {
  102. timer->user += stop->user - start->user;
  103. timer->sys += stop->sys - start->sys;
  104. timer->wall += stop->wall - start->wall;
  105. }
  106. void
  107. timevar_init ()
  108. {
  109. if (!timevar_enabled)
  110. return;
  111. /* Zero all elapsed times. */
  112. memset ((void *) timevars, 0, sizeof (timevars));
  113. /* Initialize the names of timing variables. */
  114. #define DEFTIMEVAR(identifier__, name__) \
  115. timevars[identifier__].name = name__;
  116. #include "timevar.def"
  117. #undef DEFTIMEVAR
  118. }
  119. void
  120. timevar_push (timevar_id_t timevar)
  121. {
  122. if (!timevar_enabled)
  123. return;
  124. struct timevar_def *tv = &timevars[timevar];
  125. /* Mark this timing variable as used. */
  126. tv->used = 1;
  127. /* Can't push a standalone timer. */
  128. if (tv->standalone)
  129. abort ();
  130. /* What time is it? */
  131. struct timevar_time_def const now = get_current_time ();
  132. /* If the stack isn't empty, attribute the current elapsed time to
  133. the old topmost element. */
  134. if (stack)
  135. timevar_accumulate (&stack->timevar->elapsed, &start_time, &now);
  136. /* Reset the start time; from now on, time is attributed to
  137. TIMEVAR. */
  138. start_time = now;
  139. /* See if we have a previously-allocated stack instance. If so,
  140. take it off the list. If not, malloc a new one. */
  141. struct timevar_stack_def *context = NULL;
  142. if (unused_stack_instances != NULL)
  143. {
  144. context = unused_stack_instances;
  145. unused_stack_instances = unused_stack_instances->next;
  146. }
  147. else
  148. context = (struct timevar_stack_def *)
  149. xmalloc (sizeof (struct timevar_stack_def));
  150. /* Fill it in and put it on the stack. */
  151. context->timevar = tv;
  152. context->next = stack;
  153. stack = context;
  154. }
  155. void
  156. timevar_pop (timevar_id_t timevar)
  157. {
  158. if (!timevar_enabled)
  159. return;
  160. if (&timevars[timevar] != stack->timevar)
  161. abort ();
  162. /* What time is it? */
  163. struct timevar_time_def const now = get_current_time ();
  164. /* Attribute the elapsed time to the element we're popping. */
  165. struct timevar_stack_def *popped = stack;
  166. timevar_accumulate (&popped->timevar->elapsed, &start_time, &now);
  167. /* Reset the start time; from now on, time is attributed to the
  168. element just exposed on the stack. */
  169. start_time = now;
  170. /* Take the item off the stack. */
  171. stack = stack->next;
  172. /* Don't delete the stack element; instead, add it to the list of
  173. unused elements for later use. */
  174. popped->next = unused_stack_instances;
  175. unused_stack_instances = popped;
  176. }
  177. void
  178. timevar_start (timevar_id_t timevar)
  179. {
  180. if (!timevar_enabled)
  181. return;
  182. struct timevar_def *tv = &timevars[timevar];
  183. /* Mark this timing variable as used. */
  184. tv->used = 1;
  185. /* Don't allow the same timing variable to be started more than
  186. once. */
  187. if (tv->standalone)
  188. abort ();
  189. tv->standalone = 1;
  190. set_to_current_time (&tv->start_time);
  191. }
  192. void
  193. timevar_stop (timevar_id_t timevar)
  194. {
  195. if (!timevar_enabled)
  196. return;
  197. struct timevar_def *tv = &timevars[timevar];
  198. /* TIMEVAR must have been started via timevar_start. */
  199. if (!tv->standalone)
  200. abort ();
  201. struct timevar_time_def const now = get_current_time ();
  202. timevar_accumulate (&tv->elapsed, &tv->start_time, &now);
  203. }
  204. void
  205. timevar_get (timevar_id_t timevar,
  206. struct timevar_time_def *elapsed)
  207. {
  208. struct timevar_def *tv = &timevars[timevar];
  209. *elapsed = tv->elapsed;
  210. /* Is TIMEVAR currently running as a standalone timer? */
  211. if (tv->standalone)
  212. {
  213. struct timevar_time_def const now = get_current_time ();
  214. timevar_accumulate (elapsed, &tv->start_time, &now);
  215. }
  216. /* Or is TIMEVAR at the top of the timer stack? */
  217. else if (stack->timevar == tv)
  218. {
  219. struct timevar_time_def const now = get_current_time ();
  220. timevar_accumulate (elapsed, &start_time, &now);
  221. }
  222. }
  223. void
  224. timevar_print (FILE *fp)
  225. {
  226. if (!timevar_enabled)
  227. return;
  228. /* Update timing information in case we're calling this from GDB. */
  229. if (fp == 0)
  230. fp = stderr;
  231. /* What time is it? */
  232. struct timevar_time_def const now = get_current_time ();
  233. /* If the stack isn't empty, attribute the current elapsed time to
  234. the old topmost element. */
  235. if (stack)
  236. timevar_accumulate (&stack->timevar->elapsed, &start_time, &now);
  237. /* Reset the start time; from now on, time is attributed to
  238. TIMEVAR. */
  239. start_time = now;
  240. struct timevar_time_def const* total = &timevars[tv_total].elapsed;
  241. fprintf (fp, "%-22s\n",
  242. _("Execution times (seconds)"));
  243. fprintf (fp, " %-22s %-13s %-13s %-16s\n",
  244. "", _("CPU user"), _("CPU system"), _("wall clock"));
  245. for (unsigned /* timevar_id_t */ id = 0; id < (unsigned) TIMEVAR_LAST; ++id)
  246. {
  247. /* Don't print the total execution time here; that goes at the
  248. end. */
  249. if ((timevar_id_t) id == tv_total)
  250. continue;
  251. /* Don't print timing variables that were never used. */
  252. struct timevar_def *tv = &timevars[(timevar_id_t) id];
  253. if (!tv->used)
  254. continue;
  255. /* Percentages. */
  256. const int usr = total->user ? tv->elapsed.user * 100 / total->user : 0;
  257. const int sys = total->sys ? tv->elapsed.sys * 100 / total->sys : 0;
  258. const int wall = total->wall ? tv->elapsed.wall * 100 / total->wall : 0;
  259. /* Ignore insignificant lines. */
  260. if (!usr && !sys && !wall)
  261. continue;
  262. fprintf (fp, " %-22s", tv->name);
  263. fprintf (fp, "%8.3f (%2d%%)", tv->elapsed.user * 1e-9, usr);
  264. fprintf (fp, "%8.3f (%2d%%)", tv->elapsed.sys * 1e-9, sys);
  265. fprintf (fp, "%11.6f (%2d%%)\n", tv->elapsed.wall * 1e-9, wall);
  266. }
  267. /* Print total time. */
  268. fprintf (fp, " %-22s", timevars[tv_total].name);
  269. fprintf (fp, "%8.3f ", total->user * 1e-9);
  270. fprintf (fp, "%8.3f ", total->sys * 1e-9);
  271. fprintf (fp, "%11.6f\n", total->wall * 1e-9);
  272. }