Introduce support for deferred start (fast triggering)

This CL introduces new Setup methods to both the
producer and consumer API. The API changes are designed
to be backwards compatible with the prior versions of
Android.

From the producer viewpoint: each data source now sees a
SetupDataSource() method before the StartDataSource() method.
The data source instance id and config is identical. This
gives the data source a chance to setup itself before actually
starting.

From the consumer viewpoint:
A new "deferred_start" field is added to the TraceConfig.
When deferred_start is true, the EnableTracing method
brings up all the tracing harness and calls the
SetupDataSource() methods on the producers, but doesn't
start them. Data sources are started only when calling
StartDataSource().

Note that in this CL, the ftrace data source doesn't
deal yet with the split Setup/Start phases and starts
directly upon creation. This will be fixed in a separate
CL.

Bug: 116547201
Change-Id: I2ab22c7f8ee2388f8158df733dddeef9dc9c0143
diff --git a/src/traced/probes/probes_producer.cc b/src/traced/probes/probes_producer.cc
index 5b37165..6a56c71 100644
--- a/src/traced/probes/probes_producer.cc
+++ b/src/traced/probes/probes_producer.cc
@@ -129,8 +129,10 @@
   ConnectWithRetries(socket_name, task_runner);
 }
 
-void ProbesProducer::StartDataSource(DataSourceInstanceID instance_id,
+void ProbesProducer::SetupDataSource(DataSourceInstanceID instance_id,
                                      const DataSourceConfig& config) {
+  PERFETTO_DLOG("SetupDataSource(id=%" PRIu64 ", name=%s)", instance_id,
+                config.name().c_str());
   PERFETTO_DCHECK(data_sources_.count(instance_id) == 0);
   TracingSessionID session_id = config.tracing_session_id();
   PERFETTO_CHECK(session_id > 0);
@@ -153,12 +155,27 @@
 
   session_data_sources_.emplace(session_id, data_source.get());
   data_sources_[instance_id] = std::move(data_source);
+}
 
+void ProbesProducer::StartDataSource(DataSourceInstanceID instance_id,
+                                     const DataSourceConfig& config) {
+  PERFETTO_DLOG("StartDataSource(id=%" PRIu64 ", name=%s)", instance_id,
+                config.name().c_str());
+  auto it = data_sources_.find(instance_id);
+  if (it == data_sources_.end()) {
+    PERFETTO_ELOG("Data source id=%" PRIu64 " not found", instance_id);
+    return;
+  }
+  ProbesDataSource* data_source = it->second.get();
+  if (data_source->started)
+    return;
   if (config.trace_duration_ms() != 0) {
     uint32_t timeout = 5000 + 2 * config.trace_duration_ms();
     watchdogs_.emplace(
         instance_id, base::Watchdog::GetInstance()->CreateFatalTimer(timeout));
   }
+  data_source->started = true;
+  data_source->Start();
 }
 
 std::unique_ptr<ProbesDataSource> ProbesProducer::CreateFtraceDataSource(
@@ -185,7 +202,7 @@
     ftrace_->ClearTrace();
   }
 
-  PERFETTO_LOG("Ftrace start (id=%" PRIu64 ", target_buf=%" PRIu32 ")", id,
+  PERFETTO_LOG("Ftrace setup (id=%" PRIu64 ", target_buf=%" PRIu32 ")", id,
                config.target_buffer());
   const BufferID buffer_id = static_cast<BufferID>(config.target_buffer());
   std::unique_ptr<FtraceDataSource> data_source(new FtraceDataSource(
@@ -204,7 +221,7 @@
     TracingSessionID session_id,
     DataSourceInstanceID id,
     DataSourceConfig source_config) {
-  PERFETTO_LOG("Inode file map start (id=%" PRIu64 ", target_buf=%" PRIu32 ")",
+  PERFETTO_LOG("Inode file map setup (id=%" PRIu64 ", target_buf=%" PRIu32 ")",
                id, source_config.target_buffer());
   auto buffer_id = static_cast<BufferID>(source_config.target_buffer());
   if (system_inodes_.empty())
@@ -223,9 +240,6 @@
   auto data_source =
       std::unique_ptr<ProcessStatsDataSource>(new ProcessStatsDataSource(
           session_id, endpoint_->CreateTraceWriter(buffer_id), config));
-  if (config.process_stats_config().scan_all_processes_on_start()) {
-    data_source->WriteAllProcesses();
-  }
   return std::move(data_source);
 }
 
@@ -268,7 +282,7 @@
                            size_t num_data_sources) {
   for (size_t i = 0; i < num_data_sources; i++) {
     auto it = data_sources_.find(data_source_ids[i]);
-    if (it == data_sources_.end())
+    if (it == data_sources_.end() || !it->second->started)
       continue;
     it->second->Flush();
   }
@@ -288,7 +302,7 @@
   // unordered_multimap guarantees that entries with the same key are contiguous
   // in the iteration.
   for (auto it = session_data_sources_.begin(); /* check below*/; it++) {
-    // If this is the last iteration or this is the session id has changed,
+    // If this is the last iteration or the session id has changed,
     // dispatch the metadata update to the linked data sources, if any.
     if (it == session_data_sources_.end() || it->first != last_session_id) {
       bool has_inodes = metadata && !metadata->inode_and_device.empty();
@@ -307,6 +321,8 @@
       last_session_id = it->first;
     }
     ProbesDataSource* ds = it->second;
+    if (!ds->started)
+      continue;
     switch (ds->type_id) {
       case FtraceDataSource::kTypeId:
         metadata = static_cast<FtraceDataSource*>(ds)->mutable_metadata();