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