author | ykantser |
Tue, 09 Jun 2015 10:11:11 +0200 | |
changeset 31454 | 4dba1183dc2f |
parent 30326 | 5fc89c43c103 |
child 32474 | 5b41b53a4997 |
permissions | -rw-r--r-- |
25987 | 1 |
/* |
31454
4dba1183dc2f
8085813: The targeted processes in sun/tools tests should be launched with -XX:+UsePerfData flag in order to work on embedded platforms
ykantser
parents:
30326
diff
changeset
|
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
25987 | 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 |
import static jdk.testlibrary.Asserts.assertGreaterThan; |
|
25 |
import static jdk.testlibrary.Asserts.assertTrue; |
|
26 |
||
27 |
import java.io.BufferedWriter; |
|
28 |
import java.io.File; |
|
29 |
import java.io.FileWriter; |
|
30 |
import java.io.IOException; |
|
28412 | 31 |
import java.nio.file.Files; |
32 |
import java.nio.file.Path; |
|
33 |
import java.nio.file.Paths; |
|
25987 | 34 |
import java.util.ArrayList; |
35 |
import java.util.Arrays; |
|
36 |
import java.util.List; |
|
37 |
||
38 |
import jdk.testlibrary.Asserts; |
|
39 |
import jdk.testlibrary.JDKToolLauncher; |
|
40 |
import jdk.testlibrary.OutputAnalyzer; |
|
41 |
import jdk.testlibrary.Utils; |
|
27189
b90845965ee9
8056143: interrupted java/lang/management/MemoryMXBean/LowMemoryTest.java leaves running process
jbachorik
parents:
25987
diff
changeset
|
42 |
import jdk.testlibrary.ProcessTools; |
25987 | 43 |
|
44 |
/** |
|
45 |
* The helper class for running jps utility and verifying output from it |
|
46 |
*/ |
|
47 |
public final class JpsHelper { |
|
48 |
||
49 |
/** |
|
50 |
* Helper class for handling jps arguments |
|
51 |
*/ |
|
52 |
public enum JpsArg { |
|
53 |
q, |
|
54 |
l, |
|
55 |
m, |
|
56 |
v, |
|
57 |
V; |
|
58 |
||
59 |
/** |
|
60 |
* Generate all possible combinations of {@link JpsArg} |
|
61 |
* (31 argument combinations and no arguments case) |
|
62 |
*/ |
|
63 |
public static List<List<JpsArg>> generateCombinations() { |
|
64 |
final int argCount = JpsArg.values().length; |
|
65 |
// If there are more than 30 args this algorithm will overflow. |
|
66 |
Asserts.assertLessThan(argCount, 31, "Too many args"); |
|
67 |
||
68 |
List<List<JpsArg>> combinations = new ArrayList<>(); |
|
69 |
int combinationCount = (int) Math.pow(2, argCount); |
|
70 |
for (int currCombo = 0; currCombo < combinationCount; ++currCombo) { |
|
71 |
List<JpsArg> combination = new ArrayList<>(); |
|
72 |
for (int position = 0; position < argCount; ++position) { |
|
73 |
int bit = 1 << position; |
|
74 |
if ((bit & currCombo) != 0) { |
|
75 |
combination.add(JpsArg.values()[position]); |
|
76 |
} |
|
77 |
} |
|
78 |
combinations.add(combination); |
|
79 |
} |
|
80 |
return combinations; |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Return combination of {@link JpsArg} as a String array |
|
85 |
*/ |
|
86 |
public static String[] asCmdArray(List<JpsArg> jpsArgs) { |
|
87 |
List<String> list = new ArrayList<>(); |
|
88 |
for (JpsArg jpsArg : jpsArgs) { |
|
89 |
list.add("-" + jpsArg.toString()); |
|
90 |
} |
|
91 |
return list.toArray(new String[list.size()]); |
|
92 |
} |
|
93 |
||
94 |
} |
|
95 |
||
96 |
/** |
|
31454
4dba1183dc2f
8085813: The targeted processes in sun/tools tests should be launched with -XX:+UsePerfData flag in order to work on embedded platforms
ykantser
parents:
30326
diff
changeset
|
97 |
* VM arguments to start test application with. |
4dba1183dc2f
8085813: The targeted processes in sun/tools tests should be launched with -XX:+UsePerfData flag in order to work on embedded platforms
ykantser
parents:
30326
diff
changeset
|
98 |
* -XX:+UsePerfData is required for running the tests on embedded platforms. |
25987 | 99 |
*/ |
31454
4dba1183dc2f
8085813: The targeted processes in sun/tools tests should be launched with -XX:+UsePerfData flag in order to work on embedded platforms
ykantser
parents:
30326
diff
changeset
|
100 |
public static final String[] VM_ARGS = {"-XX:+UsePerfData", "-Xmx512m", "-XX:+PrintGCDetails"}; |
25987 | 101 |
/** |
102 |
* VM flag to start test application with |
|
103 |
*/ |
|
104 |
public static final String VM_FLAG = "+DisableExplicitGC"; |
|
105 |
||
106 |
private static File vmFlagsFile = null; |
|
107 |
private static List<String> testVmArgs = null; |
|
108 |
private static File manifestFile = null; |
|
109 |
||
110 |
/** |
|
111 |
* Create a file containing VM_FLAG in the working directory |
|
112 |
*/ |
|
113 |
public static File getVmFlagsFile() throws IOException { |
|
114 |
if (vmFlagsFile == null) { |
|
115 |
vmFlagsFile = new File("vmflags"); |
|
116 |
try (BufferedWriter output = new BufferedWriter(new FileWriter(vmFlagsFile))) { |
|
117 |
output.write(VM_FLAG); |
|
118 |
} |
|
119 |
vmFlagsFile.deleteOnExit(); |
|
120 |
} |
|
121 |
return vmFlagsFile; |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Return a list of VM arguments |
|
126 |
*/ |
|
127 |
public static List<String> getVmArgs() throws IOException { |
|
128 |
if (testVmArgs == null) { |
|
129 |
testVmArgs = new ArrayList<>(); |
|
130 |
testVmArgs.addAll(Arrays.asList(VM_ARGS)); |
|
131 |
testVmArgs.add("-XX:Flags=" + getVmFlagsFile().getAbsolutePath()); |
|
132 |
} |
|
133 |
return testVmArgs; |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Start jps utility without any arguments |
|
138 |
*/ |
|
139 |
public static OutputAnalyzer jps() throws Exception { |
|
140 |
return jps(null, null); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Start jps utility with tool arguments |
|
145 |
*/ |
|
146 |
public static OutputAnalyzer jps(String... toolArgs) throws Exception { |
|
147 |
return jps(null, Arrays.asList(toolArgs)); |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Start jps utility with VM args and tool arguments |
|
152 |
*/ |
|
153 |
public static OutputAnalyzer jps(List<String> vmArgs, List<String> toolArgs) throws Exception { |
|
154 |
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jps"); |
|
30326
5fc89c43c103
8064923: [TESTBUG] jps doesn't display anything on embedded platforms and it causes some tests to fail
ykantser
parents:
28412
diff
changeset
|
155 |
launcher.addVMArg("-XX:+UsePerfData"); |
25987 | 156 |
if (vmArgs != null) { |
157 |
for (String vmArg : vmArgs) { |
|
158 |
launcher.addVMArg(vmArg); |
|
159 |
} |
|
160 |
} |
|
161 |
if (toolArgs != null) { |
|
162 |
for (String toolArg : toolArgs) { |
|
163 |
launcher.addToolArg(toolArg); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); |
|
168 |
System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); |
|
27189
b90845965ee9
8056143: interrupted java/lang/management/MemoryMXBean/LowMemoryTest.java leaves running process
jbachorik
parents:
25987
diff
changeset
|
169 |
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); |
25987 | 170 |
System.out.println(output.getOutput()); |
171 |
||
172 |
return output; |
|
173 |
} |
|
174 |
||
175 |
/** |
|
28098
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
176 |
* Verify jps stdout contains only pids and programs' name information. |
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
177 |
* jps stderr may contain VM warning messages which will be ignored. |
25987 | 178 |
* |
179 |
* The output can look like: |
|
180 |
* 35536 Jps |
|
181 |
* 35417 Main |
|
182 |
* 31103 org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar |
|
183 |
*/ |
|
184 |
public static void verifyJpsOutput(OutputAnalyzer output, String regex) throws Exception { |
|
185 |
output.shouldHaveExitValue(0); |
|
28098
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
186 |
int matchedCount = output.stdoutShouldMatchByLine(regex); |
25987 | 187 |
assertGreaterThan(matchedCount , 0, "Found no lines matching pattern: " + regex); |
28098
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
188 |
output.stderrShouldNotMatch("[E|e]xception"); |
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
189 |
output.stderrShouldNotMatch("[E|e]rror"); |
25987 | 190 |
} |
191 |
||
192 |
/** |
|
193 |
* Compare jps output with a content in a file line by line |
|
194 |
*/ |
|
195 |
public static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException { |
|
196 |
String testSrc = System.getProperty("test.src", "?"); |
|
28412 | 197 |
Path path = Paths.get(testSrc, "usage.out"); |
198 |
List<String> fileOutput = Files.readAllLines(path); |
|
25987 | 199 |
List<String> outputAsLines = output.asLines(); |
200 |
assertTrue(outputAsLines.containsAll(fileOutput), |
|
28412 | 201 |
"The ouput should contain all content of " + path.toAbsolutePath()); |
25987 | 202 |
} |
203 |
||
204 |
private static File getManifest(String className) throws IOException { |
|
205 |
if (manifestFile == null) { |
|
206 |
manifestFile = new File(className + ".mf"); |
|
207 |
try (BufferedWriter output = new BufferedWriter(new FileWriter(manifestFile))) { |
|
208 |
output.write("Main-Class: " + className + Utils.NEW_LINE); |
|
209 |
} |
|
210 |
} |
|
211 |
return manifestFile; |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Build a jar of test classes in runtime |
|
216 |
*/ |
|
217 |
public static File buildJar(String className) throws Exception { |
|
218 |
File jar = new File(className + ".jar"); |
|
219 |
||
220 |
List<String> jarArgs = new ArrayList<>(); |
|
221 |
jarArgs.add("-cfm"); |
|
222 |
jarArgs.add(jar.getAbsolutePath()); |
|
223 |
File manifestFile = getManifest(className); |
|
224 |
jarArgs.add(manifestFile.getAbsolutePath()); |
|
225 |
String testClassPath = System.getProperty("test.class.path", "?"); |
|
226 |
for (String path : testClassPath.split(File.pathSeparator)) { |
|
227 |
jarArgs.add("-C"); |
|
228 |
jarArgs.add(path); |
|
229 |
jarArgs.add("."); |
|
230 |
} |
|
231 |
||
232 |
System.out.println("Running jar " + jarArgs.toString()); |
|
233 |
sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); |
|
234 |
if (!jarTool.run(jarArgs.toArray(new String[jarArgs.size()]))) { |
|
235 |
throw new Exception("jar failed: args=" + jarArgs.toString()); |
|
236 |
} |
|
237 |
||
238 |
manifestFile.delete(); |
|
239 |
jar.deleteOnExit(); |
|
240 |
||
241 |
return jar; |
|
242 |
} |
|
243 |
||
244 |
} |