Merge "Add support for initial_display_state to the trace processor."
diff --git a/src/trace_processor/importers/proto/args_table_utils.cc b/src/trace_processor/importers/proto/args_table_utils.cc
index 508f5bf..b1dd2e0 100644
--- a/src/trace_processor/importers/proto/args_table_utils.cc
+++ b/src/trace_processor/importers/proto/args_table_utils.cc
@@ -38,6 +38,44 @@
                                         proto_descriptor_array_size);
 }
 
+util::Status ProtoToArgsTable::InternProtoFieldsIntoArgsTable(
+    const protozero::ConstBytes& cb,
+    const std::string& type,
+    const std::vector<uint16_t>& fields,
+    ArgsTracker::BoundInserter* inserter,
+    PacketSequenceStateGeneration* sequence_state) {
+  auto idx = pool_.FindDescriptorIdx(type);
+  if (!idx) {
+    return util::Status("Failed to find proto descriptor");
+  }
+
+  auto descriptor = pool_.descriptors()[*idx];
+
+  protozero::ProtoDecoder decoder(cb);
+  for (protozero::Field f = decoder.ReadField(); f.valid();
+       f = decoder.ReadField()) {
+    auto it = std::find(fields.begin(), fields.end(), f.id());
+    if (it == fields.end()) {
+      continue;
+    }
+
+    auto field_idx = descriptor.FindFieldIdxByTag(*it);
+    if (!field_idx) {
+      return util::Status("Failed to find proto descriptor");
+    }
+    auto field = descriptor.fields()[*field_idx];
+    auto status =
+        InternProtoIntoArgsTable(f.as_bytes(), field.resolved_type_name(),
+                                 inserter, sequence_state, field.name());
+
+    if (!status.ok()) {
+      return status;
+    }
+  }
+
+  return util::OkStatus();
+}
+
 util::Status ProtoToArgsTable::InternProtoIntoArgsTable(
     const protozero::ConstBytes& cb,
     const std::string& type,
diff --git a/src/trace_processor/importers/proto/args_table_utils.h b/src/trace_processor/importers/proto/args_table_utils.h
index 05f158c..c103a1f 100644
--- a/src/trace_processor/importers/proto/args_table_utils.h
+++ b/src/trace_processor/importers/proto/args_table_utils.h
@@ -136,6 +136,14 @@
       PacketSequenceStateGeneration* sequence_state,
       const std::string& key_prefix);
 
+  // Parse several fields with ids given in |fields| using reflection.
+  util::Status InternProtoFieldsIntoArgsTable(
+      const protozero::ConstBytes& cb,
+      const std::string& type,
+      const std::vector<uint16_t>& fields,
+      ArgsTracker::BoundInserter* inserter,
+      PacketSequenceStateGeneration* sequence_state);
+
   // Installs an override for the field at the specified path. We will invoke
   // |parsing_override| when the field is encountered.
   //
diff --git a/src/trace_processor/importers/proto/system_probes_module.cc b/src/trace_processor/importers/proto/system_probes_module.cc
index 1a9af08..fde63fc 100644
--- a/src/trace_processor/importers/proto/system_probes_module.cc
+++ b/src/trace_processor/importers/proto/system_probes_module.cc
@@ -35,6 +35,20 @@
   RegisterForField(TracePacket::kCpuInfoFieldNumber, context);
 }
 
