1 /* 2 * Copyright (c) 2003, 2020, 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 26 * @key stress gc randomness 27 * 28 * @summary converted from VM Testbase gc/gctests/JumbleGC002. 29 * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, quarantine] 30 * VM Testbase readme: 31 * DESCRIPTION 32 * The test checks that Garbage Collector can manage jumble in the JVM. The 33 * test fails if any unexpected exceptions and errors are thrown or the JVM 34 * is not crashed. 35 * The test starts a number of threads that is set in *.cfg file or calculates 36 * that value based on the machine. All threads have 37 * java.util.Vector field anf they fill that vector with 4 types of objects: 38 * 1. Initialized long[] 39 * 2. Uninitialized double[] 40 * 3. Initialized int[] 41 * 4. A nsk.share.gc.NonbranchyTree (number of nodes and their size depend 42 * on valkue returned by Runtime.maxMemory()) 43 * As soon as the vector is filled, each thread removes half elements of it and 44 * then fills those places of the vector again. However, all threads use just 45 * about 10% of maximum amount of memory that JVM attemts to use, so 46 * OutOfMemoryError is treated as a failure. That means GC does not work 47 * quickly enough to destroy all objects that do not have references. The 48 * procedure of filling and cleaning of the vector is repeated for 49 * INTERNAL_ITERATIONS times. 50 * 51 * @library /vmTestbase 52 * /test/lib 53 * @run driver jdk.test.lib.FileInstaller . . 54 * @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.JumbleGC002.JumbleGC002 55 */ 56 57 package gc.gctests.JumbleGC002; 58 59 import java.io.*; 60 import java.util.*; 61 62 import nsk.share.*; 63 import nsk.share.gc.*; 64 import nsk.share.test.LocalRandom; 65 66 /** 67 * This test simply does Algorithms.eatMemory() in a loop 68 * in multiple threads. 69 */ 70 public class JumbleGC002 extends ThreadedGCTest { 71 72 // The test should fill just about 10% of the heap 73 final static double PART_OF_HEAP = 0.1; 74 // Maximum number of elements in an array of primitive types 75 final static int ARRAY_MAX_LENGTH = 10; 76 // Internal number of iterations to create new objects and to drop 77 // references 78 final static int INTERNAL_ITERATIONS = 150; 79 // Size of core for each node of a tree 80 final static int EACH_NODE_SIZE = 1; 81 // Number of bytes that arrays of primitive types take in the vector 82 final static long PRIMITIVE_ARRAYS_SIZE = (long) (8 * ARRAY_MAX_LENGTH 83 + 8 * ARRAY_MAX_LENGTH + 4 * ARRAY_MAX_LENGTH); 84 85 private class Eater implements Runnable { 86 87 private Vector vector; 88 int numberOfElements; 89 int numberOfQuarters; 90 int id; 91 int nodes; 92 93 public Eater(int id, int numberOfQuarters, int nodes) { 94 this.numberOfQuarters = numberOfQuarters; 95 numberOfElements = 4 * numberOfQuarters; 96 this.id = id; 97 this.nodes = nodes; 98 } 99 100 public void run() { 101 // Make jumble in the heap! 102 initVector(); 103 while (getExecutionController().continueExecution()) { 104 fillVector(); 105 cleanVector(); 106 } 107 } 108 109 // Initialize the vector and build appropriate number of cells in it 110 private void initVector() { 111 vector = new Vector(); 112 for (int i = 0; i < numberOfElements; i++) { 113 vector.addElement(null); 114 } 115 } 116 117 // Fill the vector. It is devided into quarters. Each quarters has an 118 // initialized array of long and int, and uninitialized array of double. 119 // Each array has not more than ARRAY_MAX_LENGTH elements. The fourth 120 // element in the quarter is a NonbranchyTree. 121 private void fillVector() { 122 for (int i = 0; i < numberOfQuarters; i++) { 123 124 // Append initialized long[] 125 int length = LocalRandom.nextInt(ARRAY_MAX_LENGTH); 126 long[] l = new long[length]; 127 for (int j = 0; j < length; j++) { 128 l[j] = (long) j; 129 } 130 if (vector.elementAt(4 * i) == null) { 131 vector.setElementAt(l, 4 * i); 132 } 133 134 // Append not initialized double[] 135 length = LocalRandom.nextInt(ARRAY_MAX_LENGTH); 136 double[] d = new double[length]; 137 if (vector.elementAt(4 * i + 1) == null) { 138 vector.setElementAt(d, 4 * i + 1); 139 } 140 141 // Append initialized int[] 142 length = LocalRandom.nextInt(ARRAY_MAX_LENGTH); 143 int[] n = new int[length]; 144 for (int j = 0; j < length; j++) { 145 n[j] = j; 146 } 147 if (vector.elementAt(4 * i + 2) == null) { 148 vector.setElementAt(n, 4 * i + 2); 149 } 150 151 // Append a tree. Every even thread has a "bent" tree. 152 NonbranchyTree tree = new NonbranchyTree(nodes, 0.3f, EACH_NODE_SIZE); 153 if (id % 2 == 0) { 154 tree.bend(); 155 } 156 if (vector.elementAt(4 * i + 3) == null) { 157 vector.setElementAt(tree, 4 * i + 3); 158 } 159 } 160 } 161 162 // Drop references to half of the elements of the vector 163 private void cleanVector() { 164 int index = LocalRandom.nextInt(numberOfElements / 2); 165 for (int i = index; i < index + numberOfElements / 2; i++) { 166 vector.setElementAt(null, i); 167 } 168 } 169 } 170 171 protected Runnable createRunnable(int i) { 172 // Perform calculations specific to the test 173 long memoryForThread = (long) (Runtime.getRuntime().maxMemory() * PART_OF_HEAP / runParams.getNumberOfThreads()); 174 int numberOfQuarters; 175 176 if (i == 0) { 177 // The very first thread 178 numberOfQuarters = 1; 179 } else { 180 // All other threads 181 numberOfQuarters = 8; 182 } 183 184 // Calculate number of nodes for a tree depending on number of 185 // elements in the Vector 186 187 double freeMemory = (double) memoryForThread / numberOfQuarters 188 - (double) PRIMITIVE_ARRAYS_SIZE; 189 int nodes = (int) (freeMemory / (NonbranchyTree.MIN_NODE_SIZE + EACH_NODE_SIZE)); 190 nodes = Math.max(1, nodes); 191 log.debug("Thread " + i + " has a tree with " 192 + nodes + " node(s)."); 193 194 return new Eater(i, numberOfQuarters, nodes); 195 } 196 197 public static void main(String args[]) { 198 GC.runTest(new JumbleGC002(), args); 199 } 200 }