3 (define-module (scm memory-trace))
7 (define-public (mtrace:start-trace freq)
8 (set! usecond-interval (inexact->exact (/ 1000000 freq)))
9 (call-with-new-thread start-install-tracepoint))
11 (define-public (mtrace:stop-trace)
12 (set! continue-tracing #f))
14 (define-public mtrace:trace-depth 12)
16 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18 (define trace-points '())
19 (define continue-tracing #t)
20 (define busy-tracing #f)
21 (define trace-thread #f)
23 (define start-memory 0)
25 (define trace-count 0)
26 (define usecond-interval 100000)
27 (define (arg-procedure args)
33 (define (record-stack key continuation . args)
34 (if (eq? (current-thread) trace-thread)
37 ((cells (assoc-get 'total-cells-allocated (gc-stats)))
38 (proc (arg-procedure args))
39 (time (tms:utime (times)))
40 (stack (extract-trace continuation)))
42 (set! busy-tracing #t)
44 (trap-disable 'enter-frame)
46 (set! trace-count (1+ trace-count))
47 (ly:progress "<~a: ~a/~a>\n"
52 (set! last-count cells)
63 (set! busy-tracing #f))))
65 (define (start-install-tracepoint)
66 (set! trace-thread (current-thread))
67 (set! trace-points '())
68 (set! continue-tracing #t)
70 (set! start-memory (assoc-get 'total-cells-allocated (gc-stats)))
71 (set! start-time (tms:utime (times)))
75 (define (install-tracepoint)
77 (display "last trace not finished yet\n" (current-error-port))
79 (trap-set! enter-frame-handler record-stack)
80 (trap-enable 'enter-frame)
81 (trap-enable 'traps)))
83 (usleep usecond-interval)
85 (install-tracepoint)))
87 (define-public (mtrace:dump-results base)
89 ((stacks-name (format #f "~a.stacks" base))
90 (graph-name (format #f "~a.graph" base))
91 (graph-out (open-output-file graph-name))
92 (stacks-out (open-output-file stacks-name))
97 (ly:progress "Memory statistics to ~a and ~a..."
98 stacks-name graph-name)
99 (format graph-out "# memory trace with ~a points\n" (length trace-points))
103 ((mem (- (assoc-get 'cells r) start-memory))
104 (proc (assoc-get 'proc r))
105 (stack (assoc-get 'stack r))
106 (time (- (assoc-get 'time r) start-time)))
108 (format graph-out "~a ~a\n" time mem)
111 (format stacks-out "~5a t = ~5a - delta-mem: ~15a - ~a\n" i
113 (- mem last-mem) proc)
116 (stack (assoc-get 'stack r) stack))
117 ((>= j (vector-length stack)))
119 (format stacks-out "\t~a\n"
120 (vector-ref stack j)))))
125 (reverse trace-points))))
128 (define (test-graph . rest)
129 (mtrace:start-trace 100)
132 (mtrace:dump-results "test"))
136 (define (extract-trace continuation)
138 ((stack (make-stack continuation))
139 (depth (min (stack-length stack) mtrace:trace-depth))
140 (trace (make-vector depth #f)))
149 ((source (frame-source (stack-ref stack i))))
152 (cons (source-property source 'filename)
153 (source-property source 'line))))))