Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/gctest01/gctest01.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*/222324/*25* @test26* @key randomness27*28* @summary converted from VM Testbase gc/gctests/gctest01.29* VM Testbase keywords: [gc]30*31* @library /vmTestbase32* /test/lib33* @run main/othervm gc.gctests.gctest01.gctest01 100 1034*/3536package gc.gctests.gctest01;3738import nsk.share.test.*;39import nsk.share.log.*;40import nsk.share.gc.*;41import nsk.share.TestBug;4243//import RusageStruct;4445/* -- stress testing46create 20 memory evil threads requesting to allocate47the object of sizes from 8 to ( 2 ^ 19).48The live time of objects is very short.49Memory evil thread exits the first time memory allocation fails.50*/5152class ThreadTracker {53static int threadCount = 0;5455static synchronized int getThreadCount() {56return threadCount;57}5859static synchronized void setThreadCount(int count) {60threadCount = count;61}6263static synchronized void incr() {64threadCount++;65}6667static synchronized void decr() {68threadCount--;69}70}7172class PopulationException extends Exception {73}7475class Person {76String name;77int ssid;78int age;79int buf[];80int bufsz;81static int populationLimit;82static int currentPopulation;8384public Person(String n, int ssid, int age, int bufsz) throws PopulationException {85this.incr();86if (this.getPopulation() > this.getPopulationLimit()) {87throw new PopulationException();88}89name = n;90this.ssid = ssid;91this.age = age;92if ( bufsz > 0 ) {93this.bufsz = bufsz;94this.buf = new int[bufsz];95}96}9798static synchronized void incr() {99currentPopulation++;100}101102static synchronized int getPopulation() {103return currentPopulation;104}105106static synchronized void setPopulation(int census) {107currentPopulation = census;108}109110static synchronized void setPopulationLimit(int limit) {111populationLimit = limit;112}113114static synchronized int getPopulationLimit() {115return populationLimit;116}117}118119120121// create 20 memory evil threads requesting to allocate122// the object of sizes from 8 to ( 2 ^ 19).123// The live time of objects is very short.124public class gctest01 extends TestBase {125private String[] args;126127public gctest01(String[] args) {128setArgs(args);129}130131class memevil extends Thread {132int sum;133int bufsz = 64;134135public memevil(int bufsz) {136ThreadTracker.incr();137sum = 0;138this.bufsz = bufsz;139140}141142/* Person object is live short, it will be garbage after143control returns144*/145private boolean doit() {146try {147Person p = new Person("Duke", 100, 100, bufsz);148} catch (OutOfMemoryError e ) {149log.info(getName() + ": Out of Memory");150return false; //should free up some memory151} catch (PopulationException e) {152//we've reached the limit, so stop153return false;154}155return true;156}157158public void run() {159while ( doit() ) {160if ( LocalRandom.random() > 0.6668) {161try {162sleep(10); // to be nice163}164catch (InterruptedException e) {}165}166}167//must be done, decrement the thread count168ThreadTracker.decr();169}170}171172class escaper extends Thread {173public void run() {174while ( ThreadTracker.getThreadCount() > 0 ) {175int buf[] = new int[32];176try177{178Thread.currentThread().sleep(1000);179} catch (InterruptedException e) {180}181// log.info("Is the sun rising?");182}183}184}185186187public void run() {188int bufsz = 8;189int i = 3;190int peopleLimit = 1000;191String usage = "usage: gctest01 [NumberOfObjects [Iterations] ] ]";192int loops;193int LOOPCOUNT = 10;194195if (args.length > 0) {196try {197peopleLimit = Integer.valueOf(args[0]).intValue();198} catch (NumberFormatException e) {199log.info(usage);200throw new TestBug("Bad input to gctest01." +201" Expected integer, got: ->" + args[0] + "<-", e);202}203}204205if (args.length > 1 ) {206try {207LOOPCOUNT = Integer.valueOf(args[1]).intValue();208} catch (NumberFormatException e) {209log.error(usage);210throw new TestBug("Bad input to gctest01." +211" Expected int, got: ->" + args[1] + "<-", e);212}213214}215216double before = 0.0;217double after;218219Person.setPopulationLimit(peopleLimit);220221for (loops = 0; loops < LOOPCOUNT; loops++) {222Person.setPopulation(0);223escaper you = new escaper();224you.setName("Escaper");225you.start();226i = 3;227bufsz = 8;228while ( i < 20 )229{230memevil me = new memevil(bufsz);231me.setName("Memevil" + bufsz);232bufsz = 2*bufsz;233me.start();234i++;235Thread.currentThread().yield();236}237try238{239you.join();240}241catch (InterruptedException e)242{243e.printStackTrace();244}245} // end of loops246log.info("Test passed.");247248}249250public void setArgs(String[] args) {251this.args = args;252}253254public static void main(String args[]) {255GC.runTest(new gctest01(args), args);256}257}258259260