1 /*
2  * Copyright (c) 2015, 2018, 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.jfr.jcmd;
27 
28 import java.io.File;
29 import java.util.Arrays;
30 import java.util.stream.Collectors;
31 
32 import jdk.test.lib.Asserts;
33 import jdk.test.lib.dcmd.CommandExecutor;
34 import jdk.test.lib.dcmd.PidJcmdExecutor;
35 import jdk.test.lib.process.OutputAnalyzer;
36 
37 public class JcmdHelper {
38 
39     // Wait until recording's state became running
waitUntilRunning(String name)40     public static void waitUntilRunning(String name) throws Exception {
41         long timeoutAt = System.currentTimeMillis() + 10000;
42         while (true) {
43             OutputAnalyzer output = jcmdCheck(name, false);
44             try {
45                 // The expected output can look like this:
46                 // Recording 1: name=1 (running)
47                 output.shouldMatch("^Recording \\d+: name=" + name
48                         + " .*\\W{1}running\\W{1}");
49                 return;
50             } catch (RuntimeException e) {
51                 if (System.currentTimeMillis() > timeoutAt) {
52                     Asserts.fail("Recording not started: " + name);
53                 }
54                 Thread.sleep(100);
55             }
56         }
57     }
58 
stopAndCheck(String name)59     public static void stopAndCheck(String name) throws Exception {
60         jcmd("JFR.stop", "name=\"" + name + "\"");
61         assertRecordingNotRunning(name);
62     }
63 
stopWriteToFileAndCheck(String name, File file)64     public static void stopWriteToFileAndCheck(String name, File file) throws Exception {
65         OutputAnalyzer output = jcmd("JFR.stop",
66                 "name=\"" + name + "\"",
67                 "filename=\"" + file.getAbsolutePath() + "\"");
68         JcmdAsserts.assertStoppedAndWrittenTo(output, name, file);
69         assertRecordingNotRunning(name);
70     }
71 
stopCompressAndCheck(String name, File file)72     public static void stopCompressAndCheck(String name, File file) throws Exception {
73         OutputAnalyzer output = jcmd("JFR.stop",
74                 "name=\"" + name + "\"",
75                 "compress=true",
76                 "filename=\"" + file.getAbsolutePath() + "\"");
77         JcmdAsserts.assertStoppedAndWrittenTo(output, name, file);
78         checkAndAssertNoRecordingsAvailable();
79     }
80 
stopDefaultRecordingAndCheck()81     public static void stopDefaultRecordingAndCheck() throws Exception {
82         OutputAnalyzer output = jcmd("JFR.stop", "recording=0");
83         JcmdAsserts.assertStoppedDefaultRecording(output);
84         checkAndAssertNoRecordingsAvailable();
85     }
86 
checkAndAssertNoRecordingsAvailable()87     public static void checkAndAssertNoRecordingsAvailable() throws Exception {
88         OutputAnalyzer output = jcmd("JFR.check");
89         JcmdAsserts.assertNoRecordingsAvailable(output);
90     }
91 
assertRecordingNotExist(String name)92     public static void assertRecordingNotExist(String name) throws Exception {
93         OutputAnalyzer output = jcmdCheck(name, false);
94         JcmdAsserts.assertRecordingNotExist(output, name);
95     }
96 
assertRecordingNotRunning(String name)97     public static void assertRecordingNotRunning(String name) throws Exception {
98         OutputAnalyzer output = jcmdCheck(name, false);
99         JcmdAsserts.assertRecordingNotRunning(output, name);
100     }
101 
assertRecordingIsRunning(String name)102     public static void assertRecordingIsRunning(String name) throws Exception {
103         OutputAnalyzer output = jcmdCheck(name, false);
104         JcmdAsserts.assertRecordingIsRunning(output, name);
105     }
106 
jcmd(int expectedExitValue, String... args)107     public static OutputAnalyzer jcmd(int expectedExitValue, String... args) {
108         String argsString = Arrays.stream(args).collect(Collectors.joining(" "));
109         CommandExecutor executor = new PidJcmdExecutor();
110         OutputAnalyzer oa = executor.execute(argsString);
111         oa.shouldHaveExitValue(expectedExitValue);
112         return oa;
113     }
114 
jcmd(String... args)115     public static OutputAnalyzer jcmd(String... args) {
116         return jcmd(0, args);
117     }
118 
119 
jcmdCheck(String recordingName, boolean verbose)120     public static OutputAnalyzer jcmdCheck(String recordingName, boolean verbose) {
121         return jcmd("JFR.check", "name=" + recordingName, "verbose=" + verbose);
122     }
123 }
124