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

Commit ecec60e0 authored by Kalesh Singh's avatar Kalesh Singh
Browse files

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
Merged-In: Ib90505ec184339d5acbcd42c67dad3ad65dbd933
parent be87b84d
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -2593,11 +2593,11 @@ public final class Debug
    public static native long getIonPoolsSizeKb();
    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
     * @hide
     */
     */
    public static native long getGpuDmaBufUsageKb();
    public static native long getGpuPrivateMemoryKb();


    /**
    /**
     * Return DMA-BUF memory mapped by processes in kB.
     * Return DMA-BUF memory mapped by processes in kB.
+0 −1
Original line number Original line Diff line number Diff line
@@ -225,7 +225,6 @@ cc_library_shared {
            ],
            ],


            shared_libs: [
            shared_libs: [
                "android.hardware.memtrack-V1-ndk_platform",
                "libandroidicu",
                "libandroidicu",
                "libandroid_net",
                "libandroid_net",
                "libbpf_android",
                "libbpf_android",
+13 −20
Original line number Original line Diff line number Diff line
@@ -33,7 +33,6 @@
#include <string>
#include <string>
#include <vector>
#include <vector>


#include <aidl/android/hardware/memtrack/DeviceInfo.h>
#include <android-base/logging.h>
#include <android-base/logging.h>
#include <bionic/malloc.h>
#include <bionic/malloc.h>
#include <debuggerd/client.h>
#include <debuggerd/client.h>
@@ -46,7 +45,6 @@
#include "jni.h"
#include "jni.h"
#include <dmabufinfo/dmabuf_sysfs_stats.h>
#include <dmabufinfo/dmabuf_sysfs_stats.h>
#include <dmabufinfo/dmabufinfo.h>
#include <dmabufinfo/dmabufinfo.h>
#include <dmabufinfo/dmabuf_sysfs_stats.h>
#include <meminfo/procmeminfo.h>
#include <meminfo/procmeminfo.h>
#include <meminfo/sysmeminfo.h>
#include <meminfo/sysmeminfo.h>
#include <memtrack/memtrack.h>
#include <memtrack/memtrack.h>
@@ -858,29 +856,24 @@ static jlong android_os_Debug_getDmabufHeapPoolsSizeKb(JNIEnv* env, jobject claz
    return poolsSizeKb;
    return poolsSizeKb;
}
}


static jlong android_os_Debug_getGpuDmaBufUsageKb(JNIEnv* env, jobject clazz) {
static jlong android_os_Debug_getGpuPrivateMemoryKb(JNIEnv* env, jobject clazz) {
    std::vector<aidl::android::hardware::memtrack::DeviceInfo> gpu_device_info;
    struct memtrack_proc* p = memtrack_proc_new();
    if (!memtrack_gpu_device_info(&gpu_device_info)) {
    if (p == nullptr) {
        LOG(ERROR) << "getGpuPrivateMemoryKb: Failed to create memtrack_proc";
        return -1;
        return -1;
    }
    }


    dmabufinfo::DmabufSysfsStats stats;
    // Memtrack hal defines PID 0 as global total for GPU-private (GL) memory.
    if (!GetDmabufSysfsStats(&stats)) {
    if (memtrack_proc_get(p, 0) != 0) {
        // The memtrack HAL may not be available, avoid flooding the log.
        memtrack_proc_destroy(p);
        return -1;
        return -1;
    }
    }


    jlong sizeKb = 0;
    ssize_t gpuPrivateMem = memtrack_proc_gl_pss(p);
    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 / 1024;
    memtrack_proc_destroy(p);
    }
    return gpuPrivateMem / 1024;

    return sizeKb;
}
}


static jlong android_os_Debug_getDmabufMappedSizeKb(JNIEnv* env, jobject clazz) {
static jlong android_os_Debug_getDmabufMappedSizeKb(JNIEnv* env, jobject clazz) {
@@ -991,8 +984,8 @@ static const JNINativeMethod gMethods[] = {
            (void*)android_os_Debug_getIonHeapsSizeKb },
            (void*)android_os_Debug_getIonHeapsSizeKb },
    { "getDmabufTotalExportedKb", "()J",
    { "getDmabufTotalExportedKb", "()J",
            (void*)android_os_Debug_getDmabufTotalExportedKb },
            (void*)android_os_Debug_getDmabufTotalExportedKb },
    { "getGpuDmaBufUsageKb", "()J",
    { "getGpuPrivateMemoryKb", "()J",
            (void*)android_os_Debug_getGpuDmaBufUsageKb },
            (void*)android_os_Debug_getGpuPrivateMemoryKb },
    { "getDmabufHeapTotalExportedKb", "()J",
    { "getDmabufHeapTotalExportedKb", "()J",
            (void*)android_os_Debug_getDmabufHeapTotalExportedKb },
            (void*)android_os_Debug_getDmabufHeapTotalExportedKb },
    { "getIonPoolsSizeKb", "()J",
    { "getIonPoolsSizeKb", "()J",
+6 −6
Original line number Original line Diff line number Diff line
@@ -13831,9 +13831,9 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
            }
            final long gpuUsage = Debug.getGpuTotalUsageKb();
            final long gpuUsage = Debug.getGpuTotalUsageKb();
            if (gpuUsage >= 0) {
            if (gpuUsage >= 0) {
                final long gpuDmaBufUsage = Debug.getGpuDmaBufUsageKb();
                final long gpuPrivateUsage = Debug.getGpuPrivateMemoryKb();
                if (gpuDmaBufUsage >= 0) {
                if (gpuPrivateUsage >= 0) {
                    final long gpuPrivateUsage = gpuUsage - gpuDmaBufUsage;
                    final long gpuDmaBufUsage = gpuUsage - gpuPrivateUsage;
                    pw.print("      GPU: ");
                    pw.print("      GPU: ");
                    pw.print(stringifyKBSize(gpuUsage));
                    pw.print(stringifyKBSize(gpuUsage));
                    pw.print(" (");
                    pw.print(" (");
@@ -14701,9 +14701,9 @@ public class ActivityManagerService extends IActivityManager.Stub
        final long gpuUsage = Debug.getGpuTotalUsageKb();
        final long gpuUsage = Debug.getGpuTotalUsageKb();
        if (gpuUsage >= 0) {
        if (gpuUsage >= 0) {
            final long gpuDmaBufUsage = Debug.getGpuDmaBufUsageKb();
            final long gpuPrivateUsage = Debug.getGpuPrivateMemoryKb();
            if (gpuDmaBufUsage >= 0) {
            if (gpuPrivateUsage >= 0) {
                final long gpuPrivateUsage = gpuUsage - gpuDmaBufUsage;
                final long gpuDmaBufUsage = gpuUsage - gpuPrivateUsage;
                memInfoBuilder.append("      GPU: ");
                memInfoBuilder.append("      GPU: ");
                memInfoBuilder.append(stringifyKBSize(gpuUsage));
                memInfoBuilder.append(stringifyKBSize(gpuUsage));
                memInfoBuilder.append(" (");
                memInfoBuilder.append(" (");