blob: 05acef8b219db558d7892e987ca590d7e763ca1b [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
Yangster13fb7e42018-03-07 17:30:49 -080060bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
61 HashableDimensionKey* output) {
Yangster-mace06cfd72018-03-10 23:22:59 -080062 size_t num_matches = 0;
63 for (const auto& value : values) {
64 for (size_t i = 0; i < matcherFields.size(); ++i) {
65 const auto& matcher = matcherFields[i];
Yangster13fb7e42018-03-07 17:30:49 -080066 if (value.mField.matches(matcher)) {
67 output->addValue(value);
Yangster-mace06cfd72018-03-10 23:22:59 -080068 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
69 output->mutableValue(num_matches)->mField.setField(
70 value.mField.getField() & matcher.mMask);
Yangster-macf5204922018-02-23 13:08:03 -080071 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -080072 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080073 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -080074 }
Yangster-mace06cfd72018-03-10 23:22:59 -080075 return num_matches > 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -080076}
77
78void filterGaugeValues(const std::vector<Matcher>& matcherFields,
79 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
80 for (const auto& field : matcherFields) {
81 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080082 if (value.mField.matches(field)) {
83 output->push_back(value);
84 }
85 }
86 }
87}
88
Yangster-mac53928882018-02-25 23:02:56 -080089void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
90 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -080091 HashableDimensionKey* conditionDimension) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080092 // Get the dimension first by using dimension from what.
Yangster-mac53928882018-02-25 23:02:56 -080093 filterValues(links.metricFields, eventValues, conditionDimension);
Yao Chen8a8d16c2018-02-08 14:50:40 -080094
Yangster13fb7e42018-03-07 17:30:49 -080095 size_t count = conditionDimension->getValues().size();
96 if (count != links.conditionFields.size()) {
97 // ALOGE("WTF condition link is bad");
98 return;
99 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800100
Yangster13fb7e42018-03-07 17:30:49 -0800101 for (size_t i = 0; i < count; i++) {
102 conditionDimension->mutableValue(i)->mField.setField(
103 links.conditionFields[i].mMatcher.getField());
104 conditionDimension->mutableValue(i)->mField.setTag(
105 links.conditionFields[i].mMatcher.getTag());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800106 }
107}
108
109bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
110 if (s1.size() != s2.size()) {
111 return s1.size() < s2.size();
112 }
113
114 size_t count = s1.size();
115 for (size_t i = 0; i < count; i++) {
116 if (s1[i] != s2[i]) {
117 return s1[i] < s2[i];
118 }
119 }
120 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800121}
122
Yangster-mac20877162017-12-22 17:19:39 -0800123bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800124 if (mValues.size() != that.getValues().size()) {
125 return false;
126 }
127 size_t count = mValues.size();
128 for (size_t i = 0; i < count; i++) {
129 if (mValues[i] != (that.getValues())[i]) {
130 return false;
131 }
132 }
133 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800134};
135
136bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800137 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800138};
139
Yao Chen8a8d16c2018-02-08 14:50:40 -0800140bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
141 if (mValues.size() < that.getValues().size()) {
142 return false;
143 }
144
145 if (mValues.size() == that.getValues().size()) {
146 return (*this) == that;
147 }
148
149 for (const auto& value : that.getValues()) {
150 bool found = false;
151 for (const auto& myValue : mValues) {
152 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
153 found = true;
154 break;
155 }
156 }
157 if (!found) {
158 return false;
159 }
160 }
161
162 return true;
163}
164
165string HashableDimensionKey::toString() const {
166 std::string output;
167 for (const auto& value : mValues) {
168 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
169 value.mValue.toString().c_str());
170 }
171 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800172}
173
174bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
175 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
Yao Chen8a8d16c2018-02-08 14:50:40 -0800176 mDimensionKeyInCondition == that.getDimensionKeyInCondition();
Yangster-mac93694462018-01-22 20:49:31 -0800177};
178
Yao Chen8a8d16c2018-02-08 14:50:40 -0800179string MetricDimensionKey::toString() const {
180 return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString();
181}
182
Yangster-mac93694462018-01-22 20:49:31 -0800183bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800184 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
185 return true;
186 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
187 return false;
188 }
Yangster-mac93694462018-01-22 20:49:31 -0800189
Yao Chen8a8d16c2018-02-08 14:50:40 -0800190 return mDimensionKeyInCondition < that.getDimensionKeyInCondition();
Chenjie Yu80f91122018-01-31 20:24:50 -0800191}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800192
Yao Chend5aa01b32017-12-19 16:46:36 -0800193} // namespace statsd
194} // namespace os
Yao Chen5bfffb52018-06-21 16:58:51 -0700195} // namespace android