| 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 |
* Translated to C++ 30 May 1997 by William D Clinger of Northeastern Univ. |
| 5 |
* Translated to C and simplified for compatibility with the Gambit benchmark |
| 6 |
* suite 1 July 1999 by William D Clinger of Northeastern Univ. |
| 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 |
* Unlike the original Ellis and Kovac benchmark, we do not attempt |
| 33 |
* measure pause times. This facility should eventually be added back |
| 34 |
* in. There are several reasons for omitting it for now. The original |
| 35 |
* implementation depended on assumptions about the thread scheduler |
| 36 |
* that don't hold uniformly. The results really measure both the |
| 37 |
* scheduler and GC. Pause time measurements tend to not fit well with |
| 38 |
* current benchmark suites. As far as we know, none of the current |
| 39 |
* commercial Java implementations seriously attempt to minimize GC pause |
| 40 |
* times. |
| 41 |
*/ |
| 42 |
|
| 43 |
#include <stdio.h> |
| 44 |
|
| 45 |
int kStretchTreeDepth = 18; /* about 16Mb */ |
| 46 |
int kLongLivedTreeDepth = 16; /* about 4Mb */ |
| 47 |
int kArraySize = 500000; /* about 4Mb */ |
| 48 |
int kMinTreeDepth = 4; |
| 49 |
int kMaxTreeDepth = 16; |
| 50 |
|
| 51 |
typedef struct Node0 *Node; |
| 52 |
|
| 53 |
struct Node0 { |
| 54 |
Node left; |
| 55 |
Node right; |
| 56 |
int i, j; |
| 57 |
}; |
| 58 |
|
| 59 |
static Node make_Node(Node l, Node r) { |
| 60 |
Node result = (Node) malloc(sizeof(struct Node0)); |
| 61 |
result->left = l; |
| 62 |
result->right = r; |
| 63 |
return result; |
| 64 |
} |
| 65 |
|
| 66 |
static Node leaf_Node() { |
| 67 |
return make_Node(0, 0); |
| 68 |
} |
| 69 |
|
| 70 |
static void free_Node(Node x) { |
| 71 |
if (x->left) |
| 72 |
free_Node(x->left); |
| 73 |
if (x->right) |
| 74 |
free_Node(x->right); |
| 75 |
free(x); |
| 76 |
} |
| 77 |
|
| 78 |
/* Nodes used by a tree of a given size */ |
| 79 |
static int TreeSize(int i) { |
| 80 |
return ((1 << (i + 1)) - 1); |
| 81 |
} |
| 82 |
|
| 83 |
/* Number of iterations to use for a given tree depth */ |
| 84 |
static int NumIters(int i) { |
| 85 |
return 2 * TreeSize(kStretchTreeDepth) / TreeSize(i); |
| 86 |
} |
| 87 |
|
| 88 |
/* Build tree top down, assigning to older objects. */ |
| 89 |
static void Populate(int iDepth, Node thisNode) { |
| 90 |
if (iDepth<=0) { |
| 91 |
return; |
| 92 |
} else { |
| 93 |
iDepth--; |
| 94 |
thisNode->left = leaf_Node(); |
| 95 |
thisNode->right = leaf_Node(); |
| 96 |
Populate (iDepth, thisNode->left); |
| 97 |
Populate (iDepth, thisNode->right); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/* Build tree bottom-up */ |
| 102 |
static Node MakeTree(int iDepth) { |
| 103 |
if (iDepth<=0) { |
| 104 |
return leaf_Node(); |
| 105 |
} else { |
| 106 |
return make_Node(MakeTree(iDepth-1), |
| 107 |
MakeTree(iDepth-1)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
static void PrintDiagnostics() { |
| 112 |
#if 0 |
| 113 |
long lFreeMemory = Runtime.getRuntime().freeMemory(); |
| 114 |
long lTotalMemory = Runtime.getRuntime().totalMemory(); |
| 115 |
|
| 116 |
System.out.print(" Total memory available=" |
| 117 |
+ lTotalMemory + " bytes"); |
| 118 |
System.out.println(" Free memory=" + lFreeMemory + " bytes"); |
| 119 |
#endif |
| 120 |
} |
| 121 |
|
| 122 |
static void TimeConstruction(int depth) { |
| 123 |
long tStart, tFinish; |
| 124 |
int iNumIters = NumIters(depth); |
| 125 |
Node tempTree; |
| 126 |
int i; |
| 127 |
|
| 128 |
printf ("Creating %d trees of depth %d\n", iNumIters, depth); |
| 129 |
|
| 130 |
for (i = 0; i < iNumIters; ++i) { |
| 131 |
tempTree = leaf_Node(); |
| 132 |
Populate(depth, tempTree); |
| 133 |
free_Node(tempTree); |
| 134 |
tempTree = 0; |
| 135 |
} |
| 136 |
for (i = 0; i < iNumIters; ++i) { |
| 137 |
tempTree = MakeTree(depth); |
| 138 |
free_Node(tempTree); |
| 139 |
tempTree = 0; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
main() { |
| 144 |
Node root; |
| 145 |
Node longLivedTree; |
| 146 |
Node tempTree; |
| 147 |
double *array; |
| 148 |
int i; |
| 149 |
int d; |
| 150 |
|
| 151 |
printf ("Garbage Collector Test\n"); |
| 152 |
printf (" Live storage will peak at %d bytes.\n\n", |
| 153 |
2 * sizeof(struct Node0) * TreeSize(kLongLivedTreeDepth) + |
| 154 |
sizeof(double) * kArraySize); |
| 155 |
printf (" Stretching memory with a binary tree of depth %d\n", |
| 156 |
kStretchTreeDepth); |
| 157 |
PrintDiagnostics(); |
| 158 |
|
| 159 |
/* Stretch the memory space quickly */ |
| 160 |
tempTree = MakeTree(kStretchTreeDepth); |
| 161 |
free_Node(tempTree); |
| 162 |
tempTree = 0; |
| 163 |
|
| 164 |
/* Create a long lived object */ |
| 165 |
printf (" Creating a long-lived binary tree of depth %d\n", |
| 166 |
kLongLivedTreeDepth); |
| 167 |
longLivedTree = leaf_Node(); |
| 168 |
Populate(kLongLivedTreeDepth, longLivedTree); |
| 169 |
|
| 170 |
/* Create long-lived array, filling half of it */ |
| 171 |
printf (" Creating a long-lived array of %d doubles\n", |
| 172 |
kArraySize); |
| 173 |
array = (double *) malloc(kArraySize*sizeof(double)); |
| 174 |
for (i = 0; i < kArraySize/2; ++i) { |
| 175 |
array[i] = 1.0/i; /* sic */ |
| 176 |
} |
| 177 |
PrintDiagnostics(); |
| 178 |
|
| 179 |
for (d = kMinTreeDepth; d <= kMaxTreeDepth; d += 2) { |
| 180 |
TimeConstruction(d); |
| 181 |
} |
| 182 |
|
| 183 |
if (longLivedTree == 0 || array[1000] != 1.0/1000) |
| 184 |
printf ("Failed\n"); |
| 185 |
/* Fake reference to LongLivedTree */ |
| 186 |
/* and array */ |
| 187 |
/* to keep them from being optimized away */ |
| 188 |
|
| 189 |
PrintDiagnostics(); |
| 190 |
} |