test/hotspot/jtreg/vmTestbase/nsk/stress/except/except001.java
author jcbeyler
Wed, 21 Nov 2018 09:38:27 -0800
changeset 52640 3a7d49718852
parent 50169 dd501973095c
permissions -rw-r--r--
8213721: [Graal] Tests vmTestbase/nsk/stress/except/except* may be encountering SEGV during out-of-memory conditions Summary: Add the exception handler only for the current thread Reviewed-by: dholmes, sspitsyn, thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50169
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     1
/*
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     2
 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     4
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     8
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    13
 * accompanied this code).
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    14
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    18
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    21
 * questions.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    22
 */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    23
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    24
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    25
/*
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    26
 * @test
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    27
 * @key stress
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    28
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    29
 * @summary converted from VM testbase nsk/stress/except/except001.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    30
 * VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    31
 * VM testbase readme:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    32
 * DESCRIPTION
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    33
 *     This checks if OutOfMemoryError exception is correctly enwrapped into
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    34
 *     InvocationTargetException when thrown inside a method invoked via
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    35
 *     reflection.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    36
 *     The test tries to occupy all of memory available in the heap by
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    37
 *     allocating lots of new Object() instances. Instances of the "empty"
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    38
 *     type Object are the smallest objects, so they apparently should occupy
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    39
 *     most fine-grained fragments in the heap. Thus, there apparently should
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    40
 *     not remain any free space to incarnate new Throwable instance, and VM
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    41
 *     possibly could crash while trying to throw new OutOfMemoryError and
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    42
 *     enwrap it into new InvocationTargetException instance.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    43
 *     By the way, the test checks time elapsed to allocate memory. Both
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    44
 *     classic VM and HotSpot seem to fall into poor performance of memory
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    45
 *     allocation when heap is almost over. E.g.: HotSpot 1.3-betaH may spend
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    46
 *     more than 1 minute to allocate next Object in this case (tested on
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    47
 *     Pentium-II, 350MHz, 128Mb RAM). To avoid this problem, the test enforce
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    48
 *     OutOfMemoryError if more then 5 minutes is spent to allocate "last bytes"
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    49
 *     of memory.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    50
 * COMMENTS
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    51
 *     HotSpot releases 1.0-fcsE (both Win32 and Sparc), and 1.3-betaH (Win32)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    52
 *     fail on this test due to poor performance of memory allocation.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    53
 *         #4248801 (P3/S5) slow memory allocation when heap is almost exhausted
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    54
 *     Despite this bug is treated fixed in HotSpot 1.0.1, it still does suffer
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    55
 *     slow memory allocation when running on PC having 64Mb or less of RAM.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    56
 *     There is also a workaround involved to avoid the following bugs known
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    57
 *     for HotSpot and for classic VM:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    58
 *         #4239841 (P1/S5) 1.1: poor garbage collector performance  (HotSpot bug)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    59
 *         #4245060 (P4/S5) poor garbage collector performance       (Classic VM bug)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    60
 *     However, printing of the test's error messages, warnings, and of execution
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    61
 *     trace may fail under JDK 1.2 for Win32 even so.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    62
 *     HotSpot 2.0-devA (Win32) crashes due to the known HotSpot bug:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    63
 *         #4239828 (P1/S4) 1.3c1: VM crashes when heap is exhausted
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    64
 *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    65
 * @run main/othervm -Xms50M -Xmx200M nsk.stress.except.except001
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    66
 */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    67
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    68
package nsk.stress.except;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    69
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    70
import java.io.PrintStream;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    71
import java.lang.reflect.InvocationTargetException;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    72
import java.lang.reflect.Method;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    73
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    74
/**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    75
 * This checks if <code>OutOfMemoryError</code> exception is correctly
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    76
 * enwrapped into <code>InvocationTargetException</code> when thrown inside
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    77
 * a method invoked via reflection.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    78
 * <p>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    79
 * <p>The test tries to occupy all of memory available in the heap by
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    80
 * allocating lots of new <code>Object()</code> instances. Instances of the
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    81
 * ``empty'' type <code>Object</code> are the smallest objects, so they
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    82
 * apparently should occupy most fine-grained fragments in the heap.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    83
 * Thus, there apparently should not remain any free space to incarnate new
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    84
 * <code>Throwable</code> instance, and VM possibly could crash while trying
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    85
 * to throw new <code>OutOfMemoryError</code> and enwrap it into new
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    86
 * <code>InvocationTargetException</code> instance.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    87
 * <p>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    88
 * <p>By the way, the test checks time elapsed to allocate memory.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    89
 * Both classic VM and HotSpot seem to fall into poor performance of memory
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    90
 * allocation when heap is almost over. E.g.: HotSpot 1.3-betaH may spend
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    91
 * more than 1 minute to allocate next <code>Object</code> in this case
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    92
 * (tested on Pentium-II, 350MHz, 128Mb RAM). To workaround this problem,
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    93
 * the test enforces <code>OutOfMemoryError</code> if more then 5 minutes
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    94
 * is spent to allocate ``last bytes'' of the memory.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    95
 */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    96
