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