52850
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
|
|
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.tool;
|
|
27 |
|
|
28 |
import java.io.PrintWriter;
|
|
29 |
import java.time.Duration;
|
|
30 |
import java.time.OffsetDateTime;
|
|
31 |
import java.time.format.DateTimeFormatter;
|
|
32 |
import java.util.ArrayList;
|
|
33 |
import java.util.List;
|
|
34 |
import java.util.StringJoiner;
|
|
35 |
|
|
36 |
import jdk.jfr.AnnotationElement;
|
|
37 |
import jdk.jfr.DataAmount;
|
|
38 |
import jdk.jfr.Frequency;
|
|
39 |
import jdk.jfr.MemoryAddress;
|
|
40 |
import jdk.jfr.Percentage;
|
|
41 |
import jdk.jfr.ValueDescriptor;
|
|
42 |
import jdk.jfr.consumer.RecordedClass;
|
|
43 |
import jdk.jfr.consumer.RecordedClassLoader;
|
|
44 |
import jdk.jfr.consumer.RecordedEvent;
|
|
45 |
import jdk.jfr.consumer.RecordedFrame;
|
|
46 |
import jdk.jfr.consumer.RecordedMethod;
|
|
47 |
import jdk.jfr.consumer.RecordedObject;
|
|
48 |
import jdk.jfr.consumer.RecordedStackTrace;
|
|
49 |
import jdk.jfr.consumer.RecordedThread;
|
|
50 |
import jdk.jfr.internal.PrivateAccess;
|
|
51 |
import jdk.jfr.internal.Type;
|
|
52 |
import jdk.jfr.internal.Utils;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Print events in a human-readable format.
|
|
56 |
*
|
|
57 |
* This class is also used by {@link RecordedObject#toString()}
|
|
58 |
*/
|
|
59 |
public final class PrettyWriter extends EventPrintWriter {
|
|
60 |
private final static DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
|
|
61 |
private final static Long ZERO = 0L;
|
|
62 |
private boolean showIds;
|
|
63 |
|
|
64 |
public PrettyWriter(PrintWriter destination) {
|
|
65 |
super(destination);
|
|
66 |
}
|
|
67 |
|
|
68 |
@Override
|
|
69 |
protected void print(List<RecordedEvent> events) {
|
|
70 |
for (RecordedEvent e : events) {
|
|
71 |
print(e);
|
|
72 |
flush(false);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
public void printType(Type t) {
|
|
77 |
if (showIds) {
|
|
78 |
print("// id: ");
|
|
79 |
println(String.valueOf(t.getId()));
|
|
80 |
}
|
|
81 |
int commentIndex = t.getName().length() + 10;
|
|
82 |
String typeName = t.getName();
|
|
83 |
int index = typeName.lastIndexOf(".");
|
|
84 |
if (index != -1) {
|
|
85 |
println("@Name(\"" + typeName + "\")");
|
|
86 |
}
|
|
87 |
printAnnotations(commentIndex, t.getAnnotationElements());
|
|
88 |
print("class " + typeName.substring(index + 1));
|
|
89 |
String superType = t.getSuperType();
|
|
90 |
if (superType != null) {
|
|
91 |
print(" extends " + superType);
|
|
92 |
}
|
|
93 |
println(" {");
|
|
94 |
indent();
|
|
95 |
boolean first = true;
|
|
96 |
for (ValueDescriptor v : t.getFields()) {
|
|
97 |
printField(commentIndex, v, first);
|
|
98 |
first = false;
|
|
99 |
}
|
|
100 |
retract();
|
|
101 |
println("}");
|
|
102 |
println();
|
|
103 |
}
|
|
104 |
|
|
105 |
private void printField(int commentIndex, ValueDescriptor v, boolean first) {
|
|
106 |
if (!first) {
|
|
107 |
println();
|
|
108 |
}
|
|
109 |
printAnnotations(commentIndex, v.getAnnotationElements());
|
|
110 |
printIndent();
|
|
111 |
Type vType = PrivateAccess.getInstance().getType(v);
|
|
112 |
if (Type.SUPER_TYPE_SETTING.equals(vType.getSuperType())) {
|
|
113 |
print("static ");
|
|
114 |
}
|
|
115 |
print(makeSimpleType(v.getTypeName()));
|
|
116 |
if (v.isArray()) {
|
|
117 |
print("[]");
|
|
118 |
}
|
|
119 |
print(" ");
|
|
120 |
print(v.getName());
|
|
121 |
print(";");
|
|
122 |
printCommentRef(commentIndex, v.getTypeId());
|
|
123 |
}
|
|
124 |
|
|
125 |
private void printCommentRef(int commentIndex, long typeId) {
|
|
126 |
if (showIds) {
|
|
127 |
int column = getColumn();
|
|
128 |
if (column > commentIndex) {
|
|
129 |
print(" ");
|
|
130 |
} else {
|
|
131 |
while (column < commentIndex) {
|
|
132 |
print(" ");
|
|
133 |
column++;
|
|
134 |
}
|
|
135 |
}
|
|
136 |
println(" // id=" + typeId);
|
|
137 |
} else {
|
|
138 |
println();
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
private void printAnnotations(int commentIndex, List<AnnotationElement> annotations) {
|
|
143 |
for (AnnotationElement a : annotations) {
|
|
144 |
printIndent();
|
|
145 |
print("@");
|
|
146 |
print(makeSimpleType(a.getTypeName()));
|
|
147 |
List<ValueDescriptor> vs = a.getValueDescriptors();
|
|
148 |
if (!vs.isEmpty()) {
|
|
149 |
printAnnotation(a);
|
|
150 |
printCommentRef(commentIndex, a.getTypeId());
|
|
151 |
} else {
|
|
152 |
println();
|
|
153 |
}
|
|
154 |
}
|
|
155 |
}
|
|
156 |
|
|
157 |
private void printAnnotation(AnnotationElement a) {
|
|
158 |
StringJoiner sj = new StringJoiner(", ", "(", ")");
|
|
159 |
List<ValueDescriptor> vs = a.getValueDescriptors();
|
|
160 |
for (ValueDescriptor v : vs) {
|
|
161 |
Object o = a.getValue(v.getName());
|
|
162 |
if (vs.size() == 1 && v.getName().equals("value")) {
|
|
163 |
sj.add(textify(o));
|
|
164 |
} else {
|
|
165 |
sj.add(v.getName() + "=" + textify(o));
|
|
166 |
}
|
|
167 |
}
|
|
168 |
print(sj.toString());
|
|
169 |
}
|
|
170 |
|
|
171 |
private String textify(Object o) {
|
|
172 |
if (o.getClass().isArray()) {
|
|
173 |
Object[] array = (Object[]) o;
|
|
174 |
if (array.length == 1) {
|
|
175 |
return quoteIfNeeded(array[0]);
|
|
176 |
}
|
|
177 |
StringJoiner s = new StringJoiner(", ", "{", "}");
|
|
178 |
for (Object ob : array) {
|
|
179 |
s.add(quoteIfNeeded(ob));
|
|
180 |
}
|
|
181 |
return s.toString();
|
|
182 |
} else {
|
|
183 |
return quoteIfNeeded(o);
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
private String quoteIfNeeded(Object o) {
|
|
188 |
if (o instanceof String) {
|
|
189 |
return "\"" + o + "\"";
|
|
190 |
} else {
|
|
191 |
return String.valueOf(o);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
private String makeSimpleType(String typeName) {
|
|
196 |
int index = typeName.lastIndexOf(".");
|
|
197 |
return typeName.substring(index + 1);
|
|
198 |
}
|
|
199 |
|
|
200 |
public void print(RecordedEvent event) {
|
|
201 |
print(event.getEventType().getName(), " ");
|
|
202 |
println("{");
|
|
203 |
indent();
|
|
204 |
for (ValueDescriptor v : event.getFields()) {
|
|
205 |
String name = v.getName();
|
|
206 |
if (!isZeroDuration(event, name) && !isLateField(name)) {
|
|
207 |
printFieldValue(event, v);
|
|
208 |
}
|
|
209 |
}
|
|
210 |
if (event.getThread() != null) {
|
|
211 |
printIndent();
|
|
212 |
print(EVENT_THREAD_FIELD + " = ");
|
|
213 |
printThread(event.getThread(), "");
|
|
214 |
}
|
|
215 |
if (event.getStackTrace() != null) {
|
|
216 |
printIndent();
|
|
217 |
print(STACK_TRACE_FIELD + " = ");
|
|
218 |
printStackTrace(event.getStackTrace());
|
|
219 |
}
|
|
220 |
retract();
|
|
221 |
printIndent();
|
|
222 |
println("}");
|
|
223 |
println();
|
|
224 |
}
|
|
225 |
|
|
226 |
private boolean isZeroDuration(RecordedEvent event, String name) {
|
|
227 |
return name.equals("duration") && ZERO.equals(event.getValue("duration"));
|
|
228 |
}
|
|
229 |
|
|
230 |
private void printStackTrace(RecordedStackTrace stackTrace) {
|
|
231 |
println("[");
|
|
232 |
List<RecordedFrame> frames = stackTrace.getFrames();
|
|
233 |
indent();
|
|
234 |
int i = 0;
|
|
235 |
while (i < frames.size() && i < getStackDepth()) {
|
|
236 |
RecordedFrame frame = frames.get(i);
|
|
237 |
if (frame.isJavaFrame()) {
|
|
238 |
printIndent();
|
|
239 |
printValue(frame, null, "");
|
|
240 |
println();
|
|
241 |
i++;
|
|
242 |
}
|
|
243 |
}
|
|
244 |
if (stackTrace.isTruncated() || i == getStackDepth()) {
|
|
245 |
printIndent();
|
|
246 |
println("...");
|
|
247 |
}
|
|
248 |
retract();
|
|
249 |
printIndent();
|
|
250 |
println("]");
|
|
251 |
}
|
|
252 |
|
|
253 |
public void print(RecordedObject struct, String postFix) {
|
|
254 |
println("{");
|
|
255 |
indent();
|
|
256 |
for (ValueDescriptor v : struct.getFields()) {
|
|
257 |
printFieldValue(struct, v);
|
|
258 |
}
|
|
259 |
retract();
|
|
260 |
printIndent();
|
|
261 |
println("}" + postFix);
|
|
262 |
}
|
|
263 |
|
|
264 |
private void printFieldValue(RecordedObject struct, ValueDescriptor v) {
|
|
265 |
printIndent();
|
|
266 |
print(v.getName(), " = ");
|
|
267 |
printValue(getValue(struct, v), v, "");
|
|
268 |
}
|
|
269 |
|
|
270 |
private void printArray(Object[] array) {
|
|
271 |
println("[");
|
|
272 |
indent();
|
|
273 |
for (int i = 0; i < array.length; i++) {
|
|
274 |
printIndent();
|
|
275 |
printValue(array[i], null, i + 1 < array.length ? ", " : "");
|
|
276 |
}
|
|
277 |
retract();
|
|
278 |
printIndent();
|
|
279 |
println("]");
|
|
280 |
}
|
|
281 |
|
|
282 |
private void printValue(Object value, ValueDescriptor field, String postFix) {
|
|
283 |
if (value == null) {
|
|
284 |
println("null" + postFix);
|
|
285 |
return;
|
|
286 |
}
|
|
287 |
if (value instanceof RecordedObject) {
|
|
288 |
if (value instanceof RecordedThread) {
|
|
289 |
printThread((RecordedThread) value, postFix);
|
|
290 |
return;
|
|
291 |
}
|
|
292 |
if (value instanceof RecordedClass) {
|
|
293 |
printClass((RecordedClass) value, postFix);
|
|
294 |
return;
|
|
295 |
}
|
|
296 |
if (value instanceof RecordedClassLoader) {
|
|
297 |
printClassLoader((RecordedClassLoader) value, postFix);
|
|
298 |
return;
|
|
299 |
}
|
|
300 |
if (value instanceof RecordedFrame) {
|
|
301 |
RecordedFrame frame = (RecordedFrame) value;
|
|
302 |
if (frame.isJavaFrame()) {
|
|
303 |
printJavaFrame((RecordedFrame) value, postFix);
|
|
304 |
return;
|
|
305 |
}
|
|
306 |
}
|
|
307 |
if (value instanceof RecordedMethod) {
|
|
308 |
println(formatMethod((RecordedMethod) value));
|
|
309 |
return;
|
|
310 |
}
|
|
311 |
print((RecordedObject) value, postFix);
|
|
312 |
return;
|
|
313 |
}
|
|
314 |
if (value.getClass().isArray()) {
|
|
315 |
printArray((Object[]) value);
|
|
316 |
return;
|
|
317 |
}
|
|
318 |
if (field.getContentType() != null) {
|
|
319 |
if (printFormatted(field, value)) {
|
|
320 |
return;
|
|
321 |
}
|
|
322 |
}
|
|
323 |
String text = String.valueOf(value);
|
|
324 |
if (value instanceof String) {
|
|
325 |
text = "\"" + text + "\"";
|
|
326 |
}
|
|
327 |
println(text);
|
|
328 |
}
|
|
329 |
|
|
330 |
private void printClassLoader(RecordedClassLoader cl, String postFix) {
|
|
331 |
// Purposely not printing class loader name to avoid cluttered output
|
|
332 |
RecordedClass clazz = cl.getType();
|
|
333 |
print(clazz == null ? "null" : clazz.getName());
|
|
334 |
if (clazz != null) {
|
|
335 |
print(" (");
|
|
336 |
print("id = ");
|
|
337 |
print(String.valueOf(cl.getId()));
|
|
338 |
println(")");
|
|
339 |
}
|
|
340 |
}
|
|
341 |
|
|
342 |
private void printJavaFrame(RecordedFrame f, String postFix) {
|
|
343 |
print(formatMethod(f.getMethod()));
|
|
344 |
int line = f.getLineNumber();
|
|
345 |
if (line >= 0) {
|
|
346 |
print(" line: " + line);
|
|
347 |
}
|
|
348 |
print(postFix);
|
|
349 |
}
|
|
350 |
|
|
351 |
private String formatMethod(RecordedMethod m) {
|
|
352 |
StringBuilder sb = new StringBuilder();
|
|
353 |
sb.append(m.getType().getName());
|
|
354 |
sb.append(".");
|
|
355 |
sb.append(m.getName());
|
|
356 |
sb.append("(");
|
|
357 |
StringJoiner sj = new StringJoiner(", ");
|
|
358 |
String md = m.getDescriptor().replace("/", ".");
|
|
359 |
String parameter = md.substring(1, md.lastIndexOf(")"));
|
|
360 |
for (String qualifiedName : decodeDescriptors(parameter)) {
|
|
361 |
String typeName = qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1);
|
|
362 |
sj.add(typeName);
|
|
363 |
}
|
|
364 |
sb.append(sj);
|
|
365 |
sb.append(")");
|
|
366 |
return sb.toString();
|
|
367 |
}
|
|
368 |
|
|
369 |
private void printClass(RecordedClass clazz, String postFix) {
|
|
370 |
RecordedClassLoader classLoader = clazz.getClassLoader();
|
|
371 |
String classLoaderName = "null";
|
|
372 |
if (classLoader != null) {
|
|
373 |
if (classLoader.getName() != null) {
|
|
374 |
classLoaderName = classLoader.getName();
|
|
375 |
} else {
|
|
376 |
classLoaderName = classLoader.getType().getName();
|
|
377 |
}
|
|
378 |
}
|
|
379 |
String className = clazz.getName();
|
|
380 |
if (className.startsWith("[")) {
|
|
381 |
className = decodeDescriptors(className).get(0);
|
|
382 |
}
|
|
383 |
println(className + " (classLoader = " + classLoaderName + ")" + postFix);
|
|
384 |
}
|
|
385 |
|
|
386 |
List<String> decodeDescriptors(String descriptor) {
|
|
387 |
List<String> descriptors = new ArrayList<>();
|
|
388 |
for (int index = 0; index < descriptor.length(); index++) {
|
|
389 |
String arrayBrackets = "";
|
|
390 |
while (descriptor.charAt(index) == '[') {
|
|
391 |
arrayBrackets += "[]";
|
|
392 |
index++;
|
|
393 |
}
|
|
394 |
char c = descriptor.charAt(index);
|
|
395 |
String type;
|
|
396 |
switch (c) {
|
|
397 |
case 'L':
|
|
398 |
int endIndex = descriptor.indexOf(';', index);
|
|
399 |
type = descriptor.substring(index + 1, endIndex);
|
|
400 |
index = endIndex;
|
|
401 |
break;
|
|
402 |
case 'I':
|
|
403 |
type = "int";
|
|
404 |
break;
|
|
405 |
case 'J':
|
|
406 |
type = "long";
|
|
407 |
break;
|
|
408 |
case 'Z':
|
|
409 |
type = "boolean";
|
|
410 |
break;
|
|
411 |
case 'D':
|
|
412 |
type = "double";
|
|
413 |
break;
|
|
414 |
case 'F':
|
|
415 |
type = "float";
|
|
416 |
break;
|
|
417 |
case 'S':
|
|
418 |
type = "short";
|
|
419 |
break;
|
|
420 |
case 'C':
|
|
421 |
type = "char";
|
|
422 |
break;
|
|
423 |
case 'B':
|
|
424 |
type = "byte";
|
|
425 |
break;
|
|
426 |
default:
|
|
427 |
type = "<unknown-descriptor-type>";
|
|
428 |
}
|
|
429 |
descriptors.add(type + arrayBrackets);
|
|
430 |
}
|
|
431 |
return descriptors;
|
|
432 |
}
|
|
433 |
|
|
434 |
private void printThread(RecordedThread thread, String postFix) {
|
|
435 |
long javaThreadId = thread.getJavaThreadId();
|
|
436 |
if (javaThreadId > 0) {
|
|
437 |
println("\"" + thread.getJavaName() + "\" (javaThreadId = " + thread.getJavaThreadId() + ")" + postFix);
|
|
438 |
} else {
|
|
439 |
println("\"" + thread.getOSName() + "\" (osThreadId = " + thread.getOSThreadId() + ")" + postFix);
|
|
440 |
}
|
|
441 |
}
|
|
442 |
|
|
443 |
private boolean printFormatted(ValueDescriptor field, Object value) {
|
|
444 |
if (value instanceof Duration) {
|
|
445 |
Duration d = (Duration) value;
|
|
446 |
double s = d.toNanosPart() / 1000_000_000.0 + d.toSecondsPart();
|
|
447 |
if (s < 1.0) {
|
|
448 |
if (s < 0.001) {
|
|
449 |
println(String.format("%.3f", s * 1_000_000) + " us");
|
|
450 |
} else {
|
|
451 |
println(String.format("%.3f", s * 1_000) + " ms");
|
|
452 |
}
|
|
453 |
} else {
|
|
454 |
if (s < 1000.0) {
|
|
455 |
println(String.format("%.3f", s) + " s");
|
|
456 |
} else {
|
|
457 |
println(String.format("%.0f", s) + " s");
|
|
458 |
}
|
|
459 |
}
|
|
460 |
return true;
|
|
461 |
}
|
|
462 |
if (value instanceof OffsetDateTime) {
|
|
463 |
OffsetDateTime zdt = (OffsetDateTime) value;
|
|
464 |
println(TIME_FORMAT.format(zdt));
|
|
465 |
return true;
|
|
466 |
}
|
|
467 |
Percentage percentage = field.getAnnotation(Percentage.class);
|
|
468 |
if (percentage != null) {
|
|
469 |
if (value instanceof Number) {
|
|
470 |
double d = ((Number) value).doubleValue();
|
|
471 |
println(String.format("%.2f", d * 100) + "%");
|
|
472 |
return true;
|
|
473 |
}
|
|
474 |
}
|
|
475 |
DataAmount dataAmount = field.getAnnotation(DataAmount.class);
|
|
476 |
if (dataAmount != null) {
|
|
477 |
if (value instanceof Number) {
|
|
478 |
Number n = (Number) value;
|
|
479 |
String bytes = Utils.formatBytes(n.longValue(), " ");
|
|
480 |
if (field.getAnnotation(Frequency.class) != null) {
|
|
481 |
bytes += "/s";
|
|
482 |
}
|
|
483 |
println(bytes);
|
|
484 |
return true;
|
|
485 |
}
|
|
486 |
}
|
|
487 |
MemoryAddress memoryAddress = field.getAnnotation(MemoryAddress.class);
|
|
488 |
if (memoryAddress != null) {
|
|
489 |
if (value instanceof Number) {
|
|
490 |
long d = ((Number) value).longValue();
|
|
491 |
println(String.format("0x%08X", d));
|
|
492 |
return true;
|
|
493 |
}
|
|
494 |
}
|
|
495 |
return false;
|
|
496 |
}
|
|
497 |
|
|
498 |
public void setShowIds(boolean showIds) {
|
|
499 |
this.showIds = showIds;
|
|
500 |
}
|
|
501 |
}
|