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