Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1e9ccf55 authored by Eric Miao's avatar Eric Miao
Browse files

Add bitmaps/native allocation metrics to 'dumpsys meminfo'

Bug: 331243037
Flag: com.android.libcore.native_metrics

Add metrics for Bitmaps, HardwareBuffer, and other objects with
native allocations associated to 'dumpsys meminfo'.

E.g.

$ adb shell dumpsys meminfo com.google.android.GoogleCamera

...

 Native Allocations
                         Count                      Total(kB)
                        ------                         ------
   Bitmap (malloced):       31                           6570
Bitmap (nonmalloced):        1                           2976
      HardwareBuffer:        5                           3681
   Others (malloced):     4133                            391
Others (nonmalloced):      391                            244

Change-Id: Ifa7dcc96806617cf378001b02b20a27208e0fbff
parent fddf3266
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -255,6 +255,7 @@ import libcore.io.ForwardingOs;
import libcore.io.IoUtils;
import libcore.io.Os;
import libcore.net.event.NetworkEventDispatcher;
import libcore.util.NativeAllocationRegistry;

import org.apache.harmony.dalvik.ddmc.DdmVmInternal;

@@ -1609,6 +1610,32 @@ public final class ActivityThread extends ClientTransactionHandler
            }
        }

        @NeverCompile
        private void dumpMemInfoNativeAllocations(PrintWriter pw) {
            pw.println(" ");
            pw.println(" Native Allocations");
            printRow(pw, TWO_COUNT_COLUMN_HEADER, "", "Count", "", "Total(kB)");
            printRow(pw, TWO_COUNT_COLUMN_HEADER, "", "------", "", "------");

            for (NativeAllocationRegistry.Metrics m : NativeAllocationRegistry.getMetrics()) {
                // group into 3 major categories: Bitmap, HardwareBuffer and Other
                final String className = switch (m.getClassName()) {
                    case "android.graphics.Bitmap" -> "Bitmap";
                    case "android.hardware.HardwareBuffer" -> "HardwareBuffer";
                    default -> "Other";
                };

                if (m.getMallocedCount() != 0 || m.getMallocedBytes() != 0) {
                    printRow(pw, TWO_COUNT_COLUMNS, className + " (malloced):",
                        m.getMallocedCount(), "", m.getMallocedBytes() / 1024);
                }
                if (m.getNonmallocedCount() != 0 || m.getNonmallocedBytes() != 0) {
                    printRow(pw, TWO_COUNT_COLUMNS, className + " (nonmalloced):",
                        m.getNonmallocedCount(), "", m.getNonmallocedBytes() / 1024);
                }
            }
        }

        @NeverCompile
        private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
                boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
@@ -1707,6 +1734,10 @@ public final class ActivityThread extends ClientTransactionHandler
            printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
                    "WebViews:", webviewInstanceCount);

            if (com.android.libcore.Flags.nativeMetrics()) {
                dumpMemInfoNativeAllocations(pw);
            }

            // SQLite mem info
            pw.println(" ");
            pw.println(" SQL");