1 /* Timing variables for measuring compiler performance.
2 Copyright (C) 2000, 2002 Free Software Foundation, Inc.
3 Contributed by Alex Samuel <samuel@codesourcery.com>
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison 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 2, or (at your option) any later
12 Bison 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 Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 /* This source file is taken from the GCC source code, with slight
32 modifications that are under control of the IN_GCC preprocessor
33 variable. The !IN_GCC part of this file is specific to Bison. */
35 # include "../src/system.h"
37 # include <sys/time.h>
39 int timevar_report
= 0;
44 #ifdef HAVE_SYS_TIMES_H
45 # include <sys/times.h>
47 #ifdef HAVE_SYS_RESOURCE_H
48 #include <sys/resource.h>
55 #ifndef HAVE_STRUCT_TMS
65 #if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
66 extern int getrusage
PARAMS ((int, struct rusage
*));
68 #if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
69 extern clock_t times
PARAMS ((struct tms
*));
71 #if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
72 extern clock_t clock
PARAMS ((void));
76 # define RUSAGE_SELF 0
79 /* Calculation of scale factor to convert ticks to microseconds.
80 We mustn't use CLOCKS_PER_SEC except with clock(). */
81 #if HAVE_SYSCONF && defined _SC_CLK_TCK
82 # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
85 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
88 # define TICKS_PER_SECOND HZ /* traditional UNIX */
90 # define TICKS_PER_SECOND 100 /* often the correct value */
95 /* Prefer times to getrusage to clock (each gives successively less
99 # define HAVE_USER_TIME
100 # define HAVE_SYS_TIME
101 # define HAVE_WALL_TIME
103 #ifdef HAVE_GETRUSAGE
104 # define USE_GETRUSAGE
105 # define HAVE_USER_TIME
106 # define HAVE_SYS_TIME
110 # define HAVE_USER_TIME
115 /* libc is very likely to have snuck a call to sysconf() into one of
116 the underlying constants, and that can be very slow, so we have to
117 precompute them. Whose wonderful idea was it to make all those
118 _constants_ variable at run time, anyway? */
120 static float ticks_to_msec
;
121 #define TICKS_TO_MSEC (1.0 / TICKS_PER_SECOND)
125 static float clocks_to_msec
;
126 #define CLOCKS_TO_MSEC (1.0 / CLOCKS_PER_SEC)
134 /* See timevar.h for an explanation of timing variables. */
136 /* This macro evaluates to nonzero if timing variables are enabled. */
137 #define TIMEVAR_ENABLE (timevar_report)
139 /* A timing variable. */
143 /* Elapsed time for this variable. */
144 struct timevar_time_def elapsed
;
146 /* If this variable is timed independently of the timing stack,
147 using timevar_start, this contains the start time. */
148 struct timevar_time_def start_time
;
150 /* The name of this timing variable. */
153 /* Non-zero if this timing variable is running as a standalone
155 unsigned standalone
: 1;
157 /* Non-zero if this timing variable was ever started or pushed onto
162 /* An element on the timing stack. Elapsed time is attributed to the
163 topmost timing variable on the stack. */
165 struct timevar_stack_def
167 /* The timing variable at this stack level. */
168 struct timevar_def
*timevar
;
170 /* The next lower timing variable context in the stack. */
171 struct timevar_stack_def
*next
;
174 /* Declared timing variables. Constructed from the contents of
176 static struct timevar_def timevars
[TIMEVAR_LAST
];
178 /* The top of the timing stack. */
179 static struct timevar_stack_def
*stack
;
181 /* A list of unused (i.e. allocated and subsequently popped)
182 timevar_stack_def instances. */
183 static struct timevar_stack_def
*unused_stack_instances
;
185 /* The time at which the topmost element on the timing stack was
186 pushed. Time elapsed since then is attributed to the topmost
188 static struct timevar_time_def start_time
;
191 PARAMS ((struct timevar_time_def
*));
192 static void timevar_accumulate
193 PARAMS ((struct timevar_time_def
*, struct timevar_time_def
*,
194 struct timevar_time_def
*));
196 /* Fill the current times into TIME. The definition of this function
197 also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
198 HAVE_WALL_TIME macros. */
202 struct timevar_time_def
*now
;
214 now
->wall
= times (&tms
) * ticks_to_msec
;
216 now
->user
= tms
.tms_utime
* ticks_to_msec
;
217 now
->sys
= tms
.tms_stime
* ticks_to_msec
;
219 now
->user
= (tms
.tms_utime
+ tms
.tms_cutime
) * ticks_to_msec
;
220 now
->sys
= (tms
.tms_stime
+ tms
.tms_cstime
) * ticks_to_msec
;
224 struct rusage rusage
;
226 getrusage (RUSAGE_SELF
, &rusage
);
228 getrusage (RUSAGE_CHILDREN
, &rusage
);
230 now
->user
= rusage
.ru_utime
.tv_sec
+ rusage
.ru_utime
.tv_usec
* 1e-6;
231 now
->sys
= rusage
.ru_stime
.tv_sec
+ rusage
.ru_stime
.tv_usec
* 1e-6;
234 now
->user
= clock () * clocks_to_msec
;
239 /* Add the difference between STOP and START to TIMER. */
242 timevar_accumulate (timer
, start
, stop
)
243 struct timevar_time_def
*timer
;
244 struct timevar_time_def
*start
;
245 struct timevar_time_def
*stop
;
247 timer
->user
+= stop
->user
- start
->user
;
248 timer
->sys
+= stop
->sys
- start
->sys
;
249 timer
->wall
+= stop
->wall
- start
->wall
;
252 /* Initialize timing variables. */
260 /* Zero all elapsed times. */
261 memset ((void *) timevars
, 0, sizeof (timevars
));
263 /* Initialize the names of timing variables. */
264 #define DEFTIMEVAR(identifier__, name__) \
265 timevars[identifier__].name = name__;
266 #include "timevar.def"
270 ticks_to_msec
= TICKS_TO_MSEC
;
273 clocks_to_msec
= CLOCKS_TO_MSEC
;
277 /* Push TIMEVAR onto the timing stack. No further elapsed time is
278 attributed to the previous topmost timing variable on the stack;
279 subsequent elapsed time is attributed to TIMEVAR, until it is
280 popped or another element is pushed on top.
282 TIMEVAR cannot be running as a standalone timer. */
285 timevar_push (timevar
)
286 timevar_id_t timevar
;
288 struct timevar_def
*tv
= &timevars
[timevar
];
289 struct timevar_stack_def
*context
;
290 struct timevar_time_def now
;
295 /* Mark this timing variable as used. */
298 /* Can't push a standalone timer. */
302 /* What time is it? */
305 /* If the stack isn't empty, attribute the current elapsed time to
306 the old topmost element. */
308 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
310 /* Reset the start time; from now on, time is attributed to
314 /* See if we have a previously-allocated stack instance. If so,
315 take it off the list. If not, malloc a new one. */
316 if (unused_stack_instances
!= NULL
)
318 context
= unused_stack_instances
;
319 unused_stack_instances
= unused_stack_instances
->next
;
322 context
= (struct timevar_stack_def
*)
323 xmalloc (sizeof (struct timevar_stack_def
));
325 /* Fill it in and put it on the stack. */
326 context
->timevar
= tv
;
327 context
->next
= stack
;
331 /* Pop the topmost timing variable element off the timing stack. The
332 popped variable must be TIMEVAR. Elapsed time since the that
333 element was pushed on, or since it was last exposed on top of the
334 stack when the element above it was popped off, is credited to that
338 timevar_pop (timevar
)
339 timevar_id_t timevar
;
341 struct timevar_time_def now
;
342 struct timevar_stack_def
*popped
= stack
;
347 if (&timevars
[timevar
] != stack
->timevar
)
350 /* What time is it? */
353 /* Attribute the elapsed time to the element we're popping. */
354 timevar_accumulate (&popped
->timevar
->elapsed
, &start_time
, &now
);
356 /* Reset the start time; from now on, time is attributed to the
357 element just exposed on the stack. */
360 /* Take the item off the stack. */
363 /* Don't delete the stack element; instead, add it to the list of
364 unused elements for later use. */
365 popped
->next
= unused_stack_instances
;
366 unused_stack_instances
= popped
;
369 /* Start timing TIMEVAR independently of the timing stack. Elapsed
370 time until timevar_stop is called for the same timing variable is
371 attributed to TIMEVAR. */
374 timevar_start (timevar
)
375 timevar_id_t timevar
;
377 struct timevar_def
*tv
= &timevars
[timevar
];
382 /* Mark this timing variable as used. */
385 /* Don't allow the same timing variable to be started more than
391 get_time (&tv
->start_time
);
394 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
395 is attributed to it. */
398 timevar_stop (timevar
)
399 timevar_id_t timevar
;
401 struct timevar_def
*tv
= &timevars
[timevar
];
402 struct timevar_time_def now
;
407 /* TIMEVAR must have been started via timevar_start. */
412 timevar_accumulate (&tv
->elapsed
, &tv
->start_time
, &now
);
415 /* Fill the elapsed time for TIMEVAR into ELAPSED. Returns
416 update-to-date information even if TIMEVAR is currently running. */
419 timevar_get (timevar
, elapsed
)
420 timevar_id_t timevar
;
421 struct timevar_time_def
*elapsed
;
423 struct timevar_def
*tv
= &timevars
[timevar
];
424 struct timevar_time_def now
;
426 *elapsed
= tv
->elapsed
;
428 /* Is TIMEVAR currently running as a standalone timer? */
432 timevar_accumulate (elapsed
, &tv
->start_time
, &now
);
434 /* Or is TIMEVAR at the top of the timer stack? */
435 else if (stack
->timevar
== tv
)
438 timevar_accumulate (elapsed
, &start_time
, &now
);
442 /* Summarize timing variables to FP. The timing variable TV_TOTAL has
443 a special meaning -- it's considered to be the total elapsed time,
444 for normalizing the others, and is displayed last. */
450 /* Only print stuff if we have some sort of time information. */
451 #if defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME) || defined (HAVE_WALL_TIME)
452 unsigned int /* timevar_id_t */ id
;
453 struct timevar_time_def
*total
= &timevars
[TV_TOTAL
].elapsed
;
454 struct timevar_time_def now
;
459 /* Update timing information in case we're calling this from GDB. */
464 /* What time is it? */
467 /* If the stack isn't empty, attribute the current elapsed time to
468 the old topmost element. */
470 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
472 /* Reset the start time; from now on, time is attributed to
476 fputs (_("\nExecution times (seconds)\n"), fp
);
477 for (id
= 0; id
< (unsigned int) TIMEVAR_LAST
; ++id
)
479 struct timevar_def
*tv
= &timevars
[(timevar_id_t
) id
];
480 const float tiny
= 5e-3;
482 /* Don't print the total execution time here; that goes at the
484 if ((timevar_id_t
) id
== TV_TOTAL
)
487 /* Don't print timing variables that were never used. */
491 /* Don't print timing variables if we're going to get a row of
493 if (tv
->elapsed
.user
< tiny
494 && tv
->elapsed
.sys
< tiny
495 && tv
->elapsed
.wall
< tiny
)
498 /* The timing variable name. */
499 fprintf (fp
, " %-22s:", tv
->name
);
501 #ifdef HAVE_USER_TIME
502 /* Print user-mode time for this process. */
503 fprintf (fp
, "%7.2f (%2.0f%%) usr",
505 (total
->user
== 0 ? 0 : tv
->elapsed
.user
/ total
->user
) * 100);
506 #endif /* HAVE_USER_TIME */
509 /* Print system-mode time for this process. */
510 fprintf (fp
, "%7.2f (%2.0f%%) sys",
512 (total
->sys
== 0 ? 0 : tv
->elapsed
.sys
/ total
->sys
) * 100);
513 #endif /* HAVE_SYS_TIME */
515 #ifdef HAVE_WALL_TIME
516 /* Print wall clock time elapsed. */
517 fprintf (fp
, "%7.2f (%2.0f%%) wall",
519 (total
->wall
== 0 ? 0 : tv
->elapsed
.wall
/ total
->wall
) * 100);
520 #endif /* HAVE_WALL_TIME */
525 /* Print total time. */
526 fputs (_(" TOTAL :"), fp
);
527 #ifdef HAVE_USER_TIME
528 fprintf (fp
, "%7.2f ", total
->user
);
531 fprintf (fp
, "%7.2f ", total
->sys
);
533 #ifdef HAVE_WALL_TIME
534 fprintf (fp
, "%7.2f\n", total
->wall
);
537 #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
538 || defined (HAVE_WALL_TIME) */
541 /* Returns time (user + system) used so far by the compiler process,
547 struct timevar_time_def total_elapsed
;
548 timevar_get (TV_TOTAL
, &total_elapsed
);
549 return total_elapsed
.user
+ total_elapsed
.sys
;
552 /* Prints a message to stderr stating that time elapsed in STR is
553 TOTAL (given in microseconds). */
556 print_time (str
, total
)
560 long all_time
= get_run_time ();
562 _("time in %s: %ld.%06ld (%ld%%)\n"),
563 str
, total
/ 1000000, total
% 1000000,
565 : (long) (((100.0 * (double) total
) / (double) all_time
) + .5));