1 /* 2 * Copyright (c) 2013, 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 jdk.testlibrary; 25 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.io.OutputStream; 29 import java.io.PrintStream; 30 import java.lang.reflect.Field; 31 import java.lang.reflect.Method; 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.concurrent.CountDownLatch; 36 import java.util.Map; 37 import java.util.concurrent.ExecutionException; 38 import java.util.concurrent.Future; 39 import java.util.concurrent.TimeUnit; 40 import java.util.concurrent.TimeoutException; 41 import java.util.function.Predicate; 42 import java.util.function.Consumer; 43 import java.util.stream.Collectors; 44 45 public final class ProcessTools { 46 private static final class LineForwarder extends StreamPumper.LinePump { 47 private final PrintStream ps; 48 private final String prefix; 49 LineForwarder(String prefix, PrintStream os) { 50 this.ps = os; 51 this.prefix = prefix; 52 } 53 @Override 54 protected void processLine(String line) { 55 ps.println("[" + prefix + "] " + line); 56 } 57 } 58 59 private ProcessTools() { 60 } 61 62 /** 63 * <p>Starts a process from its builder.</p> 64 * <span>The default redirects of STDOUT and STDERR are started</span> 65 * @param name The process name 66 * @param processBuilder The process builder 67 * @return Returns the initialized process 68 * @throws IOException 69 */ 70 public static Process startProcess(String name, 71 ProcessBuilder processBuilder) 72 throws IOException { 73 return startProcess(name, processBuilder, (Consumer<String>)null); 74 } 75 76 /** 77 * <p>Starts a process from its builder.</p> 78 * <span>The default redirects of STDOUT and STDERR are started</span> 79 * <p>It is possible to monitor the in-streams via the provided {@code consumer} 80 * @param name The process name 81 * @param consumer {@linkplain Consumer} instance to process the in-streams 82 * @param processBuilder The process builder 83 * @return Returns the initialized process 84 * @throws IOException 85 */ 86 @SuppressWarnings("overloads") 87 public static Process startProcess(String name, 88 ProcessBuilder processBuilder, 89 Consumer<String> consumer) 90 throws IOException { 91 try { 92 return startProcess(name, processBuilder, consumer, null, -1, TimeUnit.NANOSECONDS); 93 } catch (InterruptedException | TimeoutException e) { 94 // will never happen 95 throw new RuntimeException(e); 96 } 97 } 98 99 /** 100 * <p>Starts a process from its builder.</p> 101 * <span>The default redirects of STDOUT and STDERR are started</span> 102 * <p> 103 * It is possible to wait for the process to get to a warmed-up state 104 * via {@linkplain Predicate} condition on the STDOUT 105 * </p> 106 * @param name The process name 107 * @param processBuilder The process builder 108 * @param linePredicate The {@linkplain Predicate} to use on the STDOUT 109 * Used to determine the moment the target app is 110 * properly warmed-up. 111 * It can be null - in that case the warmup is skipped. 112 * @param timeout The timeout for the warmup waiting; -1 = no wait; 0 = wait forever 113 * @param unit The timeout {@linkplain TimeUnit} 114 * @return Returns the initialized {@linkplain Process} 115 * @throws IOException 116 * @throws InterruptedException 117 * @throws TimeoutException 118 */ 119 public static Process startProcess(String name, 120 ProcessBuilder processBuilder, 121 final Predicate<String> linePredicate, 122 long timeout, 123 TimeUnit unit) 124 throws IOException, InterruptedException, TimeoutException { 125 return startProcess(name, processBuilder, null, linePredicate, timeout, unit); 126 } 127 128 /** 129 * <p>Starts a process from its builder.</p> 130 * <span>The default redirects of STDOUT and STDERR are started</span> 131 * <p> 132 * It is possible to wait for the process to get to a warmed-up state 133 * via {@linkplain Predicate} condition on the STDOUT and monitor the 134 * in-streams via the provided {@linkplain Consumer} 135 * </p> 136 * @param name The process name 137 * @param processBuilder The process builder 138 * @param lineConsumer The {@linkplain Consumer} the lines will be forwarded to 139 * @param linePredicate The {@linkplain Predicate} to use on the STDOUT 140 * Used to determine the moment the target app is 141 * properly warmed-up. 142 * It can be null - in that case the warmup is skipped. 143 * @param timeout The timeout for the warmup waiting; -1 = no wait; 0 = wait forever 144 * @param unit The timeout {@linkplain TimeUnit} 145 * @return Returns the initialized {@linkplain Process} 146 * @throws IOException 147 * @throws InterruptedException 148 * @throws TimeoutException 149 */ 150 public static Process startProcess(String name, 151 ProcessBuilder processBuilder, 152 final Consumer<String> lineConsumer, 153 final Predicate<String> linePredicate, 154 long timeout, 155 TimeUnit unit) 156 throws IOException, InterruptedException, TimeoutException { 157 System.out.println("["+name+"]:" + processBuilder.command().stream().collect(Collectors.joining(" "))); 158 Process p = processBuilder.start(); 159 StreamPumper stdout = new StreamPumper(p.getInputStream()); 160 StreamPumper stderr = new StreamPumper(p.getErrorStream()); 161 162 stdout.addPump(new LineForwarder(name, System.out)); 163 stderr.addPump(new LineForwarder(name, System.err)); 164 if (lineConsumer != null) { 165 StreamPumper.LinePump pump = new StreamPumper.LinePump() { 166 @Override 167 protected void processLine(String line) { 168 lineConsumer.accept(line); 169 } 170 }; 171 stdout.addPump(pump); 172 stderr.addPump(pump); 173 } 174 175 176 CountDownLatch latch = new CountDownLatch(1); 177 if (linePredicate != null) { 178 StreamPumper.LinePump pump = new StreamPumper.LinePump() { 179 @Override 180 protected void processLine(String line) { 181 if (latch.getCount() > 0 && linePredicate.test(line)) { 182 latch.countDown(); 183 } 184 } 185 }; 186 stdout.addPump(pump); 187 stderr.addPump(pump); 188 } else { 189 latch.countDown(); 190 } 191 final Future<Void> stdoutTask = stdout.process(); 192 final Future<Void> stderrTask = stderr.process(); 193 194 try { 195 if (timeout > -1) { 196 if (timeout == 0) { 197 latch.await(); 198 } else { 199 if (!latch.await(Utils.adjustTimeout(timeout), unit)) { 200 throw new TimeoutException(); 201 } 202 } 203 } 204 } catch (TimeoutException | InterruptedException e) { 205 System.err.println("Failed to start a process (thread dump follows)"); 206 for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) { 207 printStack(s.getKey(), s.getValue()); 208 } 209 210 if (p.isAlive()) { 211 p.destroyForcibly(); 212 } 213 214 stdoutTask.cancel(true); 215 stderrTask.cancel(true); 216 throw e; 217 } 218 219 return new ProcessImpl(p, stdoutTask, stderrTask); 220 } 221 222 /** 223 * <p>Starts a process from its builder.</p> 224 * <span>The default redirects of STDOUT and STDERR are started</span> 225 * <p> 226 * It is possible to wait for the process to get to a warmed-up state 227 * via {@linkplain Predicate} condition on the STDOUT. The warm-up will 228 * wait indefinitely. 229 * </p> 230 * @param name The process name 231 * @param processBuilder The process builder 232 * @param linePredicate The {@linkplain Predicate} to use on the STDOUT 233 * Used to determine the moment the target app is 234 * properly warmed-up. 235 * It can be null - in that case the warmup is skipped. 236 * @return Returns the initialized {@linkplain Process} 237 * @throws IOException 238 * @throws InterruptedException 239 * @throws TimeoutException 240 */ 241 @SuppressWarnings("overloads") 242 public static Process startProcess(String name, 243 ProcessBuilder processBuilder, 244 final Predicate<String> linePredicate) 245 throws IOException, InterruptedException, TimeoutException { 246 return startProcess(name, processBuilder, linePredicate, 0, TimeUnit.SECONDS); 247 } 248 249 /** 250 * Get the process id of the current running Java process 251 * 252 * @return Process id 253 */ 254 public static int getProcessId() { 255 long pid = ProcessHandle.current().getPid(); 256 if(pid == (int)pid) { 257 return (int)pid; 258 } else { 259 throw new RuntimeException("int does not suit pid: " + pid); 260 } 261 } 262 263 /** 264 * Get platform specific VM arguments (e.g. -d64 on 64bit Solaris) 265 * 266 * @return String[] with platform specific arguments, empty if there are 267 * none 268 */ 269 public static String[] getPlatformSpecificVMArgs() { 270 String osName = System.getProperty("os.name"); 271 String dataModel = System.getProperty("sun.arch.data.model"); 272 273 if (osName.equals("SunOS") && dataModel.equals("64")) { 274 return new String[] { "-d64" }; 275 } 276 277 return new String[] {}; 278 } 279 280 /** 281 * Create ProcessBuilder using the java launcher from the jdk to be tested, 282 * and with any platform specific arguments prepended. 283 * 284 * @param command Arguments to pass to the java command. 285 * @return The ProcessBuilder instance representing the java command. 286 */ 287 public static ProcessBuilder createJavaProcessBuilder(String... command) 288 throws Exception { 289 return createJavaProcessBuilder(false, command); 290 } 291 292 /** 293 * Create ProcessBuilder using the java launcher from the jdk to be tested, 294 * and with any platform specific arguments prepended. 295 * 296 * @param addTestVmAndJavaOptions If true, adds test.vm.opts and test.java.opts 297 * to the java arguments. 298 * @param command Arguments to pass to the java command. 299 * @return The ProcessBuilder instance representing the java command. 300 */ 301 public static ProcessBuilder createJavaProcessBuilder(boolean addTestVmAndJavaOptions, String... command) throws Exception { 302 String javapath = JDKToolFinder.getJDKTool("java"); 303 304 ArrayList<String> args = new ArrayList<>(); 305 args.add(javapath); 306 Collections.addAll(args, getPlatformSpecificVMArgs()); 307 308 if (addTestVmAndJavaOptions) { 309 // -cp is needed to make sure the same classpath is used whether the test is 310 // run in AgentVM mode or OtherVM mode. It was added to the hotspot version 311 // of this API as part of 8077608. However, for the jdk version it is only 312 // added when addTestVmAndJavaOptions is true in order to minimize 313 // disruption to existing JDK tests, which have yet to be tested with -cp 314 // being added. At some point -cp should always be added to be consistent 315 // with what the hotspot version does. 316 args.add("-cp"); 317 args.add(System.getProperty("java.class.path")); 318 Collections.addAll(args, Utils.getTestJavaOpts()); 319 } 320 321 Collections.addAll(args, command); 322 323 // Reporting 324 StringBuilder cmdLine = new StringBuilder(); 325 for (String cmd : args) 326 cmdLine.append(cmd).append(' '); 327 System.out.println("Command line: [" + cmdLine.toString() + "]"); 328 329 return new ProcessBuilder(args.toArray(new String[args.size()])); 330 } 331 332 private static void printStack(Thread t, StackTraceElement[] stack) { 333 System.out.println("\t" + t + 334 " stack: (length = " + stack.length + ")"); 335 if (t != null) { 336 for (StackTraceElement stack1 : stack) { 337 System.out.println("\t" + stack1); 338 } 339 System.out.println(); 340 } 341 } 342 343 /** 344 * Executes a test jvm process, waits for it to finish and returns the process output. 345 * The default jvm options from jtreg, test.vm.opts and test.java.opts, are added. 346 * The java from the test.jdk is used to execute the command. 347 * 348 * The command line will be like: 349 * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds 350 * 351 * The jvm process will have exited before this method returns. 352 * 353 * @param cmds User specifed arguments. 354 * @return The output from the process. 355 */ 356 public static OutputAnalyzer executeTestJvm(String... cmds) throws Exception { 357 ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds)); 358 return executeProcess(pb); 359 } 360 361 /** 362 * Executes a process, waits for it to finish and returns the process output. 363 * The process will have exited before this method returns. 364 * @param pb The ProcessBuilder to execute. 365 * @return The {@linkplain OutputAnalyzer} instance wrapping the process. 366 */ 367 public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception { 368 OutputAnalyzer output = null; 369 Process p = null; 370 boolean failed = false; 371 try { 372 p = pb.start(); 373 output = new OutputAnalyzer(p); 374 p.waitFor(); 375 376 return output; 377 } catch (Throwable t) { 378 if (p != null) { 379 p.destroyForcibly().waitFor(); 380 } 381 382 failed = true; 383 System.out.println("executeProcess() failed: " + t); 384 throw t; 385 } finally { 386 if (failed) { 387 System.err.println(getProcessLog(pb, output)); 388 } 389 } 390 } 391 392 /** 393 * Executes a process, waits for it to finish and returns the process output. 394 * 395 * The process will have exited before this method returns. 396 * 397 * @param cmds The command line to execute. 398 * @return The output from the process. 399 */ 400 public static OutputAnalyzer executeProcess(String... cmds) throws Throwable { 401 return executeProcess(new ProcessBuilder(cmds)); 402 } 403 404 /** 405 * Used to log command line, stdout, stderr and exit code from an executed process. 406 * @param pb The executed process. 407 * @param output The output from the process. 408 */ 409 public static String getProcessLog(ProcessBuilder pb, OutputAnalyzer output) { 410 String stderr = output == null ? "null" : output.getStderr(); 411 String stdout = output == null ? "null" : output.getStdout(); 412 String exitValue = output == null ? "null": Integer.toString(output.getExitValue()); 413 StringBuilder logMsg = new StringBuilder(); 414 final String nl = System.getProperty("line.separator"); 415 logMsg.append("--- ProcessLog ---" + nl); 416 logMsg.append("cmd: " + getCommandLine(pb) + nl); 417 logMsg.append("exitvalue: " + exitValue + nl); 418 logMsg.append("stderr: " + stderr + nl); 419 logMsg.append("stdout: " + stdout + nl); 420 421 return logMsg.toString(); 422 } 423 424 /** 425 * @return The full command line for the ProcessBuilder. 426 */ 427 public static String getCommandLine(ProcessBuilder pb) { 428 if (pb == null) { 429 return "null"; 430 } 431 StringBuilder cmd = new StringBuilder(); 432 for (String s : pb.command()) { 433 cmd.append(s).append(" "); 434 } 435 return cmd.toString().trim(); 436 } 437 438 /** 439 * Executes a process, waits for it to finish, prints the process output 440 * to stdout, and returns the process output. 441 * 442 * The process will have exited before this method returns. 443 * 444 * @param cmds The command line to execute. 445 * @return The {@linkplain OutputAnalyzer} instance wrapping the process. 446 */ 447 public static OutputAnalyzer executeCommand(String... cmds) 448 throws Throwable { 449 String cmdLine = Arrays.stream(cmds).collect(Collectors.joining(" ")); 450 System.out.println("Command line: [" + cmdLine + "]"); 451 OutputAnalyzer analyzer = ProcessTools.executeProcess(cmds); 452 System.out.println(analyzer.getOutput()); 453 return analyzer; 454 } 455 456 /** 457 * Executes a process, waits for it to finish, prints the process output 458 * to stdout and returns the process output. 459 * 460 * The process will have exited before this method returns. 461 * 462 * @param pb The ProcessBuilder to execute. 463 * @return The {@linkplain OutputAnalyzer} instance wrapping the process. 464 */ 465 public static OutputAnalyzer executeCommand(ProcessBuilder pb) 466 throws Throwable { 467 String cmdLine = pb.command().stream().collect(Collectors.joining(" ")); 468 System.out.println("Command line: [" + cmdLine + "]"); 469 OutputAnalyzer analyzer = ProcessTools.executeProcess(pb); 470 System.out.println(analyzer.getOutput()); 471 return analyzer; 472 } 473 474 private static class ProcessImpl extends Process { 475 476 private final Process p; 477 private final Future<Void> stdoutTask; 478 private final Future<Void> stderrTask; 479 480 public ProcessImpl(Process p, Future<Void> stdoutTask, Future<Void> stderrTask) { 481 this.p = p; 482 this.stdoutTask = stdoutTask; 483 this.stderrTask = stderrTask; 484 } 485 486 @Override 487 public OutputStream getOutputStream() { 488 return p.getOutputStream(); 489 } 490 491 @Override 492 public InputStream getInputStream() { 493 return p.getInputStream(); 494 } 495 496 @Override 497 public InputStream getErrorStream() { 498 return p.getErrorStream(); 499 } 500 501 @Override 502 public int waitFor() throws InterruptedException { 503 int rslt = p.waitFor(); 504 waitForStreams(); 505 return rslt; 506 } 507 508 @Override 509 public int exitValue() { 510 return p.exitValue(); 511 } 512 513 @Override 514 public void destroy() { 515 p.destroy(); 516 } 517 518 @Override 519 public long getPid() { 520 return p.getPid(); 521 } 522 523 @Override 524 public boolean isAlive() { 525 return p.isAlive(); 526 } 527 528 @Override 529 public Process destroyForcibly() { 530 return p.destroyForcibly(); 531 } 532 533 @Override 534 public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException { 535 boolean rslt = p.waitFor(timeout, unit); 536 if (rslt) { 537 waitForStreams(); 538 } 539 return rslt; 540 } 541 542 private void waitForStreams() throws InterruptedException { 543 try { 544 stdoutTask.get(); 545 } catch (ExecutionException e) { 546 } 547 try { 548 stderrTask.get(); 549 } catch (ExecutionException e) { 550 } 551 } 552 } 553 }