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