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