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.File;
  28 import java.io.IOException;
  29 
  30 import jdk.test.lib.hprof.HprofParser;
  31 import jdk.test.lib.hprof.model.Snapshot;
  32 
  33 import com.oracle.java.testlibrary.JDKToolFinder;
  34 import com.oracle.java.testlibrary.OutputAnalyzer;
  35 import com.oracle.java.testlibrary.dcmd.CommandExecutor;
  36 import com.oracle.java.testlibrary.dcmd.PidJcmdExecutor;
  37 
  38 /*
  39  * @test
  40  * @summary Test of diagnostic command GC.heap_dump
  41  * @library /testlibrary
  42  * @library /../../test/lib/share/classes
  43  * @modules java.base/sun.misc
  44  *          java.compiler
  45  *          java.management
  46  *          jdk.jvmstat/sun.jvmstat.monitor
  47  * @build com.oracle.java.testlibrary.*
  48  * @build com.oracle.java.testlibrary.dcmd.*
  49  * @build jdk.test.lib.hprof.*
  50  * @build jdk.test.lib.hprof.module.*
  51  * @build jdk.test.lib.hprof.parser.*
  52  * @build jdk.test.lib.hprof.utils.*
  53  * @run testng HeapDumpTest
  54  */
  55 public class HeapDumpTest {
  56     protected String heapDumpArgs = "";
  57 
  58     public void run(CommandExecutor executor) {
  59         File dump = new File("jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof");
  60         String cmd = "GC.heap_dump " + heapDumpArgs + " " + dump.getAbsolutePath();
  61         executor.execute(cmd);
  62 
  63         verifyHeapDump(dump);
  64 
  65         dump.deleteOnExit();
  66     }
  67 
  68     private void verifyHeapDump(File dump) {
  69         Assert.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
  70         try {
  71             HprofParser.parse(dump);
  72         } catch (Exception e) {
  73             e.printStackTrace();
  74             Assert.fail("Could not parse dump file " + dump.getAbsolutePath());
  75         }
  76     }
  77 
  78     /* GC.heap_dump is not available over JMX, running jcmd pid executor instead */
  79     @Test
  80     public void pid() {
  81         run(new PidJcmdExecutor());
  82     }
  83 }
  84