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