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 /*
111  * @test id=iu-aggressive
112  * @library /test/lib
113  * @modules jdk.attach/com.sun.tools.attach
114  * @requires vm.gc.Shenandoah
115  *
116  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
117  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
118  *      -XX:+ShenandoahOOMDuringEvacALot
119  *      TestJcmdHeapDump
120  *
121  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
122  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
123  *      -XX:+ShenandoahAllocFailureALot
124  *      TestJcmdHeapDump
125  *
126  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
127  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
128  *      TestJcmdHeapDump
129  */
130 
131 /*
132  * @test id=iu
133  * @requires vm.gc.Shenandoah
134  * @library /test/lib
135  * @modules jdk.attach/com.sun.tools.attach
136  *
137  * @run main/othervm/timeout=480 -Xmx16m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
138  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
139  *      TestJcmdHeapDump
140  */
141 
142 import jdk.test.lib.JDKToolLauncher;
143 import jdk.test.lib.process.OutputAnalyzer;
144 
145 import java.io.File;
146 
147 public class TestJcmdHeapDump {
148     public static void main(String[] args) {
149         long pid = ProcessHandle.current().pid();
150         JDKToolLauncher jcmd = JDKToolLauncher.createUsingTestJDK("jcmd");
151         jcmd.addToolArg(String.valueOf(pid));
152         jcmd.addToolArg("GC.heap_dump");
153         String dumpFileName = "myheapdump" + String.valueOf(pid);
154         jcmd.addToolArg(dumpFileName);
155 
156         try {
157             ProcessBuilder pb = new ProcessBuilder(jcmd.getCommand());
158             Process jcmdProc = pb.start();
159 
160             OutputAnalyzer output = new OutputAnalyzer(jcmdProc);
161             jcmdProc.waitFor();
162             output.shouldHaveExitValue(0);
163         } catch (Exception e) {
164             throw new RuntimeException("Test failed: " + e);
165         }
166 
167         File f = new File(dumpFileName);
168         if (f.exists()) {
169             f.delete();
170         } else {
171             throw new RuntimeException("Dump file not created");
172         }
173     }
174 }
--- EOF ---