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