blob: 1502a00abee92f390fd685213845c17866265ebc [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
Yao Chen8a8d16c2018-02-08 14:50:40 -080062// Filter fields using the matchers and output the results as a HashableDimensionKey.
63// Note: HashableDimensionKey is just a wrapper for vector<FieldValue>
64bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
65 vector<HashableDimensionKey>* output) {
66 output->push_back(HashableDimensionKey());
67 // Top level is only tag id. Now take the real child matchers
68 int prevAnyMatcherPrefix = 0;
69 size_t prevPrevFanout = 0;
70 size_t prevFanout = 0;
Yangster-macf5204922018-02-23 13:08:03 -080071
Yao Chen8a8d16c2018-02-08 14:50:40 -080072 // For each matcher get matched results.
Yangster-macf5204922018-02-23 13:08:03 -080073 vector<FieldValue> matchedResults(2);
Yao Chen8a8d16c2018-02-08 14:50:40 -080074 for (const auto& matcher : matcherFields) {
Yangster-macf5204922018-02-23 13:08:03 -080075 size_t num_matches = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -080076 for (const auto& value : values) {
77 // TODO: potential optimization here to break early because all fields are naturally
78 // sorted.
Yao Chen8a8d16c2018-02-08 14:50:40 -080079 if (value.mField.matches(matcher)) {
Yangster-macf5204922018-02-23 13:08:03 -080080 if (num_matches >= matchedResults.size()) {
81 matchedResults.resize(num_matches * 2);
82 }
83 matchedResults[num_matches].mField.setTag(value.mField.getTag());
84 matchedResults[num_matches].mField.setField(value.mField.getField() & matcher.mMask);
85 matchedResults[num_matches].mValue = value.mValue;
86 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -080087 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080088 }
Yangster-mac20877162017-12-22 17:19:39 -080089
Yangster-macf5204922018-02-23 13:08:03 -080090 if (num_matches == 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080091 VLOG("We can't find a dimension value for matcher (%d)%#x.", matcher.mMatcher.getTag(),
92 matcher.mMatcher.getField());
93 continue;
94 }
95
Yangster-macf5204922018-02-23 13:08:03 -080096 if (num_matches == 1) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080097 for (auto& dimension : *output) {
98 dimension.addValue(matchedResults[0]);
99 }
100 prevAnyMatcherPrefix = 0;
101 prevFanout = 0;
102 continue;
103 }
104
105 // All the complexity below is because we support ANY in dimension.
106 bool createFanout = true;
107 // createFanout is true when the matcher doesn't need to follow the prev matcher's
108 // order.
109 // e.g., get (uid, tag) from any position in attribution. because we have translated
110 // it as 2 matchers, they need to follow the same ordering, we can't create a cross
111 // product of all uid and tags.
112 // However, if the 2 matchers have different prefix, they will create a cross product
113 // e.g., [any uid] [any some other repeated field], we will create a cross product for them
114 if (prevAnyMatcherPrefix != 0) {
115 int anyMatcherPrefix = 0;
116 bool isAnyMatcher = matcher.hasAnyPositionMatcher(&anyMatcherPrefix);
117 if (isAnyMatcher && anyMatcherPrefix == prevAnyMatcherPrefix) {
118 createFanout = false;
119 } else {
120 prevAnyMatcherPrefix = anyMatcherPrefix;
121 }
122 }
123
124 // Each matcher should match exact one field, unless position is ANY
125 // When x number of fields matches a matcher, the returned dimension
126 // size is multiplied by x.
127 int oldSize;
128 if (createFanout) {
129 // First create fanout (fanout size is matchedResults.Size which could be one,
130 // which means we do nothing here)
131 oldSize = output->size();
Yangster-macf5204922018-02-23 13:08:03 -0800132 for (size_t i = 1; i < num_matches; i++) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800133 output->insert(output->end(), output->begin(), output->begin() + oldSize);
134 }
135 prevPrevFanout = oldSize;
Yangster-macf5204922018-02-23 13:08:03 -0800136 prevFanout = num_matches;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800137 } else {
138 // If we should not create fanout, e.g., uid tag from same position should be remain
139 // together.
140 oldSize = prevPrevFanout;
Yangster-macf5204922018-02-23 13:08:03 -0800141 if (prevFanout != num_matches) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800142 // sanity check.
143 ALOGE("2 Any matcher result in different output");
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800144 return false;
145 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800146 }
147 // now add the matched field value to output
Yangster-macf5204922018-02-23 13:08:03 -0800148 for (size_t i = 0; i < num_matches; i++) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800149 for (int j = 0; j < oldSize; j++) {
150 (*output)[i * oldSize + j].addValue(matchedResults[i]);
151 }
152 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800153 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800154
155 return output->size() > 0 && (*output)[0].getValues().size() > 0;
156}
157
158void filterGaugeValues(const std::vector<Matcher>& matcherFields,
159 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
160 for (const auto& field : matcherFields) {
161 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800162 if (value.mField.matches(field)) {
163 output->push_back(value);
164 }
165 }
166 }
167}
168
169void getDimensionForCondition(const LogEvent& event, Metric2Condition links,
170 vector<HashableDimensionKey>* conditionDimension) {
171 // Get the dimension first by using dimension from what.
172 filterValues(links.metricFields, event.getValues(), conditionDimension);
173
174 // Then replace the field with the dimension from condition.
175 for (auto& dim : *conditionDimension) {
176 size_t count = dim.getValues().size();
177 if (count != links.conditionFields.size()) {
178 // ALOGE("WTF condition link is bad");
179 return;
180 }
181
182 for (size_t i = 0; i < count; i++) {
183 dim.mutableValue(i)->mField.setField(links.conditionFields[i].mMatcher.getField());
184 dim.mutableValue(i)->mField.setTag(links.conditionFields[i].mMatcher.getTag());
185 }
186 }
187}
188
189bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
190 if (s1.size() != s2.size()) {
191 return s1.size() < s2.size();
192 }
193
194 size_t count = s1.size();
195 for (size_t i = 0; i < count; i++) {
196 if (s1[i] != s2[i]) {
197 return s1[i] < s2[i];
198 }
199 }
200 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800201}
202
Yangster-mac20877162017-12-22 17:19:39 -0800203bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800204 if (mValues.size() != that.getValues().size()) {
205 return false;
206 }
207 size_t count = mValues.size();
208 for (size_t i = 0; i < count; i++) {
209 if (mValues[i] != (that.getValues())[i]) {
210 return false;
211 }
212 }
213 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800214};
215
216bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800217 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800218};
219
Yao Chen8a8d16c2018-02-08 14:50:40 -0800220bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
221 if (mValues.size() < that.getValues().size()) {
222 return false;
223 }
224
225 if (mValues.size() == that.getValues().size()) {
226 return (*this) == that;
227 }
228
229 for (const auto& value : that.getValues()) {
230 bool found = false;
231 for (const auto& myValue : mValues) {
232 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
233 found = true;
234 break;
235 }
236 }
237 if (!found) {
238 return false;
239 }
240 }
241
242 return true;
243}
244
245string HashableDimensionKey::toString() const {
246 std::string output;
247 for (const auto& value : mValues) {
248 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
249 value.mValue.toString().c_str());
250 }
251 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800252}
253
254bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
255 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
Yao Chen8a8d16c2018-02-08 14:50:40 -0800256 mDimensionKeyInCondition == that.getDimensionKeyInCondition();
Yangster-mac93694462018-01-22 20:49:31 -0800257};
258
Yao Chen8a8d16c2018-02-08 14:50:40 -0800259string MetricDimensionKey::toString() const {
260 return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString();
261}
262
Yangster-mac93694462018-01-22 20:49:31 -0800263bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800264 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
265 return true;
266 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
267 return false;
268 }
Yangster-mac93694462018-01-22 20:49:31 -0800269
Yao Chen8a8d16c2018-02-08 14:50:40 -0800270 return mDimensionKeyInCondition < that.getDimensionKeyInCondition();
Chenjie Yu80f91122018-01-31 20:24:50 -0800271}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800272
Yao Chend5aa01b32017-12-19 16:46:36 -0800273} // namespace statsd
274} // namespace os
275} // namespace android