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