1 /*
  2  * Copyright (c) 2017, 2020, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 /**
 26  * @test id=aggressive
 27  * @summary Tests JVMTI heap dumps
 28  * @requires vm.gc.Shenandoah
 29  * @requires vm.jvmti
 30  * @compile TestHeapDump.java
 31  * @run main/othervm/native/timeout=300 -agentlib:TestHeapDump
 32  *      -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 33  *      -XX:+UseShenandoahGC -Xmx128m
 34  *      -XX:ShenandoahGCHeuristics=aggressive
 35  *      TestHeapDump
 36  *
 37  */
 38 
 39 /**
 40  * @test id=no-coops-aggressive
 41  * @summary Tests JVMTI heap dumps
 42  * @requires vm.gc.Shenandoah
 43  * @requires vm.jvmti
 44  * @requires vm.bits == "64"
 45  * @compile TestHeapDump.java
 46  * @run main/othervm/native/timeout=300 -agentlib:TestHeapDump
 47  *      -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 48  *      -XX:+UseShenandoahGC -Xmx128m
 49  *      -XX:ShenandoahGCHeuristics=aggressive
 50  *      -XX:-UseCompressedOops TestHeapDump
 51  */
 52 
 53 /**
 54  * @test id=aggressive-strdedup
 55  * @summary Tests JVMTI heap dumps
 56  * @requires vm.gc.Shenandoah
 57  * @requires vm.jvmti
 58  * @compile TestHeapDump.java
 59  * @run main/othervm/native/timeout=300 -agentlib:TestHeapDump
 60  *      -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 61  *      -XX:+UseShenandoahGC -Xmx128m
 62  *      -XX:ShenandoahGCHeuristics=aggressive
 63  *      -XX:+UseStringDeduplication TestHeapDump
 64  */
 65 
 66 import java.lang.ref.Reference;
 67 
 68 public class TestHeapDump {
 69 
 70     private static final int NUM_ITER = 10000;
 71 
 72     private static final int ARRAY_SIZE = 1000;
 73 
 74     private static final int EXPECTED_OBJECTS =
 75             ARRAY_SIZE +   // array reachable from instance field
 76                     1 +            // static field root
 77                     1;             // local field root
 78 
 79     static {
 80         try {
 81             System.loadLibrary("TestHeapDump");
 82         } catch (UnsatisfiedLinkError ule) {
 83             System.err.println("Could not load TestHeapDump library");
 84             System.err.println("java.library.path: "
 85                     + System.getProperty("java.library.path"));
 86             throw ule;
 87         }
 88     }
 89 
 90     native static int heapdump(Class<?> filterClass);
 91 
 92     public static void main(String args[]) {
 93         new TestHeapDump().run();
 94     }
 95 
 96     // This root needs to be discovered
 97     static Object root = new TestObject();
 98 
 99     // This field needs to be discovered
100     TestObject[] array;
101 
102     public void run() {
103         array = new TestObject[ARRAY_SIZE];
104         for (int i = 0; i < ARRAY_SIZE; i++) {
105             array[i] = new TestObject();
106         }
107         TestObject localRoot = new TestObject();
108         for (int i = 0; i < NUM_ITER; i++) {
109             int numObjs = heapdump(TestObject.class);
110             if (numObjs != EXPECTED_OBJECTS) {
111                 throw new RuntimeException("Expected " + EXPECTED_OBJECTS + " objects, but got " + numObjs);
112             }
113         }
114         Reference.reachabilityFence(array);
115         Reference.reachabilityFence(localRoot);
116     }
117 
118     // We look for the instances of this class during the heap scan
119     public static class TestObject {}
120 }