1 /* Timing variables for measuring compiler performance.
2 Copyright (C) 2000, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Alex Samuel <samuel@codesourcery.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 /* This source file is taken from the GCC source code, with slight
29 modifications that are under control of the IN_GCC preprocessor
30 variable. The !IN_GCC part of this file is specific to Bison. */
32 # include "../src/system.h"
34 # include <sys/time.h>
36 int timevar_report
= 0;
41 #ifdef HAVE_SYS_TIMES_H
42 # include <sys/times.h>
44 #ifdef HAVE_SYS_RESOURCE_H
45 #include <sys/resource.h>
52 #ifndef HAVE_STRUCT_TMS
62 #if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
63 extern int getrusage (int, struct rusage
*);
65 #if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
66 extern clock_t times (struct tms
*);
68 #if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
69 extern clock_t clock (void);
73 # define RUSAGE_SELF 0
76 /* Calculation of scale factor to convert ticks to microseconds.
77 We mustn't use CLOCKS_PER_SEC except with clock(). */
78 #if HAVE_SYSCONF && defined _SC_CLK_TCK
79 # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
82 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
85 # define TICKS_PER_SECOND HZ /* traditional UNIX */
87 # define TICKS_PER_SECOND 100 /* often the correct value */
92 /* Prefer times to getrusage to clock (each gives successively less
96 # define HAVE_USER_TIME
97 # define HAVE_SYS_TIME
98 # define HAVE_WALL_TIME
100 #ifdef HAVE_GETRUSAGE
101 # define USE_GETRUSAGE
102 # define HAVE_USER_TIME
103 # define HAVE_SYS_TIME
107 # define HAVE_USER_TIME
112 /* libc is very likely to have snuck a call to sysconf() into one of
113 the underlying constants, and that can be very slow, so we have to
114 precompute them. Whose wonderful idea was it to make all those
115 _constants_ variable at run time, anyway? */
117 static float ticks_to_msec
;
118 #define TICKS_TO_MSEC (1.0 / TICKS_PER_SECOND)
122 static float clocks_to_msec
;
123 #define CLOCKS_TO_MSEC (1.0 / CLOCKS_PER_SEC)
131 /* See timevar.h for an explanation of timing variables. */
133 /* This macro evaluates to nonzero if timing variables are enabled. */
134 #define TIMEVAR_ENABLE (timevar_report)
136 /* A timing variable. */
140 /* Elapsed time for this variable. */
141 struct timevar_time_def elapsed
;
143 /* If this variable is timed independently of the timing stack,
144 using timevar_start, this contains the start time. */
145 struct timevar_time_def start_time
;
147 /* The name of this timing variable. */
150 /* Non-zero if this timing variable is running as a standalone
152 unsigned standalone
: 1;
154 /* Non-zero if this timing variable was ever started or pushed onto
159 /* An element on the timing stack. Elapsed time is attributed to the
160 topmost timing variable on the stack. */
162 struct timevar_stack_def
164 /* The timing variable at this stack level. */
165 struct timevar_def
*timevar
;
167 /* The next lower timing variable context in the stack. */
168 struct timevar_stack_def
*next
;
171 /* Declared timing variables. Constructed from the contents of
173 static struct timevar_def timevars
[TIMEVAR_LAST
];
175 /* The top of the timing stack. */
176 static struct timevar_stack_def
*stack
;
178 /* A list of unused (i.e. allocated and subsequently popped)
179 timevar_stack_def instances. */
180 static struct timevar_stack_def
*unused_stack_instances
;
182 /* The time at which the topmost element on the timing stack was
183 pushed. Time elapsed since then is attributed to the topmost
185 static struct timevar_time_def start_time
;
187 static void get_time (struct timevar_time_def
*);
188 static void timevar_accumulate (struct timevar_time_def
*,
189 struct timevar_time_def
*,
190 struct timevar_time_def
*);
192 /* Fill the current times into TIME. The definition of this function
193 also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
194 HAVE_WALL_TIME macros. */
198 struct timevar_time_def
*now
;
210 now
->wall
= times (&tms
) * ticks_to_msec
;
212 now
->user
= tms
.tms_utime
* ticks_to_msec
;
213 now
->sys
= tms
.tms_stime
* ticks_to_msec
;
215 now
->user
= (tms
.tms_utime
+ tms
.tms_cutime
) * ticks_to_msec
;
216 now
->sys
= (tms
.tms_stime
+ tms
.tms_cstime
) * ticks_to_msec
;
220 struct rusage rusage
;
222 getrusage (RUSAGE_SELF
, &rusage
);
224 getrusage (RUSAGE_CHILDREN
, &rusage
);
226 now
->user
= rusage
.ru_utime
.tv_sec
+ rusage
.ru_utime
.tv_usec
* 1e-6;
227 now
->sys
= rusage
.ru_stime
.tv_sec
+ rusage
.ru_stime
.tv_usec
* 1e-6;
230 now
->user
= clock () * clocks_to_msec
;
235 /* Add the difference between STOP and START to TIMER. */
238 timevar_accumulate (timer
, start
, stop
)
239 struct timevar_time_def
*timer
;
240 struct timevar_time_def
*start
;
241 struct timevar_time_def
*stop
;
243 timer
->user
+= stop
->user
- start
->user
;
244 timer
->sys
+= stop
->sys
- start
->sys
;
245 timer
->wall
+= stop
->wall
- start
->wall
;
248 /* Initialize timing variables. */
256 /* Zero all elapsed times. */
257 memset ((void *) timevars
, 0, sizeof (timevars
));
259 /* Initialize the names of timing variables. */
260 #define DEFTIMEVAR(identifier__, name__) \
261 timevars[identifier__].name = name__;
262 #include "timevar.def"
266 ticks_to_msec
= TICKS_TO_MSEC
;
269 clocks_to_msec
= CLOCKS_TO_MSEC
;
273 /* Push TIMEVAR onto the timing stack. No further elapsed time is
274 attributed to the previous topmost timing variable on the stack;
275 subsequent elapsed time is attributed to TIMEVAR, until it is
276 popped or another element is pushed on top.
278 TIMEVAR cannot be running as a standalone timer. */
281 timevar_push (timevar
)
282 timevar_id_t timevar
;
284 struct timevar_def
*tv
= &timevars
[timevar
];
285 struct timevar_stack_def
*context
;
286 struct timevar_time_def now
;
291 /* Mark this timing variable as used. */
294 /* Can't push a standalone timer. */
298 /* What time is it? */
301 /* If the stack isn't empty, attribute the current elapsed time to
302 the old topmost element. */
304 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
306 /* Reset the start time; from now on, time is attributed to
310 /* See if we have a previously-allocated stack instance. If so,
311 take it off the list. If not, malloc a new one. */
312 if (unused_stack_instances
!= NULL
)
314 context
= unused_stack_instances
;
315 unused_stack_instances
= unused_stack_instances
->next
;
318 context
= (struct timevar_stack_def
*)
319 xmalloc (sizeof (struct timevar_stack_def
));
321 /* Fill it in and put it on the stack. */
322 context
->timevar
= tv
;
323 context
->next
= stack
;
327 /* Pop the topmost timing variable element off the timing stack. The
328 popped variable must be TIMEVAR. Elapsed time since the that
329 element was pushed on, or since it was last exposed on top of the
330 stack when the element above it was popped off, is credited to that
334 timevar_pop (timevar
)
335 timevar_id_t timevar
;
337 struct timevar_time_def now
;
338 struct timevar_stack_def
*popped
= stack
;
343 if (&timevars
[timevar
] != stack
->timevar
)
346 /* What time is it? */
349 /* Attribute the elapsed time to the element we're popping. */
350 timevar_accumulate (&popped
->timevar
->elapsed
, &start_time
, &now
);
352 /* Reset the start time; from now on, time is attributed to the
353 element just exposed on the stack. */
356 /* Take the item off the stack. */
359 /* Don't delete the stack element; instead, add it to the list of
360 unused elements for later use. */
361 popped
->next
= unused_stack_instances
;
362 unused_stack_instances
= popped
;
365 /* Start timing TIMEVAR independently of the timing stack. Elapsed
366 time until timevar_stop is called for the same timing variable is
367 attributed to TIMEVAR. */
370 timevar_start (timevar
)
371 timevar_id_t timevar
;
373 struct timevar_def
*tv
= &timevars
[timevar
];
378 /* Mark this timing variable as used. */
381 /* Don't allow the same timing variable to be started more than
387 get_time (&tv
->start_time
);
390 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
391 is attributed to it. */
394 timevar_stop (timevar
)
395 timevar_id_t timevar
;
397 struct timevar_def
*tv
= &timevars
[timevar
];
398 struct timevar_time_def now
;
403 /* TIMEVAR must have been started via timevar_start. */
408 timevar_accumulate (&tv
->elapsed
, &tv
->start_time
, &now
);
411 /* Fill the elapsed time for TIMEVAR into ELAPSED. Returns
412 update-to-date information even if TIMEVAR is currently running. */
415 timevar_get (timevar
, elapsed
)
416 timevar_id_t timevar
;
417 struct timevar_time_def
*elapsed
;
419 struct timevar_def
*tv
= &timevars
[timevar
];
420 struct timevar_time_def now
;
422 *elapsed
= tv
->elapsed
;
424 /* Is TIMEVAR currently running as a standalone timer? */
428 timevar_accumulate (elapsed
, &tv
->start_time
, &now
);
430 /* Or is TIMEVAR at the top of the timer stack? */
431 else if (stack
->timevar
== tv
)
434 timevar_accumulate (elapsed
, &start_time
, &now
);
438 /* Summarize timing variables to FP. The timing variable TV_TOTAL has
439 a special meaning -- it's considered to be the total elapsed time,
440 for normalizing the others, and is displayed last. */
446 /* Only print stuff if we have some sort of time information. */
447 #if defined HAVE_USER_TIME || defined HAVE_SYS_TIME || defined HAVE_WALL_TIME
448 unsigned int /* timevar_id_t */ id
;
449 struct timevar_time_def
*total
= &timevars
[TV_TOTAL
].elapsed
;
450 struct timevar_time_def now
;
455 /* Update timing information in case we're calling this from GDB. */
460 /* What time is it? */
463 /* If the stack isn't empty, attribute the current elapsed time to
464 the old topmost element. */
466 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
468 /* Reset the start time; from now on, time is attributed to
472 fputs (_("\nExecution times (seconds)\n"), fp
);
473 for (id
= 0; id
< (unsigned int) TIMEVAR_LAST
; ++id
)
475 struct timevar_def
*tv
= &timevars
[(timevar_id_t
) id
];
476 const float tiny
= 5e-3;
478 /* Don't print the total execution time here; that goes at the
480 if ((timevar_id_t
) id
== TV_TOTAL
)
483 /* Don't print timing variables that were never used. */
487 /* Don't print timing variables if we're going to get a row of
489 if (tv
->elapsed
.user
< tiny
490 && tv
->elapsed
.sys
< tiny
491 && tv
->elapsed
.wall
< tiny
)
494 /* The timing variable name. */
495 fprintf (fp
, " %-22s:", tv
->name
);
497 #ifdef HAVE_USER_TIME
498 /* Print user-mode time for this process. */
499 fprintf (fp
, "%7.2f (%2.0f%%) usr",
501 (total
->user
== 0 ? 0 : tv
->elapsed
.user
/ total
->user
) * 100);
502 #endif /* HAVE_USER_TIME */
505 /* Print system-mode time for this process. */
506 fprintf (fp
, "%7.2f (%2.0f%%) sys",
508 (total
->sys
== 0 ? 0 : tv
->elapsed
.sys
/ total
->sys
) * 100);
509 #endif /* HAVE_SYS_TIME */
511 #ifdef HAVE_WALL_TIME
512 /* Print wall clock time elapsed. */
513 fprintf (fp
, "%7.2f (%2.0f%%) wall",
515 (total
->wall
== 0 ? 0 : tv
->elapsed
.wall
/ total
->wall
) * 100);
516 #endif /* HAVE_WALL_TIME */
521 /* Print total time. */
522 fputs (_(" TOTAL :"), fp
);
523 #ifdef HAVE_USER_TIME
524 fprintf (fp
, "%7.2f ", total
->user
);
527 fprintf (fp
, "%7.2f ", total
->sys
);
529 #ifdef HAVE_WALL_TIME
530 fprintf (fp
, "%7.2f\n", total
->wall
);
533 #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
534 || defined (HAVE_WALL_TIME) */
537 /* Returns time (user + system) used so far by the compiler process,
543 struct timevar_time_def total_elapsed
;
544 timevar_get (TV_TOTAL
, &total_elapsed
);
545 return total_elapsed
.user
+ total_elapsed
.sys
;
548 /* Prints a message to stderr stating that time elapsed in STR is
549 TOTAL (given in microseconds). */
552 print_time (str
, total
)
556 long all_time
= get_run_time ();
558 _("time in %s: %ld.%06ld (%ld%%)\n"),
559 str
, total
/ 1000000, total
% 1000000,
561 : (long) (((100.0 * (double) total
) / (double) all_time
) + .5));