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