author | egahlin |
Fri, 29 Nov 2019 17:31:01 +0100 | |
changeset 59327 | 2c3578aa0bdf |
parent 59310 | 72f3dd43dd28 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58863 | 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 |
||
58863 | 26 |
package jdk.jfr.internal.consumer; |
50113 | 27 |
|
28 |
import java.io.IOException; |
|
29 |
import java.util.Collection; |
|
30 |
import java.util.List; |
|
58863 | 31 |
import java.util.StringJoiner; |
50113 | 32 |
|
33 |
import jdk.jfr.EventType; |
|
58863 | 34 |
import jdk.jfr.consumer.RecordedEvent; |
35 |
import jdk.jfr.consumer.RecordedObject; |
|
50113 | 36 |
import jdk.jfr.internal.LogLevel; |
37 |
import jdk.jfr.internal.LogTag; |
|
38 |
import jdk.jfr.internal.Logger; |
|
58863 | 39 |
import jdk.jfr.internal.LongMap; |
50113 | 40 |
import jdk.jfr.internal.MetadataDescriptor; |
41 |
import jdk.jfr.internal.Type; |
|
58863 | 42 |
import jdk.jfr.internal.Utils; |
50113 | 43 |
|
44 |
/** |
|
45 |
* Parses a chunk. |
|
46 |
* |
|
47 |
*/ |
|
58863 | 48 |
public final class ChunkParser { |
49 |
||
50 |
static final class ParserConfiguration { |
|
51 |
private final boolean reuse; |
|
52 |
private final boolean ordered; |
|
53 |
private final ParserFilter eventFilter; |
|
54 |
||
55 |
long filterStart; |
|
56 |
long filterEnd; |
|
57 |
||
58 |
ParserConfiguration(long filterStart, long filterEnd, boolean reuse, boolean ordered, ParserFilter filter) { |
|
59 |
this.filterStart = filterStart; |
|
60 |
this.filterEnd = filterEnd; |
|
61 |
this.reuse = reuse; |
|
62 |
this.ordered = ordered; |
|
63 |
this.eventFilter = filter; |
|
64 |
} |
|
65 |
||
66 |
public ParserConfiguration() { |
|
67 |
this(0, Long.MAX_VALUE, false, false, ParserFilter.ACCEPT_ALL); |
|
68 |
} |
|
50113 | 69 |
|
58863 | 70 |
public boolean isOrdered() { |
71 |
return ordered; |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
private enum CheckPointType { |
|
76 |
// Checkpoint that finishes a flush segment |
|
77 |
FLUSH(1), |
|
78 |
// Checkpoint contains chunk header information in the first pool |
|
79 |
CHUNK_HEADER(2), |
|
80 |
// Checkpoint contains only statics that will not change from chunk to chunk |
|
81 |
STATICS(4), |
|
82 |
// Checkpoint contains thread related information |
|
83 |
THREAD(8); |
|
84 |
private final int mask; |
|
85 |
private CheckPointType(int mask) { |
|
86 |
this.mask = mask; |
|
87 |
} |
|
88 |
||
89 |
private boolean is(int flags) { |
|
90 |
return (mask & flags) != 0; |
|
91 |
} |
|
50113 | 92 |
} |
93 |
||
58863 | 94 |
private static final long CONSTANT_POOL_TYPE_ID = 1; |
95 |
private static final String CHUNKHEADER = "jdk.types.ChunkHeader"; |
|
96 |
private final RecordingInput input; |
|
97 |
private final ChunkHeader chunkHeader; |
|
98 |
private final MetadataDescriptor metadata; |
|
99 |
private final TimeConverter timeConverter; |
|
100 |
private final MetadataDescriptor previousMetadata; |
|
101 |
private final LongMap<ConstantLookup> constantLookups; |
|
102 |
||
103 |
private LongMap<Type> typeMap; |
|
104 |
private LongMap<Parser> parsers; |
|
105 |
private boolean chunkFinished; |
|
106 |
||
107 |
private Runnable flushOperation; |
|
108 |
private ParserConfiguration configuration; |
|
59310
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
109 |
private volatile boolean closed; |
58863 | 110 |
|
111 |
public ChunkParser(RecordingInput input) throws IOException { |
|
112 |
this(input, new ParserConfiguration()); |
|
113 |
} |
|
114 |
||
115 |
ChunkParser(RecordingInput input, ParserConfiguration pc) throws IOException { |
|
116 |
this(new ChunkHeader(input), null, pc); |
|
117 |
} |
|
118 |
||
119 |
private ChunkParser(ChunkParser previous) throws IOException { |
|
120 |
this(new ChunkHeader(previous.input), previous, new ParserConfiguration()); |
|
121 |
} |
|
122 |
||
123 |
private ChunkParser(ChunkHeader header, ChunkParser previous, ParserConfiguration pc) throws IOException { |
|
124 |
this.configuration = pc; |
|
50113 | 125 |
this.input = header.getInput(); |
126 |
this.chunkHeader = header; |
|
58863 | 127 |
if (previous == null) { |
128 |
this.constantLookups = new LongMap<>(); |
|
129 |
this.previousMetadata = null; |
|
130 |
} else { |
|
131 |
this.constantLookups = previous.constantLookups; |
|
132 |
this.previousMetadata = previous.metadata; |
|
133 |
this.configuration = previous.configuration; |
|
134 |
} |
|
135 |
this.metadata = header.readMetadata(previousMetadata); |
|
52850 | 136 |
this.timeConverter = new TimeConverter(chunkHeader, metadata.getGMTOffset()); |
58863 | 137 |
if (metadata != previousMetadata) { |
138 |
ParserFactory factory = new ParserFactory(metadata, constantLookups, timeConverter); |
|
139 |
parsers = factory.getParsers(); |
|
140 |
typeMap = factory.getTypeMap(); |
|
141 |
updateConfiguration(); |
|
142 |
} else { |
|
143 |
parsers = previous.parsers; |
|
144 |
typeMap = previous.typeMap; |
|
145 |
} |
|
146 |
constantLookups.forEach(c -> c.newPool()); |
|
147 |
fillConstantPools(0); |
|
148 |
constantLookups.forEach(c -> c.getLatestPool().setResolving()); |
|
149 |
constantLookups.forEach(c -> c.getLatestPool().resolve()); |
|
150 |
constantLookups.forEach(c -> c.getLatestPool().setResolved()); |
|
50113 | 151 |
|
152 |
input.position(chunkHeader.getEventStart()); |
|
153 |
} |
|
154 |
||
58863 | 155 |
public ChunkParser nextChunkParser() throws IOException { |
156 |
return new ChunkParser(chunkHeader.nextHeader(), this, configuration); |
|
157 |
} |
|
158 |
||
159 |
private void updateConfiguration() { |
|
160 |
updateConfiguration(configuration, false); |
|
161 |
} |
|
162 |
||
163 |
void updateConfiguration(ParserConfiguration configuration, boolean resetEventCache) { |
|
164 |
this.configuration = configuration; |
|
165 |
parsers.forEach(p -> { |
|
166 |
if (p instanceof EventParser) { |
|
167 |
EventParser ep = (EventParser) p; |
|
168 |
if (resetEventCache) { |
|
169 |
ep.resetCache(); |
|
170 |
} |
|
171 |
String name = ep.getEventType().getName(); |
|
172 |
ep.setOrdered(configuration.ordered); |
|
173 |
ep.setReuse(configuration.reuse); |
|
174 |
ep.setFilterStart(configuration.filterStart); |
|
175 |
ep.setFilterEnd(configuration.filterEnd); |
|
176 |
long threshold = configuration.eventFilter.getThreshold(name); |
|
177 |
if (threshold >= 0) { |
|
178 |
ep.setEnabled(true); |
|
179 |
ep.setThresholdNanos(threshold); |
|
180 |
} else { |
|
181 |
ep.setEnabled(false); |
|
182 |
ep.setThresholdNanos(Long.MAX_VALUE); |
|
183 |
} |
|
184 |
} |
|
185 |
}); |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Reads an event and returns null when segment or chunk ends. |
|
190 |
* |
|
191 |
* @param awaitNewEvents wait for new data. |
|
192 |
*/ |
|
59327
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
193 |
RecordedEvent readStreamingEvent() throws IOException { |
58863 | 194 |
long absoluteChunkEnd = chunkHeader.getEnd(); |
59327
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
195 |
RecordedEvent event = readEvent(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
196 |
if (event != null) { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
197 |
return event; |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
198 |
} |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
199 |
long lastValid = absoluteChunkEnd; |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
200 |
long metadataPosition = chunkHeader.getMetataPosition(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
201 |
long contantPosition = chunkHeader.getConstantPoolPosition(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
202 |
chunkFinished = awaitUpdatedHeader(absoluteChunkEnd, configuration.filterEnd); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
203 |
if (chunkFinished) { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
204 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "At chunk end"); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
205 |
return null; |
58863 | 206 |
} |
59327
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
207 |
absoluteChunkEnd = chunkHeader.getEnd(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
208 |
// Read metadata and constant pools for the next segment |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
209 |
if (chunkHeader.getMetataPosition() != metadataPosition) { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
210 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found new metadata in chunk. Rebuilding types and parsers"); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
211 |
MetadataDescriptor metadata = chunkHeader.readMetadata(previousMetadata); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
212 |
ParserFactory factory = new ParserFactory(metadata, constantLookups, timeConverter); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
213 |
parsers = factory.getParsers(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
214 |
typeMap = factory.getTypeMap(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
215 |
updateConfiguration(); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
216 |
} |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
217 |
if (contantPosition != chunkHeader.getConstantPoolPosition()) { |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
218 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found new constant pool data. Filling up pools with new values"); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
219 |
constantLookups.forEach(c -> c.getLatestPool().setAllResolved(false)); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
220 |
fillConstantPools(contantPosition + chunkHeader.getAbsoluteChunkStart()); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
221 |
constantLookups.forEach(c -> c.getLatestPool().setResolving()); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
222 |
constantLookups.forEach(c -> c.getLatestPool().resolve()); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
223 |
constantLookups.forEach(c -> c.getLatestPool().setResolved()); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
224 |
} |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
225 |
input.position(lastValid); |
2c3578aa0bdf
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents:
59310
diff
changeset
|
226 |
return null; |
58863 | 227 |
} |
228 |
||
229 |
/** |
|
230 |
* Reads an event and returns null when the chunk ends |
|
231 |
*/ |
|
50113 | 232 |
public RecordedEvent readEvent() throws IOException { |
58863 | 233 |
long absoluteChunkEnd = chunkHeader.getEnd(); |
50113 | 234 |
while (input.position() < absoluteChunkEnd) { |
235 |
long pos = input.position(); |
|
236 |
int size = input.readInt(); |
|
237 |
if (size == 0) { |
|
238 |
throw new IOException("Event can't have zero size"); |
|
239 |
} |
|
240 |
long typeId = input.readLong(); |
|
58863 | 241 |
Parser p = parsers.get(typeId); |
242 |
if (p instanceof EventParser) { |
|
243 |
// Fast path |
|
244 |
EventParser ep = (EventParser) p; |
|
245 |
RecordedEvent event = ep.parse(input); |
|
246 |
if (event != null) { |
|
247 |
input.position(pos + size); |
|
248 |
return event; |
|
249 |
} |
|
250 |
// Not accepted by filter |
|
251 |
} else { |
|
252 |
if (typeId == 1) { // checkpoint event |
|
253 |
if (flushOperation != null) { |
|
254 |
parseCheckpoint(); |
|
255 |
} |
|
256 |
} else { |
|
257 |
if (typeId != 0) { // Not metadata event |
|
258 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Unknown event type " + typeId); |
|
259 |
} |
|
50113 | 260 |
} |
261 |
} |
|
262 |
input.position(pos + size); |
|
263 |
} |
|
264 |
return null; |
|
265 |
} |
|
266 |
||
58863 | 267 |
private void parseCheckpoint() throws IOException { |
268 |
// Content has been parsed previously. This |
|
269 |
// is to trigger flush |
|
270 |
input.readLong(); // timestamp |
|
271 |
input.readLong(); // duration |
|
272 |
input.readLong(); // delta |
|
273 |
byte typeFlags = input.readByte(); |
|
274 |
if (CheckPointType.FLUSH.is(typeFlags)) { |
|
275 |
flushOperation.run(); |
|
276 |
} |
|
277 |
} |
|
278 |
||
59274
eb3e2a5c2bcd
8233870: JFR TestSetEndTime.java times out - onClose() is never called
egahlin
parents:
59226
diff
changeset
|
279 |
private boolean awaitUpdatedHeader(long absoluteChunkEnd, long filterEnd) throws IOException { |
58863 | 280 |
if (Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO)) { |
281 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Waiting for more data (streaming). Read so far: " + chunkHeader.getChunkSize() + " bytes"); |
|
282 |
} |
|
283 |
while (true) { |
|
59310
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
284 |
if (closed) { |
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
285 |
return true; |
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
286 |
} |
59274
eb3e2a5c2bcd
8233870: JFR TestSetEndTime.java times out - onClose() is never called
egahlin
parents:
59226
diff
changeset
|
287 |
if (chunkHeader.getLastNanos() > filterEnd) { |
eb3e2a5c2bcd
8233870: JFR TestSetEndTime.java times out - onClose() is never called
egahlin
parents:
59226
diff
changeset
|
288 |
return true; |
eb3e2a5c2bcd
8233870: JFR TestSetEndTime.java times out - onClose() is never called
egahlin
parents:
59226
diff
changeset
|
289 |
} |
58863 | 290 |
chunkHeader.refresh(); |
291 |
if (absoluteChunkEnd != chunkHeader.getEnd()) { |
|
292 |
return false; |
|
293 |
} |
|
294 |
if (chunkHeader.isFinished()) { |
|
295 |
return true; |
|
296 |
} |
|
297 |
Utils.waitFlush(1000); |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
private void fillConstantPools(long abortCP) throws IOException { |
|
302 |
long thisCP = chunkHeader.getConstantPoolPosition() + chunkHeader.getAbsoluteChunkStart(); |
|
303 |
long lastCP = -1; |
|
304 |
long delta = -1; |
|
305 |
boolean logTrace = Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE); |
|
306 |
while (thisCP != abortCP && delta != 0) { |
|
307 |
input.position(thisCP); |
|
308 |
lastCP = thisCP; |
|
50113 | 309 |
int size = input.readInt(); // size |
310 |
long typeId = input.readLong(); |
|
311 |
if (typeId != CONSTANT_POOL_TYPE_ID) { |
|
58863 | 312 |
throw new IOException("Expected check point event (id = 1) at position " + lastCP + ", but found type id = " + typeId); |
50113 | 313 |
} |
314 |
input.readLong(); // timestamp |
|
315 |
input.readLong(); // duration |
|
58863 | 316 |
delta = input.readLong(); |
317 |
thisCP += delta; |
|
50113 | 318 |
boolean flush = input.readBoolean(); |
319 |
int poolCount = input.readInt(); |
|
58863 | 320 |
final long logLastCP = lastCP; |
321 |
final long logDelta = delta; |
|
322 |
if (logTrace) { |
|
323 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, () -> { |
|
324 |
return "New constant pool: startPosition=" + logLastCP + ", size=" + size + ", deltaToNext=" + logDelta + ", flush=" + flush + ", poolCount=" + poolCount; |
|
325 |
}); |
|
326 |
} |
|
50113 | 327 |
for (int i = 0; i < poolCount; i++) { |
328 |
long id = input.readLong(); // type id |
|
58863 | 329 |
ConstantLookup lookup = constantLookups.get(id); |
50113 | 330 |
Type type = typeMap.get(id); |
58863 | 331 |
if (lookup == null) { |
50113 | 332 |
if (type == null) { |
58863 | 333 |
throw new IOException( |
334 |
"Error parsing constant pool type " + getName(id) + " at position " + input.position() + " at check point between [" + lastCP + ", " + lastCP + size + "]"); |
|
335 |
} |
|
336 |
if (type.getName() != CHUNKHEADER) { |
|
337 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Found constant pool(" + id + ") that is never used"); |
|
50113 | 338 |
} |
58863 | 339 |
ConstantMap pool = new ConstantMap(ObjectFactory.create(type, timeConverter), type.getName()); |
340 |
lookup = new ConstantLookup(pool, type); |
|
341 |
constantLookups.put(type.getId(), lookup); |
|
50113 | 342 |
} |
58863 | 343 |
Parser parser = parsers.get(id); |
50113 | 344 |
if (parser == null) { |
345 |
throw new IOException("Could not find constant pool type with id = " + id); |
|
346 |
} |
|
347 |
try { |
|
348 |
int count = input.readInt(); |
|
58863 | 349 |
if (count == 0) { |
350 |
throw new InternalError("Pool " + type.getName() + " must contain at least one element "); |
|
351 |
} |
|
352 |
if (logTrace) { |
|
353 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, "Constant Pool " + i + ": " + type.getName()); |
|
354 |
} |
|
50113 | 355 |
for (int j = 0; j < count; j++) { |
356 |
long key = input.readLong(); |
|
58863 | 357 |
Object resolved = lookup.getPreviousResolved(key); |
358 |
if (resolved == null) { |
|
359 |
Object v = parser.parse(input); |
|
360 |
logConstant(key, v, false); |
|
361 |
lookup.getLatestPool().put(key, v); |
|
362 |
} else { |
|
363 |
parser.skip(input); |
|
364 |
logConstant(key, resolved, true); |
|
365 |
lookup.getLatestPool().putResolved(key, resolved); |
|
366 |
} |
|
50113 | 367 |
} |
368 |
} catch (Exception e) { |
|
58863 | 369 |
throw new IOException("Error parsing constant pool type " + getName(id) + " at position " + input.position() + " at check point between [" + lastCP + ", " + lastCP + size + "]", |
370 |
e); |
|
50113 | 371 |
} |
372 |
} |
|
58863 | 373 |
if (input.position() != lastCP + size) { |
50113 | 374 |
throw new IOException("Size of check point event doesn't match content"); |
375 |
} |
|
376 |
} |
|
377 |
} |
|
378 |
||
58863 | 379 |
private void logConstant(long key, Object v, boolean preresolved) { |
380 |
if (!Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE)) { |
|
381 |
return; |
|
382 |
} |
|
383 |
String valueText; |
|
384 |
if (v.getClass().isArray()) { |
|
385 |
Object[] array = (Object[]) v; |
|
386 |
StringJoiner sj = new StringJoiner(", ", "{", "}"); |
|
387 |
for (int i = 0; i < array.length; i++) { |
|
388 |
sj.add(textify(array[i])); |
|
389 |
} |
|
390 |
valueText = sj.toString(); |
|
391 |
} else { |
|
392 |
valueText = textify(v); |
|
393 |
} |
|
394 |
String suffix = preresolved ? " (presolved)" :""; |
|
395 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, "Constant: " + key + " = " + valueText + suffix); |
|
396 |
} |
|
397 |
||
398 |
private String textify(Object o) { |
|
399 |
if (o == null) { // should not happen |
|
400 |
return "null"; |
|
401 |
} |
|
402 |
if (o instanceof String) { |
|
403 |
return "\"" + String.valueOf(o) + "\""; |
|
404 |
} |
|
405 |
if (o instanceof RecordedObject) { |
|
406 |
return o.getClass().getName(); |
|
407 |
} |
|
408 |
if (o.getClass().isArray()) { |
|
409 |
Object[] array = (Object[]) o; |
|
410 |
if (array.length > 0) { |
|
411 |
return textify(array[0]) + "[]"; // can it be recursive? |
|
412 |
} |
|
413 |
} |
|
414 |
return String.valueOf(o); |
|
415 |
} |
|
416 |
||
50113 | 417 |
private String getName(long id) { |
418 |
Type type = typeMap.get(id); |
|
52850 | 419 |
return type == null ? ("unknown(" + id + ")") : type.getName(); |
50113 | 420 |
} |
421 |
||
422 |
public Collection<Type> getTypes() { |
|
423 |
return metadata.getTypes(); |
|
424 |
} |
|
425 |
||
426 |
public List<EventType> getEventTypes() { |
|
427 |
return metadata.getEventTypes(); |
|
428 |
} |
|
429 |
||
58863 | 430 |
public boolean isLastChunk() throws IOException { |
50113 | 431 |
return chunkHeader.isLastChunk(); |
432 |
} |
|
433 |
||
58863 | 434 |
ChunkParser newChunkParser() throws IOException { |
435 |
return new ChunkParser(this); |
|
436 |
} |
|
437 |
||
438 |
public boolean isChunkFinished() { |
|
439 |
return chunkFinished; |
|
440 |
} |
|
441 |
||
442 |
public void setFlushOperation(Runnable flushOperation) { |
|
443 |
this.flushOperation = flushOperation; |
|
50113 | 444 |
} |
58863 | 445 |
|
446 |
public long getChunkDuration() { |
|
447 |
return chunkHeader.getDurationNanos(); |
|
448 |
} |
|
449 |
||
450 |
public long getStartNanos() { |
|
451 |
return chunkHeader.getStartNanos(); |
|
452 |
} |
|
453 |
||
59226 | 454 |
public boolean isFinalChunk() { |
455 |
return chunkHeader.isFinalChunk(); |
|
456 |
} |
|
457 |
||
59310
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
458 |
public void close() { |
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
459 |
this.closed = true; |
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
460 |
Utils.notifyFlush(); |
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
461 |
} |
72f3dd43dd28
8234888: EventStream::close doesn't abort streaming thread
egahlin
parents:
59274
diff
changeset
|
462 |
|
50113 | 463 |
} |