Statprof always stores full stack traces
[bpt/guile.git] / module / statprof.scm
1 ;;;; (statprof) -- a statistical profiler for Guile
2 ;;;; -*-scheme-*-
3 ;;;;
4 ;;;; Copyright (C) 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.
5 ;;;; Copyright (C) 2004, 2009 Andy Wingo <wingo at pobox dot com>
6 ;;;; Copyright (C) 2001 Rob Browning <rlb at defaultvalue dot org>
7 ;;;;
8 ;;;; This library is free software; you can redistribute it and/or
9 ;;;; modify it under the terms of the GNU Lesser General Public
10 ;;;; License as published by the Free Software Foundation; either
11 ;;;; version 3 of the License, or (at your option) any later version.
12 ;;;;
13 ;;;; This library is distributed in the hope that it will be useful,
14 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;;;; Lesser General Public License for more details.
17 ;;;;
18 ;;;; You should have received a copy of the GNU Lesser General Public
19 ;;;; License along with this library; if not, write to the Free Software
20 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;;;;
22 \f
23
24 ;;; Commentary:
25 ;;;
26 ;;; @code{(statprof)} is a statistical profiler for Guile.
27 ;;;
28 ;;; A simple use of statprof would look like this:
29 ;;;
30 ;;; @example
31 ;;; (statprof (lambda () (do-something))
32 ;;; #:hz 100
33 ;;; #:count-calls? #t)
34 ;;; @end example
35 ;;;
36 ;;; This would run the thunk with statistical profiling, finally
37 ;;; displaying a gprof flat-style table of statistics which could
38 ;;; something like this:
39 ;;;
40 ;;; @example
41 ;;; % cumulative self self total
42 ;;; time seconds seconds calls ms/call ms/call name
43 ;;; 35.29 0.23 0.23 2002 0.11 0.11 -
44 ;;; 23.53 0.15 0.15 2001 0.08 0.08 positive?
45 ;;; 23.53 0.15 0.15 2000 0.08 0.08 +
46 ;;; 11.76 0.23 0.08 2000 0.04 0.11 do-nothing
47 ;;; 5.88 0.64 0.04 2001 0.02 0.32 loop
48 ;;; 0.00 0.15 0.00 1 0.00 150.59 do-something
49 ;;; ...
50 ;;; @end example
51 ;;;
52 ;;; All of the numerical data with the exception of the calls column is
53 ;;; statistically approximate. In the following column descriptions, and
54 ;;; in all of statprof, "time" refers to execution time (both user and
55 ;;; system), not wall clock time.
56 ;;;
57 ;;; @table @asis
58 ;;; @item % time
59 ;;; The percent of the time spent inside the procedure itself
60 ;;; (not counting children).
61 ;;; @item cumulative seconds
62 ;;; The total number of seconds spent in the procedure, including
63 ;;; children.
64 ;;; @item self seconds
65 ;;; The total number of seconds spent in the procedure itself (not counting
66 ;;; children).
67 ;;; @item calls
68 ;;; The total number of times the procedure was called.
69 ;;; @item self ms/call
70 ;;; The average time taken by the procedure itself on each call, in ms.
71 ;;; @item total ms/call
72 ;;; The average time taken by each call to the procedure, including time
73 ;;; spent in child functions.
74 ;;; @item name
75 ;;; The name of the procedure.
76 ;;; @end table
77 ;;;
78 ;;; The profiler uses @code{eq?} and the procedure object itself to
79 ;;; identify the procedures, so it won't confuse different procedures with
80 ;;; the same name. They will show up as two different rows in the output.
81 ;;;
82 ;;; Right now the profiler is quite simplistic. I cannot provide
83 ;;; call-graphs or other higher level information. What you see in the
84 ;;; table is pretty much all there is. Patches are welcome :-)
85 ;;;
86 ;;; @section Implementation notes
87 ;;;
88 ;;; The profiler works by setting the unix profiling signal
89 ;;; @code{ITIMER_PROF} to go off after the interval you define in the call
90 ;;; to @code{statprof-reset}. When the signal fires, a sampling routine is
91 ;;; run which looks at the current procedure that's executing, and then
92 ;;; crawls up the stack, and for each procedure encountered, increments
93 ;;; that procedure's sample count. Note that if a procedure is encountered
94 ;;; multiple times on a given stack, it is only counted once. After the
95 ;;; sampling is complete, the profiler resets profiling timer to fire
96 ;;; again after the appropriate interval.
97 ;;;
98 ;;; Meanwhile, the profiler keeps track, via @code{get-internal-run-time},
99 ;;; how much CPU time (system and user -- which is also what
100 ;;; @code{ITIMER_PROF} tracks), has elapsed while code has been executing
101 ;;; within a statprof-start/stop block.
102 ;;;
103 ;;; The profiler also tries to avoid counting or timing its own code as
104 ;;; much as possible.
105 ;;;
106 ;;; Code:
107
108 (define-module (statprof)
109 #:use-module (srfi srfi-1)
110 #:use-module (srfi srfi-9)
111 #:use-module (srfi srfi-9 gnu)
112 #:autoload (ice-9 format) (format)
113 #:use-module (system vm vm)
114 #:use-module (system vm frame)
115 #:use-module (system vm debug)
116 #:use-module (system vm program)
117 #:export (statprof-active?
118 statprof-start
119 statprof-stop
120 statprof-reset
121
122 statprof-accumulated-time
123 statprof-sample-count
124 statprof-fold-call-data
125 statprof-proc-call-data
126 statprof-call-data-name
127 statprof-call-data-calls
128 statprof-call-data-cum-samples
129 statprof-call-data-self-samples
130 statprof-call-data->stats
131
132 statprof-stats-proc-name
133 statprof-stats-%-time-in-proc
134 statprof-stats-cum-secs-in-proc
135 statprof-stats-self-secs-in-proc
136 statprof-stats-calls
137 statprof-stats-self-secs-per-call
138 statprof-stats-cum-secs-per-call
139
140 statprof-display
141 statprof-display-anomalies
142 statprof-display-anomolies ; Deprecated spelling.
143
144 statprof-fetch-stacks
145 statprof-fetch-call-tree
146
147 statprof
148 with-statprof
149
150 gcprof))
151
152
153 ;; This profiler tracks two numbers for every function called while
154 ;; it's active. It tracks the total number of calls, and the number
155 ;; of times the function was active when the sampler fired.
156 ;;
157 ;; Globally the profiler tracks the total time elapsed and the number
158 ;; of times the sampler was fired.
159 ;;
160 ;; Right now, this profiler is not per-thread and is not thread safe.
161
162 (define-record-type <state>
163 (make-state accumulated-time last-start-time sample-count
164 sampling-period remaining-prof-time profile-level
165 call-counts gc-time-taken record-full-stacks?
166 stacks inside-profiler?
167 prev-sigprof-handler buffer buffer-pos)
168 state?
169 ;; Total time so far.
170 (accumulated-time accumulated-time set-accumulated-time!)
171 ;; Start-time when timer is active.
172 (last-start-time last-start-time set-last-start-time!)
173 ;; Total count of sampler calls.
174 (sample-count sample-count set-sample-count!)
175 ;; Microseconds.
176 (sampling-period sampling-period set-sampling-period!)
177 ;; Time remaining when prof suspended.
178 (remaining-prof-time remaining-prof-time set-remaining-prof-time!)
179 ;; For user start/stop nesting.
180 (profile-level profile-level set-profile-level!)
181 ;; Hash table mapping ip -> call count, or #f if not counting calls.
182 (call-counts call-counts set-call-counts!)
183 ;; GC time between statprof-start and statprof-stop.
184 (gc-time-taken gc-time-taken set-gc-time-taken!)
185 ;; If #t, stash away the stacks for future analysis.
186 (record-full-stacks? record-full-stacks? set-record-full-stacks?!)
187 ;; If record-full-stacks?, the stashed full stacks.
188 (stacks stacks set-stacks!)
189 ;; True if we are inside the profiler.
190 (inside-profiler? inside-profiler? set-inside-profiler?!)
191 ;; True if we are inside the profiler.
192 (prev-sigprof-handler prev-sigprof-handler set-prev-sigprof-handler!)
193 ;; Stack samples.
194 (buffer buffer set-buffer!)
195 (buffer-pos buffer-pos set-buffer-pos!))
196
197 (define profiler-state (make-parameter #f))
198
199 (define (fresh-buffer)
200 (make-vector 1024 #f))
201
202 (define (expand-buffer buf)
203 (let* ((size (vector-length buf))
204 (new (make-vector (* size 2) #f)))
205 (vector-move-left! buf 0 (vector-length buf) new 0)
206 new))
207
208 (define* (fresh-profiler-state #:key (count-calls? #f)
209 (sampling-period 10000)
210 (full-stacks? #f))
211 (make-state 0 #f 0 sampling-period 0 0
212 (and count-calls? (make-hash-table))
213 0 #f '() #f #f (fresh-buffer) 0))
214
215 (define (ensure-profiler-state)
216 (or (profiler-state)
217 (let ((state (fresh-profiler-state)))
218 (profiler-state state)
219 state)))
220
221 (define (existing-profiler-state)
222 (or (profiler-state)
223 (error "expected there to be a profiler state")))
224
225 (define (accumulate-time state stop-time)
226 (set-accumulated-time! state
227 (+ (accumulated-time state)
228 (- stop-time (last-start-time state)))))
229
230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 ;; SIGPROF handler
232
233 (define (sample-stack-procs state stack)
234 (when (record-full-stacks? state)
235 (set-stacks! state (cons stack (stacks state))))
236
237 (set-sample-count! state (+ (sample-count state) 1))
238
239 (let lp ((frame (stack-ref stack 0))
240 (buffer (buffer state))
241 (pos (buffer-pos state)))
242 (define (write-sample sample)
243 (vector-set! buffer pos sample))
244 (define (continue pos)
245 (lp (frame-previous frame) buffer pos))
246 (define (write-sample-and-continue sample)
247 (write-sample sample)
248 (continue (1+ pos)))
249 (cond
250 ((= pos (vector-length buffer))
251 (lp frame (expand-buffer buffer) pos))
252 ((not frame)
253 (write-sample #f)
254 (set-buffer! state buffer)
255 (set-buffer-pos! state (1+ pos)))
256 (else
257 (let ((proc (frame-procedure frame)))
258 (cond
259 ((primitive? proc)
260 (write-sample-and-continue (procedure-name proc)))
261 ((program? proc)
262 (write-sample-and-continue (frame-instruction-pointer frame)))
263 (proc (write-sample-and-continue proc))
264 ;; If proc is false, that would confuse our stack walker.
265 ;; Ignore it.
266 (else (continue pos))))))))
267
268 (define (reset-sigprof-timer usecs)
269 ;; Guile's setitimer binding is terrible.
270 (let ((prev (setitimer ITIMER_PROF 0 0 0 usecs)))
271 (+ (* (caadr prev) #e1e6) (cdadr prev))))
272
273 (define (profile-signal-handler sig)
274 (define state (existing-profiler-state))
275
276 (set-inside-profiler?! state #t)
277
278 ;; FIXME: with-statprof should be able to set an outer frame for the
279 ;; stack cut
280 (when (positive? (profile-level state))
281 (let* ((stop-time (get-internal-run-time))
282 ;; cut down to the signal handler. note that this will only
283 ;; work if statprof.scm is compiled; otherwise we get
284 ;; `eval' on the stack instead, because if it's not
285 ;; compiled, profile-signal-handler is a thunk that
286 ;; tail-calls eval. perhaps we should always compile the
287 ;; signal handler instead...
288 (stack (or (make-stack #t profile-signal-handler)
289 (pk 'what! (make-stack #t)))))
290
291 (sample-stack-procs state stack)
292 (accumulate-time state stop-time)
293 (set-last-start-time! state (get-internal-run-time))
294
295 (reset-sigprof-timer (sampling-period state))))
296
297 (set-inside-profiler?! state #f))
298
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ;; Count total calls.
301
302 (define (count-call frame)
303 (let ((state (existing-profiler-state)))
304 (unless (inside-profiler? state)
305 (accumulate-time state (get-internal-run-time))
306
307 (let* ((key (let ((proc (frame-procedure frame)))
308 (cond
309 ((primitive? proc) (procedure-name proc))
310 ((program? proc) (program-code proc))
311 (else proc))))
312 (handle (hashv-create-handle! (call-counts state) key 0)))
313 (set-cdr! handle (1+ (cdr handle))))
314
315 (set-last-start-time! state (get-internal-run-time)))))
316
317 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
318
319 (define (statprof-active?)
320 "Returns @code{#t} if @code{statprof-start} has been called more times
321 than @code{statprof-stop}, @code{#f} otherwise."
322 (define state (profiler-state))
323 (and state (positive? (profile-level state))))
324
325 ;; Do not call this from statprof internal functions -- user only.
326 (define* (statprof-start #:optional (state (ensure-profiler-state)))
327 "Start the profiler.@code{}"
328 ;; After some head-scratching, I don't *think* I need to mask/unmask
329 ;; signals here, but if I'm wrong, please let me know.
330 (set-profile-level! state (+ (profile-level state) 1))
331 (when (= (profile-level state) 1)
332 (let ((rpt (remaining-prof-time state)))
333 (set-remaining-prof-time! state 0)
334 ;; FIXME: Use per-thread run time.
335 (set-last-start-time! state (get-internal-run-time))
336 (set-gc-time-taken! state (assq-ref (gc-stats) 'gc-time-taken))
337 (let ((prev (sigaction SIGPROF profile-signal-handler)))
338 (set-prev-sigprof-handler! state (car prev)))
339 (reset-sigprof-timer (if (zero? rpt) (sampling-period state) rpt))
340 (when (call-counts state)
341 (add-hook! (vm-apply-hook) count-call))
342 (set-vm-trace-level! (1+ (vm-trace-level)))
343 #t)))
344
345 ;; Do not call this from statprof internal functions -- user only.
346 (define* (statprof-stop #:optional (state (ensure-profiler-state)))
347 "Stop the profiler.@code{}"
348 ;; After some head-scratching, I don't *think* I need to mask/unmask
349 ;; signals here, but if I'm wrong, please let me know.
350 (set-profile-level! state (- (profile-level state) 1))
351 (when (zero? (profile-level state))
352 (set-gc-time-taken! state
353 (- (assq-ref (gc-stats) 'gc-time-taken)
354 (gc-time-taken state)))
355 (set-vm-trace-level! (1- (vm-trace-level)))
356 (when (call-counts state)
357 (remove-hook! (vm-apply-hook) count-call))
358 ;; I believe that we need to do this before getting the time
359 ;; (unless we want to make things even more complicated).
360 (set-remaining-prof-time! state (reset-sigprof-timer 0))
361 (accumulate-time state (get-internal-run-time))
362 (sigaction SIGPROF (prev-sigprof-handler state))
363 (set-prev-sigprof-handler! state #f)
364 (set-last-start-time! state #f)))
365
366 (define* (statprof-reset sample-seconds sample-microseconds count-calls?
367 #:optional full-stacks?)
368 "Reset the statprof sampler interval to @var{sample-seconds} and
369 @var{sample-microseconds}. If @var{count-calls?} is true, arrange to
370 instrument procedure calls as well as collecting statistical profiling
371 data. If @var{full-stacks?} is true, collect all sampled stacks into a
372 list for later analysis.
373
374 Enables traps and debugging as necessary."
375 (when (statprof-active?)
376 (error "Can't reset profiler while profiler is running."))
377 (profiler-state
378 (fresh-profiler-state #:count-calls? count-calls?
379 #:sampling-period (+ (* sample-seconds #e1e6)
380 sample-microseconds)
381 #:full-stacks? full-stacks?))
382 (values))
383
384 (define-record-type call-data
385 (make-call-data name printable call-count cum-sample-count self-sample-count)
386 call-data?
387 (name call-data-name)
388 (printable call-data-printable)
389 (call-count call-data-call-count set-call-data-call-count!)
390 (cum-sample-count call-data-cum-sample-count set-call-data-cum-sample-count!)
391 (self-sample-count call-data-self-sample-count set-call-data-self-sample-count!))
392
393 (define (source->string source)
394 (format #f "~a:~a:~a"
395 (or (source-file source) "<current input>")
396 (source-line-for-user source)
397 (source-column source)))
398
399 (define (program-debug-info-printable pdi)
400 (let* ((addr (program-debug-info-addr pdi))
401 (name (or (and=> (program-debug-info-name pdi) symbol->string)
402 (string-append "#x" (number->string addr 16))))
403 (loc (and=> (find-source-for-addr addr) source->string)))
404 (if loc
405 (string-append name " at " loc)
406 name)))
407
408 (define (addr->pdi addr cache)
409 (cond
410 ((hashv-get-handle cache addr) => cdr)
411 (else
412 (let ((data (find-program-debug-info addr)))
413 (hashv-set! cache addr data)
414 data))))
415
416 (define (addr->printable addr pdi)
417 (if pdi
418 (program-debug-info-printable pdi)
419 (string-append "#x" (number->string addr 16))))
420
421 (define (inc-call-data-cum-sample-count! cd)
422 (set-call-data-cum-sample-count! cd (1+ (call-data-cum-sample-count cd))))
423 (define (inc-call-data-self-sample-count! cd)
424 (set-call-data-self-sample-count! cd (1+ (call-data-self-sample-count cd))))
425
426 (define (stack-samples->procedure-data state)
427 (let ((table (make-hash-table))
428 (addr-cache (make-hash-table))
429 (call-counts (call-counts state))
430 (buffer (buffer state))
431 (len (buffer-pos state)))
432 (define (addr->call-data addr)
433 (let* ((pdi (addr->pdi addr addr-cache))
434 (entry (if pdi (program-debug-info-addr pdi) addr)))
435 (or (hashv-ref table entry)
436 (let ((data (make-call-data (and=> pdi program-debug-info-name)
437 (addr->printable entry pdi)
438 (and call-counts
439 (hashv-ref call-counts entry))
440 0
441 0)))
442 (hashv-set! table entry data)
443 data))))
444
445 (define (callee->call-data callee)
446 (cond
447 ((number? callee) (addr->call-data callee))
448 ((hashv-ref table callee))
449 (else
450 (let ((data (make-call-data
451 (cond ((procedure? callee) (procedure-name callee))
452 ;; a primitive
453 ((symbol? callee) callee)
454 (else #f))
455 (with-output-to-string (lambda () (write callee)))
456 (and call-counts (hashv-ref call-counts callee))
457 0
458 0)))
459 (hashv-set! table callee data)
460 data))))
461
462 (when call-counts
463 (hash-for-each (lambda (callee count)
464 (callee->call-data callee))
465 call-counts))
466
467 (let visit-stacks ((pos 0))
468 (cond
469 ((< pos len)
470 ;; FIXME: if we are counting all procedure calls, and
471 ;; count-call is on the stack, we need to not count the part
472 ;; of the stack that is within count-call.
473 (inc-call-data-self-sample-count!
474 (callee->call-data (vector-ref buffer pos)))
475 (let visit-stack ((pos pos))
476 (let ((callee (vector-ref buffer pos)))
477 (cond
478 ((vector-ref buffer pos)
479 => (lambda (callee)
480 (inc-call-data-cum-sample-count! (callee->call-data callee))
481 (visit-stack (1+ pos))))
482 (else
483 (visit-stacks (1+ pos)))))))
484 (else table)))))
485
486 (define (statprof-fold-call-data proc init)
487 "Fold @var{proc} over the call-data accumulated by statprof. Cannot be
488 called while statprof is active. @var{proc} should take two arguments,
489 @code{(@var{call-data} @var{prior-result})}.
490
491 Note that a given proc-name may appear multiple times, but if it does,
492 it represents different functions with the same name."
493 (when (statprof-active?)
494 (error "Can't call statprof-fold-call-data while profiler is running."))
495 (hash-fold
496 (lambda (key value prior-result)
497 (proc value prior-result))
498 init
499 (stack-samples->procedure-data (existing-profiler-state))))
500
501 (define (statprof-proc-call-data proc)
502 "Returns the call-data associated with @var{proc}, or @code{#f} if
503 none is available."
504 (when (statprof-active?)
505 (error "Can't call statprof-proc-call-data while profiler is running."))
506 (hashv-ref (stack-samples->procedure-data (existing-profiler-state))
507 (cond
508 ((primitive? proc) (procedure-name proc))
509 ((program? proc) (program-code proc))
510 (else (program-code proc)))))
511
512 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
513 ;; Stats
514
515 (define (statprof-call-data->stats call-data)
516 "Returns an object of type @code{statprof-stats}."
517 ;; returns (vector proc-name
518 ;; %-time-in-proc
519 ;; cum-seconds-in-proc
520 ;; self-seconds-in-proc
521 ;; num-calls
522 ;; self-secs-per-call
523 ;; total-secs-per-call)
524
525 (define state (existing-profiler-state))
526
527 (let* ((proc-name (call-data-printable call-data))
528 (self-samples (call-data-self-sample-count call-data))
529 (cum-samples (call-data-cum-sample-count call-data))
530 (all-samples (statprof-sample-count))
531 (secs-per-sample (/ (statprof-accumulated-time)
532 (statprof-sample-count)))
533 (num-calls (and (call-counts state)
534 (statprof-call-data-calls call-data))))
535
536 (vector proc-name
537 (* (/ self-samples all-samples) 100.0)
538 (* cum-samples secs-per-sample 1.0)
539 (* self-samples secs-per-sample 1.0)
540 num-calls
541 (and num-calls ;; maybe we only sampled in children
542 (if (zero? self-samples) 0.0
543 (/ (* self-samples secs-per-sample) 1.0 num-calls)))
544 (and num-calls ;; cum-samples must be positive
545 (/ (* cum-samples secs-per-sample)
546 1.0
547 ;; num-calls might be 0 if we entered statprof during the
548 ;; dynamic extent of the call
549 (max num-calls 1))))))
550
551 (define (statprof-stats-proc-name stats) (vector-ref stats 0))
552 (define (statprof-stats-%-time-in-proc stats) (vector-ref stats 1))
553 (define (statprof-stats-cum-secs-in-proc stats) (vector-ref stats 2))
554 (define (statprof-stats-self-secs-in-proc stats) (vector-ref stats 3))
555 (define (statprof-stats-calls stats) (vector-ref stats 4))
556 (define (statprof-stats-self-secs-per-call stats) (vector-ref stats 5))
557 (define (statprof-stats-cum-secs-per-call stats) (vector-ref stats 6))
558
559 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
560
561 (define (stats-sorter x y)
562 (let ((diff (- (statprof-stats-self-secs-in-proc x)
563 (statprof-stats-self-secs-in-proc y))))
564 (positive?
565 (if (= diff 0)
566 (- (statprof-stats-cum-secs-in-proc x)
567 (statprof-stats-cum-secs-in-proc y))
568 diff))))
569
570 (define* (statprof-display #:optional (port (current-output-port))
571 (state (existing-profiler-state)))
572 "Displays a gprof-like summary of the statistics collected. Unless an
573 optional @var{port} argument is passed, uses the current output port."
574 (cond
575 ((zero? (statprof-sample-count))
576 (format port "No samples recorded.\n"))
577 (else
578 (let* ((stats-list (statprof-fold-call-data
579 (lambda (data prior-value)
580 (cons (statprof-call-data->stats data)
581 prior-value))
582 '()))
583 (sorted-stats (sort stats-list stats-sorter)))
584
585 (define (display-stats-line stats)
586 (format port "~6,2f ~9,2f ~9,2f"
587 (statprof-stats-%-time-in-proc stats)
588 (statprof-stats-cum-secs-in-proc stats)
589 (statprof-stats-self-secs-in-proc stats))
590 (if (call-counts state)
591 (if (statprof-stats-calls stats)
592 (format port " ~7d ~8,2f ~8,2f "
593 (statprof-stats-calls stats)
594 (* 1000 (statprof-stats-self-secs-per-call stats))
595 (* 1000 (statprof-stats-cum-secs-per-call stats)))
596 (format port " "))
597 (display " " port))
598 (display (statprof-stats-proc-name stats) port)
599 (newline port))
600
601 (if (call-counts state)
602 (begin
603 (format port "~5a ~10a ~7a ~8a ~8a ~8a ~8@a\n"
604 "% " "cumulative" "self" "" "self" "total" "")
605 (format port "~5a ~9a ~8a ~8a ~8a ~8a ~8@a\n"
606 "time" "seconds" "seconds" "calls" "ms/call" "ms/call" "name"))
607 (begin
608 (format port "~5a ~10a ~7a ~8@a\n"
609 "%" "cumulative" "self" "")
610 (format port "~5a ~10a ~7a ~8@a\n"
611 "time" "seconds" "seconds" "name")))
612
613 (for-each display-stats-line sorted-stats)
614
615 (display "---\n" port)
616 (simple-format #t "Sample count: ~A\n" (statprof-sample-count))
617 (simple-format #t "Total time: ~A seconds (~A seconds in GC)\n"
618 (statprof-accumulated-time)
619 (/ (gc-time-taken state)
620 1.0 internal-time-units-per-second))))))
621
622 (define* (statprof-display-anomalies #:optional (state
623 (existing-profiler-state)))
624 "A sanity check that attempts to detect anomalies in statprof's
625 statistics.@code{}"
626 (statprof-fold-call-data
627 (lambda (data prior-value)
628 (when (and (call-counts state)
629 (zero? (call-data-call-count data))
630 (positive? (call-data-cum-sample-count data)))
631 (simple-format #t
632 "==[~A ~A ~A]\n"
633 (call-data-name data)
634 (call-data-call-count data)
635 (call-data-cum-sample-count data))))
636 #f)
637 (simple-format #t "Total time: ~A\n" (statprof-accumulated-time))
638 (simple-format #t "Sample count: ~A\n" (statprof-sample-count)))
639
640 (define (statprof-display-anomolies)
641 (issue-deprecation-warning "statprof-display-anomolies is a misspelling. "
642 "Use statprof-display-anomalies instead.")
643 (statprof-display-anomalies))
644
645 (define* (statprof-accumulated-time #:optional (state
646 (existing-profiler-state)))
647 "Returns the time accumulated during the last statprof run.@code{}"
648 (/ (accumulated-time state) 1.0 internal-time-units-per-second))
649
650 (define* (statprof-sample-count #:optional (state (existing-profiler-state)))
651 "Returns the number of samples taken during the last statprof run.@code{}"
652 (sample-count state))
653
654 (define statprof-call-data-name call-data-name)
655 (define statprof-call-data-calls call-data-call-count)
656 (define statprof-call-data-cum-samples call-data-cum-sample-count)
657 (define statprof-call-data-self-samples call-data-self-sample-count)
658
659 (define* (statprof-fetch-stacks #:optional (state (existing-profiler-state)))
660 "Returns a list of stacks, as they were captured since the last call
661 to @code{statprof-reset}.
662
663 Note that stacks are only collected if the @var{full-stacks?} argument
664 to @code{statprof-reset} is true."
665 (stacks state))
666
667 (define procedure=?
668 (lambda (a b)
669 (cond
670 ((eq? a b))
671 ((and (program? a) (program? b))
672 (eq? (program-code a) (program-code b)))
673 (else
674 #f))))
675
676 ;; tree ::= (car n . tree*)
677
678 (define (lists->trees lists equal?)
679 (let lp ((in lists) (n-terminal 0) (tails '()))
680 (cond
681 ((null? in)
682 (let ((trees (map (lambda (tail)
683 (cons (car tail)
684 (lists->trees (cdr tail) equal?)))
685 tails)))
686 (cons (apply + n-terminal (map cadr trees))
687 (sort trees
688 (lambda (a b) (> (cadr a) (cadr b)))))))
689 ((null? (car in))
690 (lp (cdr in) (1+ n-terminal) tails))
691 ((find (lambda (x) (equal? (car x) (caar in)))
692 tails)
693 => (lambda (tail)
694 (lp (cdr in)
695 n-terminal
696 (assq-set! tails
697 (car tail)
698 (cons (cdar in) (cdr tail))))))
699 (else
700 (lp (cdr in)
701 n-terminal
702 (acons (caar in) (list (cdar in)) tails))))))
703
704 (define (stack->procedures stack)
705 (filter identity
706 (unfold-right (lambda (x) (not x))
707 frame-procedure
708 frame-previous
709 (stack-ref stack 0))))
710
711 (define* (statprof-fetch-call-tree #:optional (state (existing-profiler-state)))
712 "Return a call tree for the previous statprof run.
713
714 The return value is a list of nodes, each of which is of the type:
715 @code
716 node ::= (@var{proc} @var{count} . @var{nodes})
717 @end code"
718 (cons #t (lists->trees (map stack->procedures (stacks state)) procedure=?)))
719
720 (define* (statprof thunk #:key (loop 1) (hz 100) (count-calls? #f)
721 (full-stacks? #f) (port (current-output-port)))
722 "Profiles the execution of @var{thunk}.
723
724 The stack will be sampled @var{hz} times per second, and the thunk itself will
725 be called @var{loop} times.
726
727 If @var{count-calls?} is true, all procedure calls will be recorded. This
728 operation is somewhat expensive.
729
730 If @var{full-stacks?} is true, at each sample, statprof will store away the
731 whole call tree, for later analysis. Use @code{statprof-fetch-stacks} or
732 @code{statprof-fetch-call-tree} to retrieve the last-stored stacks."
733
734 (let ((state (fresh-profiler-state #:count-calls? count-calls?
735 #:sampling-period
736 (inexact->exact (round (/ 1e6 hz)))
737 #:full-stacks? full-stacks?)))
738 (parameterize ((profiler-state state))
739 (dynamic-wind
740 (lambda ()
741 (statprof-start state))
742 (lambda ()
743 (let lp ((i loop))
744 (unless (zero? i)
745 (thunk)
746 (lp (1- i)))))
747 (lambda ()
748 (statprof-stop state)
749 (statprof-display port state))))))
750
751 (define-macro (with-statprof . args)
752 "Profiles the expressions in its body.
753
754 Keyword arguments:
755
756 @table @code
757 @item #:loop
758 Execute the body @var{loop} number of times, or @code{#f} for no looping
759
760 default: @code{#f}
761 @item #:hz
762 Sampling rate
763
764 default: @code{20}
765 @item #:count-calls?
766 Whether to instrument each function call (expensive)
767
768 default: @code{#f}
769 @item #:full-stacks?
770 Whether to collect away all sampled stacks into a list
771
772 default: @code{#f}
773 @end table"
774 (define (kw-arg-ref kw args def)
775 (cond
776 ((null? args) (error "Invalid macro body"))
777 ((keyword? (car args))
778 (if (eq? (car args) kw)
779 (cadr args)
780 (kw-arg-ref kw (cddr args) def)))
781 ((eq? kw #f def) ;; asking for the body
782 args)
783 (else def))) ;; kw not found
784 `((@ (statprof) statprof)
785 (lambda () ,@(kw-arg-ref #f args #f))
786 #:loop ,(kw-arg-ref #:loop args 1)
787 #:hz ,(kw-arg-ref #:hz args 100)
788 #:count-calls? ,(kw-arg-ref #:count-calls? args #f)
789 #:full-stacks? ,(kw-arg-ref #:full-stacks? args #f)))
790
791 (define* (gcprof thunk #:key (loop 1) (full-stacks? #f))
792 "Do an allocation profile of the execution of @var{thunk}.
793
794 The stack will be sampled soon after every garbage collection, yielding
795 an approximate idea of what is causing allocation in your program.
796
797 Since GC does not occur very frequently, you may need to use the
798 @var{loop} parameter, to cause @var{thunk} to be called @var{loop}
799 times.
800
801 If @var{full-stacks?} is true, at each sample, statprof will store away the
802 whole call tree, for later analysis. Use @code{statprof-fetch-stacks} or
803 @code{statprof-fetch-call-tree} to retrieve the last-stored stacks."
804
805 (let ((state (fresh-profiler-state #:full-stacks? full-stacks?)))
806 (parameterize ((profiler-state state))
807 (define (gc-callback)
808 (unless (inside-profiler? state)
809 (set-inside-profiler?! state #t)
810
811 ;; FIXME: should be able to set an outer frame for the stack cut
812 (let ((stop-time (get-internal-run-time))
813 ;; Cut down to gc-callback, and then one before (the
814 ;; after-gc async). See the note in profile-signal-handler
815 ;; also.
816 (stack (or (make-stack #t gc-callback 0 1)
817 (pk 'what! (make-stack #t)))))
818 (sample-stack-procs state stack)
819 (accumulate-time state stop-time)
820 (set-last-start-time! state (get-internal-run-time)))
821
822 (set-inside-profiler?! state #f)))
823
824 (dynamic-wind
825 (lambda ()
826 (set-profile-level! state 1)
827 (set-last-start-time! state (get-internal-run-time))
828 (set-gc-time-taken! state (assq-ref (gc-stats) 'gc-time-taken))
829 (add-hook! after-gc-hook gc-callback))
830 (lambda ()
831 (let lp ((i loop))
832 (unless (zero? i)
833 (thunk)
834 (lp (1- i)))))
835 (lambda ()
836 (remove-hook! after-gc-hook gc-callback)
837 (set-gc-time-taken! state
838 (- (assq-ref (gc-stats) 'gc-time-taken)
839 (gc-time-taken state)))
840 (accumulate-time state (get-internal-run-time))
841 (set-profile-level! state 0)
842 (statprof-display))))))