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