blob: 6b9d0e4fdac068a583a9eab933ccf3984dd0dc8b [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
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080029// These constants must be kept in sync with those in StatsDimensionsValue.java
30const static int STATS_DIMENSIONS_VALUE_STRING_TYPE = 2;
31const static int STATS_DIMENSIONS_VALUE_INT_TYPE = 3;
32const static int STATS_DIMENSIONS_VALUE_LONG_TYPE = 4;
33// const static int STATS_DIMENSIONS_VALUE_BOOL_TYPE = 5; (commented out because
34// unused -- statsd does not correctly support bool types)
35const static int STATS_DIMENSIONS_VALUE_FLOAT_TYPE = 6;
36const static int STATS_DIMENSIONS_VALUE_TUPLE_TYPE = 7;
37
38/**
39 * Recursive helper function that populates a parent StatsDimensionsValueParcel
40 * with children StatsDimensionsValueParcels.
41 *
42 * \param dims vector of FieldValues stored by HashableDimensionKey
43 * \param index positions in dims vector to start reading children from
44 * \param depth level of parent parcel in the full StatsDimensionsValueParcel
45 * tree
46 */
47static void populateStatsDimensionsValueParcelChildren(StatsDimensionsValueParcel &parentParcel,
48 const vector<FieldValue>& dims, size_t& index,
49 int depth, int prefix) {
50 while (index < dims.size()) {
51 const FieldValue& dim = dims[index];
52 int fieldDepth = dim.mField.getDepth();
53 int fieldPrefix = dim.mField.getPrefix(depth);
54 StatsDimensionsValueParcel childParcel;
55 childParcel.field = dim.mField.getPosAtDepth(depth);
56 if (depth > 2) {
57 ALOGE("Depth > 2 not supported by StatsDimensionsValueParcel.");
58 return;
59 }
60 if (depth == fieldDepth && prefix == fieldPrefix) {
61 switch (dim.mValue.getType()) {
62 case INT:
63 childParcel.valueType = STATS_DIMENSIONS_VALUE_INT_TYPE;
64 childParcel.intValue = dim.mValue.int_value;
65 break;
66 case LONG:
67 childParcel.valueType = STATS_DIMENSIONS_VALUE_LONG_TYPE;
68 childParcel.longValue = dim.mValue.long_value;
69 break;
70 case FLOAT:
71 childParcel.valueType = STATS_DIMENSIONS_VALUE_FLOAT_TYPE;
72 childParcel.floatValue = dim.mValue.float_value;
73 break;
74 case STRING:
75 childParcel.valueType = STATS_DIMENSIONS_VALUE_STRING_TYPE;
76 childParcel.stringValue = String16(dim.mValue.str_value.c_str());
77 break;
78 default:
79 ALOGE("Encountered FieldValue with unsupported value type.");
80 break;
81 }
82 index++;
83 parentParcel.tupleValue.push_back(childParcel);
84 } else if (fieldDepth > depth && fieldPrefix == prefix) {
85 childParcel.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE;
86 populateStatsDimensionsValueParcelChildren(childParcel, dims, index, depth + 1,
87 dim.mField.getPrefix(depth + 1));
88 parentParcel.tupleValue.push_back(childParcel);
89 } else {
90 return;
91 }
92 }
93}
94
95StatsDimensionsValueParcel HashableDimensionKey::toStatsDimensionsValueParcel() const {
96 StatsDimensionsValueParcel parcel;
97 if (mValues.size() == 0) {
98 return parcel;
99 }
100
101 parcel.field = mValues[0].mField.getTag();
102 parcel.valueType = STATS_DIMENSIONS_VALUE_TUPLE_TYPE;
103
104 size_t index = 0;
105 populateStatsDimensionsValueParcelChildren(parcel, mValues, index, /*depth=*/0, /*prefix=*/0);
106 return parcel;
107}
108
Yao Chen8a8d16c2018-02-08 14:50:40 -0800109android::hash_t hashDimension(const HashableDimensionKey& value) {
110 android::hash_t hash = 0;
111 for (const auto& fieldValue : value.getValues()) {
112 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getField()));
113 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mField.getTag()));
114 hash = android::JenkinsHashMix(hash, android::hash_type((int)fieldValue.mValue.getType()));
115 switch (fieldValue.mValue.getType()) {
116 case INT:
117 hash = android::JenkinsHashMix(hash,
118 android::hash_type(fieldValue.mValue.int_value));
119 break;
120 case LONG:
121 hash = android::JenkinsHashMix(hash,
122 android::hash_type(fieldValue.mValue.long_value));
123 break;
124 case STRING:
125 hash = android::JenkinsHashMix(hash, static_cast<uint32_t>(std::hash<std::string>()(
126 fieldValue.mValue.str_value)));
127 break;
128 case FLOAT: {
Yangster-mac16b7ff72018-02-23 11:11:36 -0800129 hash = android::JenkinsHashMix(hash,
130 android::hash_type(fieldValue.mValue.float_value));
Yao Chen8a8d16c2018-02-08 14:50:40 -0800131 break;
Yangster-mac20877162017-12-22 17:19:39 -0800132 }
Yangster-macf5204922018-02-23 13:08:03 -0800133 default:
134 break;
Yangster-mac20877162017-12-22 17:19:39 -0800135 }
Yangster-mac20877162017-12-22 17:19:39 -0800136 }
137 return JenkinsHashWhiten(hash);
138}
139
tsaichristine69000e62019-10-18 17:34:52 -0700140bool filterValues(const Matcher& matcherField, const vector<FieldValue>& values,
141 FieldValue* output) {
tsaichristine10978642019-09-10 14:12:49 -0700142 for (const auto& value : values) {
143 if (value.mField.matches(matcherField)) {
tsaichristine69000e62019-10-18 17:34:52 -0700144 (*output) = value;
tsaichristine10978642019-09-10 14:12:49 -0700145 return true;
146 }
147 }
148 return false;
149}
150
Yangster13fb7e42018-03-07 17:30:49 -0800151bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
152 HashableDimensionKey* output) {
Yangster-mace06cfd72018-03-10 23:22:59 -0800153 size_t num_matches = 0;
154 for (const auto& value : values) {
155 for (size_t i = 0; i < matcherFields.size(); ++i) {
156 const auto& matcher = matcherFields[i];
Yangster13fb7e42018-03-07 17:30:49 -0800157 if (value.mField.matches(matcher)) {
158 output->addValue(value);
Yangster-mace06cfd72018-03-10 23:22:59 -0800159 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
160 output->mutableValue(num_matches)->mField.setField(
161 value.mField.getField() & matcher.mMask);
Yangster-macf5204922018-02-23 13:08:03 -0800162 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -0800163 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800164 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800165 }
Yangster-mace06cfd72018-03-10 23:22:59 -0800166 return num_matches > 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800167}
168
169void filterGaugeValues(const std::vector<Matcher>& matcherFields,
170 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
171 for (const auto& field : matcherFields) {
172 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800173 if (value.mField.matches(field)) {
174 output->push_back(value);
175 }
176 }
177 }
178}
179
Yangster-mac53928882018-02-25 23:02:56 -0800180void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
181 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800182 HashableDimensionKey* conditionDimension) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800183 // Get the dimension first by using dimension from what.
Yangster-mac53928882018-02-25 23:02:56 -0800184 filterValues(links.metricFields, eventValues, conditionDimension);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800185
Yangster13fb7e42018-03-07 17:30:49 -0800186 size_t count = conditionDimension->getValues().size();
187 if (count != links.conditionFields.size()) {
Yangster13fb7e42018-03-07 17:30:49 -0800188 return;
189 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800190
Yangster13fb7e42018-03-07 17:30:49 -0800191 for (size_t i = 0; i < count; i++) {
192 conditionDimension->mutableValue(i)->mField.setField(
tsaichristine69000e62019-10-18 17:34:52 -0700193 links.conditionFields[i].mMatcher.getField());
Yangster13fb7e42018-03-07 17:30:49 -0800194 conditionDimension->mutableValue(i)->mField.setTag(
tsaichristine69000e62019-10-18 17:34:52 -0700195 links.conditionFields[i].mMatcher.getTag());
196 }
197}
198
199void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
200 HashableDimensionKey* statePrimaryKey) {
201 // First, get the dimension from the event using the "what" fields from the
202 // MetricStateLinks.
203 filterValues(link.metricFields, eventValues, statePrimaryKey);
204
205 // Then check that the statePrimaryKey size equals the number of state fields
206 size_t count = statePrimaryKey->getValues().size();
207 if (count != link.stateFields.size()) {
208 return;
209 }
210
211 // For each dimension Value in the statePrimaryKey, set the field and tag
212 // using the state atom fields from MetricStateLinks.
213 for (size_t i = 0; i < count; i++) {
214 statePrimaryKey->mutableValue(i)->mField.setField(link.stateFields[i].mMatcher.getField());
215 statePrimaryKey->mutableValue(i)->mField.setTag(link.stateFields[i].mMatcher.getTag());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800216 }
217}
218
219bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
220 if (s1.size() != s2.size()) {
221 return s1.size() < s2.size();
222 }
223
224 size_t count = s1.size();
225 for (size_t i = 0; i < count; i++) {
226 if (s1[i] != s2[i]) {
227 return s1[i] < s2[i];
228 }
229 }
230 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800231}
232
tsaichristinec876b492019-12-10 13:47:05 -0800233bool HashableDimensionKey::operator!=(const HashableDimensionKey& that) const {
234 return !((*this) == that);
235}
236
Yangster-mac20877162017-12-22 17:19:39 -0800237bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800238 if (mValues.size() != that.getValues().size()) {
239 return false;
240 }
241 size_t count = mValues.size();
242 for (size_t i = 0; i < count; i++) {
243 if (mValues[i] != (that.getValues())[i]) {
244 return false;
245 }
246 }
247 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800248};
249
250bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800251 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800252};
253
Yao Chen8a8d16c2018-02-08 14:50:40 -0800254bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
255 if (mValues.size() < that.getValues().size()) {
256 return false;
257 }
258
259 if (mValues.size() == that.getValues().size()) {
260 return (*this) == that;
261 }
262
263 for (const auto& value : that.getValues()) {
264 bool found = false;
265 for (const auto& myValue : mValues) {
266 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
267 found = true;
268 break;
269 }
270 }
271 if (!found) {
272 return false;
273 }
274 }
275
276 return true;
277}
278
279string HashableDimensionKey::toString() const {
280 std::string output;
281 for (const auto& value : mValues) {
282 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
283 value.mValue.toString().c_str());
284 }
285 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800286}
287
288bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
289 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
tsaichristine69000e62019-10-18 17:34:52 -0700290 mStateValuesKey == that.getStateValuesKey();
Yangster-mac93694462018-01-22 20:49:31 -0800291};
292
Yao Chen8a8d16c2018-02-08 14:50:40 -0800293string MetricDimensionKey::toString() const {
tsaichristine69000e62019-10-18 17:34:52 -0700294 return mDimensionKeyInWhat.toString() + mStateValuesKey.toString();
Yao Chen8a8d16c2018-02-08 14:50:40 -0800295}
296
Yangster-mac93694462018-01-22 20:49:31 -0800297bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800298 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
299 return true;
300 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
301 return false;
302 }
Yangster-mac93694462018-01-22 20:49:31 -0800303
tsaichristine69000e62019-10-18 17:34:52 -0700304 return mStateValuesKey < that.getStateValuesKey();
Chenjie Yu80f91122018-01-31 20:24:50 -0800305}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800306
Yao Chend5aa01b32017-12-19 16:46:36 -0800307} // namespace statsd
308} // namespace os
Yao Chen5bfffb52018-06-21 16:58:51 -0700309} // namespace android