diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp index 888dab19c2479..d93e3ea65ce45 100644 --- a/core/jni/android_os_Debug.cpp +++ b/core/jni/android_os_Debug.cpp @@ -29,7 +29,6 @@ #include #include -#include #include #include #include @@ -42,6 +41,7 @@ #include #include #include "jni.h" +#include #include #include #include @@ -150,14 +150,6 @@ struct stats_t { int swappedOutPss; }; -enum pss_rollup_support { - PSS_ROLLUP_UNTRIED, - PSS_ROLLUP_SUPPORTED, - PSS_ROLLUP_UNSUPPORTED -}; - -static std::atomic g_pss_rollup_support; - #define BINDER_STATS "/proc/binder/stats" static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz) @@ -555,37 +547,9 @@ static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject o android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object); } -UniqueFile OpenSmapsOrRollup(int pid) -{ - enum pss_rollup_support rollup_support = - g_pss_rollup_support.load(std::memory_order_relaxed); - if (rollup_support != PSS_ROLLUP_UNSUPPORTED) { - std::string smaps_rollup_path = - base::StringPrintf("/proc/%d/smaps_rollup", pid); - UniqueFile fp_rollup = MakeUniqueFile(smaps_rollup_path.c_str(), "re"); - if (fp_rollup == nullptr && errno != ENOENT) { - return fp_rollup; // Actual error, not just old kernel. - } - if (fp_rollup != nullptr) { - if (rollup_support == PSS_ROLLUP_UNTRIED) { - ALOGI("using rollup pss collection"); - g_pss_rollup_support.store(PSS_ROLLUP_SUPPORTED, - std::memory_order_relaxed); - } - return fp_rollup; - } - g_pss_rollup_support.store(PSS_ROLLUP_UNSUPPORTED, - std::memory_order_relaxed); - } - - std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid); - return MakeUniqueFile(smaps_path.c_str(), "re"); -} - static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, jlongArray outUssSwapPssRss, jlongArray outMemtrack) { - char lineBuffer[1024]; jlong pss = 0; jlong rss = 0; jlong swapPss = 0; @@ -597,59 +561,14 @@ static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, pss = uss = rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other; } - { - UniqueFile fp = OpenSmapsOrRollup(pid); - - if (fp != nullptr) { - char* line; - - while (true) { - if (fgets(lineBuffer, sizeof (lineBuffer), fp.get()) == NULL) { - break; - } - line = lineBuffer; - - switch (line[0]) { - case 'P': - if (strncmp(line, "Pss:", 4) == 0) { - char* c = line + 4; - while (*c != 0 && (*c < '0' || *c > '9')) { - c++; - } - pss += atoi(c); - } else if (strncmp(line, "Private_Clean:", 14) == 0 - || strncmp(line, "Private_Dirty:", 14) == 0) { - char* c = line + 14; - while (*c != 0 && (*c < '0' || *c > '9')) { - c++; - } - uss += atoi(c); - } - break; - case 'R': - if (strncmp(line, "Rss:", 4) == 0) { - char* c = line + 4; - while (*c != 0 && (*c < '0' || *c > '9')) { - c++; - } - rss += atoi(c); - } - break; - case 'S': - if (strncmp(line, "SwapPss:", 8) == 0) { - char* c = line + 8; - jlong lSwapPss; - while (*c != 0 && (*c < '0' || *c > '9')) { - c++; - } - lSwapPss = atoi(c); - swapPss += lSwapPss; - pss += lSwapPss; // Also in swap, those pages would be accounted as Pss without SWAP - } - break; - } - } - } + ::android::meminfo::ProcMemInfo proc_mem(pid); + ::android::meminfo::MemUsage stats; + if (proc_mem.SmapsOrRollup(&stats)) { + pss += stats.pss; + uss += stats.uss; + rss += stats.rss; + swapPss = stats.swap_pss; + pss += swapPss; // Also in swap, those pages would be accounted as Pss without SWAP } if (outUssSwapPssRss != NULL) { diff --git a/core/jni/android_os_Debug.h b/core/jni/android_os_Debug.h index c7b731bdb615b..747776a1b89ba 100644 --- a/core/jni/android_os_Debug.h +++ b/core/jni/android_os_Debug.h @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -34,8 +35,6 @@ inline UniqueFile MakeUniqueFile(const char* path, const char* mode) { return UniqueFile(fopen(path, mode), safeFclose); } -UniqueFile OpenSmapsOrRollup(int pid); - } // namespace android #endif // ANDROID_OS_HW_BLOB_H diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp index 0c1a8aa18370f..798825fbe4a96 100644 --- a/core/jni/android_util_Process.cpp +++ b/core/jni/android_util_Process.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -1083,21 +1084,12 @@ static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz) static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid) { - UniqueFile file = OpenSmapsOrRollup(pid); - if (file == nullptr) { + ::android::meminfo::ProcMemInfo proc_mem(pid); + uint64_t pss; + if (!proc_mem.SmapsOrRollupPss(&pss)) { return (jlong) -1; } - // Tally up all of the Pss from the various maps - char line[256]; - jlong pss = 0; - while (fgets(line, sizeof(line), file.get())) { - jlong v; - if (sscanf(line, "Pss: %" SCNd64 " kB", &v) == 1) { - pss += v; - } - } - // Return the Pss value in bytes, not kilobytes return pss * 1024; }