Trace Processor: cleanup RPC interface

Minor cleanups to the RPC interface for the --httpd feature.
- Makes the RPC vs original TraceProcessor interfaces more
  uniform and code-search friendly.
- Adds a Get/SetCurrentTraceName() method pair.
- Adds the ability to the Rpc class to adopt an existing instance.
  This will be used for trace_processor_shell -D trace_from_cmdline.
- Makes method naming more consistent.
- Moved trace_processor.proto definitions within the
  perfetto.trace_processor namespace

Bug: 143074239
Change-Id: I29e5b6f5216c3a10afbe036c60f758cbe5dfa172
diff --git a/src/trace_processor/trace_processor_shell.cc b/src/trace_processor/trace_processor_shell.cc
index 3745634..f7a2877 100644
--- a/src/trace_processor/trace_processor_shell.cc
+++ b/src/trace_processor/trace_processor_shell.cc
@@ -839,14 +839,15 @@
     exit(1);
   }
 
-  // Ensure that we have the tracefile argument only at the end.
-  if (optind != argc - 1 || argv[optind] == nullptr) {
+  // The only case where we allow omitting the trace file path is when running
+  // in --http mode. In all other cases, the last argument must be the trace
+  // file.
+  if (optind == argc - 1 && argv[optind]) {
+    command_line_options.trace_file_path = argv[optind];
+  } else if (!command_line_options.enable_httpd) {
     PrintUsage(argv);
     exit(1);
   }
-
-  command_line_options.trace_file_path = argv[optind];
-
   return command_line_options;
 }
 
@@ -892,38 +893,41 @@
 int TraceProcessorMain(int argc, char** argv) {
   CommandLineOptions options = ParseCommandLineOptions(argc, argv);
 
-#if PERFETTO_BUILDFLAG(PERFETTO_TP_HTTPD)
-  if (options.enable_httpd) {
-    RunHttpRPCServer();
-    return 0;
-  }
-#endif
-
   // Load the trace file into the trace processor.
   Config config;
   config.force_full_sort = options.force_full_sort;
 
   std::unique_ptr<TraceProcessor> tp = TraceProcessor::CreateInstance(config);
-
-  auto t_load_start = base::GetWallTimeNs();
-  double size_mb = 0;
-  util::Status read_status =
-      ReadTrace(tp.get(), options.trace_file_path.c_str(),
-                [&size_mb](size_t parsed_size) {
-                  size_mb = parsed_size / 1E6;
-                  fprintf(stderr, "\rLoading trace: %.2f MB\r", size_mb);
-                });
-  if (!read_status.ok()) {
-    PERFETTO_ELOG("Could not read trace file (path: %s): %s",
-                  options.trace_file_path.c_str(), read_status.c_message());
-    return 1;
-  }
-  auto t_load = base::GetWallTimeNs() - t_load_start;
-  double t_load_s = t_load.count() / 1E9;
-  PERFETTO_ILOG("Trace loaded: %.2f MB (%.1f MB/s)", size_mb,
-                size_mb / t_load_s);
   g_tp = tp.get();
 
+  base::TimeNanos t_load{};
+  if (!options.trace_file_path.empty()) {
+    auto t_load_start = base::GetWallTimeNs();
+    double size_mb = 0;
+    util::Status read_status =
+        ReadTrace(tp.get(), options.trace_file_path.c_str(),
+                  [&size_mb](size_t parsed_size) {
+                    size_mb = parsed_size / 1E6;
+                    fprintf(stderr, "\rLoading trace: %.2f MB\r", size_mb);
+                  });
+    if (!read_status.ok()) {
+      PERFETTO_ELOG("Could not read trace file (path: %s): %s",
+                    options.trace_file_path.c_str(), read_status.c_message());
+      return 1;
+    }
+    t_load = base::GetWallTimeNs() - t_load_start;
+    double t_load_s = t_load.count() / 1E9;
+    PERFETTO_ILOG("Trace loaded: %.2f MB (%.1f MB/s)", size_mb,
+                  size_mb / t_load_s);
+  }  // if (!trace_file_path.empty())
+
+#if PERFETTO_BUILDFLAG(PERFETTO_TP_HTTPD)
+  if (options.enable_httpd) {
+    RunHttpRPCServer(std::move(tp));
+    return 0;
+  }
+#endif
+
 #if PERFETTO_HAS_SIGNAL_H()
   signal(SIGINT, [](int) { g_tp->InterruptQuery(); });
 #endif