ftrace: clean up unnecessary unique_ptrs and headers

Change-Id: Iffe835e3c83b24a42f2cefb99566da0b83904a7d
diff --git a/src/traced/probes/ftrace/cpu_reader.cc b/src/traced/probes/ftrace/cpu_reader.cc
index 434e07f..34891aa 100644
--- a/src/traced/probes/ftrace/cpu_reader.cc
+++ b/src/traced/probes/ftrace/cpu_reader.cc
@@ -18,7 +18,6 @@
 
 #include <dirent.h>
 #include <fcntl.h>
-#include <signal.h>
 
 #include <algorithm>
 #include <optional>
@@ -35,7 +34,7 @@
 #include "src/kallsyms/lazy_kernel_symbolizer.h"
 #include "src/traced/probes/ftrace/cpu_stats_parser.h"
 #include "src/traced/probes/ftrace/ftrace_config_muxer.h"
-#include "src/traced/probes/ftrace/ftrace_controller.h"
+#include "src/traced/probes/ftrace/ftrace_controller.h"  // FtraceClockSnapshot
 #include "src/traced/probes/ftrace/ftrace_data_source.h"
 #include "src/traced/probes/ftrace/ftrace_print_filter.h"
 #include "src/traced/probes/ftrace/proto_translation_table.h"
@@ -161,15 +160,17 @@
 using protos::pbzero::GenericFtraceEvent;
 
 CpuReader::CpuReader(size_t cpu,
+                     base::ScopedFile trace_fd,
                      const ProtoTranslationTable* table,
                      LazyKernelSymbolizer* symbolizer,
-                     const FtraceClockSnapshot* ftrace_clock_snapshot,
-                     base::ScopedFile trace_fd)
+                     protos::pbzero::FtraceClock ftrace_clock,
+                     const FtraceClockSnapshot* ftrace_clock_snapshot)
     : cpu_(cpu),
       table_(table),
       symbolizer_(symbolizer),
-      ftrace_clock_snapshot_(ftrace_clock_snapshot),
-      trace_fd_(std::move(trace_fd)) {
+      trace_fd_(std::move(trace_fd)),
+      ftrace_clock_(ftrace_clock),
+      ftrace_clock_snapshot_(ftrace_clock_snapshot) {
   PERFETTO_CHECK(trace_fd_);
   PERFETTO_CHECK(SetBlocking(*trace_fd_, false));
 }
diff --git a/src/traced/probes/ftrace/cpu_reader.h b/src/traced/probes/ftrace/cpu_reader.h
index eb24f8f..72b71fc 100644
--- a/src/traced/probes/ftrace/cpu_reader.h
+++ b/src/traced/probes/ftrace/cpu_reader.h
@@ -20,29 +20,18 @@
 #include <stdint.h>
 #include <string.h>
 
-#include <array>
-#include <atomic>
-#include <memory>
 #include <optional>
 #include <set>
-#include <thread>
 
-#include "perfetto/ext/base/paged_memory.h"
-#include "perfetto/ext/base/pipe.h"
 #include "perfetto/ext/base/scoped_file.h"
-#include "perfetto/ext/base/thread_checker.h"
 #include "perfetto/ext/traced/data_source_types.h"
 #include "perfetto/ext/tracing/core/trace_writer.h"
 #include "perfetto/protozero/message.h"
 #include "perfetto/protozero/message_handle.h"
-#include "protos/perfetto/trace/interned_data/interned_data.pbzero.h"
-#include "protos/perfetto/trace/profiling/profile_common.pbzero.h"
-#include "protos/perfetto/trace/trace_packet.pbzero.h"
-#include "src/kallsyms/kernel_symbol_map.h"
-#include "src/kallsyms/lazy_kernel_symbolizer.h"
 #include "src/traced/probes/ftrace/compact_sched.h"
 #include "src/traced/probes/ftrace/ftrace_metadata.h"
-#include "src/traced/probes/ftrace/proto_translation_table.h"
+
+#include "protos/perfetto/trace/trace_packet.pbzero.h"
 
 namespace perfetto {
 
@@ -63,8 +52,6 @@
 // tracing buffers.
 class CpuReader {
  public:
-  using FtraceEventBundle = protos::pbzero::FtraceEventBundle;
-
   // Helper class to generate `TracePacket`s when needed. Public for testing.
   class Bundler {
    public:
@@ -128,10 +115,11 @@
   };
 
   CpuReader(size_t cpu,
+            base::ScopedFile trace_fd,
             const ProtoTranslationTable* table,
             LazyKernelSymbolizer* symbolizer,
-            const FtraceClockSnapshot*,
-            base::ScopedFile trace_fd);
+            protos::pbzero::FtraceClock ftrace_clock,
+            const FtraceClockSnapshot* ftrace_clock_snapshot);
   ~CpuReader();
 
   // Reads and parses all ftrace data for this cpu (in batches), until we catch
@@ -327,10 +315,6 @@
       const FtraceClockSnapshot*,
       protos::pbzero::FtraceClock);
 
-  void set_ftrace_clock(protos::pbzero::FtraceClock clock) {
-    ftrace_clock_ = clock;
-  }
-
  private:
   CpuReader(const CpuReader&) = delete;
   CpuReader& operator=(const CpuReader&) = delete;
@@ -348,9 +332,9 @@
   const size_t cpu_;
   const ProtoTranslationTable* const table_;
   LazyKernelSymbolizer* const symbolizer_;
-  const FtraceClockSnapshot* const ftrace_clock_snapshot_;
   base::ScopedFile trace_fd_;
   protos::pbzero::FtraceClock ftrace_clock_{};
+  const FtraceClockSnapshot* const ftrace_clock_snapshot_;
 };
 
 }  // namespace perfetto
