blob: 50bb343af8f8cb51a14076e4ae93defaf964589b [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
Vova Sharaienko8ba8b352023-02-11 02:06:37 +000029std::shared_ptr<Histogram::UniformOptions> Histogram::UniformOptions::create(
30 int binCount, float minValue, float exclusiveMaxValue) {
Vova Sharaienko75f86002023-02-09 01:51:07 +000031 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 Sharaienko8ba8b352023-02-11 02:06:37 +000041 return std::shared_ptr<UniformOptions>(
42 new UniformOptions(binCount, minValue, exclusiveMaxValue));
Vova Sharaienko75f86002023-02-09 01:51:07 +000043}
44
45Histogram::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
53int 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
64Histogram::Histogram(const char* metricName, std::shared_ptr<BinOptions> binOptions)
65 : mMetricIdHash(farmhash::Fingerprint64(metricName, strlen(metricName))),
66 mBinOptions(std::move(binOptions)) {
67}
68
69void 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 Sharaienkoc2464402023-03-23 20:52:49 +000074void 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 Sharaienko75f86002023-02-09 01:51:07 +000079} // namespace expresslog
80} // namespace android