|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 | +package compiler.ciReplay; |
| 25 | + |
| 26 | +import jdk.test.lib.Asserts; |
| 27 | +import jdk.test.lib.process.OutputAnalyzer; |
| 28 | +import jdk.test.lib.process.ProcessTools; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.nio.file.Files; |
| 32 | +import java.nio.file.Path; |
| 33 | +import java.nio.file.Paths; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.List; |
| 37 | +import java.util.regex.Matcher; |
| 38 | +import java.util.regex.Pattern; |
| 39 | +import java.util.stream.Collectors; |
| 40 | + |
| 41 | +public abstract class DumpReplayBase extends CiReplayBase { |
| 42 | + |
| 43 | + private static final String DUMP_REPLAY_PATTERN = "replay_pid"; |
| 44 | + private List<File> replayFiles; |
| 45 | + private String replayFileName; |
| 46 | + |
| 47 | + @Override |
| 48 | + public void runTest(boolean needCoreDump, String... args) { |
| 49 | + throw new RuntimeException("use runTests(String...)"); |
| 50 | + } |
| 51 | + |
| 52 | + public void runTest(String... args) { |
| 53 | + if (generateReplay(args)) { |
| 54 | + testAction(); |
| 55 | + cleanup(); |
| 56 | + } else { |
| 57 | + throw new Error("Host is not configured to generate cores"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void cleanup() { |
| 63 | + replayFiles.forEach(f -> remove(f.getName())); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String getReplayFileName() { |
| 68 | + Asserts.assertEQ(replayFiles.size(), 1, "Test should only dump 1 replay file when trying to replay compile"); |
| 69 | + return replayFileName; |
| 70 | + } |
| 71 | + |
| 72 | + public boolean generateReplay(String... vmopts) { |
| 73 | + OutputAnalyzer oa; |
| 74 | + try { |
| 75 | + List<String> options = new ArrayList<>(Arrays.asList(vmopts)); |
| 76 | + options.add("-XX:CompileCommand=option," + getTestClass() + "::" + getTestMethod() + ",bool,DumpReplay,true"); |
| 77 | + options.add("-XX:+IgnoreUnrecognizedVMOptions"); |
| 78 | + options.add("-XX:TypeProfileLevel=222"); |
| 79 | + options.add("-XX:CompileCommand=compileonly," + getTestClass() + "::" + getTestMethod()); |
| 80 | + options.add("-Xbatch"); |
| 81 | + options.add(getTestClass()); |
| 82 | + oa = ProcessTools.executeProcess(ProcessTools.createTestJvm(options)); |
| 83 | + Asserts.assertEquals(oa.getExitValue(), 0, "Crash JVM exits gracefully"); |
| 84 | + replayFiles = Files.list(Paths.get(".")) |
| 85 | + .map(Path::toFile) |
| 86 | + .filter(f -> f.getName().startsWith(DUMP_REPLAY_PATTERN)).collect(Collectors.toList()); |
| 87 | + Asserts.assertFalse(replayFiles.isEmpty(), "Did not find a replay file starting with " + DUMP_REPLAY_PATTERN); |
| 88 | + replayFileName = replayFiles.get(0).getName(); |
| 89 | + } catch (Throwable t) { |
| 90 | + throw new Error("Can't create replay: " + t, t); |
| 91 | + } |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + public int getCompileIdFromFile(String replayFileName) { |
| 96 | + Pattern p = Pattern.compile("replay_pid.*_compid([0-9]+)\\.log"); |
| 97 | + Matcher matcher = p.matcher(replayFileName); |
| 98 | + if (matcher.find()) { |
| 99 | + try { |
| 100 | + return Integer.parseInt(matcher.group(1)); |
| 101 | + } catch (NumberFormatException e) { |
| 102 | + throw new RuntimeException("Could not parse compile id from filename \"" + replayFileName + "\""); |
| 103 | + } |
| 104 | + } else { |
| 105 | + throw new RuntimeException("Could not find compile id in filename \"" + replayFileName + "\""); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public List<File> getReplayFiles() { |
| 110 | + return replayFiles; |
| 111 | + } |
| 112 | +} |
0 commit comments