| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.expresslog; |
| 18 |
|
| 19 |
import android.annotation.FloatRange; |
| 20 |
import android.annotation.IntRange; |
| 21 |
import android.annotation.NonNull; |
| 22 |
|
| 23 |
import com.android.internal.util.FrameworkStatsLog; |
| 24 |
|
| 25 |
import java.util.Arrays; |
| 26 |
|
| 27 |
|
| 28 |
public final class Histogram { |
| 29 |
|
| 30 |
private final long mMetricIdHash; |
| 31 |
private final BinOptions mBinOptions; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public Histogram(@NonNull String metricId, @NonNull BinOptions binOptions) { |
| 41 |
mMetricIdHash = Utils.hashString(metricId); |
| 42 |
mBinOptions = binOptions; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public void logSample(float sample) { |
| 52 |
final int binIndex = mBinOptions.getBinForSample(sample); |
| 53 |
FrameworkStatsLog.write(FrameworkStatsLog.EXPRESS_HISTOGRAM_SAMPLE_REPORTED, mMetricIdHash, |
| 54 |
1, binIndex); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public void logSampleWithUid(int uid, float sample) { |
| 65 |
final int binIndex = mBinOptions.getBinForSample(sample); |
| 66 |
FrameworkStatsLog.write(FrameworkStatsLog.EXPRESS_UID_HISTOGRAM_SAMPLE_REPORTED, |
| 67 |
mMetricIdHash, 1, binIndex, uid); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public interface BinOptions { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
int getBinsCount(); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
int getBinForSample(float sample); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
public static final class UniformOptions implements BinOptions { |
| 93 |
|
| 94 |
private final int mBinCount; |
| 95 |
private final float mMinValue; |
| 96 |
private final float mExclusiveMaxValue; |
| 97 |
private final float mBinSize; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public UniformOptions(@IntRange(from = 1) int binCount, float minValue, |
| 112 |
float exclusiveMaxValue) { |
| 113 |
if (binCount < 1) { |
| 114 |
throw new IllegalArgumentException("Bin count should be positive number"); |
| 115 |
} |
| 116 |
|
| 117 |
if (exclusiveMaxValue <= minValue) { |
| 118 |
throw new IllegalArgumentException("Bins range invalid (maxValue < minValue)"); |
| 119 |
} |
| 120 |
|
| 121 |
mMinValue = minValue; |
| 122 |
mExclusiveMaxValue = exclusiveMaxValue; |
| 123 |
mBinSize = (mExclusiveMaxValue - minValue) / binCount; |
| 124 |
|
| 125 |
|
| 126 |
mBinCount = binCount + 2; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public int getBinsCount() { |
| 131 |
return mBinCount; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public int getBinForSample(float sample) { |
| 136 |
if (sample < mMinValue) { |
| 137 |
|
| 138 |
return 0; |
| 139 |
} else if (sample >= mExclusiveMaxValue) { |
| 140 |
|
| 141 |
return mBinCount - 1; |
| 142 |
} |
| 143 |
return (int) ((sample - mMinValue) / mBinSize + 1); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
public static final class ScaledRangeOptions implements BinOptions { |
| 149 |
|
| 150 |
final long[] mBins; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public ScaledRangeOptions(@IntRange(from = 1) int binCount, int minValue, |
| 165 |
@FloatRange(from = 1.f) float firstBinWidth, |
| 166 |
@FloatRange(from = 1.f) float scaleFactor) { |
| 167 |
if (binCount < 1) { |
| 168 |
throw new IllegalArgumentException("Bin count should be positive number"); |
| 169 |
} |
| 170 |
|
| 171 |
if (firstBinWidth < 1.f) { |
| 172 |
throw new IllegalArgumentException( |
| 173 |
"First bin width invalid (should be 1.f at minimum)"); |
| 174 |
} |
| 175 |
|
| 176 |
if (scaleFactor < 1.f) { |
| 177 |
throw new IllegalArgumentException( |
| 178 |
"Scaled factor invalid (should be 1.f at minimum)"); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
mBins = initBins(binCount + 1, minValue, firstBinWidth, scaleFactor); |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
public int getBinsCount() { |
| 187 |
return mBins.length + 1; |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public int getBinForSample(float sample) { |
| 192 |
if (sample < mBins[0]) { |
| 193 |
|
| 194 |
return 0; |
| 195 |
} else if (sample >= mBins[mBins.length - 1]) { |
| 196 |
|
| 197 |
return mBins.length; |
| 198 |
} |
| 199 |
|
| 200 |
return lower_bound(mBins, (long) sample) + 1; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
private static int lower_bound(long[] array, long sample) { |
| 205 |
int index = Arrays.binarySearch(array, sample); |
| 206 |
|
| 207 |
if (index < 0) { |
| 208 |
|
| 209 |
|
| 210 |
return Math.abs(index) - 2; |
| 211 |
} |
| 212 |
return index; |
| 213 |
} |
| 214 |
|
| 215 |
private static long[] initBins(int count, int minValue, float firstBinWidth, |
| 216 |
float scaleFactor) { |
| 217 |
long[] bins = new long[count]; |
| 218 |
bins[0] = minValue; |
| 219 |
double lastWidth = firstBinWidth; |
| 220 |
for (int i = 1; i < count; i++) { |
| 221 |
|
| 222 |
double currentBinMinValue = bins[i - 1] + lastWidth; |
| 223 |
if (currentBinMinValue > Integer.MAX_VALUE) { |
| 224 |
throw new IllegalArgumentException( |
| 225 |
"Attempted to create a bucket larger than maxint"); |
| 226 |
} |
| 227 |
|
| 228 |
bins[i] = (long) currentBinMinValue; |
| 229 |
lastWidth *= scaleFactor; |
| 230 |
} |
| 231 |
return bins; |
| 232 |
} |
| 233 |
} |
| 234 |
} |