Merge "puller cache clearing"

This commit is contained in:
TreeHugger Robot
2018-02-06 21:42:48 +00:00
committed by Android (Google) Code Review
9 changed files with 79 additions and 10 deletions

View File

@@ -146,6 +146,12 @@ void StatsLogProcessor::OnLogEvent(LogEvent* event) {
return;
}
long curTime = time(nullptr);
if (curTime - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
mStatsPullerManager.ClearPullerCacheIfNecessary(curTime);
mLastPullerCacheClearTimeSec = curTime;
}
if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) {
// Map the isolated uid to host uid if necessary.
mapIsolatedUidToHostUidIfNecessaryLocked(event);
@@ -290,6 +296,10 @@ void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
StatsdStats::getInstance().noteConfigRemoved(key);
mLastBroadcastTimes.erase(key);
if (mMetricsManagers.empty()) {
mStatsPullerManager.ForceClearPullerCache();
}
}
void StatsLogProcessor::flushIfNecessaryLocked(

View File

@@ -21,6 +21,7 @@
#include "logd/LogReader.h"
#include "metrics/MetricsManager.h"
#include "packages/UidMap.h"
#include "external/StatsPullerManager.h"
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
@@ -75,6 +76,8 @@ private:
sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
StatsPullerManager mStatsPullerManager;
sp<AnomalyMonitor> mAnomalyMonitor;
void onDumpReportLocked(const ConfigKey& key, vector<uint8_t>* outData);
@@ -96,6 +99,8 @@ private:
const long mTimeBaseSec;
long mLastPullerCacheClearTimeSec = 0;
FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);

View File

@@ -240,6 +240,10 @@ status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>&
if (!args[0].compare(String8("log-app-hook"))) {
return cmd_log_app_hook(out, args);
}
if (!args[0].compare(String8("clear-puller-cache"))) {
return cmd_clear_puller_cache(out);
}
}
print_cmd_help(out);
@@ -320,6 +324,10 @@ void StatsService::print_cmd_help(FILE* out) {
fprintf(out, "\n");
fprintf(out, "usage: adb shell cmd stats print-stats\n");
fprintf(out, " Prints some basic stats.\n");
fprintf(out, "\n");
fprintf(out, "\n");
fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
fprintf(out, " Clear cached puller data.\n");
}
status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
@@ -602,9 +610,15 @@ status_t StatsService::cmd_dump_memory_info(FILE* out) {
}
status_t StatsService::cmd_clear_puller_cache(FILE* out) {
mStatsPullerManager.ClearPullerCache();
fprintf(out, "Puller cached data removed!\n");
return NO_ERROR;
IPCThreadState* ipc = IPCThreadState::self();
VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
if (checkCallingPermission(String16(kPermissionDump))) {
int cleared = mStatsPullerManager.ForceClearPullerCache();
fprintf(out, "Puller removed %d cached data!\n", cleared);
return NO_ERROR;
} else {
return PERMISSION_DENIED;
}
}
Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,

View File

@@ -59,9 +59,24 @@ bool StatsPuller::Pull(std::vector<std::shared_ptr<LogEvent>>* data) {
return ret;
}
void StatsPuller::ClearCache() {
int StatsPuller::ForceClearCache() {
return clearCache();
}
int StatsPuller::clearCache() {
lock_guard<std::mutex> lock(mLock);
int ret = mCachedData.size();
mCachedData.clear();
mLastPullTimeSec = 0;
return ret;
}
int StatsPuller::ClearCacheIfNecessary(long timestampSec) {
if (timestampSec - mLastPullTimeSec > mCoolDownSec) {
return clearCache();
} else {
return 0;
}
}
} // namespace statsd

View File

@@ -38,7 +38,11 @@ public:
bool Pull(std::vector<std::shared_ptr<LogEvent>>* data);
void ClearCache();
// Clear cache immediately
int ForceClearCache();
// Clear cache if elapsed time is more than cooldown time
int ClearCacheIfNecessary(long timestampSec);
protected:
// The atom tag id this puller pulls
@@ -61,6 +65,8 @@ private:
std::vector<std::shared_ptr<LogEvent>> mCachedData;
long mLastPullTimeSec;
int clearCache();
};
} // namespace statsd

View File

@@ -54,8 +54,12 @@ class StatsPullerManager {
mPullerManager.SetTimeBaseSec(timeBaseSec);
}
void ClearPullerCache() {
mPullerManager.ClearPullerCache();
int ForceClearPullerCache() {
return mPullerManager.ForceClearPullerCache();
}
int ClearPullerCacheIfNecessary(long timestampSec) {
return mPullerManager.ClearPullerCacheIfNecessary(timestampSec);
}
private:

View File

@@ -199,10 +199,20 @@ void StatsPullerManagerImpl::OnAlarmFired() {
}
}
void StatsPullerManagerImpl::ClearPullerCache() {
int StatsPullerManagerImpl::ForceClearPullerCache() {
int totalCleared = 0;
for (auto puller : mPullers) {
puller.second->ClearCache();
totalCleared += puller.second->ForceClearCache();
}
return totalCleared;
}
int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(long timestampSec) {
int totalCleared = 0;
for (auto puller : mPullers) {
totalCleared += puller.second->ClearCacheIfNecessary(timestampSec);
}
return totalCleared;
}
} // namespace statsd

View File

@@ -49,7 +49,9 @@ public:
void SetTimeBaseSec(long timeBaseSec) {mTimeBaseSec = timeBaseSec;};
void ClearPullerCache();
int ForceClearPullerCache();
int ClearPullerCacheIfNecessary(long timestampSec);
private:
StatsPullerManagerImpl();

View File

@@ -85,6 +85,9 @@ public:
// Maximum size of all files that can be written to stats directory on disk.
static const int kMaxFileSize = 50 * 1024 * 1024;
// How long to try to clear puller cache from last time
static const long kPullerCacheClearIntervalSec = 1;
/**
* Report a new config has been received and report the static stats about the config.
*