Skip to content

Commit

Permalink
An option to coarsen flame graphs (async-profiler#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin authored Oct 10, 2024
1 parent 37ff942 commit 061f03d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/converter/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private static void usage() {
"Flame Graph options:\n" +
" --title STRING Flame Graph title\n" +
" --minwidth X Skip frames smaller than X%\n" +
" --grain X Coarsen Flame Graph to the given grain size\n" +
" --skip N Skip N bottom frames\n" +
" -r --reverse Reverse stack traces (icicle graph)\n" +
" -I --include REGEX Include only stacks with the specified frames\n" +
Expand Down
1 change: 1 addition & 0 deletions src/converter/one/convert/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Arguments {
public Pattern include;
public Pattern exclude;
public double minwidth;
public double grain;
public int skip;
public boolean help;
public boolean reverse;
Expand Down
4 changes: 4 additions & 0 deletions src/converter/one/convert/JfrConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ protected EventAggregator collectEvents() throws IOException {
}
}

if (args.grain > 0) {
agg.coarsen(args.grain);
}

return agg;
}

Expand Down
25 changes: 25 additions & 0 deletions src/converter/one/jfr/event/EventAggregator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class EventAggregator {
private long[] samples;
private long[] values;
private int size;
private double fraction;

public EventAggregator(boolean threads, boolean total, double factor) {
this.threads = threads;
Expand Down Expand Up @@ -67,6 +68,30 @@ public void forEach(ValueVisitor visitor) {
}
}

public void coarsen(double grain) {
for (int i = 0; i < keys.length; i++) {
if (keys[i] != null) {
long s0 = samples[i];
long s1 = round(s0 / grain);
if (s1 == 0) {
keys[i] = null;
size--;
}
samples[i] = s1;
values[i] = (long) (values[i] * ((double) s1 / s0));
}
}
}

private long round(double d) {
long r = (long) d;
if ((fraction += d - r) >= 1.0) {
fraction -= 1.0;
r++;
}
return r;
}

private int hashCode(Event e) {
return e.hashCode() + (threads ? e.tid * 31 : 0);
}
Expand Down

0 comments on commit 061f03d

Please sign in to comment.