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

Commit de701691 authored by David Chen's avatar David Chen
Browse files

UID mapping to provide app name and version.

The UID map is updated by StatsCompanionService, which listens to broadcast
updates indicating that an app was updated/installed or removed. Also,
the entire map is updated when statsd first connects to the companion
service. Also, there is a way for metrics producers to subscribe to
updates, so that they can know when an app was upgraded.

Test: Created new unit-test for mapping and manually tested for install
and remove. Did not manually test the app upgrade.

Change-Id: I6676ae5c93b75c72d9badabb36aa9c40006db07d
parent 32f38bde
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -124,6 +124,7 @@ LOCAL_SRC_FILES := \
    src/metrics/CountMetricProducer.cpp \
    src/metrics/CountMetricProducer.cpp \
    src/metrics/CountAnomalyTracker.cpp \
    src/metrics/CountAnomalyTracker.cpp \
    src/condition/condition_util.cpp \
    src/condition/condition_util.cpp \
    src/UidMap.cpp \
    $(call all-cpp-files-under, tests) \
    $(call all-cpp-files-under, tests) \


LOCAL_STATIC_LIBRARIES := \
LOCAL_STATIC_LIBRARIES := \
+38 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef STATSD_PACKAGE_INFO_LISTENER_H
#define STATSD_PACKAGE_INFO_LISTENER_H

#include <utils/RefBase.h>
#include <string>

namespace android {
namespace os {
namespace statsd {

class PackageInfoListener : public virtual android::RefBase {
public:
    // Uid map will notify this listener that the app with apk name and uid has been upgraded to
    // the specified version.
    virtual void notifyAppUpgrade(const std::string& apk, const int uid, const int version) = 0;
};

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

#endif //STATSD_PACKAGE_INFO_LISTENER_H
+3 −1
Original line number Original line Diff line number Diff line
@@ -31,7 +31,9 @@ namespace android {
namespace os {
namespace os {
namespace statsd {
namespace statsd {


StatsLogProcessor::StatsLogProcessor() : m_dropbox_writer("all-logs") {
StatsLogProcessor::StatsLogProcessor(const sp<UidMap> &uidMap)
        : m_dropbox_writer("all-logs"), m_UidMap(uidMap)
{
    // hardcoded config
    // hardcoded config
    // this should be called from StatsService when it receives a statsd_config
    // this should be called from StatsService when it receives a statsd_config
    UpdateConfig(0, buildFakeConfig());
    UpdateConfig(0, buildFakeConfig());
+4 −1
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
#include "metrics/MetricsManager.h"
#include "metrics/MetricsManager.h"
#include "stats_util.h"
#include "stats_util.h"
#include "UidMap.h"


#include <log/logprint.h>
#include <log/logprint.h>
#include <stdio.h>
#include <stdio.h>
@@ -32,7 +33,7 @@ namespace statsd {


class StatsLogProcessor : public LogListener {
class StatsLogProcessor : public LogListener {
public:
public:
    StatsLogProcessor();
    StatsLogProcessor(const sp<UidMap> &uidMap);
    virtual ~StatsLogProcessor();
    virtual ~StatsLogProcessor();


    virtual void OnLogEvent(const log_msg& msg);
    virtual void OnLogEvent(const log_msg& msg);
@@ -44,6 +45,8 @@ private:
    DropboxWriter m_dropbox_writer;
    DropboxWriter m_dropbox_writer;


    std::unordered_map<int, std::unique_ptr<MetricsManager>> mMetricsManagers;
    std::unordered_map<int, std::unique_ptr<MetricsManager>> mMetricsManagers;

    sp<UidMap> m_UidMap; // Reference to the UidMap to lookup app name and version for each uid.
};
};


}  // namespace statsd
}  // namespace statsd
+48 −1
Original line number Original line Diff line number Diff line
@@ -40,7 +40,8 @@ namespace os {
namespace statsd {
namespace statsd {


StatsService::StatsService(const sp<Looper>& handlerLooper)
StatsService::StatsService(const sp<Looper>& handlerLooper)
    : mAnomalyMonitor(new AnomalyMonitor(2)), mStatsPullerManager()  // TODO: Change this based on the config
    :   mAnomalyMonitor(new AnomalyMonitor(2)),m_UidMap(new UidMap()), mStatsPullerManager()
    // TODO: Change AnomalyMonitor initialization based on the config
{
{
    ALOGD("stats service constructed");
    ALOGD("stats service constructed");
}
}
@@ -131,6 +132,9 @@ status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>&
        if (!args[0].compare(String8("config"))) {
        if (!args[0].compare(String8("config"))) {
            return doLoadConfig(in);
            return doLoadConfig(in);
        }
        }
        if (!args[0].compare(String8("print-uid-map"))) {
            return doPrintUidMap(out);
        }
    }
    }


    printCmdHelp(out);
    printCmdHelp(out);
@@ -153,6 +157,43 @@ status_t StatsService::doLoadConfig(FILE* in) {
    }
    }
}
}


Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
                                      const vector<String16>& app) {
    if (DEBUG) ALOGD("StatsService::informAllUidData was called");

    if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
        return Status::fromExceptionCode(Status::EX_SECURITY,
                                         "Only system uid can call informAllUidData");
    }

    m_UidMap->updateMap(uid, version, app);
    if (DEBUG) ALOGD("StatsService::informAllUidData succeeded");

    return Status::ok();
}

Status StatsService::informOnePackage(const String16& app, int32_t uid, int32_t version) {
    if (DEBUG) ALOGD("StatsService::informOnePackage was called");

    if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
        return Status::fromExceptionCode(Status::EX_SECURITY,
                                         "Only system uid can call informOnePackage");
    }
    m_UidMap->updateApp(app, uid, version);
    return Status::ok();
}

Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
    if (DEBUG) ALOGD("StatsService::informOnePackageRemoved was called");

    if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
        return Status::fromExceptionCode(Status::EX_SECURITY,
                                         "Only system uid can call informOnePackageRemoved");
    }
    m_UidMap->removeApp(app, uid);
    return Status::ok();
}

Status StatsService::informAnomalyAlarmFired() {
Status StatsService::informAnomalyAlarmFired() {
    if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called");
    if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called");


@@ -261,9 +302,15 @@ status_t StatsService::doPrintStatsLog(FILE* out, const Vector<String8>& args) {
    return DropboxReader::readStatsLogs(out, args[1].string(), msec);
    return DropboxReader::readStatsLogs(out, args[1].string(), msec);
}
}


status_t StatsService::doPrintUidMap(FILE* out) {
    m_UidMap->printUidMap(out);
    return NO_ERROR;
}

void StatsService::printCmdHelp(FILE* out) {
void StatsService::printCmdHelp(FILE* out) {
    fprintf(out, "Usage:\n");
    fprintf(out, "Usage:\n");
    fprintf(out, "\t print-stats-log [tag_required] [timestamp_nsec_optional]\n");
    fprintf(out, "\t print-stats-log [tag_required] [timestamp_nsec_optional]\n");
    fprintf(out, "\t print-uid-map Prints the UID, app name, version mapping.\n");
    fprintf(out,
    fprintf(out,
            "\t config\t Loads a new config from command-line (must be proto in wire-encoded "
            "\t config\t Loads a new config from command-line (must be proto in wire-encoded "
            "format).\n");
            "format).\n");
Loading