linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/27] Constify tool pointers
@ 2024-06-26 20:36 Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 01/27] perf auxevent: Zero size dummy tool Ian Rogers
                   ` (26 more replies)
  0 siblings, 27 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

struct perf_tool provides a set of function pointers that are called
through when processing perf data. To make filling the pointers less
cumbersome, if they are NULL perf_tools__fill_defaults will add
default do nothing implementations.

This change refactors struct perf_tool to have an init function that
provides the default implementation. The special use of NULL and
perf_tools__fill_defaults are removed. As a consequence the tool
pointers can then all be made const, which better reflects the
behavior a particular perf command would expect of the tool and to
some extent can reduce the cognitive load on someone working on a
command.

v2: Remove dummy tool initialization [Adrian] and make zero sized. Add
    cs-etm fix for address sanitizer build, found necessary when
    testing dummy tool change.

Ian Rogers (27):
  perf auxevent: Zero size dummy tool
  perf cs-etm: Fix address sanitizer dso build failure
  perf tool: Constify tool pointers
  perf tool: Move fill defaults into tool.c
  perf tool: Add perf_tool__init
  perf kmem: Use perf_tool__init
  perf buildid-list: Use perf_tool__init
  perf kvm: Use perf_tool__init
  perf lock: Use perf_tool__init
  perf evlist: Use perf_tool__init
  perf record: Use perf_tool__init
  perf c2c: Use perf_tool__init
  perf script: Use perf_tool__init
  perf inject: Use perf_tool__init
  perf report: Use perf_tool__init
  perf stat: Use perf_tool__init
  perf annotate: Use perf_tool__init
  perf sched: Use perf_tool__init
  perf mem: Use perf_tool__init
  perf timechart: Use perf_tool__init
  perf diff: Use perf_tool__init
  perf data convert json: Use perf_tool__init
  perf data convert ctf: Use perf_tool__init
  perf test event_update: Ensure tools is initialized
  perf kwork: Use perf_tool__init
  perf tool: Remove perf_tool__fill_defaults
  perf session: Constify tool

 tools/perf/arch/x86/util/event.c    |   4 +-
 tools/perf/bench/synthesize.c       |   2 +-
 tools/perf/builtin-annotate.c       |  44 ++--
 tools/perf/builtin-buildid-list.c   |  10 +
 tools/perf/builtin-c2c.c            |  33 ++-
 tools/perf/builtin-diff.c           |  30 ++-
 tools/perf/builtin-evlist.c         |  10 +-
 tools/perf/builtin-inject.c         | 159 +++++++------
 tools/perf/builtin-kmem.c           |  20 +-
 tools/perf/builtin-kvm.c            |  19 +-
 tools/perf/builtin-kwork.c          |  33 ++-
 tools/perf/builtin-lock.c           |  41 ++--
 tools/perf/builtin-mem.c            |  37 +--
 tools/perf/builtin-record.c         |  47 ++--
 tools/perf/builtin-report.c         |  67 +++---
 tools/perf/builtin-sched.c          |  50 ++--
 tools/perf/builtin-script.c         | 106 ++++-----
 tools/perf/builtin-stat.c           |  26 +--
 tools/perf/builtin-timechart.c      |  25 +-
 tools/perf/builtin-top.c            |   2 +-
 tools/perf/builtin-trace.c          |   4 +-
 tools/perf/tests/cpumap.c           |   6 +-
 tools/perf/tests/dlfilter-test.c    |   2 +-
 tools/perf/tests/dwarf-unwind.c     |   2 +-
 tools/perf/tests/event_update.c     |   9 +-
 tools/perf/tests/stat.c             |   6 +-
 tools/perf/tests/thread-map.c       |   2 +-
 tools/perf/util/Build               |   1 +
 tools/perf/util/arm-spe.c           |  14 +-
 tools/perf/util/auxtrace.c          |  12 +-
 tools/perf/util/auxtrace.h          |  20 +-
 tools/perf/util/bpf-event.c         |   4 +-
 tools/perf/util/build-id.c          |  34 +--
 tools/perf/util/build-id.h          |   8 +-
 tools/perf/util/cs-etm.c            |  24 +-
 tools/perf/util/data-convert-bt.c   |  34 ++-
 tools/perf/util/data-convert-json.c |  47 ++--
 tools/perf/util/dso.h               |  10 +
 tools/perf/util/event.c             |  54 +++--
 tools/perf/util/event.h             |  38 ++--
 tools/perf/util/header.c            |   6 +-
 tools/perf/util/header.h            |   4 +-
 tools/perf/util/hisi-ptt.c          |   6 +-
 tools/perf/util/intel-bts.c         |  14 +-
 tools/perf/util/intel-pt.c          |  15 +-
 tools/perf/util/jitdump.c           |   4 +-
 tools/perf/util/s390-cpumsf.c       |  11 +-
 tools/perf/util/session.c           | 342 ++--------------------------
 tools/perf/util/session.h           |   6 +-
 tools/perf/util/synthetic-events.c  |  80 +++----
 tools/perf/util/synthetic-events.h  |  70 +++---
 tools/perf/util/tool.c              | 294 ++++++++++++++++++++++++
 tools/perf/util/tool.h              |  18 +-
 tools/perf/util/tsc.c               |   2 +-
 54 files changed, 967 insertions(+), 1001 deletions(-)
 create mode 100644 tools/perf/util/tool.c

-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply	[flat|nested] 32+ messages in thread

* [PATCH v2 01/27] perf auxevent: Zero size dummy tool
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-28 17:44   ` Adrian Hunter
  2024-06-26 20:36 ` [PATCH v2 02/27] perf cs-etm: Fix address sanitizer dso build failure Ian Rogers
                   ` (25 subsequent siblings)
  26 siblings, 1 reply; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

The dummy tool is passed as a placeholder to allow a container_of to
get additional parameters. As the tool isn't used, make it a zero
sized array saving 320 bytes on an x86_64 build.

