1 (define-module (scm memory-trace))
4 (define-public (mtrace:start-trace freq)
5 (set! usecond-interval (inexact->exact (/ 1000000 freq)))
6 (call-with-new-thread start-install-tracepoint))
8 (define-public (mtrace:stop-trace)
9 (set! continue-tracing #f))
11 (define-public mtrace:trace-depth 8)
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 (define trace-points '())
16 (define continue-tracing #t)
17 (define busy-tracing #f)
18 (define trace-thread #f)
20 (define trace-count 0)
21 (define usecond-interval 100000)
23 (define (record-stack key continuation . args)
24 (if (eq? (current-thread) trace-thread)
27 (set! busy-tracing #t)
29 (trap-disable 'enter-frame)
30 (set! trace-count (1+ trace-count))
34 (assoc 'total-cells-allocated (gc-stats))
35 (cons 'stack (extract-trace continuation)))
38 (set! busy-tracing #f))))
40 (define (start-install-tracepoint)
41 (set! trace-thread (current-thread))
44 (define (install-tracepoint)
46 (display "last trace not finished yet\n" (current-error-port))
48 (trap-set! enter-frame-handler record-stack)
49 (trap-enable 'enter-frame)
50 (trap-enable 'traps)))
52 (usleep usecond-interval)
54 (install-tracepoint)))
56 (define-public (mtrace:dump-results base)
57 (define out (open-output-file (format #f "~a.graph" base)))
58 (define stacks-out (open-output-file (format #f "~a.stacks" base)))
61 (format out "# memory trace with ~a points\n" (length trace-points))
65 (format out "~a ~a\n" i
66 (cdr (assoc 'total-cells-allocated r)))
69 (format stacks-out "~a: ~a\n"
71 (cdr (assoc 'stack r))))
74 (reverse trace-points)))
78 (define (test-graph . rest)
79 (mtrace:start-trace 100)
82 (mtrace:dump-results "test"))
86 (define (extract-trace continuation)
88 ((stack (make-stack continuation))
89 (depth (min (stack-length stack) mtrace:trace-depth))
90 (trace (make-vector depth #f)))
99 ((source (frame-source (stack-ref stack i))))
102 (cons (source-property source 'filename)
103 (source-property source 'line))))))