+ModuleResult SystemProbesModule::TokenizePacket(
+    const protos::pbzero::TracePacket::Decoder& decoder,
+    TraceBlobView*,
+    int64_t,
+    PacketSequenceState*,
+    uint32_t field_id) {
+  switch (field_id) {
+    case TracePacket::kSystemInfoFieldNumber:
+      parser_.ParseSystemInfo(decoder.system_info());
+      return ModuleResult::Handled();
+  }
+  return ModuleResult::Ignored();
+}
+
 void SystemProbesModule::ParsePacket(const TracePacket::Decoder& decoder,
                                      const TimestampedTracePiece& ttp,
                                      uint32_t field_id) {
@@ -48,9 +62,6 @@
     case TracePacket::kSysStatsFieldNumber:
       parser_.ParseSysStats(ttp.timestamp, decoder.sys_stats());
       return;
-    case TracePacket::kSystemInfoFieldNumber:
-      parser_.ParseSystemInfo(decoder.system_info());
-      return;
     case TracePacket::kCpuInfoFieldNumber:
       parser_.ParseCpuInfo(decoder.cpu_info());
       return;
diff --git a/src/trace_processor/importers/proto/system_probes_module.h b/src/trace_processor/importers/proto/system_probes_module.h
index 438eabe..8fcd94b 100644
--- a/src/trace_processor/importers/proto/system_probes_module.h
+++ b/src/trace_processor/importers/proto/system_probes_module.h
@@ -30,6 +30,12 @@
  public:
   explicit SystemProbesModule(TraceProcessorContext* context);
 
+  ModuleResult TokenizePacket(const protos::pbzero::TracePacket::Decoder&,
+                              TraceBlobView* packet,
+                              int64_t packet_timestamp,
+                              PacketSequenceState*,
+                              uint32_t field_id) override;
+
   void ParsePacket(const protos::pbzero::TracePacket::Decoder& decoder,
                    const TimestampedTracePiece& ttp,
                    uint32_t field_id) override;
diff --git a/src/trace_processor/importers/proto/track_event_parser.cc b/src/trace_processor/importers/proto/track_event_parser.cc
index 5a098d8..7bef66f 100644
--- a/src/trace_processor/importers/proto/track_event_parser.cc
+++ b/src/trace_processor/importers/proto/track_event_parser.cc
@@ -839,24 +839,10 @@
     if (event_.has_log_message()) {
       log_errors(ParseLogMessage(event_.log_message(), inserter));
     }
-    if (event_.has_cc_scheduler_state()) {
-      ParseCcScheduler(event_.cc_scheduler_state(), inserter);
-    }
-    if (event_.has_chrome_user_event()) {
-      ParseChromeUserEvent(event_.chrome_user_event(), inserter);
-    }
-    if (event_.has_chrome_legacy_ipc()) {
-      ParseChromeLegacyIpc(event_.chrome_legacy_ipc(), inserter);
-    }
-    if (event_.has_chrome_keyed_service()) {
-      ParseChromeKeyedService(event_.chrome_keyed_service(), inserter);
-    }
-    if (event_.has_chrome_histogram_sample()) {
-      ParseChromeHistogramSample(event_.chrome_histogram_sample(), inserter);
-    }
-    if (event_.has_chrome_latency_info()) {
-      ParseChromeLatencyInfo(event_.chrome_latency_info(), inserter);
-    }
+
+    log_errors(parser_->proto_to_args_.InternProtoFieldsIntoArgsTable(
+        blob_, ".perfetto.protos.TrackEvent", parser_->reflect_fields_,
+        inserter, sequence_state_));
 
     if (legacy_passthrough_utid_) {
       inserter->AddArg(parser_->legacy_event_passthrough_utid_id_,
@@ -1080,48 +1066,6 @@
     return util::OkStatus();
   }
 
-  void ParseCcScheduler(ConstBytes cc, BoundInserter* outer_inserter) {
-    parser_->proto_to_args_.InternProtoIntoArgsTable(
-        cc, ".perfetto.protos.ChromeCompositorSchedulerState", outer_inserter,
-        sequence_state_,
-        /* prefix= */ "cc_scheduler_state");
-  }
-
-  void ParseChromeUserEvent(protozero::ConstBytes chrome_user_event,
-                            BoundInserter* inserter) {
-    parser_->proto_to_args_.InternProtoIntoArgsTable(
-        chrome_user_event, ".perfetto.protos.ChromeUserEvent", inserter,
-        sequence_state_, "user_event");
-  }
-
-  void ParseChromeLegacyIpc(protozero::ConstBytes chrome_legacy_ipc,
-                            BoundInserter* inserter) {
-    parser_->proto_to_args_.InternProtoIntoArgsTable(
-        chrome_legacy_ipc, ".perfetto.protos.ChromeLegacyIpc", inserter,
-        sequence_state_, "legacy_ipc");
-  }
-
-  void ParseChromeKeyedService(protozero::ConstBytes chrome_keyed_service,
-                               BoundInserter* inserter) {
-    parser_->proto_to_args_.InternProtoIntoArgsTable(
-        chrome_keyed_service, ".perfetto.protos.ChromeKeyedService", inserter,
-        sequence_state_, "keyed_service");
-  }
-
-  void ParseChromeLatencyInfo(protozero::ConstBytes chrome_latency_info,
-                              BoundInserter* inserter) {
-    parser_->proto_to_args_.InternProtoIntoArgsTable(
-        chrome_latency_info, ".perfetto.protos.ChromeLatencyInfo", inserter,
-        sequence_state_, "latency_info");
-  }
-
-  void ParseChromeHistogramSample(protozero::ConstBytes chrome_histogram_sample,
-                                  BoundInserter* inserter) {
-    parser_->proto_to_args_.InternProtoIntoArgsTable(
-        chrome_histogram_sample, ".perfetto.protos.ChromeHistogramSample",
-        inserter, sequence_state_, "histogram_sample");
-  }
-
   TraceProcessorContext* context_;
   TraceStorage* storage_;
   TrackEventParser* parser_;
@@ -1301,6 +1245,10 @@
             "begin_frame_observer_state.last_begin_frame_args", state, field,
             inserter);
       });
