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