From 239b680a6e6f2775868c16cc7cd66bedfa5a7f13 Mon Sep 17 00:00:00 2001 From: Rafal Slawik Date: Mon, 9 Sep 2019 18:46:01 +0100 Subject: [PATCH] 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 --- cmds/statsd/src/atoms.proto | 6 +++++- .../com/android/server/am/MemoryStatUtil.java | 14 ++++++-------- .../server/stats/StatsCompanionService.java | 17 ++++++++++++----- .../android/server/am/MemoryStatUtilTest.java | 2 +- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto index ae751ce6d7abe..fe3c3d5da6df4 100644 --- a/cmds/statsd/src/atoms.proto +++ b/cmds/statsd/src/atoms.proto @@ -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; } /* diff --git a/services/core/java/com/android/server/am/MemoryStatUtil.java b/services/core/java/com/android/server/am/MemoryStatUtil.java index 95eb2c69024e6..6666cf42ab5d8 100644 --- a/services/core/java/com/android/server/am/MemoryStatUtil.java +++ b/services/core/java/com/android/server/am/MemoryStatUtil.java @@ -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); } diff --git a/services/core/java/com/android/server/stats/StatsCompanionService.java b/services/core/java/com/android/server/stats/StatsCompanionService.java index c76bbb05a3595..e176480f685d1 100644 --- a/services/core/java/com/android/server/stats/StatsCompanionService.java +++ b/services/core/java/com/android/server/stats/StatsCompanionService.java @@ -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. diff --git a/services/tests/servicestests/src/com/android/server/am/MemoryStatUtilTest.java b/services/tests/servicestests/src/com/android/server/am/MemoryStatUtilTest.java index 6678a7833f9a5..5ab44a8c2c3b1 100644 --- a/services/tests/servicestests/src/com/android/server/am/MemoryStatUtilTest.java +++ b/services/tests/servicestests/src/com/android/server/am/MemoryStatUtilTest.java @@ -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