1 /*
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 */
16 #define STATSD_DEBUG false // STOPSHIP if true
17 #include "Log.h"
18
19 #include "HashableDimensionKey.h"
20 #include "FieldValue.h"
21
22 namespace android {
23 namespace os {
24 namespace statsd {
25
26 using std::string;
27 using std::vector;
28 using android::base::StringPrintf;
29
30 /**
31 * Recursive helper function that populates a parent StatsDimensionsValueParcel
32 * with children StatsDimensionsValueParcels.
33 *
34 * \param parent parcel that will be populated with children
35 * \param childDepth depth of children FieldValues
36 * \param childPrefix expected FieldValue prefix of children
37 * \param dims vector of FieldValues stored by HashableDimensionKey
38 * \param index position in dims to start reading children from
39 */
populateStatsDimensionsValueParcelChildren(StatsDimensionsValueParcel & parent,int childDepth,int childPrefix,const vector<FieldValue> & dims,size_t & index)40 static void populateStatsDimensionsValueParcelChildren(StatsDimensionsValueParcel& parent,
41 int childDepth, int childPrefix,
42 const vector<FieldValue>& dims,
43 size_t& index) {
44 if (childDepth > 2) {
45 ALOGE("Depth > 2 not supported by StatsDimensionsValueParcel.");
46 return;
47 }
48
49 while (index < dims.size()) {
50 const FieldValue& dim = dims[index];
51 int fieldDepth = dim.mField.getDepth();
52 int fieldPrefix = dim.mField.getPrefix(childDepth);
53
54 StatsDimensionsValueParcel child;
55 child.field = dim.mField.getPosAtDepth(childDepth);
56
57 if (fieldDepth == childDepth && fieldPrefix == childPrefix) {
58 switch (dim.mValue.getType()) {
59 case INT:
60 child.valueType = STATS_DIMENSIONS_VALUE_INT_TYPE;
61 child.intValue = dim.mValue.int_value;
62 break;
63 case LONG:
64 child.valueType = STATS_DIMENSIONS_VALUE_LONG_TYPE;
65 child.longValue = dim.mValue.long_value;
66 break;
67 case FLOAT:
68 child.valueType = STATS_DIMENSIONS_VALUE_FLOAT_TYPE;
69 child.floatValue = dim.mValue.float_value;
70 break;
71 case STRING:
72 child.valueType = STATS_DIMENSIONS_VALUE_STRING_TYPE;
73 child.stringValue = dim.mValue.str_value;
74 break;
75 default:
76 ALOGE("Encountered FieldValue with unsupported value type.");
77 break;
78 }
79 index++;
80 parent.tupleValue.push_back(child);
81 } else if (fieldDepth > childDepth && fieldPrefix == childPrefix) {
82 // This FieldValue is not a child of the current parent, but it is
83 // an indirect descendant. Thus, create a direct child of TUPLE_TYPE
84 // and recurse to parcel the indirect descendants.
85 child.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE;
86 populateStatsDimensionsValueParcelChildren(child, childDepth + 1,
87 dim.mField.getPrefix(childDepth + 1), dims,
88 index);
89 parent.tupleValue.push_back(child);
90 } else {
91 return;
92 }
93 }
94 }
95
toStatsDimensionsValueParcel() const96 StatsDimensionsValueParcel HashableDimensionKey::toStatsDimensionsValueParcel() const {
97 StatsDimensionsValueParcel root;
98 if (mValues.size() == 0) {
99 return root;
100 }
101
102 root.field = mValues[0].mField.getTag();
103 root.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE;
104
105 // Children of the root correspond to top-level (depth = 0) FieldValues.
106 int childDepth = 0;
107 int childPrefix = 0;
108 size_t index = 0;
109 populateStatsDimensionsValueParcelChildren(root, childDepth, childPrefix, mValues, index);
110
111 return root;
112 }
113
hashDimension(const HashableDimensionKey & value)114 android::hash_t hashDimension(const HashableDimensionKey& value) {
115 android::hash_t hash = 0;
116 for (const auto& fieldValue : value.getValues()) {
117 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField()));
118 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag()));
119 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType()));
120 switch (fieldValue.mValue.getType()) {
121 case INT:
122 hash = android::JenkinsHashMix(hash,
123 android::hash_type(fieldValue.mValue.int_value));
124 break;
125 case LONG:
126 hash = android::JenkinsHashMix(hash,
127 android::hash_type(fieldValue.mValue.long_value));
128 break;
129 case STRING:
130 hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()(
131 fieldValue.mValue.str_value)));
132 break;
133 case FLOAT: {
134 hash = android::JenkinsHashMix(hash,
135 android::hash_type(fieldValue.mValue.float_value));
136 break;
137 }
138 case DOUBLE: {
139 hash = android::JenkinsHashMix(hash,
140 android::hash_type(fieldValue.mValue.double_value));
141 break;
142 }
143 case STORAGE: {
144 hash = android::JenkinsHashMixBytes(hash, fieldValue.mValue.storage_value.data(),
145 fieldValue.mValue.storage_value.size());
146 break;
147 }
148 default:
149 break;
150 }
151 }
152 return JenkinsHashWhiten(hash);
153 }
154
filterValues(const Matcher & matcherField,const vector<FieldValue> & values,FieldValue * output)155 bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values,
156 FieldValue* output) {
157 if (matcherField.hasAllPositionMatcher()) {
158 return false;
159 }
160 for (const auto& value : values) {
161 if (value.mField.matches(matcherField)) {
162 (*output) = value;
163 return true;
164 }
165 }
166 return false;
167 }
168
filterValues(const vector<Matcher> & matcherFields,const vector<FieldValue> & values,HashableDimensionKey * output)169 bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
170 HashableDimensionKey* output) {
171 size_t num_matches = 0;
172 for (const auto& value : values) {
173 for (size_t i = 0; i < matcherFields.size(); ++i) {
174 const auto& matcher = matcherFields[i];
175 if (value.mField.matches(matcher)) {
176 output->addValue(value);
177 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
178 output->mutableValue(num_matches)->mField.setField(
179 value.mField.getField() & matcher.mMask);
180 num_matches++;
181 }
182 }
183 }
184 return num_matches > 0;
185 }
186
filterValues(const vector<Matcher> & dimKeyMatcherFields,const vector<Matcher> & valueMatcherFields,const vector<FieldValue> & values,HashableDimensionKey & key,vector<int> & valueIndices)187 bool filterValues(const vector<Matcher>& dimKeyMatcherFields,
188 const vector<Matcher>& valueMatcherFields, const vector<FieldValue>& values,
189 HashableDimensionKey& key, vector<int>& valueIndices) {
190 size_t key_num_matches = 0;
191 size_t value_num_matches = 0;
192 for (size_t i = 0; i < values.size(); ++i) {
193 const FieldValue& value = values[i];
194 for (const auto& matcher : dimKeyMatcherFields) {
195 if (value.mField.matches(matcher)) {
196 key.addValue(value);
197 key.mutableValue(key_num_matches)->mField.setTag(value.mField.getTag());
198 key.mutableValue(key_num_matches)
199 ->mField.setField(value.mField.getField() & matcher.mMask);
200 key_num_matches++;
201 }
202 }
203 for (size_t j = 0; j < valueMatcherFields.size(); ++j) {
204 if (valueIndices[j] == -1 && value.mField.matches(valueMatcherFields[j])) {
205 valueIndices[j] = i;
206 value_num_matches++;
207 }
208 }
209 }
210 return value_num_matches == valueMatcherFields.size();
211 }
212
filterPrimaryKey(const std::vector<FieldValue> & values,HashableDimensionKey * output)213 bool filterPrimaryKey(const std::vector<FieldValue>& values, HashableDimensionKey* output) {
214 size_t num_matches = 0;
215 const int32_t simpleFieldMask = 0xff7f0000;
216 const int32_t attributionUidFieldMask = 0xff7f7f7f;
217 for (const auto& value : values) {
218 if (value.mAnnotations.isPrimaryField()) {
219 output->addValue(value);
220 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
221 const int32_t mask =
222 isAttributionUidField(value) ? attributionUidFieldMask : simpleFieldMask;
223 output->mutableValue(num_matches)->mField.setField(value.mField.getField() & mask);
224 num_matches++;
225 }
226 }
227 return num_matches > 0;
228 }
229
filterGaugeValues(const std::vector<Matcher> & matcherFields,const std::vector<FieldValue> & values,std::vector<FieldValue> * output)230 void filterGaugeValues(const std::vector<Matcher>& matcherFields,
231 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
232 for (const auto& field : matcherFields) {
233 for (const auto& value : values) {
234 if (value.mField.matches(field)) {
235 output->push_back(value);
236 }
237 }
238 }
239 }
240
getDimensionForCondition(const std::vector<FieldValue> & eventValues,const Metric2Condition & links,HashableDimensionKey * conditionDimension)241 void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
242 const Metric2Condition& links,
243 HashableDimensionKey* conditionDimension) {
244 // Get the dimension first by using dimension from what.
245 filterValues(links.metricFields, eventValues, conditionDimension);
246
247 size_t count = conditionDimension->getValues().size();
248 if (count != links.conditionFields.size()) {
249 return;
250 }
251
252 for (size_t i = 0; i < count; i++) {
253 conditionDimension->mutableValue(i)->mField.setField(
254 links.conditionFields[i].mMatcher.getField());
255 conditionDimension->mutableValue(i)->mField.setTag(
256 links.conditionFields[i].mMatcher.getTag());
257 }
258 }
259
getDimensionForState(const std::vector<FieldValue> & eventValues,const Metric2State & link,HashableDimensionKey * statePrimaryKey)260 void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
261 HashableDimensionKey* statePrimaryKey) {
262 // First, get the dimension from the event using the "what" fields from the
263 // MetricStateLinks.
264 filterValues(link.metricFields, eventValues, statePrimaryKey);
265
266 // Then check that the statePrimaryKey size equals the number of state fields
267 size_t count = statePrimaryKey->getValues().size();
268 if (count != link.stateFields.size()) {
269 return;
270 }
271
272 // For each dimension Value in the statePrimaryKey, set the field and tag
273 // using the state atom fields from MetricStateLinks.
274 for (size_t i = 0; i < count; i++) {
275 statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField());
276 statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag());
277 }
278 }
279
containsLinkedStateValues(const HashableDimensionKey & whatKey,const HashableDimensionKey & primaryKey,const vector<Metric2State> & stateLinks,const int32_t stateAtomId)280 bool containsLinkedStateValues(const HashableDimensionKey& whatKey,
281 const HashableDimensionKey& primaryKey,
282 const vector<Metric2State>& stateLinks, const int32_t stateAtomId) {
283 if (whatKey.getValues().size() < primaryKey.getValues().size()) {
284 ALOGE("Contains linked values false: whatKey is too small");
285 return false;
286 }
287
288 for (const auto& primaryValue : primaryKey.getValues()) {
289 bool found = false;
290 for (const auto& whatValue : whatKey.getValues()) {
291 if (linked(stateLinks, stateAtomId, primaryValue.mField, whatValue.mField) &&
292 primaryValue.mValue == whatValue.mValue) {
293 found = true;
294 break;
295 }
296 }
297 if (!found) {
298 return false;
299 }
300 }
301 return true;
302 }
303
linked(const vector<Metric2State> & stateLinks,const int32_t stateAtomId,const Field & stateField,const Field & metricField)304 bool linked(const vector<Metric2State>& stateLinks, const int32_t stateAtomId,
305 const Field& stateField, const Field& metricField) {
306 for (auto stateLink : stateLinks) {
307 if (stateLink.stateAtomId != stateAtomId) {
308 continue;
309 }
310
311 for (size_t i = 0; i < stateLink.stateFields.size(); i++) {
312 if (stateLink.stateFields[i].mMatcher == stateField &&
313 stateLink.metricFields[i].mMatcher == metricField) {
314 return true;
315 }
316 }
317 }
318 return false;
319 }
320
LessThan(const vector<FieldValue> & s1,const vector<FieldValue> & s2)321 bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
322 if (s1.size() != s2.size()) {
323 return s1.size() < s2.size();
324 }
325
326 size_t count = s1.size();
327 for (size_t i = 0; i < count; i++) {
328 if (s1[i] != s2[i]) {
329 return s1[i] < s2[i];
330 }
331 }
332 return false;
333 }
334
operator !=(const HashableDimensionKey & that) const335 bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const {
336 return !((*this) == that);
337 }
338
operator ==(const HashableDimensionKey & that) const339 bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
340 // according to http://go/cppref/cpp/container/vector/operator_cmp
341 return mValues == that.mValues;
342 };
343
operator <(const HashableDimensionKey & that) const344 bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
345 return LessThan(getValues(), that.getValues());
346 };
347
contains(const HashableDimensionKey & that) const348 bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
349 if (mValues.size() < that.getValues().size()) {
350 return false;
351 }
352
353 if (mValues.size() == that.getValues().size()) {
354 return (*this) == that;
355 }
356
357 for (const auto& value : that.getValues()) {
358 bool found = false;
359 for (const auto& myValue : mValues) {
360 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
361 found = true;
362 break;
363 }
364 }
365 if (!found) {
366 return false;
367 }
368 }
369
370 return true;
371 }
372
toString() const373 string HashableDimensionKey::toString() const {
374 std::string output;
375 for (const auto& value : mValues) {
376 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
377 value.mValue.toString().c_str());
378 }
379 return output;
380 }
381
operator ==(const MetricDimensionKey & that) const382 bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
383 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
384 mStateValuesKey == that.getStateValuesKey();
385 };
386
toString() const387 string MetricDimensionKey::toString() const {
388 return mDimensionKeyInWhat.toString() + mStateValuesKey.toString();
389 }
390
operator <(const MetricDimensionKey & that) const391 bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
392 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
393 return true;
394 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
395 return false;
396 }
397
398 return mStateValuesKey < that.getStateValuesKey();
399 }
400
getSize(const bool usesNestedDimensions) const401 size_t MetricDimensionKey::getSize(const bool usesNestedDimensions) const {
402 size_t dimensionKeySize = 0;
403 // Dimension/State values
404 if (usesNestedDimensions) {
405 // Assume nested dimension adds an additional atomTag + # of dimension fields
406 dimensionKeySize += sizeof(int32_t);
407 dimensionKeySize += sizeof(int32_t) * getDimensionKeyInWhat().getValues().size();
408 }
409 dimensionKeySize += getFieldValuesSizeV2(getDimensionKeyInWhat().getValues());
410 // Each state value has a atomId and group/value
411 dimensionKeySize += sizeof(int32_t) * getStateValuesKey().getValues().size();
412 dimensionKeySize += getFieldValuesSizeV2(getStateValuesKey().getValues());
413 return dimensionKeySize;
414 }
415
operator ==(const AtomDimensionKey & that) const416 bool AtomDimensionKey::operator==(const AtomDimensionKey& that) const {
417 return mAtomTag == that.getAtomTag() && mAtomFieldValues == that.getAtomFieldValues();
418 };
419
420 } // namespace statsd
421 } // namespace os
422 } // namespace android
423