blob: d95d59454dd88e700ff0c48b7ed3cf41c8ee9fe5 [file] [log] [blame]
Yao Chend5aa01b32017-12-19 16:46:36 -08001/*
2 * Copyright (C) 2017 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 */
Yao Chen8a8d16c2018-02-08 14:50:40 -080016#define DEBUG false // STOPSHIP if true
17#include "Log.h"
Yangster-mac20877162017-12-22 17:19:39 -080018
Yao Chend5aa01b32017-12-19 16:46:36 -080019#include "HashableDimensionKey.h"
Yao Chen8a8d16c2018-02-08 14:50:40 -080020#include "FieldValue.h"
Yao Chend5aa01b32017-12-19 16:46:36 -080021
22namespace android {
23namespace os {
24namespace statsd {
Yao Chen9c1debe2018-02-19 14:39:19 -080025
26using std::string;
Yao Chen8a8d16c2018-02-08 14:50:40 -080027using std::vector;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080028using android::base::StringPrintf;
Yao Chend5aa01b32017-12-19 16:46:36 -080029
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080030// These constants must be kept in sync with those in StatsDimensionsValue.java
31const static int STATS_DIMENSIONS_VALUE_STRING_TYPE = 2;
32const static int STATS_DIMENSIONS_VALUE_INT_TYPE = 3;
33const static int STATS_DIMENSIONS_VALUE_LONG_TYPE = 4;
34// const static int STATS_DIMENSIONS_VALUE_BOOL_TYPE = 5; (commented out because
35// unused -- statsd does not correctly support bool types)
36const static int STATS_DIMENSIONS_VALUE_FLOAT_TYPE = 6;
37const static int STATS_DIMENSIONS_VALUE_TUPLE_TYPE = 7;
38
39/**
40 * Recursive helper function that populates a parent StatsDimensionsValueParcel
41 * with children StatsDimensionsValueParcels.
42 *
43 * \param dims vector of FieldValues stored by HashableDimensionKey
44 * \param index positions in dims vector to start reading children from
45 * \param depth level of parent parcel in the full StatsDimensionsValueParcel
46 * tree
47 */
48static void populateStatsDimensionsValueParcelChildren(StatsDimensionsValueParcel &parentParcel,
49 const vector<FieldValue>& dims, size_t& index,
50 int depth, int prefix) {
51 while (index < dims.size()) {
52 const FieldValue& dim = dims[index];
53 int fieldDepth = dim.mField.getDepth();
54 int fieldPrefix = dim.mField.getPrefix(depth);
55 StatsDimensionsValueParcel childParcel;
56 childParcel.field = dim.mField.getPosAtDepth(depth);
57 if (depth > 2) {
58 ALOGE("Depth > 2 not supported by StatsDimensionsValueParcel.");
59 return;
60 }
61 if (depth == fieldDepth && prefix == fieldPrefix) {
62 switch (dim.mValue.getType()) {
63 case INT:
64 childParcel.valueType = STATS_DIMENSIONS_VALUE_INT_TYPE;
65 childParcel.intValue = dim.mValue.int_value;
66 break;
67 case LONG:
68 childParcel.valueType = STATS_DIMENSIONS_VALUE_LONG_TYPE;
69 childParcel.longValue = dim.mValue.long_value;
70 break;
71 case FLOAT:
72 childParcel.valueType = STATS_DIMENSIONS_VALUE_FLOAT_TYPE;
73 childParcel.floatValue = dim.mValue.float_value;
74 break;
75 case STRING:
76 childParcel.valueType = STATS_DIMENSIONS_VALUE_STRING_TYPE;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080077 childParcel.stringValue = dim.mValue.str_value;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080078 break;
79 default:
80 ALOGE("Encountered FieldValue with unsupported value type.");
81 break;
82 }
83 index++;
84 parentParcel.tupleValue.push_back(childParcel);
85 } else if (fieldDepth > depth && fieldPrefix == prefix) {
86 childParcel.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE;
87 populateStatsDimensionsValueParcelChildren(childParcel, dims, index, depth + 1,
88 dim.mField.getPrefix(depth + 1));
89 parentParcel.tupleValue.push_back(childParcel);
90 } else {
91 return;
92 }
93 }
94}
95
96StatsDimensionsValueParcel HashableDimensionKey::toStatsDimensionsValueParcel() const {
97 StatsDimensionsValueParcel parcel;
98 if (mValues.size() == 0) {
99 return parcel;
100 }
101
102 parcel.field = mValues[0].mField.getTag();
103 parcel.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE;
104
105 size_t index = 0;
106 populateStatsDimensionsValueParcelChildren(parcel, mValues, index, /*depth=*/0, /*prefix=*/0);
107 return parcel;
108}
109
Yao Chen8a8d16c2018-02-08 14:50:40 -0800110android::hash_t hashDimension(const HashableDimensionKey& value) {
111 android::hash_t hash = 0;
112 for (const auto& fieldValue : value.getValues()) {
113 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField()));
114 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag()));
115 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType()));
116 switch (fieldValue.mValue.getType()) {
117 case INT:
118 hash = android::JenkinsHashMix(hash,
119 android::hash_type(fieldValue.mValue.int_value));
120 break;
121 case LONG:
122 hash = android::JenkinsHashMix(hash,
123 android::hash_type(fieldValue.mValue.long_value));
124 break;
125 case STRING:
126 hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()(
127 fieldValue.mValue.str_value)));
128 break;
129 case FLOAT: {
Yangster-mac16b7ff72018-02-23 11:11:36 -0800130 hash = android::JenkinsHashMix(hash,
131 android::hash_type(fieldValue.mValue.float_value));
Yao Chen8a8d16c2018-02-08 14:50:40 -0800132 break;
Yangster-mac20877162017-12-22 17:19:39 -0800133 }
Yangster-macf5204922018-02-23 13:08:03 -0800134 default:
135 break;
Yangster-mac20877162017-12-22 17:19:39 -0800136 }
Yangster-mac20877162017-12-22 17:19:39 -0800137 }
138 return JenkinsHashWhiten(hash);
139}
140
tsaichristine69000e62019-10-18 17:34:52 -0700141bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values,
142 FieldValue* output) {
tsaichristine10978642019-09-10 14:12:49 -0700143 for (const auto& value : values) {
144 if (value.mField.matches(matcherField)) {
tsaichristine69000e62019-10-18 17:34:52 -0700145 (*output) = value;
tsaichristine10978642019-09-10 14:12:49 -0700146 return true;
147 }
148 }
149 return false;
150}
151
Yangster13fb7e42018-03-07 17:30:49 -0800152bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
153 HashableDimensionKey* output) {
Yangster-mace06cfd72018-03-10 23:22:59 -0800154 size_t num_matches = 0;
155 for (const auto& value : values) {
156 for (size_t i = 0; i < matcherFields.size(); ++i) {
157 const auto& matcher = matcherFields[i];
Yangster13fb7e42018-03-07 17:30:49 -0800158 if (value.mField.matches(matcher)) {
159 output->addValue(value);
Yangster-mace06cfd72018-03-10 23:22:59 -0800160 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
161 output->mutableValue(num_matches)->mField.setField(
162 value.mField.getField() & matcher.mMask);
Yangster-macf5204922018-02-23 13:08:03 -0800163 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -0800164 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800165 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800166 }
Yangster-mace06cfd72018-03-10 23:22:59 -0800167 return num_matches > 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800168}
169
170void filterGaugeValues(const std::vector<Matcher>& matcherFields,
171 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
172 for (const auto& field : matcherFields) {
173 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800174 if (value.mField.matches(field)) {
175 output->push_back(value);
176 }
177 }
178 }
179}
180
Yangster-mac53928882018-02-25 23:02:56 -0800181void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
182 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800183 HashableDimensionKey* conditionDimension) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800184 // Get the dimension first by using dimension from what.
Yangster-mac53928882018-02-25 23:02:56 -0800185 filterValues(links.metricFields, eventValues, conditionDimension);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800186
Yangster13fb7e42018-03-07 17:30:49 -0800187 size_t count = conditionDimension->getValues().size();
188 if (count != links.conditionFields.size()) {
Yangster13fb7e42018-03-07 17:30:49 -0800189 return;
190 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800191
Yangster13fb7e42018-03-07 17:30:49 -0800192 for (size_t i = 0; i < count; i++) {
193 conditionDimension->mutableValue(i)->mField.setField(
tsaichristine69000e62019-10-18 17:34:52 -0700194 links.conditionFields[i].mMatcher.getField());
Yangster13fb7e42018-03-07 17:30:49 -0800195 conditionDimension->mutableValue(i)->mField.setTag(
tsaichristine69000e62019-10-18 17:34:52 -0700196 links.conditionFields[i].mMatcher.getTag());
197 }
198}
199
200void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
201 HashableDimensionKey* statePrimaryKey) {
202 // First, get the dimension from the event using the "what" fields from the
203 // MetricStateLinks.
204 filterValues(link.metricFields, eventValues, statePrimaryKey);
205
206 // Then check that the statePrimaryKey size equals the number of state fields
207 size_t count = statePrimaryKey->getValues().size();
208 if (count != link.stateFields.size()) {
209 return;
210 }
211
212 // For each dimension Value in the statePrimaryKey, set the field and tag
213 // using the state atom fields from MetricStateLinks.
214 for (size_t i = 0; i < count; i++) {
215 statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField());
216 statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800217 }
218}
219
220bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
221 if (s1.size() != s2.size()) {
222 return s1.size() < s2.size();
223 }
224
225 size_t count = s1.size();
226 for (size_t i = 0; i < count; i++) {
227 if (s1[i] != s2[i]) {
228 return s1[i] < s2[i];
229 }
230 }
231 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800232}
233
tsaichristinec876b492019-12-10 13:47:05 -0800234bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const {
235 return !((*this) == that);
236}
237
Yangster-mac20877162017-12-22 17:19:39 -0800238bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800239 if (mValues.size() != that.getValues().size()) {
240 return false;
241 }
242 size_t count = mValues.size();
243 for (size_t i = 0; i < count; i++) {
244 if (mValues[i] != (that.getValues())[i]) {
245 return false;
246 }
247 }
248 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800249};
250
251bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800252 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800253};
254
Yao Chen8a8d16c2018-02-08 14:50:40 -0800255bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
256 if (mValues.size() < that.getValues().size()) {
257 return false;
258 }
259
260 if (mValues.size() == that.getValues().size()) {
261 return (*this) == that;
262 }
263
264 for (const auto& value : that.getValues()) {
265 bool found = false;
266 for (const auto& myValue : mValues) {
267 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
268 found = true;
269 break;
270 }
271 }
272 if (!found) {
273 return false;
274 }
275 }
276
277 return true;
278}
279
280string HashableDimensionKey::toString() const {
281 std::string output;
282 for (const auto& value : mValues) {
283 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
284 value.mValue.toString().c_str());
285 }
286 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800287}
288
289bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
290 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
tsaichristine69000e62019-10-18 17:34:52 -0700291 mStateValuesKey == that.getStateValuesKey();
Yangster-mac93694462018-01-22 20:49:31 -0800292};
293
Yao Chen8a8d16c2018-02-08 14:50:40 -0800294string MetricDimensionKey::toString() const {
tsaichristine69000e62019-10-18 17:34:52 -0700295 return mDimensionKeyInWhat.toString() + mStateValuesKey.toString();
Yao Chen8a8d16c2018-02-08 14:50:40 -0800296}
297
Yangster-mac93694462018-01-22 20:49:31 -0800298bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800299 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
300 return true;
301 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
302 return false;
303 }
Yangster-mac93694462018-01-22 20:49:31 -0800304
tsaichristine69000e62019-10-18 17:34:52 -0700305 return mStateValuesKey < that.getStateValuesKey();
Chenjie Yu80f91122018-01-31 20:24:50 -0800306}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800307
Yao Chend5aa01b32017-12-19 16:46:36 -0800308} // namespace statsd
309} // namespace os
Yao Chen5bfffb52018-06-21 16:58:51 -0700310} // namespace android