1 /*
   2  * Copyright (c) 2016, 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 package jdk.jfr.event.runtime;
  26 
  27 import static jdk.testlibrary.Asserts.assertEquals;
  28 import static jdk.testlibrary.Asserts.assertNull;
  29 import static jdk.testlibrary.Asserts.assertTrue;
  30 
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.time.Duration;
  34 import java.time.Instant;
  35 import java.util.List;
  36 
  37 import jdk.jfr.EventType;
  38 import jdk.jfr.Recording;
  39 import jdk.jfr.Timespan;
  40 import jdk.jfr.Timestamp;
  41 import jdk.jfr.ValueDescriptor;
  42 import jdk.jfr.consumer.RecordedEvent;
  43 import jdk.testlibrary.jfr.EventNames;
  44 import jdk.testlibrary.jfr.Events;
  45 
  46 /*
  47  * @test
  48  * @summary Tests that the recording properties are properly reflected in the ActiveRecording event
  49  * @key jfr
  50  * @library /lib/testlibrary
  51  * @run main/othervm jdk.jfr.event.runtime.TestActiveRecordingEvent
  52  */
  53 public final class TestActiveRecordingEvent {
  54 
  55     private static final String ACTIVE_RECORDING_EVENT_NAME = EventNames.ActiveRecording;
  56 
  57     private static final Path MY_JFR_FILEPATH = Paths.get("", "my.jfr");
  58 
  59     private static final long MAX_SIZE = 1000000L;
  60 
  61     private static final Duration MAX_AGE = Duration.ofDays(1);
  62 
  63     private static final Duration REC_DURATION = Duration.ofMinutes(10);
  64 
  65     private static final String REC_NAME = "MYNAME";
  66 
  67     public static void main(String[] args) throws Throwable {
  68         testWithPath(null);
  69         testWithPath(MY_JFR_FILEPATH);
  70     }
  71 
  72     private static void testWithPath(Path path) throws Throwable {
  73         Recording recording = new Recording();
  74         recording.enable(ACTIVE_RECORDING_EVENT_NAME);
  75 
  76         recording.setDuration(REC_DURATION);
  77         recording.setMaxSize(MAX_SIZE);
  78         recording.setMaxAge(MAX_AGE);
  79         recording.setName(REC_NAME);
  80         if (path != null) {
  81             recording.setToDisk(true);
  82             recording.setDestination(path);
  83         }
  84 
  85         long tsBeforeStart = Instant.now().toEpochMilli();
  86         recording.start();
  87         recording.stop();
  88         long tsAfterStop = Instant.now().toEpochMilli();
  89 
  90         List<RecordedEvent> events = Events.fromRecording(recording);
  91 
  92         Events.hasEvents(events);
  93         RecordedEvent ev = events.get(0);
  94 
  95         // Duration must be kept in milliseconds
  96         assertEquals(REC_DURATION.toMillis(), ev.getValue("recordingDuration"));
  97 
  98         assertEquals(MAX_SIZE, ev.getValue("maxSize"));
  99 
 100         // maxAge must be kept in milliseconds
 101         assertEquals(MAX_AGE.toMillis(), ev.getValue("maxAge"));
 102 
 103         EventType evType = ev.getEventType();
 104         ValueDescriptor durationField = evType.getField("recordingDuration");
 105         assertEquals(durationField.getAnnotation(Timespan.class).value(), Timespan.MILLISECONDS);
 106 
 107         if (path == null) {
 108             assertNull(ev.getValue("destination"));
 109         } else {
 110             assertEquals(path.toAbsolutePath().toString(), ev.getValue("destination").toString());
 111         }
 112 
 113         ValueDescriptor recordingStartField = evType.getField("recordingStart");
 114         assertEquals(recordingStartField.getAnnotation(Timestamp.class).value(), Timestamp.MILLISECONDS_SINCE_EPOCH);
 115 
 116         long tsRecordingStart = ev.getValue("recordingStart");
 117         assertTrue(tsBeforeStart <= tsRecordingStart);
 118         assertTrue(tsAfterStop >= tsRecordingStart);
 119 
 120         assertEquals(recording.getId(), ev.getValue("id"));
 121 
 122         ValueDescriptor maxAgeField = evType.getField("maxAge");
 123         assertEquals(maxAgeField.getAnnotation(Timespan.class).value(), Timespan.MILLISECONDS);
 124     }
 125 }