Debian Codegraph @

benchmarks/rnrs-benchmarks/gcold.ss

raw
1 ;
2 ; GCOld.sch x.x 00/08/03
3 ; translated from GCOld.java 2.0a 00/08/23
4 ;
5 ; Copyright 2000 Sun Microsystems, Inc. All rights reserved.
6 ;
7 ;
8
9 ; Should be good enough for this benchmark.
10
11 (library (rnrs-benchmarks gcold)
12 (export main)
13 (import (rnrs) (rnrs r5rs) (rnrs-benchmarks))
14
15 (define (newRandom)
16 (letrec ((random14
17 (lambda (n)
18 (set! x (remainder (+ (* a x) c) m))
19 (remainder (quotient x 8) n)))
20 (a 701)
21 (x 1)
22 (c 743483)
23 (m 524288)
24 (loop
25 (lambda (q r n)
26 (if (zero? q)
27 (remainder r n)
28 (loop (quotient q 16384)
29 (+ (* 16384 r) (random14 16384))
30 n)))))
31 (lambda (n)
32 (if (and (exact? n) (integer? n) (< n 16384))
33 (random14 n)
34 (loop n (random14 16384) n)))))
35
36 ; A TreeNode is a record with three fields: left, right, val.
37 ; The left and right fields contain a TreeNode or 0, and the
38 ; val field will contain the integer height of the tree.
39
40 (define-syntax newTreeNode
41 (syntax-rules ()
42 ((newTreeNode left right val)
43 (vector left right val))
44 ((newTreeNode)
45 (vector 0 0 0))))
46
47 (define-syntax TreeNode.left
48 (syntax-rules ()
49 ((TreeNode.left node)
50 (vector-ref node 0))))
51
52 (define-syntax TreeNode.right
53 (syntax-rules ()
54 ((TreeNode.right node)
55 (vector-ref node 1))))
56
57 (define-syntax TreeNode.val
58 (syntax-rules ()
59 ((TreeNode.val node)
60 (vector-ref node 2))))
61
62 (define-syntax setf
63 (syntax-rules (TreeNode.left TreeNode.right TreeNode.val)
64 ((setf (TreeNode.left node) x)
65 (vector-set! node 0 x))
66 ((setf (TreeNode.right node) x)
67 (vector-set! node 1 x))
68 ((setf (TreeNode.val node) x)
69 (vector-set! node 2 x))))
70
71 ; Args:
72 ; live-data-size: in megabytes.
73 ; work: units of mutator non-allocation work per byte allocated,
74 ; (in unspecified units. This will affect the promotion rate
75 ; printed at the end of the run: more mutator work per step implies
76 ; fewer steps per second implies fewer bytes promoted per second.)
77 ; short/long ratio: ratio of short-lived bytes allocated to long-lived
78 ; bytes allocated.
79 ; pointer mutation rate: number of pointer mutations per step.
80 ; steps: number of steps to do.
81 ;
82
83 (define (GCOld size workUnits promoteRate ptrMutRate steps)
84
85 (define (println . args)
86 (for-each display args)
87 (newline))
88
89 ; Rounds an inexact real to two decimal places.
90
91 (define (round2 x)
92 (/ (round (* 100.0 x)) 100.0))
93
94 ; Returns the height of the given tree.
95
96 (define (height t)
97 (if (eqv? t 0)
98 0
99 (+ 1 (max (height (TreeNode.left t))
100 (height (TreeNode.right t))))))
101
102 ; Returns the length of the shortest path in the given tree.
103
104 (define (shortestPath t)
105 (if (eqv? t 0)
106 0
107 (+ 1 (min (shortestPath (TreeNode.left t))
108 (shortestPath (TreeNode.right t))))))
109
110 ; Returns the number of nodes in a balanced tree of the given height.
111
112 (define (heightToNodes h)
113 (- (expt 2 h) 1))
114
115 ; Returns the height of the largest balanced tree
116 ; that has no more than the given number of nodes.
117
118 (define (nodesToHeight nodes)
119 (do ((h 1 (+ h 1))
120 (n 1 (+ n n)))
121 ((> (+ n n -1) nodes)
122 (- h 1))))
123
124 (let* (
125
126 ; Constants.
127
128 (null 0) ; Java's null
129 (pathBits 65536) ; to generate 16 random bits
130
131 (MEG 1000000)
132 (INSIGNIFICANT 999) ; this many bytes don't matter
133 (bytes/word 4)
134 (bytes/node 20) ; bytes per tree node in typical JVM
135 (words/dead 100) ; size of young garbage objects
136
137 ; Returns the number of bytes in a balanced tree of the given height.
138
139 (heightToBytes
140 (lambda (h)
141 (* bytes/node (heightToNodes h))))
142
143 ; Returns the height of the largest balanced tree
144 ; that occupies no more than the given number of bytes.
145
146 (bytesToHeight
147 (lambda (bytes)
148 (nodesToHeight (/ bytes bytes/node))))
149
150 (treeHeight 14)
151 (treeSize (heightToBytes treeHeight))
152
153 (msg1 "Usage: java GCOld <size> <work> <ratio> <mutation> <steps>")
154 (msg2 " where <size> is the live storage in megabytes")
155 (msg3 " <work> is the mutator work per step (arbitrary units)")
156 (msg4 " <ratio> is the ratio of short-lived to long-lived allocation")
157 (msg5 " <mutation> is the mutations per step")
158 (msg6 " <steps> is the number of steps")
159
160 ; Counters (and global variables that discourage optimization).
161
162 (youngBytes 0)
163 (nodes 0)
164 (actuallyMut 0)
165 (mutatorSum 0)
166 (aexport '#())
167
168 ; Global variables.
169
170 (trees '#())
171 (where 0)
172 (rnd (newRandom))
173
174 )
175
176 ; Returns a newly allocated balanced binary tree of height h.
177
178 (define (makeTree h)
179 (if (zero? h)
180 null
181 (let ((res (newTreeNode)))
182 (set! nodes (+ nodes 1))
183 (setf (TreeNode.left res) (makeTree (- h 1)))
184 (setf (TreeNode.right res) (makeTree (- h 1)))
185 (setf (TreeNode.val res) h)
186 res)))
187
188 ; Allocates approximately size megabytes of trees and stores
189 ; them into a global array.
190
191 (define (init)
192 ; Each tree will be about a megabyte.
193 (let ((ntrees (quotient (* size MEG) treeSize)))
194 (set! trees (make-vector ntrees null))
195 (println "Allocating " ntrees " trees.")
196 (println " (" (* ntrees treeSize) " bytes)")
197 (do ((i 0 (+ i 1)))
198 ((>= i ntrees))
199 (vector-set! trees i (makeTree treeHeight))
200 (doYoungGenAlloc (* promoteRate ntrees treeSize) words/dead))
201 (println " (" nodes " nodes)")))
202
203 ; Confirms that all trees are balanced and have the correct height.
204
205 (define (checkTrees)
206 (let ((ntrees (vector-length trees)))
207 (do ((i 0 (+ i 1)))
208 ((>= i ntrees))
209 (let* ((t (vector-ref trees i))
210 (h1 (height t))
211 (h2 (shortestPath t)))
212 (if (or (not (= h1 treeHeight))
213 (not (= h2 treeHeight)))
214 (println "*****BUG: " h1 " " h2))))))
215
216 ; Called only by replaceTree (below) and by itself.
217
218 (define (replaceTreeWork full partial dir)
219 (let ((canGoLeft (and (not (eq? (TreeNode.left full) null))
220 (> (TreeNode.val (TreeNode.left full))
221 (TreeNode.val partial))))
222 (canGoRight (and (not (eq? (TreeNode.right full) null))
223 (> (TreeNode.val (TreeNode.right full))
224 (TreeNode.val partial)))))
225 (cond ((and canGoLeft canGoRight)
226 (if dir
227 (replaceTreeWork (TreeNode.left full)
228 partial
229 (not dir))
230 (replaceTreeWork (TreeNode.right full)
231 partial
232 (not dir))))
233 ((and (not canGoLeft) (not canGoRight))
234 (if dir
235 (setf (TreeNode.left full) partial)
236 (setf (TreeNode.right full) partial)))
237 ((not canGoLeft)
238 (setf (TreeNode.left full) partial))
239 (else
240 (setf (TreeNode.right full) partial)))))
241
242 ; Given a balanced tree full and a smaller balanced tree partial,
243 ; replaces an appropriate subtree of full by partial, taking care
244 ; to preserve the shape of the full tree.
245
246 (define (replaceTree full partial)
247 (let ((dir (zero? (modulo (TreeNode.val partial) 2))))
248 (set! actuallyMut (+ actuallyMut 1))
249 (replaceTreeWork full partial dir)))
250
251 ; Allocates approximately n bytes of long-lived storage,
252 ; replacing oldest existing long-lived storage.
253
254 (define (oldGenAlloc n)
255 (let ((full (quotient n treeSize))
256 (partial (modulo n treeSize)))
257 ;(println "In oldGenAlloc, doing "
258 ; full
259 ; " full trees and one partial tree of size "
260 ; partial)
261 (do ((i 0 (+ i 1)))
262 ((>= i full))
263 (vector-set! trees where (makeTree treeHeight))
264 (set! where
265 (modulo (+ where 1) (vector-length trees))))
266 (let loop ((partial partial))
267 (if (> partial INSIGNIFICANT)
268 (let* ((h (bytesToHeight partial))
269 (newTree (makeTree h)))
270 (replaceTree (vector-ref trees where) newTree)
271 (set! where
272 (modulo (+ where 1) (vector-length trees)))
273 (loop (- partial (heightToBytes h))))))))
274
275 ; Interchanges two randomly selected subtrees (of same size and depth).
276
277 (define (oldGenSwapSubtrees)
278 ; Randomly pick:
279 ; * two tree indices
280 ; * A depth
281 ; * A path to that depth.
282 (let* ((index1 (rnd (vector-length trees)))
283 (index2 (rnd (vector-length trees)))
284 (depth (rnd treeHeight))
285 (path (rnd pathBits))
286 (tn1 (vector-ref trees index1))
287 (tn2 (vector-ref trees index2)))
288 (do ((i 0 (+ i 1)))
289 ((>= i depth))
290 (if (even? path)
291 (begin (set! tn1 (TreeNode.left tn1))
292 (set! tn2 (TreeNode.left tn2)))
293 (begin (set! tn1 (TreeNode.right tn1))
294 (set! tn2 (TreeNode.right tn2))))
295 (set! path (quotient path 2)))
296 (if (even? path)
297 (let ((tmp (TreeNode.left tn1)))
298 (setf (TreeNode.left tn1) (TreeNode.left tn2))
299 (setf (TreeNode.left tn2) tmp))
300 (let ((tmp (TreeNode.right tn1)))
301 (setf (TreeNode.right tn1) (TreeNode.right tn2))
302 (setf (TreeNode.right tn2) tmp)))
303 (set! actuallyMut (+ actuallyMut 2))))
304
305 ; Update "n" old-generation pointers.
306
307 (define (oldGenMut n)
308 (do ((i 0 (+ i 1)))
309 ((>= i (quotient n 2)))
310 (oldGenSwapSubtrees)))
311
312 ; Does the amount of mutator work appropriate for n bytes of young-gen
313 ; garbage allocation.
314
315 (define (doMutWork n)
316 (let ((limit (quotient (* workUnits n) 10)))
317 (do ((k 0 (+ k 1))
318 (sum 0 (+ sum 1)))
319 ((>= k limit)
320 ; We don't want dead code elimination to eliminate this loop.
321 (set! mutatorSum (+ mutatorSum sum))))))
322
323 ; Allocate n bytes of young-gen garbage, in units of "nwords"
324 ; words.
325
326 (define (doYoungGenAlloc n nwords)
327 (let ((nbytes (* nwords bytes/word)))
328 (do ((allocated 0 (+ allocated nbytes)))
329 ((>= allocated n)
330 (set! youngBytes (+ youngBytes allocated)))
331 (set! aexport (make-vector nwords 0)))))
332
333 ; Allocate "n" bytes of young-gen data; and do the
334 ; corresponding amount of old-gen allocation and pointer
335 ; mutation.
336
337 ; oldGenAlloc may perform some mutations, so this code
338 ; takes those mutations into account.
339
340 (define (doStep n)
341 (let ((mutations actuallyMut))
342 (doYoungGenAlloc n words/dead)
343 (doMutWork n)
344 ; Now do old-gen allocation
345 (oldGenAlloc (quotient n promoteRate))
346 (oldGenMut (max 0 (- (+ mutations ptrMutRate) actuallyMut)))))
347
348 (println size " megabytes")
349 (println workUnits " work units per step.")
350 (println "promotion ratio is 1:" promoteRate)
351 (println "pointer mutation rate is " ptrMutRate)
352 (println steps " steps")
353
354 (init)
355 (checkTrees)
356 (set! youngBytes 0)
357 (set! nodes 0)
358
359 (println "Initialization complete...")
360
361 (run-benchmark "GCOld"
362 1
363 (lambda (result) #t)
364 (lambda ()
365 (lambda ()
366 (do ((step 0 (+ step 1)))
367 ((>= step steps))
368 (doStep MEG)))))
369
370 (checkTrees)
371
372 (println "Allocated " steps " Mb of young gen garbage")
373 (println " (actually allocated "
374 (round2 (/ youngBytes MEG))
375 " megabytes)")
376 (println "Promoted " (round2 (/ steps promoteRate)) " Mb")
377 (println " (actually promoted "
378 (round2 (/ (* nodes bytes/node) MEG))
379 " megabytes)")
380 (if (not (zero? ptrMutRate))
381 (println "Mutated " actuallyMut " pointers"))
382
383 ; This output serves mainly to discourage optimization.
384
385 (+ mutatorSum (vector-length aexport))))
386
387 (define (main . args)
388 (GCOld 25 0 10 10 gcold-iters)))