summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmds/statsd/src/StatsLogProcessor.cpp10
-rw-r--r--cmds/statsd/src/StatsLogProcessor.h5
-rw-r--r--cmds/statsd/src/StatsService.cpp20
-rw-r--r--cmds/statsd/src/external/StatsPuller.cpp17
-rw-r--r--cmds/statsd/src/external/StatsPuller.h8
-rw-r--r--cmds/statsd/src/external/StatsPullerManager.h8
-rw-r--r--cmds/statsd/src/external/StatsPullerManagerImpl.cpp14
-rw-r--r--cmds/statsd/src/external/StatsPullerManagerImpl.h4
-rw-r--r--cmds/statsd/src/guardrail/StatsdStats.h3
9 files changed, 79 insertions, 10 deletions
diff --git a/cmds/statsd/src/StatsLogProcessor.cpp b/cmds/statsd/src/StatsLogProcessor.cpp
index a4066aa7bca5..e610fd757723 100644
--- a/cmds/statsd/src/StatsLogProcessor.cpp
+++ b/cmds/statsd/src/StatsLogProcessor.cpp
@@ -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(
diff --git a/cmds/statsd/src/StatsLogProcessor.h b/cmds/statsd/src/StatsLogProcessor.h
index 17741a8da64c..8bbcd751252a 100644
--- a/cmds/statsd/src/StatsLogProcessor.h
+++ b/cmds/statsd/src/StatsLogProcessor.h
@@ -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);
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index 32da94f862c5..3efe9b18dcba 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -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,
diff --git a/cmds/statsd/src/external/StatsPuller.cpp b/cmds/statsd/src/external/StatsPuller.cpp
index da14434737af..41e4705ee6b6 100644
--- a/cmds/statsd/src/external/StatsPuller.cpp
+++ b/cmds/statsd/src/external/StatsPuller.cpp
@@ -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
diff --git a/cmds/statsd/src/external/StatsPuller.h b/cmds/statsd/src/external/StatsPuller.h
index bc7c45f535d1..3446f9b88cdb 100644
--- a/cmds/statsd/src/external/StatsPuller.h
+++ b/cmds/statsd/src/external/StatsPuller.h
@@ -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
diff --git a/cmds/statsd/src/external/StatsPullerManager.h b/cmds/statsd/src/external/StatsPullerManager.h
index 4826d963a3ed..0dee342f0abe 100644
--- a/cmds/statsd/src/external/StatsPullerManager.h
+++ b/cmds/statsd/src/external/StatsPullerManager.h
@@ -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:
diff --git a/cmds/statsd/src/external/StatsPullerManagerImpl.cpp b/cmds/statsd/src/external/StatsPullerManagerImpl.cpp
index 71b0abe25d28..19b3a8611c18 100644
--- a/cmds/statsd/src/external/StatsPullerManagerImpl.cpp
+++ b/cmds/statsd/src/external/StatsPullerManagerImpl.cpp
@@ -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
diff --git a/cmds/statsd/src/external/StatsPullerManagerImpl.h b/cmds/statsd/src/external/StatsPullerManagerImpl.h
index fba3ade8c1bb..3535fa3e46a0 100644
--- a/cmds/statsd/src/external/StatsPullerManagerImpl.h
+++ b/cmds/statsd/src/external/StatsPullerManagerImpl.h
@@ -49,7 +49,9 @@ public:
void SetTimeBaseSec(long timeBaseSec) {mTimeBaseSec = timeBaseSec;};
- void ClearPullerCache();
+ int ForceClearPullerCache();
+
+ int ClearPullerCacheIfNecessary(long timestampSec);
private:
StatsPullerManagerImpl();
diff --git a/cmds/statsd/src/guardrail/StatsdStats.h b/cmds/statsd/src/guardrail/StatsdStats.h
index 1f4bfa62c453..f254327fcc16 100644
--- a/cmds/statsd/src/guardrail/StatsdStats.h
+++ b/cmds/statsd/src/guardrail/StatsdStats.h
@@ -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.
*