• 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             default:
139                 break;
140         }
141     }
142     return JenkinsHashWhiten(hash);
143 }
144 
filterValues(const Matcher & matcherField,const vector<FieldValue> & values,FieldValue * output)145 bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values,
146                   FieldValue* output) {
147     for (const auto& value : values) {
148         if (value.mField.matches(matcherField)) {
149             (*output) = value;
150             return true;
151         }
152     }
153     return false;
154 }
155 
filterValues(const vector<Matcher> & matcherFields,const vector<FieldValue> & values,HashableDimensionKey * output)156 bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
157                   HashableDimensionKey* output) {
158     size_t num_matches = 0;
159     for (const auto& value : values) {
160         for (size_t i = 0; i < matcherFields.size(); ++i) {
161             const auto& matcher = matcherFields[i];
162             if (value.mField.matches(matcher)) {
163                 output->addValue(value);
164                 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
165                 output->mutableValue(num_matches)->mField.setField(
166                     value.mField.getField() & matcher.mMask);
167                 num_matches++;
168             }
169         }
170     }
171     return num_matches > 0;
172 }
173 
filterValues(const vector<Matcher> & dimKeyMatcherFields,const vector<Matcher> & valueMatcherFields,const vector<FieldValue> & values,HashableDimensionKey & key,vector<int> & valueIndices)174 bool filterValues(const vector<Matcher>& dimKeyMatcherFields,
175                   const vector<Matcher>& valueMatcherFields, const vector<FieldValue>& values,
176                   HashableDimensionKey& key, vector<int>& valueIndices) {
177     size_t key_num_matches = 0;
178     size_t value_num_matches = 0;
179     for (size_t i = 0; i < values.size(); ++i) {
180         const FieldValue& value = values[i];
181         for (const auto& matcher : dimKeyMatcherFields) {
182             if (value.mField.matches(matcher)) {
183                 key.addValue(value);
184                 key.mutableValue(key_num_matches)->mField.setTag(value.mField.getTag());
185                 key.mutableValue(key_num_matches)
186                         ->mField.setField(value.mField.getField() & matcher.mMask);
187                 key_num_matches++;
188             }
189         }
190         for (size_t j = 0; j < valueMatcherFields.size(); ++j) {
191             if (valueIndices[j] == -1 && value.mField.matches(valueMatcherFields[j])) {
192                 valueIndices[j] = i;
193                 value_num_matches++;
194             }
195         }
196     }
197     return value_num_matches == valueMatcherFields.size();
198 }
199 
filterPrimaryKey(const std::vector<FieldValue> & values,HashableDimensionKey * output)200 bool filterPrimaryKey(const std::vector<FieldValue>& values, HashableDimensionKey* output) {
201     size_t num_matches = 0;
202     const int32_t simpleFieldMask = 0xff7f0000;
203     const int32_t attributionUidFieldMask = 0xff7f7f7f;
204     for (const auto& value : values) {
205         if (value.mAnnotations.isPrimaryField()) {
206             output->addValue(value);
207             output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
208             const int32_t mask =
209                     isAttributionUidField(value) ? attributionUidFieldMask : simpleFieldMask;
210             output->mutableValue(num_matches)->mField.setField(value.mField.getField() & mask);
211             num_matches++;
212         }
213     }
214     return num_matches > 0;
215 }
216 
filterGaugeValues(const std::vector<Matcher> & matcherFields,const std::vector<FieldValue> & values,std::vector<FieldValue> * output)217 void filterGaugeValues(const std::vector<Matcher>& matcherFields,
218                        const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
219     for (const auto& field : matcherFields) {
220         for (const auto& value : values) {
221             if (value.mField.matches(field)) {
222                 output->push_back(value);
223             }
224         }
225     }
226 }
227 
getDimensionForCondition(const std::vector<FieldValue> & eventValues,const Metric2Condition & links,HashableDimensionKey * conditionDimension)228 void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
229                               const Metric2Condition& links,
230                               HashableDimensionKey* conditionDimension) {
231     // Get the dimension first by using dimension from what.
232     filterValues(links.metricFields, eventValues, conditionDimension);
233 
234     size_t count = conditionDimension->getValues().size();
235     if (count != links.conditionFields.size()) {
236         return;
237     }
238 
239     for (size_t i = 0; i < count; i++) {
240         conditionDimension->mutableValue(i)->mField.setField(
241                 links.conditionFields[i].mMatcher.getField());
242         conditionDimension->mutableValue(i)->mField.setTag(
243                 links.conditionFields[i].mMatcher.getTag());
244     }
245 }
246 
getDimensionForState(const std::vector<FieldValue> & eventValues,const Metric2State & link,HashableDimensionKey * statePrimaryKey)247 void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
248                           HashableDimensionKey* statePrimaryKey) {
249     // First, get the dimension from the event using the "what" fields from the
250     // MetricStateLinks.
251     filterValues(link.metricFields, eventValues, statePrimaryKey);
252 
253     // Then check that the statePrimaryKey size equals the number of state fields
254     size_t count = statePrimaryKey->getValues().size();
255     if (count != link.stateFields.size()) {
256         return;
257     }
258 
259     // For each dimension Value in the statePrimaryKey, set the field and tag
260     // using the state atom fields from MetricStateLinks.
261     for (size_t i = 0; i < count; i++) {
262         statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField());
263         statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag());
264     }
265 }
266 
containsLinkedStateValues(const HashableDimensionKey & whatKey,const HashableDimensionKey & primaryKey,const vector<Metric2State> & stateLinks,const int32_t stateAtomId)267 bool containsLinkedStateValues(const HashableDimensionKey& whatKey,
268                                const HashableDimensionKey& primaryKey,
269                                const vector<Metric2State>& stateLinks, const int32_t stateAtomId) {
270     if (whatKey.getValues().size() < primaryKey.getValues().size()) {
271         ALOGE("Contains linked values false: whatKey is too small");
272         return false;
273     }
274 
275     for (const auto& primaryValue : primaryKey.getValues()) {
276         bool found = false;
277         for (const auto& whatValue : whatKey.getValues()) {
278             if (linked(stateLinks, stateAtomId, primaryValue.mField, whatValue.mField) &&
279                 primaryValue.mValue == whatValue.mValue) {
280                 found = true;
281                 break;
282             }
283         }
284         if (!found) {
285             return false;
286         }
287     }
288     return true;
289 }
290 
linked(const vector<Metric2State> & stateLinks,const int32_t stateAtomId,const Field & stateField,const Field & metricField)291 bool linked(const vector<Metric2State>& stateLinks, const int32_t stateAtomId,
292             const Field& stateField, const Field& metricField) {
293     for (auto stateLink : stateLinks) {
294         if (stateLink.stateAtomId != stateAtomId) {
295             continue;
296         }
297 
298         for (size_t i = 0; i < stateLink.stateFields.size(); i++) {
299             if (stateLink.stateFields[i].mMatcher == stateField &&
300                 stateLink.metricFields[i].mMatcher == metricField) {
301                 return true;
302             }
303         }
304     }
305     return false;
306 }
307 
LessThan(const vector<FieldValue> & s1,const vector<FieldValue> & s2)308 bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
309     if (s1.size() != s2.size()) {
310         return s1.size() < s2.size();
311     }
312 
313     size_t count = s1.size();
314     for (size_t i = 0; i < count; i++) {
315         if (s1[i] != s2[i]) {
316             return s1[i] < s2[i];
317         }
318     }
319     return false;
320 }
321 
operator !=(const HashableDimensionKey & that) const322 bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const {
323     return !((*this) == that);
324 }
325 
operator ==(const HashableDimensionKey & that) const326 bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
327     if (mValues.size() != that.getValues().size()) {
328         return false;
329     }
330     size_t count = mValues.size();
331     for (size_t i = 0; i < count; i++) {
332         if (mValues[i] != (that.getValues())[i]) {
333             return false;
334         }
335     }
336     return true;
337 };
338 
operator <(const HashableDimensionKey & that) const339 bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
340     return LessThan(getValues(), that.getValues());
341 };
342 
contains(const HashableDimensionKey & that) const343 bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
344     if (mValues.size() < that.getValues().size()) {
345         return false;
346     }
347 
348     if (mValues.size() == that.getValues().size()) {
349         return (*this) == that;
350     }
351 
352     for (const auto& value : that.getValues()) {
353         bool found = false;
354         for (const auto& myValue : mValues) {
355             if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
356                 found = true;
357                 break;
358             }
359         }
360         if (!found) {
361             return false;
362         }
363     }
364 
365     return true;
366 }
367 
toString() const368 string HashableDimensionKey::toString() const {
369     std::string output;
370     for (const auto& value : mValues) {
371         output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
372                                value.mValue.toString().c_str());
373     }
374     return output;
375 }
376 
operator ==(const MetricDimensionKey & that) const377 bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
378     return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
379            mStateValuesKey == that.getStateValuesKey();
380 };
381 
toString() const382 string MetricDimensionKey::toString() const {
383     return mDimensionKeyInWhat.toString() + mStateValuesKey.toString();
384 }
385 
operator <(const MetricDimensionKey & that) const386 bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
387     if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
388         return true;
389     } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
390         return false;
391     }
392 
393     return mStateValuesKey < that.getStateValuesKey();
394 }
395 
operator ==(const AtomDimensionKey & that) const396 bool AtomDimensionKey::operator==(const AtomDimensionKey& that) const {
397     return mAtomTag == that.getAtomTag() && mAtomFieldValues == that.getAtomFieldValues();
398 };
399 
400 }  // namespace statsd
401 }  // namespace os
402 }  // namespace android
403