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