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.flightrecorder;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.time.Duration;
  31 import java.time.Instant;
  32 import java.util.ArrayList;
  33 import java.util.List;
  34 
  35 import jdk.jfr.FlightRecorder;
  36 import jdk.jfr.Recording;
  37 import jdk.jfr.RecordingState;
  38 import jdk.jfr.consumer.RecordedEvent;
  39 import jdk.test.lib.Asserts;
  40 import jdk.test.lib.jfr.Events;
  41 import jdk.test.lib.jfr.SimpleEvent;
  42 
  43 /* @test
  44  * @key jfr
  45  * @requires vm.hasJFR
  46  * @library /test/lib
  47  * @run main/othervm jdk.jfr.api.flightrecorder.TestSnapshot
  48  */
  49 public class TestSnapshot {
  50     private final static int RECORDING_COUNT = 5;
  51 
  52     public static void main(String[] args) throws Exception {
  53         testEmpty();
  54         testStopped();
  55         testOngoingDisk();
  56         testOngoingMemory();
  57         testMultiple();
  58     }
  59 
  60     private static void testMultiple() throws IOException {
  61         FlightRecorder recorder = FlightRecorder.getFlightRecorder();
  62         List<Recording> recordings = new ArrayList<>();
  63         long size = 0;
  64         for (int i = 0; i < RECORDING_COUNT; i++) {
  65             Recording r = new Recording();
  66             r.enable(SimpleEvent.class);
  67             r.start();
  68             SimpleEvent se = new SimpleEvent();
  69             se.commit();
  70             r.stop();
  71             recordings.add(r);
  72             size += r.getSize();
  73         }
  74         try (Recording snapshot = recorder.takeSnapshot()) {
  75             Asserts.assertEquals(snapshot.getSize(), size);
  76             Asserts.assertGreaterThanOrEqual(snapshot.getStartTime(), recordings.get(0).getStartTime());
  77             Asserts.assertLessThanOrEqual(snapshot.getStopTime(), recordings.get(RECORDING_COUNT - 1).getStopTime());
  78             Asserts.assertGreaterThanOrEqual(snapshot.getDuration(), Duration.ZERO);
  79             assertStaticOptions(snapshot);
  80             try (InputStream is = snapshot.getStream(null, null)) {
  81                 Asserts.assertNotNull(is);
  82             }
  83 
  84             List<RecordedEvent> events = Events.fromRecording(snapshot);
  85             Events.hasEvents(events);
  86             Asserts.assertEquals(events.size(), RECORDING_COUNT);
  87             for (int i = 0; i < RECORDING_COUNT; i++) {
  88                 Asserts.assertEquals(events.get(i).getEventType().getName(), SimpleEvent.class.getName());
  89             }
  90         }
  91         for (Recording r : recordings) {
  92             r.close();
  93         }
  94     }
  95     private static void testOngoingMemory() throws IOException {
  96         testOngoing(false);
  97     }
  98 
  99     private static void testOngoingDisk() throws IOException {
 100         testOngoing(true);
 101     }
 102 
 103     private static void testOngoing(boolean disk) throws IOException {
 104         FlightRecorder recorder = FlightRecorder.getFlightRecorder();
 105         try (Recording r = new Recording()) {
 106             r.setToDisk(disk);
 107             r.enable(SimpleEvent.class);
 108             r.start();
 109             SimpleEvent se = new SimpleEvent();
 110             se.commit();
 111 
 112             try (Recording snapshot = recorder.takeSnapshot()) {
 113 
 114                 Asserts.assertGreaterThan(snapshot.getSize(), 0L);
 115                 Asserts.assertGreaterThanOrEqual(snapshot.getStartTime(), r.getStartTime());
 116                 Asserts.assertGreaterThanOrEqual(snapshot.getStopTime(), r.getStartTime());
 117                 Asserts.assertGreaterThanOrEqual(snapshot.getDuration(), Duration.ZERO);
 118                 assertStaticOptions(snapshot);
 119                 try (InputStream is = snapshot.getStream(null, null)) {
 120                     Asserts.assertNotNull(is);
 121                 }
 122 
 123                 List<RecordedEvent> events = Events.fromRecording(snapshot);
 124                 Events.hasEvents(events);
 125                 Asserts.assertEquals(events.size(), 1);
 126                 Asserts.assertEquals(events.get(0).getEventType().getName(), SimpleEvent.class.getName());
 127             }
 128 
 129             r.stop();
 130         }
 131     }
 132 
 133     private static void assertStaticOptions(Recording snapshot) {
 134         Asserts.assertTrue(snapshot.getName().startsWith("Snapshot"), "Recording name should begin with 'Snapshot'");
 135         Asserts.assertEquals(snapshot.getMaxAge(), null);
 136         Asserts.assertEquals(snapshot.getMaxSize(), 0L);
 137         Asserts.assertTrue(snapshot.getSettings().isEmpty());
 138         Asserts.assertEquals(snapshot.getState(), RecordingState.STOPPED);
 139         Asserts.assertEquals(snapshot.getDumpOnExit(), false);
 140         Asserts.assertEquals(snapshot.getDestination(), null);
 141     }
 142 
 143     private static void testStopped() throws IOException {
 144         FlightRecorder recorder = FlightRecorder.getFlightRecorder();
 145         try (Recording r = new Recording()) {
 146             r.enable(SimpleEvent.class);
 147             r.start();
 148             SimpleEvent se = new SimpleEvent();
 149             se.commit();
 150             r.stop();
 151 
 152             try (Recording snapshot = recorder.takeSnapshot()) {
 153 
 154                 Asserts.assertEquals(snapshot.getSize(), r.getSize());
 155                 Asserts.assertGreaterThanOrEqual(snapshot.getStartTime(), r.getStartTime());
 156                 Asserts.assertLessThanOrEqual(snapshot.getStopTime(), r.getStopTime());
 157                 Asserts.assertGreaterThanOrEqual(snapshot.getDuration(), Duration.ZERO);
 158                 assertStaticOptions(snapshot);
 159                 try (InputStream is = snapshot.getStream(null, null)) {
 160                     Asserts.assertNotNull(is);
 161                 }
 162 
 163                 List<RecordedEvent> events = Events.fromRecording(snapshot);
 164                 Events.hasEvents(events);
 165                 Asserts.assertEquals(events.size(), 1);
 166                 Asserts.assertEquals(events.get(0).getEventType().getName(), SimpleEvent.class.getName());
 167             }
 168         }
 169     }
 170 
 171     private static void testEmpty() throws IOException {
 172         FlightRecorder recorder = FlightRecorder.getFlightRecorder();
 173         Instant before = Instant.now().minusNanos(1);
 174         try (Recording snapshot = recorder.takeSnapshot()) {
 175             Instant after = Instant.now().plusNanos(1);
 176             Asserts.assertEquals(snapshot.getSize(), 0L);
 177             Asserts.assertLessThan(before, snapshot.getStartTime());
 178             Asserts.assertGreaterThan(after, snapshot.getStopTime());
 179             Asserts.assertEquals(snapshot.getStartTime(), snapshot.getStopTime());
 180             Asserts.assertEquals(snapshot.getDuration(), Duration.ZERO);
 181             assertStaticOptions(snapshot);
 182             Asserts.assertEquals(snapshot.getStream(null, null), null);
 183         }
 184     }
 185 
 186 }