| 1 | # vim: ts=8 sw=2 sts=0 noexpandtab: |
| 2 | ########################################################## |
| 3 | ## This script is part of the Devel::NYTProf distribution |
| 4 | ## |
| 5 | ## Copyright, contact and other information can be found |
| 6 | ## at the bottom of this file, or by going to: |
| 7 | ## http://metacpan.org/release/Devel-NYTProf/ |
| 8 | ## |
| 9 | ########################################################### |
| 10 | package Devel::NYTProfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/; |
| 11 | |
| 12 | our $VERSIONscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/$VERSION. = '6.15'; # also change in Devel::NYTProf::Core |
| 13 | |
| 14 | package # hide the package from the PAUSE indexer |
| 15 | DBscip-perl cpan Devel-NYTProf 6.15 DB/; |
| 16 | |
| 17 | # Enable specific perl debugger flags (others may be set later). |
| 18 | # Set the flags that influence compilation ASAP so we get full details |
| 19 | # (sub line ranges etc) of modules loaded as a side effect of loading |
| 20 | # Devel::NYTProf::Core (ie XSLoader, strict, Exporter etc.) |
| 21 | # See "perldoc perlvar" for details of the $^P ($PERLDB) flags |
| 22 | $^Pscip-perl cpan Devel-NYTProf 6.15 DB/`$^P`. = 0x010 # record line range of sub definition |
| 23 | | 0x100 # informative "file" names for evals |
| 24 | | 0x200; # informative names for anonymous subroutines |
| 25 | |
| 26 | requirescip-perl cpan Devel-NYTProf 6.15 DB/require(). Devel::NYTProf::Core; # loads XS and sets options |
| 27 | |
| 28 | # XXX hack, need better option handling e.g., add DB::get_option('use_db_sub') |
| 29 | my $use_db_sub = ($ENVscip-perl cpan Devel-NYTProf 6.15 DB/`%ENV`.{NYTPROF} && $ENVscip-perl cpan Devel-NYTProf 6.15 DB/`%ENV`.{NYTPROF} =~ m/\buse_db_sub=1\b/); |
| 30 | if ($use_db_sub) { # install DB::DB sub |
| 31 | *DB = ($]scip-perl cpan Devel-NYTProf 6.15 DB/`$]`. < 5.008008) |
| 32 | ? sub { goto &DB_profilerscip-perl cpan Devel-NYTProf 6.15 DB/DB_profiler(). } # workaround bug in old perl versions (slow) |
| 33 | : \&DB_profilerscip-perl cpan Devel-NYTProf 6.15 DB/DB_profiler().; |
| 34 | } |
| 35 | |
| 36 | # DB::sub shouldn't be called, but needs to exist for perl <5.8.7 (<perl@24265) |
| 37 | # Could be called in obscure cases, e.g. if "perl -d" (not -d:NYTProf) |
| 38 | # was used with Devel::NYTProf loaded some other way |
| 39 | *sub = sub { warnscip-perl cpan Devel-NYTProf 6.15 DB/warn(). "DB::sub called unexpectedly (@{[ caller(0) ]})" } |
| 40 | if $]scip-perl cpan Devel-NYTProf 6.15 DB/`$]`. < 5.008008; |
| 41 | |
| 42 | sub CLONEscip-perl cpan Devel-NYTProf 6.15 DB/CLONE(). { DB::disable_profiler } |
| 43 | |
| 44 | init_profilerscip-perl cpan Devel-NYTProf 6.15 DB/init_profiler().(); # provides true return value for module |
| 45 | |
| 46 | # put nothing here! |
| 47 | |
| 48 | __END__ |
| 49 | |
| 50 | =head1 NAME |
| 51 | |
| 52 | Devel::NYTProf - Powerful fast feature-rich Perl source code profiler |
| 53 | |
| 54 | =head1 SYNOPSIS |
| 55 | |
| 56 | # profile code and write database to ./nytprof.out |
| 57 | perl -d:NYTProf some_perl.pl |
| 58 | |
| 59 | # convert database into a set of html files, e.g., ./nytprof/index.html |
| 60 | # and open a web browser on the nytprof/index.html file |
| 61 | nytprofhtml --open |
| 62 | |
| 63 | # or into comma separated files, e.g., ./nytprof/*.csv |
| 64 | nytprofcsv |
| 65 | |
| 66 | Tim Bunce has often given talks on profiling perl code, including a detailed |
| 67 | look at how to use NYTProf and how to optimize your code. A video of his |
| 68 | YAPC::NA 2014 talk can be found at L<https://youtu.be/T7EK6RZAnEA> |
| 69 | |
| 70 | =head1 DESCRIPTION |
| 71 | |
| 72 | Devel::NYTProf is a powerful, fast, feature-rich perl source code profiler. |
| 73 | |
| 74 | =over |
| 75 | |
| 76 | =item * |
| 77 | |
| 78 | Performs per-line statement profiling for fine detail |
| 79 | |
| 80 | =item * |
| 81 | |
| 82 | Performs per-subroutine statement profiling for overview |
| 83 | |
| 84 | =item * |
| 85 | |
| 86 | Performs per-opcode profiling for slow perl builtins |
| 87 | |
| 88 | =item * |
| 89 | |
| 90 | Performs per-block statement profiling (the first profiler to do so) |
| 91 | |
| 92 | =item * |
| 93 | |
| 94 | Accounts correctly for time spent after calls return |
| 95 | |
| 96 | =item * |
| 97 | |
| 98 | Performs inclusive and exclusive timing of subroutines |
| 99 | |
| 100 | =item * |
| 101 | |
| 102 | Subroutine times are per calling location (a powerful feature) |
| 103 | |
| 104 | =item * |
| 105 | |
| 106 | Can profile compile-time activity, just run-time, or just END time |
| 107 | |
| 108 | =item * |
| 109 | |
| 110 | Uses novel techniques for efficient profiling |
| 111 | |
| 112 | =item * |
| 113 | |
| 114 | Sub-microsecond (100ns) resolution on supported systems |
| 115 | |
| 116 | =item * |
| 117 | |
| 118 | Very fast - the fastest statement and subroutine profilers for perl |
| 119 | |
| 120 | =item * |
| 121 | |
| 122 | Handles applications that fork, with no performance cost |
| 123 | |
| 124 | =item * |
| 125 | |
| 126 | Immune from noise caused by profiling overheads and I/O |
| 127 | |
| 128 | =item * |
| 129 | |
| 130 | Program being profiled can stop/start the profiler |
| 131 | |
| 132 | =item * |
| 133 | |
| 134 | Generates richly annotated and cross-linked html reports |
| 135 | |
| 136 | =item * |
| 137 | |
| 138 | Captures source code, including string evals, for stable results |
| 139 | |
| 140 | =item * |
| 141 | |
| 142 | Trivial to use with mod_perl - add one line to httpd.conf |
| 143 | |
| 144 | =item * |
| 145 | |
| 146 | Includes an extensive test suite |
| 147 | |
| 148 | =item * |
| 149 | |
| 150 | Tested on very large codebases |
| 151 | |
| 152 | =back |
| 153 | |
| 154 | NYTProf is effectively two profilers in one: a statement profiler, and a |
| 155 | subroutine profiler. |
| 156 | |
| 157 | =head2 Statement Profiling |
| 158 | |
| 159 | The statement profiler measures the time between entering one perl statement |
| 160 | and entering the next. Whenever execution reaches a new statement, the time |
| 161 | since entering the previous statement is calculated and added to the time |
| 162 | associated with the line of the source file that the previous statement starts on. |
| 163 | |
| 164 | By default the statement profiler also determines the first line of the current |
| 165 | block and the first line of the current statement, and accumulates times |
| 166 | associated with those. |
| 167 | |
| 168 | Another innovation unique to NYTProf is automatic compensation for a problem |
| 169 | inherent in simplistic statement-to-statement timing. Consider a statement that |
| 170 | calls a subroutine and then performs some other work that doesn't execute new |
| 171 | statements, for example: |
| 172 | |
| 173 | foo(...) + mkdir(...); |
| 174 | |
| 175 | In all other statement profilers the time spent in remainder of the expression |
| 176 | (mkdir in the example) will be recorded as having been spent I<on the last |
| 177 | statement executed in foo()>! Here's another example: |
| 178 | |
| 179 | while (<>) { |
| 180 | ... |
| 181 | 1; |
| 182 | } |
| 183 | |
| 184 | After the first time around the loop, any further time spent evaluating the |
| 185 | condition (waiting for input in this example) would be recorded as having |
| 186 | been spent I<on the last statement executed in the loop>! (Until perl bug |
| 187 | #60954 is fixed this problem still applies to some loops. For more information |
| 188 | see L<http://rt.perl.org/rt3/Ticket/Display.html?id=60954>) |
| 189 | |
| 190 | NYTProf avoids these problems by intercepting the opcodes which indicate that |
| 191 | control is returning into some previous statement and adjusting the profile |
| 192 | accordingly. |
| 193 | |
| 194 | The statement profiler naturally generates a lot of data which is streamed out |
| 195 | to a file in a very compact format. NYTProf takes care to not include the |
| 196 | measurement and writing overheads in the profile times (some profilers produce |
| 197 | 'noisy' data due to periodic stdio flushing). |
| 198 | |
| 199 | =head2 Subroutine Profiling |
| 200 | |
| 201 | The subroutine profiler measures the time between entering a subroutine and |
| 202 | leaving it. It then increments a call count and accumulates the duration. |
| 203 | For each subroutine called, separate counts and durations are stored I<for each |
| 204 | location that called the subroutine>. |
| 205 | |
| 206 | Subroutine entry is detected by intercepting the C<entersub> opcode. Subroutine |
| 207 | exit is detected via perl's internal save stack. As a result the subroutine |
| 208 | profiler is both fast and robust. |
| 209 | |
| 210 | =head3 Subroutine Recursion |
| 211 | |
| 212 | For subroutines that recurse directly or indirectly, such as Error::try, |
| 213 | the inclusive time is only measured for the outer-most call. |
| 214 | |
| 215 | The inclusive times of recursive calls are still measured and are accumulated |
| 216 | separately. Also the 'maximum recursion depth' per calling location is recorded. |
| 217 | |
| 218 | =head3 Goto &Subroutine |
| 219 | |
| 220 | Perl implements a C<goto &destination> as a C<return> followed by a call to |
| 221 | C<&destination>, so that's how it will appear in the report. |
| 222 | |
| 223 | The C<goto> will be shown with a very short time because it's effectively just |
| 224 | a C<return>. The C<&destination> sub will show a call I<not> from the location |
| 225 | of the C<goto> but from the location of the call to the sub that performed the C<goto>. |
| 226 | |
| 227 | =head3 accept() |
| 228 | |
| 229 | The perl built-in accept() function waits listening for a connection on a |
| 230 | socket, and so is a key part of pure-perl network service applications. |
| 231 | |
| 232 | The time spent waiting for a remotely initiated connection can be relatively |
| 233 | high but is not relevant to the performance of the application. So the accept() |
| 234 | function is treated as a special case. The subroutine profiler discounts the |
| 235 | time spent in the accept() function. It does this in a way that also discounts |
| 236 | that time from all the callers up the call stack. The effect on the reports is |
| 237 | that all accept() calls appear to be instant. |
| 238 | |
| 239 | The I<statement> profiler still shows the time actually spent in the statement |
| 240 | that executed the accept() call. |
| 241 | |
| 242 | =head2 Application Profiling |
| 243 | |
| 244 | NYTProf records extra information in the data file to capture details that may |
| 245 | be useful when analyzing the performance. It also records the filename and line |
| 246 | ranges of all the subroutines. |
| 247 | |
| 248 | NYTProf can profile applications that fork, and does so with no loss of |
| 249 | performance. |
| 250 | NYTProf detects the fork and starts writing a new profile file with the pid |
| 251 | appended to the filename. Since L<nytprofhtml> only works with a single profile |
| 252 | file you may want to merge multiple files using L<nytprofmerge>. |
| 253 | |
| 254 | =head2 Fast Profiling |
| 255 | |
| 256 | The NYTProf profiler is written almost entirely in C and great care has been |
| 257 | taken to ensure it's very efficient. |
| 258 | |
| 259 | =head2 Apache Profiling |
| 260 | |
| 261 | Just add one line near the start of your httpd.conf file: |
| 262 | |
| 263 | PerlModule Devel::NYTProf::Apache |
| 264 | |
| 265 | By default you'll get a F</tmp/nytprof.$$.out> file for the parent process and |
| 266 | a F</tmp/nytprof.$parent.out.$$> file for each worker process. |
| 267 | |
| 268 | NYTProf takes care to detect when control is returning back from perl to |
| 269 | mod_perl so time spent in mod_perl (such as waiting for the next request) |
| 270 | does not get allocated to the last statement executed. |
| 271 | |
| 272 | Works with mod_perl 1 and 2. See L<Devel::NYTProf::Apache> for more information. |
| 273 | |
| 274 | =head1 PROFILING |
| 275 | |
| 276 | Usually you'd load Devel::NYTProf on the command line using the perl -d option: |
| 277 | |
| 278 | perl -d:NYTProf some_perl.pl |
| 279 | |
| 280 | To save typing the ':NYTProf' you could set the L<C<PERL5DB> environment variable|perlrun/PERL5DB>: |
| 281 | |
| 282 | PERL5DB='use Devel::NYTProf' |
| 283 | |
| 284 | and then just perl -d would work: |
| 285 | |
| 286 | perl -d some_perl.pl |
| 287 | |
| 288 | Or you can avoid the need to add the -d option at all by using the L<C<PERL5OPT>> environment variable|perlrun/PERL5OPT>: |
| 289 | |
| 290 | PERL5OPT=-d:NYTProf |
| 291 | |
| 292 | That's also very handy when you can't alter the perl command line being used to |
| 293 | run the script you want to profile. Usually you'll want to enable the |
| 294 | L</addpid=1> option to ensure any nested invocations of perl don't overwrite the profile. |
| 295 | |
| 296 | =head1 NYTPROF ENVIRONMENT VARIABLE |
| 297 | |
| 298 | The behavior of Devel::NYTProf may be modified by setting the |
| 299 | environment variable C<NYTPROF>. It is possible to use this environment |
| 300 | variable to effect multiple setting by separating the values with a C<:>. For |
| 301 | example: |
| 302 | |
| 303 | export NYTPROF=trace=2:start=init:file=/tmp/nytprof.out |
| 304 | |
| 305 | Any colon or equal characters in a value can be escaped by preceding them with |
| 306 | a backslash. |
| 307 | |
| 308 | =head2 addpid=1 |
| 309 | |
| 310 | Append the current process id to the end of the filename. |
| 311 | |
| 312 | This avoids concurrent, or consecutive, processes from overwriting the same file. |
| 313 | If a fork is detected during profiling then the child process will automatically |
| 314 | add the process id to the filename. |
| 315 | |
| 316 | =head2 addtimestamp=1 |
| 317 | |
| 318 | Append the current time, as integer epoch seconds, to the end of the filename. |
| 319 | |
| 320 | =head2 trace=N |
| 321 | |
| 322 | Set trace level to N. 0 is off (the default). Higher values cause more detailed |
| 323 | trace output. Trace output is written to STDERR or wherever the L</log=F> |
| 324 | option has specified. |
| 325 | |
| 326 | =head2 log=F |
| 327 | |
| 328 | Specify the name of the file that L</trace=N> output should be written to. |
| 329 | |
| 330 | =head2 start=... |
| 331 | |
| 332 | Specify at which phase of program execution the profiler should be enabled: |
| 333 | |
| 334 | start=begin - start immediately (the default) |
| 335 | start=init - start at beginning of INIT phase (after compilation/use/BEGIN) |
| 336 | start=end - start at beginning of END phase |
| 337 | start=no - don't automatically start |
| 338 | |
| 339 | The start=no option is handy if you want to explicitly control profiling |
| 340 | by calling DB::enable_profile() and DB::disable_profile() yourself. |
| 341 | See L</RUN-TIME CONTROL OF PROFILING>. |
| 342 | |
| 343 | The start=init option is handy if you want to avoid profiling the loading and |
| 344 | initialization of modules. |
| 345 | |
| 346 | =head2 optimize=0 |
| 347 | |
| 348 | Disable the perl optimizer. |
| 349 | |
| 350 | By default NYTProf leaves perl's optimizer enabled. That gives you more |
| 351 | accurate profile timing overall, but can lead to I<odd> statement counts for |
| 352 | individual sets of lines. That's because the perl's peephole optimizer has |
| 353 | effectively rewritten the statements but you can't see what the rewritten |
| 354 | version looks like. |
| 355 | |
| 356 | For example: |
| 357 | |
| 358 | 1 if (...) { |
| 359 | 2 return; |
| 360 | 3 } |
| 361 | |
| 362 | may be rewritten as |
| 363 | |
| 364 | 1 return if (...) |
| 365 | |
| 366 | so the profile won't show a statement count for line 2 in your source code |
| 367 | because the C<return> was merged into the C<if> statement on the preceding line. |
| 368 | |
| 369 | Also 'empty' statements like C<1;> are removed entirely. Such statements are |
| 370 | empty because the optimizer has already removed the pointless constant in void |
| 371 | context. It then goes on to remove the now empty statement (in perl >= 5.13.7). |
| 372 | |
| 373 | Using the C<optimize=0> option disables the optimizer so you'll get lower |
| 374 | overall performance but more accurately assigned statement counts. |
| 375 | |
| 376 | If you find any other examples of the effect of optimizer on NYTProf output |
| 377 | (other than performance, obviously) please let us know. |
| 378 | |
| 379 | =head2 subs=0 |
| 380 | |
| 381 | Set to 0 to disable the collection of subroutine caller and timing details. |
| 382 | |
| 383 | =head2 blocks=1 |
| 384 | |
| 385 | Set to 1 to enable the determination of block and subroutine location per statement. |
| 386 | This makes the profiler about 50% slower (as of July 2008) and produces larger |
| 387 | output files, but you gain some valuable insight in where time is spent in the |
| 388 | blocks within large subroutines and scripts. |
| 389 | |
| 390 | =head2 stmts=0 |
| 391 | |
| 392 | Set to 0 to disable the statement profiler. (Implies C<blocks=0>.) |
| 393 | The reports won't contain any statement timing detail. |
| 394 | |
| 395 | This significantly reduces the overhead of the profiler and can also be useful |
| 396 | for profiling large applications that would normally generate a very large |
| 397 | profile data file. |
| 398 | |
| 399 | =head2 calls=N |
| 400 | |
| 401 | This option is I<new and experimental>. |
| 402 | |
| 403 | With calls=1 (the default) subroutine call I<return> events are emitted into |
| 404 | the data stream as they happen. With calls=2 subroutine call I<entry> events |
| 405 | are also emitted. With calls=0 no subroutine call events are produced. |
| 406 | This option depends on the C<subs> option being enabled, which it is by default. |
| 407 | |
| 408 | The L<nytprofcalls> utility can be used to process this data. It too is I<new |
| 409 | and experimental> and so likely to change. |
| 410 | |
| 411 | The subroutine profiler normally gathers data in memory and outputs a summary |
| 412 | when the profile data is being finalized, usually when the program has finished. |
| 413 | The summary contains aggregate information for all the calls from one location |
| 414 | to another, but the details of individual calls have been lost. |
| 415 | The calls option enables the recording of individual call events and thus |
| 416 | more detailed analysis and reporting of that data. |
| 417 | |
| 418 | =head2 leave=0 |
| 419 | |
| 420 | Set to 0 to disable the extra work done by the statement profiler |
| 421 | to allocate times accurately when |
| 422 | returning into the middle of statement. For example leaving a subroutine |
| 423 | and returning into the middle of statement, or re-evaluating a loop condition. |
| 424 | |
| 425 | This feature also ensures that in embedded environments, such as mod_perl, |
| 426 | the last statement executed doesn't accumulate the time spent 'outside perl'. |
| 427 | |
| 428 | =head2 findcaller=1 |
| 429 | |
| 430 | Force NYTProf to recalculate the name of the caller of the each sub instead of |
| 431 | 'inheriting' the name calculated when the caller was entered. (Rarely needed, |
| 432 | but might be useful in some odd cases.) |
| 433 | |
| 434 | =head2 use_db_sub=1 |
| 435 | |
| 436 | Set to 1 to enable use of the traditional DB::DB() subroutine to perform |
| 437 | profiling, instead of the faster 'opcode redirection' technique that's used by |
| 438 | default. Also effectively sets C<leave=0> (see above). |
| 439 | |
| 440 | The default 'opcode redirection' technique can't profile subroutines that were |
| 441 | compiled before NYTProf was loaded. So using use_db_sub=1 can be useful in |
| 442 | cases where you can't load the profiler early in the life of the application. |
| 443 | |
| 444 | Another side effect of C<use_db_sub=1> is that it enables recording of the |
| 445 | source code of the C<< perl -e '...' >> and C<< perl - >> input for old |
| 446 | versions of perl. See also L</savesrc=0>. |
| 447 | |
| 448 | =head2 savesrc=0 |
| 449 | |
| 450 | Disable the saving of source code. |
| 451 | |
| 452 | By default NYTProf saves a copy of all source code into the profile data file. |
| 453 | This makes the file self-contained, so the reporting tools no longer depend on |
| 454 | having the unmodified source code files available. |
| 455 | |
| 456 | With C<savesrc=0> some source code is still saved: the arguments to the |
| 457 | C<perl -e> option, the script fed to perl via STDIN when using C<perl ->, |
| 458 | and the source code of string evals. |
| 459 | |
| 460 | Saving the source code of string evals requires perl version 5.8.9+, 5.10.1+, |
| 461 | or 5.12 or later. |
| 462 | |
| 463 | Saving the source code of the C<< perl -e '...' >> or C<< perl - >> input |
| 464 | requires either a recent perl version, as above, or setting the L</use_db_sub=1> option. |
| 465 | |
| 466 | =head2 slowops=N |
| 467 | |
| 468 | Profile perl opcodes that can be slow. These include opcodes that make system |
| 469 | calls, such as C<print>, C<read>, C<sysread>, C<socket> etc., plus regular |
| 470 | expression opcodes like C<subst> and C<match>. |
| 471 | |
| 472 | If C<N> is 0 then slowops profiling is disabled. |
| 473 | |
| 474 | If C<N> is 1 then all the builtins are treated as being defined in the C<CORE> |
| 475 | package. So times for C<print> calls from anywhere in your code are merged and |
| 476 | accounted for as calls to an xsub called C<CORE::print>. |
| 477 | |
| 478 | If C<N> is 2 (the default) then builtins are treated as being defined in the |
| 479 | package that calls them. So calls to C<print> from package C<Foo> are treated |
| 480 | as calls to an xsub called C<Foo::CORE:print>. Note the single colon after CORE. |
| 481 | |
| 482 | The opcodes are currently profiled using their internal names, so C<printf> is C<prtf> |
| 483 | and the C<-x> file test is C<fteexec>. This may change in future. |
| 484 | |
| 485 | Opcodes that call subroutines, perhaps by triggering a FETCH from a tied |
| 486 | variable, currently appear in the call tree as the caller of the sub. This is |
| 487 | likely to change in future. |
| 488 | |
| 489 | =head2 usecputime=1 |
| 490 | |
| 491 | This option has been removed. Profiling won't be enabled if set. |
| 492 | |
| 493 | Use the L</clock=N> option to select a high-resolution CPU time clock, if |
| 494 | available on your system, instead. That will give you higher resolution and work |
| 495 | for the subroutine profiler as well. |
| 496 | |
| 497 | =head2 file=... |
| 498 | |
| 499 | Specify the output file to write profile data to (default: './nytprof.out'). |
| 500 | |
| 501 | =head2 compress=... |
| 502 | |
| 503 | Specify the compression level to use, if NYTProf is compiled with compression |
| 504 | support. Valid values are 0 to 9, with 0 disabling compression. The default is |
| 505 | 6 as higher values yield little extra compression but the cpu cost starts to |
| 506 | rise significantly. Using level 1 still gives you a significant reduction in file size. |
| 507 | |
| 508 | If NYTProf was not compiled with compression support, this option is silently ignored. |
| 509 | |
| 510 | =head2 clock=N |
| 511 | |
| 512 | Systems which support the C<clock_gettime()> system call typically |
| 513 | support several clocks. By default NYTProf uses CLOCK_MONOTONIC. |
| 514 | |
| 515 | This option enables you to select a different clock by specifying the |
| 516 | integer id of the clock (which may vary between operating system types). |
| 517 | If the clock you select isn't available then CLOCK_REALTIME is used. |
| 518 | |
| 519 | See L</CLOCKS> for more information. |
| 520 | |
| 521 | =head2 sigexit=1 |
| 522 | |
| 523 | When perl exits normally it runs any code defined in C<END> blocks. |
| 524 | NYTProf defines an END block that finishes profiling and writes out the final |
| 525 | profile data. |
| 526 | |
| 527 | If the process ends due to a signal then END blocks are not executed so the |
| 528 | profile will be incomplete and unusable. The C<sigexit> option tells NYTProf |
| 529 | to catch some signals (e.g. INT, HUP, PIPE, SEGV, BUS) and ensure a usable |
| 530 | profile by executing: |
| 531 | |
| 532 | DB::finish_profile(); |
| 533 | exit 1; |
| 534 | |
| 535 | You can also specify which signals to catch in this way by listing them, |
| 536 | separated by commas, as the value of the option (case is not significant): |
| 537 | |
| 538 | sigexit=int,hup |
| 539 | |
| 540 | =head2 posix_exit=1 |
| 541 | |
| 542 | The NYTProf subroutine profiler normally detects calls to C<POSIX::_exit()> |
| 543 | (which exits the process without running END blocks) and automatically calls |
| 544 | C<DB::finish_profile()> for you, so NYTProf 'just works'. |
| 545 | |
| 546 | When using the C<subs=0> option to disable the subroutine profiler the |
| 547 | C<posix_exit> option can be used to tell NYTProf to take other steps to arrange |
| 548 | for C<DB::finish_profile()> to be called before C<POSIX::_exit()>. |
| 549 | |
| 550 | =head2 libcexit=1 |
| 551 | |
| 552 | Arranges for L</finish_profile> to be called via the C library C<atexit()> function. |
| 553 | This may help some tricky cases where the process may exit without perl |
| 554 | executing the C<END> block that NYTProf uses to call /finish_profile(). |
| 555 | |
| 556 | =head2 endatexit=1 |
| 557 | |
| 558 | Sets the PERL_EXIT_DESTRUCT_END flag in the PL_exit_flags of the perl interpreter. |
| 559 | This makes perl run C<END> blocks in perl_destruct() instead of perl_run() |
| 560 | which may help in cases, like Apache, where perl is embedded but perl_run() |
| 561 | isn't called. |
| 562 | |
| 563 | =head2 forkdepth=N |
| 564 | |
| 565 | When a perl process that is being profiled executes a fork() the child process |
| 566 | is also profiled. The forkdepth option can be used to control this. If |
| 567 | forkdepth is zero then profiling will be disabled in the child process. |
| 568 | |
| 569 | If forkdepth is greater than zero then profiling will be enabled in the child |
| 570 | process and the forkdepth value in that process is decremented by one. |
| 571 | |
| 572 | If forkdepth is -1 (the default) then there's no limit on the number of |
| 573 | generations of children that are profiled. |
| 574 | |
| 575 | =head2 nameevals=0 |
| 576 | |
| 577 | The 'file name' of a string eval is normally a string like "C<(eval N)>", where |
| 578 | C<N> is a sequence number. By default NYTProf asks perl to give evals more |
| 579 | informative names like "C<(eval N)[file:line]>", where C<file> and C<line> are |
| 580 | the file and line number where the string C<eval> was executed. |
| 581 | |
| 582 | The C<nameevals=0> option can be used to disable the more informative names and |
| 583 | return to the default behaviour. This may be need in rare cases where the |
| 584 | application code is sensitive to the name given to a C<eval>. (The most common |
| 585 | case in when running test suites undef NYTProf.) |
| 586 | |
| 587 | The downside is that the NYTProf reporting tools are less useful and may get |
| 588 | confused if this option is used. |
| 589 | |
| 590 | =head2 nameanonsubs=0 |
| 591 | |
| 592 | The name of a anonymous subroutine is normally "C<__ANON__>". By default |
| 593 | NYTProf asks perl to give anonymous subroutines more informative names like |
| 594 | "C<__ANON__[file:line]>", where C<file> and C<line> are the file and line |
| 595 | number where the anonymous subroutine was defined. |
| 596 | |
| 597 | The C<nameanonsubs=0> option can be used to disable the more informative names |
| 598 | and return to the default behaviour. This may be need in rare cases where the |
| 599 | application code is sensitive to the name given to a anonymous subroutines. |
| 600 | (The most common case in when running test suites undef NYTProf.) |
| 601 | |
| 602 | The downside is that the NYTProf reporting tools are less useful and may get |
| 603 | confused if this option is used. |
| 604 | |
| 605 | =head1 RUN-TIME CONTROL OF PROFILING |
| 606 | |
| 607 | You can profile only parts of an application by calling DB::disable_profile() |
| 608 | to stop collecting profile data, and calling DB::enable_profile() to start |
| 609 | collecting profile data. |
| 610 | |
| 611 | Using the C<start=no> option lets you leave the profiler disabled initially |
| 612 | until you call DB::enable_profile() at the right moment. You still need to |
| 613 | load Devel::NYTProf as early as possible, even if you don't call |
| 614 | DB::enable_profile() until much later. That's because any code that's compiled |
| 615 | before Devel::NYTProf is loaded will not be profiled by default. See also |
| 616 | L</use_db_sub=1>. |
| 617 | |
| 618 | The profile output file can't be used until it's been properly completed and |
| 619 | closed. Calling DB::disable_profile() doesn't do that. To make a profile file |
| 620 | usable before the profiled application has completed you can call |
| 621 | DB::finish_profile(). Alternatively you could call DB::enable_profile($newfile). |
| 622 | |
| 623 | Always call the DB::enable_profile(), DB::disable_profile() or |
| 624 | DB::finish_profile() function with the C<DB::> prefix as shown because you |
| 625 | can't import them. They're provided automatically when NYTProf is in use. |
| 626 | |
| 627 | =head2 disable_profile |
| 628 | |
| 629 | DB::disable_profile() |
| 630 | |
| 631 | Stops collection of profile data until DB:enable_profile() is called. |
| 632 | |
| 633 | Subroutine calls which were made while profiling was enabled and are still on |
| 634 | the call stack (have not yet exited) will still have their profile data |
| 635 | collected when they exit. Compare with L</finish_profile> below. |
| 636 | |
| 637 | =head2 enable_profile |
| 638 | |
| 639 | DB::enable_profile($newfile) |
| 640 | DB::enable_profile() |
| 641 | |
| 642 | Enables collection of profile data. If $newfile is specified the profile data will be |
| 643 | written to $newfile (after completing and closing the previous file, if any). |
| 644 | If $newfile already exists it will be deleted first. |
| 645 | If DB::enable_profile() is called without a filename argument then profile data |
| 646 | will continue to be written to the current file (nytprof.out by default). |
| 647 | |
| 648 | =head2 finish_profile |
| 649 | |
| 650 | DB::finish_profile() |
| 651 | |
| 652 | Calls DB::disable_profile(), then completes the profile data file by writing |
| 653 | subroutine profile data, and then closes the file. The in memory subroutine |
| 654 | profile data is then discarded. |
| 655 | |
| 656 | Normally NYTProf arranges to call finish_profile() for you via an END block. |
| 657 | |
| 658 | =head1 DATA COLLECTION AND INTERPRETATION |
| 659 | |
| 660 | NYTProf tries very hard to gather accurate information. The nature of the |
| 661 | internals of perl mean that, in some cases, the information that's gathered is |
| 662 | accurate but surprising. In some cases it can appear to be misleading. |
| 663 | (Of course, in some cases it may actually be plain wrong. Caveat lector.) |
| 664 | |
| 665 | =head2 If Statement and Subroutine Timings Don't Match |
| 666 | |
| 667 | NYTProf has two profilers: a statement profiler that's invoked when perl moves |
| 668 | from one perl statement to another, and a subroutine profiler that's invoked |
| 669 | when perl calls or returns from a subroutine. |
| 670 | |
| 671 | The individual statement timings for a subroutine usually add up to slightly |
| 672 | less than the exclusive time for the subroutine. That's because the handling of |
| 673 | the subroutine call and return overheads is included in the exclusive time for |
| 674 | the subroutine. The difference may only be a few microseconds but that may |
| 675 | become noticeable for subroutines that are called hundreds of thousands of times. |
| 676 | |
| 677 | The statement profiler keeps track how much time was spent on overheads, like |
| 678 | writing statement profile data to disk. The subroutine profiler subtracts the |
| 679 | overheads that have accumulated between entering and leaving the subroutine in |
| 680 | order to give a more accurate profile. The statement profiler is generally |
| 681 | very fast because most writes get buffered for zip compression so the profiler |
| 682 | overhead per statement tends to be very small, often a single 'tick'. |
| 683 | The result is that the accumulated overhead is quite noisy. This becomes more |
| 684 | significant for subroutines that are called frequently and are also fast. |
| 685 | This may be another, smaller, contribution to the discrepancy between statement |
| 686 | time and exclusive times. |
| 687 | |
| 688 | =head2 If Headline Subroutine Timings Don't Match the Called Subs |
| 689 | |
| 690 | Overall subroutine times are reported with a headline like C<spent 10s (2+8) within ...>. |
| 691 | In this example, 10 seconds were spent inside the subroutine (the "inclusive |
| 692 | time") and, of that, 8 seconds were spent in subroutines called by this one. |
| 693 | That leaves 2 seconds as the time spent in the subroutine code itself (the |
| 694 | "exclusive time", sometimes also called the "self time"). |
| 695 | |
| 696 | The report shows the source code of the subroutine. Lines that make calls to |
| 697 | other subroutines are annotated with details of the time spent in those calls. |
| 698 | |
| 699 | Sometimes the sum of the times for calls made by the lines of code in the |
| 700 | subroutine is less than the inclusive-exclusive time reported in the headline |
| 701 | (10-2 = 8 seconds in the example above). |
| 702 | |
| 703 | What's happening here is that calls to other subroutines are being made but |
| 704 | NYTProf isn't able to determine the calling location correctly so the calls |
| 705 | don't appear in the report in the correct place. |
| 706 | |
| 707 | Using an old version of perl is one cause (see below). Another is calling |
| 708 | subroutines that exit via C<goto ⊂> - most frequently encountered in |
| 709 | AUTOLOAD subs and code using the L<Memoize> module. |
| 710 | |
| 711 | In general the overall subroutine timing is accurate and should be trusted more |
| 712 | than the sum of statement or nested sub call timings. |
| 713 | |
| 714 | =head2 Perl 5.10.1+ (or else 5.8.9+) is Recommended |
| 715 | |
| 716 | These versions of perl yield much more detailed information about calls to |
| 717 | BEGIN, CHECK, INIT, and END blocks, the code handling tied or overloaded |
| 718 | variables, and callbacks from XS code. |
| 719 | |
| 720 | Perl 5.12 will hopefully also fix an inaccuracy in the timing of the last |
| 721 | statement and the condition clause of some kinds of loops: |
| 722 | L<http://rt.perl.org/rt3/Ticket/Display.html?id=60954> |
| 723 | |
| 724 | =head2 eval $string |
| 725 | |
| 726 | Perl treats each execution of a string eval (C<eval $string;> not C<eval { ... }>) |
| 727 | as a distinct file, so NYTProf does as well. The 'files' are given names with |
| 728 | this structure: |
| 729 | |
| 730 | (eval $sequence)[$filename:$line] |
| 731 | |
| 732 | for example "C<(eval 93)[/foo/bar.pm:42]>" would be the name given to the |
| 733 | 93rd execution of a string eval by that process and, in this case, the 93rd |
| 734 | eval happened to be one at line 42 of "/foo/bar.pm". |
| 735 | |
| 736 | Nested string evals can give rise to file names like |
| 737 | |
| 738 | (eval 1047)[(eval 93)[/foo/bar.pm:42]:17] |
| 739 | |
| 740 | =head3 Merging Evals |
| 741 | |
| 742 | Some applications execute a great many string eval statements. If NYTProf generated |
| 743 | a report page for each one it would not only slow report generation but also |
| 744 | make the overall report less useful by scattering performance data too widely. |
| 745 | On the other hand, being able to see the actual source code executed by an |
| 746 | eval, along with the timing details, is often I<very> useful. |
| 747 | |
| 748 | To try to balance these conflicting needs, NYTProf currently I<merges |
| 749 | uninteresting string eval siblings>. |
| 750 | |
| 751 | What does that mean? Well, for each source code line that executed any string |
| 752 | evals, NYTProf first gathers the corresponding eval 'files' for that line |
| 753 | (known as the 'siblings') into groups keyed by distinct source code. |
| 754 | |
| 755 | Then, for each of those groups of siblings, NYTProf will 'merge' a group |
| 756 | that shares the same source code and doesn't execute any string evals itself. |
| 757 | Merging means to pick one sibling as the survivor and merge and delete all |
| 758 | the data from the others into it. |
| 759 | |
| 760 | If there are a large number of sibling groups then the data for all of them are |
| 761 | merged into one regardless. |
| 762 | |
| 763 | The report annotations will indicate when evals have been merged together. |
| 764 | |
| 765 | =head3 Merging Anonymous Subroutines |
| 766 | |
| 767 | Anonymous subroutines defined within string evals have names like this: |
| 768 | |
| 769 | main::__ANON__[(eval 75)[/foo/bar.pm:42]:12] |
| 770 | |
| 771 | That anonymous subroutine was defined on line 12 of the source code executed by |
| 772 | the string eval on line 42 of F</foo/bar.pm>. That was the 75th string eval |
| 773 | executed by the program. |
| 774 | |
| 775 | Anonymous subroutines I<defined on the same line of sibling evals that get |
| 776 | merged> are also merged. That is, the profile information is merged into |
| 777 | one and the others are discarded. |
| 778 | |
| 779 | =head3 Timing |
| 780 | |
| 781 | Care should be taken when interpreting the report annotations associated with a |
| 782 | string eval statement. Normally the report annotations embedded into the |
| 783 | source code related to timings from the I<subroutine> profiler. This isn't |
| 784 | (currently) true of annotations for string eval statements. |
| 785 | |
| 786 | This makes a significant different if the eval defines any subroutines that get |
| 787 | called I<after> the eval has returned. Because the time shown for a string eval |
| 788 | is based on the I<statement> times it will include time spent executing |
| 789 | statements within the subs defined by the eval. |
| 790 | |
| 791 | In future NYTProf may involve the subroutine profiler in timings evals and so |
| 792 | be able to avoid this issue. |
| 793 | |
| 794 | =head2 Calls from XSUBs and Opcodes |
| 795 | |
| 796 | Calls record the current filename and line number of the perl code at the time |
| 797 | the call was made. That's fine and accurate for calls from perl code. For calls |
| 798 | that originate from C code however, such as an XSUB or an opcode, the filename and |
| 799 | line number recorded are still those of the last I<perl> statement executed. |
| 800 | |
| 801 | For example, a line that calls an xsub will appear in reports to also have also |
| 802 | called any subroutines that that xsub called. This can be construed as a feature. |
| 803 | |
| 804 | As an extreme example, the first time a regular expression that uses character |
| 805 | classes is executed on a unicode string you'll find profile data like this: |
| 806 | |
| 807 | # spent 1ms within main::BEGIN@4 which was called |
| 808 | # once (1ms+0s) by main::CORE:subst at line 0 |
| 809 | 4 s/ (?: [A-Z] | [\d] )+ (?= [\s] ) //x; |
| 810 | # spent 38.8ms making 1 call to main::CORE:subst |
| 811 | # spent 25.4ms making 2 calls to utf8::SWASHNEW, avg 12.7ms/call |
| 812 | # spent 12.4ms making 1 call to utf8::AUTOLOAD |
| 813 | |
| 814 | =for comment |
| 815 | No doubt more odd cases will be added here over time. |
| 816 | |
| 817 | =head1 MAKING NYTPROF FASTER |
| 818 | |
| 819 | You can reduce the cost of profiling by adjusting some options. The trade-off |
| 820 | is reduced detail and/or accuracy in reports. |
| 821 | |
| 822 | If you don't need statement-level profiling then you can disable it via L</stmts=0>. |
| 823 | To further boost statement-level profiling performance try L</leave=0> but note that |
| 824 | I<will> apportion timings for some kinds of statements less accurate). |
| 825 | |
| 826 | If you don't need call stacks or flamegraph then disable it via L<calls=0|/calls=N>. |
| 827 | If you don't need subroutine profiling then you can disable it via L</subs=0>. |
| 828 | If you do need it but don't need timings for perl opcodes then set L<slowops=0|/slowops=N>. |
| 829 | |
| 830 | Generally speaking, setting calls=0 and slowops=0 will give you a useful boost |
| 831 | with the least loss of detail. |
| 832 | |
| 833 | Another approach is to only enable NYTProf in the sections of code that |
| 834 | interest you. See L</RUN-TIME CONTROL OF PROFILING> for more details. |
| 835 | |
| 836 | To speed up L<nytprofhtml> try using the --minimal (-m) or --no-flame options. |
| 837 | |
| 838 | =head1 REPORTS |
| 839 | |
| 840 | The L<Devel::NYTProf::Data> module provides a low-level interface for loading |
| 841 | the profile data. |
| 842 | |
| 843 | The L<Devel::NYTProf::Reader> module provides an interface for generating |
| 844 | arbitrary reports. This means that you can implement your own output format in |
| 845 | perl. (Though the module is in a state of flux and may be deprecated soon.) |
| 846 | |
| 847 | Included in the bin directory of this distribution are some scripts which |
| 848 | turn the raw profile data into more useful formats: |
| 849 | |
| 850 | =head2 nytprofhtml |
| 851 | |
| 852 | Creates attractive, richly annotated, and fully cross-linked html |
| 853 | reports (including statistics, source code and color highlighting). |
| 854 | This is the main report generation tool for NYTProf. |
| 855 | |
| 856 | =head2 nytprofcg |
| 857 | |
| 858 | Translates a profile into a format that can be loaded into KCachegrind |
| 859 | L<http://kcachegrind.github.io/> |
| 860 | |
| 861 | =head2 nytprofcalls |
| 862 | |
| 863 | Reads a profile and processes the calls events it contains. |
| 864 | |
| 865 | =head2 nytprofmerge |
| 866 | |
| 867 | Reads multiple profile data files and writes out a new file containing the merged profile data. |
| 868 | |
| 869 | =head1 LIMITATIONS |
| 870 | |
| 871 | =head2 Threads and Multiplicity |
| 872 | |
| 873 | C<Devel::NYTProf> is not currently thread safe or multiplicity safe. |
| 874 | If you'd be interested in helping to fix that then please get in |
| 875 | touch with us. Meanwhile, profiling is disabled when a thread is created, and |
| 876 | NYTProf tries to ignore any activity from perl interpreters other than the |
| 877 | first one that loaded it. |
| 878 | |
| 879 | =head2 Coro |
| 880 | |
| 881 | The C<Devel::NYTProf> subroutine profiler gets confused by the stack gymnastics |
| 882 | performed by the L<Coro> module and aborts. When profiling applications that |
| 883 | use Coro you should disable the subroutine profiler using the L</subs=0> option. |
| 884 | |
| 885 | =head2 FCGI::Engine |
| 886 | |
| 887 | Using C<open('-|')> in code running under L<FCGI::Engine> causes a panic in nytprofcalls. |
| 888 | See https://github.com/timbunce/devel-nytprof/issues/20 for more information. |
| 889 | |
| 890 | =head2 For perl < 5.8.8 it may change what caller() returns |
| 891 | |
| 892 | For example, the L<Readonly> module croaks with "Invalid tie" when profiled with |
| 893 | perl versions before 5.8.8. That's because L<Readonly> explicitly checking for |
| 894 | certain values from caller(). The L<NEXT> module is also affected. |
| 895 | |
| 896 | =head2 For perl < 5.10.1 it can't see some implicit calls and callbacks |
| 897 | |
| 898 | For perl versions prior to 5.8.9 and 5.10.1, some implicit subroutine calls |
| 899 | can't be seen by the I<subroutine> profiler. Technically this affects calls |
| 900 | made via the various perl C<call_*()> internal APIs. |
| 901 | |
| 902 | For example, BEGIN/CHECK/INIT/END blocks, the C<TIE>I<whatever> subroutine |
| 903 | called by C<tie()>, all calls made via operator overloading, and callbacks from |
| 904 | XS code, are not seen. |
| 905 | |
| 906 | The effect is that time in those subroutines is accumulated by the |
| 907 | subs that triggered the call to them. So time spent in calls invoked by |
| 908 | perl to handle overloading are accumulated by the subroutines that trigger |
| 909 | overloading (so it is measured, but the cost is dispersed across possibly many |
| 910 | calling locations). |
| 911 | |
| 912 | Although the calls aren't seen by the subroutine profiler, the individual |
| 913 | I<statements> executed by the code in the called subs are profiled by the |
| 914 | statement profiler. |
| 915 | |
| 916 | =head2 #line directives |
| 917 | |
| 918 | The reporting code currently doesn't handle #line directives, but at least it |
| 919 | warns about them. Patches welcome. |
| 920 | |
| 921 | =head2 Freed values in @_ may be mutated |
| 922 | |
| 923 | Perl has a class of bugs related to the fact that values placed in the stack |
| 924 | are not reference counted. Consider this example: |
| 925 | |
| 926 | @a = (1..9); sub s { undef @a; print $_ for @_ } s(@a); |
| 927 | |
| 928 | The C<undef @a> frees the values that C<@_> refers to. Perl can sometimes |
| 929 | detect when a freed value is accessed and treats it as an undef. However, if |
| 930 | the freed value is assigned some new value then @_ is effectively corrupted. |
| 931 | |
| 932 | NYTProf allocates new values while it's profiling, in order to record program |
| 933 | activity, and so may appear to corrupt C<@_> in this (rare) situation. If this |
| 934 | happens, NYTProf is simply exposing an existing problem in the code. |
| 935 | |
| 936 | =head2 Lvalue subroutines aren't profiled when using use_db_sub=1 |
| 937 | |
| 938 | Currently 'lvalue' subroutines (subs that can be assigned to, like C<foo() = |
| 939 | 42>) are not profiled when using use_db_sub=1. |
| 940 | |
| 941 | =head1 CLOCKS |
| 942 | |
| 943 | Here we discuss the way NYTProf gets high-resolution timing information from |
| 944 | your system and related issues. |
| 945 | |
| 946 | =head2 POSIX Clocks |
| 947 | |
| 948 | These are the clocks that your system may support if it supports the POSIX |
| 949 | C<clock_gettime()> function. Other clock sources are listed in the |
| 950 | L</Other Clocks> section below. |
| 951 | |
| 952 | The C<clock_gettime()> interface allows clocks to return times to nanosecond |
| 953 | precision. Of course few offer nanosecond I<accuracy> but the extra precision |
| 954 | helps reduce the cumulative error that naturally occurs when adding together |
| 955 | many timings. When using these clocks NYTProf outputs timings as a count of 100 |
| 956 | nanosecond ticks. |
| 957 | |
| 958 | =head3 CLOCK_MONOTONIC |
| 959 | |
| 960 | CLOCK_MONOTONIC represents the amount of time since an unspecified point in |
| 961 | the past (typically system start-up time). It increments uniformly |
| 962 | independent of adjustments to 'wallclock time'. NYTProf will use this clock by |
| 963 | default, if available. |
| 964 | |
| 965 | =head3 CLOCK_REALTIME |
| 966 | |
| 967 | CLOCK_REALTIME is typically the system's main high resolution 'wall clock time' |
| 968 | source. The same source as used for the gettimeofday() call used by most kinds |
| 969 | of perl benchmarking and profiling tools. |
| 970 | |
| 971 | The problem with real time is that it's far from simple. It tends to drift and |
| 972 | then be reset to match 'reality', either sharply or by small adjustments (via the |
| 973 | adjtime() system call). |
| 974 | |
| 975 | Surprisingly, it can also go backwards, for reasons explained in |
| 976 | http://preview.tinyurl.com/5wawnn so CLOCK_MONOTONIC is preferred. |
| 977 | |
| 978 | =head3 CLOCK_VIRTUAL |
| 979 | |
| 980 | CLOCK_VIRTUAL increments only when the CPU is running in user mode on behalf of the calling process. |
| 981 | |
| 982 | =head3 CLOCK_PROF |
| 983 | |
| 984 | CLOCK_PROF increments when the CPU is running in user I<or> kernel mode. |
| 985 | |
| 986 | =head3 CLOCK_PROCESS_CPUTIME_ID |
| 987 | |
| 988 | CLOCK_PROCESS_CPUTIME_ID represents the amount of execution time of the process associated with the clock. |
| 989 | |
| 990 | =head3 CLOCK_THREAD_CPUTIME_ID |
| 991 | |
| 992 | CLOCK_THREAD_CPUTIME_ID represents the amount of execution time of the thread associated with the clock. |
| 993 | |
| 994 | =head3 Finding Available POSIX Clocks |
| 995 | |
| 996 | On unix-like systems you can find the CLOCK_* clocks available on you system |
| 997 | using a command like: |
| 998 | |
| 999 | grep -r 'define *CLOCK_' /usr/include |
| 1000 | |
| 1001 | Look for a group that includes CLOCK_REALTIME. The integer values listed are |
| 1002 | the clock ids that you can use with the C<clock=N> option. |
| 1003 | |
| 1004 | A future version of NYTProf should be able to list the supported clocks. |
| 1005 | |
| 1006 | =head2 Other Clocks |
| 1007 | |
| 1008 | This section lists other clock sources that NYTProf may use. |
| 1009 | |
| 1010 | If your system doesn't support clock_gettime() then NYTProf will use |
| 1011 | gettimeofday(), or the nearest equivalent, |
| 1012 | |
| 1013 | =head3 gettimeofday |
| 1014 | |
| 1015 | This is the traditional high resolution time of day interface for most |
| 1016 | unix-like systems. With this clock NYTProf outputs timings as a count of 1 |
| 1017 | microsecond ticks. |
| 1018 | |
| 1019 | =head3 mach_absolute_time |
| 1020 | |
| 1021 | On Mac OS X the mach_absolute_time() function is used. With this clock NYTProf |
| 1022 | outputs timings as a count of 100 nanosecond ticks. |
| 1023 | |
| 1024 | =head3 Time::HiRes |
| 1025 | |
| 1026 | On systems which don't support other clocks, NYTProf falls back to using the |
| 1027 | L<Time::HiRes> module. With this clock NYTProf outputs timings as a count of 1 |
| 1028 | microsecond ticks. |
| 1029 | |
| 1030 | =head2 Clock References |
| 1031 | |
| 1032 | Relevant specifications and manual pages: |
| 1033 | |
| 1034 | http://www.opengroup.org/onlinepubs/000095399/functions/clock_getres.html |
| 1035 | http://linux.die.net/man/3/clock_gettime |
| 1036 | |
| 1037 | Why 'realtime' can appear to go backwards: |
| 1038 | |
| 1039 | http://preview.tinyurl.com/5wawnn |
| 1040 | |
| 1041 | The PostgreSQL pg_test_timing utility documentation has a good summary of timing issues: |
| 1042 | |
| 1043 | http://www.postgresql.org/docs/9.2/static/pgtesttiming.html |
| 1044 | |
| 1045 | =for comment |
| 1046 | http://preview.tinyurl.com/5wawnn redirects to: |
| 1047 | http://groups.google.com/group/comp.os.linux.development.apps/tree/browse_frm/thread/dc29071f2417f75f/ac44671fdb35f6db?rnum=1&_done=%2Fgroup%2Fcomp.os.linux.development.apps%2Fbrowse_frm%2Fthread%2Fdc29071f2417f75f%2Fc46264dba0863463%3Flnk%3Dst%26rnum%3D1%26 |
| 1048 | |
| 1049 | =for comment - these links seem broken |
| 1050 | http://webnews.giga.net.tw/article//mailing.freebsd.performance/710 |
| 1051 | http://sean.chittenden.org/news/2008/06/01/ |
| 1052 | |
| 1053 | =head1 CAVEATS |
| 1054 | |
| 1055 | =head2 SMP Systems |
| 1056 | |
| 1057 | On systems with multiple processors, which includes most modern machines, |
| 1058 | (from Linux docs though applicable to most SMP systems): |
| 1059 | |
| 1060 | The CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID clocks are realized on |
| 1061 | many platforms using timers from the CPUs (TSC on i386, AR.ITC on Itanium). |
| 1062 | These registers may differ between CPUs and as a consequence these clocks may |
| 1063 | return bogus results if a process is migrated to another CPU. |
| 1064 | |
| 1065 | If the CPUs in an SMP system have different clock sources then there is no way |
| 1066 | to maintain a correlation between the timer registers since each CPU will run |
| 1067 | at a slightly different frequency. If that is the case then |
| 1068 | clock_getcpuclockid(0) will return ENOENT to signify this condition. The two |
| 1069 | clocks will then only be useful if it can be ensured that a process stays on a |
| 1070 | certain CPU. |
| 1071 | |
| 1072 | The processors in an SMP system do not start all at exactly the same time and |
| 1073 | therefore the timer registers are typically running at an offset. Some |
| 1074 | architectures include code that attempts to limit these offsets on bootup. |
| 1075 | However, the code cannot guarantee to accurately tune the offsets. Glibc |
| 1076 | contains no provisions to deal with these offsets (unlike the Linux Kernel). |
| 1077 | Typically these offsets are small and therefore the effects may be negligible |
| 1078 | in most cases. |
| 1079 | |
| 1080 | In summary, SMP systems are likely to give 'noisy' profiles. |
| 1081 | Setting a L<Processor Affinity> may help. |
| 1082 | |
| 1083 | =head3 Processor Affinity |
| 1084 | |
| 1085 | Processor affinity is an aspect of task scheduling on SMP systems. |
| 1086 | "Processor affinity takes advantage of the fact that some remnants of a process |
| 1087 | may remain in one processor's state (in particular, in its cache) from the last |
| 1088 | time the process ran, and so scheduling it to run on the same processor the |
| 1089 | next time could result in the process running more efficiently than if it were |
| 1090 | to run on another processor." (From http://en.wikipedia.org/wiki/Processor_affinity) |
| 1091 | |
| 1092 | Setting an explicit processor affinity can avoid the problems described in |
| 1093 | L</SMP Systems>. |
| 1094 | |
| 1095 | Processor affinity can be set using the C<taskset> command on Linux. |
| 1096 | |
| 1097 | Note that processor affinity is inherited by child processes, so if the process |
| 1098 | you're profiling spawns cpu intensive sub processes then your process will be |
| 1099 | impacted by those more than it otherwise would. |
| 1100 | |
| 1101 | =head3 Windows |
| 1102 | |
| 1103 | B<THIS SECTION DOESN'T MATCH THE CODE> |
| 1104 | |
| 1105 | On Windows NYTProf uses Time::HiRes which uses the windows |
| 1106 | QueryPerformanceCounter() API with some extra logic to adjust for the current |
| 1107 | clock speed and try to resync the raw counter to wallclock time every so often |
| 1108 | (every 30 seconds or if the timer drifts by more than 0.5 of a seconds). |
| 1109 | This extra logic may lead to occasional spurious results. |
| 1110 | |
| 1111 | (It would be great if someone could contribute a patch to NYTProf to use |
| 1112 | QueryPerformanceCounter() directly and avoid the overheads and resyncing |
| 1113 | behaviour of Time::HiRes.) |
| 1114 | |
| 1115 | =head2 Virtual Machines |
| 1116 | |
| 1117 | I recommend you don't do performance profiling while running in a |
| 1118 | virtual machine. If you do you're likely to find inexplicable spikes |
| 1119 | of real-time appearing at unreasonable places in your code. You should pay |
| 1120 | less attention to the statement timings and rely more on the subroutine |
| 1121 | timings. They will still be noisy but less so than the statement times. |
| 1122 | |
| 1123 | You could also try using the C<clock=N> option to select a high-resolution |
| 1124 | I<cpu-time> clock instead of a real-time one. That should be much less |
| 1125 | noisy, though you will lose visibility of wait-times due to network |
| 1126 | and disk I/O, for example. |
| 1127 | |
| 1128 | =head1 BUGS |
| 1129 | |
| 1130 | Possibly. All complex software has bugs. Let me know if you find one. |
| 1131 | |
| 1132 | =head1 SEE ALSO |
| 1133 | |
| 1134 | Screenshots of L<nytprofhtml> v2.01 reports can be seen at |
| 1135 | L<http://timbunce.files.wordpress.com/2008/07/nytprof-perlcritic-index.png> and |
| 1136 | L<http://timbunce.files.wordpress.com/2008/07/nytprof-perlcritic-all-perl-files.png>. |
| 1137 | A writeup of the new features of NYTProf v2 can be found at |
| 1138 | L<http://blog.timbunce.org/2008/07/15/nytprof-v2-a-major-advance-in-perl-profilers/> |
| 1139 | and the background story, explaining the "why", can be found at |
| 1140 | L<http://blog.timbunce.org/2008/07/16/nytprof-v2-the-background-story/>. |
| 1141 | |
| 1142 | Mailing list and discussion at L<http://groups.google.com/group/develnytprof-dev> |
| 1143 | |
| 1144 | Blog posts L<http://blog.timbunce.org/tag/nytprof/> |
| 1145 | |
| 1146 | Public Github Repository and hacking instructions at L<https://github.com/timbunce/devel-nytprof/> |
| 1147 | |
| 1148 | L<nytprofhtml> is a script included that produces html reports. |
| 1149 | L<nytprofcsv> is another script included that produces plain text CSV reports. |
| 1150 | |
| 1151 | L<Devel::NYTProf::Reader> is the module that powers the report scripts. You |
| 1152 | might want to check this out if you plan to implement a custom report (though |
| 1153 | it's very likely to be deprecated in a future release). |
| 1154 | |
| 1155 | L<Devel::NYTProf::ReadStream> is the module that lets you read a profile data |
| 1156 | file as a stream of chunks of data. |
| 1157 | |
| 1158 | Other tools: |
| 1159 | |
| 1160 | DTrace L<https://speakerdeck.com/mrallen1/perl-dtrace-and-you> |
| 1161 | |
| 1162 | =head1 TROUBLESHOOTING |
| 1163 | |
| 1164 | =head2 "Profile data incomplete, ..." or "Profile format error: ..." |
| 1165 | |
| 1166 | This error message means the file doesn't contain all the expected data |
| 1167 | or the data has been corrupted in some way. |
| 1168 | That may be because it was truncated (perhaps the filesystem was full) or, |
| 1169 | more commonly, because the all the expected data hasn't been written. |
| 1170 | |
| 1171 | NYTProf writes some important data to the data file when I<finishing> profiling. |
| 1172 | If you read the file before the profiling has finished you'll get this error. |
| 1173 | |
| 1174 | If the process being profiled is still running you'll need to wait until it |
| 1175 | exits cleanly (runs C<END> blocks or L</finish_profile> is called explicitly). |
| 1176 | |
| 1177 | If the process being profiled has exited then it's likely that it met with a |
| 1178 | sudden and unnatural death that didn't give NYTProf a chance to finish the |
| 1179 | profile. If the sudden death was due to a signal, like SIGTERM, or a SIGINT |
| 1180 | from pressing Ctrl-C, then the L</sigexit=1> option may help. |
| 1181 | |
| 1182 | If the sudden death was due to calling C<POSIX::_exit($status)> then you'll |
| 1183 | need to call L</finish_profile> before calling C<POSIX::_exit>. |
| 1184 | |
| 1185 | You'll also get this error if the code trying to read the profile is itself |
| 1186 | being profiled. That's most likely to happen if you enable profiling via the |
| 1187 | C<PERL5OPT> environment variable and have forgotten to unset it. |
| 1188 | |
| 1189 | If you've encountered this error message, and you're sure you've understood the |
| 1190 | concerns described above, and you're sure they don't apply in your case, then |
| 1191 | please open an issue. Be sure to include sufficient information so I can see |
| 1192 | how you've addressed these likely causes. |
| 1193 | |
| 1194 | =head2 Some source files don't have profile information |
| 1195 | |
| 1196 | This is usually due to NYTProf being initialized after some perl files have |
| 1197 | already been compiled. |
| 1198 | |
| 1199 | If you can't alter the command line to add "C<-d:NYTProf>" you could try using |
| 1200 | the C<PERL5OPT> environment variable. See L</PROFILING>. |
| 1201 | |
| 1202 | You could also try using the L</use_db_sub=1> option. |
| 1203 | |
| 1204 | =head2 Eval ... has unknown invoking fid |
| 1205 | |
| 1206 | When using the statement profiler you may see a warning message like this: |
| 1207 | |
| 1208 | Eval '(eval 2)' (fid 9, flags:viastmt,savesrc) has unknown invoking fid 10 |
| 1209 | |
| 1210 | Notice that the eval file id (fid 9 in this case) is lower than the file id |
| 1211 | that invoked the eval (fid 10 in this case). This is a known problem caused by |
| 1212 | the way perl works and how the profiler assigns and outputs the file ids. |
| 1213 | The invoking fid is known but gets assigned a fid and output after the fid for |
| 1214 | the eval, and that causes the warning when the file is read. |
| 1215 | |
| 1216 | =head2 Warning: %d subroutine calls had negative time |
| 1217 | |
| 1218 | There are two likely causes for this: clock instability, or accumulated timing |
| 1219 | errors. |
| 1220 | |
| 1221 | Clock instability, if present on your system, is most likely to be noticeable on |
| 1222 | very small/fast subroutines that are called very few times. |
| 1223 | |
| 1224 | Accumulated timing errors can arise because the subroutine profiler uses |
| 1225 | floating point values (NVs) to store the times. They are most likely to be |
| 1226 | noticed on subroutines that are called a few times but which make a large |
| 1227 | number of calls to very fast subroutines (such as opcodes). In this case the |
| 1228 | accumulated time apparently spent making those calls can be greater than the |
| 1229 | time spent in the calling subroutine. |
| 1230 | |
| 1231 | If you rerun nytprofhtml (etc.) with the L</trace=N> option set >0 you'll see |
| 1232 | trace messages like "%s call has negative time: incl %fs, excl %fs" for each |
| 1233 | affected subroutine. |
| 1234 | |
| 1235 | Try profiling with the L</slowops=N> option set to 0 to disable the profiling |
| 1236 | of opcodes. Since opcodes often execute in a few microseconds they are a common |
| 1237 | cause of this warning. |
| 1238 | |
| 1239 | You could also try recompiling perl to use 'long doubles' for the NV floating |
| 1240 | point type (use Configure -Duselongdouble). If you try this please let me know. |
| 1241 | I'd also happily take a patch to use long doubles, if available, by default. |
| 1242 | |
| 1243 | =head2 panic: buffer overflow ... |
| 1244 | |
| 1245 | You have unusually long subroutine names in your code. You'll need to rebuild |
| 1246 | Devel::NYTProf with the NYTP_MAX_SUB_NAME_LEN environment variable set to a |
| 1247 | value longer than the longest subroutine names in your code. |
| 1248 | |
| 1249 | =head1 AUTHORS AND CONTRIBUTORS |
| 1250 | |
| 1251 | B<Tim Bunce> (L<http://blog.timbunce.org>) |
| 1252 | leads the project and has done most of the development work thus far. |
| 1253 | |
| 1254 | B<Nicholas Clark> contributed zip compression and C<nytprofmerge>. |
| 1255 | B<Chia-liang Kao> contributed C<nytprofcg>. |
| 1256 | B<Peter (Stig) Edwards> contributed the VMS port. |
| 1257 | B<Jan Dubois> contributed the Windows port. |
| 1258 | B<Gisle Aas> contributed the Devel::NYTProf::ReadStream module. |
| 1259 | B<Steve Peters> contributed greater perl version portability and use of POSIX |
| 1260 | high-resolution clocks. |
| 1261 | Other contributors are noted in the Changes file. |
| 1262 | |
| 1263 | Many thanks to B<Adam Kaplan> who created C<NYTProf> initially by forking |
| 1264 | C<Devel::FastProf> adding reporting from C<Devel::Cover> and a test suite. |
| 1265 | |
| 1266 | =head1 COPYRIGHT AND LICENSE |
| 1267 | |
| 1268 | Copyright (C) 2008 by Adam Kaplan and The New York Times Company. |
| 1269 | Copyright (C) 2008-2016 by Tim Bunce, Ireland. |
| 1270 | |
| 1271 | This library is free software; you can redistribute it and/or modify |
| 1272 | it under the same terms as Perl itself, either Perl version 5.8.8 or, |
| 1273 | at your option, any later version of Perl 5 you may have available. |
| 1274 | |
| 1275 | =head2 Background |
| 1276 | |
| 1277 | Subroutine-level profilers: |
| 1278 | |
| 1279 | Devel::DProf | 1995-10-31 | ILYAZ |
| 1280 | Devel::AutoProfiler | 2002-04-07 | GSLONDON |
| 1281 | Devel::Profiler | 2002-05-20 | SAMTREGAR |
| 1282 | Devel::Profile | 2003-04-13 | JAW |
| 1283 | Devel::DProfLB | 2006-05-11 | JAW |
| 1284 | Devel::WxProf | 2008-04-14 | MKUTTER |
| 1285 | |
| 1286 | Statement-level profilers: |
| 1287 | |
| 1288 | Devel::SmallProf | 1997-07-30 | ASHTED |
| 1289 | Devel::FastProf | 2005-09-20 | SALVA |
| 1290 | Devel::NYTProf | 2008-03-04 | AKAPLAN |
| 1291 | Devel::Profit | 2008-05-19 | LBROCARD |
| 1292 | |
| 1293 | Devel::NYTProf is a (now distant) fork of Devel::FastProf, which was itself an |
| 1294 | evolution of Devel::SmallProf. |
| 1295 | |
| 1296 | Adam Kaplan forked Devel::FastProf and added html report generation (based on |
| 1297 | Devel::Cover) and a test suite - a tricky thing to do for a profiler. |
| 1298 | Meanwhile Tim Bunce had been extending Devel::FastProf to add novel |
| 1299 | per-sub and per-block timing, plus subroutine caller tracking. |
| 1300 | |
| 1301 | When Devel::NYTProf was released Tim switched to working on Devel::NYTProf |
| 1302 | because the html report would be a good way to show the extra profile data, and |
| 1303 | the test suite made development much easier and safer. |
| 1304 | |
| 1305 | Then Tim went a little crazy and added a slew of new features, in addition to |
| 1306 | per-sub and per-block timing and subroutine caller tracking. These included the |
| 1307 | 'opcode interception' method of profiling, ultra-fast and robust inclusive |
| 1308 | subroutine timing, doubling performance, plus major changes to html reporting |
| 1309 | to display all the extra profile call and timing data in richly annotated and |
| 1310 | cross-linked reports. |
| 1311 | |
| 1312 | Steve Peters came on board along the way with patches for portability and to |
| 1313 | keep NYTProf working with the latest development perl versions. Nicholas Clark |
| 1314 | added zip compression, many optimizations, and C<nytprofmerge>. |
| 1315 | Jan Dubois contributed Windows support. |
| 1316 | |
| 1317 | Adam's work was sponsored by The New York Times Co. L<http://open.nytimes.com>. |
| 1318 | Tim's work was partly sponsored by Shopzilla L<http://www.shopzilla.com> during 2008 |
| 1319 | but hasn't been sponsored since then. |
| 1320 | |
| 1321 | For the record, Tim has never worked for the New York Times nor has he received any |
| 1322 | kind of sponsorship or support from them in relation to NYTProf. The name of |
| 1323 | this module is simply result of the history outlined above, which can be |
| 1324 | summarised as: Adam forked an existing module when he added his enhancements |
| 1325 | and Tim didn't. |
| 1326 | |
| 1327 | =cut |