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

Commit 239b680a authored by Rafal Slawik's avatar Rafal Slawik
Browse files

Add RSS high-water mark in kilobytes

The new field will be used to reduce the upload size of RSS HWM metric.
Single int32 instead of int64.

Keep both fields populated on device for backwards compatibility.

Bug: 139269772
Test: atest MemoryStatUtilTest
Change-Id: Iea850a0baceabe07586147955fcfd11c46f8ac30
parent 70f1945f
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -4094,9 +4094,13 @@ message ProcessMemoryHighWaterMark {
    // Provided by ActivityManagerService or read from /proc/PID/cmdline.
    optional string process_name = 2;

    // Deprecated: use rss_high_water_mark_in_kilobytes instead. This field is
    // computed by converting kilobytes to bytes.
    optional int64 rss_high_water_mark_in_bytes = 3 [deprecated = true];

    // RSS high-water mark. Peak RSS usage of the process. Read from the VmHWM field in
    // /proc/PID/status.
    optional int64 rss_high_water_mark_in_bytes = 3;
    optional int32 rss_high_water_mark_in_kilobytes = 4;
}

/*
+6 −8
Original line number Diff line number Diff line
@@ -126,9 +126,9 @@ public final class MemoryStatUtil {

    /**
     * Reads RSS high-water mark of a process from procfs. Returns value of the VmHWM field in
     * /proc/PID/status in bytes or 0 if not available.
     * /proc/PID/status in kilobytes or 0 if not available.
     */
    public static long readRssHighWaterMarkFromProcfs(int pid) {
    public static int readRssHighWaterMarkFromProcfs(int pid) {
        final String statusPath = String.format(Locale.US, PROC_STATUS_FILE_FMT, pid);
        return parseVmHWMFromProcfs(readFileContents(statusPath));
    }
@@ -236,17 +236,15 @@ public final class MemoryStatUtil {
    }

    /**
     * Parses RSS high watermark out from the contents of the /proc/pid/status file in procfs. The
     * returned value is in bytes.
     * Parses RSS high-water mark out from the contents of the /proc/pid/status file in procfs. The
     * returned value is in kilobytes.
     */
    @VisibleForTesting
    static long parseVmHWMFromProcfs(String procStatusContents) {
    static int parseVmHWMFromProcfs(String procStatusContents) {
        if (procStatusContents == null || procStatusContents.isEmpty()) {
            return 0;
        }
        // Convert value read from /proc/pid/status from kilobytes to bytes.
        return tryParseLong(RSS_HIGH_WATERMARK_IN_KILOBYTES, procStatusContents)
                * BYTES_IN_KILOBYTE;
        return (int) tryParseLong(RSS_HIGH_WATERMARK_IN_KILOBYTES, procStatusContents);
    }


+12 −5
Original line number Diff line number Diff line
@@ -1237,15 +1237,17 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
                LocalServices.getService(
                        ActivityManagerInternal.class).getMemoryStateForProcesses();
        for (ProcessMemoryState managedProcess : managedProcessList) {
            final long rssHighWaterMarkInBytes =
            final int rssHighWaterMarkInKilobytes =
                    readRssHighWaterMarkFromProcfs(managedProcess.pid);
            if (rssHighWaterMarkInBytes == 0) {
            if (rssHighWaterMarkInKilobytes == 0) {
                continue;
            }
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
            e.writeInt(managedProcess.uid);
            e.writeString(managedProcess.processName);
            e.writeLong(rssHighWaterMarkInBytes);
            // RSS high-water mark in bytes.
            e.writeLong((long) rssHighWaterMarkInKilobytes * 1024L);
            e.writeInt(rssHighWaterMarkInKilobytes);
            pulledData.add(e);
        }
        int[] pids = getPidsForCommands(MEMORY_INTERESTING_NATIVE_PROCESSES);
@@ -1253,11 +1255,16 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            final int pid = pids[i];
            final int uid = getUidForPid(pid);
            final String processName = readCmdlineFromProcfs(pid);
            final long rssHighWaterMarkInBytes = readRssHighWaterMarkFromProcfs(pid);
            final int rssHighWaterMarkInKilobytes = readRssHighWaterMarkFromProcfs(pid);
            if (rssHighWaterMarkInKilobytes == 0) {
                continue;
            }
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
            e.writeInt(uid);
            e.writeString(processName);
            e.writeLong(rssHighWaterMarkInBytes);
            // RSS high-water mark in bytes.
            e.writeLong((long) rssHighWaterMarkInKilobytes * 1024L);
            e.writeInt(rssHighWaterMarkInKilobytes);
            pulledData.add(e);
        }
        // Invoke rss_hwm_reset binary to reset RSS HWM counters for all processes.
+1 −1
Original line number Diff line number Diff line
@@ -302,7 +302,7 @@ public class MemoryStatUtilTest {

    @Test
    public void testParseVmHWMFromProcfs_parsesCorrectValue() {
        assertEquals(137668, parseVmHWMFromProcfs(PROC_STATUS_CONTENTS) / BYTES_IN_KILOBYTE);
        assertEquals(137668, parseVmHWMFromProcfs(PROC_STATUS_CONTENTS));
    }

    @Test