cts: fix running AnomalyTestCase#testPerfetto multiple times b2b

This CL ensures that running the #testPerfetto test back to back will
not fail because of hitting a guardrail in traced which prevents configs
which don't specify a unique session name from tracing too close
together.

To do this, we need to stop using the explicitly hardcoded bytestring
and instead create the proto bytes programatically so we can introduce
the randomness. This requires adding a dependency on Perfetto's trace
config which is fine as it is already used by the CrystalBall framework
so is well used in Android infra.

(cherry-pick of ag/11913566 needed because this is failing on rvc-dev)

Bug: 157971967
Change-Id: Ic9d94dc527265e51fa523ccfe44cfc1632d51b0c
Merged-In: Ic9d94dc527265e51fa523ccfe44cfc1632d51b0c
diff --git a/hostsidetests/statsd/Android.bp b/hostsidetests/statsd/Android.bp
index 59399ac..283d650 100644
--- a/hostsidetests/statsd/Android.bp
+++ b/hostsidetests/statsd/Android.bp
@@ -29,12 +29,13 @@
         "compatibility-host-util",
         "cts-tradefed",
         "host-libprotobuf-java-full",
+        "perfetto_config-full",
         "platformprotos",
         "tradefed",
-        "truth-prebuilt"
+        "truth-prebuilt",
     ],
     static_libs: [
-        "core_cts_test_resources"
+        "core_cts_test_resources",
     ],
     data: [
         "**/*.pbtxt",
diff --git a/hostsidetests/statsd/src/android/cts/statsd/atom/AtomTestCase.java b/hostsidetests/statsd/src/android/cts/statsd/atom/AtomTestCase.java
index 51e24ea..35c4a4c 100644
--- a/hostsidetests/statsd/src/android/cts/statsd/atom/AtomTestCase.java
+++ b/hostsidetests/statsd/src/android/cts/statsd/atom/AtomTestCase.java
@@ -62,6 +62,10 @@
 import com.google.common.io.Files;
 import com.google.protobuf.ByteString;
 
+import perfetto.protos.PerfettoConfig.DataSourceConfig;
+import perfetto.protos.PerfettoConfig.FtraceConfig;
+import perfetto.protos.PerfettoConfig.TraceConfig;
+
 import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -73,6 +77,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Queue;
+import java.util.Random;
 import java.util.Set;
 import java.util.function.Function;
 import java.util.regex.Matcher;
@@ -181,15 +186,41 @@
     /**
      * Returns a protobuf-encoded perfetto config that enables the kernel
      * ftrace tracer with sched_switch for 10 seconds.
-     * See https://android.googlesource.com/platform/external/perfetto/+/master/docs/trace-config.md
-     * for details on how to generate this.
      */
     protected ByteString getPerfettoConfig() {
-        return ByteString.copyFrom(new byte[] { 0xa, 0x3, 0x8, (byte) 0x80, 0x1, 0x12, 0x23, 0xa,
-                        0x21, 0xa, 0xc, 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x2e, 0x66, 0x74, 0x72, 0x61,
-                        0x63, 0x65, 0x10, 0x0, (byte) 0xa2, 0x6, 0xe, 0xa, 0xc, 0x73, 0x63, 0x68,
-                        0x65, 0x64, 0x5f, 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x18, (byte) 0x90,
-                        0x4e, (byte) 0x98, 0x01, 0x01 });
+        TraceConfig.Builder builder = TraceConfig.newBuilder();
+
+        TraceConfig.BufferConfig buffer = TraceConfig.BufferConfig
+            .newBuilder()
+            .setSizeKb(128)
+            .build();
+        builder.addBuffers(buffer);
+
+        FtraceConfig ftraceConfig = FtraceConfig.newBuilder()
+            .addFtraceEvents("sched/sched_switch")
+            .build();
+        DataSourceConfig dataSourceConfig = DataSourceConfig.newBuilder()
+            .setName("linux.ftrace")
+            .setTargetBuffer(0)
+            .setFtraceConfig(ftraceConfig)
+            .build();
+        TraceConfig.DataSource dataSource = TraceConfig.DataSource
+            .newBuilder()
+            .setConfig(dataSourceConfig)
+            .build();
+        builder.addDataSources(dataSource);
+
+        builder.setDurationMs(10000);
+        builder.setAllowUserBuildTracing(true);
+
+        // To avoid being hit with guardrails firing in multiple test runs back
+        // to back, we set a unique session key for each config.
+        Random random = new Random();
+        StringBuilder sessionNameBuilder = new StringBuilder("statsd-cts-");
+        sessionNameBuilder.append(random.nextInt() & Integer.MAX_VALUE);
+        builder.setUniqueSessionName(sessionNameBuilder.toString());
+
+        return builder.build().toByteString();
     }
 
     /**