blob: 3c69f9ed683f4205404e22672ad7f3fc235e1fcb [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 Chen9c1debe2018-02-19 14:39:19 -080025
26using std::string;
Yao Chen8a8d16c2018-02-08 14:50:40 -080027using std::vector;
Yao Chend5aa01b32017-12-19 16:46:36 -080028
Yao Chen8a8d16c2018-02-08 14:50:40 -080029android::hash_t hashDimension(const HashableDimensionKey& value) {
30 android::hash_t hash = 0;
31 for (const auto& fieldValue : value.getValues()) {
32 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField()));
33 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag()));
34 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType()));
35 switch (fieldValue.mValue.getType()) {
36 case INT:
37 hash = android::JenkinsHashMix(hash,
38 android::hash_type(fieldValue.mValue.int_value));
39 break;
40 case LONG:
41 hash = android::JenkinsHashMix(hash,
42 android::hash_type(fieldValue.mValue.long_value));
43 break;
44 case STRING:
45 hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()(
46 fieldValue.mValue.str_value)));
47 break;
48 case FLOAT: {
49 float floatVal = fieldValue.mValue.float_value;
50 hash = android::JenkinsHashMixBytes(hash, (uint8_t*)&floatVal, sizeof(float));
51 break;
Yangster-mac20877162017-12-22 17:19:39 -080052 }
Yangster-mac20877162017-12-22 17:19:39 -080053 }
Yangster-mac20877162017-12-22 17:19:39 -080054 }
55 return JenkinsHashWhiten(hash);
56}
57
Yao Chen8a8d16c2018-02-08 14:50:40 -080058// Filter fields using the matchers and output the results as a HashableDimensionKey.
59// Note: HashableDimensionKey is just a wrapper for vector<FieldValue>
60bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
61 vector<HashableDimensionKey>* output) {
62 output->push_back(HashableDimensionKey());
63 // Top level is only tag id. Now take the real child matchers
64 int prevAnyMatcherPrefix = 0;
65 size_t prevPrevFanout = 0;
66 size_t prevFanout = 0;
67 // For each matcher get matched results.
68 for (const auto& matcher : matcherFields) {
69 vector<FieldValue> matchedResults;
70 for (const auto& value : values) {
71 // TODO: potential optimization here to break early because all fields are naturally
72 // sorted.
Yao Chen8a8d16c2018-02-08 14:50:40 -080073 if (value.mField.matches(matcher)) {
74 matchedResults.push_back(FieldValue(
75 Field(value.mField.getTag(), (value.mField.getField() & matcher.mMask)),
76 value.mValue));
Yangster-mac20877162017-12-22 17:19:39 -080077 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080078 }
Yangster-mac20877162017-12-22 17:19:39 -080079
Yao Chen8a8d16c2018-02-08 14:50:40 -080080 if (matchedResults.size() == 0) {
81 VLOG("We can't find a dimension value for matcher (%d)%#x.", matcher.mMatcher.getTag(),
82 matcher.mMatcher.getField());
83 continue;
84 }
85
86 if (matchedResults.size() == 1) {
87 for (auto& dimension : *output) {
88 dimension.addValue(matchedResults[0]);
89 }
90 prevAnyMatcherPrefix = 0;
91 prevFanout = 0;
92 continue;
93 }
94
95 // All the complexity below is because we support ANY in dimension.
96 bool createFanout = true;
97 // createFanout is true when the matcher doesn't need to follow the prev matcher's
98 // order.
99 // e.g., get (uid, tag) from any position in attribution. because we have translated
100 // it as 2 matchers, they need to follow the same ordering, we can't create a cross
101 // product of all uid and tags.
102 // However, if the 2 matchers have different prefix, they will create a cross product
103 // e.g., [any uid] [any some other repeated field], we will create a cross product for them
104 if (prevAnyMatcherPrefix != 0) {
105 int anyMatcherPrefix = 0;
106 bool isAnyMatcher = matcher.hasAnyPositionMatcher(&anyMatcherPrefix);
107 if (isAnyMatcher && anyMatcherPrefix == prevAnyMatcherPrefix) {
108 createFanout = false;
109 } else {
110 prevAnyMatcherPrefix = anyMatcherPrefix;
111 }
112 }
113
114 // Each matcher should match exact one field, unless position is ANY
115 // When x number of fields matches a matcher, the returned dimension
116 // size is multiplied by x.
117 int oldSize;
118 if (createFanout) {
119 // First create fanout (fanout size is matchedResults.Size which could be one,
120 // which means we do nothing here)
121 oldSize = output->size();
122 for (size_t i = 1; i < matchedResults.size(); i++) {
123 output->insert(output->end(), output->begin(), output->begin() + oldSize);
124 }
125 prevPrevFanout = oldSize;
126 prevFanout = matchedResults.size();
127 } else {
128 // If we should not create fanout, e.g., uid tag from same position should be remain
129 // together.
130 oldSize = prevPrevFanout;
131 if (prevFanout != matchedResults.size()) {
132 // sanity check.
133 ALOGE("2 Any matcher result in different output");
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800134 return false;
135 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800136 }
137 // now add the matched field value to output
138 for (size_t i = 0; i < matchedResults.size(); i++) {
139 for (int j = 0; j < oldSize; j++) {
140 (*output)[i * oldSize + j].addValue(matchedResults[i]);
141 }
142 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800143 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800144
145 return output->size() > 0 && (*output)[0].getValues().size() > 0;
146}
147
148void filterGaugeValues(const std::vector<Matcher>& matcherFields,
149 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
150 for (const auto& field : matcherFields) {
151 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800152 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