blob: b50ef4a0c5a20ac7e604b69a9032bd100d4a879f [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-mac932ecec2018-02-01 10:23:52 -080019#include "anomaly/AlarmMonitor.h"
20#include "anomaly/AlarmTracker.h"
Yangster-mace2cd6d52017-11-09 20:38:30 -080021#include "anomaly/AnomalyTracker.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070022#include "condition/ConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080023#include "config/ConfigKey.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070025#include "logd/LogEvent.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026#include "matchers/LogMatchingTracker.h"
27#include "metrics/MetricProducer.h"
Yao Chend10f7b12017-12-18 12:53:50 -080028#include "packages/UidMap.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070029
Yao Chen44cf27c2017-09-14 22:32:50 -070030#include <unordered_map>
Yao Chen44cf27c2017-09-14 22:32:50 -070031
32namespace android {
33namespace os {
34namespace statsd {
35
36// A MetricsManager is responsible for managing metrics from one single config source.
Yao Chend10f7b12017-12-18 12:53:50 -080037class MetricsManager : public PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070038public:
Yao Chend10f7b12017-12-18 12:53:50 -080039 MetricsManager(const ConfigKey& configKey, const StatsdConfig& config, const long timeBaseSec,
Yangster-mac932ecec2018-02-01 10:23:52 -080040 const sp<UidMap>& uidMap, const sp<AlarmMonitor>& anomalyAlarmMonitor,
41 const sp<AlarmMonitor>& periodicAlarmMonitor);
Yao Chen44cf27c2017-09-14 22:32:50 -070042
David Chend9269e22017-12-05 13:43:51 -080043 virtual ~MetricsManager();
Yao Chen44cf27c2017-09-14 22:32:50 -070044
Yao Chencaf339d2017-10-06 16:01:10 -070045 // Return whether the configuration is valid.
46 bool isConfigValid() const;
47
Joe Onoratoc4dfae52017-10-17 23:38:21 -070048 void onLogEvent(const LogEvent& event);
Yao Chen44cf27c2017-09-14 22:32:50 -070049
Yangster-mac20877162017-12-22 17:19:39 -080050 void onAnomalyAlarmFired(
51 const uint64_t timestampNs,
Yangster-mac932ecec2018-02-01 10:23:52 -080052 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -080053
Yangster-mac932ecec2018-02-01 10:23:52 -080054 void onPeriodicAlarmFired(
55 const uint64_t timestampNs,
56 unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>>& alarmSet);
Yangster-mace2cd6d52017-11-09 20:38:30 -080057
David Chen27785a82018-01-19 17:06:45 -080058 void notifyAppUpgrade(const uint64_t& eventTimeNs, const string& apk, const int uid,
59 const int64_t version) override;
Yao Chend10f7b12017-12-18 12:53:50 -080060
David Chen27785a82018-01-19 17:06:45 -080061 void notifyAppRemoved(const uint64_t& eventTimeNs, const string& apk, const int uid) override;
Yao Chend10f7b12017-12-18 12:53:50 -080062
David Chen27785a82018-01-19 17:06:45 -080063 void onUidMapReceived(const uint64_t& eventTimeNs) override;
Yao Chend10f7b12017-12-18 12:53:50 -080064
Yao Chen147ce602017-12-22 14:35:34 -080065 bool shouldAddUidMapListener() const {
66 return !mAllowedPkg.empty();
67 }
68
Yao Chen884c8c12018-01-26 10:36:25 -080069 void dumpStates(FILE* out, bool verbose);
70
David Chen16049572018-02-01 18:27:51 -080071 // Returns the elapsed realtime when this metric manager last reported metrics.
72 uint64_t getLastReportTimeNs() {
73 return mLastReportTimeNs;
74 };
75
Yao Chen729093d2017-10-16 10:33:26 -070076 // Config source owner can call onDumpReport() to get all the metrics collected.
Yao Chen8a8d16c2018-02-08 14:50:40 -080077 virtual void onDumpReport(const uint64_t dumpTimeNs,
78 android::util::ProtoOutputStream* protoOutput);
Yao Chen729093d2017-10-16 10:33:26 -070079
David Chen1d7b0cd2017-11-15 14:20:04 -080080 // Computes the total byte size of all metrics managed by a single config source.
81 // Does not change the state.
David Chend9269e22017-12-05 13:43:51 -080082 virtual size_t byteSize();
Yao Chen44cf27c2017-09-14 22:32:50 -070083private:
Yao Chenb3561512017-11-21 18:07:17 -080084 const ConfigKey mConfigKey;
85
Yao Chend10f7b12017-12-18 12:53:50 -080086 sp<UidMap> mUidMap;
87
88 bool mConfigValid = false;
89
David Chen16049572018-02-01 18:27:51 -080090 uint64_t mLastReportTimeNs;
91
Yao Chend10f7b12017-12-18 12:53:50 -080092 // The uid log sources from StatsdConfig.
93 std::vector<int32_t> mAllowedUid;
94
95 // The pkg log sources from StatsdConfig.
96 std::vector<std::string> mAllowedPkg;
97
98 // The combined uid sources (after translating pkg name to uid).
99 // Logs from uids that are not in the list will be ignored to avoid spamming.
100 std::set<int32_t> mAllowedLogSources;
101
102 // To guard access to mAllowedLogSources
103 mutable std::mutex mAllowedLogSourcesMutex;
104
Yao Chen44cf27c2017-09-14 22:32:50 -0700105 // All event tags that are interesting to my metrics.
106 std::set<int> mTagIds;
107
Yao Chencaf339d2017-10-06 16:01:10 -0700108 // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in
Bookatzd3606c72017-10-19 10:13:49 -0700109 // MetricsManager. There are relationships between them, and the relationships are denoted by
110 // index instead of pointers. The reasons for this are: (1) the relationship between them are
111 // complicated, so storing index instead of pointers reduces the risk that A holds B's sp, and B
112 // holds A's sp. (2) When we evaluate matcher results, or condition results, we can quickly get
113 // the related results from a cache using the index.
Yao Chen44cf27c2017-09-14 22:32:50 -0700114
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800115 // Hold all the atom matchers from the config.
116 std::vector<sp<LogMatchingTracker>> mAllAtomMatchers;
Yao Chen44cf27c2017-09-14 22:32:50 -0700117
Yao Chencaf339d2017-10-06 16:01:10 -0700118 // Hold all the conditions from the config.
119 std::vector<sp<ConditionTracker>> mAllConditionTrackers;
120
121 // Hold all metrics from the config.
122 std::vector<sp<MetricProducer>> mAllMetricProducers;
123
Yangster-mace2cd6d52017-11-09 20:38:30 -0800124 // Hold all alert trackers.
125 std::vector<sp<AnomalyTracker>> mAllAnomalyTrackers;
126
Yangster-mac932ecec2018-02-01 10:23:52 -0800127 // Hold all periodic alarm trackers.
128 std::vector<sp<AlarmTracker>> mAllPeriodicAlarmTrackers;
129
Yao Chencaf339d2017-10-06 16:01:10 -0700130 // To make the log processing more efficient, we want to do as much filtering as possible
131 // before we go into individual trackers and conditions to match.
132
133 // 1st filter: check if the event tag id is in mTagIds.
134 // 2nd filter: if it is, we parse the event because there is at least one member is interested.
135 // then pass to all LogMatchingTrackers (itself also filter events by ids).
136 // 3nd filter: for LogMatchingTrackers that matched this event, we pass this event to the
137 // ConditionTrackers and MetricProducers that use this matcher.
138 // 4th filter: for ConditionTrackers that changed value due to this event, we pass
139 // new conditions to metrics that use this condition.
140
141 // The following map is initialized from the statsd_config.
142
143 // maps from the index of the LogMatchingTracker to index of MetricProducer.
144 std::unordered_map<int, std::vector<int>> mTrackerToMetricMap;
145
146 // maps from LogMatchingTracker to ConditionTracker
147 std::unordered_map<int, std::vector<int>> mTrackerToConditionMap;
148
149 // maps from ConditionTracker to MetricProducer
150 std::unordered_map<int, std::vector<int>> mConditionToMetricMap;
151
Yao Chend10f7b12017-12-18 12:53:50 -0800152 void initLogSourceWhiteList();
Yangster-mac20877162017-12-22 17:19:39 -0800153
Yangster-mac94e197c2018-01-02 16:03:03 -0800154 // The metrics that don't need to be uploaded or even reported.
155 std::set<int64_t> mNoReportMetricIds;
156
Yangster-mac20877162017-12-22 17:19:39 -0800157 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions);
158 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks);
Yangster-macb5bc7412018-01-06 23:17:45 -0800159 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice);
Yangster-mac87718e22018-01-11 16:16:26 -0800160 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster-mac93694462018-01-22 20:49:31 -0800161 FRIEND_TEST(DimensionInConditionE2eTest, TestCountMetricNoLink);
162 FRIEND_TEST(DimensionInConditionE2eTest, TestCountMetricWithLink);
163 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetricNoLink);
164 FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetricWithLink);
Yao Chen44cf27c2017-09-14 22:32:50 -0700165};
166
167} // namespace statsd
168} // namespace os
169} // namespace android