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