blob: 654e1358f2a1c46255900aa30502fcd75dcf2cec [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
tsaichristinec876b492019-12-10 13:47:05 -080074 bool operator!=(const HashableDimensionKey& that) const;
75
Yao Chend5aa01b32017-12-19 16:46:36 -080076 bool operator==(const HashableDimensionKey& that) const;
77
78 bool operator<(const HashableDimensionKey& that) const;
79
Yao Chen8a8d16c2018-02-08 14:50:40 -080080 bool contains(const HashableDimensionKey& that) const;
Yao Chend5aa01b32017-12-19 16:46:36 -080081
82private:
Yao Chen8a8d16c2018-02-08 14:50:40 -080083 std::vector<FieldValue> mValues;
Yao Chend5aa01b32017-12-19 16:46:36 -080084};
85
Yangster-mac93694462018-01-22 20:49:31 -080086class MetricDimensionKey {
tsaichristine69000e62019-10-18 17:34:52 -070087public:
Yangster-mac93694462018-01-22 20:49:31 -080088 explicit MetricDimensionKey(const HashableDimensionKey& dimensionKeyInWhat,
tsaichristine69000e62019-10-18 17:34:52 -070089 const HashableDimensionKey& stateValuesKey)
90 : mDimensionKeyInWhat(dimensionKeyInWhat), mStateValuesKey(stateValuesKey){};
Yangster-mac93694462018-01-22 20:49:31 -080091
92 MetricDimensionKey(){};
93
94 MetricDimensionKey(const MetricDimensionKey& that)
95 : mDimensionKeyInWhat(that.getDimensionKeyInWhat()),
tsaichristine69000e62019-10-18 17:34:52 -070096 mStateValuesKey(that.getStateValuesKey()){};
Yangster-mac93694462018-01-22 20:49:31 -080097
98 MetricDimensionKey& operator=(const MetricDimensionKey& from) = default;
99
100 std::string toString() const;
101
102 inline const HashableDimensionKey& getDimensionKeyInWhat() const {
103 return mDimensionKeyInWhat;
104 }
105
tsaichristine69000e62019-10-18 17:34:52 -0700106 inline const HashableDimensionKey& getStateValuesKey() const {
107 return mStateValuesKey;
Yangster-mac93694462018-01-22 20:49:31 -0800108 }
109
tsaichristine69000e62019-10-18 17:34:52 -0700110 inline void setStateValuesKey(const HashableDimensionKey& key) {
111 mStateValuesKey = key;
Yangster13fb7e42018-03-07 17:30:49 -0800112 }
113
tsaichristine69000e62019-10-18 17:34:52 -0700114 bool hasStateValuesKey() const {
115 return mStateValuesKey.getValues().size() > 0;
Yangster-mac93694462018-01-22 20:49:31 -0800116 }
117
118 bool operator==(const MetricDimensionKey& that) const;
119
120 bool operator<(const MetricDimensionKey& that) const;
121
tsaichristine69000e62019-10-18 17:34:52 -0700122private:
123 HashableDimensionKey mDimensionKeyInWhat;
124 HashableDimensionKey mStateValuesKey;
Yangster-mac93694462018-01-22 20:49:31 -0800125};
126
Yao Chen8a8d16c2018-02-08 14:50:40 -0800127android::hash_t hashDimension(const HashableDimensionKey& key);
Yangster-mac93694462018-01-22 20:49:31 -0800128
Yao Chen8a8d16c2018-02-08 14:50:40 -0800129/**
tsaichristine10978642019-09-10 14:12:49 -0700130 * Returns true if a FieldValue field matches the matcher field.
131 * The value of the FieldValue is output.
132 */
133bool filterValues(const Matcher& matcherField, const std::vector<FieldValue>& values,
tsaichristine69000e62019-10-18 17:34:52 -0700134 FieldValue* output);
tsaichristine10978642019-09-10 14:12:49 -0700135
136/**
Yao Chen8a8d16c2018-02-08 14:50:40 -0800137 * Creating HashableDimensionKeys from FieldValues using matcher.
138 *
Yangster-mace06cfd72018-03-10 23:22:59 -0800139 * This function may make modifications to the Field if the matcher has Position=FIRST,LAST or ALL
140 * in it. This is because: for example, when we create dimension from last uid in attribution chain,
Yao Chen8a8d16c2018-02-08 14:50:40 -0800141 * In one event, uid 1000 is at position 5 and it's the last
142 * In another event, uid 1000 is at position 6, and it's the last
143 * these 2 events should be mapped to the same dimension. So we will remove the original position
144 * from the dimension key for the uid field (by applying 0x80 bit mask).
145 */
146bool filterValues(const std::vector<Matcher>& matcherFields, const std::vector<FieldValue>& values,
Yangster13fb7e42018-03-07 17:30:49 -0800147 HashableDimensionKey* output);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800148
149/**
150 * Filter the values from FieldValues using the matchers.
151 *
152 * In contrast to the above function, this function will not do any modification to the original
153 * data. Considering it as taking a snapshot on the atom event.
154 */
155void filterGaugeValues(const std::vector<Matcher>& matchers, const std::vector<FieldValue>& values,
156 std::vector<FieldValue>* output);
157
Yangster-mac53928882018-02-25 23:02:56 -0800158void getDimensionForCondition(const std::vector<FieldValue>& eventValues,
159 const Metric2Condition& links,
Yangster13fb7e42018-03-07 17:30:49 -0800160 HashableDimensionKey* conditionDimension);
Yangster-mac20877162017-12-22 17:19:39 -0800161
tsaichristine69000e62019-10-18 17:34:52 -0700162/**
163 * Get dimension values using metric's "what" fields and fill statePrimaryKey's
164 * mField information using "state" fields.
165 */
166void getDimensionForState(const std::vector<FieldValue>& eventValues, const Metric2State& link,
167 HashableDimensionKey* statePrimaryKey);
168
Yao Chend5aa01b32017-12-19 16:46:36 -0800169} // namespace statsd
170} // namespace os
171} // namespace android
172
173namespace std {
174
175using android::os::statsd::HashableDimensionKey;
Yangster-mac93694462018-01-22 20:49:31 -0800176using android::os::statsd::MetricDimensionKey;
Yao Chend5aa01b32017-12-19 16:46:36 -0800177
178template <>
179struct hash<HashableDimensionKey> {
180 std::size_t operator()(const HashableDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800181 return hashDimension(key);
Yao Chend5aa01b32017-12-19 16:46:36 -0800182 }
183};
184
Yangster-mac93694462018-01-22 20:49:31 -0800185template <>
186struct hash<MetricDimensionKey> {
187 std::size_t operator()(const MetricDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800188 android::hash_t hash = hashDimension(key.getDimensionKeyInWhat());
tsaichristine69000e62019-10-18 17:34:52 -0700189 hash = android::JenkinsHashMix(hash, hashDimension(key.getStateValuesKey()));
Yangster-mac93694462018-01-22 20:49:31 -0800190 return android::JenkinsHashWhiten(hash);
191 }
192};
tsaichristine10978642019-09-10 14:12:49 -0700193} // namespace std