1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test TestGCOld 26 * @key gc 27 * @key stress 28 * @requires vm.gc=="null" 29 * @summary Stress the GC by trying to make old objects more likely to be garbage than young objects. 30 * @run main/othervm -Xmx384M -XX:+UseSerialGC TestGCOld 50 1 20 10 10000 31 * @run main/othervm -Xmx384M -XX:+UseParallelGC TestGCOld 50 1 20 10 10000 32 * @run main/othervm -Xmx384M -XX:+UseParallelGC -XX:-UseParallelOldGC TestGCOld 50 1 20 10 10000 33 * @run main/othervm -Xmx384M -XX:+UseConcMarkSweepGC TestGCOld 50 1 20 10 10000 34 * @run main/othervm -Xmx384M -XX:+UseG1GC TestGCOld 50 1 20 10 10000 35 */ 36 37 import java.text.*; 38 import java.util.Random; 39 40 class TreeNode { 41 public TreeNode left, right; 42 public int val; // will always be the height of the tree 43 } 44 45 46 /* Args: 47 live-data-size: in megabytes (approximate, will be rounded down). 48 work: units of mutator non-allocation work per byte allocated, 49 (in unspecified units. This will affect the promotion rate 50 printed at the end of the run: more mutator work per step implies 51 fewer steps per second implies fewer bytes promoted per second.) 52 short/long ratio: ratio of short-lived bytes allocated to long-lived 53 bytes allocated. 54 pointer mutation rate: number of pointer mutations per step. 55 steps: number of steps to do. 56 */ 57 58 public class TestGCOld { 59 60 // Command-line parameters. 61 62 private static int size, workUnits, promoteRate, ptrMutRate, steps; 63 64 // Constants. 65 66 private static final int MEG = 1000000; 67 private static final int INSIGNIFICANT = 999; // this many bytes don't matter 68 private static final int BYTES_PER_WORD = 4; 69 private static final int BYTES_PER_NODE = 20; // bytes per TreeNode 70 private static final int WORDS_DEAD = 100; // size of young garbage object 71 72 private final static int treeHeight = 14; 73 private final static long treeSize = heightToBytes(treeHeight); 74 75 private static final String msg1 76 = "Usage: java TestGCOld <size> <work> <ratio> <mutation> <steps>"; 77 private static final String msg2 78 = " where <size> is the live storage in megabytes"; 79 private static final String msg3 80 = " <work> is the mutator work per step (arbitrary units)"; 81 private static final String msg4 82 = " <ratio> is the ratio of short-lived to long-lived allocation"; 83 private static final String msg5 84 = " <mutation> is the mutations per step"; 85 private static final String msg6 86 = " <steps> is the number of steps"; 87 88 // Counters (and global variables that discourage optimization) 89 90 private static long youngBytes = 0; // total young bytes allocated 91 private static long nodes = 0; // total tree nodes allocated 92 private static long actuallyMut = 0; // pointer mutations in old trees 93 private static long mutatorSum = 0; // checksum to discourage optimization 94 public static int[] aexport; // exported array to discourage opt 95 96 // Global variables. 97 98 private static TreeNode[] trees; 99 private static int where = 0; // roving index into trees 100 private static Random rnd = new Random(); 101 102 // Returns the height of the given tree. 103 104 private static int height (TreeNode t) { 105 if (t == null) { 106 return 0; 107 } 108 else { 109 return 1 + Math.max (height (t.left), height (t.right)); 110 } 111 } 112 113 // Returns the length of the shortest path in the given tree. 114 115 private static int shortestPath (TreeNode t) { 116 if (t == null) { 117 return 0; 118 } 119 else { 120 return 1 + Math.min (shortestPath (t.left), shortestPath (t.right)); 121 } 122 } 123 124 // Returns the number of nodes in a balanced tree of the given height. 125 126 private static long heightToNodes (int h) { 127 if (h == 0) { 128 return 0; 129 } 130 else { 131 long n = 1; 132 while (h > 1) { 133 n = n + n; 134 h = h - 1; 135 } 136 return n + n - 1; 137 } 138 } 139 140 // Returns the number of bytes in a balanced tree of the given height. 141 142 private static long heightToBytes (int h) { 143 return BYTES_PER_NODE * heightToNodes (h); 144 } 145 146 // Returns the height of the largest balanced tree 147 // that has no more than the given number of nodes. 148 149 private static int nodesToHeight (long nodes) { 150 int h = 1; 151 long n = 1; 152 while (n + n - 1 <= nodes) { 153 n = n + n; 154 h = h + 1; 155 } 156 return h - 1; 157 } 158 159 // Returns the height of the largest balanced tree 160 // that occupies no more than the given number of bytes. 161 162 private static int bytesToHeight (long bytes) { 163 return nodesToHeight (bytes / BYTES_PER_NODE); 164 } 165 166 // Returns a newly allocated balanced binary tree of height h. 167 168 private static TreeNode makeTree(int h) { 169 if (h == 0) return null; 170 else { 171 TreeNode res = new TreeNode(); 172 nodes++; 173 res.left = makeTree(h-1); 174 res.right = makeTree(h-1); 175 res.val = h; 176 return res; 177 } 178 } 179 180 // Allocates approximately size megabytes of trees and stores 181 // them into a global array. 182 183 private static void init() { 184 int ntrees = (int) ((size * MEG) / treeSize); 185 trees = new TreeNode[ntrees]; 186 187 System.err.println("Allocating " + ntrees + " trees."); 188 System.err.println(" (" + (ntrees * treeSize) + " bytes)"); 189 for (int i = 0; i < ntrees; i++) { 190 trees[i] = makeTree(treeHeight); 191 // doYoungGenAlloc(promoteRate*ntrees*treeSize, WORDS_DEAD); 192 } 193 System.err.println(" (" + nodes + " nodes)"); 194 195 /* Allow any in-progress GC to catch up... */ 196 // try { Thread.sleep(20000); } catch (InterruptedException x) {} 197 } 198 199 // Confirms that all trees are balanced and have the correct height. 200 201 private static void checkTrees() { 202 int ntrees = trees.length; 203 for (int i = 0; i < ntrees; i++) { 204 TreeNode t = trees[i]; 205 int h1 = height(t); 206 int h2 = shortestPath(t); 207 if ((h1 != treeHeight) || (h2 != treeHeight)) { 208 System.err.println("*****BUG: " + h1 + " " + h2); 209 } 210 } 211 } 212 213 // Called only by replaceTree (below) and by itself. 214 215 private static void replaceTreeWork(TreeNode full, TreeNode partial, 216 boolean dir) { 217 boolean canGoLeft = full.left != null && full.left.val > partial.val; 218 boolean canGoRight = full.right != null && full.right.val > partial.val; 219 if (canGoLeft && canGoRight) { 220 if (dir) 221 replaceTreeWork(full.left, partial, !dir); 222 else 223 replaceTreeWork(full.right, partial, !dir); 224 } else if (!canGoLeft && !canGoRight) { 225 if (dir) 226 full.left = partial; 227 else 228 full.right = partial; 229 } else if (!canGoLeft) { 230 full.left = partial; 231 } else { 232 full.right = partial; 233 } 234 } 235 236 // Given a balanced tree full and a smaller balanced tree partial, 237 // replaces an appropriate subtree of full by partial, taking care 238 // to preserve the shape of the full tree. 239 240 private static void replaceTree(TreeNode full, TreeNode partial) { 241 boolean dir = (partial.val % 2) == 0; 242 actuallyMut++; 243 replaceTreeWork(full, partial, dir); 244 } 245 246 // Allocates approximately n bytes of long-lived storage, 247 // replacing oldest existing long-lived storage. 248 249 private static void oldGenAlloc(long n) { 250 int full = (int) (n / treeSize); 251 long partial = n % treeSize; 252 // System.out.println("In oldGenAlloc, doing " + full + " full trees " 253 // + "and one partial tree of size " + partial); 254 for (int i = 0; i < full; i++) { 255 trees[where++] = makeTree(treeHeight); 256 if (where == trees.length) where = 0; 257 } 258 while (partial > INSIGNIFICANT) { 259 int h = bytesToHeight(partial); 260 TreeNode newTree = makeTree(h); 261 replaceTree(trees[where++], newTree); 262 if (where == trees.length) where = 0; 263 partial = partial - heightToBytes(h); 264 } 265 } 266 267 // Interchanges two randomly selected subtrees (of same size and depth). 268 269 private static void oldGenSwapSubtrees() { 270 // Randomly pick: 271 // * two tree indices 272 // * A depth 273 // * A path to that depth. 274 int index1 = rnd.nextInt(trees.length); 275 int index2 = rnd.nextInt(trees.length); 276 int depth = rnd.nextInt(treeHeight); 277 int path = rnd.nextInt(); 278 TreeNode tn1 = trees[index1]; 279 TreeNode tn2 = trees[index2]; 280 for (int i = 0; i < depth; i++) { 281 if ((path & 1) == 0) { 282 tn1 = tn1.left; 283 tn2 = tn2.left; 284 } else { 285 tn1 = tn1.right; 286 tn2 = tn2.right; 287 } 288 path >>= 1; 289 } 290 TreeNode tmp; 291 if ((path & 1) == 0) { 292 tmp = tn1.left; 293 tn1.left = tn2.left; 294 tn2.left = tmp; 295 } else { 296 tmp = tn1.right; 297 tn1.right = tn2.right; 298 tn2.right = tmp; 299 } 300 actuallyMut += 2; 301 } 302 303 // Update "n" old-generation pointers. 304 305 private static void oldGenMut(long n) { 306 for (int i = 0; i < n/2; i++) { 307 oldGenSwapSubtrees(); 308 } 309 } 310 311 // Does the amount of mutator work appropriate for n bytes of young-gen 312 // garbage allocation. 313 314 private static void doMutWork(long n) { 315 int sum = 0; 316 long limit = workUnits*n/10; 317 for (long k = 0; k < limit; k++) sum++; 318 // We don't want dead code elimination to eliminate the loop above. 319 mutatorSum = mutatorSum + sum; 320 } 321 322 // Allocate n bytes of young-gen garbage, in units of "nwords" 323 // words. 324 325 private static void doYoungGenAlloc(long n, int nwords) { 326 final int nbytes = nwords*BYTES_PER_WORD; 327 int allocated = 0; 328 while (allocated < n) { 329 aexport = new int[nwords]; 330 /* System.err.println("Step"); */ 331 allocated += nbytes; 332 } 333 youngBytes = youngBytes + allocated; 334 } 335 336 // Allocate "n" bytes of young-gen data; and do the 337 // corresponding amount of old-gen allocation and pointer 338 // mutation. 339 340 // oldGenAlloc may perform some mutations, so this code 341 // takes those mutations into account. 342 343 private static void doStep(long n) { 344 long mutations = actuallyMut; 345 346 doYoungGenAlloc(n, WORDS_DEAD); 347 doMutWork(n); 348 oldGenAlloc(n / promoteRate); 349 oldGenMut(Math.max(0L, (mutations + ptrMutRate) - actuallyMut)); 350 } 351 352 public static void main(String[] args) { 353 if (args.length != 5) { 354 System.err.println(msg1); 355 System.err.println(msg2); 356 System.err.println(msg3); 357 System.err.println(msg4); 358 System.err.println(msg5); 359 System.err.println(msg6); 360 return; 361 } 362 363 size = Integer.parseInt(args[0]); 364 workUnits = Integer.parseInt(args[1]); 365 promoteRate = Integer.parseInt(args[2]); 366 ptrMutRate = Integer.parseInt(args[3]); 367 steps = Integer.parseInt(args[4]); 368 369 System.out.println(size + " megabytes of live storage"); 370 System.out.println(workUnits + " work units per step"); 371 System.out.println("promotion ratio is 1:" + promoteRate); 372 System.out.println("pointer mutation rate is " + ptrMutRate); 373 System.out.println(steps + " steps"); 374 375 init(); 376 // checkTrees(); 377 youngBytes = 0; 378 nodes = 0; 379 380 System.err.println("Initialization complete..."); 381 382 long start = System.currentTimeMillis(); 383 384 for (int step = 0; step < steps; step++) { 385 doStep(MEG); 386 } 387 388 long end = System.currentTimeMillis(); 389 float secs = ((float)(end-start))/1000.0F; 390 391 // checkTrees(); 392 393 NumberFormat nf = NumberFormat.getInstance(); 394 nf.setMaximumFractionDigits(1); 395 System.out.println("\nTook " + nf.format(secs) + " sec in steady state."); 396 nf.setMaximumFractionDigits(2); 397 System.out.println("Allocated " + steps + " Mb of young gen garbage" 398 + " (= " + nf.format(((float)steps)/secs) + 399 " Mb/sec)"); 400 System.out.println(" (actually allocated " + 401 nf.format(((float) youngBytes)/MEG) + " megabytes)"); 402 float promoted = ((float)steps) / (float)promoteRate; 403 System.out.println("Promoted " + promoted + 404 " Mb (= " + nf.format(promoted/secs) + " Mb/sec)"); 405 System.out.println(" (actually promoted " + 406 nf.format(((float) (nodes * BYTES_PER_NODE))/MEG) + 407 " megabytes)"); 408 if (ptrMutRate != 0) { 409 System.out.println("Mutated " + actuallyMut + 410 " pointers (= " + 411 nf.format(actuallyMut/secs) + " ptrs/sec)"); 412 413 } 414 // This output serves mainly to discourage optimization. 415 System.out.println("Checksum = " + (mutatorSum + aexport.length)); 416 417 } 418 }