Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/MatrixJuggleGC/MatrixJuggleGC.java
41159 views
/*1* Copyright (c) 2002, 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*/222324/*25* @test26* @key stress randomness27*28* @summary converted from VM Testbase gc/gctests/MatrixJuggleGC.29* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]30* VM Testbase readme:31* ********************************32* set TIMEOUT = 2033* *******************************34* This test creates a 2 dimensional matrix of (100X100)10,000 elements.35* Each element in this matrix houses the address of a "Cell" that36* occupies about 100 bytes. The total memory occupied by this structure is37* about 1M.38* Once this structure, has been built, 5 threads are let loose that39* randomly choose an element in this matrix and set its contents to null40* effectively creating 100bytes of garbage. The threads continue to act41* until all 5 threads combined have "nulled out" half the cells in the matrix.42* At this point, 5 refiller threads proceed to refill the empty43* matrix elements with new cells.44* Once the refiller threads have refilled all the empty matrix elements45* with new cells, the cycle begins all over again with the 5 "emptier"46* threads "nulling out" cells randomly.47* This is repeated 50 times. Every iteration produces 0.5 Meg48* of garbage. The maximum amount of live memory at use at any time is 1Meg.49* If no garbage collection takes place during any of the ten iterations,50* the total amount(live + garbage) of heap space consumed at the end51* of the program is 0.5*50 + 1 = 26Meg.52* The test fails if an OutOfMemory Exception is thrown.53* ----------------------------- --------54* | | | | | | | 100 |55* | | | | | *--|------>| bytes|56* | | | | | | --------57* -----------------------------58* . . . . . .59* . . . . . .60* . . . . . .61* .62* | | | | | |63* | | | | | |64* | | | | | |65* ------------------------------66* | | | | | |67* | | | | | |68* | | | | | |69* ------------------------------70* | | | | | |71* | | | | | |72* | | | | | |73* ------------------------------74* | | | | | |75* | | | | | |76* | | | | | |77* -----------------------------78*79* @library /vmTestbase80* /test/lib81* @run main/othervm gc.gctests.MatrixJuggleGC.MatrixJuggleGC -iterations 100000082*/8384package gc.gctests.MatrixJuggleGC;8586import nsk.share.test.*;87import nsk.share.gc.*;88import java.util.Stack;89import java.util.EmptyStackException;9091public class MatrixJuggleGC extends GCTestBase {92private int threadCount = 5;93private Matrix cm = new Matrix(100, 100);94private Stack<IndexPair> emptiedLocations = new Stack<IndexPair>();9596private class CellEmptier extends Thread {97private boolean keepEmptying(){98int numberOfCells;99int matrixSize;100101matrixSize = cm.returnArrayBound();102numberOfCells = (matrixSize + 1) * (matrixSize + 1) ;103if (cm.getCellCount() < numberOfCells/2)104return true;105else106return false;107}108109public void run() {110int i, j, matrixSize,emptyCells;111112matrixSize = cm.returnArrayBound();113while (keepEmptying()) {114i = LocalRandom.nextInt(0, matrixSize);115j = LocalRandom.nextInt(0, matrixSize);116emptiedLocations.push(new IndexPair(i,j));117cm.clear(i, j);118}119}120}121122private class CellRefiller extends Thread {123public void run() {124int i, j, emptyCells;125while (!emptiedLocations.empty()) {126try {127IndexPair pair = emptiedLocations.pop();128cm.repopulate(pair.getI(), pair.getJ());129} catch (EmptyStackException e) {130break;131}132}133}134}135136private class StackDump extends Thread {137public void run() {138int emptyCells;139while (true) {140emptyCells = emptiedLocations.size();141System.out.println("Number of empty cells = " + emptyCells);142}143}144}145146private void runIteration() {147Thread emptierArray[] = new Thread[threadCount];148Thread fillerArray[] = new Thread[threadCount];149for (int i = 0; i < threadCount; i++)150emptierArray[i] = new CellEmptier();151for (int i = 0; i < threadCount; i++)152emptierArray[i].start();153154// wait for "emptier" threads to finish their job155156int i = 0;157while (i < threadCount) {158try {159emptierArray[i].join();160} catch(InterruptedException e) {}161i++;162}163164// Now start refilling.165166for (i = 0; i < threadCount; i++)167fillerArray[i] = new CellRefiller();168for (i = 0; i < threadCount; i++)169fillerArray[i].start();170171i = 0;172while (i < threadCount ){173try {174fillerArray[i].join();175} catch(InterruptedException e){}176i++;177}178// reset count of cells179cm.resetCellCount();180}181182public void run() {183threadCount = runParams.getNumberOfThreads();184Stresser stresser = new Stresser(runParams.getStressOptions());185stresser.start(runParams.getIterations());186while (stresser.iteration())187runIteration();188stresser.finish();189}190191public static void main(String args[]) {192GC.runTest(new MatrixJuggleGC(), args);193}194}195196197