Skip to content

Commit

Permalink
Fix broken recursive merging and replace it with a depth break.
Browse files Browse the repository at this point in the history
  • Loading branch information
chumer committed Oct 1, 2021
1 parent 8397aa5 commit 926b8ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,13 @@ public ProfilerNode<T> getParent() {
* @since 0.30
*/
public boolean isRecursive() {
return isRecursiveImpl(this);
}

private boolean isRecursiveImpl(ProfilerNode<T> source) {
if (parent.sourceLocation == null) {
return false;
}
if (parent.sourceLocation.equals(source.sourceLocation)) {
if (parent.sourceLocation.equals(this.sourceLocation)) {
return true;
}
return parent.isRecursiveImpl(source);
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ private static final class SourceLocationNodes {
}

private static class OutputEntry {
// break after 128 depth spaces to handle deep recursions
private static final int DEPTH_BREAK = 128;
final SourceLocation location;
int[] tierToSamples = new int[0];
int[] tierToSelfSamples = new int[0];
Expand All @@ -522,9 +524,26 @@ private static class OutputEntry {
}
}

String format(String format, int[] showTiers, long samplePeriod, int indent, long globalTotalSamples, Integer[] tiers) {
static int computeIndentSize(int depth) {
int indent = depth % DEPTH_BREAK;
if (indent != depth) {
indent += formatIndentBreakLabel(depth - indent).length();
}
return indent;
}

private static String formatIndentBreakLabel(int skippedDepth) {
return String.format("(\u21B3%s) ", skippedDepth);
}

String format(String format, int[] showTiers, long samplePeriod, int depth, long globalTotalSamples, Integer[] tiers) {
List<Object> args = new ArrayList<>();
args.add(repeat(" ", indent) + location.getRootName());
int indent = depth % DEPTH_BREAK;
if (indent != depth) {
args.add(formatIndentBreakLabel(depth - indent) + repeat(" ", indent) + location.getRootName());
} else {
args.add(repeat(" ", indent) + location.getRootName());
}
args.add(totalSamples * samplePeriod);
args.add(percent(totalSamples, globalTotalSamples));
maybeAddTiers(args, tierToSamples, totalSamples, showTiers, tiers);
Expand Down Expand Up @@ -601,7 +620,7 @@ private void calculateMaxValues(Map<Thread, Collection<ProfilerNode<CPUSampler.P
}

private void calculateMaxValuesRec(ProfilerNode<CPUSampler.Payload> node, int depth) {
maxNameLength = Math.max(maxNameLength, node.getRootName().length() + depth);
maxNameLength = Math.max(maxNameLength, node.getRootName().length() + OutputEntry.computeIndentSize(depth));
tiers.add(node.getPayload().getNumberOfTiers() - 1);
for (ProfilerNode<CPUSampler.Payload> child : node.getChildren()) {
if (!child.isRecursive()) {
Expand Down Expand Up @@ -644,15 +663,11 @@ private void mergeEntry(List<CallTreeOutputEntry> callTreeEntries, ProfilerNode<
}

private CallTreeOutputEntry makeEntry(ProfilerNode<CPUSampler.Payload> node, int depth) {
maxNameLength = Math.max(maxNameLength, node.getRootName().length() + depth);
maxNameLength = Math.max(maxNameLength, node.getRootName().length() + OutputEntry.computeIndentSize(depth));
tiers.add(node.getPayload().getNumberOfTiers() - 1);
CallTreeOutputEntry entry = new CallTreeOutputEntry(node);
for (ProfilerNode<CPUSampler.Payload> child : node.getChildren()) {
if (child.isRecursive()) {
entry.merge(child.getPayload());
} else {
entry.children.add(makeEntry(child, depth + 1));
}
entry.children.add(makeEntry(child, depth + 1));
}
return entry;
}
Expand Down

0 comments on commit 926b8ea

Please sign in to comment.