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

Commit c3762621 authored by Rafal Slawik's avatar Rafal Slawik
Browse files

Move native process memory state pulling code

Refactor the pulling code to avoid going to AcitivityManagerService.
Instead call MemoryStatUtil that reads procfs directly from
StatsCompanionService.

Bug: 118736433
Test: atest UidAtomTest
Change-Id: Idf57e3cd33058651f363c89828f71d19f25e6450
parent 1a5e9f0d
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -160,13 +160,6 @@ public abstract class ActivityManagerInternal {
     */
    public abstract List<ProcessMemoryState> getMemoryStateForProcesses();

    /**
     * Returns a list that contains the memory stats for monitored native processes.
     *
     * The list of the monitored processes is defined in MemoryStatUtil class.
     */
    public abstract List<ProcessMemoryState> getMemoryStateForNativeProcesses();

    /**
     * Checks to see if the calling pid is allowed to handle the user. Returns adjusted user id as
     * needed.
+0 −27
Original line number Diff line number Diff line
@@ -68,9 +68,7 @@ import static android.os.Process.THREAD_GROUP_DEFAULT;
import static android.os.Process.THREAD_GROUP_RESTRICTED;
import static android.os.Process.THREAD_GROUP_TOP_APP;
import static android.os.Process.THREAD_PRIORITY_FOREGROUND;
import static android.os.Process.getPidsForCommands;
import static android.os.Process.getTotalMemory;
import static android.os.Process.getUidForPid;
import static android.os.Process.isThreadInProcess;
import static android.os.Process.killProcess;
import static android.os.Process.killProcessQuiet;
@@ -127,11 +125,8 @@ import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SERVICE;
import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_UID_OBSERVERS;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.am.MemoryStatUtil.MEMORY_STAT_INTERESTING_NATIVE_PROCESSES;
import static com.android.server.am.MemoryStatUtil.hasMemcg;
import static com.android.server.am.MemoryStatUtil.readCmdlineFromProcfs;
import static com.android.server.am.MemoryStatUtil.readMemoryStatFromFilesystem;
import static com.android.server.am.MemoryStatUtil.readMemoryStatFromProcfs;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CLEANUP;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_CONFIGURATION;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
@@ -18757,28 +18752,6 @@ public class ActivityManagerService extends IActivityManager.Stub
            return processMemoryStates;
        }
        @Override
        public List<ProcessMemoryState> getMemoryStateForNativeProcesses() {
            List<ProcessMemoryState> processMemoryStates = new ArrayList<>();
            int[] pids = getPidsForCommands(MEMORY_STAT_INTERESTING_NATIVE_PROCESSES);
            for (int i = 0; i < pids.length; i++) {
                int pid = pids[i];
                MemoryStat memoryStat = readMemoryStatFromProcfs(pid);
                if (memoryStat == null) {
                    continue;
                }
                int uid = getUidForPid(pid);
                String processName = readCmdlineFromProcfs(pid);
                int oomScore = -1; // Unused, not included in the NativeProcessMemoryState atom.
                ProcessMemoryState processMemoryState = new ProcessMemoryState(uid, processName,
                        oomScore, memoryStat.pgfault, memoryStat.pgmajfault,
                        memoryStat.rssInBytes, memoryStat.cacheInBytes, memoryStat.swapInBytes,
                        memoryStat.rssHighWatermarkInBytes, memoryStat.startTimeNanos);
                processMemoryStates.add(processMemoryState);
            }
            return processMemoryStates;
        }
        @Override
        public int handleIncomingUser(int callingPid, int callingUid, int userId,
                boolean allowAll, int allowMode, String name, String callerPackage) {
+4 −4
Original line number Diff line number Diff line
@@ -16,9 +16,9 @@

package com.android.server.am;

import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_METRICS;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_METRICS;

import android.annotation.Nullable;
import android.os.FileUtils;
@@ -45,7 +45,7 @@ public final class MemoryStatUtil {
     * <p>Processes are matched by their cmdline in procfs. Example: cat /proc/pid/cmdline returns
     * /system/bin/statsd for the stats daemon.
     */
    static final String[] MEMORY_STAT_INTERESTING_NATIVE_PROCESSES = new String[]{
    public static final String[] MEMORY_STAT_INTERESTING_NATIVE_PROCESSES = new String[]{
            "/system/bin/statsd",  // Stats daemon.
            "/system/bin/surfaceflinger",
            "/system/bin/apexd",  // APEX daemon.
@@ -146,7 +146,7 @@ public final class MemoryStatUtil {
     * Returns null if file is not found in procfs or if file has unrecognized contents.
     */
    @Nullable
    static MemoryStat readMemoryStatFromProcfs(int pid) {
    public static MemoryStat readMemoryStatFromProcfs(int pid) {
        final String statPath = String.format(Locale.US, PROC_STAT_FILE_FMT, pid);
        MemoryStat stat = parseMemoryStatFromProcfs(readFileContents(statPath));
        if (stat == null) {
@@ -163,7 +163,7 @@ public final class MemoryStatUtil {
     * Returns content of /proc/pid/cmdline (e.g. /system/bin/statsd) or an empty string
     * if the file is not available.
     */
    static String readCmdlineFromProcfs(int pid) {
    public static String readCmdlineFromProcfs(int pid) {
        String path = String.format(Locale.US, PROC_CMDLINE_FILE_FMT, pid);
        String cmdline = readFileContents(path);
        return cmdline != null ? cmdline : "";
+23 −10
Original line number Diff line number Diff line
@@ -15,7 +15,13 @@
 */
package com.android.server.stats;

import static android.os.Process.getPidsForCommands;
import static android.os.Process.getUidForPid;

import static com.android.internal.util.Preconditions.checkNotNull;
import static com.android.server.am.MemoryStatUtil.MEMORY_STAT_INTERESTING_NATIVE_PROCESSES;
import static com.android.server.am.MemoryStatUtil.readCmdlineFromProcfs;
import static com.android.server.am.MemoryStatUtil.readMemoryStatFromProcfs;

import android.annotation.Nullable;
import android.app.ActivityManagerInternal;
@@ -96,6 +102,7 @@ import com.android.server.BinderCallsStatsService;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.SystemServiceManager;
import com.android.server.am.MemoryStatUtil.MemoryStat;
import com.android.server.storage.DiskStatsFileLogger;
import com.android.server.storage.DiskStatsLoggingService;

@@ -1012,17 +1019,23 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    private void pullNativeProcessMemoryState(
            int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
        List<ProcessMemoryState> processMemoryStates = LocalServices.getService(
                ActivityManagerInternal.class).getMemoryStateForNativeProcesses();
        for (ProcessMemoryState processMemoryState : processMemoryStates) {
        int[] pids = getPidsForCommands(MEMORY_STAT_INTERESTING_NATIVE_PROCESSES);
        for (int i = 0; i < pids.length; i++) {
            int pid = pids[i];
            MemoryStat memoryStat = readMemoryStatFromProcfs(pid);
            if (memoryStat == null) {
                continue;
            }
            int uid = getUidForPid(pid);
            String processName = readCmdlineFromProcfs(pid);
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
            e.writeInt(processMemoryState.uid);
            e.writeString(processMemoryState.processName);
            e.writeLong(processMemoryState.pgfault);
            e.writeLong(processMemoryState.pgmajfault);
            e.writeLong(processMemoryState.rssInBytes);
            e.writeLong(processMemoryState.rssHighWatermarkInBytes);
            e.writeLong(processMemoryState.startTimeNanos);
            e.writeInt(uid);
            e.writeString(processName);
            e.writeLong(memoryStat.pgfault);
            e.writeLong(memoryStat.pgmajfault);
            e.writeLong(memoryStat.rssInBytes);
            e.writeLong(memoryStat.rssHighWatermarkInBytes);
            e.writeLong(memoryStat.startTimeNanos);
            pulledData.add(e);
        }
    }