Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc55e00c authored by guolun Xue's avatar guolun Xue
Browse files

Add VmShmem calculate for getRss



In kernel, /proc/<pid>/status reporting calculates total_rss as:
total_rss = anon + file + shmem
Currently Process.getRss() only returns value of total_rss, anon,
file and swap, without shmem. This patch adds shmem reporting in
getRss(), to provide all components of total_rss calculation.

Bug: 318788245
Signed-off-by: default avatar <xueguolun@xiaomi.corp-partner.google.com>
Change-Id: I7dbb4d00a08e6d93ee77768740f2acab5a58a9ea
parent fa892b02
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -1471,7 +1471,15 @@ public class Process {
    @UnsupportedAppUsage
    public static final native long getPss(int pid);

    /** @hide */
    /**
     * Gets the total Rss value for a given process, in bytes.
     *
     * @param pid the process to the Rss for
     * @return an ordered array containing multiple values, they are:
     *  [total_rss, file, anon, swap, shmem].
     *  or NULL if the value cannot be determined
     * @hide
     */
    public static final native long[] getRss(int pid);

    /**
+6 −6
Original line number Diff line number Diff line
@@ -1134,12 +1134,11 @@ static jlong android_os_Process_getPss(JNIEnv* env, jobject clazz, jint pid)

static jlongArray android_os_Process_getRss(JNIEnv* env, jobject clazz, jint pid)
{
    // total, file, anon, swap
    jlong rss[4] = {0, 0, 0, 0};
    // total, file, anon, swap, shmem
    jlong rss[5] = {0, 0, 0, 0, 0};
    std::string status_path =
            android::base::StringPrintf("/proc/%d/status", pid);
    UniqueFile file = MakeUniqueFile(status_path.c_str(), "re");

    char line[256];
    while (file != nullptr && fgets(line, sizeof(line), file.get())) {
        jlong v;
@@ -1151,17 +1150,18 @@ static jlongArray android_os_Process_getRss(JNIEnv* env, jobject clazz, jint pid
            rss[2] = v;
        } else if ( sscanf(line, "VmSwap: %" SCNd64 " kB", &v) == 1) {
            rss[3] = v;
        } else if ( sscanf(line, "RssShmem: %" SCNd64 " kB", &v) == 1) {
            rss[4] = v;
        }
    }

    jlongArray rssArray = env->NewLongArray(4);
    jlongArray rssArray = env->NewLongArray(5);
    if (rssArray == NULL) {
        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
        return NULL;
    }

    env->SetLongArrayRegion(rssArray, 0, 4, rss);

    env->SetLongArrayRegion(rssArray, 0, 5, rss);
    return rssArray;
}