blob: 070ad333fd9c850ff6744986e6a76c658c80053d [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
Yao Chend5aa01b32017-12-19 16:46:36 -080019#include "HashableDimensionKey.h"
Yao Chen8a8d16c2018-02-08 14:50:40 -080020#include "FieldValue.h"
Yao Chend5aa01b32017-12-19 16:46:36 -080021
22namespace android {
23namespace os {
24namespace statsd {
Yao Chen8a8d16c2018-02-08 14:50:40 -080025using std::vector;
Yao Chend5aa01b32017-12-19 16:46:36 -080026
Yao Chen8a8d16c2018-02-08 14:50:40 -080027android::hash_t hashDimension(const HashableDimensionKey& value) {
28 android::hash_t hash = 0;
29 for (const auto& fieldValue : value.getValues()) {
30 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField()));
31 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag()));
32 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType()));
33 switch (fieldValue.mValue.getType()) {
34 case INT:
35 hash = android::JenkinsHashMix(hash,
36 android::hash_type(fieldValue.mValue.int_value));
37 break;
38 case LONG:
39 hash = android::JenkinsHashMix(hash,
40 android::hash_type(fieldValue.mValue.long_value));
41 break;
42 case STRING:
43 hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()(
44 fieldValue.mValue.str_value)));
45 break;
46 case FLOAT: {
Yangster-mac16b7ff72018-02-23 11:11:36 -080047 hash = android::JenkinsHashMix(hash,
48 android::hash_type(fieldValue.mValue.float_value));
Yao Chen8a8d16c2018-02-08 14:50:40 -080049 break;
Yangster-mac20877162017-12-22 17:19:39 -080050 }
Yangster-mac20877162017-12-22 17:19:39 -080051 }
Yangster-mac20877162017-12-22 17:19:39 -080052 }
53 return JenkinsHashWhiten(hash);
54}
55
Yao Chen8a8d16c2018-02-08 14:50:40 -080056// Filter fields using the matchers and output the results as a HashableDimensionKey.
57// Note: HashableDimensionKey is just a wrapper for vector<FieldValue>
58bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
59 vector<HashableDimensionKey>* output) {
60 output->push_back(HashableDimensionKey());
61 // Top level is only tag id. Now take the real child matchers
62 int prevAnyMatcherPrefix = 0;
63 size_t prevPrevFanout = 0;
64 size_t prevFanout = 0;
65 // For each matcher get matched results.
66 for (const auto& matcher : matcherFields) {
67 vector<FieldValue> matchedResults;
68 for (const auto& value : values) {
69 // TODO: potential optimization here to break early because all fields are naturally
70 // sorted.
Yao Chen8a8d16c2018-02-08 14:50:40 -080071 if (value.mField.matches(matcher)) {
72 matchedResults.push_back(FieldValue(
73 Field(value.mField.getTag(), (value.mField.getField() & matcher.mMask)),
74 value.mValue));
Yangster-mac20877162017-12-22 17:19:39 -080075 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080076 }
Yangster-mac20877162017-12-22 17:19:39 -080077
Yao Chen8a8d16c2018-02-08 14:50:40 -080078 if (matchedResults.size() == 0) {
79 VLOG("We can't find a dimension value for matcher (%d)%#x.", matcher.mMatcher.getTag(),
80 matcher.mMatcher.getField());
81 continue;
82 }
83
84 if (matchedResults.size() == 1) {
85 for (auto& dimension : *output) {
86 dimension.addValue(matchedResults[0]);
87 }
88 prevAnyMatcherPrefix = 0;
89 prevFanout = 0;
90 continue;
91 }
92
93 // All the complexity below is because we support ANY in dimension.
94 bool createFanout = true;
95 // createFanout is true when the matcher doesn't need to follow the prev matcher's
96 // order.
97 // e.g., get (uid, tag) from any position in attribution. because we have translated
98 // it as 2 matchers, they need to follow the same ordering, we can't create a cross
99 // product of all uid and tags.
100 // However, if the 2 matchers have different prefix, they will create a cross product
101 // e.g., [any uid] [any some other repeated field], we will create a cross product for them
102 if (prevAnyMatcherPrefix != 0) {
103 int anyMatcherPrefix = 0;
104 bool isAnyMatcher = matcher.hasAnyPositionMatcher(&anyMatcherPrefix);
105 if (isAnyMatcher && anyMatcherPrefix == prevAnyMatcherPrefix) {
106 createFanout = false;
107 } else {
108 prevAnyMatcherPrefix = anyMatcherPrefix;
109 }
110 }
111
112 // Each matcher should match exact one field, unless position is ANY
113 // When x number of fields matches a matcher, the returned dimension
114 // size is multiplied by x.
115 int oldSize;
116 if (createFanout) {
117 // First create fanout (fanout size is matchedResults.Size which could be one,
118 // which means we do nothing here)
119 oldSize = output->size();
120 for (size_t i = 1; i < matchedResults.size(); i++) {
121 output->insert(output->end(), output->begin(), output->begin() + oldSize);
122 }
123 prevPrevFanout = oldSize;
124 prevFanout = matchedResults.size();
125 } else {
126 // If we should not create fanout, e.g., uid tag from same position should be remain
127 // together.
128 oldSize = prevPrevFanout;
129 if (prevFanout != matchedResults.size()) {
130 // sanity check.
131 ALOGE("2 Any matcher result in different output");
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800132 return false;
133 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800134 }
135 // now add the matched field value to output
136 for (size_t i = 0; i < matchedResults.size(); i++) {
137 for (int j = 0; j < oldSize; j++) {
138 (*output)[i * oldSize + j].addValue(matchedResults[i]);
139 }
140 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800141 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800142
143 return output->size() > 0 && (*output)[0].getValues().size() > 0;
144}
145
146void filterGaugeValues(const std::vector<Matcher>& matcherFields,
147 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
148 for (const auto& field : matcherFields) {
149 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800150 if (value.mField.matches(field)) {
151 output->push_back(value);
152 }
153 }
154 }
155}
156
157void getDimensionForCondition(const LogEvent& event, Metric2Condition links,
158 vector<HashableDimensionKey>* conditionDimension) {
159 // Get the dimension first by using dimension from what.
160 filterValues(links.metricFields, event.getValues(), conditionDimension);
161
162 // Then replace the field with the dimension from condition.
163 for (auto& dim : *conditionDimension) {
164 size_t count = dim.getValues().size();
165 if (count != links.conditionFields.size()) {
166 // ALOGE("WTF condition link is bad");
167 return;
168 }
169
170 for (size_t i = 0; i < count; i++) {
171 dim.mutableValue(i)->mField.setField(links.conditionFields[i].mMatcher.getField());
172 dim.mutableValue(i)->mField.setTag(links.conditionFields[i].mMatcher.getTag());
173 }
174 }
175}
176
177bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
178 if (s1.size() != s2.size()) {
179 return s1.size() < s2.size();
180 }
181
182 size_t count = s1.size();
183 for (size_t i = 0; i < count; i++) {
184 if (s1[i] != s2[i]) {
185 return s1[i] < s2[i];
186 }
187 }
188 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800189}
190
Yangster-mac20877162017-12-22 17:19:39 -0800191bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800192 if (mValues.size() != that.getValues().size()) {
193 return false;
194 }
195 size_t count = mValues.size();
196 for (size_t i = 0; i < count; i++) {
197 if (mValues[i] != (that.getValues())[i]) {
198 return false;
199 }
200 }
201 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800202};
203
204bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800205 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800206};
207
Yao Chen8a8d16c2018-02-08 14:50:40 -0800208bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
209 if (mValues.size() < that.getValues().size()) {
210 return false;
211 }
212
213 if (mValues.size() == that.getValues().size()) {
214 return (*this) == that;
215 }
216
217 for (const auto& value : that.getValues()) {
218 bool found = false;
219 for (const auto& myValue : mValues) {
220 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
221 found = true;
222 break;
223 }
224 }
225 if (!found) {
226 return false;
227 }
228 }
229
230 return true;
231}
232
233string HashableDimensionKey::toString() const {
234 std::string output;
235 for (const auto& value : mValues) {
236 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
237 value.mValue.toString().c_str());
238 }
239 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800240}
241
242bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
243 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
Yao Chen8a8d16c2018-02-08 14:50:40 -0800244 mDimensionKeyInCondition == that.getDimensionKeyInCondition();
Yangster-mac93694462018-01-22 20:49:31 -0800245};
246
Yao Chen8a8d16c2018-02-08 14:50:40 -0800247string MetricDimensionKey::toString() const {
248 return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString();
249}
250
Yangster-mac93694462018-01-22 20:49:31 -0800251bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800252 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
253 return true;
254 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
255 return false;
256 }
Yangster-mac93694462018-01-22 20:49:31 -0800257
Yao Chen8a8d16c2018-02-08 14:50:40 -0800258 return mDimensionKeyInCondition < that.getDimensionKeyInCondition();
Chenjie Yu80f91122018-01-31 20:24:50 -0800259}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800260
Yao Chend5aa01b32017-12-19 16:46:36 -0800261} // namespace statsd
262} // namespace os
263} // namespace android