Merge "Improve write_vm_creation_stats for expanded VmCreationRequested"
diff --git a/TEST_MAPPING b/TEST_MAPPING
index c967473..ea81bc4 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit": [
+  "avf-presubmit": [
     {
       "name": "MicrodroidHostTestCases"
     },
@@ -19,7 +19,7 @@
       "name": "compos_key_tests"
     }
   ],
-  "postsubmit": [
+  "avf-postsubmit": [
     {
       "name": "odsign_e2e_tests_full"
     },
diff --git a/apkdmverity/TEST_MAPPING b/apkdmverity/TEST_MAPPING
index 997b3f9..89750a8 100644
--- a/apkdmverity/TEST_MAPPING
+++ b/apkdmverity/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "postsubmit" : [
+  "avf-postsubmit" : [
     {
       "name" : "apkdmverity.test"
     }
diff --git a/authfs/TEST_MAPPING b/authfs/TEST_MAPPING
index 14f1824..3c84b76 100644
--- a/authfs/TEST_MAPPING
+++ b/authfs/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit": [
+  "avf-presubmit": [
     {
       "name": "authfs_device_test_src_lib"
     },
diff --git a/avmd/Android.bp b/avmd/Android.bp
index b09bed5..dc6a896 100644
--- a/avmd/Android.bp
+++ b/avmd/Android.bp
@@ -36,15 +36,11 @@
     ],
 }
 
-rust_test_host {
+rust_test {
     name: "avmdtool_tests",
     srcs: ["tests/*_test.rs"],
     test_suites: ["general-tests"],
-    prefer_rlib: true,
-    data: ["tests/data/*"],
+    compile_multilib: "first",
     data_bins: ["avmdtool"],
-    data_libs: [
-        "libcrypto",
-        "libz",
-    ],
+    data: ["tests/data/*"],
 }
diff --git a/avmd/TEST_MAPPING b/avmd/TEST_MAPPING
index dd687fe..ea58edb 100644
--- a/avmd/TEST_MAPPING
+++ b/avmd/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit" : [
+  "avf-presubmit" : [
     {
       "name" : "avmdtool_tests"
     }
diff --git a/libs/apkverify/TEST_MAPPING b/libs/apkverify/TEST_MAPPING
index 9248716..7224fc7 100644
--- a/libs/apkverify/TEST_MAPPING
+++ b/libs/apkverify/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit" : [
+  "avf-presubmit" : [
     {
       "name" : "libapkverify.test"
     },
diff --git a/libs/vbmeta/TEST_MAPPING b/libs/vbmeta/TEST_MAPPING
index adfcf89..e42aea0 100644
--- a/libs/vbmeta/TEST_MAPPING
+++ b/libs/vbmeta/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit" : [
+  "avf-presubmit" : [
     {
       "name" : "libvbmeta_rust.test"
     }
diff --git a/microdroid/README.md b/microdroid/README.md
index 6e5d709..fef71ce 100644
--- a/microdroid/README.md
+++ b/microdroid/README.md
@@ -162,8 +162,9 @@
 ## ADB
 
 On userdebug builds, you can have an adb connection to microdroid. To do so,
-first, add the `--debug=full` flag to the `/apex/com.android.virt/bin/vm
-run-app` command, and then
+first, delete `$TEST_ROOT/instance.img`; this is because changing debug settings
+requires a new instance. Then add the `--debug=full` flag to the
+`/apex/com.android.virt/bin/vm run-app` command, and then
 
 ```sh
 adb forward tcp:8000 vsock:$CID:5555
diff --git a/rialto/TEST_MAPPING b/rialto/TEST_MAPPING
index 8e3c085..88f616e 100644
--- a/rialto/TEST_MAPPING
+++ b/rialto/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit": [
+  "avf-presubmit": [
     {
       "name": "rialto_test"
     }
diff --git a/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java b/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
index fb92467..76bfafb 100644
--- a/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
@@ -36,6 +36,7 @@
 import com.android.tradefed.result.TestDescription;
 import com.android.tradefed.result.TestResult;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestMetrics;
 import com.android.tradefed.testtype.junit4.DeviceTestRunOptions;
 import com.android.tradefed.util.CommandResult;
 import com.android.tradefed.util.FileUtil;
@@ -58,6 +59,7 @@
 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;
@@ -80,6 +82,7 @@
 
     @Rule public TestLogData mTestLogs = new TestLogData();
     @Rule public TestName mTestName = new TestName();
+    @Rule public TestMetrics mMetrics = new TestMetrics();
 
     private int minMemorySize() throws DeviceNotAvailableException {
         CommandRunner android = new CommandRunner(getDevice());
@@ -545,6 +548,92 @@
         shutdownMicrodroid(getDevice(), cid);
     }
 
+    private static class ProcessInfo {
+        public final String mName;
+        public final int mPid;
+
+        ProcessInfo(String name, int pid) {
+            mName = name;
+            mPid = pid;
+        }
+    }
+
+    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;
+    }
+
+    private String skipFirstLine(String str) {
+        int index = str.indexOf("\n");
+        return (index < 0) ? "" : str.substring(index + 1);
+    }
+
+    private List<ProcessInfo> getRunningProcessesList() {
+        List<ProcessInfo> list = new ArrayList<ProcessInfo>();
+        skipFirstLine(runOnMicrodroid("ps", "-Ao", "PID,NAME")).lines().forEach(ps -> {
+            // Each line is '  <pid> <name>'.
+            ps = ps.trim();
+            int space = ps.indexOf(" ");
+            list.add(new ProcessInfo(
+                    ps.substring(space + 1),
+                    Integer.parseInt(ps.substring(0, space))));
+        });
+
+        return list;
+    }
+
+    private Map<String, Long> getProcMemInfo() {
+        return parseMemInfo(runOnMicrodroid("cat", "/proc/meminfo"));
+    }
+
+    private Map<String, Long> getProcSmapsRollup(int pid) {
+        String path = "/proc/" + pid + "/smaps_rollup";
+        return  parseMemInfo(skipFirstLine(runOnMicrodroid("cat", path, "||", "true")));
+    }
+
+    @Test
+    public void testMicrodroidRamUsage() throws Exception {
+        final String configPath = "assets/vm_config.json";
+        final String cid =
+                startMicrodroid(
+                        getDevice(),
+                        getBuild(),
+                        APK_NAME,
+                        PACKAGE_NAME,
+                        configPath,
+                        /* debug */ true,
+                        minMemorySize(),
+                        Optional.of(NUM_VCPUS),
+                        Optional.of(CPU_AFFINITY));
+        adbConnectToMicrodroid(getDevice(), cid);
+        waitForBootComplete();
+        rootMicrodroid();
+
+        for (Map.Entry<String, Long> stat : getProcMemInfo().entrySet()) {
+            mMetrics.addTestMetric(
+                    "avf_perf/microdroid/meminfo/" + stat.getKey().toLowerCase(),
+                    stat.getValue().toString());
+        }
+
+        for (ProcessInfo proc : getRunningProcessesList()) {
+            for (Map.Entry<String, Long> stat : getProcSmapsRollup(proc.mPid).entrySet()) {
+                String name = stat.getKey().toLowerCase();
+                mMetrics.addTestMetric(
+                        "avf_perf/microdroid/smaps/" + name + "/" + proc.mName,
+                        stat.getValue().toString());
+            }
+        }
+
+        shutdownMicrodroid(getDevice(), cid);
+    }
+
     @Test
     public void testCustomVirtualMachinePermission()
             throws DeviceNotAvailableException, IOException, JSONException {
diff --git a/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java b/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
index db2ca0f..911efbb 100644
--- a/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
+++ b/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java
@@ -358,9 +358,9 @@
         List<DataItem> rootArrayItems = ((Array) dataItems.get(0)).getDataItems();
         assertThat(rootArrayItems.size()).isAtLeast(2); // Public key and one certificate
         if (mProtectedVm) {
-            // When a true BCC is created, microdroid expects entries for at least: the root public
-            // key, pvmfw, u-boot, u-boot-env, microdroid, app payload and the service process.
-            assertThat(rootArrayItems.size()).isAtLeast(7);
+            // When a true DICE chain is created, microdroid expects entries for: u-boot,
+            // u-boot-env, microdroid, app payload and the service process.
+            assertThat(rootArrayItems.size()).isAtLeast(5);
         }
     }
 
diff --git a/virtualizationservice/TEST_MAPPING b/virtualizationservice/TEST_MAPPING
index 6456a98..8388ff2 100644
--- a/virtualizationservice/TEST_MAPPING
+++ b/virtualizationservice/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit": [
+  "avf-presubmit": [
     {
       "name": "virtualizationservice_device_test"
     }
diff --git a/vmbase/TEST_MAPPING b/vmbase/TEST_MAPPING
index 9b7e4cb..c315b4a 100644
--- a/vmbase/TEST_MAPPING
+++ b/vmbase/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit": [
+  "avf-presubmit": [
     {
       "name": "vmbase_example.integration_test"
     }
diff --git a/zipfuse/TEST_MAPPING b/zipfuse/TEST_MAPPING
index ef398a0..13055f0 100644
--- a/zipfuse/TEST_MAPPING
+++ b/zipfuse/TEST_MAPPING
@@ -1,5 +1,5 @@
 {
-  "presubmit" : [
+  "avf-presubmit" : [
     {
       "name" : "ZipFuseTest"
     }