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