blob: 9ad5176ee6fdf459de627f6edebc3ded61b5a102 [file] [log] [blame]
Yao Chencaf339d2017-10-06 16:01:10 -07001/*
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 Yu6736c892017-11-09 10:50:09 -080024#include "../external/StatsPullerManagerImpl.h"
Yao Chencaf339d2017-10-06 16:01:10 -070025#include "../matchers/LogMatchingTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070026
27namespace android {
28namespace os {
29namespace 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 Chenb3561512017-11-21 18:07:17 -080039// [key]: the config key that this config belongs to
Yao Chencaf339d2017-10-06 16:01:10 -070040// [config]: the input StatsdConfig
41// output:
42// [logTrackerMap]: this map should contain matcher name to index mapping
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080043// [allAtomMatchers]: should store the sp to all the LogMatchingTracker
Yao Chencaf339d2017-10-06 16:01:10 -070044// [allTagIds]: contains the set of all interesting tag ids to this config.
45bool initLogTrackers(const StatsdConfig& config,
Yangster-mac20877162017-12-22 17:19:39 -080046 const UidMap& uidMap,
Yao Chencaf339d2017-10-06 16:01:10 -070047 std::unordered_map<std::string, int>& logTrackerMap,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080048 std::vector<sp<LogMatchingTracker>>& allAtomMatchers,
Yao Chencaf339d2017-10-06 16:01:10 -070049 std::set<int>& allTagIds);
50
51// Initialize ConditionTrackers
52// input:
Yao Chenb3561512017-11-21 18:07:17 -080053// [key]: the config key that this config belongs to
Yao Chencaf339d2017-10-06 16:01:10 -070054// [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 Chenb3561512017-11-21 18:07:17 -080061bool initConditions(const ConfigKey& key, const StatsdConfig& config,
Yao Chencaf339d2017-10-06 16:01:10 -070062 const std::unordered_map<std::string, int>& logTrackerMap,
63 std::unordered_map<std::string, int>& conditionTrackerMap,
64 std::vector<sp<ConditionTracker>>& allConditionTrackers,
Yao Chen729093d2017-10-16 10:33:26 -070065 std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
Stefan Lafona5b51912017-12-05 21:43:52 -080066 std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks);
Yao Chencaf339d2017-10-06 16:01:10 -070067
68// Initialize MetricProducers.
69// input:
Yao Chenb3561512017-11-21 18:07:17 -080070// [key]: the config key that this config belongs to
Yao Chencaf339d2017-10-06 16:01:10 -070071// [config]: the input config
Chenjie Yu85ed8382017-12-14 16:48:54 -080072// [timeBaseSec]: start time base for all metrics
Yao Chencaf339d2017-10-06 16:01:10 -070073// [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 Chen729093d2017-10-16 10:33:26 -070080bool initMetrics(
Chenjie Yu85ed8382017-12-14 16:48:54 -080081 const ConfigKey& key, const StatsdConfig& config, const long timeBaseSec,
Yao Chenb3561512017-11-21 18:07:17 -080082 const std::unordered_map<std::string, int>& logTrackerMap,
Yao Chen729093d2017-10-16 10:33:26 -070083 const std::unordered_map<std::string, int>& conditionTrackerMap,
Stefan Lafona5b51912017-12-05 21:43:52 -080084 const std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080085 const vector<sp<LogMatchingTracker>>& allAtomMatchers,
Yao Chen729093d2017-10-16 10:33:26 -070086 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 Chencaf339d2017-10-06 16:01:10 -070090
Bookatzd3606c72017-10-19 10:13:49 -070091// Initialize MetricsManager from StatsdConfig.
Yao Chencaf339d2017-10-06 16:01:10 -070092// Parameters are the members of MetricsManager. See MetricsManager for declaration.
Yangster-mac20877162017-12-22 17:19:39 -080093bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config,
94
95 const UidMap& uidMap,
96 const long timeBaseSec, std::set<int>& allTagIds,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080097 std::vector<sp<LogMatchingTracker>>& allAtomMatchers,
Yao Chencaf339d2017-10-06 16:01:10 -070098 std::vector<sp<ConditionTracker>>& allConditionTrackers,
99 std::vector<sp<MetricProducer>>& allMetricProducers,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800100 vector<sp<AnomalyTracker>>& allAnomalyTrackers,
Yao Chencaf339d2017-10-06 16:01:10 -0700101 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 Chencaf339d2017-10-06 16:01:10 -0700105} // namespace statsd
106} // namespace os
107} // namespace android
108#endif // METRIC_UTIL_H