blob: 6af7a9adca209205b49a596e41bc29545d4d6a94 [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 */
Yangster-mac932ecec2018-02-01 10:23:52 -080016
17#pragma once
18
Yao Chencaf339d2017-10-06 16:01:10 -070019#include <set>
20#include <unordered_map>
21#include <vector>
22
Yangster-mac932ecec2018-02-01 10:23:52 -080023#include "../anomaly/AlarmTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070024#include "../condition/ConditionTracker.h"
Chenjie Yue2219202018-06-08 10:07:51 -070025#include "../external/StatsPullerManager.h"
Yao Chencaf339d2017-10-06 16:01:10 -070026#include "../matchers/LogMatchingTracker.h"
Yangster-mac932ecec2018-02-01 10:23:52 -080027#include "../metrics/MetricProducer.h"
Yao Chencaf339d2017-10-06 16:01:10 -070028
29namespace android {
30namespace os {
31namespace 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 Chenb3561512017-11-21 18:07:17 -080041// [key]: the config key that this config belongs to
Yao Chencaf339d2017-10-06 16:01:10 -070042// [config]: the input StatsdConfig
43// output:
44// [logTrackerMap]: this map should contain matcher name to index mapping
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080045// [allAtomMatchers]: should store the sp to all the LogMatchingTracker
Yao Chencaf339d2017-10-06 16:01:10 -070046// [allTagIds]: contains the set of all interesting tag ids to this config.
47bool initLogTrackers(const StatsdConfig& config,
Yangster-mac94e197c2018-01-02 16:03:03 -080048 const UidMap& uidMap,
49 std::unordered_map<int64_t, int>& logTrackerMap,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080050 std::vector<sp<LogMatchingTracker>>& allAtomMatchers,
Yao Chencaf339d2017-10-06 16:01:10 -070051 std::set<int>& allTagIds);
52
53// Initialize ConditionTrackers
54// input:
Yao Chenb3561512017-11-21 18:07:17 -080055// [key]: the config key that this config belongs to
Yao Chencaf339d2017-10-06 16:01:10 -070056// [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 Chenb3561512017-11-21 18:07:17 -080063bool initConditions(const ConfigKey& key, const StatsdConfig& config,
Yangster-mac94e197c2018-01-02 16:03:03 -080064 const std::unordered_map<int64_t, int>& logTrackerMap,
65 std::unordered_map<int64_t, int>& conditionTrackerMap,
Yao Chencaf339d2017-10-06 16:01:10 -070066 std::vector<sp<ConditionTracker>>& allConditionTrackers,
Yao Chen729093d2017-10-16 10:33:26 -070067 std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
Stefan Lafona5b51912017-12-05 21:43:52 -080068 std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks);
Yao Chencaf339d2017-10-06 16:01:10 -070069
tsaichristined21aacf2019-10-07 14:47:38 -070070// 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
78bool initStates(const StatsdConfig& config, unordered_map<int64_t, int>& stateAtomIdMap,
79 unordered_map<int64_t, unordered_map<int, int64_t>>& allStateGroupMaps);
80
Yao Chencaf339d2017-10-06 16:01:10 -070081// Initialize MetricProducers.
82// input:
Yao Chenb3561512017-11-21 18:07:17 -080083// [key]: the config key that this config belongs to
Yao Chencaf339d2017-10-06 16:01:10 -070084// [config]: the input config
Chenjie Yu85ed8382017-12-14 16:48:54 -080085// [timeBaseSec]: start time base for all metrics
Yao Chencaf339d2017-10-06 16:01:10 -070086// [logTrackerMap]: LogMatchingTracker name to index mapping from previous step.
87// [conditionTrackerMap]: condition name to index mapping
tsaichristined21aacf2019-10-07 14:47:38 -070088// [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 Chencaf339d2017-10-06 16:01:10 -070091// 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 Chen729093d2017-10-16 10:33:26 -070096bool initMetrics(
Chenjie Yue2219202018-06-08 10:07:51 -070097 const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseTimeNs,
98 const int64_t currentTimeNs, UidMap& uidMap, const sp<StatsPullerManager>& pullerManager,
Yangster-mac94e197c2018-01-02 16:03:03 -080099 const std::unordered_map<int64_t, int>& logTrackerMap,
100 const std::unordered_map<int64_t, int>& conditionTrackerMap,
Stefan Lafona5b51912017-12-05 21:43:52 -0800101 const std::unordered_map<int, std::vector<MetricConditionLink>>& eventConditionLinks,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800102 const vector<sp<LogMatchingTracker>>& allAtomMatchers,
tsaichristined21aacf2019-10-07 14:47:38 -0700103 const unordered_map<int64_t, int>& stateAtomIdMap,
104 const unordered_map<int64_t, unordered_map<int, int64_t>>& allStateGroupMaps,
Yao Chen729093d2017-10-16 10:33:26 -0700105 vector<sp<ConditionTracker>>& allConditionTrackers,
106 std::vector<sp<MetricProducer>>& allMetricProducers,
107 std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
Yangster-mac94e197c2018-01-02 16:03:03 -0800108 std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
Ruchir Rastogi21a287b2019-10-02 12:04:33 -0700109 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 Chencaf339d2017-10-06 16:01:10 -0700113
Bookatzd3606c72017-10-19 10:13:49 -0700114// Initialize MetricsManager from StatsdConfig.
Yao Chencaf339d2017-10-06 16:01:10 -0700115// Parameters are the members of MetricsManager. See MetricsManager for declaration.
David Chenbd125272018-04-04 19:02:50 -0700116bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, UidMap& uidMap,
Chenjie Yue2219202018-06-08 10:07:51 -0700117 const sp<StatsPullerManager>& pullerManager,
Yangster-mac932ecec2018-02-01 10:23:52 -0800118 const sp<AlarmMonitor>& anomalyAlarmMonitor,
Chenjie Yue2219202018-06-08 10:07:51 -0700119 const sp<AlarmMonitor>& periodicAlarmMonitor, const int64_t timeBaseNs,
120 const int64_t currentTimeNs, std::set<int>& allTagIds,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800121 std::vector<sp<LogMatchingTracker>>& allAtomMatchers,
Yao Chencaf339d2017-10-06 16:01:10 -0700122 std::vector<sp<ConditionTracker>>& allConditionTrackers,
123 std::vector<sp<MetricProducer>>& allMetricProducers,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800124 vector<sp<AnomalyTracker>>& allAnomalyTrackers,
Yangster-mac932ecec2018-02-01 10:23:52 -0800125 vector<sp<AlarmTracker>>& allPeriodicAlarmTrackers,
Yao Chencaf339d2017-10-06 16:01:10 -0700126 std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
127 std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
Yangster-mac94e197c2018-01-02 16:03:03 -0800128 std::unordered_map<int, std::vector<int>>& trackerToConditionMap,
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700129 unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
130 unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
Jeffrey Huang38d70262020-03-19 17:28:59 -0700131 std::unordered_map<int64_t, int>& alertTrackerMap,
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700132 vector<int>& metricsWithActivation,
David Chenbd125272018-04-04 19:02:50 -0700133 std::set<int64_t>& noReportMetricIds);
Yao Chencaf339d2017-10-06 16:01:10 -0700134
Yao Chencaf339d2017-10-06 16:01:10 -0700135} // namespace statsd
136} // namespace os
137} // namespace android