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