Wed Mar 20 18:15:47 1996 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[deliverable/binutils-gdb.git] / gprof / gprof.texi
CommitLineData
be4e1cd5
JO
1\input texinfo @c -*-texinfo-*-
2@setfilename gprof.info
3@settitle GNU gprof
4@setchapternewpage odd
44c8c1d5
DZ
5
6@ifinfo
7@c This is a dir.info fragment to support semi-automated addition of
8@c manuals to an info tree. zoo@cygnus.com is developing this facility.
9@format
10START-INFO-DIR-ENTRY
5ee3dd17 11* gprof: (gprof). Profiling your program's execution
44c8c1d5
DZ
12END-INFO-DIR-ENTRY
13@end format
14@end ifinfo
15
be4e1cd5
JO
16@ifinfo
17This file documents the gprof profiler of the GNU system.
18
19Copyright (C) 1988, 1992 Free Software Foundation, Inc.
20
21Permission is granted to make and distribute verbatim copies of
22this manual provided the copyright notice and this permission notice
23are preserved on all copies.
24
25@ignore
26Permission is granted to process this file through Tex and print the
27results, provided the printed document carries copying permission
28notice identical to this one except for the removal of this paragraph
29(this paragraph not being relevant to the printed manual).
30
31@end ignore
32Permission is granted to copy and distribute modified versions of this
33manual under the conditions for verbatim copying, provided that the entire
34resulting derived work is distributed under the terms of a permission
35notice identical to this one.
36
37Permission is granted to copy and distribute translations of this manual
38into another language, under the above conditions for modified versions.
39@end ifinfo
40
41@finalout
42@smallbook
43
44@titlepage
45@title GNU gprof
46@subtitle The @sc{gnu} Profiler
47@author Jay Fenlason and Richard Stallman
48
49@page
50
51This manual describes the @sc{gnu} profiler, @code{gprof}, and how you
52can use it to determine which parts of a program are taking most of the
53execution time. We assume that you know how to write, compile, and
54execute programs. @sc{gnu} @code{gprof} was written by Jay Fenlason.
55
56This manual was edited January 1993 by Jeffrey Osier.
57
58@vskip 0pt plus 1filll
59Copyright @copyright{} 1988, 1992 Free Software Foundation, Inc.
60
61Permission is granted to make and distribute verbatim copies of
62this manual provided the copyright notice and this permission notice
63are preserved on all copies.
64
65@ignore
66Permission is granted to process this file through TeX and print the
67results, provided the printed document carries copying permission
68notice identical to this one except for the removal of this paragraph
69(this paragraph not being relevant to the printed manual).
70
71@end ignore
72Permission is granted to copy and distribute modified versions of this
73manual under the conditions for verbatim copying, provided that the entire
74resulting derived work is distributed under the terms of a permission
75notice identical to this one.
76
77Permission is granted to copy and distribute translations of this manual
78into another language, under the same conditions as for modified versions.
79
80@end titlepage
81
82@ifinfo
83@node Top
84@top Profiling a Program: Where Does It Spend Its Time?
85
86This manual describes the @sc{gnu} profiler, @code{gprof}, and how you
87can use it to determine which parts of a program are taking most of the
88execution time. We assume that you know how to write, compile, and
89execute programs. @sc{gnu} @code{gprof} was written by Jay Fenlason.
90
a2b34707
JO
91This manual was updated January 1993.
92
be4e1cd5
JO
93@menu
94* Why:: What profiling means, and why it is useful.
95* Compiling:: How to compile your program for profiling.
96* Executing:: How to execute your program to generate the
97 profile data file @file{gmon.out}.
98* Invoking:: How to run @code{gprof}, and how to specify
99 options for it.
100
101* Flat Profile:: The flat profile shows how much time was spent
102 executing directly in each function.
103* Call Graph:: The call graph shows which functions called which
104 others, and how much time each function used
105 when its subroutine calls are included.
106
107* Implementation:: How the profile data is recorded and written.
108* Sampling Error:: Statistical margins of error.
109 How to accumulate data from several runs
110 to make it more accurate.
111
112* Assumptions:: Some of @code{gprof}'s measurements are based
113 on assumptions about your program
114 that could be very wrong.
115
116* Incompatibilities:: (between GNU @code{gprof} and Unix @code{gprof}.)
117@end menu
118@end ifinfo
119
120@node Why
121@chapter Why Profile
122
123Profiling allows you to learn where your program spent its time and which
124functions called which other functions while it was executing. This
125information can show you which pieces of your program are slower than you
126expected, and might be candidates for rewriting to make your program
127execute faster. It can also tell you which functions are being called more
128or less often than you expected. This may help you spot bugs that had
129otherwise been unnoticed.
130
131Since the profiler uses information collected during the actual execution
132of your program, it can be used on programs that are too large or too
133complex to analyze by reading the source. However, how your program is run
134will affect the information that shows up in the profile data. If you
135don't use some feature of your program while it is being profiled, no
136profile information will be generated for that feature.
137
138Profiling has several steps:
139
140@itemize @bullet
141@item
142You must compile and link your program with profiling enabled.
143@xref{Compiling}.
144
145@item
146You must execute your program to generate a profile data file.
147@xref{Executing}.
148
149@item
150You must run @code{gprof} to analyze the profile data.
151@xref{Invoking}.
152@end itemize
153
154The next three chapters explain these steps in greater detail.
155
156The result of the analysis is a file containing two tables, the
157@dfn{flat profile} and the @dfn{call graph} (plus blurbs which briefly
158explain the contents of these tables).
159
160The flat profile shows how much time your program spent in each function,
161and how many times that function was called. If you simply want to know
162which functions burn most of the cycles, it is stated concisely here.
163@xref{Flat Profile}.
164
165The call graph shows, for each function, which functions called it, which
166other functions it called, and how many times. There is also an estimate
167of how much time was spent in the subroutines of each function. This can
168suggest places where you might try to eliminate function calls that use a
169lot of time. @xref{Call Graph}.
170
171@node Compiling
172@chapter Compiling a Program for Profiling
173
174The first step in generating profile information for your program is
175to compile and link it with profiling enabled.
176
177To compile a source file for profiling, specify the @samp{-pg} option when
178you run the compiler. (This is in addition to the options you normally
179use.)
180
181To link the program for profiling, if you use a compiler such as @code{cc}
182to do the linking, simply specify @samp{-pg} in addition to your usual
183options. The same option, @samp{-pg}, alters either compilation or linking
184to do what is necessary for profiling. Here are examples:
185
186@example
187cc -g -c myprog.c utils.c -pg
188cc -o myprog myprog.o utils.o -pg
189@end example
190
191The @samp{-pg} option also works with a command that both compiles and links:
192
193@example
194cc -o myprog myprog.c utils.c -g -pg
195@end example
196
197If you run the linker @code{ld} directly instead of through a compiler
198such as @code{cc}, you must specify the profiling startup file
199@file{/lib/gcrt0.o} as the first input file instead of the usual startup
200file @file{/lib/crt0.o}. In addition, you would probably want to
201specify the profiling C library, @file{/usr/lib/libc_p.a}, by writing
202@samp{-lc_p} instead of the usual @samp{-lc}. This is not absolutely
203necessary, but doing this gives you number-of-calls information for
204standard library functions such as @code{read} and @code{open}. For
205example:
206
207@example
208ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p
209@end example
210
211If you compile only some of the modules of the program with @samp{-pg}, you
212can still profile the program, but you won't get complete information about
213the modules that were compiled without @samp{-pg}. The only information
214you get for the functions in those modules is the total time spent in them;
215there is no record of how many times they were called, or from where. This
216will not affect the flat profile (except that the @code{calls} field for
217the functions will be blank), but will greatly reduce the usefulness of the
218call graph.
219
220@node Executing
221@chapter Executing the Program to Generate Profile Data
222
223Once the program is compiled for profiling, you must run it in order to
224generate the information that @code{gprof} needs. Simply run the program
225as usual, using the normal arguments, file names, etc. The program should
226run normally, producing the same output as usual. It will, however, run
227somewhat slower than normal because of the time spent collecting and the
228writing the profile data.
229
230The way you run the program---the arguments and input that you give
231it---may have a dramatic effect on what the profile information shows. The
232profile data will describe the parts of the program that were activated for
233the particular input you use. For example, if the first command you give
234to your program is to quit, the profile data will show the time used in
235initialization and in cleanup, but not much else.
236
237You program will write the profile data into a file called @file{gmon.out}
238just before exiting. If there is already a file called @file{gmon.out},
239its contents are overwritten. There is currently no way to tell the
240program to write the profile data under a different name, but you can rename
241the file afterward if you are concerned that it may be overwritten.
242
243In order to write the @file{gmon.out} file properly, your program must exit
244normally: by returning from @code{main} or by calling @code{exit}. Calling
245the low-level function @code{_exit} does not write the profile data, and
246neither does abnormal termination due to an unhandled signal.
247
248The @file{gmon.out} file is written in the program's @emph{current working
249directory} at the time it exits. This means that if your program calls
250@code{chdir}, the @file{gmon.out} file will be left in the last directory
251your program @code{chdir}'d to. If you don't have permission to write in
252this directory, the file is not written. You may get a confusing error
253message if this happens. (We have not yet replaced the part of Unix
254responsible for this; when we do, we will make the error message
255comprehensible.)
256
257@node Invoking
258@chapter @code{gprof} Command Summary
259
260After you have a profile data file @file{gmon.out}, you can run @code{gprof}
261to interpret the information in it. The @code{gprof} program prints a
262flat profile and a call graph on standard output. Typically you would
263redirect the output of @code{gprof} into a file with @samp{>}.
264
265You run @code{gprof} like this:
266
267@smallexample
268gprof @var{options} [@var{executable-file} [@var{profile-data-files}@dots{}]] [> @var{outfile}]
269@end smallexample
270
271@noindent
272Here square-brackets indicate optional arguments.
273
274If you omit the executable file name, the file @file{a.out} is used. If
275you give no profile data file name, the file @file{gmon.out} is used. If
276any file is not in the proper format, or if the profile data file does not
277appear to belong to the executable file, an error message is printed.
278
279You can give more than one profile data file by entering all their names
280after the executable file name; then the statistics in all the data files
281are summed together.
282
283The following options may be used to selectively include or exclude
284functions in the output:
285
286@table @code
287@item -a
288The @samp{-a} option causes @code{gprof} to suppress the printing of
289statically declared (private) functions. (These are functions whose
290names are not listed as global, and which are not visible outside the
291file/function/block where they were defined.) Time spent in these
292functions, calls to/from them, etc, will all be attributed to the
293function that was loaded directly before it in the executable file.
294@c This is compatible with Unix @code{gprof}, but a bad idea.
295This option affects both the flat profile and the call graph.
296
32843f94
JL
297@item -D
298The @samp{-D} option causes @code{gprof} to ignore symbols which
299are not known to be functions. This option will give more accurate
300profile data on systems where it is supported (Solaris and HPUX for
301example).
302
be4e1cd5
JO
303@item -e @var{function_name}
304The @samp{-e @var{function}} option tells @code{gprof} to not print
305information about the function @var{function_name} (and its
306children@dots{}) in the call graph. The function will still be listed
307as a child of any functions that call it, but its index number will be
308shown as @samp{[not printed]}. More than one @samp{-e} option may be
309given; only one @var{function_name} may be indicated with each @samp{-e}
310option.
311
312@item -E @var{function_name}
313The @code{-E @var{function}} option works like the @code{-e} option, but
314time spent in the function (and children who were not called from
315anywhere else), will not be used to compute the percentages-of-time for
316the call graph. More than one @samp{-E} option may be given; only one
317@var{function_name} may be indicated with each @samp{-E} option.
318
319@item -f @var{function_name}
320The @samp{-f @var{function}} option causes @code{gprof} to limit the
321call graph to the function @var{function_name} and its children (and
322their children@dots{}). More than one @samp{-f} option may be given;
323only one @var{function_name} may be indicated with each @samp{-f}
324option.
325
326@item -F @var{function_name}
327The @samp{-F @var{function}} option works like the @code{-f} option, but
328only time spent in the function and its children (and their
329children@dots{}) will be used to determine total-time and
330percentages-of-time for the call graph. More than one @samp{-F} option
331may be given; only one @var{function_name} may be indicated with each
332@samp{-F} option. The @samp{-F} option overrides the @samp{-E} option.
333
334@item -k @var{from@dots{}} @var{to@dots{}}
335The @samp{-k} option allows you to delete from the profile any arcs from
336routine @var{from} to routine @var{to}.
337
4fe2350b 338@item -v
4db865d0 339The @samp{-v} flag causes @code{gprof} to print the current version
4fe2350b
DZ
340number, and then exit.
341
be4e1cd5
JO
342@item -z
343If you give the @samp{-z} option, @code{gprof} will mention all
344functions in the flat profile, even those that were never called, and
345that had no time spent in them. This is useful in conjunction with the
346@samp{-c} option for discovering which routines were never called.
347@end table
348
349The order of these options does not matter.
350
351Note that only one function can be specified with each @code{-e},
352@code{-E}, @code{-f} or @code{-F} option. To specify more than one
353function, use multiple options. For example, this command:
354
355@example
356gprof -e boring -f foo -f bar myprogram > gprof.output
357@end example
358
359@noindent
360lists in the call graph all functions that were reached from either
361@code{foo} or @code{bar} and were not reachable from @code{boring}.
362
363There are a few other useful @code{gprof} options:
364
365@table @code
366@item -b
367If the @samp{-b} option is given, @code{gprof} doesn't print the
368verbose blurbs that try to explain the meaning of all of the fields in
369the tables. This is useful if you intend to print out the output, or
370are tired of seeing the blurbs.
371
372@item -c
373The @samp{-c} option causes the static call-graph of the program to be
374discovered by a heuristic which examines the text space of the object
375file. Static-only parents or children are indicated with call counts of
376@samp{0}.
377
378@item -d @var{num}
379The @samp{-d @var{num}} option specifies debugging options.
380@c @xref{debugging}.
381
382@item -s
383The @samp{-s} option causes @code{gprof} to summarize the information
384in the profile data files it read in, and write out a profile data
385file called @file{gmon.sum}, which contains all the information from
386the profile data files that @code{gprof} read in. The file @file{gmon.sum}
387may be one of the specified input files; the effect of this is to
388merge the data in the other input files into @file{gmon.sum}.
389@xref{Sampling Error}.
390
391Eventually you can run @code{gprof} again without @samp{-s} to analyze the
392cumulative data in the file @file{gmon.sum}.
393
394@item -T
395The @samp{-T} option causes @code{gprof} to print its output in
396``traditional'' BSD style.
64c50fc5
JL
397
398@item --function-ordering
399The @samp{--function-ordering} option causes @code{gprof} to print a
400suggested function ordering for the program based on profiling data.
401This option suggests an ordering which may improve paging, tlb and
402cache behavior for the program on systems which support arbitrary
403ordering of functions in an executable.
404
405The exact details of how to force the linker to place functions
406in a particular order is system dependent and out of the scope of this
407manual.
408
409@item --file-ordering @var{map_file}
410The @samp{--file-ordering} option causes @code{gprof} to print a
411suggested .o link line ordering for the program based on profiling data.
412This option suggests an ordering which may improve paging, tlb and
413cache behavior for the program on systems which do not support arbitrary
414ordering of functions in an executable.
415
416Use of the @samp{-a} argument is highly recommended with this option.
417
418The @var{map_file} argument is a pathname to a file which provides
419function name to object file mappings. The format of the file is similar to
420the output of the program @code{nm}.
421
422@smallexample
423@group
424c-parse.o:00000000 T yyparse
425c-parse.o:00000004 C yyerrflag
426c-lang.o:00000000 T maybe_objc_method_name
427c-lang.o:00000000 T print_lang_statistics
428c-lang.o:00000000 T recognize_objc_keyword
429c-decl.o:00000000 T print_lang_identifier
430c-decl.o:00000000 T print_lang_type
431@dots{}
432
433@end group
434@end smallexample
435
436GNU @code{nm} @samp{--extern-only} @samp{--defined-only} @samp{-v} @samp{--print-file-name} can be used to create @var{map_file}.
be4e1cd5
JO
437@end table
438
439@node Flat Profile
440@chapter How to Understand the Flat Profile
441@cindex flat profile
442
443The @dfn{flat profile} shows the total amount of time your program
444spent executing each function. Unless the @samp{-z} option is given,
445functions with no apparent time spent in them, and no apparent calls
446to them, are not mentioned. Note that if a function was not compiled
447for profiling, and didn't run long enough to show up on the program
448counter histogram, it will be indistinguishable from a function that
449was never called.
450
451This is part of a flat profile for a small program:
452
453@smallexample
454@group
455Flat profile:
456
457Each sample counts as 0.01 seconds.
458 % cumulative self self total
459 time seconds seconds calls ms/call ms/call name
460 33.34 0.02 0.02 7208 0.00 0.00 open
461 16.67 0.03 0.01 244 0.04 0.12 offtime
462 16.67 0.04 0.01 8 1.25 1.25 memccpy
463 16.67 0.05 0.01 7 1.43 1.43 write
464 16.67 0.06 0.01 mcount
465 0.00 0.06 0.00 236 0.00 0.00 tzset
466 0.00 0.06 0.00 192 0.00 0.00 tolower
467 0.00 0.06 0.00 47 0.00 0.00 strlen
468 0.00 0.06 0.00 45 0.00 0.00 strchr
469 0.00 0.06 0.00 1 0.00 50.00 main
470 0.00 0.06 0.00 1 0.00 0.00 memcpy
471 0.00 0.06 0.00 1 0.00 10.11 print
472 0.00 0.06 0.00 1 0.00 0.00 profil
473 0.00 0.06 0.00 1 0.00 50.00 report
474@dots{}
475@end group
476@end smallexample
477
478@noindent
479The functions are sorted by decreasing run-time spent in them. The
480functions @samp{mcount} and @samp{profil} are part of the profiling
481aparatus and appear in every flat profile; their time gives a measure of
482the amount of overhead due to profiling.
483
484The sampling period estimates the margin of error in each of the time
485figures. A time figure that is not much larger than this is not
486reliable. In this example, the @samp{self seconds} field for
487@samp{mcount} might well be @samp{0} or @samp{0.04} in another run.
488@xref{Sampling Error}, for a complete discussion.
489
490Here is what the fields in each line mean:
491
492@table @code
493@item % time
494This is the percentage of the total execution time your program spent
495in this function. These should all add up to 100%.
496
497@item cumulative seconds
498This is the cumulative total number of seconds the computer spent
499executing this functions, plus the time spent in all the functions
500above this one in this table.
501
502@item self seconds
503This is the number of seconds accounted for by this function alone.
504The flat profile listing is sorted first by this number.
505
506@item calls
507This is the total number of times the function was called. If the
508function was never called, or the number of times it was called cannot
509be determined (probably because the function was not compiled with
510profiling enabled), the @dfn{calls} field is blank.
511
512@item self ms/call
513This represents the average number of milliseconds spent in this
514function per call, if this function is profiled. Otherwise, this field
515is blank for this function.
516
517@item total ms/call
518This represents the average number of milliseconds spent in this
519function and its descendants per call, if this function is profiled.
520Otherwise, this field is blank for this function.
521
522@item name
523This is the name of the function. The flat profile is sorted by this
524field alphabetically after the @dfn{self seconds} field is sorted.
525@end table
526
527@node Call Graph
528@chapter How to Read the Call Graph
529@cindex call graph
530
531The @dfn{call graph} shows how much time was spent in each function
532and its children. From this information, you can find functions that,
533while they themselves may not have used much time, called other
534functions that did use unusual amounts of time.
535
536Here is a sample call from a small program. This call came from the
537same @code{gprof} run as the flat profile example in the previous
538chapter.
539
540@smallexample
541@group
542granularity: each sample hit covers 2 byte(s) for 20.00% of 0.05 seconds
543
544index % time self children called name
545 <spontaneous>
546[1] 100.0 0.00 0.05 start [1]
547 0.00 0.05 1/1 main [2]
548 0.00 0.00 1/2 on_exit [28]
549 0.00 0.00 1/1 exit [59]
550-----------------------------------------------
551 0.00 0.05 1/1 start [1]
552[2] 100.0 0.00 0.05 1 main [2]
553 0.00 0.05 1/1 report [3]
554-----------------------------------------------
555 0.00 0.05 1/1 main [2]
556[3] 100.0 0.00 0.05 1 report [3]
557 0.00 0.03 8/8 timelocal [6]
558 0.00 0.01 1/1 print [9]
559 0.00 0.01 9/9 fgets [12]
560 0.00 0.00 12/34 strncmp <cycle 1> [40]
561 0.00 0.00 8/8 lookup [20]
562 0.00 0.00 1/1 fopen [21]
563 0.00 0.00 8/8 chewtime [24]
564 0.00 0.00 8/16 skipspace [44]
565-----------------------------------------------
566[4] 59.8 0.01 0.02 8+472 <cycle 2 as a whole> [4]
567 0.01 0.02 244+260 offtime <cycle 2> [7]
568 0.00 0.00 236+1 tzset <cycle 2> [26]
569-----------------------------------------------
570@end group
571@end smallexample
572
573The lines full of dashes divide this table into @dfn{entries}, one for each
574function. Each entry has one or more lines.
575
576In each entry, the primary line is the one that starts with an index number
577in square brackets. The end of this line says which function the entry is
578for. The preceding lines in the entry describe the callers of this
579function and the following lines describe its subroutines (also called
580@dfn{children} when we speak of the call graph).
581
582The entries are sorted by time spent in the function and its subroutines.
583
584The internal profiling function @code{mcount} (@pxref{Flat Profile})
585is never mentioned in the call graph.
586
587@menu
588* Primary:: Details of the primary line's contents.
589* Callers:: Details of caller-lines' contents.
590* Subroutines:: Details of subroutine-lines' contents.
591* Cycles:: When there are cycles of recursion,
592 such as @code{a} calls @code{b} calls @code{a}@dots{}
593@end menu
594
595@node Primary
596@section The Primary Line
597
598The @dfn{primary line} in a call graph entry is the line that
599describes the function which the entry is about and gives the overall
600statistics for this function.
601
602For reference, we repeat the primary line from the entry for function
603@code{report} in our main example, together with the heading line that
604shows the names of the fields:
605
606@smallexample
607@group
608index % time self children called name
609@dots{}
610[3] 100.0 0.00 0.05 1 report [3]
611@end group
612@end smallexample
613
614Here is what the fields in the primary line mean:
615
616@table @code
617@item index
618Entries are numbered with consecutive integers. Each function
619therefore has an index number, which appears at the beginning of its
620primary line.
621
622Each cross-reference to a function, as a caller or subroutine of
623another, gives its index number as well as its name. The index number
624guides you if you wish to look for the entry for that function.
625
626@item % time
627This is the percentage of the total time that was spent in this
628function, including time spent in subroutines called from this
629function.
630
631The time spent in this function is counted again for the callers of
632this function. Therefore, adding up these percentages is meaningless.
633
634@item self
635This is the total amount of time spent in this function. This
636should be identical to the number printed in the @code{seconds} field
637for this function in the flat profile.
638
639@item children
640This is the total amount of time spent in the subroutine calls made by
641this function. This should be equal to the sum of all the @code{self}
642and @code{children} entries of the children listed directly below this
643function.
644
645@item called
646This is the number of times the function was called.
647
648If the function called itself recursively, there are two numbers,
649separated by a @samp{+}. The first number counts non-recursive calls,
650and the second counts recursive calls.
651
652In the example above, the function @code{report} was called once from
653@code{main}.
654
655@item name
656This is the name of the current function. The index number is
657repeated after it.
658
659If the function is part of a cycle of recursion, the cycle number is
660printed between the function's name and the index number
661(@pxref{Cycles}). For example, if function @code{gnurr} is part of
662cycle number one, and has index number twelve, its primary line would
663be end like this:
664
665@example
666gnurr <cycle 1> [12]
667@end example
668@end table
669
670@node Callers, Subroutines, Primary, Call Graph
671@section Lines for a Function's Callers
672
673A function's entry has a line for each function it was called by.
674These lines' fields correspond to the fields of the primary line, but
675their meanings are different because of the difference in context.
676
677For reference, we repeat two lines from the entry for the function
678@code{report}, the primary line and one caller-line preceding it, together
679with the heading line that shows the names of the fields:
680
681@smallexample
682index % time self children called name
683@dots{}
684 0.00 0.05 1/1 main [2]
685[3] 100.0 0.00 0.05 1 report [3]
686@end smallexample
687
688Here are the meanings of the fields in the caller-line for @code{report}
689called from @code{main}:
690
691@table @code
692@item self
693An estimate of the amount of time spent in @code{report} itself when it was
694called from @code{main}.
695
696@item children
697An estimate of the amount of time spent in subroutines of @code{report}
698when @code{report} was called from @code{main}.
699
700The sum of the @code{self} and @code{children} fields is an estimate
701of the amount of time spent within calls to @code{report} from @code{main}.
702
703@item called
704Two numbers: the number of times @code{report} was called from @code{main},
705followed by the total number of nonrecursive calls to @code{report} from
706all its callers.
707
708@item name and index number
709The name of the caller of @code{report} to which this line applies,
710followed by the caller's index number.
711
712Not all functions have entries in the call graph; some
713options to @code{gprof} request the omission of certain functions.
714When a caller has no entry of its own, it still has caller-lines
715in the entries of the functions it calls.
716
717If the caller is part of a recursion cycle, the cycle number is
718printed between the name and the index number.
719@end table
720
721If the identity of the callers of a function cannot be determined, a
722dummy caller-line is printed which has @samp{<spontaneous>} as the
723``caller's name'' and all other fields blank. This can happen for
724signal handlers.
725@c What if some calls have determinable callers' names but not all?
726@c FIXME - still relevant?
727
728@node Subroutines, Cycles, Callers, Call Graph
729@section Lines for a Function's Subroutines
730
731A function's entry has a line for each of its subroutines---in other
732words, a line for each other function that it called. These lines'
733fields correspond to the fields of the primary line, but their meanings
734are different because of the difference in context.
735
736For reference, we repeat two lines from the entry for the function
737@code{main}, the primary line and a line for a subroutine, together
738with the heading line that shows the names of the fields:
739
740@smallexample
741index % time self children called name
742@dots{}
743[2] 100.0 0.00 0.05 1 main [2]
744 0.00 0.05 1/1 report [3]
745@end smallexample
746
747Here are the meanings of the fields in the subroutine-line for @code{main}
748calling @code{report}:
749
750@table @code
751@item self
752An estimate of the amount of time spent directly within @code{report}
753when @code{report} was called from @code{main}.
754
755@item children
756An estimate of the amount of time spent in subroutines of @code{report}
757when @code{report} was called from @code{main}.
758
759The sum of the @code{self} and @code{children} fields is an estimate
760of the total time spent in calls to @code{report} from @code{main}.
761
762@item called
763Two numbers, the number of calls to @code{report} from @code{main}
764followed by the total number of nonrecursive calls to @code{report}.
765
766@item name
767The name of the subroutine of @code{main} to which this line applies,
768followed by the subroutine's index number.
769
770If the caller is part of a recursion cycle, the cycle number is
771printed between the name and the index number.
772@end table
773
774@node Cycles,, Subroutines, Call Graph
775@section How Mutually Recursive Functions Are Described
776@cindex cycle
777@cindex recursion cycle
778
779The graph may be complicated by the presence of @dfn{cycles of
780recursion} in the call graph. A cycle exists if a function calls
781another function that (directly or indirectly) calls (or appears to
782call) the original function. For example: if @code{a} calls @code{b},
783and @code{b} calls @code{a}, then @code{a} and @code{b} form a cycle.
784
785Whenever there are call-paths both ways between a pair of functions, they
786belong to the same cycle. If @code{a} and @code{b} call each other and
787@code{b} and @code{c} call each other, all three make one cycle. Note that
788even if @code{b} only calls @code{a} if it was not called from @code{a},
789@code{gprof} cannot determine this, so @code{a} and @code{b} are still
790considered a cycle.
791
792The cycles are numbered with consecutive integers. When a function
793belongs to a cycle, each time the function name appears in the call graph
794it is followed by @samp{<cycle @var{number}>}.
795
796The reason cycles matter is that they make the time values in the call
797graph paradoxical. The ``time spent in children'' of @code{a} should
798include the time spent in its subroutine @code{b} and in @code{b}'s
799subroutines---but one of @code{b}'s subroutines is @code{a}! How much of
800@code{a}'s time should be included in the children of @code{a}, when
801@code{a} is indirectly recursive?
802
803The way @code{gprof} resolves this paradox is by creating a single entry
804for the cycle as a whole. The primary line of this entry describes the
805total time spent directly in the functions of the cycle. The
806``subroutines'' of the cycle are the individual functions of the cycle, and
807all other functions that were called directly by them. The ``callers'' of
808the cycle are the functions, outside the cycle, that called functions in
809the cycle.
810
811Here is an example portion of a call graph which shows a cycle containing
812functions @code{a} and @code{b}. The cycle was entered by a call to
813@code{a} from @code{main}; both @code{a} and @code{b} called @code{c}.
814
815@smallexample
816index % time self children called name
817----------------------------------------
818 1.77 0 1/1 main [2]
819[3] 91.71 1.77 0 1+5 <cycle 1 as a whole> [3]
820 1.02 0 3 b <cycle 1> [4]
821 0.75 0 2 a <cycle 1> [5]
822----------------------------------------
823 3 a <cycle 1> [5]
824[4] 52.85 1.02 0 0 b <cycle 1> [4]
825 2 a <cycle 1> [5]
826 0 0 3/6 c [6]
827----------------------------------------
828 1.77 0 1/1 main [2]
829 2 b <cycle 1> [4]
830[5] 38.86 0.75 0 1 a <cycle 1> [5]
831 3 b <cycle 1> [4]
832 0 0 3/6 c [6]
833----------------------------------------
834@end smallexample
835
836@noindent
837(The entire call graph for this program contains in addition an entry for
838@code{main}, which calls @code{a}, and an entry for @code{c}, with callers
839@code{a} and @code{b}.)
840
841@smallexample
842index % time self children called name
843 <spontaneous>
844[1] 100.00 0 1.93 0 start [1]
845 0.16 1.77 1/1 main [2]
846----------------------------------------
847 0.16 1.77 1/1 start [1]
848[2] 100.00 0.16 1.77 1 main [2]
849 1.77 0 1/1 a <cycle 1> [5]
850----------------------------------------
851 1.77 0 1/1 main [2]
852[3] 91.71 1.77 0 1+5 <cycle 1 as a whole> [3]
853 1.02 0 3 b <cycle 1> [4]
854 0.75 0 2 a <cycle 1> [5]
855 0 0 6/6 c [6]
856----------------------------------------
857 3 a <cycle 1> [5]
858[4] 52.85 1.02 0 0 b <cycle 1> [4]
859 2 a <cycle 1> [5]
860 0 0 3/6 c [6]
861----------------------------------------
862 1.77 0 1/1 main [2]
863 2 b <cycle 1> [4]
864[5] 38.86 0.75 0 1 a <cycle 1> [5]
865 3 b <cycle 1> [4]
866 0 0 3/6 c [6]
867----------------------------------------
868 0 0 3/6 b <cycle 1> [4]
869 0 0 3/6 a <cycle 1> [5]
870[6] 0.00 0 0 6 c [6]
871----------------------------------------
872@end smallexample
873
874The @code{self} field of the cycle's primary line is the total time
875spent in all the functions of the cycle. It equals the sum of the
876@code{self} fields for the individual functions in the cycle, found
877in the entry in the subroutine lines for these functions.
878
879The @code{children} fields of the cycle's primary line and subroutine lines
880count only subroutines outside the cycle. Even though @code{a} calls
881@code{b}, the time spent in those calls to @code{b} is not counted in
882@code{a}'s @code{children} time. Thus, we do not encounter the problem of
883what to do when the time in those calls to @code{b} includes indirect
884recursive calls back to @code{a}.
885
886The @code{children} field of a caller-line in the cycle's entry estimates
887the amount of time spent @emph{in the whole cycle}, and its other
888subroutines, on the times when that caller called a function in the cycle.
889
890The @code{calls} field in the primary line for the cycle has two numbers:
891first, the number of times functions in the cycle were called by functions
892outside the cycle; second, the number of times they were called by
893functions in the cycle (including times when a function in the cycle calls
894itself). This is a generalization of the usual split into nonrecursive and
895recursive calls.
896
897The @code{calls} field of a subroutine-line for a cycle member in the
898cycle's entry says how many time that function was called from functions in
899the cycle. The total of all these is the second number in the primary line's
900@code{calls} field.
901
902In the individual entry for a function in a cycle, the other functions in
903the same cycle can appear as subroutines and as callers. These lines show
904how many times each function in the cycle called or was called from each other
905function in the cycle. The @code{self} and @code{children} fields in these
906lines are blank because of the difficulty of defining meanings for them
907when recursion is going on.
908
909@node Implementation, Sampling Error, Call Graph, Top
910@chapter Implementation of Profiling
911
912Profiling works by changing how every function in your program is compiled
913so that when it is called, it will stash away some information about where
914it was called from. From this, the profiler can figure out what function
915called it, and can count how many times it was called. This change is made
916by the compiler when your program is compiled with the @samp{-pg} option.
917
918Profiling also involves watching your program as it runs, and keeping a
919histogram of where the program counter happens to be every now and then.
920Typically the program counter is looked at around 100 times per second of
921run time, but the exact frequency may vary from system to system.
922
923A special startup routine allocates memory for the histogram and sets up
924a clock signal handler to make entries in it. Use of this special
925startup routine is one of the effects of using @samp{gcc @dots{} -pg} to
926link. The startup file also includes an @samp{exit} function which is
927responsible for writing the file @file{gmon.out}.
928
929Number-of-calls information for library routines is collected by using a
930special version of the C library. The programs in it are the same as in
931the usual C library, but they were compiled with @samp{-pg}. If you
932link your program with @samp{gcc @dots{} -pg}, it automatically uses the
933profiling version of the library.
934
935The output from @code{gprof} gives no indication of parts of your program that
936are limited by I/O or swapping bandwidth. This is because samples of the
937program counter are taken at fixed intervals of run time. Therefore, the
938time measurements in @code{gprof} output say nothing about time that your
939program was not running. For example, a part of the program that creates
940so much data that it cannot all fit in physical memory at once may run very
941slowly due to thrashing, but @code{gprof} will say it uses little time. On
942the other hand, sampling by run time has the advantage that the amount of
943load due to other users won't directly affect the output you get.
944
945@node Sampling Error, Assumptions, Implementation, Top
946@chapter Statistical Inaccuracy of @code{gprof} Output
947
948The run-time figures that @code{gprof} gives you are based on a sampling
949process, so they are subject to statistical inaccuracy. If a function runs
950only a small amount of time, so that on the average the sampling process
951ought to catch that function in the act only once, there is a pretty good
952chance it will actually find that function zero times, or twice.
953
954By contrast, the number-of-calls figures are derived by counting, not
955sampling. They are completely accurate and will not vary from run to run
956if your program is deterministic.
957
958The @dfn{sampling period} that is printed at the beginning of the flat
959profile says how often samples are taken. The rule of thumb is that a
960run-time figure is accurate if it is considerably bigger than the sampling
961period.
962
963The actual amount of error is usually more than one sampling period. In
964fact, if a value is @var{n} times the sampling period, the @emph{expected}
965error in it is the square-root of @var{n} sampling periods. If the
966sampling period is 0.01 seconds and @code{foo}'s run-time is 1 second, the
967expected error in @code{foo}'s run-time is 0.1 seconds. It is likely to
968vary this much @emph{on the average} from one profiling run to the next.
969(@emph{Sometimes} it will vary more.)
970
971This does not mean that a small run-time figure is devoid of information.
972If the program's @emph{total} run-time is large, a small run-time for one
973function does tell you that that function used an insignificant fraction of
974the whole program's time. Usually this means it is not worth optimizing.
975
976One way to get more accuracy is to give your program more (but similar)
977input data so it will take longer. Another way is to combine the data from
978several runs, using the @samp{-s} option of @code{gprof}. Here is how:
979
980@enumerate
981@item
982Run your program once.
983
984@item
985Issue the command @samp{mv gmon.out gmon.sum}.
986
987@item
988Run your program again, the same as before.
989
990@item
991Merge the new data in @file{gmon.out} into @file{gmon.sum} with this command:
992
993@example
994gprof -s @var{executable-file} gmon.out gmon.sum
995@end example
996
997@item
998Repeat the last two steps as often as you wish.
999
1000@item
1001Analyze the cumulative data using this command:
1002
1003@example
1004gprof @var{executable-file} gmon.sum > @var{output-file}
1005@end example
1006@end enumerate
1007
1008@node Assumptions, Incompatibilities, Sampling Error, Top
1009@chapter Estimating @code{children} Times Uses an Assumption
1010
1011Some of the figures in the call graph are estimates---for example, the
1012@code{children} time values and all the the time figures in caller and
1013subroutine lines.
1014
1015There is no direct information about these measurements in the profile
1016data itself. Instead, @code{gprof} estimates them by making an assumption
1017about your program that might or might not be true.
1018
1019The assumption made is that the average time spent in each call to any
1020function @code{foo} is not correlated with who called @code{foo}. If
1021@code{foo} used 5 seconds in all, and 2/5 of the calls to @code{foo} came
1022from @code{a}, then @code{foo} contributes 2 seconds to @code{a}'s
1023@code{children} time, by assumption.
1024
1025This assumption is usually true enough, but for some programs it is far
1026from true. Suppose that @code{foo} returns very quickly when its argument
1027is zero; suppose that @code{a} always passes zero as an argument, while
1028other callers of @code{foo} pass other arguments. In this program, all the
1029time spent in @code{foo} is in the calls from callers other than @code{a}.
1030But @code{gprof} has no way of knowing this; it will blindly and
1031incorrectly charge 2 seconds of time in @code{foo} to the children of
1032@code{a}.
1033
1034@c FIXME - has this been fixed?
1035We hope some day to put more complete data into @file{gmon.out}, so that
1036this assumption is no longer needed, if we can figure out how. For the
1037nonce, the estimated figures are usually more useful than misleading.
1038
1039@node Incompatibilities, , Assumptions, Top
1040@chapter Incompatibilities with Unix @code{gprof}
1041
1042@sc{gnu} @code{gprof} and Berkeley Unix @code{gprof} use the same data
1043file @file{gmon.out}, and provide essentially the same information. But
1044there are a few differences.
1045
1046@itemize @bullet
1047@item
1048For a recursive function, Unix @code{gprof} lists the function as a
1049parent and as a child, with a @code{calls} field that lists the number
1050of recursive calls. @sc{gnu} @code{gprof} omits these lines and puts
1051the number of recursive calls in the primary line.
1052
1053@item
1054When a function is suppressed from the call graph with @samp{-e}, @sc{gnu}
1055@code{gprof} still lists it as a subroutine of functions that call it.
1056
1057@ignore - it does this now
1058@item
1059The function names printed in @sc{gnu} @code{gprof} output do not include
1060the leading underscores that are added internally to the front of all
1061C identifiers on many operating systems.
1062@end ignore
1063
1064@item
1065The blurbs, field widths, and output formats are different. @sc{gnu}
1066@code{gprof} prints blurbs after the tables, so that you can see the
1067tables without skipping the blurbs.
1068
1069@contents
1070@bye
1071
1072NEEDS AN INDEX
1073
1074Still relevant?
1075 The @file{gmon.out} file is written in the program's @emph{current working
1076 directory} at the time it exits. This means that if your program calls
1077 @code{chdir}, the @file{gmon.out} file will be left in the last directory
1078 your program @code{chdir}'d to. If you don't have permission to write in
1079 this directory, the file is not written. You may get a confusing error
1080 message if this happens. (We have not yet replaced the part of Unix
1081 responsible for this; when we do, we will make the error message
1082 comprehensible.)
1083
1084-k from to...?
1085
1086-d debugging...? should this be documented?
1087
1088-T - "traditional BSD style": How is it different? Should the
1089differences be documented?
1090
1091what is this about? (and to think, I *wrote* it...)
1092 @item -c
1093 The @samp{-c} option causes the static call-graph of the program to be
1094 discovered by a heuristic which examines the text space of the object
1095 file. Static-only parents or children are indicated with call counts of
1096 @samp{0}.
1097
1098example flat file adds up to 100.01%...
1099
1100note: time estimates now only go out to one decimal place (0.0), where
1101they used to extend two (78.67).
This page took 0.17177 seconds and 4 git commands to generate.