Debug: Use libmeminfo for Pss collection
Bug: 111694435 Test: dumpsys meminfo Change-Id: I581d7ad6acbfbb0671929d951e69b50f124f8126 Signed-off-by: Sandeep Patil <sspatil@google.com>
This commit is contained in:
@@ -29,7 +29,6 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -42,6 +41,7 @@
|
||||
#include <nativehelper/JNIHelp.h>
|
||||
#include <nativehelper/ScopedUtfChars.h>
|
||||
#include "jni.h"
|
||||
#include <meminfo/procmeminfo.h>
|
||||
#include <meminfo/sysmeminfo.h>
|
||||
#include <memtrack/memtrack.h>
|
||||
#include <memunreachable/memunreachable.h>
|
||||
@@ -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<pss_rollup_support> 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) {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <stdio.h>
|
||||
#include <meminfo/meminfo.h>
|
||||
#include <android-base/stringprintf.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <cutils/sched_policy.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/Vector.h>
|
||||
#include <meminfo/procmeminfo.h>
|
||||
#include <meminfo/sysmeminfo.h>
|
||||
#include <processgroup/processgroup.h>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user