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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package jdk.jfr.api.event; 24 25 import java.time.Duration; 26 import java.util.ArrayList; 27 import java.util.List; 28 29 import jdk.jfr.Recording; 30 import jdk.jfr.consumer.RecordedEvent; 31 import jdk.test.lib.Asserts; 32 import jdk.test.lib.jfr.CommonHelper; 33 import jdk.test.lib.jfr.EventNames; 34 import jdk.test.lib.jfr.Events; 35 import jdk.test.lib.jfr.SimpleEvent; 36 37 /** 38 * @test 39 * @summary Test for RecordedEvent.getDuration() 40 * @key jfr 41 * @requires vm.hasJFR 42 * @library /test/lib 43 * @run main/othervm jdk.jfr.api.event.TestGetDuration 44 */ 45 public class TestGetDuration { 46 47 private static final int DURATIONAL_EVENT_ID = 1; 48 private static final int INSTANT_EVENT_ID = 2; 49 main(String[] args)50 public static void main(String[] args) throws Exception { 51 verifyCustomEvents(); 52 verifyNativeEvents(); 53 } 54 verifyCustomEvents()55 private static void verifyCustomEvents() throws Exception { 56 boolean fastTimeEnabled = CommonHelper.hasFastTimeEnabled(); 57 System.out.println("Fast time enabled: " + fastTimeEnabled); 58 Recording r = new Recording(); 59 r.enable(SimpleEvent.class).withoutStackTrace(); 60 r.enable(EventNames.CPUTimeStampCounter); // for debugging purposes 61 r.start(); 62 63 SimpleEvent durational = new SimpleEvent(); 64 durational.begin(); 65 durational.id = DURATIONAL_EVENT_ID; 66 if (!fastTimeEnabled) { 67 // if we have a low resolution clock sleep until time changes 68 CommonHelper.waitForSystemCurrentMillisToChange(); 69 } 70 durational.end(); 71 durational.commit(); 72 73 SimpleEvent instant = new SimpleEvent(); 74 instant.id = INSTANT_EVENT_ID; 75 instant.commit(); 76 77 r.stop(); 78 79 List<RecordedEvent> recordedEvents = Events.fromRecording(r); 80 List<RecordedEvent> testEvents = new ArrayList<>(); 81 for (RecordedEvent e : recordedEvents) { 82 System.out.println(e); // for debugging time related issues 83 if (!e.getEventType().getName().equals(EventNames.CPUTimeStampCounter)) { 84 testEvents.add(e); 85 } 86 } 87 Events.hasEvents(testEvents); 88 for (RecordedEvent re : testEvents) { 89 int id = re.getValue("id"); 90 Asserts.assertEquals(re.getDuration(), Duration.between(re.getStartTime(), re.getEndTime())); 91 switch (id) { 92 case DURATIONAL_EVENT_ID: 93 Asserts.assertTrue(!re.getDuration().isNegative() && !re.getDuration().isZero()); 94 break; 95 case INSTANT_EVENT_ID: 96 Asserts.assertTrue(re.getDuration().isZero()); 97 break; 98 } 99 } 100 } 101 verifyNativeEvents()102 private static void verifyNativeEvents() throws Exception { 103 Recording r = new Recording(); 104 r.enable(EventNames.JVMInformation); 105 r.enable(EventNames.ThreadSleep); 106 r.start(); 107 // Should trigger a sleep event even if we 108 // have a low resolution clock 109 Thread.sleep(200); 110 r.stop(); 111 List<RecordedEvent> recordedEvents = Events.fromRecording(r); 112 Events.hasEvents(recordedEvents); 113 for (RecordedEvent re : recordedEvents) { 114 Asserts.assertEquals(re.getDuration(), Duration.between(re.getStartTime(), re.getEndTime())); 115 switch (re.getEventType().getName()) { 116 case EventNames.JVMInformation: 117 Asserts.assertTrue(re.getDuration().isZero()); 118 break; 119 case EventNames.ThreadSleep: 120 Asserts.assertTrue(!re.getDuration().isNegative() && !re.getDuration().isZero()); 121 break; 122 default: 123 Asserts.fail("Unexpected event: " + re); 124 break; 125 } 126 } 127 } 128 129 } 130