From 1a8d198b5b0c1281251e7ae1f5c2e3f292cd6dc9 Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Mon, 28 Dec 2020 09:40:06 -0500 Subject: [PATCH] Provide an interface to query dmabuf GPU allocations The DMA buffers mapped by GPU device(s) are already accounted for in the total exported dmabuf size (ionHeap) size. To remove GPU memory from lost RAM, provide an API to get only the DMA buf memory mapped by the GPU device(s). Bug: 176477627 Test: adb shell dumpsys meminfo Change-Id: Ie72f46c8de026b143629406714f413677ce6693b Merged-In: Ie72f46c8de026b143629406714f413677ce6693b --- core/java/android/os/Debug.java | 7 ++++ core/jni/Android.bp | 1 + core/jni/android_os_Debug.cpp | 29 ++++++++++++++++ .../server/am/ActivityManagerService.java | 33 ++++++++++++++++--- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java index a2cf1aee3250d..ea282afb8b8ea 100644 --- a/core/java/android/os/Debug.java +++ b/core/java/android/os/Debug.java @@ -2583,6 +2583,13 @@ public final class Debug */ public static native long getIonPoolsSizeKb(); + /** + * Return GPU DMA buffer usage in kB or -1 on error. + * + * @hide + */ + public static native long getGpuDmaBufUsageKb(); + /** * Return DMA-BUF memory mapped by processes in kB. * Notes: diff --git a/core/jni/Android.bp b/core/jni/Android.bp index 79a0dfd61e9fb..088682c9f2d83 100644 --- a/core/jni/Android.bp +++ b/core/jni/Android.bp @@ -206,6 +206,7 @@ cc_library_shared { ], shared_libs: [ + "android.hardware.memtrack-unstable-ndk_platform", "libandroidicu", "libbpf_android", "libnetdbpf", diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp index c4caef2ecdb9d..223b4dcc05497 100644 --- a/core/jni/android_os_Debug.cpp +++ b/core/jni/android_os_Debug.cpp @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -45,6 +46,7 @@ #include "jni.h" #include #include +#include #include #include #include @@ -846,6 +848,31 @@ static jlong android_os_Debug_getDmabufHeapPoolsSizeKb(JNIEnv* env, jobject claz return poolsSizeKb; } +static jlong android_os_Debug_getGpuDmaBufUsageKb(JNIEnv* env, jobject clazz) { + std::vector gpu_device_info; + if (!memtrack_gpu_device_info(&gpu_device_info)) { + return -1; + } + + dmabufinfo::DmabufSysfsStats stats; + if (!GetDmabufSysfsStats(&stats)) { + return -1; + } + + jlong sizeKb = 0; + const auto& importer_stats = stats.importer_info(); + for (const auto& dev_info : gpu_device_info) { + const auto& importer_info = importer_stats.find(dev_info.name); + if (importer_info == importer_stats.end()) { + continue; + } + + sizeKb += importer_info->second.size; + } + + return sizeKb; +} + static jlong android_os_Debug_getDmabufMappedSizeKb(JNIEnv* env, jobject clazz) { jlong dmabufPss = 0; std::vector dmabufs; @@ -954,6 +981,8 @@ static const JNINativeMethod gMethods[] = { (void*)android_os_Debug_getIonHeapsSizeKb }, { "getDmabufTotalExportedKb", "()J", (void*)android_os_Debug_getDmabufTotalExportedKb }, + { "getGpuDmaBufUsageKb", "()J", + (void*)android_os_Debug_getGpuDmaBufUsageKb }, { "getIonPoolsSizeKb", "()J", (void*)android_os_Debug_getIonPoolsSizeKb }, { "getDmabufMappedSizeKb", "()J", diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index f5df87eefafd9..a31385d7f8f0d 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -13756,7 +13756,19 @@ public class ActivityManagerService extends IActivityManager.Stub } final long gpuUsage = Debug.getGpuTotalUsageKb(); if (gpuUsage >= 0) { - pw.print(" GPU: "); pw.println(stringifyKBSize(gpuUsage)); + final long gpuDmaBufUsage = Debug.getGpuDmaBufUsageKb(); + if (gpuDmaBufUsage >= 0) { + final long gpuPrivateUsage = gpuUsage - gpuDmaBufUsage; + pw.print(" GPU: "); + pw.print(stringifyKBSize(gpuUsage)); + pw.print(" ("); + pw.print(stringifyKBSize(gpuDmaBufUsage)); + pw.print(" dmabuf + "); + pw.print(stringifyKBSize(gpuPrivateUsage)); + pw.println(" private)"); + } else { + pw.print(" GPU: "); pw.println(stringifyKBSize(gpuUsage)); + } } /* @@ -14586,9 +14598,22 @@ public class ActivityManagerService extends IActivityManager.Stub final long gpuUsage = Debug.getGpuTotalUsageKb(); if (gpuUsage >= 0) { - memInfoBuilder.append(" GPU: "); - memInfoBuilder.append(stringifyKBSize(gpuUsage)); - memInfoBuilder.append("\n"); + final long gpuDmaBufUsage = Debug.getGpuDmaBufUsageKb(); + if (gpuDmaBufUsage >= 0) { + final long gpuPrivateUsage = gpuUsage - gpuDmaBufUsage; + memInfoBuilder.append(" GPU: "); + memInfoBuilder.append(stringifyKBSize(gpuUsage)); + memInfoBuilder.append(" ("); + memInfoBuilder.append(stringifyKBSize(gpuDmaBufUsage)); + memInfoBuilder.append(" dmabuf + "); + memInfoBuilder.append(stringifyKBSize(gpuPrivateUsage)); + memInfoBuilder.append(" private)\n"); + } else { + memInfoBuilder.append(" GPU: "); + memInfoBuilder.append(stringifyKBSize(gpuUsage)); + memInfoBuilder.append("\n"); + } + } memInfoBuilder.append(" Used RAM: "); memInfoBuilder.append(stringifyKBSize(