1 /*
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 <set>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "../anomaly/AlarmTracker.h"
24 #include "../condition/ConditionTracker.h"
25 #include "../external/StatsPullerManager.h"
26 #include "../matchers/LogMatchingTracker.h"
27 #include "../metrics/MetricProducer.h"
28 
29 namespace android {
30 namespace os {
31 namespace statsd {
32 
33 // Helper functions for MetricsManager to initialize from StatsdConfig.
34 // *Note*: only initStatsdConfig() should be called from outside.
35 // All other functions are intermediate
36 // steps, created to make unit tests easier. And most of the parameters in these
37 // functions are temporary objects in the initialization phase.
38 
39 // Initialize the LogMatchingTrackers.
40 // input:
41 // [key]: the config key that this config belongs to
42 // [config]: the input StatsdConfig
43 // output:
44 // [logTrackerMap]: this map should contain matcher name to index mapping
45 // [allAtomMatchers]: should store the sp to all the LogMatchingTracker
46 // [allTagIds]: contains the set of all interesting tag ids to this config.
47 bool initLogTrackers(const StatsdConfig& config,
48                      const UidMap& uidMap,
49                      std::unordered_map<int64_t, int>& logTrackerMap,
50                      std::vector<sp<LogMatchingTracker>>& allAtomMatchers,
51                      std::set<int>& allTagIds);
52 
53 // Initialize ConditionTrackers
54 // input:
55 // [key]: the config key that this config belongs to
56 // [config]: the input config
57 // [logTrackerMap]: LogMatchingTracker name to index mapping from previous step.
58 // output:
59 // [conditionTrackerMap]: this map should contain condition name to index mapping
60 // [allConditionTrackers]: stores the sp to all the ConditionTrackers
61 // [trackerToConditionMap]: contain the mapping from index of
62 //                        log tracker to condition trackers that use the log tracker
63 bool initConditions(const ConfigKey& key, const StatsdConfig& config,
64                     const std::unordered_map<int64_t, int>& logTrackerMap,
65                     std::unordered_map<int64_t, int>& conditionTrackerMap,
66                     std::vector<sp<ConditionTracker>>& allConditionTrackers,
67                     std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
68                     std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks);
69 
70 // Initialize MetricProducers.
71 // input:
72 // [key]: the config key that this config belongs to
73 // [config]: the input config
74 // [timeBaseSec]: start time base for all metrics
75 // [logTrackerMap]: LogMatchingTracker name to index mapping from previous step.
76 // [conditionTrackerMap]: condition name to index mapping
77 // output:
78 // [allMetricProducers]: contains the list of sp to the MetricProducers created.
79 // [conditionToMetricMap]: contains the mapping from condition tracker index to
80 //                          the list of MetricProducer index
81 // [trackerToMetricMap]: contains the mapping from log tracker to MetricProducer index.
82 bool initMetrics(
83         const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseTimeNs,
84         const int64_t currentTimeNs, UidMap& uidMap, const sp<StatsPullerManager>& pullerManager,
85         const std::unordered_map<int64_t, int>& logTrackerMap,
86         const std::unordered_map<int64_t, int>& conditionTrackerMap,
87         const std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks,
88         const vector<sp<LogMatchingTracker>>& allAtomMatchers,
89         vector<sp<ConditionTracker>>& allConditionTrackers,
90         std::vector<sp<MetricProducer>>& allMetricProducers,
91         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
92         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
93         std::set<int64_t>& noReportMetricIds);
94 
95 // Initialize MetricsManager from StatsdConfig.
96 // Parameters are the members of MetricsManager. See MetricsManager for declaration.
97 bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, UidMap& uidMap,
98                       const sp<StatsPullerManager>& pullerManager,
99                       const sp<AlarmMonitor>& anomalyAlarmMonitor,
100                       const sp<AlarmMonitor>& periodicAlarmMonitor, const int64_t timeBaseNs,
101                       const int64_t currentTimeNs, std::set<int>& allTagIds,
102                       std::vector<sp<LogMatchingTracker>>& allAtomMatchers,
103                       std::vector<sp<ConditionTracker>>& allConditionTrackers,
104                       std::vector<sp<MetricProducer>>& allMetricProducers,
105                       vector<sp<AnomalyTracker>>& allAnomalyTrackers,
106                       vector<sp<AlarmTracker>>& allPeriodicAlarmTrackers,
107                       std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
108                       std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
109                       std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
110                       unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
111                       unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
112                       vector<int>& metricsWithActivation,
113                       std::set<int64_t>& noReportMetricIds);
114 
115 bool isStateTracker(const SimplePredicate& simplePredicate, std::vector<Matcher>* primaryKeys);
116 
117 }  // namespace statsd
118 }  // namespace os
119 }  // namespace android
120