perfetto.rc: Support boot tracing in android

This introduces a simple way to automatically record a perfetto trace on
boot. It's going to be expanded in the future to support early kernel
ftrace events.

The trace is recorded only if persist.debug.perfetto.boottrace is 1
at boot.

Bug: 201387964
Change-Id: I65dcb1b3b56297ae8a3b588b439b24a99efc4a61
diff --git a/perfetto.rc b/perfetto.rc
index 55ff78f..5be7758 100644
--- a/perfetto.rc
+++ b/perfetto.rc
@@ -90,3 +90,34 @@
 
 on property:persist.mm_events.enabled=false
     stop mm_events
+
+#############################################################################
+#  perfetto_boottrace - Starts a perfetto trace on boot
+#############################################################################
+#
+# There are two separate actions (a trigger action and a start action) to make
+# sure that perfetto_boottrace is started only once on boot (otherwise, whenever
+# persist.debug.perfetto.boottrace=1 is set, perfetto_boottrace would start
+# immediately).
+#
+# persist.debug.perfetto.boottrace=1 can be manually set after boot (to record
+# a trace on the next reboot) and we don't want to immediately start a trace
+# when setting the debug property. So we turn "ro.persistent_properties.ready"
+# into a trigger, and then check whether we should start tracing when the
+# trigger fires.
+on perfetto_maybe_boottrace && property:persist.debug.perfetto.boottrace=1 && property:persist.traced.enable=1
+    setprop persist.debug.perfetto.boottrace ""
+    rm /data/misc/perfetto-traces/boottrace.perfetto-trace
+    # Set by traced after listen()ing on the consumer socket. Without this,
+    # perfetto could try to connect to traced before traced is ready to listen.
+    wait_for_prop sys.trace.traced_started 1
+    start perfetto_boottrace
+
+on property:ro.persistent_properties.ready=true
+    trigger perfetto_maybe_boottrace
+
+service perfetto_boottrace /system/bin/perfetto -c /data/misc/perfetto-configs/boottrace.pbtxt --txt -o /data/misc/perfetto-traces/boottrace.perfetto-trace
+    disabled
+    oneshot
+    user shell
+    group nobody