blob: 68e2176c2e6dec09f3bfa0a71eeeacbee6ddf5d0 [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: {
47 float floatVal = fieldValue.mValue.float_value;
48 hash = android::JenkinsHashMixBytes(hash, (uint8_t*)&floatVal, sizeof(float));
49 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.
71 int32_t filteredField;
72 if (value.mField.matches(matcher)) {
73 matchedResults.push_back(FieldValue(
74 Field(value.mField.getTag(), (value.mField.getField() & matcher.mMask)),
75 value.mValue));
Yangster-mac20877162017-12-22 17:19:39 -080076 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080077 }
Yangster-mac20877162017-12-22 17:19:39 -080078
Yao Chen8a8d16c2018-02-08 14:50:40 -080079 if (matchedResults.size() == 0) {
80 VLOG("We can't find a dimension value for matcher (%d)%#x.", matcher.mMatcher.getTag(),
81 matcher.mMatcher.getField());
82 continue;
83 }
84
85 if (matchedResults.size() == 1) {
86 for (auto& dimension : *output) {
87 dimension.addValue(matchedResults[0]);
88 }
89 prevAnyMatcherPrefix = 0;
90 prevFanout = 0;
91 continue;
92 }
93
94 // All the complexity below is because we support ANY in dimension.
95 bool createFanout = true;
96 // createFanout is true when the matcher doesn't need to follow the prev matcher's
97 // order.
98 // e.g., get (uid, tag) from any position in attribution. because we have translated
99 // it as 2 matchers, they need to follow the same ordering, we can't create a cross
100 // product of all uid and tags.
101 // However, if the 2 matchers have different prefix, they will create a cross product
102 // e.g., [any uid] [any some other repeated field], we will create a cross product for them
103 if (prevAnyMatcherPrefix != 0) {
104 int anyMatcherPrefix = 0;
105 bool isAnyMatcher = matcher.hasAnyPositionMatcher(&anyMatcherPrefix);
106 if (isAnyMatcher && anyMatcherPrefix == prevAnyMatcherPrefix) {
107 createFanout = false;
108 } else {
109 prevAnyMatcherPrefix = anyMatcherPrefix;
110 }
111 }
112
113 // Each matcher should match exact one field, unless position is ANY
114 // When x number of fields matches a matcher, the returned dimension
115 // size is multiplied by x.
116 int oldSize;
117 if (createFanout) {
118 // First create fanout (fanout size is matchedResults.Size which could be one,
119 // which means we do nothing here)
120 oldSize = output->size();
121 for (size_t i = 1; i < matchedResults.size(); i++) {
122 output->insert(output->end(), output->begin(), output->begin() + oldSize);
123 }
124 prevPrevFanout = oldSize;
125 prevFanout = matchedResults.size();
126 } else {
127 // If we should not create fanout, e.g., uid tag from same position should be remain
128 // together.
129 oldSize = prevPrevFanout;
130 if (prevFanout != matchedResults.size()) {
131 // sanity check.
132 ALOGE("2 Any matcher result in different output");
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800133 return false;
134 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800135 }
136 // now add the matched field value to output
137 for (size_t i = 0; i < matchedResults.size(); i++) {
138 for (int j = 0; j < oldSize; j++) {
139 (*output)[i * oldSize + j].addValue(matchedResults[i]);
140 }
141 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800142 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800143
144 return output->size() > 0 && (*output)[0].getValues().size() > 0;
145}
146
147void filterGaugeValues(const std::vector<Matcher>& matcherFields,
148 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
149 for (const auto& field : matcherFields) {
150 for (const auto& value : values) {
151 int filteredField;
152 if (value.mField.matches(field)) {
153 output->push_back(value);
154 }
155 }
156 }
157}
158
159void getDimensionForCondition(const LogEvent& event, Metric2Condition links,
160 vector<HashableDimensionKey>* conditionDimension) {
161 // Get the dimension first by using dimension from what.
162 filterValues(links.metricFields, event.getValues(), conditionDimension);
163
164 // Then replace the field with the dimension from condition.
165 for (auto& dim : *conditionDimension) {
166 size_t count = dim.getValues().size();
167 if (count != links.conditionFields.size()) {
168 // ALOGE("WTF condition link is bad");
169 return;
170 }
171
172 for (size_t i = 0; i < count; i++) {
173 dim.mutableValue(i)->mField.setField(links.conditionFields[i].mMatcher.getField());
174 dim.mutableValue(i)->mField.setTag(links.conditionFields[i].mMatcher.getTag());
175 }
176 }
177}
178
179bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
180 if (s1.size() != s2.size()) {
181 return s1.size() < s2.size();
182 }
183
184 size_t count = s1.size();
185 for (size_t i = 0; i < count; i++) {
186 if (s1[i] != s2[i]) {
187 return s1[i] < s2[i];
188 }
189 }
190 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800191}
192
Yangster-mac20877162017-12-22 17:19:39 -0800193bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800194 if (mValues.size() != that.getValues().size()) {
195 return false;
196 }
197 size_t count = mValues.size();
198 for (size_t i = 0; i < count; i++) {
199 if (mValues[i] != (that.getValues())[i]) {
200 return false;
201 }
202 }
203 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800204};
205
206bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800207 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800208};
209
Yao Chen8a8d16c2018-02-08 14:50:40 -0800210bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
211 if (mValues.size() < that.getValues().size()) {
212 return false;
213 }
214
215 if (mValues.size() == that.getValues().size()) {
216 return (*this) == that;
217 }
218
219 for (const auto& value : that.getValues()) {
220 bool found = false;
221 for (const auto& myValue : mValues) {
222 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
223 found = true;
224 break;
225 }
226 }
227 if (!found) {
228 return false;
229 }
230 }
231
232 return true;
233}
234
235string HashableDimensionKey::toString() const {
236 std::string output;
237 for (const auto& value : mValues) {
238 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
239 value.mValue.toString().c_str());
240 }
241 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800242}
243
244bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
245 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
Yao Chen8a8d16c2018-02-08 14:50:40 -0800246 mDimensionKeyInCondition == that.getDimensionKeyInCondition();
Yangster-mac93694462018-01-22 20:49:31 -0800247};
248
Yao Chen8a8d16c2018-02-08 14:50:40 -0800249string MetricDimensionKey::toString() const {
250 return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString();
251}
252
Yangster-mac93694462018-01-22 20:49:31 -0800253bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800254 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
255 return true;
256 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
257 return false;
258 }
Yangster-mac93694462018-01-22 20:49:31 -0800259
Yao Chen8a8d16c2018-02-08 14:50:40 -0800260 return mDimensionKeyInCondition < that.getDimensionKeyInCondition();
Chenjie Yu80f91122018-01-31 20:24:50 -0800261}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800262
Yao Chend5aa01b32017-12-19 16:46:36 -0800263} // namespace statsd
264} // namespace os
265} // namespace android