diff options
| -rw-r--r-- | core/java/android/os/Debug.java | 8 | ||||
| -rw-r--r-- | core/jni/android_os_Debug.cpp | 12 | ||||
| -rw-r--r-- | services/core/java/com/android/server/am/ActivityManagerService.java | 44 |
3 files changed, 63 insertions, 1 deletions
diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java index ef1e6c9405f3..51aa06b93122 100644 --- a/core/java/android/os/Debug.java +++ b/core/java/android/os/Debug.java @@ -2741,4 +2741,12 @@ public final class Debug */ public static native boolean logAllocatorStats(); + /** + * Return the amount of memory (in kB) allocated by kernel drivers through CMA. + * @return a non-negative value or -1 on error. + * + * @hide + */ + public static native long getKernelCmaUsageKb(); + } diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp index 3c2dccd451d4..9ef17e82c38e 100644 --- a/core/jni/android_os_Debug.cpp +++ b/core/jni/android_os_Debug.cpp @@ -729,6 +729,17 @@ static jlong android_os_Debug_getGpuPrivateMemoryKb(JNIEnv* env, jobject clazz) return gpuPrivateMem / 1024; } +static jlong android_os_Debug_getKernelCmaUsageKb(JNIEnv* env, jobject clazz) { + jlong totalKernelCmaUsageKb = -1; + uint64_t size; + + if (meminfo::ReadKernelCmaUsageKb(&size)) { + totalKernelCmaUsageKb = size; + } + + return totalKernelCmaUsageKb; +} + static jlong android_os_Debug_getDmabufMappedSizeKb(JNIEnv* env, jobject clazz) { jlong dmabufPss = 0; std::vector<dmabufinfo::DmaBuffer> dmabufs; @@ -836,6 +847,7 @@ static const JNINativeMethod gMethods[] = { {"getGpuTotalUsageKb", "()J", (void*)android_os_Debug_getGpuTotalUsageKb}, {"isVmapStack", "()Z", (void*)android_os_Debug_isVmapStack}, {"logAllocatorStats", "()Z", (void*)android_os_Debug_logAllocatorStats}, + {"getKernelCmaUsageKb", "()J", (void*)android_os_Debug_getKernelCmaUsageKb}, }; int register_android_os_Debug(JNIEnv *env) diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index f5e15455e538..8de1295dfe6c 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -12796,6 +12796,28 @@ public class ActivityManagerService extends IActivityManager.Stub } } + final long kernelCmaUsage = Debug.getKernelCmaUsageKb(); + if (kernelCmaUsage >= 0) { + pw.print(" Kernel CMA: "); + pw.println(stringifyKBSize(kernelCmaUsage)); + // CMA memory can be in one of the following four states: + // + // 1. Free, in which case it is accounted for as part of MemFree, which + // is already considered in the lostRAM calculation below. + // + // 2. Allocated as part of a userspace allocated, in which case it is + // already accounted for in the total PSS value that was computed. + // + // 3. Allocated for storing compressed memory (ZRAM) on Android kernels. + // This is accounted for by calculating the amount of memory ZRAM + // consumes and including it in the lostRAM calculuation. + // + // 4. Allocated by a kernel driver, in which case, it is currently not + // attributed to any term that has been derived thus far. Since the + // allocations come from a kernel driver, add it to kernelUsed. + kernelUsed += kernelCmaUsage; + } + // Note: ION/DMA-BUF heap pools are reclaimable and hence, they are included as part of // memInfo.getCachedSizeKb(). final long lostRAM = memInfo.getTotalSizeKb() @@ -13313,12 +13335,32 @@ public class ActivityManagerService extends IActivityManager.Stub proto.write(MemInfoDumpProto.CACHED_KERNEL_KB, memInfo.getCachedSizeKb()); proto.write(MemInfoDumpProto.FREE_KB, memInfo.getFreeSizeKb()); } + // CMA memory can be in one of the following four states: + // + // 1. Free, in which case it is accounted for as part of MemFree, which + // is already considered in the lostRAM calculation below. + // + // 2. Allocated as part of a userspace allocated, in which case it is + // already accounted for in the total PSS value that was computed. + // + // 3. Allocated for storing compressed memory (ZRAM) on Android Kernels. + // This is accounted for by calculating hte amount of memory ZRAM + // consumes and including it in the lostRAM calculation. + // + // 4. Allocated by a kernel driver, in which case, it is currently not + // attributed to any term that has been derived thus far, so subtract + // it from lostRAM. + long kernelCmaUsage = Debug.getKernelCmaUsageKb(); + if (kernelCmaUsage < 0) { + kernelCmaUsage = 0; + } long lostRAM = memInfo.getTotalSizeKb() - (ss[INDEX_TOTAL_PSS] - ss[INDEX_TOTAL_SWAP_PSS]) - memInfo.getFreeSizeKb() - memInfo.getCachedSizeKb() // NR_SHMEM is subtracted twice (getCachedSizeKb() and getKernelUsedSizeKb()) + memInfo.getShmemSizeKb() - - memInfo.getKernelUsedSizeKb() - memInfo.getZramTotalSizeKb(); + - memInfo.getKernelUsedSizeKb() - memInfo.getZramTotalSizeKb() + - kernelCmaUsage; proto.write(MemInfoDumpProto.USED_PSS_KB, ss[INDEX_TOTAL_PSS] - cachedPss); proto.write(MemInfoDumpProto.USED_KERNEL_KB, memInfo.getKernelUsedSizeKb()); proto.write(MemInfoDumpProto.LOST_RAM_KB, lostRAM); |