1 /* Timing variables for measuring compiler performance.
2 Copyright (C) 2000 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 #include "coretypes.h"
29 #ifdef HAVE_SYS_TIMES_H
30 # include <sys/times.h>
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
40 #ifndef HAVE_STRUCT_TMS
50 #if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
51 extern int getrusage
PARAMS ((int, struct rusage
*));
53 #if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
54 extern clock_t times
PARAMS ((struct tms
*));
56 #if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
57 extern clock_t clock
PARAMS ((void));
61 # define RUSAGE_SELF 0
64 /* Calculation of scale factor to convert ticks to microseconds.
65 We mustn't use CLOCKS_PER_SEC except with clock(). */
66 #if HAVE_SYSCONF && defined _SC_CLK_TCK
67 # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
70 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
73 # define TICKS_PER_SECOND HZ /* traditional UNIX */
75 # define TICKS_PER_SECOND 100 /* often the correct value */
80 /* Prefer times to getrusage to clock (each gives successively less
84 # define HAVE_USER_TIME
85 # define HAVE_SYS_TIME
86 # define HAVE_WALL_TIME
89 # define USE_GETRUSAGE
90 # define HAVE_USER_TIME
91 # define HAVE_SYS_TIME
95 # define HAVE_USER_TIME
100 /* libc is very likely to have snuck a call to sysconf() into one of
101 the underlying constants, and that can be very slow, so we have to
102 precompute them. Whose wonderful idea was it to make all those
103 _constants_ variable at run time, anyway? */
105 static double ticks_to_msec
;
106 #define TICKS_TO_MSEC (1 / (double)TICKS_PER_SECOND)
110 static double clocks_to_msec
;
111 #define CLOCKS_TO_MSEC (1 / (double)CLOCKS_PER_SEC)
117 /* See timevar.h for an explanation of timing variables. */
119 /* This macro evaluates to nonzero if timing variables are enabled. */
120 #define TIMEVAR_ENABLE (time_report)
122 /* A timing variable. */
126 /* Elapsed time for this variable. */
127 struct timevar_time_def elapsed
;
129 /* If this variable is timed independently of the timing stack,
130 using timevar_start, this contains the start time. */
131 struct timevar_time_def start_time
;
133 /* The name of this timing variable. */
136 /* Nonzero if this timing variable is running as a standalone
138 unsigned standalone
: 1;
140 /* Nonzero if this timing variable was ever started or pushed onto
145 /* An element on the timing stack. Elapsed time is attributed to the
146 topmost timing variable on the stack. */
148 struct timevar_stack_def
150 /* The timing variable at this stack level. */
151 struct timevar_def
*timevar
;
153 /* The next lower timing variable context in the stack. */
154 struct timevar_stack_def
*next
;
157 /* Declared timing variables. Constructed from the contents of
159 static struct timevar_def timevars
[TIMEVAR_LAST
];
161 /* The top of the timing stack. */
162 static struct timevar_stack_def
*stack
;
164 /* A list of unused (i.e. allocated and subsequently popped)
165 timevar_stack_def instances. */
166 static struct timevar_stack_def
*unused_stack_instances
;
168 /* The time at which the topmost element on the timing stack was
169 pushed. Time elapsed since then is attributed to the topmost
171 static struct timevar_time_def start_time
;
174 PARAMS ((struct timevar_time_def
*));
175 static void timevar_accumulate
176 PARAMS ((struct timevar_time_def
*, struct timevar_time_def
*,
177 struct timevar_time_def
*));
179 /* Fill the current times into TIME. The definition of this function
180 also defines any or all of the HAVE_USER_TIME, HAVE_SYS_TIME, and
181 HAVE_WALL_TIME macros. */
185 struct timevar_time_def
*now
;
197 now
->wall
= times (&tms
) * ticks_to_msec
;
198 now
->user
= tms
.tms_utime
* ticks_to_msec
;
199 now
->sys
= tms
.tms_stime
* ticks_to_msec
;
202 struct rusage rusage
;
203 getrusage (RUSAGE_SELF
, &rusage
);
204 now
->user
= rusage
.ru_utime
.tv_sec
+ rusage
.ru_utime
.tv_usec
* 1e-6;
205 now
->sys
= rusage
.ru_stime
.tv_sec
+ rusage
.ru_stime
.tv_usec
* 1e-6;
208 now
->user
= clock () * clocks_to_msec
;
213 /* Add the difference between STOP_TIME and START_TIME to TIMER. */
216 timevar_accumulate (timer
, start_time
, stop_time
)
217 struct timevar_time_def
*timer
;
218 struct timevar_time_def
*start_time
;
219 struct timevar_time_def
*stop_time
;
221 timer
->user
+= stop_time
->user
- start_time
->user
;
222 timer
->sys
+= stop_time
->sys
- start_time
->sys
;
223 timer
->wall
+= stop_time
->wall
- start_time
->wall
;
226 /* Initialize timing variables. */
234 /* Zero all elapsed times. */
235 memset ((void *) timevars
, 0, sizeof (timevars
));
237 /* Initialize the names of timing variables. */
238 #define DEFTIMEVAR(identifier__, name__) \
239 timevars[identifier__].name = name__;
240 #include "timevar.def"
244 ticks_to_msec
= TICKS_TO_MSEC
;
247 clocks_to_msec
= CLOCKS_TO_MSEC
;
251 /* Push TIMEVAR onto the timing stack. No further elapsed time is
252 attributed to the previous topmost timing variable on the stack;
253 subsequent elapsed time is attributed to TIMEVAR, until it is
254 popped or another element is pushed on top.
256 TIMEVAR cannot be running as a standalone timer. */
259 timevar_push (timevar
)
260 timevar_id_t timevar
;
262 struct timevar_def
*tv
= &timevars
[timevar
];
263 struct timevar_stack_def
*context
;
264 struct timevar_time_def now
;
269 /* Mark this timing variable as used. */
272 /* Can't push a standalone timer. */
276 /* What time is it? */
279 /* If the stack isn't empty, attribute the current elapsed time to
280 the old topmost element. */
282 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
284 /* Reset the start time; from now on, time is attributed to
288 /* See if we have a previously-allocated stack instance. If so,
289 take it off the list. If not, malloc a new one. */
290 if (unused_stack_instances
!= NULL
)
292 context
= unused_stack_instances
;
293 unused_stack_instances
= unused_stack_instances
->next
;
296 context
= (struct timevar_stack_def
*)
297 xmalloc (sizeof (struct timevar_stack_def
));
299 /* Fill it in and put it on the stack. */
300 context
->timevar
= tv
;
301 context
->next
= stack
;
305 /* Pop the topmost timing variable element off the timing stack. The
306 popped variable must be TIMEVAR. Elapsed time since the that
307 element was pushed on, or since it was last exposed on top of the
308 stack when the element above it was popped off, is credited to that
312 timevar_pop (timevar
)
313 timevar_id_t timevar
;
315 struct timevar_time_def now
;
316 struct timevar_stack_def
*popped
= stack
;
321 if (&timevars
[timevar
] != stack
->timevar
)
324 /* What time is it? */
327 /* Attribute the elapsed time to the element we're popping. */
328 timevar_accumulate (&popped
->timevar
->elapsed
, &start_time
, &now
);
330 /* Reset the start time; from now on, time is attributed to the
331 element just exposed on the stack. */
334 /* Take the item off the stack. */
337 /* Don't delete the stack element; instead, add it to the list of
338 unused elements for later use. */
339 popped
->next
= unused_stack_instances
;
340 unused_stack_instances
= popped
;
343 /* Start timing TIMEVAR independently of the timing stack. Elapsed
344 time until timevar_stop is called for the same timing variable is
345 attributed to TIMEVAR. */
348 timevar_start (timevar
)
349 timevar_id_t timevar
;
351 struct timevar_def
*tv
= &timevars
[timevar
];
356 /* Mark this timing variable as used. */
359 /* Don't allow the same timing variable to be started more than
365 get_time (&tv
->start_time
);
368 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
369 is attributed to it. */
372 timevar_stop (timevar
)
373 timevar_id_t timevar
;
375 struct timevar_def
*tv
= &timevars
[timevar
];
376 struct timevar_time_def now
;
381 /* TIMEVAR must have been started via timevar_start. */
386 timevar_accumulate (&tv
->elapsed
, &tv
->start_time
, &now
);
389 /* Fill the elapsed time for TIMEVAR into ELAPSED. Returns
390 update-to-date information even if TIMEVAR is currently running. */
393 timevar_get (timevar
, elapsed
)
394 timevar_id_t timevar
;
395 struct timevar_time_def
*elapsed
;
397 struct timevar_def
*tv
= &timevars
[timevar
];
398 struct timevar_time_def now
;
400 *elapsed
= tv
->elapsed
;
402 /* Is TIMEVAR currently running as a standalone timer? */
406 timevar_accumulate (elapsed
, &tv
->start_time
, &now
);
408 /* Or is TIMEVAR at the top of the timer stack? */
409 else if (stack
->timevar
== tv
)
412 timevar_accumulate (elapsed
, &start_time
, &now
);
416 /* Summarize timing variables to FP. The timing variable TV_TOTAL has
417 a special meaning -- it's considered to be the total elapsed time,
418 for normalizing the others, and is displayed last. */
424 /* Only print stuff if we have some sort of time information. */
425 #if defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME) || defined (HAVE_WALL_TIME)
426 unsigned int /* timevar_id_t */ id
;
427 struct timevar_time_def
*total
= &timevars
[TV_TOTAL
].elapsed
;
428 struct timevar_time_def now
;
433 /* Update timing information in case we're calling this from GDB. */
438 /* What time is it? */
441 /* If the stack isn't empty, attribute the current elapsed time to
442 the old topmost element. */
444 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
446 /* Reset the start time; from now on, time is attributed to
450 fputs (_("\nExecution times (seconds)\n"), fp
);
451 for (id
= 0; id
< (unsigned int) TIMEVAR_LAST
; ++id
)
453 struct timevar_def
*tv
= &timevars
[(timevar_id_t
) id
];
454 const double tiny
= 5e-3;
456 /* Don't print the total execution time here; that goes at the
458 if ((timevar_id_t
) id
== TV_TOTAL
)
461 /* Don't print timing variables that were never used. */
465 /* Don't print timing variables if we're going to get a row of
467 if (tv
->elapsed
.user
< tiny
468 && tv
->elapsed
.sys
< tiny
469 && tv
->elapsed
.wall
< tiny
)
472 /* The timing variable name. */
473 fprintf (fp
, " %-22s:", tv
->name
);
475 #ifdef HAVE_USER_TIME
476 /* Print user-mode time for this process. */
477 fprintf (fp
, "%7.2f (%2.0f%%) usr",
479 (total
->user
== 0 ? 0 : tv
->elapsed
.user
/ total
->user
) * 100);
480 #endif /* HAVE_USER_TIME */
483 /* Print system-mode time for this process. */
484 fprintf (fp
, "%7.2f (%2.0f%%) sys",
486 (total
->sys
== 0 ? 0 : tv
->elapsed
.sys
/ total
->sys
) * 100);
487 #endif /* HAVE_SYS_TIME */
489 #ifdef HAVE_WALL_TIME
490 /* Print wall clock time elapsed. */
491 fprintf (fp
, "%7.2f (%2.0f%%) wall",
493 (total
->wall
== 0 ? 0 : tv
->elapsed
.wall
/ total
->wall
) * 100);
494 #endif /* HAVE_WALL_TIME */
499 /* Print total time. */
500 fputs (_(" TOTAL :"), fp
);
501 #ifdef HAVE_USER_TIME
502 fprintf (fp
, "%7.2f ", total
->user
);
505 fprintf (fp
, "%7.2f ", total
->sys
);
507 #ifdef HAVE_WALL_TIME
508 fprintf (fp
, "%7.2f\n", total
->wall
);
511 #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
512 || defined (HAVE_WALL_TIME) */
515 /* Returns time (user + system) used so far by the compiler process,
521 struct timevar_time_def total_elapsed
;
522 timevar_get (TV_TOTAL
, &total_elapsed
);
523 return total_elapsed
.user
+ total_elapsed
.sys
;
526 /* Prints a message to stderr stating that time elapsed in STR is
527 TOTAL (given in microseconds). */
530 print_time (str
, total
)
534 long all_time
= get_run_time ();
536 _("time in %s: %ld.%06ld (%ld%%)\n"),
537 str
, total
/ 1000000, total
% 1000000,
539 : (long) (((100.0 * (double) total
) / (double) all_time
) + .5));