author | ppunegov |
Thu, 26 Nov 2015 03:05:19 +0300 | |
changeset 34226 | db9dea22fbfc |
parent 34216 | 2818af1ce748 |
child 40631 | ed82623d7831 |
permissions | -rw-r--r-- |
34215 | 1 |
/* |
2 |
* Copyright (c) 2015, 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.OutputAnalyzer; |
|
29 |
import jdk.test.lib.ProcessTools; |
|
30 |
import jdk.test.lib.dcmd.CommandExecutor; |
|
31 |
import jdk.test.lib.dcmd.PidJcmdExecutor; |
|
32 |
||
33 |
import java.io.BufferedReader; |
|
34 |
import java.io.IOException; |
|
35 |
import java.io.InputStreamReader; |
|
36 |
import java.io.PrintWriter; |
|
37 |
import java.lang.reflect.Executable; |
|
38 |
import java.net.ServerSocket; |
|
39 |
import java.net.Socket; |
|
40 |
import java.util.ArrayList; |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
41 |
import java.util.Collections; |
34215 | 42 |
import java.util.List; |
43 |
import java.util.Map; |
|
44 |
||
45 |
public class Executor { |
|
46 |
private final boolean isValid; |
|
47 |
private final List<String> vmOptions; |
|
48 |
private final Map<Executable, State> states; |
|
49 |
private final List<String> jcmdCommands; |
|
34226 | 50 |
private final String execClass = System.getProperty("compiler." |
51 |
+ "compilercontrol.share.executor.executeClass", |
|
52 |
BaseAction.class.getName()); |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
53 |
private OutputAnalyzer[] jcmdOutputAnalyzers; |
34215 | 54 |
|
55 |
/** |
|
56 |
* Constructor |
|
57 |
* |
|
58 |
* @param isValid shows that the input given to the VM is valid and |
|
59 |
* VM shouldn't fail |
|
60 |
* @param vmOptions a list of VM input options |
|
61 |
* @param states a state map, or null for the non-checking execution |
|
62 |
* @param jcmdCommands a list of diagnostic commands to be preformed |
|
63 |
* on test VM |
|
64 |
*/ |
|
65 |
public Executor(boolean isValid, List<String> vmOptions, |
|
66 |
Map<Executable, State> states, List<String> jcmdCommands) { |
|
67 |
this.isValid = isValid; |
|
68 |
if (vmOptions == null) { |
|
69 |
this.vmOptions = new ArrayList<>(); |
|
70 |
} else { |
|
71 |
this.vmOptions = vmOptions; |
|
72 |
} |
|
73 |
this.states = states; |
|
74 |
this.jcmdCommands = jcmdCommands; |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Executes separate VM a gets an OutputAnalyzer instance with the results |
|
79 |
* of execution |
|
80 |
*/ |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
81 |
public List<OutputAnalyzer> execute() { |
34215 | 82 |
// Add class name that would be executed in a separate VM |
34226 | 83 |
vmOptions.add(execClass); |
34215 | 84 |
OutputAnalyzer output; |
85 |
try (ServerSocket serverSocket = new ServerSocket(0)) { |
|
86 |
if (isValid) { |
|
87 |
// Get port test VM will connect to |
|
88 |
int port = serverSocket.getLocalPort(); |
|
89 |
if (port == -1) { |
|
90 |
throw new Error("Socket is not bound: " + port); |
|
91 |
} |
|
92 |
vmOptions.add(String.valueOf(port)); |
|
93 |
if (states != null) { |
|
94 |
// add flag to show that there should be state map passed |
|
95 |
vmOptions.add("states"); |
|
96 |
} |
|
97 |
// Start separate thread to connect with test VM |
|
98 |
new Thread(() -> connectTestVM(serverSocket)).start(); |
|
99 |
} |
|
100 |
// Start test VM |
|
101 |
output = ProcessTools.executeTestJvmAllArgs( |
|
102 |
vmOptions.toArray(new String[vmOptions.size()])); |
|
103 |
} catch (Throwable thr) { |
|
104 |
throw new Error("Execution failed: " + thr.getMessage(), thr); |
|
105 |
} |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
106 |
|
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
107 |
List<OutputAnalyzer> outputList = new ArrayList<>(); |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
108 |
outputList.add(output); |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
109 |
if (jcmdOutputAnalyzers != null) { |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
110 |
Collections.addAll(outputList, jcmdOutputAnalyzers); |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
111 |
} |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
112 |
return outputList; |
34215 | 113 |
} |
114 |
||
115 |
/* |
|
116 |
* Performs connection with a test VM, sends method states and performs |
|
117 |
* JCMD operations on a test VM. |
|
118 |
*/ |
|
119 |
private void connectTestVM(ServerSocket serverSocket) { |
|
120 |
/* |
|
121 |
* There are no way to prove that accept was invoked before we started |
|
122 |
* test VM that connects to this serverSocket. Connection timeout is |
|
123 |
* enough |
|
124 |
*/ |
|
125 |
try ( |
|
126 |
Socket socket = serverSocket.accept(); |
|
127 |
PrintWriter pw = new PrintWriter(socket.getOutputStream(), |
|
128 |
true); |
|
129 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
130 |
socket.getInputStream()))) { |
|
131 |
// Get pid of the executed process |
|
132 |
int pid = Integer.parseInt(in.readLine()); |
|
133 |
Asserts.assertNE(pid, 0, "Got incorrect pid"); |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
134 |
jcmdOutputAnalyzers = executeJCMD(pid); |
34215 | 135 |
if (states != null) { |
136 |
// serialize and send state map |
|
137 |
states.forEach((executable, state) -> { |
|
138 |
pw.println("{"); |
|
139 |
pw.println(executable.toGenericString()); |
|
140 |
pw.println(state.toString()); |
|
141 |
pw.println("}"); |
|
142 |
}); |
|
143 |
} else { |
|
144 |
pw.println(); |
|
145 |
} |
|
146 |
} catch (IOException e) { |
|
147 |
throw new Error("Failed to write data: " + e.getMessage(), e); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
// Executes all diagnostic commands |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
152 |
protected OutputAnalyzer[] executeJCMD(int pid) { |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
153 |
int size = jcmdCommands.size(); |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
154 |
OutputAnalyzer[] outputArray = new OutputAnalyzer[size]; |
34215 | 155 |
CommandExecutor jcmdExecutor = new PidJcmdExecutor(String.valueOf(pid)); |
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
156 |
for (int i = 0; i < size; i++) { |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
157 |
outputArray[i] = jcmdExecutor.execute(jcmdCommands.get(i)); |
34215 | 158 |
} |
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
159 |
return outputArray; |
34215 | 160 |
} |
161 |
} |