statsd: Pass alert and config id to Perfetto

When triggering a Perfetto trace, pass the alert and config id to the
perfetto command line tool to record them into the trace. This lets us
correlate Perfetto traces with statsd alerts.

Bug: 73627502
Test: Manual
Change-Id: I301ee5e5e8bdb83d08e8d55b994c15a6541a92f2
Merged-In: I301ee5e5e8bdb83d08e8d55b994c15a6541a92f2
(cherry picked from commit 209a5915dcfe030912dda57df4fb6390385c7de3)
diff --git a/cmds/statsd/src/anomaly/subscriber_util.cpp b/cmds/statsd/src/anomaly/subscriber_util.cpp
index e796d19..3f69a2c 100644
--- a/cmds/statsd/src/anomaly/subscriber_util.cpp
+++ b/cmds/statsd/src/anomaly/subscriber_util.cpp
@@ -55,8 +55,9 @@
                 }
                 break;
             case Subscription::SubscriberInformationCase::kPerfettoDetails:
-                if (!CollectPerfettoTraceAndUploadToDropbox(subscription.perfetto_details())) {
-                    ALOGW("Failed to generate prefetto traces.");
+                if (!CollectPerfettoTraceAndUploadToDropbox(subscription.perfetto_details(),
+                                                            rule_id, configKey)) {
+                    ALOGW("Failed to generate perfetto traces.");
                 }
                 break;
             case Subscription::SubscriberInformationCase::kBroadcastSubscriberDetails:
diff --git a/cmds/statsd/src/external/Perfetto.cpp b/cmds/statsd/src/external/Perfetto.cpp
index b09d373..0554483 100644
--- a/cmds/statsd/src/external/Perfetto.cpp
+++ b/cmds/statsd/src/external/Perfetto.cpp
@@ -15,6 +15,7 @@
  */
 
 #define DEBUG false  // STOPSHIP if true
+#include "config/ConfigKey.h"
 #include "Log.h"
 
 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"  // Alert
@@ -22,6 +23,7 @@
 #include <android-base/unique_fd.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <inttypes.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -36,7 +38,9 @@
 namespace os {
 namespace statsd {
 
-bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config) {
+bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config,
+                                            int64_t alert_id,
+                                            const ConfigKey& configKey) {
     VLOG("Starting trace collection through perfetto");
 
     if (!config.has_trace_config()) {
@@ -44,6 +48,13 @@
         return false;
     }
 
+    char alertId[20];
+    char configId[20];
+    char configUid[20];
+    snprintf(alertId, sizeof(alertId), "%" PRId64, alert_id);
+    snprintf(configId, sizeof(configId), "%" PRId64, configKey.GetId());
+    snprintf(configUid, sizeof(configUid), "%d", configKey.GetUid());
+
     android::base::unique_fd readPipe;
     android::base::unique_fd writePipe;
     if (!android::base::Pipe(&readPipe, &writePipe)) {
@@ -82,7 +93,8 @@
         }
 
         execl("/system/bin/perfetto", "perfetto", "--background", "--config", "-", "--dropbox",
-              kDropboxTag, nullptr);
+              kDropboxTag, "--alert-id", alertId, "--config-id", configId, "--config-uid",
+              configUid, nullptr);
 
         // execl() doesn't return in case of success, if we get here something
         // failed.
diff --git a/cmds/statsd/src/external/Perfetto.h b/cmds/statsd/src/external/Perfetto.h
index 2a5679c..1e7f728 100644
--- a/cmds/statsd/src/external/Perfetto.h
+++ b/cmds/statsd/src/external/Perfetto.h
@@ -24,13 +24,16 @@
 namespace os {
 namespace statsd {
 
+class ConfigKey;
 class PerfettoDetails;  // Declared in statsd_config.pb.h
 
 // Starts the collection of a Perfetto trace with the given |config|.
 // The trace is uploaded to Dropbox by the perfetto cmdline util once done.
 // This method returns immediately after passing the config and does NOT wait
 // for the full duration of the trace.
-bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config);
+bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config,
+                                            int64_t alert_id,
+                                            const ConfigKey& configKey);
 
 }  // namespace statsd
 }  // namespace os