blob: bd011005a301e3bd06fd999b853b98a035e2de96 [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
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080019#include <aidl/android/os/StatsDimensionsValueParcel.h>
Yao Chend5aa01b32017-12-19 16:46:36 -080020#include <utils/JenkinsHash.h>
Yao Chen8a8d16c2018-02-08 14:50:40 -080021#include <vector>
Yao Chen8a8d16c2018-02-08 14:50:40 -080022#include "android-base/stringprintf.h"
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080023#include "FieldValue.h"
Yao Chen8a8d16c2018-02-08 14:50:40 -080024#include "logd/LogEvent.h"
Yao Chend5aa01b32017-12-19 16:46:36 -080025
26namespace android {
27namespace os {
28namespace statsd {
29
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080030using ::aidl::android::os::StatsDimensionsValueParcel;
Yao Chen8a8d16c2018-02-08 14:50:40 -080031
32struct Metric2Condition {
33 int64_t conditionId;
34 std::vector<Matcher> metricFields;
35 std::vector<Matcher> conditionFields;
36};
37
tsaichristine69000e62019-10-18 17:34:52 -070038struct Metric2State {
39 int32_t stateAtomId;
40 std::vector<Matcher> metricFields;
41 std::vector<Matcher> stateFields;
42};
43
Yao Chend5aa01b32017-12-19 16:46:36 -080044class HashableDimensionKey {
45public:
Yao Chen8a8d16c2018-02-08 14:50:40 -080046 explicit HashableDimensionKey(const std::vector<FieldValue>& values) {
47 mValues = values;
48 }
Yao Chend5aa01b32017-12-19 16:46:36 -080049
Yangster-mac932ecec2018-02-01 10:23:52 -080050 HashableDimensionKey() {};
Yao Chend5aa01b32017-12-19 16:46:36 -080051
Yao Chen8a8d16c2018-02-08 14:50:40 -080052 HashableDimensionKey(const HashableDimensionKey& that) : mValues(that.getValues()){};
Yao Chend5aa01b32017-12-19 16:46:36 -080053
Yao Chen8a8d16c2018-02-08 14:50:40 -080054 inline void addValue(const FieldValue& value) {
55 mValues.push_back(value);
56 }
57
58 inline const std::vector<FieldValue>& getValues() const {
59 return mValues;
60 }
61
62 inline std::vector<FieldValue>* mutableValues() {
63 return &mValues;
64 }
65
66 inline FieldValue* mutableValue(size_t i) {
67 if (i >= 0 && i < mValues.size()) {
68 return &(mValues[i]);
69 }
70 return nullptr;
71 }
Yao Chend5aa01b32017-12-19 16:46:36 -080072
Ruchir Rastogi56da4c32020-02-03 18:53:24 -080073 StatsDimensionsValueParcel toStatsDimensionsValueParcel() const;
74
Yao Chend5aa01b32017-12-19 16:46:36 -080075 std::string toString() const;
76
tsaichristinec876b492019-12-10 13:47:05 -080077 bool operator!=(const HashableDimensionKey& that) const;
78
Yao Chend5aa01b32017-12-19 16:46:36 -080079 bool operator==(const HashableDimensionKey& that) const;
80
81 bool operator<(const HashableDimensionKey& that) const;
82
Yao Chen8a8d16c2018-02-08 14:50:40 -080083 bool contains(const HashableDimensionKey& that) const;
Yao Chend5aa01b32017-12-19 16:46:36 -080084
85private:
Yao Chen8a8d16c2018-02-08 14:50:40 -080086 std::vector<FieldValue> mValues;
Yao Chend5aa01b32017-12-19 16:46:36 -080087};
88
Yangster-mac93694462018-01-22 20:49:31 -080089class MetricDimensionKey {
tsaichristine69000e62019-10-18 17:34:52 -070090public:
Yangster-mac93694462018-01-22 20:49:31 -080091 explicit MetricDimensionKey(const HashableDimensionKey& dimensionKeyInWhat,
tsaichristine69000e62019-10-18 17:34:52 -070092 const HashableDimensionKey& stateValuesKey)
93 : mDimensionKeyInWhat(dimensionKeyInWhat), mStateValuesKey(stateValuesKey){};
Yangster-mac93694462018-01-22 20:49:31 -080094
95 MetricDimensionKey(){};
96
97 MetricDimensionKey(const MetricDimensionKey& that)
98 : mDimensionKeyInWhat(that.getDimensionKeyInWhat()),
tsaichristine69000e62019-10-18 17:34:52 -070099 mStateValuesKey(that.getStateValuesKey()){};
Yangster-mac93694462018-01-22 20:49:31 -0800100
101 MetricDimensionKey& operator=(const MetricDimensionKey& from) = default;
102
103 std::string toString() const;
104
105 inline const HashableDimensionKey& getDimensionKeyInWhat() const {
106 return mDimensionKeyInWhat;
107 }
108
tsaichristine69000e62019-10-18 17:34:52 -0700109 inline const HashableDimensionKey& getStateValuesKey() const {
110 return mStateValuesKey;
Yangster-mac93694462018-01-22 20:49:31 -0800111 }
112
tsaichristine1449fa42020-01-02 12:12:05 -0800113 inline HashableDimensionKey* getMutableStateValuesKey() {
114 return &mStateValuesKey;
115 }
116
tsaichristine69000e62019-10-18 17:34:52 -0700117 inline void setStateValuesKey(const HashableDimensionKey& key) {
118 mStateValuesKey = key;
Yangster13fb7e42018-03-07 17:30:49 -0800119 }
120
tsaichristine69000e62019-10-18 17:34:52 -0700121 bool hasStateValuesKey() const {
122 return mStateValuesKey.getValues().size() > 0;
Yangster-mac93694462018-01-22 20:49:31 -0800123 }
124
125 bool operator==(const MetricDimensionKey& that) const;
126
127 bool operator<(const MetricDimensionKey& that) const;
128
tsaichristine69000e62019-10-18 17:34:52 -0700129private:
130 HashableDimensionKey mDimensionKeyInWhat;
131 HashableDimensionKey mStateValuesKey;
Yangster-mac93694462018-01-22 20:49:31 -0800132};
133
Yao Chen8a8d16c2018-02-08 14:50:40 -0800134android::hash_t hashDimension(const HashableDimensionKey& key);
Yangster-mac93694462018-01-22 20:49:31 -0800135
Yao Chen8a8d16c2018-02-08 14:50:40 -0800136/**
tsaichristine10978642019-09-10 14:12:49 -0700137 * Returns true if a FieldValue field matches the matcher field.
138 * The value of the FieldValue is output.
139 */
140bool filterValues(const Matcher& matcherField, const std::vector<FieldValue>& values,
tsaichristine69000e62019-10-18 17:34:52 -0700141 FieldValue* output);
tsaichristine10978642019-09-10 14:12:49 -0700142
143/**
Yao Chen8a8d16c2018-02-08 14:50:40 -0800144 * Creating HashableDimensionKeys from FieldValues using matcher.
145 *
Yangster-mace06cfd72018-03-10 23:22:59 -0800146 * This function may make modifications to the Field if the matcher has Position=FIRST,LAST or ALL
147 * in it. This is because: for example, when we create dimension from last uid in attribution chain,
Yao Chen8a8d16c2018-02-08 14:50:40 -0800148 * In one event, uid 1000 is at position 5 and it's the last
149 * In another event, uid 1000 is at position 6, and it's the last
150 * these 2 events should be mapped to the same dimension. So we will remove the original position
151 * from the dimension key for the uid field (by applying 0x80 bit mask).
152 */
153bool filterValues(const std::vector<Matcher>& matcherFields, const std::vector<FieldValue>& values,
Yangster13fb7e42018-03-07 17:30:49 -0800154 HashableDimensionKey* output);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800155
156/**
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -0700157 * Creating HashableDimensionKeys from State Primary Keys in FieldValues.
158 *
159 * This function may make modifications to the Field if the matcher has Position=FIRST,LAST or ALL
160 * in it. This is because: for example, when we create dimension from last uid in attribution chain,
161 * In one event, uid 1000 is at position 5 and it's the last
162 * In another event, uid 1000 is at position 6, and it's the last
163 * these 2 events should be mapped to the same dimension. So we will remove the original position
164 * from the dimension key for the uid field (by applying 0x80 bit mask).
165 */
166bool filterPrimaryKey(const std::vector<FieldValue>& values, HashableDimensionKey* output);
167
168/**
Yao Chen8a8d16c2018-02-08 14:50:40 -0800169 * Filter the values from FieldValues using the matchers.
170 *
171 * In contrast to the above function, this function will not do any modification to the original
172 * data. Considering it as taking a snapshot on the atom event.
173 */
174void filterGaugeValues(const std::vector<Matcher>& matchers, const std::vector<FieldValue>& values,
175 std::vector<FieldValue>* output);
176
Yangster-mac53928882018-02-25 23:02:56 -0800177void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
178 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800179 HashableDimensionKey* conditionDimension);
Yangster-mac20877162017-12-22 17:19:39 -0800180
tsaichristine69000e62019-10-18 17:34:52 -0700181/**
182 * Get dimension values using metric's "what" fields and fill statePrimaryKey's
183 * mField information using "state" fields.
184 */
185void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
186 HashableDimensionKey* statePrimaryKey);
187
tsaichristine1449fa42020-01-02 12:12:05 -0800188/**
189 * Returns true if the primaryKey values are a subset of the whatKey values.
190 * The values from the primaryKey come from the state atom, so we need to
191 * check that a link exists between the state atom field and what atom field.
192 *
193 * Example:
194 * whatKey = [Atom: 10, {uid: 1005, wakelock_name: "compose"}]
195 * statePrimaryKey = [Atom: 27, {uid: 1005}]
196 * Returns true IF one of the Metric2State links Atom 10's uid to Atom 27's uid
197 *
198 * Example:
199 * whatKey = [Atom: 10, {uid: 1005, wakelock_name: "compose"}]
200 * statePrimaryKey = [Atom: 59, {uid: 1005, package_name: "system"}]
201 * Returns false
202 */
203bool containsLinkedStateValues(const HashableDimensionKey& whatKey,
204 const HashableDimensionKey& primaryKey,
205 const std::vector<Metric2State>& stateLinks,
206 const int32_t stateAtomId);
207
208/**
209 * Returns true if there is a Metric2State link that links the stateField and
210 * the metricField (they are equal fields from different atoms).
211 */
212bool linked(const std::vector<Metric2State>& stateLinks, const int32_t stateAtomId,
213 const Field& stateField, const Field& metricField);
Yao Chend5aa01b32017-12-19 16:46:36 -0800214} // namespace statsd
215} // namespace os
216} // namespace android
217
218namespace std {
219
220using android::os::statsd::HashableDimensionKey;
Yangster-mac93694462018-01-22 20:49:31 -0800221using android::os::statsd::MetricDimensionKey;
Yao Chend5aa01b32017-12-19 16:46:36 -0800222
223template <>
224struct hash<HashableDimensionKey> {
225 std::size_t operator()(const HashableDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800226 return hashDimension(key);
Yao Chend5aa01b32017-12-19 16:46:36 -0800227 }
228};
229
Yangster-mac93694462018-01-22 20:49:31 -0800230template <>
231struct hash<MetricDimensionKey> {
232 std::size_t operator()(const MetricDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800233 android::hash_t hash = hashDimension(key.getDimensionKeyInWhat());
tsaichristine69000e62019-10-18 17:34:52 -0700234 hash = android::JenkinsHashMix(hash, hashDimension(key.getStateValuesKey()));
Yangster-mac93694462018-01-22 20:49:31 -0800235 return android::JenkinsHashWhiten(hash);
236 }
237};
tsaichristine10978642019-09-10 14:12:49 -0700238} // namespace std