From 1ff4e91e6ce051a759ec920d141303284c289020 Mon Sep 17 00:00:00 2001
From: Hridya Valsaraju <hridya@google.com>
Date: Wed, 10 Feb 2021 11:02:27 -0800
Subject: [PATCH] dumpsys: Print total DMA-BUFs exported from the DMA-BUF heaps
 framework

This patch adds the total size of DMA-BUFs exported from the DMA-BUF
heaps framework to dumpsys meminfo.

Bug: 167709539
Test: adb shell dumpsys meminfo
Change-Id: I153721a0dc91ee5084a446ba1ce4f8ec9c51c4f0
Merged-In: I153721a0dc91ee5084a446ba1ce4f8ec9c51c4f0
---
 core/java/android/os/Debug.java               |  8 ++++++++
 core/jni/android_os_Debug.cpp                 | 12 ++++++++++++
 .../server/am/ActivityManagerService.java     | 19 +++++++++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java
index ea282afb8b8e..903bea3dc2ce 100644
--- a/core/java/android/os/Debug.java
+++ b/core/java/android/os/Debug.java
@@ -2559,6 +2559,14 @@ public final class Debug
      */
     public static native long getDmabufTotalExportedKb();
 
+    /**
+     * Return total memory size in kilobytes for DMA-BUFs exported from the DMA-BUF
+     * heaps frameworks or -1 in the case of an error.
+     *
+     * @hide
+     */
+    public static native long getDmabufHeapTotalExportedKb();
+
     /**
      * Return memory size in kilobytes allocated for ION heaps or -1 if
      * /sys/kernel/ion/total_heaps_kb could not be read.
diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp
index 223b4dcc0549..0e3db46bd0c9 100644
--- a/core/jni/android_os_Debug.cpp
+++ b/core/jni/android_os_Debug.cpp
@@ -826,6 +826,16 @@ static jlong android_os_Debug_getDmabufTotalExportedKb(JNIEnv* env, jobject claz
     return dmabufTotalSizeKb;
 }
 
+static jlong android_os_Debug_getDmabufHeapTotalExportedKb(JNIEnv* env, jobject clazz) {
+    jlong dmabufHeapTotalSizeKb = -1;
+    uint64_t size;
+
+    if (meminfo::ReadDmabufHeapTotalExportedKb(&size)) {
+        dmabufHeapTotalSizeKb = size;
+    }
+    return dmabufHeapTotalSizeKb;
+}
+
 static jlong android_os_Debug_getIonPoolsSizeKb(JNIEnv* env, jobject clazz) {
     jlong poolsSizeKb = -1;
     uint64_t size;
@@ -983,6 +993,8 @@ static const JNINativeMethod gMethods[] = {
             (void*)android_os_Debug_getDmabufTotalExportedKb },
     { "getGpuDmaBufUsageKb", "()J",
             (void*)android_os_Debug_getGpuDmaBufUsageKb },
+    { "getDmabufHeapTotalExportedKb", "()J",
+            (void*)android_os_Debug_getDmabufHeapTotalExportedKb },
     { "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 a31385d7f8f0..84be0d7e67d4 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -13748,6 +13748,15 @@ public class ActivityManagerService extends IActivityManager.Stub
                     pw.println(" unmapped)");
                     kernelUsed += totalExportedDmabuf;
                 }
+
+                // totalDmabufHeapExported is included in totalExportedDmabuf above and hence do not
+                // need to be added to kernelUsed.
+                final long totalDmabufHeapExported = Debug.getDmabufHeapTotalExportedKb();
+                if (totalDmabufHeapExported >= 0) {
+                    pw.print("DMA-BUF Heaps: ");
+                    pw.println(stringifyKBSize(totalDmabufHeapExported));
+                }
+
                 final long totalDmabufHeapPool = Debug.getDmabufHeapPoolsSizeKb();
                 if (totalDmabufHeapPool >= 0) {
                     pw.print("DMA-BUF Heaps pool: ");
@@ -14588,6 +14597,16 @@ public class ActivityManagerService extends IActivityManager.Stub
                 memInfoBuilder.append("\n");
                 kernelUsed += totalExportedDmabuf;
             }
+
+            // These are included in the totalExportedDmabuf above and hence do not need to be added
+            // to kernelUsed.
+            final long totalExportedDmabufHeap = Debug.getDmabufHeapTotalExportedKb();
+            if (totalExportedDmabufHeap >= 0) {
+                memInfoBuilder.append("DMA-BUF Heap: ");
+                memInfoBuilder.append(stringifyKBSize(totalExportedDmabufHeap));
+                memInfoBuilder.append("\n");
+            }
+
             final long totalDmabufHeapPool = Debug.getDmabufHeapPoolsSizeKb();
             if (totalDmabufHeapPool >= 0) {
                 memInfoBuilder.append("DMA-BUF Heaps pool: ");
-- 
GitLab