blob: dd6735b6faca665a9c410f41b3cd838c211c0862 [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 Chen8a8d16c2018-02-08 14:50:40 -080016#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>
Bookatz6f197902018-02-05 12:30:14 -080031#include <private/android_filesystem_config.h>
David Chen16049572018-02-01 18:27:51 -080032#include <utils/SystemClock.h>
Yao Chen288c6002017-12-12 13:43:18 -080033
34using android::util::FIELD_COUNT_REPEATED;
35using android::util::FIELD_TYPE_MESSAGE;
36using android::util::ProtoOutputStream;
37
Yao Chen44cf27c2017-09-14 22:32:50 -070038using std::make_unique;
39using std::set;
40using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070041using std::unordered_map;
42using std::vector;
43
44namespace android {
45namespace os {
46namespace statsd {
47
Yao Chen288c6002017-12-12 13:43:18 -080048const int FIELD_ID_METRICS = 1;
49
Yao Chend10f7b12017-12-18 12:53:50 -080050MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config,
51 const long timeBaseSec, sp<UidMap> uidMap)
David Chen16049572018-02-01 18:27:51 -080052 : mConfigKey(key), mUidMap(uidMap), mLastReportTimeNs(0) {
Yao Chenb3561512017-11-21 18:07:17 -080053 mConfigValid =
Yangster-mac20877162017-12-22 17:19:39 -080054 initStatsdConfig(key, config, *uidMap, timeBaseSec, mTagIds, mAllAtomMatchers, mAllConditionTrackers,
Yao Chenb3561512017-11-21 18:07:17 -080055 mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap,
Yangster-mac94e197c2018-01-02 16:03:03 -080056 mTrackerToMetricMap, mTrackerToConditionMap, mNoReportMetricIds);
Yao Chenb3561512017-11-21 18:07:17 -080057
Yao Chen147ce602017-12-22 14:35:34 -080058 if (config.allowed_log_source_size() == 0) {
Yao Chend10f7b12017-12-18 12:53:50 -080059 // TODO(b/70794411): uncomment the following line and remove the hard coded log source
60 // after all configs have the log source added.
61 // mConfigValid = false;
62 // ALOGE("Log source white list is empty! This config won't get any data.");
63
Bookatz6f197902018-02-05 12:30:14 -080064 mAllowedUid.push_back(AID_ROOT);
65 mAllowedUid.push_back(AID_STATSD);
66 mAllowedUid.push_back(AID_SYSTEM);
Yao Chend10f7b12017-12-18 12:53:50 -080067 mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end());
68 } else {
Yao Chen147ce602017-12-22 14:35:34 -080069 for (const auto& source : config.allowed_log_source()) {
70 auto it = UidMap::sAidToUidMapping.find(source);
71 if (it != UidMap::sAidToUidMapping.end()) {
72 mAllowedUid.push_back(it->second);
73 } else {
74 mAllowedPkg.push_back(source);
75 }
76 }
Yao Chend10f7b12017-12-18 12:53:50 -080077
78 if (mAllowedUid.size() + mAllowedPkg.size() > StatsdStats::kMaxLogSourceCount) {
79 ALOGE("Too many log sources. This is likely to be an error in the config.");
80 mConfigValid = false;
81 } else {
82 initLogSourceWhiteList();
83 }
84 }
85
Yao Chenb3561512017-11-21 18:07:17 -080086 // Guardrail. Reject the config if it's too big.
87 if (mAllMetricProducers.size() > StatsdStats::kMaxMetricCountPerConfig ||
88 mAllConditionTrackers.size() > StatsdStats::kMaxConditionCountPerConfig ||
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080089 mAllAtomMatchers.size() > StatsdStats::kMaxMatcherCountPerConfig) {
Yao Chenb3561512017-11-21 18:07:17 -080090 ALOGE("This config is too big! Reject!");
91 mConfigValid = false;
92 }
Yao Chenf09569f2017-12-13 17:00:51 -080093
94 // TODO: add alert size.
95 // no matter whether this config is valid, log it in the stats.
96 StatsdStats::getInstance().noteConfigReceived(key, mAllMetricProducers.size(),
97 mAllConditionTrackers.size(),
98 mAllAtomMatchers.size(), 0, mConfigValid);
Yao Chen44cf27c2017-09-14 22:32:50 -070099}
100
101MetricsManager::~MetricsManager() {
Bookatzd3606c72017-10-19 10:13:49 -0700102 VLOG("~MetricsManager()");
Yao Chen44cf27c2017-09-14 22:32:50 -0700103}
104
Yao Chend10f7b12017-12-18 12:53:50 -0800105void MetricsManager::initLogSourceWhiteList() {
106 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
107 mAllowedLogSources.clear();
108 mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end());
109
110 for (const auto& pkg : mAllowedPkg) {
111 auto uids = mUidMap->getAppUid(pkg);
112 mAllowedLogSources.insert(uids.begin(), uids.end());
113 }
114 if (DEBUG) {
115 for (const auto& uid : mAllowedLogSources) {
116 VLOG("Allowed uid %d", uid);
117 }
118 }
119}
120
Yao Chencaf339d2017-10-06 16:01:10 -0700121bool MetricsManager::isConfigValid() const {
122 return mConfigValid;
123}
124
David Chen27785a82018-01-19 17:06:45 -0800125void MetricsManager::notifyAppUpgrade(const uint64_t& eventTimeNs, const string& apk, const int uid,
126 const int64_t version) {
Yao Chend10f7b12017-12-18 12:53:50 -0800127 // check if we care this package
128 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) == mAllowedPkg.end()) {
129 return;
130 }
131 // We will re-initialize the whole list because we don't want to keep the multi mapping of
132 // UID<->pkg inside MetricsManager to reduce the memory usage.
133 initLogSourceWhiteList();
134}
135
David Chen27785a82018-01-19 17:06:45 -0800136void MetricsManager::notifyAppRemoved(const uint64_t& eventTimeNs, const string& apk,
137 const int uid) {
Yao Chend10f7b12017-12-18 12:53:50 -0800138 // check if we care this package
139 if (std::find(mAllowedPkg.begin(), mAllowedPkg.end(), apk) == mAllowedPkg.end()) {
140 return;
141 }
142 // We will re-initialize the whole list because we don't want to keep the multi mapping of
143 // UID<->pkg inside MetricsManager to reduce the memory usage.
144 initLogSourceWhiteList();
145}
146
David Chen27785a82018-01-19 17:06:45 -0800147void MetricsManager::onUidMapReceived(const uint64_t& eventTimeNs) {
Yao Chend10f7b12017-12-18 12:53:50 -0800148 if (mAllowedPkg.size() == 0) {
149 return;
150 }
151 initLogSourceWhiteList();
152}
153
Yao Chen884c8c12018-01-26 10:36:25 -0800154void MetricsManager::dumpStates(FILE* out, bool verbose) {
155 fprintf(out, "ConfigKey %s, allowed source:", mConfigKey.ToString().c_str());
156 {
157 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
158 for (const auto& source : mAllowedLogSources) {
159 fprintf(out, "%d ", source);
160 }
161 }
162 fprintf(out, "\n");
163 for (const auto& producer : mAllMetricProducers) {
164 producer->dumpStates(out, verbose);
165 }
166}
167
Yao Chen8a8d16c2018-02-08 14:50:40 -0800168void MetricsManager::onDumpReport(const uint64_t dumpTimeStampNs, ProtoOutputStream* protoOutput) {
Yao Chen729093d2017-10-16 10:33:26 -0700169 VLOG("=========================Metric Reports Start==========================");
170 // one StatsLogReport per MetricProduer
Yangster-mac94e197c2018-01-02 16:03:03 -0800171 for (const auto& producer : mAllMetricProducers) {
172 if (mNoReportMetricIds.find(producer->getMetricId()) == mNoReportMetricIds.end()) {
173 long long token =
174 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_METRICS);
175 producer->onDumpReport(dumpTimeStampNs, protoOutput);
176 protoOutput->end(token);
177 }
Yao Chen729093d2017-10-16 10:33:26 -0700178 }
David Chen16049572018-02-01 18:27:51 -0800179 mLastReportTimeNs = ::android::elapsedRealtimeNano();
Yao Chen729093d2017-10-16 10:33:26 -0700180 VLOG("=========================Metric Reports End==========================");
Yao Chen729093d2017-10-16 10:33:26 -0700181}
182
Yao Chen44cf27c2017-09-14 22:32:50 -0700183// Consume the stats log if it's interesting to this metric.
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700184void MetricsManager::onLogEvent(const LogEvent& event) {
Yao Chencaf339d2017-10-06 16:01:10 -0700185 if (!mConfigValid) {
186 return;
187 }
188
Tej Singhbb8554a2018-01-26 11:59:14 -0800189 if (event.GetTagId() == android::util::APP_HOOK) { // Check that app hook fields are valid.
David Chendaa9f3a2017-12-28 16:52:22 -0800190 // TODO: Find a way to make these checks easier to maintain if the app hooks get changed.
David Chendaa9f3a2017-12-28 16:52:22 -0800191 status_t err = NO_ERROR;
Bookatzb223c4e2018-02-01 15:35:04 -0800192
193 // Uid is 3rd from last field and must match the caller's uid,
194 // unless that caller is statsd itself (statsd is allowed to spoof uids).
195 long appHookUid = event.GetLong(event.size()-2, &err);
196 int32_t loggerUid = event.GetUid();
Bookatz6f197902018-02-05 12:30:14 -0800197 if (err != NO_ERROR || (loggerUid != appHookUid && loggerUid != AID_STATSD)) {
Bookatzb223c4e2018-02-01 15:35:04 -0800198 VLOG("AppHook has invalid uid: claimed %ld but caller is %d", appHookUid, loggerUid);
David Chendaa9f3a2017-12-28 16:52:22 -0800199 return;
200 }
Bookatzb223c4e2018-02-01 15:35:04 -0800201
202 // Label is 2nd from last field and must be from [0, 15].
203 long appHookLabel = event.GetLong(event.size()-1, &err);
204 if (err != NO_ERROR || appHookLabel < 0 || appHookLabel > 15) {
205 VLOG("AppHook does not have valid label %ld", appHookLabel);
206 return;
207 }
208
David Chendaa9f3a2017-12-28 16:52:22 -0800209 // The state must be from 0,3. This part of code must be manually updated.
Bookatzb223c4e2018-02-01 15:35:04 -0800210 long appHookState = event.GetLong(event.size(), &err);
211 if (err != NO_ERROR || appHookState < 0 || appHookState > 3) {
212 VLOG("AppHook does not have valid state %ld", appHookState);
David Chendaa9f3a2017-12-28 16:52:22 -0800213 return;
214 }
Tej Singhbb8554a2018-01-26 11:59:14 -0800215 } else if (event.GetTagId() == android::util::DAVEY_OCCURRED) {
216 // Daveys can be logged from any app since they are logged in libs/hwui/JankTracker.cpp.
217 // Check that the davey duration is reasonable. Max length check is for privacy.
218 status_t err = NO_ERROR;
219 long duration = event.GetLong(event.size(), &err);
220 if (err != NO_ERROR || duration > 100000) {
221 VLOG("Davey duration is unreasonably long: %ld", duration);
222 return;
223 }
224 } else {
225 std::lock_guard<std::mutex> lock(mAllowedLogSourcesMutex);
226 if (mAllowedLogSources.find(event.GetUid()) == mAllowedLogSources.end()) {
227 VLOG("log source %d not on the whitelist", event.GetUid());
228 return;
229 }
Yao Chend10f7b12017-12-18 12:53:50 -0800230 }
231
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700232 int tagId = event.GetTagId();
Yao Chen5154a3792017-10-30 22:57:06 -0700233 uint64_t eventTime = event.GetTimestampNs();
Yao Chen44cf27c2017-09-14 22:32:50 -0700234 if (mTagIds.find(tagId) == mTagIds.end()) {
235 // not interesting...
236 return;
237 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700238
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800239 vector<MatchingState> matcherCache(mAllAtomMatchers.size(), MatchingState::kNotComputed);
Yao Chencaf339d2017-10-06 16:01:10 -0700240
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800241 for (auto& matcher : mAllAtomMatchers) {
242 matcher->onLogEvent(event, mAllAtomMatchers, matcherCache);
Yao Chen44cf27c2017-09-14 22:32:50 -0700243 }
244
Yao Chencaf339d2017-10-06 16:01:10 -0700245 // A bitmap to see which ConditionTracker needs to be re-evaluated.
246 vector<bool> conditionToBeEvaluated(mAllConditionTrackers.size(), false);
247
248 for (const auto& pair : mTrackerToConditionMap) {
249 if (matcherCache[pair.first] == MatchingState::kMatched) {
250 const auto& conditionList = pair.second;
251 for (const int conditionIndex : conditionList) {
252 conditionToBeEvaluated[conditionIndex] = true;
253 }
254 }
255 }
256
257 vector<ConditionState> conditionCache(mAllConditionTrackers.size(),
258 ConditionState::kNotEvaluated);
259 // A bitmap to track if a condition has changed value.
260 vector<bool> changedCache(mAllConditionTrackers.size(), false);
261 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
262 if (conditionToBeEvaluated[i] == false) {
263 continue;
264 }
Yao Chencaf339d2017-10-06 16:01:10 -0700265 sp<ConditionTracker>& condition = mAllConditionTrackers[i];
266 condition->evaluateCondition(event, matcherCache, mAllConditionTrackers, conditionCache,
Yao Chen967b2052017-11-07 16:36:43 -0800267 changedCache);
Yao Chen729093d2017-10-16 10:33:26 -0700268 }
269
270 for (size_t i = 0; i < mAllConditionTrackers.size(); i++) {
Yao Chen967b2052017-11-07 16:36:43 -0800271 if (changedCache[i] == false) {
Yao Chen729093d2017-10-16 10:33:26 -0700272 continue;
273 }
274 auto pair = mConditionToMetricMap.find(i);
275 if (pair != mConditionToMetricMap.end()) {
276 auto& metricList = pair->second;
277 for (auto metricIndex : metricList) {
278 // metric cares about non sliced condition, and it's changed.
279 // Push the new condition to it directly.
Yao Chen967b2052017-11-07 16:36:43 -0800280 if (!mAllMetricProducers[metricIndex]->isConditionSliced()) {
Yao Chen5154a3792017-10-30 22:57:06 -0700281 mAllMetricProducers[metricIndex]->onConditionChanged(conditionCache[i],
282 eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700283 // metric cares about sliced conditions, and it may have changed. Send
284 // notification, and the metric can query the sliced conditions that are
285 // interesting to it.
Yangsterf2bee6f2017-11-29 12:01:05 -0800286 } else {
Yao Chen5154a3792017-10-30 22:57:06 -0700287 mAllMetricProducers[metricIndex]->onSlicedConditionMayChange(eventTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700288 }
Yao Chencaf339d2017-10-06 16:01:10 -0700289 }
290 }
291 }
292
Stefan Lafonb8c9aa82017-12-03 14:27:25 -0800293 // For matched AtomMatchers, tell relevant metrics that a matched event has come.
294 for (size_t i = 0; i < mAllAtomMatchers.size(); i++) {
Yao Chencaf339d2017-10-06 16:01:10 -0700295 if (matcherCache[i] == MatchingState::kMatched) {
Yao Chenb3561512017-11-21 18:07:17 -0800296 StatsdStats::getInstance().noteMatcherMatched(mConfigKey,
Yangster-mac94e197c2018-01-02 16:03:03 -0800297 mAllAtomMatchers[i]->getId());
Yao Chencaf339d2017-10-06 16:01:10 -0700298 auto pair = mTrackerToMetricMap.find(i);
299 if (pair != mTrackerToMetricMap.end()) {
300 auto& metricList = pair->second;
301 for (const int metricIndex : metricList) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700302 // pushed metrics are never scheduled pulls
Chenjie Yua7259ab2017-12-10 08:31:05 -0800303 mAllMetricProducers[metricIndex]->onMatchedLogEvent(i, event);
Yao Chencaf339d2017-10-06 16:01:10 -0700304 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700305 }
306 }
307 }
308}
309
Yangster-mace2cd6d52017-11-09 20:38:30 -0800310void MetricsManager::onAnomalyAlarmFired(const uint64_t timestampNs,
Bookatzcc5adef22017-11-21 14:36:23 -0800311 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800312 for (const auto& itr : mAllAnomalyTrackers) {
Bookatzcc5adef22017-11-21 14:36:23 -0800313 itr->informAlarmsFired(timestampNs, anomalySet);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800314 }
315}
316
317void MetricsManager::setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor) {
318 for (auto& itr : mAllAnomalyTrackers) {
319 itr->setAnomalyMonitor(anomalyMonitor);
320 }
321}
322
yro69007c82017-10-26 20:42:57 -0700323// Returns the total byte size of all metrics managed by a single config source.
324size_t MetricsManager::byteSize() {
325 size_t totalSize = 0;
326 for (auto metricProducer : mAllMetricProducers) {
327 totalSize += metricProducer->byteSize();
328 }
329 return totalSize;
330}
331
Yao Chen44cf27c2017-09-14 22:32:50 -0700332} // namespace statsd
333} // namespace os
334} // namespace android