1 /*
2  * Copyright (c) 2013, 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 
30 import jdk.jfr.FlightRecorder;
31 import jdk.jfr.Recording;
32 import jdk.jfr.RecordingState;
33 import jdk.test.lib.process.OutputAnalyzer;
34 
35 /**
36  * @test
37  * @summary The test verifies that recording can be started with options delay|duration|maxage|maxsize
38  * @key jfr
39  *
40  * @library /lib /
41  * @run main/othervm -XX:+FlightRecorder -XX:FlightRecorderOptions=maxchunksize=2097152 jdk.jfr.jcmd.TestJcmdStartWithOptions
42  */
43 public class TestJcmdStartWithOptions {
44 
45     private static final String DIR = System.getProperty("test.src", ".");
46     private static final File SETTINGS = new File(DIR, "jcmd-testsettings3.jfc");
47 
main(String[] args)48     public static void main(String[] args) throws Exception {
49         testRecordingNotStartedTooEarly();
50         testDelayLessThan1s();
51         testDuration();
52         testDurationLessThan1s();
53         testMaxAge();
54         testMaxSize();
55     }
56 
testRecordingNotStartedTooEarly()57     static void testRecordingNotStartedTooEarly() throws Exception {
58         String name = "testRecordingNotStartedTooEarly";
59         long delay = 2 * 1000;
60         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
61                 "name=" + name,
62                 "delay=" + delay + "ms");
63         JcmdAsserts.assertRecordingIsScheduled(output, "1", "2 s");
64         for (Recording r : FlightRecorder.getFlightRecorder().getRecordings()) {
65             if (name.equals(r.getName())) {
66                 while(r.getState() != RecordingState.RUNNING) {
67                     Thread.sleep(10);
68                 }
69                 long currentTime = System.currentTimeMillis();
70                 long afterActualStart = currentTime + delay;
71                 JcmdAsserts.assertStartTimeGreaterOrEqualThanMBeanValue(name, afterActualStart);
72                 JcmdHelper.stopAndCheck(name);
73                 return;
74             }
75         }
76         throw new Exception("Could not find recording with name " + name);
77     }
78 
testDelayLessThan1s()79     private static void testDelayLessThan1s() throws Exception {
80         String name = "testDelayLessThan1s";
81         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
82                 "name=" + name,
83                 "delay=10ms");
84         JcmdAsserts.assertDelayAtLeast1s(output);
85         output = JcmdHelper.jcmd("JFR.check");
86         JcmdAsserts.assertNoRecordingsAvailable(output);
87     }
88 
testDuration()89     private static void testDuration() throws Exception {
90         String name = "testDuration";
91         long duration = 3600 * 1000;
92         String durationS = String.valueOf(duration / 1000) + "s" ;
93         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
94                 "name=" + name,
95                 "duration=" + durationS);
96         JcmdAsserts.assertRecordingHasStarted(output);
97         JcmdHelper.waitUntilRunning(name);
98         JcmdAsserts.assertDurationEqualsMBeanValue(name, duration);
99         JcmdHelper.stopAndCheck(name);
100     }
101 
testDurationLessThan1s()102     private static void testDurationLessThan1s() throws Exception {
103         String name = "testDurationLessThan1s";
104         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
105                 "name=" + name,
106                 "duration=10ms");
107         JcmdAsserts.assertDurationAtLeast1s(output);
108         JcmdHelper.checkAndAssertNoRecordingsAvailable();
109     }
110 
111     /**
112      * Check the maxage is the same as MBean value
113      */
testMaxAge()114     private static void testMaxAge() throws Exception {
115         String name = "testMaxAge";
116         long maxAge = 2 * 1000;
117         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
118                 "name=" + name,
119                 "settings=" + SETTINGS.getAbsolutePath(),
120                 "maxage=2s");
121         JcmdAsserts.assertRecordingHasStarted(output);
122         JcmdHelper.waitUntilRunning(name);
123         JcmdAsserts.assertMaxAgeEqualsMBeanValue(name, maxAge);
124         JcmdHelper.stopAndCheck(name);
125     }
126 
127     /**
128      * Check the maxsize is the same as MBean value
129      */
testMaxSize()130     private static void testMaxSize() throws Exception {
131         String name = "testMaxSize";
132         long maxSize = 2 * 1024 * 1024;
133         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
134                 "name=" + name,
135                 "settings=" + SETTINGS.getAbsolutePath(),
136                 "maxsize=" + maxSize);
137         JcmdAsserts.assertRecordingHasStarted(output);
138         JcmdHelper.waitUntilRunning(name);
139         JcmdAsserts.assertMaxSizeEqualsMBeanValue(name, maxSize);
140         JcmdHelper.stopAndCheck(name);
141     }
142 
143 }
144