Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 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 | */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 16 | |
| 17 | #pragma once |
| 18 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 19 | #include <set> |
| 20 | #include <unordered_map> |
| 21 | #include <vector> |
| 22 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 23 | #include "../anomaly/AlarmTracker.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 24 | #include "../condition/ConditionTracker.h" |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 25 | #include "../external/StatsPullerManager.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 26 | #include "../matchers/LogMatchingTracker.h" |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 27 | #include "../metrics/MetricProducer.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 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: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 41 | // [key]: the config key that this config belongs to |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 42 | // [config]: the input StatsdConfig |
| 43 | // output: |
| 44 | // [logTrackerMap]: this map should contain matcher name to index mapping |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 45 | // [allAtomMatchers]: should store the sp to all the LogMatchingTracker |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 46 | // [allTagIds]: contains the set of all interesting tag ids to this config. |
| 47 | bool initLogTrackers(const StatsdConfig& config, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 48 | const UidMap& uidMap, |
| 49 | std::unordered_map<int64_t, int>& logTrackerMap, |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 50 | std::vector<sp<LogMatchingTracker>>& allAtomMatchers, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 51 | std::set<int>& allTagIds); |
| 52 | |
| 53 | // Initialize ConditionTrackers |
| 54 | // input: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 55 | // [key]: the config key that this config belongs to |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 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 |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 63 | bool initConditions(const ConfigKey& key, const StatsdConfig& config, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 64 | const std::unordered_map<int64_t, int>& logTrackerMap, |
| 65 | std::unordered_map<int64_t, int>& conditionTrackerMap, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 66 | std::vector<sp<ConditionTracker>>& allConditionTrackers, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 67 | std::unordered_map<int, std::vector<int>>& trackerToConditionMap, |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 68 | std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 69 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 70 | // Initialize State maps using State protos in the config. These maps will |
| 71 | // eventually be passed to MetricProducers to initialize their state info. |
| 72 | // input: |
| 73 | // [config]: the input config |
| 74 | // output: |
| 75 | // [stateAtomIdMap]: this map should contain the mapping from state ids to atom ids |
| 76 | // [allStateGroupMaps]: this map should contain the mapping from states ids and state |
| 77 | // values to state group ids for all states |
| 78 | bool initStates(const StatsdConfig& config, unordered_map<int64_t, int>& stateAtomIdMap, |
| 79 | unordered_map<int64_t, unordered_map<int, int64_t>>& allStateGroupMaps); |
| 80 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 81 | // Initialize MetricProducers. |
| 82 | // input: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 83 | // [key]: the config key that this config belongs to |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 84 | // [config]: the input config |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 85 | // [timeBaseSec]: start time base for all metrics |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 86 | // [logTrackerMap]: LogMatchingTracker name to index mapping from previous step. |
| 87 | // [conditionTrackerMap]: condition name to index mapping |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 88 | // [stateAtomIdMap]: contains the mapping from state ids to atom ids |
| 89 | // [allStateGroupMaps]: contains the mapping from atom ids and state values to |
| 90 | // state group ids for all states |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 91 | // output: |
| 92 | // [allMetricProducers]: contains the list of sp to the MetricProducers created. |
| 93 | // [conditionToMetricMap]: contains the mapping from condition tracker index to |
| 94 | // the list of MetricProducer index |
| 95 | // [trackerToMetricMap]: contains the mapping from log tracker to MetricProducer index. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 96 | bool initMetrics( |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 97 | const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseTimeNs, |
| 98 | const int64_t currentTimeNs, UidMap& uidMap, const sp<StatsPullerManager>& pullerManager, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 99 | const std::unordered_map<int64_t, int>& logTrackerMap, |
| 100 | const std::unordered_map<int64_t, int>& conditionTrackerMap, |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 101 | const std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks, |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 102 | const vector<sp<LogMatchingTracker>>& allAtomMatchers, |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 103 | const unordered_map<int64_t, int>& stateAtomIdMap, |
| 104 | const unordered_map<int64_t, unordered_map<int, int64_t>>& allStateGroupMaps, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 105 | vector<sp<ConditionTracker>>& allConditionTrackers, |
| 106 | std::vector<sp<MetricProducer>>& allMetricProducers, |
| 107 | std::unordered_map<int, std::vector<int>>& conditionToMetricMap, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 108 | std::unordered_map<int, std::vector<int>>& trackerToMetricMap, |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 109 | std::set<int64_t>& noReportMetricIds, |
| 110 | std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap, |
| 111 | std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap, |
| 112 | std::vector<int>& metricsWithActivation); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 113 | |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 114 | // Initialize MetricsManager from StatsdConfig. |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 115 | // Parameters are the members of MetricsManager. See MetricsManager for declaration. |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 116 | bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, UidMap& uidMap, |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 117 | const sp<StatsPullerManager>& pullerManager, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 118 | const sp<AlarmMonitor>& anomalyAlarmMonitor, |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 119 | const sp<AlarmMonitor>& periodicAlarmMonitor, const int64_t timeBaseNs, |
| 120 | const int64_t currentTimeNs, std::set<int>& allTagIds, |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 121 | std::vector<sp<LogMatchingTracker>>& allAtomMatchers, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 122 | std::vector<sp<ConditionTracker>>& allConditionTrackers, |
| 123 | std::vector<sp<MetricProducer>>& allMetricProducers, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 124 | vector<sp<AnomalyTracker>>& allAnomalyTrackers, |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 125 | vector<sp<AlarmTracker>>& allPeriodicAlarmTrackers, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 126 | std::unordered_map<int, std::vector<int>>& conditionToMetricMap, |
| 127 | std::unordered_map<int, std::vector<int>>& trackerToMetricMap, |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 128 | std::unordered_map<int, std::vector<int>>& trackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 129 | unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap, |
| 130 | unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 131 | std::unordered_map<int64_t, int>& alertTrackerMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 132 | vector<int>& metricsWithActivation, |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 133 | std::set<int64_t>& noReportMetricIds); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 134 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 135 | } // namespace statsd |
| 136 | } // namespace os |
| 137 | } // namespace android |