1 /*
   2  * Copyright (c) 2015, 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.IOException;
  28 
  29 import com.oracle.java.testlibrary.JDKToolFinder;
  30 import com.oracle.java.testlibrary.OutputAnalyzer;
  31 import com.oracle.java.testlibrary.dcmd.CommandExecutor;
  32 import com.oracle.java.testlibrary.dcmd.PidJcmdExecutor;
  33 
  34 /*
  35  * @test
  36  * @summary Test of diagnostic command GC.heap_dump
  37  * @library /testlibrary
  38  * @modules java.base/sun.misc
  39  *          java.compiler
  40  *          java.management
  41  *          jdk.jvmstat/sun.jvmstat.monitor
  42  * @build com.oracle.java.testlibrary.*
  43  * @build com.oracle.java.testlibrary.dcmd.*
  44  * @run testng HeapDumpTest
  45  */
  46 public class HeapDumpTest {
  47     protected String heapDumpArgs = "";
  48 
  49     public void run(CommandExecutor executor) {
  50         String fileName = "jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof";
  51         String cmd = "GC.heap_dump " + heapDumpArgs + " " + fileName;
  52         executor.execute(cmd);
  53 
  54         verifyHeapDump(fileName);
  55     }
  56 
  57     private void verifyHeapDump(String fileName) {
  58         String jhat = JDKToolFinder.getJDKTool("jhat");
  59         String[] cmd = { jhat, "-parseonly", "true", fileName };
  60 
  61         ProcessBuilder pb = new ProcessBuilder(cmd);
  62         pb.redirectErrorStream(true);
  63         Process p = null;
  64         OutputAnalyzer output = null;
  65 
  66         try {
  67             p = pb.start();
  68             output = new OutputAnalyzer(p);
  69 
  70             /*
  71              * Some hprof dumps of all objects contain constantPoolOop references that cannot be resolved, so we ignore
  72              * failures about resolving constantPoolOop fields using a negative lookahead
  73              */
  74             output.shouldNotMatch(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*");
  75         } catch (IOException e) {
  76             Assert.fail("Test error: Caught exception while reading stdout/err of jhat", e);
  77         } finally {
  78             if (p != null) {
  79                 p.destroy();
  80             }
  81         }
  82 
  83         if (output.getExitValue() != 0) {
  84             Assert.fail("Test error: jhat exit code was nonzero");
  85         }
  86     }
  87 
  88     /* GC.heap_dump is not available over JMX, running jcmd pid executor instead */
  89     @Test
  90     public void pid() {
  91         run(new PidJcmdExecutor());
  92     }
  93 }
  94