Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except011.java
41155 views
/*1* Copyright (c) 1999, 2018, 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 stress27*28* @summary converted from VM testbase nsk/stress/except/except011.29* VM testbase keywords: [stress, slow, nonconcurrent, quick]30* VM testbase readme:31* DESCRIPTION32* This checks if various exceptions are thrown (and caught) correctly33* when there apparently are no free space in the heap to allocate new34* Throwable instance.35* The test tries to occupy all of memory available in the heap by allocating36* lots of new Object() instances. Instances of the type Object are the smallest37* objects, so they apparently should occupy most fine-grained fragments in the38* heap and leave no free space for new Throwable instance. After that, the test39* provokes various exceptions (e.g.: by executing integer division by 0 and so40* on), and checks if appropriate exceptions are thrown.41* COMMENTS42* The test needs a lot of memory to start up, so it should not run under older43* JDK 1.1.x release due to its poorer heap utilization. Also, some checks are44* skipped when testing classic VM, because OutOfMemoryError is correctly thrown45* instead of target exception.46* When the test is being self-initiating (i.e.: eating heap), memory occupation47* is terminated if memory allocation slows down crucially. This is a workaround48* intended to avoid the HotSpot bug:49* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted50* There is also a workaround involved to avoid the following bugs known51* for HotSpot and for classic VM:52* #4239841 (P1/S5) 1.1: poor garbage collector performance (HotSpot bug)53* #4245060 (P4/S5) poor garbage collector performance (Classic VM bug)54* However, printing of the test's error messages, warnings, and of execution55* trace fails under JDK 1.2 for Win32 even so. If the test fails due to this56* problem, exit status 96 is returned instead of 97.57* JDK 1.3 classic VM for Sparc may crash (core dump) due to the known bug:58* #4245057 (P2/S3) VM crashes when heap is exhausted59*60* @run main/othervm -Xms50M -Xmx200M nsk.stress.except.except01161*/6263package nsk.stress.except;6465import java.io.PrintStream;6667/**68* This checks if various exceptions are thrown (and caught) correctly69* when there apparently are no free space in the heap to allocate new70* <code>Throwable</code> instance.71* <p>72* <p>The test tries to occupy all of memory available in the heap by73* allocating lots of new <code>Object()</code> instances. Instances of the74* type <code>Object</code> are the smallest objects, so they apparently should75* occupy most fine-grained fragments in the heap and leave no free space for76* new <code>Throwable</code> instance. After that, the test provokes various77* exceptions (e.g.: by executing integer division by 0 and so on), and checks78* if appropriate exceptions are thrown.79* <p>80* <p>Note, that memory occupation is terminated if memory allocation slows81* down crucially. This is a workaround intended to avoid the HotSpot bug:82* <br> 83* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted84* <p>85* <p>There is also a workaround involved to avoid the following bugs known86* for HotSpot and for classic VM:87* <br> 88* #4239841 (P1/S5) 1.1: poor garbage collector performance89* <br> 90* #4245060 (P4/S5) poor garbage collector performance91* <br>However, printing of the test's error messages, warnings, and of92* execution trace may fail even so. If the test fails due to poor GC93* performance, exit status 96 is returned instead of 97.94* <p>95* <p>Also note, that the test needs a lot of memory to start up, so it should96* not run under older JDK 1.1.x release due to its poor heap utilization.97*/98public class except011 {99/**100* Either allow or supress printing of execution trace.101*/102private static boolean TRACE_ON = false;103/**104* Either allow or supress printing of warning messages.105*/106private static final boolean WARN_ON = true;107/*108* Storage for a lot of tiny objects109* "static volatile" keywords are for preventing heap optimization110*/111private static volatile Object pool[] = null;112/**113* Temporary <code>log</code> for error messages, warnings and/or execution trace.114*115* @see #messages116*/117private static String log[] = new String[1000]; // up to 1000 messages118/**119* How many <code>messages</code> were submitted to the <code>log</code>.120*121* @see #log122*/123private static int messages = 0;124125private static final String className = "nsk.stress.except.except011oops";126127/**128* Re-call to the method <code>run(out)</code> (ignore <code>args[]</code>),129* and print the test summary - either test passed of failed.130*/131public static int run(String args[], PrintStream out) {132if (args.length > 0) {133if (args[0].toLowerCase().startsWith("-v"))134TRACE_ON = true;135}136137int exitCode = run(out);138pool = null;139System.gc();140// Print the log[] and the test summary:141try {142for (int i = 0; i < messages; i++)143out.println(log[i]);144if (exitCode == 0) {145if (TRACE_ON)146out.println("Test passed.");147} else148out.println("Test failed.");149} catch (OutOfMemoryError oome) {150// Poor performance of garbage collector:151exitCode = 1;152}153154return exitCode;155}156157/**158* Allocate as much <code>Object</code> instances as possible to bring JVM159* into stress, and then check if exceptions are correctly thrown accordingly160* to various situations like integer division by 0, etc.161*/162private static int run(PrintStream out) {163out.println("# While printing this message, JVM seems to initiate the output");164out.println("# stream, so that it will not need more memory to print later,");165out.println("# when the heap would fail to provide more memory.");166out.println("# ");167out.println("# Note, that the test maintains especial static log[] field in");168out.println("# order to avoid printing when the heap seems exhausted.");169out.println("# Nevertheless, printing could arise OutOfMemoryError even");170out.println("# after all the memory allocated by the test is released.");171out.println("# ");172out.println("# That problem is caused by the known JDK/HotSpot bugs:");173out.println("# 4239841 (P1/S5) 1.1: poor garbage collector performance");174out.println("# 4245060 (P4/S5) poor garbage collector performance");175out.println("# ");176out.println("# This message is just intended to work-around that problem.");177out.println("# If printing should fail even so, the test will try to return");178out.println("# the exit status 96 instead of 97 to indicate the problem.");179out.println("# However, the test may fail or even crash on some platforms");180out.println("# suffering the bug 4239841 or 4245060.");181182// Sum up exit code:183int exitCode = 0; // apparently PASSED184int skipped = 0; // some checks may correctly suffer OutOfMemoryError185Class oops;186// Allocate repository for a lots of tiny objects:187for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)188try {189pool = new Object[size];190} catch (OutOfMemoryError oome) {191}192if (pool == null)193throw new Error("HS bug: cannot allocate new Object[1]");194int poolSize = pool.length;195196int index = 0;197pool[index++] = new Object();198199// Sum up time spent, when it was hard to JVM to allocate next object200// (i.e.: when JVM has spent more than 1 second to allocate new object):201double totalDelay = 0;202long timeMark = System.currentTimeMillis();203try {204for (; index < poolSize; index++) {205//-------------------------206pool[index] = new Object();207long nextTimeMark = System.currentTimeMillis();208long elapsed = nextTimeMark - timeMark;209timeMark = nextTimeMark;210//----------------------211if (elapsed > 1000) {212double seconds = elapsed / 1000.0;213if (TRACE_ON)214out.println(215"pool[" + index + "]=new Object(); // elapsed " + seconds + "s");216totalDelay += seconds;217if (totalDelay > 60) {218if (TRACE_ON)219out.println(220"Memory allocation became slow; so, heap seems exhausted.");221break;222}223}224}225} catch (OutOfMemoryError oome) {226if (TRACE_ON)227log[messages++] = "Heap seems exhausted - OutOfMemoryError thrown.";228}229230if (index > poolSize - 1000) {231if (WARN_ON)232log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";233}234235// Check ExceptionInInitializerError:236try {237oops = Class.forName(className);238log[messages++] = "Failure: ExceptionInInitializerError failed to throw";239exitCode = 2;240} catch (ExceptionInInitializerError eiie) {241String message = eiie.getException().getMessage();242if (!message.equals("except011oops")) {243log[messages++] =244"Failure: ExceptionInInitializerError: unexpected target exception";245exitCode = 2;246} else if (TRACE_ON)247log[messages++] = "Success: ExceptionInInitializerError thrown as expected";248} catch (ClassNotFoundException cnfe) {249log[messages++] = "Failure: ExceptionInInitializerError: target class not found";250exitCode = 2;251} catch (OutOfMemoryError oome) {252if (WARN_ON)253log[messages++] =254"Skipped: ExceptionInInitializerError: thrown OutOfMemoryError";255skipped++;256}257258return exitCode;259}260261/**262* Re-call to <code>run(args,out)</code>, and return JCK-like exit status.263* (The stream <code>out</code> is assigned to <code>System.out</code> here.)264*265* @see #run(String[], PrintStream)266*/267public static void main(String args[]) {268Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {269// Last try. If there is some OOME, test should end correctly270@Override271public void uncaughtException(Thread t, Throwable e) {272try {273pool = null;274log = null;275System.gc(); // Empty memory to be able to write to the output276if (e instanceof OutOfMemoryError) {277try {278System.out.println("OOME : Test Skipped");279System.exit(0);280} catch (Throwable ignore) {281} // No code in the handler can provoke correct exceptions.282} else {283e.printStackTrace();284throw (RuntimeException) e;285}286} catch (OutOfMemoryError oome) {287}288}289});290int exitCode = run(args, System.out);291System.exit(exitCode + 95);292// JCK-like exit status.293}294295}296297298