1 This is .././gprof/gprof.info, produced by makeinfo version 4.8 from 2 .././gprof/gprof.texi. 3 4 START-INFO-DIR-ENTRY 5 * gprof: (gprof). Profiling your program's execution 6 END-INFO-DIR-ENTRY 7 8 This file documents the gprof profiler of the GNU system. 9 10 Copyright (C) 1988, 92, 97, 98, 99, 2000, 2001, 2003 Free Software 11 Foundation, Inc. 12 13 Permission is granted to copy, distribute and/or modify this document 14 under the terms of the GNU Free Documentation License, Version 1.1 or 15 any later version published by the Free Software Foundation; with no 16 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover 17 Texts. A copy of the license is included in the section entitled "GNU 18 Free Documentation License". 19 20 21 File: gprof.info, Node: Top, Next: Introduction, Up: (dir) 22 23 Profiling a Program: Where Does It Spend Its Time? 24 ************************************************** 25 26 This manual describes the GNU profiler, `gprof', and how you can use it 27 to determine which parts of a program are taking most of the execution 28 time. We assume that you know how to write, compile, and execute 29 programs. GNU `gprof' was written by Jay Fenlason. 30 31 This document is distributed under the terms of the GNU Free 32 Documentation License. A copy of the license is included in the 33 section entitled "GNU Free Documentation License". 34 35 * Menu: 36 37 * Introduction:: What profiling means, and why it is useful. 38 39 * Compiling:: How to compile your program for profiling. 40 * Executing:: Executing your program to generate profile data 41 * Invoking:: How to run `gprof', and its options 42 43 * Output:: Interpreting `gprof''s output 44 45 * Inaccuracy:: Potential problems you should be aware of 46 * How do I?:: Answers to common questions 47 * Incompatibilities:: (between GNU `gprof' and Unix `gprof'.) 48 * Details:: Details of how profiling is done 49 * GNU Free Documentation License:: GNU Free Documentation License 50 51 52 File: gprof.info, Node: Introduction, Next: Compiling, Prev: Top, Up: Top 53 54 1 Introduction to Profiling 55 *************************** 56 57 Profiling allows you to learn where your program spent its time and 58 which functions called which other functions while it was executing. 59 This information can show you which pieces of your program are slower 60 than you expected, and might be candidates for rewriting to make your 61 program execute faster. It can also tell you which functions are being 62 called more or less often than you expected. This may help you spot 63 bugs that had otherwise been unnoticed. 64 65 Since the profiler uses information collected during the actual 66 execution of your program, it can be used on programs that are too 67 large or too complex to analyze by reading the source. However, how 68 your program is run will affect the information that shows up in the 69 profile data. If you don't use some feature of your program while it 70 is being profiled, no profile information will be generated for that 71 feature. 72 73 Profiling has several steps: 74 75 * You must compile and link your program with profiling enabled. 76 *Note Compiling::. 77 78 * You must execute your program to generate a profile data file. 79 *Note Executing::. 80 81 * You must run `gprof' to analyze the profile data. *Note 82 Invoking::. 83 84 The next three chapters explain these steps in greater detail. 85 86 Several forms of output are available from the analysis. 87 88 The "flat profile" shows how much time your program spent in each 89 function, and how many times that function was called. If you simply 90 want to know which functions burn most of the cycles, it is stated 91 concisely here. *Note Flat Profile::. 92 93 The "call graph" shows, for each function, which functions called 94 it, which other functions it called, and how many times. There is also 95 an estimate of how much time was spent in the subroutines of each 96 function. This can suggest places where you might try to eliminate 97 function calls that use a lot of time. *Note Call Graph::. 98 99 The "annotated source" listing is a copy of the program's source 100 code, labeled with the number of times each line of the program was 101 executed. *Note Annotated Source::. 102 103 To better understand how profiling works, you may wish to read a 104 description of its implementation. *Note Implementation::. 105 106 107 File: gprof.info, Node: Compiling, Next: Executing, Prev: Introduction, Up: Top 108 109 2 Compiling a Program for Profiling 110 *********************************** 111 112 The first step in generating profile information for your program is to 113 compile and link it with profiling enabled. 114 115 To compile a source file for profiling, specify the `-pg' option when 116 you run the compiler. (This is in addition to the options you normally 117 use.) 118 119 To link the program for profiling, if you use a compiler such as `cc' 120 to do the linking, simply specify `-pg' in addition to your usual 121 options. The same option, `-pg', alters either compilation or linking 122 to do what is necessary for profiling. Here are examples: 123 124 cc -g -c myprog.c utils.c -pg 125 cc -o myprog myprog.o utils.o -pg 126 127 The `-pg' option also works with a command that both compiles and 128 links: 129 130 cc -o myprog myprog.c utils.c -g -pg 131 132 Note: The `-pg' option must be part of your compilation options as 133 well as your link options. If it is not then no call-graph data will 134 be gathered and when you run `gprof' you will get an error message like 135 this: 136 137 gprof: gmon.out file is missing call-graph data 138 139 If you add the `-Q' switch to suppress the printing of the call 140 graph data you will still be able to see the time samples: 141 142 Flat profile: 143 144 Each sample counts as 0.01 seconds. 145 % cumulative self self total 146 time seconds seconds calls Ts/call Ts/call name 147 44.12 0.07 0.07 zazLoop 148 35.29 0.14 0.06 main 149 20.59 0.17 0.04 bazMillion 150 151 % the percentage of the total running time of the 152 153 If you run the linker `ld' directly instead of through a compiler 154 such as `cc', you may have to specify a profiling startup file 155 `gcrt0.o' as the first input file instead of the usual startup file 156 `crt0.o'. In addition, you would probably want to specify the 157 profiling C library, `libc_p.a', by writing `-lc_p' instead of the 158 usual `-lc'. This is not absolutely necessary, but doing this gives 159 you number-of-calls information for standard library functions such as 160 `read' and `open'. For example: 161 162 ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p 163 164 If you compile only some of the modules of the program with `-pg', 165 you can still profile the program, but you won't get complete 166 information about the modules that were compiled without `-pg'. The 167 only information you get for the functions in those modules is the 168 total time spent in them; there is no record of how many times they 169 were called, or from where. This will not affect the flat profile 170 (except that the `calls' field for the functions will be blank), but 171 will greatly reduce the usefulness of the call graph. 172 173 If you wish to perform line-by-line profiling, you will also need to 174 specify the `-g' option, instructing the compiler to insert debugging 175 symbols into the program that match program addresses to source code 176 lines. *Note Line-by-line::. 177 178 In addition to the `-pg' and `-g' options, older versions of GCC 179 required you to specify the `-a' option when compiling in order to 180 instrument it to perform basic-block counting. Newer versions do not 181 require this option and will not accept it; basic-block counting is 182 always enabled when `-pg' is on. 183 184 When basic-block counting is enabled, as the program runs it will 185 count how many times it executed each branch of each `if' statement, 186 each iteration of each `do' loop, etc. This will enable `gprof' to 187 construct an annotated source code listing showing how many times each 188 line of code was executed. 189 190 It also worth noting that GCC supports a different profiling method 191 which is enabled by the `-fprofile-arcs', `-ftest-coverage' and 192 `-fprofile-values' switches. These switches do not produce data which 193 is useful to `gprof' however, so they are not discussed further here. 194 There is also the `-finstrument-functions' switch which will cause GCC 195 to insert calls to special user supplied instrumentation routines at 196 the entry and exit of every function in their program. This can be 197 used to implement an alternative profiling scheme. 198 199 200 File: gprof.info, Node: Executing, Next: Invoking, Prev: Compiling, Up: Top 201 202 3 Executing the Program 203 *********************** 204 205 Once the program is compiled for profiling, you must run it in order to 206 generate the information that `gprof' needs. Simply run the program as 207 usual, using the normal arguments, file names, etc. The program should 208 run normally, producing the same output as usual. It will, however, run 209 somewhat slower than normal because of the time spent collecting and the 210 writing the profile data. 211 212 The way you run the program--the arguments and input that you give 213 it--may have a dramatic effect on what the profile information shows. 214 The profile data will describe the parts of the program that were 215 activated for the particular input you use. For example, if the first 216 command you give to your program is to quit, the profile data will show 217 the time used in initialization and in cleanup, but not much else. 218 219 Your program will write the profile data into a file called 220 `gmon.out' just before exiting. If there is already a file called 221 `gmon.out', its contents are overwritten. There is currently no way to 222 tell the program to write the profile data under a different name, but 223 you can rename the file afterwards if you are concerned that it may be 224 overwritten. 225 226 In order to write the `gmon.out' file properly, your program must 227 exit normally: by returning from `main' or by calling `exit'. Calling 228 the low-level function `_exit' does not write the profile data, and 229 neither does abnormal termination due to an unhandled signal. 230 231 The `gmon.out' file is written in the program's _current working 232 directory_ at the time it exits. This means that if your program calls 233 `chdir', the `gmon.out' file will be left in the last directory your 234 program `chdir''d to. If you don't have permission to write in this 235 directory, the file is not written, and you will get an error message. 236 237 Older versions of the GNU profiling library may also write a file 238 called `bb.out'. This file, if present, contains an human-readable 239 listing of the basic-block execution counts. Unfortunately, the 240 appearance of a human-readable `bb.out' means the basic-block counts 241 didn't get written into `gmon.out'. The Perl script `bbconv.pl', 242 included with the `gprof' source distribution, will convert a `bb.out' 243 file into a format readable by `gprof'. Invoke it like this: 244 245 bbconv.pl < bb.out > BH-DATA 246 247 This translates the information in `bb.out' into a form that `gprof' 248 can understand. But you still need to tell `gprof' about the existence 249 of this translated information. To do that, include BB-DATA on the 250 `gprof' command line, _along with `gmon.out'_, like this: 251 252 gprof OPTIONS EXECUTABLE-FILE gmon.out BB-DATA [YET-MORE-PROFILE-DATA-FILES...] [> OUTFILE] 253 254 255 File: gprof.info, Node: Invoking, Next: Output, Prev: Executing, Up: Top 256 257 4 `gprof' Command Summary 258 ************************* 259 260 After you have a profile data file `gmon.out', you can run `gprof' to 261 interpret the information in it. The `gprof' program prints a flat 262 profile and a call graph on standard output. Typically you would 263 redirect the output of `gprof' into a file with `>'. 264 265 You run `gprof' like this: 266 267 gprof OPTIONS [EXECUTABLE-FILE [PROFILE-DATA-FILES...]] [> OUTFILE] 268 269 Here square-brackets indicate optional arguments. 270 271 If you omit the executable file name, the file `a.out' is used. If 272 you give no profile data file name, the file `gmon.out' is used. If 273 any file is not in the proper format, or if the profile data file does 274 not appear to belong to the executable file, an error message is 275 printed. 276 277 You can give more than one profile data file by entering all their 278 names after the executable file name; then the statistics in all the 279 data files are summed together. 280 281 The order of these options does not matter. 282 283 * Menu: 284 285 * Output Options:: Controlling `gprof''s output style 286 * Analysis Options:: Controlling how `gprof' analyses its data 287 * Miscellaneous Options:: 288 * Deprecated Options:: Options you no longer need to use, but which 289 have been retained for compatibility 290 * Symspecs:: Specifying functions to include or exclude 291 292 293 File: gprof.info, Node: Output Options, Next: Analysis Options, Up: Invoking 294 295 4.1 Output Options 296 ================== 297 298 These options specify which of several output formats `gprof' should 299 produce. 300 301 Many of these options take an optional "symspec" to specify 302 functions to be included or excluded. These options can be specified 303 multiple times, with different symspecs, to include or exclude sets of 304 symbols. *Note Symspecs::. 305 306 Specifying any of these options overrides the default (`-p -q'), 307 which prints a flat profile and call graph analysis for all functions. 308 309 `-A[SYMSPEC]' 310 `--annotated-source[=SYMSPEC]' 311 The `-A' option causes `gprof' to print annotated source code. If 312 SYMSPEC is specified, print output only for matching symbols. 313 *Note Annotated Source::. 314 315 `-b' 316 `--brief' 317 If the `-b' option is given, `gprof' doesn't print the verbose 318 blurbs that try to explain the meaning of all of the fields in the 319 tables. This is useful if you intend to print out the output, or 320 are tired of seeing the blurbs. 321 322 `-C[SYMSPEC]' 323 `--exec-counts[=SYMSPEC]' 324 The `-C' option causes `gprof' to print a tally of functions and 325 the number of times each was called. If SYMSPEC is specified, 326 print tally only for matching symbols. 327 328 If the profile data file contains basic-block count records, 329 specifying the `-l' option, along with `-C', will cause basic-block 330 execution counts to be tallied and displayed. 331 332 `-i' 333 `--file-info' 334 The `-i' option causes `gprof' to display summary information 335 about the profile data file(s) and then exit. The number of 336 histogram, call graph, and basic-block count records is displayed. 337 338 `-I DIRS' 339 `--directory-path=DIRS' 340 The `-I' option specifies a list of search directories in which to 341 find source files. Environment variable GPROF_PATH can also be 342 used to convey this information. Used mostly for annotated source 343 output. 344 345 `-J[SYMSPEC]' 346 `--no-annotated-source[=SYMSPEC]' 347 The `-J' option causes `gprof' not to print annotated source code. 348 If SYMSPEC is specified, `gprof' prints annotated source, but 349 excludes matching symbols. 350 351 `-L' 352 `--print-path' 353 Normally, source filenames are printed with the path component 354 suppressed. The `-L' option causes `gprof' to print the full 355 pathname of source filenames, which is determined from symbolic 356 debugging information in the image file and is relative to the 357 directory in which the compiler was invoked. 358 359 `-p[SYMSPEC]' 360 `--flat-profile[=SYMSPEC]' 361 The `-p' option causes `gprof' to print a flat profile. If 362 SYMSPEC is specified, print flat profile only for matching symbols. 363 *Note Flat Profile::. 364 365 `-P[SYMSPEC]' 366 `--no-flat-profile[=SYMSPEC]' 367 The `-P' option causes `gprof' to suppress printing a flat profile. 368 If SYMSPEC is specified, `gprof' prints a flat profile, but 369 excludes matching symbols. 370 371 `-q[SYMSPEC]' 372 `--graph[=SYMSPEC]' 373 The `-q' option causes `gprof' to print the call graph analysis. 374 If SYMSPEC is specified, print call graph only for matching symbols 375 and their children. *Note Call Graph::. 376 377 `-Q[SYMSPEC]' 378 `--no-graph[=SYMSPEC]' 379 The `-Q' option causes `gprof' to suppress printing the call graph. 380 If SYMSPEC is specified, `gprof' prints a call graph, but excludes 381 matching symbols. 382 383 `-t' 384 `--table-length=NUM' 385 The `-t' option causes the NUM most active source lines in each 386 source file to be listed when source annotation is enabled. The 387 default is 10. 388 389 `-y' 390 `--separate-files' 391 This option affects annotated source output only. Normally, 392 `gprof' prints annotated source files to standard-output. If this 393 option is specified, annotated source for a file named 394 `path/FILENAME' is generated in the file `FILENAME-ann'. If the 395 underlying filesystem would truncate `FILENAME-ann' so that it 396 overwrites the original `FILENAME', `gprof' generates annotated 397 source in the file `FILENAME.ann' instead (if the original file 398 name has an extension, that extension is _replaced_ with `.ann'). 399 400 `-Z[SYMSPEC]' 401 `--no-exec-counts[=SYMSPEC]' 402 The `-Z' option causes `gprof' not to print a tally of functions 403 and the number of times each was called. If SYMSPEC is specified, 404 print tally, but exclude matching symbols. 405 406 `-r' 407 `--function-ordering' 408 The `--function-ordering' option causes `gprof' to print a 409 suggested function ordering for the program based on profiling 410 data. This option suggests an ordering which may improve paging, 411 tlb and cache behavior for the program on systems which support 412 arbitrary ordering of functions in an executable. 413 414 The exact details of how to force the linker to place functions in 415 a particular order is system dependent and out of the scope of this 416 manual. 417 418 `-R MAP_FILE' 419 `--file-ordering MAP_FILE' 420 The `--file-ordering' option causes `gprof' to print a suggested 421 .o link line ordering for the program based on profiling data. 422 This option suggests an ordering which may improve paging, tlb and 423 cache behavior for the program on systems which do not support 424 arbitrary ordering of functions in an executable. 425 426 Use of the `-a' argument is highly recommended with this option. 427 428 The MAP_FILE argument is a pathname to a file which provides 429 function name to object file mappings. The format of the file is 430 similar to the output of the program `nm'. 431 432 c-parse.o:00000000 T yyparse 433 c-parse.o:00000004 C yyerrflag 434 c-lang.o:00000000 T maybe_objc_method_name 435 c-lang.o:00000000 T print_lang_statistics 436 c-lang.o:00000000 T recognize_objc_keyword 437 c-decl.o:00000000 T print_lang_identifier 438 c-decl.o:00000000 T print_lang_type 439 ... 440 441 To create a MAP_FILE with GNU `nm', type a command like `nm 442 --extern-only --defined-only -v --print-file-name program-name'. 443 444 `-T' 445 `--traditional' 446 The `-T' option causes `gprof' to print its output in 447 "traditional" BSD style. 448 449 `-w WIDTH' 450 `--width=WIDTH' 451 Sets width of output lines to WIDTH. Currently only used when 452 printing the function index at the bottom of the call graph. 453 454 `-x' 455 `--all-lines' 456 This option affects annotated source output only. By default, 457 only the lines at the beginning of a basic-block are annotated. 458 If this option is specified, every line in a basic-block is 459 annotated by repeating the annotation for the first line. This 460 behavior is similar to `tcov''s `-a'. 461 462 `--demangle[=STYLE]' 463 `--no-demangle' 464 These options control whether C++ symbol names should be demangled 465 when printing output. The default is to demangle symbols. The 466 `--no-demangle' option may be used to turn off demangling. 467 Different compilers have different mangling styles. The optional 468 demangling style argument can be used to choose an appropriate 469 demangling style for your compiler. 470 471 472 File: gprof.info, Node: Analysis Options, Next: Miscellaneous Options, Prev: Output Options, Up: Invoking 473 474 4.2 Analysis Options 475 ==================== 476 477 `-a' 478 `--no-static' 479 The `-a' option causes `gprof' to suppress the printing of 480 statically declared (private) functions. (These are functions 481 whose names are not listed as global, and which are not visible 482 outside the file/function/block where they were defined.) Time 483 spent in these functions, calls to/from them, etc, will all be 484 attributed to the function that was loaded directly before it in 485 the executable file. This option affects both the flat profile 486 and the call graph. 487 488 `-c' 489 `--static-call-graph' 490 The `-c' option causes the call graph of the program to be 491 augmented by a heuristic which examines the text space of the 492 object file and identifies function calls in the binary machine 493 code. Since normal call graph records are only generated when 494 functions are entered, this option identifies children that could 495 have been called, but never were. Calls to functions that were 496 not compiled with profiling enabled are also identified, but only 497 if symbol table entries are present for them. Calls to dynamic 498 library routines are typically _not_ found by this option. 499 Parents or children identified via this heuristic are indicated in 500 the call graph with call counts of `0'. 501 502 `-D' 503 `--ignore-non-functions' 504 The `-D' option causes `gprof' to ignore symbols which are not 505 known to be functions. This option will give more accurate 506 profile data on systems where it is supported (Solaris and HPUX for 507 example). 508 509 `-k FROM/TO' 510 The `-k' option allows you to delete from the call graph any arcs 511 from symbols matching symspec FROM to those matching symspec TO. 512 513 `-l' 514 `--line' 515 The `-l' option enables line-by-line profiling, which causes 516 histogram hits to be charged to individual source code lines, 517 instead of functions. If the program was compiled with 518 basic-block counting enabled, this option will also identify how 519 many times each line of code was executed. While line-by-line 520 profiling can help isolate where in a large function a program is 521 spending its time, it also significantly increases the running 522 time of `gprof', and magnifies statistical inaccuracies. *Note 523 Sampling Error::. 524 525 `-m NUM' 526 `--min-count=NUM' 527 This option affects execution count output only. Symbols that are 528 executed less than NUM times are suppressed. 529 530 `-n[SYMSPEC]' 531 `--time[=SYMSPEC]' 532 The `-n' option causes `gprof', in its call graph analysis, to 533 only propagate times for symbols matching SYMSPEC. 534 535 `-N[SYMSPEC]' 536 `--no-time[=SYMSPEC]' 537 The `-n' option causes `gprof', in its call graph analysis, not to 538 propagate times for symbols matching SYMSPEC. 539 540 `-z' 541 `--display-unused-functions' 542 If you give the `-z' option, `gprof' will mention all functions in 543 the flat profile, even those that were never called, and that had 544 no time spent in them. This is useful in conjunction with the 545 `-c' option for discovering which routines were never called. 546 547 548 549 File: gprof.info, Node: Miscellaneous Options, Next: Deprecated Options, Prev: Analysis Options, Up: Invoking 550 551 4.3 Miscellaneous Options 552 ========================= 553 554 `-d[NUM]' 555 `--debug[=NUM]' 556 The `-d NUM' option specifies debugging options. If NUM is not 557 specified, enable all debugging. *Note Debugging::. 558 559 `-h' 560 `--help' 561 The `-h' option prints command line usage. 562 563 `-ONAME' 564 `--file-format=NAME' 565 Selects the format of the profile data files. Recognized formats 566 are `auto' (the default), `bsd', `4.4bsd', `magic', and `prof' 567 (not yet supported). 568 569 `-s' 570 `--sum' 571 The `-s' option causes `gprof' to summarize the information in the 572 profile data files it read in, and write out a profile data file 573 called `gmon.sum', which contains all the information from the 574 profile data files that `gprof' read in. The file `gmon.sum' may 575 be one of the specified input files; the effect of this is to 576 merge the data in the other input files into `gmon.sum'. 577 578 Eventually you can run `gprof' again without `-s' to analyze the 579 cumulative data in the file `gmon.sum'. 580 581 `-v' 582 `--version' 583 The `-v' flag causes `gprof' to print the current version number, 584 and then exit. 585 586 587 588 File: gprof.info, Node: Deprecated Options, Next: Symspecs, Prev: Miscellaneous Options, Up: Invoking 589 590 4.4 Deprecated Options 591 ====================== 592 593 These options have been replaced with newer versions that use 594 symspecs. 595 596 `-e FUNCTION_NAME' 597 The `-e FUNCTION' option tells `gprof' to not print information 598 about the function FUNCTION_NAME (and its children...) in the call 599 graph. The function will still be listed as a child of any 600 functions that call it, but its index number will be shown as 601 `[not printed]'. More than one `-e' option may be given; only one 602 FUNCTION_NAME may be indicated with each `-e' option. 603 604 `-E FUNCTION_NAME' 605 The `-E FUNCTION' option works like the `-e' option, but time 606 spent in the function (and children who were not called from 607 anywhere else), will not be used to compute the 608 percentages-of-time for the call graph. More than one `-E' option 609 may be given; only one FUNCTION_NAME may be indicated with each 610 `-E' option. 611 612 `-f FUNCTION_NAME' 613 The `-f FUNCTION' option causes `gprof' to limit the call graph to 614 the function FUNCTION_NAME and its children (and their 615 children...). More than one `-f' option may be given; only one 616 FUNCTION_NAME may be indicated with each `-f' option. 617 618 `-F FUNCTION_NAME' 619 The `-F FUNCTION' option works like the `-f' option, but only time 620 spent in the function and its children (and their children...) 621 will be used to determine total-time and percentages-of-time for 622 the call graph. More than one `-F' option may be given; only one 623 FUNCTION_NAME may be indicated with each `-F' option. The `-F' 624 option overrides the `-E' option. 625 626 627 Note that only one function can be specified with each `-e', `-E', 628 `-f' or `-F' option. To specify more than one function, use multiple 629 options. For example, this command: 630 631 gprof -e boring -f foo -f bar myprogram > gprof.output 632 633 lists in the call graph all functions that were reached from either 634 `foo' or `bar' and were not reachable from `boring'. 635 636 637 File: gprof.info, Node: Symspecs, Prev: Deprecated Options, Up: Invoking 638 639 4.5 Symspecs 640 ============ 641 642 Many of the output options allow functions to be included or excluded 643 using "symspecs" (symbol specifications), which observe the following 644 syntax: 645 646 filename_containing_a_dot 647 | funcname_not_containing_a_dot 648 | linenumber 649 | ( [ any_filename ] `:' ( any_funcname | linenumber ) ) 650 651 Here are some sample symspecs: 652 653 `main.c' 654 Selects everything in file `main.c'--the dot in the string tells 655 `gprof' to interpret the string as a filename, rather than as a 656 function name. To select a file whose name does not contain a 657 dot, a trailing colon should be specified. For example, `odd:' is 658 interpreted as the file named `odd'. 659 660 `main' 661 Selects all functions named `main'. 662 663 Note that there may be multiple instances of the same function name 664 because some of the definitions may be local (i.e., static). 665 Unless a function name is unique in a program, you must use the 666 colon notation explained below to specify a function from a 667 specific source file. 668 669 Sometimes, function names contain dots. In such cases, it is 670 necessary to add a leading colon to the name. For example, 671 `:.mul' selects function `.mul'. 672 673 In some object file formats, symbols have a leading underscore. 674 `gprof' will normally not print these underscores. When you name a 675 symbol in a symspec, you should type it exactly as `gprof' prints 676 it in its output. For example, if the compiler produces a symbol 677 `_main' from your `main' function, `gprof' still prints it as 678 `main' in its output, so you should use `main' in symspecs. 679 680 `main.c:main' 681 Selects function `main' in file `main.c'. 682 683 `main.c:134' 684 Selects line 134 in file `main.c'. 685 686 687 File: gprof.info, Node: Output, Next: Inaccuracy, Prev: Invoking, Up: Top 688 689 5 Interpreting `gprof''s Output 690 ******************************* 691 692 `gprof' can produce several different output styles, the most important 693 of which are described below. The simplest output styles (file 694 information, execution count, and function and file ordering) are not 695 described here, but are documented with the respective options that 696 trigger them. *Note Output Options::. 697 698 * Menu: 699 700 * Flat Profile:: The flat profile shows how much time was spent 701 executing directly in each function. 702 * Call Graph:: The call graph shows which functions called which 703 others, and how much time each function used 704 when its subroutine calls are included. 705 * Line-by-line:: `gprof' can analyze individual source code lines 706 * Annotated Source:: The annotated source listing displays source code 707 labeled with execution counts 708 709 710 File: gprof.info, Node: Flat Profile, Next: Call Graph, Up: Output 711 712 5.1 The Flat Profile 713 ==================== 714 715 The "flat profile" shows the total amount of time your program spent 716 executing each function. Unless the `-z' option is given, functions 717 with no apparent time spent in them, and no apparent calls to them, are 718 not mentioned. Note that if a function was not compiled for profiling, 719 and didn't run long enough to show up on the program counter histogram, 720 it will be indistinguishable from a function that was never called. 721 722 This is part of a flat profile for a small program: 723 724 Flat profile: 725 726 Each sample counts as 0.01 seconds. 727 % cumulative self self total 728 time seconds seconds calls ms/call ms/call name 729 33.34 0.02 0.02 7208 0.00 0.00 open 730 16.67 0.03 0.01 244 0.04 0.12 offtime 731 16.67 0.04 0.01 8 1.25 1.25 memccpy 732 16.67 0.05 0.01 7 1.43 1.43 write 733 16.67 0.06 0.01 mcount 734 0.00 0.06 0.00 236 0.00 0.00 tzset 735 0.00 0.06 0.00 192 0.00 0.00 tolower 736 0.00 0.06 0.00 47 0.00 0.00 strlen 737 0.00 0.06 0.00 45 0.00 0.00 strchr 738 0.00 0.06 0.00 1 0.00 50.00 main 739 0.00 0.06 0.00 1 0.00 0.00 memcpy 740 0.00 0.06 0.00 1 0.00 10.11 print 741 0.00 0.06 0.00 1 0.00 0.00 profil 742 0.00 0.06 0.00 1 0.00 50.00 report 743 ... 744 745 The functions are sorted by first by decreasing run-time spent in them, 746 then by decreasing number of calls, then alphabetically by name. The 747 functions `mcount' and `profil' are part of the profiling apparatus and 748 appear in every flat profile; their time gives a measure of the amount 749 of overhead due to profiling. 750 751 Just before the column headers, a statement appears indicating how 752 much time each sample counted as. This "sampling period" estimates the 753 margin of error in each of the time figures. A time figure that is not 754 much larger than this is not reliable. In this example, each sample 755 counted as 0.01 seconds, suggesting a 100 Hz sampling rate. The 756 program's total execution time was 0.06 seconds, as indicated by the 757 `cumulative seconds' field. Since each sample counted for 0.01 758 seconds, this means only six samples were taken during the run. Two of 759 the samples occurred while the program was in the `open' function, as 760 indicated by the `self seconds' field. Each of the other four samples 761 occurred one each in `offtime', `memccpy', `write', and `mcount'. 762 Since only six samples were taken, none of these values can be regarded 763 as particularly reliable. In another run, the `self seconds' field for 764 `mcount' might well be `0.00' or `0.02'. *Note Sampling Error::, for a 765 complete discussion. 766 767 The remaining functions in the listing (those whose `self seconds' 768 field is `0.00') didn't appear in the histogram samples at all. 769 However, the call graph indicated that they were called, so therefore 770 they are listed, sorted in decreasing order by the `calls' field. 771 Clearly some time was spent executing these functions, but the paucity 772 of histogram samples prevents any determination of how much time each 773 took. 774 775 Here is what the fields in each line mean: 776 777 `% time' 778 This is the percentage of the total execution time your program 779 spent in this function. These should all add up to 100%. 780 781 `cumulative seconds' 782 This is the cumulative total number of seconds the computer spent 783 executing this functions, plus the time spent in all the functions 784 above this one in this table. 785 786 `self seconds' 787 This is the number of seconds accounted for by this function alone. 788 The flat profile listing is sorted first by this number. 789 790 `calls' 791 This is the total number of times the function was called. If the 792 function was never called, or the number of times it was called 793 cannot be determined (probably because the function was not 794 compiled with profiling enabled), the "calls" field is blank. 795 796 `self ms/call' 797 This represents the average number of milliseconds spent in this 798 function per call, if this function is profiled. Otherwise, this 799 field is blank for this function. 800 801 `total ms/call' 802 This represents the average number of milliseconds spent in this 803 function and its descendants per call, if this function is 804 profiled. Otherwise, this field is blank for this function. This 805 is the only field in the flat profile that uses call graph 806 analysis. 807 808 `name' 809 This is the name of the function. The flat profile is sorted by 810 this field alphabetically after the "self seconds" and "calls" 811 fields are sorted. 812 813 814 File: gprof.info, Node: Call Graph, Next: Line-by-line, Prev: Flat Profile, Up: Output 815 816 5.2 The Call Graph 817 ================== 818 819 The "call graph" shows how much time was spent in each function and its 820 children. From this information, you can find functions that, while 821 they themselves may not have used much time, called other functions 822 that did use unusual amounts of time. 823 824 Here is a sample call from a small program. This call came from the 825 same `gprof' run as the flat profile example in the previous chapter. 826 827 granularity: each sample hit covers 2 byte(s) for 20.00% of 0.05 seconds 828 829 index % time self children called name 830 <spontaneous> 831 [1] 100.0 0.00 0.05 start [1] 832 0.00 0.05 1/1 main [2] 833 0.00 0.00 1/2 on_exit [28] 834 0.00 0.00 1/1 exit [59] 835 ----------------------------------------------- 836 0.00 0.05 1/1 start [1] 837 [2] 100.0 0.00 0.05 1 main [2] 838 0.00 0.05 1/1 report [3] 839 ----------------------------------------------- 840 0.00 0.05 1/1 main [2] 841 [3] 100.0 0.00 0.05 1 report [3] 842 0.00 0.03 8/8 timelocal [6] 843 0.00 0.01 1/1 print [9] 844 0.00 0.01 9/9 fgets [12] 845 0.00 0.00 12/34 strncmp <cycle 1> [40] 846 0.00 0.00 8/8 lookup [20] 847 0.00 0.00 1/1 fopen [21] 848 0.00 0.00 8/8 chewtime [24] 849 0.00 0.00 8/16 skipspace [44] 850 ----------------------------------------------- 851 [4] 59.8 0.01 0.02 8+472 <cycle 2 as a whole> [4] 852 0.01 0.02 244+260 offtime <cycle 2> [7] 853 0.00 0.00 236+1 tzset <cycle 2> [26] 854 ----------------------------------------------- 855 856 The lines full of dashes divide this table into "entries", one for 857 each function. Each entry has one or more lines. 858 859 In each entry, the primary line is the one that starts with an index 860 number in square brackets. The end of this line says which function 861 the entry is for. The preceding lines in the entry describe the 862 callers of this function and the following lines describe its 863 subroutines (also called "children" when we speak of the call graph). 864 865 The entries are sorted by time spent in the function and its 866 subroutines. 867 868 The internal profiling function `mcount' (*note Flat Profile::) is 869 never mentioned in the call graph. 870 871 * Menu: 872 873 * Primary:: Details of the primary line's contents. 874 * Callers:: Details of caller-lines' contents. 875 * Subroutines:: Details of subroutine-lines' contents. 876 * Cycles:: When there are cycles of recursion, 877 such as `a' calls `b' calls `a'... 878 879 880 File: gprof.info, Node: Primary, Next: Callers, Up: Call Graph 881 882 5.2.1 The Primary Line 883 ---------------------- 884 885 The "primary line" in a call graph entry is the line that describes the 886 function which the entry is about and gives the overall statistics for 887 this function. 888 889 For reference, we repeat the primary line from the entry for function 890 `report' in our main example, together with the heading line that shows 891 the names of the fields: 892 893 index % time self children called name 894 ... 895 [3] 100.0 0.00 0.05 1 report [3] 896 897 Here is what the fields in the primary line mean: 898 899 `index' 900 Entries are numbered with consecutive integers. Each function 901 therefore has an index number, which appears at the beginning of 902 its primary line. 903 904 Each cross-reference to a function, as a caller or subroutine of 905 another, gives its index number as well as its name. The index 906 number guides you if you wish to look for the entry for that 907 function. 908 909 `% time' 910 This is the percentage of the total time that was spent in this 911 function, including time spent in subroutines called from this 912 function. 913 914 The time spent in this function is counted again for the callers of 915 this function. Therefore, adding up these percentages is 916 meaningless. 917 918 `self' 919 This is the total amount of time spent in this function. This 920 should be identical to the number printed in the `seconds' field 921 for this function in the flat profile. 922 923 `children' 924 This is the total amount of time spent in the subroutine calls 925 made by this function. This should be equal to the sum of all the 926 `self' and `children' entries of the children listed directly 927 below this function. 928 929 `called' 930 This is the number of times the function was called. 931 932 If the function called itself recursively, there are two numbers, 933 separated by a `+'. The first number counts non-recursive calls, 934 and the second counts recursive calls. 935 936 In the example above, the function `report' was called once from 937 `main'. 938 939 `name' 940 This is the name of the current function. The index number is 941 repeated after it. 942 943 If the function is part of a cycle of recursion, the cycle number 944 is printed between the function's name and the index number (*note 945 Cycles::). For example, if function `gnurr' is part of cycle 946 number one, and has index number twelve, its primary line would be 947 end like this: 948 949 gnurr <cycle 1> [12] 950 951 952 File: gprof.info, Node: Callers, Next: Subroutines, Prev: Primary, Up: Call Graph 953 954 5.2.2 Lines for a Function's Callers 955 ------------------------------------ 956 957 A function's entry has a line for each function it was called by. 958 These lines' fields correspond to the fields of the primary line, but 959 their meanings are different because of the difference in context. 960 961 For reference, we repeat two lines from the entry for the function 962 `report', the primary line and one caller-line preceding it, together 963 with the heading line that shows the names of the fields: 964 965 index % time self children called name 966 ... 967 0.00 0.05 1/1 main [2] 968 [3] 100.0 0.00 0.05 1 report [3] 969 970 Here are the meanings of the fields in the caller-line for `report' 971 called from `main': 972 973 `self' 974 An estimate of the amount of time spent in `report' itself when it 975 was called from `main'. 976 977 `children' 978 An estimate of the amount of time spent in subroutines of `report' 979 when `report' was called from `main'. 980 981 The sum of the `self' and `children' fields is an estimate of the 982 amount of time spent within calls to `report' from `main'. 983 984 `called' 985 Two numbers: the number of times `report' was called from `main', 986 followed by the total number of non-recursive calls to `report' 987 from all its callers. 988 989 `name and index number' 990 The name of the caller of `report' to which this line applies, 991 followed by the caller's index number. 992 993 Not all functions have entries in the call graph; some options to 994 `gprof' request the omission of certain functions. When a caller 995 has no entry of its own, it still has caller-lines in the entries 996 of the functions it calls. 997 998 If the caller is part of a recursion cycle, the cycle number is 999 printed between the name and the index number. 1000 1001 If the identity of the callers of a function cannot be determined, a 1002 dummy caller-line is printed which has `<spontaneous>' as the "caller's 1003 name" and all other fields blank. This can happen for signal handlers. 1004 1005 1006 File: gprof.info, Node: Subroutines, Next: Cycles, Prev: Callers, Up: Call Graph 1007 1008 5.2.3 Lines for a Function's Subroutines 1009 ---------------------------------------- 1010 1011 A function's entry has a line for each of its subroutines--in other 1012 words, a line for each other function that it called. These lines' 1013 fields correspond to the fields of the primary line, but their meanings 1014 are different because of the difference in context. 1015 1016 For reference, we repeat two lines from the entry for the function 1017 `main', the primary line and a line for a subroutine, together with the 1018 heading line that shows the names of the fields: 1019 1020 index % time self children called name 1021 ... 1022 [2] 100.0 0.00 0.05 1 main [2] 1023 0.00 0.05 1/1 report [3] 1024 1025 Here are the meanings of the fields in the subroutine-line for `main' 1026 calling `report': 1027 1028 `self' 1029 An estimate of the amount of time spent directly within `report' 1030 when `report' was called from `main'. 1031 1032 `children' 1033 An estimate of the amount of time spent in subroutines of `report' 1034 when `report' was called from `main'. 1035 1036 The sum of the `self' and `children' fields is an estimate of the 1037 total time spent in calls to `report' from `main'. 1038 1039 `called' 1040 Two numbers, the number of calls to `report' from `main' followed 1041 by the total number of non-recursive calls to `report'. This 1042 ratio is used to determine how much of `report''s `self' and 1043 `children' time gets credited to `main'. *Note Assumptions::. 1044 1045 `name' 1046 The name of the subroutine of `main' to which this line applies, 1047 followed by the subroutine's index number. 1048 1049 If the caller is part of a recursion cycle, the cycle number is 1050 printed between the name and the index number. 1051 1052 1053 File: gprof.info, Node: Cycles, Prev: Subroutines, Up: Call Graph 1054 1055 5.2.4 How Mutually Recursive Functions Are Described 1056 ---------------------------------------------------- 1057 1058 The graph may be complicated by the presence of "cycles of recursion" 1059 in the call graph. A cycle exists if a function calls another function 1060 that (directly or indirectly) calls (or appears to call) the original 1061 function. For example: if `a' calls `b', and `b' calls `a', then `a' 1062 and `b' form a cycle. 1063 1064 Whenever there are call paths both ways between a pair of functions, 1065 they belong to the same cycle. If `a' and `b' call each other and `b' 1066 and `c' call each other, all three make one cycle. Note that even if 1067 `b' only calls `a' if it was not called from `a', `gprof' cannot 1068 determine this, so `a' and `b' are still considered a cycle. 1069 1070 The cycles are numbered with consecutive integers. When a function 1071 belongs to a cycle, each time the function name appears in the call 1072 graph it is followed by `<cycle NUMBER>'. 1073 1074 The reason cycles matter is that they make the time values in the 1075 call graph paradoxical. The "time spent in children" of `a' should 1076 include the time spent in its subroutine `b' and in `b''s 1077 subroutines--but one of `b''s subroutines is `a'! How much of `a''s 1078 time should be included in the children of `a', when `a' is indirectly 1079 recursive? 1080 1081 The way `gprof' resolves this paradox is by creating a single entry 1082 for the cycle as a whole. The primary line of this entry describes the 1083 total time spent directly in the functions of the cycle. The 1084 "subroutines" of the cycle are the individual functions of the cycle, 1085 and all other functions that were called directly by them. The 1086 "callers" of the cycle are the functions, outside the cycle, that 1087 called functions in the cycle. 1088 1089 Here is an example portion of a call graph which shows a cycle 1090 containing functions `a' and `b'. The cycle was entered by a call to 1091 `a' from `main'; both `a' and `b' called `c'. 1092 1093 index % time self children called name 1094 ---------------------------------------- 1095 1.77 0 1/1 main [2] 1096 [3] 91.71 1.77 0 1+5 <cycle 1 as a whole> [3] 1097 1.02 0 3 b <cycle 1> [4] 1098 0.75 0 2 a <cycle 1> [5] 1099 ---------------------------------------- 1100 3 a <cycle 1> [5] 1101 [4] 52.85 1.02 0 0 b <cycle 1> [4] 1102 2 a <cycle 1> [5] 1103 0 0 3/6 c [6] 1104 ---------------------------------------- 1105 1.77 0 1/1 main [2] 1106 2 b <cycle 1> [4] 1107 [5] 38.86 0.75 0 1 a <cycle 1> [5] 1108 3 b <cycle 1> [4] 1109 0 0 3/6 c [6] 1110 ---------------------------------------- 1111 1112 (The entire call graph for this program contains in addition an entry 1113 for `main', which calls `a', and an entry for `c', with callers `a' and 1114 `b'.) 1115 1116 index % time self children called name 1117 <spontaneous> 1118 [1] 100.00 0 1.93 0 start [1] 1119 0.16 1.77 1/1 main [2] 1120 ---------------------------------------- 1121 0.16 1.77 1/1 start [1] 1122 [2] 100.00 0.16 1.77 1 main [2] 1123 1.77 0 1/1 a <cycle 1> [5] 1124 ---------------------------------------- 1125 1.77 0 1/1 main [2] 1126 [3] 91.71 1.77 0 1+5 <cycle 1 as a whole> [3] 1127 1.02 0 3 b <cycle 1> [4] 1128 0.75 0 2 a <cycle 1> [5] 1129 0 0 6/6 c [6] 1130 ---------------------------------------- 1131 3 a <cycle 1> [5] 1132 [4] 52.85 1.02 0 0 b <cycle 1> [4] 1133 2 a <cycle 1> [5] 1134 0 0 3/6 c [6] 1135 ---------------------------------------- 1136 1.77 0 1/1 main [2] 1137 2 b <cycle 1> [4] 1138 [5] 38.86 0.75 0 1 a <cycle 1> [5] 1139 3 b <cycle 1> [4] 1140 0 0 3/6 c [6] 1141 ---------------------------------------- 1142 0 0 3/6 b <cycle 1> [4] 1143 0 0 3/6 a <cycle 1> [5] 1144 [6] 0.00 0 0 6 c [6] 1145 ---------------------------------------- 1146 1147 The `self' field of the cycle's primary line is the total time spent 1148 in all the functions of the cycle. It equals the sum of the `self' 1149 fields for the individual functions in the cycle, found in the entry in 1150 the subroutine lines for these functions. 1151 1152 The `children' fields of the cycle's primary line and subroutine 1153 lines count only subroutines outside the cycle. Even though `a' calls 1154 `b', the time spent in those calls to `b' is not counted in `a''s 1155 `children' time. Thus, we do not encounter the problem of what to do 1156 when the time in those calls to `b' includes indirect recursive calls 1157 back to `a'. 1158 1159 The `children' field of a caller-line in the cycle's entry estimates 1160 the amount of time spent _in the whole cycle_, and its other 1161 subroutines, on the times when that caller called a function in the 1162 cycle. 1163 1164 The `calls' field in the primary line for the cycle has two numbers: 1165 first, the number of times functions in the cycle were called by 1166 functions outside the cycle; second, the number of times they were 1167 called by functions in the cycle (including times when a function in 1168 the cycle calls itself). This is a generalization of the usual split 1169 into non-recursive and recursive calls. 1170 1171 The `calls' field of a subroutine-line for a cycle member in the 1172 cycle's entry says how many time that function was called from 1173 functions in the cycle. The total of all these is the second number in 1174 the primary line's `calls' field. 1175 1176 In the individual entry for a function in a cycle, the other 1177 functions in the same cycle can appear as subroutines and as callers. 1178 These lines show how many times each function in the cycle called or 1179 was called from each other function in the cycle. The `self' and 1180 `children' fields in these lines are blank because of the difficulty of 1181 defining meanings for them when recursion is going on. 1182 1183 1184 File: gprof.info, Node: Line-by-line, Next: Annotated Source, Prev: Call Graph, Up: Output 1185 1186 5.3 Line-by-line Profiling 1187 ========================== 1188 1189 `gprof''s `-l' option causes the program to perform "line-by-line" 1190 profiling. In this mode, histogram samples are assigned not to 1191 functions, but to individual lines of source code. The program usually 1192 must be compiled with a `-g' option, in addition to `-pg', in order to 1193 generate debugging symbols for tracking source code lines. 1194 1195 The flat profile is the most useful output table in line-by-line 1196 mode. The call graph isn't as useful as normal, since the current 1197 version of `gprof' does not propagate call graph arcs from source code 1198 lines to the enclosing function. The call graph does, however, show 1199 each line of code that called each function, along with a count. 1200 1201 Here is a section of `gprof''s output, without line-by-line 1202 profiling. Note that `ct_init' accounted for four histogram hits, and 1203 13327 calls to `init_block'. 1204 1205 Flat profile: 1206 1207 Each sample counts as 0.01 seconds. 1208 % cumulative self self total 1209 time seconds seconds calls us/call us/call name 1210 30.77 0.13 0.04 6335 6.31 6.31 ct_init 1211 1212 1213 Call graph (explanation follows) 1214 1215 1216 granularity: each sample hit covers 4 byte(s) for 7.69% of 0.13 seconds 1217 1218 index % time self children called name 1219 1220 0.00 0.00 1/13496 name_too_long 1221 0.00 0.00 40/13496 deflate 1222 0.00 0.00 128/13496 deflate_fast 1223 0.00 0.00 13327/13496 ct_init 1224 [7] 0.0 0.00 0.00 13496 init_block 1225 1226 Now let's look at some of `gprof''s output from the same program run, 1227 this time with line-by-line profiling enabled. Note that `ct_init''s 1228 four histogram hits are broken down into four lines of source code - 1229 one hit occurred on each of lines 349, 351, 382 and 385. In the call 1230 graph, note how `ct_init''s 13327 calls to `init_block' are broken down 1231 into one call from line 396, 3071 calls from line 384, 3730 calls from 1232 line 385, and 6525 calls from 387. 1233 1234 Flat profile: 1235 1236 Each sample counts as 0.01 seconds. 1237 % cumulative self 1238 time seconds seconds calls name 1239 7.69 0.10 0.01 ct_init (trees.c:349) 1240 7.69 0.11 0.01 ct_init (trees.c:351) 1241 7.69 0.12 0.01 ct_init (trees.c:382) 1242 7.69 0.13 0.01 ct_init (trees.c:385) 1243 1244 1245 Call graph (explanation follows) 1246 1247 1248 granularity: each sample hit covers 4 byte(s) for 7.69% of 0.13 seconds 1249 1250 % time self children called name 1251 1252 0.00 0.00 1/13496 name_too_long (gzip.c:1440) 1253 0.00 0.00 1/13496 deflate (deflate.c:763) 1254 0.00 0.00 1/13496 ct_init (trees.c:396) 1255 0.00 0.00 2/13496 deflate (deflate.c:727) 1256 0.00 0.00 4/13496 deflate (deflate.c:686) 1257 0.00 0.00 5/13496 deflate (deflate.c:675) 1258 0.00 0.00 12/13496 deflate (deflate.c:679) 1259 0.00 0.00 16/13496 deflate (deflate.c:730) 1260 0.00 0.00 128/13496 deflate_fast (deflate.c:654) 1261 0.00 0.00 3071/13496 ct_init (trees.c:384) 1262 0.00 0.00 3730/13496 ct_init (trees.c:385) 1263 0.00 0.00 6525/13496 ct_init (trees.c:387) 1264 [6] 0.0 0.00 0.00 13496 init_block (trees.c:408) 1265 1266 1267 File: gprof.info, Node: Annotated Source, Prev: Line-by-line, Up: Output 1268 1269 5.4 The Annotated Source Listing 1270 ================================ 1271 1272 `gprof''s `-A' option triggers an annotated source listing, which lists 1273 the program's source code, each function labeled with the number of 1274 times it was called. You may also need to specify the `-I' option, if 1275 `gprof' can't find the source code files. 1276 1277 Compiling with `gcc ... -g -pg -a' augments your program with 1278 basic-block counting code, in addition to function counting code. This 1279 enables `gprof' to determine how many times each line of code was 1280 executed. For example, consider the following function, taken from 1281 gzip, with line numbers added: 1282 1283 1 ulg updcrc(s, n) 1284 2 uch *s; 1285 3 unsigned n; 1286 4 { 1287 5 register ulg c; 1288 6 1289 7 static ulg crc = (ulg)0xffffffffL; 1290 8 1291 9 if (s == NULL) { 1292 10 c = 0xffffffffL; 1293 11 } else { 1294 12 c = crc; 1295 13 if (n) do { 1296 14 c = crc_32_tab[...]; 1297 15 } while (--n); 1298 16 } 1299 17 crc = c; 1300 18 return c ^ 0xffffffffL; 1301 19 } 1302 1303 `updcrc' has at least five basic-blocks. One is the function 1304 itself. The `if' statement on line 9 generates two more basic-blocks, 1305 one for each branch of the `if'. A fourth basic-block results from the 1306 `if' on line 13, and the contents of the `do' loop form the fifth 1307 basic-block. The compiler may also generate additional basic-blocks to 1308 handle various special cases. 1309 1310 A program augmented for basic-block counting can be analyzed with 1311 `gprof -l -A'. I also suggest use of the `-x' option, which ensures 1312 that each line of code is labeled at least once. Here is `updcrc''s 1313 annotated source listing for a sample `gzip' run: 1314 1315 ulg updcrc(s, n) 1316 uch *s; 1317 unsigned n; 1318 2 ->{ 1319 register ulg c; 1320 1321 static ulg crc = (ulg)0xffffffffL; 1322 1323 2 -> if (s == NULL) { 1324 1 -> c = 0xffffffffL; 1325 1 -> } else { 1326 1 -> c = crc; 1327 1 -> if (n) do { 1328 26312 -> c = crc_32_tab[...]; 1329 26312,1,26311 -> } while (--n); 1330 } 1331 2 -> crc = c; 1332 2 -> return c ^ 0xffffffffL; 1333 2 ->} 1334 1335 In this example, the function was called twice, passing once through 1336 each branch of the `if' statement. The body of the `do' loop was 1337 executed a total of 26312 times. Note how the `while' statement is 1338 annotated. It began execution 26312 times, once for each iteration 1339 through the loop. One of those times (the last time) it exited, while 1340 it branched back to the beginning of the loop 26311 times. 1341 1342 1343 File: gprof.info, Node: Inaccuracy, Next: How do I?, Prev: Output, Up: Top 1344 1345 6 Inaccuracy of `gprof' Output 1346 ****************************** 1347 1348 * Menu: 1349 1350 * Sampling Error:: Statistical margins of error 1351 * Assumptions:: Estimating children times 1352 1353 1354 File: gprof.info, Node: Sampling Error, Next: Assumptions, Up: Inaccuracy 1355 1356 6.1 Statistical Sampling Error 1357 ============================== 1358 1359 The run-time figures that `gprof' gives you are based on a sampling 1360 process, so they are subject to statistical inaccuracy. If a function 1361 runs only a small amount of time, so that on the average the sampling 1362 process ought to catch that function in the act only once, there is a 1363 pretty good chance it will actually find that function zero times, or 1364 twice. 1365 1366 By contrast, the number-of-calls and basic-block figures are derived 1367 by counting, not sampling. They are completely accurate and will not 1368 vary from run to run if your program is deterministic. 1369 1370 The "sampling period" that is printed at the beginning of the flat 1371 profile says how often samples are taken. The rule of thumb is that a 1372 run-time figure is accurate if it is considerably bigger than the 1373 sampling period. 1374 1375 The actual amount of error can be predicted. For N samples, the 1376 _expected_ error is the square-root of N. For example, if the sampling 1377 period is 0.01 seconds and `foo''s run-time is 1 second, N is 100 1378 samples (1 second/0.01 seconds), sqrt(N) is 10 samples, so the expected 1379 error in `foo''s run-time is 0.1 seconds (10*0.01 seconds), or ten 1380 percent of the observed value. Again, if the sampling period is 0.01 1381 seconds and `bar''s run-time is 100 seconds, N is 10000 samples, 1382 sqrt(N) is 100 samples, so the expected error in `bar''s run-time is 1 1383 second, or one percent of the observed value. It is likely to vary 1384 this much _on the average_ from one profiling run to the next. 1385 (_Sometimes_ it will vary more.) 1386 1387 This does not mean that a small run-time figure is devoid of 1388 information. If the program's _total_ run-time is large, a small 1389 run-time for one function does tell you that that function used an 1390 insignificant fraction of the whole program's time. Usually this means 1391 it is not worth optimizing. 1392 1393 One way to get more accuracy is to give your program more (but 1394 similar) input data so it will take longer. Another way is to combine 1395 the data from several runs, using the `-s' option of `gprof'. Here is 1396 how: 1397 1398 1. Run your program once. 1399 1400 2. Issue the command `mv gmon.out gmon.sum'. 1401 1402 3. Run your program again, the same as before. 1403 1404 4. Merge the new data in `gmon.out' into `gmon.sum' with this command: 1405 1406 gprof -s EXECUTABLE-FILE gmon.out gmon.sum 1407 1408 5. Repeat the last two steps as often as you wish. 1409 1410 6. Analyze the cumulative data using this command: 1411 1412 gprof EXECUTABLE-FILE gmon.sum > OUTPUT-FILE 1413 1414 1415 File: gprof.info, Node: Assumptions, Prev: Sampling Error, Up: Inaccuracy 1416 1417 6.2 Estimating `children' Times 1418 =============================== 1419 1420 Some of the figures in the call graph are estimates--for example, the 1421 `children' time values and all the time figures in caller and 1422 subroutine lines. 1423 1424 There is no direct information about these measurements in the 1425 profile data itself. Instead, `gprof' estimates them by making an 1426 assumption about your program that might or might not be true. 1427 1428 The assumption made is that the average time spent in each call to 1429 any function `foo' is not correlated with who called `foo'. If `foo' 1430 used 5 seconds in all, and 2/5 of the calls to `foo' came from `a', 1431 then `foo' contributes 2 seconds to `a''s `children' time, by 1432 assumption. 1433 1434 This assumption is usually true enough, but for some programs it is 1435 far from true. Suppose that `foo' returns very quickly when its 1436 argument is zero; suppose that `a' always passes zero as an argument, 1437 while other callers of `foo' pass other arguments. In this program, 1438 all the time spent in `foo' is in the calls from callers other than `a'. 1439 But `gprof' has no way of knowing this; it will blindly and incorrectly 1440 charge 2 seconds of time in `foo' to the children of `a'. 1441 1442 We hope some day to put more complete data into `gmon.out', so that 1443 this assumption is no longer needed, if we can figure out how. For the 1444 nonce, the estimated figures are usually more useful than misleading. 1445 1446 1447 File: gprof.info, Node: How do I?, Next: Incompatibilities, Prev: Inaccuracy, Up: Top 1448 1449 7 Answers to Common Questions 1450 ***************************** 1451 1452 How can I get more exact information about hot spots in my program? 1453 Looking at the per-line call counts only tells part of the story. 1454 Because `gprof' can only report call times and counts by function, 1455 the best way to get finer-grained information on where the program 1456 is spending its time is to re-factor large functions into sequences 1457 of calls to smaller ones. Beware however that this can introduce 1458 artifical hot spots since compiling with `-pg' adds a significant 1459 overhead to function calls. An alternative solution is to use a 1460 non-intrusive profiler, e.g. oprofile. 1461 1462 How do I find which lines in my program were executed the most times? 1463 Compile your program with basic-block counting enabled, run it, 1464 then use the following pipeline: 1465 1466 gprof -l -C OBJFILE | sort -k 3 -n -r 1467 1468 This listing will show you the lines in your code executed most 1469 often, but not necessarily those that consumed the most time. 1470 1471 How do I find which lines in my program called a particular function? 1472 Use `gprof -l' and lookup the function in the call graph. The 1473 callers will be broken down by function and line number. 1474 1475 How do I analyze a program that runs for less than a second? 1476 Try using a shell script like this one: 1477 1478 for i in `seq 1 100`; do 1479 fastprog 1480 mv gmon.out gmon.out.$i 1481 done 1482 1483 gprof -s fastprog gmon.out.* 1484 1485 gprof fastprog gmon.sum 1486 1487 If your program is completely deterministic, all the call counts 1488 will be simple multiples of 100 (i.e. a function called once in 1489 each run will appear with a call count of 100). 1490 1491 1492 1493 File: gprof.info, Node: Incompatibilities, Next: Details, Prev: How do I?, Up: Top 1494 1495 8 Incompatibilities with Unix `gprof' 1496 ************************************* 1497 1498 GNU `gprof' and Berkeley Unix `gprof' use the same data file 1499 `gmon.out', and provide essentially the same information. But there 1500 are a few differences. 1501 1502 * GNU `gprof' uses a new, generalized file format with support for 1503 basic-block execution counts and non-realtime histograms. A magic 1504 cookie and version number allows `gprof' to easily identify new 1505 style files. Old BSD-style files can still be read. *Note File 1506 Format::. 1507 1508 * For a recursive function, Unix `gprof' lists the function as a 1509 parent and as a child, with a `calls' field that lists the number 1510 of recursive calls. GNU `gprof' omits these lines and puts the 1511 number of recursive calls in the primary line. 1512 1513 * When a function is suppressed from the call graph with `-e', GNU 1514 `gprof' still lists it as a subroutine of functions that call it. 1515 1516 * GNU `gprof' accepts the `-k' with its argument in the form 1517 `from/to', instead of `from to'. 1518 1519 * In the annotated source listing, if there are multiple basic 1520 blocks on the same line, GNU `gprof' prints all of their counts, 1521 separated by commas. 1522 1523 * The blurbs, field widths, and output formats are different. GNU 1524 `gprof' prints blurbs after the tables, so that you can see the 1525 tables without skipping the blurbs. 1526 1527 1528 File: gprof.info, Node: Details, Next: GNU Free Documentation License, Prev: Incompatibilities, Up: Top 1529 1530 9 Details of Profiling 1531 ********************** 1532 1533 * Menu: 1534 1535 * Implementation:: How a program collects profiling information 1536 * File Format:: Format of `gmon.out' files 1537 * Internals:: `gprof''s internal operation 1538 * Debugging:: Using `gprof''s `-d' option 1539 1540 1541 File: gprof.info, Node: Implementation, Next: File Format, Up: Details 1542 1543 9.1 Implementation of Profiling 1544 =============================== 1545 1546 Profiling works by changing how every function in your program is 1547 compiled so that when it is called, it will stash away some information 1548 about where it was called from. From this, the profiler can figure out 1549 what function called it, and can count how many times it was called. 1550 This change is made by the compiler when your program is compiled with 1551 the `-pg' option, which causes every function to call `mcount' (or 1552 `_mcount', or `__mcount', depending on the OS and compiler) as one of 1553 its first operations. 1554 1555 The `mcount' routine, included in the profiling library, is 1556 responsible for recording in an in-memory call graph table both its 1557 parent routine (the child) and its parent's parent. This is typically 1558 done by examining the stack frame to find both the address of the 1559 child, and the return address in the original parent. Since this is a 1560 very machine-dependent operation, `mcount' itself is typically a short 1561 assembly-language stub routine that extracts the required information, 1562 and then calls `__mcount_internal' (a normal C function) with two 1563 arguments - `frompc' and `selfpc'. `__mcount_internal' is responsible 1564 for maintaining the in-memory call graph, which records `frompc', 1565 `selfpc', and the number of times each of these call arcs was traversed. 1566 1567 GCC Version 2 provides a magical function 1568 (`__builtin_return_address'), which allows a generic `mcount' function 1569 to extract the required information from the stack frame. However, on 1570 some architectures, most notably the SPARC, using this builtin can be 1571 very computationally expensive, and an assembly language version of 1572 `mcount' is used for performance reasons. 1573 1574 Number-of-calls information for library routines is collected by 1575 using a special version of the C library. The programs in it are the 1576 same as in the usual C library, but they were compiled with `-pg'. If 1577 you link your program with `gcc ... -pg', it automatically uses the 1578 profiling version of the library. 1579 1580 Profiling also involves watching your program as it runs, and 1581 keeping a histogram of where the program counter happens to be every 1582 now and then. Typically the program counter is looked at around 100 1583 times per second of run time, but the exact frequency may vary from 1584 system to system. 1585 1586 This is done is one of two ways. Most UNIX-like operating systems 1587 provide a `profil()' system call, which registers a memory array with 1588 the kernel, along with a scale factor that determines how the program's 1589 address space maps into the array. Typical scaling values cause every 1590 2 to 8 bytes of address space to map into a single array slot. On 1591 every tick of the system clock (assuming the profiled program is 1592 running), the value of the program counter is examined and the 1593 corresponding slot in the memory array is incremented. Since this is 1594 done in the kernel, which had to interrupt the process anyway to handle 1595 the clock interrupt, very little additional system overhead is required. 1596 1597 However, some operating systems, most notably Linux 2.0 (and 1598 earlier), do not provide a `profil()' system call. On such a system, 1599 arrangements are made for the kernel to periodically deliver a signal 1600 to the process (typically via `setitimer()'), which then performs the 1601 same operation of examining the program counter and incrementing a slot 1602 in the memory array. Since this method requires a signal to be 1603 delivered to user space every time a sample is taken, it uses 1604 considerably more overhead than kernel-based profiling. Also, due to 1605 the added delay required to deliver the signal, this method is less 1606 accurate as well. 1607 1608 A special startup routine allocates memory for the histogram and 1609 either calls `profil()' or sets up a clock signal handler. This 1610 routine (`monstartup') can be invoked in several ways. On Linux 1611 systems, a special profiling startup file `gcrt0.o', which invokes 1612 `monstartup' before `main', is used instead of the default `crt0.o'. 1613 Use of this special startup file is one of the effects of using `gcc 1614 ... -pg' to link. On SPARC systems, no special startup files are used. 1615 Rather, the `mcount' routine, when it is invoked for the first time 1616 (typically when `main' is called), calls `monstartup'. 1617 1618 If the compiler's `-a' option was used, basic-block counting is also 1619 enabled. Each object file is then compiled with a static array of 1620 counts, initially zero. In the executable code, every time a new 1621 basic-block begins (i.e. when an `if' statement appears), an extra 1622 instruction is inserted to increment the corresponding count in the 1623 array. At compile time, a paired array was constructed that recorded 1624 the starting address of each basic-block. Taken together, the two 1625 arrays record the starting address of every basic-block, along with the 1626 number of times it was executed. 1627 1628 The profiling library also includes a function (`mcleanup') which is 1629 typically registered using `atexit()' to be called as the program 1630 exits, and is responsible for writing the file `gmon.out'. Profiling 1631 is turned off, various headers are output, and the histogram is 1632 written, followed by the call-graph arcs and the basic-block counts. 1633 1634 The output from `gprof' gives no indication of parts of your program 1635 that are limited by I/O or swapping bandwidth. This is because samples 1636 of the program counter are taken at fixed intervals of the program's 1637 run time. Therefore, the time measurements in `gprof' output say 1638 nothing about time that your program was not running. For example, a 1639 part of the program that creates so much data that it cannot all fit in 1640 physical memory at once may run very slowly due to thrashing, but 1641 `gprof' will say it uses little time. On the other hand, sampling by 1642 run time has the advantage that the amount of load due to other users 1643 won't directly affect the output you get. 1644 1645 1646 File: gprof.info, Node: File Format, Next: Internals, Prev: Implementation, Up: Details 1647 1648 9.2 Profiling Data File Format 1649 ============================== 1650 1651 The old BSD-derived file format used for profile data does not contain a 1652 magic cookie that allows to check whether a data file really is a 1653 `gprof' file. Furthermore, it does not provide a version number, thus 1654 rendering changes to the file format almost impossible. GNU `gprof' 1655 uses a new file format that provides these features. For backward 1656 compatibility, GNU `gprof' continues to support the old BSD-derived 1657 format, but not all features are supported with it. For example, 1658 basic-block execution counts cannot be accommodated by the old file 1659 format. 1660 1661 The new file format is defined in header file `gmon_out.h'. It 1662 consists of a header containing the magic cookie and a version number, 1663 as well as some spare bytes available for future extensions. All data 1664 in a profile data file is in the native format of the target for which 1665 the profile was collected. GNU `gprof' adapts automatically to the 1666 byte-order in use. 1667 1668 In the new file format, the header is followed by a sequence of 1669 records. Currently, there are three different record types: histogram 1670 records, call-graph arc records, and basic-block execution count 1671 records. Each file can contain any number of each record type. When 1672 reading a file, GNU `gprof' will ensure records of the same type are 1673 compatible with each other and compute the union of all records. For 1674 example, for basic-block execution counts, the union is simply the sum 1675 of all execution counts for each basic-block. 1676 1677 9.2.1 Histogram Records 1678 ----------------------- 1679 1680 Histogram records consist of a header that is followed by an array of 1681 bins. The header contains the text-segment range that the histogram 1682 spans, the size of the histogram in bytes (unlike in the old BSD 1683 format, this does not include the size of the header), the rate of the 1684 profiling clock, and the physical dimension that the bin counts 1685 represent after being scaled by the profiling clock rate. The physical 1686 dimension is specified in two parts: a long name of up to 15 characters 1687 and a single character abbreviation. For example, a histogram 1688 representing real-time would specify the long name as "seconds" and the 1689 abbreviation as "s". This feature is useful for architectures that 1690 support performance monitor hardware (which, fortunately, is becoming 1691 increasingly common). For example, under DEC OSF/1, the "uprofile" 1692 command can be used to produce a histogram of, say, instruction cache 1693 misses. In this case, the dimension in the histogram header could be 1694 set to "i-cache misses" and the abbreviation could be set to "1" 1695 (because it is simply a count, not a physical dimension). Also, the 1696 profiling rate would have to be set to 1 in this case. 1697 1698 Histogram bins are 16-bit numbers and each bin represent an equal 1699 amount of text-space. For example, if the text-segment is one thousand 1700 bytes long and if there are ten bins in the histogram, each bin 1701 represents one hundred bytes. 1702 1703 9.2.2 Call-Graph Records 1704 ------------------------ 1705 1706 Call-graph records have a format that is identical to the one used in 1707 the BSD-derived file format. It consists of an arc in the call graph 1708 and a count indicating the number of times the arc was traversed during 1709 program execution. Arcs are specified by a pair of addresses: the 1710 first must be within caller's function and the second must be within 1711 the callee's function. When performing profiling at the function 1712 level, these addresses can point anywhere within the respective 1713 function. However, when profiling at the line-level, it is better if 1714 the addresses are as close to the call-site/entry-point as possible. 1715 This will ensure that the line-level call-graph is able to identify 1716 exactly which line of source code performed calls to a function. 1717 1718 9.2.3 Basic-Block Execution Count Records 1719 ----------------------------------------- 1720 1721 Basic-block execution count records consist of a header followed by a 1722 sequence of address/count pairs. The header simply specifies the 1723 length of the sequence. In an address/count pair, the address 1724 identifies a basic-block and the count specifies the number of times 1725 that basic-block was executed. Any address within the basic-address can 1726 be used. 1727 1728 1729 File: gprof.info, Node: Internals, Next: Debugging, Prev: File Format, Up: Details 1730 1731 9.3 `gprof''s Internal Operation 1732 ================================ 1733 1734 Like most programs, `gprof' begins by processing its options. During 1735 this stage, it may building its symspec list (`sym_ids.c:sym_id_add'), 1736 if options are specified which use symspecs. `gprof' maintains a 1737 single linked list of symspecs, which will eventually get turned into 1738 12 symbol tables, organized into six include/exclude pairs - one pair 1739 each for the flat profile (INCL_FLAT/EXCL_FLAT), the call graph arcs 1740 (INCL_ARCS/EXCL_ARCS), printing in the call graph 1741 (INCL_GRAPH/EXCL_GRAPH), timing propagation in the call graph 1742 (INCL_TIME/EXCL_TIME), the annotated source listing 1743 (INCL_ANNO/EXCL_ANNO), and the execution count listing 1744 (INCL_EXEC/EXCL_EXEC). 1745 1746 After option processing, `gprof' finishes building the symspec list 1747 by adding all the symspecs in `default_excluded_list' to the exclude 1748 lists EXCL_TIME and EXCL_GRAPH, and if line-by-line profiling is 1749 specified, EXCL_FLAT as well. These default excludes are not added to 1750 EXCL_ANNO, EXCL_ARCS, and EXCL_EXEC. 1751 1752 Next, the BFD library is called to open the object file, verify that 1753 it is an object file, and read its symbol table (`core.c:core_init'), 1754 using `bfd_canonicalize_symtab' after mallocing an appropriately sized 1755 array of symbols. At this point, function mappings are read (if the 1756 `--file-ordering' option has been specified), and the core text space 1757 is read into memory (if the `-c' option was given). 1758 1759 `gprof''s own symbol table, an array of Sym structures, is now built. 1760 This is done in one of two ways, by one of two routines, depending on 1761 whether line-by-line profiling (`-l' option) has been enabled. For 1762 normal profiling, the BFD canonical symbol table is scanned. For 1763 line-by-line profiling, every text space address is examined, and a new 1764 symbol table entry gets created every time the line number changes. In 1765 either case, two passes are made through the symbol table - one to 1766 count the size of the symbol table required, and the other to actually 1767 read the symbols. In between the two passes, a single array of type 1768 `Sym' is created of the appropriate length. Finally, 1769 `symtab.c:symtab_finalize' is called to sort the symbol table and 1770 remove duplicate entries (entries with the same memory address). 1771 1772 The symbol table must be a contiguous array for two reasons. First, 1773 the `qsort' library function (which sorts an array) will be used to 1774 sort the symbol table. Also, the symbol lookup routine 1775 (`symtab.c:sym_lookup'), which finds symbols based on memory address, 1776 uses a binary search algorithm which requires the symbol table to be a 1777 sorted array. Function symbols are indicated with an `is_func' flag. 1778 Line number symbols have no special flags set. Additionally, a symbol 1779 can have an `is_static' flag to indicate that it is a local symbol. 1780 1781 With the symbol table read, the symspecs can now be translated into 1782 Syms (`sym_ids.c:sym_id_parse'). Remember that a single symspec can 1783 match multiple symbols. An array of symbol tables (`syms') is created, 1784 each entry of which is a symbol table of Syms to be included or 1785 excluded from a particular listing. The master symbol table and the 1786 symspecs are examined by nested loops, and every symbol that matches a 1787 symspec is inserted into the appropriate syms table. This is done 1788 twice, once to count the size of each required symbol table, and again 1789 to build the tables, which have been malloced between passes. From now 1790 on, to determine whether a symbol is on an include or exclude symspec 1791 list, `gprof' simply uses its standard symbol lookup routine on the 1792 appropriate table in the `syms' array. 1793 1794 Now the profile data file(s) themselves are read 1795 (`gmon_io.c:gmon_out_read'), first by checking for a new-style 1796 `gmon.out' header, then assuming this is an old-style BSD `gmon.out' if 1797 the magic number test failed. 1798 1799 New-style histogram records are read by `hist.c:hist_read_rec'. For 1800 the first histogram record, allocate a memory array to hold all the 1801 bins, and read them in. When multiple profile data files (or files 1802 with multiple histogram records) are read, the starting address, ending 1803 address, number of bins and sampling rate must match between the 1804 various histograms, or a fatal error will result. If everything 1805 matches, just sum the additional histograms into the existing in-memory 1806 array. 1807 1808 As each call graph record is read (`call_graph.c:cg_read_rec'), the 1809 parent and child addresses are matched to symbol table entries, and a 1810 call graph arc is created by `cg_arcs.c:arc_add', unless the arc fails 1811 a symspec check against INCL_ARCS/EXCL_ARCS. As each arc is added, a 1812 linked list is maintained of the parent's child arcs, and of the child's 1813 parent arcs. Both the child's call count and the arc's call count are 1814 incremented by the record's call count. 1815 1816 Basic-block records are read (`basic_blocks.c:bb_read_rec'), but 1817 only if line-by-line profiling has been selected. Each basic-block 1818 address is matched to a corresponding line symbol in the symbol table, 1819 and an entry made in the symbol's bb_addr and bb_calls arrays. Again, 1820 if multiple basic-block records are present for the same address, the 1821 call counts are cumulative. 1822 1823 A gmon.sum file is dumped, if requested (`gmon_io.c:gmon_out_write'). 1824 1825 If histograms were present in the data files, assign them to symbols 1826 (`hist.c:hist_assign_samples') by iterating over all the sample bins 1827 and assigning them to symbols. Since the symbol table is sorted in 1828 order of ascending memory addresses, we can simple follow along in the 1829 symbol table as we make our pass over the sample bins. This step 1830 includes a symspec check against INCL_FLAT/EXCL_FLAT. Depending on the 1831 histogram scale factor, a sample bin may span multiple symbols, in 1832 which case a fraction of the sample count is allocated to each symbol, 1833 proportional to the degree of overlap. This effect is rare for normal 1834 profiling, but overlaps are more common during line-by-line profiling, 1835 and can cause each of two adjacent lines to be credited with half a 1836 hit, for example. 1837 1838 If call graph data is present, `cg_arcs.c:cg_assemble' is called. 1839 First, if `-c' was specified, a machine-dependent routine (`find_call') 1840 scans through each symbol's machine code, looking for subroutine call 1841 instructions, and adding them to the call graph with a zero call count. 1842 A topological sort is performed by depth-first numbering all the 1843 symbols (`cg_dfn.c:cg_dfn'), so that children are always numbered less 1844 than their parents, then making a array of pointers into the symbol 1845 table and sorting it into numerical order, which is reverse topological 1846 order (children appear before parents). Cycles are also detected at 1847 this point, all members of which are assigned the same topological 1848 number. Two passes are now made through this sorted array of symbol 1849 pointers. The first pass, from end to beginning (parents to children), 1850 computes the fraction of child time to propagate to each parent and a 1851 print flag. The print flag reflects symspec handling of 1852 INCL_GRAPH/EXCL_GRAPH, with a parent's include or exclude (print or no 1853 print) property being propagated to its children, unless they 1854 themselves explicitly appear in INCL_GRAPH or EXCL_GRAPH. A second 1855 pass, from beginning to end (children to parents) actually propagates 1856 the timings along the call graph, subject to a check against 1857 INCL_TIME/EXCL_TIME. With the print flag, fractions, and timings now 1858 stored in the symbol structures, the topological sort array is now 1859 discarded, and a new array of pointers is assembled, this time sorted 1860 by propagated time. 1861 1862 Finally, print the various outputs the user requested, which is now 1863 fairly straightforward. The call graph (`cg_print.c:cg_print') and 1864 flat profile (`hist.c:hist_print') are regurgitations of values already 1865 computed. The annotated source listing 1866 (`basic_blocks.c:print_annotated_source') uses basic-block information, 1867 if present, to label each line of code with call counts, otherwise only 1868 the function call counts are presented. 1869 1870 The function ordering code is marginally well documented in the 1871 source code itself (`cg_print.c'). Basically, the functions with the 1872 most use and the most parents are placed first, followed by other 1873 functions with the most use, followed by lower use functions, followed 1874 by unused functions at the end. 1875 1876 1877 File: gprof.info, Node: Debugging, Prev: Internals, Up: Details 1878 1879 9.3.1 Debugging `gprof' 1880 ----------------------- 1881 1882 If `gprof' was compiled with debugging enabled, the `-d' option 1883 triggers debugging output (to stdout) which can be helpful in 1884 understanding its operation. The debugging number specified is 1885 interpreted as a sum of the following options: 1886 1887 2 - Topological sort 1888 Monitor depth-first numbering of symbols during call graph analysis 1889 1890 4 - Cycles 1891 Shows symbols as they are identified as cycle heads 1892 1893 16 - Tallying 1894 As the call graph arcs are read, show each arc and how the total 1895 calls to each function are tallied 1896 1897 32 - Call graph arc sorting 1898 Details sorting individual parents/children within each call graph 1899 entry 1900 1901 64 - Reading histogram and call graph records 1902 Shows address ranges of histograms as they are read, and each call 1903 graph arc 1904 1905 128 - Symbol table 1906 Reading, classifying, and sorting the symbol table from the object 1907 file. For line-by-line profiling (`-l' option), also shows line 1908 numbers being assigned to memory addresses. 1909 1910 256 - Static call graph 1911 Trace operation of `-c' option 1912 1913 512 - Symbol table and arc table lookups 1914 Detail operation of lookup routines 1915 1916 1024 - Call graph propagation 1917 Shows how function times are propagated along the call graph 1918 1919 2048 - Basic-blocks 1920 Shows basic-block records as they are read from profile data (only 1921 meaningful with `-l' option) 1922 1923 4096 - Symspecs 1924 Shows symspec-to-symbol pattern matching operation 1925 1926 8192 - Annotate source 1927 Tracks operation of `-A' option 1928 1929 1930 File: gprof.info, Node: GNU Free Documentation License, Prev: Details, Up: Top 1931 1932 10 GNU Free Documentation License 1933 ********************************* 1934 1935 GNU Free Documentation License 1936 1937 Version 1.1, March 2000 1938 1939 Copyright (C) 2000 Free Software Foundation, Inc. 51 Franklin 1940 Street, Fifth Floor, Boston, MA 02110-1301 USA 1941 1942 Everyone is permitted to copy and distribute verbatim copies of 1943 this license document, but changing it is not allowed. 1944 1945 0. PREAMBLE 1946 1947 The purpose of this License is to make a manual, textbook, or other 1948 written document "free" in the sense of freedom: to assure everyone the 1949 effective freedom to copy and redistribute it, with or without 1950 modifying it, either commercially or noncommercially. Secondarily, 1951 this License preserves for the author and publisher a way to get credit 1952 for their work, while not being considered responsible for 1953 modifications made by others. 1954 1955 This License is a kind of "copyleft", which means that derivative 1956 works of the document must themselves be free in the same sense. It 1957 complements the GNU General Public License, which is a copyleft license 1958 designed for free software. 1959 1960 We have designed this License in order to use it for manuals for free 1961 software, because free software needs free documentation: a free 1962 program should come with manuals providing the same freedoms that the 1963 software does. But this License is not limited to software manuals; it 1964 can be used for any textual work, regardless of subject matter or 1965 whether it is published as a printed book. We recommend this License 1966 principally for works whose purpose is instruction or reference. 1967 1968 1. APPLICABILITY AND DEFINITIONS 1969 1970 This License applies to any manual or other work that contains a 1971 notice placed by the copyright holder saying it can be distributed 1972 under the terms of this License. The "Document", below, refers to any 1973 such manual or work. Any member of the public is a licensee, and is 1974 addressed as "you". 1975 1976 A "Modified Version" of the Document means any work containing the 1977 Document or a portion of it, either copied verbatim, or with 1978 modifications and/or translated into another language. 1979 1980 A "Secondary Section" is a named appendix or a front-matter section 1981 of the Document that deals exclusively with the relationship of the 1982 publishers or authors of the Document to the Document's overall subject 1983 (or to related matters) and contains nothing that could fall directly 1984 within that overall subject. (For example, if the Document is in part a 1985 textbook of mathematics, a Secondary Section may not explain any 1986 mathematics.) The relationship could be a matter of historical 1987 connection with the subject or with related matters, or of legal, 1988 commercial, philosophical, ethical or political position regarding them. 1989 1990 The "Invariant Sections" are certain Secondary Sections whose titles 1991 are designated, as being those of Invariant Sections, in the notice 1992 that says that the Document is released under this License. 1993 1994 The "Cover Texts" are certain short passages of text that are listed, 1995 as Front-Cover Texts or Back-Cover Texts, in the notice that says that 1996 the Document is released under this License. 1997 1998 A "Transparent" copy of the Document means a machine-readable copy, 1999 represented in a format whose specification is available to the general 2000 public, whose contents can be viewed and edited directly and 2001 straightforwardly with generic text editors or (for images composed of 2002 pixels) generic paint programs or (for drawings) some widely available 2003 drawing editor, and that is suitable for input to text formatters or 2004 for automatic translation to a variety of formats suitable for input to 2005 text formatters. A copy made in an otherwise Transparent file format 2006 whose markup has been designed to thwart or discourage subsequent 2007 modification by readers is not Transparent. A copy that is not 2008 "Transparent" is called "Opaque". 2009 2010 Examples of suitable formats for Transparent copies include plain 2011 ASCII without markup, Texinfo input format, LaTeX input format, SGML or 2012 XML using a publicly available DTD, and standard-conforming simple HTML 2013 designed for human modification. Opaque formats include PostScript, 2014 PDF, proprietary formats that can be read and edited only by 2015 proprietary word processors, SGML or XML for which the DTD and/or 2016 processing tools are not generally available, and the machine-generated 2017 HTML produced by some word processors for output purposes only. 2018 2019 The "Title Page" means, for a printed book, the title page itself, 2020 plus such following pages as are needed to hold, legibly, the material 2021 this License requires to appear in the title page. For works in 2022 formats which do not have any title page as such, "Title Page" means 2023 the text near the most prominent appearance of the work's title, 2024 preceding the beginning of the body of the text. 2025 2026 2. VERBATIM COPYING 2027 2028 You may copy and distribute the Document in any medium, either 2029 commercially or noncommercially, provided that this License, the 2030 copyright notices, and the license notice saying this License applies 2031 to the Document are reproduced in all copies, and that you add no other 2032 conditions whatsoever to those of this License. You may not use 2033 technical measures to obstruct or control the reading or further 2034 copying of the copies you make or distribute. However, you may accept 2035 compensation in exchange for copies. If you distribute a large enough 2036 number of copies you must also follow the conditions in section 3. 2037 2038 You may also lend copies, under the same conditions stated above, and 2039 you may publicly display copies. 2040 2041 3. COPYING IN QUANTITY 2042 2043 If you publish printed copies of the Document numbering more than 2044 100, and the Document's license notice requires Cover Texts, you must 2045 enclose the copies in covers that carry, clearly and legibly, all these 2046 Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts 2047 on the back cover. Both covers must also clearly and legibly identify 2048 you as the publisher of these copies. The front cover must present the 2049 full title with all words of the title equally prominent and visible. 2050 You may add other material on the covers in addition. Copying with 2051 changes limited to the covers, as long as they preserve the title of 2052 the Document and satisfy these conditions, can be treated as verbatim 2053 copying in other respects. 2054 2055 If the required texts for either cover are too voluminous to fit 2056 legibly, you should put the first ones listed (as many as fit 2057 reasonably) on the actual cover, and continue the rest onto adjacent 2058 pages. 2059 2060 If you publish or distribute Opaque copies of the Document numbering 2061 more than 100, you must either include a machine-readable Transparent 2062 copy along with each Opaque copy, or state in or with each Opaque copy 2063 a publicly-accessible computer-network location containing a complete 2064 Transparent copy of the Document, free of added material, which the 2065 general network-using public has access to download anonymously at no 2066 charge using public-standard network protocols. If you use the latter 2067 option, you must take reasonably prudent steps, when you begin 2068 distribution of Opaque copies in quantity, to ensure that this 2069 Transparent copy will remain thus accessible at the stated location 2070 until at least one year after the last time you distribute an Opaque 2071 copy (directly or through your agents or retailers) of that edition to 2072 the public. 2073 2074 It is requested, but not required, that you contact the authors of 2075 the Document well before redistributing any large number of copies, to 2076 give them a chance to provide you with an updated version of the 2077 Document. 2078 2079 4. MODIFICATIONS 2080 2081 You may copy and distribute a Modified Version of the Document under 2082 the conditions of sections 2 and 3 above, provided that you release the 2083 Modified Version under precisely this License, with the Modified 2084 Version filling the role of the Document, thus licensing distribution 2085 and modification of the Modified Version to whoever possesses a copy of 2086 it. In addition, you must do these things in the Modified Version: 2087 2088 A. Use in the Title Page (and on the covers, if any) a title distinct 2089 from that of the Document, and from those of previous versions 2090 (which should, if there were any, be listed in the History section 2091 of the Document). You may use the same title as a previous version 2092 if the original publisher of that version gives permission. B. List on 2093 the Title Page, as authors, one or more persons or entities 2094 responsible for authorship of the modifications in the Modified 2095 Version, together with at least five of the principal authors of the 2096 Document (all of its principal authors, if it has less than five). C. 2097 State on the Title page the name of the publisher of the Modified 2098 Version, as the publisher. D. Preserve all the copyright notices of 2099 the Document. E. Add an appropriate copyright notice for your 2100 modifications adjacent to the other copyright notices. F. Include, 2101 immediately after the copyright notices, a license notice giving the 2102 public permission to use the Modified Version under the terms of 2103 this License, in the form shown in the Addendum below. G. Preserve in 2104 that license notice the full lists of Invariant Sections and 2105 required Cover Texts given in the Document's license notice. H. 2106 Include an unaltered copy of this License. I. Preserve the section 2107 entitled "History", and its title, and add to it an item stating at 2108 least the title, year, new authors, and publisher of the Modified 2109 Version as given on the Title Page. If there is no section entitled 2110 "History" in the Document, create one stating the title, year, 2111 authors, and publisher of the Document as given on its Title Page, 2112 then add an item describing the Modified Version as stated in the 2113 previous sentence. J. Preserve the network location, if any, given in 2114 the Document for public access to a Transparent copy of the 2115 Document, and likewise the network locations given in the Document 2116 for previous versions it was based on. These may be placed in the 2117 "History" section. You may omit a network location for a work that 2118 was published at least four years before the Document itself, or if 2119 the original publisher of the version it refers to gives permission. 2120 K. In any section entitled "Acknowledgements" or "Dedications", 2121 preserve the section's title, and preserve in the section all the 2122 substance and tone of each of the contributor acknowledgements 2123 and/or dedications given therein. L. Preserve all the Invariant 2124 Sections of the Document, unaltered in their text and in their 2125 titles. Section numbers or the equivalent are not considered part 2126 of the section titles. M. Delete any section entitled "Endorsements". 2127 Such a section may not be included in the Modified Version. N. Do 2128 not retitle any existing section as "Endorsements" or to conflict in 2129 title with any Invariant Section. 2130 2131 If the Modified Version includes new front-matter sections or 2132 appendices that qualify as Secondary Sections and contain no material 2133 copied from the Document, you may at your option designate some or all 2134 of these sections as invariant. To do this, add their titles to the 2135 list of Invariant Sections in the Modified Version's license notice. 2136 These titles must be distinct from any other section titles. 2137 2138 You may add a section entitled "Endorsements", provided it contains 2139 nothing but endorsements of your Modified Version by various 2140 parties-for example, statements of peer review or that the text has 2141 been approved by an organization as the authoritative definition of a 2142 standard. 2143 2144 You may add a passage of up to five words as a Front-Cover Text, and 2145 a passage of up to 25 words as a Back-Cover Text, to the end of the list 2146 of Cover Texts in the Modified Version. Only one passage of 2147 Front-Cover Text and one of Back-Cover Text may be added by (or through 2148 arrangements made by) any one entity. If the Document already includes 2149 a cover text for the same cover, previously added by you or by 2150 arrangement made by the same entity you are acting on behalf of, you 2151 may not add another; but you may replace the old one, on explicit 2152 permission from the previous publisher that added the old one. 2153 2154 The author(s) and publisher(s) of the Document do not by this License 2155 give permission to use their names for publicity for or to assert or 2156 imply endorsement of any Modified Version. 2157 2158 5. COMBINING DOCUMENTS 2159 2160 You may combine the Document with other documents released under this 2161 License, under the terms defined in section 4 above for modified 2162 versions, provided that you include in the combination all of the 2163 Invariant Sections of all of the original documents, unmodified, and 2164 list them all as Invariant Sections of your combined work in its 2165 license notice. 2166 2167 The combined work need only contain one copy of this License, and 2168 multiple identical Invariant Sections may be replaced with a single 2169 copy. If there are multiple Invariant Sections with the same name but 2170 different contents, make the title of each such section unique by 2171 adding at the end of it, in parentheses, the name of the original 2172 author or publisher of that section if known, or else a unique number. 2173 Make the same adjustment to the section titles in the list of Invariant 2174 Sections in the license notice of the combined work. 2175 2176 In the combination, you must combine any sections entitled "History" 2177 in the various original documents, forming one section entitled 2178 "History"; likewise combine any sections entitled "Acknowledgements", 2179 and any sections entitled "Dedications". You must delete all sections 2180 entitled "Endorsements." 2181 2182 6. COLLECTIONS OF DOCUMENTS 2183 2184 You may make a collection consisting of the Document and other 2185 documents released under this License, and replace the individual 2186 copies of this License in the various documents with a single copy that 2187 is included in the collection, provided that you follow the rules of 2188 this License for verbatim copying of each of the documents in all other 2189 respects. 2190 2191 You may extract a single document from such a collection, and 2192 distribute it individually under this License, provided you insert a 2193 copy of this License into the extracted document, and follow this 2194 License in all other respects regarding verbatim copying of that 2195 document. 2196 2197 7. AGGREGATION WITH INDEPENDENT WORKS 2198 2199 A compilation of the Document or its derivatives with other separate 2200 and independent documents or works, in or on a volume of a storage or 2201 distribution medium, does not as a whole count as a Modified Version of 2202 the Document, provided no compilation copyright is claimed for the 2203 compilation. Such a compilation is called an "aggregate", and this 2204 License does not apply to the other self-contained works thus compiled 2205 with the Document, on account of their being thus compiled, if they are 2206 not themselves derivative works of the Document. 2207 2208 If the Cover Text requirement of section 3 is applicable to these 2209 copies of the Document, then if the Document is less than one quarter 2210 of the entire aggregate, the Document's Cover Texts may be placed on 2211 covers that surround only the Document within the aggregate. Otherwise 2212 they must appear on covers around the whole aggregate. 2213 2214 8. TRANSLATION 2215 2216 Translation is considered a kind of modification, so you may 2217 distribute translations of the Document under the terms of section 4. 2218 Replacing Invariant Sections with translations requires special 2219 permission from their copyright holders, but you may include 2220 translations of some or all Invariant Sections in addition to the 2221 original versions of these Invariant Sections. You may include a 2222 translation of this License provided that you also include the original 2223 English version of this License. In case of a disagreement between the 2224 translation and the original English version of this License, the 2225 original English version will prevail. 2226 2227 9. TERMINATION 2228 2229 You may not copy, modify, sublicense, or distribute the Document 2230 except as expressly provided for under this License. Any other attempt 2231 to copy, modify, sublicense or distribute the Document is void, and will 2232 automatically terminate your rights under this License. However, 2233 parties who have received copies, or rights, from you under this 2234 License will not have their licenses terminated so long as such parties 2235 remain in full compliance. 2236 2237 10. FUTURE REVISIONS OF THIS LICENSE 2238 2239 The Free Software Foundation may publish new, revised versions of 2240 the GNU Free Documentation License from time to time. Such new 2241 versions will be similar in spirit to the present version, but may 2242 differ in detail to address new problems or concerns. See 2243 http://www.gnu.org/copyleft/. 2244 2245 Each version of the License is given a distinguishing version number. 2246 If the Document specifies that a particular numbered version of this 2247 License "or any later version" applies to it, you have the option of 2248 following the terms and conditions either of that specified version or 2249 of any later version that has been published (not as a draft) by the 2250 Free Software Foundation. If the Document does not specify a version 2251 number of this License, you may choose any version ever published (not 2252 as a draft) by the Free Software Foundation. 2253 2254 ADDENDUM: How to use this License for your documents 2255 2256 To use this License in a document you have written, include a copy of 2257 the License in the document and put the following copyright and license 2258 notices just after the title page: 2259 2260 Copyright (c) YEAR YOUR NAME. 2261 Permission is granted to copy, distribute and/or modify this document 2262 under the terms of the GNU Free Documentation License, Version 1.1 2263 or any later version published by the Free Software Foundation; 2264 with the Invariant Sections being LIST THEIR TITLES, with the 2265 Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST. 2266 A copy of the license is included in the section entitled "GNU 2267 Free Documentation License". 2268 2269 If you have no Invariant Sections, write "with no Invariant Sections" 2270 instead of saying which ones are invariant. If you have no Front-Cover 2271 Texts, write "no Front-Cover Texts" instead of "Front-Cover Texts being 2272 LIST"; likewise for Back-Cover Texts. 2273 2274 If your document contains nontrivial examples of program code, we 2275 recommend releasing these examples in parallel under your choice of 2276 free software license, such as the GNU General Public License, to 2277 permit their use in free software. 2278 2279 2280 2281 Tag Table: 2282 Node: Top735 2283 Node: Introduction1974 2284 Node: Compiling4304 2285 Node: Executing8522 2286 Node: Invoking11314 2287 Node: Output Options12729 2288 Node: Analysis Options19751 2289 Node: Miscellaneous Options22953 2290 Node: Deprecated Options24186 2291 Node: Symspecs26265 2292 Node: Output28091 2293 Node: Flat Profile29117 2294 Node: Call Graph34047 2295 Node: Primary37262 2296 Node: Callers39803 2297 Node: Subroutines41920 2298 Node: Cycles43729 2299 Node: Line-by-line50503 2300 Node: Annotated Source54237 2301 Node: Inaccuracy57093 2302 Node: Sampling Error57351 2303 Node: Assumptions59921 2304 Node: How do I?61390 2305 Node: Incompatibilities63206 2306 Node: Details64674 2307 Node: Implementation65067 2308 Node: File Format70964 2309 Node: Internals75254 2310 Node: Debugging83631 2311 Node: GNU Free Documentation License85236 2312 2313 End Tag Table 2314