Move getProcMemInfo() from MicrodroidTestCase to ProcessUtil.
Bug: 249409434
Test: atest
com.android.microdroid.test.MicrodroidTestCase#testMicrodroidRamUsage
Change-Id: I1550a6dbd25dbe03a7751a37c3c405500caa7207
Change-Id: I918c1f0f4f3634298d7f8b2febcc6f8a1d7b5d18
diff --git a/tests/helper/src/java/com/android/microdroid/test/common/ProcessUtil.java b/tests/helper/src/java/com/android/microdroid/test/common/ProcessUtil.java
index 611a572..d85929d 100644
--- a/tests/helper/src/java/com/android/microdroid/test/common/ProcessUtil.java
+++ b/tests/helper/src/java/com/android/microdroid/test/common/ProcessUtil.java
@@ -36,14 +36,14 @@
public static List<SMapEntry> getProcessSmaps(int pid, Function<String, String> shellExecutor)
throws IOException {
String path = "/proc/" + pid + "/smaps";
- return parseSmaps(shellExecutor.apply("cat " + path + " || true"));
+ return parseMemoryInfo(shellExecutor.apply("cat " + path + " || true"));
}
/** Gets metrics key and values mapping of specified process id */
public static Map<String, Long> getProcessSmapsRollup(
int pid, Function<String, String> shellExecutor) throws IOException {
String path = "/proc/" + pid + "/smaps_rollup";
- List<SMapEntry> entries = parseSmaps(shellExecutor.apply("cat " + path + " || true"));
+ List<SMapEntry> entries = parseMemoryInfo(shellExecutor.apply("cat " + path + " || true"));
if (entries.size() > 1) {
throw new RuntimeException(
"expected at most one entry in smaps_rollup, got " + entries.size());
@@ -54,6 +54,21 @@
return new HashMap<String, Long>();
}
+ /** Gets global memory metrics key and values mapping */
+ public static Map<String, Long> getProcessMemoryMap(
+ Function<String, String> shellExecutor) throws IOException {
+ // The input file of parseMemoryInfo need a header string as the key of output entries.
+ // /proc/meminfo doesn't have this line so add one as the key.
+ String header = "device memory info\n";
+ List<SMapEntry> entries = parseMemoryInfo(header
+ + shellExecutor.apply("cat /proc/meminfo"));
+ if (entries.size() != 1) {
+ throw new RuntimeException(
+ "expected one entry in /proc/meminfo, got " + entries.size());
+ }
+ return entries.get(0).metrics;
+ }
+
/** Gets process id and process name mapping of the device */
public static Map<Integer, String> getProcessMap(Function<String, String> shellExecutor)
throws IOException {
@@ -77,7 +92,7 @@
// To ensures that only one object is created at a time.
private ProcessUtil() {}
- private static List<SMapEntry> parseSmaps(String file) {
+ private static List<SMapEntry> parseMemoryInfo(String file) {
List<SMapEntry> entries = new ArrayList<SMapEntry>();
for (String line : file.split("\n")) {
line = line.trim();
diff --git a/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java b/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
index 7a71254..9dd2a15 100644
--- a/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
@@ -71,7 +71,6 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -684,34 +683,6 @@
shutdownMicrodroid(getDevice(), cid);
}
- /**
- * TODO(b/249409434): to be replaced by ProcessUtil
- *
- * @deprecated use ProcessUtil instead.
- */
- @Deprecated
- private Map<String, Long> parseMemInfo(String file) {
- Map<String, Long> stats = new HashMap<>();
- file.lines().forEach(line -> {
- if (line.endsWith(" kB")) line = line.substring(0, line.length() - 3);
-
- String[] elems = line.split(":");
- assertThat(elems.length).isEqualTo(2);
- stats.put(elems[0].trim(), Long.parseLong(elems[1].trim()));
- });
- return stats;
- }
-
- /**
- * TODO(b/249409434): to be replaced by ProcessUtil
- *
- * @deprecated use ProcessUtil instead.
- */
- @Deprecated
- private Map<String, Long> getProcMemInfo() {
- return parseMemInfo(runOnMicrodroid("cat", "/proc/meminfo"));
- }
-
@Test
public void testMicrodroidRamUsage() throws Exception {
final String configPath = "assets/vm_config.json";
@@ -729,7 +700,8 @@
waitForBootComplete();
rootMicrodroid();
- for (Map.Entry<String, Long> stat : getProcMemInfo().entrySet()) {
+ for (Map.Entry<String, Long> stat :
+ ProcessUtil.getProcessMemoryMap(cmd -> runOnMicrodroid(cmd)).entrySet()) {
mMetrics.addTestMetric(
mMetricPrefix + "meminfo/" + stat.getKey().toLowerCase(),
stat.getValue().toString());