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