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.api.event;
  26 
  27 import java.time.Duration;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.consumer.RecordedEvent;
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.jfr.CommonHelper;
  35 import jdk.test.lib.jfr.EventNames;
  36 import jdk.test.lib.jfr.Events;
  37 import jdk.test.lib.jfr.SimpleEvent;
  38 
  39 /**
  40  * @test
  41  * @summary Test for RecordedEvent.getDuration()
  42  * @key jfr
  43  *
  44  * @library /lib /
  45  * @run main/othervm jdk.jfr.api.event.TestGetDuration
  46  */
  47 public class TestGetDuration {
  48 
  49     private static final int DURATIONAL_EVENT_ID = 1;
  50     private static final int INSTANT_EVENT_ID = 2;
  51 
  52     public static void main(String[] args) throws Exception {
  53         verifyCustomEvents();
  54         verifyNativeEvents();
  55     }
  56 
  57     private static void verifyCustomEvents() throws Exception {
  58         boolean fastTimeEnabled = CommonHelper.hasFastTimeEnabled();
  59         System.out.println("Fast time enabled: " + fastTimeEnabled);
  60         Recording r = new Recording();
  61         r.enable(SimpleEvent.class).withoutStackTrace();
  62         r.enable(EventNames.CPUTimeStampCounter); // for debugging purposes
  63         r.start();
  64 
  65         SimpleEvent durational = new SimpleEvent();
  66         durational.begin();
  67         durational.id = DURATIONAL_EVENT_ID;
  68         if (!fastTimeEnabled) {
  69             // if we have a low resolution clock sleep until time changes
  70             CommonHelper.waitForSystemCurrentMillisToChange();
  71         }
  72         durational.end();
  73         durational.commit();
  74 
  75         SimpleEvent instant = new SimpleEvent();
  76         instant.id = INSTANT_EVENT_ID;
  77         instant.commit();
  78 
  79         r.stop();
  80 
  81         List<RecordedEvent> recordedEvents = Events.fromRecording(r);
  82         List<RecordedEvent> testEvents = new ArrayList<>();
  83         for (RecordedEvent e : recordedEvents) {
  84             System.out.println(e); // for debugging time related issues
  85             if (!e.getEventType().getName().equals(EventNames.CPUTimeStampCounter)) {
  86                 testEvents.add(e);
  87             }
  88         }
  89         Events.hasEvents(testEvents);
  90         for (RecordedEvent re : testEvents) {
  91             int id = re.getValue("id");
  92             Asserts.assertEquals(re.getDuration(), Duration.between(re.getStartTime(), re.getEndTime()));
  93             switch (id) {
  94                 case DURATIONAL_EVENT_ID:
  95                     Asserts.assertTrue(!re.getDuration().isNegative() && !re.getDuration().isZero());
  96                     break;
  97                 case INSTANT_EVENT_ID:
  98                     Asserts.assertTrue(re.getDuration().isZero());
  99                     break;
 100             }
 101         }
 102     }
 103 
 104     private static void verifyNativeEvents() throws Exception {
 105         Recording r = new Recording();
 106         r.enable(EventNames.JVMInformation);
 107         r.enable(EventNames.ThreadSleep);
 108         r.start();
 109         // Should trigger a sleep event even if we
 110         // have a low resolution clock
 111         Thread.sleep(200);
 112         r.stop();
 113         List<RecordedEvent> recordedEvents = Events.fromRecording(r);
 114         Events.hasEvents(recordedEvents);
 115         for (RecordedEvent re : recordedEvents) {
 116             Asserts.assertEquals(re.getDuration(), Duration.between(re.getStartTime(), re.getEndTime()));
 117             switch (re.getEventType().getName()) {
 118                 case EventNames.JVMInformation:
 119                     Asserts.assertTrue(re.getDuration().isZero());
 120                     break;
 121                 case EventNames.ThreadSleep:
 122                     Asserts.assertTrue(!re.getDuration().isNegative() && !re.getDuration().isZero());
 123                     break;
 124                 default:
 125                     Asserts.fail("Unexpected event: " + re);
 126                     break;
 127             }
 128         }
 129     }
 130 
 131 }