Vova Sharaienko | 75f8600 | 2023-02-09 01:51:07 +0000 | [diff] [blame^] | 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 | #include "include/Histogram.h" |
| 18 | |
| 19 | #define LOG_TAG "tex" |
| 20 | |
| 21 | #include <log/log.h> |
| 22 | #include <statslog_express.h> |
| 23 | #include <string.h> |
| 24 | #include <utils/hash/farmhash.h> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace expresslog { |
| 28 | |
| 29 | Histogram::UniformOptions* Histogram::UniformOptions::create(int binCount, float minValue, |
| 30 | float exclusiveMaxValue) { |
| 31 | if (binCount < 1) { |
| 32 | ALOGE("Bin count should be positive number"); |
| 33 | return nullptr; |
| 34 | } |
| 35 | |
| 36 | if (exclusiveMaxValue <= minValue) { |
| 37 | ALOGE("Bins range invalid (maxValue < minValue)"); |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | return new UniformOptions(binCount, minValue, exclusiveMaxValue); |
| 42 | } |
| 43 | |
| 44 | Histogram::UniformOptions::UniformOptions(int binCount, float minValue, float exclusiveMaxValue) |
| 45 | : // Implicitly add 2 for the extra undeflow & overflow bins |
| 46 | mBinCount(binCount + 2), |
| 47 | mMinValue(minValue), |
| 48 | mExclusiveMaxValue(exclusiveMaxValue), |
| 49 | mBinSize((exclusiveMaxValue - minValue) / binCount) { |
| 50 | } |
| 51 | |
| 52 | int Histogram::UniformOptions::getBinForSample(float sample) const { |
| 53 | if (sample < mMinValue) { |
| 54 | // goes to underflow |
| 55 | return 0; |
| 56 | } else if (sample >= mExclusiveMaxValue) { |
| 57 | // goes to overflow |
| 58 | return mBinCount - 1; |
| 59 | } |
| 60 | return (int)((sample - mMinValue) / mBinSize + 1); |
| 61 | } |
| 62 | |
| 63 | Histogram::Histogram(const char* metricName, std::shared_ptr<BinOptions> binOptions) |
| 64 | : mMetricIdHash(farmhash::Fingerprint64(metricName, strlen(metricName))), |
| 65 | mBinOptions(std::move(binOptions)) { |
| 66 | } |
| 67 | |
| 68 | void Histogram::logSample(float sample) const { |
| 69 | const int binIndex = mBinOptions->getBinForSample(sample); |
| 70 | stats_write(EXPRESS_HISTOGRAM_SAMPLE_REPORTED, mMetricIdHash, /*count*/ 1, binIndex); |
| 71 | } |
| 72 | |
| 73 | } // namespace expresslog |
| 74 | } // namespace android |