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

Commit b62e20c7 authored by Chenjie Yu's avatar Chenjie Yu Committed by android-build-merger
Browse files

Merge "add dump report reason to reports" into pi-dev am: a194a6be

am: e0f7fc0a

Change-Id: I4b97af127a219ded41891d90d44dd5940b7dd0af
parents 2a466b1a e0f7fc0a
Loading
Loading
Loading
Loading
+17 −10
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ const int FIELD_ID_LAST_REPORT_ELAPSED_NANOS = 3;
const int FIELD_ID_CURRENT_REPORT_ELAPSED_NANOS = 4;
const int FIELD_ID_LAST_REPORT_WALL_CLOCK_NANOS = 5;
const int FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS = 6;
const int FIELD_ID_DUMP_REPORT_REASON = 8;

#define NS_PER_HOUR 3600 * NS_PER_SEC

@@ -183,7 +184,7 @@ void StatsLogProcessor::OnLogEvent(LogEvent* event, bool reconnected) {
            mInReconnection = false;
            StatsdStats::getInstance().noteLogLost(currentTimestampNs);
            // Persist the data before we reset. Do we want this?
            WriteDataToDiskLocked();
            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.
            std::vector<ConfigKey> configKeys;
@@ -251,7 +252,7 @@ void StatsLogProcessor::OnConfigUpdatedLocked(
                           mAnomalyAlarmMonitor, mPeriodicAlarmMonitor);
    auto it = mMetricsManagers.find(key);
    if (it != mMetricsManagers.end()) {
        WriteDataToDiskLocked(it->first);
        WriteDataToDiskLocked(it->first, CONFIG_UPDATED);
    }
    if (newMetricsManager->isConfigValid()) {
        mUidMap->OnConfigUpdated(key);
@@ -292,6 +293,7 @@ void StatsLogProcessor::dumpStates(FILE* out, bool verbose) {
 */
void StatsLogProcessor::onDumpReport(const ConfigKey& key, const int64_t dumpTimeStampNs,
                                     const bool include_current_partial_bucket,
                                     const DumpReportReason dumpReportReason,
                                     vector<uint8_t>* outData) {
    std::lock_guard<std::mutex> lock(mMetricsMutex);

@@ -317,7 +319,8 @@ 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);
        onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket, &proto);
        onConfigMetricsReportLocked(key, dumpTimeStampNs, include_current_partial_bucket,
                                    dumpReportReason, &proto);
        proto.end(reportsToken);
        // End of ConfigMetricsReport (reports).
    } else {
@@ -346,6 +349,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 DumpReportReason dumpReportReason,
                                                    ProtoOutputStream* proto) {
    // We already checked whether key exists in mMetricsManagers in
    // WriteDataToDisk.
@@ -374,6 +378,8 @@ void StatsLogProcessor::onConfigMetricsReportLocked(const ConfigKey& key,
                (long long)lastReportWallClockNs);
    proto->write(FIELD_TYPE_INT64 | FIELD_ID_CURRENT_REPORT_WALL_CLOCK_NANOS,
                (long long)getWallClockNs());
    // Dump report reason
    proto->write(FIELD_TYPE_INT32 | FIELD_ID_DUMP_REPORT_REASON, dumpReportReason);
}

void StatsLogProcessor::resetConfigsLocked(const int64_t timestampNs,
@@ -409,7 +415,7 @@ void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
    std::lock_guard<std::mutex> lock(mMetricsMutex);
    auto it = mMetricsManagers.find(key);
    if (it != mMetricsManagers.end()) {
        WriteDataToDiskLocked(key);
        WriteDataToDiskLocked(key, CONFIG_REMOVED);
        mMetricsManagers.erase(it);
        mUidMap->OnConfigRemoved(key);
    }
@@ -455,10 +461,11 @@ void StatsLogProcessor::flushIfNecessaryLocked(
    }
}

void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key) {
void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key,
                                              const DumpReportReason dumpReportReason) {
    ProtoOutputStream proto;
    onConfigMetricsReportLocked(key, getElapsedRealtimeNs(),
                                true /* include_current_partial_bucket*/, &proto);
                                true /* include_current_partial_bucket*/, 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(),
@@ -470,15 +477,15 @@ void StatsLogProcessor::WriteDataToDiskLocked(const ConfigKey& key) {
    proto.flush(fd.get());
}

void StatsLogProcessor::WriteDataToDiskLocked() {
void StatsLogProcessor::WriteDataToDiskLocked(const DumpReportReason dumpReportReason) {
    for (auto& pair : mMetricsManagers) {
        WriteDataToDiskLocked(pair.first);
        WriteDataToDiskLocked(pair.first, dumpReportReason);
    }
}

void StatsLogProcessor::WriteDataToDisk() {
void StatsLogProcessor::WriteDataToDisk(bool isShutdown) {
    std::lock_guard<std::mutex> lock(mMetricsMutex);
    WriteDataToDiskLocked();
    WriteDataToDiskLocked(DEVICE_SHUTDOWN);
}

void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) {
+17 −4
Original line number Diff line number Diff line
@@ -32,6 +32,17 @@ namespace android {
namespace os {
namespace statsd {

// Keep this in sync with DumpReportReason enum in stats_log.proto
enum DumpReportReason {
    DEVICE_SHUTDOWN = 1,
    CONFIG_UPDATED = 2,
    CONFIG_REMOVED = 3,
    GET_DATA_CALLED = 4,
    ADB_DUMP = 5,
    CONFIG_RESET = 6,
    STATSCOMPANION_DIED = 7
};

class StatsLogProcessor : public ConfigListener {
public:
    StatsLogProcessor(const sp<UidMap>& uidMap, const sp<AlarmMonitor>& anomalyAlarmMonitor,
@@ -52,7 +63,8 @@ public:
    size_t GetMetricsSize(const ConfigKey& key) const;

    void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
                      const bool include_current_partial_bucket, vector<uint8_t>* outData);
                      const bool include_current_partial_bucket,
                      const DumpReportReason dumpReportReason, vector<uint8_t>* outData);

    /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
    void onAnomalyAlarmFired(
@@ -65,7 +77,7 @@ public:
            unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);

    /* Flushes data to disk. Data on memory will be gone after written to disk. */
    void WriteDataToDisk();
    void WriteDataToDisk(bool shutdown);

    inline sp<UidMap> getUidMap() {
        return mUidMap;
@@ -109,11 +121,12 @@ private:
    void OnConfigUpdatedLocked(
        const int64_t currentTimestampNs, const ConfigKey& key, const StatsdConfig& config);

    void WriteDataToDiskLocked();
    void WriteDataToDiskLocked(const ConfigKey& key);
    void WriteDataToDiskLocked(DumpReportReason dumpReportReason);
    void WriteDataToDiskLocked(const ConfigKey& key, DumpReportReason dumpReportReason);

    void onConfigMetricsReportLocked(const ConfigKey& key, const int64_t dumpTimeStampNs,
                                     const bool include_current_partial_bucket,
                                     const DumpReportReason dumpReportReason,
                                     util::ProtoOutputStream* proto);

    /* Check if we should send a broadcast if approaching memory limits and if we're over, we
+8 −9
Original line number Diff line number Diff line
@@ -592,7 +592,7 @@ status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String
        if (good) {
            vector<uint8_t> data;
            mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(),
                                     false /* include_current_bucket*/, &data);
                                     false /* include_current_bucket*/, ADB_DUMP, &data);
            // TODO: print the returned StatsLogReport to file instead of printing to logcat.
            if (proto) {
                for (size_t i = 0; i < data.size(); i ++) {
@@ -658,7 +658,7 @@ status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args)

status_t StatsService::cmd_write_data_to_disk(FILE* out) {
    fprintf(out, "Writing data to disk\n");
    mProcessor->WriteDataToDisk();
    mProcessor->WriteDataToDisk(false);
    return NO_ERROR;
}

@@ -815,11 +815,10 @@ Status StatsService::systemRunning() {
    return Status::ok();
}

Status StatsService::writeDataToDisk() {
Status StatsService::informDeviceShutdown(bool isShutdown) {
    ENFORCE_UID(AID_SYSTEM);

    VLOG("StatsService::writeDataToDisk");
    mProcessor->WriteDataToDisk();
    VLOG("StatsService::informDeviceShutdown");
    mProcessor->WriteDataToDisk(isShutdown);
    return Status::ok();
}

@@ -866,8 +865,8 @@ Status StatsService::getData(int64_t key, const String16& packageName, vector<ui
    IPCThreadState* ipc = IPCThreadState::self();
    VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
    ConfigKey configKey(ipc->getCallingUid(), key);
    mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(),
                             false /* include_current_bucket*/, output);
    mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), false /* include_current_bucket*/,
                             GET_DATA_CALLED, output);
    return Status::ok();
}

@@ -966,7 +965,7 @@ Status StatsService::unsetBroadcastSubscriber(int64_t configId,

void StatsService::binderDied(const wp <IBinder>& who) {
    ALOGW("statscompanion service died");
    mProcessor->WriteDataToDisk();
    mProcessor->WriteDataToDisk(STATSCOMPANION_DIED);
    mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
    mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
    SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
+1 −1
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ public:
                                    const vector<String16>& app);
    virtual Status informOnePackage(const String16& app, int32_t uid, int64_t version);
    virtual Status informOnePackageRemoved(const String16& app, int32_t uid);
    virtual Status writeDataToDisk();
    virtual Status informDeviceShutdown(bool isShutdown);

    /**
     * Called right before we start processing events.
+6 −5
Original line number Diff line number Diff line
@@ -221,7 +221,7 @@ void UidMap::removeApp(const int64_t& timestamp, const String16& app_16, const i
    {
        lock_guard<mutex> lock(mMutex);

        int32_t prevVersion = 0;
        int64_t prevVersion = 0;
        auto key = std::make_pair(uid, app);
        auto it = mMap.find(key);
        if (it != mMap.end() && !it->second.deleted) {
@@ -324,8 +324,9 @@ void UidMap::appendUidMap(const int64_t& timestamp, const ConfigKey& key,
                         (long long)record.timestampNs);
            proto->write(FIELD_TYPE_STRING | FIELD_ID_CHANGE_PACKAGE, record.package);
            proto->write(FIELD_TYPE_INT32 | FIELD_ID_CHANGE_UID, (int)record.uid);
            proto->write(FIELD_TYPE_INT32 | FIELD_ID_CHANGE_NEW_VERSION, (int)record.version);
            proto->write(FIELD_TYPE_INT32 | FIELD_ID_CHANGE_PREV_VERSION, (int)record.prevVersion);
            proto->write(FIELD_TYPE_INT64 | FIELD_ID_CHANGE_NEW_VERSION, (long long)record.version);
            proto->write(FIELD_TYPE_INT64 | FIELD_ID_CHANGE_PREV_VERSION,
                         (long long)record.prevVersion);
            proto->end(changesToken);
        }
    }
@@ -338,8 +339,8 @@ void UidMap::appendUidMap(const int64_t& timestamp, const ConfigKey& key,
        uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
                                      FIELD_ID_SNAPSHOT_PACKAGE_INFO);
        proto->write(FIELD_TYPE_STRING | FIELD_ID_SNAPSHOT_PACKAGE_NAME, kv.first.second);
        proto->write(FIELD_TYPE_INT32 | FIELD_ID_SNAPSHOT_PACKAGE_VERSION,
                     (int)kv.second.versionCode);
        proto->write(FIELD_TYPE_INT64 | FIELD_ID_SNAPSHOT_PACKAGE_VERSION,
                     (long long)kv.second.versionCode);
        proto->write(FIELD_TYPE_INT32 | FIELD_ID_SNAPSHOT_PACKAGE_UID, kv.first.first);
        proto->write(FIELD_TYPE_BOOL | FIELD_ID_SNAPSHOT_PACKAGE_DELETED, kv.second.deleted);
        proto->end(token);
Loading