1 /* Timing variables for measuring compiler performance.
2 Copyright (C) 2000, 2002, 2004, 2005, 2006, 2009 Free Software
4 Contributed by Alex Samuel <samuel@codesourcery.com>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 /* This source file is taken from the GCC source code, with slight
30 modifications that are under control of the IN_GCC preprocessor
31 variable. The !IN_GCC part of this file is specific to Bison. */
33 # include "../src/system.h"
35 # include <sys/time.h>
37 int timevar_report
= 0;
42 #ifdef HAVE_SYS_TIMES_H
43 # include <sys/times.h>
45 #ifdef HAVE_SYS_RESOURCE_H
46 #include <sys/resource.h>
53 #ifndef HAVE_STRUCT_TMS
63 #if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
64 extern int getrusage (int, struct rusage
*);
66 #if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
67 extern clock_t times (struct tms
*);
69 #if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
70 extern clock_t clock (void);
74 # define RUSAGE_SELF 0
77 /* Calculation of scale factor to convert ticks to microseconds.
78 We mustn't use CLOCKS_PER_SEC except with clock(). */
79 #if HAVE_SYSCONF && defined _SC_CLK_TCK
80 # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
83 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
86 # define TICKS_PER_SECOND HZ /* traditional UNIX */
88 # define TICKS_PER_SECOND 100 /* often the correct value */
93 /* Prefer times to getrusage to clock (each gives successively less
97 # define HAVE_USER_TIME
98 # define HAVE_SYS_TIME
99 # define HAVE_WALL_TIME
101 #ifdef HAVE_GETRUSAGE
102 # define USE_GETRUSAGE
103 # define HAVE_USER_TIME
104 # define HAVE_SYS_TIME
108 # define HAVE_USER_TIME
113 /* libc is very likely to have snuck a call to sysconf() into one of
114 the underlying constants, and that can be very slow, so we have to
115 precompute them. Whose wonderful idea was it to make all those
116 _constants_ variable at run time, anyway? */
118 static float ticks_to_msec
;
119 #define TICKS_TO_MSEC (1.0 / TICKS_PER_SECOND)
123 static float clocks_to_msec
;
124 #define CLOCKS_TO_MSEC (1.0 / CLOCKS_PER_SEC)
132 /* See timevar.h for an explanation of timing variables. */
134 /* This macro evaluates to nonzero if timing variables are enabled. */
135 #define TIMEVAR_ENABLE (timevar_report)
137 /* A timing variable. */
141 /* Elapsed time for this variable. */
142 struct timevar_time_def elapsed
;
144 /* If this variable is timed independently of the timing stack,
145 using timevar_start, this contains the start time. */
146 struct timevar_time_def start_time
;
148 /* The name of this timing variable. */
151 /* Non-zero if this timing variable is running as a standalone
153 unsigned standalone
: 1;
155 /* Non-zero if this timing variable was ever started or pushed onto
160 /* An element on the timing stack. Elapsed time is attributed to the
161 topmost timing variable on the stack. */
163 struct timevar_stack_def
165 /* The timing variable at this stack level. */
166 struct timevar_def
*timevar
;
168 /* The next lower timing variable context in the stack. */
169 struct timevar_stack_def
*next
;
172 /* Declared timing variables. Constructed from the contents of
174 static struct timevar_def timevars
[TIMEVAR_LAST
];
176 /* The top of the timing stack. */
177 static struct timevar_stack_def
*stack
;
179 /* A list of unused (i.e. allocated and subsequently popped)
180 timevar_stack_def instances. */
181 static struct timevar_stack_def
*unused_stack_instances
;
183 /* The time at which the topmost element on the timing stack was
184 pushed. Time elapsed since then is attributed to the topmost
186 static struct timevar_time_def start_time
;
188 static void get_time (struct timevar_time_def
*);
189 static void timevar_accumulate (struct timevar_time_def
*,
190 struct timevar_time_def
*,
191 struct timevar_time_def
*);
193 /* Fill the current times into TIME. The definition of this function
194 also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
195 HAVE_WALL_TIME macros. */
199 struct timevar_time_def
*now
;
211 now
->wall
= times (&tms
) * ticks_to_msec
;
213 now
->user
= tms
.tms_utime
* ticks_to_msec
;
214 now
->sys
= tms
.tms_stime
* ticks_to_msec
;
216 now
->user
= (tms
.tms_utime
+ tms
.tms_cutime
) * ticks_to_msec
;
217 now
->sys
= (tms
.tms_stime
+ tms
.tms_cstime
) * ticks_to_msec
;
221 struct rusage rusage
;
223 getrusage (RUSAGE_SELF
, &rusage
);
225 getrusage (RUSAGE_CHILDREN
, &rusage
);
227 now
->user
= rusage
.ru_utime
.tv_sec
+ rusage
.ru_utime
.tv_usec
* 1e-6;
228 now
->sys
= rusage
.ru_stime
.tv_sec
+ rusage
.ru_stime
.tv_usec
* 1e-6;
231 now
->user
= clock () * clocks_to_msec
;
236 /* Add the difference between STOP and START to TIMER. */
239 timevar_accumulate (timer
, start
, stop
)
240 struct timevar_time_def
*timer
;
241 struct timevar_time_def
*start
;
242 struct timevar_time_def
*stop
;
244 timer
->user
+= stop
->user
- start
->user
;
245 timer
->sys
+= stop
->sys
- start
->sys
;
246 timer
->wall
+= stop
->wall
- start
->wall
;
249 /* Initialize timing variables. */
257 /* Zero all elapsed times. */
258 memset ((void *) timevars
, 0, sizeof (timevars
));
260 /* Initialize the names of timing variables. */
261 #define DEFTIMEVAR(identifier__, name__) \
262 timevars[identifier__].name = name__;
263 #include "timevar.def"
267 ticks_to_msec
= TICKS_TO_MSEC
;
270 clocks_to_msec
= CLOCKS_TO_MSEC
;
274 /* Push TIMEVAR onto the timing stack. No further elapsed time is
275 attributed to the previous topmost timing variable on the stack;
276 subsequent elapsed time is attributed to TIMEVAR, until it is
277 popped or another element is pushed on top.
279 TIMEVAR cannot be running as a standalone timer. */
282 timevar_push (timevar
)
283 timevar_id_t timevar
;
285 struct timevar_def
*tv
= &timevars
[timevar
];
286 struct timevar_stack_def
*context
;
287 struct timevar_time_def now
;
292 /* Mark this timing variable as used. */
295 /* Can't push a standalone timer. */
299 /* What time is it? */
302 /* If the stack isn't empty, attribute the current elapsed time to
303 the old topmost element. */
305 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
307 /* Reset the start time; from now on, time is attributed to
311 /* See if we have a previously-allocated stack instance. If so,
312 take it off the list. If not, malloc a new one. */
313 if (unused_stack_instances
!= NULL
)
315 context
= unused_stack_instances
;
316 unused_stack_instances
= unused_stack_instances
->next
;
319 context
= (struct timevar_stack_def
*)
320 xmalloc (sizeof (struct timevar_stack_def
));
322 /* Fill it in and put it on the stack. */
323 context
->timevar
= tv
;
324 context
->next
= stack
;
328 /* Pop the topmost timing variable element off the timing stack. The
329 popped variable must be TIMEVAR. Elapsed time since the that
330 element was pushed on, or since it was last exposed on top of the
331 stack when the element above it was popped off, is credited to that
335 timevar_pop (timevar
)
336 timevar_id_t timevar
;
338 struct timevar_time_def now
;
339 struct timevar_stack_def
*popped
= stack
;
344 if (&timevars
[timevar
] != stack
->timevar
)
347 /* What time is it? */
350 /* Attribute the elapsed time to the element we're popping. */
351 timevar_accumulate (&popped
->timevar
->elapsed
, &start_time
, &now
);
353 /* Reset the start time; from now on, time is attributed to the
354 element just exposed on the stack. */
357 /* Take the item off the stack. */
360 /* Don't delete the stack element; instead, add it to the list of
361 unused elements for later use. */
362 popped
->next
= unused_stack_instances
;
363 unused_stack_instances
= popped
;
366 /* Start timing TIMEVAR independently of the timing stack. Elapsed
367 time until timevar_stop is called for the same timing variable is
368 attributed to TIMEVAR. */
371 timevar_start (timevar
)
372 timevar_id_t timevar
;
374 struct timevar_def
*tv
= &timevars
[timevar
];
379 /* Mark this timing variable as used. */
382 /* Don't allow the same timing variable to be started more than
388 get_time (&tv
->start_time
);
391 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
392 is attributed to it. */
395 timevar_stop (timevar
)
396 timevar_id_t timevar
;
398 struct timevar_def
*tv
= &timevars
[timevar
];
399 struct timevar_time_def now
;
404 /* TIMEVAR must have been started via timevar_start. */
409 timevar_accumulate (&tv
->elapsed
, &tv
->start_time
, &now
);
412 /* Fill the elapsed time for TIMEVAR into ELAPSED. Returns
413 update-to-date information even if TIMEVAR is currently running. */
416 timevar_get (timevar
, elapsed
)
417 timevar_id_t timevar
;
418 struct timevar_time_def
*elapsed
;
420 struct timevar_def
*tv
= &timevars
[timevar
];
421 struct timevar_time_def now
;
423 *elapsed
= tv
->elapsed
;
425 /* Is TIMEVAR currently running as a standalone timer? */
429 timevar_accumulate (elapsed
, &tv
->start_time
, &now
);
431 /* Or is TIMEVAR at the top of the timer stack? */
432 else if (stack
->timevar
== tv
)
435 timevar_accumulate (elapsed
, &start_time
, &now
);
439 /* Summarize timing variables to FP. The timing variable TV_TOTAL has
440 a special meaning -- it's considered to be the total elapsed time,
441 for normalizing the others, and is displayed last. */
447 /* Only print stuff if we have some sort of time information. */
448 #if defined HAVE_USER_TIME || defined HAVE_SYS_TIME || defined HAVE_WALL_TIME
449 unsigned int /* timevar_id_t */ id
;
450 struct timevar_time_def
*total
= &timevars
[TV_TOTAL
].elapsed
;
451 struct timevar_time_def now
;
456 /* Update timing information in case we're calling this from GDB. */
461 /* What time is it? */
464 /* If the stack isn't empty, attribute the current elapsed time to
465 the old topmost element. */
467 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
469 /* Reset the start time; from now on, time is attributed to
473 fputs (_("\nExecution times (seconds)\n"), fp
);
474 for (id
= 0; id
< (unsigned int) TIMEVAR_LAST
; ++id
)
476 struct timevar_def
*tv
= &timevars
[(timevar_id_t
) id
];
477 const float tiny
= 5e-3;
479 /* Don't print the total execution time here; that goes at the
481 if ((timevar_id_t
) id
== TV_TOTAL
)
484 /* Don't print timing variables that were never used. */
488 /* Don't print timing variables if we're going to get a row of
490 if (tv
->elapsed
.user
< tiny
491 && tv
->elapsed
.sys
< tiny
492 && tv
->elapsed
.wall
< tiny
)
495 /* The timing variable name. */
496 fprintf (fp
, " %-22s:", tv
->name
);
498 #ifdef HAVE_USER_TIME
499 /* Print user-mode time for this process. */
500 fprintf (fp
, "%7.2f (%2.0f%%) usr",
502 (total
->user
== 0 ? 0 : tv
->elapsed
.user
/ total
->user
) * 100);
503 #endif /* HAVE_USER_TIME */
506 /* Print system-mode time for this process. */
507 fprintf (fp
, "%7.2f (%2.0f%%) sys",
509 (total
->sys
== 0 ? 0 : tv
->elapsed
.sys
/ total
->sys
) * 100);
510 #endif /* HAVE_SYS_TIME */
512 #ifdef HAVE_WALL_TIME
513 /* Print wall clock time elapsed. */
514 fprintf (fp
, "%7.2f (%2.0f%%) wall",
516 (total
->wall
== 0 ? 0 : tv
->elapsed
.wall
/ total
->wall
) * 100);
517 #endif /* HAVE_WALL_TIME */
522 /* Print total time. */
523 fputs (_(" TOTAL :"), fp
);
524 #ifdef HAVE_USER_TIME
525 fprintf (fp
, "%7.2f ", total
->user
);
528 fprintf (fp
, "%7.2f ", total
->sys
);
530 #ifdef HAVE_WALL_TIME
531 fprintf (fp
, "%7.2f\n", total
->wall
);
534 #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
535 || defined (HAVE_WALL_TIME) */
538 /* Returns time (user + system) used so far by the compiler process,
544 struct timevar_time_def total_elapsed
;
545 timevar_get (TV_TOTAL
, &total_elapsed
);
546 return total_elapsed
.user
+ total_elapsed
.sys
;
549 /* Prints a message to stderr stating that time elapsed in STR is
550 TOTAL (given in microseconds). */
553 print_time (str
, total
)
557 long all_time
= get_run_time ();
559 _("time in %s: %ld.%06ld (%ld%%)\n"),
560 str
, total
/ 1000000, total
% 1000000,
562 : (long) (((100.0 * (double) total
) / (double) all_time
) + .5));