Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa22d65f authored by Chenjie Yu's avatar Chenjie Yu
Browse files

puller cache clearing

+ add adb command to manually clear puller cache
+ try to clear puller cache every 10s

Test: manual test
Change-Id: I8005cacd189de1880fcaeb030efbe21e6d3c0244
parent 043863ac
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -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(
+5 −0
Original line number Diff line number Diff line
@@ -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);
+17 −3
Original line number Diff line number Diff line
@@ -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");
    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,
+16 −1
Original line number Diff line number Diff line
@@ -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
+7 −1
Original line number Diff line number Diff line
@@ -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
Loading