blob: d0c83119dc43e66337a303169ee0ffaf21fccdb1 [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
Yangster-macf5204922018-02-23 13:08:03 -080019#include <mutex>
20
Yao Chend5aa01b32017-12-19 16:46:36 -080021#include "HashableDimensionKey.h"
Yao Chen8a8d16c2018-02-08 14:50:40 -080022#include "FieldValue.h"
Yao Chend5aa01b32017-12-19 16:46:36 -080023
24namespace android {
25namespace os {
26namespace statsd {
Yao Chen9c1debe2018-02-19 14:39:19 -080027
28using std::string;
Yao Chen8a8d16c2018-02-08 14:50:40 -080029using std::vector;
Yao Chend5aa01b32017-12-19 16:46:36 -080030
Yao Chen8a8d16c2018-02-08 14:50:40 -080031android::hash_t hashDimension(const HashableDimensionKey& value) {
32 android::hash_t hash = 0;
33 for (const auto& fieldValue : value.getValues()) {
34 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField()));
35 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag()));
36 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType()));
37 switch (fieldValue.mValue.getType()) {
38 case INT:
39 hash = android::JenkinsHashMix(hash,
40 android::hash_type(fieldValue.mValue.int_value));
41 break;
42 case LONG:
43 hash = android::JenkinsHashMix(hash,
44 android::hash_type(fieldValue.mValue.long_value));
45 break;
46 case STRING:
47 hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()(
48 fieldValue.mValue.str_value)));
49 break;
50 case FLOAT: {
Yangster-mac16b7ff72018-02-23 11:11:36 -080051 hash = android::JenkinsHashMix(hash,
52 android::hash_type(fieldValue.mValue.float_value));
Yao Chen8a8d16c2018-02-08 14:50:40 -080053 break;
Yangster-mac20877162017-12-22 17:19:39 -080054 }
Yangster-macf5204922018-02-23 13:08:03 -080055 default:
56 break;
Yangster-mac20877162017-12-22 17:19:39 -080057 }
Yangster-mac20877162017-12-22 17:19:39 -080058 }
59 return JenkinsHashWhiten(hash);
60}
61
Yangster13fb7e42018-03-07 17:30:49 -080062bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
63 HashableDimensionKey* output) {
64 for (size_t i = 0; i < matcherFields.size(); ++i) {
65 const auto& matcher = matcherFields[i];
66 bool found = false;
67 for (const auto& value : values) {
68 // TODO: potential optimization here to break early because all fields are naturally
69 // sorted.
70 if (value.mField.matches(matcher)) {
71 output->addValue(value);
72 output->mutableValue(i)->mField.setTag(value.mField.getTag());
73 output->mutableValue(i)->mField.setField(value.mField.getField() & matcher.mMask);
74 found = true;
75 break;
76 }
77 }
78
79 if (!found) {
80 VLOG("We can't find a dimension value for matcher (%d)%#x.", matcher.mMatcher.getTag(),
81 matcher.mMatcher.getField());
82 return false;
83 }
84 }
85
86 return true;
87}
88
Yao Chen8a8d16c2018-02-08 14:50:40 -080089// Filter fields using the matchers and output the results as a HashableDimensionKey.
90// Note: HashableDimensionKey is just a wrapper for vector<FieldValue>
91bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
92 vector<HashableDimensionKey>* output) {
93 output->push_back(HashableDimensionKey());
94 // Top level is only tag id. Now take the real child matchers
95 int prevAnyMatcherPrefix = 0;
96 size_t prevPrevFanout = 0;
97 size_t prevFanout = 0;
Yangster-macf5204922018-02-23 13:08:03 -080098
Yao Chen8a8d16c2018-02-08 14:50:40 -080099 // For each matcher get matched results.
Yangster-macf5204922018-02-23 13:08:03 -0800100 vector<FieldValue> matchedResults(2);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800101 for (const auto& matcher : matcherFields) {
Yangster-macf5204922018-02-23 13:08:03 -0800102 size_t num_matches = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800103 for (const auto& value : values) {
104 // TODO: potential optimization here to break early because all fields are naturally
105 // sorted.
Yao Chen8a8d16c2018-02-08 14:50:40 -0800106 if (value.mField.matches(matcher)) {
Yangster-macf5204922018-02-23 13:08:03 -0800107 if (num_matches >= matchedResults.size()) {
108 matchedResults.resize(num_matches * 2);
109 }
110 matchedResults[num_matches].mField.setTag(value.mField.getTag());
111 matchedResults[num_matches].mField.setField(value.mField.getField() & matcher.mMask);
112 matchedResults[num_matches].mValue = value.mValue;
113 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -0800114 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800115 }
Yangster-mac20877162017-12-22 17:19:39 -0800116
Yangster-macf5204922018-02-23 13:08:03 -0800117 if (num_matches == 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800118 VLOG("We can't find a dimension value for matcher (%d)%#x.", matcher.mMatcher.getTag(),
119 matcher.mMatcher.getField());
120 continue;
121 }
122
Yangster-macf5204922018-02-23 13:08:03 -0800123 if (num_matches == 1) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800124 for (auto& dimension : *output) {
125 dimension.addValue(matchedResults[0]);
126 }
127 prevAnyMatcherPrefix = 0;
128 prevFanout = 0;
129 continue;
130 }
131
132 // All the complexity below is because we support ANY in dimension.
133 bool createFanout = true;
134 // createFanout is true when the matcher doesn't need to follow the prev matcher's
135 // order.
136 // e.g., get (uid, tag) from any position in attribution. because we have translated
137 // it as 2 matchers, they need to follow the same ordering, we can't create a cross
138 // product of all uid and tags.
139 // However, if the 2 matchers have different prefix, they will create a cross product
140 // e.g., [any uid] [any some other repeated field], we will create a cross product for them
141 if (prevAnyMatcherPrefix != 0) {
142 int anyMatcherPrefix = 0;
143 bool isAnyMatcher = matcher.hasAnyPositionMatcher(&anyMatcherPrefix);
144 if (isAnyMatcher && anyMatcherPrefix == prevAnyMatcherPrefix) {
145 createFanout = false;
146 } else {
147 prevAnyMatcherPrefix = anyMatcherPrefix;
148 }
149 }
150
151 // Each matcher should match exact one field, unless position is ANY
152 // When x number of fields matches a matcher, the returned dimension
153 // size is multiplied by x.
154 int oldSize;
155 if (createFanout) {
156 // First create fanout (fanout size is matchedResults.Size which could be one,
157 // which means we do nothing here)
158 oldSize = output->size();
Yangster-macf5204922018-02-23 13:08:03 -0800159 for (size_t i = 1; i < num_matches; i++) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800160 output->insert(output->end(), output->begin(), output->begin() + oldSize);
161 }
162 prevPrevFanout = oldSize;
Yangster-macf5204922018-02-23 13:08:03 -0800163 prevFanout = num_matches;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800164 } else {
165 // If we should not create fanout, e.g., uid tag from same position should be remain
166 // together.
167 oldSize = prevPrevFanout;
Yangster-macf5204922018-02-23 13:08:03 -0800168 if (prevFanout != num_matches) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800169 // sanity check.
170 ALOGE("2 Any matcher result in different output");
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800171 return false;
172 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800173 }
174 // now add the matched field value to output
Yangster-macf5204922018-02-23 13:08:03 -0800175 for (size_t i = 0; i < num_matches; i++) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800176 for (int j = 0; j < oldSize; j++) {
177 (*output)[i * oldSize + j].addValue(matchedResults[i]);
178 }
179 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800180 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800181
182 return output->size() > 0 && (*output)[0].getValues().size() > 0;
183}
184
185void filterGaugeValues(const std::vector<Matcher>& matcherFields,
186 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
187 for (const auto& field : matcherFields) {
188 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800189 if (value.mField.matches(field)) {
190 output->push_back(value);
191 }
192 }
193 }
194}
195
Yangster-mac53928882018-02-25 23:02:56 -0800196void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
197 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800198 HashableDimensionKey* conditionDimension) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800199 // Get the dimension first by using dimension from what.
Yangster-mac53928882018-02-25 23:02:56 -0800200 filterValues(links.metricFields, eventValues, conditionDimension);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800201
Yangster13fb7e42018-03-07 17:30:49 -0800202 size_t count = conditionDimension->getValues().size();
203 if (count != links.conditionFields.size()) {
204 // ALOGE("WTF condition link is bad");
205 return;
206 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800207
Yangster13fb7e42018-03-07 17:30:49 -0800208 for (size_t i = 0; i < count; i++) {
209 conditionDimension->mutableValue(i)->mField.setField(
210 links.conditionFields[i].mMatcher.getField());
211 conditionDimension->mutableValue(i)->mField.setTag(
212 links.conditionFields[i].mMatcher.getTag());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800213 }
214}
215
216bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
217 if (s1.size() != s2.size()) {
218 return s1.size() < s2.size();
219 }
220
221 size_t count = s1.size();
222 for (size_t i = 0; i < count; i++) {
223 if (s1[i] != s2[i]) {
224 return s1[i] < s2[i];
225 }
226 }
227 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800228}
229
Yangster-mac20877162017-12-22 17:19:39 -0800230bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800231 if (mValues.size() != that.getValues().size()) {
232 return false;
233 }
234 size_t count = mValues.size();
235 for (size_t i = 0; i < count; i++) {
236 if (mValues[i] != (that.getValues())[i]) {
237 return false;
238 }
239 }
240 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800241};
242
243bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800244 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800245};
246
Yao Chen8a8d16c2018-02-08 14:50:40 -0800247bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
248 if (mValues.size() < that.getValues().size()) {
249 return false;
250 }
251
252 if (mValues.size() == that.getValues().size()) {
253 return (*this) == that;
254 }
255
256 for (const auto& value : that.getValues()) {
257 bool found = false;
258 for (const auto& myValue : mValues) {
259 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
260 found = true;
261 break;
262 }
263 }
264 if (!found) {
265 return false;
266 }
267 }
268
269 return true;
270}
271
272string HashableDimensionKey::toString() const {
273 std::string output;
274 for (const auto& value : mValues) {
275 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
276 value.mValue.toString().c_str());
277 }
278 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800279}
280
281bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
282 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
Yao Chen8a8d16c2018-02-08 14:50:40 -0800283 mDimensionKeyInCondition == that.getDimensionKeyInCondition();
Yangster-mac93694462018-01-22 20:49:31 -0800284};
285
Yao Chen8a8d16c2018-02-08 14:50:40 -0800286string MetricDimensionKey::toString() const {
287 return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString();
288}
289
Yangster-mac93694462018-01-22 20:49:31 -0800290bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800291 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
292 return true;
293 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
294 return false;
295 }
Yangster-mac93694462018-01-22 20:49:31 -0800296
Yao Chen8a8d16c2018-02-08 14:50:40 -0800297 return mDimensionKeyInCondition < that.getDimensionKeyInCondition();
Chenjie Yu80f91122018-01-31 20:24:50 -0800298}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800299
Yao Chend5aa01b32017-12-19 16:46:36 -0800300} // namespace statsd
301} // namespace os
302} // namespace android