1 /* Timing variables for measuring compiler performance.
2 Copyright (C) 2000, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Alex Samuel <samuel@codesourcery.com>
5 This file is part of GCC.
7 GCC 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 GCC 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 GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #ifdef HAVE_SYS_TIMES_H
25 # include <sys/times.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
30 #include "coretypes.h"
40 #ifndef HAVE_STRUCT_TMS
51 # define RUSAGE_SELF 0
54 /* Calculation of scale factor to convert ticks to microseconds.
55 We mustn't use CLOCKS_PER_SEC except with clock(). */
56 #if HAVE_SYSCONF && defined _SC_CLK_TCK
57 # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
60 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
63 # define TICKS_PER_SECOND HZ /* traditional UNIX */
65 # define TICKS_PER_SECOND 100 /* often the correct value */
70 /* Prefer times to getrusage to clock (each gives successively less
73 # if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
74 extern clock_t times (struct tms
*);
77 # define HAVE_USER_TIME
78 # define HAVE_SYS_TIME
79 # define HAVE_WALL_TIME
82 # if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
83 extern int getrusage (int, struct rusage
*);
85 # define USE_GETRUSAGE
86 # define HAVE_USER_TIME
87 # define HAVE_SYS_TIME
90 # if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
91 extern clock_t clock (void);
94 # define HAVE_USER_TIME
99 /* libc is very likely to have snuck a call to sysconf() into one of
100 the underlying constants, and that can be very slow, so we have to
101 precompute them. Whose wonderful idea was it to make all those
102 _constants_ variable at run time, anyway? */
104 static double ticks_to_msec
;
105 #define TICKS_TO_MSEC (1 / (double)TICKS_PER_SECOND)
109 static double clocks_to_msec
;
110 #define CLOCKS_TO_MSEC (1 / (double)CLOCKS_PER_SEC)
118 /* See timevar.h for an explanation of timing variables. */
120 /* A timing variable. */
124 /* Elapsed time for this variable. */
125 struct timevar_time_def elapsed
;
127 /* If this variable is timed independently of the timing stack,
128 using timevar_start, this contains the start time. */
129 struct timevar_time_def start_time
;
131 /* The name of this timing variable. */
134 /* Nonzero if this timing variable is running as a standalone
136 unsigned standalone
: 1;
138 /* Nonzero if this timing variable was ever started or pushed onto
143 /* An element on the timing stack. Elapsed time is attributed to the
144 topmost timing variable on the stack. */
146 struct timevar_stack_def
148 /* The timing variable at this stack level. */
149 struct timevar_def
*timevar
;
151 /* The next lower timing variable context in the stack. */
152 struct timevar_stack_def
*next
;
155 /* Declared timing variables. Constructed from the contents of
157 static struct timevar_def timevars
[TIMEVAR_LAST
];
159 /* The top of the timing stack. */
160 static struct timevar_stack_def
*stack
;
162 /* A list of unused (i.e. allocated and subsequently popped)
163 timevar_stack_def instances. */
164 static struct timevar_stack_def
*unused_stack_instances
;
166 /* The time at which the topmost element on the timing stack was
167 pushed. Time elapsed since then is attributed to the topmost
169 static struct timevar_time_def start_time
;
171 static void get_time (struct timevar_time_def
*);
172 static void timevar_accumulate (struct timevar_time_def
*,
173 struct timevar_time_def
*,
174 struct timevar_time_def
*);
176 /* Fill the current times into TIME. The definition of this function
177 also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
178 HAVE_WALL_TIME macros. */
181 get_time (struct timevar_time_def
*now
)
193 now
->wall
= times (&tms
) * ticks_to_msec
;
194 now
->user
= tms
.tms_utime
* ticks_to_msec
;
195 now
->sys
= tms
.tms_stime
* ticks_to_msec
;
198 struct rusage rusage
;
199 getrusage (RUSAGE_SELF
, &rusage
);
200 now
->user
= rusage
.ru_utime
.tv_sec
+ rusage
.ru_utime
.tv_usec
* 1e-6;
201 now
->sys
= rusage
.ru_stime
.tv_sec
+ rusage
.ru_stime
.tv_usec
* 1e-6;
204 now
->user
= clock () * clocks_to_msec
;
209 /* Add the difference between STOP_TIME and START_TIME to TIMER. */
212 timevar_accumulate (struct timevar_time_def
*timer
,
213 struct timevar_time_def
*start_time
,
214 struct timevar_time_def
*stop_time
)
216 timer
->user
+= stop_time
->user
- start_time
->user
;
217 timer
->sys
+= stop_time
->sys
- start_time
->sys
;
218 timer
->wall
+= stop_time
->wall
- start_time
->wall
;
221 /* Initialize timing variables. */
226 timevar_enable
= true;
228 /* Zero all elapsed times. */
229 memset (timevars
, 0, sizeof (timevars
));
231 /* Initialize the names of timing variables. */
232 #define DEFTIMEVAR(identifier__, name__) \
233 timevars[identifier__].name = name__;
234 #include "timevar.def"
238 ticks_to_msec
= TICKS_TO_MSEC
;
241 clocks_to_msec
= CLOCKS_TO_MSEC
;
245 /* Push TIMEVAR onto the timing stack. No further elapsed time is
246 attributed to the previous topmost timing variable on the stack;
247 subsequent elapsed time is attributed to TIMEVAR, until it is
248 popped or another element is pushed on top.
250 TIMEVAR cannot be running as a standalone timer. */
253 timevar_push_1 (timevar_id_t timevar
)
255 struct timevar_def
*tv
= &timevars
[timevar
];
256 struct timevar_stack_def
*context
;
257 struct timevar_time_def now
;
259 /* Mark this timing variable as used. */
262 /* Can't push a standalone timer. */
263 gcc_assert (!tv
->standalone
);
265 /* What time is it? */
268 /* If the stack isn't empty, attribute the current elapsed time to
269 the old topmost element. */
271 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
273 /* Reset the start time; from now on, time is attributed to
277 /* See if we have a previously-allocated stack instance. If so,
278 take it off the list. If not, malloc a new one. */
279 if (unused_stack_instances
!= NULL
)
281 context
= unused_stack_instances
;
282 unused_stack_instances
= unused_stack_instances
->next
;
285 context
= xmalloc (sizeof (struct timevar_stack_def
));
287 /* Fill it in and put it on the stack. */
288 context
->timevar
= tv
;
289 context
->next
= stack
;
293 /* Pop the topmost timing variable element off the timing stack. The
294 popped variable must be TIMEVAR. Elapsed time since the that
295 element was pushed on, or since it was last exposed on top of the
296 stack when the element above it was popped off, is credited to that
300 timevar_pop_1 (timevar_id_t timevar
)
302 struct timevar_time_def now
;
303 struct timevar_stack_def
*popped
= stack
;
305 gcc_assert (&timevars
[timevar
] == stack
->timevar
);
307 /* What time is it? */
310 /* Attribute the elapsed time to the element we're popping. */
311 timevar_accumulate (&popped
->timevar
->elapsed
, &start_time
, &now
);
313 /* Reset the start time; from now on, time is attributed to the
314 element just exposed on the stack. */
317 /* Take the item off the stack. */
320 /* Don't delete the stack element; instead, add it to the list of
321 unused elements for later use. */
322 popped
->next
= unused_stack_instances
;
323 unused_stack_instances
= popped
;
326 /* Start timing TIMEVAR independently of the timing stack. Elapsed
327 time until timevar_stop is called for the same timing variable is
328 attributed to TIMEVAR. */
331 timevar_start (timevar_id_t timevar
)
333 struct timevar_def
*tv
= &timevars
[timevar
];
338 /* Mark this timing variable as used. */
341 /* Don't allow the same timing variable to be started more than
343 gcc_assert (!tv
->standalone
);
346 get_time (&tv
->start_time
);
349 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
350 is attributed to it. */
353 timevar_stop (timevar_id_t timevar
)
355 struct timevar_def
*tv
= &timevars
[timevar
];
356 struct timevar_time_def now
;
361 /* TIMEVAR must have been started via timevar_start. */
362 gcc_assert (tv
->standalone
);
365 timevar_accumulate (&tv
->elapsed
, &tv
->start_time
, &now
);
368 /* Summarize timing variables to FP. The timing variable TV_TOTAL has
369 a special meaning -- it's considered to be the total elapsed time,
370 for normalizing the others, and is displayed last. */
373 timevar_print (FILE *fp
)
375 /* Only print stuff if we have some sort of time information. */
376 #if defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME) || defined (HAVE_WALL_TIME)
377 unsigned int /* timevar_id_t */ id
;
378 struct timevar_time_def
*total
= &timevars
[TV_TOTAL
].elapsed
;
379 struct timevar_time_def now
;
384 /* Update timing information in case we're calling this from GDB. */
389 /* What time is it? */
392 /* If the stack isn't empty, attribute the current elapsed time to
393 the old topmost element. */
395 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
397 /* Reset the start time; from now on, time is attributed to
401 fputs (_("\nExecution times (seconds)\n"), fp
);
402 for (id
= 0; id
< (unsigned int) TIMEVAR_LAST
; ++id
)
404 struct timevar_def
*tv
= &timevars
[(timevar_id_t
) id
];
405 const double tiny
= 5e-3;
407 /* Don't print the total execution time here; that goes at the
409 if ((timevar_id_t
) id
== TV_TOTAL
)
412 /* Don't print timing variables that were never used. */
416 /* Don't print timing variables if we're going to get a row of
418 if (tv
->elapsed
.user
< tiny
419 && tv
->elapsed
.sys
< tiny
420 && tv
->elapsed
.wall
< tiny
)
423 /* The timing variable name. */
424 fprintf (fp
, " %-22s:", tv
->name
);
426 #ifdef HAVE_USER_TIME
427 /* Print user-mode time for this process. */
428 fprintf (fp
, "%7.2f (%2.0f%%) usr",
430 (total
->user
== 0 ? 0 : tv
->elapsed
.user
/ total
->user
) * 100);
431 #endif /* HAVE_USER_TIME */
434 /* Print system-mode time for this process. */
435 fprintf (fp
, "%7.2f (%2.0f%%) sys",
437 (total
->sys
== 0 ? 0 : tv
->elapsed
.sys
/ total
->sys
) * 100);
438 #endif /* HAVE_SYS_TIME */
440 #ifdef HAVE_WALL_TIME
441 /* Print wall clock time elapsed. */
442 fprintf (fp
, "%7.2f (%2.0f%%) wall",
444 (total
->wall
== 0 ? 0 : tv
->elapsed
.wall
/ total
->wall
) * 100);
445 #endif /* HAVE_WALL_TIME */
450 /* Print total time. */
451 fputs (_(" TOTAL :"), fp
);
452 #ifdef HAVE_USER_TIME
453 fprintf (fp
, "%7.2f ", total
->user
);
456 fprintf (fp
, "%7.2f ", total
->sys
);
458 #ifdef HAVE_WALL_TIME
459 fprintf (fp
, "%7.2f\n", total
->wall
);
462 #ifdef ENABLE_CHECKING
463 fprintf (fp
, "Extra diagnostic checks enabled; compiler may run slowly.\n");
464 fprintf (fp
, "Configure with --disable-checking to disable checks.\n");
467 #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
468 || defined (HAVE_WALL_TIME) */
471 /* Prints a message to stderr stating that time elapsed in STR is
472 TOTAL (given in microseconds). */
475 print_time (const char *str
, long total
)
477 long all_time
= get_run_time ();
479 _("time in %s: %ld.%06ld (%ld%%)\n"),
480 str
, total
/ 1000000, total
% 1000000,
482 : (long) (((100.0 * (double) total
) / (double) all_time
) + .5));