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