From c6deb0fd9d00a3144becb22980a67085817a9c3f Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Tue, 13 Jul 2021 00:29:11 +0000 Subject: [PATCH] Add API to get GPU private memory Remove getGpuDmabufUsageKb since the kernel no longer supports the sysfs nodes that this depends on. Add getGpuPrivateMemoryKb which uses the memtrack hal to report the system-wide GPU private memory. Bug: 193226716 Bug: 193465681 Bug: 192621117 Test: dumpsys meminfo Change-Id: Ib90505ec184339d5acbcd42c67dad3ad65dbd933 --- core/java/android/os/Debug.java | 4 +-- core/jni/Android.bp | 1 - core/jni/android_os_Debug.cpp | 33 ++++++++----------- .../server/am/ActivityManagerService.java | 6 ++-- .../com/android/server/am/AppProfiler.java | 6 ++-- .../server/stats/pull/SystemMemoryUtil.java | 6 +--- 6 files changed, 22 insertions(+), 34 deletions(-) diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java index d90e129d36f7b..b4930fa931ebf 100644 --- a/core/java/android/os/Debug.java +++ b/core/java/android/os/Debug.java @@ -2599,11 +2599,11 @@ public final class Debug public static native long getIonPoolsSizeKb(); /** - * Return GPU DMA buffer usage in kB or -1 on error. + * Returns the global total GPU-private memory in kB or -1 on error. * * @hide */ - public static native long getGpuDmaBufUsageKb(); + public static native long getGpuPrivateMemoryKb(); /** * Return DMA-BUF memory mapped by processes in kB. diff --git a/core/jni/Android.bp b/core/jni/Android.bp index 125182cab2546..91a19e087bf17 100644 --- a/core/jni/Android.bp +++ b/core/jni/Android.bp @@ -237,7 +237,6 @@ cc_library_shared { ], shared_libs: [ - "android.hardware.memtrack-V1-ndk_platform", "audioclient-types-aidl-cpp", "audioflinger-aidl-cpp", "av-types-aidl-cpp", diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp index 41804480122c3..4c2b114c724ae 100644 --- a/core/jni/android_os_Debug.cpp +++ b/core/jni/android_os_Debug.cpp @@ -33,7 +33,6 @@ #include #include -#include #include #include #include @@ -46,7 +45,6 @@ #include "jni.h" #include #include -#include #include #include #include @@ -861,29 +859,24 @@ 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)) { +static jlong android_os_Debug_getGpuPrivateMemoryKb(JNIEnv* env, jobject clazz) { + struct memtrack_proc* p = memtrack_proc_new(); + if (p == nullptr) { + LOG(ERROR) << "getGpuPrivateMemoryKb: Failed to create memtrack_proc"; return -1; } - dmabufinfo::DmabufSysfsStats stats; - if (!GetDmabufSysfsStats(&stats)) { + // Memtrack hal defines PID 0 as global total for GPU-private (GL) memory. + if (memtrack_proc_get(p, 0) != 0) { + // The memtrack HAL may not be available, avoid flooding the log. + memtrack_proc_destroy(p); 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; - } + ssize_t gpuPrivateMem = memtrack_proc_gl_pss(p); - sizeKb += importer_info->second.size / 1024; - } - - return sizeKb; + memtrack_proc_destroy(p); + return gpuPrivateMem / 1024; } static jlong android_os_Debug_getDmabufMappedSizeKb(JNIEnv* env, jobject clazz) { @@ -994,8 +987,8 @@ static const JNINativeMethod gMethods[] = { (void*)android_os_Debug_getIonHeapsSizeKb }, { "getDmabufTotalExportedKb", "()J", (void*)android_os_Debug_getDmabufTotalExportedKb }, - { "getGpuDmaBufUsageKb", "()J", - (void*)android_os_Debug_getGpuDmaBufUsageKb }, + { "getGpuPrivateMemoryKb", "()J", + (void*)android_os_Debug_getGpuPrivateMemoryKb }, { "getDmabufHeapTotalExportedKb", "()J", (void*)android_os_Debug_getDmabufHeapTotalExportedKb }, { "getIonPoolsSizeKb", "()J", diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 4ec5559a061d3..fc2d4c6da9221 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -11048,9 +11048,9 @@ public class ActivityManagerService extends IActivityManager.Stub } final long gpuUsage = Debug.getGpuTotalUsageKb(); if (gpuUsage >= 0) { - final long gpuDmaBufUsage = Debug.getGpuDmaBufUsageKb(); - if (gpuDmaBufUsage >= 0) { - final long gpuPrivateUsage = gpuUsage - gpuDmaBufUsage; + final long gpuPrivateUsage = Debug.getGpuPrivateMemoryKb(); + if (gpuPrivateUsage >= 0) { + final long gpuDmaBufUsage = gpuUsage - gpuPrivateUsage; pw.print(" GPU: "); pw.print(stringifyKBSize(gpuUsage)); pw.print(" ("); diff --git a/services/core/java/com/android/server/am/AppProfiler.java b/services/core/java/com/android/server/am/AppProfiler.java index 74094e500de7a..36c0de9192793 100644 --- a/services/core/java/com/android/server/am/AppProfiler.java +++ b/services/core/java/com/android/server/am/AppProfiler.java @@ -1561,9 +1561,9 @@ public class AppProfiler { final long gpuUsage = Debug.getGpuTotalUsageKb(); if (gpuUsage >= 0) { - final long gpuDmaBufUsage = Debug.getGpuDmaBufUsageKb(); - if (gpuDmaBufUsage >= 0) { - final long gpuPrivateUsage = gpuUsage - gpuDmaBufUsage; + final long gpuPrivateUsage = Debug.getGpuPrivateMemoryKb(); + if (gpuPrivateUsage >= 0) { + final long gpuDmaBufUsage = gpuUsage - gpuPrivateUsage; memInfoBuilder.append(" GPU: "); memInfoBuilder.append(stringifyKBSize(gpuUsage)); memInfoBuilder.append(" ("); diff --git a/services/core/java/com/android/server/stats/pull/SystemMemoryUtil.java b/services/core/java/com/android/server/stats/pull/SystemMemoryUtil.java index 9f8b27f5f1bfb..30b6e688bab65 100644 --- a/services/core/java/com/android/server/stats/pull/SystemMemoryUtil.java +++ b/services/core/java/com/android/server/stats/pull/SystemMemoryUtil.java @@ -28,7 +28,7 @@ final class SystemMemoryUtil { static Metrics getMetrics() { int totalIonKb = (int) Debug.getDmabufHeapTotalExportedKb(); int gpuTotalUsageKb = (int) Debug.getGpuTotalUsageKb(); - int gpuDmaBufUsageKb = (int) Debug.getGpuDmaBufUsageKb(); + int gpuPrivateAllocationsKb = (int) Debug.getGpuPrivateMemoryKb(); int dmaBufTotalExportedKb = (int) Debug.getDmabufTotalExportedKb(); long[] mInfos = new long[Debug.MEMINFO_COUNT]; @@ -58,10 +58,6 @@ final class SystemMemoryUtil { accountedKb += mInfos[Debug.MEMINFO_KERNEL_STACK]; } - int gpuPrivateAllocationsKb = -1; - if (gpuTotalUsageKb >= 0 && gpuDmaBufUsageKb >= 0) { - gpuPrivateAllocationsKb = gpuTotalUsageKb - gpuDmaBufUsageKb; - } // If we can distinguish gpu private allocs it means the dmabuf metrics // are supported already. if (dmaBufTotalExportedKb >= 0 && gpuPrivateAllocationsKb >= 0) {