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