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

Commit 98eafdb1 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Use elapsed realtime instead of times based on wall clock, which can...

Merge "Use elapsed realtime instead of times based on wall clock, which can jump around and go backwards."
parents 11817c6b 330af58f
Loading
Loading
Loading
Loading
+16 −14
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <android-base/file.h>
#include <dirent.h>
#include "StatsLogProcessor.h"
#include "stats_log_util.h"
#include "android-base/stringprintf.h"
#include "guardrail/StatsdStats.h"
#include "metrics/CountMetricProducer.h"
@@ -59,8 +60,8 @@ const int FIELD_ID_ID = 2;
// for ConfigMetricsReport
const int FIELD_ID_METRICS = 1;
const int FIELD_ID_UID_MAP = 2;
const int FIELD_ID_LAST_REPORT_NANOS = 3;
const int FIELD_ID_CURRENT_REPORT_NANOS = 4;
const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3;
const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4;

#define STATS_DATA_DIR "/data/misc/stats-data"

@@ -136,7 +137,7 @@ void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) {
void StatsLogProcessor::OnLogEvent(LogEvent* event) {
    std::lock_guard<std::mutex> lock(mMetricsMutex);
    StatsdStats::getInstance().noteAtomLogged(
        event->GetTagId(), event->GetTimestampNs() / NS_PER_SEC);
        event->GetTagId(), event->GetElapsedTimestampNs() / NS_PER_SEC);

    // Hard-coded logic to update the isolated uid's in the uid-map.
    // The field numbers need to be currently updated by hand with atoms.proto
@@ -148,10 +149,10 @@ void StatsLogProcessor::OnLogEvent(LogEvent* event) {
        return;
    }

    long curTime = time(nullptr);
    if (curTime - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
        mStatsPullerManager.ClearPullerCacheIfNecessary(curTime);
        mLastPullerCacheClearTimeSec = curTime;
    uint64_t curTimeSec = getElapsedRealtimeSec();
    if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) {
        mStatsPullerManager.ClearPullerCacheIfNecessary(curTimeSec);
        mLastPullerCacheClearTimeSec = curTimeSec;
    }

    if (event->GetTagId() != android::util::ISOLATED_UID_CHANGED) {
@@ -162,7 +163,7 @@ void StatsLogProcessor::OnLogEvent(LogEvent* event) {
    // pass the event to metrics managers.
    for (auto& pair : mMetricsManagers) {
        pair.second->onLogEvent(*event);
        flushIfNecessaryLocked(event->GetTimestampNs(), pair.first, *(pair.second));
        flushIfNecessaryLocked(event->GetElapsedTimestampNs(), pair.first, *(pair.second));
    }
}

@@ -242,6 +243,7 @@ void StatsLogProcessor::onDumpReportLocked(const ConfigKey& key, const uint64_t
    long long reportsToken =
            proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS);

    int64_t lastReportTimeNs = it->second->getLastReportTimeNs();
    // First, fill in ConfigMetricsReport using current data on memory, which
    // starts from filling in StatsLogReport's.
    it->second->onDumpReport(dumpTimeStampNs, &proto);
@@ -254,10 +256,10 @@ void StatsLogProcessor::onDumpReportLocked(const ConfigKey& key, const uint64_t
    proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UID_MAP, uidMapBuffer, uidMapSize);

    // Fill in the timestamps.
    proto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_NANOS,
                (long long)it->second->getLastReportTimeNs());
    proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_NANOS,
                (long long)::android::elapsedRealtimeNano());
    proto.write(FIELD_TYPE_INT64 | FIELD_ID_LAST_REPORT_ELAPSED_NANOS,
                (long long)lastReportTimeNs);
    proto.write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS,
                (long long)dumpTimeStampNs);

    // End of ConfigMetricsReport (reports).
    proto.end(reportsToken);
@@ -340,8 +342,8 @@ void StatsLogProcessor::WriteDataToDisk() {
        vector<uint8_t> data;
        onDumpReportLocked(key, time(nullptr) * NS_PER_SEC, &data);
        // TODO: Add a guardrail to prevent accumulation of file on disk.
        string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR, time(nullptr),
                                        key.GetUid(), (long long)key.GetId());
        string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR,
             (long)getWallClockSec(), key.GetUid(), (long long)key.GetId());
        StorageManager::writeFile(file_name.c_str(), &data[0], data.size());
    }
}
+15 −16
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#include "Log.h"

