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