diff options
Diffstat (limited to 'cmds/statsd/src/StatsLogProcessor.cpp')
-rw-r--r-- | cmds/statsd/src/StatsLogProcessor.cpp | 112 |
1 files changed, 47 insertions, 65 deletions
diff --git a/cmds/statsd/src/StatsLogProcessor.cpp b/cmds/statsd/src/StatsLogProcessor.cpp index e7f1caf26932..f0f599317352 100644 --- a/cmds/statsd/src/StatsLogProcessor.cpp +++ b/cmds/statsd/src/StatsLogProcessor.cpp @@ -72,18 +72,20 @@ const int FIELD_ID_STRINGS = 9; #define STATS_DATA_DIR "/data/misc/stats-data" StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, + const sp<StatsPullerManager>& pullerManager, const sp<AlarmMonitor>& anomalyAlarmMonitor, const sp<AlarmMonitor>& periodicAlarmMonitor, const int64_t timeBaseNs, const std::function<bool(const ConfigKey&)>& sendBroadcast) : mUidMap(uidMap), + mPullerManager(pullerManager), mAnomalyAlarmMonitor(anomalyAlarmMonitor), mPeriodicAlarmMonitor(periodicAlarmMonitor), mSendBroadcast(sendBroadcast), mTimeBaseNs(timeBaseNs), mLargestTimestampSeen(0), mLastTimestampSeen(0) { - mStatsPullerManager.ForceClearPullerCache(); + mPullerManager->ForceClearPullerCache(); } StatsLogProcessor::~StatsLogProcessor() { @@ -152,17 +154,13 @@ void StatsLogProcessor::onIsolatedUidChangedEventLocked(const LogEvent& event) { if (is_create) { mUidMap->assignIsolatedUid(isolated_uid, parent_uid); } else { - mUidMap->removeIsolatedUid(isolated_uid, parent_uid); + mUidMap->removeIsolatedUid(isolated_uid); } } else { ALOGE("Failed to parse uid in the isolated uid change event."); } } -void StatsLogProcessor::OnLogEvent(LogEvent* event) { - OnLogEvent(event, false); -} - void StatsLogProcessor::resetConfigs() { std::lock_guard<std::mutex> lock(mMetricsMutex); resetConfigsLocked(getElapsedRealtimeNs()); @@ -176,7 +174,7 @@ void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs) { resetConfigsLocked(timestampNs, configKeys); } -void StatsLogProcessor::OnLogEvent(LogEvent* event, bool reconnected) { +void StatsLogProcessor::OnLogEvent(LogEvent* event) { std::lock_guard<std::mutex> lock(mMetricsMutex); #ifdef VERY_VERBOSE_PRINTING @@ -186,41 +184,6 @@ void StatsLogProcessor::OnLogEvent(LogEvent* event, bool reconnected) { #endif const int64_t currentTimestampNs = event->GetElapsedTimestampNs(); - if (reconnected && mLastTimestampSeen != 0) { - // LogReader tells us the connection has just been reset. Now we need - // to enter reconnection state to find the last CP. - mInReconnection = true; - } - - if (mInReconnection) { - // We see the checkpoint - if (currentTimestampNs == mLastTimestampSeen) { - mInReconnection = false; - // Found the CP. ignore this event, and we will start to read from next event. - return; - } - if (currentTimestampNs > mLargestTimestampSeen) { - // We see a new log but CP has not been found yet. Give up now. - mLogLossCount++; - mInReconnection = false; - StatsdStats::getInstance().noteLogLost(currentTimestampNs); - // Persist the data before we reset. Do we want this? - WriteDataToDiskLocked(CONFIG_RESET); - // We see fresher event before we see the checkpoint. We might have lost data. - // The best we can do is to reset. - resetConfigsLocked(currentTimestampNs); - } else { - // Still in search of the CP. Keep going. - return; - } - } - - mLogCount++; - mLastTimestampSeen = currentTimestampNs; - if (mLargestTimestampSeen < currentTimestampNs) { - mLargestTimestampSeen = currentTimestampNs; - } - resetIfConfigTtlExpiredLocked(currentTimestampNs); StatsdStats::getInstance().noteAtomLogged( @@ -238,7 +201,7 @@ void StatsLogProcessor::OnLogEvent(LogEvent* event, bool reconnected) { int64_t curTimeSec = getElapsedRealtimeSec(); if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) { - mStatsPullerManager.ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC); + mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC); mLastPullerCacheClearTimeSec = curTimeSec; } @@ -266,8 +229,8 @@ void StatsLogProcessor::OnConfigUpdatedLocked( const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) { VLOG("Updated configuration for key %s", key.ToString().c_str()); sp<MetricsManager> newMetricsManager = - new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, - mAnomalyAlarmMonitor, mPeriodicAlarmMonitor); + new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager, + mAnomalyAlarmMonitor, mPeriodicAlarmMonitor); if (newMetricsManager->isConfigValid()) { mUidMap->OnConfigUpdated(key); if (newMetricsManager->shouldAddUidMapListener()) { @@ -294,35 +257,40 @@ size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const { return it->second->byteSize(); } -void StatsLogProcessor::dumpStates(FILE* out, bool verbose) { +void StatsLogProcessor::dumpStates(int out, bool verbose) { std::lock_guard<std::mutex> lock(mMetricsMutex); - fprintf(out, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size()); + FILE* fout = fdopen(out, "w"); + if (fout == NULL) { + return; + } + fprintf(fout, "MetricsManager count: %lu\n", (unsigned long)mMetricsManagers.size()); for (auto metricsManager : mMetricsManagers) { - metricsManager.second->dumpStates(out, verbose); + metricsManager.second->dumpStates(fout, verbose); } + + fclose(fout); } /* - * onDumpReport dumps serialized ConfigMetricsReportList into outData. + * onDumpReport dumps serialized ConfigMetricsReportList into proto. */ void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs, const bool include_current_partial_bucket, + const bool erase_data, const DumpReportReason dumpReportReason, - vector<uint8_t>* outData) { + ProtoOutputStream* proto) { std::lock_guard<std::mutex> lock(mMetricsMutex); - ProtoOutputStream proto; - // Start of ConfigKey. - uint64_t configKeyToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); - proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); - proto.write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId()); - proto.end(configKeyToken); + uint64_t configKeyToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_ID_CONFIG_KEY); + proto->write(FIELD_TYPE_INT32 | FIELD_ID_UID, key.GetUid()); + proto->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)key.GetId()); + proto->end(configKeyToken); // End of ConfigKey. // Then, check stats-data directory to see there's any file containing // ConfigMetricsReport from previous shutdowns to concatenate to reports. - StorageManager::appendConfigMetricsReport(key, &proto); + StorageManager::appendConfigMetricsReport(key, proto); auto it = mMetricsManagers.find(key); if (it != mMetricsManagers.end()) { @@ -332,14 +300,27 @@ void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTim // Start of ConfigMetricsReport (reports). uint64_t reportsToken = - proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS); + proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_REPORTS); onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket, - dumpReportReason, &proto); - proto.end(reportsToken); + erase_data, dumpReportReason, proto); + proto->end(reportsToken); // End of ConfigMetricsReport (reports). } else { ALOGW("Config source %s does not exist", key.ToString().c_str()); } +} + +/* + * onDumpReport dumps serialized ConfigMetricsReportList into outData. + */ +void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs, + const bool include_current_partial_bucket, + const bool erase_data, + const DumpReportReason dumpReportReason, + vector<uint8_t>* outData) { + ProtoOutputStream proto; + onDumpReport(key, dumpTimeStampNs, include_current_partial_bucket, erase_data, + dumpReportReason, &proto); if (outData != nullptr) { outData->clear(); @@ -363,6 +344,7 @@ void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTim void StatsLogProcessor::onConfigMetricsReportLocked(const ConfigKey& key, const int64_t dumpTimeStampNs, const bool include_current_partial_bucket, + const bool erase_data, const DumpReportReason dumpReportReason, ProtoOutputStream* proto) { // We already checked whether key exists in mMetricsManagers in @@ -379,7 +361,7 @@ void StatsLogProcessor::onConfigMetricsReportLocked(const ConfigKey& key, // First, fill in ConfigMetricsReport using current data on memory, which // starts from filling in StatsLogReport's. it->second->onDumpReport(dumpTimeStampNs, include_current_partial_bucket, - &str_set, proto); + erase_data, &str_set, proto); // Fill in UidMap if there is at least one metric to report. // This skips the uid map if it's an empty config. @@ -453,7 +435,7 @@ void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { mLastBroadcastTimes.erase(key); if (mMetricsManagers.empty()) { - mStatsPullerManager.ForceClearPullerCache(); + mPullerManager->ForceClearPullerCache(); } } @@ -473,7 +455,7 @@ void StatsLogProcessor::flushIfNecessaryLocked( if (totalBytes > StatsdStats::kMaxMetricsBytesPerConfig) { // Too late. We need to start clearing data. metricsManager.dropData(timestampNs); - StatsdStats::getInstance().noteDataDropped(key); + StatsdStats::getInstance().noteDataDropped(key, totalBytes); VLOG("StatsD had to toss out metrics for %s", key.ToString().c_str()); } else if ((totalBytes > StatsdStats::kBytesPerConfigTriggerGetData) || (mOnDiskDataConfigs.find(key) != mOnDiskDataConfigs.end())) { @@ -510,7 +492,7 @@ void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key, } ProtoOutputStream proto; onConfigMetricsReportLocked(key, timestampNs, true /* include_current_partial_bucket*/, - dumpReportReason, &proto); + true /* erase_data */, dumpReportReason, &proto); string file_name = StringPrintf("%s/%ld_%d_%lld", STATS_DATA_DIR, (long)getWallClockSec(), key.GetUid(), (long long)key.GetId()); android::base::unique_fd fd(open(file_name.c_str(), @@ -538,7 +520,7 @@ void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason) void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) { std::lock_guard<std::mutex> lock(mMetricsMutex); - mStatsPullerManager.OnAlarmFired(timestampNs); + mPullerManager->OnAlarmFired(timestampNs); } int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) { |