1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. 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 org.testng.annotations.Test;
  25 import org.testng.Assert;
  26 
  27 import java.io.File;
  28 import java.nio.file.Files;
  29 import java.io.IOException;
  30 import java.util.List;
  31 
  32 import jdk.test.lib.hprof.HprofParser;
  33 import jdk.test.lib.hprof.model.Snapshot;
  34 
  35 import jdk.test.lib.JDKToolFinder;
  36 import jdk.test.lib.OutputAnalyzer;
  37 import jdk.test.lib.dcmd.CommandExecutor;
  38 import jdk.test.lib.dcmd.PidJcmdExecutor;
  39 
  40 /*
  41  * @test
  42  * @summary Test of diagnostic command GC.heap_dump
  43  * @library /testlibrary
  44  * @library /test/lib/share/classes
  45  * @modules java.base/sun.misc
  46  *          java.compiler
  47  *          java.management
  48  *          jdk.jvmstat/sun.jvmstat.monitor
  49  * @build jdk.test.lib.*
  50  * @build jdk.test.lib.dcmd.*
  51  * @build jdk.test.lib.hprof.*
  52  * @build jdk.test.lib.hprof.model.*
  53  * @build jdk.test.lib.hprof.parser.*
  54  * @build jdk.test.lib.hprof.util.*
  55  * @run testng HeapDumpTest
  56  */
  57 public class HeapDumpTest {
  58     protected String heapDumpArgs = "";
  59 
  60     public void run(CommandExecutor executor) throws IOException {
  61         File dump = new File("jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof");
  62         if (dump.exists()) {
  63             dump.delete();
  64         }
  65 
  66         String cmd = "GC.heap_dump " + heapDumpArgs + " " + dump.getAbsolutePath();
  67         executor.execute(cmd);
  68 
  69         verifyHeapDump(dump);
  70         dump.delete();
  71     }
  72 
  73     private void verifyHeapDump(File dump) {
  74         Assert.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
  75         try {
  76             File out = HprofParser.parse(dump);
  77 
  78             Assert.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");
  79             List<String> lines = Files.readAllLines(out.toPath());
  80             Assert.assertTrue(lines.size() > 0, "hprof parser output file is empty");
  81             for (String line : lines) {
  82                 Assert.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));
  83             }
  84 
  85             out.delete();
  86         } catch (Exception e) {
  87             e.printStackTrace();
  88             Assert.fail("Could not parse dump file " + dump.getAbsolutePath());
  89         }
  90     }
  91 
  92     /* GC.heap_dump is not available over JMX, running jcmd pid executor instead */
  93     @Test
  94     public void pid() throws IOException {
  95         run(new PidJcmdExecutor());
  96     }
  97 }
  98