1 ;;;; (statprof) -- a statistical profiler for Guile
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>
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.
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.
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
26 ;;; @code{(statprof)} is a statistical profiler for Guile.
28 ;;; A simple use of statprof would look like this:
31 ;;; (statprof (lambda () (do-something))
33 ;;; #:count-calls? #t)
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:
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
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.
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
64 ;;; @item self seconds
65 ;;; The total number of seconds spent in the procedure itself (not counting
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.
75 ;;; The name of the procedure.
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.
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 :-)
86 ;;; @section Implementation notes
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.
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.
103 ;;; The profiler also tries to avoid counting or timing its own code as
104 ;;; much as possible.
108 (define-module (statprof)
109 #:use-module (srfi srfi-1)
110 #:use-module (srfi srfi-9)
111 #:use-module (srfi srfi-9 gnu)
112 #:autoload (ice-9 format) (format)
113 #:use-module (system vm vm)
114 #:use-module (system vm frame)
115 #:use-module (system vm debug)
116 #:use-module (system vm program)
117 #:export (statprof-active?
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
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
137 statprof-stats-self-secs-per-call
138 statprof-stats-cum-secs-per-call
141 statprof-display-anomalies
142 statprof-display-anomolies ; Deprecated spelling.
144 statprof-fetch-stacks
145 statprof-fetch-call-tree
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.
157 ;; Globally the profiler tracks the total time elapsed and the number
158 ;; of times the sampler was fired.
160 ;; Right now, this profiler is not per-thread and is not thread safe.
162 (define-record-type <state>
163 (make-state accumulated-time last-start-time sample-count
164 sampling-period remaining-prof-time profile-level
165 call-counts gc-time-taken record-full-stacks?
166 stacks inside-profiler?
167 prev-sigprof-handler buffer buffer-pos)
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!)
176 (sampling-period sampling-period set-sampling-period!)
177 ;; Time remaining when prof suspended.
178 (remaining-prof-time remaining-prof-time set-remaining-prof-time!)
179 ;; For user start/stop nesting.
180 (profile-level profile-level set-profile-level!)
181 ;; Hash table mapping ip -> call count, or #f if not counting calls.
182 (call-counts call-counts set-call-counts!)
183 ;; GC time between statprof-start and statprof-stop.
184 (gc-time-taken gc-time-taken set-gc-time-taken!)
185 ;; If #t, stash away the stacks for future analysis.
186 (record-full-stacks? record-full-stacks? set-record-full-stacks?!)
187 ;; If record-full-stacks?, the stashed full stacks.
188 (stacks stacks set-stacks!)
189 ;; True if we are inside the profiler.
190 (inside-profiler? inside-profiler? set-inside-profiler?!)
191 ;; True if we are inside the profiler.
192 (prev-sigprof-handler prev-sigprof-handler set-prev-sigprof-handler!)
194 (buffer buffer set-buffer!)
195 (buffer-pos buffer-pos set-buffer-pos!))
197 (define profiler-state (make-parameter #f))
199 (define (fresh-buffer)
200 (make-vector 1024 #f))
202 (define (expand-buffer buf)
203 (let* ((size (vector-length buf))
204 (new (make-vector (* size 2) #f)))
205 (vector-move-left! buf 0 (vector-length buf) new 0)
208 (define* (fresh-profiler-state #:key (count-calls? #f)
209 (sampling-period 10000)
211 (make-state 0 #f 0 sampling-period 0 0
212 (and count-calls? (make-hash-table))
213 0 #f '() #f #f (fresh-buffer) 0))
215 (define (ensure-profiler-state)
217 (let ((state (fresh-profiler-state)))
218 (profiler-state state)
221 (define (existing-profiler-state)
223 (error "expected there to be a profiler state")))
225 (define (accumulate-time state stop-time)
226 (set-accumulated-time! state
227 (+ (accumulated-time state)
228 (- stop-time (last-start-time state)))))
230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 (define (sample-stack-procs state stack)
234 (when (record-full-stacks? state)
235 (set-stacks! state (cons stack (stacks state))))
237 (set-sample-count! state (+ (sample-count state) 1))
239 (let lp ((frame (stack-ref stack 0))
240 (buffer (buffer state))
241 (pos (buffer-pos state)))
242 (define (write-sample sample)
243 (vector-set! buffer pos sample))
244 (define (continue pos)
245 (lp (frame-previous frame) buffer pos))
246 (define (write-sample-and-continue sample)
247 (write-sample sample)
250 ((= pos (vector-length buffer))
251 (lp frame (expand-buffer buffer) pos))
254 (set-buffer! state buffer)
255 (set-buffer-pos! state (1+ pos)))
257 (let ((proc (frame-procedure frame)))
260 (write-sample-and-continue (procedure-name proc)))
262 (write-sample-and-continue (frame-instruction-pointer frame)))
263 (proc (write-sample-and-continue proc))
264 ;; If proc is false, that would confuse our stack walker.
266 (else (continue pos))))))))
268 (define (reset-sigprof-timer usecs)
269 ;; Guile's setitimer binding is terrible.
270 (let ((prev (setitimer ITIMER_PROF 0 0 0 usecs)))
271 (+ (* (caadr prev) #e1e6) (cdadr prev))))
273 (define (profile-signal-handler sig)
274 (define state (existing-profiler-state))
276 (set-inside-profiler?! state #t)
278 ;; FIXME: with-statprof should be able to set an outer frame for the
280 (when (positive? (profile-level state))
281 (let* ((stop-time (get-internal-run-time))
282 ;; cut down to the signal handler. note that this will only
283 ;; work if statprof.scm is compiled; otherwise we get
284 ;; `eval' on the stack instead, because if it's not
285 ;; compiled, profile-signal-handler is a thunk that
286 ;; tail-calls eval. perhaps we should always compile the
287 ;; signal handler instead...
288 (stack (or (make-stack #t profile-signal-handler)
289 (pk 'what! (make-stack #t)))))
291 (sample-stack-procs state stack)
292 (accumulate-time state stop-time)
293 (set-last-start-time! state (get-internal-run-time))
295 (reset-sigprof-timer (sampling-period state))))
297 (set-inside-profiler?! state #f))
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ;; Count total calls.
302 (define (count-call frame)
303 (let ((state (existing-profiler-state)))
304 (unless (inside-profiler? state)
305 (accumulate-time state (get-internal-run-time))
307 (let* ((key (let ((proc (frame-procedure frame)))
309 ((primitive? proc) (procedure-name proc))
310 ((program? proc) (program-code proc))
312 (handle (hashv-create-handle! (call-counts state) key 0)))
313 (set-cdr! handle (1+ (cdr handle))))
315 (set-last-start-time! state (get-internal-run-time)))))
317 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319 (define (statprof-active?)
320 "Returns @code{#t} if @code{statprof-start} has been called more times
321 than @code{statprof-stop}, @code{#f} otherwise."
322 (define state (profiler-state))
323 (and state (positive? (profile-level state))))
325 ;; Do not call this from statprof internal functions -- user only.
326 (define* (statprof-start #:optional (state (ensure-profiler-state)))
327 "Start the profiler.@code{}"
328 ;; After some head-scratching, I don't *think* I need to mask/unmask
329 ;; signals here, but if I'm wrong, please let me know.
330 (set-profile-level! state (+ (profile-level state) 1))
331 (when (= (profile-level state) 1)
332 (let ((rpt (remaining-prof-time state)))
333 (set-remaining-prof-time! state 0)
334 ;; FIXME: Use per-thread run time.
335 (set-last-start-time! state (get-internal-run-time))
336 (set-gc-time-taken! state (assq-ref (gc-stats) 'gc-time-taken))
337 (let ((prev (sigaction SIGPROF profile-signal-handler)))
338 (set-prev-sigprof-handler! state (car prev)))
339 (reset-sigprof-timer (if (zero? rpt) (sampling-period state) rpt))
340 (when (call-counts state)
341 (add-hook! (vm-apply-hook) count-call))
342 (set-vm-trace-level! (1+ (vm-trace-level)))
345 ;; Do not call this from statprof internal functions -- user only.
346 (define* (statprof-stop #:optional (state (ensure-profiler-state)))
347 "Stop the profiler.@code{}"
348 ;; After some head-scratching, I don't *think* I need to mask/unmask
349 ;; signals here, but if I'm wrong, please let me know.
350 (set-profile-level! state (- (profile-level state) 1))
351 (when (zero? (profile-level state))
352 (set-gc-time-taken! state
353 (- (assq-ref (gc-stats) 'gc-time-taken)
354 (gc-time-taken state)))
355 (set-vm-trace-level! (1- (vm-trace-level)))
356 (when (call-counts state)
357 (remove-hook! (vm-apply-hook) count-call))
358 ;; I believe that we need to do this before getting the time
359 ;; (unless we want to make things even more complicated).
360 (set-remaining-prof-time! state (reset-sigprof-timer 0))
361 (accumulate-time state (get-internal-run-time))
362 (sigaction SIGPROF (prev-sigprof-handler state))
363 (set-prev-sigprof-handler! state #f)
364 (set-last-start-time! state #f)))
366 (define* (statprof-reset sample-seconds sample-microseconds count-calls?
367 #:optional full-stacks?)
368 "Reset the statprof sampler interval to @var{sample-seconds} and
369 @var{sample-microseconds}. If @var{count-calls?} is true, arrange to
370 instrument procedure calls as well as collecting statistical profiling
371 data. If @var{full-stacks?} is true, collect all sampled stacks into a
372 list for later analysis.
374 Enables traps and debugging as necessary."
375 (when (statprof-active?)
376 (error "Can't reset profiler while profiler is running."))
378 (fresh-profiler-state #:count-calls? count-calls?
379 #:sampling-period (+ (* sample-seconds #e1e6)
381 #:full-stacks? full-stacks?))
384 (define-record-type call-data
385 (make-call-data name printable call-count cum-sample-count self-sample-count)
387 (name call-data-name)
388 (printable call-data-printable)
389 (call-count call-data-call-count set-call-data-call-count!)
390 (cum-sample-count call-data-cum-sample-count set-call-data-cum-sample-count!)
391 (self-sample-count call-data-self-sample-count set-call-data-self-sample-count!))
393 (define (source->string source)
394 (format #f "~a:~a:~a"
395 (or (source-file source) "<current input>")
396 (source-line-for-user source)
397 (source-column source)))
399 (define (program-debug-info-printable pdi)
400 (let* ((addr (program-debug-info-addr pdi))
401 (name (or (and=> (program-debug-info-name pdi) symbol->string)
402 (string-append "#x" (number->string addr 16))))
403 (loc (and=> (find-source-for-addr addr) source->string)))
405 (string-append name " at " loc)
408 (define (addr->pdi addr cache)
410 ((hashv-get-handle cache addr) => cdr)
412 (let ((data (find-program-debug-info addr)))
413 (hashv-set! cache addr data)
416 (define (addr->printable addr pdi)
418 (program-debug-info-printable pdi)
419 (string-append "#x" (number->string addr 16))))
421 (define (inc-call-data-cum-sample-count! cd)
422 (set-call-data-cum-sample-count! cd (1+ (call-data-cum-sample-count cd))))
423 (define (inc-call-data-self-sample-count! cd)
424 (set-call-data-self-sample-count! cd (1+ (call-data-self-sample-count cd))))
426 (define (stack-samples->procedure-data state)
427 (let ((table (make-hash-table))
428 (addr-cache (make-hash-table))
429 (call-counts (call-counts state))
430 (buffer (buffer state))
431 (len (buffer-pos state)))
432 (define (addr->call-data addr)
433 (let* ((pdi (addr->pdi addr addr-cache))
434 (entry (if pdi (program-debug-info-addr pdi) addr)))
435 (or (hashv-ref table entry)
436 (let ((data (make-call-data (and=> pdi program-debug-info-name)
437 (addr->printable entry pdi)
439 (hashv-ref call-counts entry))
442 (hashv-set! table entry data)
445 (define (callee->call-data callee)
447 ((number? callee) (addr->call-data callee))
448 ((hashv-ref table callee))
450 (let ((data (make-call-data
451 (cond ((procedure? callee) (procedure-name callee))
453 ((symbol? callee) callee)
455 (with-output-to-string (lambda () (write callee)))
456 (and call-counts (hashv-ref call-counts callee))
459 (hashv-set! table callee data)
463 (hash-for-each (lambda (callee count)
464 (callee->call-data callee))
467 (let visit-stacks ((pos 0))
470 ;; FIXME: if we are counting all procedure calls, and
471 ;; count-call is on the stack, we need to not count the part
472 ;; of the stack that is within count-call.
473 (inc-call-data-self-sample-count!
474 (callee->call-data (vector-ref buffer pos)))
475 (let visit-stack ((pos pos))
476 (let ((callee (vector-ref buffer pos)))
478 ((vector-ref buffer pos)
480 (inc-call-data-cum-sample-count! (callee->call-data callee))
481 (visit-stack (1+ pos))))
483 (visit-stacks (1+ pos)))))))
486 (define (statprof-fold-call-data proc init)
487 "Fold @var{proc} over the call-data accumulated by statprof. Cannot be
488 called while statprof is active. @var{proc} should take two arguments,
489 @code{(@var{call-data} @var{prior-result})}.
491 Note that a given proc-name may appear multiple times, but if it does,
492 it represents different functions with the same name."
493 (when (statprof-active?)
494 (error "Can't call statprof-fold-call-data while profiler is running."))
496 (lambda (key value prior-result)
497 (proc value prior-result))
499 (stack-samples->procedure-data (existing-profiler-state))))
501 (define (statprof-proc-call-data proc)
502 "Returns the call-data associated with @var{proc}, or @code{#f} if
504 (when (statprof-active?)
505 (error "Can't call statprof-proc-call-data while profiler is running."))
506 (hashv-ref (stack-samples->procedure-data (existing-profiler-state))
508 ((primitive? proc) (procedure-name proc))
509 ((program? proc) (program-code proc))
510 (else (program-code proc)))))
512 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
515 (define (statprof-call-data->stats call-data)
516 "Returns an object of type @code{statprof-stats}."
517 ;; returns (vector proc-name
519 ;; cum-seconds-in-proc
520 ;; self-seconds-in-proc
522 ;; self-secs-per-call
523 ;; total-secs-per-call)
525 (define state (existing-profiler-state))
527 (let* ((proc-name (call-data-printable call-data))
528 (self-samples (call-data-self-sample-count call-data))
529 (cum-samples (call-data-cum-sample-count call-data))
530 (all-samples (statprof-sample-count))
531 (secs-per-sample (/ (statprof-accumulated-time)
532 (statprof-sample-count)))
533 (num-calls (and (call-counts state)
534 (statprof-call-data-calls call-data))))
537 (* (/ self-samples all-samples) 100.0)
538 (* cum-samples secs-per-sample 1.0)
539 (* self-samples secs-per-sample 1.0)
541 (and num-calls ;; maybe we only sampled in children
542 (if (zero? self-samples) 0.0
543 (/ (* self-samples secs-per-sample) 1.0 num-calls)))
544 (and num-calls ;; cum-samples must be positive
545 (/ (* cum-samples secs-per-sample)
547 ;; num-calls might be 0 if we entered statprof during the
548 ;; dynamic extent of the call
549 (max num-calls 1))))))
551 (define (statprof-stats-proc-name stats) (vector-ref stats 0))
552 (define (statprof-stats-%-time-in-proc stats) (vector-ref stats 1))
553 (define (statprof-stats-cum-secs-in-proc stats) (vector-ref stats 2))
554 (define (statprof-stats-self-secs-in-proc stats) (vector-ref stats 3))
555 (define (statprof-stats-calls stats) (vector-ref stats 4))
556 (define (statprof-stats-self-secs-per-call stats) (vector-ref stats 5))
557 (define (statprof-stats-cum-secs-per-call stats) (vector-ref stats 6))
559 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
561 (define (stats-sorter x y)
562 (let ((diff (- (statprof-stats-self-secs-in-proc x)
563 (statprof-stats-self-secs-in-proc y))))
566 (- (statprof-stats-cum-secs-in-proc x)
567 (statprof-stats-cum-secs-in-proc y))
570 (define* (statprof-display #:optional (port (current-output-port))
571 (state (existing-profiler-state)))
572 "Displays a gprof-like summary of the statistics collected. Unless an
573 optional @var{port} argument is passed, uses the current output port."
575 ((zero? (statprof-sample-count))
576 (format port "No samples recorded.\n"))
578 (let* ((stats-list (statprof-fold-call-data
579 (lambda (data prior-value)
580 (cons (statprof-call-data->stats data)
583 (sorted-stats (sort stats-list stats-sorter)))
585 (define (display-stats-line stats)
586 (format port "~6,2f ~9,2f ~9,2f"
587 (statprof-stats-%-time-in-proc stats)
588 (statprof-stats-cum-secs-in-proc stats)
589 (statprof-stats-self-secs-in-proc stats))
590 (if (call-counts state)
591 (if (statprof-stats-calls stats)
592 (format port " ~7d ~8,2f ~8,2f "
593 (statprof-stats-calls stats)
594 (* 1000 (statprof-stats-self-secs-per-call stats))
595 (* 1000 (statprof-stats-cum-secs-per-call stats)))
598 (display (statprof-stats-proc-name stats) port)
601 (if (call-counts state)
603 (format port "~5a ~10a ~7a ~8a ~8a ~8a ~8@a\n"
604 "% " "cumulative" "self" "" "self" "total" "")
605 (format port "~5a ~9a ~8a ~8a ~8a ~8a ~8@a\n"
606 "time" "seconds" "seconds" "calls" "ms/call" "ms/call" "name"))
608 (format port "~5a ~10a ~7a ~8@a\n"
609 "%" "cumulative" "self" "")
610 (format port "~5a ~10a ~7a ~8@a\n"
611 "time" "seconds" "seconds" "name")))
613 (for-each display-stats-line sorted-stats)
615 (display "---\n" port)
616 (simple-format #t "Sample count: ~A\n" (statprof-sample-count))
617 (simple-format #t "Total time: ~A seconds (~A seconds in GC)\n"
618 (statprof-accumulated-time)
619 (/ (gc-time-taken state)
620 1.0 internal-time-units-per-second))))))
622 (define* (statprof-display-anomalies #:optional (state
623 (existing-profiler-state)))
624 "A sanity check that attempts to detect anomalies in statprof's
626 (statprof-fold-call-data
627 (lambda (data prior-value)
628 (when (and (call-counts state)
629 (zero? (call-data-call-count data))
630 (positive? (call-data-cum-sample-count data)))
633 (call-data-name data)
634 (call-data-call-count data)
635 (call-data-cum-sample-count data))))
637 (simple-format #t "Total time: ~A\n" (statprof-accumulated-time))
638 (simple-format #t "Sample count: ~A\n" (statprof-sample-count)))
640 (define (statprof-display-anomolies)
641 (issue-deprecation-warning "statprof-display-anomolies is a misspelling. "
642 "Use statprof-display-anomalies instead.")
643 (statprof-display-anomalies))
645 (define* (statprof-accumulated-time #:optional (state
646 (existing-profiler-state)))
647 "Returns the time accumulated during the last statprof run.@code{}"
648 (/ (accumulated-time state) 1.0 internal-time-units-per-second))
650 (define* (statprof-sample-count #:optional (state (existing-profiler-state)))
651 "Returns the number of samples taken during the last statprof run.@code{}"
652 (sample-count state))
654 (define statprof-call-data-name call-data-name)
655 (define statprof-call-data-calls call-data-call-count)
656 (define statprof-call-data-cum-samples call-data-cum-sample-count)
657 (define statprof-call-data-self-samples call-data-self-sample-count)
659 (define* (statprof-fetch-stacks #:optional (state (existing-profiler-state)))
660 "Returns a list of stacks, as they were captured since the last call
661 to @code{statprof-reset}.
663 Note that stacks are only collected if the @var{full-stacks?} argument
664 to @code{statprof-reset} is true."
671 ((and (program? a) (program? b))
672 (eq? (program-code a) (program-code b)))
676 ;; tree ::= (car n . tree*)
678 (define (lists->trees lists equal?)
679 (let lp ((in lists) (n-terminal 0) (tails '()))
682 (let ((trees (map (lambda (tail)
684 (lists->trees (cdr tail) equal?)))
686 (cons (apply + n-terminal (map cadr trees))
688 (lambda (a b) (> (cadr a) (cadr b)))))))
690 (lp (cdr in) (1+ n-terminal) tails))
691 ((find (lambda (x) (equal? (car x) (caar in)))
698 (cons (cdar in) (cdr tail))))))
702 (acons (caar in) (list (cdar in)) tails))))))
704 (define (stack->procedures stack)
706 (unfold-right (lambda (x) (not x))
709 (stack-ref stack 0))))
711 (define* (statprof-fetch-call-tree #:optional (state (existing-profiler-state)))
712 "Return a call tree for the previous statprof run.
714 The return value is a list of nodes, each of which is of the type:
716 node ::= (@var{proc} @var{count} . @var{nodes})
718 (cons #t (lists->trees (map stack->procedures (stacks state)) procedure=?)))
720 (define* (statprof thunk #:key (loop 1) (hz 100) (count-calls? #f)
721 (full-stacks? #f) (port (current-output-port)))
722 "Profiles the execution of @var{thunk}.
724 The stack will be sampled @var{hz} times per second, and the thunk itself will
725 be called @var{loop} times.
727 If @var{count-calls?} is true, all procedure calls will be recorded. This
728 operation is somewhat expensive.
730 If @var{full-stacks?} is true, at each sample, statprof will store away the
731 whole call tree, for later analysis. Use @code{statprof-fetch-stacks} or
732 @code{statprof-fetch-call-tree} to retrieve the last-stored stacks."
734 (let ((state (fresh-profiler-state #:count-calls? count-calls?
736 (inexact->exact (round (/ 1e6 hz)))
737 #:full-stacks? full-stacks?)))
738 (parameterize ((profiler-state state))
741 (statprof-start state))
748 (statprof-stop state)
749 (statprof-display port state))))))
751 (define-macro (with-statprof . args)
752 "Profiles the expressions in its body.
758 Execute the body @var{loop} number of times, or @code{#f} for no looping
766 Whether to instrument each function call (expensive)
770 Whether to collect away all sampled stacks into a list
774 (define (kw-arg-ref kw args def)
776 ((null? args) (error "Invalid macro body"))
777 ((keyword? (car args))
778 (if (eq? (car args) kw)
780 (kw-arg-ref kw (cddr args) def)))
781 ((eq? kw #f def) ;; asking for the body
783 (else def))) ;; kw not found
784 `((@ (statprof) statprof)
785 (lambda () ,@(kw-arg-ref #f args #f))
786 #:loop ,(kw-arg-ref #:loop args 1)
787 #:hz ,(kw-arg-ref #:hz args 100)
788 #:count-calls? ,(kw-arg-ref #:count-calls? args #f)
789 #:full-stacks? ,(kw-arg-ref #:full-stacks? args #f)))
791 (define* (gcprof thunk #:key (loop 1) (full-stacks? #f))
792 "Do an allocation profile of the execution of @var{thunk}.
794 The stack will be sampled soon after every garbage collection, yielding
795 an approximate idea of what is causing allocation in your program.
797 Since GC does not occur very frequently, you may need to use the
798 @var{loop} parameter, to cause @var{thunk} to be called @var{loop}
801 If @var{full-stacks?} is true, at each sample, statprof will store away the
802 whole call tree, for later analysis. Use @code{statprof-fetch-stacks} or
803 @code{statprof-fetch-call-tree} to retrieve the last-stored stacks."
805 (let ((state (fresh-profiler-state #:full-stacks? full-stacks?)))
806 (parameterize ((profiler-state state))
807 (define (gc-callback)
808 (unless (inside-profiler? state)
809 (set-inside-profiler?! state #t)
811 ;; FIXME: should be able to set an outer frame for the stack cut
812 (let ((stop-time (get-internal-run-time))
813 ;; Cut down to gc-callback, and then one before (the
814 ;; after-gc async). See the note in profile-signal-handler
816 (stack (or (make-stack #t gc-callback 0 1)
817 (pk 'what! (make-stack #t)))))
818 (sample-stack-procs state stack)
819 (accumulate-time state stop-time)
820 (set-last-start-time! state (get-internal-run-time)))
822 (set-inside-profiler?! state #f)))
826 (set-profile-level! state 1)
827 (set-last-start-time! state (get-internal-run-time))
828 (set-gc-time-taken! state (assq-ref (gc-stats) 'gc-time-taken))
829 (add-hook! after-gc-hook gc-callback))
836 (remove-hook! after-gc-hook gc-callback)
837 (set-gc-time-taken! state
838 (- (assq-ref (gc-stats) 'gc-time-taken)
839 (gc-time-taken state)))
840 (accumulate-time state (get-internal-run-time))
841 (set-profile-level! state 0)
842 (statprof-display))))))