blob: b9b86ce13c8bb72a2619bd31efb396974cad47ae [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 */
16
17#pragma once
18
19#include <utils/JenkinsHash.h>
Yao Chen8a8d16c2018-02-08 14:50:40 -080020#include <vector>
21#include "FieldValue.h"
22#include "android-base/stringprintf.h"
23#include "logd/LogEvent.h"
Yao Chend5aa01b32017-12-19 16:46:36 -080024
25namespace android {
26namespace os {
27namespace statsd {
28
Yao Chen8a8d16c2018-02-08 14:50:40 -080029using android::base::StringPrintf;
30
31struct Metric2Condition {
32 int64_t conditionId;
33 std::vector<Matcher> metricFields;
34 std::vector<Matcher> conditionFields;
35};
36
tsaichristine69000e62019-10-18 17:34:52 -070037struct Metric2State {
38 int32_t stateAtomId;
39 std::vector<Matcher> metricFields;
40 std::vector<Matcher> stateFields;
41};
42
Yao Chend5aa01b32017-12-19 16:46:36 -080043class HashableDimensionKey {
44public:
Yao Chen8a8d16c2018-02-08 14:50:40 -080045 explicit HashableDimensionKey(const std::vector<FieldValue>& values) {
46 mValues = values;
47 }
Yao Chend5aa01b32017-12-19 16:46:36 -080048
Yangster-mac932ecec2018-02-01 10:23:52 -080049 HashableDimensionKey() {};
Yao Chend5aa01b32017-12-19 16:46:36 -080050
Yao Chen8a8d16c2018-02-08 14:50:40 -080051 HashableDimensionKey(const HashableDimensionKey& that) : mValues(that.getValues()){};
Yao Chend5aa01b32017-12-19 16:46:36 -080052
Yao Chen8a8d16c2018-02-08 14:50:40 -080053 inline void addValue(const FieldValue& value) {
54 mValues.push_back(value);
55 }
56
57 inline const std::vector<FieldValue>& getValues() const {
58 return mValues;
59 }
60
61 inline std::vector<FieldValue>* mutableValues() {
62 return &mValues;
63 }
64
65 inline FieldValue* mutableValue(size_t i) {
66 if (i >= 0 && i < mValues.size()) {
67 return &(mValues[i]);
68 }
69 return nullptr;
70 }
Yao Chend5aa01b32017-12-19 16:46:36 -080071
72 std::string toString() const;
73
Yao Chend5aa01b32017-12-19 16:46:36 -080074 bool operator==(const HashableDimensionKey& that) const;
75
76 bool operator<(const HashableDimensionKey& that) const;
77
Yao Chen8a8d16c2018-02-08 14:50:40 -080078 bool contains(const HashableDimensionKey& that) const;
Yao Chend5aa01b32017-12-19 16:46:36 -080079
80private:
Yao Chen8a8d16c2018-02-08 14:50:40 -080081 std::vector<FieldValue> mValues;
Yao Chend5aa01b32017-12-19 16:46:36 -080082};
83
Yangster-mac93694462018-01-22 20:49:31 -080084class MetricDimensionKey {
tsaichristine69000e62019-10-18 17:34:52 -070085public:
Yangster-mac93694462018-01-22 20:49:31 -080086 explicit MetricDimensionKey(const HashableDimensionKey& dimensionKeyInWhat,
tsaichristine69000e62019-10-18 17:34:52 -070087 const HashableDimensionKey& stateValuesKey)
88 : mDimensionKeyInWhat(dimensionKeyInWhat), mStateValuesKey(stateValuesKey){};
Yangster-mac93694462018-01-22 20:49:31 -080089
90 MetricDimensionKey(){};
91
92 MetricDimensionKey(const MetricDimensionKey& that)
93 : mDimensionKeyInWhat(that.getDimensionKeyInWhat()),
tsaichristine69000e62019-10-18 17:34:52 -070094 mStateValuesKey(that.getStateValuesKey()){};
Yangster-mac93694462018-01-22 20:49:31 -080095
96 MetricDimensionKey& operator=(const MetricDimensionKey& from) = default;
97
98 std::string toString() const;
99
100 inline const HashableDimensionKey& getDimensionKeyInWhat() const {
101 return mDimensionKeyInWhat;
102 }
103
tsaichristine69000e62019-10-18 17:34:52 -0700104 inline const HashableDimensionKey& getStateValuesKey() const {
105 return mStateValuesKey;
Yangster-mac93694462018-01-22 20:49:31 -0800106 }
107
tsaichristine69000e62019-10-18 17:34:52 -0700108 inline void setStateValuesKey(const HashableDimensionKey& key) {
109 mStateValuesKey = key;
Yangster13fb7e42018-03-07 17:30:49 -0800110 }
111
tsaichristine69000e62019-10-18 17:34:52 -0700112 bool hasStateValuesKey() const {
113 return mStateValuesKey.getValues().size() > 0;
Yangster-mac93694462018-01-22 20:49:31 -0800114 }
115
116 bool operator==(const MetricDimensionKey& that) const;
117
118 bool operator<(const MetricDimensionKey& that) const;
119
tsaichristine69000e62019-10-18 17:34:52 -0700120private:
121 HashableDimensionKey mDimensionKeyInWhat;
122 HashableDimensionKey mStateValuesKey;
Yangster-mac93694462018-01-22 20:49:31 -0800123};
124
Yao Chen8a8d16c2018-02-08 14:50:40 -0800125android::hash_t hashDimension(const HashableDimensionKey& key);
Yangster-mac93694462018-01-22 20:49:31 -0800126
Yao Chen8a8d16c2018-02-08 14:50:40 -0800127/**
tsaichristine10978642019-09-10 14:12:49 -0700128 * Returns true if a FieldValue field matches the matcher field.
129 * The value of the FieldValue is output.
130 */
131bool filterValues(const Matcher& matcherField, const std::vector<FieldValue>& values,
tsaichristine69000e62019-10-18 17:34:52 -0700132 FieldValue* output);
tsaichristine10978642019-09-10 14:12:49 -0700133
134/**
Yao Chen8a8d16c2018-02-08 14:50:40 -0800135 * Creating HashableDimensionKeys from FieldValues using matcher.
136 *
Yangster-mace06cfd72018-03-10 23:22:59 -0800137 * This function may make modifications to the Field if the matcher has Position=FIRST,LAST or ALL
138 * in it. This is because: for example, when we create dimension from last uid in attribution chain,
Yao Chen8a8d16c2018-02-08 14:50:40 -0800139 * In one event, uid 1000 is at position 5 and it's the last
140 * In another event, uid 1000 is at position 6, and it's the last
141 * these 2 events should be mapped to the same dimension. So we will remove the original position
142 * from the dimension key for the uid field (by applying 0x80 bit mask).
143 */
144bool filterValues(const std::vector<Matcher>& matcherFields, const std::vector<FieldValue>& values,
Yangster13fb7e42018-03-07 17:30:49 -0800145 HashableDimensionKey* output);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800146
147/**
148 * Filter the values from FieldValues using the matchers.
149 *
150 * In contrast to the above function, this function will not do any modification to the original
151 * data. Considering it as taking a snapshot on the atom event.
152 */
153void filterGaugeValues(const std::vector<Matcher>& matchers, const std::vector<FieldValue>& values,
154 std::vector<FieldValue>* output);
155
Yangster-mac53928882018-02-25 23:02:56 -0800156void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
157 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800158 HashableDimensionKey* conditionDimension);
Yangster-mac20877162017-12-22 17:19:39 -0800159
tsaichristine69000e62019-10-18 17:34:52 -0700160/**
161 * Get dimension values using metric's "what" fields and fill statePrimaryKey's
162 * mField information using "state" fields.
163 */
164void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
165 HashableDimensionKey* statePrimaryKey);
166
Yao Chend5aa01b32017-12-19 16:46:36 -0800167} // namespace statsd
168} // namespace os
169} // namespace android
170
171namespace std {
172
173using android::os::statsd::HashableDimensionKey;
Yangster-mac93694462018-01-22 20:49:31 -0800174using android::os::statsd::MetricDimensionKey;
Yao Chend5aa01b32017-12-19 16:46:36 -0800175
176template <>
177struct hash<HashableDimensionKey> {
178 std::size_t operator()(const HashableDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800179 return hashDimension(key);
Yao Chend5aa01b32017-12-19 16:46:36 -0800180 }
181};
182
Yangster-mac93694462018-01-22 20:49:31 -0800183template <>
184struct hash<MetricDimensionKey> {
185 std::size_t operator()(const MetricDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800186 android::hash_t hash = hashDimension(key.getDimensionKeyInWhat());
tsaichristine69000e62019-10-18 17:34:52 -0700187 hash = android::JenkinsHashMix(hash, hashDimension(key.getStateValuesKey()));
Yangster-mac93694462018-01-22 20:49:31 -0800188 return android::JenkinsHashWhiten(hash);
189 }
190};
tsaichristine10978642019-09-10 14:12:49 -0700191} // namespace std