1 /*
   2  * Copyright (c) 2015, 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.consumer.RecordedEvent;
  31 import jdk.jfr.consumer.RecordedThread;
  32 import jdk.jfr.consumer.RecordingFile;
  33 import jdk.testlibrary.Asserts;
  34 import jdk.testlibrary.jfr.EventNames;
  35 import jdk.testlibrary.jfr.Events;
  36 import jdk.testlibrary.OutputAnalyzer;
  37 import jdk.testlibrary.jfr.JcmdAsserts;
  38 import jdk.testlibrary.jfr.JcmdHelper;
  39 
  40 /*
  41  * @test
  42  * @summary The test verifies that recording can be started with setting file(s)
  43  * @key jfr
  44  * @library /lib/testlibrary
  45  * @run main/othervm jdk.jfr.jcmd.TestJcmdStartWithSettings
  46  */
  47 public class TestJcmdStartWithSettings {
  48 
  49     private static final String DIR = System.getProperty("test.src", ".");
  50     private static final File SETTINGS = new File(DIR, "jcmd-testsettings.jfc");
  51     private static final File SETTINGS2 = new File(DIR, "jcmd-testsettings.2.jfc");
  52 
  53     public static void main(String[] args) throws Exception {
  54         testSingleSettingFile();
  55         testManySettingFiles();
  56         testPresetSettings();
  57         testNonExistingSettingFile();
  58     }
  59 
  60     private static void testSingleSettingFile() throws Exception {
  61         String name = "testSingleSettingFile";
  62         File recording = new File(name + ".jfr");
  63 
  64         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
  65                 "name=" + name,
  66                 "duration=1h",
  67                 "settings=" + SETTINGS.getCanonicalPath(),
  68                 "filename=" + recording.getCanonicalPath());
  69         JcmdAsserts.assertRecordingHasStarted(output);
  70         JcmdHelper.waitUntilRunning(name);
  71         output = JcmdHelper.jcmdCheck(name, true);
  72         JcmdAsserts.assertThreadSleepThresholdIsSet(output);
  73 
  74         Thread.sleep(100);
  75         JcmdHelper.stopAndCheck(name);
  76         assertHasEvent(recording, EventNames.ThreadSleep, Thread.currentThread().getName());
  77     }
  78 
  79     /**
  80      * Start a recording with two setting files and
  81      * verify Java Thread Sleep and Java Monitor Wait events have been recorded.
  82      */
  83     private static void testManySettingFiles() throws Exception {
  84         String name = "testManySettingFiles";
  85         File recording = new File(name + ".jfr");
  86 
  87         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
  88                 "name=" + name,
  89                 "duration=1h",
  90                 "settings=" + SETTINGS.getCanonicalPath(),
  91                 "settings=" + SETTINGS2.getCanonicalPath(),
  92                 "filename=" + recording.getCanonicalPath());
  93         JcmdAsserts.assertRecordingHasStarted(output);
  94         JcmdHelper.waitUntilRunning(name);
  95         output = JcmdHelper.jcmdCheck(name, true);
  96         JcmdAsserts.assertThreadSleepThresholdIsSet(output);
  97         JcmdAsserts.assertMonitorWaitThresholdIsSet(output);
  98 
  99         // Generate Monitor Wait event
 100         ThreadWait threadWait = new ThreadWait();
 101         threadWait.start();
 102         Thread.sleep(300);
 103         threadWait.join();
 104 
 105         JcmdHelper.stopAndCheck(name);
 106         assertHasEvent(recording, EventNames.ThreadSleep, Thread.currentThread().getName());
 107         assertHasEvent(recording, EventNames.JavaMonitorWait, threadWait.getName());
 108     }
 109 
 110     /**
 111      * It should be possible to use "profile" as non-path preset,
 112      * both with and without '.jfc'
 113      */
 114     private static void testPresetSettings() throws Exception {
 115         String name = "testPresetSettingsJfc";
 116         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
 117                 "name=" + name,
 118                 "settings=profile.jfc");
 119         JcmdAsserts.assertRecordingHasStarted(output);
 120         JcmdHelper.waitUntilRunning(name);
 121         JcmdHelper.stopAndCheck(name);
 122 
 123         name = "testPresetSettingsNoJfc";
 124         output = JcmdHelper.jcmd("JFR.start",
 125                 "name=" + name,
 126                 "settings=profile");
 127         JcmdAsserts.assertRecordingHasStarted(output);
 128         JcmdHelper.waitUntilRunning(name);
 129         JcmdHelper.stopAndCheck(name);
 130     }
 131 
 132     /**
 133      * It should not be possible to start a recording
 134      * with a non-existing setting file
 135      */
 136     private static void testNonExistingSettingFile() throws Exception {
 137         String name = "testNonExistingSettingFile";
 138         OutputAnalyzer output = JcmdHelper.jcmd("JFR.start",
 139                 "name=" + name,
 140                 "settings=nonexisting.jfc");
 141         JcmdAsserts.assertNotAbleToFindSettingsFile(output);
 142         JcmdHelper.assertRecordingNotExist(name);
 143     }
 144 
 145     private static void assertHasEvent(File file, String eventType, String threadName) throws Exception {
 146         for (RecordedEvent event : RecordingFile.readAllEvents(file.toPath())) {
 147             if (Events.isEventType(event, eventType)) {
 148                 System.out.println(event);
 149                 RecordedThread t = event.getThread();
 150                 if (t == null) {
 151                     throw new Exception("Thread null for event " + eventType);
 152                 }
 153                 if (threadName.equals(t.getJavaName())) {
 154                     System.out.println("Found event: " + event);
 155                     return;
 156                 }
 157             }
 158         }
 159         Asserts.fail("No events of type " + eventType);
 160     }
 161 
 162     static class ThreadWait extends Thread {
 163 
 164         public ThreadWait() {
 165             setName("ThreadWait");
 166         }
 167 
 168         @Override
 169         public void run() {
 170             try {
 171                 synchronized (this) {
 172                     wait(100);
 173                 }
 174             } catch (InterruptedException e) {
 175                 e.printStackTrace();
 176             }
 177         }
 178     }
 179 }