1 /* 2 * Copyright (c) 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.api.flightrecorder; 27 28 import static jdk.test.lib.Asserts.assertEquals; 29 import static jdk.test.lib.Asserts.assertTrue; 30 31 import java.time.Duration; 32 import java.util.Map; 33 34 import jdk.jfr.Event; 35 import jdk.jfr.EventType; 36 import jdk.jfr.Recording; 37 38 /** 39 * @test 40 * @key jfr 41 * 42 * @library /lib / 43 * @run main/othervm jdk.jfr.api.flightrecorder.TestGetSettings 44 */ 45 public class TestGetSettings { 46 47 public static void main(String[] args) throws Throwable { 48 final long minThresholdNanos = 1000000; 49 final String dummyEventPath = "mydummy/event/path"; 50 final String myEventSettingName = String.valueOf(EventType.getEventType(MyEvent.class).getId()); 51 System.out.println("myEventSettingName=" + myEventSettingName); 52 53 // Settings should be merged to include the most number of events (minimum threshold). 54 Recording r1 = new Recording(); 55 r1.enable(MyEvent.class).withThreshold(Duration.ofNanos(minThresholdNanos * 3)); 56 r1.enable(MyEvent.class).withThreshold(Duration.ofNanos(minThresholdNanos * 2)); 57 r1.enable(dummyEventPath).withThreshold(Duration.ofNanos(minThresholdNanos)); 58 r1.start(); 59 60 ExpectedSetting[] expectedR1 = { 61 new ExpectedSetting(myEventSettingName, "enabled", "true"), 62 new ExpectedSetting(myEventSettingName, "threshold", Long.toString(minThresholdNanos * 2) + " ns"), 63 new ExpectedSetting(dummyEventPath, "enabled", "true"), 64 new ExpectedSetting(dummyEventPath, "threshold", Long.toString(minThresholdNanos) + " ns"), 65 }; 66 67 verifySettings(r1.getSettings(), expectedR1); 68 69 // Start another recording. Recorder settings should be merged from both recordings. 70 Recording r2 = new Recording(); 71 r2.enable(MyEvent.class).withThreshold(Duration.ofNanos(minThresholdNanos)); 72 r2.disable(dummyEventPath); 73 r2.start(); 74 75 ExpectedSetting[] expectedR2 = { 76 new ExpectedSetting(myEventSettingName, "enabled", "true"), 77 new ExpectedSetting(myEventSettingName, "threshold", Long.toString(minThresholdNanos) + " ns"), 78 new ExpectedSetting(dummyEventPath, "enabled", "false") 79 }; 80 81 verifySettings(r1.getSettings(), expectedR1); 82 verifySettings(r2.getSettings(), expectedR2); 83 84 // Stop first recording. Recorder should use settings from r2. 85 r1.stop(); 86 verifySettings(r2.getSettings(), expectedR2); 87 88 r2.stop(); 89 r1.close(); 90 r2.close(); 91 } 92 93 private static void verifySettings(Map<String, String> settings, ExpectedSetting ... expectedSettings) { 94 for (String name : settings.keySet()) { 95 System.out.printf("Settings: %s=%s%n", name, settings.get(name)); 96 } 97 for (ExpectedSetting expected : expectedSettings) { 98 boolean isFound = false; 99 for (String name : settings.keySet()) { 100 if (name.contains(expected.name) && name.contains(expected.option)) { 101 final String value = settings.get(name); 102 String msg = String.format("got: %s=%s, expected: %s", name, value, expected.toString()); 103 assertEquals(value, expected.value, msg); 104 System.out.println("OK: " + msg); 105 isFound = true; 106 break; 107 } 108 } 109 assertTrue(isFound, "Missing setting " + expected.toString()); 110 } 111 } 112 113 private static class MyEvent extends Event { 114 } 115 116 private static class ExpectedSetting { 117 String name; 118 String option; 119 String value; 120 121 public ExpectedSetting(String name, String option, String value) { 122 this.name = name; 123 this.option = option; 124 this.value = value; 125 } 126 127 @Override 128 public String toString() { 129 return String.format("name=%s, option=%s, value=%s", name, option, value); 130 } 131 } 132 }