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