Add C++11 header <cuchar>.
[official-gcc.git] / gcc / timevar.c
blob82497270d6aab2eb10ac3d446430da60c847d90c
1 /* Timing variables for measuring compiler performance.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Contributed by Alex Samuel <samuel@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "timevar.h"
26 #ifndef HAVE_CLOCK_T
27 typedef int clock_t;
28 #endif
30 #ifndef HAVE_STRUCT_TMS
31 struct tms
33 clock_t tms_utime;
34 clock_t tms_stime;
35 clock_t tms_cutime;
36 clock_t tms_cstime;
38 #endif
40 #ifndef RUSAGE_SELF
41 # define RUSAGE_SELF 0
42 #endif
44 /* Calculation of scale factor to convert ticks to microseconds.
45 We mustn't use CLOCKS_PER_SEC except with clock(). */
46 #if HAVE_SYSCONF && defined _SC_CLK_TCK
47 # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
48 #else
49 # ifdef CLK_TCK
50 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
51 # else
52 # ifdef HZ
53 # define TICKS_PER_SECOND HZ /* traditional UNIX */
54 # else
55 # define TICKS_PER_SECOND 100 /* often the correct value */
56 # endif
57 # endif
58 #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
89 /* libc is very likely to have snuck a call to sysconf() into one of
90 the underlying constants, and that can be very slow, so we have to
91 precompute them. Whose wonderful idea was it to make all those
92 _constants_ variable at run time, anyway? */
93 #ifdef USE_TIMES
94 static double ticks_to_msec;
95 #define TICKS_TO_MSEC (1 / (double)TICKS_PER_SECOND)
96 #endif
98 #ifdef USE_CLOCK
99 static double clocks_to_msec;
100 #define CLOCKS_TO_MSEC (1 / (double)CLOCKS_PER_SEC)
101 #endif
103 /* Non-NULL if timevars should be used. In GCC, this happens with
104 the -ftime-report flag. */
106 timer *g_timer;
108 /* Total amount of memory allocated by garbage collector. */
110 size_t timevar_ggc_mem_total;
112 /* The amount of memory that will cause us to report the timevar even
113 if the time spent is not significant. */
115 #define GGC_MEM_BOUND (1 << 20)
117 /* See timevar.h for an explanation of timing variables. */
119 static void get_time (struct timevar_time_def *);
120 static void timevar_accumulate (struct timevar_time_def *,
121 struct timevar_time_def *,
122 struct timevar_time_def *);
124 /* The implementation of timing events for jit client code, allowing
125 arbitrary named items to appear on the timing stack. */
127 class timer::named_items
129 public:
130 named_items (timer *t);
131 ~named_items ();
133 void push (const char *item_name);
134 void pop ();
135 void print (FILE *fp, const timevar_time_def *total);
137 private:
138 /* Which timer instance does this relate to? */
139 timer *m_timer;
141 /* Dictionary, mapping from item names to timevar_def.
142 Note that currently we merely store/compare the raw string
143 pointers provided by client code; we don't take a copy,
144 or use strcmp. */
145 hash_map <const char *, timer::timevar_def> m_hash_map;
147 /* The order in which items were originally inserted. */
148 auto_vec <const char *> m_names;
151 /* The constructor for class timer::named_items. */
153 timer::named_items::named_items (timer *t)
154 : m_timer (t),
155 m_hash_map (),
156 m_names ()
160 /* The destructor for class timer::named_items. */
162 timer::named_items::~named_items ()
166 /* Push the named item onto the timer stack. */
168 void
169 timer::named_items::push (const char *item_name)
171 gcc_assert (item_name);
173 bool existed;
174 timer::timevar_def *def = &m_hash_map.get_or_insert (item_name, &existed);
175 if (!existed)
177 def->elapsed.user = 0;
178 def->elapsed.sys = 0;
179 def->elapsed.wall = 0;
180 def->name = item_name;
181 def->standalone = 0;
182 m_names.safe_push (item_name);
184 m_timer->push_internal (def);
187 /* Pop the top item from the timer stack. */
189 void
190 timer::named_items::pop ()
192 m_timer->pop_internal ();
195 /* Print the given client item. Helper function for timer::print. */
197 void
198 timer::named_items::print (FILE *fp, const timevar_time_def *total)
200 unsigned int i;
201 const char *item_name;
202 fprintf (fp, "Client items:\n");
203 FOR_EACH_VEC_ELT (m_names, i, item_name)
205 timer::timevar_def *def = m_hash_map.get (item_name);
206 gcc_assert (def);
207 m_timer->print_row (fp, total, def);
211 /* Fill the current times into TIME. The definition of this function
212 also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
213 HAVE_WALL_TIME macros. */
215 static void
216 get_time (struct timevar_time_def *now)
218 now->user = 0;
219 now->sys = 0;
220 now->wall = 0;
221 now->ggc_mem = timevar_ggc_mem_total;
224 #ifdef USE_TIMES
225 struct tms tms;
226 now->wall = times (&tms) * ticks_to_msec;
227 now->user = tms.tms_utime * ticks_to_msec;
228 now->sys = tms.tms_stime * ticks_to_msec;
229 #endif
230 #ifdef USE_GETRUSAGE
231 struct rusage rusage;
232 getrusage (RUSAGE_SELF, &rusage);
233 now->user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6;
234 now->sys = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6;
235 #endif
236 #ifdef USE_CLOCK
237 now->user = clock () * clocks_to_msec;
238 #endif
242 /* Add the difference between STOP_TIME and START_TIME to TIMER. */
244 static void
245 timevar_accumulate (struct timevar_time_def *timer,
246 struct timevar_time_def *start_time,
247 struct timevar_time_def *stop_time)
249 timer->user += stop_time->user - start_time->user;
250 timer->sys += stop_time->sys - start_time->sys;
251 timer->wall += stop_time->wall - start_time->wall;
252 timer->ggc_mem += stop_time->ggc_mem - start_time->ggc_mem;
255 /* Class timer's constructor. */
257 timer::timer () :
258 m_stack (NULL),
259 m_unused_stack_instances (NULL),
260 m_start_time (),
261 m_jit_client_items (NULL)
263 /* Zero all elapsed times. */
264 memset (m_timevars, 0, sizeof (m_timevars));
266 /* Initialize the names of timing variables. */
267 #define DEFTIMEVAR(identifier__, name__) \
268 m_timevars[identifier__].name = name__;
269 #include "timevar.def"
270 #undef DEFTIMEVAR
272 /* Initialize configuration-specific state.
273 Ideally this would be one-time initialization. */
274 #ifdef USE_TIMES
275 ticks_to_msec = TICKS_TO_MSEC;
276 #endif
277 #ifdef USE_CLOCK
278 clocks_to_msec = CLOCKS_TO_MSEC;
279 #endif
282 /* Class timer's destructor. */
284 timer::~timer ()
286 timevar_stack_def *iter, *next;
288 for (iter = m_stack; iter; iter = next)
290 next = iter->next;
291 free (iter);
293 for (iter = m_unused_stack_instances; iter; iter = next)
295 next = iter->next;
296 free (iter);
299 delete m_jit_client_items;
302 /* Initialize timing variables. */
304 void
305 timevar_init (void)
307 if (g_timer)
308 return;
310 g_timer = new timer ();
313 /* Push TIMEVAR onto the timing stack. No further elapsed time is
314 attributed to the previous topmost timing variable on the stack;
315 subsequent elapsed time is attributed to TIMEVAR, until it is
316 popped or another element is pushed on top.
318 TIMEVAR cannot be running as a standalone timer. */
320 void
321 timer::push (timevar_id_t timevar)
323 struct timevar_def *tv = &m_timevars[timevar];
324 push_internal (tv);
327 /* Push TV onto the timing stack, either one of the builtin ones
328 for a timevar_id_t, or one provided by client code to libgccjit. */
330 void
331 timer::push_internal (struct timevar_def *tv)
333 struct timevar_stack_def *context;
334 struct timevar_time_def now;
336 gcc_assert (tv);
338 /* Mark this timing variable as used. */
339 tv->used = 1;
341 /* Can't push a standalone timer. */
342 gcc_assert (!tv->standalone);
344 /* What time is it? */
345 get_time (&now);
347 /* If the stack isn't empty, attribute the current elapsed time to
348 the old topmost element. */
349 if (m_stack)
350 timevar_accumulate (&m_stack->timevar->elapsed, &m_start_time, &now);
352 /* Reset the start time; from now on, time is attributed to
353 TIMEVAR. */
354 m_start_time = now;
356 /* See if we have a previously-allocated stack instance. If so,
357 take it off the list. If not, malloc a new one. */
358 if (m_unused_stack_instances != NULL)
360 context = m_unused_stack_instances;
361 m_unused_stack_instances = m_unused_stack_instances->next;
363 else
364 context = XNEW (struct timevar_stack_def);
366 /* Fill it in and put it on the stack. */
367 context->timevar = tv;
368 context->next = m_stack;
369 m_stack = context;
372 /* Pop the topmost timing variable element off the timing stack. The
373 popped variable must be TIMEVAR. Elapsed time since the that
374 element was pushed on, or since it was last exposed on top of the
375 stack when the element above it was popped off, is credited to that
376 timing variable. */
378 void
379 timer::pop (timevar_id_t timevar)
381 gcc_assert (&m_timevars[timevar] == m_stack->timevar);
383 pop_internal ();
386 /* Pop the topmost item from the stack, either one of the builtin ones
387 for a timevar_id_t, or one provided by client code to libgccjit. */
389 void
390 timer::pop_internal ()
392 struct timevar_time_def now;
393 struct timevar_stack_def *popped = m_stack;
395 /* What time is it? */
396 get_time (&now);
398 /* Attribute the elapsed time to the element we're popping. */
399 timevar_accumulate (&popped->timevar->elapsed, &m_start_time, &now);
401 /* Reset the start time; from now on, time is attributed to the
402 element just exposed on the stack. */
403 m_start_time = now;
405 /* Take the item off the stack. */
406 m_stack = m_stack->next;
408 /* Don't delete the stack element; instead, add it to the list of
409 unused elements for later use. */
410 popped->next = m_unused_stack_instances;
411 m_unused_stack_instances = popped;
414 /* Start timing TIMEVAR independently of the timing stack. Elapsed
415 time until timevar_stop is called for the same timing variable is
416 attributed to TIMEVAR. */
418 void
419 timevar_start (timevar_id_t timevar)
421 if (!g_timer)
422 return;
424 g_timer->start (timevar);
427 /* See timevar_start above. */
429 void
430 timer::start (timevar_id_t timevar)
432 struct timevar_def *tv = &m_timevars[timevar];
434 /* Mark this timing variable as used. */
435 tv->used = 1;
437 /* Don't allow the same timing variable to be started more than
438 once. */
439 gcc_assert (!tv->standalone);
440 tv->standalone = 1;
442 get_time (&tv->start_time);
445 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
446 is attributed to it. */
448 void
449 timevar_stop (timevar_id_t timevar)
451 if (!g_timer)
452 return;
454 g_timer->stop (timevar);
457 /* See timevar_stop above. */
459 void
460 timer::stop (timevar_id_t timevar)
462 struct timevar_def *tv = &m_timevars[timevar];
463 struct timevar_time_def now;
465 /* TIMEVAR must have been started via timevar_start. */
466 gcc_assert (tv->standalone);
467 tv->standalone = 0; /* Enable a restart. */
469 get_time (&now);
470 timevar_accumulate (&tv->elapsed, &tv->start_time, &now);
474 /* Conditionally start timing TIMEVAR independently of the timing stack.
475 If the timer is already running, leave it running and return true.
476 Otherwise, start the timer and return false.
477 Elapsed time until the corresponding timevar_cond_stop
478 is called for the same timing variable is attributed to TIMEVAR. */
480 bool
481 timevar_cond_start (timevar_id_t timevar)
483 if (!g_timer)
484 return false;
486 return g_timer->cond_start (timevar);
489 /* See timevar_cond_start above. */
491 bool
492 timer::cond_start (timevar_id_t timevar)
494 struct timevar_def *tv = &m_timevars[timevar];
496 /* Mark this timing variable as used. */
497 tv->used = 1;
499 if (tv->standalone)
500 return true; /* The timevar is already running. */
502 /* Don't allow the same timing variable
503 to be unconditionally started more than once. */
504 tv->standalone = 1;
506 get_time (&tv->start_time);
507 return false; /* The timevar was not already running. */
510 /* Conditionally stop timing TIMEVAR. The RUNNING parameter must come
511 from the return value of a dynamically matching timevar_cond_start.
512 If the timer had already been RUNNING, do nothing. Otherwise, time
513 elapsed since timevar_cond_start was called is attributed to it. */
515 void
516 timevar_cond_stop (timevar_id_t timevar, bool running)
518 if (!g_timer || running)
519 return;
521 g_timer->cond_stop (timevar);
524 /* See timevar_cond_stop above. */
526 void
527 timer::cond_stop (timevar_id_t timevar)
529 struct timevar_def *tv;
530 struct timevar_time_def now;
532 tv = &m_timevars[timevar];
534 /* TIMEVAR must have been started via timevar_cond_start. */
535 gcc_assert (tv->standalone);
536 tv->standalone = 0; /* Enable a restart. */
538 get_time (&now);
539 timevar_accumulate (&tv->elapsed, &tv->start_time, &now);
542 /* Push the named item onto the timing stack. */
544 void
545 timer::push_client_item (const char *item_name)
547 gcc_assert (item_name);
549 /* Lazily create the named_items instance. */
550 if (!m_jit_client_items)
551 m_jit_client_items = new named_items (this);
553 m_jit_client_items->push (item_name);
556 /* Pop the top-most client item from the timing stack. */
558 void
559 timer::pop_client_item ()
561 gcc_assert (m_jit_client_items);
562 m_jit_client_items->pop ();
565 /* Validate that phase times are consistent. */
567 void
568 timer::validate_phases (FILE *fp) const
570 unsigned int /* timevar_id_t */ id;
571 const timevar_time_def *total = &m_timevars[TV_TOTAL].elapsed;
572 double phase_user = 0.0;
573 double phase_sys = 0.0;
574 double phase_wall = 0.0;
575 size_t phase_ggc_mem = 0;
576 static char phase_prefix[] = "phase ";
577 const double tolerance = 1.000001; /* One part in a million. */
579 for (id = 0; id < (unsigned int) TIMEVAR_LAST; ++id)
581 const timevar_def *tv = &m_timevars[(timevar_id_t) id];
583 /* Don't evaluate timing variables that were never used. */
584 if (!tv->used)
585 continue;
587 if (strncmp (tv->name, phase_prefix, sizeof phase_prefix - 1) == 0)
589 phase_user += tv->elapsed.user;
590 phase_sys += tv->elapsed.sys;
591 phase_wall += tv->elapsed.wall;
592 phase_ggc_mem += tv->elapsed.ggc_mem;
596 if (phase_user > total->user * tolerance
597 || phase_sys > total->sys * tolerance
598 || phase_wall > total->wall * tolerance
599 || phase_ggc_mem > total->ggc_mem * tolerance)
602 fprintf (fp, "Timing error: total of phase timers exceeds total time.\n");
603 if (phase_user > total->user)
604 fprintf (fp, "user %24.18e > %24.18e\n", phase_user, total->user);
605 if (phase_sys > total->sys)
606 fprintf (fp, "sys %24.18e > %24.18e\n", phase_sys, total->sys);
607 if (phase_wall > total->wall)
608 fprintf (fp, "wall %24.18e > %24.18e\n", phase_wall, total->wall);
609 if (phase_ggc_mem > total->ggc_mem)
610 fprintf (fp, "ggc_mem %24lu > %24lu\n", (unsigned long)phase_ggc_mem,
611 (unsigned long)total->ggc_mem);
612 gcc_unreachable ();
616 /* Helper function for timer::print. */
618 void
619 timer::print_row (FILE *fp,
620 const timevar_time_def *total,
621 const timevar_def *tv)
623 /* The timing variable name. */
624 fprintf (fp, " %-24s:", tv->name);
626 #ifdef HAVE_USER_TIME
627 /* Print user-mode time for this process. */
628 fprintf (fp, "%7.2f (%2.0f%%) usr",
629 tv->elapsed.user,
630 (total->user == 0 ? 0 : tv->elapsed.user / total->user) * 100);
631 #endif /* HAVE_USER_TIME */
633 #ifdef HAVE_SYS_TIME
634 /* Print system-mode time for this process. */
635 fprintf (fp, "%7.2f (%2.0f%%) sys",
636 tv->elapsed.sys,
637 (total->sys == 0 ? 0 : tv->elapsed.sys / total->sys) * 100);
638 #endif /* HAVE_SYS_TIME */
640 #ifdef HAVE_WALL_TIME
641 /* Print wall clock time elapsed. */
642 fprintf (fp, "%7.2f (%2.0f%%) wall",
643 tv->elapsed.wall,
644 (total->wall == 0 ? 0 : tv->elapsed.wall / total->wall) * 100);
645 #endif /* HAVE_WALL_TIME */
647 /* Print the amount of ggc memory allocated. */
648 fprintf (fp, "%8u kB (%2.0f%%) ggc",
649 (unsigned) (tv->elapsed.ggc_mem >> 10),
650 (total->ggc_mem == 0
652 : (float) tv->elapsed.ggc_mem / total->ggc_mem) * 100);
654 putc ('\n', fp);
657 /* Summarize timing variables to FP. The timing variable TV_TOTAL has
658 a special meaning -- it's considered to be the total elapsed time,
659 for normalizing the others, and is displayed last. */
661 void
662 timer::print (FILE *fp)
664 /* Only print stuff if we have some sort of time information. */
665 #if defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME) || defined (HAVE_WALL_TIME)
666 unsigned int /* timevar_id_t */ id;
667 const timevar_time_def *total = &m_timevars[TV_TOTAL].elapsed;
668 struct timevar_time_def now;
670 /* Update timing information in case we're calling this from GDB. */
672 if (fp == 0)
673 fp = stderr;
675 /* What time is it? */
676 get_time (&now);
678 /* If the stack isn't empty, attribute the current elapsed time to
679 the old topmost element. */
680 if (m_stack)
681 timevar_accumulate (&m_stack->timevar->elapsed, &m_start_time, &now);
683 /* Reset the start time; from now on, time is attributed to
684 TIMEVAR. */
685 m_start_time = now;
687 fputs ("\nExecution times (seconds)\n", fp);
688 if (m_jit_client_items)
689 fputs ("GCC items:\n", fp);
690 for (id = 0; id < (unsigned int) TIMEVAR_LAST; ++id)
692 const timevar_def *tv = &m_timevars[(timevar_id_t) id];
693 const double tiny = 5e-3;
695 /* Don't print the total execution time here; that goes at the
696 end. */
697 if ((timevar_id_t) id == TV_TOTAL)
698 continue;
700 /* Don't print timing variables that were never used. */
701 if (!tv->used)
702 continue;
704 /* Don't print timing variables if we're going to get a row of
705 zeroes. */
706 if (tv->elapsed.user < tiny
707 && tv->elapsed.sys < tiny
708 && tv->elapsed.wall < tiny
709 && tv->elapsed.ggc_mem < GGC_MEM_BOUND)
710 continue;
712 print_row (fp, total, tv);
714 if (m_jit_client_items)
715 m_jit_client_items->print (fp, total);
717 /* Print total time. */
718 fputs (" TOTAL :", fp);
719 #ifdef HAVE_USER_TIME
720 fprintf (fp, "%7.2f ", total->user);
721 #endif
722 #ifdef HAVE_SYS_TIME
723 fprintf (fp, "%7.2f ", total->sys);
724 #endif
725 #ifdef HAVE_WALL_TIME
726 fprintf (fp, "%7.2f ", total->wall);
727 #endif
728 fprintf (fp, "%8u kB\n", (unsigned) (total->ggc_mem >> 10));
730 #ifdef ENABLE_CHECKING
731 fprintf (fp, "Extra diagnostic checks enabled; compiler may run slowly.\n");
732 fprintf (fp, "Configure with --enable-checking=release to disable checks.\n");
733 #endif
734 #ifndef ENABLE_ASSERT_CHECKING
735 fprintf (fp, "Internal checks disabled; compiler is not suited for release.\n");
736 fprintf (fp, "Configure with --enable-checking=release to enable checks.\n");
737 #endif
739 #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
740 || defined (HAVE_WALL_TIME) */
742 validate_phases (fp);
745 /* Get the name of the topmost item. For use by jit for validating
746 inputs to gcc_jit_timer_pop. */
747 const char *
748 timer::get_topmost_item_name () const
750 if (m_stack)
751 return m_stack->timevar->name;
752 else
753 return NULL;
756 /* Prints a message to stderr stating that time elapsed in STR is
757 TOTAL (given in microseconds). */
759 void
760 print_time (const char *str, long total)
762 long all_time = get_run_time ();
763 fprintf (stderr,
764 "time in %s: %ld.%06ld (%ld%%)\n",
765 str, total / 1000000, total % 1000000,
766 all_time == 0 ? 0
767 : (long) (((100.0 * (double) total) / (double) all_time) + .5));