public class except001 {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    97
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    98
     * This field allows or supresses printing with <code>display()</code>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
    99
     * method.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   100
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   101
     * @see #display(Object)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   102
     * @see #complain(Object)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   103
     * @see #out
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   104
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   105
    private static boolean MODE_VERBOSE = true;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   106
    /*
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   107
    * Storage for a lot of tiny objects
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   108
    * "static volatile" keywords are for preventing heap optimization
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   109
    */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   110
    private static volatile Object pool[] = null;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   111
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   112
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   113
     * Print execution trace if <code>MODE_VERBOSE</code> is <code>true</code>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   114
     * (optional).
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   115
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   116
     * @see #MODE_VERBOSE
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   117
     * @see #complain(Object)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   118
     * @see #out
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   119
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   120
    private static void display(Object message) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   121
        if (MODE_VERBOSE)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   122
            out.println(message.toString());
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   123
        out.flush();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   124
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   125
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   126
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   127
     * Print error <code>message</code>.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   128
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   129
     * @see #display(Object)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   130
     * @see #out
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   131
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   132
    private static void complain(Object message) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   133
        out.println("# " + message);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   134
        out.flush();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   135
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   136
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   137
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   138
     * The log-stream assigned at runtime by the method
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   139
     * <code>run(args,out)</code>.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   140
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   141
     * @see #display(Object)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   142
     * @see #complain(Object)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   143
     * @see #run(String[], PrintStream)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   144
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   145
    private static PrintStream out;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   146
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   147
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   148
     * Try to allocate lots of instances of the type <code>Object</code>.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   149
     * Such instances are most fine-grained, and thus they should occupy
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   150
     * smallest fragments of free memory in the heap.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   151
     * <p>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   152
     * <p>By the way, break the test, if JVM has spent more than
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   153
     * 5 minutes to allocate latest portions of memory.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   154
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   155
    public static void raiseOutOfMemory() throws OutOfMemoryError {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   156
        try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   157
            // Repository for objects, which should be allocated:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   158
            int index = 0;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   159
            for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   160
                try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   161
                    pool = new Object[size];
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   162
                } catch (OutOfMemoryError oome) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   163
                }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   164
            if (pool == null)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   165
                throw new Error("HS bug: cannot allocate new Object[1]");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   166
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   167
            // Sum up time spent, when it was hard to JVM to allocate next object
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   168
            // (i.e.: when JVM has spent more than 1 second to allocate new object):
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   169
            double totalDelay = 0;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   170
            long timeMark = System.currentTimeMillis();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   171
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   172
            for (; index < pool.length; index++) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   173
                //-------------------------
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   174
                pool[index] = new Object();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   175
                long nextTimeMark = System.currentTimeMillis();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   176
                long elapsed = nextTimeMark - timeMark;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   177
                timeMark = nextTimeMark;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   178
                //----------------------
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   179
                if (elapsed > 1000) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   180
                    double seconds = elapsed / 1000.0;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   181
                    display(
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   182
                            "pool[" + index +
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   183
                                    "]=new Object(); // elapsed " + seconds + "s");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   184
                    totalDelay += seconds;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   185
                    if (totalDelay > 300) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   186
                        complain(
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   187
                                "Memory allocation became slow: so heap seems exhausted.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   188
                        throw new OutOfMemoryError();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   189
                    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   190
                }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   191
            }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   192
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   193
            // This method should never return:
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   194
            throw new Error("TEST_BUG: failed to provoke OutOfMemoryError");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   195
        } finally {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   196
            // Make sure there will be enough memory for next object allocation
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   197
             pool = null;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   198
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   199
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   200
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   201
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   202
     * Invoke the method <code>raiseOutOfMemory()</code> with reflection,
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   203
     * and check if the exception it throws is just
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   204
     * <code>OutOfMemoryError</code> enwrapped into
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   205
     * <code>InvocationTargetException</code> instance.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   206
     * <p>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   207
     * <p>Before the test begins, <code>this.out</code> filed is assigned
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   208
     * to the parameter <code>out</code>. Parameter <code>args[]</code>
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   209
     * is ignored.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   210
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   211
     * @see #raiseOutOfMemory()
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   212
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   213
    public static int run(String args[], PrintStream out) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   214
        out.println("# While printing this message, JVM seems to initiate the output");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   215
        out.println("# stream, so that it will not need more memory to print later,");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   216
        out.println("# when the heap would fail to provide more memory.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   217
        out.println("# ");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   218
        out.println("# That problem is caused by the known JDK/HotSpot bugs:");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   219
        out.println("#     4239841 (P1/S5) 1.1: poor garbage collector performance");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   220
        out.println("#     4245060 (P4/S5) poor garbage collector performance");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   221
        out.println("# ");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   222
        out.println("# This message is just intended to work-around that problem.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   223
        out.println("# If printing should fail even so.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   224
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   225
        if (args.length > 0) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   226
            if (args[0].toLowerCase().startsWith("-v"))
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   227
                MODE_VERBOSE = true;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   228
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   229
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   230
        except001.out = out;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   231
        Class testClass = except001.class;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   232
        try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   233
            Method testMethod = testClass.getMethod("raiseOutOfMemory", new Class [0]);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   234
            Object junk = testMethod.invoke(null, new Object [0]);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   235
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   236
        } catch (InvocationTargetException ite) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   237
            Throwable targetException = ite.getTargetException();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   238
            if (targetException instanceof OutOfMemoryError) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   239
                display("OutOfMemoryError thrown as expected.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   240
                display("Test passed.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   241
                return 0;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   242
            }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   243
            complain("Unexpected InvocationTargetException: " + targetException);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   244
            complain("Test failed.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   245
            return 2;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   246
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   247
        } catch (Exception exception) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   248
            complain("Unexpected exception: " + exception);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   249
            complain("Test failed.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   250
            return 2;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   251
        }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   252
        //
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   253
        complain("The test has finished unexpectedly.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   254
        complain("Test failed.");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   255
        return 2;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   256
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   257
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   258
    /**
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   259
     * Re-call to <code>run(args,out)</code>, and return JCK-like exit status.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   260
     * (The stream <code>out</code> is assigned to <code>System.out</code> here.)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   261
     *
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   262
     * @see #run(String[], PrintStream)
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   263
     */
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   264
    public static void main(String args[]) {
52640
3a7d49718852 8213721: [Graal] Tests vmTestbase/nsk/stress/except/except* may be encountering SEGV during out-of-memory conditions
jcbeyler
parents: 50169
diff changeset
   265
        Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
50169
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   266
            // Last try. If there is some exception outside the code, test should end correctly
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   267
            @Override
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   268
            public void uncaughtException(Thread t, Throwable e) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   269
                try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   270
                    pool = null;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   271
                    System.gc();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   272
                    if (e instanceof OutOfMemoryError) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   273
                        try {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   274
                            System.out.println("OOME : Test Skipped");
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   275
                            System.exit(95);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   276
                        } catch (Throwable ignore) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   277
                        } // No code in the handler can provoke correct exceptions.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   278
                    } else {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   279
                        e.printStackTrace();
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   280
                        throw (RuntimeException) e;
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   281
                    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   282
                } catch (OutOfMemoryError oome) {
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   283
                }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   284
            }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   285
        });
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   286
        int exitCode = run(args, System.out);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   287
        System.exit(exitCode + 95);
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   288
        // JCK-like exit status.
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   289
    }
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   290
dd501973095c 8199271: [TESTBUG] open source VM testbase stress tests
lmesnik
parents:
diff changeset
   291
}