1 /* Timing variables for measuring compiler performance.
3 Copyright (C) 2000, 2002, 2004, 2006, 2009-2013 Free Software
6 Contributed by Alex Samuel <samuel@codesourcery.com>
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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 (int, struct rusage
*);
68 #if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
69 extern clock_t times (struct tms
*);
71 #if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
72 extern clock_t clock (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
;
190 static void get_time (struct timevar_time_def
*);
191 static void timevar_accumulate (struct timevar_time_def
*,
192 struct timevar_time_def
*,
193 struct timevar_time_def
*);
195 /* Fill the current times into TIME. The definition of this function
196 also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
197 HAVE_WALL_TIME macros. */
201 struct timevar_time_def
*now
;
213 now
->wall
= times (&tms
) * ticks_to_msec
;
215 now
->user
= tms
.tms_utime
* ticks_to_msec
;
216 now
->sys
= tms
.tms_stime
* ticks_to_msec
;
218 now
->user
= (tms
.tms_utime
+ tms
.tms_cutime
) * ticks_to_msec
;
219 now
->sys
= (tms
.tms_stime
+ tms
.tms_cstime
) * ticks_to_msec
;
223 struct rusage rusage
;
225 getrusage (RUSAGE_SELF
, &rusage
);
227 getrusage (RUSAGE_CHILDREN
, &rusage
);
229 now
->user
= rusage
.ru_utime
.tv_sec
+ rusage
.ru_utime
.tv_usec
* 1e-6;
230 now
->sys
= rusage
.ru_stime
.tv_sec
+ rusage
.ru_stime
.tv_usec
* 1e-6;
233 now
->user
= clock () * clocks_to_msec
;
238 /* Add the difference between STOP and START to TIMER. */
241 timevar_accumulate (timer
, start
, stop
)
242 struct timevar_time_def
*timer
;
243 struct timevar_time_def
*start
;
244 struct timevar_time_def
*stop
;
246 timer
->user
+= stop
->user
- start
->user
;
247 timer
->sys
+= stop
->sys
- start
->sys
;
248 timer
->wall
+= stop
->wall
- start
->wall
;
251 /* Initialize timing variables. */
259 /* Zero all elapsed times. */
260 memset ((void *) timevars
, 0, sizeof (timevars
));
262 /* Initialize the names of timing variables. */
263 #define DEFTIMEVAR(identifier__, name__) \
264 timevars[identifier__].name = name__;
265 #include "timevar.def"
269 ticks_to_msec
= TICKS_TO_MSEC
;
272 clocks_to_msec
= CLOCKS_TO_MSEC
;
276 /* Push TIMEVAR onto the timing stack. No further elapsed time is
277 attributed to the previous topmost timing variable on the stack;
278 subsequent elapsed time is attributed to TIMEVAR, until it is
279 popped or another element is pushed on top.
281 TIMEVAR cannot be running as a standalone timer. */
284 timevar_push (timevar
)
285 timevar_id_t timevar
;
287 struct timevar_def
*tv
= &timevars
[timevar
];
288 struct timevar_stack_def
*context
;
289 struct timevar_time_def now
;
294 /* Mark this timing variable as used. */
297 /* Can't push a standalone timer. */
301 /* What time is it? */
304 /* If the stack isn't empty, attribute the current elapsed time to
305 the old topmost element. */
307 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
309 /* Reset the start time; from now on, time is attributed to
313 /* See if we have a previously-allocated stack instance. If so,
314 take it off the list. If not, malloc a new one. */
315 if (unused_stack_instances
!= NULL
)
317 context
= unused_stack_instances
;
318 unused_stack_instances
= unused_stack_instances
->next
;
321 context
= (struct timevar_stack_def
*)
322 xmalloc (sizeof (struct timevar_stack_def
));
324 /* Fill it in and put it on the stack. */
325 context
->timevar
= tv
;
326 context
->next
= stack
;
330 /* Pop the topmost timing variable element off the timing stack. The
331 popped variable must be TIMEVAR. Elapsed time since the that
332 element was pushed on, or since it was last exposed on top of the
333 stack when the element above it was popped off, is credited to that
337 timevar_pop (timevar
)
338 timevar_id_t timevar
;
340 struct timevar_time_def now
;
341 struct timevar_stack_def
*popped
= stack
;
346 if (&timevars
[timevar
] != stack
->timevar
)
349 /* What time is it? */
352 /* Attribute the elapsed time to the element we're popping. */
353 timevar_accumulate (&popped
->timevar
->elapsed
, &start_time
, &now
);
355 /* Reset the start time; from now on, time is attributed to the
356 element just exposed on the stack. */
359 /* Take the item off the stack. */
362 /* Don't delete the stack element; instead, add it to the list of
363 unused elements for later use. */
364 popped
->next
= unused_stack_instances
;
365 unused_stack_instances
= popped
;
368 /* Start timing TIMEVAR independently of the timing stack. Elapsed
369 time until timevar_stop is called for the same timing variable is
370 attributed to TIMEVAR. */
373 timevar_start (timevar
)
374 timevar_id_t timevar
;
376 struct timevar_def
*tv
= &timevars
[timevar
];
381 /* Mark this timing variable as used. */
384 /* Don't allow the same timing variable to be started more than
390 get_time (&tv
->start_time
);
393 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
394 is attributed to it. */
397 timevar_stop (timevar
)
398 timevar_id_t timevar
;
400 struct timevar_def
*tv
= &timevars
[timevar
];
401 struct timevar_time_def now
;
406 /* TIMEVAR must have been started via timevar_start. */
411 timevar_accumulate (&tv
->elapsed
, &tv
->start_time
, &now
);
414 /* Fill the elapsed time for TIMEVAR into ELAPSED. Returns
415 update-to-date information even if TIMEVAR is currently running. */
418 timevar_get (timevar
, elapsed
)
419 timevar_id_t timevar
;
420 struct timevar_time_def
*elapsed
;
422 struct timevar_def
*tv
= &timevars
[timevar
];
423 struct timevar_time_def now
;
425 *elapsed
= tv
->elapsed
;
427 /* Is TIMEVAR currently running as a standalone timer? */
431 timevar_accumulate (elapsed
, &tv
->start_time
, &now
);
433 /* Or is TIMEVAR at the top of the timer stack? */
434 else if (stack
->timevar
== tv
)
437 timevar_accumulate (elapsed
, &start_time
, &now
);
441 /* Summarize timing variables to FP. The timing variable TV_TOTAL has
442 a special meaning -- it's considered to be the total elapsed time,
443 for normalizing the others, and is displayed last. */
449 /* Only print stuff if we have some sort of time information. */
450 #if defined HAVE_USER_TIME || defined HAVE_SYS_TIME || defined HAVE_WALL_TIME
451 unsigned int /* timevar_id_t */ id
;
452 struct timevar_time_def
*total
= &timevars
[TV_TOTAL
].elapsed
;
453 struct timevar_time_def now
;
458 /* Update timing information in case we're calling this from GDB. */
463 /* What time is it? */
466 /* If the stack isn't empty, attribute the current elapsed time to
467 the old topmost element. */
469 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
471 /* Reset the start time; from now on, time is attributed to
475 fputs (_("\nExecution times (seconds)\n"), fp
);
476 for (id
= 0; id
< (unsigned int) TIMEVAR_LAST
; ++id
)
478 struct timevar_def
*tv
= &timevars
[(timevar_id_t
) id
];
479 const float tiny
= 5e-3;
481 /* Don't print the total execution time here; that goes at the
483 if ((timevar_id_t
) id
== TV_TOTAL
)
486 /* Don't print timing variables that were never used. */
490 /* Don't print timing variables if we're going to get a row of
492 if (tv
->elapsed
.user
< tiny
493 && tv
->elapsed
.sys
< tiny
494 && tv
->elapsed
.wall
< tiny
)
497 /* The timing variable name. */
498 fprintf (fp
, " %-22s:", tv
->name
);
500 #ifdef HAVE_USER_TIME
501 /* Print user-mode time for this process. */
502 fprintf (fp
, "%7.2f (%2.0f%%) usr",
504 (total
->user
== 0 ? 0 : tv
->elapsed
.user
/ total
->user
) * 100);
505 #endif /* HAVE_USER_TIME */
508 /* Print system-mode time for this process. */
509 fprintf (fp
, "%7.2f (%2.0f%%) sys",
511 (total
->sys
== 0 ? 0 : tv
->elapsed
.sys
/ total
->sys
) * 100);
512 #endif /* HAVE_SYS_TIME */
514 #ifdef HAVE_WALL_TIME
515 /* Print wall clock time elapsed. */
516 fprintf (fp
, "%7.2f (%2.0f%%) wall",
518 (total
->wall
== 0 ? 0 : tv
->elapsed
.wall
/ total
->wall
) * 100);
519 #endif /* HAVE_WALL_TIME */
524 /* Print total time. */
525 fputs (_(" TOTAL :"), fp
);
526 #ifdef HAVE_USER_TIME
527 fprintf (fp
, "%7.2f ", total
->user
);
530 fprintf (fp
, "%7.2f ", total
->sys
);
532 #ifdef HAVE_WALL_TIME
533 fprintf (fp
, "%7.2f\n", total
->wall
);
536 #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
537 || defined (HAVE_WALL_TIME) */
540 /* Returns time (user + system) used so far by the compiler process,
546 struct timevar_time_def total_elapsed
;
547 timevar_get (TV_TOTAL
, &total_elapsed
);
548 return total_elapsed
.user
+ total_elapsed
.sys
;
551 /* Prints a message to stderr stating that time elapsed in STR is
552 TOTAL (given in microseconds). */
555 print_time (str
, total
)
559 long all_time
= get_run_time ();
561 _("time in %s: %ld.%06ld (%ld%%)\n"),
562 str
, total
/ 1000000, total
% 1000000,
564 : (long) (((100.0 * (double) total
) / (double) all_time
) + .5));