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