Merge "Change BinderLatencyBuckets to use int[] instead of ArrayList to improve performance."
This commit is contained in:
@@ -20,8 +20,7 @@ import android.util.Slog;
|
|||||||
|
|
||||||
import com.android.internal.annotations.VisibleForTesting;
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the bucket thresholds (with a custom logarithmic scale) for a histogram to store
|
* Generates the bucket thresholds (with a custom logarithmic scale) for a histogram to store
|
||||||
@@ -29,7 +28,7 @@ import java.util.Collections;
|
|||||||
*/
|
*/
|
||||||
public class BinderLatencyBuckets {
|
public class BinderLatencyBuckets {
|
||||||
private static final String TAG = "BinderLatencyBuckets";
|
private static final String TAG = "BinderLatencyBuckets";
|
||||||
private ArrayList<Integer> mBuckets;
|
private final int[] mBuckets;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bucketCount the number of buckets the histogram should have
|
* @param bucketCount the number of buckets the histogram should have
|
||||||
@@ -37,12 +36,11 @@ public class BinderLatencyBuckets {
|
|||||||
* @param scaleFactor the rate in which each consecutive bucket increases (before rounding)
|
* @param scaleFactor the rate in which each consecutive bucket increases (before rounding)
|
||||||
*/
|
*/
|
||||||
public BinderLatencyBuckets(int bucketCount, int firstBucketSize, float scaleFactor) {
|
public BinderLatencyBuckets(int bucketCount, int firstBucketSize, float scaleFactor) {
|
||||||
mBuckets = new ArrayList<>(bucketCount - 1);
|
int[] buffer = new int[bucketCount - 1];
|
||||||
mBuckets.add(firstBucketSize);
|
buffer[0] = firstBucketSize;
|
||||||
|
|
||||||
// Last value and the target are disjoint as we never want to create buckets smaller than 1.
|
// Last value and the target are disjoint as we never want to create buckets smaller than 1.
|
||||||
double lastTarget = firstBucketSize;
|
double lastTarget = firstBucketSize;
|
||||||
int lastValue = firstBucketSize;
|
|
||||||
|
|
||||||
// First bucket is already created and the last bucket is anything greater than the final
|
// First bucket is already created and the last bucket is anything greater than the final
|
||||||
// bucket in the list, so create 'bucketCount' - 2 buckets.
|
// bucket in the list, so create 'bucketCount' - 2 buckets.
|
||||||
@@ -50,29 +48,29 @@ public class BinderLatencyBuckets {
|
|||||||
// Increase the target bucket limit value by the scale factor.
|
// Increase the target bucket limit value by the scale factor.
|
||||||
double nextTarget = lastTarget * scaleFactor;
|
double nextTarget = lastTarget * scaleFactor;
|
||||||
|
|
||||||
if (nextTarget > Integer.MAX_VALUE || lastValue == Integer.MAX_VALUE) {
|
if (nextTarget > Integer.MAX_VALUE) {
|
||||||
// Do not throw an exception here as this should not affect binder calls.
|
// Do not throw an exception here as this should not affect binder calls.
|
||||||
Slog.w(TAG, "Attempted to create a bucket larger than maxint");
|
Slog.w(TAG, "Attempted to create a bucket larger than maxint");
|
||||||
|
mBuckets = Arrays.copyOfRange(buffer, 0, i);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((int) nextTarget > lastValue) {
|
if ((int) nextTarget > buffer[i - 1]) {
|
||||||
// Convert the target bucket limit value to an integer.
|
// Convert the target bucket limit value to an integer.
|
||||||
mBuckets.add((int) nextTarget);
|
buffer[i] = (int) nextTarget;
|
||||||
lastValue = (int) nextTarget;
|
|
||||||
} else {
|
} else {
|
||||||
// Avoid creating redundant buckets, so bucket size should be 1 at a minimum.
|
// Avoid creating redundant buckets, so bucket size should be 1 at a minimum.
|
||||||
mBuckets.add(lastValue + 1);
|
buffer[i] = buffer[i - 1] + 1;
|
||||||
lastValue = lastValue + 1;
|
|
||||||
}
|
}
|
||||||
lastTarget = nextTarget;
|
lastTarget = nextTarget;
|
||||||
}
|
}
|
||||||
|
mBuckets = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets the bucket index to insert the provided sample in. */
|
/** Gets the bucket index to insert the provided sample in. */
|
||||||
public int sampleToBucket(int sample) {
|
public int sampleToBucket(int sample) {
|
||||||
if (sample > mBuckets.get(mBuckets.size() - 1)) {
|
if (sample >= mBuckets[mBuckets.length - 1]) {
|
||||||
return mBuckets.size();
|
return mBuckets.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Binary search returns the element index if it is contained in the list - in this case the
|
// Binary search returns the element index if it is contained in the list - in this case the
|
||||||
@@ -80,12 +78,12 @@ public class BinderLatencyBuckets {
|
|||||||
// Otherwise, it returns (-(insertion point) - 1), where insertion point is the point where
|
// Otherwise, it returns (-(insertion point) - 1), where insertion point is the point where
|
||||||
// to insert the element so that the array remains sorted - in this case the bucket index
|
// to insert the element so that the array remains sorted - in this case the bucket index
|
||||||
// is the insertion point.
|
// is the insertion point.
|
||||||
int searchResult = Collections.binarySearch(mBuckets, sample);
|
int searchResult = Arrays.binarySearch(mBuckets, sample);
|
||||||
return searchResult < 0 ? -(1 + searchResult) : searchResult + 1;
|
return searchResult < 0 ? -(1 + searchResult) : searchResult + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public ArrayList<Integer> getBuckets() {
|
public int[] getBuckets() {
|
||||||
return mBuckets;
|
return mBuckets;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ public class BinderLatencyBucketsTest {
|
|||||||
public void testBucketThresholds() {
|
public void testBucketThresholds() {
|
||||||
BinderLatencyBuckets latencyBuckets = new BinderLatencyBuckets(10, 2, 1.45f);
|
BinderLatencyBuckets latencyBuckets = new BinderLatencyBuckets(10, 2, 1.45f);
|
||||||
assertThat(latencyBuckets.getBuckets())
|
assertThat(latencyBuckets.getBuckets())
|
||||||
|
.asList()
|
||||||
.containsExactly(2, 3, 4, 6, 8, 12, 18, 26, 39)
|
.containsExactly(2, 3, 4, 6, 8, 12, 18, 26, 39)
|
||||||
.inOrder();
|
.inOrder();
|
||||||
}
|
}
|
||||||
@@ -58,6 +59,7 @@ public class BinderLatencyBucketsTest {
|
|||||||
public void testMaxIntBuckets() {
|
public void testMaxIntBuckets() {
|
||||||
BinderLatencyBuckets latencyBuckets = new BinderLatencyBuckets(5, Integer.MAX_VALUE / 2, 2);
|
BinderLatencyBuckets latencyBuckets = new BinderLatencyBuckets(5, Integer.MAX_VALUE / 2, 2);
|
||||||
assertThat(latencyBuckets.getBuckets())
|
assertThat(latencyBuckets.getBuckets())
|
||||||
|
.asList()
|
||||||
.containsExactly(Integer.MAX_VALUE / 2, Integer.MAX_VALUE - 1)
|
.containsExactly(Integer.MAX_VALUE / 2, Integer.MAX_VALUE - 1)
|
||||||
.inOrder();
|
.inOrder();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user