test/hotspot/jtreg/serviceability/sa/TestHeapDumpForInvokeDynamic.java
author ihse
Tue, 20 Feb 2018 21:47:54 +0100
changeset 48923 b75c9e2e3b1f
parent 47216 71c04702a3d5
child 50791 b1e90a8a876c
permissions -rw-r--r--
8198450: Make jdk.internal.vm.compiler/module-info.java.extra reproducable Reviewed-by: erikj, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42889
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     1
/*
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     4
 *
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     8
 *
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    13
 * accompanied this code).
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    14
 *
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    18
 *
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    21
 * questions.
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    22
 */
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    23
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    24
import java.util.ArrayList;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    25
import java.util.List;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    26
import java.io.File;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    27
import java.nio.file.Files;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    28
import java.io.IOException;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    29
import java.io.BufferedInputStream;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    30
import java.util.stream.Collectors;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    31
import java.io.FileInputStream;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    32
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    33
import sun.jvm.hotspot.HotSpotAgent;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    34
import sun.jvm.hotspot.debugger.*;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    35
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    36
import jdk.test.lib.apps.LingeredApp;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    37
import jdk.test.lib.JDKToolLauncher;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    38
import jdk.test.lib.JDKToolFinder;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    39
import jdk.test.lib.Platform;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    40
import jdk.test.lib.process.ProcessTools;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    41
import jdk.test.lib.process.OutputAnalyzer;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    42
import jdk.test.lib.Utils;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    43
import jdk.test.lib.Asserts;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    44
import jdk.test.lib.hprof.HprofParser;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    45
import jdk.test.lib.hprof.parser.HprofReader;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    46
import jdk.test.lib.hprof.parser.PositionDataInputStream;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    47
import jdk.test.lib.hprof.model.Snapshot;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    48
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    49
/*
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    50
 * @test
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    51
 * @library /test/lib
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    52
 * @requires os.family != "mac"
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    53
 * @modules java.base/jdk.internal.misc
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    54
 *          jdk.hotspot.agent/sun.jvm.hotspot
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    55
 *          jdk.hotspot.agent/sun.jvm.hotspot.utilities
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    56
 *          jdk.hotspot.agent/sun.jvm.hotspot.oops
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    57
 *          jdk.hotspot.agent/sun.jvm.hotspot.debugger
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    58
 * @run main/othervm TestHeapDumpForInvokeDynamic
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    59
 */
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    60
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    61
public class TestHeapDumpForInvokeDynamic {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    62
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    63
    private static LingeredAppWithInvokeDynamic theApp = null;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    64
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    65
    private static void verifyHeapDump(String heapFile) {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    66
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    67
        File heapDumpFile = new File(heapFile);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    68
        Asserts.assertTrue(heapDumpFile.exists() && heapDumpFile.isFile(),
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    69
                          "Could not create dump file " + heapDumpFile.getAbsolutePath());
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    70
        try (PositionDataInputStream in = new PositionDataInputStream(
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    71
                new BufferedInputStream(new FileInputStream(heapFile)))) {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    72
            int i = in.readInt();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    73
            if (HprofReader.verifyMagicNumber(i)) {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    74
                Snapshot sshot;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    75
                HprofReader r = new HprofReader(heapFile, in, 0,
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    76
                                                false, 0);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    77
                sshot = r.read();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    78
            } else {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    79
                throw new IOException("Unrecognized magic number: " + i);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    80
            }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    81
        } catch (Exception e) {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    82
            e.printStackTrace();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    83
            Asserts.fail("Could not read dump file " + heapFile);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    84
        } finally {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    85
            heapDumpFile.delete();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    86
        }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    87
    }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    88
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    89
    private static void attachDumpAndVerify(String heapDumpFileName,
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    90
                                            long lingeredAppPid) throws Exception {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    91
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    92
        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    93
        launcher.addToolArg("jmap");
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    94
        launcher.addToolArg("--binaryheap");
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    95
        launcher.addToolArg("--dumpfile");
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    96
        launcher.addToolArg(heapDumpFileName);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    97
        launcher.addToolArg("--pid");
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    98
        launcher.addToolArg(Long.toString(lingeredAppPid));
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
    99
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   100
        ProcessBuilder processBuilder = new ProcessBuilder();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   101
        processBuilder.command(launcher.getCommand());
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   102
        System.out.println(
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   103
            processBuilder.command().stream().collect(Collectors.joining(" ")));
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   104
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   105
        OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   106
        SAOutput.shouldHaveExitValue(0);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   107
        SAOutput.shouldContain("heap written to");
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   108
        SAOutput.shouldContain(heapDumpFileName);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   109
        System.out.println(SAOutput.getOutput());
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   110
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   111
        verifyHeapDump(heapDumpFileName);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   112
    }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   113
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   114
    public static void main (String... args) throws Exception {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   115
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   116
        String heapDumpFileName = "lambdaHeapDump.bin";
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   117
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   118
        if (!Platform.shouldSAAttach()) {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   119
            System.out.println(
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   120
               "SA attach not expected to work - test skipped.");
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   121
            return;
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   122
        }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   123
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   124
        File heapDumpFile = new File(heapDumpFileName);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   125
        if (heapDumpFile.exists()) {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   126
            heapDumpFile.delete();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   127
        }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   128
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   129
        try {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   130
            List<String> vmArgs = new ArrayList<String>();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   131
            vmArgs.add("-XX:+UsePerfData");
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   132
            vmArgs.addAll(Utils.getVmOptions());
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   133
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   134
            theApp = new LingeredAppWithInvokeDynamic();
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   135
            LingeredApp.startApp(vmArgs, theApp);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   136
            attachDumpAndVerify(heapDumpFileName, theApp.getPid());
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   137
        } finally {
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   138
            LingeredApp.stopApp(theApp);
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   139
        }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   140
    }
0b0ae99d8639 8159127: hprof heap dumps broken for lambda classdata
dsamersoff
parents:
diff changeset
   141
}