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

Commit 822b10ae authored by Alec Mouri's avatar Alec Mouri
Browse files

[GpuStats] Lazily-register statsd callbacks

Previously statsd callbacks were registered when GpuStats was
instantiated. But, statsd is enforcing permissions to verify that the
calling process had permission to register callbacks. The permission
enforcement is happening too early so the callback registration silently
fails. As a workaround, perform callback registration the first time
GpuStats is asked to track stats instead.

Bug: 151682677
Test: builds, boots
Test: adb logcat | grep REGISTER_STATS
Test: adb shell cmd stats pull-source 10054
Test: adb shell cmd stats pull-source 10055
Change-Id: I2d4a4e320825ee5cb393a6560baa8756779b729f
parent 89290018
Loading
Loading
Loading
Loading
+16 −9
Original line number Original line Diff line number Diff line
@@ -30,17 +30,12 @@


namespace android {
namespace android {


GpuStats::GpuStats() {
    AStatsManager_registerPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO,
                                           GpuStats::pullAtomCallback, nullptr, this);
    AStatsManager_registerPullAtomCallback(android::util::GPU_STATS_APP_INFO,
                                           GpuStats::pullAtomCallback, nullptr, this);
}

GpuStats::~GpuStats() {
GpuStats::~GpuStats() {
    if (mStatsdRegistered) {
        AStatsManager_unregisterPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO);
        AStatsManager_unregisterPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO);
        AStatsManager_unregisterPullAtomCallback(android::util::GPU_STATS_APP_INFO);
        AStatsManager_unregisterPullAtomCallback(android::util::GPU_STATS_APP_INFO);
    }
    }
}


static void addLoadingCount(GpuStatsInfo::Driver driver, bool isDriverLoaded,
static void addLoadingCount(GpuStatsInfo::Driver driver, bool isDriverLoaded,
                            GpuStatsGlobalInfo* const outGlobalInfo) {
                            GpuStatsGlobalInfo* const outGlobalInfo) {
@@ -97,6 +92,7 @@ void GpuStats::insertDriverStats(const std::string& driverPackageName,
    ATRACE_CALL();
    ATRACE_CALL();


    std::lock_guard<std::mutex> lock(mLock);
    std::lock_guard<std::mutex> lock(mLock);
    registerStatsdCallbacksIfNeeded();
    ALOGV("Received:\n"
    ALOGV("Received:\n"
          "\tdriverPackageName[%s]\n"
          "\tdriverPackageName[%s]\n"
          "\tdriverVersionName[%s]\n"
          "\tdriverVersionName[%s]\n"
@@ -150,6 +146,7 @@ void GpuStats::insertTargetStats(const std::string& appPackageName,
    const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
    const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);


    std::lock_guard<std::mutex> lock(mLock);
    std::lock_guard<std::mutex> lock(mLock);
    registerStatsdCallbacksIfNeeded();
    if (!mAppStats.count(appStatsKey)) {
    if (!mAppStats.count(appStatsKey)) {
        return;
        return;
    }
    }
@@ -179,6 +176,16 @@ void GpuStats::interceptSystemDriverStatsLocked() {
    mGlobalStats[0].glesVersion = property_get_int32("ro.opengles.version", 0);
    mGlobalStats[0].glesVersion = property_get_int32("ro.opengles.version", 0);
}
}


void GpuStats::registerStatsdCallbacksIfNeeded() {
    if (!mStatsdRegistered) {
        AStatsManager_registerPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO,
                                               GpuStats::pullAtomCallback, nullptr, this);
        AStatsManager_registerPullAtomCallback(android::util::GPU_STATS_APP_INFO,
                                               GpuStats::pullAtomCallback, nullptr, this);
        mStatsdRegistered = true;
    }
}

void GpuStats::dump(const Vector<String16>& args, std::string* result) {
void GpuStats::dump(const Vector<String16>& args, std::string* result) {
    ATRACE_CALL();
    ATRACE_CALL();


+4 −1
Original line number Original line Diff line number Diff line
@@ -30,7 +30,6 @@ namespace android {


class GpuStats {
class GpuStats {
public:
public:
    GpuStats();
    ~GpuStats();
    ~GpuStats();


    // Insert new gpu driver stats into global stats and app stats.
    // Insert new gpu driver stats into global stats and app stats.
@@ -66,12 +65,16 @@ private:
    void dumpAppLocked(std::string* result);
    void dumpAppLocked(std::string* result);
    // Append cpuVulkanVersion and glesVersion to system driver stats
    // Append cpuVulkanVersion and glesVersion to system driver stats
    void interceptSystemDriverStatsLocked();
    void interceptSystemDriverStatsLocked();
    // Registers statsd callbacks if they have not already been registered
    void registerStatsdCallbacksIfNeeded();


    // Below limits the memory usage of GpuStats to be less than 10KB. This is
    // Below limits the memory usage of GpuStats to be less than 10KB. This is
    // the preferred number for statsd while maintaining nice data quality.
    // the preferred number for statsd while maintaining nice data quality.
    static const size_t MAX_NUM_APP_RECORDS = 100;
    static const size_t MAX_NUM_APP_RECORDS = 100;
    // GpuStats access should be guarded by mLock.
    // GpuStats access should be guarded by mLock.
    std::mutex mLock;
    std::mutex mLock;
    // True if statsd callbacks have been registered.
    bool mStatsdRegistered = false;
    // Key is driver version code.
    // Key is driver version code.
    std::unordered_map<uint64_t, GpuStatsGlobalInfo> mGlobalStats;
    std::unordered_map<uint64_t, GpuStatsGlobalInfo> mGlobalStats;
    // Key is <app package name>+<driver version code>.
    // Key is <app package name>+<driver version code>.