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 GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #ifdef HAVE_SYS_TIMES_H
27 # include <sys/times.h>
29 #ifdef HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
37 #ifndef HAVE_STRUCT_TMS
47 #if defined HAVE_DECL_GETRUSAGE && !HAVE_DECL_GETRUSAGE
48 extern int getrusage
PARAMS ((int, struct rusage
*));
50 #if defined HAVE_DECL_TIMES && !HAVE_DECL_TIMES
51 extern clock_t times
PARAMS ((struct tms
*));
53 #if defined HAVE_DECL_CLOCK && !HAVE_DECL_CLOCK
54 extern clock_t clock
PARAMS ((void));
58 # define RUSAGE_SELF 0
61 /* Calculation of scale factor to convert ticks to microseconds.
62 We mustn't use CLOCKS_PER_SEC except with clock(). */
63 #if HAVE_SYSCONF && defined _SC_CLK_TCK
64 # define TICKS_PER_SECOND sysconf (_SC_CLK_TCK) /* POSIX 1003.1-1996 */
67 # define TICKS_PER_SECOND CLK_TCK /* POSIX 1003.1-1988; obsolescent */
70 # define TICKS_PER_SECOND HZ /* traditional UNIX */
72 # define TICKS_PER_SECOND 100 /* often the correct value */
77 /* Prefer times to getrusage to clock (each gives successively less
81 # define HAVE_USER_TIME
82 # define HAVE_SYS_TIME
83 # define HAVE_WALL_TIME
86 # define USE_GETRUSAGE
87 # define HAVE_USER_TIME
88 # define HAVE_SYS_TIME
92 # define HAVE_USER_TIME
97 /* libc is very likely to have snuck a call to sysconf() into one of
98 the underlying constants, and that can be very slow, so we have to
99 precompute them. Whose wonderful idea was it to make all those
100 _constants_ variable at run time, anyway? */
102 static int ticks_to_msec
;
103 #define TICKS_TO_MSEC (1000 / TICKS_PER_SECOND)
107 static int clocks_to_msec
;
108 #define CLOCKS_TO_MSEC (1000 / CLOCKS_PER_SEC)
114 /* See timevar.h for an explanation of timing variables. */
116 /* This macro evaluates to non-zero if timing variables are enabled. */
117 #define TIMEVAR_ENABLE (time_report)
119 /* A timing variable. */
123 /* Elapsed time for this variable. */
124 struct timevar_time_def elapsed
;
126 /* If this variable is timed independently of the timing stack,
127 using timevar_start, this contains the start time. */
128 struct timevar_time_def start_time
;
130 /* The name of this timing variable. */
133 /* Non-zero if this timing variable is running as a standalone
135 unsigned standalone
: 1;
137 /* Non-zero if this timing variable was ever started or pushed onto
142 /* An element on the timing stack. Elapsed time is attributed to the
143 topmost timing variable on the stack. */
145 struct timevar_stack_def
147 /* The timing variable at this stack level. */
148 struct timevar_def
*timevar
;
150 /* The next lower timing variable context in the stack. */
151 struct timevar_stack_def
*next
;
154 /* Declared timing variables. Constructed from the contents of
156 static struct timevar_def timevars
[TIMEVAR_LAST
];
158 /* The top of the timing stack. */
159 static struct timevar_stack_def
*stack
;
161 /* A list of unused (i.e. allocated and subsequently popped)
162 timevar_stack_def instances. */
163 static struct timevar_stack_def
*unused_stack_instances
;
165 /* The time at which the topmost element on the timing stack was
166 pushed. Time elapsed since then is attributed to the topmost
168 static struct timevar_time_def start_time
;
171 PARAMS ((struct timevar_time_def
*));
172 static void timevar_accumulate
173 PARAMS ((struct timevar_time_def
*, 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 HAVA_WALL_TIME macros. */
182 struct timevar_time_def
*now
;
194 now
->wall
= times (&tms
) * ticks_to_msec
;
195 now
->user
= tms
.tms_utime
* ticks_to_msec
;
196 now
->sys
= tms
.tms_stime
* ticks_to_msec
;
199 struct rusage rusage
;
200 getrusage (RUSAGE_SELF
, &rusage
);
201 now
->user
= rusage
.ru_utime
.tv_sec
* 1000 + rusage
.ru_utime
.tv_usec
/ 1000;
202 now
->sys
= rusage
.ru_stime
.tv_sec
* 1000 + rusage
.ru_stime
.tv_usec
/ 1000;
205 now
->user
= clock () * clocks_to_msec
;
210 /* Add the difference between STOP_TIME and START_TIME to TIMER. */
213 timevar_accumulate (timer
, start_time
, stop_time
)
214 struct timevar_time_def
*timer
;
215 struct timevar_time_def
*start_time
;
216 struct timevar_time_def
*stop_time
;
218 timer
->user
+= stop_time
->user
- start_time
->user
;
219 timer
->sys
+= stop_time
->sys
- start_time
->sys
;
220 timer
->wall
+= stop_time
->wall
- start_time
->wall
;
223 /* Initialize timing variables. */
231 /* Zero all elapsed times. */
232 memset ((void *) timevars
, 0, sizeof (timevars
));
234 /* Initialize the names of timing variables. */
235 #define DEFTIMEVAR(identifer__, name__) \
236 timevars[identifer__].name = name__;
237 #include "timevar.def"
241 ticks_to_msec
= TICKS_TO_MSEC
;
244 clocks_to_msec
= CLOCKS_TO_MSEC
;
248 /* Push TIMEVAR onto the timing stack. No further elapsed time is
249 attributed to the previous topmost timing variable on the stack;
250 subsequent elapsed time is attributed to TIMEVAR, until it is
251 popped or another element is pushed on top.
253 TIMEVAR cannot be running as a standalone timer. */
256 timevar_push (timevar
)
257 timevar_id_t timevar
;
259 struct timevar_def
*tv
= &timevars
[timevar
];
260 struct timevar_stack_def
*context
;
261 struct timevar_time_def now
;
266 /* Mark this timing variable as used. */
269 /* Can't push a standalone timer. */
273 /* What time is it? */
276 /* If the stack isn't empty, attribute the current elapsed time to
277 the old topmost element. */
279 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
281 /* Reset the start time; from now on, time is attributed to
285 /* See if we have a previously-allocated stack instance. If so,
286 take it off the list. If not, malloc a new one. */
287 if (unused_stack_instances
!= NULL
)
289 context
= unused_stack_instances
;
290 unused_stack_instances
= unused_stack_instances
->next
;
293 context
= (struct timevar_stack_def
*)
294 xmalloc (sizeof (struct timevar_stack_def
));
296 /* Fill it in and put it on the stack. */
297 context
->timevar
= tv
;
298 context
->next
= stack
;
302 /* Pop the topmost timing variable element off the timing stack. The
303 popped variable must be TIMEVAR. Elapsed time since the that
304 element was pushed on, or since it was last exposed on top of the
305 stack when the element above it was popped off, is credited to that
309 timevar_pop (timevar
)
310 timevar_id_t timevar
;
312 struct timevar_time_def now
;
313 struct timevar_stack_def
*popped
= stack
;
318 if (&timevars
[timevar
] != stack
->timevar
)
321 /* What time is it? */
324 /* Attribute the elapsed time to the element we're popping. */
325 timevar_accumulate (&popped
->timevar
->elapsed
, &start_time
, &now
);
327 /* Reset the start time; from now on, time is attributed to the
328 element just exposed on the stack. */
331 /* Take the item off the stack. */
334 /* Don't delete the stack element; instead, add it to the list of
335 unused elements for later use. */
336 popped
->next
= unused_stack_instances
;
337 unused_stack_instances
= popped
;
340 /* Start timing TIMEVAR independently of the timing stack. Elapsed
341 time until timevar_stop is called for the same timing variable is
342 attributed to TIMEVAR. */
345 timevar_start (timevar
)
346 timevar_id_t timevar
;
348 struct timevar_def
*tv
= &timevars
[timevar
];
353 /* Mark this timing variable as used. */
356 /* Don't allow the same timing variable to be started more than
362 get_time (&tv
->start_time
);
365 /* Stop timing TIMEVAR. Time elapsed since timevar_start was called
366 is attributed to it. */
369 timevar_stop (timevar
)
370 timevar_id_t timevar
;
372 struct timevar_def
*tv
= &timevars
[timevar
];
373 struct timevar_time_def now
;
378 /* TIMEVAR must have been started via timevar_start. */
383 timevar_accumulate (&tv
->elapsed
, &tv
->start_time
, &now
);
386 /* Fill the elapsed time for TIMEVAR into ELAPSED. Returns
387 update-to-date information even if TIMEVAR is currently running. */
390 timevar_get (timevar
, elapsed
)
391 timevar_id_t timevar
;
392 struct timevar_time_def
*elapsed
;
394 struct timevar_def
*tv
= &timevars
[timevar
];
395 struct timevar_time_def now
;
397 *elapsed
= tv
->elapsed
;
399 /* Is TIMEVAR currently running as a standalone timer? */
403 timevar_accumulate (elapsed
, &tv
->start_time
, &now
);
405 /* Or is TIMEVAR at the top of the timer stack? */
406 else if (stack
->timevar
== tv
)
409 timevar_accumulate (elapsed
, &start_time
, &now
);
413 /* Summarize timing variables to FP. The timing variable TV_TOTAL has
414 a special meaning -- it's considered to be the total elapsed time,
415 for normalizing the others, and is displayed last. */
421 /* Only print stuff if we have some sort of time information. */
422 #if defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME) || defined (HAVE_WALL_TIME)
423 unsigned int /* timevar_id_t */ id
;
424 struct timevar_time_def
*total
= &timevars
[TV_TOTAL
].elapsed
;
425 struct timevar_time_def now
;
430 /* Update timing information in case we're calling this from GDB. */
435 /* What time is it? */
438 /* If the stack isn't empty, attribute the current elapsed time to
439 the old topmost element. */
441 timevar_accumulate (&stack
->timevar
->elapsed
, &start_time
, &now
);
443 /* Reset the start time; from now on, time is attributed to
447 fputs (_("\nExecution times (seconds)\n"), fp
);
448 for (id
= 0; id
< (unsigned int) TIMEVAR_LAST
; ++id
)
450 struct timevar_def
*tv
= &timevars
[(timevar_id_t
) id
];
452 /* Don't print the total execution time here; that goes at the
454 if ((timevar_id_t
) id
== TV_TOTAL
)
457 /* Don't print timing variables that were never used. */
461 /* Don't print timing variables if we're going to get a row of
463 if (tv
->elapsed
.user
< 10 && tv
->elapsed
.sys
< 10
464 && tv
->elapsed
.wall
< 10)
467 /* The timing variable name. */
468 fprintf (fp
, " %-22s:", tv
->name
);
470 #ifdef HAVE_USER_TIME
471 /* Print user-mode time for this process. */
472 fprintf (fp
, "%4ld.%02ld (%2.0f%%) usr",
473 tv
->elapsed
.user
/ 1000, (tv
->elapsed
.user
% 1000) / 10,
474 (total
->user
== 0) ? 0.0
475 : (100.0 * tv
->elapsed
.user
/ (double) total
->user
));
476 #endif /* HAVE_USER_TIME */
479 /* Print system-mode time for this process. */
480 fprintf (fp
, "%4ld.%02ld (%2.0f%%) sys",
481 tv
->elapsed
.sys
/ 1000, (tv
->elapsed
.sys
% 1000) / 10,
482 (total
->sys
== 0) ? 0.0
483 : (100.0 * tv
->elapsed
.sys
/ (double) total
->sys
));
484 #endif /* HAVE_SYS_TIME */
486 #ifdef HAVE_WALL_TIME
487 /* Print wall clock time elapsed. */
488 fprintf (fp
, "%4ld.%02ld (%2.0f%%) wall",
489 tv
->elapsed
.wall
/ 1000, (tv
->elapsed
.wall
% 1000) / 10,
490 (total
->wall
== 0) ? 0.0
491 : (100.0 * tv
->elapsed
.wall
/ (double) total
->wall
));
492 #endif /* HAVE_WALL_TIME */
497 /* Print total time. */
498 fputs (_(" TOTAL :"), fp
);
499 #ifdef HAVE_USER_TIME
500 fprintf (fp
, "%4ld.%02ld ",
501 total
->user
/ 1000, (total
->user
% 1000) / 10);
504 fprintf (fp
, "%4ld.%02ld ",
505 total
->sys
/ 1000, (total
->sys
% 1000) / 10);
507 #ifdef HAVE_WALL_TIME
508 fprintf (fp
, "%4ld.%02ld\n",
509 total
->wall
/ 1000, (total
->wall
% 1000) / 10);
512 #endif /* defined (HAVE_USER_TIME) || defined (HAVE_SYS_TIME)
513 || defined (HAVE_WALL_TIME) */
516 /* Returns time (user + system) used so far by the compiler process,
522 struct timevar_time_def total_elapsed
;
523 timevar_get (TV_TOTAL
, &total_elapsed
);
524 return total_elapsed
.user
+ total_elapsed
.sys
;
527 /* Prints a message to stderr stating that time elapsed in STR is
528 TOTAL (given in microseconds). */
531 print_time (str
, total
)
535 long all_time
= get_run_time ();
537 _("time in %s: %ld.%06ld (%ld%%)\n"),
538 str
, total
/ 1000000, total
% 1000000,
540 : (long) (((100.0 * (double) total
) / (double) all_time
) + .5));