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);
StatsdStats::getInstance().noteConfigRemoved(key);
mLastBroadcastTimes.erase(key);
+
+ if (mMetricsManagers.empty()) {
+ mStatsPullerManager.ForceClearPullerCache();
+ }
}
void StatsLogProcessor::flushIfNecessaryLocked(
#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"
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);
const long mTimeBaseSec;
+ long mLastPullerCacheClearTimeSec = 0;
+
FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
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);
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) {
}
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,
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
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
std::vector<std::shared_ptr<LogEvent>> mCachedData;
long mLastPullTimeSec;
+
+ int clearCache();
};
} // namespace statsd
mPullerManager.SetTimeBaseSec(timeBaseSec);
}
- void ClearPullerCache() {
- mPullerManager.ClearPullerCache();
+ int ForceClearPullerCache() {
+ return mPullerManager.ForceClearPullerCache();
+ }
+
+ int ClearPullerCacheIfNecessary(long timestampSec) {
+ return mPullerManager.ClearPullerCacheIfNecessary(timestampSec);
}
private:
}
}
-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
void SetTimeBaseSec(long timeBaseSec) {mTimeBaseSec = timeBaseSec;};
- void ClearPullerCache();
+ int ForceClearPullerCache();
+
+ int ClearPullerCacheIfNecessary(long timestampSec);
private:
StatsPullerManagerImpl();
// 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.
*