Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/gctest02/gctest02.java
41155 views
/*1* Copyright (c) 2002, 2021, 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*/22//gctest02.java232425/*26* @test27* @key randomness28*29* @summary converted from VM Testbase gc/gctests/gctest02.30* VM Testbase keywords: [gc]31*32* @library /vmTestbase33* /test/lib34* @run main/othervm gc.gctests.gctest02.gctest02 10035*/3637package gc.gctests.gctest02;3839import nsk.share.TestFailure;40import nsk.share.TestBug;41import nsk.share.test.LocalRandom;4243/* stress testing44create 16 memory evil threads requesting to allocate45the object of sizes from 8 to ( 2 ^ 19).46The live time of objects is random (0 ~ 1000).47Here we let the threads that reference the objects48to simulate the object life time.49*/5051class PopulationException extends Exception {52//this exception is used to signal that we've53//reached the end of the test54}5556class ThreadCount {57static int count= 0;58static synchronized void inc() { count++; }59static synchronized void dec() { count --; }60static synchronized int get() { return count; }61}6263class Person {64String name;65int ssid;66int age;67int buf[];68int bufsz;69static int populationCount = 0;70static int populationLimit = 0;7172Person(String n, int ssid, int age, int bufsz)73throws PopulationException {74name = n;75this.ssid = ssid;76this.age = age;77if ( bufsz > 0 ) {78this.bufsz = bufsz;79this.buf = new int[bufsz];80}81incPopulation();82if (getPopulation() > getPopulationLimit()) {83throw new PopulationException();84}85}86public static synchronized int getPopulationLimit() {87return populationLimit;88}89public static synchronized void setPopulationLimit(int newLimit) {90populationLimit = newLimit;91}92public static synchronized int getPopulation() {93return populationCount;94}95public static synchronized void incPopulation() {96populationCount ++;97}9899}100101// hr (humane resource) dept is using objects.102// Put the hr thread to sleep to keep the reference to objects103class hr extends Thread {104Person pp;105int lifetime;106107hr(Person p, int l) {108pp = p;109lifetime = l;110}111112public void run() {113// just sleep to emulate the life time of object referenced by p114try { sleep(lifetime); }115catch (InterruptedException e) {}116}117}118119class Memevil extends Thread {120int sum;121int bufsz = 64;122boolean debug = false;123124Memevil(int bufsz) {125sum = 0;126this.bufsz = bufsz;127}128/* Person object is live short, it will be garbage after129* control returns130*/131private boolean doit() {132try {133Person p = new Person("Duke", 100, 100, bufsz);134hr useit = new hr(p, (int)(100*LocalRandom.random()));135useit.start();136return true;137}138catch (PopulationException e) {139return false;140}141catch (OutOfMemoryError e ) {142System.err.println(getName() + ": Out of Memory");143return false;144}145}146public void run() {147while ( doit() ) {148if ( LocalRandom.random() > 0.6668) {149try {150sleep(10); // to be nice151}152catch (InterruptedException e) {153}154}155Thread.yield();156}157//we've reached the population limit, so we're exiting the thread158ThreadCount.dec();159}160}161162class Escaper extends Thread {163public void run() {164while ( ThreadCount.get() > 0 ) {165int buf[] = new int[32];166{167Thread.yield();168}169}170}171}172173public class gctest02 {174public static void main(String args[] ) {175int bufsz = 8;176int peopleLimit = 1000;177Memevil me=null;178if (args.length > 0)179{180try181{182peopleLimit = Integer.valueOf(args[0]).intValue();183}184catch (NumberFormatException e)185{186throw new TestBug(187"Bad input to gctest02. Expected integer, got: ->"188+ args[0] + "<-", e);189}190}191192Person.setPopulationLimit(peopleLimit);193for (int ii=0; ii<40; ii++) {194bufsz = 8;195Person.populationCount = 0;196Escaper you = new Escaper();197you.setName("Escaper");198ThreadCount.inc();199you.start();200me = new Memevil(bufsz);201me.setName("Memevil" + bufsz);202bufsz = 2*bufsz;203me.start();204Thread.yield();205for (int i=1; i<11; i++) {206ThreadCount.inc();207me = new Memevil(bufsz);208me.setName("Memevil" + bufsz);209bufsz = 2*bufsz;210me.start();211Thread.yield();212}213try {214you.join();215}216catch (InterruptedException e) {217throw new TestFailure("InterruptedException in gctest2.main()");218}219for (int i=1; i<11; i++) {220try { me.join(); }221catch (InterruptedException e) {222throw new TestFailure("InterruptedException in gctest2.main()");223}224}225}226System.out.println("Test passed.");227}228}229230231