diff options
Diffstat (limited to 'cmds/statsd/src/StatsLogProcessor.cpp')
-rw-r--r-- | cmds/statsd/src/StatsLogProcessor.cpp | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/cmds/statsd/src/StatsLogProcessor.cpp b/cmds/statsd/src/StatsLogProcessor.cpp index ec02b121d0dd..4e0a8ebdf380 100644 --- a/cmds/statsd/src/StatsLogProcessor.cpp +++ b/cmds/statsd/src/StatsLogProcessor.cpp @@ -611,11 +611,8 @@ void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key, void StatsLogProcessor::SaveActiveConfigsToDisk(int64_t currentTimeNs) { std::lock_guard<std::mutex> lock(mMetricsMutex); - const int64_t timeNs = getElapsedRealtimeNs(); // Do not write to disk if we already have in the last few seconds. - // This is to avoid overwriting files that would have the same name if we - // write twice in the same second. if (static_cast<unsigned long long> (timeNs) < mLastActiveMetricsWriteNs + WRITE_DATA_COOL_DOWN_SEC * NS_PER_SEC) { ALOGI("Statsd skipping writing active metrics to disk. Already wrote data in last %d seconds", @@ -625,13 +622,7 @@ void StatsLogProcessor::SaveActiveConfigsToDisk(int64_t currentTimeNs) { mLastActiveMetricsWriteNs = timeNs; ProtoOutputStream proto; - for (const auto& pair : mMetricsManagers) { - const sp<MetricsManager>& metricsManager = pair.second; - uint64_t configToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | - FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG); - metricsManager->writeActiveConfigToProtoOutputStream(currentTimeNs, &proto); - proto.end(configToken); - } + WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, DEVICE_SHUTDOWN, &proto); string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR); StorageManager::deleteFile(file_name.c_str()); @@ -644,9 +635,24 @@ void StatsLogProcessor::SaveActiveConfigsToDisk(int64_t currentTimeNs) { proto.flush(fd.get()); } -void StatsLogProcessor::LoadActiveConfigsFromDisk() { +void StatsLogProcessor::WriteActiveConfigsToProtoOutputStream( + int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) { std::lock_guard<std::mutex> lock(mMetricsMutex); + WriteActiveConfigsToProtoOutputStreamLocked(currentTimeNs, reason, proto); +} +void StatsLogProcessor::WriteActiveConfigsToProtoOutputStreamLocked( + int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto) { + for (const auto& pair : mMetricsManagers) { + const sp<MetricsManager>& metricsManager = pair.second; + uint64_t configToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | + FIELD_ID_ACTIVE_CONFIG_LIST_CONFIG); + metricsManager->writeActiveConfigToProtoOutputStream(currentTimeNs, reason, proto); + proto->end(configToken); + } +} +void StatsLogProcessor::LoadActiveConfigsFromDisk() { + std::lock_guard<std::mutex> lock(mMetricsMutex); string file_name = StringPrintf("%s/active_metrics", STATS_ACTIVE_METRIC_DIR); int fd = open(file_name.c_str(), O_RDONLY | O_CLOEXEC); if (-1 == fd) { @@ -670,6 +676,19 @@ void StatsLogProcessor::LoadActiveConfigsFromDisk() { StorageManager::deleteFile(file_name.c_str()); return; } + // Passing in mTimeBaseNs only works as long as we only load from disk is when statsd starts. + SetConfigsActiveStateLocked(activeConfigList, mTimeBaseNs); + StorageManager::deleteFile(file_name.c_str()); +} + +void StatsLogProcessor::SetConfigsActiveState(const ActiveConfigList& activeConfigList, + int64_t currentTimeNs) { + std::lock_guard<std::mutex> lock(mMetricsMutex); + SetConfigsActiveStateLocked(activeConfigList, currentTimeNs); +} + +void StatsLogProcessor::SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList, + int64_t currentTimeNs) { for (int i = 0; i < activeConfigList.config_size(); i++) { const auto& config = activeConfigList.config(i); ConfigKey key(config.uid(), config.id()); @@ -679,11 +698,9 @@ void StatsLogProcessor::LoadActiveConfigsFromDisk() { continue; } VLOG("Setting active config %s", key.ToString().c_str()); - it->second->loadActiveConfig(config, mTimeBaseNs); + it->second->loadActiveConfig(config, currentTimeNs); } VLOG("Successfully loaded %d active configs.", activeConfigList.config_size()); - - StorageManager::deleteFile(file_name.c_str()); } void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason, |