blob: 29249f4a6c5527befbe873b88245d34e69a9d5cd [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 *
Ruchir Rastogi3db62042020-03-04 14:07:08 -080043 * \param parent parcel that will be populated with children
44 * \param childDepth depth of children FieldValues
45 * \param childPrefix expected FieldValue prefix of children
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080046 * \param dims vector of FieldValues stored by HashableDimensionKey
Ruchir Rastogi3db62042020-03-04 14:07:08 -080047 * \param index position in dims to start reading children from
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080048 */
Ruchir Rastogi3db62042020-03-04 14:07:08 -080049static void populateStatsDimensionsValueParcelChildren(StatsDimensionsValueParcel& parent,
50 int childDepth, int childPrefix,
51 const vector<FieldValue>& dims,
52 size_t& index) {
53 if (childDepth > 2) {
54 ALOGE("Depth > 2 not supported by StatsDimensionsValueParcel.");
55 return;
56 }
57
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080058 while (index < dims.size()) {
59 const FieldValue& dim = dims[index];
60 int fieldDepth = dim.mField.getDepth();
Ruchir Rastogi3db62042020-03-04 14:07:08 -080061 int fieldPrefix = dim.mField.getPrefix(childDepth);
62
63 StatsDimensionsValueParcel child;
64 child.field = dim.mField.getPosAtDepth(childDepth);
65
66 if (fieldDepth == childDepth && fieldPrefix == childPrefix) {
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080067 switch (dim.mValue.getType()) {
68 case INT:
Ruchir Rastogi3db62042020-03-04 14:07:08 -080069 child.valueType = STATS_DIMENSIONS_VALUE_INT_TYPE;
70 child.intValue = dim.mValue.int_value;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080071 break;
72 case LONG:
Ruchir Rastogi3db62042020-03-04 14:07:08 -080073 child.valueType = STATS_DIMENSIONS_VALUE_LONG_TYPE;
74 child.longValue = dim.mValue.long_value;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080075 break;
76 case FLOAT:
Ruchir Rastogi3db62042020-03-04 14:07:08 -080077 child.valueType = STATS_DIMENSIONS_VALUE_FLOAT_TYPE;
78 child.floatValue = dim.mValue.float_value;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080079 break;
80 case STRING:
Ruchir Rastogi3db62042020-03-04 14:07:08 -080081 child.valueType = STATS_DIMENSIONS_VALUE_STRING_TYPE;
82 child.stringValue = dim.mValue.str_value;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080083 break;
84 default:
85 ALOGE("Encountered FieldValue with unsupported value type.");
86 break;
87 }
88 index++;
Ruchir Rastogi3db62042020-03-04 14:07:08 -080089 parent.tupleValue.push_back(child);
90 } else if (fieldDepth > childDepth && fieldPrefix == childPrefix) {
91 // This FieldValue is not a child of the current parent, but it is
92 // an indirect descendant. Thus, create a direct child of TUPLE_TYPE
93 // and recurse to parcel the indirect descendants.
94 child.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE;
95 populateStatsDimensionsValueParcelChildren(child, childDepth + 1,
96 dim.mField.getPrefix(childDepth + 1), dims,
97 index);
98 parent.tupleValue.push_back(child);
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080099 } else {
100 return;
101 }
102 }
103}
104
105StatsDimensionsValueParcel HashableDimensionKey::toStatsDimensionsValueParcel() const {
Ruchir Rastogi3db62042020-03-04 14:07:08 -0800106 StatsDimensionsValueParcel root;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -0800107 if (mValues.size() == 0) {
Ruchir Rastogi3db62042020-03-04 14:07:08 -0800108 return root;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -0800109 }
110
Ruchir Rastogi3db62042020-03-04 14:07:08 -0800111 root.field = mValues[0].mField.getTag();
112 root.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -0800113
Ruchir Rastogi3db62042020-03-04 14:07:08 -0800114 // Children of the root correspond to top-level (depth = 0) FieldValues.
115 int childDepth = 0;
116 int childPrefix = 0;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -0800117 size_t index = 0;
Ruchir Rastogi3db62042020-03-04 14:07:08 -0800118 populateStatsDimensionsValueParcelChildren(root, childDepth, childPrefix, mValues, index);
119
120 return root;
Ruchir Rastogi56da4c32020-02-03 18:53:24 -0800121}
122
Yao Chen8a8d16c2018-02-08 14:50:40 -0800123android::hash_t hashDimension(const HashableDimensionKey& value) {
124 android::hash_t hash = 0;
125 for (const auto& fieldValue : value.getValues()) {
126 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField()));
127 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag()));
128 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType()));
129 switch (fieldValue.mValue.getType()) {
130 case INT:
131 hash = android::JenkinsHashMix(hash,
132 android::hash_type(fieldValue.mValue.int_value));
133 break;
134 case LONG:
135 hash = android::JenkinsHashMix(hash,
136 android::hash_type(fieldValue.mValue.long_value));
137 break;
138 case STRING:
139 hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()(
140 fieldValue.mValue.str_value)));
141 break;
142 case FLOAT: {
Yangster-mac16b7ff72018-02-23 11:11:36 -0800143 hash = android::JenkinsHashMix(hash,
144 android::hash_type(fieldValue.mValue.float_value));
Yao Chen8a8d16c2018-02-08 14:50:40 -0800145 break;
Yangster-mac20877162017-12-22 17:19:39 -0800146 }
Yangster-macf5204922018-02-23 13:08:03 -0800147 default:
148 break;
Yangster-mac20877162017-12-22 17:19:39 -0800149 }
Yangster-mac20877162017-12-22 17:19:39 -0800150 }
151 return JenkinsHashWhiten(hash);
152}
153
tsaichristine69000e62019-10-18 17:34:52 -0700154bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values,
155 FieldValue* output) {
tsaichristine10978642019-09-10 14:12:49 -0700156 for (const auto& value : values) {
157 if (value.mField.matches(matcherField)) {
tsaichristine69000e62019-10-18 17:34:52 -0700158 (*output) = value;
tsaichristine10978642019-09-10 14:12:49 -0700159 return true;
160 }
161 }
162 return false;
163}
164
Yangster13fb7e42018-03-07 17:30:49 -0800165bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
166 HashableDimensionKey* output) {
Yangster-mace06cfd72018-03-10 23:22:59 -0800167 size_t num_matches = 0;
168 for (const auto& value : values) {
169 for (size_t i = 0; i < matcherFields.size(); ++i) {
170 const auto& matcher = matcherFields[i];
Yangster13fb7e42018-03-07 17:30:49 -0800171 if (value.mField.matches(matcher)) {
172 output->addValue(value);
Yangster-mace06cfd72018-03-10 23:22:59 -0800173 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
174 output->mutableValue(num_matches)->mField.setField(
175 value.mField.getField() & matcher.mMask);
Yangster-macf5204922018-02-23 13:08:03 -0800176 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -0800177 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800178 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800179 }
Yangster-mace06cfd72018-03-10 23:22:59 -0800180 return num_matches > 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800181}
182
183void filterGaugeValues(const std::vector<Matcher>& matcherFields,
184 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
185 for (const auto& field : matcherFields) {
186 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800187 if (value.mField.matches(field)) {
188 output->push_back(value);
189 }
190 }
191 }
192}
193
Yangster-mac53928882018-02-25 23:02:56 -0800194void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
195 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800196 HashableDimensionKey* conditionDimension) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800197 // Get the dimension first by using dimension from what.
Yangster-mac53928882018-02-25 23:02:56 -0800198 filterValues(links.metricFields, eventValues, conditionDimension);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800199
Yangster13fb7e42018-03-07 17:30:49 -0800200 size_t count = conditionDimension->getValues().size();
201 if (count != links.conditionFields.size()) {
Yangster13fb7e42018-03-07 17:30:49 -0800202 return;
203 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800204
Yangster13fb7e42018-03-07 17:30:49 -0800205 for (size_t i = 0; i < count; i++) {
206 conditionDimension->mutableValue(i)->mField.setField(
tsaichristine69000e62019-10-18 17:34:52 -0700207 links.conditionFields[i].mMatcher.getField());
Yangster13fb7e42018-03-07 17:30:49 -0800208 conditionDimension->mutableValue(i)->mField.setTag(
tsaichristine69000e62019-10-18 17:34:52 -0700209 links.conditionFields[i].mMatcher.getTag());
210 }
211}
212
213void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
214 HashableDimensionKey* statePrimaryKey) {
215 // First, get the dimension from the event using the "what" fields from the
216 // MetricStateLinks.
217 filterValues(link.metricFields, eventValues, statePrimaryKey);
218
219 // Then check that the statePrimaryKey size equals the number of state fields
220 size_t count = statePrimaryKey->getValues().size();
221 if (count != link.stateFields.size()) {
222 return;
223 }
224
225 // For each dimension Value in the statePrimaryKey, set the field and tag
226 // using the state atom fields from MetricStateLinks.
227 for (size_t i = 0; i < count; i++) {
228 statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField());
229 statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800230 }
231}
232
tsaichristine1449fa42020-01-02 12:12:05 -0800233bool containsLinkedStateValues(const HashableDimensionKey& whatKey,
234 const HashableDimensionKey& primaryKey,
235 const vector<Metric2State>& stateLinks, const int32_t stateAtomId) {
236 if (whatKey.getValues().size() < primaryKey.getValues().size()) {
237 ALOGE("Contains linked values false: whatKey is too small");
238 return false;
239 }
240
241 for (const auto& primaryValue : primaryKey.getValues()) {
242 bool found = false;
243 for (const auto& whatValue : whatKey.getValues()) {
244 if (linked(stateLinks, stateAtomId, primaryValue.mField, whatValue.mField) &&
245 primaryValue.mValue == whatValue.mValue) {
246 found = true;
247 break;
248 }
249 }
250 if (!found) {
251 return false;
252 }
253 }
254 return true;
255}
256
257bool linked(const vector<Metric2State>& stateLinks, const int32_t stateAtomId,
258 const Field& stateField, const Field& metricField) {
259 for (auto stateLink : stateLinks) {
260 if (stateLink.stateAtomId != stateAtomId) {
261 continue;
262 }
263
264 for (size_t i = 0; i < stateLink.stateFields.size(); i++) {
265 if (stateLink.stateFields[i].mMatcher == stateField &&
266 stateLink.metricFields[i].mMatcher == metricField) {
267 return true;
268 }
269 }
270 }
271 return false;
272}
273
Yao Chen8a8d16c2018-02-08 14:50:40 -0800274bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
275 if (s1.size() != s2.size()) {
276 return s1.size() < s2.size();
277 }
278
279 size_t count = s1.size();
280 for (size_t i = 0; i < count; i++) {
281 if (s1[i] != s2[i]) {
282 return s1[i] < s2[i];
283 }
284 }
285 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800286}
287
tsaichristinec876b492019-12-10 13:47:05 -0800288bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const {
289 return !((*this) == that);
290}
291
Yangster-mac20877162017-12-22 17:19:39 -0800292bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800293 if (mValues.size() != that.getValues().size()) {
294 return false;
295 }
296 size_t count = mValues.size();
297 for (size_t i = 0; i < count; i++) {
298 if (mValues[i] != (that.getValues())[i]) {
299 return false;
300 }
301 }
302 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800303};
304
305bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800306 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800307};
308
Yao Chen8a8d16c2018-02-08 14:50:40 -0800309bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
310 if (mValues.size() < that.getValues().size()) {
311 return false;
312 }
313
314 if (mValues.size() == that.getValues().size()) {
315 return (*this) == that;
316 }
317
318 for (const auto& value : that.getValues()) {
319 bool found = false;
320 for (const auto& myValue : mValues) {
321 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
322 found = true;
323 break;
324 }
325 }
326 if (!found) {
327 return false;
328 }
329 }
330
331 return true;
332}
333
334string HashableDimensionKey::toString() const {
335 std::string output;
336 for (const auto& value : mValues) {
337 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
338 value.mValue.toString().c_str());
339 }
340 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800341}
342
343bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
344 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
tsaichristine69000e62019-10-18 17:34:52 -0700345 mStateValuesKey == that.getStateValuesKey();
Yangster-mac93694462018-01-22 20:49:31 -0800346};
347
Yao Chen8a8d16c2018-02-08 14:50:40 -0800348string MetricDimensionKey::toString() const {
tsaichristine69000e62019-10-18 17:34:52 -0700349 return mDimensionKeyInWhat.toString() + mStateValuesKey.toString();
Yao Chen8a8d16c2018-02-08 14:50:40 -0800350}
351
Yangster-mac93694462018-01-22 20:49:31 -0800352bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800353 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
354 return true;
355 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
356 return false;
357 }
Yangster-mac93694462018-01-22 20:49:31 -0800358
tsaichristine69000e62019-10-18 17:34:52 -0700359 return mStateValuesKey < that.getStateValuesKey();
Chenjie Yu80f91122018-01-31 20:24:50 -0800360}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800361
Yao Chend5aa01b32017-12-19 16:46:36 -0800362} // namespace statsd
363} // namespace os
Yao Chen5bfffb52018-06-21 16:58:51 -0700364} // namespace android