Debian Codegraph @

core/java/com/android/internal/expresslog/Histogram.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 /** Histogram encapsulates StatsD write API calls */
28 public final class Histogram {
29
30 private final long mMetricIdHash;
31 private final BinOptions mBinOptions;
32
33 /**
34 * Creates Histogram metric logging wrapper
35 *
36 * @param metricId to log, logging will be no-op if metricId is not defined in the TeX catalog
37 * @param binOptions to calculate bin index for samples
38 * @hide
39 */
40 public Histogram(@NonNull String metricId, @NonNull BinOptions binOptions) {
41 mMetricIdHash = Utils.hashString(metricId);
42 mBinOptions = binOptions;
43 }
44
45 /**
46 * Logs increment sample count for automatically calculated bin
47 *
48 * @param sample value
49 * @hide
50 */
51 public void logSample(float sample) {
52 final int binIndex = mBinOptions.getBinForSample(sample);
53 FrameworkStatsLog.write(FrameworkStatsLog.EXPRESS_HISTOGRAM_SAMPLE_REPORTED, mMetricIdHash,
54 /*count*/ 1, binIndex);
55 }
56
57 /**
58 * Logs increment sample count for automatically calculated bin
59 *
60 * @param uid used as a dimension for the count metric
61 * @param sample value
62 * @hide
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, /*count*/ 1, binIndex, uid);
68 }
69
70 /** Used by Histogram to map data sample to corresponding bin */
71 public interface BinOptions {
72 /**
73 * Returns bins count to be used by a histogram
74 *
75 * @return bins count used to initialize Options, including overflow & underflow bins
76 * @hide
77 */
78 int getBinsCount();
79
80 /**
81 * Returns bin index for the input sample value
82 * index == 0 stands for underflow
83 * index == getBinsCount() - 1 stands for overflow
84 *
85 * @return zero based index
86 * @hide
87 */
88 int getBinForSample(float sample);
89 }
90
91 /** Used by Histogram to map data sample to corresponding bin for uniform bins */
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 * Creates options for uniform (linear) sized bins
101 *
102 * @param binCount amount of histogram bins. 2 bin indexes will be calculated
103 * automatically to represent underflow & overflow bins
104 * @param minValue is included in the first bin, values less than minValue
105 * go to underflow bin
106 * @param exclusiveMaxValue is included in the overflow bucket. For accurate
107 * measure up to kMax, then exclusiveMaxValue
108 * should be set to kMax + 1
109 * @hide
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 // Implicitly add 2 for the extra underflow & overflow bins
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 // goes to underflow
138 return 0;
139 } else if (sample >= mExclusiveMaxValue) {
140 // goes to overflow
141 return mBinCount - 1;
142 }
143 return (int) ((sample - mMinValue) / mBinSize + 1);
144 }
145 }
146
147 /** Used by Histogram to map data sample to corresponding bin for scaled bins */
148 public static final class ScaledRangeOptions implements BinOptions {
149 // store minimum value per bin
150 final long[] mBins;
151
152 /**
153 * Creates options for scaled range bins
154 *
155 * @param binCount amount of histogram bins. 2 bin indexes will be calculated
156 * automatically to represent underflow & overflow bins
157 * @param minValue is included in the first bin, values less than minValue
158 * go to underflow bin
159 * @param firstBinWidth used to represent first bin width and as a reference to calculate
160 * width for consecutive bins
161 * @param scaleFactor used to calculate width for consecutive bins
162 * @hide
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 // precalculating bins ranges (no need to create a bin for underflow reference value)
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 // goes to underflow
194 return 0;
195 } else if (sample >= mBins[mBins.length - 1]) {
196 // goes to overflow
197 return mBins.length;
198 }
199
200 return lower_bound(mBins, (long) sample) + 1;
201 }
202
203 // To find lower bound using binary search implementation of Arrays utility class
204 private static int lower_bound(long[] array, long sample) {
205 int index = Arrays.binarySearch(array, sample);
206 // If key is not present in the array
207 if (index < 0) {
208 // Index specify the position of the key when inserted in the sorted array
209 // so the element currently present at this position will be the lower bound
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 // current bin minValue = previous bin width * scaleFactor
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 }