blob: c90282d572d420fe31a2dd677c5bd27350c08cbd [file] [log] [blame]
Vova Sharaienko75f86002023-02-09 01:51:07 +00001//
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
26namespace android {
27namespace expresslog {
28
29Histogram::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
44Histogram::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
52int 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
63Histogram::Histogram(const char* metricName, std::shared_ptr<BinOptions> binOptions)
64 : mMetricIdHash(farmhash::Fingerprint64(metricName, strlen(metricName))),
65 mBinOptions(std::move(binOptions)) {
66}
67
68void 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