xref: /dflybsd-src/contrib/gcc-8.0/gcc/timevar.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Timing variables for measuring compiler performance.
2*38fd1498Szrj    Copyright (C) 2000-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Contributed by Alex Samuel <samuel@codesourcery.com>
4*38fd1498Szrj 
5*38fd1498Szrj This file is part of GCC.
6*38fd1498Szrj 
7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
8*38fd1498Szrj the terms of the GNU General Public License as published by the Free
9*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
10*38fd1498Szrj version.
11*38fd1498Szrj 
12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*38fd1498Szrj for more details.
16*38fd1498Szrj 
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
20*38fd1498Szrj 
21*38fd1498Szrj #include "config.h"
22*38fd1498Szrj #include "system.h"
23*38fd1498Szrj #include "coretypes.h"
24*38fd1498Szrj #include "timevar.h"
25*38fd1498Szrj #include "options.h"
26*38fd1498Szrj 
27*38fd1498Szrj #ifndef HAVE_CLOCK_T
28*38fd1498Szrj typedef int clock_t;
29*38fd1498Szrj #endif
30*38fd1498Szrj 
31*38fd1498Szrj #ifndef HAVE_STRUCT_TMS
32*38fd1498Szrj struct tms
33*38fd1498Szrj {
34*38fd1498Szrj   clock_t tms_utime;
35*38fd1498Szrj   clock_t tms_stime;
36*38fd1498Szrj   clock_t tms_cutime;
37*38fd1498Szrj   clock_t tms_cstime;
38*38fd1498Szrj };
39*38fd1498Szrj #endif
40*38fd1498Szrj 
41*38fd1498Szrj #ifndef RUSAGE_SELF
42*38fd1498Szrj # define RUSAGE_SELF 0
43*38fd1498Szrj #endif
44*38fd1498Szrj 
45*38fd1498Szrj /* Calculation of scale factor to convert ticks to microseconds.
46*38fd1498Szrj    We mustn't use CLOCKS_PER_SEC except with clock().  */
47*38fd1498Szrj #if HAVE_SYSCONF && defined _SC_CLK_TCK
48*38fd1498Szrj # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
49*38fd1498Szrj #else
50*38fd1498Szrj # ifdef CLK_TCK
51*38fd1498Szrj #  define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
52*38fd1498Szrj # else
53*38fd1498Szrj #  ifdef HZ
54*38fd1498Szrj #   define TICKS_PER_SECOND HZ  /* traditional UNIX */
55*38fd1498Szrj #  else
56*38fd1498Szrj #   define TICKS_PER_SECOND 100 /* often the correct value */
57*38fd1498Szrj #  endif
58*38fd1498Szrj # endif
59*38fd1498Szrj #endif
60*38fd1498Szrj 
61*38fd1498Szrj /* Prefer times to getrusage to clock (each gives successively less
62*38fd1498Szrj    information).  */
63*38fd1498Szrj #ifdef HAVE_TIMES
64*38fd1498Szrj # if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
65*38fd1498Szrj   extern clock_t times (struct tms *);
66*38fd1498Szrj # endif
67*38fd1498Szrj # define USE_TIMES
68*38fd1498Szrj # define HAVE_USER_TIME
69*38fd1498Szrj # define HAVE_SYS_TIME
70*38fd1498Szrj # define HAVE_WALL_TIME
71*38fd1498Szrj #else
72*38fd1498Szrj #ifdef HAVE_GETRUSAGE
73*38fd1498Szrj # if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
74*38fd1498Szrj   extern int getrusage (int, struct rusage *);
75*38fd1498Szrj # endif
76*38fd1498Szrj # define USE_GETRUSAGE
77*38fd1498Szrj # define HAVE_USER_TIME
78*38fd1498Szrj # define HAVE_SYS_TIME
79*38fd1498Szrj #else
80*38fd1498Szrj #ifdef HAVE_CLOCK
81*38fd1498Szrj # if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
82*38fd1498Szrj   extern clock_t clock (void);
83*38fd1498Szrj # endif
84*38fd1498Szrj # define USE_CLOCK
85*38fd1498Szrj # define HAVE_USER_TIME
86*38fd1498Szrj #endif
87*38fd1498Szrj #endif
88*38fd1498Szrj #endif
89*38fd1498Szrj 
90*38fd1498Szrj /* libc is very likely to have snuck a call to sysconf() into one of
91*38fd1498Szrj    the underlying constants, and that can be very slow, so we have to
92*38fd1498Szrj    precompute them.  Whose wonderful idea was it to make all those
93*38fd1498Szrj    _constants_ variable at run time, anyway?  */
94*38fd1498Szrj #ifdef USE_TIMES
95*38fd1498Szrj static double ticks_to_msec;
96*38fd1498Szrj #define TICKS_TO_MSEC (1 / (double)TICKS_PER_SECOND)
97*38fd1498Szrj #endif
98*38fd1498Szrj 
99*38fd1498Szrj #ifdef USE_CLOCK
100*38fd1498Szrj static double clocks_to_msec;
101*38fd1498Szrj #define CLOCKS_TO_MSEC (1 / (double)CLOCKS_PER_SEC)
102*38fd1498Szrj #endif
103*38fd1498Szrj 
104*38fd1498Szrj /* Non-NULL if timevars should be used.  In GCC, this happens with
105*38fd1498Szrj    the -ftime-report flag.  */
106*38fd1498Szrj 
107*38fd1498Szrj timer *g_timer;
108*38fd1498Szrj 
109*38fd1498Szrj /* Total amount of memory allocated by garbage collector.  */
110*38fd1498Szrj 
111*38fd1498Szrj size_t timevar_ggc_mem_total;
112*38fd1498Szrj 
113*38fd1498Szrj /* The amount of memory that will cause us to report the timevar even
114*38fd1498Szrj    if the time spent is not significant.  */
115*38fd1498Szrj 
116*38fd1498Szrj #define GGC_MEM_BOUND (1 << 20)
117*38fd1498Szrj 
118*38fd1498Szrj /* See timevar.h for an explanation of timing variables.  */
119*38fd1498Szrj 
120*38fd1498Szrj static void get_time (struct timevar_time_def *);
121*38fd1498Szrj static void timevar_accumulate (struct timevar_time_def *,
122*38fd1498Szrj 				struct timevar_time_def *,
123*38fd1498Szrj 				struct timevar_time_def *);
124*38fd1498Szrj 
125*38fd1498Szrj /* The implementation of timing events for jit client code, allowing
126*38fd1498Szrj    arbitrary named items to appear on the timing stack.  */
127*38fd1498Szrj 
128*38fd1498Szrj class timer::named_items
129*38fd1498Szrj {
130*38fd1498Szrj  public:
131*38fd1498Szrj   named_items (timer *t);
132*38fd1498Szrj   ~named_items ();
133*38fd1498Szrj 
134*38fd1498Szrj   void push (const char *item_name);
135*38fd1498Szrj   void pop ();
136*38fd1498Szrj   void print (FILE *fp, const timevar_time_def *total);
137*38fd1498Szrj 
138*38fd1498Szrj  private:
139*38fd1498Szrj   /* Which timer instance does this relate to?  */
140*38fd1498Szrj   timer *m_timer;
141*38fd1498Szrj 
142*38fd1498Szrj   /* Dictionary, mapping from item names to timevar_def.
143*38fd1498Szrj      Note that currently we merely store/compare the raw string
144*38fd1498Szrj      pointers provided by client code; we don't take a copy,
145*38fd1498Szrj      or use strcmp.  */
146*38fd1498Szrj   hash_map <const char *, timer::timevar_def> m_hash_map;
147*38fd1498Szrj 
148*38fd1498Szrj   /* The order in which items were originally inserted.  */
149*38fd1498Szrj   auto_vec <const char *> m_names;
150*38fd1498Szrj };
151*38fd1498Szrj 
152*38fd1498Szrj /* The constructor for class timer::named_items.  */
153*38fd1498Szrj 
named_items(timer * t)154*38fd1498Szrj timer::named_items::named_items (timer *t)
155*38fd1498Szrj : m_timer (t),
156*38fd1498Szrj   m_hash_map (),
157*38fd1498Szrj   m_names ()
158*38fd1498Szrj {
159*38fd1498Szrj }
160*38fd1498Szrj 
161*38fd1498Szrj /* The destructor for class timer::named_items.  */
162*38fd1498Szrj 
~named_items()163*38fd1498Szrj timer::named_items::~named_items ()
164*38fd1498Szrj {
165*38fd1498Szrj }
166*38fd1498Szrj 
167*38fd1498Szrj /* Push the named item onto the timer stack.  */
168*38fd1498Szrj 
169*38fd1498Szrj void
push(const char * item_name)170*38fd1498Szrj timer::named_items::push (const char *item_name)
171*38fd1498Szrj {
172*38fd1498Szrj   gcc_assert (item_name);
173*38fd1498Szrj 
174*38fd1498Szrj   bool existed;
175*38fd1498Szrj   timer::timevar_def *def = &m_hash_map.get_or_insert (item_name, &existed);
176*38fd1498Szrj   if (!existed)
177*38fd1498Szrj     {
178*38fd1498Szrj       def->elapsed.user = 0;
179*38fd1498Szrj       def->elapsed.sys = 0;
180*38fd1498Szrj       def->elapsed.wall = 0;
181*38fd1498Szrj       def->name = item_name;
182*38fd1498Szrj       def->standalone = 0;
183*38fd1498Szrj       m_names.safe_push (item_name);
184*38fd1498Szrj     }
185*38fd1498Szrj   m_timer->push_internal (def);
186*38fd1498Szrj }
187*38fd1498Szrj 
188*38fd1498Szrj /* Pop the top item from the timer stack.  */
189*38fd1498Szrj 
190*38fd1498Szrj void
pop()191*38fd1498Szrj timer::named_items::pop ()
192*38fd1498Szrj {
193*38fd1498Szrj   m_timer->pop_internal ();
194*38fd1498Szrj }
195*38fd1498Szrj 
196*38fd1498Szrj /* Print the given client item.  Helper function for timer::print.  */
197*38fd1498Szrj 
198*38fd1498Szrj void
print(FILE * fp,const timevar_time_def * total)199*38fd1498Szrj timer::named_items::print (FILE *fp, const timevar_time_def *total)
200*38fd1498Szrj {
201*38fd1498Szrj   unsigned int i;
202*38fd1498Szrj   const char *item_name;
203*38fd1498Szrj   fprintf (fp, "Client items:\n");
204*38fd1498Szrj   FOR_EACH_VEC_ELT (m_names, i, item_name)
205*38fd1498Szrj     {
206*38fd1498Szrj       timer::timevar_def *def = m_hash_map.get (item_name);
207*38fd1498Szrj       gcc_assert (def);
208*38fd1498Szrj       m_timer->print_row (fp, total, def->name, def->elapsed);
209*38fd1498Szrj     }
210*38fd1498Szrj }
211*38fd1498Szrj 
212*38fd1498Szrj /* Fill the current times into TIME.  The definition of this function
213*38fd1498Szrj    also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
214*38fd1498Szrj    HAVE_WALL_TIME macros.  */
215*38fd1498Szrj 
216*38fd1498Szrj static void
get_time(struct timevar_time_def * now)217*38fd1498Szrj get_time (struct timevar_time_def *now)
218*38fd1498Szrj {
219*38fd1498Szrj   now->user = 0;
220*38fd1498Szrj   now->sys  = 0;
221*38fd1498Szrj   now->wall = 0;
222*38fd1498Szrj   now->ggc_mem = timevar_ggc_mem_total;
223*38fd1498Szrj 
224*38fd1498Szrj   {
225*38fd1498Szrj #ifdef USE_TIMES
226*38fd1498Szrj     struct tms tms;
227*38fd1498Szrj     now->wall = times (&tms)  * ticks_to_msec;
228*38fd1498Szrj     now->user = tms.tms_utime * ticks_to_msec;
229*38fd1498Szrj     now->sys  = tms.tms_stime * ticks_to_msec;
230*38fd1498Szrj #endif
231*38fd1498Szrj #ifdef USE_GETRUSAGE
232*38fd1498Szrj     struct rusage rusage;
233*38fd1498Szrj     getrusage (RUSAGE_SELF, &rusage);
234*38fd1498Szrj     now->user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec * 1e-6;
235*38fd1498Szrj     now->sys  = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec * 1e-6;
236*38fd1498Szrj #endif
237*38fd1498Szrj #ifdef USE_CLOCK
238*38fd1498Szrj     now->user = clock () * clocks_to_msec;
239*38fd1498Szrj #endif
240*38fd1498Szrj   }
241*38fd1498Szrj }
242*38fd1498Szrj 
243*38fd1498Szrj /* Add the difference between STOP_TIME and START_TIME to TIMER.  */
244*38fd1498Szrj 
245*38fd1498Szrj static void
timevar_accumulate(struct timevar_time_def * timer,struct timevar_time_def * start_time,struct timevar_time_def * stop_time)246*38fd1498Szrj timevar_accumulate (struct timevar_time_def *timer,
247*38fd1498Szrj 		    struct timevar_time_def *start_time,
248*38fd1498Szrj 		    struct timevar_time_def *stop_time)
249*38fd1498Szrj {
250*38fd1498Szrj   timer->user += stop_time->user - start_time->user;
251*38fd1498Szrj   timer->sys += stop_time->sys - start_time->sys;
252*38fd1498Szrj   timer->wall += stop_time->wall - start_time->wall;
253*38fd1498Szrj   timer->ggc_mem += stop_time->ggc_mem - start_time->ggc_mem;
254*38fd1498Szrj }
255*38fd1498Szrj 
256*38fd1498Szrj /* Class timer's constructor.  */
257*38fd1498Szrj 
timer()258*38fd1498Szrj timer::timer () :
259*38fd1498Szrj   m_stack (NULL),
260*38fd1498Szrj   m_unused_stack_instances (NULL),
261*38fd1498Szrj   m_start_time (),
262*38fd1498Szrj   m_jit_client_items (NULL)
263*38fd1498Szrj {
264*38fd1498Szrj   /* Zero all elapsed times.  */
265*38fd1498Szrj   memset (m_timevars, 0, sizeof (m_timevars));
266*38fd1498Szrj 
267*38fd1498Szrj   /* Initialize the names of timing variables.  */
268*38fd1498Szrj #define DEFTIMEVAR(identifier__, name__) \
269*38fd1498Szrj   m_timevars[identifier__].name = name__;
270*38fd1498Szrj #include "timevar.def"
271*38fd1498Szrj #undef DEFTIMEVAR
272*38fd1498Szrj 
273*38fd1498Szrj   /* Initialize configuration-specific state.
274*38fd1498Szrj      Ideally this would be one-time initialization.  */
275*38fd1498Szrj #ifdef USE_TIMES
276*38fd1498Szrj   ticks_to_msec = TICKS_TO_MSEC;
277*38fd1498Szrj #endif
278*38fd1498Szrj #ifdef USE_CLOCK
279*38fd1498Szrj   clocks_to_msec = CLOCKS_TO_MSEC;
280*38fd1498Szrj #endif
281*38fd1498Szrj }
282*38fd1498Szrj 
283*38fd1498Szrj /* Class timer's destructor.  */
284*38fd1498Szrj 
~timer()285*38fd1498Szrj timer::~timer ()
286*38fd1498Szrj {
287*38fd1498Szrj   timevar_stack_def *iter, *next;
288*38fd1498Szrj 
289*38fd1498Szrj   for (iter = m_stack; iter; iter = next)
290*38fd1498Szrj     {
291*38fd1498Szrj       next = iter->next;
292*38fd1498Szrj       free (iter);
293*38fd1498Szrj     }
294*38fd1498Szrj   for (iter = m_unused_stack_instances; iter; iter = next)
295*38fd1498Szrj     {
296*38fd1498Szrj       next = iter->next;
297*38fd1498Szrj       free (iter);
298*38fd1498Szrj     }
299*38fd1498Szrj   for (unsigned i = 0; i < TIMEVAR_LAST; ++i)
300*38fd1498Szrj     delete m_timevars[i].children;
301*38fd1498Szrj 
302*38fd1498Szrj   delete m_jit_client_items;
303*38fd1498Szrj }
304*38fd1498Szrj 
305*38fd1498Szrj /* Initialize timing variables.  */
306*38fd1498Szrj 
307*38fd1498Szrj void
timevar_init(void)308*38fd1498Szrj timevar_init (void)
309*38fd1498Szrj {
310*38fd1498Szrj   if (g_timer)
311*38fd1498Szrj     return;
312*38fd1498Szrj 
313*38fd1498Szrj   g_timer = new timer ();
314*38fd1498Szrj }
315*38fd1498Szrj 
316*38fd1498Szrj /* Push TIMEVAR onto the timing stack.  No further elapsed time is
317*38fd1498Szrj    attributed to the previous topmost timing variable on the stack;
318*38fd1498Szrj    subsequent elapsed time is attributed to TIMEVAR, until it is
319*38fd1498Szrj    popped or another element is pushed on top.
320*38fd1498Szrj 
321*38fd1498Szrj    TIMEVAR cannot be running as a standalone timer.  */
322*38fd1498Szrj 
323*38fd1498Szrj void
push(timevar_id_t timevar)324*38fd1498Szrj timer::push (timevar_id_t timevar)
325*38fd1498Szrj {
326*38fd1498Szrj   struct timevar_def *tv = &m_timevars[timevar];
327*38fd1498Szrj   push_internal (tv);
328*38fd1498Szrj }
329*38fd1498Szrj 
330*38fd1498Szrj /* Push TV onto the timing stack, either one of the builtin ones
331*38fd1498Szrj    for a timevar_id_t, or one provided by client code to libgccjit.  */
332*38fd1498Szrj 
333*38fd1498Szrj void
push_internal(struct timevar_def * tv)334*38fd1498Szrj timer::push_internal (struct timevar_def *tv)
335*38fd1498Szrj {
336*38fd1498Szrj   struct timevar_stack_def *context;
337*38fd1498Szrj   struct timevar_time_def now;
338*38fd1498Szrj 
339*38fd1498Szrj   gcc_assert (tv);
340*38fd1498Szrj 
341*38fd1498Szrj   /* Mark this timing variable as used.  */
342*38fd1498Szrj   tv->used = 1;
343*38fd1498Szrj 
344*38fd1498Szrj   /* Can't push a standalone timer.  */
345*38fd1498Szrj   gcc_assert (!tv->standalone);
346*38fd1498Szrj 
347*38fd1498Szrj   /* What time is it?  */
348*38fd1498Szrj   get_time (&now);
349*38fd1498Szrj 
350*38fd1498Szrj   /* If the stack isn't empty, attribute the current elapsed time to
351*38fd1498Szrj      the old topmost element.  */
352*38fd1498Szrj   if (m_stack)
353*38fd1498Szrj     timevar_accumulate (&m_stack->timevar->elapsed, &m_start_time, &now);
354*38fd1498Szrj 
355*38fd1498Szrj   /* Reset the start time; from now on, time is attributed to
356*38fd1498Szrj      TIMEVAR.  */
357*38fd1498Szrj   m_start_time = now;
358*38fd1498Szrj 
359*38fd1498Szrj   /* See if we have a previously-allocated stack instance.  If so,
360*38fd1498Szrj      take it off the list.  If not, malloc a new one.  */
361*38fd1498Szrj   if (m_unused_stack_instances != NULL)
362*38fd1498Szrj     {
363*38fd1498Szrj       context = m_unused_stack_instances;
364*38fd1498Szrj       m_unused_stack_instances = m_unused_stack_instances->next;
365*38fd1498Szrj     }
366*38fd1498Szrj   else
367*38fd1498Szrj     context = XNEW (struct timevar_stack_def);
368*38fd1498Szrj 
369*38fd1498Szrj   /* Fill it in and put it on the stack.  */
370*38fd1498Szrj   context->timevar = tv;
371*38fd1498Szrj   context->next = m_stack;
372*38fd1498Szrj   m_stack = context;
373*38fd1498Szrj }
374*38fd1498Szrj 
375*38fd1498Szrj /* Pop the topmost timing variable element off the timing stack.  The
376*38fd1498Szrj    popped variable must be TIMEVAR.  Elapsed time since the that
377*38fd1498Szrj    element was pushed on, or since it was last exposed on top of the
378*38fd1498Szrj    stack when the element above it was popped off, is credited to that
379*38fd1498Szrj    timing variable.  */
380*38fd1498Szrj 
381*38fd1498Szrj void
pop(timevar_id_t timevar)382*38fd1498Szrj timer::pop (timevar_id_t timevar)
383*38fd1498Szrj {
384*38fd1498Szrj   gcc_assert (&m_timevars[timevar] == m_stack->timevar);
385*38fd1498Szrj 
386*38fd1498Szrj   pop_internal ();
387*38fd1498Szrj }
388*38fd1498Szrj 
389*38fd1498Szrj /* Pop the topmost item from the stack, either one of the builtin ones
390*38fd1498Szrj    for a timevar_id_t, or one provided by client code to libgccjit.  */
391*38fd1498Szrj 
392*38fd1498Szrj void
pop_internal()393*38fd1498Szrj timer::pop_internal ()
394*38fd1498Szrj {
395*38fd1498Szrj   struct timevar_time_def now;
396*38fd1498Szrj   struct timevar_stack_def *popped = m_stack;
397*38fd1498Szrj 
398*38fd1498Szrj   /* What time is it?  */
399*38fd1498Szrj   get_time (&now);
400*38fd1498Szrj 
401*38fd1498Szrj   /* Attribute the elapsed time to the element we're popping.  */
402*38fd1498Szrj   timevar_accumulate (&popped->timevar->elapsed, &m_start_time, &now);
403*38fd1498Szrj 
404*38fd1498Szrj   /* Take the item off the stack.  */
405*38fd1498Szrj   m_stack = m_stack->next;
406*38fd1498Szrj 
407*38fd1498Szrj   /* Record the elapsed sub-time to the parent as well.  */
408*38fd1498Szrj   if (m_stack && time_report_details)
409*38fd1498Szrj     {
410*38fd1498Szrj       if (! m_stack->timevar->children)
411*38fd1498Szrj 	m_stack->timevar->children = new child_map_t (5);
412*38fd1498Szrj       bool existed_p;
413*38fd1498Szrj       timevar_time_def &time
414*38fd1498Szrj 	= m_stack->timevar->children->get_or_insert (popped->timevar, &existed_p);
415*38fd1498Szrj       if (! existed_p)
416*38fd1498Szrj 	memset (&time, 0, sizeof (timevar_time_def));
417*38fd1498Szrj       timevar_accumulate (&time, &m_start_time, &now);
418*38fd1498Szrj     }
419*38fd1498Szrj 
420*38fd1498Szrj   /* Reset the start time; from now on, time is attributed to the
421*38fd1498Szrj      element just exposed on the stack.  */
422*38fd1498Szrj   m_start_time = now;
423*38fd1498Szrj 
424*38fd1498Szrj   /* Don't delete the stack element; instead, add it to the list of
425*38fd1498Szrj      unused elements for later use.  */
426*38fd1498Szrj   popped->next = m_unused_stack_instances;
427*38fd1498Szrj   m_unused_stack_instances = popped;
428*38fd1498Szrj }
429*38fd1498Szrj 
430*38fd1498Szrj /* Start timing TIMEVAR independently of the timing stack.  Elapsed
431*38fd1498Szrj    time until timevar_stop is called for the same timing variable is
432*38fd1498Szrj    attributed to TIMEVAR.  */
433*38fd1498Szrj 
434*38fd1498Szrj void
timevar_start(timevar_id_t timevar)435*38fd1498Szrj timevar_start (timevar_id_t timevar)
436*38fd1498Szrj {
437*38fd1498Szrj   if (!g_timer)
438*38fd1498Szrj     return;
439*38fd1498Szrj 
440*38fd1498Szrj   g_timer->start (timevar);
441*38fd1498Szrj }
442*38fd1498Szrj 
443*38fd1498Szrj /* See timevar_start above.  */
444*38fd1498Szrj 
445*38fd1498Szrj void
start(timevar_id_t timevar)446*38fd1498Szrj timer::start (timevar_id_t timevar)
447*38fd1498Szrj {
448*38fd1498Szrj   struct timevar_def *tv = &m_timevars[timevar];
449*38fd1498Szrj 
450*38fd1498Szrj   /* Mark this timing variable as used.  */
451*38fd1498Szrj   tv->used = 1;
452*38fd1498Szrj 
453*38fd1498Szrj   /* Don't allow the same timing variable to be started more than
454*38fd1498Szrj      once.  */
455*38fd1498Szrj   gcc_assert (!tv->standalone);
456*38fd1498Szrj   tv->standalone = 1;
457*38fd1498Szrj 
458*38fd1498Szrj   get_time (&tv->start_time);
459*38fd1498Szrj }
460*38fd1498Szrj 
461*38fd1498Szrj /* Stop timing TIMEVAR.  Time elapsed since timevar_start was called
462*38fd1498Szrj    is attributed to it.  */
463*38fd1498Szrj 
464*38fd1498Szrj void
timevar_stop(timevar_id_t timevar)465*38fd1498Szrj timevar_stop (timevar_id_t timevar)
466*38fd1498Szrj {
467*38fd1498Szrj   if (!g_timer)
468*38fd1498Szrj     return;
469*38fd1498Szrj 
470*38fd1498Szrj   g_timer->stop (timevar);
471*38fd1498Szrj }
472*38fd1498Szrj 
473*38fd1498Szrj /* See timevar_stop above.  */
474*38fd1498Szrj 
475*38fd1498Szrj void
stop(timevar_id_t timevar)476*38fd1498Szrj timer::stop (timevar_id_t timevar)
477*38fd1498Szrj {
478*38fd1498Szrj   struct timevar_def *tv = &m_timevars[timevar];
479*38fd1498Szrj   struct timevar_time_def now;
480*38fd1498Szrj 
481*38fd1498Szrj   /* TIMEVAR must have been started via timevar_start.  */
482*38fd1498Szrj   gcc_assert (tv->standalone);
483*38fd1498Szrj   tv->standalone = 0; /* Enable a restart.  */
484*38fd1498Szrj 
485*38fd1498Szrj   get_time (&now);
486*38fd1498Szrj   timevar_accumulate (&tv->elapsed, &tv->start_time, &now);
487*38fd1498Szrj }
488*38fd1498Szrj 
489*38fd1498Szrj 
490*38fd1498Szrj /* Conditionally start timing TIMEVAR independently of the timing stack.
491*38fd1498Szrj    If the timer is already running, leave it running and return true.
492*38fd1498Szrj    Otherwise, start the timer and return false.
493*38fd1498Szrj    Elapsed time until the corresponding timevar_cond_stop
494*38fd1498Szrj    is called for the same timing variable is attributed to TIMEVAR.  */
495*38fd1498Szrj 
496*38fd1498Szrj bool
timevar_cond_start(timevar_id_t timevar)497*38fd1498Szrj timevar_cond_start (timevar_id_t timevar)
498*38fd1498Szrj {
499*38fd1498Szrj   if (!g_timer)
500*38fd1498Szrj     return false;
501*38fd1498Szrj 
502*38fd1498Szrj   return g_timer->cond_start (timevar);
503*38fd1498Szrj }
504*38fd1498Szrj 
505*38fd1498Szrj /* See timevar_cond_start above.  */
506*38fd1498Szrj 
507*38fd1498Szrj bool
cond_start(timevar_id_t timevar)508*38fd1498Szrj timer::cond_start (timevar_id_t timevar)
509*38fd1498Szrj {
510*38fd1498Szrj   struct timevar_def *tv = &m_timevars[timevar];
511*38fd1498Szrj 
512*38fd1498Szrj   /* Mark this timing variable as used.  */
513*38fd1498Szrj   tv->used = 1;
514*38fd1498Szrj 
515*38fd1498Szrj   if (tv->standalone)
516*38fd1498Szrj     return true;  /* The timevar is already running.  */
517*38fd1498Szrj 
518*38fd1498Szrj   /* Don't allow the same timing variable
519*38fd1498Szrj      to be unconditionally started more than once.  */
520*38fd1498Szrj   tv->standalone = 1;
521*38fd1498Szrj 
522*38fd1498Szrj   get_time (&tv->start_time);
523*38fd1498Szrj   return false;  /* The timevar was not already running.  */
524*38fd1498Szrj }
525*38fd1498Szrj 
526*38fd1498Szrj /* Conditionally stop timing TIMEVAR.  The RUNNING parameter must come
527*38fd1498Szrj    from the return value of a dynamically matching timevar_cond_start.
528*38fd1498Szrj    If the timer had already been RUNNING, do nothing.  Otherwise, time
529*38fd1498Szrj    elapsed since timevar_cond_start was called is attributed to it.  */
530*38fd1498Szrj 
531*38fd1498Szrj void
timevar_cond_stop(timevar_id_t timevar,bool running)532*38fd1498Szrj timevar_cond_stop (timevar_id_t timevar, bool running)
533*38fd1498Szrj {
534*38fd1498Szrj   if (!g_timer || running)
535*38fd1498Szrj     return;
536*38fd1498Szrj 
537*38fd1498Szrj   g_timer->cond_stop (timevar);
538*38fd1498Szrj }
539*38fd1498Szrj 
540*38fd1498Szrj /* See timevar_cond_stop above.  */
541*38fd1498Szrj 
542*38fd1498Szrj void
cond_stop(timevar_id_t timevar)543*38fd1498Szrj timer::cond_stop (timevar_id_t timevar)
544*38fd1498Szrj {
545*38fd1498Szrj   struct timevar_def *tv;
546*38fd1498Szrj   struct timevar_time_def now;
547*38fd1498Szrj 
548*38fd1498Szrj   tv = &m_timevars[timevar];
549*38fd1498Szrj 
550*38fd1498Szrj   /* TIMEVAR must have been started via timevar_cond_start.  */
551*38fd1498Szrj   gcc_assert (tv->standalone);
552*38fd1498Szrj   tv->standalone = 0; /* Enable a restart.  */
553*38fd1498Szrj 
554*38fd1498Szrj   get_time (&now);
555*38fd1498Szrj   timevar_accumulate (&tv->elapsed, &tv->start_time, &now);
556*38fd1498Szrj }
557*38fd1498Szrj 
558*38fd1498Szrj /* Push the named item onto the timing stack.  */
559*38fd1498Szrj 
560*38fd1498Szrj void
push_client_item(const char * item_name)561*38fd1498Szrj timer::push_client_item (const char *item_name)
562*38fd1498Szrj {
563*38fd1498Szrj   gcc_assert (item_name);
564*38fd1498Szrj 
565*38fd1498Szrj   /* Lazily create the named_items instance.  */
566*38fd1498Szrj   if (!m_jit_client_items)
567*38fd1498Szrj     m_jit_client_items = new named_items (this);
568*38fd1498Szrj 
569*38fd1498Szrj   m_jit_client_items->push (item_name);
570*38fd1498Szrj }
571*38fd1498Szrj 
572*38fd1498Szrj /* Pop the top-most client item from the timing stack.  */
573*38fd1498Szrj 
574*38fd1498Szrj void
pop_client_item()575*38fd1498Szrj timer::pop_client_item ()
576*38fd1498Szrj {
577*38fd1498Szrj   gcc_assert (m_jit_client_items);
578*38fd1498Szrj   m_jit_client_items->pop ();
579*38fd1498Szrj }
580*38fd1498Szrj 
581*38fd1498Szrj /* Validate that phase times are consistent.  */
582*38fd1498Szrj 
583*38fd1498Szrj void
validate_phases(FILE * fp)584*38fd1498Szrj timer::validate_phases (FILE *fp) const
585*38fd1498Szrj {
586*38fd1498Szrj   unsigned int /* timevar_id_t */ id;
587*38fd1498Szrj   const timevar_time_def *total = &m_timevars[TV_TOTAL].elapsed;
588*38fd1498Szrj   double phase_user = 0.0;
589*38fd1498Szrj   double phase_sys = 0.0;
590*38fd1498Szrj   double phase_wall = 0.0;
591*38fd1498Szrj   size_t phase_ggc_mem = 0;
592*38fd1498Szrj   static char phase_prefix[] = "phase ";
593*38fd1498Szrj   const double tolerance = 1.000001;  /* One part in a million.  */
594*38fd1498Szrj 
595*38fd1498Szrj   for (id = 0; id < (unsigned int) TIMEVAR_LAST; ++id)
596*38fd1498Szrj     {
597*38fd1498Szrj       const timevar_def *tv = &m_timevars[(timevar_id_t) id];
598*38fd1498Szrj 
599*38fd1498Szrj       /* Don't evaluate timing variables that were never used.  */
600*38fd1498Szrj       if (!tv->used)
601*38fd1498Szrj 	continue;
602*38fd1498Szrj 
603*38fd1498Szrj       if (strncmp (tv->name, phase_prefix, sizeof phase_prefix - 1) == 0)
604*38fd1498Szrj 	{
605*38fd1498Szrj 	  phase_user += tv->elapsed.user;
606*38fd1498Szrj 	  phase_sys += tv->elapsed.sys;
607*38fd1498Szrj 	  phase_wall += tv->elapsed.wall;
608*38fd1498Szrj 	  phase_ggc_mem += tv->elapsed.ggc_mem;
609*38fd1498Szrj 	}
610*38fd1498Szrj     }
611*38fd1498Szrj 
612*38fd1498Szrj   if (phase_user > total->user * tolerance
613*38fd1498Szrj       || phase_sys > total->sys * tolerance
614*38fd1498Szrj       || phase_wall > total->wall * tolerance
615*38fd1498Szrj       || phase_ggc_mem > total->ggc_mem * tolerance)
616*38fd1498Szrj     {
617*38fd1498Szrj 
618*38fd1498Szrj       fprintf (fp, "Timing error: total of phase timers exceeds total time.\n");
619*38fd1498Szrj       if (phase_user > total->user)
620*38fd1498Szrj 	fprintf (fp, "user    %24.18e > %24.18e\n", phase_user, total->user);
621*38fd1498Szrj       if (phase_sys > total->sys)
622*38fd1498Szrj 	fprintf (fp, "sys     %24.18e > %24.18e\n", phase_sys, total->sys);
623*38fd1498Szrj       if (phase_wall > total->wall)
624*38fd1498Szrj 	fprintf (fp, "wall    %24.18e > %24.18e\n", phase_wall, total->wall);
625*38fd1498Szrj       if (phase_ggc_mem > total->ggc_mem)
626*38fd1498Szrj 	fprintf (fp, "ggc_mem %24lu > %24lu\n", (unsigned long)phase_ggc_mem,
627*38fd1498Szrj 		 (unsigned long)total->ggc_mem);
628*38fd1498Szrj       gcc_unreachable ();
629*38fd1498Szrj     }
630*38fd1498Szrj }
631*38fd1498Szrj 
632*38fd1498Szrj /* Helper function for timer::print.  */
633*38fd1498Szrj 
634*38fd1498Szrj void
print_row(FILE * fp,const timevar_time_def * total,const char * name,const timevar_time_def & elapsed)635*38fd1498Szrj timer::print_row (FILE *fp,
636*38fd1498Szrj 		  const timevar_time_def *total,
637*38fd1498Szrj 		  const char *name, const timevar_time_def &elapsed)
638*38fd1498Szrj {
639*38fd1498Szrj   /* The timing variable name.  */
640*38fd1498Szrj   fprintf (fp, " %-35s:", name);
641*38fd1498Szrj 
642*38fd1498Szrj #ifdef HAVE_USER_TIME
643*38fd1498Szrj   /* Print user-mode time for this process.  */
644*38fd1498Szrj   fprintf (fp, "%7.2f (%3.0f%%)",
645*38fd1498Szrj 	   elapsed.user,
646*38fd1498Szrj 	   (total->user == 0 ? 0 : elapsed.user / total->user) * 100);
647*38fd1498Szrj #endif /* HAVE_USER_TIME */
648*38fd1498Szrj 
649*38fd1498Szrj #ifdef HAVE_SYS_TIME
650*38fd1498Szrj   /* Print system-mode time for this process.  */
651*38fd1498Szrj   fprintf (fp, "%7.2f (%3.0f%%)",
652*38fd1498Szrj 	   elapsed.sys,
653*38fd1498Szrj 	   (total->sys == 0 ? 0 : elapsed.sys / total->sys) * 100);
654*38fd1498Szrj #endif /* HAVE_SYS_TIME */
655*38fd1498Szrj 
656*38fd1498Szrj #ifdef HAVE_WALL_TIME
657*38fd1498Szrj   /* Print wall clock time elapsed.  */
658*38fd1498Szrj   fprintf (fp, "%7.2f (%3.0f%%)",
659*38fd1498Szrj 	   elapsed.wall,
660*38fd1498Szrj 	   (total->wall == 0 ? 0 : elapsed.wall / total->wall) * 100);
661*38fd1498Szrj #endif /* HAVE_WALL_TIME */
662*38fd1498Szrj 
663*38fd1498Szrj   /* Print the amount of ggc memory allocated.  */
664*38fd1498Szrj   fprintf (fp, "%8u kB (%3.0f%%)",
665*38fd1498Szrj 	   (unsigned) (elapsed.ggc_mem >> 10),
666*38fd1498Szrj 	   (total->ggc_mem == 0
667*38fd1498Szrj 	    ? 0
668*38fd1498Szrj 	    : (float) elapsed.ggc_mem / total->ggc_mem) * 100);
669*38fd1498Szrj 
670*38fd1498Szrj   putc ('\n', fp);
671*38fd1498Szrj }
672*38fd1498Szrj 
673*38fd1498Szrj /* Return whether ELAPSED is all zero.  */
674*38fd1498Szrj 
675*38fd1498Szrj bool
all_zero(const timevar_time_def & elapsed)676*38fd1498Szrj timer::all_zero (const timevar_time_def &elapsed)
677*38fd1498Szrj {
678*38fd1498Szrj   const double tiny = 5e-3;
679*38fd1498Szrj   return (elapsed.user < tiny
680*38fd1498Szrj 	  && elapsed.sys < tiny
681*38fd1498Szrj 	  && elapsed.wall < tiny
682*38fd1498Szrj 	  && elapsed.ggc_mem < GGC_MEM_BOUND);
683*38fd1498Szrj }
684*38fd1498Szrj 
685*38fd1498Szrj /* Summarize timing variables to FP.  The timing variable TV_TOTAL has
686*38fd1498Szrj    a special meaning -- it's considered to be the total elapsed time,
687*38fd1498Szrj    for normalizing the others, and is displayed last.  */
688*38fd1498Szrj 
689*38fd1498Szrj void
print(FILE * fp)690*38fd1498Szrj timer::print (FILE *fp)
691*38fd1498Szrj {
692*38fd1498Szrj   /* Only print stuff if we have some sort of time information.  */
693*38fd1498Szrj #if defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME) || defined (HAVE_WALL_TIME)
694*38fd1498Szrj   unsigned int /* timevar_id_t */ id;
695*38fd1498Szrj   const timevar_time_def *total = &m_timevars[TV_TOTAL].elapsed;
696*38fd1498Szrj   struct timevar_time_def now;
697*38fd1498Szrj 
698*38fd1498Szrj   /* Update timing information in case we're calling this from GDB.  */
699*38fd1498Szrj 
700*38fd1498Szrj   if (fp == 0)
701*38fd1498Szrj     fp = stderr;
702*38fd1498Szrj 
703*38fd1498Szrj   /* What time is it?  */
704*38fd1498Szrj   get_time (&now);
705*38fd1498Szrj 
706*38fd1498Szrj   /* If the stack isn't empty, attribute the current elapsed time to
707*38fd1498Szrj      the old topmost element.  */
708*38fd1498Szrj   if (m_stack)
709*38fd1498Szrj     timevar_accumulate (&m_stack->timevar->elapsed, &m_start_time, &now);
710*38fd1498Szrj 
711*38fd1498Szrj   /* Reset the start time; from now on, time is attributed to
712*38fd1498Szrj      TIMEVAR.  */
713*38fd1498Szrj   m_start_time = now;
714*38fd1498Szrj 
715*38fd1498Szrj   fprintf (fp, "\n%-35s%16s%14s%14s%18s\n", "Time variable", "usr", "sys",
716*38fd1498Szrj 	   "wall", "GGC");
717*38fd1498Szrj   if (m_jit_client_items)
718*38fd1498Szrj     fputs ("GCC items:\n", fp);
719*38fd1498Szrj   for (id = 0; id < (unsigned int) TIMEVAR_LAST; ++id)
720*38fd1498Szrj     {
721*38fd1498Szrj       const timevar_def *tv = &m_timevars[(timevar_id_t) id];
722*38fd1498Szrj 
723*38fd1498Szrj       /* Don't print the total execution time here; that goes at the
724*38fd1498Szrj 	 end.  */
725*38fd1498Szrj       if ((timevar_id_t) id == TV_TOTAL)
726*38fd1498Szrj 	continue;
727*38fd1498Szrj 
728*38fd1498Szrj       /* Don't print timing variables that were never used.  */
729*38fd1498Szrj       if (!tv->used)
730*38fd1498Szrj 	continue;
731*38fd1498Szrj 
732*38fd1498Szrj       bool any_children_with_time = false;
733*38fd1498Szrj       if (tv->children)
734*38fd1498Szrj 	for (child_map_t::iterator i = tv->children->begin ();
735*38fd1498Szrj 	     i != tv->children->end (); ++i)
736*38fd1498Szrj 	  if (! all_zero ((*i).second))
737*38fd1498Szrj 	    {
738*38fd1498Szrj 	      any_children_with_time = true;
739*38fd1498Szrj 	      break;
740*38fd1498Szrj 	    }
741*38fd1498Szrj 
742*38fd1498Szrj       /* Don't print timing variables if we're going to get a row of
743*38fd1498Szrj          zeroes.  Unless there are children with non-zero time.  */
744*38fd1498Szrj       if (! any_children_with_time
745*38fd1498Szrj 	  && all_zero (tv->elapsed))
746*38fd1498Szrj 	continue;
747*38fd1498Szrj 
748*38fd1498Szrj       print_row (fp, total, tv->name, tv->elapsed);
749*38fd1498Szrj 
750*38fd1498Szrj       if (tv->children)
751*38fd1498Szrj 	for (child_map_t::iterator i = tv->children->begin ();
752*38fd1498Szrj 	     i != tv->children->end (); ++i)
753*38fd1498Szrj 	  {
754*38fd1498Szrj 	    timevar_def *tv2 = (*i).first;
755*38fd1498Szrj 	    /* Don't print timing variables if we're going to get a row of
756*38fd1498Szrj 	       zeroes.  */
757*38fd1498Szrj 	    if (! all_zero ((*i).second))
758*38fd1498Szrj 	      {
759*38fd1498Szrj 		char lname[256];
760*38fd1498Szrj 		snprintf (lname, 256, "`- %s", tv2->name);
761*38fd1498Szrj 		print_row (fp, total, lname, (*i).second);
762*38fd1498Szrj 	      }
763*38fd1498Szrj 	  }
764*38fd1498Szrj     }
765*38fd1498Szrj   if (m_jit_client_items)
766*38fd1498Szrj     m_jit_client_items->print (fp, total);
767*38fd1498Szrj 
768*38fd1498Szrj   /* Print total time.  */
769*38fd1498Szrj   fprintf (fp, " %-35s:", "TOTAL");
770*38fd1498Szrj #ifdef HAVE_USER_TIME
771*38fd1498Szrj   fprintf (fp, "%7.2f      ", total->user);
772*38fd1498Szrj #endif
773*38fd1498Szrj #ifdef HAVE_SYS_TIME
774*38fd1498Szrj   fprintf (fp, "%8.2f      ", total->sys);
775*38fd1498Szrj #endif
776*38fd1498Szrj #ifdef HAVE_WALL_TIME
777*38fd1498Szrj   fprintf (fp, "%8.2f      ", total->wall);
778*38fd1498Szrj #endif
779*38fd1498Szrj   fprintf (fp, "%9u kB\n", (unsigned) (total->ggc_mem >> 10));
780*38fd1498Szrj 
781*38fd1498Szrj   if (CHECKING_P || flag_checking)
782*38fd1498Szrj     fprintf (fp, "Extra diagnostic checks enabled; compiler may run slowly.\n");
783*38fd1498Szrj   if (CHECKING_P)
784*38fd1498Szrj     fprintf (fp, "Configure with --enable-checking=release to disable checks.\n");
785*38fd1498Szrj #ifndef ENABLE_ASSERT_CHECKING
786*38fd1498Szrj   fprintf (fp, "Internal checks disabled; compiler is not suited for release.\n");
787*38fd1498Szrj   fprintf (fp, "Configure with --enable-checking=release to enable checks.\n");
788*38fd1498Szrj #endif
789*38fd1498Szrj 
790*38fd1498Szrj #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
791*38fd1498Szrj 	  || defined (HAVE_WALL_TIME) */
792*38fd1498Szrj 
793*38fd1498Szrj   validate_phases (fp);
794*38fd1498Szrj }
795*38fd1498Szrj 
796*38fd1498Szrj /* Get the name of the topmost item.  For use by jit for validating
797*38fd1498Szrj    inputs to gcc_jit_timer_pop.  */
798*38fd1498Szrj const char *
get_topmost_item_name()799*38fd1498Szrj timer::get_topmost_item_name () const
800*38fd1498Szrj {
801*38fd1498Szrj   if (m_stack)
802*38fd1498Szrj     return m_stack->timevar->name;
803*38fd1498Szrj   else
804*38fd1498Szrj     return NULL;
805*38fd1498Szrj }
806*38fd1498Szrj 
807*38fd1498Szrj /* Prints a message to stderr stating that time elapsed in STR is
808*38fd1498Szrj    TOTAL (given in microseconds).  */
809*38fd1498Szrj 
810*38fd1498Szrj void
print_time(const char * str,long total)811*38fd1498Szrj print_time (const char *str, long total)
812*38fd1498Szrj {
813*38fd1498Szrj   long all_time = get_run_time ();
814*38fd1498Szrj   fprintf (stderr,
815*38fd1498Szrj 	   "time in %s: %ld.%06ld (%ld%%)\n",
816*38fd1498Szrj 	   str, total / 1000000, total % 1000000,
817*38fd1498Szrj 	   all_time == 0 ? 0
818*38fd1498Szrj 	   : (long) (((100.0 * (double) total) / (double) all_time) + .5));
819*38fd1498Szrj }
820