From 2018bf03f9fd96aa0326d74f23abdbaa3d4ee143 Mon Sep 17 00:00:00 2001 From: Suren Baghdasaryan Date: Wed, 18 Dec 2019 09:52:42 -0800 Subject: Prevent double-counting of kernel stack size in dumpsys reports When kernel is configured with CONFIG_VMAP_STACK=y kernel stacks are mapped into process space and therefore already accounted as part of the value returned by ReadVmallocInfo and reported under MEMINFO_VM_ALLOC_USED category. Introduce isVmapStack function to check if kernel is configured with that option and if so do not exclude kernel stack size in the calculation of memory used by kernel. Bug: 146088882 Test: dumpsys meminfo with CONFIG_VMAP_STACK enabled and disabled Change-Id: I9230bbf0ab05524cf52ded35171fd9ec3da16fa0 Signed-off-by: Suren Baghdasaryan --- core/java/android/os/Debug.java | 8 ++++++++ .../com/android/internal/util/MemInfoReader.java | 9 ++++++--- core/jni/android_os_Debug.cpp | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java index 422761c4dca5..d7ed05577697 100644 --- a/core/java/android/os/Debug.java +++ b/core/java/android/os/Debug.java @@ -2526,4 +2526,12 @@ public final class Debug * @hide */ public static native long getIonMappedSizeKb(); + + /** + * Return whether virtually-mapped kernel stacks are enabled (CONFIG_VMAP_STACK). + * Note: caller needs config_gz read sepolicy permission + * + * @hide + */ + public static native boolean isVmapStack(); } diff --git a/core/java/com/android/internal/util/MemInfoReader.java b/core/java/com/android/internal/util/MemInfoReader.java index 362bc92e40ad..580c2fa66de2 100644 --- a/core/java/com/android/internal/util/MemInfoReader.java +++ b/core/java/com/android/internal/util/MemInfoReader.java @@ -107,9 +107,12 @@ public final class MemInfoReader { * Amount of RAM that is in use by the kernel for actual allocations. */ public long getKernelUsedSizeKb() { - return mInfos[Debug.MEMINFO_SHMEM] + mInfos[Debug.MEMINFO_SLAB_UNRECLAIMABLE] - + mInfos[Debug.MEMINFO_VM_ALLOC_USED] + mInfos[Debug.MEMINFO_PAGE_TABLES] - + mInfos[Debug.MEMINFO_KERNEL_STACK]; + long size = mInfos[Debug.MEMINFO_SHMEM] + mInfos[Debug.MEMINFO_SLAB_UNRECLAIMABLE] + + mInfos[Debug.MEMINFO_VM_ALLOC_USED] + mInfos[Debug.MEMINFO_PAGE_TABLES]; + if (!Debug.isVmapStack()) { + size += mInfos[Debug.MEMINFO_KERNEL_STACK]; + } + return size; } public long getSwapTotalSizeKb() { diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp index 3e7b6b3a6bff..52ce54bc8924 100644 --- a/core/jni/android_os_Debug.cpp +++ b/core/jni/android_os_Debug.cpp @@ -50,6 +50,7 @@ #include #include #include "android_os_Debug.h" +#include namespace android { @@ -833,6 +834,23 @@ static jlong android_os_Debug_getIonMappedSizeKb(JNIEnv* env, jobject clazz) { return ionPss; } +static jboolean android_os_Debug_isVmapStack(JNIEnv *env, jobject clazz) +{ + static enum { + CONFIG_UNKNOWN, + CONFIG_SET, + CONFIG_UNSET, + } cfg_state = CONFIG_UNKNOWN; + + if (cfg_state == CONFIG_UNKNOWN) { + const std::map configs = + vintf::VintfObject::GetInstance()->getRuntimeInfo()->kernelConfigs(); + std::map::const_iterator it = configs.find("CONFIG_VMAP_STACK"); + cfg_state = (it != configs.end() && it->second == "y") ? CONFIG_SET : CONFIG_UNSET; + } + return cfg_state == CONFIG_SET; +} + /* * JNI registration. */ @@ -882,6 +900,8 @@ static const JNINativeMethod gMethods[] = { (void*)android_os_Debug_getIonPoolsSizeKb }, { "getIonMappedSizeKb", "()J", (void*)android_os_Debug_getIonMappedSizeKb }, + { "isVmapStack", "()Z", + (void*)android_os_Debug_isVmapStack }, }; int register_android_os_Debug(JNIEnv *env) -- cgit v1.2.3-59-g8ed1b