Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/Executor.java
41161 views
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
24
package compiler.compilercontrol.share.scenario;
25
26
import compiler.compilercontrol.share.actions.BaseAction;
27
import jdk.test.lib.Asserts;
28
import jdk.test.lib.management.InputArguments;
29
import jdk.test.lib.process.OutputAnalyzer;
30
import jdk.test.lib.process.ProcessTools;
31
import jdk.test.lib.dcmd.CommandExecutor;
32
import jdk.test.lib.dcmd.PidJcmdExecutor;
33
34
import java.io.BufferedReader;
35
import java.io.IOException;
36
import java.io.InputStreamReader;
37
import java.io.PrintWriter;
38
import java.lang.reflect.Executable;
39
import java.net.ServerSocket;
40
import java.net.Socket;
41
import java.util.ArrayList;
42
import java.util.Arrays;
43
import java.util.Collections;
44
import java.util.List;
45
import java.util.Map;
46
47
public class Executor {
48
private final boolean isValid;
49
private final List<String> vmOptions;
50
private final Map<Executable, State> states;
51
private final List<String> jcmdCommands;
52
private final String execClass = System.getProperty("compiler."
53
+ "compilercontrol.share.executor.executeClass",
54
BaseAction.class.getName());
55
private OutputAnalyzer[] jcmdOutputAnalyzers;
56
57
/**
58
* Constructor
59
*
60
* @param isValid shows that the input given to the VM is valid and
61
* VM shouldn't fail
62
* @param vmOptions a list of VM input options
63
* @param states a state map, or null for the non-checking execution
64
* @param jcmdCommands a list of diagnostic commands to be preformed
65
* on test VM
66
*/
67
public Executor(boolean isValid, List<String> vmOptions,
68
Map<Executable, State> states, List<String> jcmdCommands) {
69
this.isValid = isValid;
70
if (vmOptions == null) {
71
this.vmOptions = new ArrayList<>();
72
} else {
73
this.vmOptions = vmOptions;
74
}
75
this.states = states;
76
this.jcmdCommands = jcmdCommands;
77
}
78
79
/**
80
* Executes separate VM a gets an OutputAnalyzer instance with the results
81
* of execution
82
*/
83
public List<OutputAnalyzer> execute() {
84
// Add class name that would be executed in a separate VM
85
vmOptions.add(execClass);
86
OutputAnalyzer output;
87
try (ServerSocket serverSocket = new ServerSocket(0)) {
88
{
89
// Get port test VM will connect to
90
int port = serverSocket.getLocalPort();
91
if (port == -1) {
92
throw new Error("Socket is not bound: " + port);
93
}
94
vmOptions.add(String.valueOf(port));
95
if (states != null) {
96
// add flag to show that there should be state map passed
97
vmOptions.add("states");
98
}
99
// Start separate thread to connect with test VM
100
new Thread(() -> connectTestVM(serverSocket)).start();
101
}
102
// Start a test VM using vm flags from @run and from vm options
103
String[] vmInputArgs = InputArguments.getVmInputArgs();
104
String[] cmds = Arrays.copyOf(vmInputArgs,
105
vmInputArgs.length + vmOptions.size());
106
System.arraycopy(vmOptions.toArray(), 0, cmds, vmInputArgs.length,
107
vmOptions.size());
108
output = ProcessTools.executeTestJvm(cmds);
109
} catch (Throwable thr) {
110
throw new Error("Execution failed: " + thr.getMessage(), thr);
111
}
112
113
List<OutputAnalyzer> outputList = new ArrayList<>();
114
outputList.add(output);
115
if (jcmdOutputAnalyzers != null) {
116
Collections.addAll(outputList, jcmdOutputAnalyzers);
117
}
118
return outputList;
119
}
120
121
/*
122
* Performs connection with a test VM, sends method states and performs
123
* JCMD operations on a test VM.
124
*/
125
private void connectTestVM(ServerSocket serverSocket) {
126
/*
127
* There are no way to prove that accept was invoked before we started
128
* test VM that connects to this serverSocket. Connection timeout is
129
* enough
130
*/
131
try (
132
Socket socket = serverSocket.accept();
133
PrintWriter pw = new PrintWriter(socket.getOutputStream(),
134
true);
135
BufferedReader in = new BufferedReader(new InputStreamReader(
136
socket.getInputStream()))) {
137
// Get pid of the executed process
138
int pid = Integer.parseInt(in.readLine());
139
Asserts.assertNE(pid, 0, "Got incorrect pid");
140
jcmdOutputAnalyzers = executeJCMD(pid);
141
if (states != null) {
142
// serialize and send state map
143
states.forEach((executable, state) -> {
144
pw.println("{");
145
pw.println(executable.toGenericString());
146
pw.println(state.toString());
147
pw.println("}");
148
});
149
} else {
150
pw.println();
151
}
152
} catch (IOException e) {
153
// hotspot process may exit because of invalid directives,
154
// suppress the IOException of closed socket.
155
if (!e.getMessage().equals("Socket closed")) {
156
throw new Error("Failed to write data: " + e.getMessage(), e);
157
}
158
}
159
}
160
161
// Executes all diagnostic commands
162
protected OutputAnalyzer[] executeJCMD(int pid) {
163
int size = jcmdCommands.size();
164
OutputAnalyzer[] outputArray = new OutputAnalyzer[size];
165
CommandExecutor jcmdExecutor = new PidJcmdExecutor(String.valueOf(pid));
166
for (int i = 0; i < size; i++) {
167
outputArray[i] = jcmdExecutor.execute(jcmdCommands.get(i));
168
}
169
return outputArray;
170
}
171
}
172
173