1 /*
   2  * Copyright (c) 2020 SAP SE. 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 import java.io.File;
  25 import java.nio.file.Files;
  26 import java.io.IOException;
  27 import java.util.List;
  28 
  29 import jdk.test.lib.hprof.HprofParser;
  30 import jdk.test.lib.hprof.parser.Reader;
  31 import jdk.test.lib.hprof.model.Snapshot;
  32 
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.dcmd.PidJcmdExecutor;
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 
  37 /*
  38  * @test
  39  * @requires vm.gc.Serial
  40  * @summary Test of diagnostic command GC.heap_dump with gzipped output (Serial GC)
  41  * @library /test/lib
  42  * @modules java.base/jdk.internal.misc
  43  *          java.compiler
  44  *          java.management
  45  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  46  * @run main/othervm -XX:+UseSerialGC HeapDumpCompressedTest
  47  */
  48 
  49 /*
  50  * @test
  51  * @requires vm.gc.Parallel
  52  * @summary Test of diagnostic command GC.heap_dump with gzipped output (Parallel GC)
  53  * @library /test/lib
  54  * @modules java.base/jdk.internal.misc
  55  *          java.compiler
  56  *          java.management
  57  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  58  * @run main/othervm -XX:+UseParallelGC HeapDumpCompressedTest
  59  */
  60 
  61 /*
  62  * @test
  63  * @requires vm.gc.G1
  64  * @summary Test of diagnostic command GC.heap_dump with gzipped output (G1 GC)
  65  * @library /test/lib
  66  * @modules java.base/jdk.internal.misc
  67  *          java.compiler
  68  *          java.management
  69  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  70  * @run main/othervm -XX:+UseG1GC HeapDumpCompressedTest
  71  */
  72 
  73 /*
  74  * @test
  75  * @requires vm.gc.Z
  76  * @summary Test of diagnostic command GC.heap_dump with gzipped output (Z GC)
  77  * @library /test/lib
  78  * @modules java.base/jdk.internal.misc
  79  *          java.compiler
  80  *          java.management
  81  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  82  * @run main/othervm -XX:+UseZGC HeapDumpCompressedTest
  83  */
  84 
  85 /*
  86  * @test
  87  * @requires vm.gc.Shenandoah
  88  * @summary Test of diagnostic command GC.heap_dump with gzipped output (Shenandoah GC)
  89  * @library /test/lib
  90  * @modules java.base/jdk.internal.misc
  91  *          java.compiler
  92  *          java.management
  93  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  94  * @run main/othervm -XX:+UseShenandoahGC HeapDumpCompressedTest
  95  */
  96 
  97 /*
  98  * @test
  99  * @requires vm.gc.Epsilon
 100  * @summary Test of diagnostic command GC.heap_dump with gzipped output (Epsilon GC)
 101  * @library /test/lib
 102  * @modules java.base/jdk.internal.misc
 103  *          java.compiler
 104  *          java.management
 105  *          jdk.internal.jvmstat/sun.jvmstat.monitor
 106  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC HeapDumpCompressedTest
 107  */
 108 
 109 public class HeapDumpCompressedTest {
 110     public static HeapDumpCompressedTest ref;
 111 
 112     public static void main(String[] args) throws Exception {
 113         PidJcmdExecutor executor = new PidJcmdExecutor();
 114         ref = new HeapDumpCompressedTest();
 115         File dump = new File("jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof.gz");
 116 
 117         if (dump.exists()) {
 118             dump.delete();
 119         }
 120 
 121         // Check we detect an invalid compression level.
 122         OutputAnalyzer output = executor.execute("GC.heap_dump -gz=0 " +
 123                                                   dump.getAbsolutePath());
 124         output.shouldContain("Compression level out of range");
 125 
 126         // Check we can create a gzipped dump.
 127         output = executor.execute("GC.heap_dump -gz=1 " + dump.getAbsolutePath());
 128         output.shouldContain("Heap dump file created");
 129 
 130         // Check we detect an already present heap dump.
 131         output = executor.execute("GC.heap_dump -gz=1 " + dump.getAbsolutePath());
 132         output.shouldContain("Unable to create ");
 133 
 134         verifyHeapDump(dump);
 135         dump.delete();
 136     }
 137 
 138     private static void verifyHeapDump(File dump) throws Exception {
 139 
 140         Asserts.assertTrue(dump.exists() && dump.isFile(),
 141                            "Could not create dump file " + dump.getAbsolutePath());
 142 
 143         try {
 144             File out = HprofParser.parse(dump);
 145 
 146             Asserts.assertTrue(out != null && out.exists() && out.isFile(),
 147                                "Could not find hprof parser output file");
 148             List<String> lines = Files.readAllLines(out.toPath());
 149             Asserts.assertTrue(lines.size() > 0, "hprof parser output file is empty");
 150             for (String line : lines) {
 151                 Asserts.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve " +
 152                                                  "object.*constantPoolOop.*).*"));
 153             }
 154 
 155             out.delete();
 156         } catch (Exception e) {
 157             e.printStackTrace();
 158             Asserts.fail("Could not parse dump file " + dump.getAbsolutePath());
 159         }
 160     }
 161 }
 162