1Devel::NYTProf::Core(3)User Contributed Perl DocumentatioDnevel::NYTProf::Core(3)
2
3
4
6 Devel::NYTProf::Core - load internals of Devel::NYTProf
7
9 This module is not meant to be used directly. See Devel::NYTProf,
10 Devel::NYTProf::Data, and Devel::NYTProf::Reader.
11
12 While it's not meant to be used directly, it is a handy place to
13 document some internals.
14
16 The subroutine profiler intercepts the "entersub" opcode which perl
17 uses to invoke a subroutine, both XS subs (henceforth xsubs) and pure
18 perl subs.
19
20 The following sections outline the way the subroutine profiler works:
21
22 Before the subroutine call
23 The profiler records the current time, the current value of
24 cumulative_subr_secs (as initial_subr_secs), and the current
25 cumulative_overhead_ticks (as initial_overhead_ticks).
26
27 The statement profiler measures time at the start and end of processing
28 for each statement (so time spent in the profiler, writing to the file
29 for example, is excluded.) It accumulates the measured overhead into
30 the cumulative_overhead_ticks variable.
31
32 In a similar way, the subroutine profiler measures the exclusive time
33 spent in subroutines and accumulates it into the cumulative_subr_secs
34 global.
35
36 Make the subroutine call
37 The call is made by executing the original perl internal code for the
38 "entersub" opcode.
39
40 Calling a perl subroutine
41
42 If the sub being called is a perl sub then when the entersub opcode
43 returns, back into the subroutine profiler, the subroutine has been
44 'entered' but the first opcode of the subroutine hasn't been executed
45 yet. Crucially though, a new scope has been entered by the entersub
46 opcode.
47
48 The subroutine profiler then pushes a destructor onto the context
49 stack. The destructor is effectively just inside the sub, like a
50 "local", and so will be triggered when the subroutine exits by any
51 means. Also, because it was the first thing push onto the context
52 stack, it will be triggered after any activity caused by the
53 subroutines scope exiting.
54
55 When the destructor is invoked it calls a function which completes the
56 measurement of the time spent in the sub (see below).
57
58 In this way the profiling of perl subroutines is very accurate and
59 robust.
60
61 Calling an xsub
62
63 If the sub being called is an xsub, then control doesn't return from
64 the entersub opcode until the xsub has returned. The profiler detects
65 this and calls the function which completes the measurement of the time
66 spent in the xsub.
67
68 So far so good, but there's a problem. What if the xsub doesn't return
69 normally but throws an exception instead?
70
71 In that case (currently) the profiler acts as if the xsub was never
72 called. Time spent inside the xsub will be allocated to the calling
73 sub.
74
75 Completing the measurement
76 The function which completes the timing of a subroutine call does the
77 following:
78
79 It calculates the time spent in the statement profiler:
80
81 overhead_ticks = cumulative_overhead_ticks - initial_overhead_ticks
82
83 and subtracts that from the total time spent 'inside' the subroutine:
84
85 incl_subr_sec = (time now - time call was made) - overhead_ticks
86
87 That gives us an accurate inclusive time. To get the exclusive time it
88 calculates the time spent in subroutines called from the subroutine
89 call we're measuring:
90
91 called_sub_secs = cumulative_subr_secs - initial_subr_secs
92
93 and subtracts that from the incl_subr_sec:
94
95 excl_subr_sec = incl_subr_sec - called_sub_secs
96
97 To make that easier to follow, consider a call to a sub that calls no
98 others. In that case cumulative_subr_secs remains unchanged during the
99 call, so called_sub_secs is zero, and excl_subr_sec is the same as
100 incl_subr_sec.
101
102 Finally, it adds the exclusive time to the cumulative exclusive time:
103
104 cumulative_subr_secs += excl_subr_sec
105
107 Tim Bunce, <http://www.tim.bunce.name> and <http://blog.timbunce.org>
108
110 Copyright (C) 2008, 2009 by Tim Bunce.
111
112 This library is free software; you can redistribute it and/or modify it
113 under the same terms as Perl itself, either Perl version 5.8.8 or, at
114 your option, any later version of Perl 5 you may have available.
115
116
117
118perl v5.32.0 2020-07-28 Devel::NYTProf::Core(3)