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