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.destination;
  27 
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.List;
  32 
  33 import jdk.jfr.Recording;
  34 import jdk.jfr.consumer.RecordedEvent;
  35 import jdk.jfr.consumer.RecordingFile;
  36 import jdk.test.lib.Asserts;
  37 import jdk.test.lib.jfr.Events;
  38 import jdk.test.lib.jfr.SimpleEventHelper;
  39 
  40 /**
  41  * @test
  42  * @summary Test setDestination with concurrent recordings
  43  * @key jfr
  44  * @requires vm.hasJFR
  45  * @library /test/lib
  46  * @run main/othervm -Xlog:jfr=trace jdk.jfr.api.recording.destination.TestDestMultiple
  47  */
  48 public class TestDestMultiple {
  49 
  50     public static void main(String[] args) throws Throwable {
  51         Recording rA = new Recording();
  52         Recording rB = new Recording();
  53 
  54         Path destA = Paths.get(".", "recA.jfr");
  55         Path destB = Paths.get(".", "recB.jfr");
  56         rA.setDestination(destA);
  57         rB.setDestination(destB);
  58 
  59         // Enable event in one recording and disable in the other.
  60         // Both recordings should still get the events, since we always
  61         // get the "union" of all settings.
  62         SimpleEventHelper.enable(rA, true);
  63         SimpleEventHelper.enable(rB, false);
  64 
  65         SimpleEventHelper.createEvent(0); // To no recording
  66 
  67         rA.start();
  68         SimpleEventHelper.createEvent(1); // Only to recA
  69 
  70         rB.start();
  71         SimpleEventHelper.createEvent(2); // To both recordings
  72 
  73         rA.stop();
  74 
  75         // This event will not be in recB.
  76         // The reason is that recA has stopped so event is no longer enabled.
  77         SimpleEventHelper.createEvent(3);
  78 
  79         // Enable the event and create a new event for recB
  80         SimpleEventHelper.enable(rB, true);
  81         SimpleEventHelper.createEvent(4);
  82 
  83         rB.stop();
  84         SimpleEventHelper.createEvent(5); // To no recording
  85 
  86         rB.close();
  87         rA.close();
  88 
  89         verifyRecording(destA, 1, 2);
  90         verifyRecording(destB, 2, 4);
  91     }
  92 
  93     private static void verifyRecording(Path path, int... ids) throws Exception {
  94         Asserts.assertTrue(Files.exists(path), "Recording file does not exist: " + path);
  95         int countEvent = 0;
  96         List<RecordedEvent> events = RecordingFile.readAllEvents(path);
  97         for (RecordedEvent event : events) {
  98             int id = Events.assertField(event, "id").getValue();
  99             System.out.printf("Recording '%s' id=%d%n", path, id);
 100         }
 101         for (RecordedEvent event : events) {
 102             int id = Events.assertField(event, "id").getValue();
 103             System.out.printf("Recording '%s' id=%d%n", path, id);
 104             Asserts.assertTrue(ids.length > countEvent, "Found extra event");
 105             Events.assertField(event, "id").equal(ids[countEvent]);
 106             ++countEvent;
 107         }
 108         // We expect exactly 4 events in each file. 2 events * 2 chunks
 109         Asserts.assertEquals(countEvent, ids.length, "Found too few events");
 110     }
 111 }