24 package jdk.testlibrary; |
24 package jdk.testlibrary; |
25 |
25 |
26 import java.util.Arrays; |
26 import java.util.Arrays; |
27 |
27 |
28 /** |
28 /** |
29 * Super class for tests which need to attach jcmd to the current process. |
29 * Helper class for starting jcmd process. |
|
30 * <pre> |
|
31 * - jcmd will send diagnostic requests to the current java process: |
|
32 * jcmd pid_to_current_process PerfCounter.print |
|
33 * - jcmd will be run without sending request to any JVM |
|
34 * jcmd -h |
|
35 * </pre> |
30 */ |
36 */ |
31 public class JcmdBase { |
37 public final class JcmdBase { |
32 |
38 |
33 private static ProcessBuilder processBuilder = new ProcessBuilder(); |
39 private static ProcessBuilder processBuilder = new ProcessBuilder(); |
34 |
40 |
|
41 private JcmdBase() { |
|
42 // Private constructor to prevent class instantiation |
|
43 } |
|
44 |
35 /** |
45 /** |
36 * Attach jcmd to the current process |
46 * Sends the diagnostic command request to the current process |
37 * |
47 * |
38 * @param toolArgs |
48 * @see #jcmd(boolean, String[], String[]) |
39 * jcmd command line parameters, e.g. VM.flags |
49 */ |
40 * @return jcmd output |
50 public final static OutputAnalyzer jcmd(String... jcmdArgs) |
|
51 throws Exception { |
|
52 return jcmd(true, null, jcmdArgs); |
|
53 } |
|
54 |
|
55 /** |
|
56 * Sends the diagnostic command request to the current process. |
|
57 * jcmd will be run with specified {@code vmArgs}. |
|
58 * |
|
59 * @see #jcmd(boolean, String[], String[]) |
|
60 */ |
|
61 public final static OutputAnalyzer jcmd(String[] vmArgs, |
|
62 String[] jcmdArgs) throws Exception { |
|
63 return jcmd(true, vmArgs, jcmdArgs); |
|
64 } |
|
65 |
|
66 /** |
|
67 * Runs jcmd without sending request to any JVM |
|
68 * |
|
69 * @see #jcmd(boolean, String[], String[]) |
|
70 */ |
|
71 public final static OutputAnalyzer jcmdNoPid(String[] vmArgs, |
|
72 String[] jcmdArgs) throws Exception { |
|
73 return jcmd(false, vmArgs, jcmdArgs); |
|
74 } |
|
75 |
|
76 /** |
|
77 * If {@code requestToCurrentProcess} is {@code true} |
|
78 * sends a diagnostic command request to the current process. |
|
79 * If {@code requestToCurrentProcess} is {@code false} |
|
80 * runs jcmd without sending request to any JVM. |
|
81 * |
|
82 * @param requestToCurrentProcess |
|
83 * Defines if jcmd will send request to the current process |
|
84 * @param vmArgs |
|
85 * jcmd will be run with VM arguments specified, |
|
86 * e.g. -XX:+UsePerfData |
|
87 * @param jcmdArgs |
|
88 * jcmd will be run with option or command and its arguments |
|
89 * specified, e.g. VM.flags |
|
90 * @return The output from {@link OutputAnalyzer} object |
41 * @throws Exception |
91 * @throws Exception |
42 */ |
92 */ |
43 public final static OutputAnalyzer jcmd(String... toolArgs) |
93 private static final OutputAnalyzer jcmd(boolean requestToCurrentProcess, |
44 throws Exception { |
94 String[] vmArgs, String[] jcmdArgs) throws Exception { |
45 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jcmd"); |
95 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jcmd"); |
46 launcher.addToolArg(Integer.toString(ProcessTools.getProcessId())); |
96 if (vmArgs != null) { |
47 for (String toolArg : toolArgs) { |
97 for (String vmArg : vmArgs) { |
48 launcher.addToolArg(toolArg); |
98 launcher.addVMArg(vmArg); |
|
99 } |
|
100 } |
|
101 if (requestToCurrentProcess) { |
|
102 launcher.addToolArg(Integer.toString(ProcessTools.getProcessId())); |
|
103 } |
|
104 if (jcmdArgs != null) { |
|
105 for (String toolArg : jcmdArgs) { |
|
106 launcher.addToolArg(toolArg); |
|
107 } |
49 } |
108 } |
50 processBuilder.command(launcher.getCommand()); |
109 processBuilder.command(launcher.getCommand()); |
51 System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); |
110 System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); |
52 OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); |
111 OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); |
53 System.out.println(output.getOutput()); |
112 System.out.println(output.getOutput()); |
54 |
113 |
55 output.shouldHaveExitValue(0); |
|
56 |
|
57 return output; |
114 return output; |
58 } |
115 } |
59 |
116 |
60 } |
117 } |