From 0b7c1774ba42daef7c80bf2f00fe1c0327e756ae Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Tue, 23 Nov 2021 11:42:09 -0800 Subject: [PATCH] Fix GL memory not being removed from lost RAM Devices upgrading to S are not required to update their memtrack HAL implementation and won't support reporting the total GPU private memory. In these cases it's likely the HAL reports 0 for the total GPU private memory, since in S the HAL defition only overloads the already existing getMemory() API to report this. Return an error value in these cases to prevent swapping the sum of GL type memory for all processes with the total GPU private memory when not supported as doing this effectively leaves the GL memory as part of lost RAM. Bug: 203245390 Test: dumpsys meminfo Change-Id: I478143e435b047bf349bd4ec6808707a5db1505b --- core/jni/android_os_Debug.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp index 4c2b114c724ae..5e0d9b32380c3 100644 --- a/core/jni/android_os_Debug.cpp +++ b/core/jni/android_os_Debug.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -859,7 +860,22 @@ static jlong android_os_Debug_getDmabufHeapPoolsSizeKb(JNIEnv* env, jobject claz return poolsSizeKb; } +static bool halSupportsGpuPrivateMemory() { + int productApiLevel = + android::base::GetIntProperty("ro.product.first_api_level", + android::base::GetIntProperty("ro.build.version.sdk", + __ANDROID_API_FUTURE__)); + int boardApiLevel = + android::base::GetIntProperty("ro.board.api_level", + android::base::GetIntProperty("ro.board.first_api_level", + __ANDROID_API_FUTURE__)); + + return std::min(productApiLevel, boardApiLevel) >= __ANDROID_API_S__; +} + static jlong android_os_Debug_getGpuPrivateMemoryKb(JNIEnv* env, jobject clazz) { + static bool gpuPrivateMemorySupported = halSupportsGpuPrivateMemory(); + struct memtrack_proc* p = memtrack_proc_new(); if (p == nullptr) { LOG(ERROR) << "getGpuPrivateMemoryKb: Failed to create memtrack_proc"; @@ -876,6 +892,12 @@ static jlong android_os_Debug_getGpuPrivateMemoryKb(JNIEnv* env, jobject clazz) ssize_t gpuPrivateMem = memtrack_proc_gl_pss(p); memtrack_proc_destroy(p); + + // Old HAL implementations may return 0 for GPU private memory if not supported + if (gpuPrivateMem == 0 && !gpuPrivateMemorySupported) { + return -1; + } + return gpuPrivateMem / 1024; }