| 1 |
# vim: ts=8 sw=2 sts=0 noexpandtab: |
| 2 |
|
| 3 |
HACKING Devel::NYTProf |
| 4 |
====================== |
| 5 |
|
| 6 |
We encourage hacking Devel::NYTProf! |
| 7 |
|
| 8 |
OBTAINING THE CURRENT RELEASE |
| 9 |
----------------------------- |
| 10 |
The current official release can be obtained from CPAN |
| 11 |
http://metacpan.org/release/Devel-NYTProf/ |
| 12 |
|
| 13 |
OBTAINING THE LATEST DEVELOPMENT CODE |
| 14 |
------------------------------------- |
| 15 |
You can grab the head of the latest trunk code from the GitHub repository. See: |
| 16 |
https://github.com/timbunce/devel-nytprof |
| 17 |
|
| 18 |
CONTRIBUTING |
| 19 |
------------ |
| 20 |
Please work with the latest code from the repository - see above. |
| 21 |
|
| 22 |
To report bugs or to discuss Devel-NYTProf's ongoing development, |
| 23 |
please file an issue at: |
| 24 |
https://github.com/timbunce/devel-nytprof/issues |
| 25 |
|
| 26 |
Please submit patches via pull requests at: |
| 27 |
https://github.com/timbunce/devel-nytprof/pulls |
| 28 |
|
| 29 |
When developing, please ensure that no new compiler warnings are output. |
| 30 |
|
| 31 |
TESTING |
| 32 |
------- |
| 33 |
Please try to add tests covering any changes you make. |
| 34 |
|
| 35 |
You can run individual tests like this: |
| 36 |
|
| 37 |
perl -Mblib t/30-util.t |
| 38 |
|
| 39 |
... or: |
| 40 |
|
| 41 |
prove -vb t/30-util.t |
| 42 |
|
| 43 |
Most tests include some underlying perl code that's being profiled. |
| 44 |
Either as a .p file, which can be profiled like this: |
| 45 |
|
| 46 |
perl -Mblib -MDevel::NYTProf t/test01.p |
| 47 |
|
| 48 |
or embedded at the end of a t/*.t file, which can be profiled like this: |
| 49 |
|
| 50 |
perl -Mblib -MDevel::NYTProf -x t/70-subname |
| 51 |
|
| 52 |
The output will be in the ./nytprof.out file. |
| 53 |
|
| 54 |
RESOURCES |
| 55 |
--------- |
| 56 |
Google Devel Group (must subscribe here): |
| 57 |
http://groups.google.com/group/develnytprof-dev |
| 58 |
|
| 59 |
TODO (unsorted, unprioritized, unconsidered, even unreasonable and daft :) |
| 60 |
---- |
| 61 |
|
| 62 |
*** For build/test |
| 63 |
|
| 64 |
Add (very) basic nytprofhtml test (ie it runs and produces output) |
| 65 |
|
| 66 |
Add tests for evals in regex: s/.../ ...perl code... /e |
| 67 |
|
| 68 |
Add tests for -block and -sub csv reports. |
| 69 |
|
| 70 |
Add tests with various kinds of blocks and loops (if, do, while, until, etc). |
| 71 |
|
| 72 |
Add mechanism to specify inside the .p file that NYTProf |
| 73 |
should not be loaded via the command line. That's needed to test |
| 74 |
behaviors in environments where perl is init'd first. Such as mod_perl. |
| 75 |
Then we can test things like not having the sub line range for some subs. |
| 76 |
|
| 77 |
*** For core only |
| 78 |
|
| 79 |
See MemoryProfiling.pod file |
| 80 |
|
| 81 |
Store raw NYTPROF option string in the data file. |
| 82 |
Include parsed version in report index page. |
| 83 |
|
| 84 |
Add actual size and mtime of fid to data file. (Already in data file as zero, |
| 85 |
just needs the stat() call.) Don't alter errno. |
| 86 |
|
| 87 |
Add help option which would print a summary of the options and exit. |
| 88 |
Could also print list of available clocks for the clock=N option |
| 89 |
(using a set of #ifdef's) |
| 90 |
|
| 91 |
The subroutine profiler could calculate the running variance of the samples |
| 92 |
using this logic http://www.johndcook.com/standard_deviation.html |
| 93 |
so the reports can display the standard deviation. |
| 94 |
|
| 95 |
Replace DB::enable_profiling() and DB::disable_profiling() with $DB::profile = 1|0; |
| 96 |
That a more consistent API with $DB::single etc., but more importantly it lets |
| 97 |
users leave the code in place when NYTProf is not loaded. It'll just do nothing, |
| 98 |
whereas currently the user will get a fatal error if NYTProf isn't loaded. |
| 99 |
It also allows smart things like use of local() for temporary overrides. |
| 100 |
|
| 101 |
Combine current profile_* globals into a single global int using bit fields. |
| 102 |
That way assigning to $DB::profile can offer a finer degree of control. |
| 103 |
Specifically to enable/disable the sub or statement profiler separately. |
| 104 |
|
| 105 |
Add mechanism to enable control of profiling on a per-sub-name and/or |
| 106 |
per-package-name basis. For example, specify a regex and whenever a sub is |
| 107 |
entered (for the first time, to make it cheap) check if the sub name matches |
| 108 |
the regex. If it does then save the current $DB::profile value and set a new one. |
| 109 |
When the sub exits restore the previous $DB::profile value. |
| 110 |
|
| 111 |
Work around OP_UNSTACK bug (http://rt.perl.org/rt3/Ticket/Display.html?id=60954) |
| 112 |
while ( foo() ) { # all calls to foo should be from here |
| 113 |
... |
| 114 |
... # no calls to foo() should appear here |
| 115 |
} |
| 116 |
|
| 117 |
*** For core and reports |
| 118 |
|
| 119 |
Add @INC to data file so reports can be made more readable by removing |
| 120 |
(possibly very long) library paths where appropriate. |
| 121 |
Tricky thing is that @INC can change during the life of the program. |
| 122 |
One approach might be to output it whenever we assign a new fid |
| 123 |
but only if different to the last @INC that was ouput. |
| 124 |
|
| 125 |
Add marker with timestamp for phases BEGIN, CHECK, INIT, END |
| 126 |
(could combine with pid marker) |
| 127 |
Add marker with timestamp for enable_profile and disable_profile. |
| 128 |
Could also dump-and-zero the sub profiler data so we could report per-phase timing. |
| 129 |
The goals here are to |
| 130 |
a) know how long the different phases of execution took mostly for general interest, and |
| 131 |
b) know how much time was spent with the profiler enabled to calculate accurate |
| 132 |
percentages and also be able to spot 'leaks' in the data processing (e.g. if |
| 133 |
the sum of the statement times don't match the time spent with the profiler |
| 134 |
enabled, due to nested string evals for example). |
| 135 |
|
| 136 |
Add flags to sub call info to indicate what phase (BEGIN, etc) the call |
| 137 |
happened in. That'll allow call graphs to ignore BEGIN-time calls |
| 138 |
(which tend to make graphviz output too noisy to be useful). |
| 139 |
|
| 140 |
*** For reports only |
| 141 |
|
| 142 |
::Reader and its data structures need to be refactored to death. |
| 143 |
The whole reporting framework needs a rewrite to use a single 'thin' command |
| 144 |
line and classes for the Model (lines, files, subs), View (html, csv etc), |
| 145 |
and Controller (composing views to form reports). |
| 146 |
Dependent on a richer data model. |
| 147 |
|
| 148 |
Then rework bin/ntyprof* to use the new subclasses |
| 149 |
Ideally end up with a single nytprof command that just sets up the appropriate |
| 150 |
classes to do the work. |
| 151 |
|
| 152 |
Trim leading @INC portion from filename in __ANON__[/very/long/path/...] |
| 153 |
in report output. (Keep full path in link/tooltip/title as it may be ambiguous when shortened). |
| 154 |
|
| 155 |
Add help link in reports. Could go to docs page on search.cpan.org. |
| 156 |
|
| 157 |
Add a 'permalink' icon (eg infinity symbol) to the right of lines that define |
| 158 |
subs to make it easer to email/IM links to particular places in the code. |
| 159 |
|
| 160 |
Report could track which subs it has reported caller info for |
| 161 |
and so be able to identify subs that were called but haven't been included |
| 162 |
in the report because we didn't know where the sub was. |
| 163 |
They could them be included in a separate 'miscellaneous' page. |
| 164 |
This is a more general way to view the problem of xsubs in packages |
| 165 |
for which we don't have any perl source code. |
| 166 |
|
| 167 |
Consider restoring inclusive-time treemap with an appropriate description |
| 168 |
to explain how to interpret it. |
| 169 |
|
| 170 |
*** Other - mostly unsorted - stuff *** |
| 171 |
|
| 172 |
Intercept all opcodes that may fork and run perl code in the child |
| 173 |
ie fork, open, entersub (ie xs), others? |
| 174 |
and fflush before executing the op (so fpurge isn't strictly required) |
| 175 |
and reinit_if_forked() afterwards |
| 176 |
add option to force reinit_if_forked check per stmt just-in-case |
| 177 |
Alternatively it might be better to use pthread_atfork() [if available] with a |
| 178 |
child handler. The man page says "Remember: only async-cancel-safe functions |
| 179 |
are allowed on the child side of fork()" so it seems that the safe thing to do |
| 180 |
is to use a volatile flag variable, and change its value in the handler to |
| 181 |
signal to the main code. |
| 182 |
|
| 183 |
Support profiling programs which use threads: |
| 184 |
- move all relevant globals into a structure |
| 185 |
- add lock around output to file |
| 186 |
|
| 187 |
Set options via import so perl -d:NYTProf=... works. Very handy. May need |
| 188 |
alternative option syntax. Also perl gives special meaning to 't' option |
| 189 |
(threads) so we should reserve the same for eventual thread support. |
| 190 |
Problem with this is that the import() call happens after init_profiler() |
| 191 |
so limits the usefulness. So we'd need to limit it to certain options |
| 192 |
(trace would certainly be useful). |
| 193 |
|
| 194 |
Add resolution of __ANON__ sub names (eg imported 'constants') where possible. |
| 195 |
[I can't recall what I meant by that now. I think this means where an anon sub |
| 196 |
has been imported, if the refcnt is 1 then use the imported name instead of the |
| 197 |
__ANON__ name.] |
| 198 |
|
| 199 |
The appending of an @line to BEGIN subs should also be applied to END subs. |
| 200 |
|
| 201 |
Record $AUTOLOAD when AUTOLOAD() called. Perhaps as ...::AUTOLOAD[$AUTOLOAD] |
| 202 |
Or perhaps just use the original name if the 'resolved' one is AUTOLOAD. |
| 203 |
Could be argued either way. |
| 204 |
|
| 205 |
More generally, consider the problem of code where one code path is fast |
| 206 |
and just sets $sql = ... (for example) and another code path executes the |
| 207 |
sql. Some $sql may be fast and others slow. The profile can't separate the |
| 208 |
timings based on what was in $sql because the code path was the same in both |
| 209 |
cases. (For sql DBI::Profile can be used, but the underlying issue is general.) |
| 210 |
|
| 211 |
The sub_caller information is currently one level deep. It would be good to |
| 212 |
make it two levels. Especially because it would allow you to "see through" |
| 213 |
AUTOLOADs and other kinds of 'dispatch' subs. |
| 214 |
|
| 215 |
Refactor this HACKING file! |
| 216 |
|
| 217 |
The data file includes the information mapping a line-level line to the |
| 218 |
corresponding block-level and sub-level lines. This should be added to the data |
| 219 |
structure. It would enable a much richer visualization of which lines have |
| 220 |
contributed to the 'rolled up' counts. That's especially tricky to work out |
| 221 |
with the block level view. |
| 222 |
|
| 223 |
Following on from that I have a totally crazy idea that the browsers css engine |
| 224 |
could be used to highlight the corresponding rollup line when hovering over a |
| 225 |
source line, and/or the opposite. Needs lots of thought, but it's an interesting idea. |
| 226 |
|
| 227 |
Profile and optimize report generation |
| 228 |
|
| 229 |
Bug or limitation?: sub calls in a continue { ... } block of a while () get |
| 230 |
associated with the 'next;' within the loop. Fixed by perl change 33710? |
| 231 |
|
| 232 |
Investigate style.css problem when using --outfile=some/other/dir |
| 233 |
|
| 234 |
Class::MOP should update %DB::sub (if $^P & 0x10 set) when it creates methods. |
| 235 |
Sub::Name should do same (extracting the file and line from the ANON[...:...]) |
| 236 |
|
| 237 |
Profile should report _both_ the 'raw original' filename (possibly relative) |
| 238 |
used by the application being profiled, plus an absolute filename determined |
| 239 |
ASAP (to avoid problems with scripts that chdir). |
| 240 |
|
| 241 |
Add (very) basic nytprofhtml test (ie it runs and produces output) so we check the VERSION has been updated. |
| 242 |
|
| 243 |
In the called by list in html: "by $subname line $line of $file" |
| 244 |
make the file not include the @INC portion |
| 245 |
|
| 246 |
Monitor and report when method cache is invalidated. Watch generation number |
| 247 |
and output a tag when it changes. Report locations of changes. Highlight those |
| 248 |
that happen after INIT phase. |
| 249 |
|
| 250 |
Fix testing of t/*.pm_x files which are currently being ignored. |
| 251 |
|
| 252 |
The autosplit handling doesn't address the naming of pseudo-fids from string evals |
| 253 |
inside autoloaded subs, like "(eval 0)[test14.pm (autosplit into auto/test14/bar.al):17]" |
| 254 |
The 'file name' for the eval fid needs to be edited when read in to remove the |
| 255 |
' (autosplit...', but care should be taken to not remove the text for evals in |
| 256 |
autosplit files for which we've not been able to alias to the parent. |
| 257 |
|
| 258 |
Add a FID_ATTRIB tag to allow additional info about fids to be generated after |
| 259 |
the initial fid info is output. |
| 260 |
|
| 261 |
Use FID_ATTRIB tag to record autoload fids being aliases to a fid so that |
| 262 |
reports can include a list of autoloaded subs. |
| 263 |
|
| 264 |
Check if pp_leavegiven and pp_leavewhen need handling in init_profiler(). |
| 265 |
|
| 266 |
Copy the nytprof.out file into the output report dir, so a report is more |
| 267 |
'self-contained' and can be archived and thrown around as a tarball/zip and |
| 268 |
still used for further analysis. |
| 269 |
|
| 270 |
To stress-test NYTProf using perl's own test suite, set env vars: |
| 271 |
NYTPROF='file=/tmp/nytprof.out:addpid=1:nameanonsubs=0:nameevals=0' |
| 272 |
PERL5OPT='-d:NYTProf' |
| 273 |
and hack t/TEST to not delete the PERL5OPT env var. |
| 274 |
|
| 275 |
The findcaller option doesn't notice if the caller is an opcode. |
| 276 |
Opcodes that call subs (like subst calling utf8::SWASHGET) probably shouldn't |
| 277 |
appear as the caller in the call-tree because, while strictly accurate, it's |
| 278 |
probably less useful from the users perspective. |
| 279 |
Fixing that part is easy but handling incl/excl time needs more thought. |
| 280 |
|
| 281 |
For xsubs and opcodes that call perl subs: |
| 282 |
In the subroutine prologue that currently lists where the sub was called from, |
| 283 |
for xsubs & opcodes, add a list of subs that it called (typically none). |
| 284 |
That would be handy because currently calls from xsubs & opcodes appear in the |
| 285 |
reports at the line of the last _perl_ statement executed, and not in the fake |
| 286 |
'stub' that we add to the end of the package source. |
| 287 |
|
| 288 |
Use gethrtime() on platforms that support it. |
| 289 |
http://developers.sun.com/solaris/articles/time_stamp.html |
| 290 |
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=332 |
| 291 |
|
| 292 |
Currently lvalue subs aren't profiled when use_db_sub is in effect. |
| 293 |
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2010-02/msg00824.html |
| 294 |
|
| 295 |
Idle conjecture: perhaps loading the profile data into an SQLite database |
| 296 |
would be a better approach to reporting. |
| 297 |
(An nytprofimport utility could read an nytprof.out and write an nytprof.db) |
| 298 |
Then, rather than hand-crafting inflexible data structures we could use SQL |
| 299 |
(or, say, DBIx::Class) to return relevant data. Would also provide another |
| 300 |
route for other data sources (languages/tools) to be included in a report. |
| 301 |
Could also simplify: comparing profiles, merging profiles, ... |
| 302 |
Also 'pipelines' of plugin db-to-db transformations could be developed. |
| 303 |
Any volunteers to explore writing an nytprofimport for SQLite? |
| 304 |
|
| 305 |
Option to add sub call and return events into the data file as they happen. |
| 306 |
Would enable a dprofpp -T like output. See https://rt.cpan.org/Ticket/Display.html?id=50766 |
| 307 |
Especially relevant as DProf is being removed from the code and this is |
| 308 |
something NYTProf still doesn't do. |
| 309 |
|
| 310 |
String evals could/should be tied into the subroutine profiler. |
| 311 |
That would give inclusive timings which we don't have at the moment. |
| 312 |
The evals appear in the html report as if they're calls but the timings are |
| 313 |
statement timings of the eval fid that don't include time spent in subs defined |
| 314 |
elsewhere but called from the string eval. The inconsistency is confusing. |
| 315 |
|
| 316 |
Modify csv output to optionally include extra metadata lines. Use for testing. |
| 317 |
|
| 318 |
In html report where sub callers are shown (called N times by S from F at line L) |
| 319 |
also show in compact form links to the callers of the caller, if there aren't too many. |
| 320 |
This makes it faster to climb the call stack. A simple mouseover will show the |
| 321 |
filename of the caller (perhaps a tooltip could give the file and sub). |
| 322 |
Also needed for modules that take shortcuts calling XS functions (*cough DBI*). |
| 323 |
And/or, add a simple up arrow that'll just to the calling sub. Typically that'll |
| 324 |
be the surrounding "sub foo { ..." (which'll be handy for big subs) but for |
| 325 |
cases where the call to that sub wasn't recorded (eg pre 5.8.9 or the DBI's |
| 326 |
xs calling optimization) it'll be the most recent sub entry that was recorded. |
| 327 |
|
| 328 |
Generate extra report pages for xsubs in packages that don't have source code. |
| 329 |
They're currently all dumped into the 'main' file. |
| 330 |
|
| 331 |
Docs describing how the subroutine profiler works need updating. |
| 332 |
Add 'u' key to treemap to trigger moving 'up' a level. |
| 333 |
Add "calls N subs" to treemap mouseover box |
| 334 |
Upgrade treemap to JIT version 2 (which has transition animations). |
| 335 |
|
| 336 |
Sub profiler should avoid sv_setpvf(subname_sv, "%s::%s", stash_name, GvNAME(gv)); |
| 337 |
because it's expensive (Perl_sv_setpvf_nocontext accounts for 29% of pp_entersub_profiler). |
| 338 |
Use a two level hash: HvNAME(GvSTASH(gv)) then GvNAME(gv). |
| 339 |
Should then also be able to avoid newSV/free for subname_sv (which accounts for 50% of its time). |
| 340 |
|
| 341 |
The subroutine profiler spends a lot of its time allocating the two |
| 342 |
calle[rd]_subname_sv SVs in subr_entry_setup and freeing them in subr_entry_destroy. |
| 343 |
If, instead of freeing the SVs they were chained into a freelist that |
| 344 |
subr_entry_setup could pull from, we'd get a significant boost in performance |
| 345 |
for the subroutine profiler. It would also greatly reduce the risk of |
| 346 |
NYTProf overwriting a recently freed but still on the stack SV. |
| 347 |
|
| 348 |
Would be good to be able to measure the time spent in require. The easy way |
| 349 |
would be to treat it as a slowop. I recall enabling this in the early days |
| 350 |
of slowop support but running into problems. It would be good to revisit. |
| 351 |
This would help with profiling issues like: |
| 352 |
http://blog.moose.perl.org/2010/08/moose-110-and-classmop-105-now-compiling-10-faster.html |
| 353 |
|
| 354 |
We need a start=runtime option to start at the _end_ of any INIT subs. |
| 355 |
(The current start=init option is the closest we have but it's not very |
| 356 |
useful if lots of other work is done in INIT blocks.) |
| 357 |
|
| 358 |
We need an option to discount the time spent in specific subs, like we do for |
| 359 |
CORE:accept so that time pure-perl webservers spend waiting for the next |
| 360 |
request doesn't distort the profile. Probably just needs a hash lookup. |
| 361 |
http://stackoverflow.com/questions/4132270/how-to-ignore-some-subroutine-calls-in-nytprof-reporting/14085984#14085984 |
| 362 |
|
| 363 |
Remove nytprofcsv |
| 364 |
|
| 365 |
Add "goto skip_sub_profile;" to this?: (and/or figure out why already_counted++ isn't enough) |
| 366 |
/* catch profile_subs being turned off by disable_profile call */ |
| 367 |
if (!profile_subs) |
| 368 |
subr_entry->already_counted++; |
| 369 |
|
| 370 |
study http://www.postgresql.org/docs/9.2/static/pgtesttiming.html |
| 371 |
See pg contrib/pg_test_timing/pg_test_timing.c |
| 372 |
|
| 373 |
Show /sys/devices/system/clocksource/clocksource0/available_clocksource |
| 374 |
and /sys/devices/system/clocksource/clocksource0/current_clocksource |
| 375 |
in the Makefile.PL and test output, if present |
| 376 |
|