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