• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/AtomMatchingTracker.h"
27 #include "metrics/MetricProducer.h"
28 
29 namespace android {
30 namespace os {
31 namespace statsd {
32 
33 // Helper functions for creating, validating, and updating config components from StatsdConfig.
34 // Should only be called from metrics_manager_util and config_update_utils.
35 
36 // Create a AtomMatchingTracker.
37 // input:
38 // [logMatcher]: the input AtomMatcher from the StatsdConfig
39 // [index]: the index of the matcher
40 // output:
41 // new AtomMatchingTracker, or null if the tracker is unable to be created
42 sp<AtomMatchingTracker> createAtomMatchingTracker(const AtomMatcher& logMatcher, const int index,
43                                                   const sp<UidMap>& uidMap);
44 
45 // Create a ConditionTracker.
46 // input:
47 // [predicate]: the input Predicate from the StatsdConfig
48 // [index]: the index of the condition tracker
49 // [atomMatchingTrackerMap]: map of atom matcher id to its index in allAtomMatchingTrackers
50 // output:
51 // new ConditionTracker, or null if the tracker is unable to be created
52 sp<ConditionTracker> createConditionTracker(
53         const ConfigKey& key, const Predicate& predicate, const int index,
54         const unordered_map<int64_t, int>& atomMatchingTrackerMap);
55 
56 // Get the hash of a metric, combining the activation if the metric has one.
57 bool getMetricProtoHash(const StatsdConfig& config, const google::protobuf::MessageLite& metric,
58                         const int64_t id,
59                         const std::unordered_map<int64_t, int>& metricToActivationMap,
60                         uint64_t& metricHash);
61 
62 // 1. Validates matcher existence
63 // 2. Enforces matchers with dimensions and those used for trigger_event are about one atom
64 // 3. Gets matcher index and updates tracker to metric map
65 bool handleMetricWithAtomMatchingTrackers(
66         const int64_t matcherId, const int metricIndex, const bool enforceOneAtom,
67         const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
68         const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
69         std::unordered_map<int, std::vector<int>>& trackerToMetricMap, int& logTrackerIndex);
70 
71 // 1. Validates condition existence, including those in links
72 // 2. Gets condition index and updates condition to metric map
73 bool handleMetricWithConditions(
74         const int64_t condition, const int metricIndex,
75         const std::unordered_map<int64_t, int>& conditionTrackerMap,
76         const ::google::protobuf::RepeatedPtrField<::android::os::statsd::MetricConditionLink>&
77                 links,
78         const std::vector<sp<ConditionTracker>>& allConditionTrackers, int& conditionIndex,
79         std::unordered_map<int, std::vector<int>>& conditionToMetricMap);
80 
81 // Validates a metricActivation and populates state.
82 // Fills the new event activation/deactivation maps, preserving the existing activations.
83 // Returns false if there are errors.
84 bool handleMetricActivationOnConfigUpdate(
85         const StatsdConfig& config, const int64_t metricId, const int metricIndex,
86         const std::unordered_map<int64_t, int>& metricToActivationMap,
87         const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
88         const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
89         const std::unordered_map<int, shared_ptr<Activation>>& oldEventActivationMap,
90         std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
91         std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
92         std::vector<int>& metricsWithActivation,
93         std::unordered_map<int, shared_ptr<Activation>>& newEventActivationMap,
94         std::unordered_map<int, std::vector<shared_ptr<Activation>>>& newEventDeactivationMap);
95 
96 // Creates a CountMetricProducer and updates the vectors/maps used by MetricsManager with
97 // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error.
98 optional<sp<MetricProducer>> createCountMetricProducerAndUpdateMetadata(
99         const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseNs,
100         const int64_t currentTimeNs, const CountMetric& metric, const int metricIndex,
101         const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
102         const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
103         std::vector<sp<ConditionTracker>>& allConditionTrackers,
104         const std::unordered_map<int64_t, int>& conditionTrackerMap,
105         const std::vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
106         const std::unordered_map<int64_t, int>& stateAtomIdMap,
107         const std::unordered_map<int64_t, std::unordered_map<int, int64_t>>& allStateGroupMaps,
108         const std::unordered_map<int64_t, int>& metricToActivationMap,
109         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
110         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
111         std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
112         std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
113         std::vector<int>& metricsWithActivation);
114 
115 // Creates a DurationMetricProducer and updates the vectors/maps used by MetricsManager with
116 // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error.
117 optional<sp<MetricProducer>> createDurationMetricProducerAndUpdateMetadata(
118         const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseNs,
119         const int64_t currentTimeNs, const DurationMetric& metric, const int metricIndex,
120         const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
121         const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
122         std::vector<sp<ConditionTracker>>& allConditionTrackers,
123         const std::unordered_map<int64_t, int>& conditionTrackerMap,
124         const std::vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
125         const std::unordered_map<int64_t, int>& stateAtomIdMap,
126         const std::unordered_map<int64_t, std::unordered_map<int, int64_t>>& allStateGroupMaps,
127         const std::unordered_map<int64_t, int>& metricToActivationMap,
128         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
129         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
130         std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
131         std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
132         std::vector<int>& metricsWithActivation);
133 
134 // Creates an EventMetricProducer and updates the vectors/maps used by MetricsManager with
135 // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error.
136 optional<sp<MetricProducer>> createEventMetricProducerAndUpdateMetadata(
137         const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseNs,
138         const EventMetric& metric, const int metricIndex,
139         const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
140         const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
141         std::vector<sp<ConditionTracker>>& allConditionTrackers,
142         const std::unordered_map<int64_t, int>& conditionTrackerMap,
143         const std::vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
144         const std::unordered_map<int64_t, int>& metricToActivationMap,
145         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
146         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
147         std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
148         std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
149         std::vector<int>& metricsWithActivation);
150 
151 // Creates a CountMetricProducer and updates the vectors/maps used by MetricsManager with
152 // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error.
153 optional<sp<MetricProducer>> createValueMetricProducerAndUpdateMetadata(
154         const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseNs,
155         const int64_t currentTimeNs, const sp<StatsPullerManager>& pullerManager,
156         const ValueMetric& metric, const int metricIndex,
157         const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
158         const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
159         std::vector<sp<ConditionTracker>>& allConditionTrackers,
160         const std::unordered_map<int64_t, int>& conditionTrackerMap,
161         const std::vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
162         const sp<EventMatcherWizard>& matcherWizard,
163         const std::unordered_map<int64_t, int>& stateAtomIdMap,
164         const std::unordered_map<int64_t, std::unordered_map<int, int64_t>>& allStateGroupMaps,
165         const std::unordered_map<int64_t, int>& metricToActivationMap,
166         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
167         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
168         std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
169         std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
170         std::vector<int>& metricsWithActivation);
171 
172 // Creates a GaugeMetricProducer and updates the vectors/maps used by MetricsManager with
173 // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error.
174 optional<sp<MetricProducer>> createGaugeMetricProducerAndUpdateMetadata(
175         const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseNs,
176         const int64_t currentTimeNs, const sp<StatsPullerManager>& pullerManager,
177         const GaugeMetric& metric, const int metricIndex,
178         const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
179         const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
180         std::vector<sp<ConditionTracker>>& allConditionTrackers,
181         const std::unordered_map<int64_t, int>& conditionTrackerMap,
182         const std::vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
183         const sp<EventMatcherWizard>& matcherWizard,
184         const std::unordered_map<int64_t, int>& metricToActivationMap,
185         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
186         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
187         std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
188         std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
189         std::vector<int>& metricsWithActivation);
190 
191 // Creates an AnomalyTracker and adds it to the appropriate metric.
192 // Returns an sp to the AnomalyTracker, or nullopt if there was an error.
193 optional<sp<AnomalyTracker>> createAnomalyTracker(
194         const Alert& alert, const sp<AlarmMonitor>& anomalyAlarmMonitor,
195         const UpdateStatus& updateStatus, const int64_t currentTimeNs,
196         const std::unordered_map<int64_t, int>& metricProducerMap,
197         std::vector<sp<MetricProducer>>& allMetricProducers);
198 
199 // Templated function for adding subscriptions to alarms or alerts. Returns true if successful.
200 template <typename T>
initSubscribersForSubscriptionType(const StatsdConfig & config,const Subscription_RuleType ruleType,const std::unordered_map<int64_t,int> & ruleMap,std::vector<T> & allRules)201 bool initSubscribersForSubscriptionType(const StatsdConfig& config,
202                                         const Subscription_RuleType ruleType,
203                                         const std::unordered_map<int64_t, int>& ruleMap,
204                                         std::vector<T>& allRules) {
205     for (int i = 0; i < config.subscription_size(); ++i) {
206         const Subscription& subscription = config.subscription(i);
207         if (subscription.rule_type() != ruleType) {
208             continue;
209         }
210         if (subscription.subscriber_information_case() ==
211             Subscription::SubscriberInformationCase::SUBSCRIBER_INFORMATION_NOT_SET) {
212             ALOGW("subscription \"%lld\" has no subscriber info.\"", (long long)subscription.id());
213             return false;
214         }
215         const auto& itr = ruleMap.find(subscription.rule_id());
216         if (itr == ruleMap.end()) {
217             ALOGW("subscription \"%lld\" has unknown rule id: \"%lld\"",
218                   (long long)subscription.id(), (long long)subscription.rule_id());
219             return false;
220         }
221         const int ruleIndex = itr->second;
222         allRules[ruleIndex]->addSubscription(subscription);
223     }
224     return true;
225 }
226 
227 // Helper functions for MetricsManager to initialize from StatsdConfig.
228 // *Note*: only initStatsdConfig() should be called from outside.
229 // All other functions are intermediate
230 // steps, created to make unit tests easier. And most of the parameters in these
231 // functions are temporary objects in the initialization phase.
232 
233 // Initialize the AtomMatchingTrackers.
234 // input:
235 // [key]: the config key that this config belongs to
236 // [config]: the input StatsdConfig
237 // output:
238 // [atomMatchingTrackerMap]: this map should contain matcher name to index mapping
239 // [allAtomMatchingTrackers]: should store the sp to all the AtomMatchingTracker
240 // [allTagIds]: contains the set of all interesting tag ids to this config.
241 bool initAtomMatchingTrackers(const StatsdConfig& config, const sp<UidMap>& uidMap,
242                               std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
243                               std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
244                               std::set<int>& allTagIds);
245 
246 // Initialize ConditionTrackers
247 // input:
248 // [key]: the config key that this config belongs to
249 // [config]: the input config
250 // [atomMatchingTrackerMap]: AtomMatchingTracker name to index mapping from previous step.
251 // output:
252 // [conditionTrackerMap]: this map should contain condition name to index mapping
253 // [allConditionTrackers]: stores the sp to all the ConditionTrackers
254 // [trackerToConditionMap]: contain the mapping from index of
255 //                        log tracker to condition trackers that use the log tracker
256 // [initialConditionCache]: stores the initial conditions for each ConditionTracker
257 bool initConditions(const ConfigKey& key, const StatsdConfig& config,
258                     const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
259                     std::unordered_map<int64_t, int>& conditionTrackerMap,
260                     std::vector<sp<ConditionTracker>>& allConditionTrackers,
261                     std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
262                     std::vector<ConditionState>& initialConditionCache);
263 
264 // Initialize State maps using State protos in the config. These maps will
265 // eventually be passed to MetricProducers to initialize their state info.
266 // input:
267 // [config]: the input config
268 // output:
269 // [stateAtomIdMap]: this map should contain the mapping from state ids to atom ids
270 // [allStateGroupMaps]: this map should contain the mapping from states ids and state
271 //                      values to state group ids for all states
272 // [stateProtoHashes]: contains a map of state id to the hash of the State proto from the config
273 bool initStates(const StatsdConfig& config, unordered_map<int64_t, int>& stateAtomIdMap,
274                 unordered_map<int64_t, unordered_map<int, int64_t>>& allStateGroupMaps,
275                 std::map<int64_t, uint64_t>& stateProtoHashes);
276 
277 // Initialize MetricProducers.
278 // input:
279 // [key]: the config key that this config belongs to
280 // [config]: the input config
281 // [timeBaseSec]: start time base for all metrics
282 // [atomMatchingTrackerMap]: AtomMatchingTracker name to index mapping from previous step.
283 // [conditionTrackerMap]: condition name to index mapping
284 // [stateAtomIdMap]: contains the mapping from state ids to atom ids
285 // [allStateGroupMaps]: contains the mapping from atom ids and state values to
286 //                      state group ids for all states
287 // output:
288 // [allMetricProducers]: contains the list of sp to the MetricProducers created.
289 // [conditionToMetricMap]: contains the mapping from condition tracker index to
290 //                          the list of MetricProducer index
291 // [trackerToMetricMap]: contains the mapping from log tracker to MetricProducer index.
292 bool initMetrics(
293         const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseTimeNs,
294         const int64_t currentTimeNs, const sp<StatsPullerManager>& pullerManager,
295         const std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
296         const std::unordered_map<int64_t, int>& conditionTrackerMap,
297         const vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
298         const unordered_map<int64_t, int>& stateAtomIdMap,
299         const unordered_map<int64_t, unordered_map<int, int64_t>>& allStateGroupMaps,
300         vector<sp<ConditionTracker>>& allConditionTrackers,
301         const std::vector<ConditionState>& initialConditionCache,
302         std::vector<sp<MetricProducer>>& allMetricProducers,
303         std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
304         std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
305         std::set<int64_t>& noReportMetricIds,
306         std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
307         std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
308         std::vector<int>& metricsWithActivation);
309 
310 // Initialize alarms
311 // Is called both on initialize new configs and config updates since alarms do not have any state.
312 bool initAlarms(const StatsdConfig& config, const ConfigKey& key,
313                 const sp<AlarmMonitor>& periodicAlarmMonitor, const int64_t timeBaseNs,
314                 const int64_t currentTimeNs, std::vector<sp<AlarmTracker>>& allAlarmTrackers);
315 
316 // Initialize MetricsManager from StatsdConfig.
317 // Parameters are the members of MetricsManager. See MetricsManager for declaration.
318 bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, const sp<UidMap>& uidMap,
319                       const sp<StatsPullerManager>& pullerManager,
320                       const sp<AlarmMonitor>& anomalyAlarmMonitor,
321                       const sp<AlarmMonitor>& periodicAlarmMonitor, const int64_t timeBaseNs,
322                       const int64_t currentTimeNs, std::set<int>& allTagIds,
323                       std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
324                       std::unordered_map<int64_t, int>& atomMatchingTrackerMap,
325                       std::vector<sp<ConditionTracker>>& allConditionTrackers,
326                       std::unordered_map<int64_t, int>& conditionTrackerMap,
327                       std::vector<sp<MetricProducer>>& allMetricProducers,
328                       std::unordered_map<int64_t, int>& metricProducerMap,
329                       vector<sp<AnomalyTracker>>& allAnomalyTrackers,
330                       vector<sp<AlarmTracker>>& allPeriodicAlarmTrackers,
331                       std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
332                       std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
333                       std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
334                       std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
335                       std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
336                       std::unordered_map<int64_t, int>& alertTrackerMap,
337                       std::vector<int>& metricsWithActivation,
338                       std::map<int64_t, uint64_t>& stateProtoHashes,
339                       std::set<int64_t>& noReportMetricIds);
340 
341 }  // namespace statsd
342 }  // namespace os
343 }  // namespace android
344