PageRenderTime 65ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/gcc/timevar.c

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