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