Home | History | Annotate | Download | only in guardrail
      1 /*
      2  * Copyright 2017, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 #define DEBUG false  // STOPSHIP if true
     17 #include "Log.h"
     18 
     19 #include "StatsdStats.h"
     20 
     21 #include <android/util/ProtoOutputStream.h>
     22 #include "../stats_log_util.h"
     23 #include "statslog.h"
     24 #include "storage/StorageManager.h"
     25 
     26 namespace android {
     27 namespace os {
     28 namespace statsd {
     29 
     30 using android::util::FIELD_COUNT_REPEATED;
     31 using android::util::FIELD_TYPE_BOOL;
     32 using android::util::FIELD_TYPE_FLOAT;
     33 using android::util::FIELD_TYPE_INT32;
     34 using android::util::FIELD_TYPE_INT64;
     35 using android::util::FIELD_TYPE_MESSAGE;
     36 using android::util::FIELD_TYPE_STRING;
     37 using android::util::ProtoOutputStream;
     38 using std::lock_guard;
     39 using std::map;
     40 using std::shared_ptr;
     41 using std::string;
     42 using std::vector;
     43 
     44 const int FIELD_ID_BEGIN_TIME = 1;
     45 const int FIELD_ID_END_TIME = 2;
     46 const int FIELD_ID_CONFIG_STATS = 3;
     47 const int FIELD_ID_ATOM_STATS = 7;
     48 const int FIELD_ID_UIDMAP_STATS = 8;
     49 const int FIELD_ID_ANOMALY_ALARM_STATS = 9;
     50 // const int FIELD_ID_PULLED_ATOM_STATS = 10; // The proto is written in stats_log_util.cpp
     51 const int FIELD_ID_LOGGER_ERROR_STATS = 11;
     52 const int FIELD_ID_PERIODIC_ALARM_STATS = 12;
     53 const int FIELD_ID_LOG_LOSS_STATS = 14;
     54 const int FIELD_ID_SYSTEM_SERVER_RESTART = 15;
     55 
     56 const int FIELD_ID_ATOM_STATS_TAG = 1;
     57 const int FIELD_ID_ATOM_STATS_COUNT = 2;
     58 
     59 const int FIELD_ID_ANOMALY_ALARMS_REGISTERED = 1;
     60 const int FIELD_ID_PERIODIC_ALARMS_REGISTERED = 1;
     61 
     62 const int FIELD_ID_LOGGER_STATS_TIME = 1;
     63 const int FIELD_ID_LOGGER_STATS_ERROR_CODE = 2;
     64 
     65 const int FIELD_ID_CONFIG_STATS_UID = 1;
     66 const int FIELD_ID_CONFIG_STATS_ID = 2;
     67 const int FIELD_ID_CONFIG_STATS_CREATION = 3;
     68 const int FIELD_ID_CONFIG_STATS_RESET = 19;
     69 const int FIELD_ID_CONFIG_STATS_DELETION = 4;
     70 const int FIELD_ID_CONFIG_STATS_METRIC_COUNT = 5;
     71 const int FIELD_ID_CONFIG_STATS_CONDITION_COUNT = 6;
     72 const int FIELD_ID_CONFIG_STATS_MATCHER_COUNT = 7;
     73 const int FIELD_ID_CONFIG_STATS_ALERT_COUNT = 8;
     74 const int FIELD_ID_CONFIG_STATS_VALID = 9;
     75 const int FIELD_ID_CONFIG_STATS_BROADCAST = 10;
     76 const int FIELD_ID_CONFIG_STATS_DATA_DROP = 11;
     77 const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME = 12;
     78 const int FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES = 20;
     79 const int FIELD_ID_CONFIG_STATS_MATCHER_STATS = 13;
     80 const int FIELD_ID_CONFIG_STATS_CONDITION_STATS = 14;
     81 const int FIELD_ID_CONFIG_STATS_METRIC_STATS = 15;
     82 const int FIELD_ID_CONFIG_STATS_ALERT_STATS = 16;
     83 const int FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS = 17;
     84 const int FIELD_ID_CONFIG_STATS_ANNOTATION = 18;
     85 const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT64 = 1;
     86 const int FIELD_ID_CONFIG_STATS_ANNOTATION_INT32 = 2;
     87 
     88 const int FIELD_ID_MATCHER_STATS_ID = 1;
     89 const int FIELD_ID_MATCHER_STATS_COUNT = 2;
     90 const int FIELD_ID_CONDITION_STATS_ID = 1;
     91 const int FIELD_ID_CONDITION_STATS_COUNT = 2;
     92 const int FIELD_ID_METRIC_STATS_ID = 1;
     93 const int FIELD_ID_METRIC_STATS_COUNT = 2;
     94 const int FIELD_ID_ALERT_STATS_ID = 1;
     95 const int FIELD_ID_ALERT_STATS_COUNT = 2;
     96 
     97 const int FIELD_ID_UID_MAP_CHANGES = 1;
     98 const int FIELD_ID_UID_MAP_BYTES_USED = 2;
     99 const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 3;
    100 const int FIELD_ID_UID_MAP_DELETED_APPS = 4;
    101 
    102 const std::map<int, std::pair<size_t, size_t>> StatsdStats::kAtomDimensionKeySizeLimitMap = {
    103         {android::util::CPU_TIME_PER_UID_FREQ, {6000, 10000}},
    104 };
    105 
    106 // TODO: add stats for pulled atoms.
    107 StatsdStats::StatsdStats() {
    108     mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
    109     mStartTimeSec = getWallClockSec();
    110 }
    111 
    112 StatsdStats& StatsdStats::getInstance() {
    113     static StatsdStats statsInstance;
    114     return statsInstance;
    115 }
    116 
    117 void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
    118     // The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
    119     if (mIceBox.size() == kMaxIceBoxSize) {
    120         mIceBox.pop_front();
    121     }
    122     mIceBox.push_back(stats);
    123 }
    124 
    125 void StatsdStats::noteConfigReceived(
    126         const ConfigKey& key, int metricsCount, int conditionsCount, int matchersCount,
    127         int alertsCount, const std::list<std::pair<const int64_t, const int32_t>>& annotations,
    128         bool isValid) {
    129     lock_guard<std::mutex> lock(mLock);
    130     int32_t nowTimeSec = getWallClockSec();
    131 
    132     // If there is an existing config for the same key, icebox the old config.
    133     noteConfigRemovedInternalLocked(key);
    134 
    135     shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
    136     configStats->uid = key.GetUid();
    137     configStats->id = key.GetId();
    138     configStats->creation_time_sec = nowTimeSec;
    139     configStats->metric_count = metricsCount;
    140     configStats->condition_count = conditionsCount;
    141     configStats->matcher_count = matchersCount;
    142     configStats->alert_count = alertsCount;
    143     configStats->is_valid = isValid;
    144     for (auto& v : annotations) {
    145         configStats->annotations.emplace_back(v);
    146     }
    147 
    148     if (isValid) {
    149         mConfigStats[key] = configStats;
    150     } else {
    151         configStats->deletion_time_sec = nowTimeSec;
    152         addToIceBoxLocked(configStats);
    153     }
    154 }
    155 
    156 void StatsdStats::noteConfigRemovedInternalLocked(const ConfigKey& key) {
    157     auto it = mConfigStats.find(key);
    158     if (it != mConfigStats.end()) {
    159         int32_t nowTimeSec = getWallClockSec();
    160         it->second->deletion_time_sec = nowTimeSec;
    161         addToIceBoxLocked(it->second);
    162         mConfigStats.erase(it);
    163     }
    164 }
    165 
    166 void StatsdStats::noteConfigRemoved(const ConfigKey& key) {
    167     lock_guard<std::mutex> lock(mLock);
    168     noteConfigRemovedInternalLocked(key);
    169 }
    170 
    171 void StatsdStats::noteConfigResetInternalLocked(const ConfigKey& key) {
    172     auto it = mConfigStats.find(key);
    173     if (it != mConfigStats.end()) {
    174         it->second->reset_time_sec = getWallClockSec();
    175     }
    176 }
    177 
    178 void StatsdStats::noteConfigReset(const ConfigKey& key) {
    179     lock_guard<std::mutex> lock(mLock);
    180     noteConfigResetInternalLocked(key);
    181 }
    182 
    183 void StatsdStats::noteLogLost(int64_t timestampNs) {
    184     lock_guard<std::mutex> lock(mLock);
    185     if (mLogLossTimestampNs.size() == kMaxLoggerErrors) {
    186         mLogLossTimestampNs.pop_front();
    187     }
    188     mLogLossTimestampNs.push_back(timestampNs);
    189 }
    190 
    191 void StatsdStats::noteBroadcastSent(const ConfigKey& key) {
    192     noteBroadcastSent(key, getWallClockSec());
    193 }
    194 
    195 void StatsdStats::noteBroadcastSent(const ConfigKey& key, int32_t timeSec) {
    196     lock_guard<std::mutex> lock(mLock);
    197     auto it = mConfigStats.find(key);
    198     if (it == mConfigStats.end()) {
    199         ALOGE("Config key %s not found!", key.ToString().c_str());
    200         return;
    201     }
    202     if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
    203         it->second->broadcast_sent_time_sec.pop_front();
    204     }
    205     it->second->broadcast_sent_time_sec.push_back(timeSec);
    206 }
    207 
    208 void StatsdStats::noteDataDropped(const ConfigKey& key) {
    209     noteDataDropped(key, getWallClockSec());
    210 }
    211 
    212 void StatsdStats::noteDataDropped(const ConfigKey& key, int32_t timeSec) {
    213     lock_guard<std::mutex> lock(mLock);
    214     auto it = mConfigStats.find(key);
    215     if (it == mConfigStats.end()) {
    216         ALOGE("Config key %s not found!", key.ToString().c_str());
    217         return;
    218     }
    219     if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
    220         it->second->data_drop_time_sec.pop_front();
    221     }
    222     it->second->data_drop_time_sec.push_back(timeSec);
    223 }
    224 
    225 void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes) {
    226     noteMetricsReportSent(key, num_bytes, getWallClockSec());
    227 }
    228 
    229 void StatsdStats::noteMetricsReportSent(const ConfigKey& key, const size_t num_bytes,
    230                                         int32_t timeSec) {
    231     lock_guard<std::mutex> lock(mLock);
    232     auto it = mConfigStats.find(key);
    233     if (it == mConfigStats.end()) {
    234         ALOGE("Config key %s not found!", key.ToString().c_str());
    235         return;
    236     }
    237     if (it->second->dump_report_stats.size() == kMaxTimestampCount) {
    238         it->second->dump_report_stats.pop_front();
    239     }
    240     it->second->dump_report_stats.push_back(std::make_pair(timeSec, num_bytes));
    241 }
    242 
    243 void StatsdStats::noteUidMapDropped(int deltas) {
    244     lock_guard<std::mutex> lock(mLock);
    245     mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
    246 }
    247 
    248 void StatsdStats::noteUidMapAppDeletionDropped() {
    249     lock_guard<std::mutex> lock(mLock);
    250     mUidMapStats.deleted_apps++;
    251 }
    252 
    253 void StatsdStats::setUidMapChanges(int changes) {
    254     lock_guard<std::mutex> lock(mLock);
    255     mUidMapStats.changes = changes;
    256 }
    257 
    258 void StatsdStats::setCurrentUidMapMemory(int bytes) {
    259     lock_guard<std::mutex> lock(mLock);
    260     mUidMapStats.bytes_used = bytes;
    261 }
    262 
    263 void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
    264     lock_guard<std::mutex> lock(mLock);
    265     // if name doesn't exist before, it will create the key with count 0.
    266     auto statsIt = mConfigStats.find(key);
    267     if (statsIt == mConfigStats.end()) {
    268         return;
    269     }
    270 
    271     auto& conditionSizeMap = statsIt->second->condition_stats;
    272     if (size > conditionSizeMap[id]) {
    273         conditionSizeMap[id] = size;
    274     }
    275 }
    276 
    277 void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
    278     lock_guard<std::mutex> lock(mLock);
    279     // if name doesn't exist before, it will create the key with count 0.
    280     auto statsIt = mConfigStats.find(key);
    281     if (statsIt == mConfigStats.end()) {
    282         return;
    283     }
    284     auto& metricsDimensionMap = statsIt->second->metric_stats;
    285     if (size > metricsDimensionMap[id]) {
    286         metricsDimensionMap[id] = size;
    287     }
    288 }
    289 
    290 void StatsdStats::noteMetricDimensionInConditionSize(
    291         const ConfigKey& key, const int64_t& id, int size) {
    292     lock_guard<std::mutex> lock(mLock);
    293     // if name doesn't exist before, it will create the key with count 0.
    294     auto statsIt = mConfigStats.find(key);
    295     if (statsIt == mConfigStats.end()) {
    296         return;
    297     }
    298     auto& metricsDimensionMap = statsIt->second->metric_dimension_in_condition_stats;
    299     if (size > metricsDimensionMap[id]) {
    300         metricsDimensionMap[id] = size;
    301     }
    302 }
    303 
    304 void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
    305     lock_guard<std::mutex> lock(mLock);
    306 
    307     auto statsIt = mConfigStats.find(key);
    308     if (statsIt == mConfigStats.end()) {
    309         return;
    310     }
    311     statsIt->second->matcher_stats[id]++;
    312 }
    313 
    314 void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
    315     lock_guard<std::mutex> lock(mLock);
    316     auto statsIt = mConfigStats.find(key);
    317     if (statsIt == mConfigStats.end()) {
    318         return;
    319     }
    320     statsIt->second->alert_stats[id]++;
    321 }
    322 
    323 void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
    324     lock_guard<std::mutex> lock(mLock);
    325     mAnomalyAlarmRegisteredStats++;
    326 }
    327 
    328 void StatsdStats::noteRegisteredPeriodicAlarmChanged() {
    329     lock_guard<std::mutex> lock(mLock);
    330     mPeriodicAlarmRegisteredStats++;
    331 }
    332 
    333 void StatsdStats::updateMinPullIntervalSec(int pullAtomId, long intervalSec) {
    334     lock_guard<std::mutex> lock(mLock);
    335     mPulledAtomStats[pullAtomId].minPullIntervalSec = intervalSec;
    336 }
    337 
    338 void StatsdStats::notePull(int pullAtomId) {
    339     lock_guard<std::mutex> lock(mLock);
    340     mPulledAtomStats[pullAtomId].totalPull++;
    341 }
    342 
    343 void StatsdStats::notePullFromCache(int pullAtomId) {
    344     lock_guard<std::mutex> lock(mLock);
    345     mPulledAtomStats[pullAtomId].totalPullFromCache++;
    346 }
    347 
    348 void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
    349     lock_guard<std::mutex> lock(mLock);
    350 
    351     if (atomId > android::util::kMaxPushedAtomId) {
    352         ALOGW("not interested in atom %d", atomId);
    353         return;
    354     }
    355 
    356     mPushedAtomStats[atomId]++;
    357 }
    358 
    359 void StatsdStats::noteSystemServerRestart(int32_t timeSec) {
    360     lock_guard<std::mutex> lock(mLock);
    361 
    362     if (mSystemServerRestartSec.size() == kMaxSystemServerRestarts) {
    363         mSystemServerRestartSec.pop_front();
    364     }
    365     mSystemServerRestartSec.push_back(timeSec);
    366 }
    367 
    368 void StatsdStats::noteLoggerError(int error) {
    369     lock_guard<std::mutex> lock(mLock);
    370     // grows strictly one at a time. so it won't > kMaxLoggerErrors
    371     if (mLoggerErrors.size() == kMaxLoggerErrors) {
    372         mLoggerErrors.pop_front();
    373     }
    374     mLoggerErrors.push_back(std::make_pair(getWallClockSec(), error));
    375 }
    376 
    377 void StatsdStats::reset() {
    378     lock_guard<std::mutex> lock(mLock);
    379     resetInternalLocked();
    380 }
    381 
    382 void StatsdStats::resetInternalLocked() {
    383     // Reset the historical data, but keep the active ConfigStats
    384     mStartTimeSec = getWallClockSec();
    385     mIceBox.clear();
    386     std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
    387     mAnomalyAlarmRegisteredStats = 0;
    388     mPeriodicAlarmRegisteredStats = 0;
    389     mLoggerErrors.clear();
    390     mSystemServerRestartSec.clear();
    391     mLogLossTimestampNs.clear();
    392     for (auto& config : mConfigStats) {
    393         config.second->broadcast_sent_time_sec.clear();
    394         config.second->data_drop_time_sec.clear();
    395         config.second->dump_report_stats.clear();
    396         config.second->annotations.clear();
    397         config.second->matcher_stats.clear();
    398         config.second->condition_stats.clear();
    399         config.second->metric_stats.clear();
    400         config.second->metric_dimension_in_condition_stats.clear();
    401         config.second->alert_stats.clear();
    402     }
    403 }
    404 
    405 string buildTimeString(int64_t timeSec) {
    406     time_t t = timeSec;
    407     struct tm* tm = localtime(&t);
    408     char timeBuffer[80];
    409     strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
    410     return string(timeBuffer);
    411 }
    412 
    413 void StatsdStats::dumpStats(FILE* out) const {
    414     lock_guard<std::mutex> lock(mLock);
    415     time_t t = mStartTimeSec;
    416     struct tm* tm = localtime(&t);
    417     char timeBuffer[80];
    418     strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p\n", tm);
    419     fprintf(out, "Stats collection start second: %s\n", timeBuffer);
    420     fprintf(out, "%lu Config in icebox: \n", (unsigned long)mIceBox.size());
    421     for (const auto& configStats : mIceBox) {
    422         fprintf(out,
    423                 "Config {%d_%lld}: creation=%d, deletion=%d, reset=%d, #metric=%d, #condition=%d, "
    424                 "#matcher=%d, #alert=%d,  valid=%d\n",
    425                 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
    426                 configStats->deletion_time_sec, configStats->reset_time_sec,
    427                 configStats->metric_count,
    428                 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
    429                 configStats->is_valid);
    430 
    431         for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
    432             fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
    433         }
    434 
    435         for (const auto& dataDropTime : configStats->data_drop_time_sec) {
    436             fprintf(out, "\tdata drop time: %d\n", dataDropTime);
    437         }
    438     }
    439     fprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
    440     for (auto& pair : mConfigStats) {
    441         auto& configStats = pair.second;
    442         fprintf(out,
    443                 "Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
    444                 "#matcher=%d, #alert=%d,  valid=%d\n",
    445                 configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
    446                 configStats->deletion_time_sec, configStats->metric_count,
    447                 configStats->condition_count, configStats->matcher_count, configStats->alert_count,
    448                 configStats->is_valid);
    449         for (const auto& annotation : configStats->annotations) {
    450             fprintf(out, "\tannotation: %lld, %d\n", (long long)annotation.first,
    451                     annotation.second);
    452         }
    453 
    454         for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
    455             fprintf(out, "\tbroadcast time: %s(%lld)\n",
    456                     buildTimeString(broadcastTime).c_str(), (long long)broadcastTime);
    457         }
    458 
    459         for (const auto& dataDropTime : configStats->data_drop_time_sec) {
    460             fprintf(out, "\tdata drop time: %s(%lld)\n",
    461                     buildTimeString(dataDropTime).c_str(), (long long)dataDropTime);
    462         }
    463 
    464         for (const auto& dump : configStats->dump_report_stats) {
    465             fprintf(out, "\tdump report time: %s(%lld) bytes: %lld\n",
    466                     buildTimeString(dump.first).c_str(), (long long)dump.first,
    467                     (long long)dump.second);
    468         }
    469 
    470         for (const auto& stats : pair.second->matcher_stats) {
    471             fprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
    472         }
    473 
    474         for (const auto& stats : pair.second->condition_stats) {
    475             fprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
    476                     stats.second);
    477         }
    478 
    479         for (const auto& stats : pair.second->condition_stats) {
    480             fprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
    481                     stats.second);
    482         }
    483 
    484         for (const auto& stats : pair.second->alert_stats) {
    485             fprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
    486         }
    487     }
    488     fprintf(out, "********Disk Usage stats***********\n");
    489     StorageManager::printStats(out);
    490     fprintf(out, "********Pushed Atom stats***********\n");
    491     const size_t atomCounts = mPushedAtomStats.size();
    492     for (size_t i = 2; i < atomCounts; i++) {
    493         if (mPushedAtomStats[i] > 0) {
    494             fprintf(out, "Atom %lu->%d\n", (unsigned long)i, mPushedAtomStats[i]);
    495         }
    496     }
    497 
    498     fprintf(out, "********Pulled Atom stats***********\n");
    499     for (const auto& pair : mPulledAtomStats) {
    500         fprintf(out, "Atom %d->%ld, %ld, %ld\n", (int)pair.first, (long)pair.second.totalPull,
    501                 (long)pair.second.totalPullFromCache, (long)pair.second.minPullIntervalSec);
    502     }
    503 
    504     if (mAnomalyAlarmRegisteredStats > 0) {
    505         fprintf(out, "********AnomalyAlarmStats stats***********\n");
    506         fprintf(out, "Anomaly alarm registrations: %d\n", mAnomalyAlarmRegisteredStats);
    507     }
    508 
    509     if (mPeriodicAlarmRegisteredStats > 0) {
    510         fprintf(out, "********SubscriberAlarmStats stats***********\n");
    511         fprintf(out, "Subscriber alarm registrations: %d\n", mPeriodicAlarmRegisteredStats);
    512     }
    513 
    514     fprintf(out, "UID map stats: bytes=%d, changes=%d, deleted=%d, changes lost=%d\n",
    515             mUidMapStats.bytes_used, mUidMapStats.changes, mUidMapStats.deleted_apps,
    516             mUidMapStats.dropped_changes);
    517 
    518     for (const auto& error : mLoggerErrors) {
    519         time_t error_time = error.first;
    520         struct tm* error_tm = localtime(&error_time);
    521         char buffer[80];
    522         strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M%p\n", error_tm);
    523         fprintf(out, "Logger error %d at %s\n", error.second, buffer);
    524     }
    525 
    526     for (const auto& restart : mSystemServerRestartSec) {
    527         fprintf(out, "System server restarts at %s(%lld)\n",
    528             buildTimeString(restart).c_str(), (long long)restart);
    529     }
    530 
    531     for (const auto& loss : mLogLossTimestampNs) {
    532         fprintf(out, "Log loss detected at %lld (elapsedRealtimeNs)\n", (long long)loss);
    533     }
    534 }
    535 
    536 void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
    537     uint64_t token =
    538             proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
    539     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
    540     proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
    541     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
    542     if (configStats.reset_time_sec != 0) {
    543         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_RESET, configStats.reset_time_sec);
    544     }
    545     if (configStats.deletion_time_sec != 0) {
    546         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
    547                      configStats.deletion_time_sec);
    548     }
    549     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
    550     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
    551                  configStats.condition_count);
    552     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
    553     proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
    554     proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
    555 
    556     for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
    557         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
    558                      broadcast);
    559     }
    560 
    561     for (const auto& drop : configStats.data_drop_time_sec) {
    562         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP | FIELD_COUNT_REPEATED,
    563                      drop);
    564     }
    565 
    566     for (const auto& dump : configStats.dump_report_stats) {
    567         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_TIME |
    568                      FIELD_COUNT_REPEATED,
    569                      dump.first);
    570     }
    571 
    572     for (const auto& dump : configStats.dump_report_stats) {
    573         proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_DUMP_REPORT_BYTES |
    574                      FIELD_COUNT_REPEATED,
    575                      (long long)dump.second);
    576     }
    577 
    578     for (const auto& annotation : configStats.annotations) {
    579         uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
    580                                       FIELD_ID_CONFIG_STATS_ANNOTATION);
    581         proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT64,
    582                      (long long)annotation.first);
    583         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ANNOTATION_INT32, annotation.second);
    584         proto->end(token);
    585     }
    586 
    587     for (const auto& pair : configStats.matcher_stats) {
    588         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
    589                                           FIELD_ID_CONFIG_STATS_MATCHER_STATS);
    590         proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
    591         proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
    592         proto->end(tmpToken);
    593     }
    594 
    595     for (const auto& pair : configStats.condition_stats) {
    596         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
    597                                           FIELD_ID_CONFIG_STATS_CONDITION_STATS);
    598         proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
    599         proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
    600         proto->end(tmpToken);
    601     }
    602 
    603     for (const auto& pair : configStats.metric_stats) {
    604         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
    605                                           FIELD_ID_CONFIG_STATS_METRIC_STATS);
    606         proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
    607         proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
    608         proto->end(tmpToken);
    609     }
    610     for (const auto& pair : configStats.metric_dimension_in_condition_stats) {
    611         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
    612                                          FIELD_ID_CONFIG_STATS_METRIC_DIMENSION_IN_CONDITION_STATS);
    613         proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
    614         proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
    615         proto->end(tmpToken);
    616     }
    617 
    618     for (const auto& pair : configStats.alert_stats) {
    619         uint64_t tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
    620                                           FIELD_ID_CONFIG_STATS_ALERT_STATS);
    621         proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
    622         proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
    623         proto->end(tmpToken);
    624     }
    625 
    626     proto->end(token);
    627 }
    628 
    629 void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
    630     lock_guard<std::mutex> lock(mLock);
    631 
    632     ProtoOutputStream proto;
    633     proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
    634     proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
    635 
    636     for (const auto& configStats : mIceBox) {
    637         addConfigStatsToProto(*configStats, &proto);
    638     }
    639 
    640     for (auto& pair : mConfigStats) {
    641         addConfigStatsToProto(*(pair.second), &proto);
    642     }
    643 
    644     const size_t atomCounts = mPushedAtomStats.size();
    645     for (size_t i = 2; i < atomCounts; i++) {
    646         if (mPushedAtomStats[i] > 0) {
    647             uint64_t token =
    648                     proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM_STATS | FIELD_COUNT_REPEATED);
    649             proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_TAG, (int32_t)i);
    650             proto.write(FIELD_TYPE_INT32 | FIELD_ID_ATOM_STATS_COUNT, mPushedAtomStats[i]);
    651             proto.end(token);
    652         }
    653     }
    654 
    655     for (const auto& pair : mPulledAtomStats) {
    656         android::os::statsd::writePullerStatsToStream(pair, &proto);
    657     }
    658 
    659     if (mAnomalyAlarmRegisteredStats > 0) {
    660         uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_ANOMALY_ALARM_STATS);
    661         proto.write(FIELD_TYPE_INT32 | FIELD_ID_ANOMALY_ALARMS_REGISTERED,
    662                     mAnomalyAlarmRegisteredStats);
    663         proto.end(token);
    664     }
    665 
    666     if (mPeriodicAlarmRegisteredStats > 0) {
    667         uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_PERIODIC_ALARM_STATS);
    668         proto.write(FIELD_TYPE_INT32 | FIELD_ID_PERIODIC_ALARMS_REGISTERED,
    669                     mPeriodicAlarmRegisteredStats);
    670         proto.end(token);
    671     }
    672 
    673     uint64_t uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
    674     proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
    675     proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
    676     proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
    677     proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DELETED_APPS, mUidMapStats.deleted_apps);
    678     proto.end(uidMapToken);
    679 
    680     for (const auto& error : mLoggerErrors) {
    681         uint64_t token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
    682                                       FIELD_COUNT_REPEATED);
    683         proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_TIME, error.first);
    684         proto.write(FIELD_TYPE_INT32 | FIELD_ID_LOGGER_STATS_ERROR_CODE, error.second);
    685         proto.end(token);
    686     }
    687 
    688     for (const auto& loss : mLogLossTimestampNs) {
    689         proto.write(FIELD_TYPE_INT64 | FIELD_ID_LOG_LOSS_STATS | FIELD_COUNT_REPEATED,
    690                     (long long)loss);
    691     }
    692 
    693     for (const auto& restart : mSystemServerRestartSec) {
    694         proto.write(FIELD_TYPE_INT32 | FIELD_ID_SYSTEM_SERVER_RESTART | FIELD_COUNT_REPEATED,
    695                     restart);
    696     }
    697 
    698     output->clear();
    699     size_t bufferSize = proto.size();
    700     output->resize(bufferSize);
    701 
    702     size_t pos = 0;
    703     auto it = proto.data();
    704     while (it.readBuffer() != NULL) {
    705         size_t toRead = it.currentToRead();
    706         std::memcpy(&((*output)[pos]), it.readBuffer(), toRead);
    707         pos += toRead;
    708         it.rp()->move(toRead);
    709     }
    710 
    711     if (reset) {
    712         resetInternalLocked();
    713     }
    714 
    715     VLOG("reset=%d, returned proto size %lu", reset, (unsigned long)bufferSize);
    716 }
    717 
    718 }  // namespace statsd
    719 }  // namespace os
    720 }  // namespace android
    721