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 | */ |
| 16 | #ifndef METRIC_UTIL_H |
| 17 | #define METRIC_UTIL_H |
| 18 | #include <memory> |
| 19 | #include <set> |
| 20 | #include <unordered_map> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "../condition/ConditionTracker.h" |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 24 | #include "../external/StatsPullerManagerImpl.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 25 | #include "../matchers/LogMatchingTracker.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace statsd { |
| 30 | |
| 31 | // Helper functions for MetricsManager to initialize from StatsdConfig. |
| 32 | // *Note*: only initStatsdConfig() should be called from outside. |
| 33 | // All other functions are intermediate |
| 34 | // steps, created to make unit tests easier. And most of the parameters in these |
| 35 | // functions are temporary objects in the initialization phase. |
| 36 | |
| 37 | // Initialize the LogMatchingTrackers. |
| 38 | // input: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 39 | // [key]: the config key that this config belongs to |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 40 | // [config]: the input StatsdConfig |
| 41 | // output: |
| 42 | // [logTrackerMap]: this map should contain matcher name to index mapping |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 43 | // [allAtomMatchers]: should store the sp to all the LogMatchingTracker |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 44 | // [allTagIds]: contains the set of all interesting tag ids to this config. |
| 45 | bool initLogTrackers(const StatsdConfig& config, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 46 | const UidMap& uidMap, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 47 | std::unordered_map<std::string, int>& logTrackerMap, |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 48 | std::vector<sp<LogMatchingTracker>>& allAtomMatchers, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 49 | std::set<int>& allTagIds); |
| 50 | |
| 51 | // Initialize ConditionTrackers |
| 52 | // input: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 53 | // [key]: the config key that this config belongs to |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 54 | // [config]: the input config |
| 55 | // [logTrackerMap]: LogMatchingTracker name to index mapping from previous step. |
| 56 | // output: |
| 57 | // [conditionTrackerMap]: this map should contain condition name to index mapping |
| 58 | // [allConditionTrackers]: stores the sp to all the ConditionTrackers |
| 59 | // [trackerToConditionMap]: contain the mapping from index of |
| 60 | // log tracker to condition trackers that use the log tracker |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 61 | bool initConditions(const ConfigKey& key, const StatsdConfig& config, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 62 | const std::unordered_map<std::string, int>& logTrackerMap, |
| 63 | std::unordered_map<std::string, int>& conditionTrackerMap, |
| 64 | std::vector<sp<ConditionTracker>>& allConditionTrackers, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 65 | std::unordered_map<int, std::vector<int>>& trackerToConditionMap, |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 66 | std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 67 | |
| 68 | // Initialize MetricProducers. |
| 69 | // input: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 70 | // [key]: the config key that this config belongs to |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 71 | // [config]: the input config |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 72 | // [timeBaseSec]: start time base for all metrics |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 73 | // [logTrackerMap]: LogMatchingTracker name to index mapping from previous step. |
| 74 | // [conditionTrackerMap]: condition name to index mapping |
| 75 | // output: |
| 76 | // [allMetricProducers]: contains the list of sp to the MetricProducers created. |
| 77 | // [conditionToMetricMap]: contains the mapping from condition tracker index to |
| 78 | // the list of MetricProducer index |
| 79 | // [trackerToMetricMap]: contains the mapping from log tracker to MetricProducer index. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 80 | bool initMetrics( |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 81 | const ConfigKey& key, const StatsdConfig& config, const long timeBaseSec, |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 82 | const std::unordered_map<std::string, int>& logTrackerMap, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 83 | const std::unordered_map<std::string, int>& conditionTrackerMap, |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 84 | const std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks, |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 85 | const vector<sp<LogMatchingTracker>>& allAtomMatchers, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 86 | vector<sp<ConditionTracker>>& allConditionTrackers, |
| 87 | std::vector<sp<MetricProducer>>& allMetricProducers, |
| 88 | std::unordered_map<int, std::vector<int>>& conditionToMetricMap, |
| 89 | std::unordered_map<int, std::vector<int>>& trackerToMetricMap); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 90 | |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 91 | // Initialize MetricsManager from StatsdConfig. |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 92 | // Parameters are the members of MetricsManager. See MetricsManager for declaration. |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 93 | bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, |
| 94 | |
| 95 | const UidMap& uidMap, |
| 96 | const long timeBaseSec, std::set<int>& allTagIds, |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 97 | std::vector<sp<LogMatchingTracker>>& allAtomMatchers, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 98 | std::vector<sp<ConditionTracker>>& allConditionTrackers, |
| 99 | std::vector<sp<MetricProducer>>& allMetricProducers, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 100 | vector<sp<AnomalyTracker>>& allAnomalyTrackers, |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 101 | std::unordered_map<int, std::vector<int>>& conditionToMetricMap, |
| 102 | std::unordered_map<int, std::vector<int>>& trackerToMetricMap, |
| 103 | std::unordered_map<int, std::vector<int>>& trackerToConditionMap); |
| 104 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 105 | } // namespace statsd |
| 106 | } // namespace os |
| 107 | } // namespace android |
| 108 | #endif // METRIC_UTIL_H |