+
+  for (uint16_t index : kReflectFields) {
+    reflect_fields_.push_back(index);
+  }
 }
 
 void TrackEventParser::ParseTrackDescriptor(
diff --git a/src/trace_processor/importers/proto/track_event_parser.h b/src/trace_processor/importers/proto/track_event_parser.h
index ebae388..b3abc4c 100644
--- a/src/trace_processor/importers/proto/track_event_parser.h
+++ b/src/trace_processor/importers/proto/track_event_parser.h
@@ -35,6 +35,12 @@
 
 namespace trace_processor {
 
+// Field numbers to be added to args table automatically via reflection
+//
+// TODO(ddrone): replace with a predicate on field id to import new fields
+// automatically
+static constexpr uint16_t kReflectFields[] = {24, 25, 26, 27, 28, 29};
+
 class PacketSequenceStateGeneration;
 class TraceProcessorContext;
 
@@ -94,6 +100,8 @@
   std::array<StringId, 9> chrome_process_name_ids_;
   std::array<StringId, 14> chrome_thread_name_ids_;
   std::array<StringId, 4> counter_unit_ids_;
+
+  std::vector<uint16_t> reflect_fields_;
 };
 
 }  // namespace trace_processor
diff --git a/test/trace_processor/track_event_typed_args_args.out b/test/trace_processor/track_event_typed_args_args.out
index b61f865..e6d06f7 100644
--- a/test/trace_processor/track_event_typed_args_args.out
+++ b/test/trace_processor/track_event_typed_args_args.out
@@ -1,10 +1,10 @@
 "arg_set_id","flat_key","key","int_value","string_value"
-1,"user_event.action","user_event.action","[NULL]","NewTab"
-2,"legacy_ipc.message_class","legacy_ipc.message_class","[NULL]","CLASS_AUTOMATION"
-2,"legacy_ipc.message_line","legacy_ipc.message_line",10,"[NULL]"
-3,"keyed_service.name","keyed_service.name","[NULL]","MediaRouter"
-4,"latency_info.component_info.component_type","latency_info.component_info[0].component_type","[NULL]","COMPONENT_INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL"
-4,"latency_info.component_info.time_us","latency_info.component_info[0].time_us",1201,"[NULL]"
-4,"latency_info.component_info.time_us","latency_info.component_info[1].time_us",928310,"[NULL]"
-4,"latency_info.is_coalesced","latency_info.is_coalesced",1,"[NULL]"
-4,"latency_info.trace_id","latency_info.trace_id",7,"[NULL]"
+1,"chrome_user_event.action","chrome_user_event.action","[NULL]","NewTab"
+2,"chrome_legacy_ipc.message_class","chrome_legacy_ipc.message_class","[NULL]","CLASS_AUTOMATION"
+2,"chrome_legacy_ipc.message_line","chrome_legacy_ipc.message_line",10,"[NULL]"
+3,"chrome_keyed_service.name","chrome_keyed_service.name","[NULL]","MediaRouter"
+4,"chrome_latency_info.component_info.component_type","chrome_latency_info.component_info[0].component_type","[NULL]","COMPONENT_INPUT_EVENT_LATENCY_FIRST_SCROLL_UPDATE_ORIGINAL"
+4,"chrome_latency_info.component_info.time_us","chrome_latency_info.component_info[0].time_us",1201,"[NULL]"
+4,"chrome_latency_info.component_info.time_us","chrome_latency_info.component_info[1].time_us",928310,"[NULL]"
+4,"chrome_latency_info.is_coalesced","chrome_latency_info.is_coalesced",1,"[NULL]"
+4,"chrome_latency_info.trace_id","chrome_latency_info.trace_id",7,"[NULL]"
diff --git a/ui/src/controller/trace_controller.ts b/ui/src/controller/trace_controller.ts
index a480b09..a18e05a 100644
--- a/ui/src/controller/trace_controller.ts
+++ b/ui/src/controller/trace_controller.ts
@@ -603,8 +603,8 @@
           tid,
           upid,
           pid,
-          ifnull(thread_track.name, thread.name) as threadName,
-          ifnull(process_track.name, process.name) as processName,
+          thread.name as threadName,
+          process.name as processName,
           total_dur as totalDur,
           ifnull(has_sched, false) as hasSched
         from
@@ -612,9 +612,7 @@
           left join (select utid, count(1), true as has_sched
               from sched group by utid
           ) using(utid)
-          left join process_track using(upid)
           left join process using(upid)
-          left join thread_track using(utid)
           left join (select upid, sum(dur) as total_dur
               from sched join thread using(utid)
               group by upid