2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 2013, 2015 Free Software Foundation, Inc.
4 @c See the file guile.texi for copying conditions.
9 @code{(statprof)} is a fairly simple statistical profiler for Guile.
11 A simple use of statprof would look like this:
14 (statprof-reset 0 50000 #t)
21 This would reset statprof, clearing all accumulated statistics, then
22 start profiling, run some code, stop profiling, and finally display a
23 gprof flat-style table of statistics which will look something like
27 % cumulative self self total
28 time seconds seconds calls ms/call ms/call name
29 35.29 0.23 0.23 2002 0.11 0.11 -
30 23.53 0.15 0.15 2001 0.08 0.08 positive?
31 23.53 0.15 0.15 2000 0.08 0.08 +
32 11.76 0.23 0.08 2000 0.04 0.11 do-nothing
33 5.88 0.64 0.04 2001 0.02 0.32 loop
34 0.00 0.15 0.00 1 0.00 150.59 do-something
38 All of the numerical data with the exception of the calls column is
39 statistically approximate. In the following column descriptions, and in
40 all of statprof, "time" refers to execution time (both user and system),
45 The percent of the time spent inside the procedure itself (not counting
48 @item cumulative seconds
49 The total number of seconds spent in the procedure, including children.
52 The total number of seconds spent in the procedure itself (not counting
56 The total number of times the procedure was called.
59 The average time taken by the procedure itself on each call, in ms.
62 The average time taken by each call to the procedure, including time
63 spent in child functions.
66 The name of the procedure.
70 The profiler uses @code{eq?} and the procedure object itself to identify
71 the procedures, so it won't confuse different procedures with the same
72 name. They will show up as two different rows in the output.
74 Right now the profiler is quite simplistic. I cannot provide call-graphs
75 or other higher level information. What you see in the table is pretty
76 much all there is. Patches are welcome :-)
78 @section Implementation notes
79 The profiler works by setting the unix profiling signal
80 @code{ITIMER_PROF} to go off after the interval you define in the call
81 to @code{statprof-reset}. When the signal fires, a sampling routine is
82 run which looks at the current procedure that's executing, and then
83 crawls up the stack, and for each procedure encountered, increments that
84 procedure's sample count. Note that if a procedure is encountered
85 multiple times on a given stack, it is only counted once. After the
86 sampling is complete, the profiler resets profiling timer to fire again
87 after the appropriate interval.
89 Meanwhile, the profiler keeps track, via @code{get-internal-run-time},
90 how much CPU time (system and user -- which is also what
91 @code{ITIMER_PROF} tracks), has elapsed while code has been executing
92 within a statprof-start/stop block.
94 The profiler also tries to avoid counting or timing its own code as much
98 @anchor{statprof statprof-active?}@defun statprof-active?
99 Returns @code{#t} if @code{statprof-start} has been called more times
100 than @code{statprof-stop}, @code{#f} otherwise.
104 @anchor{statprof statprof-start}@defun statprof-start
105 Start the profiler.@code{}
109 @anchor{statprof statprof-stop}@defun statprof-stop
110 Stop the profiler.@code{}
114 @anchor{statprof statprof-reset}@defun statprof-reset sample-seconds sample-microseconds count-calls? [full-stacks?]
115 Reset the statprof sampler interval to @var{sample-seconds} and
116 @var{sample-microseconds}. If @var{count-calls?} is true, arrange to
117 instrument procedure calls as well as collecting statistical profiling
118 data. If @var{full-stacks?} is true, collect all sampled stacks into a
119 list for later analysis.
121 Enables traps and debugging as necessary.
125 @anchor{statprof statprof-accumulated-time}@defun statprof-accumulated-time
126 Returns the time accumulated during the last statprof run.@code{}
130 @anchor{statprof statprof-sample-count}@defun statprof-sample-count
131 Returns the number of samples taken during the last statprof run.@code{}
135 @anchor{statprof statprof-fold-call-data}@defun statprof-fold-call-data proc init
136 Fold @var{proc} over the call-data accumulated by statprof. Cannot be
137 called while statprof is active. @var{proc} should take two arguments,
138 @code{(@var{call-data} @var{prior-result})}.
140 Note that a given proc-name may appear multiple times, but if it does,
141 it represents different functions with the same name.
145 @anchor{statprof statprof-proc-call-data}@defun statprof-proc-call-data proc
146 Returns the call-data associated with @var{proc}, or @code{#f} if none
151 @anchor{statprof statprof-call-data-name}@defun statprof-call-data-name cd
154 @anchor{statprof statprof-call-data-calls}@defun statprof-call-data-calls cd
157 @anchor{statprof statprof-call-data-cum-samples}@defun statprof-call-data-cum-samples cd
160 @anchor{statprof statprof-call-data-self-samples}@defun statprof-call-data-self-samples cd
163 @anchor{statprof statprof-call-data->stats}@defun statprof-call-data->stats call-data
164 Returns an object of type @code{statprof-stats}.
168 @anchor{statprof statprof-stats-proc-name}@defun statprof-stats-proc-name stats
171 @anchor{statprof statprof-stats-%-time-in-proc}@defun statprof-stats-%-time-in-proc stats
174 @anchor{statprof statprof-stats-cum-secs-in-proc}@defun statprof-stats-cum-secs-in-proc stats
177 @anchor{statprof statprof-stats-self-secs-in-proc}@defun statprof-stats-self-secs-in-proc stats
180 @anchor{statprof statprof-stats-calls}@defun statprof-stats-calls stats
183 @anchor{statprof statprof-stats-self-secs-per-call}@defun statprof-stats-self-secs-per-call stats
186 @anchor{statprof statprof-stats-cum-secs-per-call}@defun statprof-stats-cum-secs-per-call stats
189 @anchor{statprof statprof-display}@defun statprof-display . _
190 Displays a gprof-like summary of the statistics collected. Unless an
191 optional @var{port} argument is passed, uses the current output port.
195 @anchor{statprof statprof-display-anomolies}@defun statprof-display-anomolies
196 A sanity check that attempts to detect anomolies in statprof's
201 @anchor{statprof statprof-fetch-stacks}@defun statprof-fetch-stacks
202 Returns a list of stacks, as they were captured since the last call to
203 @code{statprof-reset}.
205 Note that stacks are only collected if the @var{full-stacks?} argument
206 to @code{statprof-reset} is true.
210 @anchor{statprof statprof-fetch-call-tree}@defun statprof-fetch-call-tree
212 Return a call tree for the previous statprof run.
214 The return value is a list of nodes, each of which is of the type:
216 node ::= (@@var@{proc@} @@var@{count@} . @@var@{nodes@})
222 @anchor{statprof statprof}@defun statprof thunk [#:loop] [#:hz] [#:count-calls?] [#:full-stacks?]
223 Profile the execution of @var{thunk}, and return its return values.
225 The stack will be sampled @var{hz} times per second, and the thunk
226 itself will be called @var{loop} times.
228 If @var{count-calls?} is true, all procedure calls will be recorded.
229 This operation is somewhat expensive.
231 If @var{full-stacks?} is true, at each sample, statprof will store away
232 the whole call tree, for later analysis. Use
233 @code{statprof-fetch-stacks} or @code{statprof-fetch-call-tree} to
234 retrieve the last-stored stacks.
238 @anchor{statprof with-statprof}@defspec with-statprof args
239 Profile the expressions in the body, and return the body's return
246 Execute the body @var{loop} number of times, or @code{#f} for no looping
256 Whether to instrument each function call (expensive)
261 Whether to collect away all sampled stacks into a list
269 @anchor{statprof gcprof}@defun gcprof thunk [#:loop] [#:full-stacks?]
270 Do an allocation profile of the execution of @var{thunk}.
272 The stack will be sampled soon after every garbage collection, yielding
273 an approximate idea of what is causing allocation in your program.
275 Since GC does not occur very frequently, you may need to use the
276 @var{loop} parameter, to cause @var{thunk} to be called @var{loop}
279 If @var{full-stacks?} is true, at each sample, statprof will store away
280 the whole call tree, for later analysis. Use
281 @code{statprof-fetch-stacks} or @code{statprof-fetch-call-tree} to
282 retrieve the last-stored stacks.