Skip to content

Commit

Permalink
Make profiler output readable
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Sep 17, 2024
1 parent 23eddfe commit 82b31da
Showing 1 changed file with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.mojang.logging.LogUtils;
import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.longs.LongArrayFIFOQueue;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
import org.slf4j.Logger;
Expand Down Expand Up @@ -169,6 +171,7 @@ private static final class ProfileNode {
public final List<ProfileNode> children = new ArrayList<>();
public long childrenTimingCount;
public int depth = -1;
public boolean lastChild;

private ProfileNode(final ProfileNode parent, final int nodeId, final LProfilerRegistry.ProfilerEntry profiler,
final long totalTime, final long totalCount) {
Expand All @@ -188,11 +191,6 @@ public static final record ProfilingData(
long[] timers,
long[] counters
) {
private static final char[][] INDENT_PATTERNS = new char[][] {
"|---".toCharArray(),
"|+++".toCharArray(),
};

public List<String> dumpToString() {
final List<LProfileGraph.GraphNode> graphDFS = this.graph.getDFS();
final Reference2ReferenceOpenHashMap<LProfileGraph.GraphNode, ProfileNode> nodeMap = new Reference2ReferenceOpenHashMap<>();
Expand Down Expand Up @@ -232,14 +230,10 @@ public List<String> dumpToString() {
totalTime += node.totalTime;
}

final ArrayDeque<ProfileNode> flatOrderedNodes = new ArrayDeque<>();

ProfileNode profileNode;
final StringBuilder builder = new StringBuilder();
while ((profileNode = orderedNodes.pollFirst()) != null) {
if (profileNode.nodeId != LProfileGraph.ROOT_NODE && profileNode.totalCount == 0L) {
// skip nodes not recorded
continue;
}

final int depth = profileNode.depth;
profileNode.children.sort((final ProfileNode p1, final ProfileNode p2) -> {
final int typeCompare = p1.profiler.type().compareTo(p2.profiler.type());
Expand All @@ -257,12 +251,32 @@ public List<String> dumpToString() {
}
});

boolean first = true;
for (int i = profileNode.children.size() - 1; i >= 0; --i) {
final ProfileNode child = profileNode.children.get(i);
if (child.totalCount == 0L) {
// skip nodes not recorded
continue;
}
if (first) {
child.lastChild = true;
first = false;
}
child.depth = depth + 1;
orderedNodes.addFirst(child);
}

flatOrderedNodes.addLast(profileNode);
}

final StringBuilder builder = new StringBuilder();
final IntList closed = new IntArrayList();
while ((profileNode = flatOrderedNodes.pollFirst()) != null) {
final int depth = profileNode.depth;
closed.removeIf((int d) -> d >= depth);
if (profileNode.lastChild) {
closed.add(depth);
}
if (profileNode.nodeId == LProfileGraph.ROOT_NODE) {
// don't display root
continue;
Expand All @@ -280,9 +294,18 @@ public List<String> dumpToString() {
// <indent>#<name> avg X sum Y
builder.setLength(0);
// prepare indent
final char[] indent = INDENT_PATTERNS[ret.size() % INDENT_PATTERNS.length];
for (int i = 0; i < depth; ++i) {
builder.append(indent);
if (i == depth - 1) {
if (flatOrderedNodes.peekFirst() == null || profileNode.lastChild) {
builder.append(" └─");
} else {
builder.append(" ├─");
}
} else if (!closed.contains(i + 1)) {
builder.append(" │ ");
} else {
builder.append(" ");
}
}

switch (profilerEntry.type()) {
Expand Down

0 comments on commit 82b31da

Please sign in to comment.