From 0233fba2500c58b34d76a0d5dd1cfc04fd6073b5 Mon Sep 17 00:00:00 2001 From: Ioannis Ilkos Date: Mon, 15 Feb 2021 16:58:42 +0000 Subject: [PATCH] Add private gpu and total dmabuf size to SystemMemory atom Changes: - Transition to measuring dmabuf export size (ion allocations are most recently implemented via dmabufs anyway, the total ion size will be a subset of the total dmabuf exported memory) - For supported kernels (5.4+) that have sysfs dmabuf accounting, account for private GPU driver memory vs GPU driver dmabuf memory. - Adjust the accounted memory calculations based on the above. Bug: 149462065 Bug: 172201884 Test: build, statsd_testdrive Change-Id: Ie4e4d0b4fc4495896374fbebc70e29e32469b5e3 --- .../stats/pull/StatsPullAtomService.java | 4 ++- .../server/stats/pull/SystemMemoryUtil.java | 32 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java index 15c72b34dbc0a..342dbfa3f59d2 100644 --- a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java +++ b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java @@ -2134,7 +2134,9 @@ public class StatsPullAtomService extends SystemService { metrics.kernelStackKb, metrics.totalIonKb, metrics.unaccountedKb, - metrics.gpuTotalUsageKb)); + metrics.gpuTotalUsageKb, + metrics.gpuPrivateAllocationsKb, + metrics.dmaBufTotalExportedKb)); return StatsManager.PULL_SUCCESS; } 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 1e80c4fe89fb2..628c1d61ce9a6 100644 --- a/services/core/java/com/android/server/stats/pull/SystemMemoryUtil.java +++ b/services/core/java/com/android/server/stats/pull/SystemMemoryUtil.java @@ -28,6 +28,8 @@ final class SystemMemoryUtil { static Metrics getMetrics() { int totalIonKb = (int) Debug.getIonHeapsSizeKb(); int gpuTotalUsageKb = (int) Debug.getGpuTotalUsageKb(); + int gpuDmaBufUsageKb = (int) Debug.getGpuDmaBufUsageKb(); + int dmaBufTotalExportedKb = (int) Debug.getDmabufTotalExportedKb(); long[] mInfos = new long[Debug.MEMINFO_COUNT]; Debug.getMemInfo(mInfos); @@ -49,14 +51,36 @@ final class SystemMemoryUtil { + mInfos[Debug.MEMINFO_SLAB_UNRECLAIMABLE] + kReclaimableKb + mInfos[Debug.MEMINFO_VM_ALLOC_USED] - + mInfos[Debug.MEMINFO_PAGE_TABLES] - + Math.max(totalIonKb, 0); + + mInfos[Debug.MEMINFO_PAGE_TABLES]; if (!Debug.isVmapStack()) { // See b/146088882 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) { + // If we can calculate the overlap between dma memory and gpu + // drivers we can do more accurate tracking. But this is only + // available on 5.4+ kernels. + accountedKb += dmaBufTotalExportedKb + gpuPrivateAllocationsKb; + } else { + // If we cannot distinguish, accept that we will double count the + // dma buffers also used by the gpu driver. + accountedKb += Math.max(0, gpuTotalUsageKb); + if (dmaBufTotalExportedKb >= 0) { + accountedKb += dmaBufTotalExportedKb; + } else if (totalIonKb >= 0) { + // ION is a subset of total exported dmabuf memory. + accountedKb += totalIonKb; + } + } + Metrics result = new Metrics(); result.unreclaimableSlabKb = (int) mInfos[Debug.MEMINFO_SLAB_UNRECLAIMABLE]; result.vmallocUsedKb = (int) mInfos[Debug.MEMINFO_VM_ALLOC_USED]; @@ -64,6 +88,8 @@ final class SystemMemoryUtil { result.kernelStackKb = (int) mInfos[Debug.MEMINFO_KERNEL_STACK]; result.totalIonKb = totalIonKb; result.gpuTotalUsageKb = gpuTotalUsageKb; + result.gpuPrivateAllocationsKb = gpuPrivateAllocationsKb; + result.dmaBufTotalExportedKb = dmaBufTotalExportedKb; result.unaccountedKb = (int) (mInfos[Debug.MEMINFO_TOTAL] - accountedKb); return result; } @@ -75,6 +101,8 @@ final class SystemMemoryUtil { public int kernelStackKb; public int totalIonKb; public int gpuTotalUsageKb; + public int gpuPrivateAllocationsKb; + public int dmaBufTotalExportedKb; public int unaccountedKb; } }