diff --git a/src/traced/probes/ftrace/ftrace_controller.cc b/src/traced/probes/ftrace/ftrace_controller.cc
index 907e772..82b4a9b 100644
--- a/src/traced/probes/ftrace/ftrace_controller.cc
+++ b/src/traced/probes/ftrace/ftrace_controller.cc
@@ -175,9 +175,8 @@
 
   SyscallTable syscalls = SyscallTable::FromCurrentArch();
 
-  std::unique_ptr<FtraceConfigMuxer> muxer =
-      std::unique_ptr<FtraceConfigMuxer>(new FtraceConfigMuxer(
-          ftrace_procfs.get(), table.get(), std::move(syscalls), vendor_evts));
+  auto muxer = std::make_unique<FtraceConfigMuxer>(
+      ftrace_procfs.get(), table.get(), std::move(syscalls), vendor_evts);
   return std::unique_ptr<FtraceController>(
       new FtraceController(std::move(ftrace_procfs), std::move(table),
                            std::move(muxer), runner, observer));
@@ -190,9 +189,7 @@
                                    Observer* observer)
     : task_runner_(task_runner),
       observer_(observer),
-      symbolizer_(new LazyKernelSymbolizer()),
       primary_(std::move(ftrace_procfs), std::move(table), std::move(muxer)),
