1 /*
2  * Copyright (C) 2024 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 #define STATSD_DEBUG false  // STOPSHIP if true
18 #include "Log.h"
19 
20 #include "HistogramValue.h"
21 
22 #include <android/util/ProtoOutputStream.h>
23 
24 #include <algorithm>
25 #include <functional>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 
30 using android::util::FIELD_COUNT_REPEATED;
31 using android::util::FIELD_TYPE_SINT32;
32 using android::util::ProtoOutputStream;
33 using std::string;
34 using std::vector;
35 
36 namespace android {
37 namespace os {
38 namespace statsd {
39 namespace {
40 constexpr int FIELD_ID_COUNT = 1;
41 }  // anonymous namespace
42 
43 const HistogramValue HistogramValue::ERROR_BINS_MISMATCH = HistogramValue({-1});
44 const HistogramValue HistogramValue::ERROR_BIN_COUNT_TOO_HIGH = HistogramValue({-2});
45 
toString() const46 string HistogramValue::toString() const {
47     std::stringstream result("{");
48     std::copy(mBinCounts.begin(), mBinCounts.end(), std::ostream_iterator<int>(result, ", "));
49     return result.str() + "}";
50 }
51 
isEmpty() const52 bool HistogramValue::isEmpty() const {
53     return mBinCounts.empty() ||
54            std::all_of(mBinCounts.begin(), mBinCounts.end(), [](int count) { return count <= 0; });
55 }
56 
getSize() const57 size_t HistogramValue::getSize() const {
58     return sizeof(int) * mBinCounts.size();
59 }
60 
toProto(ProtoOutputStream & protoOutput) const61 void HistogramValue::toProto(ProtoOutputStream& protoOutput) const {
62     for (int binCount : mBinCounts) {
63         protoOutput.write(FIELD_TYPE_SINT32 | FIELD_COUNT_REPEATED | FIELD_ID_COUNT, binCount);
64     }
65 }
66 
addValue(float value,const BinStarts & binStarts)67 void HistogramValue::addValue(float value, const BinStarts& binStarts) {
68     if (mBinCounts.empty()) {
69         mBinCounts.resize(binStarts.size(), 0);
70     }
71     size_t index = 0;
72     for (; index < binStarts.size() - 1; index++) {
73         if (value < binStarts[index + 1]) {
74             break;
75         }
76     }
77     mBinCounts[index]++;
78 }
79 
getCompactedHistogramValue() const80 HistogramValue HistogramValue::getCompactedHistogramValue() const {
81     size_t compactSize = getCompactedBinCountsSize(mBinCounts);
82     HistogramValue result;
83     result.mBinCounts.reserve(compactSize);
84     int zeroCount = 0;
85     for (int binCount : mBinCounts) {
86         if (binCount <= 0) {
87             zeroCount++;
88         } else {
89             if (zeroCount > 1) {
90                 result.mBinCounts.push_back(-zeroCount);
91             } else if (zeroCount == 1) {
92                 result.mBinCounts.push_back(0);
93             }
94             result.mBinCounts.push_back(binCount);
95             zeroCount = 0;
96         }
97     }
98     if (zeroCount > 1) {
99         result.mBinCounts.push_back(-zeroCount);
100     } else if (zeroCount == 1) {
101         result.mBinCounts.push_back(0);
102     }
103 
104     result.mCompacted = true;
105     return result;
106 }
107 
isValid() const108 bool HistogramValue::isValid() const {
109     return mCompacted ||
110            std::all_of(mBinCounts.begin(), mBinCounts.end(), [](int count) { return count >= 0; });
111 }
112 
operator +=(const HistogramValue & rhs)113 HistogramValue& HistogramValue::operator+=(const HistogramValue& rhs) {
114     if (mBinCounts.size() < rhs.mBinCounts.size()) {
115         ALOGE("HistogramValue::operator+=() arg has too many bins");
116         *this = ERROR_BINS_MISMATCH;
117         return *this;
118     }
119     for (size_t i = 0; i < rhs.mBinCounts.size(); i++) {
120         mBinCounts[i] += rhs.mBinCounts[i];
121     }
122     return *this;
123 }
124 
operator +(HistogramValue lhs,const HistogramValue & rhs)125 HistogramValue operator+(HistogramValue lhs, const HistogramValue& rhs) {
126     lhs += rhs;
127     return lhs;
128 }
129 
operator -=(const HistogramValue & rhs)130 HistogramValue& HistogramValue::operator-=(const HistogramValue& rhs) {
131     if (mBinCounts.size() < rhs.mBinCounts.size()) {
132         ALOGE("HistogramValue::operator-=() arg has too many bins");
133         *this = ERROR_BINS_MISMATCH;
134         return *this;
135     }
136     for (size_t i = 0; i < rhs.mBinCounts.size(); i++) {
137         if (mBinCounts[i] < rhs.mBinCounts[i]) {
138             ALOGE("HistogramValue::operator-=() arg has a bin count that is too high");
139             *this = ERROR_BIN_COUNT_TOO_HIGH;
140             return *this;
141         }
142         mBinCounts[i] -= rhs.mBinCounts[i];
143     }
144     return *this;
145 }
146 
operator -(HistogramValue lhs,const HistogramValue & rhs)147 HistogramValue operator-(HistogramValue lhs, const HistogramValue& rhs) {
148     lhs -= rhs;
149     return lhs;
150 }
151 
operator ==(const HistogramValue & lhs,const HistogramValue & rhs)152 bool operator==(const HistogramValue& lhs, const HistogramValue& rhs) {
153     return lhs.mBinCounts == rhs.mBinCounts;
154 }
155 
operator !=(const HistogramValue & lhs,const HistogramValue & rhs)156 bool operator!=(const HistogramValue& lhs, const HistogramValue& rhs) {
157     return lhs.mBinCounts != rhs.mBinCounts;
158 }
159 
operator <(const HistogramValue & lhs,const HistogramValue & rhs)160 bool operator<(const HistogramValue& lhs, const HistogramValue& rhs) {
161     ALOGE("HistogramValue::operator<() should not be called");
162     return false;
163 }
164 
operator >(const HistogramValue & lhs,const HistogramValue & rhs)165 bool operator>(const HistogramValue& lhs, const HistogramValue& rhs) {
166     ALOGE("HistogramValue::operator>() should not be called");
167     return false;
168 }
169 
operator <=(const HistogramValue & lhs,const HistogramValue & rhs)170 bool operator<=(const HistogramValue& lhs, const HistogramValue& rhs) {
171     ALOGE("HistogramValue::operator<=() should not be called");
172     return false;
173 }
174 
operator >=(const HistogramValue & lhs,const HistogramValue & rhs)175 bool operator>=(const HistogramValue& lhs, const HistogramValue& rhs) {
176     ALOGE("HistogramValue::operator>=() should not be called");
177     return false;
178 }
179 
getCompactedBinCountsSize(const std::vector<int> & binCounts)180 size_t getCompactedBinCountsSize(const std::vector<int>& binCounts) {
181     if (binCounts.empty()) {
182         return 0;
183     }
184     size_t compactSize = 1;
185     for (size_t i = 1; i < binCounts.size(); i++) {
186         // If current index i and the previous index i-1 hold 0, ie. this is a consecutive bin with
187         // 0, then this bin will be compressed after compaction and not be counted towards the
188         // compacted size. Hence, only increment compactSize if at least one of current index or
189         // the previous index have non-zero bin counts.
190         if (binCounts[i] != 0 || binCounts[i - 1] != 0) {
191             compactSize++;
192         }
193     }
194     return compactSize;
195 }
196 
197 }  // namespace statsd
198 }  // namespace os
199 }  // namespace android
200