#include "StatsService.h"
#include "stats_log_util.h"
#include "android-base/stringprintf.h"
#include "config/ConfigKey.h"
#include "config/ConfigManager.h"
@@ -79,7 +80,8 @@ StatsService::StatsService(const sp<Looper>& handlerLooper)
    mUidMap = new UidMap();
    StatsPuller::SetUidMap(mUidMap);
    mConfigManager = new ConfigManager();
    mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, time(nullptr), [this](const ConfigKey& key) {
    mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, getElapsedRealtimeSec(),
        [this](const ConfigKey& key) {
            sp<IStatsCompanionService> sc = getStatsCompanionService();
            auto receiver = mConfigManager->GetConfigReceiver(key);
            if (sc == nullptr) {
@@ -90,7 +92,8 @@ StatsService::StatsService(const sp<Looper>& handlerLooper)
            } else {
                sc->sendDataBroadcast(receiver);
            }
    });
        }
    );

    mConfigManager->AddListener(mProcessor);

@@ -668,11 +671,7 @@ Status StatsService::informAnomalyAlarmFired() {
        return Status::fromExceptionCode(Status::EX_SECURITY,
                                         "Only system uid can call informAnomalyAlarmFired");
    }

    // TODO: This may be a bug. time(nullptr) can be off (wrt AlarmManager's time) and cause us to
    //       miss the alarm! Eventually we will switch to using elapsedRealTime everywhere,
    //       which may hopefully fix the problem, so we'll leave this alone for now.
    uint64_t currentTimeSec = time(nullptr);
    uint64_t currentTimeSec = getElapsedRealtimeSec();
    std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet =
            mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
    if (anomalySet.size() > 0) {
+6 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include "guardrail/StatsdStats.h"
#include "logd/LogEvent.h"
#include "statslog.h"
#include "stats_log_util.h"

using std::make_shared;
using std::shared_ptr;
@@ -62,7 +63,9 @@ bool CpuTimePerUidFreqPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
        return false;
    }

    uint64_t timestamp = time(nullptr) * NS_PER_SEC;
    int64_t wallClockTimestampNs = getWallClockNs();
    int64_t elapsedTimestampNs = getElapsedRealtimeNs();

    char buf[kLineBufferSize];
    // first line prints the format and frequencies
    fin.getline(buf, kLineBufferSize);
@@ -77,7 +80,8 @@ bool CpuTimePerUidFreqPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
        int idx = 0;
        do {
            timeMs = std::stoull(pch);
            auto ptr = make_shared<LogEvent>(android::util::CPU_TIME_PER_UID_FREQ, timestamp);
            auto ptr = make_shared<LogEvent>(android::util::CPU_TIME_PER_UID_FREQ,
                wallClockTimestampNs, elapsedTimestampNs);
            ptr->write(uid);
            ptr->write(idx);
            ptr->write(timeMs);
+5 −2
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include "guardrail/StatsdStats.h"
#include "logd/LogEvent.h"
#include "statslog.h"
#include "stats_log_util.h"

using std::make_shared;
using std::shared_ptr;
@@ -57,7 +58,8 @@ bool CpuTimePerUidPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
        return false;
    }

    uint64_t timestamp = time(nullptr) * NS_PER_SEC;
    int64_t wallClockTimestampNs = getWallClockNs();
    int64_t elapsedTimestampNs = getElapsedRealtimeNs();
    char buf[kLineBufferSize];
    char* pch;
    while (!fin.eof()) {
@@ -70,7 +72,8 @@ bool CpuTimePerUidPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
        pch = strtok(buf, " ");
        uint64_t sysTimeMs = std::stoull(pch);

        auto ptr = make_shared<LogEvent>(android::util::CPU_TIME_PER_UID, timestamp);
        auto ptr = make_shared<LogEvent>(android::util::CPU_TIME_PER_UID,
            wallClockTimestampNs, elapsedTimestampNs);
        ptr->write(uid);
        ptr->write(userTimeMs);
        ptr->write(sysTimeMs);
+5 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include "guardrail/StatsdStats.h"
#include "logd/LogEvent.h"
#include "statslog.h"
#include "stats_log_util.h"

using std::make_shared;
using std::shared_ptr;
@@ -57,7 +58,9 @@ bool KernelUidCpuActiveTimeReader::PullInternal(vector<shared_ptr<LogEvent>>* da
        return false;
    }

    uint64_t timestamp = time(nullptr) * NS_PER_SEC;
    int64_t wallClockTimestampNs = getWallClockNs();
    int64_t elapsedTimestampNs = getElapsedRealtimeNs();

    char buf[kLineBufferSize];
    char* pch;
    while (!fin.eof()) {
@@ -70,7 +73,7 @@ bool KernelUidCpuActiveTimeReader::PullInternal(vector<shared_ptr<LogEvent>>* da
        int idx = 0;
        do {
            timeMs = std::stoull(pch);
            auto ptr = make_shared<LogEvent>(mTagId, timestamp);
            auto ptr = make_shared<LogEvent>(mTagId, wallClockTimestampNs, elapsedTimestampNs);
            ptr->write(uid);
            ptr->write(idx);
            ptr->write(timeMs);
Loading