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

Commit 6cb7145d authored by Shadman Shadid's avatar Shadman Shadid
Browse files

Update memcg puller for hwm

Flag: com.android.server.stats.add_memcg_memory_information_puller
Test: Manual
Bug: 441966850
Change-Id: I8e15e23ada5f7114bcea36c478d653902036bdbc
parent 79a784c5
Loading
Loading
Loading
Loading
+67 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ public final class MemcgProcMemoryUtil {
     /**
      * Reads memcg accounting of memory stats of a process.
      *
      * Returns values of memory.current, memory.swap in bytes or -1 if not available.
      * Returns values of memory.current, memory.swap in bytes or null if not available.
      */
    public static MemcgMemorySnapshot readMemcgMemorySnapshot(int pid) {
        String cgroupPath = getCgroupPathForPid(pid);
@@ -50,6 +50,22 @@ public final class MemcgProcMemoryUtil {
        return readMemcgMemorySnapshot(cgroupPath);
    }

    /**
      *
      * Reads memcg accounting of memory hwm stats of a process.
      *
      * Returns values of memory.current.peak, memory.swap.peak in bytes or null if not available.
     */
    public static MemcgHighWaterMarkMemorySnapshot readHighWaterMarkMemorySnapshot(
            int pid
    ) {
        String cgroupPath = getCgroupPathForPid(pid);
        if (cgroupPath == null) {
            return null;
        }
        return readMemcgHighWaterMarkMemorySnapshot(cgroupPath);
    }

    /**
     * Gets the cgroup v2 path for a given process ID (pid).
     *
@@ -73,6 +89,34 @@ public final class MemcgProcMemoryUtil {
        return null;
    }

    private static MemcgHighWaterMarkMemorySnapshot
            readMemcgHighWaterMarkMemorySnapshot(String cgroupPath) {
        Path fullMemcgPath = Paths.get(CGROUP_ROOT, cgroupPath);

        final MemcgHighWaterMarkMemorySnapshot snapshot = new MemcgHighWaterMarkMemorySnapshot();
        Long memoryPeak = readSingleValueFromMemcgFile(
                fullMemcgPath,
                "memory.current.peak",
                MEMCG_MEMORY_FORMAT
        );
        if (memoryPeak == null) {
            return null;
        }

        Long swapMemoryPeak = readSingleValueFromMemcgFile(
                fullMemcgPath,
                "memory.swap.peak",
                MEMCG_MEMORY_FORMAT
        );
        if (swapMemoryPeak == null) {
            return null;
        }

        snapshot.memcgMemoryPeakInBytes = memoryPeak;
        snapshot.memcgSwapMemoryPeakInBytes = swapMemoryPeak;
        return snapshot;
    }

    private static MemcgMemorySnapshot readMemcgMemorySnapshot(String cgroupPath) {
        Path fullMemcgPath = Paths.get(CGROUP_ROOT, cgroupPath);

@@ -111,6 +155,28 @@ public final class MemcgProcMemoryUtil {
        return snapshot;
    }

    private static Long readSingleValueFromMemcgFile(
            Path fullMemcgPath, String fileKey, int[] fileFormat) {
        long[] memoryValue = new long[1];
        String memoryNodePath = fullMemcgPath.resolve(fileKey).toString();
        if (Process.readProcFile(
                memoryNodePath,
                fileFormat,
                null,
                memoryValue,
                null
        )) {
            return memoryValue[0];
        } else {
            return null;
        }
    }

    public static final class MemcgHighWaterMarkMemorySnapshot {
        public long memcgMemoryPeakInBytes;
        public long memcgSwapMemoryPeakInBytes;
    }

    public static final class MemcgMemorySnapshot {
        public long memcgMemoryInBytes;
        public long memcgSwapMemoryInBytes;
+40 −3
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import static android.telephony.TelephonyManager.UNKNOWN_CARRIER_ID;
import static android.util.MathUtils.constrain;
import static android.view.Display.HdrCapabilities.HDR_TYPE_INVALID;

import static com.android.internal.os.MemcgProcMemoryUtil.readHighWaterMarkMemorySnapshot;
import static com.android.internal.os.MemcgProcMemoryUtil.readMemcgMemorySnapshot;
import static com.android.internal.os.ProcfsMemoryUtil.DmaBufType;
import static com.android.internal.os.ProcfsMemoryUtil.getProcessCmdlines;
@@ -77,7 +78,7 @@ import static com.android.internal.util.FrameworkStatsLog.TIME_ZONE_DETECTOR_STA
import static com.android.internal.util.FrameworkStatsLog.TIME_ZONE_DETECTOR_STATE__DETECTION_MODE__TELEPHONY;
import static com.android.internal.util.FrameworkStatsLog.TIME_ZONE_DETECTOR_STATE__DETECTION_MODE__UNKNOWN;
import static com.android.server.stats.Flags.addMobileBytesTransferByProcStatePuller;
import static com.android.server.stats.Flags.addMemcgInformationPuller;
import static com.android.server.stats.Flags.addMemcgMemoryInformationPuller;
import static com.android.server.stats.pull.IonMemoryUtil.readProcessSystemIonHeapSizesFromDebugfs;
import static com.android.server.stats.pull.IonMemoryUtil.readSystemIonHeapSizeFromDebugfs;
import static com.android.server.stats.pull.netstats.NetworkStatsUtils.fromPublicNetworkStats;
@@ -215,6 +216,7 @@ import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidClusterTimeRea
import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidFreqTimeReader;
import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidUserSysTimeReader;
import com.android.internal.os.LooperStats;
import com.android.internal.os.MemcgProcMemoryUtil.MemcgHighWaterMarkMemorySnapshot;
import com.android.internal.os.MemcgProcMemoryUtil.MemcgMemorySnapshot;
import com.android.internal.os.PowerProfile;
import com.android.internal.os.ProcessCpuTracker;
@@ -230,7 +232,6 @@ import com.android.server.LocalManagerRegistry;
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.health.HealthServiceWrapper;
import com.android.server.notification.NotificationManagerService;
import com.android.server.pinner.PinnerService;
@@ -473,7 +474,7 @@ public class StatsPullAtomService extends SystemService {
                addMobileBytesTransferByProcStatePuller();

    public static final boolean ENABLE_MEMCG_STATS_PULLER =
                addMemcgInformationPuller();
                addMemcgMemoryInformationPuller();

    private static final ArrayMap<String, Integer> mPreviousThermalThrottlingStatus =
            new ArrayMap<>();
@@ -862,6 +863,8 @@ public class StatsPullAtomService extends SystemService {
                        return pullPressureStallInformation(atomTag, data);
                    case FrameworkStatsLog.MEMCG_MEMORY_SNAPSHOT:
                        return pullMemcgProcessMemoryInformation(atomTag, data);
                    case FrameworkStatsLog.MEMCG_MEMORY_HIGH_WATER_MARK_SNAPSHOT:
                        return pullMemcgProcessHighMemoryHighWatermark(atomTag, data);
                    default:
                        throw new UnsupportedOperationException("Unknown tagId=" + atomTag);
                }
@@ -1064,6 +1067,7 @@ public class StatsPullAtomService extends SystemService {
        registerBatteryLife();
        if (ENABLE_MEMCG_STATS_PULLER) {
            registerMemcgInformation();
            registerMemcgMemoryHighWaterMark();
        }
    }

@@ -5258,6 +5262,18 @@ public class StatsPullAtomService extends SystemService {
        );
    }

    private void registerMemcgMemoryHighWaterMark() {
        int tagId = FrameworkStatsLog.MEMCG_MEMORY_HIGH_WATER_MARK_SNAPSHOT;
        mStatsManager.setPullAtomCallback(
                tagId,
                new PullAtomMetadata.Builder()
                        .setCoolDownMillis(MILLIS_PER_SEC)
                        .build(),
                DIRECT_EXECUTOR,
                mStatsCallbackImpl
        );
    }

    int pullMemcgProcessMemoryInformation(int atomTag, List<StatsEvent> pulledData) {
        List<ProcessMemoryState> managedProcessList =
                LocalServices.getService(ActivityManagerInternal.class)
@@ -5278,6 +5294,27 @@ public class StatsPullAtomService extends SystemService {
        return StatsManager.PULL_SUCCESS;
    }

    int pullMemcgProcessHighMemoryHighWatermark(int atomTag, List<StatsEvent> pulledData) {
        List<ProcessMemoryState> managedProcessList =
                LocalServices.getService(ActivityManagerInternal.class)
                        .getMemoryStateForProcesses();
        for (ProcessMemoryState managedProcess : managedProcessList) {
            final MemcgHighWaterMarkMemorySnapshot snapshot =
                    readHighWaterMarkMemorySnapshot(managedProcess.pid);
            if (snapshot == null) {
                continue;
            }
            pulledData.add(FrameworkStatsLog.buildStatsEvent(
                    atomTag,
                    managedProcess.uid,
                    managedProcess.processName,
                    snapshot.memcgMemoryPeakInBytes / 1024,
                    snapshot.memcgSwapMemoryPeakInBytes / 1024
            ));
        }
        return StatsManager.PULL_SUCCESS;
    }

    private int toProtoPsiResourceType(PsiData.ResourceType resourceType) {
        if (resourceType == PsiData.ResourceType.CPU) {
            return PRESSURE_STALL_INFORMATION__PSI_RESOURCE__PSI_RESOURCE_CPU;
+2 −2
Original line number Diff line number Diff line
@@ -43,8 +43,8 @@ flag {
}

flag {
    name: "add_memcg_information_puller"
    namespace: "statsd"
    name: "add_memcg_memory_information_puller"
    namespace: "watch_software_performance"
    description: "Adds memcgv2 atom logging"
    bug: "434920925"
}
 No newline at end of file