PerfettoHelper: wait until tracing is fully active before returning from the shell invocation

Right now the "perfetto" process starts tracing, backgrounds, and
immediately returns to the original shell. With --background-wait,
"perfetto" waits until the tracing service tells it that all requested
data sources have acknowledged that they've started before returning to
shell. By deferring this return, the start of the test being traced can
be deferred until we know that all data is being captured.

Typically this doesn't make much of a difference. However in this case
we'll be combining it with a change to intentionally do more upfront
work when setting up kernel address symbolisation in the ftrace data
source, to avoid overlapping that work with the test being measured. One
reason to avoid overlap is because kernel symbol table parsing is
cpu-bound, and therefore causes the OS to temporarily raise the CPU
frequencies, which in turn distorts the apparent performance of the test
being measured.

Bug: 235066144
Change-Id: I641de91b2676d817444f2a7d41e06b899db4f3e1
diff --git a/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java b/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java
index 2b8f2bf..2a0732f 100644
--- a/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java
+++ b/libraries/collectors-helper/perfetto/src/com/android/helpers/PerfettoHelper.java
@@ -34,10 +34,12 @@
 public class PerfettoHelper {
 
     private static final String LOG_TAG = PerfettoHelper.class.getSimpleName();
-    // Command to start the perfetto tracing in the background.
-    // perfetto -b -c /data/misc/perfetto-traces/trace_config.pb -o
-    // /data/misc/perfetto-traces/trace_output.pb
-    private static final String PERFETTO_START_CMD = "perfetto --background -c %s%s -o %s";
+    // Command to start the perfetto tracing in the background. The "perfetto" process will wait
+    // until tracing is fully started (i.e. all data sources are active) before backgrounding and
+    // returning from the original shell invocation.
+    //   perfetto --background-wait -c /data/misc/perfetto-traces/trace_config.pb -o
+    //   /data/misc/perfetto-traces/trace_output.pb
+    private static final String PERFETTO_START_CMD = "perfetto --background-wait -c %s%s -o %s";
     private static final String PERFETTO_TMP_OUTPUT_FILE =
             "/data/misc/perfetto-traces/trace_output.pb";
     // Additional arg to indicate that the perfetto config file is text format.
@@ -103,6 +105,12 @@
             if (startOutput != null && !startOutput.isEmpty()) {
                 mPerfettoProcId = Integer.parseInt(startOutput.trim());
             }
+            // TODO(b/235066144): evaluate whether this load-bearing(!) sleep is still necessary.
+            // Setting up tracing with kallsyms symbolisation requires traced_probes to do cpu-bound
+            // address table creation for 500+ ms, which was observed to boost cpu frequencies.
+            // This helper does wait until kallsyms setup is done before proceeding
+            // (--background-wait), but this extra sleep aims to let the device quiesce before the
+            // test being measured is started.
             SystemClock.sleep(1000);
             if(!isTestPerfettoRunning()) {
                 return false;