timevar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* Timing variables for measuring compiler performance.
  2. Copyright (C) 2000-2015 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 under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. 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. #include "config.h"
  17. #include "system.h"
  18. #include "timevar.h"
  19. #ifndef HAVE_CLOCK_T
  20. typedef int clock_t;
  21. #endif
  22. #ifndef HAVE_STRUCT_TMS
  23. struct tms
  24. {
  25. clock_t tms_utime;
  26. clock_t tms_stime;
  27. clock_t tms_cutime;
  28. clock_t tms_cstime;
  29. };
  30. #endif
  31. #ifndef RUSAGE_SELF
  32. # define RUSAGE_SELF 0
  33. #endif
  34. /* Calculation of scale factor to convert ticks to microseconds.
  35. We mustn't use CLOCKS_PER_SEC except with clock(). */
  36. #if HAVE_SYSCONF && defined _SC_CLK_TCK
  37. # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
  38. #else
  39. # ifdef CLK_TCK
  40. # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
  41. # else
  42. # ifdef HZ
  43. # define TICKS_PER_SECOND HZ /* traditional UNIX */
  44. # else
  45. # define TICKS_PER_SECOND 100 /* often the correct value */
  46. # endif
  47. # endif
  48. #endif
  49. /* Prefer times to getrusage to clock (each gives successively less
  50. information). */
  51. #ifdef HAVE_TIMES
  52. # if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
  53. extern clock_t times (struct tms *);
  54. # endif
  55. # define USE_TIMES
  56. # define HAVE_USER_TIME
  57. # define HAVE_SYS_TIME
  58. # define HAVE_WALL_TIME
  59. #else
  60. #ifdef HAVE_GETRUSAGE
  61. # if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
  62. extern int getrusage (int, struct rusage *);
  63. # endif
  64. # define USE_GETRUSAGE
  65. # define HAVE_USER_TIME
  66. # define HAVE_SYS_TIME
  67. #else
  68. #ifdef HAVE_CLOCK
  69. # if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
  70. extern clock_t clock (void);
  71. # endif
  72. # define USE_CLOCK
  73. # define HAVE_USER_TIME
  74. #endif
  75. #endif
  76. #endif
  77. /* libc is very likely to have snuck a call to sysconf() into one of
  78. the underlying constants, and that can be very slow, so we have to
  79. precompute them. Whose wonderful idea was it to make all those
  80. _constants_ variable at run time, anyway? */
  81. #ifdef USE_TIMES
  82. static double ticks_to_msec;
  83. #define TICKS_TO_MSEC (1 / (double)TICKS_PER_SECOND)
  84. #endif
  85. #ifdef USE_CLOCK
  86. static double clocks_to_msec;
  87. #define CLOCKS_TO_MSEC (1 / (double)CLOCKS_PER_SEC)
  88. #endif
  89. /* True if timevars should be used. In GCC, this happens with
  90. the -ftime-report flag. */
  91. bool timevar_enable;
  92. /* Total amount of memory allocated by garbage collector. */
  93. size_t timevar_ggc_mem_total;
  94. /* The amount of memory that will cause us to report the timevar even
  95. if the time spent is not significant. */
  96. #define GGC_MEM_BOUND (1 << 20)
  97. /* See timevar.h for an explanation of timing variables. */
  98. /* A timing variable. */
  99. struct timevar_def
  100. {
  101. /* Elapsed time for this variable. */
  102. struct timevar_time_def elapsed;
  103. /* If this variable is timed independently of the timing stack,
  104. using timevar_start, this contains the start time. */
  105. struct timevar_time_def start_time;
  106. /* The name of this timing variable. */
  107. const char *name;
  108. /* Nonzero if this timing variable is running as a standalone
  109. timer. */
  110. unsigned standalone : 1;
  111. /* Nonzero if this timing variable was ever started or pushed onto
  112. the timing stack. */
  113. unsigned used : 1;
  114. };
  115. /* An element on the timing stack. Elapsed time is attributed to the
  116. topmost timing variable on the stack. */
  117. struct timevar_stack_def
  118. {
  119. /* The timing variable at this stack level. */
  120. struct timevar_def *timevar;
  121. /* The next lower timing variable context in the stack. */
  122. struct timevar_stack_def *next;
  123. };
  124. /* Declared timing variables. Constructed from the contents of
  125. timevar.def. */
  126. static struct timevar_def timevars[TIMEVAR_LAST];
  127. /* The top of the timing stack. */
  128. static struct timevar_stack_def *stack;
  129. /* A list of unused (i.e. allocated and subsequently popped)
  130. timevar_stack_def instances. */
  131. static struct timevar_stack_def *unused_stack_instances;
  132. /* The time at which the topmost element on the timing stack was
  133. pushed. Time elapsed since then is attributed to the topmost
  134. element. */
  135. static struct timevar_time_def start_time;
  136. static void get_time (struct timevar_time_def *);
  137. static void timevar_accumulate (struct timevar_time_def *,
  138. struct timevar_time_def *,
  139. struct timevar_time_def *);
  140. /* Fill the current times into TIME. The definition of this function
  141. also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
  142. HAVE_WALL_TIME macros. */
  143. static void
  144. get_time (struct timevar_time_def *now)
  145. {
  146. now->user = 0;
  147. now->sys = 0;
  148. now->wall = 0;
  149. now->ggc_mem = timevar_ggc_mem_total;
  150. if (!timevar_enable)
  151. return;
  152. {
  153. #ifdef USE_TIMES
  154. struct tms tms;
  155. now->wall = times (&tms) * ticks_to_msec;
  156. now->user = tms.tms_utime * ticks_to_msec;
  157. now->sys = tms.tms_stime * ticks_to_msec;
  158. #endif
  159. #ifdef USE_GETRUSAGE
  160. struct rusage rusage;
  161. getrusage (RUSAGE_SELF, &rusage);
  162. now->user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6;
  163. now->sys = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6;
  164. #endif
  165. #ifdef USE_CLOCK
  166. now->user = clock () * clocks_to_msec;
  167. #endif
  168. }
  169. }
  170. /* Add the difference between STOP_TIME and START_TIME to TIMER. */
  171. static void
  172. timevar_accumulate (struct timevar_time_def *timer,
  173. struct timevar_time_def *start_time,
  174. struct timevar_time_def *stop_time)
  175. {
  176. timer->user += stop_time->user - start_time->user;
  177. timer->sys += stop_time->sys - start_time->sys;
  178. timer->wall += stop_time->wall - start_time->wall;
  179. timer->ggc_mem += stop_time->ggc_mem - start_time->ggc_mem;
  180. }
  181. /* Initialize timing variables. */
  182. void
  183. timevar_init (void)
  184. {
  185. if (timevar_enable)
  186. return;
  187. timevar_enable = true;
  188. /* Zero all elapsed times. */
  189. memset (timevars, 0, sizeof (timevars));
  190. /* Initialize the names of timing variables. */
  191. #define DEFTIMEVAR(identifier__, name__) \
  192. timevars[identifier__].name = name__;
  193. #include "timevar.def"
  194. #undef DEFTIMEVAR
  195. #ifdef USE_TIMES
  196. ticks_to_msec = TICKS_TO_MSEC;
  197. #endif
  198. #ifdef USE_CLOCK
  199. clocks_to_msec = CLOCKS_TO_MSEC;
  200. #endif
  201. }
  202. /* Push TIMEVAR onto the timing stack. No further elapsed time is
  203. attributed to the previous topmost timing variable on the stack;
  204. subsequent elapsed time is attributed to TIMEVAR, until it is
  205. popped or another element is pushed on top.
  206. TIMEVAR cannot be running as a standalone timer. */
  207. void
  208. timevar_push_1 (timevar_id_t timevar)
  209. {
  210. struct timevar_def *tv = &timevars[timevar];
  211. struct timevar_stack_def *context;
  212. struct timevar_time_def now;
  213. /* Mark this timing variable as used. */
  214. tv->used = 1;
  215. /* Can't push a standalone timer. */
  216. gcc_assert (!tv->standalone);
  217. /* What time is it? */
  218. get_time (&now);
  219. /* If the stack isn't empty, attribute the current elapsed time to
  220. the old topmost element. */
  221. if (stack)
  222. timevar_accumulate (&stack->timevar->elapsed, &start_time, &now);
  223. /* Reset the start time; from now on, time is attributed to
  224. TIMEVAR. */
  225. start_time = now;
  226. /* See if we have a previously-allocated stack instance. If so,
  227. take it off the list. If not, malloc a new one. */
  228. if (unused_stack_instances != NULL)
  229. {
  230. context = unused_stack_instances;
  231. unused_stack_instances = unused_stack_instances->next;
  232. }
  233. else
  234. context = XNEW (struct timevar_stack_def);
  235. /* Fill it in and put it on the stack. */
  236. context->timevar = tv;
  237. context->next = stack;
  238. stack = context;
  239. }
  240. /* Pop the topmost timing variable element off the timing stack. The
  241. popped variable must be TIMEVAR. Elapsed time since the that
  242. element was pushed on, or since it was last exposed on top of the
  243. stack when the element above it was popped off, is credited to that
  244. timing variable. */
  245. void
  246. timevar_pop_1 (timevar_id_t timevar)
  247. {
  248. struct timevar_time_def now;
  249. struct timevar_stack_def *popped = stack;
  250. gcc_assert (&timevars[timevar] == stack->timevar);
  251. /* What time is it? */
  252. get_time (&now);
  253. /* Attribute the elapsed time to the element we're popping. */
  254. timevar_accumulate (&popped->timevar->elapsed, &start_time, &now);
  255. /* Reset the start time; from now on, time is attributed to the
  256. element just exposed on the stack. */
  257. start_time = now;
  258. /* Take the item off the stack. */
  259. stack = stack->next;
  260. /* Don't delete the stack element; instead, add it to the list of
  261. unused elements for later use. */
  262. popped->next = unused_stack_instances;
  263. unused_stack_instances = popped;
  264. }
  265. /* Start timing TIMEVAR independently of the timing stack. Elapsed
  266. time until timevar_stop is called for the same timing variable is
  267. attributed to TIMEVAR. */
  268. void
  269. timevar_start (timevar_id_t timevar)
  270. {
  271. struct timevar_def *tv = &timevars[timevar];
  272. if (!timevar_enable)
  273. return;
  274. /* Mark this timing variable as used. */
  275. tv->used = 1;
  276. /* Don't allow the same timing variable to be started more than
  277. once. */
  278. gcc_assert (!tv->standalone);
  279. tv->standalone = 1;
  280. get_time (&tv->start_time);
  281. }
  282. /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
  283. is attributed to it. */
  284. void
  285. timevar_stop (timevar_id_t timevar)
  286. {
  287. struct timevar_def *tv = &timevars[timevar];
  288. struct timevar_time_def now;
  289. if (!timevar_enable)
  290. return;
  291. /* TIMEVAR must have been started via timevar_start. */
  292. gcc_assert (tv->standalone);
  293. tv->standalone = 0; /* Enable a restart. */
  294. get_time (&now);
  295. timevar_accumulate (&tv->elapsed, &tv->start_time, &now);
  296. }
  297. /* Conditionally start timing TIMEVAR independently of the timing stack.
  298. If the timer is already running, leave it running and return true.
  299. Otherwise, start the timer and return false.
  300. Elapsed time until the corresponding timevar_cond_stop
  301. is called for the same timing variable is attributed to TIMEVAR. */
  302. bool
  303. timevar_cond_start (timevar_id_t timevar)
  304. {
  305. struct timevar_def *tv = &timevars[timevar];
  306. if (!timevar_enable)
  307. return false;
  308. /* Mark this timing variable as used. */
  309. tv->used = 1;
  310. if (tv->standalone)
  311. return true; /* The timevar is already running. */
  312. /* Don't allow the same timing variable
  313. to be unconditionally started more than once. */
  314. tv->standalone = 1;
  315. get_time (&tv->start_time);
  316. return false; /* The timevar was not already running. */
  317. }
  318. /* Conditionally stop timing TIMEVAR. The RUNNING parameter must come
  319. from the return value of a dynamically matching timevar_cond_start.
  320. If the timer had already been RUNNING, do nothing. Otherwise, time
  321. elapsed since timevar_cond_start was called is attributed to it. */
  322. void
  323. timevar_cond_stop (timevar_id_t timevar, bool running)
  324. {
  325. struct timevar_def *tv;
  326. struct timevar_time_def now;
  327. if (!timevar_enable || running)
  328. return;
  329. tv = &timevars[timevar];
  330. /* TIMEVAR must have been started via timevar_cond_start. */
  331. gcc_assert (tv->standalone);
  332. tv->standalone = 0; /* Enable a restart. */
  333. get_time (&now);
  334. timevar_accumulate (&tv->elapsed, &tv->start_time, &now);
  335. }
  336. /* Validate that phase times are consistent. */
  337. static void
  338. validate_phases (FILE *fp)
  339. {
  340. unsigned int /* timevar_id_t */ id;
  341. struct timevar_time_def *total = &timevars[TV_TOTAL].elapsed;
  342. double phase_user = 0.0;
  343. double phase_sys = 0.0;
  344. double phase_wall = 0.0;
  345. size_t phase_ggc_mem = 0;
  346. static char phase_prefix[] = "phase ";
  347. const double tolerance = 1.000001; /* One part in a million. */
  348. for (id = 0; id < (unsigned int) TIMEVAR_LAST; ++id)
  349. {
  350. struct timevar_def *tv = &timevars[(timevar_id_t) id];
  351. /* Don't evaluate timing variables that were never used. */
  352. if (!tv->used)
  353. continue;
  354. if (strncmp (tv->name, phase_prefix, sizeof phase_prefix - 1) == 0)
  355. {
  356. phase_user += tv->elapsed.user;
  357. phase_sys += tv->elapsed.sys;
  358. phase_wall += tv->elapsed.wall;
  359. phase_ggc_mem += tv->elapsed.ggc_mem;
  360. }
  361. }
  362. if (phase_user > total->user * tolerance
  363. || phase_sys > total->sys * tolerance
  364. || phase_wall > total->wall * tolerance
  365. || phase_ggc_mem > total->ggc_mem * tolerance)
  366. {
  367. fprintf (fp, "Timing error: total of phase timers exceeds total time.\n");
  368. if (phase_user > total->user)
  369. fprintf (fp, "user %24.18e > %24.18e\n", phase_user, total->user);
  370. if (phase_sys > total->sys)
  371. fprintf (fp, "sys %24.18e > %24.18e\n", phase_sys, total->sys);
  372. if (phase_wall > total->wall)
  373. fprintf (fp, "wall %24.18e > %24.18e\n", phase_wall, total->wall);
  374. if (phase_ggc_mem > total->ggc_mem)
  375. fprintf (fp, "ggc_mem %24lu > %24lu\n", (unsigned long)phase_ggc_mem,
  376. (unsigned long)total->ggc_mem);
  377. gcc_unreachable ();
  378. }
  379. }
  380. /* Summarize timing variables to FP. The timing variable TV_TOTAL has
  381. a special meaning -- it's considered to be the total elapsed time,
  382. for normalizing the others, and is displayed last. */
  383. void
  384. timevar_print (FILE *fp)
  385. {
  386. /* Only print stuff if we have some sort of time information. */
  387. #if defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME) || defined (HAVE_WALL_TIME)
  388. unsigned int /* timevar_id_t */ id;
  389. struct timevar_time_def *total = &timevars[TV_TOTAL].elapsed;
  390. struct timevar_time_def now;
  391. if (!timevar_enable)
  392. return;
  393. /* Update timing information in case we're calling this from GDB. */
  394. if (fp == 0)
  395. fp = stderr;
  396. /* What time is it? */
  397. get_time (&now);
  398. /* If the stack isn't empty, attribute the current elapsed time to
  399. the old topmost element. */
  400. if (stack)
  401. timevar_accumulate (&stack->timevar->elapsed, &start_time, &now);
  402. /* Reset the start time; from now on, time is attributed to
  403. TIMEVAR. */
  404. start_time = now;
  405. fputs ("\nExecution times (seconds)\n", fp);
  406. for (id = 0; id < (unsigned int) TIMEVAR_LAST; ++id)
  407. {
  408. struct timevar_def *tv = &timevars[(timevar_id_t) id];
  409. const double tiny = 5e-3;
  410. /* Don't print the total execution time here; that goes at the
  411. end. */
  412. if ((timevar_id_t) id == TV_TOTAL)
  413. continue;
  414. /* Don't print timing variables that were never used. */
  415. if (!tv->used)
  416. continue;
  417. /* Don't print timing variables if we're going to get a row of
  418. zeroes. */
  419. if (tv->elapsed.user < tiny
  420. && tv->elapsed.sys < tiny
  421. && tv->elapsed.wall < tiny
  422. && tv->elapsed.ggc_mem < GGC_MEM_BOUND)
  423. continue;
  424. /* The timing variable name. */
  425. fprintf (fp, " %-24s:", tv->name);
  426. #ifdef HAVE_USER_TIME
  427. /* Print user-mode time for this process. */
  428. fprintf (fp, "%7.2f (%2.0f%%) usr",
  429. tv->elapsed.user,
  430. (total->user == 0 ? 0 : tv->elapsed.user / total->user) * 100);
  431. #endif /* HAVE_USER_TIME */
  432. #ifdef HAVE_SYS_TIME
  433. /* Print system-mode time for this process. */
  434. fprintf (fp, "%7.2f (%2.0f%%) sys",
  435. tv->elapsed.sys,
  436. (total->sys == 0 ? 0 : tv->elapsed.sys / total->sys) * 100);
  437. #endif /* HAVE_SYS_TIME */
  438. #ifdef HAVE_WALL_TIME
  439. /* Print wall clock time elapsed. */
  440. fprintf (fp, "%7.2f (%2.0f%%) wall",
  441. tv->elapsed.wall,
  442. (total->wall == 0 ? 0 : tv->elapsed.wall / total->wall) * 100);
  443. #endif /* HAVE_WALL_TIME */
  444. /* Print the amount of ggc memory allocated. */
  445. fprintf (fp, "%8u kB (%2.0f%%) ggc",
  446. (unsigned) (tv->elapsed.ggc_mem >> 10),
  447. (total->ggc_mem == 0
  448. ? 0
  449. : (float) tv->elapsed.ggc_mem / total->ggc_mem) * 100);
  450. putc ('\n', fp);
  451. }
  452. /* Print total time. */
  453. fputs (" TOTAL :", fp);
  454. #ifdef HAVE_USER_TIME
  455. fprintf (fp, "%7.2f ", total->user);
  456. #endif
  457. #ifdef HAVE_SYS_TIME
  458. fprintf (fp, "%7.2f ", total->sys);
  459. #endif
  460. #ifdef HAVE_WALL_TIME
  461. fprintf (fp, "%7.2f ", total->wall);
  462. #endif
  463. fprintf (fp, "%8u kB\n", (unsigned) (total->ggc_mem >> 10));
  464. #ifdef ENABLE_CHECKING
  465. fprintf (fp, "Extra diagnostic checks enabled; compiler may run slowly.\n");
  466. fprintf (fp, "Configure with --enable-checking=release to disable checks.\n");
  467. #endif
  468. #ifndef ENABLE_ASSERT_CHECKING
  469. fprintf (fp, "Internal checks disabled; compiler is not suited for release.\n");
  470. fprintf (fp, "Configure with --enable-checking=release to enable checks.\n");
  471. #endif
  472. #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
  473. || defined (HAVE_WALL_TIME) */
  474. validate_phases (fp);
  475. }
  476. /* Prints a message to stderr stating that time elapsed in STR is
  477. TOTAL (given in microseconds). */
  478. void
  479. print_time (const char *str, long total)
  480. {
  481. long all_time = get_run_time ();
  482. fprintf (stderr,
  483. "time in %s: %ld.%06ld (%ld%%)\n",
  484. str, total / 1000000, total % 1000000,
  485. all_time == 0 ? 0
  486. : (long) (((100.0 * (double) total) / (double) all_time) + .5));
  487. }