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

Commit 475677e7 authored by Jeffrey Huang's avatar Jeffrey Huang
Browse files

Load Metadata from Disk

Bug: 148280505
Test: bit statsd_test:*
Test: Manual - Added Config, Tested System Server Crash
Test: Manual - Added Config, Tested Device restart
Change-Id: Icd1b1e57bbf24e4a5676820a2b955f2034d4b37d
parent b8f5403d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -340,6 +340,7 @@ cc_test {
        "tests/log_event/LogEventQueue_test.cpp",
        "tests/LogEntryMatcher_test.cpp",
        "tests/LogEvent_test.cpp",
        "tests/metadata_util_test.cpp",
        "tests/metrics/CountMetricProducer_test.cpp",
        "tests/metrics/DurationMetricProducer_test.cpp",
        "tests/metrics/EventMetricProducer_test.cpp",
+54 −0
Original line number Diff line number Diff line
@@ -902,6 +902,60 @@ void StatsLogProcessor::WriteMetadataToProtoLocked(int64_t currentWallClockTimeN
    }
}

void StatsLogProcessor::LoadMetadataFromDisk(int64_t currentWallClockTimeNs,
                                             int64_t systemElapsedTimeNs) {
    std::lock_guard<std::mutex> lock(mMetricsMutex);
    string file_name = StringPrintf("%s/metadata", STATS_METADATA_DIR);
    int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC);
    if (-1 == fd) {
        VLOG("Attempt to read %s but failed", file_name.c_str());
        StorageManager::deleteFile(file_name.c_str());
        return;
    }
    string content;
    if (!android::base::ReadFdToString(fd, &content)) {
        ALOGE("Attempt to read %s but failed", file_name.c_str());
        close(fd);
        StorageManager::deleteFile(file_name.c_str());
        return;
    }

    close(fd);

    metadata::StatsMetadataList statsMetadataList;
    if (!statsMetadataList.ParseFromString(content)) {
        ALOGE("Attempt to read %s but failed; failed to metadata", file_name.c_str());
        StorageManager::deleteFile(file_name.c_str());
        return;
    }
    SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
    StorageManager::deleteFile(file_name.c_str());
}

void StatsLogProcessor::SetMetadataState(const metadata::StatsMetadataList& statsMetadataList,
                                         int64_t currentWallClockTimeNs,
                                         int64_t systemElapsedTimeNs) {
    std::lock_guard<std::mutex> lock(mMetricsMutex);
    SetMetadataStateLocked(statsMetadataList, currentWallClockTimeNs, systemElapsedTimeNs);
}

void StatsLogProcessor::SetMetadataStateLocked(
        const metadata::StatsMetadataList& statsMetadataList,
        int64_t currentWallClockTimeNs,
        int64_t systemElapsedTimeNs) {
    for (const metadata::StatsMetadata& metadata : statsMetadataList.stats_metadata()) {
        ConfigKey key(metadata.config_key().uid(), metadata.config_key().config_id());
        auto it = mMetricsManagers.find(key);
        if (it == mMetricsManagers.end()) {
            ALOGE("No config found for configKey %s", key.ToString().c_str());
            continue;
        }
        VLOG("Setting metadata %s", key.ToString().c_str());
        it->second->loadMetadata(metadata, currentWallClockTimeNs, systemElapsedTimeNs);
    }
    VLOG("Successfully loaded %d metadata.", statsMetadataList.stats_metadata_size());
}

void StatsLogProcessor::WriteActiveConfigsToProtoOutputStream(
        int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) {
    std::lock_guard<std::mutex> lock(mMetricsMutex);
+14 −0
Original line number Diff line number Diff line
@@ -98,6 +98,15 @@ public:
                              int64_t systemElapsedTimeNs,
                              metadata::StatsMetadataList* metadataList);

    /* Load stats metadata for configs and metrics from disk. */
    void LoadMetadataFromDisk(int64_t currentWallClockTimeNs,
                              int64_t systemElapsedTimeNs);

    /* Sets the metadata for all configs and metrics */
    void SetMetadataState(const metadata::StatsMetadataList& statsMetadataList,
                          int64_t currentWallClockTimeNs,
                          int64_t systemElapsedTimeNs);

    /* Sets the active status/ttl for all configs and metrics to the status in ActiveConfigList. */
    void SetConfigsActiveState(const ActiveConfigList& activeConfigList, int64_t currentTimeNs);

@@ -182,6 +191,10 @@ private:
    void SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList,
                                     int64_t currentTimeNs);

    void SetMetadataStateLocked(const metadata::StatsMetadataList& statsMetadataList,
                                int64_t currentWallClockTimeNs,
                                int64_t systemElapsedTimeNs);

    void WriteMetadataToProtoLocked(int64_t currentWallClockTimeNs,
                                    int64_t systemElapsedTimeNs,
                                    metadata::StatsMetadataList* metadataList);
@@ -297,6 +310,7 @@ private:
    FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
    FRIEND_TEST(AnomalyDetectionE2eTest, TestCountMetric_save_refractory_to_disk_no_data_written);
    FRIEND_TEST(AnomalyDetectionE2eTest, TestCountMetric_save_refractory_to_disk);
    FRIEND_TEST(AnomalyDetectionE2eTest, TestCountMetric_load_refractory_from_disk);
    FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
    FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
    FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
+2 −0
Original line number Diff line number Diff line
@@ -1042,6 +1042,7 @@ Status StatsService::statsCompanionReady() {
void StatsService::Startup() {
    mConfigManager->Startup();
    mProcessor->LoadActiveConfigsFromDisk();
    mProcessor->LoadMetadataFromDisk(getWallClockNs(), getElapsedRealtimeNs());
}

void StatsService::Terminate() {
@@ -1298,6 +1299,7 @@ void StatsService::statsCompanionServiceDiedImpl() {
                mProcessor->SetConfigsActiveState(activeConfigs, systemServerRestartNs);
            }
        }
        mProcessor->SetMetadataState(metadataList, getWallClockNs(), systemServerRestartNs);
    }
    mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
    mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
+18 −0
Original line number Diff line number Diff line
@@ -298,6 +298,24 @@ bool AnomalyTracker::writeAlertMetadataToProto(int64_t currentWallClockTimeNs,
    return metadataWritten;
}

void AnomalyTracker::loadAlertMetadata(
        const metadata::AlertMetadata& alertMetadata,
        int64_t currentWallClockTimeNs,
        int64_t systemElapsedTimeNs) {
    for (const metadata::AlertDimensionKeyedData& keyedData :
            alertMetadata.alert_dim_keyed_data()) {
        if ((uint64_t) keyedData.last_refractory_ends_sec() < currentWallClockTimeNs / NS_PER_SEC) {
            // Do not update the timestamp if it has already expired.
            continue;
        }
        MetricDimensionKey metricKey = loadMetricDimensionKeyFromProto(
                keyedData.dimension_key());
        int32_t refractoryPeriodEndsSec = (int32_t) keyedData.last_refractory_ends_sec() -
                currentWallClockTimeNs / NS_PER_SEC + systemElapsedTimeNs / NS_PER_SEC;
        mRefractoryPeriodEndsSec[metricKey] = refractoryPeriodEndsSec;
    }
}

}  // namespace statsd
}  // namespace os
}  // namespace android
Loading