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.recording.event; 27 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.util.Random; 32 33 import jdk.jfr.EventType; 34 import jdk.jfr.Recording; 35 import jdk.test.lib.Asserts; 36 import jdk.test.lib.jfr.SimpleEvent; 37 import jdk.test.lib.jfr.SimpleEventHelper; 38 39 /** 40 * @test 41 * @summary Enable, disable, enable event during recording. 42 * @key jfr 43 * @requires vm.hasJFR 44 * @library /test/lib 45 * @run main/othervm jdk.jfr.api.recording.event.TestRecordingEnableDisable 46 */ 47 public class TestRecordingEnableDisable { 48 private static final String EVENT_PATH = "java.file_write"; 49 private static final Random rand = new Random(0); 50 51 public static void main(String[] args) throws Throwable { 52 53 Recording rA = new Recording(); 54 Recording rB = new Recording(); 55 56 rA.setName("rA"); 57 rB.setName("rB"); 58 59 final Path path = Paths.get(".", "my.jfr"); 60 rA.start(); 61 rB.start(); 62 63 for (int i = 0; i < 30; ++i) { 64 SimpleEventHelper.createEvent(i); 65 if (isMyEventEnabled(rA, rB)) { 66 System.out.println("MyEvent enabled"); 67 } 68 else { 69 System.out.println("MyEvent disabled"); 70 } 71 72 Files.write(path, "A".getBytes()); 73 if (isIoEnabled(rA, rB)) { 74 System.out.println("IoEvent enabled"); 75 } 76 else { 77 System.out.println("IoEvent disabled"); 78 } 79 Recording r = ((i % 2) == 0) ? rA : rB; 80 updateSettings(r); 81 } 82 83 rA.stop(); 84 rB.stop(); 85 rA.close(); 86 rB.close(); 87 } 88 89 90 private static void updateSettings(Recording r) { 91 int operationIndex = rand.nextInt(4); 92 switch (operationIndex) { 93 case 0: 94 SimpleEventHelper.enable(r, true); 95 break; 96 case 1: 97 SimpleEventHelper.enable(r, false); 98 break; 99 case 2: 100 r.enable(EVENT_PATH).withoutStackTrace(); 101 break; 102 case 3: 103 r.disable(EVENT_PATH); 104 break; 105 default: 106 Asserts.fail("Wrong operataionIndex. Test error"); 107 } 108 } 109 110 private static boolean isMyEventEnabled(Recording rA, Recording rB) { 111 long eventTypeId = EventType.getEventType(SimpleEvent.class).getId(); 112 String settingName = "@" + eventTypeId + "#enabled"; 113 return isEnabled(rA, settingName) || isEnabled(rB, settingName); 114 } 115 116 private static boolean isIoEnabled(Recording rA, Recording rB) { 117 String settingName = EVENT_PATH + "#enabled"; 118 return isEnabled(rA, settingName) || isEnabled(rB, settingName); 119 } 120 121 private static boolean isEnabled(Recording r, String settingName) { 122 System.out.printf("R(%s) %s=%s%n", r.getName(), settingName, r.getSettings().get(settingName)); 123 return Boolean.parseBoolean(r.getSettings().get(settingName)); 124 } 125 }