Remove one atomic integer operation in SingleStatRecorder.
This can be computed as sum after the fact. This also reduces the number of
times the same variable is hit.
@@ -18,7 +18,6 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.profiler.MetricData.HistogramElement;
-import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
@@ -33,7 +32,6 @@ public class SingleStatRecorder implements StatRecorder {
private final int buckets;
private final Object description;
private final AtomicIntegerArray histogram;
- private final AtomicInteger count = new AtomicInteger(0);
private final AtomicLong sum = new AtomicLong(0);
private final AtomicLong sumSquared = new AtomicLong(0);
private final LongAccumulator max = new LongAccumulator(Math::max, -1);
@@ -57,7 +55,10 @@ public class SingleStatRecorder implements StatRecorder {
from = to;
}
result.add(new HistogramElement(Range.atLeast(from), histogram.get(histogram.length() - 1)));
- int n = count.get();
+ int n = 0;
+ for (int i = 0; i < histogram.length(); i++) {
+ n += histogram.get(i);
+ }
double stddev;
if (n == 1) {
stddev = 0;
@@ -65,13 +66,12 @@ public class SingleStatRecorder implements StatRecorder {
stddev = Math.sqrt((sumSquared.longValue() - sum.get() * sum.doubleValue() / n) / n);
}
return new MetricData(
- description, result.build(), count.get(), sum.doubleValue() / n, stddev, max.intValue());
+ description, result.build(), n, sum.doubleValue() / n, stddev, max.intValue());
}
@Override
public void addStat(int duration, Object obj) {
int histogramBucket = Math.min(32 - Integer.numberOfLeadingZeros(duration), buckets - 1);
- count.incrementAndGet();
sum.addAndGet(duration);
sumSquared.addAndGet(duration * duration);
max.accumulate(duration);
