Replace $letrec with $rec
[bpt/guile.git] / doc / ref / statprof.texi
CommitLineData
58c4a39d
AW
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
cdcba5b2 3@c Copyright (C) 2013, 2015 Free Software Foundation, Inc.
58c4a39d
AW
4@c See the file guile.texi for copying conditions.
5
6@node Statprof
7@section Statprof
8
9@code{(statprof)} is a fairly simple statistical profiler for Guile.
10
11A simple use of statprof would look like this:
12
13@example
14(statprof-reset 0 50000 #t)
15(statprof-start)
16(do-something)
17(statprof-stop)
18(statprof-display)
19@end example
20
21This would reset statprof, clearing all accumulated statistics, then
22start profiling, run some code, stop profiling, and finally display a
23gprof flat-style table of statistics which will look something like
24this:
25
26@example
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
35 ...
36@end example
37
38All of the numerical data with the exception of the calls column is
39statistically approximate. In the following column descriptions, and in
40all of statprof, "time" refers to execution time (both user and system),
41not wall clock time.
42
43@table @asis
44@item % time
45The percent of the time spent inside the procedure itself (not counting
46children).
47
48@item cumulative seconds
49The total number of seconds spent in the procedure, including children.
50
51@item self seconds
52The total number of seconds spent in the procedure itself (not counting
53children).
54
55@item calls
56The total number of times the procedure was called.
57
58@item self ms/call
59The average time taken by the procedure itself on each call, in ms.
60
61@item total ms/call
62The average time taken by each call to the procedure, including time
63spent in child functions.
64
65@item name
66The name of the procedure.
67
68@end table
69
70The profiler uses @code{eq?} and the procedure object itself to identify
71the procedures, so it won't confuse different procedures with the same
72name. They will show up as two different rows in the output.
73
74Right now the profiler is quite simplistic. I cannot provide call-graphs
75or other higher level information. What you see in the table is pretty
76much all there is. Patches are welcome :-)
77
78@section Implementation notes
79The profiler works by setting the unix profiling signal
80@code{ITIMER_PROF} to go off after the interval you define in the call
81to @code{statprof-reset}. When the signal fires, a sampling routine is
82run which looks at the current procedure that's executing, and then
83crawls up the stack, and for each procedure encountered, increments that
84procedure's sample count. Note that if a procedure is encountered
85multiple times on a given stack, it is only counted once. After the
86sampling is complete, the profiler resets profiling timer to fire again
87after the appropriate interval.
88
89Meanwhile, the profiler keeps track, via @code{get-internal-run-time},
90how much CPU time (system and user -- which is also what
91@code{ITIMER_PROF} tracks), has elapsed while code has been executing
92within a statprof-start/stop block.
93
94The profiler also tries to avoid counting or timing its own code as much
95as possible.
96
97@section Usage
98@anchor{statprof statprof-active?}@defun statprof-active?
99Returns @code{#t} if @code{statprof-start} has been called more times
100than @code{statprof-stop}, @code{#f} otherwise.
101
102@end defun
103
104@anchor{statprof statprof-start}@defun statprof-start
105Start the profiler.@code{}
106
107@end defun
108
109@anchor{statprof statprof-stop}@defun statprof-stop
110Stop the profiler.@code{}
111
112@end defun
113
114@anchor{statprof statprof-reset}@defun statprof-reset sample-seconds sample-microseconds count-calls? [full-stacks?]
115Reset the statprof sampler interval to @var{sample-seconds} and
116@var{sample-microseconds}. If @var{count-calls?} is true, arrange to
117instrument procedure calls as well as collecting statistical profiling
118data. If @var{full-stacks?} is true, collect all sampled stacks into a
119list for later analysis.
120
121Enables traps and debugging as necessary.
122
123@end defun
124
125@anchor{statprof statprof-accumulated-time}@defun statprof-accumulated-time
126Returns the time accumulated during the last statprof run.@code{}
127
128@end defun
129
130@anchor{statprof statprof-sample-count}@defun statprof-sample-count
131Returns the number of samples taken during the last statprof run.@code{}
132
133@end defun
134
135@anchor{statprof statprof-fold-call-data}@defun statprof-fold-call-data proc init
136Fold @var{proc} over the call-data accumulated by statprof. Cannot be
137called while statprof is active. @var{proc} should take two arguments,
138@code{(@var{call-data} @var{prior-result})}.
139
140Note that a given proc-name may appear multiple times, but if it does,
141it represents different functions with the same name.
142
143@end defun
144
145@anchor{statprof statprof-proc-call-data}@defun statprof-proc-call-data proc
146Returns the call-data associated with @var{proc}, or @code{#f} if none
147is available.
148
149@end defun
150
151@anchor{statprof statprof-call-data-name}@defun statprof-call-data-name cd
152@end defun
153
154@anchor{statprof statprof-call-data-calls}@defun statprof-call-data-calls cd
155@end defun
156
157@anchor{statprof statprof-call-data-cum-samples}@defun statprof-call-data-cum-samples cd
158@end defun
159
160@anchor{statprof statprof-call-data-self-samples}@defun statprof-call-data-self-samples cd
161@end defun
162
163@anchor{statprof statprof-call-data->stats}@defun statprof-call-data->stats call-data
164Returns an object of type @code{statprof-stats}.
165
166@end defun
167
168@anchor{statprof statprof-stats-proc-name}@defun statprof-stats-proc-name stats
169@end defun
170
171@anchor{statprof statprof-stats-%-time-in-proc}@defun statprof-stats-%-time-in-proc stats
172@end defun
173
174@anchor{statprof statprof-stats-cum-secs-in-proc}@defun statprof-stats-cum-secs-in-proc stats
175@end defun
176
177@anchor{statprof statprof-stats-self-secs-in-proc}@defun statprof-stats-self-secs-in-proc stats
178@end defun
179
180@anchor{statprof statprof-stats-calls}@defun statprof-stats-calls stats
181@end defun
182
183@anchor{statprof statprof-stats-self-secs-per-call}@defun statprof-stats-self-secs-per-call stats
184@end defun
185
186@anchor{statprof statprof-stats-cum-secs-per-call}@defun statprof-stats-cum-secs-per-call stats
187@end defun
188
189@anchor{statprof statprof-display}@defun statprof-display . _
190Displays a gprof-like summary of the statistics collected. Unless an
191optional @var{port} argument is passed, uses the current output port.
192
193@end defun
194
195@anchor{statprof statprof-display-anomolies}@defun statprof-display-anomolies
196A sanity check that attempts to detect anomolies in statprof's
197statistics.@code{}
198
199@end defun
200
201@anchor{statprof statprof-fetch-stacks}@defun statprof-fetch-stacks
202Returns a list of stacks, as they were captured since the last call to
203@code{statprof-reset}.
204
205Note that stacks are only collected if the @var{full-stacks?} argument
206to @code{statprof-reset} is true.
207
208@end defun
209
210@anchor{statprof statprof-fetch-call-tree}@defun statprof-fetch-call-tree
211@verbatim
212Return a call tree for the previous statprof run.
213
214The return value is a list of nodes, each of which is of the type:
215@@code
216 node ::= (@@var@{proc@} @@var@{count@} . @@var@{nodes@})
217@@end code
218@end verbatim
219
220@end defun
221
222@anchor{statprof statprof}@defun statprof thunk [#:loop] [#:hz] [#:count-calls?] [#:full-stacks?]
cdcba5b2 223Profile the execution of @var{thunk}, and return its return values.
58c4a39d
AW
224
225The stack will be sampled @var{hz} times per second, and the thunk
226itself will be called @var{loop} times.
227
228If @var{count-calls?} is true, all procedure calls will be recorded.
229This operation is somewhat expensive.
230
231If @var{full-stacks?} is true, at each sample, statprof will store away
232the whole call tree, for later analysis. Use
233@code{statprof-fetch-stacks} or @code{statprof-fetch-call-tree} to
234retrieve the last-stored stacks.
235
236@end defun
237
238@anchor{statprof with-statprof}@defspec with-statprof args
cdcba5b2
LC
239Profile the expressions in the body, and return the body's return
240value.
58c4a39d
AW
241
242Keyword arguments:
243
244@table @code
245@item #:loop
246Execute the body @var{loop} number of times, or @code{#f} for no looping
247
248default: @code{#f}
249
250@item #:hz
251Sampling rate
252
253default: @code{20}
254
255@item #:count-calls?
256Whether to instrument each function call (expensive)
257
258default: @code{#f}
259
260@item #:full-stacks?
261Whether to collect away all sampled stacks into a list
262
263default: @code{#f}
264
265@end table
266
267@end defspec
268
269@anchor{statprof gcprof}@defun gcprof thunk [#:loop] [#:full-stacks?]
270Do an allocation profile of the execution of @var{thunk}.
271
272The stack will be sampled soon after every garbage collection, yielding
273an approximate idea of what is causing allocation in your program.
274
275Since 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}
277times.
278
279If @var{full-stacks?} is true, at each sample, statprof will store away
280the whole call tree, for later analysis. Use
281@code{statprof-fetch-stacks} or @code{statprof-fetch-call-tree} to
282retrieve the last-stored stacks.
283
284@end defun