1 @cindex Profiling, statistical
3 The @code{sb-sprof} module, loadable by
7 provides an alternate profiler which works by taking samples of the
8 program execution at regular intervals, instead of instrumenting
9 functions like @code{sb-profile:profile} does. You might find
10 @code{sb-sprof} more useful than the deterministic profiler when profiling
11 functions in the @code{common-lisp}-package, SBCL internals, or code
12 where the instrumenting overhead is excessive.
14 Additionally @code{sb-sprof} includes a limited deterministic profiler
15 which can be used for reporting the amounts of calls to some functions
18 @subsection Example Usage
25 (declaim (optimize speed))
27 (defun cpu-test-inner (a i)
34 (dotimes (i (expt 2 n) a)
35 (setf a (cpu-test-inner a i)))))
39 ;;; Take up to 1000 samples of running (CPU-TEST 26), and give a flat
40 ;;; table report at the end. Profiling will end one the body has been
41 ;;; evaluated once, whether or not 1000 samples have been taken.
42 (sb-sprof:with-profiling (:max-samples 1000
47 ;;; Record call counts for functions defined on symbols in the CL-USER
49 (sb-sprof:profile-call-counts "CL-USER")
51 ;;; Take 1000 samples of running (CPU-TEST 24), and give a flat
52 ;;; table report at the end. The body will be re-evaluated in a loop
53 ;;; until 1000 samples have been taken. A sample count will be printed
54 ;;; after each iteration.
55 (sb-sprof:with-profiling (:max-samples 1000
61 ;;;; Allocation profiling
63 (defun foo (&rest args)
64 (mapcar (lambda (x) (float x 1d0)) args))
68 (apply #'foo (loop repeat n collect n)))
70 (sb-sprof:with-profiling (:max-samples 10000
78 The flat report format will show a table of all functions that the
79 profiler encountered on the call stack during sampling, ordered by the
80 number of samples taken while executing that function.
84 Nr Count % Count % Count % Calls Function
85 ------------------------------------------------------------------------
86 1 69 24.4 97 34.3 69 24.4 67108864 CPU-TEST-INNER
87 2 64 22.6 64 22.6 133 47.0 - SB-VM::GENERIC-+
88 3 39 13.8 256 90.5 172 60.8 1 CPU-TEST
89 4 31 11.0 31 11.0 203 71.7 - SB-KERNEL:TWO-ARG-XOR
92 For each function, the table will show three absolute and relative
93 sample counts. The Self column shows samples taken while directly
94 executing that function. The Total column shows samples taken while
95 executing that function or functions called from it (sampled to a
96 platform-specific depth). The Cumul column shows the sum of all
97 Self columns up to and including that line in the table.
99 Additionally the Calls column will record the amount of calls that were
100 made to the function during the profiling run. This value will only
101 be reported for functions that have been explicitly marked for call counting
102 with @code{profile-call-counts}.
104 The profiler also hooks into the disassembler such that instructions which
105 have been sampled are annotated with their relative frequency of
106 sampling. This information is not stored across different sampling
110 ; 6CF: 702E JO L4 ; 6/242 samples
111 ; 6D1: D1E3 SHL EBX, 1
113 ; 6D5: L2: F6C303 TEST BL, 3 ; 2/242 samples
115 ; 6DA: 8BC3 MOV EAX, EBX ; 5/242 samples
116 ; 6DC: L3: 83F900 CMP ECX, 0 ; 4/242 samples
119 @subsection Platform support
121 This module is known not to work consistently on the Alpha platform,
122 for technical reasons related to the implementation of a machine
123 language idiom for marking sections of code to be treated as atomic by
124 the garbage collector; However, it should work on other platforms,
125 and the deficiency on the Alpha will eventually be rectified.
127 Allocation profiling is only supported on SBCL builds that use
128 the generational garbage collector. Tracking of call stacks at a
129 depth of more than two levels is only supported on x86 and x86-64.
133 @include macro-sb-sprof-with-profiling.texinfo
134 @include macro-sb-sprof-with-sampling.texinfo
136 @subsection Functions
138 @include fun-sb-sprof-report.texinfo
140 @include fun-sb-sprof-reset.texinfo
142 @include fun-sb-sprof-start-profiling.texinfo
144 @include fun-sb-sprof-stop-profiling.texinfo
146 @include fun-sb-sprof-profile-call-counts.texinfo
148 @include fun-sb-sprof-unprofile-call-counts.texinfo
150 @subsection Variables
152 @include var-sb-sprof-star-max-samples-star.texinfo
154 @include var-sb-sprof-star-sample-interval-star.texinfo
158 @code{sb-sprof} is an SBCL port, with enhancements, of Gerd
159 Moellmann's statistical profiler for CMUCL.