src/jdk.jfr/share/classes/jdk/jfr/internal/tool/EventPrintWriter.java
changeset 52850 f527b24990d7
child 58145 bc54ed8d908a
equal deleted inserted replaced
52849:eef755718cb2 52850:f527b24990d7
       
     1 /*
       
     2  * Copyright (c) 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.internal.tool;
       
    27 
       
    28 import java.io.FileNotFoundException;
       
    29 import java.io.IOException;
       
    30 import java.io.PrintWriter;
       
    31 import java.nio.file.Path;
       
    32 import java.util.ArrayList;
       
    33 import java.util.HashMap;
       
    34 import java.util.List;
       
    35 import java.util.Map;
       
    36 import java.util.function.Predicate;
       
    37 
       
    38 import jdk.jfr.EventType;
       
    39 import jdk.jfr.Timespan;
       
    40 import jdk.jfr.Timestamp;
       
    41 import jdk.jfr.ValueDescriptor;
       
    42 import jdk.jfr.consumer.RecordedEvent;
       
    43 import jdk.jfr.consumer.RecordedObject;
       
    44 import jdk.jfr.consumer.RecordingFile;
       
    45 import jdk.jfr.internal.consumer.RecordingInternals;
       
    46 
       
    47 abstract class EventPrintWriter extends StructuredWriter {
       
    48 
       
    49     enum ValueType {
       
    50         TIMESPAN, TIMESTAMP, OTHER
       
    51     }
       
    52 
       
    53     protected static final String STACK_TRACE_FIELD = "stackTrace";
       
    54     protected static final String EVENT_THREAD_FIELD = "eventThread";
       
    55 
       
    56     private Predicate<EventType> eventFilter = x -> true;
       
    57     private int stackDepth;
       
    58 
       
    59     // cach that will speed up annotation lookup
       
    60     private Map<ValueDescriptor, ValueType> typeOfValues = new HashMap<>();
       
    61 
       
    62     EventPrintWriter(PrintWriter p) {
       
    63         super(p);
       
    64     }
       
    65 
       
    66     abstract protected void print(List<RecordedEvent> events);
       
    67 
       
    68     void print(Path source) throws FileNotFoundException, IOException {
       
    69         List<RecordedEvent> events = new ArrayList<>(500_000);
       
    70         printBegin();
       
    71         try (RecordingFile file = new RecordingFile(source)) {
       
    72             while (file.hasMoreEvents()) {
       
    73                 RecordedEvent event = file.readEvent();
       
    74                 if (acceptEvent(event)) {
       
    75                     events.add(event);
       
    76                 }
       
    77                 if (RecordingInternals.INSTANCE.isLastEventInChunk(file)) {
       
    78                     RecordingInternals.INSTANCE.sort(events);
       
    79                     print(events);
       
    80                     events.clear();
       
    81                 }
       
    82             }
       
    83         }
       
    84         printEnd();
       
    85         flush(true);
       
    86     }
       
    87 
       
    88     protected void printEnd() {
       
    89     }
       
    90 
       
    91     protected void printBegin() {
       
    92     }
       
    93 
       
    94     public final void setEventFilter(Predicate<EventType> eventFilter) {
       
    95         this.eventFilter = eventFilter;
       
    96     }
       
    97 
       
    98     protected final boolean acceptEvent(RecordedEvent event) {
       
    99         return eventFilter.test(event.getEventType());
       
   100     }
       
   101 
       
   102     protected final int getStackDepth() {
       
   103         return stackDepth;
       
   104     }
       
   105 
       
   106     protected final boolean isLateField(String name) {
       
   107         return name.equals(EVENT_THREAD_FIELD) || name.equals(STACK_TRACE_FIELD);
       
   108     }
       
   109 
       
   110     public void setStackDepth(int stackDepth) {
       
   111         this.stackDepth = stackDepth;
       
   112     }
       
   113 
       
   114     protected Object getValue(RecordedObject object, ValueDescriptor v) {
       
   115         ValueType valueType = typeOfValues.get(v);
       
   116         if (valueType == null) {
       
   117             valueType = determineValueType(v);
       
   118             typeOfValues.put(v, valueType);
       
   119         }
       
   120         switch (valueType) {
       
   121         case TIMESPAN:
       
   122             return object.getDuration(v.getName());
       
   123         case TIMESTAMP:
       
   124             return RecordingInternals.INSTANCE.getOffsetDataTime(object, v.getName());
       
   125         default:
       
   126             return object.getValue(v.getName());
       
   127         }
       
   128     }
       
   129     // It's expensive t check
       
   130     private ValueType determineValueType(ValueDescriptor v) {
       
   131         if (v.getAnnotation(Timespan.class) != null) {
       
   132             return ValueType.TIMESPAN;
       
   133         }
       
   134         if (v.getAnnotation(Timestamp.class) != null) {
       
   135             return ValueType.TIMESTAMP;
       
   136         }
       
   137         return ValueType.OTHER;
       
   138     }
       
   139 }