blob: af8b3af6ea610e48ee744a5dc35d3766c0c9d68d [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
Yangster13fb7e42018-03-07 17:30:49 -080062bool filterValues(const vector<Matcher>& matcherFields, const vector<FieldValue>& values,
63 HashableDimensionKey* output) {
Yangster-mace06cfd72018-03-10 23:22:59 -080064 size_t num_matches = 0;
65 for (const auto& value : values) {
66 for (size_t i = 0; i < matcherFields.size(); ++i) {
67 const auto& matcher = matcherFields[i];
Yangster13fb7e42018-03-07 17:30:49 -080068 if (value.mField.matches(matcher)) {
69 output->addValue(value);
Yangster-mace06cfd72018-03-10 23:22:59 -080070 output->mutableValue(num_matches)->mField.setTag(value.mField.getTag());
71 output->mutableValue(num_matches)->mField.setField(
72 value.mField.getField() & matcher.mMask);
Yangster-macf5204922018-02-23 13:08:03 -080073 num_matches++;
Yangster-mac20877162017-12-22 17:19:39 -080074 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080075 }
Yangster-mac7ba8fc32018-01-24 16:16:46 -080076 }
Yangster-mace06cfd72018-03-10 23:22:59 -080077 return num_matches > 0;
Yao Chen8a8d16c2018-02-08 14:50:40 -080078}
79
80void filterGaugeValues(const std::vector<Matcher>& matcherFields,
81 const std::vector<FieldValue>& values, std::vector<FieldValue>* output) {
82 for (const auto& field : matcherFields) {
83 for (const auto& value : values) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080084 if (value.mField.matches(field)) {
85 output->push_back(value);
86 }
87 }
88 }
89}
90
Yangster-mac53928882018-02-25 23:02:56 -080091void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
92 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -080093 HashableDimensionKey* conditionDimension) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080094 // Get the dimension first by using dimension from what.
Yangster-mac53928882018-02-25 23:02:56 -080095 filterValues(links.metricFields, eventValues, conditionDimension);
Yao Chen8a8d16c2018-02-08 14:50:40 -080096
Yangster13fb7e42018-03-07 17:30:49 -080097 size_t count = conditionDimension->getValues().size();
98 if (count != links.conditionFields.size()) {
99 // ALOGE("WTF condition link is bad");
100 return;
101 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800102
Yangster13fb7e42018-03-07 17:30:49 -0800103 for (size_t i = 0; i < count; i++) {
104 conditionDimension->mutableValue(i)->mField.setField(
105 links.conditionFields[i].mMatcher.getField());
106 conditionDimension->mutableValue(i)->mField.setTag(
107 links.conditionFields[i].mMatcher.getTag());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800108 }
109}
110
111bool LessThan(const vector<FieldValue>& s1, const vector<FieldValue>& s2) {
112 if (s1.size() != s2.size()) {
113 return s1.size() < s2.size();
114 }
115
116 size_t count = s1.size();
117 for (size_t i = 0; i < count; i++) {
118 if (s1[i] != s2[i]) {
119 return s1[i] < s2[i];
120 }
121 }
122 return false;
Yangster-mac7ba8fc32018-01-24 16:16:46 -0800123}
124
Yangster-mac20877162017-12-22 17:19:39 -0800125bool HashableDimensionKey::operator==(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800126 if (mValues.size() != that.getValues().size()) {
127 return false;
128 }
129 size_t count = mValues.size();
130 for (size_t i = 0; i < count; i++) {
131 if (mValues[i] != (that.getValues())[i]) {
132 return false;
133 }
134 }
135 return true;
Yao Chend5aa01b32017-12-19 16:46:36 -0800136};
137
138bool HashableDimensionKey::operator<(const HashableDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800139 return LessThan(getValues(), that.getValues());
Yao Chend5aa01b32017-12-19 16:46:36 -0800140};
141
Yao Chen8a8d16c2018-02-08 14:50:40 -0800142bool HashableDimensionKey::contains(const HashableDimensionKey& that) const {
143 if (mValues.size() < that.getValues().size()) {
144 return false;
145 }
146
147 if (mValues.size() == that.getValues().size()) {
148 return (*this) == that;
149 }
150
151 for (const auto& value : that.getValues()) {
152 bool found = false;
153 for (const auto& myValue : mValues) {
154 if (value.mField == myValue.mField && value.mValue == myValue.mValue) {
155 found = true;
156 break;
157 }
158 }
159 if (!found) {
160 return false;
161 }
162 }
163
164 return true;
165}
166
167string HashableDimensionKey::toString() const {
168 std::string output;
169 for (const auto& value : mValues) {
170 output += StringPrintf("(%d)%#x->%s ", value.mField.getTag(), value.mField.getField(),
171 value.mValue.toString().c_str());
172 }
173 return output;
Yangster-mac93694462018-01-22 20:49:31 -0800174}
175
176bool MetricDimensionKey::operator==(const MetricDimensionKey& that) const {
177 return mDimensionKeyInWhat == that.getDimensionKeyInWhat() &&
Yao Chen8a8d16c2018-02-08 14:50:40 -0800178 mDimensionKeyInCondition == that.getDimensionKeyInCondition();
Yangster-mac93694462018-01-22 20:49:31 -0800179};
180
Yao Chen8a8d16c2018-02-08 14:50:40 -0800181string MetricDimensionKey::toString() const {
182 return mDimensionKeyInWhat.toString() + mDimensionKeyInCondition.toString();
183}
184
Yangster-mac93694462018-01-22 20:49:31 -0800185bool MetricDimensionKey::operator<(const MetricDimensionKey& that) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800186 if (mDimensionKeyInWhat < that.getDimensionKeyInWhat()) {
187 return true;
188 } else if (that.getDimensionKeyInWhat() < mDimensionKeyInWhat) {
189 return false;
190 }
Yangster-mac93694462018-01-22 20:49:31 -0800191
Yao Chen8a8d16c2018-02-08 14:50:40 -0800192 return mDimensionKeyInCondition < that.getDimensionKeyInCondition();
Chenjie Yu80f91122018-01-31 20:24:50 -0800193}
Yao Chen8a8d16c2018-02-08 14:50:40 -0800194
Yao Chend5aa01b32017-12-19 16:46:36 -0800195} // namespace statsd
196} // namespace os
Yao Chen5bfffb52018-06-21 16:58:51 -0700197} // namespace android