Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/ciReplay/CiReplayBase.java
41149 views
1
/*
2
* Copyright (c) 2016, 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 compiler.whitebox.CompilerWhiteBoxTest;
27
import java.io.IOException;
28
import java.io.File;
29
import java.io.BufferedReader;
30
import java.io.FileReader;
31
import java.nio.file.Files;
32
import java.nio.file.Path;
33
import java.nio.file.Paths;
34
import java.nio.file.StandardOpenOption;
35
import java.util.ArrayList;
36
import java.util.Arrays;
37
import java.util.List;
38
import java.util.Optional;
39
import java.util.regex.Pattern;
40
import java.util.regex.Matcher;
41
import jdk.test.lib.Platform;
42
import jdk.test.lib.process.ProcessTools;
43
import jdk.test.lib.process.OutputAnalyzer;
44
import jdk.test.lib.Asserts;
45
import jdk.test.lib.Utils;
46
import jdk.test.lib.util.CoreUtils;
47
48
public abstract class CiReplayBase {
49
public static final String REPLAY_FILE_NAME = "test_replay.txt";
50
public static final boolean CLIENT_VM_AVAILABLE;
51
public static final boolean SERVER_VM_AVAILABLE;
52
public static final String TIERED_ENABLED_VM_OPTION = "-XX:+TieredCompilation";
53
public static final String TIERED_DISABLED_VM_OPTION = "-XX:-TieredCompilation";
54
public static final String ENABLE_COREDUMP_ON_CRASH = "-XX:+CreateCoredumpOnCrash";
55
public static final String DISABLE_COREDUMP_ON_CRASH = "-XX:-CreateCoredumpOnCrash";
56
public static final String CLIENT_VM_OPTION = "-client";
57
public static final String SERVER_VM_OPTION = "-server";
58
public static final String TEST_CORE_FILE_NAME = "test_core";
59
public static final String RUN_SHELL_NO_LIMIT = "ulimit -c unlimited && ";
60
private static final String REPLAY_FILE_OPTION = "-XX:ReplayDataFile=" + REPLAY_FILE_NAME;
61
private static final String LOCATIONS_STRING = "location: ";
62
private static final String HS_ERR_NAME = "hs_err_pid";
63
private static final String RUN_SHELL_ZERO_LIMIT = "ulimit -S -c 0 && ";
64
private static final String VERSION_OPTION = "-version";
65
private static final String[] REPLAY_GENERATION_OPTIONS = new String[]{"-Xms128m", "-Xmx128m",
66
"-XX:MetaspaceSize=4m", "-XX:MaxMetaspaceSize=16m", "-XX:InitialCodeCacheSize=512k",
67
"-XX:ReservedCodeCacheSize=4m", "-XX:ThreadStackSize=512", "-XX:VMThreadStackSize=512",
68
"-XX:CompilerThreadStackSize=512", "-XX:ParallelGCThreads=1", "-XX:CICompilerCount=2",
69
"-XX:-BackgroundCompilation", "-XX:CompileCommand=inline,java.io.PrintStream::*",
70
"-XX:+IgnoreUnrecognizedVMOptions", "-XX:TypeProfileLevel=222", // extra profile data as a stress test
71
"-XX:CICrashAt=1", "-XX:+DumpReplayDataOnError",
72
"-XX:+PreferInterpreterNativeStubs", REPLAY_FILE_OPTION};
73
private static final String[] REPLAY_OPTIONS = new String[]{DISABLE_COREDUMP_ON_CRASH,
74
"-XX:+IgnoreUnrecognizedVMOptions", "-XX:TypeProfileLevel=222",
75
"-XX:+ReplayCompiles", REPLAY_FILE_OPTION};
76
protected final Optional<Boolean> runServer;
77
private static int dummy;
78
79
public static class TestMain {
80
public static void main(String[] args) {
81
for (int i = 0; i < 20_000; i++) {
82
test(i);
83
}
84
}
85
86
static void test(int i) {
87
if ((i % 1000) == 0) {
88
System.out.println("Hello World!");
89
}
90
}
91
}
92
93
static {
94
try {
95
CLIENT_VM_AVAILABLE = ProcessTools.executeTestJvm(CLIENT_VM_OPTION, VERSION_OPTION)
96
.getOutput().contains("Client");
97
SERVER_VM_AVAILABLE = ProcessTools.executeTestJvm(SERVER_VM_OPTION, VERSION_OPTION)
98
.getOutput().contains("Server");
99
} catch(Throwable t) {
100
throw new Error("Initialization failed: " + t, t);
101
}
102
}
103
104
public CiReplayBase() {
105
runServer = Optional.empty();
106
}
107
108
public CiReplayBase(String args[]) {
109
if (args.length != 1 || (!"server".equals(args[0]) && !"client".equals(args[0]))) {
110
throw new Error("Expected 1 argument: [server|client]");
111
}
112
runServer = Optional.of("server".equals(args[0]));
113
}
114
115
public void runTest(boolean needCoreDump, String... args) {
116
cleanup();
117
if (generateReplay(needCoreDump, args)) {
118
testAction();
119
cleanup();
120
} else {
121
throw new Error("Host is not configured to generate cores");
122
}
123
}
124
125
public abstract void testAction();
126
127
private static void remove(String item) {
128
File toDelete = new File(item);
129
toDelete.delete();
130
if (Platform.isWindows()) {
131
Utils.waitForCondition(() -> !toDelete.exists());
132
}
133
}
134
135
private static void removeFromCurrentDirectoryStartingWith(String prefix) {
136
Arrays.stream(new File(".").listFiles())
137
.filter(f -> f.getName().startsWith(prefix))
138
.forEach(File::delete);
139
}
140
141
public static void cleanup() {
142
removeFromCurrentDirectoryStartingWith("core");
143
removeFromCurrentDirectoryStartingWith("replay");
144
removeFromCurrentDirectoryStartingWith(HS_ERR_NAME);
145
remove(TEST_CORE_FILE_NAME);
146
remove(REPLAY_FILE_NAME);
147
}
148
149
public boolean generateReplay(boolean needCoreDump, String... vmopts) {
150
OutputAnalyzer crashOut;
151
String crashOutputString;
152
try {
153
List<String> options = new ArrayList<>();
154
options.addAll(Arrays.asList(REPLAY_GENERATION_OPTIONS));
155
options.addAll(Arrays.asList(vmopts));
156
options.add(needCoreDump ? ENABLE_COREDUMP_ON_CRASH : DISABLE_COREDUMP_ON_CRASH);
157
if (needCoreDump) {
158
// CiReplayBase$TestMain needs to be quoted because of shell eval
159
options.add("-XX:CompileOnly='" + TestMain.class.getName() + "::test'");
160
options.add("'" + TestMain.class.getName() + "'");
161
crashOut = ProcessTools.executeProcess(
162
CoreUtils.addCoreUlimitCommand(
163
ProcessTools.createTestJvm(options.toArray(new String[0]))));
164
} else {
165
options.add("-XX:CompileOnly=" + TestMain.class.getName() + "::test");
166
options.add(TestMain.class.getName());
167
crashOut = ProcessTools.executeProcess(ProcessTools.createTestJvm(options));
168
}
169
crashOutputString = crashOut.getOutput();
170
Asserts.assertNotEquals(crashOut.getExitValue(), 0, "Crash JVM exits gracefully");
171
Files.write(Paths.get("crash.out"), crashOutputString.getBytes(),
172
StandardOpenOption.CREATE, StandardOpenOption.WRITE,
173
StandardOpenOption.TRUNCATE_EXISTING);
174
} catch (Throwable t) {
175
throw new Error("Can't create replay: " + t, t);
176
}
177
if (needCoreDump) {
178
try {
179
String coreFileLocation = CoreUtils.getCoreFileLocation(crashOutputString, crashOut.pid());
180
Files.move(Paths.get(coreFileLocation), Paths.get(TEST_CORE_FILE_NAME));
181
} catch (IOException ioe) {
182
throw new Error("Can't move core file: " + ioe, ioe);
183
}
184
}
185
removeFromCurrentDirectoryStartingWith(HS_ERR_NAME);
186
return true;
187
}
188
189
public void commonTests() {
190
positiveTest();
191
if (Platform.isTieredSupported()) {
192
positiveTest(TIERED_ENABLED_VM_OPTION);
193
}
194
}
195
196
public int startTest(String... additionalVmOpts) {
197
try {
198
List<String> allAdditionalOpts = new ArrayList<>();
199
allAdditionalOpts.addAll(Arrays.asList(REPLAY_OPTIONS));
200
allAdditionalOpts.addAll(Arrays.asList(additionalVmOpts));
201
OutputAnalyzer oa = ProcessTools.executeProcess(getTestJvmCommandlineWithPrefix(
202
RUN_SHELL_ZERO_LIMIT, allAdditionalOpts.toArray(new String[0])));
203
return oa.getExitValue();
204
} catch (Throwable t) {
205
throw new Error("Can't run replay process: " + t, t);
206
}
207
}
208
209
public void runVmTests() {
210
boolean runServerValue = runServer.orElseThrow(() -> new Error("runServer must be set"));
211
if (runServerValue) {
212
if (CLIENT_VM_AVAILABLE) {
213
negativeTest(CLIENT_VM_OPTION);
214
}
215
} else {
216
if (SERVER_VM_AVAILABLE) {
217
negativeTest(TIERED_DISABLED_VM_OPTION, SERVER_VM_OPTION);
218
if (Platform.isTieredSupported()) {
219
positiveTest(TIERED_ENABLED_VM_OPTION, SERVER_VM_OPTION);
220
}
221
}
222
}
223
nonTieredTests(runServerValue ? CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION
224
: CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);
225
}
226
227
public int getCompLevelFromReplay() {
228
try(BufferedReader br = new BufferedReader(new FileReader(REPLAY_FILE_NAME))) {
229
return br.lines()
230
.filter(s -> s.startsWith("compile "))
231
.map(s -> s.split("\\s+")[5])
232
.map(Integer::parseInt)
233
.findAny()
234
.get();
235
} catch (IOException ioe) {
236
throw new Error("Failed to read replay data: " + ioe, ioe);
237
}
238
}
239
240
public void positiveTest(String... additionalVmOpts) {
241
Asserts.assertEQ(startTest(additionalVmOpts), 0, "Unexpected exit code for positive case: "
242
+ Arrays.toString(additionalVmOpts));
243
}
244
245
public void negativeTest(String... additionalVmOpts) {
246
Asserts.assertNE(startTest(additionalVmOpts), 0, "Unexpected exit code for negative case: "
247
+ Arrays.toString(additionalVmOpts));
248
}
249
250
public void nonTieredTests(int compLevel) {
251
int replayDataCompLevel = getCompLevelFromReplay();
252
if (replayDataCompLevel == compLevel) {
253
positiveTest(TIERED_DISABLED_VM_OPTION);
254
} else {
255
negativeTest(TIERED_DISABLED_VM_OPTION);
256
}
257
}
258
259
private String[] getTestJvmCommandlineWithPrefix(String prefix, String... args) {
260
try {
261
String cmd = ProcessTools.getCommandLine(ProcessTools.createTestJvm(args));
262
return new String[]{"sh", "-c", prefix
263
+ (Platform.isWindows() ? cmd.replace('\\', '/').replace(";", "\\;").replace("|", "\\|") : cmd)};
264
} catch(Throwable t) {
265
throw new Error("Can't create process builder: " + t, t);
266
}
267
}
268
}
269
270