author | iignatyev |
Wed, 12 Apr 2017 19:28:47 -0700 | |
changeset 44584 | 8cd22f56bfca |
parent 40631 | ed82623d7831 |
permissions | -rw-r--r-- |
34215 | 1 |
/* |
44584
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
34215 | 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; |
|
44584
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
28 |
import jdk.test.lib.management.InputArguments; |
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
34226
diff
changeset
|
29 |
import jdk.test.lib.process.OutputAnalyzer; |
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
34226
diff
changeset
|
30 |
import jdk.test.lib.process.ProcessTools; |
34215 | 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; |
|
44584
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
42 |
import java.util.Arrays; |
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
43 |
import java.util.Collections; |
34215 | 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; |
|
34226 | 52 |
private final String execClass = System.getProperty("compiler." |
53 |
+ "compilercontrol.share.executor.executeClass", |
|
54 |
BaseAction.class.getName()); |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
55 |
private OutputAnalyzer[] jcmdOutputAnalyzers; |
34215 | 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 |
*/ |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
83 |
public List<OutputAnalyzer> execute() { |
34215 | 84 |
// Add class name that would be executed in a separate VM |
34226 | 85 |
vmOptions.add(execClass); |
34215 | 86 |
OutputAnalyzer output; |
87 |
try (ServerSocket serverSocket = new ServerSocket(0)) { |
|
88 |
if (isValid) { |
|
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 |
} |
|
44584
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
102 |
// Start a test VM using vm flags from @run and from vm options |
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
103 |
String[] vmInputArgs = InputArguments.getVmInputArgs(); |
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
104 |
String[] cmds = Arrays.copyOf(vmInputArgs, |
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
105 |
vmInputArgs.length + vmOptions.size()); |
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
106 |
System.arraycopy(vmOptions.toArray(), 0, cmds, vmInputArgs.length, |
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
107 |
vmOptions.size()); |
8cd22f56bfca
8164944: Refactor ProcessTools to get rid of dependency on java.management
iignatyev
parents:
40631
diff
changeset
|
108 |
output = ProcessTools.executeTestJvm(cmds); |
34215 | 109 |
} catch (Throwable thr) { |
110 |
throw new Error("Execution failed: " + thr.getMessage(), thr); |
|
111 |
} |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
112 |
|
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
113 |
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
|
114 |
outputList.add(output); |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
115 |
if (jcmdOutputAnalyzers != null) { |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
116 |
Collections.addAll(outputList, jcmdOutputAnalyzers); |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
117 |
} |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
118 |
return outputList; |
34215 | 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"); |
|
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
140 |
jcmdOutputAnalyzers = executeJCMD(pid); |
34215 | 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 |
throw new Error("Failed to write data: " + e.getMessage(), e); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
// 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
|
158 |
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
|
159 |
int size = jcmdCommands.size(); |
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
160 |
OutputAnalyzer[] outputArray = new OutputAnalyzer[size]; |
34215 | 161 |
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
|
162 |
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
|
163 |
outputArray[i] = jcmdExecutor.execute(jcmdCommands.get(i)); |
34215 | 164 |
} |
34216
2818af1ce748
8138993: JEP-JDK-8046155: Test task: add check for Compiler.directives_print diagnostic command
ppunegov
parents:
34215
diff
changeset
|
165 |
return outputArray; |
34215 | 166 |
} |
167 |
} |