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.
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.jfr.jcmd;
25 
26 import java.io.File;
27 import java.util.Arrays;
28 import java.util.stream.Collectors;
29 
30 import jdk.test.lib.Asserts;
31 import jdk.test.lib.dcmd.CommandExecutor;
32 import jdk.test.lib.dcmd.PidJcmdExecutor;
33 import jdk.test.lib.process.OutputAnalyzer;
34 
35 public class JcmdHelper {
36 
37     // Wait until recording's state became running
waitUntilRunning(String name)38     public static void waitUntilRunning(String name) throws Exception {
39         long timeoutAt = System.currentTimeMillis() + 10000;
40         while (true) {
41             OutputAnalyzer output = jcmdCheck(name, false);
42             try {
43                 // The expected output can look like this:
44                 // Recording 1: name=1 (running)
45                 output.shouldMatch("^Recording \\d+: name=" + name
46                         + " .*\\W{1}running\\W{1}");
47                 return;
48             } catch (RuntimeException e) {
49                 if (System.currentTimeMillis() > timeoutAt) {
50                     Asserts.fail("Recording not started: " + name);
51                 }
52                 Thread.sleep(100);
53             }
54         }
55     }
56 
stopAndCheck(String name)57     public static void stopAndCheck(String name) throws Exception {
58         jcmd("JFR.stop", "name=\"" + name + "\"");
59         assertRecordingNotRunning(name);
60     }
61 
stopWriteToFileAndCheck(String name, File file)62     public static void stopWriteToFileAndCheck(String name, File file) throws Exception {
63         OutputAnalyzer output = jcmd("JFR.stop",
64                 "name=\"" + name + "\"",
65                 "filename=\"" + file.getAbsolutePath() + "\"");
66         JcmdAsserts.assertStoppedAndWrittenTo(output, name, file);
67         assertRecordingNotRunning(name);
68     }
69 
stopCompressAndCheck(String name, File file)70     public static void stopCompressAndCheck(String name, File file) throws Exception {
71         OutputAnalyzer output = jcmd("JFR.stop",
72                 "name=\"" + name + "\"",
73                 "compress=true",
74                 "filename=\"" + file.getAbsolutePath() + "\"");
75         JcmdAsserts.assertStoppedAndWrittenTo(output, name, file);
76         checkAndAssertNoRecordingsAvailable();
77     }
78 
stopDefaultRecordingAndCheck()79     public static void stopDefaultRecordingAndCheck() throws Exception {
80         OutputAnalyzer output = jcmd("JFR.stop", "recording=0");
81         JcmdAsserts.assertStoppedDefaultRecording(output);
82         checkAndAssertNoRecordingsAvailable();
83     }
84 
checkAndAssertNoRecordingsAvailable()85     public static void checkAndAssertNoRecordingsAvailable() throws Exception {
86         OutputAnalyzer output = jcmd("JFR.check");
87         JcmdAsserts.assertNoRecordingsAvailable(output);
88     }
89 
assertRecordingNotExist(String name)90     public static void assertRecordingNotExist(String name) throws Exception {
91         OutputAnalyzer output = jcmdCheck(name, false);
92         JcmdAsserts.assertRecordingNotExist(output, name);
93     }
94 
assertRecordingNotRunning(String name)95     public static void assertRecordingNotRunning(String name) throws Exception {
96         OutputAnalyzer output = jcmdCheck(name, false);
97         JcmdAsserts.assertRecordingNotRunning(output, name);
98     }
99 
assertRecordingIsRunning(String name)100     public static void assertRecordingIsRunning(String name) throws Exception {
101         OutputAnalyzer output = jcmdCheck(name, false);
102         JcmdAsserts.assertRecordingIsRunning(output, name);
103     }
104 
jcmd(int expectedExitValue, String... args)105     public static OutputAnalyzer jcmd(int expectedExitValue, String... args) {
106         String argsString = Arrays.stream(args).collect(Collectors.joining(" "));
107         CommandExecutor executor = new PidJcmdExecutor();
108         OutputAnalyzer oa = executor.execute(argsString);
109         oa.shouldHaveExitValue(expectedExitValue);
110         return oa;
111     }
112 
jcmd(String... args)113     public static OutputAnalyzer jcmd(String... args) {
114         return jcmd(0, args);
115     }
116 
117 
jcmdCheck(String recordingName, boolean verbose)118     public static OutputAnalyzer jcmdCheck(String recordingName, boolean verbose) {
119         return jcmd("JFR.check", "name=" + recordingName, "verbose=" + verbose);
120     }
121 }
122