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 
  26 package jdk.jfr.consumer;
  27 
  28 import java.time.Duration;
  29 import java.time.Instant;
  30 import java.util.List;
  31 
  32 import jdk.jfr.EventType;
  33 import jdk.jfr.ValueDescriptor;
  34 import jdk.jfr.internal.EventInstrumentation;
  35 
  36 /**
  37  * A recorded event.
  38  *
  39  * @since 9
  40  */
  41 public final class RecordedEvent extends RecordedObject {
  42     private final EventType eventType;
  43     private final long startTime;
  44     private final long endTime;
  45 
  46     // package private
  47     RecordedEvent(EventType type, List<ValueDescriptor> vds, Object[] values, long startTime, long endTime, TimeConverter timeConverter) {
  48         super(vds, values, timeConverter);
  49         this.eventType = type;
  50         this.startTime = startTime;
  51         this.endTime = endTime;
  52     }
  53 
  54     /**
  55      * Returns the stack trace that was created when the event was committed, or
  56      * {@code null} if the event lacks a stack trace.
  57      *
  58      * @return stack trace, or {@code null} if not available for event
  59      */
  60     public RecordedStackTrace getStackTrace() {
  61         return getTyped(EventInstrumentation.FIELD_STACK_TRACE, RecordedStackTrace.class, null);
  62     }
  63 
  64     /**
  65      * Returns the thread from which the event was committed, or {@code null} if
  66      * the thread was not recorded.
  67      *
  68      * @return thread, or {@code null} if not available for event
  69      */
  70     public RecordedThread getThread() {
  71         return getTyped(EventInstrumentation.FIELD_EVENT_THREAD, RecordedThread.class, null);
  72     }
  73 
  74     /**
  75      * Returns the event type that describes the event.
  76      *
  77      * @return the event type, not {@code null}
  78      */
  79     public EventType getEventType() {
  80         return eventType;
  81     }
  82 
  83     /**
  84      * Returns the start time of the event.
  85      * <p>
  86      * If the event is an instant event, then the start time and end time are the same.
  87      *
  88      * @return the start time, not {@code null}
  89      */
  90     public Instant getStartTime() {
  91         return Instant.ofEpochSecond(0, startTime);
  92     }
  93 
  94     /**
  95      * Returns the end time of the event.
  96      * <p>
  97      * If the event is an instant event, then the start time and end time are the same.
  98      *
  99      * @return the end time, not {@code null}
 100      */
 101     public Instant getEndTime() {
 102         return Instant.ofEpochSecond(0, endTime);
 103     }
 104 
 105     /**
 106      * Returns the duration of the event, measured in nanoseconds.
 107      *
 108      * @return the duration in nanoseconds, not {@code null}
 109      */
 110     public Duration getDuration() {
 111         return Duration.ofNanos(endTime - startTime);
 112     }
 113 
 114     /**
 115      * Returns the list of descriptors that describes the fields of the event.
 116      *
 117      * @return descriptors, not {@code null}
 118      */
 119     @Override
 120     public List<ValueDescriptor> getFields() {
 121         return getEventType().getFields();
 122     }
 123 }