• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
filterValues(const std::vector<Matcher> & matcherFields,const std::vector<FieldValue> & values,bool omitMatches)230 vector<FieldValue> filterValues(const std::vector<Matcher>& matcherFields,
231                                 const std::vector<FieldValue>& values, bool omitMatches) {
232     if (matcherFields.empty()) {
233         return values;
234     }
235 
236     vector<FieldValue> output;
237     for (const auto& field : matcherFields) {
238         for (const auto& value : values) {
239             if (value.mField.matches(field) ^ omitMatches) {
240                 output.push_back(value);
241             }
242         }
243     }
244     return output;
245 }
246 
getDimensionForCondition(const std::vector<FieldValue> & eventValues,const Metric2Condition & links,HashableDimensionKey * conditionDimension)247 void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
248                               const Metric2Condition& links,
249                               HashableDimensionKey* conditionDimension) {
250     // Get the dimension first by using dimension from what.
251     filterValues(links.metricFields, eventValues, conditionDimension);
252 
253     size_t count = conditionDimension->getValues().size();
254     if (count != links.conditionFields.size()) {
255         return;
256     }
257 
258     for (size_t i = 0; i < count; i++) {
259         conditionDimension->mutableValue(i)->mField.setField(
260                 links.conditionFields[i].mMatcher.getField());
261         conditionDimension->mutableValue(i)->mField.setTag(
262                 links.conditionFields[i].mMatcher.getTag());
263     }
264 }
265 
getDimensionForState(const std::vector<FieldValue> & eventValues,const Metric2State & link,HashableDimensionKey * statePrimaryKey)266 void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
267                           HashableDimensionKey* statePrimaryKey) {
268     // First, get the dimension from the event using the "what" fields from the
269     // MetricStateLinks.
270     filterValues(link.metricFields, eventValues, statePrimaryKey);
271 
272     // Then check that the statePrimaryKey size equals the number of state fields
273     size_t count = statePrimaryKey->getValues().size();
274     if (count != link.stateFields.size()) {
275         return;
276     }
277 
278     // For each dimension Value in the statePrimaryKey, set the field and tag
279     // using the state atom fields from MetricStateLinks.
280     for (size_t i = 0; i < count; i++) {
281         statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField());
282         statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag());
283     }
284 }
285 
containsLinkedStateValues(const HashableDimensionKey & whatKey,const HashableDimensionKey & primaryKey,const vector<Metric2State> & stateLinks,const int32_t stateAtomId)286 bool containsLinkedStateValues(const HashableDimensionKey& whatKey,
287                                const HashableDimensionKey& primaryKey,
288                                const vector<Metric2State>& stateLinks, const int32_t stateAtomId) {
289     if (whatKey.getValues().size() < primaryKey.getValues().size()) {
290         ALOGE("Contains linked values false: whatKey is too small");
291         return false;
292     }
293 
294     for (const auto& primaryValue : primaryKey.getValues()) {
295         bool found = false;
296         for (const auto& whatValue : whatKey.getValues()) {
297             if (linked(stateLinks, stateAtomId, primaryValue.mField, whatValue.mField) &&
298                 primaryValue.mValue == whatValue.mValue) {
299                 found = true;
300                 break;
301             }
302         }
303         if (!found) {
304             return false;
305         }
306     }
307     return true;
308 }
309 
linked(const vector<Metric2State> & stateLinks,const int32_t stateAtomId,const Field & stateField,const Field & metricField)310 bool linked(const vector<Metric2State>& stateLinks, const int32_t stateAtomId,
311             const Field& stateField, const Field& metricField) {
312     for (auto stateLink : stateLinks) {
313         if (stateLink.stateAtomId != stateAtomId) {
314             continue;
315         }
316 
317         for (size_t i = 0; i < stateLink.stateFields.size(); i++) {
318             if (stateLink.stateFields[i].mMatcher == stateField &&
319                 stateLink.metricFields[i].mMatcher == metricField) {
320                 return true;
321             }
322         }
323     }
324     return false;
325 }
326 
LessThan(const vector<FieldValue> & s1,const vector<FieldValue> & s2)327 bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
328     if (s1.size() != s2.size()) {
329         return s1.size() < s2.size();
330     }
331 
332     size_t count = s1.size();
333     for (size_t i = 0; i < count; i++) {
334         if (s1[i] != s2[i]) {
335             return s1[i] < s2[i];
336         }
337     }
338     return false;
339 }
340 
operator !=(const HashableDimensionKey & that) const341 bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const {
342     return !((*this) == that);
343 }
344 
operator ==(const HashableDimensionKey & that) const345 bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
346     // according to http://go/cppref/cpp/container/vector/operator_cmp
347     return mValues == that.mValues;
348 };
349 
operator <(const HashableDimensionKey & that) const350 bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
351     return LessThan(getValues(), that.getValues());
352 };
353 
contains(const HashableDimensionKey & that) const354 bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
355     if (mValues.size() < that.getValues().size()) {
356         return false;
357     }
358 
359     if (mValues.size() == that.getValues().size()) {
360         return (*this) == that;
361     }
362 
363     for (const auto& value : that.getValues()) {
364         bool found = false;
365         for (const auto& myValue : mValues) {
366             if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
367                 found = true;
368                 break;
369             }
370         }
371         if (!found) {
372             return false;
373         }
374     }
375 
376     return true;
377 }
378 
toString() const379 string HashableDimensionKey::toString() const {
380     std::string output;
381     for (const auto& value : mValues) {
382         output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
383                                value.mValue.toString().c_str());
384     }
385     return output;
386 }
387 
operator ==(const MetricDimensionKey & that) const388 bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
389     return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
390            mStateValuesKey == that.getStateValuesKey();
391 };
392 
toString() const393 string MetricDimensionKey::toString() const {
394     return mDimensionKeyInWhat.toString() + mStateValuesKey.toString();
395 }
396 
operator <(const MetricDimensionKey & that) const397 bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
398     if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
399         return true;
400     } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
401         return false;
402     }
403 
404     return mStateValuesKey < that.getStateValuesKey();
405 }
406 
getSize(const bool usesNestedDimensions) const407 size_t MetricDimensionKey::getSize(const bool usesNestedDimensions) const {
408     size_t dimensionKeySize = 0;
409     // Dimension/State values
410     if (usesNestedDimensions) {
411         // Assume nested dimension adds an additional atomTag + # of dimension fields
412         dimensionKeySize += sizeof(int32_t);
413         dimensionKeySize += sizeof(int32_t) * getDimensionKeyInWhat().getValues().size();
414     }
415     dimensionKeySize += getFieldValuesSizeV2(getDimensionKeyInWhat().getValues());
416     // Each state value has a atomId and group/value
417     dimensionKeySize += sizeof(int32_t) * getStateValuesKey().getValues().size();
418     dimensionKeySize += getFieldValuesSizeV2(getStateValuesKey().getValues());
419     return dimensionKeySize;
420 }
421 
operator ==(const AtomDimensionKey & that) const422 bool AtomDimensionKey::operator==(const AtomDimensionKey& that) const {
423     return mAtomTag == that.getAtomTag() && mAtomFieldValues == that.getAtomFieldValues();
424 };
425 
426 }  // namespace statsd
427 }  // namespace os
428 }  // namespace android
429