Add the public driver that sequences bucketing, target materialization, work
estimation, and parallel source correlation.  This provides one
orchestration boundary between perf ingestion and GCOV export.

afdo_tools/ChangeLog:

        * src/autofdo_correlator.cc: New file.
        * src/autofdo_correlator.h: New file.

Signed-off-by: Kugan Vivekanandarajah <[email protected]>
Co-authored-by: Dhruv Chawla <[email protected]>
Co-authored-by: Rahul Shinde <[email protected]>
---
 afdo_tools/src/autofdo_correlator.cc | 93 ++++++++++++++++++++++++++++
 afdo_tools/src/autofdo_correlator.h  | 48 ++++++++++++++
 2 files changed, 141 insertions(+)
 create mode 100644 afdo_tools/src/autofdo_correlator.cc
 create mode 100644 afdo_tools/src/autofdo_correlator.h

diff --git a/afdo_tools/src/autofdo_correlator.cc 
b/afdo_tools/src/autofdo_correlator.cc
new file mode 100644
index 00000000000..11e272bf959
--- /dev/null
+++ b/afdo_tools/src/autofdo_correlator.cc
@@ -0,0 +1,93 @@
+/* Copyright The GNU Toolchain Authors
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "autofdo_correlator.h"
+
+#include "autofdo_correlate_detail.h"
+#include "autofdo_debug_dump.h"
+#include "autofdo_options.h"
+#include "branch_stack_reader_base.h"
+#include "profile_timer.h"
+
+#include <algorithm>
+#include <cstdio>
+#include <string>
+
+namespace autofdo {
+
+bool
+correlate_samples_to_source (profile_builder &profile,
+                            const perf_data::profile_reader &perf,
+                            dwarf_debug::dwarf_reader &dw,
+                            const profiler_run_options &run_opts)
+{
+  correlate_detail::correlate_context ctx (dw);
+  profile_timer::begin_stage (_("correlate bucket"));
+  if (!correlate_detail::bucket_and_select_targets (ctx, perf, run_opts))
+    return false;
+  if (const perf_data::branch_stack_reader_base *branch_stack
+      = perf.as_branch_stack_reader ())
+    ctx.range_mass_addr_step = branch_stack->range_mass_addr_step ();
+  profile_timer::end_stage (std::to_string (ctx.targets.size ())
+                           + _(" targets"));
+
+  profile_timer::begin_stage (_("correlate materialize"));
+  correlate_detail::materialize_correlate_targets (ctx, profile);
+  profile_timer::end_stage (std::to_string (ctx.targets.size ())
+                           + _(" builders"));
+
+  profile_timer::begin_stage (_("correlate workers"));
+  for (correlate_detail::correlate_target &target : ctx.targets)
+    target.estimated_work = correlate_detail::estimate_correlate_target_work (
+      target, ctx.range_mass_addr_step);
+  std::sort (ctx.targets.begin (),
+            ctx.targets.end (),
+            [] (const correlate_detail::correlate_target &a,
+                const correlate_detail::correlate_target &b)
+            {
+              if (a.estimated_work != b.estimated_work)
+                return a.estimated_work > b.estimated_work;
+              if (a.maps.start_addr != b.maps.start_addr)
+                return a.maps.start_addr < b.maps.start_addr;
+              return dwarf_debug::string_value (a.func_name)
+                     < dwarf_debug::string_value (b.func_name);
+            });
+
+  dwarf_debug::correlate_symbolize_map symbolize_snapshot;
+  dwarf_debug::correlate_symbolize_map *symbolize_out
+    = run_opts.dump_address_mapping.empty () ? nullptr : &symbolize_snapshot;
+  fprintf (stderr,
+          _("AFDO: correlating %zu functions (%u threads)...\n"),
+          ctx.targets.size (),
+          ctx.num_threads);
+  const size_t functions_done
+    = correlate_detail::run_correlate_target_workers (ctx, symbolize_out);
+  profile_timer::end_stage (std::to_string (functions_done)
+                           + _(" functions, ")
+                           + std::to_string (ctx.num_threads) + _(" threads"));
+
+  if (symbolize_out != nullptr
+      && !write_address_mapping_dump (
+       run_opts.dump_address_mapping, symbolize_snapshot))
+    fprintf (stderr,
+            _("Warning: failed to write address mapping to %s (continuing)\n"),
+            run_opts.dump_address_mapping.c_str ());
+  return true;
+}
+
+} /* namespace autofdo.  */
diff --git a/afdo_tools/src/autofdo_correlator.h 
b/afdo_tools/src/autofdo_correlator.h
new file mode 100644
index 00000000000..23f459b2f88
--- /dev/null
+++ b/afdo_tools/src/autofdo_correlator.h
@@ -0,0 +1,48 @@
+/* Copyright The GNU Toolchain Authors
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_AUTOFDO_CORRELATOR_H
+#define GCC_AUTOFDO_CORRELATOR_H
+
+namespace dwarf_debug {
+class dwarf_reader;
+}
+
+namespace perf_data {
+class profile_reader;
+}
+
+namespace autofdo {
+
+class profile_builder;
+struct profiler_run_options;
+
+/* correlate_samples_to_source() is the sole public entry.
+   After return: finalize_after_correlate() then build_gcov_profile().
+
+   Pipeline: (1) bucket/select targets, (2) materialize builders + anchors,
+   (3) sort by work, (4) per-function workers, (5) optional addr dump.  */
+
+bool correlate_samples_to_source (profile_builder &profile,
+                                 const perf_data::profile_reader &perf,
+                                 dwarf_debug::dwarf_reader &dw,
+                                 const profiler_run_options &run_opts);
+
+} /* namespace autofdo.  */
+
+#endif /* GCC_AUTOFDO_CORRELATOR_H.  */
-- 
2.34.1

Reply via email to