blob: 5e156bb26caa94473c20772b35c7707141762bdf [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
tsaichristine10978642019-09-10 14:12:49 -070062bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values, Value* output) {
63 for (const auto& value : values) {
64 if (value.mField.matches(matcherField)) {
65 (*output) = value.mValue;
66 return true;
67 }
68 }
69 return false;
70}
71
Yangster13fb7e42018-03-07 17:30:49 -080072bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
73 HashableDimensionKey* output) {
Yangster-mace06cfd72018-03-10 23:22:59 -080074 size_t num_matches = 0;
75 for (const auto& value : values) {
76 for (size_t i = 0; i < matcherFields.size(); ++i) {
77 const auto& matcher = matcherFields[i];
Yangster13fb7e42018-03-07 17:30:49 -080078 if (value.mField.matches(matcher)) {
79 output->addValue(value);
Yangster-mace06cfd72018-03-10 23:22:59 -080080 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
81 output->mutableValue(num_matches)->mField.setField(
82 value.mField.getField() & matcher.mMask);
Yangster-macf5204922018-02-23 13:08:03 -080083 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -080084 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080085 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -080086 }
Yangster-mace06cfd72018-03-10 23:22:59 -080087 return num_matches > 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -080088}
89
90void filterGaugeValues(const std::vector<Matcher>& matcherFields,
91 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
92 for (const auto& field : matcherFields) {
93 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080094 if (value.mField.matches(field)) {
95 output->push_back(value);
96 }
97 }
98 }
99}
100
Yangster-mac53928882018-02-25 23:02:56 -0800101void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
102 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800103 HashableDimensionKey* conditionDimension) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800104 // Get the dimension first by using dimension from what.
Yangster-mac53928882018-02-25 23:02:56 -0800105 filterValues(links.metricFields, eventValues, conditionDimension);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800106
Yangster13fb7e42018-03-07 17:30:49 -0800107 size_t count = conditionDimension->getValues().size();
108 if (count != links.conditionFields.size()) {
109 // ALOGE("WTF condition link is bad");
110 return;
111 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800112
Yangster13fb7e42018-03-07 17:30:49 -0800113 for (size_t i = 0; i < count; i++) {
114 conditionDimension->mutableValue(i)->mField.setField(
115 links.conditionFields[i].mMatcher.getField());
116 conditionDimension->mutableValue(i)->mField.setTag(
117 links.conditionFields[i].mMatcher.getTag());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800118 }
119}
120
121bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
122 if (s1.size() != s2.size()) {
123 return s1.size() < s2.size();
124 }
125
126 size_t count = s1.size();
127 for (size_t i = 0; i < count; i++) {
128 if (s1[i] != s2[i]) {
129 return s1[i] < s2[i];
130 }
131 }
132 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800133}
134
Yangster-mac20877162017-12-22 17:19:39 -0800135bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800136 if (mValues.size() != that.getValues().size()) {
137 return false;
138 }
139 size_t count = mValues.size();
140 for (size_t i = 0; i < count; i++) {
141 if (mValues[i] != (that.getValues())[i]) {
142 return false;
143 }
144 }
145 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800146};
147
148bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800149 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800150};
151
Yao Chen8a8d16c2018-02-08 14:50:40 -0800152bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
153 if (mValues.size() < that.getValues().size()) {
154 return false;
155 }
156
157 if (mValues.size() == that.getValues().size()) {
158 return (*this) == that;
159 }
160
161 for (const auto& value : that.getValues()) {
162 bool found = false;
163 for (const auto& myValue : mValues) {
164 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
165 found = true;
166 break;
167 }
168 }
169 if (!found) {
170 return false;
171 }
172 }
173
174 return true;
175}
176
177string HashableDimensionKey::toString() const {
178 std::string output;
179 for (const auto& value : mValues) {
180 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
181 value.mValue.toString().c_str());
182 }
183 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800184}
185
186bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
187 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
Yao Chen8a8d16c2018-02-08 14:50:40 -0800188 mDimensionKeyInCondition == that.getDimensionKeyInCondition();
Yangster-mac93694462018-01-22 20:49:31 -0800189};
190
Yao Chen8a8d16c2018-02-08 14:50:40 -0800191string MetricDimensionKey::toString() const {
192 return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString();
193}
194
Yangster-mac93694462018-01-22 20:49:31 -0800195bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800196 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
197 return true;
198 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
199 return false;
200 }
Yangster-mac93694462018-01-22 20:49:31 -0800201
Yao Chen8a8d16c2018-02-08 14:50:40 -0800202 return mDimensionKeyInCondition < that.getDimensionKeyInCondition();
Chenjie Yu80f91122018-01-31 20:24:50 -0800203}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800204
Yao Chend5aa01b32017-12-19 16:46:36 -0800205} // namespace statsd
206} // namespace os
Yao Chen5bfffb52018-06-21 16:58:51 -0700207} // namespace android