Skip to content

Commit 1643ee0

Browse files
committed
Implements memory profiling
1 parent 3fdd9c9 commit 1643ee0

File tree

7 files changed

+44
-24
lines changed

7 files changed

+44
-24
lines changed

eval/compile.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
(make-vector (*rival-profile-executions*))
334334
(make-flvector (*rival-profile-executions*))
335335
(make-vector (*rival-profile-executions*))
336+
(make-vector (*rival-profile-executions*))
336337
(make-vector (*rival-profile-executions*))))
337338

338339
;; Defining instructions that do not depend on input arguments

eval/machine.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
profile-instruction
4242
profile-number
4343
profile-time
44+
profile-memory
4445
profile-precision
4546
profile-iteration))
4647

eval/main.rkt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
(struct exn:rival:invalid exn:rival (pt))
4141
(struct exn:rival:unsamplable exn:rival (pt))
4242

43-
(struct execution (name number precision time iteration) #:prefab)
43+
(struct execution (name number precision time memory iteration) #:prefab)
4444

4545
(define (rival-profile machine param)
4646
(match param
@@ -52,15 +52,17 @@
5252
(define profile-instruction (rival-machine-profile-instruction machine))
5353
(define profile-number (rival-machine-profile-number machine))
5454
(define profile-time (rival-machine-profile-time machine))
55+
(define profile-memory (rival-machine-profile-memory machine))
5556
(define profile-precision (rival-machine-profile-precision machine))
5657
(define profile-iteration (rival-machine-profile-iteration machine))
5758
(begin0 (for/vector #:length profile-ptr
5859
([instruction (in-vector profile-instruction 0 profile-ptr)]
5960
[number (in-vector profile-number 0 profile-ptr)]
6061
[precision (in-vector profile-precision 0 profile-ptr)]
6162
[time (in-flvector profile-time 0 profile-ptr)]
63+
[memory (in-vector profile-memory 0 profile-ptr)]
6264
[iter (in-vector profile-iteration 0 profile-ptr)])
63-
(execution instruction number precision time iter))
65+
(execution instruction number precision time memory iter))
6466
(set-rival-machine-profile-ptr! machine 0))]))
6567

