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 TestHeapDump
  27  * @summary Tests JVMTI heap dumps
  28  * @requires vm.gc.Shenandoah
  29  * @compile TestHeapDump.java
  30  * @run main/othervm/native/timeout=300 -agentlib:TestHeapDump -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xmx128m -XX:ShenandoahGCHeuristics=aggressive                        TestHeapDump
  31  *
  32  */
  33 
  34 /**
  35  * @test TestHeapDump
  36  * @summary Tests JVMTI heap dumps
  37  * @requires vm.gc.Shenandoah
  38  * @requires vm.bits == "64"
  39  * @compile TestHeapDump.java
  40  * @run main/othervm/native/timeout=300 -agentlib:TestHeapDump -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xmx128m -XX:ShenandoahGCHeuristics=aggressive -XX:-UseCompressedOops TestHeapDump
  41  */
  42 
  43 /**
  44  * @test TestHeapDump
  45  * @summary Tests JVMTI heap dumps
  46  * @requires vm.gc.Shenandoah
  47  * @compile TestHeapDump.java
  48  * @run main/othervm/native/timeout=300 -agentlib:TestHeapDump -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -Xmx128m -XX:ShenandoahGCHeuristics=aggressive                         -XX:+UseStringDeduplication TestHeapDump
  49  */
  50 
  51 import java.lang.ref.Reference;
  52 
  53 public class TestHeapDump {
  54 
  55     private static final int NUM_ITER = 10000;
  56 
  57     private static final int ARRAY_SIZE = 1000;
  58 
  59     private static final int EXPECTED_OBJECTS =
  60             ARRAY_SIZE +   // array reachable from instance field
  61                     1 +            // static field root
  62                     1;             // local field root
  63 
  64     static {
  65         try {
  66             System.loadLibrary("TestHeapDump");
  67         } catch (UnsatisfiedLinkError ule) {
  68             System.err.println("Could not load TestHeapDump library");
  69             System.err.println("java.library.path: "
  70                     + System.getProperty("java.library.path"));
  71             throw ule;
  72         }
  73     }
  74 
  75     native static int heapdump(Class<?> filterClass);
  76 
  77     public static void main(String args[]) {
  78         new TestHeapDump().run();
  79     }
  80 
  81     // This root needs to be discovered
  82     static Object root = new TestObject();
  83 
  84     // This field needs to be discovered
  85     TestObject[] array;
  86 
  87     public void run() {
  88         array = new TestObject[ARRAY_SIZE];
  89         for (int i = 0; i < ARRAY_SIZE; i++) {
  90             array[i] = new TestObject();
  91         }
  92         TestObject localRoot = new TestObject();
  93         for (int i = 0; i < NUM_ITER; i++) {
  94             int numObjs = heapdump(TestObject.class);
  95             if (numObjs != EXPECTED_OBJECTS) {
  96                 throw new RuntimeException("Expected " + EXPECTED_OBJECTS + " objects, but got " + numObjs);
  97             }
  98         }
  99         Reference.reachabilityFence(array);
 100         Reference.reachabilityFence(localRoot);
 101     }
 102 
 103     // We look for the instances of this class during the heap scan
 104     public static class TestObject {}
 105 }