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