1 /*
  2  * Copyright (c) 2021, 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=passive
 28  * @library /test/lib
 29  * @modules jdk.attach/com.sun.tools.attach
 30  * @requires vm.gc.Shenandoah
 31  *
 32  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 33  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 34  *      -XX:+ShenandoahDegeneratedGC
 35  *      TestJcmdHeapDump
 36  *
 37  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 38  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 39  *      -XX:-ShenandoahDegeneratedGC
 40  *      TestJcmdHeapDump
 41  */
 42 
 43 /*
 44  * @test id=aggressive
 45  * @library /test/lib
 46  * @modules jdk.attach/com.sun.tools.attach
 47  * @requires vm.gc.Shenandoah
 48  *
 49  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 50  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 51  *      -XX:+ShenandoahOOMDuringEvacALot
 52  *      TestJcmdHeapDump
 53  *
 54  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 55  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 56  *      -XX:+ShenandoahAllocFailureALot
 57  *      TestJcmdHeapDump
 58  *
 59  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 60  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
 61  *      TestJcmdHeapDump
 62  */
 63 
 64 /*
 65  * @test id=adaptive
 66  * @library /test/lib
 67  * @modules jdk.attach/com.sun.tools.attach
 68  * @requires vm.gc.Shenandoah
 69  *
 70  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 71  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 72  *      -Dtarget=10000
 73  *      TestJcmdHeapDump
 74  */
 75 
 76 /*
 77  * @test id=generational
 78  * @library /test/lib
 79  * @modules jdk.attach/com.sun.tools.attach
 80  * @requires vm.gc.Shenandoah
 81  *
 82  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 83  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive -XX:ShenandoahGCMode=generational
 84  *      -Dtarget=10000
 85  *      TestJcmdHeapDump
 86  */
 87 
 88 /*
 89  * @test id=static
 90  * @library /test/lib
 91  * @modules jdk.attach/com.sun.tools.attach
 92  * @requires vm.gc.Shenandoah
 93  *
 94  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 95  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
 96  *      TestJcmdHeapDump
 97  */
 98 
 99 /*
100  * @test id=compact
101  * @library /test/lib
102  * @modules jdk.attach/com.sun.tools.attach
103  * @requires vm.gc.Shenandoah
104  *
105  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
106  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
107  *     TestJcmdHeapDump
108  */
109 
110 import jdk.test.lib.JDKToolLauncher;
111 import jdk.test.lib.process.ProcessTools;
112 import jdk.test.lib.process.OutputAnalyzer;
113 
114 import java.io.File;
115 
116 public class TestJcmdHeapDump {
117     public static void main(String[] args) {
118         long pid = ProcessHandle.current().pid();
119         JDKToolLauncher jcmd = JDKToolLauncher.createUsingTestJDK("jcmd");
120         jcmd.addToolArg(String.valueOf(pid));
121         jcmd.addToolArg("GC.heap_dump");
122         String dumpFileName = "myheapdump" + String.valueOf(pid);
123         jcmd.addToolArg(dumpFileName);
124 
125         try {
126             OutputAnalyzer output = ProcessTools.executeProcess(jcmd.getCommand());
127             output.shouldHaveExitValue(0);
128         } catch (Exception e) {
129             throw new RuntimeException("Test failed: " + e);
130         }
131 
132         File f = new File(dumpFileName);
133         if (f.exists()) {
134             f.delete();
135         } else {
136             throw new RuntimeException("Dump file not created");
137         }
138     }
139 }