blob: 9cae70a9de806fe3d7f048374acdae4e5a566337 [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32:50 -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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#pragma once
Yao Chen44cf27c2017-09-14 22:32:50 -070018
Yangster-mace2cd6d52017-11-09 20:38:30 -080019#include "anomaly/AnomalyMonitor.h"
20#include "anomaly/AnomalyTracker.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021#include "condition/ConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080022#include "config/ConfigKey.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070024#include "logd/LogEvent.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025#include "matchers/LogMatchingTracker.h"
26#include "metrics/MetricProducer.h"
Yao Chend10f7b12017-12-18 12:53:50 -080027#include "packages/UidMap.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070028
Yao Chen44cf27c2017-09-14 22:32:50 -070029#include <unordered_map>
Yao Chen44cf27c2017-09-14 22:32:50 -070030
31namespace android {
32namespace os {
33namespace statsd {
34
35// A MetricsManager is responsible for managing metrics from one single config source.
Yao Chend10f7b12017-12-18 12:53:50 -080036class MetricsManager : public PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070037public:
Yao Chend10f7b12017-12-18 12:53:50 -080038 MetricsManager(const ConfigKey& configKey, const StatsdConfig& config, const long timeBaseSec,
39 sp<UidMap> uidMap);
Yao Chen44cf27c2017-09-14 22:32:50 -070040
David Chend9269e22017-12-05 13:43:51 -080041 virtual ~MetricsManager();
Yao Chen44cf27c2017-09-14 22:32:50 -070042
Yao Chencaf339d2017-10-06 16:01:10 -070043 // Return whether the configuration is valid.
44 bool isConfigValid() const;
45
Joe Onoratoc4dfae52017-10-17 23:38:21 -070046 void onLogEvent(const LogEvent& event);
Yao Chen44cf27c2017-09-14 22:32:50 -070047
Yangster-mac20877162017-12-22 17:19:39 -080048 void onAnomalyAlarmFired(
49 const uint64_t timestampNs,
50 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet);
Yangster-mace2cd6d52017-11-09 20:38:30 -080051
52 void setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor);
53
David Chen27785a82018-01-19 17:06:45 -080054 void notifyAppUpgrade(const uint64_t& eventTimeNs, const string& apk, const int uid,
55 const int64_t version) override;
Yao Chend10f7b12017-12-18 12:53:50 -080056
David Chen27785a82018-01-19 17:06:45 -080057 void notifyAppRemoved(const uint64_t& eventTimeNs, const string& apk, const int uid) override;
Yao Chend10f7b12017-12-18 12:53:50 -080058
David Chen27785a82018-01-19 17:06:45 -080059 void onUidMapReceived(const uint64_t& eventTimeNs) override;
Yao Chend10f7b12017-12-18 12:53:50 -080060
Yao Chen147ce602017-12-22 14:35:34 -080061 bool shouldAddUidMapListener() const {
62 return !mAllowedPkg.empty();
63 }
64
Yao Chen884c8c12018-01-26 10:36:25 -080065 void dumpStates(FILE* out, bool verbose);
66
Yao Chen729093d2017-10-16 10:33:26 -070067 // Config source owner can call onDumpReport() to get all the metrics collected.
Yao Chen288c6002017-12-12 13:43:18 -080068 virtual void onDumpReport(android::util::ProtoOutputStream* protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -080069 virtual void onDumpReport(const uint64_t& dumpTimeStampNs, ConfigMetricsReport* report);
Yao Chen729093d2017-10-16 10:33:26 -070070
David Chen1d7b0cd2017-11-15 14:20:04 -080071 // Computes the total byte size of all metrics managed by a single config source.
72 // Does not change the state.
David Chend9269e22017-12-05 13:43:51 -080073 virtual size_t byteSize();
Yao Chen44cf27c2017-09-14 22:32:50 -070074private:
Yao Chenb3561512017-11-21 18:07:17 -080075 const ConfigKey mConfigKey;
76
Yao Chend10f7b12017-12-18 12:53:50 -080077 sp<UidMap> mUidMap;
78
79 bool mConfigValid = false;
80
81 // The uid log sources from StatsdConfig.
82 std::vector<int32_t> mAllowedUid;
83
84 // The pkg log sources from StatsdConfig.
85 std::vector<std::string> mAllowedPkg;
86
87 // The combined uid sources (after translating pkg name to uid).
88 // Logs from uids that are not in the list will be ignored to avoid spamming.
89 std::set<int32_t> mAllowedLogSources;
90
91 // To guard access to mAllowedLogSources
92 mutable std::mutex mAllowedLogSourcesMutex;
93
Yao Chen44cf27c2017-09-14 22:32:50 -070094 // All event tags that are interesting to my metrics.
95 std::set<int> mTagIds;
96
Yao Chencaf339d2017-10-06 16:01:10 -070097 // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in
Bookatzd3606c72017-10-19 10:13:49 -070098 // MetricsManager. There are relationships between them, and the relationships are denoted by
99 // index instead of pointers. The reasons for this are: (1) the relationship between them are
100 // complicated, so storing index instead of pointers reduces the risk that A holds B's sp, and B
101 // holds A's sp. (2) When we evaluate matcher results, or condition results, we can quickly get
102 // the related results from a cache using the index.
Yao Chen44cf27c2017-09-14 22:32:50 -0700103
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800104 // Hold all the atom matchers from the config.
105 std::vector<sp<LogMatchingTracker>> mAllAtomMatchers;
Yao Chen44cf27c2017-09-14 22:32:50 -0700106
Yao Chencaf339d2017-10-06 16:01:10 -0700107 // Hold all the conditions from the config.
108 std::vector<sp<ConditionTracker>> mAllConditionTrackers;
109
110 // Hold all metrics from the config.
111 std::vector<sp<MetricProducer>> mAllMetricProducers;
112
Yangster-mace2cd6d52017-11-09 20:38:30 -0800113 // Hold all alert trackers.
114 std::vector<sp<AnomalyTracker>> mAllAnomalyTrackers;
115
Yao Chencaf339d2017-10-06 16:01:10 -0700116 // To make the log processing more efficient, we want to do as much filtering as possible
117 // before we go into individual trackers and conditions to match.
118
119 // 1st filter: check if the event tag id is in mTagIds.
120 // 2nd filter: if it is, we parse the event because there is at least one member is interested.
121 // then pass to all LogMatchingTrackers (itself also filter events by ids).
122 // 3nd filter: for LogMatchingTrackers that matched this event, we pass this event to the
123 // ConditionTrackers and MetricProducers that use this matcher.
124 // 4th filter: for ConditionTrackers that changed value due to this event, we pass
125 // new conditions to metrics that use this condition.
126
127 // The following map is initialized from the statsd_config.
128
129 // maps from the index of the LogMatchingTracker to index of MetricProducer.
130 std::unordered_map<int, std::vector<int>> mTrackerToMetricMap;
131
132 // maps from LogMatchingTracker to ConditionTracker
133 std::unordered_map<int, std::vector<int>> mTrackerToConditionMap;
134
135 // maps from ConditionTracker to MetricProducer
136 std::unordered_map<int, std::vector<int>> mConditionToMetricMap;
137
Yao Chend10f7b12017-12-18 12:53:50 -0800138 void initLogSourceWhiteList();
Yangster-mac20877162017-12-22 17:19:39 -0800139
Yangster-mac94e197c2018-01-02 16:03:03 -0800140 // The metrics that don't need to be uploaded or even reported.
141 std::set<int64_t> mNoReportMetricIds;
142
Yangster-mac20877162017-12-22 17:19:39 -0800143 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions);
144 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks);
Yangster-macb5bc7412018-01-06 23:17:45 -0800145 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice);
Yangster-mac87718e22018-01-11 16:16:26 -0800146 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster-mac93694462018-01-22 20:49:31 -0800147 FRIEND_TEST(DimensionInConditionE2eTest, TestCountMetricNoLink);
148 FRIEND_TEST(DimensionInConditionE2eTest, TestCountMetricWithLink);
149 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetricNoLink);
150 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetricWithLink);
Yao Chen44cf27c2017-09-14 22:32:50 -0700151};
152
153} // namespace statsd
154} // namespace os
155} // namespace android