Debian Codegraph @

benchmarks/rnrs-benchmarks/gcbench.ss

raw
1 ; This is adapted from a benchmark written by John Ellis and Pete Kovac
2 ; of Post Communications.
3 ; It was modified by Hans Boehm of Silicon Graphics.
4 ; It was translated into Scheme by William D Clinger of Northeastern Univ;
5 ; the Scheme version uses (RUN-BENCHMARK <string> <thunk>)
6 ; Last modified 30 May 1997.
7 ;
8 ; This is no substitute for real applications. No actual application
9 ; is likely to behave in exactly this way. However, this benchmark was
10 ; designed to be more representative of real applications than other
11 ; Java GC benchmarks of which we are aware.
12 ; It attempts to model those properties of allocation requests that
13 ; are important to current GC techniques.
14 ; It is designed to be used either to obtain a single overall performance
15 ; number, or to give a more detailed estimate of how collector
16 ; performance varies with object lifetimes. It prints the time
17 ; required to allocate and collect balanced binary trees of various
18 ; sizes. Smaller trees result in shorter object lifetimes. Each cycle
19 ; allocates roughly the same amount of memory.
20 ; Two data structures are kept around during the entire process, so
21 ; that the measured performance is representative of applications
22 ; that maintain some live in-memory data. One of these is a tree
23 ; containing many pointers. The other is a large array containing
24 ; double precision floating point numbers. Both should be of comparable
25 ; size.
26 ;
27 ; The results are only really meaningful together with a specification
28 ; of how much memory was used. It is possible to trade memory for
29 ; better time performance. This benchmark should be run in a 32 MB
30 ; heap, though we don't currently know how to enforce that uniformly.
31
32 ; In the Java version, this routine prints the heap size and the amount
33 ; of free memory. There is no portable way to do this in Scheme; each
34 ; implementation needs its own version.
35
36 (library (rnrs-benchmarks gcbench)
37 (export main)
38 (import (rnrs) (rnrs r5rs) (rnrs arithmetic flonums) (rnrs-benchmarks))
39
40 (define (run-benchmark2 name thunk)
41 (display name)
42 (newline)
43 (thunk))
44
45 (define (PrintDiagnostics)
46 (display " Total memory available= ???????? bytes")
47 (display " Free memory= ???????? bytes")
48 (newline))
49
50 (define (gcbench kStretchTreeDepth)
51
52 ; Nodes used by a tree of a given size
53 (define (TreeSize i)
54 (- (expt 2 (+ i 1)) 1))
55
56 ; Number of iterations to use for a given tree depth
57 (define (NumIters i)
58 (quotient (* 2 (TreeSize kStretchTreeDepth))
59 (TreeSize i)))
60
61 ; Parameters are determined by kStretchTreeDepth.
62 ; In Boehm's version the parameters were fixed as follows:
63 ; public static final int kStretchTreeDepth = 18; // about 16Mb
64 ; public static final int kLongLivedTreeDepth = 16; // about 4Mb
65 ; public static final int kArraySize = 500000; // about 4Mb
66 ; public static final int kMinTreeDepth = 4;
67 ; public static final int kMaxTreeDepth = 16;
68 ; In Larceny the storage numbers above would be 12 Mby, 3 Mby, 6 Mby.
69
70 (let* ((kLongLivedTreeDepth (- kStretchTreeDepth 2))
71 (kArraySize (* 4 (TreeSize kLongLivedTreeDepth)))
72 (kMinTreeDepth 4)
73 (kMaxTreeDepth kLongLivedTreeDepth))
74
75 ; Elements 3 and 4 of the allocated vectors are useless.
76
77 (let* ((make-empty-node (lambda () (make-vector 4 0)))
78 (make-node
79 (lambda (l r)
80 (let ((v (make-empty-node)))
81 (vector-set! v 0 l)
82 (vector-set! v 1 r)
83 v)))
84 (node.left (lambda (node) (vector-ref node 0)))
85 (node.right (lambda (node) (vector-ref node 1)))
86 (node.left-set! (lambda (node x) (vector-set! node 0 x)))
87 (node.right-set! (lambda (node x) (vector-set! node 1 x))))
88
89 ; Build tree top down, assigning to older objects.
90 (define (Populate iDepth thisNode)
91 (if (<= iDepth 0)
92 #f
93 (let ((iDepth (- iDepth 1)))
94 (node.left-set! thisNode (make-empty-node))
95 (node.right-set! thisNode (make-empty-node))
96 (Populate iDepth (node.left thisNode))
97 (Populate iDepth (node.right thisNode)))))
98
99 ; Build tree bottom-up
100 (define (MakeTree iDepth)
101 (if (<= iDepth 0)
102 (make-empty-node)
103 (make-node (MakeTree (- iDepth 1))
104 (MakeTree (- iDepth 1)))))
105
106 (define (TimeConstruction depth)
107 (let ((iNumIters (NumIters depth)))
108 (display (string-append "Creating "
109 (number->string iNumIters)
110 " trees of depth "
111 (number->string depth)))
112 (newline)
113 (run-benchmark2
114 "GCBench: Top down construction"
115 (lambda ()
116 (do ((i 0 (+ i 1)))
117 ((>= i iNumIters))
118 (Populate depth (make-empty-node)))))
119 (run-benchmark2
120 "GCBench: Bottom up construction"
121 (lambda ()
122 (do ((i 0 (+ i 1)))
123 ((>= i iNumIters))
124 (MakeTree depth))))))
125
126 (define (main)
127 (display "Garbage Collector Test")
128 (newline)
129 (display (string-append
130 " Stretching memory with a binary tree of depth "
131 (number->string kStretchTreeDepth)))
132 (newline)
133 (PrintDiagnostics)
134 (run-benchmark2
135 "GCBench: Main"
136 (lambda ()
137 ; Stretch the memory space quickly
138 (MakeTree kStretchTreeDepth)
139
140 ; Create a long lived object
141 (display (string-append
142 " Creating a long-lived binary tree of depth "
143 (number->string kLongLivedTreeDepth)))
144 (newline)
145 (let ((longLivedTree (make-empty-node)))
146 (Populate kLongLivedTreeDepth longLivedTree)
147
148 ; Create long-lived array, filling half of it
149 (display (string-append
150 " Creating a long-lived array of "
151 (number->string kArraySize)
152 " inexact reals"))
153 (newline)
154 (let ((array (make-vector kArraySize 0.0)))
155 (do ((i 0 (+ i 1)))
156 ((>= i (quotient kArraySize 2)))
157 (vector-set! array i (/ 1.0 (exact->inexact (+ i 1)))))
158 (PrintDiagnostics)
159
160 (do ((d kMinTreeDepth (+ d 2)))
161 ((> d kMaxTreeDepth))
162 (TimeConstruction d))
163
164 (if (or (eq? longLivedTree '())
165 (let ((n (min 1000
166 (- (quotient (vector-length array)
167 2)
168 1))))
169 (not (fl=? (vector-ref array n)
170 (/ 1.0 (exact->inexact (+ n 1)))))))
171 (begin (display "Failed") (newline)))
172 ; fake reference to LongLivedTree
173 ; and array
174 ; to keep them from being optimized away
175 ))))
176 (PrintDiagnostics))
177
178 (main))))
179
180 (define (main . rest)
181 (let ((k (if (null? rest) 18 (car rest))))
182 (display "The garbage collector should touch about ")
183 (display (expt 2 (- k 13)))
184 (display " megabytes of heap storage.")
185 (newline)
186 (display "The use of more or less memory will skew the results.")
187 (newline)
188 (run-benchmark
189 (string-append "GCBench" (number->string k))
190 gcbench-iters
191 (lambda (result) #t)
192 (lambda (k) (lambda () (gcbench k)))
193 k))))