Debian Codegraph @

benchmarks.larceny/src/gcbench.scm

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 (define (run-benchmark2 name thunk)
37 (display name)
38 (newline)
39 (thunk))
40
41 (define (PrintDiagnostics)
42 (display " Total memory available= ???????? bytes")
43 (display " Free memory= ???????? bytes")
44 (newline))
45
46 (define (gcbench kStretchTreeDepth)
47
48 ; Nodes used by a tree of a given size
49 (define (TreeSize i)
50 (- (expt 2 (+ i 1)) 1))
51
52 ; Number of iterations to use for a given tree depth
53 (define (NumIters i)
54 (quotient (* 2 (TreeSize kStretchTreeDepth))
55 (TreeSize i)))
56
57 ; Parameters are determined by kStretchTreeDepth.
58 ; In Boehm's version the parameters were fixed as follows:
59 ; public static final int kStretchTreeDepth = 18; // about 16Mb
60 ; public static final int kLongLivedTreeDepth = 16; // about 4Mb
61 ; public static final int kArraySize = 500000; // about 4Mb
62 ; public static final int kMinTreeDepth = 4;
63 ; public static final int kMaxTreeDepth = 16;
64 ; In Larceny the storage numbers above would be 12 Mby, 3 Mby, 6 Mby.
65
66 (let* ((kLongLivedTreeDepth (- kStretchTreeDepth 2))
67 (kArraySize (* 4 (TreeSize kLongLivedTreeDepth)))
68 (kMinTreeDepth 4)
69 (kMaxTreeDepth kLongLivedTreeDepth))
70
71 ; Elements 3 and 4 of the allocated vectors are useless.
72
73 (let* ((make-empty-node (lambda () (make-vector 4 0)))
74 (make-node
75 (lambda (l r)
76 (let ((v (make-empty-node)))
77 (vector-set! v 0 l)
78 (vector-set! v 1 r)
79 v)))
80 (node.left (lambda (node) (vector-ref node 0)))
81 (node.right (lambda (node) (vector-ref node 1)))
82 (node.left-set! (lambda (node x) (vector-set! node 0 x)))
83 (node.right-set! (lambda (node x) (vector-set! node 1 x))))
84
85 ; Build tree top down, assigning to older objects.
86 (define (Populate iDepth thisNode)
87 (if (<= iDepth 0)
88 #f
89 (let ((iDepth (- iDepth 1)))
90 (node.left-set! thisNode (make-empty-node))
91 (node.right-set! thisNode (make-empty-node))
92 (Populate iDepth (node.left thisNode))
93 (Populate iDepth (node.right thisNode)))))
94
95 ; Build tree bottom-up
96 (define (MakeTree iDepth)
97 (if (<= iDepth 0)
98 (make-empty-node)
99 (make-node (MakeTree (- iDepth 1))
100 (MakeTree (- iDepth 1)))))
101
102 (define (TimeConstruction depth)
103 (let ((iNumIters (NumIters depth)))
104 (display (string-append "Creating "
105 (number->string iNumIters)
106 " trees of depth "
107 (number->string depth)))
108 (newline)
109 (run-benchmark2
110 "GCBench: Top down construction"
111 (lambda ()
112 (do ((i 0 (+ i 1)))
113 ((>= i iNumIters))
114 (Populate depth (make-empty-node)))))
115 (run-benchmark2
116 "GCBench: Bottom up construction"
117 (lambda ()
118 (do ((i 0 (+ i 1)))
119 ((>= i iNumIters))
120 (MakeTree depth))))))
121
122 (define (main)
123 (display "Garbage Collector Test")
124 (newline)
125 (display (string-append
126 " Stretching memory with a binary tree of depth "
127 (number->string kStretchTreeDepth)))
128 (newline)
129 (PrintDiagnostics)
130 (run-benchmark2
131 "GCBench: Main"
132 (lambda ()
133 ; Stretch the memory space quickly
134 (MakeTree kStretchTreeDepth)
135
136 ; Create a long lived object
137 (display (string-append
138 " Creating a long-lived binary tree of depth "
139 (number->string kLongLivedTreeDepth)))
140 (newline)
141 (let ((longLivedTree (make-empty-node)))
142 (Populate kLongLivedTreeDepth longLivedTree)
143
144 ; Create long-lived array, filling half of it
145 (display (string-append
146 " Creating a long-lived array of "
147 (number->string kArraySize)
148 " inexact reals"))
149 (newline)
150 (let ((array (make-vector kArraySize 0.0)))
151 (do ((i 0 (+ i 1)))
152 ((>= i (quotient kArraySize 2)))
153 (vector-set! array i (/ 1.0 (exact->inexact (+ i 1)))))
154 (PrintDiagnostics)
155
156 (do ((d kMinTreeDepth (+ d 2)))
157 ((> d kMaxTreeDepth))
158 (TimeConstruction d))
159
160 (if (or (eq? longLivedTree '())
161 (let ((n (min 1000
162 (- (quotient (vector-length array)
163 2)
164 1))))
165 (not (FLOAT= (vector-ref array n)
166 (/ 1.0 (exact->inexact (+ n 1)))))))
167 (begin (display "Failed") (newline)))
168 ; fake reference to LongLivedTree
169 ; and array
170 ; to keep them from being optimized away
171 ))))
172 (PrintDiagnostics))
173
174 (main))))
175
176 (define (main . rest)
177 (let ((k (if (null? rest) 18 (car rest))))
178 (display "The garbage collector should touch about ")
179 (display (expt 2 (- k 13)))
180 (display " megabytes of heap storage.")
181 (newline)
182 (display "The use of more or less memory will skew the results.")
183 (newline)
184 (run-benchmark
185 (string-append "GCBench" (number->string k))
186 gcbench-iters
187 (lambda (result) #t)
188 (lambda (k) (lambda () (gcbench k)))
189 k)))