timevar.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* Timing variables for measuring compiler performance.
  2. Copyright (C) 2000, 2002, 2004, 2006, 2009-2013 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 <http://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #if IN_GCC
  17. #include "system.h"
  18. #include "intl.h"
  19. #include "rtl.h"
  20. #else
  21. #if defined(_musl_)
  22. #define HAVE_SYS_TIMES_H 1
  23. #define HAVE_STRUCT_TMS 1
  24. #define HAVE_CLOCK_T 1
  25. #endif
  26. /* This source file is taken from the GCC source code, with slight
  27. modifications that are under control of the IN_GCC preprocessor
  28. variable. The !IN_GCC part of this file is specific to Bison. */
  29. # include "bison-system.h"
  30. # if HAVE_SYS_TIME_H
  31. # include <sys/time.h>
  32. # endif
  33. int timevar_report = 0;
  34. #endif
  35. #ifdef HAVE_SYS_TIMES_H
  36. # include <sys/times.h>
  37. #endif
  38. #ifdef HAVE_SYS_RESOURCE_H
  39. #include <sys/resource.h>
  40. #endif
  41. #ifndef HAVE_CLOCK_T
  42. typedef int clock_t;
  43. #endif
  44. #ifndef HAVE_STRUCT_TMS
  45. struct tms
  46. {
  47. clock_t tms_utime;
  48. clock_t tms_stime;
  49. clock_t tms_cutime;
  50. clock_t tms_cstime;
  51. };
  52. #endif
  53. #if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
  54. extern int getrusage (int, struct rusage *);
  55. #endif
  56. #if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
  57. extern clock_t times (struct tms *);
  58. #endif
  59. #if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
  60. extern clock_t clock (void);
  61. #endif
  62. #ifndef RUSAGE_SELF
  63. # define RUSAGE_SELF 0
  64. #endif
  65. /* Calculation of scale factor to convert ticks to microseconds.
  66. We mustn't use CLOCKS_PER_SEC except with clock(). */
  67. #if HAVE_SYSCONF && defined _SC_CLK_TCK
  68. # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
  69. #else
  70. # ifdef CLK_TCK
  71. # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
  72. # else
  73. # ifdef HZ
  74. # define TICKS_PER_SECOND HZ /* traditional UNIX */
  75. # else
  76. # define TICKS_PER_SECOND 100 /* often the correct value */
  77. # endif
  78. # endif
  79. #endif
  80. /* Prefer times to getrusage to clock (each gives successively less
  81. information). */
  82. #ifdef HAVE_TIMES
  83. # define USE_TIMES
  84. # define HAVE_USER_TIME
  85. # define HAVE_SYS_TIME
  86. # define HAVE_WALL_TIME
  87. #else
  88. #ifdef HAVE_GETRUSAGE
  89. # define USE_GETRUSAGE
  90. # define HAVE_USER_TIME
  91. # define HAVE_SYS_TIME
  92. #else
  93. #ifdef HAVE_CLOCK
  94. # define USE_CLOCK
  95. # define HAVE_USER_TIME
  96. #endif
  97. #endif
  98. #endif
  99. /* libc is very likely to have snuck a call to sysconf() into one of
  100. the underlying constants, and that can be very slow, so we have to
  101. precompute them. Whose wonderful idea was it to make all those
  102. _constants_ variable at run time, anyway? */
  103. #ifdef USE_TIMES
  104. static float ticks_to_msec;
  105. #define TICKS_TO_MSEC (1.0 / TICKS_PER_SECOND)
  106. #endif
  107. #ifdef USE_CLOCK
  108. static float clocks_to_msec;
  109. #define CLOCKS_TO_MSEC (1.0 / CLOCKS_PER_SEC)
  110. #endif
  111. #if IN_GCC
  112. #include "flags.h"
  113. #endif
  114. #include "timevar.h"
  115. /* See timevar.h for an explanation of timing variables. */
  116. /* This macro evaluates to nonzero if timing variables are enabled. */
  117. #define TIMEVAR_ENABLE (timevar_report)
  118. /* A timing variable. */
  119. struct timevar_def
  120. {
  121. /* Elapsed time for this variable. */
  122. struct timevar_time_def elapsed;
  123. /* If this variable is timed independently of the timing stack,
  124. using timevar_start, this contains the start time. */
  125. struct timevar_time_def start_time;
  126. /* The name of this timing variable. */
  127. const char *name;
  128. /* Non-zero if this timing variable is running as a standalone
  129. timer. */
  130. unsigned standalone : 1;
  131. /* Non-zero if this timing variable was ever started or pushed onto
  132. the timing stack. */
  133. unsigned used : 1;
  134. };
  135. /* An element on the timing stack. Elapsed time is attributed to the
  136. topmost timing variable on the stack. */
  137. struct timevar_stack_def
  138. {
  139. /* The timing variable at this stack level. */
  140. struct timevar_def *timevar;
  141. /* The next lower timing variable context in the stack. */
  142. struct timevar_stack_def *next;
  143. };
  144. /* Declared timing variables. Constructed from the contents of
  145. timevar.def. */
  146. static struct timevar_def timevars[TIMEVAR_LAST];
  147. /* The top of the timing stack. */
  148. static struct timevar_stack_def *stack;
  149. /* A list of unused (i.e. allocated and subsequently popped)
  150. timevar_stack_def instances. */
  151. static struct timevar_stack_def *unused_stack_instances;
  152. /* The time at which the topmost element on the timing stack was
  153. pushed. Time elapsed since then is attributed to the topmost
  154. element. */
  155. static struct timevar_time_def start_time;
  156. static void get_time (struct timevar_time_def *);
  157. static void timevar_accumulate (struct timevar_time_def *,
  158. struct timevar_time_def *,
  159. struct timevar_time_def *);
  160. /* Fill the current times into TIME. The definition of this function
  161. also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
  162. HAVE_WALL_TIME macros. */
  163. static void
  164. get_time (now)
  165. struct timevar_time_def *now;
  166. {
  167. now->user = 0;
  168. now->sys = 0;
  169. now->wall = 0;
  170. if (!TIMEVAR_ENABLE)
  171. return;
  172. {
  173. #ifdef USE_TIMES
  174. struct tms tms;
  175. now->wall = times (&tms) * ticks_to_msec;
  176. #if IN_GCC
  177. now->user = tms.tms_utime * ticks_to_msec;
  178. now->sys = tms.tms_stime * ticks_to_msec;
  179. #else
  180. now->user = (tms.tms_utime + tms.tms_cutime) * ticks_to_msec;
  181. now->sys = (tms.tms_stime + tms.tms_cstime) * ticks_to_msec;
  182. #endif
  183. #endif
  184. #ifdef USE_GETRUSAGE
  185. struct rusage rusage;
  186. #if IN_GCC
  187. getrusage (RUSAGE_SELF, &rusage);
  188. #else
  189. getrusage (RUSAGE_CHILDREN, &rusage);
  190. #endif
  191. now->user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6;
  192. now->sys = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6;
  193. #endif
  194. #ifdef USE_CLOCK
  195. now->user = clock () * clocks_to_msec;
  196. #endif
  197. }
  198. }
  199. /* Add the difference between STOP and START to TIMER. */
  200. static void
  201. timevar_accumulate (timer, start, stop)
  202. struct timevar_time_def *timer;
  203. struct timevar_time_def *start;
  204. struct timevar_time_def *stop;
  205. {
  206. timer->user += stop->user - start->user;
  207. timer->sys += stop->sys - start->sys;
  208. timer->wall += stop->wall - start->wall;
  209. }
  210. /* Initialize timing variables. */
  211. void
  212. init_timevar ()
  213. {
  214. if (!TIMEVAR_ENABLE)
  215. return;
  216. /* Zero all elapsed times. */
  217. memset ((void *) timevars, 0, sizeof (timevars));
  218. /* Initialize the names of timing variables. */
  219. #define DEFTIMEVAR(identifier__, name__) \
  220. timevars[identifier__].name = name__;
  221. #include "timevar.def"
  222. #undef DEFTIMEVAR
  223. #ifdef USE_TIMES
  224. ticks_to_msec = TICKS_TO_MSEC;
  225. #endif
  226. #ifdef USE_CLOCK
  227. clocks_to_msec = CLOCKS_TO_MSEC;
  228. #endif
  229. }
  230. /* Push TIMEVAR onto the timing stack. No further elapsed time is
  231. attributed to the previous topmost timing variable on the stack;
  232. subsequent elapsed time is attributed to TIMEVAR, until it is
  233. popped or another element is pushed on top.
  234. TIMEVAR cannot be running as a standalone timer. */
  235. void
  236. timevar_push (timevar)
  237. timevar_id_t timevar;
  238. {
  239. struct timevar_def *tv = &timevars[timevar];
  240. struct timevar_stack_def *context;
  241. struct timevar_time_def now;
  242. if (!TIMEVAR_ENABLE)
  243. return;
  244. /* Mark this timing variable as used. */
  245. tv->used = 1;
  246. /* Can't push a standalone timer. */
  247. if (tv->standalone)
  248. abort ();
  249. /* What time is it? */
  250. get_time (&now);
  251. /* If the stack isn't empty, attribute the current elapsed time to
  252. the old topmost element. */
  253. if (stack)
  254. timevar_accumulate (&stack->timevar->elapsed, &start_time, &now);
  255. /* Reset the start time; from now on, time is attributed to
  256. TIMEVAR. */
  257. start_time = now;
  258. /* See if we have a previously-allocated stack instance. If so,
  259. take it off the list. If not, malloc a new one. */
  260. if (unused_stack_instances != NULL)
  261. {
  262. context = unused_stack_instances;
  263. unused_stack_instances = unused_stack_instances->next;
  264. }
  265. else
  266. context = (struct timevar_stack_def *)
  267. xmalloc (sizeof (struct timevar_stack_def));
  268. /* Fill it in and put it on the stack. */
  269. context->timevar = tv;
  270. context->next = stack;
  271. stack = context;
  272. }
  273. /* Pop the topmost timing variable element off the timing stack. The
  274. popped variable must be TIMEVAR. Elapsed time since the that
  275. element was pushed on, or since it was last exposed on top of the
  276. stack when the element above it was popped off, is credited to that
  277. timing variable. */
  278. void
  279. timevar_pop (timevar)
  280. timevar_id_t timevar;
  281. {
  282. struct timevar_time_def now;
  283. struct timevar_stack_def *popped = stack;
  284. if (!TIMEVAR_ENABLE)
  285. return;
  286. if (&timevars[timevar] != stack->timevar)
  287. abort ();
  288. /* What time is it? */
  289. get_time (&now);
  290. /* Attribute the elapsed time to the element we're popping. */
  291. timevar_accumulate (&popped->timevar->elapsed, &start_time, &now);
  292. /* Reset the start time; from now on, time is attributed to the
  293. element just exposed on the stack. */
  294. start_time = now;
  295. /* Take the item off the stack. */
  296. stack = stack->next;
  297. /* Don't delete the stack element; instead, add it to the list of
  298. unused elements for later use. */
  299. popped->next = unused_stack_instances;
  300. unused_stack_instances = popped;
  301. }
  302. /* Start timing TIMEVAR independently of the timing stack. Elapsed
  303. time until timevar_stop is called for the same timing variable is
  304. attributed to TIMEVAR. */
  305. void
  306. timevar_start (timevar)
  307. timevar_id_t timevar;
  308. {
  309. struct timevar_def *tv = &timevars[timevar];
  310. if (!TIMEVAR_ENABLE)
  311. return;
  312. /* Mark this timing variable as used. */
  313. tv->used = 1;
  314. /* Don't allow the same timing variable to be started more than
  315. once. */
  316. if (tv->standalone)
  317. abort ();
  318. tv->standalone = 1;
  319. get_time (&tv->start_time);
  320. }
  321. /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
  322. is attributed to it. */
  323. void
  324. timevar_stop (timevar)
  325. timevar_id_t timevar;
  326. {
  327. struct timevar_def *tv = &timevars[timevar];
  328. struct timevar_time_def now;
  329. if (!TIMEVAR_ENABLE)
  330. return;
  331. /* TIMEVAR must have been started via timevar_start. */
  332. if (!tv->standalone)
  333. abort ();
  334. get_time (&now);
  335. timevar_accumulate (&tv->elapsed, &tv->start_time, &now);
  336. }
  337. /* Fill the elapsed time for TIMEVAR into ELAPSED. Returns
  338. update-to-date information even if TIMEVAR is currently running. */
  339. void
  340. timevar_get (timevar, elapsed)
  341. timevar_id_t timevar;
  342. struct timevar_time_def *elapsed;
  343. {
  344. struct timevar_def *tv = &timevars[timevar];
  345. struct timevar_time_def now;
  346. *elapsed = tv->elapsed;
  347. /* Is TIMEVAR currently running as a standalone timer? */
  348. if (tv->standalone)
  349. {
  350. get_time (&now);
  351. timevar_accumulate (elapsed, &tv->start_time, &now);
  352. }
  353. /* Or is TIMEVAR at the top of the timer stack? */
  354. else if (stack->timevar == tv)
  355. {
  356. get_time (&now);
  357. timevar_accumulate (elapsed, &start_time, &now);
  358. }
  359. }
  360. /* Summarize timing variables to FP. The timing variable TV_TOTAL has
  361. a special meaning -- it's considered to be the total elapsed time,
  362. for normalizing the others, and is displayed last. */
  363. void
  364. timevar_print (fp)
  365. FILE *fp;
  366. {
  367. /* Only print stuff if we have some sort of time information. */
  368. #if defined HAVE_USER_TIME || defined HAVE_SYS_TIME || defined HAVE_WALL_TIME
  369. unsigned int /* timevar_id_t */ id;
  370. struct timevar_time_def *total = &timevars[TV_TOTAL].elapsed;
  371. struct timevar_time_def now;
  372. if (!TIMEVAR_ENABLE)
  373. return;
  374. /* Update timing information in case we're calling this from GDB. */
  375. if (fp == 0)
  376. fp = stderr;
  377. /* What time is it? */
  378. get_time (&now);
  379. /* If the stack isn't empty, attribute the current elapsed time to
  380. the old topmost element. */
  381. if (stack)
  382. timevar_accumulate (&stack->timevar->elapsed, &start_time, &now);
  383. /* Reset the start time; from now on, time is attributed to
  384. TIMEVAR. */
  385. start_time = now;
  386. fputs (_("\nExecution times (seconds)\n"), fp);
  387. for (id = 0; id < (unsigned int) TIMEVAR_LAST; ++id)
  388. {
  389. struct timevar_def *tv = &timevars[(timevar_id_t) id];
  390. const float tiny = 5e-3;
  391. /* Don't print the total execution time here; that goes at the
  392. end. */
  393. if ((timevar_id_t) id == TV_TOTAL)
  394. continue;
  395. /* Don't print timing variables that were never used. */
  396. if (!tv->used)
  397. continue;
  398. /* Don't print timing variables if we're going to get a row of
  399. zeroes. */
  400. if (tv->elapsed.user < tiny
  401. && tv->elapsed.sys < tiny
  402. && tv->elapsed.wall < tiny)
  403. continue;
  404. /* The timing variable name. */
  405. fprintf (fp, " %-22s:", tv->name);
  406. #ifdef HAVE_USER_TIME
  407. /* Print user-mode time for this process. */
  408. fprintf (fp, "%7.2f (%2.0f%%) usr",
  409. tv->elapsed.user,
  410. (total->user == 0 ? 0 : tv->elapsed.user / total->user) * 100);
  411. #endif /* HAVE_USER_TIME */
  412. #ifdef HAVE_SYS_TIME
  413. /* Print system-mode time for this process. */
  414. fprintf (fp, "%7.2f (%2.0f%%) sys",
  415. tv->elapsed.sys,
  416. (total->sys == 0 ? 0 : tv->elapsed.sys / total->sys) * 100);
  417. #endif /* HAVE_SYS_TIME */
  418. #ifdef HAVE_WALL_TIME
  419. /* Print wall clock time elapsed. */
  420. fprintf (fp, "%7.2f (%2.0f%%) wall",
  421. tv->elapsed.wall,
  422. (total->wall == 0 ? 0 : tv->elapsed.wall / total->wall) * 100);
  423. #endif /* HAVE_WALL_TIME */
  424. putc ('\n', fp);
  425. }
  426. /* Print total time. */
  427. fputs (_(" TOTAL :"), fp);
  428. #ifdef HAVE_USER_TIME
  429. fprintf (fp, "%7.2f ", total->user);
  430. #endif
  431. #ifdef HAVE_SYS_TIME
  432. fprintf (fp, "%7.2f ", total->sys);
  433. #endif
  434. #ifdef HAVE_WALL_TIME
  435. fprintf (fp, "%7.2f\n", total->wall);
  436. #endif
  437. #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
  438. || defined (HAVE_WALL_TIME) */
  439. }
  440. /* Returns time (user + system) used so far by the compiler process,
  441. in microseconds. */
  442. long
  443. get_run_time ()
  444. {
  445. struct timevar_time_def total_elapsed;
  446. timevar_get (TV_TOTAL, &total_elapsed);
  447. return total_elapsed.user + total_elapsed.sys;
  448. }
  449. /* Prints a message to stderr stating that time elapsed in STR is
  450. TOTAL (given in microseconds). */
  451. void
  452. print_time (str, total)
  453. const char *str;
  454. long total;
  455. {
  456. long all_time = get_run_time ();
  457. fprintf (stderr,
  458. _("time in %s: %ld.%06ld (%ld%%)\n"),
  459. str, total / 1000000, total % 1000000,
  460. all_time == 0 ? 0
  461. : (long) (((100.0 * (double) total) / (double) all_time) + .5));
  462. }