6668
(define (ival-real x)

eval/run.rkt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
(set-rival-machine-bumps! machine 0)
2121
(*bumps-activated* #f))
2222

23-
(define (rival-machine-record machine name number precision time iter)
23+
(define (rival-machine-record machine name number precision time memory iter)
2424
(define profile-ptr (rival-machine-profile-ptr machine))
2525
(define profile-instruction (rival-machine-profile-instruction machine))
2626
(when (< profile-ptr (vector-length profile-instruction))
2727
(define profile-number (rival-machine-profile-number machine))
2828
(define profile-time (rival-machine-profile-time machine))
29+
(define profile-memory (rival-machine-profile-memory machine))
2930
(define profile-precision (rival-machine-profile-precision machine))
3031
(define profile-iteration (rival-machine-profile-iteration machine))
3132
(vector-set! profile-instruction profile-ptr name)
3233
(vector-set! profile-number profile-ptr number)
34+
(vector-set! profile-memory profile-ptr memory)
3335
(vector-set! profile-precision profile-ptr precision)
3436
(vector-set! profile-iteration profile-ptr iter)
3537
(flvector-set! profile-time profile-ptr time)
@@ -56,13 +58,15 @@
5658
(define out
5759
(match hint
5860
[#t ; instruction should be reevaluated
59-
(define start (current-inexact-milliseconds))
61+
(define start-time (current-inexact-milliseconds))
62+
(define start-memory (current-memory-use #f))
6063
(define res
6164
(parameterize ([bf-precision precision])
6265
(apply-instruction instr vregs)))
6366
(define name (object-name (car instr)))
64-
(define time (- (current-inexact-milliseconds) start))
65-
(rival-machine-record machine name n precision time iter)
67+
(define time (- (current-inexact-milliseconds) start-time))
68+
(define memory (max 0 (- (current-memory-use #f) start-memory)))
69+
(rival-machine-record machine name n precision time memory iter)
6670
res]
6771
[(? integer? _) (vector-ref vregs (list-ref instr hint))] ; result is known
6872
[(? ival? _) hint])) ; result is known
@@ -112,12 +116,14 @@
112116

113117
(define (rival-machine-adjust machine vhint)
114118
(define iter (rival-machine-iteration machine))
115-
(let ([start (current-inexact-milliseconds)])
119+
(let ([start-time (current-inexact-milliseconds)]
120+
[start-memory (current-memory-use #f)])
116121
(unless (zero? iter)
117122
(backward-pass machine vhint))
118123
(rival-machine-record machine
119124
'adjust
120125
-1
121126
(* iter 1000)
122-
(- (current-inexact-milliseconds) start)
127+
(- (current-inexact-milliseconds) start-time)
128+
(max 0 (- (current-memory-use #f) start-memory))
123129
iter)))

infra/run-baseline.rkt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
profile-instruction
3333
profile-number
3434
profile-time
35+
profile-memory
3536
profile-precision
3637
profile-iteration))
3738

@@ -191,6 +192,7 @@
191192
(make-vector (*rival-profile-executions*))
192193
(make-flvector (*rival-profile-executions*))
193194
(make-vector (*rival-profile-executions*))
195+
(make-vector (*rival-profile-executions*))
194196
(make-vector (*rival-profile-executions*))))
195197

196198
; ------------------------------------------- APPLY --------------------------------------------------
@@ -226,7 +228,8 @@
226228
(list (ival (or bad? stuck?) (not good?)) hint* hint*-converged?))
227229

228230
(define (baseline-machine-adjust machine)
229-
(let ([start (current-inexact-milliseconds)])
231+
(let ([start-time (current-inexact-milliseconds)]
232+
[start-memory (current-memory-use #f)])
230233
(define new-prec (bf-precision))
231234
(set-baseline-machine-precision! machine new-prec)
232235
(vector-fill! (baseline-machine-precisions machine) new-prec)
@@ -285,7 +288,8 @@
285288
'adjust
286289
-1
287290
(* iter 1000)
288-
(- (current-inexact-milliseconds) start)
291+
(- (current-inexact-milliseconds) start-time)
292+
(max 0 (- (current-memory-use #f) start-memory))
289293
iter)))
290294

291295
(define (drop-self-pointers tail-regs n)
@@ -318,13 +322,15 @@
318322
(define out
319323
(match hint
320324
[#t
321-
(define start (current-inexact-milliseconds))
325+
(define start-time (current-inexact-milliseconds))
326+
(define start-memory (current-memory-use #f))
322327
(define res
323328
(parameterize ([bf-precision precision])
324329
(apply-instruction instr vregs)))
325330
(define name (object-name (car instr)))
326-
(define time (- (current-inexact-milliseconds) start))
327-
(baseline-machine-record machine name n precision time iter)
331+
(define time (- (current-inexact-milliseconds) start-time))
332+
(define memory (max 0 (- (current-memory-use #f) start-memory)))
333+
(baseline-machine-record machine name n precision time memory iter)
328334
res]
329335
[(? integer? _) (vector-ref vregs (list-ref instr hint))]
330336
[(? ival? _) hint]))
@@ -382,27 +388,31 @@
382388
(define profile-instruction (baseline-machine-profile-instruction machine))
383389
(define profile-number (baseline-machine-profile-number machine))
384390
(define profile-time (baseline-machine-profile-time machine))
391+
(define profile-memory (baseline-machine-profile-memory machine))
385392
(define profile-precision (baseline-machine-profile-precision machine))
386393
(define profile-iteration (baseline-machine-profile-iteration machine))
387394
(begin0 (for/vector #:length profile-ptr
388395
([instruction (in-vector profile-instruction 0 profile-ptr)]
389396
[number (in-vector profile-number 0 profile-ptr)]
390397
[precision (in-vector profile-precision 0 profile-ptr)]
391398
[time (in-flvector profile-time 0 profile-ptr)]
399+
[memory (in-vector profile-memory 0 profile-ptr)]
392400
[iter (in-vector profile-iteration 0 profile-ptr)])
393-
(execution instruction number precision time iter))
401+
(execution instruction number precision time memory iter))
394402
(set-baseline-machine-profile-ptr! machine 0))]))
395403

396-
(define (baseline-machine-record machine name number precision time iter)
404+
(define (baseline-machine-record machine name number precision time memory iter)
397405
(define profile-ptr (baseline-machine-profile-ptr machine))
398406
(define profile-instruction (baseline-machine-profile-instruction machine))
399407
(when (< profile-ptr (vector-length profile-instruction))
400408
(define profile-number (baseline-machine-profile-number machine))
401409
(define profile-time (baseline-machine-profile-time machine))
410+
(define profile-memory (baseline-machine-profile-memory machine))
402411
(define profile-precision (baseline-machine-profile-precision machine))
403412
(define profile-iteration (baseline-machine-profile-iteration machine))
404413
(vector-set! profile-instruction profile-ptr name)
405414
(vector-set! profile-number profile-ptr number)
415+
(vector-set! profile-memory profile-ptr memory)
406416
(vector-set! profile-precision profile-ptr precision)
407417
(vector-set! profile-iteration profile-ptr iter)
408418
(flvector-set! profile-time profile-ptr time)

repl.rkt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
(define iter 0)
2929
(define last #f)
3030
(for/list ([exec (in-vector execs)])
31-
(match-define (execution name id precision time _) exec)
31+
(match-define (execution name id precision time _ _) exec)
3232
(when (and last (< id last))
3333
(set! iter (+ iter 1)))
3434
(set! last id)
@@ -106,7 +106,7 @@
106106
#:when (and (= (modulo col 2) 0) (> col 2))
107107
(define iter (- (/ col 2) 1))
108108
(list-find-match execs*
109-
(cons (== iter) (execution 'adjust _ _ time _))
109+
(cons (== iter) (execution 'adjust _ _ time _ _))
110110
(~r (* time 1000) #:precision '(= 1)))]
111111
[(2 col) ""]
112112
[((== (+ 3 num-instructions)) _) "------"]
@@ -126,19 +126,19 @@
126126
[(row 0)
127127
(define id (+ (- row 3) num-args))
128128
(list-find-match execs*
129-
(cons _ (execution name (== id) _ _ _))
129+
(cons _ (execution name (== id) _ _ _ _))
130130
(normalize-function-name (~a name)))]
131131
[(row col)
132132
#:when (= (modulo col 2) 1) ; precision
133133
(define id (+ (- row 3) num-args))
134134
(define iter (/ (- col 1) 2))
135-
(list-find-match execs* (cons (== iter) (execution _ (== id) prec _ _)) prec)]
135+
(list-find-match execs* (cons (== iter) (execution _ (== id) prec _ _ _)) prec)]
136136
[(row col)
137137
#:when (= (modulo col 2) 0) ; time
138138
(define id (+ (- row 3) num-args))
139139
(define iter (/ (- col 2) 2))
140140
(list-find-match execs*
141-
(cons (== iter) (execution _ (== id) _ time _))
141+
(cons (== iter) (execution _ (== id) _ time _ _))
142142
(~r (* time 1000) #:precision '(= 1)))]))))
143143

144144
(define (rival-repl p)

time.rkt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
(define h (make-hash))
152152
(define max-prec 0)
153153
(for ([exec (in-vector rival-executions)])
154-
(match-define (execution name number precision time _) exec)
154+
(match-define (execution name number precision time _ _) exec)
155155
(unless (equal? name 'adjust)
156156
(define precision* (hash-ref h (list name number) (λ () 0)))
157157
(hash-set! h (list name number) (max precision precision*))
@@ -397,9 +397,9 @@
397397
(define total-t (+ total-c total-v total-i total-u))
398398
(define total-mem (/ (exact->inexact total-mem-bytes) (* 1024 1024)))
399399
(printf "\nTotal Time: ~as\n" (~r total-t #:precision '(= 3)))
400-
(printf "Total Memory: ~a MiB\n"
401-
(~r total-mem #:precision '(= 3)))
402-
(define footer (list "Total" total-t total-c count-v total-v count-i total-i count-u total-u total-mem))
400+
(printf "Total Memory: ~a MiB\n" (~r total-mem #:precision '(= 3)))
401+
(define footer
402+
(list "Total" total-t total-c count-v total-v count-i total-i count-u total-u total-mem))
403403
(values table footer))
404404

405405
(define (html-write port)

0 commit comments

Comments
 (0)