Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/JumbleGC002/JumbleGC002.java
41159 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key stress randomness26*27* @summary converted from VM Testbase gc/gctests/JumbleGC002.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, quarantine]29* VM Testbase readme:30* DESCRIPTION31* The test checks that Garbage Collector can manage jumble in the JVM. The32* test fails if any unexpected exceptions and errors are thrown or the JVM33* is not crashed.34* The test starts a number of threads that is set in *.cfg file or calculates35* that value based on the machine. All threads have36* java.util.Vector field anf they fill that vector with 4 types of objects:37* 1. Initialized long[]38* 2. Uninitialized double[]39* 3. Initialized int[]40* 4. A nsk.share.gc.NonbranchyTree (number of nodes and their size depend41* on valkue returned by Runtime.maxMemory())42* As soon as the vector is filled, each thread removes half elements of it and43* then fills those places of the vector again. However, all threads use just44* about 10% of maximum amount of memory that JVM attemts to use, so45* OutOfMemoryError is treated as a failure. That means GC does not work46* quickly enough to destroy all objects that do not have references. The47* procedure of filling and cleaning of the vector is repeated for48* INTERNAL_ITERATIONS times.49*50* @library /vmTestbase51* /test/lib52* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.JumbleGC002.JumbleGC00253*/5455package gc.gctests.JumbleGC002;5657import java.io.*;58import java.util.*;5960import nsk.share.*;61import nsk.share.gc.*;62import nsk.share.test.LocalRandom;6364/**65* This test simply does Algorithms.eatMemory() in a loop66* in multiple threads.67*/68public class JumbleGC002 extends ThreadedGCTest {6970// The test should fill just about 10% of the heap71final static double PART_OF_HEAP = 0.1;72// Maximum number of elements in an array of primitive types73final static int ARRAY_MAX_LENGTH = 10;74// Internal number of iterations to create new objects and to drop75// references76final static int INTERNAL_ITERATIONS = 150;77// Size of core for each node of a tree78final static int EACH_NODE_SIZE = 1;79// Number of bytes that arrays of primitive types take in the vector80final static long PRIMITIVE_ARRAYS_SIZE = (long) (8 * ARRAY_MAX_LENGTH81+ 8 * ARRAY_MAX_LENGTH + 4 * ARRAY_MAX_LENGTH);8283private class Eater implements Runnable {8485private Vector vector;86int numberOfElements;87int numberOfQuarters;88int id;89int nodes;9091public Eater(int id, int numberOfQuarters, int nodes) {92this.numberOfQuarters = numberOfQuarters;93numberOfElements = 4 * numberOfQuarters;94this.id = id;95this.nodes = nodes;96}9798public void run() {99// Make jumble in the heap!100initVector();101while (getExecutionController().continueExecution()) {102fillVector();103cleanVector();104}105}106107// Initialize the vector and build appropriate number of cells in it108private void initVector() {109vector = new Vector();110for (int i = 0; i < numberOfElements; i++) {111vector.addElement(null);112}113}114115// Fill the vector. It is devided into quarters. Each quarters has an116// initialized array of long and int, and uninitialized array of double.117// Each array has not more than ARRAY_MAX_LENGTH elements. The fourth118// element in the quarter is a NonbranchyTree.119private void fillVector() {120for (int i = 0; i < numberOfQuarters; i++) {121122// Append initialized long[]123int length = LocalRandom.nextInt(ARRAY_MAX_LENGTH);124long[] l = new long[length];125for (int j = 0; j < length; j++) {126l[j] = (long) j;127}128if (vector.elementAt(4 * i) == null) {129vector.setElementAt(l, 4 * i);130}131132// Append not initialized double[]133length = LocalRandom.nextInt(ARRAY_MAX_LENGTH);134double[] d = new double[length];135if (vector.elementAt(4 * i + 1) == null) {136vector.setElementAt(d, 4 * i + 1);137}138139// Append initialized int[]140length = LocalRandom.nextInt(ARRAY_MAX_LENGTH);141int[] n = new int[length];142for (int j = 0; j < length; j++) {143n[j] = j;144}145if (vector.elementAt(4 * i + 2) == null) {146vector.setElementAt(n, 4 * i + 2);147}148149// Append a tree. Every even thread has a "bent" tree.150NonbranchyTree tree = new NonbranchyTree(nodes, 0.3f, EACH_NODE_SIZE);151if (id % 2 == 0) {152tree.bend();153}154if (vector.elementAt(4 * i + 3) == null) {155vector.setElementAt(tree, 4 * i + 3);156}157}158}159160// Drop references to half of the elements of the vector161private void cleanVector() {162int index = LocalRandom.nextInt(numberOfElements / 2);163for (int i = index; i < index + numberOfElements / 2; i++) {164vector.setElementAt(null, i);165}166}167}168169protected Runnable createRunnable(int i) {170// Perform calculations specific to the test171long memoryForThread = (long) (Runtime.getRuntime().maxMemory() * PART_OF_HEAP / runParams.getNumberOfThreads());172int numberOfQuarters;173174if (i == 0) {175// The very first thread176numberOfQuarters = 1;177} else {178// All other threads179numberOfQuarters = 8;180}181182// Calculate number of nodes for a tree depending on number of183// elements in the Vector184185double freeMemory = (double) memoryForThread / numberOfQuarters186- (double) PRIMITIVE_ARRAYS_SIZE;187int nodes = (int) (freeMemory / (NonbranchyTree.MIN_NODE_SIZE + EACH_NODE_SIZE));188nodes = Math.max(1, nodes);189log.debug("Thread " + i + " has a tree with "190+ nodes + " node(s).");191192return new Eater(i, numberOfQuarters, nodes);193}194195public static void main(String args[]) {196GC.runTest(new JumbleGC002(), args);197}198}199200201