blob: 7b21fb00f19d9600c0aa252b05ab793f71cb3cec [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 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-macf5204922018-02-23 13:08:03 -080053 default:
54 break;
Yangster-mac20877162017-12-22 17:19:39 -080055 }
Yangster-mac20877162017-12-22 17:19:39 -080056 }
57 return JenkinsHashWhiten(hash);
58}
59
Yao Chen8a8d16c2018-02-08 14:50:40 -080060// Filter fields using the matchers and output the results as a HashableDimensionKey.
61// Note: HashableDimensionKey is just a wrapper for vector<FieldValue>
62bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
63 vector<HashableDimensionKey>* output) {
64 output->push_back(HashableDimensionKey());
65 // Top level is only tag id. Now take the real child matchers
66 int prevAnyMatcherPrefix = 0;
67 size_t prevPrevFanout = 0;
68 size_t prevFanout = 0;
Yangster-macf5204922018-02-23 13:08:03 -080069
Yao Chen8a8d16c2018-02-08 14:50:40 -080070 // For each matcher get matched results.
Yangster-macf5204922018-02-23 13:08:03 -080071 vector<FieldValue> matchedResults(2);
Yao Chen8a8d16c2018-02-08 14:50:40 -080072 for (const auto& matcher : matcherFields) {
Yangster-macf5204922018-02-23 13:08:03 -080073 size_t num_matches = 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -080074 for (const auto& value : values) {
75 // TODO: potential optimization here to break early because all fields are naturally
76 // sorted.
Yao Chen8a8d16c2018-02-08 14:50:40 -080077 if (value.mField.matches(matcher)) {
Yangster-macf5204922018-02-23 13:08:03 -080078 if (num_matches >= matchedResults.size()) {
79 matchedResults.resize(num_matches * 2);
80 }
81 matchedResults[num_matches].mField.setTag(value.mField.getTag());
82 matchedResults[num_matches].mField.setField(value.mField.getField() & matcher.mMask);
83 matchedResults[num_matches].mValue = value.mValue;
84 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -080085 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080086 }
Yangster-mac20877162017-12-22 17:19:39 -080087
Yangster-macf5204922018-02-23 13:08:03 -080088 if (num_matches == 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080089 VLOG("We can't find a dimension value for matcher (%d)%#x.", matcher.mMatcher.getTag(),
90 matcher.mMatcher.getField());
91 continue;
92 }
93
Yangster-macf5204922018-02-23 13:08:03 -080094 if (num_matches == 1) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080095 for (auto& dimension : *output) {
96 dimension.addValue(matchedResults[0]);
97 }
98 prevAnyMatcherPrefix = 0;
99 prevFanout = 0;
100 continue;
101 }
102
103 // All the complexity below is because we support ANY in dimension.
104 bool createFanout = true;
105 // createFanout is true when the matcher doesn't need to follow the prev matcher's
106 // order.
107 // e.g., get (uid, tag) from any position in attribution. because we have translated
108 // it as 2 matchers, they need to follow the same ordering, we can't create a cross
109 // product of all uid and tags.
110 // However, if the 2 matchers have different prefix, they will create a cross product
111 // e.g., [any uid] [any some other repeated field], we will create a cross product for them
112 if (prevAnyMatcherPrefix != 0) {
113 int anyMatcherPrefix = 0;
114 bool isAnyMatcher = matcher.hasAnyPositionMatcher(&anyMatcherPrefix);
115 if (isAnyMatcher && anyMatcherPrefix == prevAnyMatcherPrefix) {
116 createFanout = false;
117 } else {
118 prevAnyMatcherPrefix = anyMatcherPrefix;
119 }
120 }
121
122 // Each matcher should match exact one field, unless position is ANY
123 // When x number of fields matches a matcher, the returned dimension
124 // size is multiplied by x.
125 int oldSize;
126 if (createFanout) {
127 // First create fanout (fanout size is matchedResults.Size which could be one,
128 // which means we do nothing here)
129 oldSize = output->size();
Yangster-macf5204922018-02-23 13:08:03 -0800130 for (size_t i = 1; i < num_matches; i++) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800131 output->insert(output->end(), output->begin(), output->begin() + oldSize);
132 }
133 prevPrevFanout = oldSize;
Yangster-macf5204922018-02-23 13:08:03 -0800134 prevFanout = num_matches;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800135 } else {
136 // If we should not create fanout, e.g., uid tag from same position should be remain
137 // together.
138 oldSize = prevPrevFanout;
Yangster-macf5204922018-02-23 13:08:03 -0800139 if (prevFanout != num_matches) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800140 // sanity check.
141 ALOGE("2 Any matcher result in different output");
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800142 return false;
143 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800144 }
145 // now add the matched field value to output
Yangster-macf5204922018-02-23 13:08:03 -0800146 for (size_t i = 0; i < num_matches; i++) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800147 for (int j = 0; j < oldSize; j++) {
148 (*output)[i * oldSize + j].addValue(matchedResults[i]);
149 }
150 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800151 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800152
153 return output->size() > 0 && (*output)[0].getValues().size() > 0;
154}
155
156void filterGaugeValues(const std::vector<Matcher>& matcherFields,
157 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
158 for (const auto& field : matcherFields) {
159 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800160 if (value.mField.matches(field)) {
161 output->push_back(value);
162 }
163 }
164 }
165}
166
167void getDimensionForCondition(const LogEvent& event, Metric2Condition links,
168 vector<HashableDimensionKey>* conditionDimension) {
169 // Get the dimension first by using dimension from what.
170 filterValues(links.metricFields, event.getValues(), conditionDimension);
171
172 // Then replace the field with the dimension from condition.
173 for (auto& dim : *conditionDimension) {
174 size_t count = dim.getValues().size();
175 if (count != links.conditionFields.size()) {
176 // ALOGE("WTF condition link is bad");
177 return;
178 }
179
180 for (size_t i = 0; i < count; i++) {
181 dim.mutableValue(i)->mField.setField(links.conditionFields[i].mMatcher.getField());
182 dim.mutableValue(i)->mField.setTag(links.conditionFields[i].mMatcher.getTag());
183 }
184 }
185}
186
187bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
188 if (s1.size() != s2.size()) {
189 return s1.size() < s2.size();
190 }
191
192 size_t count = s1.size();
193 for (size_t i = 0; i < count; i++) {
194 if (s1[i] != s2[i]) {
195 return s1[i] < s2[i];
196 }
197 }
198 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800199}
200
Yangster-mac20877162017-12-22 17:19:39 -0800201bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800202 if (mValues.size() != that.getValues().size()) {
203 return false;
204 }
205 size_t count = mValues.size();
206 for (size_t i = 0; i < count; i++) {
207 if (mValues[i] != (that.getValues())[i]) {
208 return false;
209 }
210 }
211 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800212};
213
214bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800215 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800216};
217
Yao Chen8a8d16c2018-02-08 14:50:40 -0800218bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
219 if (mValues.size() < that.getValues().size()) {
220 return false;
221 }
222
223 if (mValues.size() == that.getValues().size()) {
224 return (*this) == that;
225 }
226
227 for (const auto& value : that.getValues()) {
228 bool found = false;
229 for (const auto& myValue : mValues) {
230 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
231 found = true;
232 break;
233 }
234 }
235 if (!found) {
236 return false;
237 }
238 }
239
240 return true;
241}
242
243string HashableDimensionKey::toString() const {
244 std::string output;
245 for (const auto& value : mValues) {
246 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
247 value.mValue.toString().c_str());
248 }
249 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800250}
251
252bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
253 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
Yao Chen8a8d16c2018-02-08 14:50:40 -0800254 mDimensionKeyInCondition == that.getDimensionKeyInCondition();
Yangster-mac93694462018-01-22 20:49:31 -0800255};
256
Yao Chen8a8d16c2018-02-08 14:50:40 -0800257string MetricDimensionKey::toString() const {
258 return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString();
259}
260
Yangster-mac93694462018-01-22 20:49:31 -0800261bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800262 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
263 return true;
264 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
265 return false;
266 }
Yangster-mac93694462018-01-22 20:49:31 -0800267
Yao Chen8a8d16c2018-02-08 14:50:40 -0800268 return mDimensionKeyInCondition < that.getDimensionKeyInCondition();
Chenjie Yu80f91122018-01-31 20:24:50 -0800269}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800270
Yao Chend5aa01b32017-12-19 16:46:36 -0800271} // namespace statsd
272} // namespace os
273} // namespace android