s390-cpumsf's dummy tool struct was unused, so remove.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/arm-spe.c     | 6 +++---
 tools/perf/util/cs-etm.c      | 6 +++---
 tools/perf/util/intel-bts.c   | 6 +++---
 tools/perf/util/intel-pt.c    | 7 +++----
 tools/perf/util/s390-cpumsf.c | 5 -----
 5 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index afbd5869f6bf..ee0d5064ddd4 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -1074,8 +1074,8 @@ static void arm_spe_print_info(__u64 *arr)
 }
 
 struct arm_spe_synth {
-	struct perf_tool dummy_tool;
 	struct perf_session *session;
+	struct perf_tool dummy_tool[0];
 };
 
 static int arm_spe_event_synth(struct perf_tool *tool,
@@ -1084,7 +1084,7 @@ static int arm_spe_event_synth(struct perf_tool *tool,
 			       struct machine *machine __maybe_unused)
 {
 	struct arm_spe_synth *arm_spe_synth =
-		      container_of(tool, struct arm_spe_synth, dummy_tool);
+		      container_of(tool, struct arm_spe_synth, dummy_tool[0]);
 
 	return perf_session__deliver_synth_event(arm_spe_synth->session,
 						 event, NULL);
@@ -1098,7 +1098,7 @@ static int arm_spe_synth_event(struct perf_session *session,
 	memset(&arm_spe_synth, 0, sizeof(struct arm_spe_synth));
 	arm_spe_synth.session = session;
 
-	return perf_event__synthesize_attr(&arm_spe_synth.dummy_tool, attr, 1,
+	return perf_event__synthesize_attr(arm_spe_synth.dummy_tool, attr, 1,
 					   &id, arm_spe_event_synth);
 }
 
diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index 32818bd7cd17..9fd2967d4e3f 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -1596,8 +1596,8 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
 }
 
 struct cs_etm_synth {
-	struct perf_tool dummy_tool;
 	struct perf_session *session;
+	struct perf_tool dummy_tool[0];
 };
 
 static int cs_etm__event_synth(struct perf_tool *tool,
@@ -1606,7 +1606,7 @@ static int cs_etm__event_synth(struct perf_tool *tool,
 			       struct machine *machine __maybe_unused)
 {
 	struct cs_etm_synth *cs_etm_synth =
-		      container_of(tool, struct cs_etm_synth, dummy_tool);
+		      container_of(tool, struct cs_etm_synth, dummy_tool[0]);
 
 	return perf_session__deliver_synth_event(cs_etm_synth->session,
 						 event, NULL);
@@ -1620,7 +1620,7 @@ static int cs_etm__synth_event(struct perf_session *session,
 	memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
 	cs_etm_synth.session = session;
 
-	return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
+	return perf_event__synthesize_attr(cs_etm_synth.dummy_tool, attr, 1,
 					   &id, cs_etm__event_synth);
 }
 
diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c
index ec1b3bd9f530..fb8fec3a3c36 100644
--- a/tools/perf/util/intel-bts.c
+++ b/tools/perf/util/intel-bts.c
@@ -738,8 +738,8 @@ static bool intel_bts_evsel_is_auxtrace(struct perf_session *session,
 }
 
 struct intel_bts_synth {
-	struct perf_tool dummy_tool;
 	struct perf_session *session;
+	struct perf_tool dummy_tool[0];
 };
 
 static int intel_bts_event_synth(struct perf_tool *tool,
@@ -748,7 +748,7 @@ static int intel_bts_event_synth(struct perf_tool *tool,
 				 struct machine *machine __maybe_unused)
 {
 	struct intel_bts_synth *intel_bts_synth =
-			container_of(tool, struct intel_bts_synth, dummy_tool);
+			container_of(tool, struct intel_bts_synth, dummy_tool[0]);
 
 	return perf_session__deliver_synth_event(intel_bts_synth->session,
 						 event, NULL);
@@ -762,7 +762,7 @@ static int intel_bts_synth_event(struct perf_session *session,
 	memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
 	intel_bts_synth.session = session;
 
-	return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
+	return perf_event__synthesize_attr(intel_bts_synth.dummy_tool, attr, 1,
 					   &id, intel_bts_event_synth);
 }
 
diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index d6d7b7512505..b8b90773baa2 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -3660,8 +3660,8 @@ static int intel_pt_queue_data(struct perf_session *session,
 }
 
 struct intel_pt_synth {
-	struct perf_tool dummy_tool;
 	struct perf_session *session;
+	struct perf_tool dummy_tool[0];
 };
 
 static int intel_pt_event_synth(struct perf_tool *tool,
@@ -3670,7 +3670,7 @@ static int intel_pt_event_synth(struct perf_tool *tool,
 				struct machine *machine __maybe_unused)
 {
 	struct intel_pt_synth *intel_pt_synth =
-			container_of(tool, struct intel_pt_synth, dummy_tool);
+			container_of(tool, struct intel_pt_synth, dummy_tool[0]);
 
 	return perf_session__deliver_synth_event(intel_pt_synth->session, event,
 						 NULL);
@@ -3687,8 +3687,7 @@ static int intel_pt_synth_event(struct perf_session *session, const char *name,
 
 	memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
 	intel_pt_synth.session = session;
-
-	err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
+	err = perf_event__synthesize_attr(intel_pt_synth.dummy_tool, attr, 1,
 					  &id, intel_pt_event_synth);
 	if (err)
 		pr_err("%s: failed to synthesize '%s' event type\n",
diff --git a/tools/perf/util/s390-cpumsf.c b/tools/perf/util/s390-cpumsf.c
index 6fe478b0b61b..4ec583e511af 100644
--- a/tools/perf/util/s390-cpumsf.c
+++ b/tools/perf/util/s390-cpumsf.c
@@ -952,11 +952,6 @@ s390_cpumsf_process_event(struct perf_session *session,
 	return err;
 }
 
-struct s390_cpumsf_synth {
-	struct perf_tool cpumsf_tool;
-	struct perf_session *session;
-};
-
 static int
 s390_cpumsf_process_auxtrace_event(struct perf_session *session,
 				   union perf_event *event __maybe_unused,
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 02/27] perf cs-etm: Fix address sanitizer dso build failure
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 01/27] perf auxevent: Zero size dummy tool Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 04/27] perf tool: Move fill defaults into tool.c Ian Rogers
                   ` (24 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

cs-etm.c had been missed from having accessor functions added for the
sake of reference count checking. Add the function calls and missing
dso accessor functions.

Fixes: ee756ef7491e ("perf dso: Add reference count checking and accessor functions")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/cs-etm.c | 10 +++++-----
 tools/perf/util/dso.h    | 10 ++++++++++
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index 9fd2967d4e3f..e1a7eebbb15f 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -1013,7 +1013,7 @@ static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
 	if (!dso)
 		goto out;
 
-	if (dso->data.status == DSO_DATA_STATUS_ERROR &&
+	if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR &&
 	    dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))
 		goto out;
 
@@ -1027,11 +1027,11 @@ static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
 	if (len <= 0) {
 		ui__warning_once("CS ETM Trace: Missing DSO. Use 'perf archive' or debuginfod to export data from the traced system.\n"
 				 "              Enable CONFIG_PROC_KCORE or use option '-k /path/to/vmlinux' for kernel symbols.\n");
-		if (!dso->auxtrace_warned) {
+		if (!dso__auxtrace_warned(dso)) {
 			pr_err("CS ETM Trace: Debug data not found for address %#"PRIx64" in %s\n",
-				    address,
-				    dso->long_name ? dso->long_name : "Unknown");
-			dso->auxtrace_warned = true;
+				address,
+				dso__long_name(dso) ? dso__long_name(dso) : "Unknown");
+			dso__set_auxtrace_warned(dso);
 		}
 		goto out;
 	}
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index d72f3b8c37f6..878c1f441868 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -280,6 +280,16 @@ static inline void dso__set_annotate_warned(struct dso *dso)
 	RC_CHK_ACCESS(dso)->annotate_warned = 1;
 }
 
+static inline bool dso__auxtrace_warned(const struct dso *dso)
+{
+	return RC_CHK_ACCESS(dso)->auxtrace_warned;
+}
+
+static inline void dso__set_auxtrace_warned(struct dso *dso)
+{
+	RC_CHK_ACCESS(dso)->auxtrace_warned = 1;
+}
+
 static inline struct auxtrace_cache *dso__auxtrace_cache(struct dso *dso)
 {
 	return RC_CHK_ACCESS(dso)->auxtrace_cache;
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 04/27] perf tool: Move fill defaults into tool.c
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 01/27] perf auxevent: Zero size dummy tool Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 02/27] perf cs-etm: Fix address sanitizer dso build failure Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 05/27] perf tool: Add perf_tool__init Ian Rogers
                   ` (23 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

The aim here is to eventually make perf_tool__fill_defaults an init
function so that the tools struct is more const.

Create a tool.c to go along with tool.h. Move perf_tool__fill_defaults
out of session.c into tool.c along with the default stub values. Add
perf_tool__compressed_is_stub for a test in
perf_session__process_user_event.

perf_session__process_compressed_event is only used from being default
initialized so migrate into tool.c.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/Build     |   1 +
 tools/perf/util/session.c | 310 +-----------------------------------
 tools/perf/util/session.h |   2 -
 tools/perf/util/tool.c    | 325 ++++++++++++++++++++++++++++++++++++++
 tools/perf/util/tool.h    |   4 +
 5 files changed, 331 insertions(+), 311 deletions(-)
 create mode 100644 tools/perf/util/tool.c

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index da64efd8718f..b412a17bae6b 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -65,6 +65,7 @@ perf-y += map.o
 perf-y += maps.o
 perf-y += pstack.o
 perf-y += session.o
+perf-y += tool.o
 perf-y += sample-raw.o
 perf-y += s390-sample-raw.o
 perf-y += amd-sample-raw.o
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 278955b8e73a..e1d38d91a1b6 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -38,68 +38,6 @@
 #include "units.h"
 #include <internal/lib.h>
 
-#ifdef HAVE_ZSTD_SUPPORT
-static int perf_session__process_compressed_event(struct perf_session *session,
-						  union perf_event *event, u64 file_offset,
-						  const char *file_path)
-{
-	void *src;
-	size_t decomp_size, src_size;
-	u64 decomp_last_rem = 0;
-	size_t mmap_len, decomp_len = session->header.env.comp_mmap_len;
-	struct decomp *decomp, *decomp_last = session->active_decomp->decomp_last;
-
-	if (decomp_last) {
-		decomp_last_rem = decomp_last->size - decomp_last->head;
-		decomp_len += decomp_last_rem;
-	}
-
-	mmap_len = sizeof(struct decomp) + decomp_len;
-	decomp = mmap(NULL, mmap_len, PROT_READ|PROT_WRITE,
-		      MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
-	if (decomp == MAP_FAILED) {
-		pr_err("Couldn't allocate memory for decompression\n");
-		return -1;
-	}
-
-	decomp->file_pos = file_offset;
-	decomp->file_path = file_path;
-	decomp->mmap_len = mmap_len;
-	decomp->head = 0;
-
-	if (decomp_last_rem) {
-		memcpy(decomp->data, &(decomp_last->data[decomp_last->head]), decomp_last_rem);
-		decomp->size = decomp_last_rem;
-	}
-
-	src = (void *)event + sizeof(struct perf_record_compressed);
-	src_size = event->pack.header.size - sizeof(struct perf_record_compressed);
-
-	decomp_size = zstd_decompress_stream(session->active_decomp->zstd_decomp, src, src_size,
-				&(decomp->data[decomp_last_rem]), decomp_len - decomp_last_rem);
-	if (!decomp_size) {
-		munmap(decomp, mmap_len);
-		pr_err("Couldn't decompress data\n");
-		return -1;
-	}
-
-	decomp->size += decomp_size;
-
-	if (session->active_decomp->decomp == NULL)
-		session->active_decomp->decomp = decomp;
-	else
-		session->active_decomp->decomp_last->next = decomp;
-
-	session->active_decomp->decomp_last = decomp;
-
-	pr_debug("decomp (B): %zd to %zd\n", src_size, decomp_size);
-
-	return 0;
-}
-#else /* !HAVE_ZSTD_SUPPORT */
-#define perf_session__process_compressed_event perf_session__process_compressed_event_stub
-#endif
-
 static int perf_session__deliver_event(struct perf_session *session,
 				       union perf_event *event,
 				       const struct perf_tool *tool,
@@ -319,251 +257,6 @@ void perf_session__delete(struct perf_session *session)
 	free(session);
 }
 
-static int process_event_synth_tracing_data_stub(struct perf_session *session
-						 __maybe_unused,
-						 union perf_event *event
-						 __maybe_unused)
-{
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int process_event_synth_attr_stub(const struct perf_tool *tool __maybe_unused,
-					 union perf_event *event __maybe_unused,
-					 struct evlist **pevlist
-					 __maybe_unused)
-{
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int process_event_synth_event_update_stub(const struct perf_tool *tool __maybe_unused,
-						 union perf_event *event __maybe_unused,
-						 struct evlist **pevlist
-						 __maybe_unused)
-{
-	if (dump_trace)
-		perf_event__fprintf_event_update(event, stdout);
-
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int process_event_sample_stub(const struct perf_tool *tool __maybe_unused,
-				     union perf_event *event __maybe_unused,
-				     struct perf_sample *sample __maybe_unused,
-				     struct evsel *evsel __maybe_unused,
-				     struct machine *machine __maybe_unused)
-{
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int process_event_stub(const struct perf_tool *tool __maybe_unused,
-			      union perf_event *event __maybe_unused,
-			      struct perf_sample *sample __maybe_unused,
-			      struct machine *machine __maybe_unused)
-{
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int process_finished_round_stub(const struct perf_tool *tool __maybe_unused,
-				       union perf_event *event __maybe_unused,
-				       struct ordered_events *oe __maybe_unused)
-{
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int skipn(int fd, off_t n)
-{
-	char buf[4096];
-	ssize_t ret;
-
-	while (n > 0) {
-		ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
-		if (ret <= 0)
-			return ret;
-		n -= ret;
-	}
-
-	return 0;
-}
-
-static s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused,
-				       union perf_event *event)
-{
-	dump_printf(": unhandled!\n");
-	if (perf_data__is_pipe(session->data))
-		skipn(perf_data__fd(session->data), event->auxtrace.size);
-	return event->auxtrace.size;
-}
-
-static int process_event_op2_stub(struct perf_session *session __maybe_unused,
-				  union perf_event *event __maybe_unused)
-{
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-
-static
-int process_event_thread_map_stub(struct perf_session *session __maybe_unused,
-				  union perf_event *event __maybe_unused)
-{
-	if (dump_trace)
-		perf_event__fprintf_thread_map(event, stdout);
-
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static
-int process_event_cpu_map_stub(struct perf_session *session __maybe_unused,
-			       union perf_event *event __maybe_unused)
-{
-	if (dump_trace)
-		perf_event__fprintf_cpu_map(event, stdout);
-
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static
-int process_event_stat_config_stub(struct perf_session *session __maybe_unused,
-				   union perf_event *event __maybe_unused)
-{
-	if (dump_trace)
-		perf_event__fprintf_stat_config(event, stdout);
-
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int process_stat_stub(struct perf_session *perf_session __maybe_unused,
-			     union perf_event *event)
-{
-	if (dump_trace)
-		perf_event__fprintf_stat(event, stdout);
-
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int process_stat_round_stub(struct perf_session *perf_session __maybe_unused,
-				   union perf_event *event)
-{
-	if (dump_trace)
-		perf_event__fprintf_stat_round(event, stdout);
-
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int process_event_time_conv_stub(struct perf_session *perf_session __maybe_unused,
-					union perf_event *event)
-{
-	if (dump_trace)
-		perf_event__fprintf_time_conv(event, stdout);
-
-	dump_printf(": unhandled!\n");
-	return 0;
-}
-
-static int perf_session__process_compressed_event_stub(struct perf_session *session __maybe_unused,
-						       union perf_event *event __maybe_unused,
-						       u64 file_offset __maybe_unused,
-						       const char *file_path __maybe_unused)
-{
-       dump_printf(": unhandled!\n");
-       return 0;
-}
-
-void perf_tool__fill_defaults(struct perf_tool *tool)
-{
-	if (tool->sample == NULL)
-		tool->sample = process_event_sample_stub;
-	if (tool->mmap == NULL)
-		tool->mmap = process_event_stub;
-	if (tool->mmap2 == NULL)
-		tool->mmap2 = process_event_stub;
-	if (tool->comm == NULL)
-		tool->comm = process_event_stub;
-	if (tool->namespaces == NULL)
-		tool->namespaces = process_event_stub;
-	if (tool->cgroup == NULL)
-		tool->cgroup = process_event_stub;
-	if (tool->fork == NULL)
-		tool->fork = process_event_stub;
-	if (tool->exit == NULL)
-		tool->exit = process_event_stub;
-	if (tool->lost == NULL)
-		tool->lost = perf_event__process_lost;
-	if (tool->lost_samples == NULL)
-		tool->lost_samples = perf_event__process_lost_samples;
-	if (tool->aux == NULL)
-		tool->aux = perf_event__process_aux;
-	if (tool->itrace_start == NULL)
-		tool->itrace_start = perf_event__process_itrace_start;
-	if (tool->context_switch == NULL)
-		tool->context_switch = perf_event__process_switch;
-	if (tool->ksymbol == NULL)
-		tool->ksymbol = perf_event__process_ksymbol;
-	if (tool->bpf == NULL)
-		tool->bpf = perf_event__process_bpf;
-	if (tool->text_poke == NULL)
-		tool->text_poke = perf_event__process_text_poke;
-	if (tool->aux_output_hw_id == NULL)
-		tool->aux_output_hw_id = perf_event__process_aux_output_hw_id;
-	if (tool->read == NULL)
-		tool->read = process_event_sample_stub;
-	if (tool->throttle == NULL)
-		tool->throttle = process_event_stub;
-	if (tool->unthrottle == NULL)
-		tool->unthrottle = process_event_stub;
-	if (tool->attr == NULL)
-		tool->attr = process_event_synth_attr_stub;
-	if (tool->event_update == NULL)
-		tool->event_update = process_event_synth_event_update_stub;
-	if (tool->tracing_data == NULL)
-		tool->tracing_data = process_event_synth_tracing_data_stub;
-	if (tool->build_id == NULL)
-		tool->build_id = process_event_op2_stub;
-	if (tool->finished_round == NULL) {
-		if (tool->ordered_events)
-			tool->finished_round = perf_event__process_finished_round;
-		else
-			tool->finished_round = process_finished_round_stub;
-	}
-	if (tool->id_index == NULL)
-		tool->id_index = process_event_op2_stub;
-	if (tool->auxtrace_info == NULL)
-		tool->auxtrace_info = process_event_op2_stub;
-	if (tool->auxtrace == NULL)
-		tool->auxtrace = process_event_auxtrace_stub;
-	if (tool->auxtrace_error == NULL)
-		tool->auxtrace_error = process_event_op2_stub;
-	if (tool->thread_map == NULL)
-		tool->thread_map = process_event_thread_map_stub;
-	if (tool->cpu_map == NULL)
-		tool->cpu_map = process_event_cpu_map_stub;
-	if (tool->stat_config == NULL)
-		tool->stat_config = process_event_stat_config_stub;
-	if (tool->stat == NULL)
-		tool->stat = process_stat_stub;
-	if (tool->stat_round == NULL)
-		tool->stat_round = process_stat_round_stub;
-	if (tool->time_conv == NULL)
-		tool->time_conv = process_event_time_conv_stub;
-	if (tool->feature == NULL)
-		tool->feature = process_event_op2_stub;
-	if (tool->compressed == NULL)
-		tool->compressed = perf_session__process_compressed_event;
-	if (tool->finished_init == NULL)
-		tool->finished_init = process_event_op2_stub;
-}
-
 static void swap_sample_id_all(union perf_event *event, void *data)
 {
 	void *end = (void *) event + event->header.size;
@@ -1672,8 +1365,7 @@ static s64 perf_session__process_user_event(struct perf_session *session,
 	int fd = perf_data__fd(session->data);
 	int err;
 
-	if (event->header.type != PERF_RECORD_COMPRESSED ||
-	    tool->compressed == perf_session__process_compressed_event_stub)
+	if (event->header.type != PERF_RECORD_COMPRESSED || perf_tool__compressed_is_stub(tool))
 		dump_event(session->evlist, event, file_offset, &sample, file_path);
 
 	/* These events are processed right away */
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index a298de3c85d8..9c9c531052fd 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -92,8 +92,6 @@ int perf_session__process_events(struct perf_session *session);
 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
 			      u64 timestamp, u64 file_offset, const char *file_path);
 
-void perf_tool__fill_defaults(struct perf_tool *tool);
-
 int perf_session__resolve_callchain(struct perf_session *session,
 				    struct evsel *evsel,
 				    struct thread *thread,
diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c
new file mode 100644
index 000000000000..17219ecb8fa6
--- /dev/null
+++ b/tools/perf/util/tool.c
@@ -0,0 +1,325 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "data.h"
+#include "debug.h"
+#include "header.h"
+#include "session.h"
+#include "stat.h"
+#include "tool.h"
+#include "tsc.h"
+#include <sys/mman.h>
+#include <unistd.h>
+
+#ifdef HAVE_ZSTD_SUPPORT
+static int perf_session__process_compressed_event(struct perf_session *session,
+						  union perf_event *event, u64 file_offset,
+						  const char *file_path)
+{
+	void *src;
+	size_t decomp_size, src_size;
+	u64 decomp_last_rem = 0;
+	size_t mmap_len, decomp_len = session->header.env.comp_mmap_len;
+	struct decomp *decomp, *decomp_last = session->active_decomp->decomp_last;
+
+	if (decomp_last) {
+		decomp_last_rem = decomp_last->size - decomp_last->head;
+		decomp_len += decomp_last_rem;
+	}
+
+	mmap_len = sizeof(struct decomp) + decomp_len;
+	decomp = mmap(NULL, mmap_len, PROT_READ|PROT_WRITE,
+		      MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+	if (decomp == MAP_FAILED) {
+		pr_err("Couldn't allocate memory for decompression\n");
+		return -1;
+	}
+
+	decomp->file_pos = file_offset;
+	decomp->file_path = file_path;
+	decomp->mmap_len = mmap_len;
+	decomp->head = 0;
+
+	if (decomp_last_rem) {
+		memcpy(decomp->data, &(decomp_last->data[decomp_last->head]), decomp_last_rem);
+		decomp->size = decomp_last_rem;
+	}
+
+	src = (void *)event + sizeof(struct perf_record_compressed);
+	src_size = event->pack.header.size - sizeof(struct perf_record_compressed);
+
+	decomp_size = zstd_decompress_stream(session->active_decomp->zstd_decomp, src, src_size,
+				&(decomp->data[decomp_last_rem]), decomp_len - decomp_last_rem);
+	if (!decomp_size) {
+		munmap(decomp, mmap_len);
+		pr_err("Couldn't decompress data\n");
+		return -1;
+	}
+
+	decomp->size += decomp_size;
+
+	if (session->active_decomp->decomp == NULL)
+		session->active_decomp->decomp = decomp;
+	else
+		session->active_decomp->decomp_last->next = decomp;
+
+	session->active_decomp->decomp_last = decomp;
+
+	pr_debug("decomp (B): %zd to %zd\n", src_size, decomp_size);
+
+	return 0;
+}
+#endif
+
+static int process_event_synth_tracing_data_stub(struct perf_session *session
+						 __maybe_unused,
+						 union perf_event *event
+						 __maybe_unused)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int process_event_synth_attr_stub(const struct perf_tool *tool __maybe_unused,
+					 union perf_event *event __maybe_unused,
+					 struct evlist **pevlist
+					 __maybe_unused)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int process_event_synth_event_update_stub(const struct perf_tool *tool __maybe_unused,
+						 union perf_event *event __maybe_unused,
+						 struct evlist **pevlist
+						 __maybe_unused)
+{
+	if (dump_trace)
+		perf_event__fprintf_event_update(event, stdout);
+
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int process_event_sample_stub(const struct perf_tool *tool __maybe_unused,
+				     union perf_event *event __maybe_unused,
+				     struct perf_sample *sample __maybe_unused,
+				     struct evsel *evsel __maybe_unused,
+				     struct machine *machine __maybe_unused)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int process_event_stub(const struct perf_tool *tool __maybe_unused,
+			      union perf_event *event __maybe_unused,
+			      struct perf_sample *sample __maybe_unused,
+			      struct machine *machine __maybe_unused)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int process_finished_round_stub(const struct perf_tool *tool __maybe_unused,
+				       union perf_event *event __maybe_unused,
+				       struct ordered_events *oe __maybe_unused)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int skipn(int fd, off_t n)
+{
+	char buf[4096];
+	ssize_t ret;
+
+	while (n > 0) {
+		ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
+		if (ret <= 0)
+			return ret;
+		n -= ret;
+	}
+
+	return 0;
+}
+
+static s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused,
+				       union perf_event *event)
+{
+	dump_printf(": unhandled!\n");
+	if (perf_data__is_pipe(session->data))
+		skipn(perf_data__fd(session->data), event->auxtrace.size);
+	return event->auxtrace.size;
+}
+
+static int process_event_op2_stub(struct perf_session *session __maybe_unused,
+				  union perf_event *event __maybe_unused)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+
+static
+int process_event_thread_map_stub(struct perf_session *session __maybe_unused,
+				  union perf_event *event __maybe_unused)
+{
+	if (dump_trace)
+		perf_event__fprintf_thread_map(event, stdout);
+
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static
+int process_event_cpu_map_stub(struct perf_session *session __maybe_unused,
+			       union perf_event *event __maybe_unused)
+{
+	if (dump_trace)
+		perf_event__fprintf_cpu_map(event, stdout);
+
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static
+int process_event_stat_config_stub(struct perf_session *session __maybe_unused,
+				   union perf_event *event __maybe_unused)
+{
+	if (dump_trace)
+		perf_event__fprintf_stat_config(event, stdout);
+
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int process_stat_stub(struct perf_session *perf_session __maybe_unused,
+			     union perf_event *event)
+{
+	if (dump_trace)
+		perf_event__fprintf_stat(event, stdout);
+
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int process_stat_round_stub(struct perf_session *perf_session __maybe_unused,
+				   union perf_event *event)
+{
+	if (dump_trace)
+		perf_event__fprintf_stat_round(event, stdout);
+
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int process_event_time_conv_stub(struct perf_session *perf_session __maybe_unused,
+					union perf_event *event)
+{
+	if (dump_trace)
+		perf_event__fprintf_time_conv(event, stdout);
+
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+static int perf_session__process_compressed_event_stub(struct perf_session *session __maybe_unused,
+						       union perf_event *event __maybe_unused,
+						       u64 file_offset __maybe_unused,
+						       const char *file_path __maybe_unused)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
+void perf_tool__fill_defaults(struct perf_tool *tool)
+{
+	if (tool->sample == NULL)
+		tool->sample = process_event_sample_stub;
+	if (tool->mmap == NULL)
+		tool->mmap = process_event_stub;
+	if (tool->mmap2 == NULL)
+		tool->mmap2 = process_event_stub;
+	if (tool->comm == NULL)
+		tool->comm = process_event_stub;
+	if (tool->namespaces == NULL)
+		tool->namespaces = process_event_stub;
+	if (tool->cgroup == NULL)
+		tool->cgroup = process_event_stub;
+	if (tool->fork == NULL)
+		tool->fork = process_event_stub;
+	if (tool->exit == NULL)
+		tool->exit = process_event_stub;
+	if (tool->lost == NULL)
+		tool->lost = perf_event__process_lost;
+	if (tool->lost_samples == NULL)
+		tool->lost_samples = perf_event__process_lost_samples;
+	if (tool->aux == NULL)
+		tool->aux = perf_event__process_aux;
+	if (tool->itrace_start == NULL)
+		tool->itrace_start = perf_event__process_itrace_start;
+	if (tool->context_switch == NULL)
+		tool->context_switch = perf_event__process_switch;
+	if (tool->ksymbol == NULL)
+		tool->ksymbol = perf_event__process_ksymbol;
+	if (tool->bpf == NULL)
+		tool->bpf = perf_event__process_bpf;
+	if (tool->text_poke == NULL)
+		tool->text_poke = perf_event__process_text_poke;
+	if (tool->aux_output_hw_id == NULL)
+		tool->aux_output_hw_id = perf_event__process_aux_output_hw_id;
+	if (tool->read == NULL)
+		tool->read = process_event_sample_stub;
+	if (tool->throttle == NULL)
+		tool->throttle = process_event_stub;
+	if (tool->unthrottle == NULL)
+		tool->unthrottle = process_event_stub;
+	if (tool->attr == NULL)
+		tool->attr = process_event_synth_attr_stub;
+	if (tool->event_update == NULL)
+		tool->event_update = process_event_synth_event_update_stub;
+	if (tool->tracing_data == NULL)
+		tool->tracing_data = process_event_synth_tracing_data_stub;
+	if (tool->build_id == NULL)
+		tool->build_id = process_event_op2_stub;
+	if (tool->finished_round == NULL) {
+		if (tool->ordered_events)
+			tool->finished_round = perf_event__process_finished_round;
+		else
+			tool->finished_round = process_finished_round_stub;
+	}
+	if (tool->id_index == NULL)
+		tool->id_index = process_event_op2_stub;
+	if (tool->auxtrace_info == NULL)
+		tool->auxtrace_info = process_event_op2_stub;
+	if (tool->auxtrace == NULL)
+		tool->auxtrace = process_event_auxtrace_stub;
+	if (tool->auxtrace_error == NULL)
+		tool->auxtrace_error = process_event_op2_stub;
+	if (tool->thread_map == NULL)
+		tool->thread_map = process_event_thread_map_stub;
+	if (tool->cpu_map == NULL)
+		tool->cpu_map = process_event_cpu_map_stub;
+	if (tool->stat_config == NULL)
+		tool->stat_config = process_event_stat_config_stub;
+	if (tool->stat == NULL)
+		tool->stat = process_stat_stub;
+	if (tool->stat_round == NULL)
+		tool->stat_round = process_stat_round_stub;
+	if (tool->time_conv == NULL)
+		tool->time_conv = process_event_time_conv_stub;
+	if (tool->feature == NULL)
+		tool->feature = process_event_op2_stub;
+	if (tool->compressed == NULL) {
+#ifdef HAVE_ZSTD_SUPPORT
+		tool->compressed = perf_session__process_compressed_event;
+#else
+		tool->compressed = perf_session__process_compressed_event_stub;
+#endif
+	}
+	if (tool->finished_init == NULL)
+		tool->finished_init = process_event_op2_stub;
+}
+
+bool perf_tool__compressed_is_stub(const struct perf_tool *tool)
+{
+	return tool->compressed == perf_session__process_compressed_event_stub;
+}
diff --git a/tools/perf/util/tool.h b/tools/perf/util/tool.h
index b192d44fe91f..7913b698033a 100644
--- a/tools/perf/util/tool.h
+++ b/tools/perf/util/tool.h
@@ -88,4 +88,8 @@ struct perf_tool {
 	enum show_feature_header show_feat_hdr;
 };
 
+void perf_tool__fill_defaults(struct perf_tool *tool);
+
+bool perf_tool__compressed_is_stub(const struct perf_tool *tool);
+
 #endif /* __PERF_TOOL_H */
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 05/27] perf tool: Add perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (2 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 04/27] perf tool: Move fill defaults into tool.c Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 06/27] perf kmem: Use perf_tool__init Ian Rogers
                   ` (22 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Add init function that behaves like perf_tool__fill_defaults but
assumes all values haven't been initialized.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/tool.c | 58 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/tool.h |  1 +
 2 files changed, 59 insertions(+)

diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c
index 17219ecb8fa6..cbd9b888bd73 100644
--- a/tools/perf/util/tool.c
+++ b/tools/perf/util/tool.c
@@ -230,6 +230,64 @@ static int perf_session__process_compressed_event_stub(struct perf_session *sess
 	return 0;
 }
 
+void perf_tool__init(struct perf_tool *tool, bool ordered_events)
+{
+	tool->ordered_events = ordered_events;
+	tool->ordering_requires_timestamps = false;
+	tool->namespace_events = false;
+	tool->cgroup_events = false;
+	tool->no_warn = false;
+	tool->show_feat_hdr = SHOW_FEAT_NO_HEADER;
+
+	tool->sample = process_event_sample_stub;
+	tool->mmap = process_event_stub;
+	tool->mmap2 = process_event_stub;
+	tool->comm = process_event_stub;
+	tool->namespaces = process_event_stub;
+	tool->cgroup = process_event_stub;
+	tool->fork = process_event_stub;
+	tool->exit = process_event_stub;
+	tool->lost = perf_event__process_lost;
+	tool->lost_samples = perf_event__process_lost_samples;
+	tool->aux = perf_event__process_aux;
+	tool->itrace_start = perf_event__process_itrace_start;
+	tool->context_switch = perf_event__process_switch;
+	tool->ksymbol = perf_event__process_ksymbol;
+	tool->bpf = perf_event__process_bpf;
+	tool->text_poke = perf_event__process_text_poke;
+	tool->aux_output_hw_id = perf_event__process_aux_output_hw_id;
+	tool->read = process_event_sample_stub;
+	tool->throttle = process_event_stub;
+	tool->unthrottle = process_event_stub;
+	tool->attr = process_event_synth_attr_stub;
+	tool->event_update = process_event_synth_event_update_stub;
+	tool->tracing_data = process_event_synth_tracing_data_stub;
+	tool->build_id = process_event_op2_stub;
+
+	if (ordered_events)
+		tool->finished_round = perf_event__process_finished_round;
+	else
+		tool->finished_round = process_finished_round_stub;
+
+	tool->id_index = process_event_op2_stub;
+	tool->auxtrace_info = process_event_op2_stub;
+	tool->auxtrace = process_event_auxtrace_stub;
+	tool->auxtrace_error = process_event_op2_stub;
+	tool->thread_map = process_event_thread_map_stub;
+	tool->cpu_map = process_event_cpu_map_stub;
+	tool->stat_config = process_event_stat_config_stub;
+	tool->stat = process_stat_stub;
+	tool->stat_round = process_stat_round_stub;
+	tool->time_conv = process_event_time_conv_stub;
+	tool->feature = process_event_op2_stub;
+#ifdef HAVE_ZSTD_SUPPORT
+	tool->compressed = perf_session__process_compressed_event;
+#else
+	tool->compressed = perf_session__process_compressed_event_stub;
+#endif
+	tool->finished_init = process_event_op2_stub;
+}
+
 void perf_tool__fill_defaults(struct perf_tool *tool)
 {
 	if (tool->sample == NULL)
diff --git a/tools/perf/util/tool.h b/tools/perf/util/tool.h
index 7913b698033a..897c6c44b6b2 100644
--- a/tools/perf/util/tool.h
+++ b/tools/perf/util/tool.h
@@ -88,6 +88,7 @@ struct perf_tool {
 	enum show_feature_header show_feat_hdr;
 };
 
+void perf_tool__init(struct perf_tool *tool, bool ordered_events);
 void perf_tool__fill_defaults(struct perf_tool *tool);
 
 bool perf_tool__compressed_is_stub(const struct perf_tool *tool);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 06/27] perf kmem: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (3 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 05/27] perf tool: Add perf_tool__init Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 07/27] perf buildid-list: " Ian Rogers
                   ` (21 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Reduce the scope of the tool from global/static to just that of the
cmd_kmem function where the session is scoped. Use the perf_tool__init
to initialize default values.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-kmem.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 859ff018eace..b3cbac40b8c7 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -986,15 +986,6 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	return err;
 }
 
-static struct perf_tool perf_kmem = {
-	.sample		 = process_sample_event,
-	.comm		 = perf_event__process_comm,
-	.mmap		 = perf_event__process_mmap,
-	.mmap2		 = perf_event__process_mmap2,
-	.namespaces	 = perf_event__process_namespaces,
-	.ordered_events	 = true,
-};
-
 static double fragmentation(unsigned long n_req, unsigned long n_alloc)
 {
 	if (n_alloc == 0)
@@ -1971,6 +1962,7 @@ int cmd_kmem(int argc, const char **argv)
 		NULL
 	};
 	struct perf_session *session;
+	struct perf_tool perf_kmem;
 	static const char errmsg[] = "No %s allocation events found.  Have you run 'perf kmem record --%s'?\n";
 	int ret = perf_config(kmem_config, NULL);
 
@@ -1998,6 +1990,13 @@ int cmd_kmem(int argc, const char **argv)
 
 	data.path = input_name;
 
+	perf_tool__init(&perf_kmem, /*ordered_events=*/true);
+	perf_kmem.sample	= process_sample_event;
+	perf_kmem.comm		= perf_event__process_comm;
+	perf_kmem.mmap		= perf_event__process_mmap;
+	perf_kmem.mmap2		= perf_event__process_mmap2;
+	perf_kmem.namespaces	= perf_event__process_namespaces;
+
 	kmem_session = session = perf_session__new(&data, &perf_kmem);
 	if (IS_ERR(session))
 		return PTR_ERR(session);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 07/27] perf buildid-list: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (4 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 06/27] perf kmem: Use perf_tool__init Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 08/27] perf kvm: " Ian Rogers
                   ` (20 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Reduce scope of build_id__mark_dso_hit_ops to the scope of function
perf_session__list_build_ids, its only use, and use perf_tool__init
for the default values. Move perf_event__exit_del_thread to event.[ch]
so it can be used in builtin-buildid-list.c.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-buildid-list.c | 10 ++++++++++
 tools/perf/util/build-id.c        | 32 -------------------------------
 tools/perf/util/build-id.h        |  4 +---
 tools/perf/util/event.c           | 20 +++++++++++++++++++
 tools/perf/util/event.h           |  4 ++++
 5 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/tools/perf/builtin-buildid-list.c b/tools/perf/builtin-buildid-list.c
index 383d5de36ce4..52dfacaff8e3 100644
--- a/tools/perf/builtin-buildid-list.c
+++ b/tools/perf/builtin-buildid-list.c
@@ -89,6 +89,7 @@ static int perf_session__list_build_ids(bool force, bool with_hits)
 		.mode  = PERF_DATA_MODE_READ,
 		.force = force,
 	};
+	struct perf_tool build_id__mark_dso_hit_ops;
 
 	symbol__elf_init();
 	/*
@@ -97,6 +98,15 @@ static int perf_session__list_build_ids(bool force, bool with_hits)
 	if (filename__fprintf_build_id(input_name, stdout) > 0)
 		goto out;
 
+	perf_tool__init(&build_id__mark_dso_hit_ops, /*ordered_events=*/true);
+	build_id__mark_dso_hit_ops.sample	= build_id__mark_dso_hit;
+	build_id__mark_dso_hit_ops.mmap		= perf_event__process_mmap;
+	build_id__mark_dso_hit_ops.mmap2	= perf_event__process_mmap2;
+	build_id__mark_dso_hit_ops.fork		= perf_event__process_fork;
+	build_id__mark_dso_hit_ops.exit		= perf_event__exit_del_thread;
+	build_id__mark_dso_hit_ops.attr		= perf_event__process_attr;
+	build_id__mark_dso_hit_ops.build_id	= perf_event__process_build_id;
+
 	session = perf_session__new(&data, &build_id__mark_dso_hit_ops);
 	if (IS_ERR(session))
 		return PTR_ERR(session);
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 098fcc625d91..451d145fa4ed 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -67,38 +67,6 @@ int build_id__mark_dso_hit(const struct perf_tool *tool __maybe_unused,
 	return 0;
 }
 
-static int perf_event__exit_del_thread(const struct perf_tool *tool __maybe_unused,
-				       union perf_event *event,
-				       struct perf_sample *sample
-				       __maybe_unused,
-				       struct machine *machine)
-{
-	struct thread *thread = machine__findnew_thread(machine,
-							event->fork.pid,
-							event->fork.tid);
-
-	dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
-		    event->fork.ppid, event->fork.ptid);
-
-	if (thread) {
-		machine__remove_thread(machine, thread);
-		thread__put(thread);
-	}
-
-	return 0;
-}
-
-struct perf_tool build_id__mark_dso_hit_ops = {
-	.sample	= build_id__mark_dso_hit,
-	.mmap	= perf_event__process_mmap,
-	.mmap2	= perf_event__process_mmap2,
-	.fork	= perf_event__process_fork,
-	.exit	= perf_event__exit_del_thread,
-	.attr		 = perf_event__process_attr,
-	.build_id	 = perf_event__process_build_id,
-	.ordered_events	 = true,
-};
-
 int build_id__sprintf(const struct build_id *build_id, char *bf)
 {
 	char *bid = bf;
diff --git a/tools/perf/util/build-id.h b/tools/perf/util/build-id.h
index ae87c4c58d5b..a212497bfdb0 100644
--- a/tools/perf/util/build-id.h
+++ b/tools/perf/util/build-id.h
@@ -16,11 +16,9 @@ struct build_id {
 	size_t	size;
 };
 
-struct nsinfo;
-
-extern struct perf_tool build_id__mark_dso_hit_ops;
 struct dso;
 struct feat_fd;
+struct nsinfo;
 
 void build_id__init(struct build_id *bid, const u8 *data, size_t size);
 int build_id__sprintf(const struct build_id *build_id, char *bf);
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index c2f0e7f40ad5..aac96d5d1917 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -426,6 +426,26 @@ int perf_event__process_exit(const struct perf_tool *tool __maybe_unused,
 	return machine__process_exit_event(machine, event, sample);
 }
 
+int perf_event__exit_del_thread(const struct perf_tool *tool __maybe_unused,
+				union perf_event *event,
+				struct perf_sample *sample __maybe_unused,
+				struct machine *machine)
+{
+	struct thread *thread = machine__findnew_thread(machine,
+							event->fork.pid,
+							event->fork.tid);
+
+	dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
+		    event->fork.ppid, event->fork.ptid);
+
+	if (thread) {
+		machine__remove_thread(machine, thread);
+		thread__put(thread);
+	}
+
+	return 0;
+}
+
 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
 {
 	return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s]\n",
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index 4b24f1c580fd..f8742e6230a5 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -319,6 +319,10 @@ int perf_event__process_exit(const struct perf_tool *tool,
 			     union perf_event *event,
 			     struct perf_sample *sample,
 			     struct machine *machine);
+int perf_event__exit_del_thread(const struct perf_tool *tool,
+				union perf_event *event,
+				struct perf_sample *sample,
+				struct machine *machine);
 int perf_event__process_ksymbol(const struct perf_tool *tool,
 				union perf_event *event,
 				struct perf_sample *sample,
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 08/27] perf kvm: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (5 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 07/27] perf buildid-list: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 09/27] perf lock: " Ian Rogers
                   ` (19 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-kvm.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index a3b903cf4311..692267b1b7e8 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -1603,19 +1603,17 @@ static int read_events(struct perf_kvm_stat *kvm)
 {
 	int ret;
 
-	struct perf_tool eops = {
-		.sample			= process_sample_event,
-		.comm			= perf_event__process_comm,
-		.namespaces		= perf_event__process_namespaces,
-		.ordered_events		= true,
-	};
 	struct perf_data file = {
 		.path  = kvm->file_name,
 		.mode  = PERF_DATA_MODE_READ,
 		.force = kvm->force,
 	};
 
-	kvm->tool = eops;
+	perf_tool__init(&kvm->tool, /*ordered_events=*/true);
+	kvm->tool.sample	= process_sample_event;
+	kvm->tool.comm		= perf_event__process_comm;
+	kvm->tool.namespaces	= perf_event__process_namespaces;
+
 	kvm->session = perf_session__new(&file, &kvm->tool);
 	if (IS_ERR(kvm->session)) {
 		pr_err("Initializing perf session failed\n");
@@ -1919,14 +1917,13 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,
 
 
 	/* event handling */
+	perf_tool__init(&kvm->tool, /*ordered_events=*/true);
 	kvm->tool.sample = process_sample_event;
 	kvm->tool.comm   = perf_event__process_comm;
 	kvm->tool.exit   = perf_event__process_exit;
 	kvm->tool.fork   = perf_event__process_fork;
 	kvm->tool.lost   = process_lost_event;
 	kvm->tool.namespaces  = perf_event__process_namespaces;
-	kvm->tool.ordered_events = true;
-	perf_tool__fill_defaults(&kvm->tool);
 
 	/* set defaults */
 	kvm->display_time = 1;
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 09/27] perf lock: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (6 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 08/27] perf kvm: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 10/27] perf evlist: " Ian Rogers
                   ` (18 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-lock.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c
index 6efa9d646637..2c216427e929 100644
--- a/tools/perf/builtin-lock.c
+++ b/tools/perf/builtin-lock.c
@@ -1933,22 +1933,21 @@ static bool force;
 static int __cmd_report(bool display_info)
 {
 	int err = -EINVAL;
-	struct perf_tool eops = {
-		.attr		 = perf_event__process_attr,
-		.event_update	 = process_event_update,
-		.sample		 = process_sample_event,
-		.comm		 = perf_event__process_comm,
-		.mmap		 = perf_event__process_mmap,
-		.namespaces	 = perf_event__process_namespaces,
-		.tracing_data	 = perf_event__process_tracing_data,
-		.ordered_events	 = true,
-	};
+	struct perf_tool eops;
 	struct perf_data data = {
 		.path  = input_name,
 		.mode  = PERF_DATA_MODE_READ,
 		.force = force,
 	};
 
+	perf_tool__init(&eops, /*ordered_events=*/true);
+	eops.attr		 = perf_event__process_attr;
+	eops.event_update	 = process_event_update;
+	eops.sample		 = process_sample_event;
+	eops.comm		 = perf_event__process_comm;
+	eops.mmap		 = perf_event__process_mmap;
+	eops.namespaces		 = perf_event__process_namespaces;
+	eops.tracing_data	 = perf_event__process_tracing_data;
 	session = perf_session__new(&data, &eops);
 	if (IS_ERR(session)) {
 		pr_err("Initializing perf session failed\n");
@@ -2069,15 +2068,7 @@ static int check_lock_contention_options(const struct option *options,
 static int __cmd_contention(int argc, const char **argv)
 {
 	int err = -EINVAL;
-	struct perf_tool eops = {
-		.attr		 = perf_event__process_attr,
-		.event_update	 = process_event_update,
-		.sample		 = process_sample_event,
-		.comm		 = perf_event__process_comm,
-		.mmap		 = perf_event__process_mmap,
-		.tracing_data	 = perf_event__process_tracing_data,
-		.ordered_events	 = true,
-	};
+	struct perf_tool eops;
 	struct perf_data data = {
 		.path  = input_name,
 		.mode  = PERF_DATA_MODE_READ,
@@ -2100,6 +2091,14 @@ static int __cmd_contention(int argc, const char **argv)
 
 	con.result = &lockhash_table[0];
 
+	perf_tool__init(&eops, /*ordered_events=*/true);
+	eops.attr		 = perf_event__process_attr;
+	eops.event_update	 = process_event_update;
+	eops.sample		 = process_sample_event;
+	eops.comm		 = perf_event__process_comm;
+	eops.mmap		 = perf_event__process_mmap;
+	eops.tracing_data	 = perf_event__process_tracing_data;
+
 	session = perf_session__new(use_bpf ? NULL : &data, &eops);
 	if (IS_ERR(session)) {
 		pr_err("Initializing perf session failed\n");
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 10/27] perf evlist: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (7 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 09/27] perf lock: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 11/27] perf record: " Ian Rogers
                   ` (17 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-evlist.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/perf/builtin-evlist.c b/tools/perf/builtin-evlist.c
index 7117656939e7..818ab21c3f73 100644
--- a/tools/perf/builtin-evlist.c
+++ b/tools/perf/builtin-evlist.c
@@ -35,13 +35,13 @@ static int __cmd_evlist(const char *file_name, struct perf_attr_details *details
 		.mode      = PERF_DATA_MODE_READ,
 		.force     = details->force,
 	};
-	struct perf_tool tool = {
-		/* only needed for pipe mode */
-		.attr = perf_event__process_attr,
-		.feature = process_header_feature,
-	};
+	struct perf_tool tool;
 	bool has_tracepoint = false;
 
+	perf_tool__init(&tool, /*ordered_events=*/false);
+	/* only needed for pipe mode */
+	tool.attr = perf_event__process_attr;
+	tool.feature = process_header_feature;
 	session = perf_session__new(&data, &tool);
 	if (IS_ERR(session))
 		return PTR_ERR(session);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 11/27] perf record: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (8 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 10/27] perf evlist: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 12/27] perf c2c: " Ian Rogers
                   ` (16 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-record.c | 33 ++++++++++++++++++++-------------
 tools/perf/util/tool.c      | 10 +++++-----
 tools/perf/util/tool.h      |  6 ++++++
 3 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index e8677f9e1ccb..4b9309b4c07e 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -193,6 +193,15 @@ static const char *affinity_tags[PERF_AFFINITY_MAX] = {
 	"SYS", "NODE", "CPU"
 };
 
+static int build_id__process_mmap(const struct perf_tool *tool, union perf_event *event,
+				  struct perf_sample *sample, struct machine *machine);
+static int build_id__process_mmap2(const struct perf_tool *tool, union perf_event *event,
+				   struct perf_sample *sample, struct machine *machine);
+static int process_timestamp_boundary(const struct perf_tool *tool,
+				      union perf_event *event,
+				      struct perf_sample *sample,
+				      struct machine *machine);
+
 #ifndef HAVE_GETTID
 static inline pid_t gettid(void)
 {
@@ -1458,7 +1467,7 @@ static int process_buildids(struct record *rec)
 	 * first/last samples.
 	 */
 	if (rec->buildid_all && !rec->timestamp_boundary)
-		rec->tool.sample = NULL;
+		rec->tool.sample = process_event_sample_stub;
 
 	return perf_session__process_events(session);
 }
@@ -2386,6 +2395,16 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 		signal(SIGUSR2, SIG_IGN);
 	}
 
+	perf_tool__init(tool, /*ordered_events=*/true);
+	tool->sample		= process_sample_event;
+	tool->fork		= perf_event__process_fork;
+	tool->exit		= perf_event__process_exit;
+	tool->comm		= perf_event__process_comm;
+	tool->namespaces	= perf_event__process_namespaces;
+	tool->mmap		= build_id__process_mmap;
+	tool->mmap2		= build_id__process_mmap2;
+	tool->itrace_start	= process_timestamp_boundary;
+	tool->aux		= process_timestamp_boundary;
 	session = perf_session__new(data, tool);
 	if (IS_ERR(session)) {
 		pr_err("Perf session creation failed.\n");
@@ -3326,18 +3345,6 @@ static struct record record = {
 		.ctl_fd_ack          = -1,
 		.synth               = PERF_SYNTH_ALL,
 	},
-	.tool = {
-		.sample		= process_sample_event,
-		.fork		= perf_event__process_fork,
-		.exit		= perf_event__process_exit,
-		.comm		= perf_event__process_comm,
-		.namespaces	= perf_event__process_namespaces,
-		.mmap		= build_id__process_mmap,
-		.mmap2		= build_id__process_mmap2,
-		.itrace_start	= process_timestamp_boundary,
-		.aux		= process_timestamp_boundary,
-		.ordered_events	= true,
-	},
 };
 
 const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c
index cbd9b888bd73..a4ca16e5eefe 100644
--- a/tools/perf/util/tool.c
+++ b/tools/perf/util/tool.c
@@ -99,11 +99,11 @@ static int process_event_synth_event_update_stub(const struct perf_tool *tool __
 	return 0;
 }
 
-static int process_event_sample_stub(const struct perf_tool *tool __maybe_unused,
-				     union perf_event *event __maybe_unused,
-				     struct perf_sample *sample __maybe_unused,
-				     struct evsel *evsel __maybe_unused,
-				     struct machine *machine __maybe_unused)
+int process_event_sample_stub(const struct perf_tool *tool __maybe_unused,
+			      union perf_event *event __maybe_unused,
+			      struct perf_sample *sample __maybe_unused,
+			      struct evsel *evsel __maybe_unused,
+			      struct machine *machine __maybe_unused)
 {
 	dump_printf(": unhandled!\n");
 	return 0;
diff --git a/tools/perf/util/tool.h b/tools/perf/util/tool.h
index 897c6c44b6b2..fb7e32d98dda 100644
--- a/tools/perf/util/tool.h
+++ b/tools/perf/util/tool.h
@@ -93,4 +93,10 @@ void perf_tool__fill_defaults(struct perf_tool *tool);
 
 bool perf_tool__compressed_is_stub(const struct perf_tool *tool);
 
+int process_event_sample_stub(const struct perf_tool *tool,
+			      union perf_event *event,
+			      struct perf_sample *sample,
+			      struct evsel *evsel,
+			      struct machine *machine);
+
 #endif /* __PERF_TOOL_H */
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 12/27] perf c2c: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (9 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 11/27] perf record: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 13/27] perf script: " Ian Rogers
                   ` (15 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-c2c.c | 31 +++++++++++++------------------
 1 file changed, 13 insertions(+), 18 deletions(-)

diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index 88c131d05186..cd2bd573bfc3 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -385,24 +385,6 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
 	goto out;
 }
 
-static struct perf_c2c c2c = {
-	.tool = {
-		.sample		= process_sample_event,
-		.mmap		= perf_event__process_mmap,
-		.mmap2		= perf_event__process_mmap2,
-		.comm		= perf_event__process_comm,
-		.exit		= perf_event__process_exit,
-		.fork		= perf_event__process_fork,
-		.lost		= perf_event__process_lost,
-		.attr		= perf_event__process_attr,
-		.auxtrace_info  = perf_event__process_auxtrace_info,
-		.auxtrace       = perf_event__process_auxtrace,
-		.auxtrace_error = perf_event__process_auxtrace_error,
-		.ordered_events	= true,
-		.ordering_requires_timestamps = true,
-	},
-};
-
 static const char * const c2c_usage[] = {
 	"perf c2c {record|report}",
 	NULL
@@ -3070,6 +3052,19 @@ static int perf_c2c__report(int argc, const char **argv)
 	data.path  = input_name;
 	data.force = symbol_conf.force;
 
+	perf_tool__init(&c2c.tool, /*ordered_events=*/true);
+	c2c.tool.sample		= process_sample_event;
+	c2c.tool.mmap		= perf_event__process_mmap;
+	c2c.tool.mmap2		= perf_event__process_mmap2;
+	c2c.tool.comm		= perf_event__process_comm;
+	c2c.tool.exit		= perf_event__process_exit;
+	c2c.tool.fork		= perf_event__process_fork;
+	c2c.tool.lost		= perf_event__process_lost;
+	c2c.tool.attr		= perf_event__process_attr;
+	c2c.tool.auxtrace_info  = perf_event__process_auxtrace_info;
+	c2c.tool.auxtrace       = perf_event__process_auxtrace;
+	c2c.tool.auxtrace_error = perf_event__process_auxtrace_error;
+	c2c.tool.ordering_requires_timestamps = true;
 	session = perf_session__new(&data, &c2c.tool);
 	if (IS_ERR(session)) {
 		err = PTR_ERR(session);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 13/27] perf script: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (10 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 12/27] perf c2c: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 14/27] perf inject: " Ian Rogers
                   ` (14 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-script.c | 65 +++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 35 deletions(-)

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 394bce9f5338..b4fc2971335b 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -3899,38 +3899,7 @@ int cmd_script(int argc, const char **argv)
 	const char *dlfilter_file = NULL;
 	const char **__argv;
 	int i, j, err = 0;
-	struct perf_script script = {
-		.tool = {
-			.sample		 = process_sample_event,
-			.mmap		 = perf_event__process_mmap,
-			.mmap2		 = perf_event__process_mmap2,
-			.comm		 = perf_event__process_comm,
-			.namespaces	 = perf_event__process_namespaces,
-			.cgroup		 = perf_event__process_cgroup,
-			.exit		 = perf_event__process_exit,
-			.fork		 = perf_event__process_fork,
-			.attr		 = process_attr,
-			.event_update   = perf_event__process_event_update,
-#ifdef HAVE_LIBTRACEEVENT
-			.tracing_data	 = perf_event__process_tracing_data,
-#endif
-			.feature	 = process_feature_event,
-			.build_id	 = perf_event__process_build_id,
-			.id_index	 = perf_event__process_id_index,
-			.auxtrace_info	 = perf_script__process_auxtrace_info,
-			.auxtrace	 = perf_event__process_auxtrace,
-			.auxtrace_error	 = perf_event__process_auxtrace_error,
-			.stat		 = perf_event__process_stat_event,
-			.stat_round	 = process_stat_round_event,
-			.stat_config	 = process_stat_config_event,
-			.thread_map	 = process_thread_map_event,
-			.cpu_map	 = process_cpu_map_event,
-			.throttle	 = process_throttle_event,
-			.unthrottle	 = process_throttle_event,
-			.ordered_events	 = true,
-			.ordering_requires_timestamps = true,
-		},
-	};
+	struct perf_script script = {};
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,
 	};
@@ -4102,10 +4071,8 @@ int cmd_script(int argc, const char **argv)
 	data.path  = input_name;
 	data.force = symbol_conf.force;
 
-	if (unsorted_dump) {
+	if (unsorted_dump)
 		dump_trace = true;
-		script.tool.ordered_events = false;
-	}
 
 	if (symbol__validate_sym_arguments())
 		return -1;
@@ -4296,6 +4263,34 @@ int cmd_script(int argc, const char **argv)
 		use_browser = 0;
 	}
 
+	perf_tool__init(&script.tool, !unsorted_dump);
+	script.tool.sample		 = process_sample_event;
+	script.tool.mmap		 = perf_event__process_mmap;
+	script.tool.mmap2		 = perf_event__process_mmap2;
+	script.tool.comm		 = perf_event__process_comm;
+	script.tool.namespaces		 = perf_event__process_namespaces;
+	script.tool.cgroup		 = perf_event__process_cgroup;
+	script.tool.exit		 = perf_event__process_exit;
+	script.tool.fork		 = perf_event__process_fork;
+	script.tool.attr		 = process_attr;
+	script.tool.event_update	 = perf_event__process_event_update;
+#ifdef HAVE_LIBTRACEEVENT
+	script.tool.tracing_data	 = perf_event__process_tracing_data;
+#endif
+	script.tool.feature		 = process_feature_event;
+	script.tool.build_id		 = perf_event__process_build_id;
+	script.tool.id_index		 = perf_event__process_id_index;
+	script.tool.auxtrace_info	 = perf_script__process_auxtrace_info;
+	script.tool.auxtrace		 = perf_event__process_auxtrace;
+	script.tool.auxtrace_error	 = perf_event__process_auxtrace_error;
+	script.tool.stat		 = perf_event__process_stat_event;
+	script.tool.stat_round		 = process_stat_round_event;
+	script.tool.stat_config		 = process_stat_config_event;
+	script.tool.thread_map		 = process_thread_map_event;
+	script.tool.cpu_map		 = process_cpu_map_event;
+	script.tool.throttle		 = process_throttle_event;
+	script.tool.unthrottle		 = process_throttle_event;
+	script.tool.ordering_requires_timestamps = true;
 	session = perf_session__new(&data, &script.tool);
 	if (IS_ERR(session))
 		return PTR_ERR(session);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 14/27] perf inject: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (11 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 13/27] perf script: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 15/27] perf report: " Ian Rogers
                   ` (13 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-inject.c | 89 +++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 47 deletions(-)

diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index be74e2bf1f00..2866756d5060 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -2165,46 +2165,6 @@ static int __cmd_inject(struct perf_inject *inject)
 int cmd_inject(int argc, const char **argv)
 {
 	struct perf_inject inject = {
-		.tool = {
-			.sample		= perf_event__repipe_sample,
-			.read		= perf_event__repipe_sample,
-			.mmap		= perf_event__repipe,
-			.mmap2		= perf_event__repipe,
-			.comm		= perf_event__repipe,
-			.namespaces	= perf_event__repipe,
-			.cgroup		= perf_event__repipe,
-			.fork		= perf_event__repipe,
-			.exit		= perf_event__repipe,
-			.lost		= perf_event__repipe,
-			.lost_samples	= perf_event__repipe,
-			.aux		= perf_event__repipe,
-			.itrace_start	= perf_event__repipe,
-			.aux_output_hw_id = perf_event__repipe,
-			.context_switch	= perf_event__repipe,
-			.throttle	= perf_event__repipe,
-			.unthrottle	= perf_event__repipe,
-			.ksymbol	= perf_event__repipe,
-			.bpf		= perf_event__repipe,
-			.text_poke	= perf_event__repipe,
-			.attr		= perf_event__repipe_attr,
-			.event_update	= perf_event__repipe_event_update,
-			.tracing_data	= perf_event__repipe_op2_synth,
-			.finished_round	= perf_event__repipe_oe_synth,
-			.build_id	= perf_event__repipe_op2_synth,
-			.id_index	= perf_event__repipe_op2_synth,
-			.auxtrace_info	= perf_event__repipe_op2_synth,
-			.auxtrace_error	= perf_event__repipe_op2_synth,
-			.time_conv	= perf_event__repipe_op2_synth,
-			.thread_map	= perf_event__repipe_op2_synth,
-			.cpu_map	= perf_event__repipe_op2_synth,
-			.stat_config	= perf_event__repipe_op2_synth,
-			.stat		= perf_event__repipe_op2_synth,
-			.stat_round	= perf_event__repipe_op2_synth,
-			.feature	= perf_event__repipe_op2_synth,
-			.finished_init	= perf_event__repipe_op2_synth,
-			.compressed	= perf_event__repipe_op4_synth,
-			.auxtrace	= perf_event__repipe_auxtrace,
-		},
 		.input_name  = "-",
 		.samples = LIST_HEAD_INIT(inject.samples),
 		.output = {
@@ -2269,6 +2229,7 @@ int cmd_inject(int argc, const char **argv)
 		"perf inject [<options>]",
 		NULL
 	};
+	bool ordered_events;
 
 	if (!inject.itrace_synth_opts.set) {
 		/* Disable eager loading of kernel symbols that adds overhead to perf inject. */
@@ -2333,7 +2294,47 @@ int cmd_inject(int argc, const char **argv)
 		if (strcmp(inject.input_name, "-"))
 			repipe = false;
 	}
-
+	ordered_events = inject.jit_mode || inject.sched_stat ||
+		(inject.build_ids && !inject.build_id_all);
+	perf_tool__init(&inject.tool, ordered_events);
+	inject.tool.sample		= perf_event__repipe_sample;
+	inject.tool.read		= perf_event__repipe_sample;
+	inject.tool.mmap		= perf_event__repipe;
+	inject.tool.mmap2		= perf_event__repipe;
+	inject.tool.comm		= perf_event__repipe;
+	inject.tool.namespaces		= perf_event__repipe;
+	inject.tool.cgroup		= perf_event__repipe;
+	inject.tool.fork		= perf_event__repipe;
+	inject.tool.exit		= perf_event__repipe;
+	inject.tool.lost		= perf_event__repipe;
+	inject.tool.lost_samples	= perf_event__repipe;
+	inject.tool.aux			= perf_event__repipe;
+	inject.tool.itrace_start	= perf_event__repipe;
+	inject.tool.aux_output_hw_id	= perf_event__repipe;
+	inject.tool.context_switch	= perf_event__repipe;
+	inject.tool.throttle		= perf_event__repipe;
+	inject.tool.unthrottle		= perf_event__repipe;
+	inject.tool.ksymbol		= perf_event__repipe;
+	inject.tool.bpf			= perf_event__repipe;
+	inject.tool.text_poke		= perf_event__repipe;
+	inject.tool.attr		= perf_event__repipe_attr;
+	inject.tool.event_update	= perf_event__repipe_event_update;
+	inject.tool.tracing_data	= perf_event__repipe_op2_synth;
+	inject.tool.finished_round	= perf_event__repipe_oe_synth;
+	inject.tool.build_id		= perf_event__repipe_op2_synth;
+	inject.tool.id_index		= perf_event__repipe_op2_synth;
+	inject.tool.auxtrace_info	= perf_event__repipe_op2_synth;
+	inject.tool.auxtrace_error	= perf_event__repipe_op2_synth;
+	inject.tool.time_conv		= perf_event__repipe_op2_synth;
+	inject.tool.thread_map		= perf_event__repipe_op2_synth;
+	inject.tool.cpu_map		= perf_event__repipe_op2_synth;
+	inject.tool.stat_config		= perf_event__repipe_op2_synth;
+	inject.tool.stat		= perf_event__repipe_op2_synth;
+	inject.tool.stat_round		= perf_event__repipe_op2_synth;
+	inject.tool.feature		= perf_event__repipe_op2_synth;
+	inject.tool.finished_init	= perf_event__repipe_op2_synth;
+	inject.tool.compressed		= perf_event__repipe_op4_synth;
+	inject.tool.auxtrace		= perf_event__repipe_auxtrace;
 	inject.session = __perf_session__new(&data, repipe,
 					     output_fd(&inject),
 					     &inject.tool);
@@ -2372,7 +2373,6 @@ int cmd_inject(int argc, const char **argv)
 		 * mmaps. We cannot generate the buildid hit list and
 		 * inject the jit mmaps at the same time for now.
 		 */
-		inject.tool.ordered_events = true;
 		inject.tool.ordering_requires_timestamps = true;
 		if (known_build_ids != NULL) {
 			inject.known_build_ids =
@@ -2385,15 +2385,10 @@ int cmd_inject(int argc, const char **argv)
 		}
 	}
 
-	if (inject.sched_stat) {
-		inject.tool.ordered_events = true;
-	}
-
 #ifdef HAVE_JITDUMP
 	if (inject.jit_mode) {
 		inject.tool.mmap2	   = perf_event__jit_repipe_mmap2;
 		inject.tool.mmap	   = perf_event__jit_repipe_mmap;
-		inject.tool.ordered_events = true;
 		inject.tool.ordering_requires_timestamps = true;
 		/*
 		 * JIT MMAP injection injects all MMAP events in one go, so it
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 15/27] perf report: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (12 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 14/27] perf inject: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 16/27] perf stat: " Ian Rogers
                   ` (12 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-report.c | 55 ++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 28 deletions(-)

diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 2313bacea0d2..86501edd5a7d 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -799,7 +799,7 @@ static int process_attr(const struct perf_tool *tool __maybe_unused,
 
 static void stats_setup(struct report *rep)
 {
-	memset(&rep->tool, 0, sizeof(rep->tool));
+	perf_tool__init(&rep->tool, /*ordered_events=*/false);
 	rep->tool.attr = process_attr;
 	rep->tool.sample = count_sample_event;
 	rep->tool.lost_samples = count_lost_samples_event;
@@ -817,8 +817,7 @@ static int stats_print(struct report *rep)
 
 static void tasks_setup(struct report *rep)
 {
-	memset(&rep->tool, 0, sizeof(rep->tool));
-	rep->tool.ordered_events = true;
+	perf_tool__init(&rep->tool, /*ordered_events=*/true);
 	if (rep->mmaps_mode) {
 		rep->tool.mmap = perf_event__process_mmap;
 		rep->tool.mmap2 = perf_event__process_mmap2;
@@ -1273,30 +1272,6 @@ int cmd_report(int argc, const char **argv)
 		NULL
 	};
 	struct report report = {
-		.tool = {
-			.sample		 = process_sample_event,
-			.mmap		 = perf_event__process_mmap,
-			.mmap2		 = perf_event__process_mmap2,
-			.comm		 = perf_event__process_comm,
-			.namespaces	 = perf_event__process_namespaces,
-			.cgroup		 = perf_event__process_cgroup,
-			.exit		 = perf_event__process_exit,
-			.fork		 = perf_event__process_fork,
-			.lost		 = perf_event__process_lost,
-			.read		 = process_read_event,
-			.attr		 = process_attr,
-#ifdef HAVE_LIBTRACEEVENT
-			.tracing_data	 = perf_event__process_tracing_data,
-#endif
-			.build_id	 = perf_event__process_build_id,
-			.id_index	 = perf_event__process_id_index,
-			.auxtrace_info	 = perf_event__process_auxtrace_info,
-			.auxtrace	 = perf_event__process_auxtrace,
-			.event_update	 = perf_event__process_event_update,
-			.feature	 = process_feature_event,
-			.ordered_events	 = true,
-			.ordering_requires_timestamps = true,
-		},
 		.max_stack		 = PERF_MAX_STACK_DEPTH,
 		.pretty_printing_style	 = "normal",
 		.socket_filter		 = -1,
@@ -1478,6 +1453,7 @@ int cmd_report(int argc, const char **argv)
 	};
 	int ret = hists__init();
 	char sort_tmp[128];
+	bool ordered_events = true;
 
 	if (ret < 0)
 		goto exit;
@@ -1532,7 +1508,7 @@ int cmd_report(int argc, const char **argv)
 		report.tasks_mode = true;
 
 	if (dump_trace && report.disable_order)
-		report.tool.ordered_events = false;
+		ordered_events = false;
 
 	if (quiet)
 		perf_quiet_option();
@@ -1563,6 +1539,29 @@ int cmd_report(int argc, const char **argv)
 	symbol_conf.skip_empty = report.skip_empty;
 
 repeat:
+	perf_tool__init(&report.tool, ordered_events);
+	report.tool.sample		 = process_sample_event;
+	report.tool.mmap		 = perf_event__process_mmap;
+	report.tool.mmap2		 = perf_event__process_mmap2;
+	report.tool.comm		 = perf_event__process_comm;
+	report.tool.namespaces		 = perf_event__process_namespaces;
+	report.tool.cgroup		 = perf_event__process_cgroup;
+	report.tool.exit		 = perf_event__process_exit;
+	report.tool.fork		 = perf_event__process_fork;
+	report.tool.lost		 = perf_event__process_lost;
+	report.tool.read		 = process_read_event;
+	report.tool.attr		 = process_attr;
+#ifdef HAVE_LIBTRACEEVENT
+	report.tool.tracing_data	 = perf_event__process_tracing_data;
+#endif
+	report.tool.build_id		 = perf_event__process_build_id;
+	report.tool.id_index		 = perf_event__process_id_index;
+	report.tool.auxtrace_info	 = perf_event__process_auxtrace_info;
+	report.tool.auxtrace		 = perf_event__process_auxtrace;
+	report.tool.event_update	 = perf_event__process_event_update;
+	report.tool.feature		 = process_feature_event;
+	report.tool.ordering_requires_timestamps = true;
+
 	session = perf_session__new(&data, &report.tool);
 	if (IS_ERR(session)) {
 		ret = PTR_ERR(session);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 16/27] perf stat: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (13 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 15/27] perf report: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 17/27] perf annotate: " Ian Rogers
                   ` (11 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-stat.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index d460c46f15fd..a3d77a55d17f 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -2271,15 +2271,6 @@ static const char * const stat_report_usage[] = {
 };
 
 static struct perf_stat perf_stat = {
-	.tool = {
-		.attr		= perf_event__process_attr,
-		.event_update	= perf_event__process_event_update,
-		.thread_map	= process_thread_map_event,
-		.cpu_map	= process_cpu_map_event,
-		.stat_config	= process_stat_config_event,
-		.stat		= perf_event__process_stat_event,
-		.stat_round	= process_stat_round_event,
-	},
 	.aggr_mode	= AGGR_UNSET,
 	.aggr_level	= 0,
 };
@@ -2322,6 +2313,15 @@ static int __cmd_report(int argc, const char **argv)
 	perf_stat.data.path = input_name;
 	perf_stat.data.mode = PERF_DATA_MODE_READ;
 
+	perf_tool__init(&perf_stat.tool, /*ordered_events=*/false);
+	perf_stat.tool.attr		= perf_event__process_attr;
+	perf_stat.tool.event_update	= perf_event__process_event_update;
+	perf_stat.tool.thread_map	= process_thread_map_event;
+	perf_stat.tool.cpu_map		= process_cpu_map_event;
+	perf_stat.tool.stat_config	= process_stat_config_event;
+	perf_stat.tool.stat		= perf_event__process_stat_event;
+	perf_stat.tool.stat_round	= process_stat_round_event;
+
 	session = perf_session__new(&perf_stat.data, &perf_stat.tool);
 	if (IS_ERR(session))
 		return PTR_ERR(session);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 17/27] perf annotate: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (14 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 16/27] perf stat: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 18/27] perf sched: " Ian Rogers
                   ` (10 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-annotate.c | 42 +++++++++++++++++------------------
 1 file changed, 20 insertions(+), 22 deletions(-)

diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index c0685d2c8de1..598ab854b9f3 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -686,28 +686,7 @@ static const char * const annotate_usage[] = {
 
 int cmd_annotate(int argc, const char **argv)
 {
-	struct perf_annotate annotate = {
-		.tool = {
-			.sample	= process_sample_event,
-			.mmap	= perf_event__process_mmap,
-			.mmap2	= perf_event__process_mmap2,
-			.comm	= perf_event__process_comm,
-			.exit	= perf_event__process_exit,
-			.fork	= perf_event__process_fork,
-			.namespaces = perf_event__process_namespaces,
-			.attr	= perf_event__process_attr,
-			.build_id = perf_event__process_build_id,
-#ifdef HAVE_LIBTRACEEVENT
-			.tracing_data   = perf_event__process_tracing_data,
-#endif
-			.id_index	= perf_event__process_id_index,
-			.auxtrace_info	= perf_event__process_auxtrace_info,
-			.auxtrace	= perf_event__process_auxtrace,
-			.feature	= process_feature_event,
-			.ordered_events = true,
-			.ordering_requires_timestamps = true,
-		},
-	};
+	struct perf_annotate annotate = {};
 	struct perf_data data = {
 		.mode  = PERF_DATA_MODE_READ,
 	};
@@ -864,6 +843,25 @@ int cmd_annotate(int argc, const char **argv)
 
 	data.path = input_name;
 
+	perf_tool__init(&annotate.tool, /*ordered_events=*/true);
+	annotate.tool.sample	= process_sample_event;
+	annotate.tool.mmap	= perf_event__process_mmap;
+	annotate.tool.mmap2	= perf_event__process_mmap2;
+	annotate.tool.comm	= perf_event__process_comm;
+	annotate.tool.exit	= perf_event__process_exit;
+	annotate.tool.fork	= perf_event__process_fork;
+	annotate.tool.namespaces = perf_event__process_namespaces;
+	annotate.tool.attr	= perf_event__process_attr;
+	annotate.tool.build_id = perf_event__process_build_id;
+#ifdef HAVE_LIBTRACEEVENT
+	annotate.tool.tracing_data   = perf_event__process_tracing_data;
+#endif
+	annotate.tool.id_index	= perf_event__process_id_index;
+	annotate.tool.auxtrace_info	= perf_event__process_auxtrace_info;
+	annotate.tool.auxtrace	= perf_event__process_auxtrace;
+	annotate.tool.feature	= process_feature_event;
+	annotate.tool.ordering_requires_timestamps = true;
+
 	annotate.session = perf_session__new(&data, &annotate.tool);
 	if (IS_ERR(annotate.session))
 		return PTR_ERR(annotate.session);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 18/27] perf sched: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (15 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 17/27] perf annotate: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 19/27] perf mem: " Ian Rogers
                   ` (9 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-sched.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 2d24250f60fa..2b227b01d64b 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -3013,7 +3013,6 @@ static int perf_sched__timehist(struct perf_sched *sched)
 	sched->tool.tracing_data = perf_event__process_tracing_data;
 	sched->tool.build_id	 = perf_event__process_build_id;
 
-	sched->tool.ordered_events = true;
 	sched->tool.ordering_requires_timestamps = true;
 
 	symbol_conf.use_callchain = sched->show_callchain;
@@ -3511,14 +3510,6 @@ int cmd_sched(int argc, const char **argv)
 {
 	static const char default_sort_order[] = "avg, max, switch, runtime";
 	struct perf_sched sched = {
-		.tool = {
-			.sample		 = perf_sched__process_tracepoint_sample,
-			.comm		 = perf_sched__process_comm,
-			.namespaces	 = perf_event__process_namespaces,
-			.lost		 = perf_event__process_lost,
-			.fork		 = perf_sched__process_fork_event,
-			.ordered_events = true,
-		},
 		.cmp_pid	      = LIST_HEAD_INIT(sched.cmp_pid),
 		.sort_list	      = LIST_HEAD_INIT(sched.sort_list),
 		.sort_order	      = default_sort_order,
@@ -3635,6 +3626,13 @@ int cmd_sched(int argc, const char **argv)
 	};
 	int ret;
 
+	perf_tool__init(&sched.tool, /*ordered_events=*/true);
+	sched.tool.sample	 = perf_sched__process_tracepoint_sample;
+	sched.tool.comm		 = perf_sched__process_comm;
+	sched.tool.namespaces	 = perf_event__process_namespaces;
+	sched.tool.lost		 = perf_event__process_lost;
+	sched.tool.fork		 = perf_sched__process_fork_event;
+
 	argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
 					sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
 	if (!argc)
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 19/27] perf mem: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (16 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 18/27] perf sched: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 20/27] perf timechart: " Ian Rogers
                   ` (8 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-mem.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/tools/perf/builtin-mem.c b/tools/perf/builtin-mem.c
index c2038a32543c..c12ca0ec1237 100644
--- a/tools/perf/builtin-mem.c
+++ b/tools/perf/builtin-mem.c
@@ -285,7 +285,23 @@ static int report_raw_events(struct perf_mem *mem)
 		.force = mem->force,
 	};
 	int ret;
-	struct perf_session *session = perf_session__new(&data, &mem->tool);
+	struct perf_session *session;
+
+	perf_tool__init(&mem->tool, /*ordered_events=*/true);
+	mem->tool.sample		= process_sample_event;
+	mem->tool.mmap		= perf_event__process_mmap;
+	mem->tool.mmap2		= perf_event__process_mmap2;
+	mem->tool.comm		= perf_event__process_comm;
+	mem->tool.lost		= perf_event__process_lost;
+	mem->tool.fork		= perf_event__process_fork;
+	mem->tool.attr		= perf_event__process_attr;
+	mem->tool.build_id	= perf_event__process_build_id;
+	mem->tool.namespaces	= perf_event__process_namespaces;
+	mem->tool.auxtrace_info  = perf_event__process_auxtrace_info;
+	mem->tool.auxtrace       = perf_event__process_auxtrace;
+	mem->tool.auxtrace_error = perf_event__process_auxtrace_error;
+
+	session = perf_session__new(&data, &mem->tool);
 
 	if (IS_ERR(session))
 		return PTR_ERR(session);
@@ -449,21 +465,6 @@ int cmd_mem(int argc, const char **argv)
 {
 	struct stat st;
 	struct perf_mem mem = {
-		.tool = {
-			.sample		= process_sample_event,
-			.mmap		= perf_event__process_mmap,
-			.mmap2		= perf_event__process_mmap2,
-			.comm		= perf_event__process_comm,
-			.lost		= perf_event__process_lost,
-			.fork		= perf_event__process_fork,
-			.attr		= perf_event__process_attr,
-			.build_id	= perf_event__process_build_id,
-			.namespaces	= perf_event__process_namespaces,
-			.auxtrace_info  = perf_event__process_auxtrace_info,
-			.auxtrace       = perf_event__process_auxtrace,
-			.auxtrace_error = perf_event__process_auxtrace_error,
-			.ordered_events	= true,
-		},
 		.input_name		 = "perf.data",
 		/*
 		 * default to both load an store sampling
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 20/27] perf timechart: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (17 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 19/27] perf mem: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 21/27] perf diff: " Ian Rogers
                   ` (7 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-timechart.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
index 5bf818baa662..218c8b44d7be 100644
--- a/tools/perf/builtin-timechart.c
+++ b/tools/perf/builtin-timechart.c
@@ -1606,10 +1606,16 @@ static int __cmd_timechart(struct timechart *tchart, const char *output_name)
 		.mode  = PERF_DATA_MODE_READ,
 		.force = tchart->force,
 	};
-
-	struct perf_session *session = perf_session__new(&data, &tchart->tool);
+	struct perf_session *session;
 	int ret = -EINVAL;
 
+	perf_tool__init(&tchart->tool, /*ordered_events=*/true);
+	tchart->tool.comm		 = process_comm_event;
+	tchart->tool.fork		 = process_fork_event;
+	tchart->tool.exit		 = process_exit_event;
+	tchart->tool.sample		 = process_sample_event;
+
+	session = perf_session__new(&data, &tchart->tool);
 	if (IS_ERR(session))
 		return PTR_ERR(session);
 
@@ -1924,13 +1930,6 @@ parse_time(const struct option *opt, const char *arg, int __maybe_unused unset)
 int cmd_timechart(int argc, const char **argv)
 {
 	struct timechart tchart = {
-		.tool = {
-			.comm		 = process_comm_event,
-			.fork		 = process_fork_event,
-			.exit		 = process_exit_event,
-			.sample		 = process_sample_event,
-			.ordered_events	 = true,
-		},
 		.proc_num = 15,
 		.min_time = NSEC_PER_MSEC,
 		.merge_dist = 1000,
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 21/27] perf diff: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (18 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 20/27] perf timechart: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 22/27] perf data convert json: " Ian Rogers
                   ` (6 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-diff.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index 4c0567882a7a..28c5208fcdc9 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -467,21 +467,7 @@ static int diff__process_sample_event(const struct perf_tool *tool,
 	return ret;
 }
 
-static struct perf_diff pdiff = {
-	.tool = {
-		.sample	= diff__process_sample_event,
-		.mmap	= perf_event__process_mmap,
-		.mmap2	= perf_event__process_mmap2,
-		.comm	= perf_event__process_comm,
-		.exit	= perf_event__process_exit,
-		.fork	= perf_event__process_fork,
-		.lost	= perf_event__process_lost,
-		.namespaces = perf_event__process_namespaces,
-		.cgroup = perf_event__process_cgroup,
-		.ordered_events = true,
-		.ordering_requires_timestamps = true,
-	},
-};
+static struct perf_diff pdiff;
 
 static struct evsel *evsel_match(struct evsel *evsel,
 				      struct evlist *evlist)
@@ -1959,6 +1945,18 @@ int cmd_diff(int argc, const char **argv)
 	if (ret < 0)
 		return ret;
 
+	perf_tool__init(&pdiff.tool, /*ordered_events=*/true);
+	pdiff.tool.sample	= diff__process_sample_event;
+	pdiff.tool.mmap	= perf_event__process_mmap;
+	pdiff.tool.mmap2	= perf_event__process_mmap2;
+	pdiff.tool.comm	= perf_event__process_comm;
+	pdiff.tool.exit	= perf_event__process_exit;
+	pdiff.tool.fork	= perf_event__process_fork;
+	pdiff.tool.lost	= perf_event__process_lost;
+	pdiff.tool.namespaces = perf_event__process_namespaces;
+	pdiff.tool.cgroup = perf_event__process_cgroup;
+	pdiff.tool.ordering_requires_timestamps = true;
+
 	perf_config(diff__config, NULL);
 
 	argc = parse_options(argc, argv, options, diff_usage, 0);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 22/27] perf data convert json: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (19 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 21/27] perf diff: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 23/27] perf data convert ctf: " Ian Rogers
                   ` (5 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/data-convert-json.c | 43 ++++++++++++++---------------
 1 file changed, 20 insertions(+), 23 deletions(-)

diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
index 905ea9823f9d..20bfb0884e9e 100644
--- a/tools/perf/util/data-convert-json.c
+++ b/tools/perf/util/data-convert-json.c
@@ -316,39 +316,36 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
 	struct perf_session *session;
 	int fd;
 	int ret = -1;
-
 	struct convert_json c = {
-		.tool = {
-			.sample         = process_sample_event,
-			.mmap           = perf_event__process_mmap,
-			.mmap2          = perf_event__process_mmap2,
-			.comm           = perf_event__process_comm,
-			.namespaces     = perf_event__process_namespaces,
-			.cgroup         = perf_event__process_cgroup,
-			.exit           = perf_event__process_exit,
-			.fork           = perf_event__process_fork,
-			.lost           = perf_event__process_lost,
-#ifdef HAVE_LIBTRACEEVENT
-			.tracing_data   = perf_event__process_tracing_data,
-#endif
-			.build_id       = perf_event__process_build_id,
-			.id_index       = perf_event__process_id_index,
-			.auxtrace_info  = perf_event__process_auxtrace_info,
-			.auxtrace       = perf_event__process_auxtrace,
-			.event_update   = perf_event__process_event_update,
-			.ordered_events = true,
-			.ordering_requires_timestamps = true,
-		},
 		.first = true,
 		.events_count = 0,
 	};
-
 	struct perf_data data = {
 		.mode = PERF_DATA_MODE_READ,
 		.path = input_name,
 		.force = opts->force,
 	};
 
+	perf_tool__init(&c.tool, /*ordered_events=*/true);
+	c.tool.sample         = process_sample_event;
+	c.tool.mmap           = perf_event__process_mmap;
+	c.tool.mmap2          = perf_event__process_mmap2;
+	c.tool.comm           = perf_event__process_comm;
+	c.tool.namespaces     = perf_event__process_namespaces;
+	c.tool.cgroup         = perf_event__process_cgroup;
+	c.tool.exit           = perf_event__process_exit;
+	c.tool.fork           = perf_event__process_fork;
+	c.tool.lost           = perf_event__process_lost;
+#ifdef HAVE_LIBTRACEEVENT
+	c.tool.tracing_data   = perf_event__process_tracing_data;
+#endif
+	c.tool.build_id       = perf_event__process_build_id;
+	c.tool.id_index       = perf_event__process_id_index;
+	c.tool.auxtrace_info  = perf_event__process_auxtrace_info;
+	c.tool.auxtrace       = perf_event__process_auxtrace;
+	c.tool.event_update   = perf_event__process_event_update;
+	c.tool.ordering_requires_timestamps = true;
+
 	if (opts->all) {
 		pr_err("--all is currently unsupported for JSON output.\n");
 		goto err;
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 23/27] perf data convert ctf: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (20 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 22/27] perf data convert json: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 24/27] perf test event_update: Ensure tools is initialized Ian Rogers
                   ` (4 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/data-convert-bt.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/tools/perf/util/data-convert-bt.c b/tools/perf/util/data-convert-bt.c
index 9e2170604b66..021e9b1d5cc5 100644
--- a/tools/perf/util/data-convert-bt.c
+++ b/tools/perf/util/data-convert-bt.c
@@ -1607,25 +1607,23 @@ int bt_convert__perf2ctf(const char *input, const char *path,
 		.mode      = PERF_DATA_MODE_READ,
 		.force     = opts->force,
 	};
-	struct convert c = {
-		.tool = {
-			.sample          = process_sample_event,
-			.mmap            = perf_event__process_mmap,
-			.mmap2           = perf_event__process_mmap2,
-			.comm            = perf_event__process_comm,
-			.exit            = perf_event__process_exit,
-			.fork            = perf_event__process_fork,
-			.lost            = perf_event__process_lost,
-			.tracing_data    = perf_event__process_tracing_data,
-			.build_id        = perf_event__process_build_id,
-			.namespaces      = perf_event__process_namespaces,
-			.ordered_events  = true,
-			.ordering_requires_timestamps = true,
-		},
-	};
+	struct convert c = {};
 	struct ctf_writer *cw = &c.writer;
 	int err;
 
+	perf_tool__init(&c.tool, /*ordered_events=*/true);
+	c.tool.sample          = process_sample_event;
+	c.tool.mmap            = perf_event__process_mmap;
+	c.tool.mmap2           = perf_event__process_mmap2;
+	c.tool.comm            = perf_event__process_comm;
+	c.tool.exit            = perf_event__process_exit;
+	c.tool.fork            = perf_event__process_fork;
+	c.tool.lost            = perf_event__process_lost;
+	c.tool.tracing_data    = perf_event__process_tracing_data;
+	c.tool.build_id        = perf_event__process_build_id;
+	c.tool.namespaces      = perf_event__process_namespaces;
+	c.tool.ordering_requires_timestamps = true;
+
 	if (opts->all) {
 		c.tool.comm = process_comm_event;
 		c.tool.exit = process_exit_event;
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 24/27] perf test event_update: Ensure tools is initialized
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (21 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 23/27] perf data convert ctf: " Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 25/27] perf kwork: Use perf_tool__init Ian Rogers
                   ` (3 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Ensure tool is initialized to avoid lazy initialization pattern so
that more uses of struct perf_tool can be made const.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/event_update.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/perf/tests/event_update.c b/tools/perf/tests/event_update.c
index fdecad920f59..d6b4ce3ef4ee 100644
--- a/tools/perf/tests/event_update.c
+++ b/tools/perf/tests/event_update.c
@@ -103,6 +103,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtes
 	TEST_ASSERT_VAL("failed to synthesize attr update scale",
 			!perf_event__synthesize_event_update_scale(NULL, evsel, process_event_scale));
 
+	perf_tool__init(&tmp.tool, /*ordered_events=*/false);
 	tmp.name = evsel__name(evsel);
 
 	TEST_ASSERT_VAL("failed to synthesize attr update name",
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 25/27] perf kwork: Use perf_tool__init
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (22 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 24/27] perf test event_update: Ensure tools is initialized Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 26/27] perf tool: Remove perf_tool__fill_defaults Ian Rogers
                   ` (2 subsequent siblings)
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Use perf_tool__init so that more uses of struct perf_tool can be const
and not relying on perf_tool__fill_defaults.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-kwork.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
index 8ffaa80a2d1d..6a4281b8fd10 100644
--- a/tools/perf/builtin-kwork.c
+++ b/tools/perf/builtin-kwork.c
@@ -2322,12 +2322,6 @@ int cmd_kwork(int argc, const char **argv)
 {
 	static struct perf_kwork kwork = {
 		.class_list          = LIST_HEAD_INIT(kwork.class_list),
-		.tool = {
-			.mmap		= perf_event__process_mmap,
-			.mmap2		= perf_event__process_mmap2,
-			.sample		= perf_kwork__process_tracepoint_sample,
-			.ordered_events = true,
-		},
 		.atom_page_list      = LIST_HEAD_INIT(kwork.atom_page_list),
 		.sort_list           = LIST_HEAD_INIT(kwork.sort_list),
 		.cmp_id              = LIST_HEAD_INIT(kwork.cmp_id),
@@ -2462,6 +2456,11 @@ int cmd_kwork(int argc, const char **argv)
 		"record", "report", "latency", "timehist", "top", NULL
 	};
 
+	perf_tool__init(&kwork.tool, /*ordered_events=*/true);
+	kwork.tool.mmap	  = perf_event__process_mmap;
+	kwork.tool.mmap2  = perf_event__process_mmap2;
+	kwork.tool.sample = perf_kwork__process_tracepoint_sample;
+
 	argc = parse_options_subcommand(argc, argv, kwork_options,
 					kwork_subcommands, kwork_usage,
 					PARSE_OPT_STOP_AT_NON_OPTION);
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 26/27] perf tool: Remove perf_tool__fill_defaults
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (23 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 25/27] perf kwork: Use perf_tool__init Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-26 20:36 ` [PATCH v2 27/27] perf session: Constify tool Ian Rogers
  2024-06-28 17:24 ` [PATCH v2 00/27] Constify tool pointers Namhyung Kim
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Now all tools are fully initialized prior to use it has no use so
remove.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/session.c |  6 ---
 tools/perf/util/tool.c    | 89 ---------------------------------------
 tools/perf/util/tool.h    |  1 -
 3 files changed, 96 deletions(-)

diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index e1d38d91a1b6..54f0ba9b065b 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1750,8 +1750,6 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
 	ssize_t err;
 	void *p;
 
-	perf_tool__fill_defaults(tool);
-
 	head = 0;
 	cur_size = sizeof(union perf_event);
 
@@ -2159,8 +2157,6 @@ static int __perf_session__process_events(struct perf_session *session)
 	struct ui_progress prog;
 	int err;
 
-	perf_tool__fill_defaults(tool);
-
 	if (rd.data_size == 0)
 		return -1;
 
@@ -2213,8 +2209,6 @@ static int __perf_session__process_dir_events(struct perf_session *session)
 	u64 total_size = perf_data__size(session->data);
 	struct reader *rd;
 
-	perf_tool__fill_defaults(tool);
-
 	ui_progress__init_size(&prog, total_size, "Sorting events...");
 
 	nr_readers = 1;
diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c
index a4ca16e5eefe..3b7f390f26eb 100644
--- a/tools/perf/util/tool.c
+++ b/tools/perf/util/tool.c
@@ -288,95 +288,6 @@ void perf_tool__init(struct perf_tool *tool, bool ordered_events)
 	tool->finished_init = process_event_op2_stub;
 }
 
-void perf_tool__fill_defaults(struct perf_tool *tool)
-{
-	if (tool->sample == NULL)
-		tool->sample = process_event_sample_stub;
-	if (tool->mmap == NULL)
-		tool->mmap = process_event_stub;
-	if (tool->mmap2 == NULL)
-		tool->mmap2 = process_event_stub;
-	if (tool->comm == NULL)
-		tool->comm = process_event_stub;
-	if (tool->namespaces == NULL)
-		tool->namespaces = process_event_stub;
-	if (tool->cgroup == NULL)
-		tool->cgroup = process_event_stub;
-	if (tool->fork == NULL)
-		tool->fork = process_event_stub;
-	if (tool->exit == NULL)
-		tool->exit = process_event_stub;
-	if (tool->lost == NULL)
-		tool->lost = perf_event__process_lost;
-	if (tool->lost_samples == NULL)
-		tool->lost_samples = perf_event__process_lost_samples;
-	if (tool->aux == NULL)
-		tool->aux = perf_event__process_aux;
-	if (tool->itrace_start == NULL)
-		tool->itrace_start = perf_event__process_itrace_start;
-	if (tool->context_switch == NULL)
-		tool->context_switch = perf_event__process_switch;
-	if (tool->ksymbol == NULL)
-		tool->ksymbol = perf_event__process_ksymbol;
-	if (tool->bpf == NULL)
-		tool->bpf = perf_event__process_bpf;
-	if (tool->text_poke == NULL)
-		tool->text_poke = perf_event__process_text_poke;
-	if (tool->aux_output_hw_id == NULL)
-		tool->aux_output_hw_id = perf_event__process_aux_output_hw_id;
-	if (tool->read == NULL)
-		tool->read = process_event_sample_stub;
-	if (tool->throttle == NULL)
-		tool->throttle = process_event_stub;
-	if (tool->unthrottle == NULL)
-		tool->unthrottle = process_event_stub;
-	if (tool->attr == NULL)
-		tool->attr = process_event_synth_attr_stub;
-	if (tool->event_update == NULL)
-		tool->event_update = process_event_synth_event_update_stub;
-	if (tool->tracing_data == NULL)
-		tool->tracing_data = process_event_synth_tracing_data_stub;
-	if (tool->build_id == NULL)
-		tool->build_id = process_event_op2_stub;
-	if (tool->finished_round == NULL) {
-		if (tool->ordered_events)
-			tool->finished_round = perf_event__process_finished_round;
-		else
-			tool->finished_round = process_finished_round_stub;
-	}
-	if (tool->id_index == NULL)
-		tool->id_index = process_event_op2_stub;
-	if (tool->auxtrace_info == NULL)
-		tool->auxtrace_info = process_event_op2_stub;
-	if (tool->auxtrace == NULL)
-		tool->auxtrace = process_event_auxtrace_stub;
-	if (tool->auxtrace_error == NULL)
-		tool->auxtrace_error = process_event_op2_stub;
-	if (tool->thread_map == NULL)
-		tool->thread_map = process_event_thread_map_stub;
-	if (tool->cpu_map == NULL)
-		tool->cpu_map = process_event_cpu_map_stub;
-	if (tool->stat_config == NULL)
-		tool->stat_config = process_event_stat_config_stub;
-	if (tool->stat == NULL)
-		tool->stat = process_stat_stub;
-	if (tool->stat_round == NULL)
-		tool->stat_round = process_stat_round_stub;
-	if (tool->time_conv == NULL)
-		tool->time_conv = process_event_time_conv_stub;
-	if (tool->feature == NULL)
-		tool->feature = process_event_op2_stub;
-	if (tool->compressed == NULL) {
-#ifdef HAVE_ZSTD_SUPPORT
-		tool->compressed = perf_session__process_compressed_event;
-#else
-		tool->compressed = perf_session__process_compressed_event_stub;
-#endif
-	}
-	if (tool->finished_init == NULL)
-		tool->finished_init = process_event_op2_stub;
-}
-
 bool perf_tool__compressed_is_stub(const struct perf_tool *tool)
 {
 	return tool->compressed == perf_session__process_compressed_event_stub;
diff --git a/tools/perf/util/tool.h b/tools/perf/util/tool.h
index fb7e32d98dda..0874e7924687 100644
--- a/tools/perf/util/tool.h
+++ b/tools/perf/util/tool.h
@@ -89,7 +89,6 @@ struct perf_tool {
 };
 
 void perf_tool__init(struct perf_tool *tool, bool ordered_events);
-void perf_tool__fill_defaults(struct perf_tool *tool);
 
 bool perf_tool__compressed_is_stub(const struct perf_tool *tool);
 
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v2 27/27] perf session: Constify tool
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (24 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 26/27] perf tool: Remove perf_tool__fill_defaults Ian Rogers
@ 2024-06-26 20:36 ` Ian Rogers
  2024-06-28 17:24 ` [PATCH v2 00/27] Constify tool pointers Namhyung Kim
  26 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-26 20:36 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Ian Rogers, Adrian Hunter, Kan Liang, John Garry, Will Deacon,
	James Clark, Mike Leach, Leo Yan, Suzuki K Poulose, Yicong Yang,
	Jonathan Cameron, Nick Terrell, Nick Desaulniers, Oliver Upton,
	Anshuman Khandual, Song Liu, Ilkka Koskinen, Huacai Chen,
	Yanteng Si, Sun Haiyong, linux-kernel, linux-perf-users,
	linux-arm-kernel

Make tool const now that all uses are const and
perf_tool__fill_defaults won't be used. The aim is to better capture
that sessions don't mutate tools.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/session.c | 6 +++---
 tools/perf/util/session.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 54f0ba9b065b..f07ac6dd6762 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1741,7 +1741,7 @@ static int __perf_session__process_decomp_events(struct perf_session *session);
 static int __perf_session__process_pipe_events(struct perf_session *session)
 {
 	struct ordered_events *oe = &session->ordered_events;
-	struct perf_tool *tool = session->tool;
+	const struct perf_tool *tool = session->tool;
 	union perf_event *event;
 	uint32_t size, cur_size = 0;
 	void *buf = NULL;
@@ -2153,7 +2153,7 @@ static int __perf_session__process_events(struct perf_session *session)
 		.in_place_update = session->data->in_place_update,
 	};
 	struct ordered_events *oe = &session->ordered_events;
-	struct perf_tool *tool = session->tool;
+	const struct perf_tool *tool = session->tool;
 	struct ui_progress prog;
 	int err;
 
@@ -2203,7 +2203,7 @@ static int __perf_session__process_events(struct perf_session *session)
 static int __perf_session__process_dir_events(struct perf_session *session)
 {
 	struct perf_data *data = session->data;
-	struct perf_tool *tool = session->tool;
+	const struct perf_tool *tool = session->tool;
 	int i, ret, readers, nr_readers;
 	struct ui_progress prog;
 	u64 total_size = perf_data__size(session->data);
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index 9c9c531052fd..067429d882f4 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -43,7 +43,7 @@ struct perf_session {
 	u64			one_mmap_offset;
 	struct ordered_events	ordered_events;
 	struct perf_data	*data;
-	struct perf_tool	*tool;
+	const struct perf_tool	*tool;
 	u64			bytes_transferred;
 	u64			bytes_compressed;
 	struct zstd_data	zstd_data;
-- 
2.45.2.741.gdbec12cfda-goog



^ permalink raw reply related	[flat|nested] 32+ messages in thread

* Re: [PATCH v2 00/27] Constify tool pointers
  2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
                   ` (25 preceding siblings ...)
  2024-06-26 20:36 ` [PATCH v2 27/27] perf session: Constify tool Ian Rogers
@ 2024-06-28 17:24 ` Namhyung Kim
  2024-06-28 17:52   ` Ian Rogers
  26 siblings, 1 reply; 32+ messages in thread
From: Namhyung Kim @ 2024-06-28 17:24 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
	Kan Liang, John Garry, Will Deacon, James Clark, Mike Leach,
	Leo Yan, Suzuki K Poulose, Yicong Yang, Jonathan Cameron,
	Nick Terrell, Nick Desaulniers, Oliver Upton, Anshuman Khandual,
	Song Liu, Ilkka Koskinen, Huacai Chen, Yanteng Si, Sun Haiyong,
	linux-kernel, linux-perf-users, linux-arm-kernel

On Wed, Jun 26, 2024 at 01:36:02PM -0700, Ian Rogers wrote:
> struct perf_tool provides a set of function pointers that are called
> through when processing perf data. To make filling the pointers less
> cumbersome, if they are NULL perf_tools__fill_defaults will add
> default do nothing implementations.
> 
> This change refactors struct perf_tool to have an init function that
> provides the default implementation. The special use of NULL and
> perf_tools__fill_defaults are removed. As a consequence the tool
> pointers can then all be made const, which better reflects the
> behavior a particular perf command would expect of the tool and to
> some extent can reduce the cognitive load on someone working on a
> command.

I thought you actually wanted to make the tool const (rodata) but it
seems you leave it as is but treat it as const.

I'm curious if we can change the event delivery code something like:

  if (tool->func)
      tool->func(...);
  else
      stub_func(...);

Then probably we don't need to touch the tool and make it const.
Thoughts?

Thanks,
Namhyung

> 
> v2: Remove dummy tool initialization [Adrian] and make zero sized. Add
>     cs-etm fix for address sanitizer build, found necessary when
>     testing dummy tool change.
> 
> Ian Rogers (27):
>   perf auxevent: Zero size dummy tool
>   perf cs-etm: Fix address sanitizer dso build failure
>   perf tool: Constify tool pointers
>   perf tool: Move fill defaults into tool.c
>   perf tool: Add perf_tool__init
>   perf kmem: Use perf_tool__init
>   perf buildid-list: Use perf_tool__init
>   perf kvm: Use perf_tool__init
>   perf lock: Use perf_tool__init
>   perf evlist: Use perf_tool__init
>   perf record: Use perf_tool__init
>   perf c2c: Use perf_tool__init
>   perf script: Use perf_tool__init
>   perf inject: Use perf_tool__init
>   perf report: Use perf_tool__init
>   perf stat: Use perf_tool__init
>   perf annotate: Use perf_tool__init
>   perf sched: Use perf_tool__init
>   perf mem: Use perf_tool__init
>   perf timechart: Use perf_tool__init
>   perf diff: Use perf_tool__init
>   perf data convert json: Use perf_tool__init
>   perf data convert ctf: Use perf_tool__init
>   perf test event_update: Ensure tools is initialized
>   perf kwork: Use perf_tool__init
>   perf tool: Remove perf_tool__fill_defaults
>   perf session: Constify tool
> 
>  tools/perf/arch/x86/util/event.c    |   4 +-
>  tools/perf/bench/synthesize.c       |   2 +-
>  tools/perf/builtin-annotate.c       |  44 ++--
>  tools/perf/builtin-buildid-list.c   |  10 +
>  tools/perf/builtin-c2c.c            |  33 ++-
>  tools/perf/builtin-diff.c           |  30 ++-
>  tools/perf/builtin-evlist.c         |  10 +-
>  tools/perf/builtin-inject.c         | 159 +++++++------
>  tools/perf/builtin-kmem.c           |  20 +-
>  tools/perf/builtin-kvm.c            |  19 +-
>  tools/perf/builtin-kwork.c          |  33 ++-
>  tools/perf/builtin-lock.c           |  41 ++--
>  tools/perf/builtin-mem.c            |  37 +--
>  tools/perf/builtin-record.c         |  47 ++--
>  tools/perf/builtin-report.c         |  67 +++---
>  tools/perf/builtin-sched.c          |  50 ++--
>  tools/perf/builtin-script.c         | 106 ++++-----
>  tools/perf/builtin-stat.c           |  26 +--
>  tools/perf/builtin-timechart.c      |  25 +-
>  tools/perf/builtin-top.c            |   2 +-
>  tools/perf/builtin-trace.c          |   4 +-
>  tools/perf/tests/cpumap.c           |   6 +-
>  tools/perf/tests/dlfilter-test.c    |   2 +-
>  tools/perf/tests/dwarf-unwind.c     |   2 +-
>  tools/perf/tests/event_update.c     |   9 +-
>  tools/perf/tests/stat.c             |   6 +-
>  tools/perf/tests/thread-map.c       |   2 +-
>  tools/perf/util/Build               |   1 +
>  tools/perf/util/arm-spe.c           |  14 +-
>  tools/perf/util/auxtrace.c          |  12 +-
>  tools/perf/util/auxtrace.h          |  20 +-
>  tools/perf/util/bpf-event.c         |   4 +-
>  tools/perf/util/build-id.c          |  34 +--
>  tools/perf/util/build-id.h          |   8 +-
>  tools/perf/util/cs-etm.c            |  24 +-
>  tools/perf/util/data-convert-bt.c   |  34 ++-
>  tools/perf/util/data-convert-json.c |  47 ++--
>  tools/perf/util/dso.h               |  10 +
>  tools/perf/util/event.c             |  54 +++--
>  tools/perf/util/event.h             |  38 ++--
>  tools/perf/util/header.c            |   6 +-
>  tools/perf/util/header.h            |   4 +-
>  tools/perf/util/hisi-ptt.c          |   6 +-
>  tools/perf/util/intel-bts.c         |  14 +-
>  tools/perf/util/intel-pt.c          |  15 +-
>  tools/perf/util/jitdump.c           |   4 +-
>  tools/perf/util/s390-cpumsf.c       |  11 +-
>  tools/perf/util/session.c           | 342 ++--------------------------
>  tools/perf/util/session.h           |   6 +-
>  tools/perf/util/synthetic-events.c  |  80 +++----
>  tools/perf/util/synthetic-events.h  |  70 +++---
>  tools/perf/util/tool.c              | 294 ++++++++++++++++++++++++
>  tools/perf/util/tool.h              |  18 +-
>  tools/perf/util/tsc.c               |   2 +-
>  54 files changed, 967 insertions(+), 1001 deletions(-)
>  create mode 100644 tools/perf/util/tool.c
> 
> -- 
> 2.45.2.741.gdbec12cfda-goog
> 


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v2 01/27] perf auxevent: Zero size dummy tool
  2024-06-26 20:36 ` [PATCH v2 01/27] perf auxevent: Zero size dummy tool Ian Rogers
@ 2024-06-28 17:44   ` Adrian Hunter
  2024-06-28 18:34     ` Ian Rogers
  0 siblings, 1 reply; 32+ messages in thread
From: Adrian Hunter @ 2024-06-28 17:44 UTC (permalink / raw)
  To: Ian Rogers, Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Kan Liang, John Garry, Will Deacon, James Clark, Mike Leach,
	Leo Yan, Suzuki K Poulose, Yicong Yang, Jonathan Cameron,
	Nick Terrell, Nick Desaulniers, Oliver Upton, Anshuman Khandual,
	Song Liu, Ilkka Koskinen, Huacai Chen, Yanteng Si, Sun Haiyong,
	linux-kernel, linux-perf-users, linux-arm-kernel

In subject: "auxevent" -> "auxtrace"

On 26/06/24 23:36, Ian Rogers wrote:
> The dummy tool is passed as a placeholder to allow a container_of to
> get additional parameters. As the tool isn't used, make it a zero
> sized array saving 320 bytes on an x86_64 build.
> 
> s390-cpumsf's dummy tool struct was unused, so remove.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/util/arm-spe.c     | 6 +++---
>  tools/perf/util/cs-etm.c      | 6 +++---
>  tools/perf/util/intel-bts.c   | 6 +++---
>  tools/perf/util/intel-pt.c    | 7 +++----
>  tools/perf/util/s390-cpumsf.c | 5 -----
>  5 files changed, 12 insertions(+), 18 deletions(-)
> 
> diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
> index afbd5869f6bf..ee0d5064ddd4 100644
> --- a/tools/perf/util/arm-spe.c
> +++ b/tools/perf/util/arm-spe.c
> @@ -1074,8 +1074,8 @@ static void arm_spe_print_info(__u64 *arr)
>  }
>  
>  struct arm_spe_synth {
> -	struct perf_tool dummy_tool;
>  	struct perf_session *session;
> +	struct perf_tool dummy_tool[0];

[] is preferred to [0]

>  };
>  
>  static int arm_spe_event_synth(struct perf_tool *tool,
> @@ -1084,7 +1084,7 @@ static int arm_spe_event_synth(struct perf_tool *tool,
>  			       struct machine *machine __maybe_unused)
>  {
>  	struct arm_spe_synth *arm_spe_synth =
> -		      container_of(tool, struct arm_spe_synth, dummy_tool);
> +		      container_of(tool, struct arm_spe_synth, dummy_tool[0]);
>  
>  	return perf_session__deliver_synth_event(arm_spe_synth->session,
>  						 event, NULL);
> @@ -1098,7 +1098,7 @@ static int arm_spe_synth_event(struct perf_session *session,
>  	memset(&arm_spe_synth, 0, sizeof(struct arm_spe_synth));
>  	arm_spe_synth.session = session;
>  
> -	return perf_event__synthesize_attr(&arm_spe_synth.dummy_tool, attr, 1,
> +	return perf_event__synthesize_attr(arm_spe_synth.dummy_tool, attr, 1,

Passing a pointer to an object that doesn't exist there is NAK IMO

It would be better to just write another version of
perf_event__synthesize_attr().

>  					   &id, arm_spe_event_synth);
>  }
>  
> diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
> index 32818bd7cd17..9fd2967d4e3f 100644
> --- a/tools/perf/util/cs-etm.c
> +++ b/tools/perf/util/cs-etm.c
> @@ -1596,8 +1596,8 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
>  }
>  
>  struct cs_etm_synth {
> -	struct perf_tool dummy_tool;
>  	struct perf_session *session;
> +	struct perf_tool dummy_tool[0];
>  };
>  
>  static int cs_etm__event_synth(struct perf_tool *tool,
> @@ -1606,7 +1606,7 @@ static int cs_etm__event_synth(struct perf_tool *tool,
>  			       struct machine *machine __maybe_unused)
>  {
>  	struct cs_etm_synth *cs_etm_synth =
> -		      container_of(tool, struct cs_etm_synth, dummy_tool);
> +		      container_of(tool, struct cs_etm_synth, dummy_tool[0]);
>  
>  	return perf_session__deliver_synth_event(cs_etm_synth->session,
>  						 event, NULL);
> @@ -1620,7 +1620,7 @@ static int cs_etm__synth_event(struct perf_session *session,
>  	memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
>  	cs_etm_synth.session = session;
>  
> -	return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
> +	return perf_event__synthesize_attr(cs_etm_synth.dummy_tool, attr, 1,
>  					   &id, cs_etm__event_synth);
>  }
>  
> diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c
> index ec1b3bd9f530..fb8fec3a3c36 100644
> --- a/tools/perf/util/intel-bts.c
> +++ b/tools/perf/util/intel-bts.c
> @@ -738,8 +738,8 @@ static bool intel_bts_evsel_is_auxtrace(struct perf_session *session,
>  }
>  
>  struct intel_bts_synth {
> -	struct perf_tool dummy_tool;
>  	struct perf_session *session;
> +	struct perf_tool dummy_tool[0];
>  };
>  
>  static int intel_bts_event_synth(struct perf_tool *tool,
> @@ -748,7 +748,7 @@ static int intel_bts_event_synth(struct perf_tool *tool,
>  				 struct machine *machine __maybe_unused)
>  {
>  	struct intel_bts_synth *intel_bts_synth =
> -			container_of(tool, struct intel_bts_synth, dummy_tool);
> +			container_of(tool, struct intel_bts_synth, dummy_tool[0]);
>  
>  	return perf_session__deliver_synth_event(intel_bts_synth->session,
>  						 event, NULL);
> @@ -762,7 +762,7 @@ static int intel_bts_synth_event(struct perf_session *session,
>  	memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
>  	intel_bts_synth.session = session;
>  
> -	return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
> +	return perf_event__synthesize_attr(intel_bts_synth.dummy_tool, attr, 1,
>  					   &id, intel_bts_event_synth);
>  }
>  
> diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
> index d6d7b7512505..b8b90773baa2 100644
> --- a/tools/perf/util/intel-pt.c
> +++ b/tools/perf/util/intel-pt.c
> @@ -3660,8 +3660,8 @@ static int intel_pt_queue_data(struct perf_session *session,
>  }
>  
>  struct intel_pt_synth {
> -	struct perf_tool dummy_tool;
>  	struct perf_session *session;
> +	struct perf_tool dummy_tool[0];
>  };
>  
>  static int intel_pt_event_synth(struct perf_tool *tool,
> @@ -3670,7 +3670,7 @@ static int intel_pt_event_synth(struct perf_tool *tool,
>  				struct machine *machine __maybe_unused)
>  {
>  	struct intel_pt_synth *intel_pt_synth =
> -			container_of(tool, struct intel_pt_synth, dummy_tool);
> +			container_of(tool, struct intel_pt_synth, dummy_tool[0]);
>  
>  	return perf_session__deliver_synth_event(intel_pt_synth->session, event,
>  						 NULL);
> @@ -3687,8 +3687,7 @@ static int intel_pt_synth_event(struct perf_session *session, const char *name,
>  
>  	memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
>  	intel_pt_synth.session = session;
> -
> -	err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
> +	err = perf_event__synthesize_attr(intel_pt_synth.dummy_tool, attr, 1,
>  					  &id, intel_pt_event_synth);
>  	if (err)
>  		pr_err("%s: failed to synthesize '%s' event type\n",
> diff --git a/tools/perf/util/s390-cpumsf.c b/tools/perf/util/s390-cpumsf.c
> index 6fe478b0b61b..4ec583e511af 100644
> --- a/tools/perf/util/s390-cpumsf.c
> +++ b/tools/perf/util/s390-cpumsf.c
> @@ -952,11 +952,6 @@ s390_cpumsf_process_event(struct perf_session *session,
>  	return err;
>  }
>  
> -struct s390_cpumsf_synth {
> -	struct perf_tool cpumsf_tool;
> -	struct perf_session *session;
> -};

Should really be a separate patch

> -
>  static int
>  s390_cpumsf_process_auxtrace_event(struct perf_session *session,
>  				   union perf_event *event __maybe_unused,



^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v2 00/27] Constify tool pointers
  2024-06-28 17:24 ` [PATCH v2 00/27] Constify tool pointers Namhyung Kim
@ 2024-06-28 17:52   ` Ian Rogers
  2024-06-28 22:07     ` Namhyung Kim
  0 siblings, 1 reply; 32+ messages in thread
From: Ian Rogers @ 2024-06-28 17:52 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
	Kan Liang, John Garry, Will Deacon, James Clark, Mike Leach,
	Leo Yan, Suzuki K Poulose, Yicong Yang, Jonathan Cameron,
	Nick Terrell, Nick Desaulniers, Oliver Upton, Anshuman Khandual,
	Song Liu, Ilkka Koskinen, Huacai Chen, Yanteng Si, Sun Haiyong,
	linux-kernel, linux-perf-users, linux-arm-kernel

On Fri, Jun 28, 2024 at 10:25 AM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Wed, Jun 26, 2024 at 01:36:02PM -0700, Ian Rogers wrote:
> > struct perf_tool provides a set of function pointers that are called
> > through when processing perf data. To make filling the pointers less
> > cumbersome, if they are NULL perf_tools__fill_defaults will add
> > default do nothing implementations.
> >
> > This change refactors struct perf_tool to have an init function that
> > provides the default implementation. The special use of NULL and
> > perf_tools__fill_defaults are removed. As a consequence the tool
> > pointers can then all be made const, which better reflects the
> > behavior a particular perf command would expect of the tool and to
> > some extent can reduce the cognitive load on someone working on a
> > command.
>
> I thought you actually wanted to make the tool const (rodata) but it
> seems you leave it as is but treat it as const.

So I think that is a next step on top of these changes but it would
need something a bit special as we want to default initialize some
fields but then initialize others. Something like (which wouldn't
work):

.tool = DEFAULT_TOOL_STUBS({
               .sample         = process_sample_event,
               .fork           = perf_event__process_fork,
               .exit           = perf_event__process_exit,
               .comm           = perf_event__process_comm,
               .namespaces     = perf_event__process_namespaces,
               .mmap           = build_id__process_mmap,
               .mmap2          = build_id__process_mmap2,
               .itrace_start   = process_timestamp_boundary,
               .aux            = process_timestamp_boundary})

Being const is just saying hey all these event callbacks aren't going
to mutate the tool, something I wanted to rule out as part of a change
I'm working on.

> I'm curious if we can change the event delivery code something like:
>
>   if (tool->func)
>       tool->func(...);
>   else
>       stub_func(...);
>
> Then probably we don't need to touch the tool and make it const.
> Thoughts?

It works but the approach needs to change all tool func callers. I
think it is also more obvious as an API to have a default value and
override it, rather than giving special properties to NULL that
callers should adhere to - we're doing a kind of poor man's virtual
method dispatch and you wouldn't typically expect a NULL check as part
of that.

Thanks,
Ian

> Thanks,
> Namhyung


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v2 01/27] perf auxevent: Zero size dummy tool
  2024-06-28 17:44   ` Adrian Hunter
@ 2024-06-28 18:34     ` Ian Rogers
  0 siblings, 0 replies; 32+ messages in thread
From: Ian Rogers @ 2024-06-28 18:34 UTC (permalink / raw)
  To: Adrian Hunter
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
	Kan Liang, John Garry, Will Deacon, James Clark, Mike Leach,
	Leo Yan, Suzuki K Poulose, Yicong Yang, Jonathan Cameron,
	Nick Terrell, Nick Desaulniers, Oliver Upton, Anshuman Khandual,
	Song Liu, Ilkka Koskinen, Huacai Chen, Yanteng Si, Sun Haiyong,
	linux-kernel, linux-perf-users, linux-arm-kernel

On Fri, Jun 28, 2024 at 10:45 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> In subject: "auxevent" -> "auxtrace"

Ack.

> On 26/06/24 23:36, Ian Rogers wrote:
> > The dummy tool is passed as a placeholder to allow a container_of to
> > get additional parameters. As the tool isn't used, make it a zero
> > sized array saving 320 bytes on an x86_64 build.
> >
> > s390-cpumsf's dummy tool struct was unused, so remove.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/util/arm-spe.c     | 6 +++---
> >  tools/perf/util/cs-etm.c      | 6 +++---
> >  tools/perf/util/intel-bts.c   | 6 +++---
> >  tools/perf/util/intel-pt.c    | 7 +++----
> >  tools/perf/util/s390-cpumsf.c | 5 -----
> >  5 files changed, 12 insertions(+), 18 deletions(-)
> >
> > diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
> > index afbd5869f6bf..ee0d5064ddd4 100644
> > --- a/tools/perf/util/arm-spe.c
> > +++ b/tools/perf/util/arm-spe.c
> > @@ -1074,8 +1074,8 @@ static void arm_spe_print_info(__u64 *arr)
> >  }
> >
> >  struct arm_spe_synth {
> > -     struct perf_tool dummy_tool;
> >       struct perf_session *session;
> > +     struct perf_tool dummy_tool[0];
>
> [] is preferred to [0]
>
> >  };
> >
> >  static int arm_spe_event_synth(struct perf_tool *tool,
> > @@ -1084,7 +1084,7 @@ static int arm_spe_event_synth(struct perf_tool *tool,
> >                              struct machine *machine __maybe_unused)
> >  {
> >       struct arm_spe_synth *arm_spe_synth =
> > -                   container_of(tool, struct arm_spe_synth, dummy_tool);
> > +                   container_of(tool, struct arm_spe_synth, dummy_tool[0]);
> >
> >       return perf_session__deliver_synth_event(arm_spe_synth->session,
> >                                                event, NULL);
> > @@ -1098,7 +1098,7 @@ static int arm_spe_synth_event(struct perf_session *session,
> >       memset(&arm_spe_synth, 0, sizeof(struct arm_spe_synth));
> >       arm_spe_synth.session = session;
> >
> > -     return perf_event__synthesize_attr(&arm_spe_synth.dummy_tool, attr, 1,
> > +     return perf_event__synthesize_attr(arm_spe_synth.dummy_tool, attr, 1,
>
> Passing a pointer to an object that doesn't exist there is NAK IMO

Not disagreeing but I think it is at least more intention revealing
and efficient than passing a struct that isn't initialized because we
know the tool won't be used.

> It would be better to just write another version of
> perf_event__synthesize_attr().

I think we've gotten ourselves into this mess because we're doing poor
man's OO and we're trying to pass values via a tools/container_of
rather than just pass a value. I'm not keen on making this change
larger but I'll see if this can be done and we just explicitly pass
some "void* data" like value.

Thanks,
Ian

> >                                          &id, arm_spe_event_synth);
> >  }
> >
> > diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
> > index 32818bd7cd17..9fd2967d4e3f 100644
> > --- a/tools/perf/util/cs-etm.c
> > +++ b/tools/perf/util/cs-etm.c
> > @@ -1596,8 +1596,8 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
> >  }
> >
> >  struct cs_etm_synth {
> > -     struct perf_tool dummy_tool;
> >       struct perf_session *session;
> > +     struct perf_tool dummy_tool[0];
> >  };
> >
> >  static int cs_etm__event_synth(struct perf_tool *tool,
> > @@ -1606,7 +1606,7 @@ static int cs_etm__event_synth(struct perf_tool *tool,
> >                              struct machine *machine __maybe_unused)
> >  {
> >       struct cs_etm_synth *cs_etm_synth =
> > -                   container_of(tool, struct cs_etm_synth, dummy_tool);
> > +                   container_of(tool, struct cs_etm_synth, dummy_tool[0]);
> >
> >       return perf_session__deliver_synth_event(cs_etm_synth->session,
> >                                                event, NULL);
> > @@ -1620,7 +1620,7 @@ static int cs_etm__synth_event(struct perf_session *session,
> >       memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
> >       cs_etm_synth.session = session;
> >
> > -     return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
> > +     return perf_event__synthesize_attr(cs_etm_synth.dummy_tool, attr, 1,
> >                                          &id, cs_etm__event_synth);
> >  }
> >
> > diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c
> > index ec1b3bd9f530..fb8fec3a3c36 100644
> > --- a/tools/perf/util/intel-bts.c
> > +++ b/tools/perf/util/intel-bts.c
> > @@ -738,8 +738,8 @@ static bool intel_bts_evsel_is_auxtrace(struct perf_session *session,
> >  }
> >
> >  struct intel_bts_synth {
> > -     struct perf_tool dummy_tool;
> >       struct perf_session *session;
> > +     struct perf_tool dummy_tool[0];
> >  };
> >
> >  static int intel_bts_event_synth(struct perf_tool *tool,
> > @@ -748,7 +748,7 @@ static int intel_bts_event_synth(struct perf_tool *tool,
> >                                struct machine *machine __maybe_unused)
> >  {
> >       struct intel_bts_synth *intel_bts_synth =
> > -                     container_of(tool, struct intel_bts_synth, dummy_tool);
> > +                     container_of(tool, struct intel_bts_synth, dummy_tool[0]);
> >
> >       return perf_session__deliver_synth_event(intel_bts_synth->session,
> >                                                event, NULL);
> > @@ -762,7 +762,7 @@ static int intel_bts_synth_event(struct perf_session *session,
> >       memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
> >       intel_bts_synth.session = session;
> >
> > -     return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
> > +     return perf_event__synthesize_attr(intel_bts_synth.dummy_tool, attr, 1,
> >                                          &id, intel_bts_event_synth);
> >  }
> >
> > diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
> > index d6d7b7512505..b8b90773baa2 100644
> > --- a/tools/perf/util/intel-pt.c
> > +++ b/tools/perf/util/intel-pt.c
> > @@ -3660,8 +3660,8 @@ static int intel_pt_queue_data(struct perf_session *session,
> >  }
> >
> >  struct intel_pt_synth {
> > -     struct perf_tool dummy_tool;
> >       struct perf_session *session;
> > +     struct perf_tool dummy_tool[0];
> >  };
> >
> >  static int intel_pt_event_synth(struct perf_tool *tool,
> > @@ -3670,7 +3670,7 @@ static int intel_pt_event_synth(struct perf_tool *tool,
> >                               struct machine *machine __maybe_unused)
> >  {
> >       struct intel_pt_synth *intel_pt_synth =
> > -                     container_of(tool, struct intel_pt_synth, dummy_tool);
> > +                     container_of(tool, struct intel_pt_synth, dummy_tool[0]);
> >
> >       return perf_session__deliver_synth_event(intel_pt_synth->session, event,
> >                                                NULL);
> > @@ -3687,8 +3687,7 @@ static int intel_pt_synth_event(struct perf_session *session, const char *name,
> >
> >       memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
> >       intel_pt_synth.session = session;
> > -
> > -     err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
> > +     err = perf_event__synthesize_attr(intel_pt_synth.dummy_tool, attr, 1,
> >                                         &id, intel_pt_event_synth);
> >       if (err)
> >               pr_err("%s: failed to synthesize '%s' event type\n",
> > diff --git a/tools/perf/util/s390-cpumsf.c b/tools/perf/util/s390-cpumsf.c
> > index 6fe478b0b61b..4ec583e511af 100644
> > --- a/tools/perf/util/s390-cpumsf.c
> > +++ b/tools/perf/util/s390-cpumsf.c
> > @@ -952,11 +952,6 @@ s390_cpumsf_process_event(struct perf_session *session,
> >       return err;
> >  }
> >
> > -struct s390_cpumsf_synth {
> > -     struct perf_tool cpumsf_tool;
> > -     struct perf_session *session;
> > -};
>
> Should really be a separate patch
>
> > -
> >  static int
> >  s390_cpumsf_process_auxtrace_event(struct perf_session *session,
> >                                  union perf_event *event __maybe_unused,
>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v2 00/27] Constify tool pointers
  2024-06-28 17:52   ` Ian Rogers
@ 2024-06-28 22:07     ` Namhyung Kim
  0 siblings, 0 replies; 32+ messages in thread
From: Namhyung Kim @ 2024-06-28 22:07 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
	Kan Liang, John Garry, Will Deacon, James Clark, Mike Leach,
	Leo Yan, Suzuki K Poulose, Yicong Yang, Jonathan Cameron,
	Nick Terrell, Nick Desaulniers, Oliver Upton, Anshuman Khandual,
	Song Liu, Ilkka Koskinen, Huacai Chen, Yanteng Si, Sun Haiyong,
	linux-kernel, linux-perf-users, linux-arm-kernel

On Fri, Jun 28, 2024 at 10:52 AM Ian Rogers <irogers@google.com> wrote:
>
> On Fri, Jun 28, 2024 at 10:25 AM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > On Wed, Jun 26, 2024 at 01:36:02PM -0700, Ian Rogers wrote:
> > > struct perf_tool provides a set of function pointers that are called
> > > through when processing perf data. To make filling the pointers less
> > > cumbersome, if they are NULL perf_tools__fill_defaults will add
> > > default do nothing implementations.
> > >
> > > This change refactors struct perf_tool to have an init function that
> > > provides the default implementation. The special use of NULL and
> > > perf_tools__fill_defaults are removed. As a consequence the tool
> > > pointers can then all be made const, which better reflects the
> > > behavior a particular perf command would expect of the tool and to
> > > some extent can reduce the cognitive load on someone working on a
> > > command.
> >
> > I thought you actually wanted to make the tool const (rodata) but it
> > seems you leave it as is but treat it as const.
>
> So I think that is a next step on top of these changes but it would
> need something a bit special as we want to default initialize some
> fields but then initialize others. Something like (which wouldn't
> work):
>
> .tool = DEFAULT_TOOL_STUBS({
>                .sample         = process_sample_event,
>                .fork           = perf_event__process_fork,
>                .exit           = perf_event__process_exit,
>                .comm           = perf_event__process_comm,
>                .namespaces     = perf_event__process_namespaces,
>                .mmap           = build_id__process_mmap,
>                .mmap2          = build_id__process_mmap2,
>                .itrace_start   = process_timestamp_boundary,
>                .aux            = process_timestamp_boundary})
>
> Being const is just saying hey all these event callbacks aren't going
> to mutate the tool, something I wanted to rule out as part of a change
> I'm working on.
>
> > I'm curious if we can change the event delivery code something like:
> >
> >   if (tool->func)
> >       tool->func(...);
> >   else
> >       stub_func(...);
> >
> > Then probably we don't need to touch the tool and make it const.
> > Thoughts?
>
> It works but the approach needs to change all tool func callers. I
> think it is also more obvious as an API to have a default value and
> override it, rather than giving special properties to NULL that
> callers should adhere to - we're doing a kind of poor man's virtual
> method dispatch and you wouldn't typically expect a NULL check as part
> of that.

I guess we only have a few callers in util/session.c.

Thanks,
Namhyung


^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2024-06-28 22:08 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-26 20:36 [PATCH v2 00/27] Constify tool pointers Ian Rogers
2024-06-26 20:36 ` [PATCH v2 01/27] perf auxevent: Zero size dummy tool Ian Rogers
2024-06-28 17:44   ` Adrian Hunter
2024-06-28 18:34     ` Ian Rogers
2024-06-26 20:36 ` [PATCH v2 02/27] perf cs-etm: Fix address sanitizer dso build failure Ian Rogers
2024-06-26 20:36 ` [PATCH v2 04/27] perf tool: Move fill defaults into tool.c Ian Rogers
2024-06-26 20:36 ` [PATCH v2 05/27] perf tool: Add perf_tool__init Ian Rogers
2024-06-26 20:36 ` [PATCH v2 06/27] perf kmem: Use perf_tool__init Ian Rogers
2024-06-26 20:36 ` [PATCH v2 07/27] perf buildid-list: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 08/27] perf kvm: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 09/27] perf lock: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 10/27] perf evlist: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 11/27] perf record: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 12/27] perf c2c: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 13/27] perf script: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 14/27] perf inject: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 15/27] perf report: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 16/27] perf stat: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 17/27] perf annotate: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 18/27] perf sched: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 19/27] perf mem: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 20/27] perf timechart: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 21/27] perf diff: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 22/27] perf data convert json: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 23/27] perf data convert ctf: " Ian Rogers
2024-06-26 20:36 ` [PATCH v2 24/27] perf test event_update: Ensure tools is initialized Ian Rogers
2024-06-26 20:36 ` [PATCH v2 25/27] perf kwork: Use perf_tool__init Ian Rogers
2024-06-26 20:36 ` [PATCH v2 26/27] perf tool: Remove perf_tool__fill_defaults Ian Rogers
2024-06-26 20:36 ` [PATCH v2 27/27] perf session: Constify tool Ian Rogers
2024-06-28 17:24 ` [PATCH v2 00/27] Constify tool pointers Namhyung Kim
2024-06-28 17:52   ` Ian Rogers
2024-06-28 22:07     ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).