Refactorings: call-data has source, stats is a record
[bpt/guile.git] / module / statprof.scm
CommitLineData
47f3ce52
AW
1;;;; (statprof) -- a statistical profiler for Guile
2;;;; -*-scheme-*-
3;;;;
998f8494 4;;;; Copyright (C) 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.
47f3ce52
AW
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:
998f8494 25;;;
62fd93e2 26;;; @code{(statprof)} is a statistical profiler for Guile.
998f8494
AW
27;;;
28;;; A simple use of statprof would look like this:
29;;;
30;;; @example
188e2ae3
AW
31;;; (statprof (lambda () (do-something))
32;;; #:hz 100
33;;; #:count-calls? #t)
998f8494
AW
34;;; @end example
35;;;
188e2ae3
AW
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:
998f8494
AW
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;;;
47f3ce52
AW
106;;; Code:
107
47f3ce52
AW
108(define-module (statprof)
109 #:use-module (srfi srfi-1)
62fd93e2 110 #:use-module (srfi srfi-9)
e4a8775d 111 #:use-module (srfi srfi-9 gnu)
47f3ce52 112 #:autoload (ice-9 format) (format)
e1138ba1
AW
113 #:use-module (system vm vm)
114 #:use-module (system vm frame)
3f9f4a2d 115 #:use-module (system vm debug)
e1138ba1 116 #:use-module (system vm program)
47f3ce52
AW
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
91db6c4f
AW
141 statprof-display-anomalies
142 statprof-display-anomolies ; Deprecated spelling.
47f3ce52
AW
143
144 statprof-fetch-stacks
145 statprof-fetch-call-tree
146
e1138ba1 147 statprof
2d239a78
AW
148 with-statprof
149
150 gcprof))
47f3ce52
AW
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
62fd93e2
AW
162(define-record-type <state>
163 (make-state accumulated-time last-start-time sample-count
19bf8caf 164 sampling-period remaining-prof-time profile-level
cd073eb4 165 call-counts gc-time-taken inside-profiler?
3f9f4a2d 166 prev-sigprof-handler buffer buffer-pos)
62fd93e2
AW
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!)
19bf8caf
AW
174 ;; Microseconds.
175 (sampling-period sampling-period set-sampling-period!)
62fd93e2
AW
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!)
3f9f4a2d
AW
180 ;; Hash table mapping ip -> call count, or #f if not counting calls.
181 (call-counts call-counts set-call-counts!)
62fd93e2
AW
182 ;; GC time between statprof-start and statprof-stop.
183 (gc-time-taken gc-time-taken set-gc-time-taken!)
56bfce7c 184 ;; True if we are inside the profiler.
3072d762
AW
185 (inside-profiler? inside-profiler? set-inside-profiler?!)
186 ;; True if we are inside the profiler.
3f9f4a2d
AW
187 (prev-sigprof-handler prev-sigprof-handler set-prev-sigprof-handler!)
188 ;; Stack samples.
189 (buffer buffer set-buffer!)
190 (buffer-pos buffer-pos set-buffer-pos!))
62fd93e2
AW
191
192(define profiler-state (make-parameter #f))
193
3f9f4a2d
AW
194(define (fresh-buffer)
195 (make-vector 1024 #f))
196
197(define (expand-buffer buf)
198 (let* ((size (vector-length buf))
199 (new (make-vector (* size 2) #f)))
200 (vector-move-left! buf 0 (vector-length buf) new 0)
201 new))
202
4eb1fb9b 203(define* (fresh-profiler-state #:key (count-calls? #f)
cd073eb4
AW
204 (sampling-period 10000))
205 (make-state 0 #f 0
206 sampling-period 0 0
207 (and count-calls? (make-hash-table)) 0 #f
208 #f (fresh-buffer) 0))
4eb1fb9b 209
62fd93e2
AW
210(define (ensure-profiler-state)
211 (or (profiler-state)
4eb1fb9b 212 (let ((state (fresh-profiler-state)))
62fd93e2
AW
213 (profiler-state state)
214 state)))
47f3ce52 215
45a7de82
AW
216(define (existing-profiler-state)
217 (or (profiler-state)
218 (error "expected there to be a profiler state")))
219
62fd93e2
AW
220(define (accumulate-time state stop-time)
221 (set-accumulated-time! state
222 (+ (accumulated-time state)
62fd93e2 223 (- stop-time (last-start-time state)))))
47f3ce52 224
47f3ce52
AW
225;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226;; SIGPROF handler
227
e4a8775d 228(define (sample-stack-procs state stack)
3f9f4a2d
AW
229 (set-sample-count! state (+ (sample-count state) 1))
230
231 (let lp ((frame (stack-ref stack 0))
232 (buffer (buffer state))
233 (pos (buffer-pos state)))
234 (define (write-sample sample)
235 (vector-set! buffer pos sample))
236 (define (continue pos)
237 (lp (frame-previous frame) buffer pos))
238 (define (write-sample-and-continue sample)
239 (write-sample sample)
240 (continue (1+ pos)))
241 (cond
242 ((= pos (vector-length buffer))
243 (lp frame (expand-buffer buffer) pos))
244 ((not frame)
245 (write-sample #f)
246 (set-buffer! state buffer)
247 (set-buffer-pos! state (1+ pos)))
248 (else
249 (let ((proc (frame-procedure frame)))
250 (cond
251 ((primitive? proc)
252 (write-sample-and-continue (procedure-name proc)))
253 ((program? proc)
254 (write-sample-and-continue (frame-instruction-pointer frame)))
255 (proc (write-sample-and-continue proc))
256 ;; If proc is false, that would confuse our stack walker.
257 ;; Ignore it.
258 (else (continue pos))))))))
47f3ce52 259
19bf8caf 260(define (reset-sigprof-timer usecs)
e68ed839
AW
261 ;; Guile's setitimer binding is terrible.
262 (let ((prev (setitimer ITIMER_PROF 0 0 0 usecs)))
263 (+ (* (caadr prev) #e1e6) (cdadr prev))))
19bf8caf 264
47f3ce52 265(define (profile-signal-handler sig)
45a7de82 266 (define state (existing-profiler-state))
62fd93e2 267
56bfce7c 268 (set-inside-profiler?! state #t)
47f3ce52
AW
269
270 ;; FIXME: with-statprof should be able to set an outer frame for the
271 ;; stack cut
cad444e3
AW
272 (when (positive? (profile-level state))
273 (let* ((stop-time (get-internal-run-time))
274 ;; cut down to the signal handler. note that this will only
275 ;; work if statprof.scm is compiled; otherwise we get
276 ;; `eval' on the stack instead, because if it's not
277 ;; compiled, profile-signal-handler is a thunk that
278 ;; tail-calls eval. perhaps we should always compile the
279 ;; signal handler instead...
280 (stack (or (make-stack #t profile-signal-handler)
546efe25
AW
281 (pk 'what! (make-stack #t)))))
282
283 (sample-stack-procs state stack)
284 (accumulate-time state stop-time)
285 (set-last-start-time! state (get-internal-run-time))
286
19bf8caf 287 (reset-sigprof-timer (sampling-period state))))
e1138ba1 288
56bfce7c 289 (set-inside-profiler?! state #f))
47f3ce52
AW
290
291;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292;; Count total calls.
293
e1138ba1 294(define (count-call frame)
3f9f4a2d
AW
295 (let ((state (existing-profiler-state)))
296 (unless (inside-profiler? state)
297 (accumulate-time state (get-internal-run-time))
62fd93e2 298
3f9f4a2d
AW
299 (let* ((key (let ((proc (frame-procedure frame)))
300 (cond
301 ((primitive? proc) (procedure-name proc))
302 ((program? proc) (program-code proc))
303 (else proc))))
304 (handle (hashv-create-handle! (call-counts state) key 0)))
305 (set-cdr! handle (1+ (cdr handle))))
47f3ce52 306
3f9f4a2d 307 (set-last-start-time! state (get-internal-run-time)))))
47f3ce52
AW
308
309;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310
311(define (statprof-active?)
312 "Returns @code{#t} if @code{statprof-start} has been called more times
313than @code{statprof-stop}, @code{#f} otherwise."
45a7de82
AW
314 (define state (profiler-state))
315 (and state (positive? (profile-level state))))
47f3ce52
AW
316
317;; Do not call this from statprof internal functions -- user only.
13a977dd 318(define* (statprof-start #:optional (state (ensure-profiler-state)))
47f3ce52
AW
319 "Start the profiler.@code{}"
320 ;; After some head-scratching, I don't *think* I need to mask/unmask
321 ;; signals here, but if I'm wrong, please let me know.
62fd93e2 322 (set-profile-level! state (+ (profile-level state) 1))
cad444e3 323 (when (= (profile-level state) 1)
19bf8caf
AW
324 (let ((rpt (remaining-prof-time state)))
325 (set-remaining-prof-time! state 0)
cad444e3
AW
326 ;; FIXME: Use per-thread run time.
327 (set-last-start-time! state (get-internal-run-time))
3476a369 328 (set-gc-time-taken! state (assq-ref (gc-stats) 'gc-time-taken))
3072d762
AW
329 (let ((prev (sigaction SIGPROF profile-signal-handler)))
330 (set-prev-sigprof-handler! state (car prev)))
19bf8caf 331 (reset-sigprof-timer (if (zero? rpt) (sampling-period state) rpt))
3f9f4a2d 332 (when (call-counts state)
cad444e3
AW
333 (add-hook! (vm-apply-hook) count-call))
334 (set-vm-trace-level! (1+ (vm-trace-level)))
335 #t)))
47f3ce52
AW
336
337;; Do not call this from statprof internal functions -- user only.
13a977dd 338(define* (statprof-stop #:optional (state (ensure-profiler-state)))
47f3ce52
AW
339 "Stop the profiler.@code{}"
340 ;; After some head-scratching, I don't *think* I need to mask/unmask
341 ;; signals here, but if I'm wrong, please let me know.
62fd93e2 342 (set-profile-level! state (- (profile-level state) 1))
cad444e3
AW
343 (when (zero? (profile-level state))
344 (set-gc-time-taken! state
3476a369 345 (- (assq-ref (gc-stats) 'gc-time-taken)
cad444e3
AW
346 (gc-time-taken state)))
347 (set-vm-trace-level! (1- (vm-trace-level)))
3f9f4a2d 348 (when (call-counts state)
cad444e3
AW
349 (remove-hook! (vm-apply-hook) count-call))
350 ;; I believe that we need to do this before getting the time
351 ;; (unless we want to make things even more complicated).
19bf8caf 352 (set-remaining-prof-time! state (reset-sigprof-timer 0))
cad444e3 353 (accumulate-time state (get-internal-run-time))
3072d762
AW
354 (sigaction SIGPROF (prev-sigprof-handler state))
355 (set-prev-sigprof-handler! state #f)
cad444e3 356 (set-last-start-time! state #f)))
47f3ce52 357
e640b440
AW
358(define* (statprof-reset sample-seconds sample-microseconds count-calls?
359 #:optional full-stacks?)
47f3ce52
AW
360 "Reset the statprof sampler interval to @var{sample-seconds} and
361@var{sample-microseconds}. If @var{count-calls?} is true, arrange to
362instrument procedure calls as well as collecting statistical profiling
cd073eb4
AW
363data. (The optional @var{full-stacks?} argument is deprecated; statprof
364always collects full stacks.)"
4d0c358b 365 (when (statprof-active?)
4eb1fb9b 366 (error "Can't reset profiler while profiler is running."))
3072d762
AW
367 (profiler-state
368 (fresh-profiler-state #:count-calls? count-calls?
369 #:sampling-period (+ (* sample-seconds #e1e6)
cd073eb4 370 sample-microseconds)))
13a977dd 371 (values))
47f3ce52 372
3f9f4a2d 373(define-record-type call-data
e3997e70
AW
374 (make-call-data name source printable
375 call-count cum-sample-count self-sample-count)
3f9f4a2d
AW
376 call-data?
377 (name call-data-name)
e3997e70 378 (source call-data-source)
3f9f4a2d
AW
379 (printable call-data-printable)
380 (call-count call-data-call-count set-call-data-call-count!)
381 (cum-sample-count call-data-cum-sample-count set-call-data-cum-sample-count!)
382 (self-sample-count call-data-self-sample-count set-call-data-self-sample-count!))
383
384(define (source->string source)
385 (format #f "~a:~a:~a"
386 (or (source-file source) "<current input>")
387 (source-line-for-user source)
388 (source-column source)))
389
390(define (program-debug-info-printable pdi)
391 (let* ((addr (program-debug-info-addr pdi))
392 (name (or (and=> (program-debug-info-name pdi) symbol->string)
393 (string-append "#x" (number->string addr 16))))
394 (loc (and=> (find-source-for-addr addr) source->string)))
395 (if loc
396 (string-append name " at " loc)
397 name)))
398
399(define (addr->pdi addr cache)
400 (cond
401 ((hashv-get-handle cache addr) => cdr)
402 (else
403 (let ((data (find-program-debug-info addr)))
404 (hashv-set! cache addr data)
405 data))))
406
407(define (addr->printable addr pdi)
408 (if pdi
409 (program-debug-info-printable pdi)
410 (string-append "#x" (number->string addr 16))))
411
412(define (inc-call-data-cum-sample-count! cd)
413 (set-call-data-cum-sample-count! cd (1+ (call-data-cum-sample-count cd))))
414(define (inc-call-data-self-sample-count! cd)
415 (set-call-data-self-sample-count! cd (1+ (call-data-self-sample-count cd))))
416
417(define (stack-samples->procedure-data state)
418 (let ((table (make-hash-table))
419 (addr-cache (make-hash-table))
420 (call-counts (call-counts state))
421 (buffer (buffer state))
422 (len (buffer-pos state)))
423 (define (addr->call-data addr)
424 (let* ((pdi (addr->pdi addr addr-cache))
425 (entry (if pdi (program-debug-info-addr pdi) addr)))
426 (or (hashv-ref table entry)
427 (let ((data (make-call-data (and=> pdi program-debug-info-name)
e3997e70 428 (find-source-for-addr entry)
3f9f4a2d
AW
429 (addr->printable entry pdi)
430 (and call-counts
431 (hashv-ref call-counts entry))
432 0
433 0)))
434 (hashv-set! table entry data)
435 data))))
436
437 (define (callee->call-data callee)
438 (cond
439 ((number? callee) (addr->call-data callee))
440 ((hashv-ref table callee))
441 (else
442 (let ((data (make-call-data
443 (cond ((procedure? callee) (procedure-name callee))
444 ;; a primitive
445 ((symbol? callee) callee)
446 (else #f))
e3997e70 447 #f
3f9f4a2d
AW
448 (with-output-to-string (lambda () (write callee)))
449 (and call-counts (hashv-ref call-counts callee))
450 0
451 0)))
452 (hashv-set! table callee data)
453 data))))
454
455 (when call-counts
456 (hash-for-each (lambda (callee count)
457 (callee->call-data callee))
458 call-counts))
459
460 (let visit-stacks ((pos 0))
461 (cond
462 ((< pos len)
463 ;; FIXME: if we are counting all procedure calls, and
464 ;; count-call is on the stack, we need to not count the part
465 ;; of the stack that is within count-call.
466 (inc-call-data-self-sample-count!
467 (callee->call-data (vector-ref buffer pos)))
468 (let visit-stack ((pos pos))
cd073eb4
AW
469 (cond
470 ((vector-ref buffer pos)
471 => (lambda (callee)
472 (inc-call-data-cum-sample-count! (callee->call-data callee))
473 (visit-stack (1+ pos))))
474 (else
475 (visit-stacks (1+ pos))))))
3f9f4a2d
AW
476 (else table)))))
477
cd073eb4
AW
478(define (stack-samples->callee-lists state)
479 (let ((buffer (buffer state))
480 (len (buffer-pos state)))
481 (let visit-stacks ((pos 0) (out '()))
482 (cond
483 ((< pos len)
484 ;; FIXME: if we are counting all procedure calls, and
485 ;; count-call is on the stack, we need to not count the part
486 ;; of the stack that is within count-call.
487 (let visit-stack ((pos pos) (stack '()))
488 (cond
489 ((vector-ref buffer pos)
490 => (lambda (callee)
491 (visit-stack (1+ pos) (cons callee stack))))
492 (else
493 (visit-stacks (1+ pos) (cons (reverse stack) out))))))
494 (else (reverse out))))))
495
47f3ce52
AW
496(define (statprof-fold-call-data proc init)
497 "Fold @var{proc} over the call-data accumulated by statprof. Cannot be
498called while statprof is active. @var{proc} should take two arguments,
499@code{(@var{call-data} @var{prior-result})}.
500
501Note that a given proc-name may appear multiple times, but if it does,
502it represents different functions with the same name."
4d0c358b
AW
503 (when (statprof-active?)
504 (error "Can't call statprof-fold-call-data while profiler is running."))
47f3ce52
AW
505 (hash-fold
506 (lambda (key value prior-result)
507 (proc value prior-result))
508 init
3f9f4a2d 509 (stack-samples->procedure-data (existing-profiler-state))))
47f3ce52
AW
510
511(define (statprof-proc-call-data proc)
512 "Returns the call-data associated with @var{proc}, or @code{#f} if
513none is available."
4d0c358b
AW
514 (when (statprof-active?)
515 (error "Can't call statprof-proc-call-data while profiler is running."))
3f9f4a2d
AW
516 (hashv-ref (stack-samples->procedure-data (existing-profiler-state))
517 (cond
518 ((primitive? proc) (procedure-name proc))
519 ((program? proc) (program-code proc))
520 (else (program-code proc)))))
47f3ce52
AW
521
522;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
523;; Stats
524
e3997e70
AW
525(define-record-type stats
526 (make-stats proc-name %-time-in-proc cum-secs-in-proc self-secs-in-proc
527 calls self-secs-per-call cum-secs-per-call)
528 stats?
529 (proc-name statprof-stats-proc-name)
530 (%-time-in-proc statprof-stats-%-time-in-proc)
531 (cum-secs-in-proc statprof-stats-cum-secs-in-proc)
532 (self-secs-in-proc statprof-stats-self-secs-in-proc)
533 (calls statprof-stats-calls)
534 (self-secs-per-call statprof-stats-self-secs-per-call)
535 (cum-secs-per-call statprof-stats-cum-secs-per-call))
536
47f3ce52
AW
537(define (statprof-call-data->stats call-data)
538 "Returns an object of type @code{statprof-stats}."
45a7de82 539 (define state (existing-profiler-state))
62fd93e2 540
c165c50d 541 (let* ((proc-name (call-data-printable call-data))
47f3ce52
AW
542 (self-samples (call-data-self-sample-count call-data))
543 (cum-samples (call-data-cum-sample-count call-data))
544 (all-samples (statprof-sample-count))
545 (secs-per-sample (/ (statprof-accumulated-time)
546 (statprof-sample-count)))
3f9f4a2d
AW
547 (num-calls (and (call-counts state)
548 (statprof-call-data-calls call-data))))
47f3ce52 549
e3997e70
AW
550 (make-stats proc-name
551 (* (/ self-samples all-samples) 100.0)
552 (* cum-samples secs-per-sample 1.0)
553 (* self-samples secs-per-sample 1.0)
554 num-calls
555 (and num-calls ;; maybe we only sampled in children
556 (if (zero? self-samples) 0.0
557 (/ (* self-samples secs-per-sample) 1.0 num-calls)))
558 (and num-calls ;; cum-samples must be positive
559 (/ (* cum-samples secs-per-sample)
560 1.0
561 ;; num-calls might be 0 if we entered statprof during the
562 ;; dynamic extent of the call
563 (max num-calls 1))))))
47f3ce52
AW
564
565;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
566
567(define (stats-sorter x y)
568 (let ((diff (- (statprof-stats-self-secs-in-proc x)
569 (statprof-stats-self-secs-in-proc y))))
570 (positive?
571 (if (= diff 0)
572 (- (statprof-stats-cum-secs-in-proc x)
573 (statprof-stats-cum-secs-in-proc y))
574 diff))))
575
91db6c4f
AW
576(define* (statprof-display #:optional (port (current-output-port))
577 (state (existing-profiler-state)))
47f3ce52
AW
578 "Displays a gprof-like summary of the statistics collected. Unless an
579optional @var{port} argument is passed, uses the current output port."
47f3ce52
AW
580 (cond
581 ((zero? (statprof-sample-count))
582 (format port "No samples recorded.\n"))
583 (else
584 (let* ((stats-list (statprof-fold-call-data
585 (lambda (data prior-value)
586 (cons (statprof-call-data->stats data)
587 prior-value))
588 '()))
589 (sorted-stats (sort stats-list stats-sorter)))
590
591 (define (display-stats-line stats)
3f9f4a2d
AW
592 (format port "~6,2f ~9,2f ~9,2f"
593 (statprof-stats-%-time-in-proc stats)
594 (statprof-stats-cum-secs-in-proc stats)
595 (statprof-stats-self-secs-in-proc stats))
596 (if (call-counts state)
597 (if (statprof-stats-calls stats)
598 (format port " ~7d ~8,2f ~8,2f "
599 (statprof-stats-calls stats)
600 (* 1000 (statprof-stats-self-secs-per-call stats))
601 (* 1000 (statprof-stats-cum-secs-per-call stats)))
602 (format port " "))
603 (display " " port))
47f3ce52
AW
604 (display (statprof-stats-proc-name stats) port)
605 (newline port))
606
3f9f4a2d 607 (if (call-counts state)
47f3ce52
AW
608 (begin
609 (format port "~5a ~10a ~7a ~8a ~8a ~8a ~8@a\n"
610 "% " "cumulative" "self" "" "self" "total" "")
611 (format port "~5a ~9a ~8a ~8a ~8a ~8a ~8@a\n"
612 "time" "seconds" "seconds" "calls" "ms/call" "ms/call" "name"))
613 (begin
614 (format port "~5a ~10a ~7a ~8@a\n"
615 "%" "cumulative" "self" "")
616 (format port "~5a ~10a ~7a ~8@a\n"
617 "time" "seconds" "seconds" "name")))
618
619 (for-each display-stats-line sorted-stats)
620
621 (display "---\n" port)
622 (simple-format #t "Sample count: ~A\n" (statprof-sample-count))
623 (simple-format #t "Total time: ~A seconds (~A seconds in GC)\n"
624 (statprof-accumulated-time)
3476a369
AW
625 (/ (gc-time-taken state)
626 1.0 internal-time-units-per-second))))))
47f3ce52 627
91db6c4f
AW
628(define* (statprof-display-anomalies #:optional (state
629 (existing-profiler-state)))
630 "A sanity check that attempts to detect anomalies in statprof's
47f3ce52
AW
631statistics.@code{}"
632 (statprof-fold-call-data
633 (lambda (data prior-value)
3f9f4a2d 634 (when (and (call-counts state)
cad444e3
AW
635 (zero? (call-data-call-count data))
636 (positive? (call-data-cum-sample-count data)))
637 (simple-format #t
638 "==[~A ~A ~A]\n"
639 (call-data-name data)
640 (call-data-call-count data)
641 (call-data-cum-sample-count data))))
47f3ce52
AW
642 #f)
643 (simple-format #t "Total time: ~A\n" (statprof-accumulated-time))
644 (simple-format #t "Sample count: ~A\n" (statprof-sample-count)))
645
91db6c4f
AW
646(define (statprof-display-anomolies)
647 (issue-deprecation-warning "statprof-display-anomolies is a misspelling. "
648 "Use statprof-display-anomalies instead.")
649 (statprof-display-anomalies))
650
651(define* (statprof-accumulated-time #:optional (state
652 (existing-profiler-state)))
47f3ce52 653 "Returns the time accumulated during the last statprof run.@code{}"
91db6c4f 654 (/ (accumulated-time state) 1.0 internal-time-units-per-second))
47f3ce52 655
91db6c4f 656(define* (statprof-sample-count #:optional (state (existing-profiler-state)))
47f3ce52 657 "Returns the number of samples taken during the last statprof run.@code{}"
91db6c4f 658 (sample-count state))
47f3ce52
AW
659
660(define statprof-call-data-name call-data-name)
661(define statprof-call-data-calls call-data-call-count)
662(define statprof-call-data-cum-samples call-data-cum-sample-count)
663(define statprof-call-data-self-samples call-data-self-sample-count)
664
91db6c4f 665(define* (statprof-fetch-stacks #:optional (state (existing-profiler-state)))
47f3ce52 666 "Returns a list of stacks, as they were captured since the last call
cd073eb4
AW
667to @code{statprof-reset}."
668 (stack-samples->callee-lists state))
47f3ce52
AW
669
670(define procedure=?
663212bb
AW
671 (lambda (a b)
672 (cond
673 ((eq? a b))
0bd1e9c6 674 ((and (program? a) (program? b))
d1100525 675 (eq? (program-code a) (program-code b)))
663212bb
AW
676 (else
677 #f))))
47f3ce52
AW
678
679;; tree ::= (car n . tree*)
680
681(define (lists->trees lists equal?)
682 (let lp ((in lists) (n-terminal 0) (tails '()))
683 (cond
684 ((null? in)
685 (let ((trees (map (lambda (tail)
686 (cons (car tail)
687 (lists->trees (cdr tail) equal?)))
688 tails)))
689 (cons (apply + n-terminal (map cadr trees))
690 (sort trees
691 (lambda (a b) (> (cadr a) (cadr b)))))))
692 ((null? (car in))
693 (lp (cdr in) (1+ n-terminal) tails))
694 ((find (lambda (x) (equal? (car x) (caar in)))
695 tails)
696 => (lambda (tail)
697 (lp (cdr in)
698 n-terminal
699 (assq-set! tails
700 (car tail)
701 (cons (cdar in) (cdr tail))))))
702 (else
703 (lp (cdr in)
704 n-terminal
705 (acons (caar in) (list (cdar in)) tails))))))
706
91db6c4f 707(define* (statprof-fetch-call-tree #:optional (state (existing-profiler-state)))
47f3ce52
AW
708 "Return a call tree for the previous statprof run.
709
710The return value is a list of nodes, each of which is of the type:
711@code
712 node ::= (@var{proc} @var{count} . @var{nodes})
713@end code"
cd073eb4
AW
714 (define (callee->printable callee)
715 (cond
716 ((number? callee)
717 (addr->printable callee (find-program-debug-info callee)))
718 (else
719 (with-output-to-string (lambda () (write callee))))))
720 (define (memoizev/1 proc table)
721 (lambda (x)
722 (cond
723 ((hashv-get-handle table x) => cdr)
724 (else
725 (let ((res (proc x)))
726 (hashv-set! table x res)
727 res)))))
728 (let ((callee->printable (memoizev/1 callee->printable (make-hash-table))))
729 (cons #t (lists->trees (map (lambda (callee-list)
730 (map callee->printable callee-list))
731 (stack-samples->callee-lists state))
732 equal?))))
47f3ce52 733
e1138ba1 734(define* (statprof thunk #:key (loop 1) (hz 100) (count-calls? #f)
cd073eb4 735 (port (current-output-port)) full-stacks?)
e1138ba1
AW
736 "Profiles the execution of @var{thunk}.
737
738The stack will be sampled @var{hz} times per second, and the thunk itself will
739be called @var{loop} times.
740
741If @var{count-calls?} is true, all procedure calls will be recorded. This
cd073eb4 742operation is somewhat expensive."
e1138ba1 743
13a977dd
AW
744 (let ((state (fresh-profiler-state #:count-calls? count-calls?
745 #:sampling-period
cd073eb4 746 (inexact->exact (round (/ 1e6 hz))))))
fd5dfcce
AW
747 (parameterize ((profiler-state state))
748 (dynamic-wind
749 (lambda ()
13a977dd 750 (statprof-start state))
fd5dfcce
AW
751 (lambda ()
752 (let lp ((i loop))
753 (unless (zero? i)
754 (thunk)
755 (lp (1- i)))))
756 (lambda ()
13a977dd
AW
757 (statprof-stop state)
758 (statprof-display port state))))))
e1138ba1 759
47f3ce52
AW
760(define-macro (with-statprof . args)
761 "Profiles the expressions in its body.
762
763Keyword arguments:
764
765@table @code
766@item #:loop
767Execute the body @var{loop} number of times, or @code{#f} for no looping
768
769default: @code{#f}
770@item #:hz
771Sampling rate
772
773default: @code{20}
774@item #:count-calls?
775Whether to instrument each function call (expensive)
776
47f3ce52
AW
777default: @code{#f}
778@end table"
779 (define (kw-arg-ref kw args def)
780 (cond
781 ((null? args) (error "Invalid macro body"))
782 ((keyword? (car args))
783 (if (eq? (car args) kw)
784 (cadr args)
785 (kw-arg-ref kw (cddr args) def)))
786 ((eq? kw #f def) ;; asking for the body
787 args)
788 (else def))) ;; kw not found
e1138ba1
AW
789 `((@ (statprof) statprof)
790 (lambda () ,@(kw-arg-ref #f args #f))
791 #:loop ,(kw-arg-ref #:loop args 1)
792 #:hz ,(kw-arg-ref #:hz args 100)
793 #:count-calls? ,(kw-arg-ref #:count-calls? args #f)
794 #:full-stacks? ,(kw-arg-ref #:full-stacks? args #f)))
795
cd073eb4 796(define* (gcprof thunk #:key (loop 1) full-stacks?)
2d239a78
AW
797 "Do an allocation profile of the execution of @var{thunk}.
798
799The stack will be sampled soon after every garbage collection, yielding
800an approximate idea of what is causing allocation in your program.
801
802Since GC does not occur very frequently, you may need to use the
803@var{loop} parameter, to cause @var{thunk} to be called @var{loop}
cd073eb4 804times."
2d239a78 805
cd073eb4 806 (let ((state (fresh-profiler-state)))
fd5dfcce 807 (parameterize ((profiler-state state))
fd5dfcce 808 (define (gc-callback)
a7ede58d 809 (unless (inside-profiler? state)
fd5dfcce
AW
810 (set-inside-profiler?! state #t)
811
812 ;; FIXME: should be able to set an outer frame for the stack cut
813 (let ((stop-time (get-internal-run-time))
814 ;; Cut down to gc-callback, and then one before (the
815 ;; after-gc async). See the note in profile-signal-handler
816 ;; also.
817 (stack (or (make-stack #t gc-callback 0 1)
818 (pk 'what! (make-stack #t)))))
819 (sample-stack-procs state stack)
820 (accumulate-time state stop-time)
821 (set-last-start-time! state (get-internal-run-time)))
cd073eb4 822
a7ede58d 823 (set-inside-profiler?! state #f)))
fd5dfcce
AW
824
825 (dynamic-wind
826 (lambda ()
a7ede58d
AW
827 (set-profile-level! state 1)
828 (set-last-start-time! state (get-internal-run-time))
829 (set-gc-time-taken! state (assq-ref (gc-stats) 'gc-time-taken))
830 (add-hook! after-gc-hook gc-callback))
fd5dfcce
AW
831 (lambda ()
832 (let lp ((i loop))
833 (unless (zero? i)
834 (thunk)
835 (lp (1- i)))))
836 (lambda ()
a7ede58d
AW
837 (remove-hook! after-gc-hook gc-callback)
838 (set-gc-time-taken! state
839 (- (assq-ref (gc-stats) 'gc-time-taken)
840 (gc-time-taken state)))
841 (accumulate-time state (get-internal-run-time))
842 (set-profile-level! state 0)
fd5dfcce 843 (statprof-display))))))