author | egahlin |
Mon, 16 Sep 2019 09:45:22 +0200 | |
branch | JEP-349-branch |
changeset 58145 | bc54ed8d908a |
parent 58112 | e7754025004b |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58112 | 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.internal; |
|
27 |
||
28 |
import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_CONSTANT_POOL; |
|
29 |
import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_DIMENSION; |
|
30 |
import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_ID; |
|
31 |
import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_NAME; |
|
32 |
import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_SIMPLE_TYPE; |
|
33 |
import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_SUPER_TYPE; |
|
34 |
import static jdk.jfr.internal.MetadataDescriptor.ATTRIBUTE_TYPE_ID; |
|
35 |
import static jdk.jfr.internal.MetadataDescriptor.ELEMENT_ANNOTATION; |
|
36 |
import static jdk.jfr.internal.MetadataDescriptor.ELEMENT_FIELD; |
|
37 |
import static jdk.jfr.internal.MetadataDescriptor.ELEMENT_SETTING; |
|
38 |
import static jdk.jfr.internal.MetadataDescriptor.ELEMENT_TYPE; |
|
39 |
||
40 |
import java.io.DataInput; |
|
41 |
import java.io.IOException; |
|
42 |
import java.util.ArrayList; |
|
43 |
import java.util.Collections; |
|
44 |
import java.util.HashMap; |
|
45 |
import java.util.List; |
|
46 |
import java.util.Map; |
|
47 |
||
48 |
import jdk.jfr.AnnotationElement; |
|
49 |
import jdk.jfr.SettingDescriptor; |
|
50 |
import jdk.jfr.ValueDescriptor; |
|
51 |
import jdk.jfr.internal.MetadataDescriptor.Element; |
|
57360 | 52 |
import jdk.jfr.internal.consumer.RecordingInput; |
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58112
diff
changeset
|
53 |
import jdk.jfr.internal.consumer.StringParser; |
50113 | 54 |
|
55 |
/** |
|
56 |
* Parses metadata. |
|
57 |
* |
|
58 |
*/ |
|
59 |
final class MetadataReader { |
|
60 |
||
61 |
private final DataInput input; |
|
62 |
private final List<String> pool; |
|
63 |
private final MetadataDescriptor descriptor; |
|
64 |
private final Map<Long, Type> types = new HashMap<>(); |
|
65 |
||
57360 | 66 |
public MetadataReader(RecordingInput input) throws IOException { |
50113 | 67 |
this.input = input; |
68 |
int size = input.readInt(); |
|
69 |
this.pool = new ArrayList<>(size); |
|
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58112
diff
changeset
|
70 |
StringParser p = new StringParser(null, false); |
50113 | 71 |
for (int i = 0; i < size; i++) { |
57360 | 72 |
this.pool.add((String) p.parse(input)); |
50113 | 73 |
} |
74 |
descriptor = new MetadataDescriptor(); |
|
75 |
Element root = createElement(); |
|
76 |
Element metadata = root.elements("metadata").get(0); |
|
77 |
declareTypes(metadata); |
|
78 |
defineTypes(metadata); |
|
79 |
annotateTypes(metadata); |
|
80 |
buildEvenTypes(); |
|
81 |
Element time = root.elements("region").get(0); |
|
82 |
descriptor.gmtOffset = time.attribute(MetadataDescriptor.ATTRIBUTE_GMT_OFFSET, 1); |
|
83 |
descriptor.locale = time.attribute(MetadataDescriptor.ATTRIBUTE_LOCALE, ""); |
|
84 |
descriptor.root = root; |
|
52413 | 85 |
if (Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE)) { |
50113 | 86 |
List<Type> ts = new ArrayList<>(types.values()); |
87 |
Collections.sort(ts, (x,y) -> x.getName().compareTo(y.getName())); |
|
88 |
for (Type t : ts) { |
|
89 |
t.log("Found", LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE); |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
private String readString() throws IOException { |
|
95 |
return pool.get(readInt()); |
|
96 |
} |
|
97 |
||
98 |
private int readInt() throws IOException { |
|
99 |
return input.readInt(); |
|
100 |
} |
|
101 |
||
102 |
private Element createElement() throws IOException { |
|
103 |
String name = readString(); |
|
104 |
Element e = new Element(name); |
|
105 |
int attributeCount = readInt(); |
|
106 |
for (int i = 0; i < attributeCount; i++) { |
|
107 |
e.addAttribute(readString(), readString()); |
|
108 |
} |
|
109 |
int childrenCount = readInt(); |
|
110 |
for (int i = 0; i < childrenCount; i++) { |
|
111 |
e.add(createElement()); |
|
112 |
} |
|
113 |
return e; |
|
114 |
} |
|
115 |
||
116 |
private void annotateTypes(Element metadata) throws IOException { |
|
117 |
for (Element typeElement : metadata.elements(ELEMENT_TYPE)) { |
|
118 |
Type type = getType(ATTRIBUTE_ID, typeElement); |
|
119 |
ArrayList<AnnotationElement> aes = new ArrayList<>(); |
|
120 |
for (Element annotationElement : typeElement.elements(ELEMENT_ANNOTATION)) { |
|
121 |
aes.add(makeAnnotation(annotationElement)); |
|
122 |
} |
|
123 |
aes.trimToSize(); |
|
124 |
type.setAnnotations(aes); |
|
125 |
||
126 |
int index = 0; |
|
127 |
if (type instanceof PlatformEventType) { |
|
128 |
List<SettingDescriptor> settings = ((PlatformEventType) type).getAllSettings(); |
|
129 |
for (Element settingElement : typeElement.elements(ELEMENT_SETTING)) { |
|
130 |
ArrayList<AnnotationElement> annotations = new ArrayList<>(); |
|
131 |
for (Element annotationElement : settingElement.elements(ELEMENT_ANNOTATION)) { |
|
132 |
annotations.add(makeAnnotation(annotationElement)); |
|
133 |
} |
|
134 |
annotations.trimToSize(); |
|
135 |
PrivateAccess.getInstance().setAnnotations(settings.get(index), annotations); |
|
136 |
index++; |
|
137 |
} |
|
138 |
} |
|
139 |
index = 0; |
|
140 |
List<ValueDescriptor> fields = type.getFields(); |
|
141 |
for (Element fieldElement : typeElement.elements(ELEMENT_FIELD)) { |
|
142 |
ArrayList<AnnotationElement> annotations = new ArrayList<>(); |
|
143 |
for (Element annotationElement : fieldElement.elements(ELEMENT_ANNOTATION)) { |
|
144 |
annotations.add(makeAnnotation(annotationElement)); |
|
145 |
} |
|
146 |
annotations.trimToSize(); |
|
147 |
PrivateAccess.getInstance().setAnnotations(fields.get(index), annotations); |
|
148 |
index++; |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
private AnnotationElement makeAnnotation(Element annotationElement) throws IOException { |
|
154 |
Type annotationType = getType(ATTRIBUTE_TYPE_ID, annotationElement); |
|
155 |
List<Object> values = new ArrayList<>(); |
|
156 |
for (ValueDescriptor v : annotationType.getFields()) { |
|
157 |
if (v.isArray()) { |
|
158 |
List<Object> list = new ArrayList<>(); |
|
159 |
int index = 0; |
|
160 |
while (true) { |
|
161 |
String text = annotationElement.attribute(v.getName() + "-" + index); |
|
162 |
if (text == null) { |
|
163 |
break; |
|
164 |
} |
|
165 |
list.add(objectify(v.getTypeName(), text)); |
|
166 |
index++; |
|
167 |
} |
|
168 |
Object object = Utils.makePrimitiveArray(v.getTypeName(), list); |
|
169 |
if (object == null) { |
|
170 |
throw new IOException("Unsupported type " + list + " in array"); |
|
171 |
} |
|
172 |
values.add(object); |
|
173 |
} else { |
|
174 |
String text = annotationElement.attribute(v.getName()); |
|
175 |
values.add(objectify(v.getTypeName(), text)); |
|
176 |
} |
|
177 |
} |
|
178 |
return PrivateAccess.getInstance().newAnnotation(annotationType, values, false); |
|
179 |
} |
|
180 |
||
181 |
private Object objectify(String typeName, String text) throws IOException { |
|
182 |
try { |
|
183 |
switch (typeName) { |
|
184 |
case "int": |
|
185 |
return Integer.valueOf(text); |
|
186 |
case "long": |
|
187 |
return Long.valueOf(text); |
|
188 |
case "double": |
|
189 |
return Double.valueOf(text); |
|
190 |
case "float": |
|
191 |
return Float.valueOf(text); |
|
192 |
case "short": |
|
193 |
return Short.valueOf(text); |
|
194 |
case "char": |
|
195 |
if (text.length() != 1) { |
|
196 |
throw new IOException("Unexpected size of char"); |
|
197 |
} |
|
198 |
return text.charAt(0); |
|
199 |
case "byte": |
|
200 |
return Byte.valueOf(text); |
|
201 |
case "boolean": |
|
202 |
return Boolean.valueOf(text); |
|
203 |
case "java.lang.String": |
|
204 |
return text; |
|
205 |
} |
|
206 |
} catch (IllegalArgumentException iae) { |
|
207 |
throw new IOException("Could not parse text representation of " + typeName); |
|
208 |
} |
|
209 |
throw new IOException("Unsupported type for annotation " + typeName); |
|
210 |
} |
|
211 |
||
212 |
private Type getType(String attribute, Element element) { |
|
213 |
long id = element.longValue(attribute); |
|
214 |
Type type = types.get(id); |
|
215 |
if (type == null) { |
|
216 |
String name = element.attribute("type"); |
|
217 |
throw new IllegalStateException("Type '" + id + "' is not defined for " + name); |
|
218 |
} |
|
219 |
return type; |
|
220 |
} |
|
221 |
||
222 |
private void buildEvenTypes() { |
|
223 |
for (Type type : descriptor.types) { |
|
224 |
if (type instanceof PlatformEventType) { |
|
225 |
descriptor.eventTypes.add(PrivateAccess.getInstance().newEventType((PlatformEventType) type)); |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
private void defineTypes(Element metadata) { |
|
231 |
for (Element typeElement : metadata.elements(ELEMENT_TYPE)) { |
|
232 |
long id = typeElement.attribute(ATTRIBUTE_ID, -1); |
|
233 |
Type t = types.get(id); |
|
234 |
for (Element fieldElement : typeElement.elements(ELEMENT_SETTING)) { |
|
235 |
String name = fieldElement.attribute(ATTRIBUTE_NAME); |
|
236 |
String defaultValue = fieldElement.attribute(ATTRIBUTE_NAME); |
|
237 |
Type settingType = getType(ATTRIBUTE_TYPE_ID, fieldElement); |
|
238 |
PlatformEventType eventType = (PlatformEventType) t; |
|
239 |
eventType.add(PrivateAccess.getInstance().newSettingDescriptor(settingType, name, defaultValue, new ArrayList<>(2))); |
|
240 |
} |
|
241 |
for (Element fieldElement : typeElement.elements(ELEMENT_FIELD)) { |
|
242 |
String name = fieldElement.attribute(ATTRIBUTE_NAME); |
|
243 |
Type fieldType = getType(ATTRIBUTE_TYPE_ID, fieldElement); |
|
244 |
long dimension = fieldElement.attribute(ATTRIBUTE_DIMENSION, 0); |
|
245 |
boolean constantPool = fieldElement.attribute(ATTRIBUTE_CONSTANT_POOL) != null; |
|
246 |
// Add annotation later, because they may refer to undefined |
|
247 |
// types at this stage |
|
248 |
t.add(PrivateAccess.getInstance().newValueDescriptor(name, fieldType, new ArrayList<>(), (int) dimension, constantPool, null)); |
|
249 |
} |
|
250 |
t.trimFields(); |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
private void declareTypes(Element metadata) { |
|
255 |
for (Element typeElement : metadata.elements(ELEMENT_TYPE)) { |
|
256 |
String typeName = typeElement.attribute(ATTRIBUTE_NAME); |
|
257 |
String superType = typeElement.attribute(ATTRIBUTE_SUPER_TYPE); |
|
258 |
boolean simpleType = typeElement.attribute(ATTRIBUTE_SIMPLE_TYPE) != null; |
|
259 |
long id = typeElement.attribute(ATTRIBUTE_ID, -1); |
|
260 |
Type t; |
|
261 |
if (Type.SUPER_TYPE_EVENT.equals(superType)) { |
|
262 |
t = new PlatformEventType(typeName, id, false, false); |
|
263 |
} else { |
|
264 |
t = new Type(typeName, superType, id, false, simpleType); |
|
265 |
} |
|
266 |
types.put(id, t); |
|
267 |
descriptor.types.add(t); |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
public MetadataDescriptor getDescriptor() { |
|
272 |
return descriptor; |
|
273 |
} |
|
274 |
} |