提交 becf968f 编写于 作者: G goetz

Merge

......@@ -57,9 +57,14 @@ import jdk.jfr.internal.Utils;
* This class is also used by {@link RecordedObject#toString()}
*/
public final class PrettyWriter extends EventPrintWriter {
private static final Duration MILLSECOND = Duration.ofMillis(1);
private static final Duration SECOND = Duration.ofSeconds(1);
private static final Duration MINUTE = Duration.ofMinutes(1);
private static final String TYPE_OLD_OBJECT = Type.TYPES_PREFIX + "OldObject";
private final static DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
private final static Long ZERO = 0L;
private boolean showIds;
private RecordedEvent currentEvent;
public PrettyWriter(PrintWriter destination) {
super(destination);
......@@ -198,6 +203,7 @@ public final class PrettyWriter extends EventPrintWriter {
}
public void print(RecordedEvent event) {
currentEvent = event;
print(event.getEventType().getName(), " ");
println("{");
indent();
......@@ -308,7 +314,11 @@ public final class PrettyWriter extends EventPrintWriter {
println(formatMethod((RecordedMethod) value));
return;
}
print((RecordedObject) value, postFix);
if (field.getTypeName().equals(TYPE_OLD_OBJECT)) {
printOldObject((RecordedObject) value);
return;
}
print((RecordedObject) value, postFix);
return;
}
if (value.getClass().isArray()) {
......@@ -356,6 +366,70 @@ public final class PrettyWriter extends EventPrintWriter {
println(text);
}
private void printOldObject(RecordedObject object) {
println(" [");
indent();
printIndent();
try {
printReferenceChain(object);
} catch (IllegalArgumentException iae) {
// Could not find a field
// Not possible to validate fields beforehand using RecordedObject#hasField
// since nested objects, for example object.referrer.array.index, requires
// an actual array object (which may be null).
}
retract();
printIndent();
println("]");
}
private void printReferenceChain(RecordedObject object) {
printObject(object, currentEvent.getLong("arrayElements"));
for (RecordedObject ref = object.getValue("referrer"); ref != null; ref = object.getValue("referrer")) {
long skip = ref.getLong("skip");
if (skip > 0) {
printIndent();
println("...");
}
String objectHolder = "";
long size = Long.MIN_VALUE;
RecordedObject array = ref.getValue("array");
if (array != null) {
long index = array.getLong("index");
size = array.getLong("size");
objectHolder = "[" + index + "]";
}
RecordedObject field = ref.getValue("field");
if (field != null) {
objectHolder = field.getString("name");
}
printIndent();
print(objectHolder);
print(" : ");
object = ref.getValue("object");
if (object != null) {
printObject(object, size);
}
}
}
void printObject(RecordedObject object, long arraySize) {
RecordedClass clazz = object.getClass("type");
if (clazz != null) {
String className = clazz.getName();
if (className!= null && className.startsWith("[")) {
className = decodeDescriptors(className, arraySize > 0 ? Long.toString(arraySize) : "").get(0);
}
print(className);
String description = object.getString("description");
if (description != null) {
print(" ");
print(description);
}
}
println();
}
private void printClassLoader(RecordedClassLoader cl, String postFix) {
// Purposely not printing class loader name to avoid cluttered output
RecordedClass clazz = cl.getType();
......@@ -386,7 +460,7 @@ public final class PrettyWriter extends EventPrintWriter {
StringJoiner sj = new StringJoiner(", ");
String md = m.getDescriptor().replace("/", ".");
String parameter = md.substring(1, md.lastIndexOf(")"));
for (String qualifiedName : decodeDescriptors(parameter)) {
for (String qualifiedName : decodeDescriptors(parameter, "")) {
String typeName = qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1);
sj.add(typeName);
}
......@@ -407,17 +481,18 @@ public final class PrettyWriter extends EventPrintWriter {
}
String className = clazz.getName();
if (className.startsWith("[")) {
className = decodeDescriptors(className).get(0);
className = decodeDescriptors(className, "").get(0);
}
println(className + " (classLoader = " + classLoaderName + ")" + postFix);
}
List<String> decodeDescriptors(String descriptor) {
List<String> decodeDescriptors(String descriptor, String arraySize) {
List<String> descriptors = new ArrayList<>();
for (int index = 0; index < descriptor.length(); index++) {
String arrayBrackets = "";
while (descriptor.charAt(index) == '[') {
arrayBrackets += "[]";
arrayBrackets = arrayBrackets + "[" + arraySize + "]" ;
arraySize = "";
index++;
}
char c = descriptor.charAt(index);
......@@ -476,19 +551,14 @@ public final class PrettyWriter extends EventPrintWriter {
println("N/A");
return true;
}
double s = d.toNanosPart() / 1000_000_000.0 + d.toSecondsPart();
if (s < 1.0) {
if (s < 0.001) {
println(String.format("%.3f", s * 1_000_000) + " us");
} else {
println(String.format("%.3f", s * 1_000) + " ms");
}
if(d.compareTo(MILLSECOND) < 0){
println(String.format("%.3f us", (double)d.toNanos() / 1_000));
} else if(d.compareTo(SECOND) < 0){
println(String.format("%.3f ms", (double)d.toNanos() / 1_000_000));
} else if(d.compareTo(MINUTE) < 0){
println(String.format("%.3f s", (double)d.toMillis() / 1_000));
} else {
if (s < 1000.0) {
println(String.format("%.3f", s) + " s");
} else {
println(String.format("%.0f", s) + " s");
}
println(String.format("%d s", d.toSeconds()));
}
return true;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册