blob: 5b75b97a0764adda9e4d8e0c9cf6e4bb34dbeace [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: {
Yangster-mac16b7ff72018-02-23 11:11:36 -080049 hash = android::JenkinsHashMix(hash,
50 android::hash_type(fieldValue.mValue.float_value));
Yao Chen8a8d16c2018-02-08 14:50:40 -080051 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
tsaichristine69000e62019-10-18 17:34:52 -070060bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values,
61 FieldValue* output) {
tsaichristine10978642019-09-10 14:12:49 -070062 for (const auto& value : values) {
63 if (value.mField.matches(matcherField)) {
tsaichristine69000e62019-10-18 17:34:52 -070064 (*output) = value;
tsaichristine10978642019-09-10 14:12:49 -070065 return true;
66 }
67 }
68 return false;
69}
70
Yangster13fb7e42018-03-07 17:30:49 -080071bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
72 HashableDimensionKey* output) {
Yangster-mace06cfd72018-03-10 23:22:59 -080073 size_t num_matches = 0;
74 for (const auto& value : values) {
75 for (size_t i = 0; i < matcherFields.size(); ++i) {
76 const auto& matcher = matcherFields[i];
Yangster13fb7e42018-03-07 17:30:49 -080077 if (value.mField.matches(matcher)) {
78 output->addValue(value);
Yangster-mace06cfd72018-03-10 23:22:59 -080079 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
80 output->mutableValue(num_matches)->mField.setField(
81 value.mField.getField() & matcher.mMask);
Yangster-macf5204922018-02-23 13:08:03 -080082 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -080083 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080084 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -080085 }
Yangster-mace06cfd72018-03-10 23:22:59 -080086 return num_matches > 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -080087}
88
89void filterGaugeValues(const std::vector<Matcher>& matcherFields,
90 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
91 for (const auto& field : matcherFields) {
92 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080093 if (value.mField.matches(field)) {
94 output->push_back(value);
95 }
96 }
97 }
98}
99
Yangster-mac53928882018-02-25 23:02:56 -0800100void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
101 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800102 HashableDimensionKey* conditionDimension) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800103 // Get the dimension first by using dimension from what.
Yangster-mac53928882018-02-25 23:02:56 -0800104 filterValues(links.metricFields, eventValues, conditionDimension);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800105
Yangster13fb7e42018-03-07 17:30:49 -0800106 size_t count = conditionDimension->getValues().size();
107 if (count != links.conditionFields.size()) {
Yangster13fb7e42018-03-07 17:30:49 -0800108 return;
109 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800110
Yangster13fb7e42018-03-07 17:30:49 -0800111 for (size_t i = 0; i < count; i++) {
112 conditionDimension->mutableValue(i)->mField.setField(
tsaichristine69000e62019-10-18 17:34:52 -0700113 links.conditionFields[i].mMatcher.getField());
Yangster13fb7e42018-03-07 17:30:49 -0800114 conditionDimension->mutableValue(i)->mField.setTag(
tsaichristine69000e62019-10-18 17:34:52 -0700115 links.conditionFields[i].mMatcher.getTag());
116 }
117}
118
119void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
120 HashableDimensionKey* statePrimaryKey) {
121 // First, get the dimension from the event using the "what" fields from the
122 // MetricStateLinks.
123 filterValues(link.metricFields, eventValues, statePrimaryKey);
124
125 // Then check that the statePrimaryKey size equals the number of state fields
126 size_t count = statePrimaryKey->getValues().size();
127 if (count != link.stateFields.size()) {
128 return;
129 }
130
131 // For each dimension Value in the statePrimaryKey, set the field and tag
132 // using the state atom fields from MetricStateLinks.
133 for (size_t i = 0; i < count; i++) {
134 statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField());
135 statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800136 }
137}
138
139bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
140 if (s1.size() != s2.size()) {
141 return s1.size() < s2.size();
142 }
143
144 size_t count = s1.size();
145 for (size_t i = 0; i < count; i++) {
146 if (s1[i] != s2[i]) {
147 return s1[i] < s2[i];
148 }
149 }
150 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800151}
152
tsaichristinec876b492019-12-10 13:47:05 -0800153bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const {
154 return !((*this) == that);
155}
156
Yangster-mac20877162017-12-22 17:19:39 -0800157bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800158 if (mValues.size() != that.getValues().size()) {
159 return false;
160 }
161 size_t count = mValues.size();
162 for (size_t i = 0; i < count; i++) {
163 if (mValues[i] != (that.getValues())[i]) {
164 return false;
165 }
166 }
167 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800168};
169
170bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800171 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800172};
173
Yao Chen8a8d16c2018-02-08 14:50:40 -0800174bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
175 if (mValues.size() < that.getValues().size()) {
176 return false;
177 }
178
179 if (mValues.size() == that.getValues().size()) {
180 return (*this) == that;
181 }
182
183 for (const auto& value : that.getValues()) {
184 bool found = false;
185 for (const auto& myValue : mValues) {
186 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
187 found = true;
188 break;
189 }
190 }
191 if (!found) {
192 return false;
193 }
194 }
195
196 return true;
197}
198
199string HashableDimensionKey::toString() const {
200 std::string output;
201 for (const auto& value : mValues) {
202 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
203 value.mValue.toString().c_str());
204 }
205 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800206}
207
208bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
209 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
tsaichristine69000e62019-10-18 17:34:52 -0700210 mStateValuesKey == that.getStateValuesKey();
Yangster-mac93694462018-01-22 20:49:31 -0800211};
212
Yao Chen8a8d16c2018-02-08 14:50:40 -0800213string MetricDimensionKey::toString() const {
tsaichristine69000e62019-10-18 17:34:52 -0700214 return mDimensionKeyInWhat.toString() + mStateValuesKey.toString();
Yao Chen8a8d16c2018-02-08 14:50:40 -0800215}
216
Yangster-mac93694462018-01-22 20:49:31 -0800217bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800218 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
219 return true;
220 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
221 return false;
222 }
Yangster-mac93694462018-01-22 20:49:31 -0800223
tsaichristine69000e62019-10-18 17:34:52 -0700224 return mStateValuesKey < that.getStateValuesKey();
Chenjie Yu80f91122018-01-31 20:24:50 -0800225}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800226
Yao Chend5aa01b32017-12-19 16:46:36 -0800227} // namespace statsd
228} // namespace os
Yao Chen5bfffb52018-06-21 16:58:51 -0700229} // namespace android