blob: 436a8801896ff92264fd38bde755f86ae92f6929 [file] [log] [blame]
Chenjie Yub038b702017-12-18 15:15:34 -08001/*
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
17#define DEBUG false // STOPSHIP if true
18#include "Log.h"
19
20#include "StatsPuller.h"
Chenjie Yue2219202018-06-08 10:07:51 -070021#include "StatsPullerManager.h"
Chenjie Yub038b702017-12-18 15:15:34 -080022#include "guardrail/StatsdStats.h"
Chenjie Yu80f91122018-01-31 20:24:50 -080023#include "puller_util.h"
Yangster-mac330af582018-02-08 15:24:38 -080024#include "stats_log_util.h"
Chenjie Yub038b702017-12-18 15:15:34 -080025
26namespace android {
27namespace os {
28namespace statsd {
29
30using std::lock_guard;
31
Chenjie Yu80f91122018-01-31 20:24:50 -080032sp<UidMap> StatsPuller::mUidMap = nullptr;
33void StatsPuller::SetUidMap(const sp<UidMap>& uidMap) { mUidMap = uidMap; }
34
Chenjie Yub038b702017-12-18 15:15:34 -080035// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
36StatsPuller::StatsPuller(const int tagId)
37 : mTagId(tagId) {
Chenjie Yue2219202018-06-08 10:07:51 -070038 mCoolDownNs = StatsPullerManager::kAllPullAtomInfo.find(tagId)->second.coolDownNs;
Chenjie Yu1a0a9412018-03-28 10:07:22 -070039 VLOG("Puller for tag %d created. Cooldown set to %lld", mTagId, (long long)mCoolDownNs);
Chenjie Yub038b702017-12-18 15:15:34 -080040}
41
Chenjie Yu1a0a9412018-03-28 10:07:22 -070042bool StatsPuller::Pull(const int64_t elapsedTimeNs, std::vector<std::shared_ptr<LogEvent>>* data) {
Chenjie Yub038b702017-12-18 15:15:34 -080043 lock_guard<std::mutex> lock(mLock);
Chenjie Yu1a0a9412018-03-28 10:07:22 -070044 int64_t wallClockTimeNs = getWallClockNs();
Chenjie Yub038b702017-12-18 15:15:34 -080045 StatsdStats::getInstance().notePull(mTagId);
Chenjie Yu1a0a9412018-03-28 10:07:22 -070046 if (elapsedTimeNs - mLastPullTimeNs < mCoolDownNs) {
Chenjie Yub038b702017-12-18 15:15:34 -080047 (*data) = mCachedData;
48 StatsdStats::getInstance().notePullFromCache(mTagId);
49 return true;
50 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -070051 if (mMinPullIntervalNs > elapsedTimeNs - mLastPullTimeNs) {
52 mMinPullIntervalNs = elapsedTimeNs - mLastPullTimeNs;
53 StatsdStats::getInstance().updateMinPullIntervalSec(mTagId,
54 mMinPullIntervalNs / NS_PER_SEC);
Chenjie Yub038b702017-12-18 15:15:34 -080055 }
56 mCachedData.clear();
Chenjie Yu1a0a9412018-03-28 10:07:22 -070057 mLastPullTimeNs = elapsedTimeNs;
Chenjie Yub038b702017-12-18 15:15:34 -080058 bool ret = PullInternal(&mCachedData);
Chenjie Yu1a0a9412018-03-28 10:07:22 -070059 for (const shared_ptr<LogEvent>& data : mCachedData) {
60 data->setElapsedTimestampNs(elapsedTimeNs);
61 data->setLogdWallClockTimestampNs(wallClockTimeNs);
62 }
Chenjie Yu9e59f932018-03-30 16:41:38 -070063 if (ret && mCachedData.size() > 0) {
Chenjie Yu80f91122018-01-31 20:24:50 -080064 mergeIsolatedUidsToHostUid(mCachedData, mUidMap, mTagId);
65 (*data) = mCachedData;
Chenjie Yub038b702017-12-18 15:15:34 -080066 }
67 return ret;
68}
69
Chenjie Yufa22d652018-02-05 14:37:48 -080070int StatsPuller::ForceClearCache() {
71 return clearCache();
72}
73
74int StatsPuller::clearCache() {
Chenjie Yue72252b2018-02-01 13:19:35 -080075 lock_guard<std::mutex> lock(mLock);
Chenjie Yufa22d652018-02-05 14:37:48 -080076 int ret = mCachedData.size();
Chenjie Yue72252b2018-02-01 13:19:35 -080077 mCachedData.clear();
Chenjie Yu1a0a9412018-03-28 10:07:22 -070078 mLastPullTimeNs = 0;
Chenjie Yufa22d652018-02-05 14:37:48 -080079 return ret;
80}
81
Chenjie Yu1a0a9412018-03-28 10:07:22 -070082int StatsPuller::ClearCacheIfNecessary(int64_t timestampNs) {
83 if (timestampNs - mLastPullTimeNs > mCoolDownNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -080084 return clearCache();
85 } else {
86 return 0;
87 }
Chenjie Yue72252b2018-02-01 13:19:35 -080088}
89
Chenjie Yub038b702017-12-18 15:15:34 -080090} // namespace statsd
91} // namespace os
92} // namespace android