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