• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 <vector>
20 
21 #include "anomaly/AlarmMonitor.h"
22 #include "anomaly/AlarmTracker.h"
23 #include "condition/ConditionTracker.h"
24 #include "external/StatsPullerManager.h"
25 #include "matchers/AtomMatchingTracker.h"
26 #include "metrics/MetricProducer.h"
27 
28 namespace android {
29 namespace os {
30 namespace statsd {
31 
32 // Helper functions for MetricsManager to update itself from a new StatsdConfig.
33 // *Note*: only updateStatsdConfig() should be called from outside this file.
34 // All other functions are intermediate steps, created to make unit testing easier.
35 
36 // Recursive function to determine if a matcher needs to be updated.
37 // input:
38 // [config]: the input StatsdConfig
39 // [matcherIdx]: the index of the current matcher to be updated
40 // [oldAtomMatchingTrackerMap]: matcher id to index mapping in the existing MetricsManager
41 // [oldAtomMatchingTrackers]: stores the existing AtomMatchingTrackers
42 // [newAtomMatchingTrackerMap]: matcher id to index mapping in the input StatsdConfig
43 // output:
44 // [matchersToUpdate]: vector of the update status of each matcher. The matcherIdx index will
45 //                     be updated from UPDATE_UNKNOWN after this call.
46 // [cycleTracker]: intermediate param used during recursion.
47 // Returns nullopt if successful and InvalidConfigReason if not.
48 optional<InvalidConfigReason> determineMatcherUpdateStatus(
49         const StatsdConfig& config, const int matcherIdx,
50         const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
51         const std::vector<sp<AtomMatchingTracker>>& oldAtomMatchingTrackers,
52         const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
53         std::vector<UpdateStatus>& matchersToUpdate, std::vector<bool>& cycleTracker);
54 
55 // Updates the AtomMatchingTrackers.
56 // input:
57 // [config]: the input StatsdConfig
58 // [oldAtomMatchingTrackerMap]: existing matcher id to index mapping
59 // [oldAtomMatchingTrackers]: stores the existing AtomMatchingTrackers
60 // output:
61 // [allTagIdsToMatchersMap]: maps of tag ids to atom matchers
62 // [newAtomMatchingTrackerMap]: new matcher id to index mapping
63 // [newAtomMatchingTrackers]: stores the new AtomMatchingTrackers
64 // [replacedMatchers]: set of matcher ids that changed and have been replaced
65 // Returns nullopt if successful and InvalidConfigReason if not.
66 optional<InvalidConfigReason> updateAtomMatchingTrackers(
67         const StatsdConfig& config, const sp<UidMap>& uidMap,
68         const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
69         const std::vector<sp<AtomMatchingTracker>>& oldAtomMatchingTrackers,
70         std::unordered_map<int, std::vector<int>>& allTagIdsToMatchersMap,
71         std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
72         std::vector<sp<AtomMatchingTracker>>& newAtomMatchingTrackers,
73         std::set<int64_t>& replacedMatchers);
74 
75 // Recursive function to determine if a condition needs to be updated.
76 // input:
77 // [config]: the input StatsdConfig
78 // [conditionIdx]: the index of the current condition to be updated
79 // [oldConditionTrackerMap]: condition id to index mapping in the existing MetricsManager
80 // [oldConditionTrackers]: stores the existing ConditionTrackers
81 // [newConditionTrackerMap]: condition id to index mapping in the input StatsdConfig
82 // [replacedMatchers]: set of replaced matcher ids. conditions using these matchers must be replaced
83 // output:
84 // [conditionsToUpdate]: vector of the update status of each condition. The conditionIdx index will
85 //                       be updated from UPDATE_UNKNOWN after this call.
86 // [cycleTracker]: intermediate param used during recursion.
87 // Returns nullopt if successful and InvalidConfigReason if not.
88 optional<InvalidConfigReason> determineConditionUpdateStatus(
89         const StatsdConfig& config, const int conditionIdx,
90         const std::unordered_map<int64_t, int>& oldConditionTrackerMap,
91         const std::vector<sp<ConditionTracker>>& oldConditionTrackers,
92         const std::unordered_map<int64_t, int>& newConditionTrackerMap,
93         const std::set<int64_t>& replacedMatchers, std::vector<UpdateStatus>& conditionsToUpdate,
94         std::vector<bool>& cycleTracker);
95 
96 // Updates ConditionTrackers
97 // input:
98 // [config]: the input config
99 // [atomMatchingTrackerMap]: AtomMatchingTracker name to index mapping from previous step.
100 // [replacedMatchers]: ids of replaced matchers. conditions depending on these must also be replaced
101 // [oldConditionTrackerMap]: existing matcher id to index mapping
102 // [oldConditionTrackers]: stores the existing ConditionTrackers
103 // output:
104 // [newConditionTrackerMap]: new condition id to index mapping
105 // [newConditionTrackers]: stores the sp to all the ConditionTrackers
106 // [trackerToConditionMap]: contains the mapping from the index of an atom matcher
107 //                          to indices of condition trackers that use the matcher
108 // [conditionCache]: stores the current conditions for each ConditionTracker
109 // [replacedConditions]: set of condition ids that have changed and have been replaced
110 // Returns nullopt if successful and InvalidConfigReason if not.
111 optional<InvalidConfigReason> updateConditions(
112         const ConfigKey& key, const StatsdConfig& config,
113         const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
114         const std::set<int64_t>& replacedMatchers,
115         const std::unordered_map<int64_t, int>& oldConditionTrackerMap,
116         const std::vector<sp<ConditionTracker>>& oldConditionTrackers,
117         std::unordered_map<int64_t, int>& newConditionTrackerMap,
118         std::vector<sp<ConditionTracker>>& newConditionTrackers,
119         std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
120         std::vector<ConditionState>& conditionCache, std::set<int64_t>& replacedConditions);
121 
122 optional<InvalidConfigReason> updateStates(
123         const StatsdConfig& config, const std::map<int64_t, uint64_t>& oldStateProtoHashes,
124         std::unordered_map<int64_t, int>& stateAtomIdMap,
125         std::unordered_map<int64_t, std::unordered_map<int, int64_t>>& allStateGroupMaps,
126         std::map<int64_t, uint64_t>& newStateProtoHashes, std::set<int64_t>& replacedStates);
127 
128 // Function to determine the update status (preserve/replace/new) of all metrics in the config.
129 // [config]: the input StatsdConfig
130 // [oldMetricProducerMap]: metric id to index mapping in the existing MetricsManager
131 // [oldMetricProducers]: stores the existing MetricProducers
132 // [metricToActivationMap]:  map from metric id to metric activation index
133 // [replacedMatchers]: set of replaced matcher ids. metrics using these matchers must be replaced
134 // [replacedConditions]: set of replaced conditions. metrics using these conditions must be replaced
135 // [replacedStates]: set of replaced state ids. metrics using these states must be replaced
136 // output:
137 // [metricsToUpdate]: update status of each metric. Will be changed from UPDATE_UNKNOWN
138 // Returns nullopt if successful and InvalidConfigReason if not.
139 optional<InvalidConfigReason> determineAllMetricUpdateStatuses(
140         const StatsdConfig& config, const unordered_map<int64_t, int>& oldMetricProducerMap,
141         const vector<sp<MetricProducer>>& oldMetricProducers,
142         const unordered_map<int64_t, int>& metricToActivationMap,
143         const set<int64_t>& replacedMatchers, const set<int64_t>& replacedConditions,
144         const set<int64_t>& replacedStates, vector<UpdateStatus>& metricsToUpdate);
145 
146 // Update MetricProducers.
147 // input:
148 // [key]: the config key that this config belongs to
149 // [config]: the input config
150 // [timeBaseNs]: start time base for all metrics
151 // [currentTimeNs]: time of the config update
152 // [atomMatchingTrackerMap]: AtomMatchingTracker name to index mapping from previous step.
153 // [replacedMatchers]: ids of replaced matchers. Metrics depending on these must also be replaced
154 // [allAtomMatchingTrackers]: stores the sp of the atom matchers.
155 // [conditionTrackerMap]: condition name to index mapping
156 // [replacedConditions]: set of condition ids that have changed and have been replaced
157 // [stateAtomIdMap]: contains the mapping from state ids to atom ids
158 // [allStateGroupMaps]: contains the mapping from atom ids and state values to
159 //                      state group ids for all states
160 // output:
161 // [allMetricProducers]: contains the list of sp to the MetricProducers created.
162 // [conditionToMetricMap]: contains the mapping from condition tracker index to
163 //                          the list of MetricProducer index
164 // [trackerToMetricMap]: contains the mapping from log tracker to MetricProducer index.
165 // Returns nullopt if successful and InvalidConfigReason if not.
166 optional<InvalidConfigReason> updateMetrics(
167         const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseNs,
168         const int64_t currentTimeNs, const sp<StatsPullerManager>& pullerManager,
169         const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
170         const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
171         const std::set<int64_t>& replacedMatchers,
172         const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
173         const std::unordered_map<int64_t, int>& conditionTrackerMap,
174         const std::set<int64_t>& replacedConditions,
175         std::vector<sp<ConditionTracker>>& allConditionTrackers,
176         const std::vector<ConditionState>& initialConditionCache,
177         const std::unordered_map<int64_t, int>& stateAtomIdMap,
178         const std::unordered_map<int64_t, std::unordered_map<int, int64_t>>& allStateGroupMaps,
179         const std::set<int64_t>& replacedStates,
180         const std::unordered_map<int64_t, int>& oldMetricProducerMap,
181         const std::vector<sp<MetricProducer>>& oldMetricProducers,
182         std::unordered_map<int64_t, int>& newMetricProducerMap,
183         std::vector<sp<MetricProducer>>& newMetricProducers,
184         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
185         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
186         std::set<int64_t>& noReportMetricIds,
187         std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
188         std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
189         std::vector<int>& metricsWithActivation, std::set<int64_t>& replacedMetrics);
190 
191 // Function to determine the update status (preserve/replace/new) of an alert.
192 // [alert]: the input Alert
193 // [oldAlertTrackerMap]: alert id to index mapping in the existing MetricsManager
194 // [oldAnomalyTrackers]: stores the existing AnomalyTrackers
195 // [replacedMetrics]: set of replaced metric ids. alerts using these metrics must be replaced
196 // output:
197 // [updateStatus]: update status of the alert. Will be changed from UPDATE_UNKNOWN
198 // Returns nullopt if successful and InvalidConfigReason if not.
199 optional<InvalidConfigReason> determineAlertUpdateStatus(
200         const Alert& alert, const std::unordered_map<int64_t, int>& oldAlertTrackerMap,
201         const std::vector<sp<AnomalyTracker>>& oldAnomalyTrackers,
202         const std::set<int64_t>& replacedMetrics, UpdateStatus& updateStatus);
203 
204 // Update MetricProducers.
205 // input:
206 // [config]: the input config
207 // [currentTimeNs]: time of the config update
208 // [metricProducerMap]: metric id to index mapping in the new config
209 // [replacedMetrics]: set of metric ids that have changed and were replaced
210 // [oldAlertTrackerMap]: alert id to index mapping in the existing MetricsManager.
211 // [oldAnomalyTrackers]: stores the existing AnomalyTrackers
212 // [anomalyAlarmMonitor]: AlarmMonitor used for duration metric anomaly detection
213 // [allMetricProducers]: stores the sp of the metric producers, AnomalyTrackers need to be added.
214 // [stateAtomIdMap]: contains the mapping from state ids to atom ids
215 // [allStateGroupMaps]: contains the mapping from atom ids and state values to
216 //                      state group ids for all states
217 // output:
218 // [newAlertTrackerMap]: mapping of alert id to index in the new config
219 // [newAnomalyTrackers]: contains the list of sp to the AnomalyTrackers created.
220 // Returns nullopt if successful and InvalidConfigReason if not.
221 optional<InvalidConfigReason> updateAlerts(
222         const StatsdConfig& config, const int64_t currentTimeNs,
223         const std::unordered_map<int64_t, int>& metricProducerMap,
224         const std::set<int64_t>& replacedMetrics,
225         const std::unordered_map<int64_t, int>& oldAlertTrackerMap,
226         const std::vector<sp<AnomalyTracker>>& oldAnomalyTrackers,
227         const sp<AlarmMonitor>& anomalyAlarmMonitor,
228         std::vector<sp<MetricProducer>>& allMetricProducers,
229         std::unordered_map<int64_t, int>& newAlertTrackerMap,
230         std::vector<sp<AnomalyTracker>>& newAnomalyTrackers);
231 
232 // Updates the existing MetricsManager from a new StatsdConfig.
233 // Parameters are the members of MetricsManager. See MetricsManager for declaration.
234 optional<InvalidConfigReason> updateStatsdConfig(
235         const ConfigKey& key, const StatsdConfig& config, const sp<UidMap>& uidMap,
236         const sp<StatsPullerManager>& pullerManager, const sp<AlarmMonitor>& anomalyAlarmMonitor,
237         const sp<AlarmMonitor>& periodicAlarmMonitor, const int64_t timeBaseNs,
238         const int64_t currentTimeNs,
239         const std::vector<sp<AtomMatchingTracker>>& oldAtomMatchingTrackers,
240         const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
241         const std::vector<sp<ConditionTracker>>& oldConditionTrackers,
242         const std::unordered_map<int64_t, int>& oldConditionTrackerMap,
243         const std::vector<sp<MetricProducer>>& oldMetricProducers,
244         const std::unordered_map<int64_t, int>& oldMetricProducerMap,
245         const std::vector<sp<AnomalyTracker>>& oldAnomalyTrackers,
246         const std::unordered_map<int64_t, int>& oldAlertTrackerMap,
247         const std::map<int64_t, uint64_t>& oldStateProtoHashes,
248         std::unordered_map<int, std::vector<int>>& allTagIdsToMatchersMap,
249         std::vector<sp<AtomMatchingTracker>>& newAtomMatchingTrackers,
250         std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
251         std::vector<sp<ConditionTracker>>& newConditionTrackers,
252         std::unordered_map<int64_t, int>& newConditionTrackerMap,
253         std::vector<sp<MetricProducer>>& newMetricProducers,
254         std::unordered_map<int64_t, int>& newMetricProducerMap,
255         std::vector<sp<AnomalyTracker>>& newAlertTrackers,
256         std::unordered_map<int64_t, int>& newAlertTrackerMap,
257         std::vector<sp<AlarmTracker>>& newPeriodicAlarmTrackers,
258         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
259         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
260         std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
261         std::unordered_map<int, std::vector<int>>& activationTrackerToMetricMap,
262         std::unordered_map<int, std::vector<int>>& deactivationTrackerToMetricMap,
263         std::vector<int>& metricsWithActivation, std::map<int64_t, uint64_t>& newStateProtoHashes,
264         std::set<int64_t>& noReportMetricIds);
265 
266 }  // namespace statsd
267 }  // namespace os
268 }  // namespace android
269