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