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.jfr.consumer.RecordedEvent;
  36 import jdk.test.lib.Asserts;
  37 import jdk.test.lib.jfr.EventNames;
  38 import jdk.test.lib.jfr.Events;
  39 import jdk.test.lib.jfr.SimpleEvent;
  40 import jdk.test.lib.jfr.SimpleEventHelper;
  41 
  42 /**
  43  * @test
  44  * @summary Enable, disable, enable event during recording.
  45  * @key jfr
  46  *
  47  * @library /lib /
  48  * @run main/othervm jdk.jfr.api.recording.event.TestReEnableMultiple
  49  */
  50 public class TestReEnableMultiple {
  51     private static final String EVENT_PATH = EventNames.FileWrite;
  52     private static final Random rand = new Random(0);
  53 
  54     public static void main(String[] args) throws Throwable {
  55         Recording rA = new Recording();
  56         Recording rB = new Recording();
  57 
  58         final Path path = Paths.get(".", "dummy.txt").toAbsolutePath();
  59         rA.start();
  60         rB.start();
  61 
  62         SimpleEventHelper.enable(rA, false);
  63         SimpleEventHelper.enable(rA, false);
  64 
  65         int expectedMyEvents = 0;
  66         int expectedIoEvents = 0;
  67         for (int i = 0; i < 20; ++i) {
  68             SimpleEventHelper.createEvent(i);
  69             if (isMyEventEnabled(rA, rB)) {
  70                 expectedMyEvents++;
  71                 System.out.println("Expect MyEvent.id " + i);
  72             }
  73 
  74             Files.write(path, "A".getBytes());
  75             if (isIoEnabled(rA, rB)) {
  76                 expectedIoEvents++;
  77             }
  78 
  79             for (int j = 0; j < 4; ++j) {
  80                 Recording r = (rand.nextInt(2) == 0) ? rA : rB;
  81                 updateSettings(r);
  82             }
  83         }
  84 
  85         rA.stop();
  86         rB.stop();
  87 
  88         verifyEventCount(rA, expectedMyEvents, expectedIoEvents, path);
  89         verifyEventCount(rB, expectedMyEvents, expectedIoEvents, path);
  90 
  91         rA.close();
  92         rB.close();
  93     }
  94 
  95     private static void verifyEventCount(Recording r, int expectedMyEvents, int expectedIoEvents, Path path) throws Exception {
  96         int actualMyEvents = 0;
  97         int actualIoEvents = 0;
  98         for (RecordedEvent event : Events.fromRecording(r)) {
  99             if (Events.isEventType(event, EVENT_PATH)) {
 100                 if (path.toString().equals(Events.assertField(event, "path").getValue())) {
 101                     actualIoEvents++;
 102                 }
 103             } else {
 104                 Asserts.assertTrue(Events.isEventType(event, SimpleEvent.class.getName()));
 105                 System.out.println("Got MyEvent.id=" + Events.assertField(event, "id").getValue());
 106                 actualMyEvents++;
 107             }
 108         }
 109         System.out.printf("MyEvents: expected=%d, actual=%d%n", expectedMyEvents, actualMyEvents);
 110         System.out.printf("IoEvents: expected=%d, actual=%d%n", expectedIoEvents, actualIoEvents);
 111         Asserts.assertEquals(expectedMyEvents, actualMyEvents, "Wrong number of MyEvents");
 112         Asserts.assertEquals(expectedIoEvents, actualIoEvents, "Wrong number of IoEvents");
 113     }
 114 
 115     private static void updateSettings(Recording r) {
 116         boolean doEnable = rand.nextInt(3) == 0; // Disable 2 of 3, since event
 117                                                  // is enabled by union of
 118                                                  // recordings.
 119         boolean doMyEvent = rand.nextInt(2) == 0;
 120         if (doMyEvent) {
 121             SimpleEventHelper.enable(r, doEnable);
 122         } else {
 123             if (doEnable) {
 124                 r.enable(EVENT_PATH).withoutStackTrace();
 125             } else {
 126                 r.disable(EVENT_PATH);
 127             }
 128         }
 129     }
 130 
 131     private static boolean isMyEventEnabled(Recording rA, Recording rB) {
 132         long eventTypeId = EventType.getEventType(SimpleEvent.class).getId();
 133         String settingName = eventTypeId + "#enabled";
 134         return isEnabled(rA, settingName) || isEnabled(rB, settingName);
 135     }
 136 
 137     private static boolean isIoEnabled(Recording rA, Recording rB) {
 138         String settingName = EVENT_PATH + "#enabled";
 139         return isEnabled(rA, settingName) || isEnabled(rB, settingName);
 140     }
 141 
 142     private static boolean isEnabled(Recording r, String settingName) {
 143         // System.out.printf("R(%s) %s=%s%n", r.getName(), settingName,
 144         // r.getSettings().get(settingName));
 145         return Boolean.parseBoolean(r.getSettings().get(settingName));
 146     }
 147 }