blob: 5cfb1239d30e8446080f990a14746659d25ca118 [file] [log] [blame]
Yao Chencaf339d2017-10-06 16:01:10 -07001/*
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
Tej Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false // STOPSHIP if true
Yao Chen10535b92017-11-27 11:31:55 -080018#include "Log.h"
19
Yangster-mac932ecec2018-02-01 10:23:52 -080020#include "metrics_manager_util.h"
21
Yao Chencaf339d2017-10-06 16:01:10 -070022#include "../condition/CombinationConditionTracker.h"
23#include "../condition/SimpleConditionTracker.h"
Yao Chen580ea3212018-02-26 14:21:54 -080024#include "../condition/StateTracker.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070025#include "../external/StatsPullerManager.h"
Yao Chencaf339d2017-10-06 16:01:10 -070026#include "../matchers/CombinationLogMatchingTracker.h"
27#include "../matchers/SimpleLogMatchingTracker.h"
Yangster-mac32f07af2018-10-13 17:08:11 -070028#include "../matchers/EventMatcherWizard.h"
Yangster-mac932ecec2018-02-01 10:23:52 -080029#include "../metrics/CountMetricProducer.h"
30#include "../metrics/DurationMetricProducer.h"
31#include "../metrics/EventMetricProducer.h"
32#include "../metrics/GaugeMetricProducer.h"
33#include "../metrics/ValueMetricProducer.h"
34
Yao Chencaf339d2017-10-06 16:01:10 -070035#include "stats_util.h"
Yao Chen580ea3212018-02-26 14:21:54 -080036#include "statslog.h"
Yao Chencaf339d2017-10-06 16:01:10 -070037
Colin Crossf74defb2018-10-26 13:04:41 -070038#include <inttypes.h>
39
Yao Chencaf339d2017-10-06 16:01:10 -070040using std::set;
41using std::string;
42using std::unordered_map;
43using std::vector;
44
45namespace android {
46namespace os {
47namespace statsd {
48
Yao Chen8a8d16c2018-02-08 14:50:40 -080049namespace {
50
51bool hasLeafNode(const FieldMatcher& matcher) {
52 if (!matcher.has_field()) {
53 return false;
54 }
55 for (int i = 0; i < matcher.child_size(); ++i) {
56 if (hasLeafNode(matcher.child(i))) {
57 return true;
58 }
59 }
60 return true;
61}
62
63} // namespace
64
Yangster-mac94e197c2018-01-02 16:03:03 -080065bool handleMetricWithLogTrackers(const int64_t what, const int metricIndex,
Yao Chen5154a372017-10-30 22:57:06 -070066 const bool usedForDimension,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080067 const vector<sp<LogMatchingTracker>>& allAtomMatchers,
Yangster-mac94e197c2018-01-02 16:03:03 -080068 const unordered_map<int64_t, int>& logTrackerMap,
Yao Chen5110bed2017-10-23 12:50:02 -070069 unordered_map<int, std::vector<int>>& trackerToMetricMap,
70 int& logTrackerIndex) {
71 auto logTrackerIt = logTrackerMap.find(what);
Yao Chen729093d2017-10-16 10:33:26 -070072 if (logTrackerIt == logTrackerMap.end()) {
Yangster-mac94e197c2018-01-02 16:03:03 -080073 ALOGW("cannot find the AtomMatcher \"%lld\" in config", (long long)what);
Yao Chen5110bed2017-10-23 12:50:02 -070074 return false;
Yao Chen729093d2017-10-16 10:33:26 -070075 }
Yangster-mac20877162017-12-22 17:19:39 -080076 if (usedForDimension && allAtomMatchers[logTrackerIt->second]->getAtomIds().size() > 1) {
Yangster-mac94e197c2018-01-02 16:03:03 -080077 ALOGE("AtomMatcher \"%lld\" has more than one tag ids. When a metric has dimension, "
Stefan Lafon7c8f0a52017-11-21 14:49:09 -080078 "the \"what\" can only about one atom type.",
Yangster-mac94e197c2018-01-02 16:03:03 -080079 (long long)what);
Yao Chen5154a372017-10-30 22:57:06 -070080 return false;
81 }
Yao Chen5110bed2017-10-23 12:50:02 -070082 logTrackerIndex = logTrackerIt->second;
83 auto& metric_list = trackerToMetricMap[logTrackerIndex];
84 metric_list.push_back(metricIndex);
85 return true;
86}
87
Chenjie Yu88588972018-08-03 09:49:22 -070088bool handlePullMetricTriggerWithLogTrackers(
89 const int64_t trigger, const int metricIndex,
90 const vector<sp<LogMatchingTracker>>& allAtomMatchers,
91 const unordered_map<int64_t, int>& logTrackerMap,
92 unordered_map<int, std::vector<int>>& trackerToMetricMap, int& logTrackerIndex) {
93 auto logTrackerIt = logTrackerMap.find(trigger);
94 if (logTrackerIt == logTrackerMap.end()) {
95 ALOGW("cannot find the AtomMatcher \"%lld\" in config", (long long)trigger);
96 return false;
97 }
98 if (allAtomMatchers[logTrackerIt->second]->getAtomIds().size() > 1) {
99 ALOGE("AtomMatcher \"%lld\" has more than one tag ids."
100 "Trigger can only be one atom type.",
101 (long long)trigger);
102 return false;
103 }
104 logTrackerIndex = logTrackerIt->second;
105 auto& metric_list = trackerToMetricMap[logTrackerIndex];
106 metric_list.push_back(metricIndex);
107 return true;
108}
109
Yao Chen5110bed2017-10-23 12:50:02 -0700110bool handleMetricWithConditions(
Yangster-mac94e197c2018-01-02 16:03:03 -0800111 const int64_t condition, const int metricIndex,
112 const unordered_map<int64_t, int>& conditionTrackerMap,
Stefan Lafona5b51912017-12-05 21:43:52 -0800113 const ::google::protobuf::RepeatedPtrField<::android::os::statsd::MetricConditionLink>&
Yao Chen5110bed2017-10-23 12:50:02 -0700114 links,
115 vector<sp<ConditionTracker>>& allConditionTrackers, int& conditionIndex,
116 unordered_map<int, std::vector<int>>& conditionToMetricMap) {
117 auto condition_it = conditionTrackerMap.find(condition);
118 if (condition_it == conditionTrackerMap.end()) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800119 ALOGW("cannot find Predicate \"%lld\" in the config", (long long)condition);
Yao Chen5110bed2017-10-23 12:50:02 -0700120 return false;
121 }
122
123 for (const auto& link : links) {
124 auto it = conditionTrackerMap.find(link.condition());
125 if (it == conditionTrackerMap.end()) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800126 ALOGW("cannot find Predicate \"%lld\" in the config", (long long)link.condition());
Yao Chen5110bed2017-10-23 12:50:02 -0700127 return false;
128 }
129 allConditionTrackers[condition_it->second]->setSliced(true);
130 allConditionTrackers[it->second]->setSliced(true);
Yao Chen5110bed2017-10-23 12:50:02 -0700131 }
132 conditionIndex = condition_it->second;
133
134 // will create new vector if not exist before.
135 auto& metricList = conditionToMetricMap[condition_it->second];
136 metricList.push_back(metricIndex);
137 return true;
Yao Chen729093d2017-10-16 10:33:26 -0700138}
139
Yangster-mac20877162017-12-22 17:19:39 -0800140bool initLogTrackers(const StatsdConfig& config, const UidMap& uidMap,
Yangster-mac94e197c2018-01-02 16:03:03 -0800141 unordered_map<int64_t, int>& logTrackerMap,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800142 vector<sp<LogMatchingTracker>>& allAtomMatchers, set<int>& allTagIds) {
143 vector<AtomMatcher> matcherConfigs;
144 const int atomMatcherCount = config.atom_matcher_size();
145 matcherConfigs.reserve(atomMatcherCount);
146 allAtomMatchers.reserve(atomMatcherCount);
Yao Chencaf339d2017-10-06 16:01:10 -0700147
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800148 for (int i = 0; i < atomMatcherCount; i++) {
149 const AtomMatcher& logMatcher = config.atom_matcher(i);
Yao Chencaf339d2017-10-06 16:01:10 -0700150
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800151 int index = allAtomMatchers.size();
Yao Chencaf339d2017-10-06 16:01:10 -0700152 switch (logMatcher.contents_case()) {
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800153 case AtomMatcher::ContentsCase::kSimpleAtomMatcher:
154 allAtomMatchers.push_back(new SimpleLogMatchingTracker(
Yangster-mac94e197c2018-01-02 16:03:03 -0800155 logMatcher.id(), index, logMatcher.simple_atom_matcher(), uidMap));
Yao Chencaf339d2017-10-06 16:01:10 -0700156 break;
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800157 case AtomMatcher::ContentsCase::kCombination:
158 allAtomMatchers.push_back(
Yangster-mac94e197c2018-01-02 16:03:03 -0800159 new CombinationLogMatchingTracker(logMatcher.id(), index));
Yao Chencaf339d2017-10-06 16:01:10 -0700160 break;
161 default:
Yangster-mac94e197c2018-01-02 16:03:03 -0800162 ALOGE("Matcher \"%lld\" malformed", (long long)logMatcher.id());
Yao Chencaf339d2017-10-06 16:01:10 -0700163 return false;
164 // continue;
165 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800166 if (logTrackerMap.find(logMatcher.id()) != logTrackerMap.end()) {
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800167 ALOGE("Duplicate AtomMatcher found!");
Yao Chencaf339d2017-10-06 16:01:10 -0700168 return false;
169 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800170 logTrackerMap[logMatcher.id()] = index;
Yao Chencaf339d2017-10-06 16:01:10 -0700171 matcherConfigs.push_back(logMatcher);
172 }
173
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800174 vector<bool> stackTracker2(allAtomMatchers.size(), false);
175 for (auto& matcher : allAtomMatchers) {
176 if (!matcher->init(matcherConfigs, allAtomMatchers, logTrackerMap, stackTracker2)) {
Yao Chencaf339d2017-10-06 16:01:10 -0700177 return false;
178 }
179 // Collect all the tag ids that are interesting. TagIds exist in leaf nodes only.
Yangster-mac20877162017-12-22 17:19:39 -0800180 const set<int>& tagIds = matcher->getAtomIds();
Yao Chencaf339d2017-10-06 16:01:10 -0700181 allTagIds.insert(tagIds.begin(), tagIds.end());
182 }
183 return true;
184}
185
Yao Chen580ea3212018-02-26 14:21:54 -0800186/**
187 * A StateTracker is built from a SimplePredicate which has only "start", and no "stop"
188 * or "stop_all". The start must be an atom matcher that matches a state atom. It must
189 * have dimension, the dimension must be the state atom's primary fields plus exclusive state
190 * field. For example, the StateTracker is used in tracking UidProcessState and ScreenState.
191 *
192 */
193bool isStateTracker(const SimplePredicate& simplePredicate, vector<Matcher>* primaryKeys) {
194 // 1. must not have "stop". must have "dimension"
195 if (!simplePredicate.has_stop() && simplePredicate.has_dimensions()) {
Yao Chenc40a19d2018-03-15 16:48:25 -0700196 auto it = android::util::AtomsInfo::kStateAtomsFieldOptions.find(
197 simplePredicate.dimensions().field());
Yao Chen580ea3212018-02-26 14:21:54 -0800198 // 2. must be based on a state atom.
Yao Chenc40a19d2018-03-15 16:48:25 -0700199 if (it != android::util::AtomsInfo::kStateAtomsFieldOptions.end()) {
Yao Chen580ea3212018-02-26 14:21:54 -0800200 // 3. dimension must be primary fields + state field IN ORDER
201 size_t expectedDimensionCount = it->second.primaryFields.size() + 1;
202 vector<Matcher> dimensions;
203 translateFieldMatcher(simplePredicate.dimensions(), &dimensions);
204 if (dimensions.size() != expectedDimensionCount) {
205 return false;
206 }
207 // 3.1 check the primary fields first.
208 size_t index = 0;
209 for (const auto& field : it->second.primaryFields) {
210 Matcher matcher = getSimpleMatcher(it->first, field);
211 if (!(matcher == dimensions[index])) {
212 return false;
213 }
214 primaryKeys->push_back(matcher);
215 index++;
216 }
217 Matcher stateFieldMatcher =
218 getSimpleMatcher(it->first, it->second.exclusiveField);
219 // 3.2 last dimension should be the exclusive field.
220 if (!(dimensions.back() == stateFieldMatcher)) {
221 return false;
222 }
223 return true;
224 }
225 }
226 return false;
227} // namespace statsd
228
Yao Chenb3561512017-11-21 18:07:17 -0800229bool initConditions(const ConfigKey& key, const StatsdConfig& config,
Yangster-mac94e197c2018-01-02 16:03:03 -0800230 const unordered_map<int64_t, int>& logTrackerMap,
231 unordered_map<int64_t, int>& conditionTrackerMap,
Yao Chencaf339d2017-10-06 16:01:10 -0700232 vector<sp<ConditionTracker>>& allConditionTrackers,
233 unordered_map<int, std::vector<int>>& trackerToConditionMap) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800234 vector<Predicate> conditionConfigs;
235 const int conditionTrackerCount = config.predicate_size();
Yao Chen729093d2017-10-16 10:33:26 -0700236 conditionConfigs.reserve(conditionTrackerCount);
237 allConditionTrackers.reserve(conditionTrackerCount);
Yao Chencaf339d2017-10-06 16:01:10 -0700238
Yao Chen729093d2017-10-16 10:33:26 -0700239 for (int i = 0; i < conditionTrackerCount; i++) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800240 const Predicate& condition = config.predicate(i);
Yao Chencaf339d2017-10-06 16:01:10 -0700241 int index = allConditionTrackers.size();
242 switch (condition.contents_case()) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800243 case Predicate::ContentsCase::kSimplePredicate: {
Yao Chen580ea3212018-02-26 14:21:54 -0800244 vector<Matcher> primaryKeys;
245 if (isStateTracker(condition.simple_predicate(), &primaryKeys)) {
246 allConditionTrackers.push_back(new StateTracker(key, condition.id(), index,
247 condition.simple_predicate(),
248 logTrackerMap, primaryKeys));
249 } else {
250 allConditionTrackers.push_back(new SimpleConditionTracker(
251 key, condition.id(), index, condition.simple_predicate(),
252 logTrackerMap));
253 }
Yao Chencaf339d2017-10-06 16:01:10 -0700254 break;
255 }
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800256 case Predicate::ContentsCase::kCombination: {
Yao Chencaf339d2017-10-06 16:01:10 -0700257 allConditionTrackers.push_back(
Yangster-mac94e197c2018-01-02 16:03:03 -0800258 new CombinationConditionTracker(condition.id(), index));
Yao Chencaf339d2017-10-06 16:01:10 -0700259 break;
260 }
261 default:
Yangster-mac94e197c2018-01-02 16:03:03 -0800262 ALOGE("Predicate \"%lld\" malformed", (long long)condition.id());
Yao Chencaf339d2017-10-06 16:01:10 -0700263 return false;
264 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800265 if (conditionTrackerMap.find(condition.id()) != conditionTrackerMap.end()) {
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800266 ALOGE("Duplicate Predicate found!");
Yao Chencaf339d2017-10-06 16:01:10 -0700267 return false;
268 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800269 conditionTrackerMap[condition.id()] = index;
Yao Chencaf339d2017-10-06 16:01:10 -0700270 conditionConfigs.push_back(condition);
271 }
272
273 vector<bool> stackTracker(allConditionTrackers.size(), false);
274 for (size_t i = 0; i < allConditionTrackers.size(); i++) {
275 auto& conditionTracker = allConditionTrackers[i];
276 if (!conditionTracker->init(conditionConfigs, allConditionTrackers, conditionTrackerMap,
277 stackTracker)) {
278 return false;
279 }
280 for (const int trackerIndex : conditionTracker->getLogTrackerIndex()) {
281 auto& conditionList = trackerToConditionMap[trackerIndex];
282 conditionList.push_back(i);
283 }
284 }
285 return true;
286}
287
Chenjie Yue2219202018-06-08 10:07:51 -0700288bool initMetrics(const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseTimeNs,
289 const int64_t currentTimeNs, UidMap& uidMap,
290 const sp<StatsPullerManager>& pullerManager,
291 const unordered_map<int64_t, int>& logTrackerMap,
Yangster-mac94e197c2018-01-02 16:03:03 -0800292 const unordered_map<int64_t, int>& conditionTrackerMap,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800293 const vector<sp<LogMatchingTracker>>& allAtomMatchers,
Yao Chen729093d2017-10-16 10:33:26 -0700294 vector<sp<ConditionTracker>>& allConditionTrackers,
Yao Chencaf339d2017-10-06 16:01:10 -0700295 vector<sp<MetricProducer>>& allMetricProducers,
296 unordered_map<int, std::vector<int>>& conditionToMetricMap,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800297 unordered_map<int, std::vector<int>>& trackerToMetricMap,
David Chenbd125272018-04-04 19:02:50 -0700298 unordered_map<int64_t, int>& metricMap, std::set<int64_t>& noReportMetricIds) {
Yao Chen729093d2017-10-16 10:33:26 -0700299 sp<ConditionWizard> wizard = new ConditionWizard(allConditionTrackers);
Yangster-mac32f07af2018-10-13 17:08:11 -0700300 sp<EventMatcherWizard> matcherWizard = new EventMatcherWizard(allAtomMatchers);
Yao Chen93fe3a32017-11-02 13:52:59 -0700301 const int allMetricsCount = config.count_metric_size() + config.duration_metric_size() +
302 config.event_metric_size() + config.value_metric_size();
Yao Chen729093d2017-10-16 10:33:26 -0700303 allMetricProducers.reserve(allMetricsCount);
Chenjie Yu6736c892017-11-09 10:50:09 -0800304 StatsPullerManager statsPullerManager;
Yangster-mac20877162017-12-22 17:19:39 -0800305
Yao Chencaf339d2017-10-06 16:01:10 -0700306 // Build MetricProducers for each metric defined in config.
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700307 // build CountMetricProducer
Yao Chencaf339d2017-10-06 16:01:10 -0700308 for (int i = 0; i < config.count_metric_size(); i++) {
309 const CountMetric& metric = config.count_metric(i);
310 if (!metric.has_what()) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800311 ALOGW("cannot find \"what\" in CountMetric \"%lld\"", (long long)metric.id());
Yao Chencaf339d2017-10-06 16:01:10 -0700312 return false;
313 }
314
Yao Chen729093d2017-10-16 10:33:26 -0700315 int metricIndex = allMetricProducers.size();
Yangster-mac94e197c2018-01-02 16:03:03 -0800316 metricMap.insert({metric.id(), metricIndex});
Yao Chen5110bed2017-10-23 12:50:02 -0700317 int trackerIndex;
Yangster-mac468ff042018-01-17 12:26:34 -0800318 if (!handleMetricWithLogTrackers(metric.what(), metricIndex,
319 metric.has_dimensions_in_what(),
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800320 allAtomMatchers, logTrackerMap, trackerToMetricMap,
Yao Chen5154a372017-10-30 22:57:06 -0700321 trackerIndex)) {
Yao Chencaf339d2017-10-06 16:01:10 -0700322 return false;
323 }
324
Yao Chen5110bed2017-10-23 12:50:02 -0700325 int conditionIndex = -1;
Yao Chencaf339d2017-10-06 16:01:10 -0700326 if (metric.has_condition()) {
Yao Chen10535b92017-11-27 11:31:55 -0800327 bool good = handleMetricWithConditions(
328 metric.condition(), metricIndex, conditionTrackerMap, metric.links(),
329 allConditionTrackers, conditionIndex, conditionToMetricMap);
330 if (!good) {
331 return false;
332 }
Yao Chen5c925ad2017-11-15 14:15:46 -0800333 } else {
334 if (metric.links_size() > 0) {
Stefan Lafona5b51912017-12-05 21:43:52 -0800335 ALOGW("metrics has a MetricConditionLink but doesn't have a condition");
Yao Chen5c925ad2017-11-15 14:15:46 -0800336 return false;
337 }
Yao Chencaf339d2017-10-06 16:01:10 -0700338 }
Yao Chen5110bed2017-10-23 12:50:02 -0700339
Yao Chen93fe3a32017-11-02 13:52:59 -0700340 sp<MetricProducer> countProducer =
Chenjie Yue1361ed2018-07-23 17:33:09 -0700341 new CountMetricProducer(key, metric, conditionIndex, wizard, timeBaseTimeNs, currentTimeNs);
Yao Chencaf339d2017-10-06 16:01:10 -0700342 allMetricProducers.push_back(countProducer);
343 }
344
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700345 // build DurationMetricProducer
Yao Chen729093d2017-10-16 10:33:26 -0700346 for (int i = 0; i < config.duration_metric_size(); i++) {
347 int metricIndex = allMetricProducers.size();
Yao Chen5110bed2017-10-23 12:50:02 -0700348 const DurationMetric& metric = config.duration_metric(i);
Yangster-mac94e197c2018-01-02 16:03:03 -0800349 metricMap.insert({metric.id(), metricIndex});
Yao Chen5154a372017-10-30 22:57:06 -0700350
351 auto what_it = conditionTrackerMap.find(metric.what());
352 if (what_it == conditionTrackerMap.end()) {
353 ALOGE("DurationMetric's \"what\" is invalid");
354 return false;
355 }
356
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800357 const Predicate& durationWhat = config.predicate(what_it->second);
Yao Chen5154a372017-10-30 22:57:06 -0700358
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800359 if (durationWhat.contents_case() != Predicate::ContentsCase::kSimplePredicate) {
Yao Chen5154a372017-10-30 22:57:06 -0700360 ALOGE("DurationMetric's \"what\" must be a simple condition");
361 return false;
362 }
363
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800364 const auto& simplePredicate = durationWhat.simple_predicate();
Yao Chen5154a372017-10-30 22:57:06 -0700365
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800366 bool nesting = simplePredicate.count_nesting();
Yao Chen0ea19902017-11-15 15:44:45 -0800367
Yao Chen5110bed2017-10-23 12:50:02 -0700368 int trackerIndices[3] = {-1, -1, -1};
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800369 if (!simplePredicate.has_start() ||
370 !handleMetricWithLogTrackers(simplePredicate.start(), metricIndex,
Yangster-mac468ff042018-01-17 12:26:34 -0800371 metric.has_dimensions_in_what(), allAtomMatchers,
Yao Chen5154a372017-10-30 22:57:06 -0700372 logTrackerMap, trackerToMetricMap, trackerIndices[0])) {
Yao Chen5110bed2017-10-23 12:50:02 -0700373 ALOGE("Duration metrics must specify a valid the start event matcher");
Yao Chen729093d2017-10-16 10:33:26 -0700374 return false;
375 }
Yao Chencaf339d2017-10-06 16:01:10 -0700376
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800377 if (simplePredicate.has_stop() &&
378 !handleMetricWithLogTrackers(simplePredicate.stop(), metricIndex,
Yangster-mac468ff042018-01-17 12:26:34 -0800379 metric.has_dimensions_in_what(), allAtomMatchers,
Yao Chen5154a372017-10-30 22:57:06 -0700380 logTrackerMap, trackerToMetricMap, trackerIndices[1])) {
Yao Chen5110bed2017-10-23 12:50:02 -0700381 return false;
Yao Chen729093d2017-10-16 10:33:26 -0700382 }
383
Stefan Lafon12d01fa2017-12-04 20:56:09 -0800384 if (simplePredicate.has_stop_all() &&
385 !handleMetricWithLogTrackers(simplePredicate.stop_all(), metricIndex,
Yangster-mac468ff042018-01-17 12:26:34 -0800386 metric.has_dimensions_in_what(), allAtomMatchers,
Yao Chen5154a372017-10-30 22:57:06 -0700387 logTrackerMap, trackerToMetricMap, trackerIndices[2])) {
Yao Chen5110bed2017-10-23 12:50:02 -0700388 return false;
Yao Chen729093d2017-10-16 10:33:26 -0700389 }
390
Yangster-mac20877162017-12-22 17:19:39 -0800391 FieldMatcher internalDimensions = simplePredicate.dimensions();
Yao Chen5154a372017-10-30 22:57:06 -0700392
Yao Chen729093d2017-10-16 10:33:26 -0700393 int conditionIndex = -1;
394
Yao Chen5c925ad2017-11-15 14:15:46 -0800395 if (metric.has_condition()) {
Yao Chen10535b92017-11-27 11:31:55 -0800396 bool good = handleMetricWithConditions(
397 metric.condition(), metricIndex, conditionTrackerMap, metric.links(),
398 allConditionTrackers, conditionIndex, conditionToMetricMap);
399 if (!good) {
400 return false;
401 }
Yao Chen5c925ad2017-11-15 14:15:46 -0800402 } else {
403 if (metric.links_size() > 0) {
Stefan Lafona5b51912017-12-05 21:43:52 -0800404 ALOGW("metrics has a MetricConditionLink but doesn't have a condition");
Yao Chen5c925ad2017-11-15 14:15:46 -0800405 return false;
406 }
Yao Chen729093d2017-10-16 10:33:26 -0700407 }
408
Yao Chen5154a372017-10-30 22:57:06 -0700409 sp<MetricProducer> durationMetric = new DurationMetricProducer(
Yao Chenb3561512017-11-21 18:07:17 -0800410 key, metric, conditionIndex, trackerIndices[0], trackerIndices[1],
Chenjie Yue1361ed2018-07-23 17:33:09 -0700411 trackerIndices[2], nesting, wizard, internalDimensions, timeBaseTimeNs, currentTimeNs);
Yao Chen729093d2017-10-16 10:33:26 -0700412
413 allMetricProducers.push_back(durationMetric);
414 }
Yao Chen5110bed2017-10-23 12:50:02 -0700415
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700416 // build EventMetricProducer
Yao Chen5110bed2017-10-23 12:50:02 -0700417 for (int i = 0; i < config.event_metric_size(); i++) {
418 int metricIndex = allMetricProducers.size();
419 const EventMetric& metric = config.event_metric(i);
Yangster-mac94e197c2018-01-02 16:03:03 -0800420 metricMap.insert({metric.id(), metricIndex});
421 if (!metric.has_id() || !metric.has_what()) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800422 ALOGW("cannot find the metric name or what in config");
Yao Chen5110bed2017-10-23 12:50:02 -0700423 return false;
424 }
425 int trackerIndex;
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800426 if (!handleMetricWithLogTrackers(metric.what(), metricIndex, false, allAtomMatchers,
Yao Chen5154a372017-10-30 22:57:06 -0700427 logTrackerMap, trackerToMetricMap, trackerIndex)) {
Yao Chen5110bed2017-10-23 12:50:02 -0700428 return false;
429 }
430
431 int conditionIndex = -1;
432 if (metric.has_condition()) {
Yao Chen10535b92017-11-27 11:31:55 -0800433 bool good = handleMetricWithConditions(
434 metric.condition(), metricIndex, conditionTrackerMap, metric.links(),
435 allConditionTrackers, conditionIndex, conditionToMetricMap);
436 if (!good) {
437 return false;
438 }
Yao Chen5c925ad2017-11-15 14:15:46 -0800439 } else {
440 if (metric.links_size() > 0) {
Stefan Lafona5b51912017-12-05 21:43:52 -0800441 ALOGW("metrics has a MetricConditionLink but doesn't have a condition");
Yao Chen5c925ad2017-11-15 14:15:46 -0800442 return false;
443 }
Yao Chen5110bed2017-10-23 12:50:02 -0700444 }
445
Yao Chen93fe3a32017-11-02 13:52:59 -0700446 sp<MetricProducer> eventMetric =
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700447 new EventMetricProducer(key, metric, conditionIndex, wizard, timeBaseTimeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700448
Yao Chen5110bed2017-10-23 12:50:02 -0700449 allMetricProducers.push_back(eventMetric);
450 }
451
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700452 // build ValueMetricProducer
Chenjie Yub3dda412017-10-24 13:41:59 -0700453 for (int i = 0; i < config.value_metric_size(); i++) {
454 const ValueMetric& metric = config.value_metric(i);
455 if (!metric.has_what()) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800456 ALOGW("cannot find \"what\" in ValueMetric \"%lld\"", (long long)metric.id());
Chenjie Yub3dda412017-10-24 13:41:59 -0700457 return false;
458 }
Chenjie Yud7e3a222018-11-28 21:29:44 +0000459 if (!metric.has_value_field()) {
460 ALOGW("cannot find \"value_field\" in ValueMetric \"%lld\"", (long long)metric.id());
461 return false;
462 }
463 std::vector<Matcher> fieldMatchers;
464 translateFieldMatcher(metric.value_field(), &fieldMatchers);
465 if (fieldMatchers.size() < 1) {
466 ALOGW("incorrect \"value_field\" in ValueMetric \"%lld\"", (long long)metric.id());
467 return false;
468 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700469
Chenjie Yub3dda412017-10-24 13:41:59 -0700470 int metricIndex = allMetricProducers.size();
Yangster-mac94e197c2018-01-02 16:03:03 -0800471 metricMap.insert({metric.id(), metricIndex});
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700472 int trackerIndex;
Yangster-mac468ff042018-01-17 12:26:34 -0800473 if (!handleMetricWithLogTrackers(metric.what(), metricIndex,
474 metric.has_dimensions_in_what(),
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800475 allAtomMatchers, logTrackerMap, trackerToMetricMap,
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700476 trackerIndex)) {
477 return false;
478 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700479
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800480 sp<LogMatchingTracker> atomMatcher = allAtomMatchers.at(trackerIndex);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700481 // If it is pulled atom, it should be simple matcher with one tagId.
Yangster-mac20877162017-12-22 17:19:39 -0800482 if (atomMatcher->getAtomIds().size() != 1) {
Chenjie Yud9dfda72017-12-11 17:41:20 -0800483 return false;
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700484 }
Yangster-mac20877162017-12-22 17:19:39 -0800485 int atomTagId = *(atomMatcher->getAtomIds().begin());
Chenjie Yud9dfda72017-12-11 17:41:20 -0800486 int pullTagId = statsPullerManager.PullerForMatcherExists(atomTagId) ? atomTagId : -1;
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700487
488 int conditionIndex = -1;
489 if (metric.has_condition()) {
Yao Chen10535b92017-11-27 11:31:55 -0800490 bool good = handleMetricWithConditions(
491 metric.condition(), metricIndex, conditionTrackerMap, metric.links(),
492 allConditionTrackers, conditionIndex, conditionToMetricMap);
493 if (!good) {
494 return false;
495 }
Yao Chen5c925ad2017-11-15 14:15:46 -0800496 } else {
497 if (metric.links_size() > 0) {
Stefan Lafona5b51912017-12-05 21:43:52 -0800498 ALOGW("metrics has a MetricConditionLink but doesn't have a condition");
Yao Chen5c925ad2017-11-15 14:15:46 -0800499 return false;
500 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700501 }
502
Chenjie Yu054ce9c2018-11-12 15:27:29 -0800503 sp<MetricProducer> valueProducer = new ValueMetricProducer(
504 key, metric, conditionIndex, wizard, trackerIndex, matcherWizard, pullTagId,
505 timeBaseTimeNs, currentTimeNs, pullerManager);
Chenjie Yub3dda412017-10-24 13:41:59 -0700506 allMetricProducers.push_back(valueProducer);
507 }
Yangster1d4d6862017-10-31 12:58:51 -0700508
509 // Gauge metrics.
510 for (int i = 0; i < config.gauge_metric_size(); i++) {
511 const GaugeMetric& metric = config.gauge_metric(i);
512 if (!metric.has_what()) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800513 ALOGW("cannot find \"what\" in GaugeMetric \"%lld\"", (long long)metric.id());
Chenjie Yud9dfda72017-12-11 17:41:20 -0800514 return false;
515 }
516
Yangster-mac20877162017-12-22 17:19:39 -0800517 if ((!metric.gauge_fields_filter().has_include_all() ||
518 (metric.gauge_fields_filter().include_all() == false)) &&
519 !hasLeafNode(metric.gauge_fields_filter().fields())) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800520 ALOGW("Incorrect field filter setting in GaugeMetric %lld", (long long)metric.id());
Chenjie Yu2b4fc9d2017-12-18 15:20:00 -0800521 return false;
522 }
Yangster-mac20877162017-12-22 17:19:39 -0800523 if ((metric.gauge_fields_filter().has_include_all() &&
524 metric.gauge_fields_filter().include_all() == true) &&
525 hasLeafNode(metric.gauge_fields_filter().fields())) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800526 ALOGW("Incorrect field filter setting in GaugeMetric %lld", (long long)metric.id());
Yangster1d4d6862017-10-31 12:58:51 -0700527 return false;
528 }
529
530 int metricIndex = allMetricProducers.size();
Yangster-mac94e197c2018-01-02 16:03:03 -0800531 metricMap.insert({metric.id(), metricIndex});
Yangster1d4d6862017-10-31 12:58:51 -0700532 int trackerIndex;
Yangster-mac468ff042018-01-17 12:26:34 -0800533 if (!handleMetricWithLogTrackers(metric.what(), metricIndex,
534 metric.has_dimensions_in_what(),
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800535 allAtomMatchers, logTrackerMap, trackerToMetricMap,
Yangster1d4d6862017-10-31 12:58:51 -0700536 trackerIndex)) {
537 return false;
538 }
539
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800540 sp<LogMatchingTracker> atomMatcher = allAtomMatchers.at(trackerIndex);
Chenjie Yu88588972018-08-03 09:49:22 -0700541 // For GaugeMetric atom, it should be simple matcher with one tagId.
Yangster-mac20877162017-12-22 17:19:39 -0800542 if (atomMatcher->getAtomIds().size() != 1) {
Chenjie Yud9dfda72017-12-11 17:41:20 -0800543 return false;
Yangster1d4d6862017-10-31 12:58:51 -0700544 }
Yangster-mac20877162017-12-22 17:19:39 -0800545 int atomTagId = *(atomMatcher->getAtomIds().begin());
Chenjie Yud9dfda72017-12-11 17:41:20 -0800546 int pullTagId = statsPullerManager.PullerForMatcherExists(atomTagId) ? atomTagId : -1;
Yangster1d4d6862017-10-31 12:58:51 -0700547
Chenjie Yu88588972018-08-03 09:49:22 -0700548 int triggerTrackerIndex;
549 int triggerAtomId = -1;
Chenjie Yue077fd22018-11-19 13:29:40 -0800550 if (metric.has_trigger_event()) {
551 if (pullTagId == -1) {
552 ALOGW("Pull atom not specified for trigger");
553 return false;
554 }
555 // event_trigger should be used with FIRST_N_SAMPLES
556 if (metric.sampling_type() != GaugeMetric::FIRST_N_SAMPLES) {
Chenjie Yu88588972018-08-03 09:49:22 -0700557 return false;
558 }
559 if (!handlePullMetricTriggerWithLogTrackers(metric.trigger_event(), metricIndex,
560 allAtomMatchers, logTrackerMap,
561 trackerToMetricMap, triggerTrackerIndex)) {
562 return false;
563 }
564 sp<LogMatchingTracker> triggerAtomMatcher = allAtomMatchers.at(triggerTrackerIndex);
565 triggerAtomId = *(triggerAtomMatcher->getAtomIds().begin());
566 }
567
Chenjie Yue077fd22018-11-19 13:29:40 -0800568 if (!metric.has_trigger_event() && pullTagId != -1 &&
569 metric.sampling_type() == GaugeMetric::FIRST_N_SAMPLES) {
570 ALOGW("FIRST_N_SAMPLES is only for pushed event or pull_on_trigger");
571 return false;
572 }
573
Yangster1d4d6862017-10-31 12:58:51 -0700574 int conditionIndex = -1;
575 if (metric.has_condition()) {
Yao Chen10535b92017-11-27 11:31:55 -0800576 bool good = handleMetricWithConditions(
577 metric.condition(), metricIndex, conditionTrackerMap, metric.links(),
578 allConditionTrackers, conditionIndex, conditionToMetricMap);
579 if (!good) {
580 return false;
581 }
Yao Chen5c925ad2017-11-15 14:15:46 -0800582 } else {
583 if (metric.links_size() > 0) {
Stefan Lafona5b51912017-12-05 21:43:52 -0800584 ALOGW("metrics has a MetricConditionLink but doesn't have a condition");
Yao Chen5c925ad2017-11-15 14:15:46 -0800585 return false;
586 }
Yangster1d4d6862017-10-31 12:58:51 -0700587 }
588
Chenjie Yu88588972018-08-03 09:49:22 -0700589 sp<MetricProducer> gaugeProducer = new GaugeMetricProducer(
Yangster-mac32f07af2018-10-13 17:08:11 -0700590 key, metric, conditionIndex, wizard,
591 trackerIndex, matcherWizard, pullTagId, triggerAtomId, atomTagId,
Chenjie Yu88588972018-08-03 09:49:22 -0700592 timeBaseTimeNs, currentTimeNs, pullerManager);
Yangster1d4d6862017-10-31 12:58:51 -0700593 allMetricProducers.push_back(gaugeProducer);
594 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800595 for (int i = 0; i < config.no_report_metric_size(); ++i) {
596 const auto no_report_metric = config.no_report_metric(i);
597 if (metricMap.find(no_report_metric) == metricMap.end()) {
Colin Crossf74defb2018-10-26 13:04:41 -0700598 ALOGW("no_report_metric %" PRId64 " not exist", no_report_metric);
Yangster-mac94e197c2018-01-02 16:03:03 -0800599 return false;
600 }
601 noReportMetricIds.insert(no_report_metric);
602 }
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800603 for (const auto& it : allMetricProducers) {
David Chenbd125272018-04-04 19:02:50 -0700604 uidMap.addListener(it);
605 }
Yao Chencaf339d2017-10-06 16:01:10 -0700606 return true;
607}
608
Yangster-mac20877162017-12-22 17:19:39 -0800609bool initAlerts(const StatsdConfig& config,
Yangster-mac94e197c2018-01-02 16:03:03 -0800610 const unordered_map<int64_t, int>& metricProducerMap,
Yangster-mac932ecec2018-02-01 10:23:52 -0800611 const sp<AlarmMonitor>& anomalyAlarmMonitor,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800612 vector<sp<MetricProducer>>& allMetricProducers,
613 vector<sp<AnomalyTracker>>& allAnomalyTrackers) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800614 unordered_map<int64_t, int> anomalyTrackerMap;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800615 for (int i = 0; i < config.alert_size(); i++) {
616 const Alert& alert = config.alert(i);
Yangster-mac94e197c2018-01-02 16:03:03 -0800617 const auto& itr = metricProducerMap.find(alert.metric_id());
Yangster-mace2cd6d52017-11-09 20:38:30 -0800618 if (itr == metricProducerMap.end()) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800619 ALOGW("alert \"%lld\" has unknown metric id: \"%lld\"", (long long)alert.id(),
620 (long long)alert.metric_id());
Yangster-mace2cd6d52017-11-09 20:38:30 -0800621 return false;
622 }
Bookatz6bf98252018-03-14 10:44:24 -0700623 if (!alert.has_trigger_if_sum_gt()) {
624 ALOGW("invalid alert: missing threshold");
625 return false;
626 }
Yangster-maca7fb12d2018-01-03 17:17:20 -0800627 if (alert.trigger_if_sum_gt() < 0 || alert.num_buckets() <= 0) {
628 ALOGW("invalid alert: threshold=%f num_buckets= %d",
629 alert.trigger_if_sum_gt(), alert.num_buckets());
Bookatzcc5adef2017-11-21 14:36:23 -0800630 return false;
631 }
Yangster-mace2cd6d52017-11-09 20:38:30 -0800632 const int metricIndex = itr->second;
Bookatz450099d2017-11-30 17:09:30 -0800633 sp<MetricProducer> metric = allMetricProducers[metricIndex];
Yangster-mac932ecec2018-02-01 10:23:52 -0800634 sp<AnomalyTracker> anomalyTracker = metric->addAnomalyTracker(alert, anomalyAlarmMonitor);
Bookatz1476ef22018-02-13 12:26:01 -0800635 if (anomalyTracker == nullptr) {
636 // The ALOGW for this invalid alert was already displayed in addAnomalyTracker().
637 return false;
Bookatzcc5adef2017-11-21 14:36:23 -0800638 }
Bookatz1476ef22018-02-13 12:26:01 -0800639 anomalyTrackerMap.insert(std::make_pair(alert.id(), allAnomalyTrackers.size()));
640 allAnomalyTrackers.push_back(anomalyTracker);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800641 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800642 for (int i = 0; i < config.subscription_size(); ++i) {
643 const Subscription& subscription = config.subscription(i);
Yangster-mac932ecec2018-02-01 10:23:52 -0800644 if (subscription.rule_type() != Subscription::ALERT) {
645 continue;
646 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800647 if (subscription.subscriber_information_case() ==
648 Subscription::SubscriberInformationCase::SUBSCRIBER_INFORMATION_NOT_SET) {
649 ALOGW("subscription \"%lld\" has no subscriber info.\"",
650 (long long)subscription.id());
651 return false;
652 }
653 const auto& itr = anomalyTrackerMap.find(subscription.rule_id());
654 if (itr == anomalyTrackerMap.end()) {
655 ALOGW("subscription \"%lld\" has unknown rule id: \"%lld\"",
656 (long long)subscription.id(), (long long)subscription.rule_id());
657 return false;
658 }
659 const int anomalyTrackerIndex = itr->second;
660 allAnomalyTrackers[anomalyTrackerIndex]->addSubscription(subscription);
661 }
Yangster-mace2cd6d52017-11-09 20:38:30 -0800662 return true;
663}
664
Yangster-mac932ecec2018-02-01 10:23:52 -0800665bool initAlarms(const StatsdConfig& config, const ConfigKey& key,
666 const sp<AlarmMonitor>& periodicAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700667 const int64_t timeBaseNs, const int64_t currentTimeNs,
Yangster-mac932ecec2018-02-01 10:23:52 -0800668 vector<sp<AlarmTracker>>& allAlarmTrackers) {
669 unordered_map<int64_t, int> alarmTrackerMap;
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700670 int64_t startMillis = timeBaseNs / 1000 / 1000;
671 int64_t currentTimeMillis = currentTimeNs / 1000 /1000;
Yangster-mac932ecec2018-02-01 10:23:52 -0800672 for (int i = 0; i < config.alarm_size(); i++) {
673 const Alarm& alarm = config.alarm(i);
674 if (alarm.offset_millis() <= 0) {
675 ALOGW("Alarm offset_millis should be larger than 0.");
676 return false;
677 }
678 if (alarm.period_millis() <= 0) {
679 ALOGW("Alarm period_millis should be larger than 0.");
680 return false;
681 }
682 alarmTrackerMap.insert(std::make_pair(alarm.id(), allAlarmTrackers.size()));
683 allAlarmTrackers.push_back(
Yangster-macc04feba2018-04-02 14:37:33 -0700684 new AlarmTracker(startMillis, currentTimeMillis,
685 alarm, key, periodicAlarmMonitor));
Yangster-mac932ecec2018-02-01 10:23:52 -0800686 }
687 for (int i = 0; i < config.subscription_size(); ++i) {
688 const Subscription& subscription = config.subscription(i);
689 if (subscription.rule_type() != Subscription::ALARM) {
690 continue;
691 }
692 if (subscription.subscriber_information_case() ==
693 Subscription::SubscriberInformationCase::SUBSCRIBER_INFORMATION_NOT_SET) {
694 ALOGW("subscription \"%lld\" has no subscriber info.\"",
695 (long long)subscription.id());
696 return false;
697 }
698 const auto& itr = alarmTrackerMap.find(subscription.rule_id());
699 if (itr == alarmTrackerMap.end()) {
700 ALOGW("subscription \"%lld\" has unknown rule id: \"%lld\"",
701 (long long)subscription.id(), (long long)subscription.rule_id());
702 return false;
703 }
704 const int trackerIndex = itr->second;
705 allAlarmTrackers[trackerIndex]->addSubscription(subscription);
706 }
707 return true;
708}
709
Yangster-mac849dfdc22018-10-12 15:41:45 -0700710bool initMetricActivations(const ConfigKey& key, const StatsdConfig& config,
711 const int64_t currentTimeNs,
712 const unordered_map<int64_t, int> &logEventTrackerMap,
713 const unordered_map<int64_t, int> &metricProducerMap,
714 vector<sp<MetricProducer>>& allMetricProducers,
715 unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700716 unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
Yangster-mac849dfdc22018-10-12 15:41:45 -0700717 vector<int>& metricsWithActivation) {
718 for (int i = 0; i < config.metric_activation_size(); ++i) {
719 const MetricActivation& metric_activation = config.metric_activation(i);
720 auto itr = metricProducerMap.find(metric_activation.metric_id());
721 if (itr == metricProducerMap.end()) {
722 ALOGE("Metric id not found in metric activation: %lld",
723 (long long)metric_activation.metric_id());
724 return false;
725 }
726 const int metricTrackerIndex = itr->second;
727 if (metricTrackerIndex < 0 || metricTrackerIndex >= (int)allMetricProducers.size()) {
728 ALOGE("Invalid metric tracker index.");
729 return false;
730 }
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700731 const sp<MetricProducer>& metric = allMetricProducers[metricTrackerIndex];
Yangster-mac849dfdc22018-10-12 15:41:45 -0700732 metricsWithActivation.push_back(metricTrackerIndex);
733 for (int j = 0; j < metric_activation.event_activation_size(); ++j) {
734 const EventActivation& activation = metric_activation.event_activation(j);
735 auto logTrackerIt = logEventTrackerMap.find(activation.atom_matcher_id());
736 if (logTrackerIt == logEventTrackerMap.end()) {
737 ALOGE("Atom matcher not found for event activation.");
738 return false;
739 }
740 const int atomMatcherIndex = logTrackerIt->second;
741 activationAtomTrackerToMetricMap[atomMatcherIndex].push_back(
742 metricTrackerIndex);
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700743
Muhammad Qureshi15f8da92019-04-05 10:10:40 -0700744 ActivationType activationType;
745 if (activation.has_activation_type()) {
746 activationType = activation.activation_type();
747 } else {
748 activationType = metric_activation.activation_type();
749 }
750
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700751 if (activation.has_deactivation_atom_matcher_id()) {
752 auto deactivationAtomMatcherIt =
753 logEventTrackerMap.find(activation.deactivation_atom_matcher_id());
754 if (deactivationAtomMatcherIt == logEventTrackerMap.end()) {
755 ALOGE("Atom matcher not found for event deactivation.");
756 return false;
757 }
758 const int deactivationMatcherIndex = deactivationAtomMatcherIt->second;
759 deactivationAtomTrackerToMetricMap[deactivationMatcherIndex]
760 .push_back(metricTrackerIndex);
Muhammad Qureshi15f8da92019-04-05 10:10:40 -0700761 metric->addActivation(atomMatcherIndex, activationType, activation.ttl_seconds(),
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700762 deactivationMatcherIndex);
763 } else {
Muhammad Qureshi15f8da92019-04-05 10:10:40 -0700764 metric->addActivation(atomMatcherIndex, activationType, activation.ttl_seconds());
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700765 }
Yangster-mac849dfdc22018-10-12 15:41:45 -0700766 }
767 }
768 return true;
769}
770
Tej Singh35c7a572019-05-01 16:47:54 -0700771void prepareFirstBucket(const vector<sp<MetricProducer>>& allMetricProducers) {
Tej Singh597c7162019-04-17 16:41:45 -0700772 for (const auto& metric: allMetricProducers) {
Tej Singh35c7a572019-05-01 16:47:54 -0700773 metric->prepareFirstBucket();
Tej Singh597c7162019-04-17 16:41:45 -0700774 }
775}
776
David Chenbd125272018-04-04 19:02:50 -0700777bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, UidMap& uidMap,
Chenjie Yue2219202018-06-08 10:07:51 -0700778 const sp<StatsPullerManager>& pullerManager,
Yangster-mac932ecec2018-02-01 10:23:52 -0800779 const sp<AlarmMonitor>& anomalyAlarmMonitor,
Chenjie Yue2219202018-06-08 10:07:51 -0700780 const sp<AlarmMonitor>& periodicAlarmMonitor, const int64_t timeBaseNs,
781 const int64_t currentTimeNs, set<int>& allTagIds,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800782 vector<sp<LogMatchingTracker>>& allAtomMatchers,
Yao Chencaf339d2017-10-06 16:01:10 -0700783 vector<sp<ConditionTracker>>& allConditionTrackers,
784 vector<sp<MetricProducer>>& allMetricProducers,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800785 vector<sp<AnomalyTracker>>& allAnomalyTrackers,
Yangster-mac932ecec2018-02-01 10:23:52 -0800786 vector<sp<AlarmTracker>>& allPeriodicAlarmTrackers,
Yao Chencaf339d2017-10-06 16:01:10 -0700787 unordered_map<int, std::vector<int>>& conditionToMetricMap,
788 unordered_map<int, std::vector<int>>& trackerToMetricMap,
Yangster-mac94e197c2018-01-02 16:03:03 -0800789 unordered_map<int, std::vector<int>>& trackerToConditionMap,
Yangster-mac849dfdc22018-10-12 15:41:45 -0700790 unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700791 unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
Yangster-mac849dfdc22018-10-12 15:41:45 -0700792 vector<int>& metricsWithActivation,
David Chenbd125272018-04-04 19:02:50 -0700793 std::set<int64_t>& noReportMetricIds) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800794 unordered_map<int64_t, int> logTrackerMap;
795 unordered_map<int64_t, int> conditionTrackerMap;
796 unordered_map<int64_t, int> metricProducerMap;
Yao Chencaf339d2017-10-06 16:01:10 -0700797
Yangster-mac20877162017-12-22 17:19:39 -0800798 if (!initLogTrackers(config, uidMap, logTrackerMap, allAtomMatchers, allTagIds)) {
Yao Chencaf339d2017-10-06 16:01:10 -0700799 ALOGE("initLogMatchingTrackers failed");
800 return false;
801 }
Tej Singh484524a2018-02-01 15:10:05 -0800802 VLOG("initLogMatchingTrackers succeed...");
Yao Chencaf339d2017-10-06 16:01:10 -0700803
Yao Chenb3561512017-11-21 18:07:17 -0800804 if (!initConditions(key, config, logTrackerMap, conditionTrackerMap, allConditionTrackers,
Yao Chencaf339d2017-10-06 16:01:10 -0700805 trackerToConditionMap)) {
806 ALOGE("initConditionTrackers failed");
807 return false;
808 }
809
Chenjie Yue2219202018-06-08 10:07:51 -0700810 if (!initMetrics(key, config, timeBaseNs, currentTimeNs, uidMap, pullerManager, logTrackerMap,
811 conditionTrackerMap, allAtomMatchers, allConditionTrackers, allMetricProducers,
David Chenbd125272018-04-04 19:02:50 -0700812 conditionToMetricMap, trackerToMetricMap, metricProducerMap,
813 noReportMetricIds)) {
Yao Chencaf339d2017-10-06 16:01:10 -0700814 ALOGE("initMetricProducers failed");
815 return false;
816 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800817 if (!initAlerts(config, metricProducerMap, anomalyAlarmMonitor, allMetricProducers,
818 allAnomalyTrackers)) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800819 ALOGE("initAlerts failed");
820 return false;
821 }
Yangster-macc04feba2018-04-02 14:37:33 -0700822 if (!initAlarms(config, key, periodicAlarmMonitor,
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700823 timeBaseNs, currentTimeNs, allPeriodicAlarmTrackers)) {
Yangster-mac932ecec2018-02-01 10:23:52 -0800824 ALOGE("initAlarms failed");
825 return false;
826 }
Yangster-mac849dfdc22018-10-12 15:41:45 -0700827 if (!initMetricActivations(key, config, currentTimeNs, logTrackerMap, metricProducerMap,
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700828 allMetricProducers, activationAtomTrackerToMetricMap,
829 deactivationAtomTrackerToMetricMap, metricsWithActivation)) {
Yangster-mac849dfdc22018-10-12 15:41:45 -0700830 ALOGE("initMetricActivations failed");
831 return false;
832 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800833
Tej Singh35c7a572019-05-01 16:47:54 -0700834 prepareFirstBucket(allMetricProducers);
Tej Singh597c7162019-04-17 16:41:45 -0700835
Yao Chencaf339d2017-10-06 16:01:10 -0700836 return true;
837}
838
839} // namespace statsd
840} // namespace os
841} // namespace android