traced_probes: Fix bug with instances_

R: primiano@google.com
Bug: 74067426
Change-Id: Idf21e0bc302a2d36aa1d0e6d4d6ab2d60d7c676f
diff --git a/src/traced/probes/probes_producer.cc b/src/traced/probes/probes_producer.cc
index 77d1c02..663ca21 100644
--- a/src/traced/probes/probes_producer.cc
+++ b/src/traced/probes/probes_producer.cc
@@ -93,13 +93,12 @@
 void ProbesProducer::CreateDataSourceInstance(
     DataSourceInstanceID id,
     const DataSourceConfig& source_config) {
-  instances_[id] = source_config.name();
   if (source_config.name() == kFtraceSourceName) {
     CreateFtraceDataSourceInstance(id, source_config);
-  } else if (source_config.name() == kProcessStatsSourceName) {
-    CreateProcessStatsDataSourceInstance(source_config);
   } else if (source_config.name() == kInodeFileMapSourceName) {
     CreateInodeFileMapDataSourceInstance(id, source_config);
+  } else if (source_config.name() == kProcessStatsSourceName) {
+    CreateProcessStatsDataSourceInstance(id, source_config);
   } else {
     PERFETTO_ELOG("Data source name: %s not recognised.",
                   source_config.name().c_str());
@@ -171,7 +170,10 @@
 }
 
 void ProbesProducer::CreateProcessStatsDataSourceInstance(
+    DataSourceInstanceID id,
     const DataSourceConfig& source_config) {
+  PERFETTO_DCHECK(process_stats_sources_.count(id) == 0);
+  process_stats_sources_.insert(id);
   auto trace_writer = endpoint_->CreateTraceWriter(
       static_cast<BufferID>(source_config.target_buffer()));
   procfs_utils::ProcessMap processes;
@@ -243,17 +245,13 @@
 
 void ProbesProducer::TearDownDataSourceInstance(DataSourceInstanceID id) {
   PERFETTO_LOG("Producer stop (id=%" PRIu64 ")", id);
-  PERFETTO_DCHECK(instances_.count(id));
-  if (instances_[id] == kFtraceSourceName) {
-    size_t removed = delegates_.erase(id);
-    PERFETTO_DCHECK(removed == 1);
-    // Might return 0 if trace_duration_ms == 0.
-    watchdogs_.erase(id);
-  } else if (instances_[id] == kInodeFileMapSourceName) {
-    size_t removed = file_map_sources_.erase(id);
-    PERFETTO_DCHECK(removed == 1);
-    watchdogs_.erase(id);
-  }
+  // |id| could be the id of any of the datasources we handle:
+  PERFETTO_DCHECK((delegates_.count(id) + process_stats_sources_.count(id) +
+                   file_map_sources_.count(id)) == 1);
+  delegates_.erase(id);
+  process_stats_sources_.erase(id);
+  file_map_sources_.erase(id);
+  watchdogs_.erase(id);
 }
 
 void ProbesProducer::ConnectWithRetries(const char* socket_name,