-      ftrace_clock_snapshot_(new FtraceClockSnapshot()),
       weak_factory_(this) {}
 
 FtraceController::~FtraceController() {
@@ -223,15 +220,16 @@
 
   PERFETTO_DCHECK(instance->per_cpu.empty());
   size_t num_cpus = instance->ftrace_procfs->NumberOfCpus();
+  const auto ftrace_clock = instance->ftrace_config_muxer->ftrace_clock();
   instance->per_cpu.clear();
   instance->per_cpu.reserve(num_cpus);
   size_t period_page_quota =
       instance->ftrace_config_muxer->GetPerCpuBufferSizePages();
   for (size_t cpu = 0; cpu < num_cpus; cpu++) {
-    auto reader = std::unique_ptr<CpuReader>(
-        new CpuReader(cpu, instance->table.get(), symbolizer_.get(),
-                      ftrace_clock_snapshot_.get(),
-                      instance->ftrace_procfs->OpenPipeForCpu(cpu)));
+    auto reader = std::make_unique<CpuReader>(
+        cpu, instance->ftrace_procfs->OpenPipeForCpu(cpu),
+        instance->table.get(), &symbolizer_, ftrace_clock,
+        &ftrace_clock_snapshot_);
     instance->per_cpu.emplace_back(std::move(reader), period_page_quota);
   }
 
@@ -239,8 +237,8 @@
   // manual clock snapshots so that the trace parser can do a best effort
   // conversion back to boot. This is primarily for old kernels that predate
   // boot support, and therefore default to "global" clock.
-  if (instance == &primary_ && instance->ftrace_config_muxer->ftrace_clock() !=
-                                   FtraceClock::FTRACE_CLOCK_UNSPECIFIED) {
+  if (instance == &primary_ &&
+      ftrace_clock != FtraceClock::FTRACE_CLOCK_UNSPECIFIED) {
     cpu_zero_stats_fd_ = primary_.ftrace_procfs->OpenCpuStats(0 /* cpu */);
     MaybeSnapshotFtraceClock();
   }
@@ -350,7 +348,6 @@
 
   bool all_cpus_done = true;
   uint8_t* parsing_buf = reinterpret_cast<uint8_t*>(parsing_mem_.Get());
-  const auto ftrace_clock = instance->ftrace_config_muxer->ftrace_clock();
   for (size_t i = 0; i < instance->per_cpu.size(); i++) {
     size_t orig_quota = instance->per_cpu[i].period_page_quota;
     if (orig_quota == 0)
@@ -358,7 +355,6 @@
 
     size_t max_pages = std::min(orig_quota, kMaxPagesPerCpuPerReadTick);
     CpuReader& cpu_reader = *instance->per_cpu[i].reader;
-    cpu_reader.set_ftrace_clock(ftrace_clock);
     size_t pages_read =
         cpu_reader.ReadCycle(parsing_buf, kParsingBufferSizePages, max_pages,
                              instance->started_data_sources);
@@ -452,7 +448,7 @@
     return;
 
   if (!retain_ksyms_on_stop_) {
-    symbolizer_->Destroy();
+    symbolizer_.Destroy();
   }
   retain_ksyms_on_stop_ = false;
 
@@ -513,7 +509,7 @@
   // Note that we're already recording data into the kernel ftrace
   // buffers while doing the symbol parsing.
   if (data_source->config().symbolize_ksyms()) {
-    symbolizer_->GetOrCreateKernelSymbolMap();
+    symbolizer_.GetOrCreateKernelSymbolMap();
     // If at least one config sets the KSYMS_RETAIN flag, keep the ksysm map
     // around in StopIfNeeded().
     const auto KRET = FtraceConfig::KSYMS_RETAIN;
@@ -546,8 +542,8 @@
     return;
 
   DumpAllCpuStats(instance->ftrace_procfs.get(), stats_out);
-  if (symbolizer_ && symbolizer_->is_valid()) {
-    auto* symbol_map = symbolizer_->GetOrCreateKernelSymbolMap();
+  if (symbolizer_.is_valid()) {
+    auto* symbol_map = symbolizer_.GetOrCreateKernelSymbolMap();
     stats_out->kernel_symbols_parsed =
         static_cast<uint32_t>(symbol_map->num_syms());
     stats_out->kernel_symbols_mem_kb =
@@ -565,10 +561,10 @@
   // Snapshot the boot clock *before* reading CPU stats so that
   // two clocks are as close togher as possible (i.e. if it was the
   // other way round, we'd skew by the const of string parsing).
-  ftrace_clock_snapshot_->boot_clock_ts = base::GetBootTimeNs().count();
+  ftrace_clock_snapshot_.boot_clock_ts = base::GetBootTimeNs().count();
 
   // A value of zero will cause this snapshot to be skipped.
-  ftrace_clock_snapshot_->ftrace_clock_ts =
+  ftrace_clock_snapshot_.ftrace_clock_ts =
       ReadFtraceNowTs(cpu_zero_stats_fd_).value_or(0);
 }
 
@@ -663,11 +659,11 @@
 
   auto syscalls = SyscallTable::FromCurrentArch();
 
-  auto muxer = std::unique_ptr<FtraceConfigMuxer>(new FtraceConfigMuxer(
+  auto muxer = std::make_unique<FtraceConfigMuxer>(
       ftrace_procfs.get(), table.get(), std::move(syscalls), vendor_evts,
-      /* secondary_instance= */ true));
-  return std::unique_ptr<FtraceInstanceState>(new FtraceInstanceState(
-      std::move(ftrace_procfs), std::move(table), std::move(muxer)));
+      /* secondary_instance= */ true);
+  return std::make_unique<FtraceInstanceState>(
+      std::move(ftrace_procfs), std::move(table), std::move(muxer));
 }
 
 // TODO(rsavitski): we want to eventually add support for the default
diff --git a/src/traced/probes/ftrace/ftrace_controller.h b/src/traced/probes/ftrace/ftrace_controller.h
index e4f3848..be5b328 100644
--- a/src/traced/probes/ftrace/ftrace_controller.h
+++ b/src/traced/probes/ftrace/ftrace_controller.h
@@ -30,6 +30,7 @@
 #include "perfetto/ext/base/utils.h"
 #include "perfetto/ext/base/weak_ptr.h"
 #include "perfetto/ext/tracing/core/basic_types.h"
+#include "src/kallsyms/lazy_kernel_symbolizer.h"
 #include "src/traced/probes/ftrace/cpu_reader.h"
 #include "src/traced/probes/ftrace/ftrace_config_utils.h"
 
@@ -153,7 +154,7 @@
   base::TaskRunner* const task_runner_;
   Observer* const observer_;
   base::PagedMemory parsing_mem_;
-  std::unique_ptr<LazyKernelSymbolizer> symbolizer_;
+  LazyKernelSymbolizer symbolizer_;
   FtraceConfigId next_cfg_id_ = 1;
   int generation_ = 0;
   bool retain_ksyms_on_stop_ = false;
@@ -169,7 +170,7 @@
   // Additional state for snapshotting non-boot ftrace clock, specific to the
   // primary_ instance:
   base::ScopedFile cpu_zero_stats_fd_;
-  std::unique_ptr<FtraceClockSnapshot> ftrace_clock_snapshot_;
+  FtraceClockSnapshot ftrace_clock_snapshot_;
 
   base::WeakPtrFactory<FtraceController> weak_factory_;  // Keep last.
 };