blob: 18426dd2a216845e13bf461394155bbf38b80cca [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 */
Yao Chen44cf27c2017-09-14 22:32:50 -070016#define DEBUG true // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070018#include "MetricsManager.h"
Yao Chend10f7b12017-12-18 12:53:50 -080019#include "statslog.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070020
Yao Chen44cf27c2017-09-14 22:32:50 -070021#include "CountMetricProducer.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070022#include "condition/CombinationConditionTracker.h"
23#include "condition/SimpleConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080024#include "guardrail/StatsdStats.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070025#include "matchers/CombinationLogMatchingTracker.h"
26#include "matchers/SimpleLogMatchingTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070027#include "metrics_manager_util.h"
28#include "stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070029
Yao Chen93fe3a32017-11-02 13:52:59 -070030#include <log/logprint.h>
Yao Chen288c6002017-12-12 13:43:18 -080031
32using android::util::FIELD_COUNT_REPEATED;
33using android::util::FIELD_TYPE_MESSAGE;
34using android::util::ProtoOutputStream;
35
Yao Chen44cf27c2017-09-14 22:32:50 -070036using std::make_unique;
37using std::set;
38using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070039using std::unordered_map;
40using std::vector;
41
42namespace android {
43namespace os {
44namespace statsd {
45
Yao Chen288c6002017-12-12 13:43:18 -080046const int FIELD_ID_METRICS = 1;
47
Yao Chend10f7b12017-12-18 12:53:50 -080048MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config,
49 const long timeBaseSec, sp<UidMap> uidMap)
50 : mConfigKey(key), mUidMap(uidMap) {
Yao Chenb3561512017-11-21 18:07:17 -080051 mConfigValid =
Chenjie Yu85ed8382017-12-14 16:48:54 -080052 initStatsdConfig(key, config, timeBaseSec, mTagIds, mAllAtomMatchers, mAllConditionTrackers,
Yao Chenb3561512017-11-21 18:07:17 -080053 mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap,
54 mTrackerToMetricMap, mTrackerToConditionMap);
55
Yao Chend10f7b12017-12-18 12:53:50 -080056 if (!config.has_log_source()) {
57 // TODO(b/70794411): uncomment the following line and remove the hard coded log source
58 // after all configs have the log source added.
59 // mConfigValid = false;
60 // ALOGE("Log source white list is empty! This config won't get any data.");
61
62 mAllowedUid.push_back(1000);
63 mAllowedUid.push_back(0);
64 mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end());
65 } else {
66 mAllowedUid.insert(mAllowedUid.begin(), config.log_source().uid().begin(),
67 config.log_source().uid().end());
68 mAllowedPkg.insert(mAllowedPkg.begin(), config.log_source().package().begin(),
69 config.log_source().package().end());
70
71 if (mAllowedUid.size() + mAllowedPkg.size() > StatsdStats::kMaxLogSourceCount) {
72 ALOGE("Too many log sources. This is likely to be an error in the config.");
73 mConfigValid = false;
74 } else {
75 initLogSourceWhiteList();
76 }
77 }
78
Yao Chenb3561512017-11-21 18:07:17 -080079 // Guardrail. Reject the config if it's too big.
80 if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
81 mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080082 mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
Yao Chenb3561512017-11-21 18:07:17 -080083 ALOGE("This config is too big! Reject!");
84 mConfigValid = false;
85 }
Yao Chenf09569f2017-12-13 17:00:51 -080086
87 // TODO: add alert size.
88 // no matter whether this config is valid, log it in the stats.
89 StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(),
90 mAllConditionTrackers.size(),
91 mAllAtomMatchers.size(), 0, mConfigValid);
Yao Chen44cf27c2017-09-14 22:32:50 -070092}
93
94MetricsManager::~MetricsManager() {
Bookatzd3606c72017-10-19 10:13:49 -070095 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -070096}
97
Yao Chend10f7b12017-12-18 12:53:50 -080098void MetricsManager::initLogSourceWhiteList() {
99 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
100 mAllowedLogSources.clear();
101 mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end());
102
103 for (const auto& pkg : mAllowedPkg) {
104 auto uids = mUidMap->getAppUid(pkg);
105 mAllowedLogSources.insert(uids.begin(), uids.end());
106 }
107 if (DEBUG) {
108 for (const auto& uid : mAllowedLogSources) {
109 VLOG("Allowed uid %d", uid);
110 }
111 }
112}
113
Yao Chencaf339d2017-10-06 16:01:10 -0700114bool MetricsManager::isConfigValid() const {
115 return mConfigValid;
116}
117
Yao Chend10f7b12017-12-18 12:53:50 -0800118void MetricsManager::notifyAppUpgrade(const string& apk, const int uid, const int64_t version) {
119 // check if we care this package
120 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) == mAllowedPkg.end()) {
121 return;
122 }
123 // We will re-initialize the whole list because we don't want to keep the multi mapping of
124 // UID<->pkg inside MetricsManager to reduce the memory usage.
125 initLogSourceWhiteList();
126}
127
128void MetricsManager::notifyAppRemoved(const string& apk, const int uid) {
129 // check if we care this package
130 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) == mAllowedPkg.end()) {
131 return;
132 }
133 // We will re-initialize the whole list because we don't want to keep the multi mapping of
134 // UID<->pkg inside MetricsManager to reduce the memory usage.
135 initLogSourceWhiteList();
136}
137
138void MetricsManager::onUidMapReceived() {
139 if (mAllowedPkg.size() == 0) {
140 return;
141 }
142 initLogSourceWhiteList();
143}
144
Yao Chen288c6002017-12-12 13:43:18 -0800145void MetricsManager::onDumpReport(ProtoOutputStream* protoOutput) {
Yao Chen729093d2017-10-16 10:33:26 -0700146 VLOG("=========================Metric Reports Start==========================");
Yao Chen288c6002017-12-12 13:43:18 -0800147 uint64_t dumpTimeStampNs = time(nullptr) * NS_PER_SEC;
Yao Chen729093d2017-10-16 10:33:26 -0700148 // one StatsLogReport per MetricProduer
Yao Chen729093d2017-10-16 10:33:26 -0700149 for (auto& metric : mAllMetricProducers) {
Yao Chen288c6002017-12-12 13:43:18 -0800150 long long token =
151 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS);
152 metric->onDumpReport(dumpTimeStampNs, protoOutput);
153 protoOutput->end(token);
Yao Chen729093d2017-10-16 10:33:26 -0700154 }
155 VLOG("=========================Metric Reports End==========================");
Yao Chen729093d2017-10-16 10:33:26 -0700156}
157
Yao Chen44cf27c2017-09-14 22:32:50 -0700158// Consume the stats log if it's interesting to this metric.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700159void MetricsManager::onLogEvent(const LogEvent& event) {
Yao Chencaf339d2017-10-06 16:01:10 -0700160 if (!mConfigValid) {
161 return;
162 }
163
Yao Chend10f7b12017-12-18 12:53:50 -0800164 if (event.GetTagId() != android::util::APP_HOOK) {
165 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
166 if (mAllowedLogSources.find(event.GetUid()) == mAllowedLogSources.end()) {
167 VLOG("log source %d not on the whitelist", event.GetUid());
168 return;
169 }
David Chendaa9f3a2017-12-28 16:52:22 -0800170 } else { // Check that app hook fields are valid.
171 // TODO: Find a way to make these checks easier to maintain if the app hooks get changed.
172
173 // Label is 2nd from last field and must be from [0, 15].
174 status_t err = NO_ERROR;
175 long label = event.GetLong(event.size()-1, &err);
176 if (err != NO_ERROR || label < 0 || label > 15) {
177 VLOG("App hook does not have valid label %ld", label);
178 return;
179 }
180 // The state must be from 0,3. This part of code must be manually updated.
181 long apphookState = event.GetLong(event.size(), &err);
182 if (err != NO_ERROR || apphookState < 0 || apphookState > 3) {
183 VLOG("App hook does not have valid state %ld", apphookState);
184 return;
185 }
Yao Chend10f7b12017-12-18 12:53:50 -0800186 }
187
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700188 int tagId = event.GetTagId();
Yao Chen5154a3792017-10-30 22:57:06 -0700189 uint64_t eventTime = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -0700190 if (mTagIds.find(tagId) == mTagIds.end()) {
191 // not interesting...
192 return;
193 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700194
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800195 vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed);
Yao Chencaf339d2017-10-06 16:01:10 -0700196
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800197 for (auto& matcher : mAllAtomMatchers) {
198 matcher->onLogEvent(event, mAllAtomMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -0700199 }
200
Yao Chencaf339d2017-10-06 16:01:10 -0700201 // A bitmap to see which ConditionTracker needs to be re-evaluated.
202 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
203
204 for (const auto& pair : mTrackerToConditionMap) {
205 if (matcherCache[pair.first] == MatchingState::kMatched) {
206 const auto& conditionList = pair.second;
207 for (const int conditionIndex : conditionList) {
208 conditionToBeEvaluated[conditionIndex] = true;
209 }
210 }
211 }
212
213 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
214 ConditionState::kNotEvaluated);
215 // A bitmap to track if a condition has changed value.
216 vector<bool> changedCache(mAllConditionTrackers.size(), false);
217 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
218 if (conditionToBeEvaluated[i] == false) {
219 continue;
220 }
Yao Chencaf339d2017-10-06 16:01:10 -0700221 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
222 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800223 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700224 }
225
226 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800227 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700228 continue;
229 }
230 auto pair = mConditionToMetricMap.find(i);
231 if (pair != mConditionToMetricMap.end()) {
232 auto& metricList = pair->second;
233 for (auto metricIndex : metricList) {
234 // metric cares about non sliced condition, and it's changed.
235 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800236 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700237 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
238 eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700239 // metric cares about sliced conditions, and it may have changed. Send
240 // notification, and the metric can query the sliced conditions that are
241 // interesting to it.
Yangsterf2bee6f2017-11-29 12:01:05 -0800242 } else {
Yao Chen5154a3792017-10-30 22:57:06 -0700243 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700244 }
Yao Chencaf339d2017-10-06 16:01:10 -0700245 }
246 }
247 }
248
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800249 // For matched AtomMatchers, tell relevant metrics that a matched event has come.
250 for (size_t i = 0; i < mAllAtomMatchers.size(); i++) {
Yao Chencaf339d2017-10-06 16:01:10 -0700251 if (matcherCache[i] == MatchingState::kMatched) {
Yao Chenb3561512017-11-21 18:07:17 -0800252 StatsdStats::getInstance().noteMatcherMatched(mConfigKey,
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800253 mAllAtomMatchers[i]->getName());
Yao Chencaf339d2017-10-06 16:01:10 -0700254 auto pair = mTrackerToMetricMap.find(i);
255 if (pair != mTrackerToMetricMap.end()) {
256 auto& metricList = pair->second;
257 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700258 // pushed metrics are never scheduled pulls
Chenjie Yua7259ab2017-12-10 08:31:05 -0800259 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700260 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700261 }
262 }
263 }
264}
265
Yangster-mace2cd6d52017-11-09 20:38:30 -0800266void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs,
Bookatzcc5adef22017-11-21 14:36:23 -0800267 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800268 for (const auto& itr : mAllAnomalyTrackers) {
Bookatzcc5adef22017-11-21 14:36:23 -0800269 itr->informAlarmsFired(timestampNs, anomalySet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800270 }
271}
272
273void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) {
274 for (auto& itr : mAllAnomalyTrackers) {
275 itr->setAnomalyMonitor(anomalyMonitor);
276 }
277}
278
yro69007c82017-10-26 20:42:57 -0700279// Returns the total byte size of all metrics managed by a single config source.
280size_t MetricsManager::byteSize() {
281 size_t totalSize = 0;
282 for (auto metricProducer : mAllMetricProducers) {
283 totalSize += metricProducer->byteSize();
284 }
285 return totalSize;
286}
287
Yao Chen44cf27c2017-09-14 22:32:50 -0700288} // namespace statsd
289} // namespace os
290} // namespace android