[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
diff --git a/services/gpuservice/gpustats/GpuStats.cpp b/services/gpuservice/gpustats/GpuStats.cpp
index d094532..263bf61 100644
--- a/services/gpuservice/gpustats/GpuStats.cpp
+++ b/services/gpuservice/gpustats/GpuStats.cpp
@@ -30,16 +30,11 @@
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() {
- AStatsManager_unregisterPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO);
- AStatsManager_unregisterPullAtomCallback(android::util::GPU_STATS_APP_INFO);
+ if (mStatsdRegistered) {
+ AStatsManager_unregisterPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO);
+ AStatsManager_unregisterPullAtomCallback(android::util::GPU_STATS_APP_INFO);
+ }
}
static void addLoadingCount(GpuStatsInfo::Driver driver, bool isDriverLoaded,
@@ -97,6 +92,7 @@
ATRACE_CALL();
std::lock_guard<std::mutex> lock(mLock);
+ registerStatsdCallbacksIfNeeded();
ALOGV("Received:\n"
"\tdriverPackageName[%s]\n"
"\tdriverVersionName[%s]\n"
@@ -150,6 +146,7 @@
const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
std::lock_guard<std::mutex> lock(mLock);
+ registerStatsdCallbacksIfNeeded();
if (!mAppStats.count(appStatsKey)) {
return;
}
@@ -179,6 +176,16 @@
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) {
ATRACE_CALL();
diff --git a/services/gpuservice/gpustats/include/gpustats/GpuStats.h b/services/gpuservice/gpustats/include/gpustats/GpuStats.h
index 8ca4e4e..55f0da1 100644
--- a/services/gpuservice/gpustats/include/gpustats/GpuStats.h
+++ b/services/gpuservice/gpustats/include/gpustats/GpuStats.h
@@ -30,7 +30,6 @@
class GpuStats {
public:
- GpuStats();
~GpuStats();
// Insert new gpu driver stats into global stats and app stats.
@@ -66,12 +65,16 @@
void dumpAppLocked(std::string* result);
// Append cpuVulkanVersion and glesVersion to system driver stats
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
// the preferred number for statsd while maintaining nice data quality.
static const size_t MAX_NUM_APP_RECORDS = 100;
// GpuStats access should be guarded by mLock.
std::mutex mLock;
+ // True if statsd callbacks have been registered.
+ bool mStatsdRegistered = false;
// Key is driver version code.
std::unordered_map<uint64_t, GpuStatsGlobalInfo> mGlobalStats;
// Key is <app package name>+<driver version code>.