1 /* 2 * Copyright (c) 2014, 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 /** 25 * @test 26 * @bug 8035668 27 * @summary Test checks case when target application finishes execution and jstat didn't complete work. 28 * jstat is started with interval = 100 (jstat -compiler 100) and monitored application finishes 29 * after 500ms. This shouldn't cause crash or hang in target application or in jstat. 30 * 31 * @library /lib/testlibrary 32 * @modules java.management 33 * jdk.jcmd 34 * 35 * @build jdk.testlibrary.* JStatInterval 36 * @run main JStatInterval 37 */ 38 39 import jdk.testlibrary.ProcessTools; 40 import jdk.testlibrary.JDKToolLauncher; 41 42 import java.io.BufferedReader; 43 import java.io.InputStreamReader; 44 import java.util.concurrent.TimeUnit; 45 import java.util.concurrent.atomic.AtomicBoolean; 46 import java.util.concurrent.atomic.AtomicInteger; 47 48 public class JStatInterval { 49 private static final String READY = "READY"; 50 private static final String ERROR = "!ERROR"; 51 52 public static class Application { 53 public static void main(String[] args) { 54 try { 55 System.out.println(READY); 56 System.out.flush(); 57 int exitCode = System.in.read(); 58 Thread.sleep(500); 59 System.exit(exitCode); 60 } catch (Exception e) { 61 System.out.println(ERROR); 62 System.out.flush(); 63 throw new Error(e); 64 } 65 } 66 } 67 public static void main(String[] args) throws Exception { 68 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 69 "-cp", 70 System.getProperty("test.class.path"), 71 "-XX:+UsePerfData", 72 Application.class.getName() 73 ); 74 AtomicBoolean error = new AtomicBoolean(false); 75 Process app = ProcessTools.startProcess( 76 "application", 77 pb, 78 line -> { 79 if (line.equals(READY)) { 80 return true; 81 } else if (line.equals(ERROR)) { 82 error.set(true); 83 return true; 84 } 85 return false; 86 }, 87 10, 88 TimeUnit.SECONDS 89 ); 90 if (error.get()) { 91 throw new Error("Unable to start the monitored application."); 92 } 93 94 String pidStr = String.valueOf(app.getPid()); 95 JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jstat"); 96 l.addToolArg("-compiler"); 97 l.addToolArg(pidStr); 98 l.addToolArg("100"); 99 100 ProcessBuilder jstatDef = new ProcessBuilder(l.getCommand()); 101 Process jstat = ProcessTools.startProcess( 102 "jstat", 103 jstatDef, 104 line -> { 105 if (line.trim().toLowerCase().startsWith("compiled")) { 106 return true; 107 } 108 return false; 109 }, 110 10, 111 TimeUnit.SECONDS 112 ); 113 114 app.getOutputStream().write(0); 115 app.getOutputStream().flush(); 116 117 if (app.waitFor() != 0) { 118 throw new Error("Error detected upon exiting the monitored application with active jstat"); 119 } 120 if (jstat.waitFor() != 0) { 121 throw new Error("Error detected in jstat when monitored application has exited prematurely"); 122 } 123 } 124 }