Merge "Modify TelephonyHistogram.java functions." into nyc-mr1-dev

This commit is contained in:
Sanket Padawe
2016-06-17 17:24:12 +00:00
committed by Android (Google) Code Review

View File

@@ -31,35 +31,35 @@ import java.util.List;
public final class TelephonyHistogram implements Parcelable { public final class TelephonyHistogram implements Parcelable {
// Type of Telephony histogram Eg: RIL histogram will have all timing data associated with // Type of Telephony histogram Eg: RIL histogram will have all timing data associated with
// RIL calls. Similarly we can have any other Telephony histogram. // RIL calls. Similarly we can have any other Telephony histogram.
private final int category; private final int mCategory;
// Unique Id identifying a sample within particular category of histogram // Unique Id identifying a sample within particular category of histogram
private final int id; private final int mId;
// Min time taken in ms // Min time taken in ms
private int minTimeMs; private int mMinTimeMs;
// Max time taken in ms // Max time taken in ms
private int maxTimeMs; private int mMaxTimeMs;
// Average time taken in ms // Average time taken in ms
private int averageTimeMs; private int mAverageTimeMs;
// Total count of samples // Total count of samples
private int sampleCount; private int mSampleCount;
// Array storing time taken for first #RANGE_CALCULATION_COUNT samples of histogram. // Array storing time taken for first #RANGE_CALCULATION_COUNT samples of histogram.
private int[] initialTimings; private int[] mInitialTimings;
// Total number of time ranges expected (must be greater than 1) // Total number of time ranges expected (must be greater than 1)
private final int bucketCount; private final int mBucketCount;
// Array storing endpoints of range buckets. Calculated based on values of minTime & maxTime // Array storing endpoints of range buckets. Calculated based on values of minTime & maxTime
// after totalTimeCount is #RANGE_CALCULATION_COUNT. // after totalTimeCount is #RANGE_CALCULATION_COUNT.
private final int[] bucketEndPoints; private final int[] mBucketEndPoints;
// Array storing counts for each time range starting from smallest value range // Array storing counts for each time range starting from smallest value range
private final int[] bucketCounters; private final int[] mBucketCounters;
/** /**
* Constant for Telephony category * Constant for Telephony category
@@ -81,69 +81,85 @@ public final class TelephonyHistogram implements Parcelable {
if (bucketCount <= 1) { if (bucketCount <= 1) {
throw new IllegalArgumentException("Invalid number of buckets"); throw new IllegalArgumentException("Invalid number of buckets");
} }
this.category = category; mCategory = category;
this.id = id; mId = id;
this.minTimeMs = Integer.MAX_VALUE; mMinTimeMs = Integer.MAX_VALUE;
this.maxTimeMs = 0; mMaxTimeMs = 0;
this.averageTimeMs = 0; mAverageTimeMs = 0;
this.sampleCount = 0; mSampleCount = 0;
initialTimings = new int[RANGE_CALCULATION_COUNT]; mInitialTimings = new int[RANGE_CALCULATION_COUNT];
this.bucketCount = bucketCount; mBucketCount = bucketCount;
bucketEndPoints = new int[bucketCount - 1]; mBucketEndPoints = new int[bucketCount - 1];
bucketCounters = new int[bucketCount]; mBucketCounters = new int[bucketCount];
} }
public TelephonyHistogram(TelephonyHistogram th) { public TelephonyHistogram(TelephonyHistogram th) {
category = th.getCategory(); mCategory = th.getCategory();
id = th.getId(); mId = th.getId();
minTimeMs = th.getMinTime(); mMinTimeMs = th.getMinTime();
maxTimeMs = th.getMaxTime(); mMaxTimeMs = th.getMaxTime();
averageTimeMs = th.getAverageTime(); mAverageTimeMs = th.getAverageTime();
sampleCount = th.getSampleCount(); mSampleCount = th.getSampleCount();
initialTimings = th.getInitialTimings(); mInitialTimings = th.getInitialTimings();
bucketCount = th.getBucketCount(); mBucketCount = th.getBucketCount();
bucketEndPoints = th.getBucketEndPoints(); mBucketEndPoints = th.getBucketEndPoints();
bucketCounters = th.getBucketCounters(); mBucketCounters = th.getBucketCounters();
} }
public int getCategory() { public int getCategory() {
return category; return mCategory;
} }
public int getId() { public int getId() {
return id; return mId;
} }
public int getMinTime() { public int getMinTime() {
return minTimeMs; return mMinTimeMs;
} }
public int getMaxTime() { public int getMaxTime() {
return maxTimeMs; return mMaxTimeMs;
} }
public int getAverageTime() { public int getAverageTime() {
return averageTimeMs; return mAverageTimeMs;
} }
public int getSampleCount () { public int getSampleCount () {
return sampleCount; return mSampleCount;
} }
private int[] getInitialTimings() { private int[] getInitialTimings() {
return initialTimings; return mInitialTimings;
} }
public int getBucketCount() { public int getBucketCount() {
return bucketCount; return mBucketCount;
} }
public int[] getBucketEndPoints() { public int[] getBucketEndPoints() {
return getDeepCopyOfArray(bucketEndPoints); if (mSampleCount > 1 && mSampleCount < 10) {
int[] tempEndPoints = new int[mBucketCount - 1];
calculateBucketEndPoints(tempEndPoints);
return tempEndPoints;
} else {
return getDeepCopyOfArray(mBucketEndPoints);
}
} }
public int[] getBucketCounters() { public int[] getBucketCounters() {
return getDeepCopyOfArray(bucketCounters); if (mSampleCount > 1 && mSampleCount < 10) {
int[] tempEndPoints = new int[mBucketCount - 1];
int[] tempBucketCounters = new int[mBucketCount];
calculateBucketEndPoints(tempEndPoints);
for (int j = 0; j < mSampleCount; j++) {
addToBucketCounter(tempEndPoints, tempBucketCounters, mInitialTimings[j]);
}
return tempBucketCounters;
} else {
return getDeepCopyOfArray(mBucketCounters);
}
} }
private int[] getDeepCopyOfArray(int[] array) { private int[] getDeepCopyOfArray(int[] array) {
@@ -152,7 +168,7 @@ public final class TelephonyHistogram implements Parcelable {
return clone; return clone;
} }
private void addToBucketCounter(int time) { private void addToBucketCounter(int[] bucketEndPoints, int[] bucketCounters, int time) {
int i; int i;
for (i = 0; i < bucketEndPoints.length; i++) { for (i = 0; i < bucketEndPoints.length; i++) {
if (time <= bucketEndPoints[i]) { if (time <= bucketEndPoints[i]) {
@@ -163,6 +179,13 @@ public final class TelephonyHistogram implements Parcelable {
bucketCounters[i]++; bucketCounters[i]++;
} }
private void calculateBucketEndPoints(int[] bucketEndPoints) {
for (int i = 1; i < mBucketCount; i++) {
int endPt = mMinTimeMs + (i * (mMaxTimeMs - mMinTimeMs)) / mBucketCount;
bucketEndPoints[i - 1] = endPt;
}
}
// Add new value of time taken // Add new value of time taken
// This function updates minTime, maxTime, averageTime & totalTimeCount every time it is // This function updates minTime, maxTime, averageTime & totalTimeCount every time it is
// called. initialTimings[] is updated if totalTimeCount <= #RANGE_CALCULATION_COUNT. When // called. initialTimings[] is updated if totalTimeCount <= #RANGE_CALCULATION_COUNT. When
@@ -172,65 +195,62 @@ public final class TelephonyHistogram implements Parcelable {
public void addTimeTaken(int time) { public void addTimeTaken(int time) {
// Initialize all fields if its first entry or if integer overflow is going to occur while // Initialize all fields if its first entry or if integer overflow is going to occur while
// trying to calculate averageTime // trying to calculate averageTime
if (sampleCount == 0 || (sampleCount == Integer.MAX_VALUE)) { if (mSampleCount == 0 || (mSampleCount == Integer.MAX_VALUE)) {
if (sampleCount == 0) { if (mSampleCount == 0) {
minTimeMs = time; mMinTimeMs = time;
maxTimeMs = time; mMaxTimeMs = time;
averageTimeMs = time; mAverageTimeMs = time;
} else { } else {
initialTimings = new int[RANGE_CALCULATION_COUNT]; mInitialTimings = new int[RANGE_CALCULATION_COUNT];
} }
sampleCount = 1; mSampleCount = 1;
Arrays.fill(initialTimings, 0); Arrays.fill(mInitialTimings, 0);
initialTimings[0] = time; mInitialTimings[0] = time;
Arrays.fill(bucketEndPoints, 0); Arrays.fill(mBucketEndPoints, 0);
Arrays.fill(bucketCounters, 0); Arrays.fill(mBucketCounters, 0);
} else { } else {
if (time < minTimeMs) { if (time < mMinTimeMs) {
minTimeMs = time; mMinTimeMs = time;
} }
if (time > maxTimeMs) { if (time > mMaxTimeMs) {
maxTimeMs = time; mMaxTimeMs = time;
} }
long totalTime = ((long)averageTimeMs) * sampleCount + time; long totalTime = ((long)mAverageTimeMs) * mSampleCount + time;
averageTimeMs = (int)(totalTime/++sampleCount); mAverageTimeMs = (int)(totalTime/++mSampleCount);
if (sampleCount < RANGE_CALCULATION_COUNT) { if (mSampleCount < RANGE_CALCULATION_COUNT) {
initialTimings[sampleCount - 1] = time; mInitialTimings[mSampleCount - 1] = time;
} else if (sampleCount == RANGE_CALCULATION_COUNT) { } else if (mSampleCount == RANGE_CALCULATION_COUNT) {
initialTimings[sampleCount - 1] = time; mInitialTimings[mSampleCount - 1] = time;
// Calculate bucket endpoints based on bucketCount expected // Calculate bucket endpoints based on bucketCount expected
for (int i = 1; i < bucketCount; i++) { calculateBucketEndPoints(mBucketEndPoints);
int endPt = minTimeMs + (i * (maxTimeMs - minTimeMs)) / bucketCount;
bucketEndPoints[i - 1] = endPt;
}
// Use values stored in initialTimings[] to update bucketCounters // Use values stored in initialTimings[] to update bucketCounters
for (int j = 0; j < RANGE_CALCULATION_COUNT; j++) { for (int j = 0; j < RANGE_CALCULATION_COUNT; j++) {
addToBucketCounter(initialTimings[j]); addToBucketCounter(mBucketEndPoints, mBucketCounters, mInitialTimings[j]);
} }
initialTimings = null; mInitialTimings = null;
} else { } else {
addToBucketCounter(time); addToBucketCounter(mBucketEndPoints, mBucketCounters, time);
} }
} }
} }
public String toString() { public String toString() {
String basic = " Histogram id = " + id + " Time(ms): min = " + minTimeMs + " max = " String basic = " Histogram id = " + mId + " Time(ms): min = " + mMinTimeMs + " max = "
+ maxTimeMs + " avg = " + averageTimeMs + " Count = " + sampleCount; + mMaxTimeMs + " avg = " + mAverageTimeMs + " Count = " + mSampleCount;
if (sampleCount < RANGE_CALCULATION_COUNT) { if (mSampleCount < RANGE_CALCULATION_COUNT) {
return basic; return basic;
} else { } else {
StringBuffer intervals = new StringBuffer(" Interval Endpoints:"); StringBuffer intervals = new StringBuffer(" Interval Endpoints:");
for (int i = 0; i < bucketEndPoints.length; i++) { for (int i = 0; i < mBucketEndPoints.length; i++) {
intervals.append(" " + bucketEndPoints[i]); intervals.append(" " + mBucketEndPoints[i]);
} }
intervals.append(" Interval counters:"); intervals.append(" Interval counters:");
for (int i = 0; i < bucketCounters.length; i++) { for (int i = 0; i < mBucketCounters.length; i++) {
intervals.append(" " + bucketCounters[i]); intervals.append(" " + mBucketCounters[i]);
} }
return basic + intervals; return basic + intervals;
} }
@@ -251,39 +271,39 @@ public final class TelephonyHistogram implements Parcelable {
}; };
public TelephonyHistogram(Parcel in) { public TelephonyHistogram(Parcel in) {
category = in.readInt(); mCategory = in.readInt();
id = in.readInt(); mId = in.readInt();
minTimeMs = in.readInt(); mMinTimeMs = in.readInt();
maxTimeMs = in.readInt(); mMaxTimeMs = in.readInt();
averageTimeMs = in.readInt(); mAverageTimeMs = in.readInt();
sampleCount = in.readInt(); mSampleCount = in.readInt();
if (in.readInt() == PRESENT) { if (in.readInt() == PRESENT) {
initialTimings = new int[RANGE_CALCULATION_COUNT]; mInitialTimings = new int[RANGE_CALCULATION_COUNT];
in.readIntArray(initialTimings); in.readIntArray(mInitialTimings);
} }
bucketCount = in.readInt(); mBucketCount = in.readInt();
bucketEndPoints = new int[bucketCount - 1]; mBucketEndPoints = new int[mBucketCount - 1];
in.readIntArray(bucketEndPoints); in.readIntArray(mBucketEndPoints);
bucketCounters = new int[bucketCount]; mBucketCounters = new int[mBucketCount];
in.readIntArray(bucketCounters); in.readIntArray(mBucketCounters);
} }
public void writeToParcel(Parcel out, int flags) { public void writeToParcel(Parcel out, int flags) {
out.writeInt(category); out.writeInt(mCategory);
out.writeInt(id); out.writeInt(mId);
out.writeInt(minTimeMs); out.writeInt(mMinTimeMs);
out.writeInt(maxTimeMs); out.writeInt(mMaxTimeMs);
out.writeInt(averageTimeMs); out.writeInt(mAverageTimeMs);
out.writeInt(sampleCount); out.writeInt(mSampleCount);
if (initialTimings == null) { if (mInitialTimings == null) {
out.writeInt(ABSENT); out.writeInt(ABSENT);
} else { } else {
out.writeInt(PRESENT); out.writeInt(PRESENT);
out.writeIntArray(initialTimings); out.writeIntArray(mInitialTimings);
} }
out.writeInt(bucketCount); out.writeInt(mBucketCount);
out.writeIntArray(bucketEndPoints); out.writeIntArray(mBucketEndPoints);
out.writeIntArray(bucketCounters); out.writeIntArray(mBucketCounters);
} }
@Override @Override