Merge "Use /proc/pid/smaps_rollup when available" into oc-mr1-dev

This commit is contained in:
Daniel Colascione
2017-08-23 03:45:14 +00:00
committed by Android (Google) Code Review
3 changed files with 77 additions and 18 deletions

View File

@@ -29,6 +29,7 @@
#include <time.h>
#include <unistd.h>
#include <atomic>
#include <iomanip>
#include <string>
@@ -44,16 +45,11 @@
#include "jni.h"
#include <memtrack/memtrack.h>
#include <memunreachable/memunreachable.h>
#include "android_os_Debug.h"
namespace android
{
static void safeFclose(FILE* fp) {
if (fp) fclose(fp);
}
using UniqueFile = std::unique_ptr<FILE, decltype(&safeFclose)>;
static inline UniqueFile MakeUniqueFile(const char* path, const char* mode) {
return UniqueFile(fopen(path, mode), safeFclose);
}
@@ -155,6 +151,14 @@ 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)
@@ -548,6 +552,33 @@ 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 outUssSwapPss, jlongArray outMemtrack)
{
@@ -563,12 +594,11 @@ static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
}
{
std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
UniqueFile fp = MakeUniqueFile(smaps_path.c_str(), "re");
UniqueFile fp = OpenSmapsOrRollup(pid);
if (fp != nullptr) {
while (true) {
if (fgets(line, 1024, fp.get()) == NULL) {
if (fgets(line, sizeof (line), fp.get()) == NULL) {
break;
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_OS_DEBUG_H
#define ANDROID_OS_DEBUG_H
#include <memory>
#include <stdio.h>
namespace android {
inline void safeFclose(FILE* fp) {
if (fp) fclose(fp);
}
using UniqueFile = std::unique_ptr<FILE, decltype(&safeFclose)>;
UniqueFile OpenSmapsOrRollup(int pid);
} // namespace android
#endif // ANDROID_OS_HW_BLOB_H

View File

@@ -31,6 +31,7 @@
#include "android_util_Binder.h"
#include <nativehelper/JNIHelp.h>
#include "android_os_Debug.h"
#include <dirent.h>
#include <fcntl.h>
@@ -1092,27 +1093,21 @@ static jlong android_os_Process_getElapsedCpuTime(JNIEnv* env, jobject clazz)
static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid)
{
char filename[64];
snprintf(filename, sizeof(filename), "/proc/%" PRId32 "/smaps", pid);
FILE * file = fopen(filename, "r");
if (!file) {
UniqueFile file = OpenSmapsOrRollup(pid);
if (file == nullptr) {
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)) {
while (fgets(line, sizeof(line), file.get())) {
jlong v;
if (sscanf(line, "Pss: %" SCNd64 " kB", &v) == 1) {
pss += v;
}
}
fclose(file);
// Return the Pss value in bytes, not kilobytes
return pss * 1024;
}