Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpTest.java
41153 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.Test;24import org.testng.Assert;2526import java.io.File;27import java.nio.file.Files;28import java.io.IOException;29import java.util.List;3031import jdk.test.lib.hprof.HprofParser;32import jdk.test.lib.hprof.model.Snapshot;3334import jdk.test.lib.JDKToolFinder;35import jdk.test.lib.process.OutputAnalyzer;36import jdk.test.lib.dcmd.CommandExecutor;37import jdk.test.lib.dcmd.PidJcmdExecutor;3839/*40* @test41* @summary Test of diagnostic command GC.heap_dump42* @library /test/lib43* @modules java.base/jdk.internal.misc44* java.compiler45* java.management46* jdk.internal.jvmstat/sun.jvmstat.monitor47* @run testng HeapDumpTest48*/49public class HeapDumpTest {50protected String heapDumpArgs = "";5152public void run(CommandExecutor executor) throws IOException {53File dump = new File("jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof");54if (dump.exists()) {55dump.delete();56}5758String cmd = "GC.heap_dump " + heapDumpArgs + " " + dump.getAbsolutePath();59executor.execute(cmd);6061verifyHeapDump(dump);62dump.delete();63}6465private void verifyHeapDump(File dump) {66Assert.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());67try {68File out = HprofParser.parse(dump);6970Assert.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");71List<String> lines = Files.readAllLines(out.toPath());72Assert.assertTrue(lines.size() > 0, "hprof parser output file is empty");73for (String line : lines) {74Assert.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));75}7677out.delete();78} catch (Exception e) {79e.printStackTrace();80Assert.fail("Could not parse dump file " + dump.getAbsolutePath());81}82}8384/* GC.heap_dump is not available over JMX, running jcmd pid executor instead */85@Test86public void pid() throws IOException {87run(new PidJcmdExecutor());88}89}90919293