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
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
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/>. */
23 #include "coretypes.h"
30 #ifndef HAVE_STRUCT_TMS
41 # define RUSAGE_SELF 0
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 */
50 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
53 # define TICKS_PER_SECOND HZ /* traditional UNIX */
55 # define TICKS_PER_SECOND 100 /* often the correct value */
60 /* Prefer times to getrusage to clock (each gives successively less
63 # if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
64 extern clock_t times (struct tms
*);
67 # define HAVE_USER_TIME
68 # define HAVE_SYS_TIME
69 # define HAVE_WALL_TIME
72 # if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
73 extern int getrusage (int, struct rusage
*);
75 # define USE_GETRUSAGE
76 # define HAVE_USER_TIME
77 # define HAVE_SYS_TIME
80 # if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
81 extern clock_t clock (void);
84 # define HAVE_USER_TIME
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? */
94 static double ticks_to_msec
;
95 #define TICKS_TO_MSEC (1 / (double)TICKS_PER_SECOND)
99 static double clocks_to_msec
;
100 #define CLOCKS_TO_MSEC (1 / (double)CLOCKS_PER_SEC)
103 /* Non-NULL if timevars should be used. In GCC, this happens with
104 the -ftime-report flag. */
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
130 named_items (timer
*t
);
133 void push (const char *item_name
);
135 void print (FILE *fp
, const timevar_time_def
*total
);
138 /* Which timer instance does this relate to? */
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,
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
)
160 /* The destructor for class timer::named_items. */
162 timer::named_items::~named_items ()
166 /* Push the named item onto the timer stack. */
169 timer::named_items::push (const char *item_name
)
171 gcc_assert (item_name
);
174 timer::timevar_def
*def
= &m_hash_map
.get_or_insert (item_name
, &existed
);
177 def
->elapsed
.user
= 0;
178 def
->elapsed
.sys
= 0;
179 def
->elapsed
.wall
= 0;
180 def
->name
= item_name
;
182 m_names
.safe_push (item_name
);
184 m_timer
->push_internal (def
);
187 /* Pop the top item from the timer stack. */
190 timer::named_items::pop ()
192 m_timer
->pop_internal ();
195 /* Print the given client item. Helper function for timer::print. */
198 timer::named_items::print (FILE *fp
, const timevar_time_def
*total
)
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
);
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. */
216 get_time (struct timevar_time_def
*now
)
221 now
->ggc_mem
= timevar_ggc_mem_total
;
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
;
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;
237 now
->user
= clock () * clocks_to_msec
;
242 /* Add the difference between STOP_TIME and START_TIME to TIMER. */
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. */
259 m_unused_stack_instances (NULL
),
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"
272 /* Initialize configuration-specific state.
273 Ideally this would be one-time initialization. */
275 ticks_to_msec
= TICKS_TO_MSEC
;
278 clocks_to_msec
= CLOCKS_TO_MSEC
;
282 /* Class timer's destructor. */
286 timevar_stack_def
*iter
, *next
;
288 for (iter
= m_stack
; iter
; iter
= next
)
293 for (iter
= m_unused_stack_instances
; iter
; iter
= next
)
299 delete m_jit_client_items
;
302 /* Initialize timing variables. */
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. */
321 timer::push (timevar_id_t timevar
)
323 struct timevar_def
*tv
= &m_timevars
[timevar
];
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. */
331 timer::push_internal (struct timevar_def
*tv
)
333 struct timevar_stack_def
*context
;
334 struct timevar_time_def now
;
338 /* Mark this timing variable as used. */
341 /* Can't push a standalone timer. */
342 gcc_assert (!tv
->standalone
);
344 /* What time is it? */
347 /* If the stack isn't empty, attribute the current elapsed time to
348 the old topmost element. */
350 timevar_accumulate (&m_stack
->timevar
->elapsed
, &m_start_time
, &now
);
352 /* Reset the start time; from now on, time is attributed to
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
;
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
;
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
379 timer::pop (timevar_id_t timevar
)
381 gcc_assert (&m_timevars
[timevar
] == m_stack
->timevar
);
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. */
390 timer::pop_internal ()
392 struct timevar_time_def now
;
393 struct timevar_stack_def
*popped
= m_stack
;
395 /* What time is it? */
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. */
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. */
419 timevar_start (timevar_id_t timevar
)
424 g_timer
->start (timevar
);
427 /* See timevar_start above. */
430 timer::start (timevar_id_t timevar
)
432 struct timevar_def
*tv
= &m_timevars
[timevar
];
434 /* Mark this timing variable as used. */
437 /* Don't allow the same timing variable to be started more than
439 gcc_assert (!tv
->standalone
);
442 get_time (&tv
->start_time
);
445 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
446 is attributed to it. */
449 timevar_stop (timevar_id_t timevar
)
454 g_timer
->stop (timevar
);
457 /* See timevar_stop above. */
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. */
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. */
481 timevar_cond_start (timevar_id_t timevar
)
486 return g_timer
->cond_start (timevar
);
489 /* See timevar_cond_start above. */
492 timer::cond_start (timevar_id_t timevar
)
494 struct timevar_def
*tv
= &m_timevars
[timevar
];
496 /* Mark this timing variable as used. */
500 return true; /* The timevar is already running. */
502 /* Don't allow the same timing variable
503 to be unconditionally started more than once. */
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. */
516 timevar_cond_stop (timevar_id_t timevar
, bool running
)
518 if (!g_timer
|| running
)
521 g_timer
->cond_stop (timevar
);
524 /* See timevar_cond_stop above. */
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. */
539 timevar_accumulate (&tv
->elapsed
, &tv
->start_time
, &now
);
542 /* Push the named item onto the timing stack. */
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. */
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. */
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. */
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
);
616 /* Helper function for timer::print. */
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",
630 (total
->user
== 0 ? 0 : tv
->elapsed
.user
/ total
->user
) * 100);
631 #endif /* HAVE_USER_TIME */
634 /* Print system-mode time for this process. */
635 fprintf (fp
, "%7.2f (%2.0f%%) 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",
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),
652 : (float) tv
->elapsed
.ggc_mem
/ total
->ggc_mem
) * 100);
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. */
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. */
675 /* What time is it? */
678 /* If the stack isn't empty, attribute the current elapsed time to
679 the old topmost element. */
681 timevar_accumulate (&m_stack
->timevar
->elapsed
, &m_start_time
, &now
);
683 /* Reset the start time; from now on, time is attributed to
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
697 if ((timevar_id_t
) id
== TV_TOTAL
)
700 /* Don't print timing variables that were never used. */
704 /* Don't print timing variables if we're going to get a row of
706 if (tv
->elapsed
.user
< tiny
707 && tv
->elapsed
.sys
< tiny
708 && tv
->elapsed
.wall
< tiny
709 && tv
->elapsed
.ggc_mem
< GGC_MEM_BOUND
)
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
);
723 fprintf (fp
, "%7.2f ", total
->sys
);
725 #ifdef HAVE_WALL_TIME
726 fprintf (fp
, "%7.2f ", total
->wall
);
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");
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");
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. */
748 timer::get_topmost_item_name () const
751 return m_stack
->timevar
->name
;
756 /* Prints a message to stderr stating that time elapsed in STR is
757 TOTAL (given in microseconds). */
760 print_time (const char *str
, long total
)
762 long all_time
= get_run_time ();
764 "time in %s: %ld.%06ld (%ld%%)\n",
765 str
, total
/ 1000000, total
% 1000000,
767 : (long) (((100.0 * (double) total
) / (double) all_time
) + .5));