blob: 89fe317834d81b9981d4c39723d23c75b8014729 [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
Yao Chend5aa01b32017-12-19 16:46:36 -080037class HashableDimensionKey {
38public:
Yao Chen8a8d16c2018-02-08 14:50:40 -080039 explicit HashableDimensionKey(const std::vector<FieldValue>& values) {
40 mValues = values;
41 }
Yao Chend5aa01b32017-12-19 16:46:36 -080042
43 HashableDimensionKey(){};
44
Yao Chen8a8d16c2018-02-08 14:50:40 -080045 HashableDimensionKey(const HashableDimensionKey& that) : mValues(that.getValues()){};
Yao Chend5aa01b32017-12-19 16:46:36 -080046
Yao Chen8a8d16c2018-02-08 14:50:40 -080047 inline void addValue(const FieldValue& value) {
48 mValues.push_back(value);
49 }
50
51 inline const std::vector<FieldValue>& getValues() const {
52 return mValues;
53 }
54
55 inline std::vector<FieldValue>* mutableValues() {
56 return &mValues;
57 }
58
59 inline FieldValue* mutableValue(size_t i) {
60 if (i >= 0 && i < mValues.size()) {
61 return &(mValues[i]);
62 }
63 return nullptr;
64 }
Yao Chend5aa01b32017-12-19 16:46:36 -080065
66 std::string toString() const;
67
Yao Chen8a8d16c2018-02-08 14:50:40 -080068 inline const char* c_str() const {
69 return toString().c_str();
Yangster-mac93694462018-01-22 20:49:31 -080070 }
71
Yao Chend5aa01b32017-12-19 16:46:36 -080072 bool operator==(const HashableDimensionKey& that) const;
73
74 bool operator<(const HashableDimensionKey& that) const;
75
Yao Chen8a8d16c2018-02-08 14:50:40 -080076 bool contains(const HashableDimensionKey& that) const;
Yao Chend5aa01b32017-12-19 16:46:36 -080077
78private:
Yao Chen8a8d16c2018-02-08 14:50:40 -080079 std::vector<FieldValue> mValues;
Yao Chend5aa01b32017-12-19 16:46:36 -080080};
81
Yangster-mac93694462018-01-22 20:49:31 -080082class MetricDimensionKey {
83 public:
84 explicit MetricDimensionKey(const HashableDimensionKey& dimensionKeyInWhat,
85 const HashableDimensionKey& dimensionKeyInCondition)
86 : mDimensionKeyInWhat(dimensionKeyInWhat),
87 mDimensionKeyInCondition(dimensionKeyInCondition) {};
88
89 MetricDimensionKey(){};
90
91 MetricDimensionKey(const MetricDimensionKey& that)
92 : mDimensionKeyInWhat(that.getDimensionKeyInWhat()),
93 mDimensionKeyInCondition(that.getDimensionKeyInCondition()) {};
94
95 MetricDimensionKey& operator=(const MetricDimensionKey& from) = default;
96
97 std::string toString() const;
98
99 inline const HashableDimensionKey& getDimensionKeyInWhat() const {
100 return mDimensionKeyInWhat;
101 }
102
103 inline const HashableDimensionKey& getDimensionKeyInCondition() const {
104 return mDimensionKeyInCondition;
105 }
106
107 bool hasDimensionKeyInCondition() const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800108 return mDimensionKeyInCondition.getValues().size() > 0;
Yangster-mac93694462018-01-22 20:49:31 -0800109 }
110
111 bool operator==(const MetricDimensionKey& that) const;
112
113 bool operator<(const MetricDimensionKey& that) const;
114
115 inline const char* c_str() const {
116 return toString().c_str();
117 }
118 private:
119 HashableDimensionKey mDimensionKeyInWhat;
120 HashableDimensionKey mDimensionKeyInCondition;
121};
122
Yao Chen8a8d16c2018-02-08 14:50:40 -0800123android::hash_t hashDimension(const HashableDimensionKey& key);
Yangster-mac93694462018-01-22 20:49:31 -0800124
Yao Chen8a8d16c2018-02-08 14:50:40 -0800125/**
126 * Creating HashableDimensionKeys from FieldValues using matcher.
127 *
128 * This function may make modifications to the Field if the matcher has Position=LAST or ANY in
129 * it. This is because: for example, when we create dimension from last uid in attribution chain,
130 * In one event, uid 1000 is at position 5 and it's the last
131 * In another event, uid 1000 is at position 6, and it's the last
132 * these 2 events should be mapped to the same dimension. So we will remove the original position
133 * from the dimension key for the uid field (by applying 0x80 bit mask).
134 */
135bool filterValues(const std::vector<Matcher>& matcherFields, const std::vector<FieldValue>& values,
136 std::vector<HashableDimensionKey>* output);
137
138/**
139 * Filter the values from FieldValues using the matchers.
140 *
141 * In contrast to the above function, this function will not do any modification to the original
142 * data. Considering it as taking a snapshot on the atom event.
143 */
144void filterGaugeValues(const std::vector<Matcher>& matchers, const std::vector<FieldValue>& values,
145 std::vector<FieldValue>* output);
146
147void getDimensionForCondition(const LogEvent& event, Metric2Condition links,
148 std::vector<HashableDimensionKey>* conditionDimension);
Yangster-mac20877162017-12-22 17:19:39 -0800149
Yao Chend5aa01b32017-12-19 16:46:36 -0800150} // namespace statsd
151} // namespace os
152} // namespace android
153
154namespace std {
155
156using android::os::statsd::HashableDimensionKey;
Yangster-mac93694462018-01-22 20:49:31 -0800157using android::os::statsd::MetricDimensionKey;
Yao Chend5aa01b32017-12-19 16:46:36 -0800158
159template <>
160struct hash<HashableDimensionKey> {
161 std::size_t operator()(const HashableDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800162 return hashDimension(key);
Yao Chend5aa01b32017-12-19 16:46:36 -0800163 }
164};
165
Yangster-mac93694462018-01-22 20:49:31 -0800166template <>
167struct hash<MetricDimensionKey> {
168 std::size_t operator()(const MetricDimensionKey& key) const {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800169 android::hash_t hash = hashDimension(key.getDimensionKeyInWhat());
170 hash = android::JenkinsHashMix(hash, hashDimension(key.getDimensionKeyInCondition()));
Yangster-mac93694462018-01-22 20:49:31 -0800171 return android::JenkinsHashWhiten(hash);
172 }
173};
174} // namespace std