50113
|
1 |
/*
|
57360
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
50113
|
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.io.IOException;
|
|
29 |
import java.util.Collection;
|
|
30 |
import java.util.List;
|
57360
|
31 |
import java.util.StringJoiner;
|
50113
|
32 |
|
|
33 |
import jdk.jfr.EventType;
|
|
34 |
import jdk.jfr.internal.LogLevel;
|
|
35 |
import jdk.jfr.internal.LogTag;
|
|
36 |
import jdk.jfr.internal.Logger;
|
|
37 |
import jdk.jfr.internal.MetadataDescriptor;
|
|
38 |
import jdk.jfr.internal.Type;
|
57360
|
39 |
import jdk.jfr.internal.Utils;
|
50113
|
40 |
import jdk.jfr.internal.consumer.ChunkHeader;
|
57360
|
41 |
import jdk.jfr.internal.consumer.Parser;
|
50113
|
42 |
import jdk.jfr.internal.consumer.RecordingInput;
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Parses a chunk.
|
|
46 |
*
|
|
47 |
*/
|
|
48 |
final class ChunkParser {
|
|
49 |
private static final long CONSTANT_POOL_TYPE_ID = 1;
|
|
50 |
private final RecordingInput input;
|
|
51 |
private final ChunkHeader chunkHeader;
|
|
52 |
private final MetadataDescriptor metadata;
|
|
53 |
private final TimeConverter timeConverter;
|
57360
|
54 |
private final MetadataDescriptor previousMetadata;
|
|
55 |
private final long pollInterval;
|
|
56 |
private final LongMap<ConstantLookup> constantLookups;
|
|
57 |
|
|
58 |
private LongMap<Type> typeMap;
|
|
59 |
private LongMap<Parser> parsers;
|
|
60 |
private boolean chunkFinished;
|
|
61 |
private InternalEventFilter eventFilter = InternalEventFilter.ACCEPT_ALL;
|
50113
|
62 |
|
|
63 |
public ChunkParser(RecordingInput input) throws IOException {
|
57360
|
64 |
this(new ChunkHeader(input), null, 500);
|
50113
|
65 |
}
|
|
66 |
|
57360
|
67 |
private ChunkParser(ChunkHeader header, ChunkParser previous, long pollInterval) throws IOException {
|
50113
|
68 |
this.input = header.getInput();
|
|
69 |
this.chunkHeader = header;
|
57360
|
70 |
if (previous == null) {
|
|
71 |
this.pollInterval = 500;
|
|
72 |
this.constantLookups = new LongMap<>();
|
|
73 |
this.previousMetadata = null;
|
|
74 |
} else {
|
|
75 |
this.constantLookups = previous.constantLookups;
|
|
76 |
this.previousMetadata = previous.metadata;
|
|
77 |
this.pollInterval = previous.pollInterval;
|
|
78 |
}
|
|
79 |
this.metadata = header.readMetadata(previousMetadata);
|
52850
|
80 |
this.timeConverter = new TimeConverter(chunkHeader, metadata.getGMTOffset());
|
57360
|
81 |
if (metadata != previousMetadata) {
|
|
82 |
ParserFactory factory = new ParserFactory(metadata, constantLookups, timeConverter);
|
|
83 |
parsers = factory.getParsers();
|
|
84 |
typeMap = factory.getTypeMap();
|
|
85 |
} else {
|
|
86 |
parsers = previous.parsers;
|
|
87 |
typeMap = previous.typeMap;
|
|
88 |
}
|
|
89 |
constantLookups.forEach(c -> c.newPool());
|
|
90 |
fillConstantPools(0);
|
|
91 |
constantLookups.forEach(c -> c.getLatestPool().setResolving());
|
|
92 |
constantLookups.forEach(c -> c.getLatestPool().resolve());
|
|
93 |
constantLookups.forEach(c -> c.getLatestPool().setResolved());
|
50113
|
94 |
|
|
95 |
input.position(chunkHeader.getEventStart());
|
|
96 |
}
|
|
97 |
|
57360
|
98 |
public void setParserFilter(InternalEventFilter filter) {
|
|
99 |
// Disable low level filter, since it doesn't work
|
|
100 |
// when a psrser is shared
|
|
101 |
// this.eventFilter = filter;
|
|
102 |
// updateParserFilters();
|
|
103 |
}
|
|
104 |
|
|
105 |
public InternalEventFilter getEventFilter() {
|
|
106 |
return this.eventFilter;
|
|
107 |
}
|
|
108 |
|
|
109 |
private void updateParserFilters() {
|
|
110 |
parsers.forEach(p -> {
|
|
111 |
if (p instanceof EventParser) {
|
|
112 |
EventParser ep = (EventParser) p;
|
|
113 |
long threshold = eventFilter.getThreshold(ep.getEventType().getName());
|
|
114 |
if (threshold >= 0) {
|
|
115 |
ep.setEnabled(true);
|
|
116 |
ep.setThreshold(timeConverter.convertDurationNanos(threshold));
|
|
117 |
} else {
|
|
118 |
ep.setThreshold(-1L);
|
|
119 |
}
|
|
120 |
}
|
|
121 |
});
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Reads an event and returns null when segment or chunk ends.
|
|
126 |
*/
|
|
127 |
public RecordedEvent readStreamingEvent(boolean awaitNewEvents) throws IOException {
|
|
128 |
long absoluteChunkEnd = chunkHeader.getEnd();
|
|
129 |
while (true) {
|
|
130 |
RecordedEvent event = readEvent();
|
|
131 |
if (event != null) {
|
|
132 |
return event;
|
|
133 |
}
|
|
134 |
if (!awaitNewEvents) {
|
|
135 |
return null;
|
|
136 |
}
|
|
137 |
long lastValid = absoluteChunkEnd;
|
|
138 |
long metadataPoistion = chunkHeader.getMetataPosition();
|
|
139 |
long contantPosition = chunkHeader.getConstantPoolPosition();
|
|
140 |
chunkFinished = awaitUpdatedHeader(absoluteChunkEnd);
|
|
141 |
if (chunkFinished) {
|
|
142 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "At chunk end");
|
|
143 |
return null;
|
|
144 |
}
|
|
145 |
absoluteChunkEnd = chunkHeader.getEnd();
|
|
146 |
// Read metadata and constant pools for the next segment
|
|
147 |
if (chunkHeader.getMetataPosition() != metadataPoistion) {
|
|
148 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found new metadata in chunk. Rebuilding types and parsers");
|
|
149 |
MetadataDescriptor metadata = chunkHeader.readMetadata(previousMetadata);
|
|
150 |
ParserFactory factory = new ParserFactory(metadata, constantLookups, timeConverter);
|
|
151 |
parsers = factory.getParsers();
|
|
152 |
typeMap = factory.getTypeMap();
|
|
153 |
updateParserFilters();
|
|
154 |
}
|
|
155 |
if (contantPosition != chunkHeader.getConstantPoolPosition()) {
|
|
156 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found new constant pool data. Filling up pools with new values");
|
|
157 |
constantLookups.forEach(c -> c.getLatestPool().setAllResolved(false));
|
|
158 |
fillConstantPools(contantPosition + chunkHeader.getAbsoluteChunkStart());
|
|
159 |
constantLookups.forEach(c -> c.getLatestPool().setResolving());
|
|
160 |
constantLookups.forEach(c -> c.getLatestPool().resolve());
|
|
161 |
constantLookups.forEach(c -> c.getLatestPool().setResolved());
|
|
162 |
}
|
|
163 |
input.position(lastValid);
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
/**
|
|
168 |
* Reads an event and returns null when the chunk ends
|
|
169 |
*/
|
50113
|
170 |
public RecordedEvent readEvent() throws IOException {
|
57360
|
171 |
long absoluteChunkEnd = chunkHeader.getEnd();
|
50113
|
172 |
while (input.position() < absoluteChunkEnd) {
|
|
173 |
long pos = input.position();
|
|
174 |
int size = input.readInt();
|
|
175 |
if (size == 0) {
|
|
176 |
throw new IOException("Event can't have zero size");
|
|
177 |
}
|
|
178 |
long typeId = input.readLong();
|
57360
|
179 |
// Skip metadata and constant pool events (id = 0, id = 1)
|
|
180 |
if (typeId > CONSTANT_POOL_TYPE_ID) {
|
|
181 |
Parser p = parsers.get(typeId);
|
|
182 |
if (p instanceof EventParser) {
|
|
183 |
EventParser ep = (EventParser) p;
|
|
184 |
RecordedEvent event = ep.parse(input);
|
|
185 |
if (event != null) {
|
|
186 |
input.position(pos + size);
|
|
187 |
return event;
|
|
188 |
}
|
50113
|
189 |
}
|
|
190 |
}
|
|
191 |
input.position(pos + size);
|
|
192 |
}
|
|
193 |
return null;
|
|
194 |
}
|
|
195 |
|
57360
|
196 |
private boolean awaitUpdatedHeader(long absoluteChunkEnd) throws IOException {
|
|
197 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Waiting for more data (streaming). Read so far: " + chunkHeader.getChunkSize() + " bytes");
|
|
198 |
while (true) {
|
|
199 |
chunkHeader.refresh();
|
|
200 |
if (absoluteChunkEnd != chunkHeader.getEnd()) {
|
|
201 |
return false;
|
|
202 |
}
|
|
203 |
if (chunkHeader.isFinished()) {
|
|
204 |
return true;
|
|
205 |
}
|
|
206 |
Utils.takeNap(pollInterval);
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
private void fillConstantPools(long abortCP) throws IOException {
|
|
211 |
long thisCP = chunkHeader.getConstantPoolPosition() + chunkHeader.getAbsoluteChunkStart();
|
|
212 |
long lastCP = -1;
|
|
213 |
long delta = -1;
|
|
214 |
boolean log = Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE);
|
|
215 |
while (thisCP != abortCP && delta != 0) {
|
|
216 |
input.position(thisCP);
|
|
217 |
lastCP = thisCP;
|
50113
|
218 |
int size = input.readInt(); // size
|
|
219 |
long typeId = input.readLong();
|
|
220 |
if (typeId != CONSTANT_POOL_TYPE_ID) {
|
57360
|
221 |
throw new IOException("Expected check point event (id = 1) at position " + lastCP + ", but found type id = " + typeId);
|
50113
|
222 |
}
|
|
223 |
input.readLong(); // timestamp
|
|
224 |
input.readLong(); // duration
|
57360
|
225 |
delta = input.readLong();
|
|
226 |
thisCP += delta;
|
50113
|
227 |
boolean flush = input.readBoolean();
|
|
228 |
int poolCount = input.readInt();
|
57360
|
229 |
final long logLastCP = lastCP;
|
|
230 |
final long logDelta = delta;
|
50113
|
231 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, () -> {
|
57360
|
232 |
return "New constant pool: startPosition=" + logLastCP + ", size=" + size + ", deltaToNext=" + logDelta + ", flush=" + flush + ", poolCount=" + poolCount;
|
50113
|
233 |
});
|
|
234 |
for (int i = 0; i < poolCount; i++) {
|
|
235 |
long id = input.readLong(); // type id
|
57360
|
236 |
ConstantLookup lookup = constantLookups.get(id);
|
50113
|
237 |
Type type = typeMap.get(id);
|
57360
|
238 |
if (lookup == null) {
|
50113
|
239 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found constant pool(" + id + ") that is never used");
|
|
240 |
if (type == null) {
|
57360
|
241 |
throw new IOException(
|
|
242 |
"Error parsing constant pool type " + getName(id) + " at position " + input.position() + " at check point between [" + lastCP + ", " + lastCP + size + "]");
|
50113
|
243 |
}
|
57360
|
244 |
ConstantMap pool = new ConstantMap(ObjectFactory.create(type, timeConverter), type.getName());
|
|
245 |
constantLookups.put(type.getId(), new ConstantLookup(pool, type));
|
50113
|
246 |
}
|
57360
|
247 |
Parser parser = parsers.get(id);
|
50113
|
248 |
if (parser == null) {
|
|
249 |
throw new IOException("Could not find constant pool type with id = " + id);
|
|
250 |
}
|
|
251 |
try {
|
|
252 |
int count = input.readInt();
|
57360
|
253 |
if (count == 0) {
|
|
254 |
throw new InternalError("Pool " + type.getName() + " must contain at least one element ");
|
|
255 |
}
|
|
256 |
if (log) {
|
|
257 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, "Constant Pool " + i + ": " + type.getName());
|
|
258 |
}
|
50113
|
259 |
for (int j = 0; j < count; j++) {
|
|
260 |
long key = input.readLong();
|
57360
|
261 |
// Object resolved = lookup.getCurrent(key);
|
|
262 |
// Disable cache Object resolved = lookup.getResolved(key);
|
|
263 |
// if (resolved == null) {
|
|
264 |
Object v = parser.parse(input);
|
|
265 |
logConstant(key, v, false);
|
|
266 |
lookup.getLatestPool().put(key, v);
|
|
267 |
// } else {
|
|
268 |
// parser.skip(input);
|
|
269 |
// logConstant(key, resolved, true);
|
|
270 |
// Disable cache lookup.getLatestPool().putResolved(key, resolved);
|
|
271 |
// }
|
50113
|
272 |
}
|
|
273 |
} catch (Exception e) {
|
57360
|
274 |
throw new IOException("Error parsing constant pool type " + getName(id) + " at position " + input.position() + " at check point between [" + lastCP + ", " + lastCP + size + "]",
|
|
275 |
e);
|
50113
|
276 |
}
|
|
277 |
}
|
57360
|
278 |
if (input.position() != lastCP + size) {
|
50113
|
279 |
throw new IOException("Size of check point event doesn't match content");
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
57360
|
284 |
private void logConstant(long key, Object v, boolean preresolved) {
|
|
285 |
if (!Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE)) {
|
|
286 |
return;
|
|
287 |
}
|
|
288 |
String valueText;
|
|
289 |
if (v.getClass().isArray()) {
|
|
290 |
Object[] array = (Object[]) v;
|
|
291 |
StringJoiner sj = new StringJoiner(", ", "{", "}");
|
|
292 |
for (int i = 0; i < array.length; i++) {
|
|
293 |
sj.add(textify(array[i]));
|
|
294 |
}
|
|
295 |
valueText = sj.toString();
|
|
296 |
} else {
|
|
297 |
valueText = textify(v);
|
|
298 |
}
|
|
299 |
String suffix = preresolved ? " (presolved)" :"";
|
|
300 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, "Constant: " + key + " = " + valueText + suffix);
|
|
301 |
}
|
|
302 |
|
|
303 |
private String textify(Object o) {
|
|
304 |
if (o == null) { // should not happen
|
|
305 |
return "null";
|
|
306 |
}
|
|
307 |
if (o instanceof String) {
|
|
308 |
return "\"" + String.valueOf(o) + "\"";
|
|
309 |
}
|
|
310 |
if (o instanceof RecordedObject) {
|
|
311 |
return o.getClass().getName();
|
|
312 |
}
|
|
313 |
if (o.getClass().isArray()) {
|
|
314 |
Object[] array = (Object[]) o;
|
|
315 |
if (array.length > 0) {
|
|
316 |
return textify(array[0]) + "[]"; // can it be recursive?
|
|
317 |
}
|
|
318 |
}
|
|
319 |
return String.valueOf(o);
|
|
320 |
}
|
|
321 |
|
50113
|
322 |
private String getName(long id) {
|
|
323 |
Type type = typeMap.get(id);
|
52850
|
324 |
return type == null ? ("unknown(" + id + ")") : type.getName();
|
50113
|
325 |
}
|
|
326 |
|
|
327 |
public Collection<Type> getTypes() {
|
|
328 |
return metadata.getTypes();
|
|
329 |
}
|
|
330 |
|
|
331 |
public List<EventType> getEventTypes() {
|
|
332 |
return metadata.getEventTypes();
|
|
333 |
}
|
|
334 |
|
57360
|
335 |
public boolean isLastChunk() throws IOException {
|
50113
|
336 |
return chunkHeader.isLastChunk();
|
|
337 |
}
|
|
338 |
|
|
339 |
public ChunkParser nextChunkParser() throws IOException {
|
57360
|
340 |
return new ChunkParser(chunkHeader.nextHeader(), this, pollInterval);
|
|
341 |
}
|
|
342 |
|
|
343 |
public boolean isChunkFinished() {
|
|
344 |
return chunkFinished;
|
50113
|
345 |
}
|
|
346 |
}
|