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 | |
Vova Sharaienko | 8ba8b35 | 2023-02-11 02:06:37 +0000 | [diff] [blame] | 29 | std::shared_ptr<Histogram::UniformOptions> Histogram::UniformOptions::create( |
| 30 | int binCount, float minValue, float exclusiveMaxValue) { |
Vova Sharaienko | 75f8600 | 2023-02-09 01:51:07 +0000 | [diff] [blame] | 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 | |
Vova Sharaienko | 8ba8b35 | 2023-02-11 02:06:37 +0000 | [diff] [blame] | 41 | return std::shared_ptr<UniformOptions>( |
| 42 | new UniformOptions(binCount, minValue, exclusiveMaxValue)); |
Vova Sharaienko | 75f8600 | 2023-02-09 01:51:07 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | Histogram::UniformOptions::UniformOptions(int binCount, float minValue, float exclusiveMaxValue) |
| 46 | : // Implicitly add 2 for the extra undeflow & overflow bins |
| 47 | mBinCount(binCount + 2), |
| 48 | mMinValue(minValue), |
| 49 | mExclusiveMaxValue(exclusiveMaxValue), |
| 50 | mBinSize((exclusiveMaxValue - minValue) / binCount) { |
| 51 | } |
| 52 | |
| 53 | int Histogram::UniformOptions::getBinForSample(float sample) const { |
| 54 | if (sample < mMinValue) { |
| 55 | // goes to underflow |
| 56 | return 0; |
| 57 | } else if (sample >= mExclusiveMaxValue) { |
| 58 | // goes to overflow |
| 59 | return mBinCount - 1; |
| 60 | } |
| 61 | return (int)((sample - mMinValue) / mBinSize + 1); |
| 62 | } |
| 63 | |
| 64 | Histogram::Histogram(const char* metricName, std::shared_ptr<BinOptions> binOptions) |
| 65 | : mMetricIdHash(farmhash::Fingerprint64(metricName, strlen(metricName))), |
| 66 | mBinOptions(std::move(binOptions)) { |
| 67 | } |
| 68 | |
| 69 | void Histogram::logSample(float sample) const { |
| 70 | const int binIndex = mBinOptions->getBinForSample(sample); |
| 71 | stats_write(EXPRESS_HISTOGRAM_SAMPLE_REPORTED, mMetricIdHash, /*count*/ 1, binIndex); |
| 72 | } |
| 73 | |
Vova Sharaienko | c246440 | 2023-03-23 20:52:49 +0000 | [diff] [blame] | 74 | void Histogram::logSampleWithUid(int32_t uid, float sample) const { |
| 75 | const int binIndex = mBinOptions->getBinForSample(sample); |
| 76 | stats_write(EXPRESS_UID_HISTOGRAM_SAMPLE_REPORTED, mMetricIdHash, /*count*/ 1, binIndex, uid); |
| 77 | } |
| 78 | |
Vova Sharaienko | 75f8600 | 2023-02-09 01:51:07 +0000 | [diff] [blame] | 79 | } // namespace expresslog |
| 80 | } // namespace android |