author | egahlin |
Mon, 16 Sep 2019 09:45:22 +0200 | |
branch | JEP-349-branch |
changeset 58145 | bc54ed8d908a |
parent 57470 | src/jdk.jfr/share/classes/jdk/jfr/consumer/ParserFactory.java@025c9b8eaefd |
child 58197 | 0ef79bd7fb5c |
permissions | -rw-r--r-- |
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 |
||
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
57470
diff
changeset
|
26 |
package jdk.jfr.internal.consumer; |
50113 | 27 |
|
28 |
import java.io.IOException; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.List; |
|
31 |
||
32 |
import jdk.jfr.EventType; |
|
33 |
import jdk.jfr.ValueDescriptor; |
|
57373 | 34 |
import jdk.jfr.internal.LongMap; |
50113 | 35 |
import jdk.jfr.internal.MetadataDescriptor; |
36 |
import jdk.jfr.internal.PrivateAccess; |
|
37 |
import jdk.jfr.internal.Type; |
|
57360 | 38 |
import jdk.jfr.internal.consumer.Parser; |
50113 | 39 |
import jdk.jfr.internal.consumer.RecordingInput; |
40 |
||
41 |
/** |
|
42 |
* Class that create parsers suitable for reading events and constant pools |
|
43 |
*/ |
|
44 |
final class ParserFactory { |
|
45 |
private final LongMap<Parser> parsers = new LongMap<>(); |
|
46 |
private final TimeConverter timeConverter; |
|
47 |
private final LongMap<Type> types = new LongMap<>(); |
|
57360 | 48 |
private final LongMap<ConstantLookup> constantLookups; |
50113 | 49 |
|
57360 | 50 |
public ParserFactory(MetadataDescriptor metadata, LongMap<ConstantLookup> constantLookups, TimeConverter timeConverter) throws IOException { |
51 |
this.constantLookups = constantLookups; |
|
50113 | 52 |
this.timeConverter = timeConverter; |
53 |
for (Type t : metadata.getTypes()) { |
|
54 |
types.put(t.getId(), t); |
|
55 |
} |
|
57360 | 56 |
List<Type> typeList = new ArrayList<>(); |
57 |
types.forEach(typeList::add); |
|
58 |
for (Type t : typeList) { |
|
50113 | 59 |
if (!t.getFields().isEmpty()) { // Avoid primitives |
57360 | 60 |
CompositeParser cp = createCompositeParser(t, false); |
50113 | 61 |
if (t.isSimpleType()) { // Reduce to nested parser |
57360 | 62 |
parsers.put(t.getId(), cp.parsers[0]); |
50113 | 63 |
} |
64 |
||
65 |
} |
|
66 |
} |
|
67 |
// Override event types with event parsers |
|
68 |
for (EventType t : metadata.getEventTypes()) { |
|
69 |
parsers.put(t.getId(), createEventParser(t)); |
|
70 |
} |
|
71 |
} |
|
72 |
||
73 |
public LongMap<Parser> getParsers() { |
|
74 |
return parsers; |
|
75 |
} |
|
76 |
||
77 |
public LongMap<Type> getTypeMap() { |
|
78 |
return types; |
|
79 |
} |
|
80 |
||
81 |
private EventParser createEventParser(EventType eventType) throws IOException { |
|
82 |
List<Parser> parsers = new ArrayList<Parser>(); |
|
83 |
for (ValueDescriptor f : eventType.getFields()) { |
|
57360 | 84 |
parsers.add(createParser(f, true)); |
50113 | 85 |
} |
86 |
return new EventParser(timeConverter, eventType, parsers.toArray(new Parser[0])); |
|
87 |
} |
|
88 |
||
57360 | 89 |
private Parser createParser(ValueDescriptor v, boolean event) throws IOException { |
50113 | 90 |
boolean constantPool = PrivateAccess.getInstance().isConstantPool(v); |
91 |
if (v.isArray()) { |
|
92 |
Type valueType = PrivateAccess.getInstance().getType(v); |
|
93 |
ValueDescriptor element = PrivateAccess.getInstance().newValueDescriptor(v.getName(), valueType, v.getAnnotationElements(), 0, constantPool, null); |
|
57360 | 94 |
return new ArrayParser(createParser(element, event)); |
50113 | 95 |
} |
96 |
long id = v.getTypeId(); |
|
97 |
Type type = types.get(id); |
|
98 |
if (type == null) { |
|
99 |
throw new IOException("Type '" + v.getTypeName() + "' is not defined"); |
|
100 |
} |
|
101 |
if (constantPool) { |
|
57360 | 102 |
ConstantLookup lookup = constantLookups.get(id); |
103 |
if (lookup == null) { |
|
104 |
ConstantMap pool = new ConstantMap(ObjectFactory.create(type, timeConverter), type.getName()); |
|
105 |
lookup = new ConstantLookup(pool, type); |
|
106 |
constantLookups.put(id, lookup); |
|
50113 | 107 |
} |
57360 | 108 |
if (event) { |
109 |
return new EventValueConstantParser(lookup); |
|
110 |
} |
|
111 |
return new ConstantValueParser(lookup); |
|
50113 | 112 |
} |
113 |
Parser parser = parsers.get(id); |
|
114 |
if (parser == null) { |
|
115 |
if (!v.getFields().isEmpty()) { |
|
57360 | 116 |
return createCompositeParser(type, event); |
50113 | 117 |
} else { |
57360 | 118 |
return registerParserType(type, createPrimitiveParser(type, constantPool)); |
50113 | 119 |
} |
120 |
} |
|
121 |
return parser; |
|
122 |
} |
|
123 |
||
57360 | 124 |
private Parser createPrimitiveParser(Type type, boolean event) throws IOException { |
50113 | 125 |
switch (type.getName()) { |
126 |
case "int": |
|
127 |
return new IntegerParser(); |
|
128 |
case "long": |
|
129 |
return new LongParser(); |
|
130 |
case "float": |
|
131 |
return new FloatParser(); |
|
132 |
case "double": |
|
133 |
return new DoubleParser(); |
|
134 |
case "char": |
|
135 |
return new CharacterParser(); |
|
136 |
case "boolean": |
|
137 |
return new BooleanParser(); |
|
138 |
case "short": |
|
139 |
return new ShortParser(); |
|
140 |
case "byte": |
|
141 |
return new ByteParser(); |
|
142 |
case "java.lang.String": |
|
143 |
ConstantMap pool = new ConstantMap(ObjectFactory.create(type, timeConverter), type.getName()); |
|
57360 | 144 |
ConstantLookup lookup = new ConstantLookup(pool, type); |
145 |
constantLookups.put(type.getId(), lookup); |
|
146 |
return new StringParser(lookup, event); |
|
50113 | 147 |
default: |
148 |
throw new IOException("Unknown primitive type " + type.getName()); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
private Parser registerParserType(Type t, Parser parser) { |
|
153 |
Parser p = parsers.get(t.getId()); |
|
154 |
// check if parser exists (known type) |
|
155 |
if (p != null) { |
|
156 |
return p; |
|
157 |
} |
|
158 |
parsers.put(t.getId(), parser); |
|
159 |
return parser; |
|
160 |
} |
|
161 |
||
57360 | 162 |
private CompositeParser createCompositeParser(Type type, boolean event) throws IOException { |
50113 | 163 |
List<ValueDescriptor> vds = type.getFields(); |
164 |
Parser[] parsers = new Parser[vds.size()]; |
|
165 |
CompositeParser composite = new CompositeParser(parsers); |
|
166 |
// need to pre-register so recursive types can be handled |
|
167 |
registerParserType(type, composite); |
|
168 |
||
169 |
int index = 0; |
|
170 |
for (ValueDescriptor vd : vds) { |
|
57360 | 171 |
parsers[index++] = createParser(vd, event); |
50113 | 172 |
} |
173 |
return composite; |
|
174 |
} |
|
175 |
||
176 |
private static final class BooleanParser extends Parser { |
|
177 |
@Override |
|
178 |
public Object parse(RecordingInput input) throws IOException { |
|
179 |
return input.readBoolean() ? Boolean.TRUE : Boolean.FALSE; |
|
180 |
} |
|
57360 | 181 |
|
182 |
@Override |
|
183 |
public void skip(RecordingInput input) throws IOException { |
|
184 |
input.skipBytes(1); |
|
185 |
} |
|
50113 | 186 |
} |
187 |
||
188 |
private static final class ByteParser extends Parser { |
|
189 |
@Override |
|
190 |
public Object parse(RecordingInput input) throws IOException { |
|
191 |
return Byte.valueOf(input.readByte()); |
|
192 |
} |
|
57360 | 193 |
|
194 |
@Override |
|
195 |
public void skip(RecordingInput input) throws IOException { |
|
196 |
input.skipBytes(1); |
|
197 |
} |
|
50113 | 198 |
} |
199 |
||
200 |
private static final class LongParser extends Parser { |
|
57360 | 201 |
private Object lastLongObject = Long.valueOf(0); |
202 |
private long last = 0; |
|
203 |
||
50113 | 204 |
@Override |
205 |
public Object parse(RecordingInput input) throws IOException { |
|
57360 | 206 |
long l = input.readLong(); |
57470 | 207 |
if (l == last) { |
208 |
return lastLongObject; |
|
57360 | 209 |
} |
57470 | 210 |
last = l; |
211 |
lastLongObject = Long.valueOf(l); |
|
57360 | 212 |
return lastLongObject; |
213 |
} |
|
214 |
||
215 |
@Override |
|
216 |
public void skip(RecordingInput input) throws IOException { |
|
217 |
input.readLong(); |
|
50113 | 218 |
} |
219 |
} |
|
220 |
||
221 |
private static final class IntegerParser extends Parser { |
|
57360 | 222 |
private Integer lastIntegergObject = Integer.valueOf(0); |
223 |
private int last = 0; |
|
224 |
||
50113 | 225 |
@Override |
226 |
public Object parse(RecordingInput input) throws IOException { |
|
57360 | 227 |
int i = input.readInt(); |
228 |
if (i != last) { |
|
229 |
last = i; |
|
230 |
lastIntegergObject = Integer.valueOf(i); |
|
231 |
} |
|
232 |
return lastIntegergObject; |
|
233 |
} |
|
234 |
||
235 |
@Override |
|
236 |
public void skip(RecordingInput input) throws IOException { |
|
237 |
input.readInt(); |
|
50113 | 238 |
} |
239 |
} |
|
240 |
||
241 |
private static final class ShortParser extends Parser { |
|
242 |
@Override |
|
243 |
public Object parse(RecordingInput input) throws IOException { |
|
244 |
return Short.valueOf(input.readShort()); |
|
245 |
} |
|
57360 | 246 |
|
247 |
@Override |
|
248 |
public void skip(RecordingInput input) throws IOException { |
|
249 |
input.readShort(); |
|
250 |
} |
|
50113 | 251 |
} |
252 |
||
253 |
private static final class CharacterParser extends Parser { |
|
254 |
@Override |
|
255 |
public Object parse(RecordingInput input) throws IOException { |
|
256 |
return Character.valueOf(input.readChar()); |
|
257 |
} |
|
57360 | 258 |
|
259 |
@Override |
|
260 |
public void skip(RecordingInput input) throws IOException { |
|
261 |
input.readChar(); |
|
262 |
} |
|
50113 | 263 |
} |
264 |
||
265 |
private static final class FloatParser extends Parser { |
|
266 |
@Override |
|
267 |
public Object parse(RecordingInput input) throws IOException { |
|
268 |
return Float.valueOf(input.readFloat()); |
|
269 |
} |
|
57360 | 270 |
|
271 |
@Override |
|
272 |
public void skip(RecordingInput input) throws IOException { |
|
273 |
input.skipBytes(Float.SIZE); |
|
274 |
} |
|
50113 | 275 |
} |
276 |
||
277 |
private static final class DoubleParser extends Parser { |
|
278 |
@Override |
|
279 |
public Object parse(RecordingInput input) throws IOException { |
|
280 |
return Double.valueOf(input.readDouble()); |
|
281 |
} |
|
282 |
||
283 |
@Override |
|
57360 | 284 |
public void skip(RecordingInput input) throws IOException { |
285 |
input.skipBytes(Double.SIZE); |
|
50113 | 286 |
} |
287 |
} |
|
288 |
||
289 |
private final static class ArrayParser extends Parser { |
|
290 |
private final Parser elementParser; |
|
291 |
||
292 |
public ArrayParser(Parser elementParser) { |
|
293 |
this.elementParser = elementParser; |
|
294 |
} |
|
295 |
||
296 |
@Override |
|
297 |
public Object parse(RecordingInput input) throws IOException { |
|
298 |
final int size = input.readInt(); |
|
299 |
final Object[] array = new Object[size]; |
|
300 |
for (int i = 0; i < size; i++) { |
|
301 |
array[i] = elementParser.parse(input); |
|
302 |
} |
|
303 |
return array; |
|
304 |
} |
|
57360 | 305 |
|
306 |
@Override |
|
307 |
public void skip(RecordingInput input) throws IOException { |
|
308 |
final int size = input.readInt(); |
|
309 |
for (int i = 0; i < size; i++) { |
|
310 |
elementParser.skip(input); |
|
311 |
} |
|
312 |
} |
|
50113 | 313 |
} |
314 |
||
57360 | 315 |
final static class CompositeParser extends Parser { |
50113 | 316 |
private final Parser[] parsers; |
317 |
||
318 |
public CompositeParser(Parser[] valueParsers) { |
|
319 |
this.parsers = valueParsers; |
|
320 |
} |
|
321 |
||
322 |
@Override |
|
323 |
public Object parse(RecordingInput input) throws IOException { |
|
324 |
final Object[] values = new Object[parsers.length]; |
|
325 |
for (int i = 0; i < values.length; i++) { |
|
326 |
values[i] = parsers[i].parse(input); |
|
327 |
} |
|
328 |
return values; |
|
329 |
} |
|
57360 | 330 |
|
331 |
@Override |
|
332 |
public void skip(RecordingInput input) throws IOException { |
|
333 |
for (int i = 0; i < parsers.length; i++) { |
|
334 |
parsers[i].skip(input); |
|
335 |
} |
|
336 |
} |
|
50113 | 337 |
} |
338 |
||
57360 | 339 |
public static final class EventValueConstantParser extends Parser { |
340 |
private final ConstantLookup lookup; |
|
341 |
private Object lastValue = 0; |
|
342 |
private long lastKey = -1; |
|
343 |
EventValueConstantParser(ConstantLookup lookup) { |
|
344 |
this.lookup = lookup; |
|
50113 | 345 |
} |
346 |
||
347 |
@Override |
|
348 |
public Object parse(RecordingInput input) throws IOException { |
|
57360 | 349 |
long key = input.readLong(); |
350 |
if (key == lastKey) { |
|
351 |
return lastValue; |
|
352 |
} |
|
353 |
lastKey = key; |
|
354 |
lastValue = lookup.getCurrentResolved(key); |
|
355 |
return lastValue; |
|
356 |
} |
|
357 |
||
358 |
@Override |
|
359 |
public void skip(RecordingInput input) throws IOException { |
|
360 |
input.readLong(); |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
public static final class ConstantValueParser extends Parser { |
|
365 |
private final ConstantLookup lookup; |
|
366 |
ConstantValueParser(ConstantLookup lookup) { |
|
367 |
this.lookup = lookup; |
|
368 |
} |
|
369 |
||
370 |
@Override |
|
371 |
public Object parse(RecordingInput input) throws IOException { |
|
372 |
return lookup.getCurrent(input.readLong()); |
|
373 |
} |
|
374 |
||
375 |
@Override |
|
376 |
public void skip(RecordingInput input) throws IOException { |
|
377 |
input.readLong(); |
|
50113 | 378 |
} |
379 |
} |
|
380 |
} |