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