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
  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 public class HeapDumpCompressedTest {
  50     public static HeapDumpCompressedTest ref;
  51 
  52     public static void main(String[] args) throws Exception {
  53         PidJcmdExecutor executor = new PidJcmdExecutor();
  54         ref = new HeapDumpCompressedTest();
  55         File dump = new File("jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof.gz");
  56 
  57         if (dump.exists()) {
  58             dump.delete();
  59         }
  60 
  61         // Check we detect an invalid compression level.
  62         OutputAnalyzer output = executor.execute("GC.heap_dump -gz -gz-level=10 " + dump.getAbsolutePath());
  63         output.shouldContain("Compression level out of range");
  64 
  65         // Check we can create a gzipped dump.
  66         executor.execute("GC.heap_dump -gz " + dump.getAbsolutePath());
  67 
  68         // Check we detect an already present heap dump.
  69         output = executor.execute("GC.heap_dump -gz " + dump.getAbsolutePath());
  70         output.shouldContain("Unable to create ");
  71 
  72         // Check the level only if we write gzipped.
  73         output = executor.execute("GC.heap_dump -gz-level=11 " + dump.getAbsolutePath());
  74         output.shouldContain("Unable to create ");
  75 
  76         verifyHeapDump(dump);
  77         dump.delete();
  78     }
  79 
  80     private static void verifyHeapDump(File dump) throws Exception {
  81 
  82         Asserts.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
  83         try {
  84             File out = HprofParser.parse(dump);
  85 
  86             Asserts.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");
  87             List<String> lines = Files.readAllLines(out.toPath());
  88             Asserts.assertTrue(lines.size() > 0, "hprof parser output file is empty");
  89             for (String line : lines) {
  90                 Asserts.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));
  91             }
  92 
  93             out.delete();
  94         } catch (Exception e) {
  95             e.printStackTrace();
  96             Asserts.fail("Could not parse dump file " + dump.getAbsolutePath());
  97         }
  98     }
  99 }
 100