* [PATCH v6 01/27] perf auxtrace: Remove dummy tools
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
@ 2024-07-18 0:59 ` Ian Rogers
2024-07-18 0:59 ` [PATCH v6 02/27] perf s390-cpumsf: Remove unused struct Ian Rogers
` (25 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 0:59 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, Athira Rajeev,
Huacai Chen, Yanteng Si, Sun Haiyong, linux-kernel,
linux-perf-users, linux-arm-kernel
Cc: Leo Yan
Add perf_session__deliver_synth_attr_event that synthesizes a
perf_record_header_attr event with one id. Remove use of
perf_event__synthesize_attr that necessitates the use of the dummy
tool in order to pass the session.
Signed-off-by: Ian Rogers <irogers@google.com>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Adrian Hunter <adrian.hunter@intel.com>
Tested-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/util/arm-spe.c | 49 ++++++++-----------------------------
tools/perf/util/cs-etm.c | 33 ++-----------------------
tools/perf/util/intel-bts.c | 31 +----------------------
tools/perf/util/intel-pt.c | 24 +-----------------
tools/perf/util/session.c | 24 ++++++++++++++++++
tools/perf/util/session.h | 3 +++
6 files changed, 41 insertions(+), 123 deletions(-)
diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index afbd5869f6bf..fa40f3cb6266 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -1073,35 +1073,6 @@ static void arm_spe_print_info(__u64 *arr)
fprintf(stdout, arm_spe_info_fmts[ARM_SPE_PMU_TYPE], arr[ARM_SPE_PMU_TYPE]);
}
-struct arm_spe_synth {
- struct perf_tool dummy_tool;
- struct perf_session *session;
-};
-
-static int arm_spe_event_synth(struct perf_tool *tool,
- union perf_event *event,
- struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
-{
- struct arm_spe_synth *arm_spe_synth =
- container_of(tool, struct arm_spe_synth, dummy_tool);
-
- return perf_session__deliver_synth_event(arm_spe_synth->session,
- event, NULL);
-}
-
-static int arm_spe_synth_event(struct perf_session *session,
- struct perf_event_attr *attr, u64 id)
-{
- struct arm_spe_synth arm_spe_synth;
-
- 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,
- &id, arm_spe_event_synth);
-}
-
static void arm_spe_set_event_name(struct evlist *evlist, u64 id,
const char *name)
{
@@ -1172,7 +1143,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
spe->sample_flc = true;
/* Level 1 data cache miss */
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->l1d_miss_id = id;
@@ -1180,7 +1151,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
id += 1;
/* Level 1 data cache access */
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->l1d_access_id = id;
@@ -1192,7 +1163,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
spe->sample_llc = true;
/* Last level cache miss */
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->llc_miss_id = id;
@@ -1200,7 +1171,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
id += 1;
/* Last level cache access */
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->llc_access_id = id;
@@ -1212,7 +1183,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
spe->sample_tlb = true;
/* TLB miss */
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->tlb_miss_id = id;
@@ -1220,7 +1191,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
id += 1;
/* TLB access */
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->tlb_access_id = id;
@@ -1232,7 +1203,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
spe->sample_branch = true;
/* Branch miss */
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->branch_miss_id = id;
@@ -1244,7 +1215,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
spe->sample_remote_access = true;
/* Remote access */
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->remote_access_id = id;
@@ -1255,7 +1226,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
if (spe->synth_opts.mem) {
spe->sample_memory = true;
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->memory_id = id;
@@ -1276,7 +1247,7 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
attr.config = PERF_COUNT_HW_INSTRUCTIONS;
attr.sample_period = spe->synth_opts.period;
spe->instructions_sample_period = attr.sample_period;
- err = arm_spe_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
spe->instructions_id = id;
diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index 5e9fbcfad7d4..7fd976ea954b 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -1595,35 +1595,6 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
return ret;
}
-struct cs_etm_synth {
- struct perf_tool dummy_tool;
- struct perf_session *session;
-};
-
-static int cs_etm__event_synth(struct perf_tool *tool,
- union perf_event *event,
- struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
-{
- struct cs_etm_synth *cs_etm_synth =
- container_of(tool, struct cs_etm_synth, dummy_tool);
-
- return perf_session__deliver_synth_event(cs_etm_synth->session,
- event, NULL);
-}
-
-static int cs_etm__synth_event(struct perf_session *session,
- struct perf_event_attr *attr, u64 id)
-{
- struct cs_etm_synth cs_etm_synth;
-
- 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,
- &id, cs_etm__event_synth);
-}
-
static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
struct perf_session *session)
{
@@ -1675,7 +1646,7 @@ static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
attr.sample_period = 1;
attr.sample_type |= PERF_SAMPLE_ADDR;
- err = cs_etm__synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
etm->branches_sample_type = attr.sample_type;
@@ -1698,7 +1669,7 @@ static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
attr.config = PERF_COUNT_HW_INSTRUCTIONS;
attr.sample_period = etm->synth_opts.period;
etm->instructions_sample_period = attr.sample_period;
- err = cs_etm__synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
etm->instructions_sample_type = attr.sample_type;
diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c
index ec1b3bd9f530..160d346cb450 100644
--- a/tools/perf/util/intel-bts.c
+++ b/tools/perf/util/intel-bts.c
@@ -737,35 +737,6 @@ static bool intel_bts_evsel_is_auxtrace(struct perf_session *session,
return evsel->core.attr.type == bts->pmu_type;
}
-struct intel_bts_synth {
- struct perf_tool dummy_tool;
- struct perf_session *session;
-};
-
-static int intel_bts_event_synth(struct perf_tool *tool,
- union perf_event *event,
- struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
-{
- struct intel_bts_synth *intel_bts_synth =
- container_of(tool, struct intel_bts_synth, dummy_tool);
-
- return perf_session__deliver_synth_event(intel_bts_synth->session,
- event, NULL);
-}
-
-static int intel_bts_synth_event(struct perf_session *session,
- struct perf_event_attr *attr, u64 id)
-{
- struct intel_bts_synth intel_bts_synth;
-
- 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,
- &id, intel_bts_event_synth);
-}
-
static int intel_bts_synth_events(struct intel_bts *bts,
struct perf_session *session)
{
@@ -814,7 +785,7 @@ static int intel_bts_synth_events(struct intel_bts *bts,
attr.sample_type |= PERF_SAMPLE_ADDR;
pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
id, (u64)attr.sample_type);
- err = intel_bts_synth_event(session, &attr, id);
+ err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err) {
pr_err("%s: failed to synthesize 'branches' event type\n",
__func__);
diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index d6d7b7512505..1fc03bfd14c3 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -3659,37 +3659,15 @@ static int intel_pt_queue_data(struct perf_session *session,
data_offset, timestamp);
}
-struct intel_pt_synth {
- struct perf_tool dummy_tool;
- struct perf_session *session;
-};
-
-static int intel_pt_event_synth(struct perf_tool *tool,
- union perf_event *event,
- struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
-{
- struct intel_pt_synth *intel_pt_synth =
- container_of(tool, struct intel_pt_synth, dummy_tool);
-
- return perf_session__deliver_synth_event(intel_pt_synth->session, event,
- NULL);
-}
-
static int intel_pt_synth_event(struct perf_session *session, const char *name,
struct perf_event_attr *attr, u64 id)
{
- struct intel_pt_synth intel_pt_synth;
int err;
pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
name, id, (u64)attr->sample_type);
- 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,
- &id, intel_pt_event_synth);
+ err = perf_session__deliver_synth_attr_event(session, attr, id);
if (err)
pr_err("%s: failed to synthesize '%s' event type\n",
__func__, name);
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 5596bed1b8c8..18a8f4a84045 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1764,6 +1764,30 @@ int perf_session__deliver_synth_event(struct perf_session *session,
return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0, NULL);
}
+int perf_session__deliver_synth_attr_event(struct perf_session *session,
+ const struct perf_event_attr *attr,
+ u64 id)
+{
+ union {
+ struct {
+ struct perf_record_header_attr attr;
+ u64 ids[1];
+ } attr_id;
+ union perf_event ev;
+ } ev = {
+ .attr_id.attr.header.type = PERF_RECORD_HEADER_ATTR,
+ .attr_id.attr.header.size = sizeof(ev.attr_id),
+ .attr_id.ids[0] = id,
+ };
+
+ if (attr->size != sizeof(ev.attr_id.attr.attr)) {
+ pr_debug("Unexpected perf_event_attr size\n");
+ return -EINVAL;
+ }
+ ev.attr_id.attr.attr = *attr;
+ return perf_session__deliver_synth_event(session, &ev.ev, NULL);
+}
+
static void event_swap(union perf_event *event, bool sample_id_all)
{
perf_event__swap_op swap;
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index 4c29dc86956f..cc28976bb00e 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -154,6 +154,9 @@ extern volatile int session_done;
int perf_session__deliver_synth_event(struct perf_session *session,
union perf_event *event,
struct perf_sample *sample);
+int perf_session__deliver_synth_attr_event(struct perf_session *session,
+ const struct perf_event_attr *attr,
+ u64 id);
int perf_session__dsos_hit_all(struct perf_session *session);
--
2.45.2.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 02/27] perf s390-cpumsf: Remove unused struct
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
2024-07-18 0:59 ` [PATCH v6 01/27] perf auxtrace: Remove dummy tools Ian Rogers
@ 2024-07-18 0:59 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 04/27] perf tool: Move fill defaults into tool.c Ian Rogers
` (24 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 0:59 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, Athira Rajeev,
Huacai Chen, Yanteng Si, Sun Haiyong, linux-kernel,
linux-perf-users, linux-arm-kernel
struct s390_cpumsf_synth was likely cargo culted from other auxtrace
examples. It has no users, so remove.
Signed-off-by: Ian Rogers <irogers@google.com>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
---
tools/perf/util/s390-cpumsf.c | 5 -----
1 file changed, 5 deletions(-)
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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 04/27] perf tool: Move fill defaults into tool.c
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
2024-07-18 0:59 ` [PATCH v6 01/27] perf auxtrace: Remove dummy tools Ian Rogers
2024-07-18 0:59 ` [PATCH v6 02/27] perf s390-cpumsf: Remove unused struct Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 05/27] perf tool: Add perf_tool__init Ian Rogers
` (23 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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 0f18fe81ef0b..f3633342374a 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -65,6 +65,7 @@ perf-util-y += map.o
perf-util-y += maps.o
perf-util-y += pstack.o
perf-util-y += session.o
+perf-util-y += tool.o
perf-util-y += sample-raw.o
perf-util-y += s390-sample-raw.o
perf-util-y += amd-sample-raw.o
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 7e9827e61d1b..5359ee055c85 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 6886cc85600f..7f69baeae7fb 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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 05/27] perf tool: Add perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (2 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 04/27] perf tool: Move fill defaults into tool.c Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 06/27] perf kmem: Use perf_tool__init Ian Rogers
` (22 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 06/27] perf kmem: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (3 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 05/27] perf tool: Add perf_tool__init Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 07/27] perf buildid-list: " Ian Rogers
` (21 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 07/27] perf buildid-list: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (4 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 06/27] perf kmem: Use perf_tool__init Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 08/27] perf kvm: " Ian Rogers
` (20 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 08/27] perf kvm: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (5 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 07/27] perf buildid-list: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 09/27] perf lock: " Ian Rogers
` (19 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 09/27] perf lock: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (6 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 08/27] perf kvm: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 10/27] perf evlist: " Ian Rogers
` (18 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 10/27] perf evlist: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (7 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 09/27] perf lock: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 11/27] perf record: " Ian Rogers
` (17 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 11/27] perf record: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (8 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 10/27] perf evlist: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 12/27] perf c2c: " Ian Rogers
` (16 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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 f27f725913d7..12a5170d9193 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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 12/27] perf c2c: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (9 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 11/27] perf record: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 13/27] perf script: " Ian Rogers
` (15 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 13/27] perf script: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (10 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 12/27] perf c2c: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 14/27] perf inject: " Ian Rogers
` (14 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 14/27] perf inject: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (11 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 13/27] perf script: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 15/27] perf report: " Ian Rogers
` (13 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 15/27] perf report: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (12 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 14/27] perf inject: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 16/27] perf stat: " Ian Rogers
` (12 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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 c402d9b3dca6..cd5caf1c974c 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;
@@ -1272,30 +1271,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,
@@ -1477,6 +1452,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;
@@ -1531,7 +1507,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();
@@ -1562,6 +1538,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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 16/27] perf stat: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (13 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 15/27] perf report: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 17/27] perf annotate: " Ian Rogers
` (11 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 17/27] perf annotate: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (14 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 16/27] perf stat: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 18/27] perf sched: " Ian Rogers
` (10 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 18/27] perf sched: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (15 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 17/27] perf annotate: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 19/27] perf mem: " Ian Rogers
` (9 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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 2c60bd3a8149..34fe8e540c43 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -3100,7 +3100,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;
@@ -3605,14 +3604,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,
@@ -3733,6 +3724,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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 19/27] perf mem: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (16 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 18/27] perf sched: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 20/27] perf timechart: " Ian Rogers
` (8 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 20/27] perf timechart: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (17 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 19/27] perf mem: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 21/27] perf diff: " Ian Rogers
` (7 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 21/27] perf diff: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (18 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 20/27] perf timechart: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 22/27] perf data convert json: " Ian Rogers
` (6 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 22/27] perf data convert json: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (19 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 21/27] perf diff: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 23/27] perf data convert ctf: " Ian Rogers
` (5 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 23/27] perf data convert ctf: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (20 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 22/27] perf data convert json: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 24/27] perf test event_update: Ensure tools is initialized Ian Rogers
` (4 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 24/27] perf test event_update: Ensure tools is initialized
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (21 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 23/27] perf data convert ctf: " Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 25/27] perf kwork: Use perf_tool__init Ian Rogers
` (3 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 25/27] perf kwork: Use perf_tool__init
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (22 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 24/27] perf test event_update: Ensure tools is initialized Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 26/27] perf tool: Remove perf_tool__fill_defaults Ian Rogers
` (2 subsequent siblings)
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 26/27] perf tool: Remove perf_tool__fill_defaults
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (23 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 25/27] perf kwork: Use perf_tool__init Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-18 1:00 ` [PATCH v6 27/27] perf session: Constify tool Ian Rogers
2024-07-19 8:50 ` [PATCH v6 00/27] Constify tool pointers Adrian Hunter
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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 5359ee055c85..ad5da61d2330 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1776,8 +1776,6 @@ static int __perf_session__process_pipe_events(struct perf_session *session)
void *p;
bool update_prog = false;
- perf_tool__fill_defaults(tool);
-
/*
* If it's from a file saving pipe data (by redirection), it would have
* a file name other than "-". Then we can get the total size and show
@@ -2201,8 +2199,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;
@@ -2255,8 +2251,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, "Processing 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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* [PATCH v6 27/27] perf session: Constify tool
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (24 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 26/27] perf tool: Remove perf_tool__fill_defaults Ian Rogers
@ 2024-07-18 1:00 ` Ian Rogers
2024-07-19 8:50 ` [PATCH v6 00/27] Constify tool pointers Adrian Hunter
26 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-18 1:00 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, Athira Rajeev,
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>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.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 ad5da61d2330..0c7cc092887d 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1765,7 +1765,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;
struct ui_progress prog;
union perf_event *event;
uint32_t size, cur_size = 0;
@@ -2195,7 +2195,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;
@@ -2245,7 +2245,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 7f69baeae7fb..7c8dd6956330 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.1089.g2a221341d9-goog
^ permalink raw reply related [flat|nested] 39+ messages in thread* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-18 0:59 [PATCH v6 00/27] Constify tool pointers Ian Rogers
` (25 preceding siblings ...)
2024-07-18 1:00 ` [PATCH v6 27/27] perf session: Constify tool Ian Rogers
@ 2024-07-19 8:50 ` Adrian Hunter
2024-07-19 16:26 ` Ian Rogers
26 siblings, 1 reply; 39+ messages in thread
From: Adrian Hunter @ 2024-07-19 8:50 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, Athira Rajeev, Huacai Chen, Yanteng Si,
Sun Haiyong, linux-kernel, linux-perf-users, linux-arm-kernel
On 18/07/24 03:59, 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.
>
> v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
The tags were really meant only for patch 1, the email that was replied to.
But now for patches 2 and 3:
Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
Looking at patches 4 to 25, they do not seem to offer any benefit.
Instead of patch 26, presumably perf_tool__fill_defaults() could
be moved to __perf_session__new(), which perhaps would allow
patch 27 as it is.
> v5: Rebase dropping asan fix merged by Namhyung.
> v4: Simplify perf_session__deliver_synth_attr_event following Adrian's
> suggestions.
> v3: Just remove auxtrace dummy tools [Adrian] and make s390-cpumsf
> struct removal its own patch [Adrian].
> 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 auxtrace: Remove dummy tools
> perf s390-cpumsf: Remove unused struct
> 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 | 55 +----
> 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 | 39 +--
> tools/perf/util/data-convert-bt.c | 34 ++-
> tools/perf/util/data-convert-json.c | 47 ++--
> 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 | 37 +--
> tools/perf/util/intel-pt.c | 30 +--
> tools/perf/util/jitdump.c | 4 +-
> tools/perf/util/s390-cpumsf.c | 11 +-
> tools/perf/util/session.c | 366 +++-------------------------
> tools/perf/util/session.h | 9 +-
> 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 +-
> 53 files changed, 977 insertions(+), 1102 deletions(-)
> create mode 100644 tools/perf/util/tool.c
>
^ permalink raw reply [flat|nested] 39+ messages in thread* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-19 8:50 ` [PATCH v6 00/27] Constify tool pointers Adrian Hunter
@ 2024-07-19 16:26 ` Ian Rogers
2024-07-22 8:28 ` Adrian Hunter
2024-08-12 13:53 ` Arnaldo Carvalho de Melo
0 siblings, 2 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-19 16:26 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, Athira Rajeev, Huacai Chen, Yanteng Si,
Sun Haiyong, linux-kernel, linux-perf-users, linux-arm-kernel
On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> On 18/07/24 03:59, 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.
> >
> > v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
>
> The tags were really meant only for patch 1, the email that was replied to.
>
> But now for patches 2 and 3:
>
> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
Sorry for that, you'd mentioned that pt and bts testing which is
impacted by more than just patch 1.
> Looking at patches 4 to 25, they do not seem to offer any benefit.
>
> Instead of patch 26, presumably perf_tool__fill_defaults() could
> be moved to __perf_session__new(), which perhaps would allow
> patch 27 as it is.
What I'm trying to do in the series is make it so that the tool isn't
mutated during its use by session. Ideally we'd be passing a const
tool to session_new, that's not possible because there's a hack to fix
ordered events and pipe mode in session__new:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
Imo, it isn't great to pass a tool to session__new where you say you
want ordered events and then session just goes to change that for you.
Altering that behavior was beyond the scope of this clean up, so tool
is only const after session__new.
The reason for doing this is to make it so that when I have a tool I
can reason that nobody is doing things to change it under my feet. My
builtin_cmd is in charge of what the tool is rather than some code
buried in util that thought it was going to do me a favor. The code is
a refactor and so the benefit is intended to be for the developer and
how they reason about the use of tool. We generally use _init
functions rather than having _fill_defaults, so there is a consistency
argument. I don't expect any impact in terms of performance... Moving
perf_tool__fill_defaults to __perf_session__new had issues with the
existing code where NULL would be written over a function pointer
expecting the later fill_defaults to fix it up, doesn't address coding
consistency where _init is the norm, and adds another reason the tool
passed to session__new can't be const.
Thanks,
Ian
> > v5: Rebase dropping asan fix merged by Namhyung.
> > v4: Simplify perf_session__deliver_synth_attr_event following Adrian's
> > suggestions.
> > v3: Just remove auxtrace dummy tools [Adrian] and make s390-cpumsf
> > struct removal its own patch [Adrian].
> > 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 auxtrace: Remove dummy tools
> > perf s390-cpumsf: Remove unused struct
> > 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 | 55 +----
> > 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 | 39 +--
> > tools/perf/util/data-convert-bt.c | 34 ++-
> > tools/perf/util/data-convert-json.c | 47 ++--
> > 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 | 37 +--
> > tools/perf/util/intel-pt.c | 30 +--
> > tools/perf/util/jitdump.c | 4 +-
> > tools/perf/util/s390-cpumsf.c | 11 +-
> > tools/perf/util/session.c | 366 +++-------------------------
> > tools/perf/util/session.h | 9 +-
> > 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 +-
> > 53 files changed, 977 insertions(+), 1102 deletions(-)
> > create mode 100644 tools/perf/util/tool.c
> >
>
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-19 16:26 ` Ian Rogers
@ 2024-07-22 8:28 ` Adrian Hunter
2024-07-22 16:06 ` Ian Rogers
2024-08-12 13:53 ` Arnaldo Carvalho de Melo
1 sibling, 1 reply; 39+ messages in thread
From: Adrian Hunter @ 2024-07-22 8:28 UTC (permalink / raw)
To: Ian Rogers
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, Athira Rajeev, Huacai Chen, Yanteng Si,
Sun Haiyong, linux-kernel, linux-perf-users, linux-arm-kernel
On 19/07/24 19:26, Ian Rogers wrote:
> On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>>
>> On 18/07/24 03:59, 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.
>>>
>>> v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
>>
>> The tags were really meant only for patch 1, the email that was replied to.
>>
>> But now for patches 2 and 3:
>>
>> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
>
> Sorry for that, you'd mentioned that pt and bts testing which is
> impacted by more than just patch 1.
>
>> Looking at patches 4 to 25, they do not seem to offer any benefit.
>>
>> Instead of patch 26, presumably perf_tool__fill_defaults() could
>> be moved to __perf_session__new(), which perhaps would allow
>> patch 27 as it is.
>
> What I'm trying to do in the series is make it so that the tool isn't
> mutated during its use by session. Ideally we'd be passing a const
> tool to session_new, that's not possible because there's a hack to fix
> ordered events and pipe mode in session__new:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
> Imo, it isn't great to pass a tool to session__new where you say you
> want ordered events and then session just goes to change that for you.
> Altering that behavior was beyond the scope of this clean up, so tool
> is only const after session__new.
Seems like a separate issue. Since the session is created
by __perf_session__new(), session->tool will always be a pointer
to a const tool once there is:
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index 7f69baeae7fb..7c8dd6956330 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;
>
> The reason for doing this is to make it so that when I have a tool I
> can reason that nobody is doing things to change it under my feet.
It still can be changed by the caller of __perf_session__new(), since
the tool itself is not const.
Anything using container_of() like:
static int process_sample_event(const struct perf_tool *tool,
union perf_event *event,
struct perf_sample *sample,
struct evsel *evsel,
struct machine *machine)
{
struct perf_script *scr = container_of(tool, struct perf_script, tool);
can then change scr->tool without even having to cast away const.
Really, 'tool' needs to be defined as const in the first place.
> My
> builtin_cmd is in charge of what the tool is rather than some code
> buried in util that thought it was going to do me a favor. The code is
> a refactor and so the benefit is intended to be for the developer and
> how they reason about the use of tool.
It creates another question though: since there is a lot of code
before perf_tool__init() is called, does the caller mistakenly
change tool before calling perf_tool__init()
> how they reason about the use of tool. We generally use _init
> functions rather than having _fill_defaults, so there is a consistency
> argument.
The caller does not need the "defaults", so why would it set them up.
The session could just as easily do:
if (tool->cb)
tool->cb(...);
else
cb_stub(...);
> I don't expect any impact in terms of performance... Moving
> perf_tool__fill_defaults to __perf_session__new had issues with the
> existing code where NULL would be written over a function pointer
> expecting the later fill_defaults to fix it up, doesn't address coding
> consistency where _init is the norm, and adds another reason the tool
> passed to session__new can't be const.
perf_tool__init() is not a steeping stone to making 'tool' a
const in the first place.
^ permalink raw reply related [flat|nested] 39+ messages in thread* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-22 8:28 ` Adrian Hunter
@ 2024-07-22 16:06 ` Ian Rogers
2024-07-22 17:45 ` Namhyung Kim
0 siblings, 1 reply; 39+ messages in thread
From: Ian Rogers @ 2024-07-22 16:06 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, Athira Rajeev, Huacai Chen, Yanteng Si,
Sun Haiyong, linux-kernel, linux-perf-users, linux-arm-kernel
On Mon, Jul 22, 2024 at 1:29 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> On 19/07/24 19:26, Ian Rogers wrote:
> > On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> >>
> >> On 18/07/24 03:59, 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.
> >>>
> >>> v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
> >>
> >> The tags were really meant only for patch 1, the email that was replied to.
> >>
> >> But now for patches 2 and 3:
> >>
> >> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
> >
> > Sorry for that, you'd mentioned that pt and bts testing which is
> > impacted by more than just patch 1.
> >
> >> Looking at patches 4 to 25, they do not seem to offer any benefit.
> >>
> >> Instead of patch 26, presumably perf_tool__fill_defaults() could
> >> be moved to __perf_session__new(), which perhaps would allow
> >> patch 27 as it is.
> >
> > What I'm trying to do in the series is make it so that the tool isn't
> > mutated during its use by session. Ideally we'd be passing a const
> > tool to session_new, that's not possible because there's a hack to fix
> > ordered events and pipe mode in session__new:
> > https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
> > Imo, it isn't great to pass a tool to session__new where you say you
> > want ordered events and then session just goes to change that for you.
> > Altering that behavior was beyond the scope of this clean up, so tool
> > is only const after session__new.
>
> Seems like a separate issue. Since the session is created
> by __perf_session__new(), session->tool will always be a pointer
> to a const tool once there is:
>
> diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
> index 7f69baeae7fb..7c8dd6956330 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;
That's the case after these changes, but not before.
> >
> > The reason for doing this is to make it so that when I have a tool I
> > can reason that nobody is doing things to change it under my feet.
>
> It still can be changed by the caller of __perf_session__new(), since
> the tool itself is not const.
>
> Anything using container_of() like:
>
> static int process_sample_event(const struct perf_tool *tool,
> union perf_event *event,
> struct perf_sample *sample,
> struct evsel *evsel,
> struct machine *machine)
> {
> struct perf_script *scr = container_of(tool, struct perf_script, tool);
>
> can then change scr->tool without even having to cast away const.
Agreed, but such things happen in builtin_cmd where the tool is
defined and presumably they know what they are doing. My objection is
to code in util mutating the tool as I want the tool to have
predictable behavior. As callers that take a tool can call fill in
defaults (not all) then the tool has to be mutable and I don't want
this to be the case.
> Really, 'tool' needs to be defined as const in the first place.
I'd like this. The problem is initializing all the function pointers
and making such initialization robust to extra functions being added
to the tool API. It can be done in a long winded way but I couldn't
devise macro magic to do it. The other problem is around variables
like ordered_events that can't currently be const. The patches move us
closer to this being a possibility.
> > My
> > builtin_cmd is in charge of what the tool is rather than some code
> > buried in util that thought it was going to do me a favor. The code is
> > a refactor and so the benefit is intended to be for the developer and
> > how they reason about the use of tool.
>
> It creates another question though: since there is a lot of code
> before perf_tool__init() is called, does the caller mistakenly
> change tool before calling perf_tool__init()
If they do this their function pointers will be clobbered and their
code won't behave as expected, which I'd expect to be easy to observe.
In C++ if you were to initialize memory and then use the memory for a
placement new to create an object which would call the constructor,
the expected behavior would be that the initialized memory's values
would get overridden. I see the use of _init and _exit in the code as
being our poor man replacements of constructors and destructors.
> > how they reason about the use of tool. We generally use _init
> > functions rather than having _fill_defaults, so there is a consistency
> > argument.
>
> The caller does not need the "defaults", so why would it set them up.
> The session could just as easily do:
>
> if (tool->cb)
> tool->cb(...);
> else
> cb_stub(...);
Multiplied by every stub, we'd probably need a helper function, how to
handle argument passing. There's nothing wrong with this as an idea
but I think of this code as trying to create a visitor pattern and
this is a visitor pattern with a hard time for the caller.
> > I don't expect any impact in terms of performance... Moving
> > perf_tool__fill_defaults to __perf_session__new had issues with the
> > existing code where NULL would be written over a function pointer
> > expecting the later fill_defaults to fix it up, doesn't address coding
> > consistency where _init is the norm, and adds another reason the tool
> > passed to session__new can't be const.
>
> perf_tool__init() is not a steeping stone to making 'tool' a
> const in the first place.
It is because the patch series gets rid of fill in defaults which is
why we have a mutable tool passed around. I don't think this is up for
debate as the patch series clearly goes from a non-const
tool to a const tool at the end. Changing perf_tool__init to make all
the function pointers NULL and then making every caller have to do a:
if (tool->cb)
tool->cb(...);
else
cb_stub(...);
I think it is a less elegant solution at the end, it is also a large
and more invasive change. The various refactorings to make tool const,
changing the aux use of tool, etc. wouldn't be impacted by such a
change but I think it is out of scope for this patch series.
Thanks,
Ian
^ permalink raw reply [flat|nested] 39+ messages in thread* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-22 16:06 ` Ian Rogers
@ 2024-07-22 17:45 ` Namhyung Kim
2024-07-22 17:50 ` Ian Rogers
0 siblings, 1 reply; 39+ messages in thread
From: Namhyung Kim @ 2024-07-22 17:45 UTC (permalink / raw)
To: Ian Rogers
Cc: Adrian Hunter, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, 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, Athira Rajeev,
Huacai Chen, Yanteng Si, Sun Haiyong, linux-kernel,
linux-perf-users, linux-arm-kernel
Hi,
On Mon, Jul 22, 2024 at 9:06 AM Ian Rogers <irogers@google.com> wrote:
>
> On Mon, Jul 22, 2024 at 1:29 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> >
> > On 19/07/24 19:26, Ian Rogers wrote:
> > > On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> > >>
> > >> On 18/07/24 03:59, 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.
> > >>>
> > >>> v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
> > >>
> > >> The tags were really meant only for patch 1, the email that was replied to.
> > >>
> > >> But now for patches 2 and 3:
> > >>
> > >> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
> > >
> > > Sorry for that, you'd mentioned that pt and bts testing which is
> > > impacted by more than just patch 1.
> > >
> > >> Looking at patches 4 to 25, they do not seem to offer any benefit.
> > >>
> > >> Instead of patch 26, presumably perf_tool__fill_defaults() could
> > >> be moved to __perf_session__new(), which perhaps would allow
> > >> patch 27 as it is.
> > >
> > > What I'm trying to do in the series is make it so that the tool isn't
> > > mutated during its use by session. Ideally we'd be passing a const
> > > tool to session_new, that's not possible because there's a hack to fix
> > > ordered events and pipe mode in session__new:
> > > https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
> > > Imo, it isn't great to pass a tool to session__new where you say you
> > > want ordered events and then session just goes to change that for you.
> > > Altering that behavior was beyond the scope of this clean up, so tool
> > > is only const after session__new.
> >
> > Seems like a separate issue. Since the session is created
> > by __perf_session__new(), session->tool will always be a pointer
> > to a const tool once there is:
> >
> > diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
> > index 7f69baeae7fb..7c8dd6956330 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;
>
> That's the case after these changes, but not before.
>
> > >
> > > The reason for doing this is to make it so that when I have a tool I
> > > can reason that nobody is doing things to change it under my feet.
> >
> > It still can be changed by the caller of __perf_session__new(), since
> > the tool itself is not const.
> >
> > Anything using container_of() like:
> >
> > static int process_sample_event(const struct perf_tool *tool,
> > union perf_event *event,
> > struct perf_sample *sample,
> > struct evsel *evsel,
> > struct machine *machine)
> > {
> > struct perf_script *scr = container_of(tool, struct perf_script, tool);
> >
> > can then change scr->tool without even having to cast away const.
>
> Agreed, but such things happen in builtin_cmd where the tool is
> defined and presumably they know what they are doing. My objection is
> to code in util mutating the tool as I want the tool to have
> predictable behavior. As callers that take a tool can call fill in
> defaults (not all) then the tool has to be mutable and I don't want
> this to be the case.
>
> > Really, 'tool' needs to be defined as const in the first place.
>
> I'd like this. The problem is initializing all the function pointers
> and making such initialization robust to extra functions being added
> to the tool API. It can be done in a long winded way but I couldn't
> devise macro magic to do it. The other problem is around variables
> like ordered_events that can't currently be const. The patches move us
> closer to this being a possibility.
>
> > > My
> > > builtin_cmd is in charge of what the tool is rather than some code
> > > buried in util that thought it was going to do me a favor. The code is
> > > a refactor and so the benefit is intended to be for the developer and
> > > how they reason about the use of tool.
> >
> > It creates another question though: since there is a lot of code
> > before perf_tool__init() is called, does the caller mistakenly
> > change tool before calling perf_tool__init()
>
> If they do this their function pointers will be clobbered and their
> code won't behave as expected, which I'd expect to be easy to observe.
> In C++ if you were to initialize memory and then use the memory for a
> placement new to create an object which would call the constructor,
> the expected behavior would be that the initialized memory's values
> would get overridden. I see the use of _init and _exit in the code as
> being our poor man replacements of constructors and destructors.
>
> > > how they reason about the use of tool. We generally use _init
> > > functions rather than having _fill_defaults, so there is a consistency
> > > argument.
> >
> > The caller does not need the "defaults", so why would it set them up.
> > The session could just as easily do:
> >
> > if (tool->cb)
> > tool->cb(...);
> > else
> > cb_stub(...);
>
> Multiplied by every stub, we'd probably need a helper function, how to
> handle argument passing. There's nothing wrong with this as an idea
> but I think of this code as trying to create a visitor pattern and
> this is a visitor pattern with a hard time for the caller.
>
> > > I don't expect any impact in terms of performance... Moving
> > > perf_tool__fill_defaults to __perf_session__new had issues with the
> > > existing code where NULL would be written over a function pointer
> > > expecting the later fill_defaults to fix it up, doesn't address coding
> > > consistency where _init is the norm, and adds another reason the tool
> > > passed to session__new can't be const.
> >
> > perf_tool__init() is not a steeping stone to making 'tool' a
> > const in the first place.
>
> It is because the patch series gets rid of fill in defaults which is
> why we have a mutable tool passed around. I don't think this is up for
> debate as the patch series clearly goes from a non-const
> tool to a const tool at the end. Changing perf_tool__init to make all
> the function pointers NULL and then making every caller have to do a:
>
> if (tool->cb)
> tool->cb(...);
> else
> cb_stub(...);
>
> I think it is a less elegant solution at the end, it is also a large
> and more invasive change. The various refactorings to make tool const,
> changing the aux use of tool, etc. wouldn't be impacted by such a
> change but I think it is out of scope for this patch series.
I don't think it's a large and invasive change. The tools are mostly
zero-initialized so we don't need to reset to NULL. And tool->cb is
called mostly from two functions: machines__deliver_event() and
perf_session__process_user_event(). Can we change them to check
NULL and get rid of perf_tool__fill_defaults() to keep it const?
Thanks,
Namhyung
^ permalink raw reply [flat|nested] 39+ messages in thread* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-22 17:45 ` Namhyung Kim
@ 2024-07-22 17:50 ` Ian Rogers
2024-07-22 18:04 ` Ian Rogers
0 siblings, 1 reply; 39+ messages in thread
From: Ian Rogers @ 2024-07-22 17:50 UTC (permalink / raw)
To: Namhyung Kim
Cc: Adrian Hunter, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, 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, Athira Rajeev,
Huacai Chen, Yanteng Si, Sun Haiyong, linux-kernel,
linux-perf-users, linux-arm-kernel
On Mon, Jul 22, 2024 at 10:45 AM Namhyung Kim <namhyung@kernel.org> wrote:
>
> Hi,
>
> On Mon, Jul 22, 2024 at 9:06 AM Ian Rogers <irogers@google.com> wrote:
> >
> > On Mon, Jul 22, 2024 at 1:29 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> > >
> > > On 19/07/24 19:26, Ian Rogers wrote:
> > > > On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> > > >>
> > > >> On 18/07/24 03:59, 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.
> > > >>>
> > > >>> v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
> > > >>
> > > >> The tags were really meant only for patch 1, the email that was replied to.
> > > >>
> > > >> But now for patches 2 and 3:
> > > >>
> > > >> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
> > > >
> > > > Sorry for that, you'd mentioned that pt and bts testing which is
> > > > impacted by more than just patch 1.
> > > >
> > > >> Looking at patches 4 to 25, they do not seem to offer any benefit.
> > > >>
> > > >> Instead of patch 26, presumably perf_tool__fill_defaults() could
> > > >> be moved to __perf_session__new(), which perhaps would allow
> > > >> patch 27 as it is.
> > > >
> > > > What I'm trying to do in the series is make it so that the tool isn't
> > > > mutated during its use by session. Ideally we'd be passing a const
> > > > tool to session_new, that's not possible because there's a hack to fix
> > > > ordered events and pipe mode in session__new:
> > > > https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
> > > > Imo, it isn't great to pass a tool to session__new where you say you
> > > > want ordered events and then session just goes to change that for you.
> > > > Altering that behavior was beyond the scope of this clean up, so tool
> > > > is only const after session__new.
> > >
> > > Seems like a separate issue. Since the session is created
> > > by __perf_session__new(), session->tool will always be a pointer
> > > to a const tool once there is:
> > >
> > > diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
> > > index 7f69baeae7fb..7c8dd6956330 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;
> >
> > That's the case after these changes, but not before.
> >
> > > >
> > > > The reason for doing this is to make it so that when I have a tool I
> > > > can reason that nobody is doing things to change it under my feet.
> > >
> > > It still can be changed by the caller of __perf_session__new(), since
> > > the tool itself is not const.
> > >
> > > Anything using container_of() like:
> > >
> > > static int process_sample_event(const struct perf_tool *tool,
> > > union perf_event *event,
> > > struct perf_sample *sample,
> > > struct evsel *evsel,
> > > struct machine *machine)
> > > {
> > > struct perf_script *scr = container_of(tool, struct perf_script, tool);
> > >
> > > can then change scr->tool without even having to cast away const.
> >
> > Agreed, but such things happen in builtin_cmd where the tool is
> > defined and presumably they know what they are doing. My objection is
> > to code in util mutating the tool as I want the tool to have
> > predictable behavior. As callers that take a tool can call fill in
> > defaults (not all) then the tool has to be mutable and I don't want
> > this to be the case.
> >
> > > Really, 'tool' needs to be defined as const in the first place.
> >
> > I'd like this. The problem is initializing all the function pointers
> > and making such initialization robust to extra functions being added
> > to the tool API. It can be done in a long winded way but I couldn't
> > devise macro magic to do it. The other problem is around variables
> > like ordered_events that can't currently be const. The patches move us
> > closer to this being a possibility.
> >
> > > > My
> > > > builtin_cmd is in charge of what the tool is rather than some code
> > > > buried in util that thought it was going to do me a favor. The code is
> > > > a refactor and so the benefit is intended to be for the developer and
> > > > how they reason about the use of tool.
> > >
> > > It creates another question though: since there is a lot of code
> > > before perf_tool__init() is called, does the caller mistakenly
> > > change tool before calling perf_tool__init()
> >
> > If they do this their function pointers will be clobbered and their
> > code won't behave as expected, which I'd expect to be easy to observe.
> > In C++ if you were to initialize memory and then use the memory for a
> > placement new to create an object which would call the constructor,
> > the expected behavior would be that the initialized memory's values
> > would get overridden. I see the use of _init and _exit in the code as
> > being our poor man replacements of constructors and destructors.
> >
> > > > how they reason about the use of tool. We generally use _init
> > > > functions rather than having _fill_defaults, so there is a consistency
> > > > argument.
> > >
> > > The caller does not need the "defaults", so why would it set them up.
> > > The session could just as easily do:
> > >
> > > if (tool->cb)
> > > tool->cb(...);
> > > else
> > > cb_stub(...);
> >
> > Multiplied by every stub, we'd probably need a helper function, how to
> > handle argument passing. There's nothing wrong with this as an idea
> > but I think of this code as trying to create a visitor pattern and
> > this is a visitor pattern with a hard time for the caller.
> >
> > > > I don't expect any impact in terms of performance... Moving
> > > > perf_tool__fill_defaults to __perf_session__new had issues with the
> > > > existing code where NULL would be written over a function pointer
> > > > expecting the later fill_defaults to fix it up, doesn't address coding
> > > > consistency where _init is the norm, and adds another reason the tool
> > > > passed to session__new can't be const.
> > >
> > > perf_tool__init() is not a steeping stone to making 'tool' a
> > > const in the first place.
> >
> > It is because the patch series gets rid of fill in defaults which is
> > why we have a mutable tool passed around. I don't think this is up for
> > debate as the patch series clearly goes from a non-const
> > tool to a const tool at the end. Changing perf_tool__init to make all
> > the function pointers NULL and then making every caller have to do a:
> >
> > if (tool->cb)
> > tool->cb(...);
> > else
> > cb_stub(...);
> >
> > I think it is a less elegant solution at the end, it is also a large
> > and more invasive change. The various refactorings to make tool const,
> > changing the aux use of tool, etc. wouldn't be impacted by such a
> > change but I think it is out of scope for this patch series.
>
> I don't think it's a large and invasive change. The tools are mostly
> zero-initialized so we don't need to reset to NULL. And tool->cb is
> called mostly from two functions: machines__deliver_event() and
> perf_session__process_user_event(). Can we change them to check
> NULL and get rid of perf_tool__fill_defaults() to keep it const?
As I said above, I don't think that is good style and is out of scope
here. It clearly can be done as follow up, but I don't see how that
fixes the style issue.
Thanks,
Ian
^ permalink raw reply [flat|nested] 39+ messages in thread* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-22 17:50 ` Ian Rogers
@ 2024-07-22 18:04 ` Ian Rogers
2024-07-23 9:38 ` Adrian Hunter
0 siblings, 1 reply; 39+ messages in thread
From: Ian Rogers @ 2024-07-22 18:04 UTC (permalink / raw)
To: Namhyung Kim
Cc: Adrian Hunter, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, 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, Athira Rajeev,
Huacai Chen, Yanteng Si, Sun Haiyong, linux-kernel,
linux-perf-users, linux-arm-kernel
On Mon, Jul 22, 2024 at 10:50 AM Ian Rogers <irogers@google.com> wrote:
>
> On Mon, Jul 22, 2024 at 10:45 AM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > Hi,
> >
> > On Mon, Jul 22, 2024 at 9:06 AM Ian Rogers <irogers@google.com> wrote:
> > >
> > > On Mon, Jul 22, 2024 at 1:29 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> > > >
> > > > On 19/07/24 19:26, Ian Rogers wrote:
> > > > > On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> > > > >>
> > > > >> On 18/07/24 03:59, 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.
> > > > >>>
> > > > >>> v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
> > > > >>
> > > > >> The tags were really meant only for patch 1, the email that was replied to.
> > > > >>
> > > > >> But now for patches 2 and 3:
> > > > >>
> > > > >> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
> > > > >
> > > > > Sorry for that, you'd mentioned that pt and bts testing which is
> > > > > impacted by more than just patch 1.
> > > > >
> > > > >> Looking at patches 4 to 25, they do not seem to offer any benefit.
> > > > >>
> > > > >> Instead of patch 26, presumably perf_tool__fill_defaults() could
> > > > >> be moved to __perf_session__new(), which perhaps would allow
> > > > >> patch 27 as it is.
> > > > >
> > > > > What I'm trying to do in the series is make it so that the tool isn't
> > > > > mutated during its use by session. Ideally we'd be passing a const
> > > > > tool to session_new, that's not possible because there's a hack to fix
> > > > > ordered events and pipe mode in session__new:
> > > > > https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
> > > > > Imo, it isn't great to pass a tool to session__new where you say you
> > > > > want ordered events and then session just goes to change that for you.
> > > > > Altering that behavior was beyond the scope of this clean up, so tool
> > > > > is only const after session__new.
> > > >
> > > > Seems like a separate issue. Since the session is created
> > > > by __perf_session__new(), session->tool will always be a pointer
> > > > to a const tool once there is:
> > > >
> > > > diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
> > > > index 7f69baeae7fb..7c8dd6956330 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;
> > >
> > > That's the case after these changes, but not before.
> > >
> > > > >
> > > > > The reason for doing this is to make it so that when I have a tool I
> > > > > can reason that nobody is doing things to change it under my feet.
> > > >
> > > > It still can be changed by the caller of __perf_session__new(), since
> > > > the tool itself is not const.
> > > >
> > > > Anything using container_of() like:
> > > >
> > > > static int process_sample_event(const struct perf_tool *tool,
> > > > union perf_event *event,
> > > > struct perf_sample *sample,
> > > > struct evsel *evsel,
> > > > struct machine *machine)
> > > > {
> > > > struct perf_script *scr = container_of(tool, struct perf_script, tool);
> > > >
> > > > can then change scr->tool without even having to cast away const.
> > >
> > > Agreed, but such things happen in builtin_cmd where the tool is
> > > defined and presumably they know what they are doing. My objection is
> > > to code in util mutating the tool as I want the tool to have
> > > predictable behavior. As callers that take a tool can call fill in
> > > defaults (not all) then the tool has to be mutable and I don't want
> > > this to be the case.
> > >
> > > > Really, 'tool' needs to be defined as const in the first place.
> > >
> > > I'd like this. The problem is initializing all the function pointers
> > > and making such initialization robust to extra functions being added
> > > to the tool API. It can be done in a long winded way but I couldn't
> > > devise macro magic to do it. The other problem is around variables
> > > like ordered_events that can't currently be const. The patches move us
> > > closer to this being a possibility.
> > >
> > > > > My
> > > > > builtin_cmd is in charge of what the tool is rather than some code
> > > > > buried in util that thought it was going to do me a favor. The code is
> > > > > a refactor and so the benefit is intended to be for the developer and
> > > > > how they reason about the use of tool.
> > > >
> > > > It creates another question though: since there is a lot of code
> > > > before perf_tool__init() is called, does the caller mistakenly
> > > > change tool before calling perf_tool__init()
> > >
> > > If they do this their function pointers will be clobbered and their
> > > code won't behave as expected, which I'd expect to be easy to observe.
> > > In C++ if you were to initialize memory and then use the memory for a
> > > placement new to create an object which would call the constructor,
> > > the expected behavior would be that the initialized memory's values
> > > would get overridden. I see the use of _init and _exit in the code as
> > > being our poor man replacements of constructors and destructors.
> > >
> > > > > how they reason about the use of tool. We generally use _init
> > > > > functions rather than having _fill_defaults, so there is a consistency
> > > > > argument.
> > > >
> > > > The caller does not need the "defaults", so why would it set them up.
> > > > The session could just as easily do:
> > > >
> > > > if (tool->cb)
> > > > tool->cb(...);
> > > > else
> > > > cb_stub(...);
> > >
> > > Multiplied by every stub, we'd probably need a helper function, how to
> > > handle argument passing. There's nothing wrong with this as an idea
> > > but I think of this code as trying to create a visitor pattern and
> > > this is a visitor pattern with a hard time for the caller.
> > >
> > > > > I don't expect any impact in terms of performance... Moving
> > > > > perf_tool__fill_defaults to __perf_session__new had issues with the
> > > > > existing code where NULL would be written over a function pointer
> > > > > expecting the later fill_defaults to fix it up, doesn't address coding
> > > > > consistency where _init is the norm, and adds another reason the tool
> > > > > passed to session__new can't be const.
> > > >
> > > > perf_tool__init() is not a steeping stone to making 'tool' a
> > > > const in the first place.
> > >
> > > It is because the patch series gets rid of fill in defaults which is
> > > why we have a mutable tool passed around. I don't think this is up for
> > > debate as the patch series clearly goes from a non-const
> > > tool to a const tool at the end. Changing perf_tool__init to make all
> > > the function pointers NULL and then making every caller have to do a:
> > >
> > > if (tool->cb)
> > > tool->cb(...);
> > > else
> > > cb_stub(...);
> > >
> > > I think it is a less elegant solution at the end, it is also a large
> > > and more invasive change. The various refactorings to make tool const,
> > > changing the aux use of tool, etc. wouldn't be impacted by such a
> > > change but I think it is out of scope for this patch series.
> >
> > I don't think it's a large and invasive change. The tools are mostly
> > zero-initialized so we don't need to reset to NULL. And tool->cb is
> > called mostly from two functions: machines__deliver_event() and
> > perf_session__process_user_event(). Can we change them to check
> > NULL and get rid of perf_tool__fill_defaults() to keep it const?
>
> As I said above, I don't think that is good style and is out of scope
> here. It clearly can be done as follow up, but I don't see how that
> fixes the style issue.
Just to be clear on what the style issue is. We already have code:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-record.c?h=perf-tools-next#n1461
```
if (rec->buildid_all && !rec->timestamp_boundary)
rec->tool.sample = NULL;
```
that relies on the special behavior of NULL in a function pointer
being changed at dispatch time - a simple reading of that code would
be anyone calling the function pointer would get a segv. I'm trying to
make it so that NULL isn't magic in the context of tool and you can
simply look at the tool to understand what its behavior is, much as a
virtual method table would work if we could do proper object-oriented
development.
Thanks,
Ian
> Thanks,
> Ian
^ permalink raw reply [flat|nested] 39+ messages in thread* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-22 18:04 ` Ian Rogers
@ 2024-07-23 9:38 ` Adrian Hunter
2024-07-23 12:50 ` Ian Rogers
0 siblings, 1 reply; 39+ messages in thread
From: Adrian Hunter @ 2024-07-23 9:38 UTC (permalink / raw)
To: Ian Rogers, Namhyung Kim
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
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, Athira Rajeev, Huacai Chen, Yanteng Si,
Sun Haiyong, linux-kernel, linux-perf-users, linux-arm-kernel
On 22/07/24 21:04, Ian Rogers wrote:
> On Mon, Jul 22, 2024 at 10:50 AM Ian Rogers <irogers@google.com> wrote:
>>
>> On Mon, Jul 22, 2024 at 10:45 AM Namhyung Kim <namhyung@kernel.org> wrote:
>>>
>>> Hi,
>>>
>>> On Mon, Jul 22, 2024 at 9:06 AM Ian Rogers <irogers@google.com> wrote:
>>>>
>>>> On Mon, Jul 22, 2024 at 1:29 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>>>>>
>>>>> On 19/07/24 19:26, Ian Rogers wrote:
>>>>>> On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>>>>>>>
>>>>>>> On 18/07/24 03:59, 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.
>>>>>>>>
>>>>>>>> v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
>>>>>>>
>>>>>>> The tags were really meant only for patch 1, the email that was replied to.
>>>>>>>
>>>>>>> But now for patches 2 and 3:
>>>>>>>
>>>>>>> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
>>>>>>
>>>>>> Sorry for that, you'd mentioned that pt and bts testing which is
>>>>>> impacted by more than just patch 1.
>>>>>>
>>>>>>> Looking at patches 4 to 25, they do not seem to offer any benefit.
>>>>>>>
>>>>>>> Instead of patch 26, presumably perf_tool__fill_defaults() could
>>>>>>> be moved to __perf_session__new(), which perhaps would allow
>>>>>>> patch 27 as it is.
>>>>>>
>>>>>> What I'm trying to do in the series is make it so that the tool isn't
>>>>>> mutated during its use by session. Ideally we'd be passing a const
>>>>>> tool to session_new, that's not possible because there's a hack to fix
>>>>>> ordered events and pipe mode in session__new:
>>>>>> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
>>>>>> Imo, it isn't great to pass a tool to session__new where you say you
>>>>>> want ordered events and then session just goes to change that for you.
>>>>>> Altering that behavior was beyond the scope of this clean up, so tool
>>>>>> is only const after session__new.
>>>>>
>>>>> Seems like a separate issue. Since the session is created
>>>>> by __perf_session__new(), session->tool will always be a pointer
>>>>> to a const tool once there is:
>>>>>
>>>>> diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
>>>>> index 7f69baeae7fb..7c8dd6956330 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;
>>>>
>>>> That's the case after these changes, but not before.
>>>>
>>>>>>
>>>>>> The reason for doing this is to make it so that when I have a tool I
>>>>>> can reason that nobody is doing things to change it under my feet.
>>>>>
>>>>> It still can be changed by the caller of __perf_session__new(), since
>>>>> the tool itself is not const.
>>>>>
>>>>> Anything using container_of() like:
>>>>>
>>>>> static int process_sample_event(const struct perf_tool *tool,
>>>>> union perf_event *event,
>>>>> struct perf_sample *sample,
>>>>> struct evsel *evsel,
>>>>> struct machine *machine)
>>>>> {
>>>>> struct perf_script *scr = container_of(tool, struct perf_script, tool);
>>>>>
>>>>> can then change scr->tool without even having to cast away const.
>>>>
>>>> Agreed, but such things happen in builtin_cmd where the tool is
>>>> defined and presumably they know what they are doing. My objection is
>>>> to code in util mutating the tool as I want the tool to have
>>>> predictable behavior. As callers that take a tool can call fill in
>>>> defaults (not all) then the tool has to be mutable and I don't want
>>>> this to be the case.
>>>>
>>>>> Really, 'tool' needs to be defined as const in the first place.
>>>>
>>>> I'd like this. The problem is initializing all the function pointers
>>>> and making such initialization robust to extra functions being added
>>>> to the tool API. It can be done in a long winded way but I couldn't
>>>> devise macro magic to do it. The other problem is around variables
>>>> like ordered_events that can't currently be const. The patches move us
>>>> closer to this being a possibility.
>>>>
>>>>>> My
>>>>>> builtin_cmd is in charge of what the tool is rather than some code
>>>>>> buried in util that thought it was going to do me a favor. The code is
>>>>>> a refactor and so the benefit is intended to be for the developer and
>>>>>> how they reason about the use of tool.
>>>>>
>>>>> It creates another question though: since there is a lot of code
>>>>> before perf_tool__init() is called, does the caller mistakenly
>>>>> change tool before calling perf_tool__init()
>>>>
>>>> If they do this their function pointers will be clobbered and their
>>>> code won't behave as expected, which I'd expect to be easy to observe.
>>>> In C++ if you were to initialize memory and then use the memory for a
>>>> placement new to create an object which would call the constructor,
>>>> the expected behavior would be that the initialized memory's values
>>>> would get overridden. I see the use of _init and _exit in the code as
>>>> being our poor man replacements of constructors and destructors.
>>>>
>>>>>> how they reason about the use of tool. We generally use _init
>>>>>> functions rather than having _fill_defaults, so there is a consistency
>>>>>> argument.
>>>>>
>>>>> The caller does not need the "defaults", so why would it set them up.
>>>>> The session could just as easily do:
>>>>>
>>>>> if (tool->cb)
>>>>> tool->cb(...);
>>>>> else
>>>>> cb_stub(...);
>>>>
>>>> Multiplied by every stub, we'd probably need a helper function, how to
>>>> handle argument passing. There's nothing wrong with this as an idea
>>>> but I think of this code as trying to create a visitor pattern and
>>>> this is a visitor pattern with a hard time for the caller.
>>>>
>>>>>> I don't expect any impact in terms of performance... Moving
>>>>>> perf_tool__fill_defaults to __perf_session__new had issues with the
>>>>>> existing code where NULL would be written over a function pointer
>>>>>> expecting the later fill_defaults to fix it up, doesn't address coding
>>>>>> consistency where _init is the norm, and adds another reason the tool
>>>>>> passed to session__new can't be const.
>>>>>
>>>>> perf_tool__init() is not a steeping stone to making 'tool' a
>>>>> const in the first place.
>>>>
>>>> It is because the patch series gets rid of fill in defaults which is
>>>> why we have a mutable tool passed around. I don't think this is up for
>>>> debate as the patch series clearly goes from a non-const
>>>> tool to a const tool at the end. Changing perf_tool__init to make all
>>>> the function pointers NULL and then making every caller have to do a:
>>>>
>>>> if (tool->cb)
>>>> tool->cb(...);
>>>> else
>>>> cb_stub(...);
>>>>
>>>> I think it is a less elegant solution at the end, it is also a large
>>>> and more invasive change. The various refactorings to make tool const,
>>>> changing the aux use of tool, etc. wouldn't be impacted by such a
>>>> change but I think it is out of scope for this patch series.
>>>
>>> I don't think it's a large and invasive change. The tools are mostly
>>> zero-initialized so we don't need to reset to NULL. And tool->cb is
>>> called mostly from two functions: machines__deliver_event() and
>>> perf_session__process_user_event(). Can we change them to check
>>> NULL and get rid of perf_tool__fill_defaults() to keep it const?
>>
>> As I said above, I don't think that is good style and is out of scope
>> here. It clearly can be done as follow up, but I don't see how that
>> fixes the style issue.
>
> Just to be clear on what the style issue is. We already have code:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-record.c?h=perf-tools-next#n1461
> ```
> if (rec->buildid_all && !rec->timestamp_boundary)
> rec->tool.sample = NULL;
> ```
> that relies on the special behavior of NULL in a function pointer
> being changed at dispatch time - a simple reading of that code would
> be anyone calling the function pointer would get a segv. I'm trying to
> make it so that NULL isn't magic in the context of tool and you can
> simply look at the tool to understand what its behavior is, much as a
> virtual method table would work if we could do proper object-oriented
> development.
In C, NULL or zero is often used as a special value to mean
no-value. Optional callbacks that are NULL is also not remarkable.
^ permalink raw reply [flat|nested] 39+ messages in thread* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-23 9:38 ` Adrian Hunter
@ 2024-07-23 12:50 ` Ian Rogers
0 siblings, 0 replies; 39+ messages in thread
From: Ian Rogers @ 2024-07-23 12:50 UTC (permalink / raw)
To: Adrian Hunter
Cc: Namhyung Kim, Peter Zijlstra, Ingo Molnar,
Arnaldo Carvalho de Melo, 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, Athira Rajeev,
Huacai Chen, Yanteng Si, Sun Haiyong, linux-kernel,
linux-perf-users, linux-arm-kernel
On Tue, Jul 23, 2024 at 2:38 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> On 22/07/24 21:04, Ian Rogers wrote:
> > On Mon, Jul 22, 2024 at 10:50 AM Ian Rogers <irogers@google.com> wrote:
> >>
> >> On Mon, Jul 22, 2024 at 10:45 AM Namhyung Kim <namhyung@kernel.org> wrote:
> >>>
> >>> Hi,
> >>>
> >>> On Mon, Jul 22, 2024 at 9:06 AM Ian Rogers <irogers@google.com> wrote:
> >>>>
> >>>> On Mon, Jul 22, 2024 at 1:29 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> >>>>>
> >>>>> On 19/07/24 19:26, Ian Rogers wrote:
> >>>>>> On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> >>>>>>>
> >>>>>>> On 18/07/24 03:59, 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.
> >>>>>>>>
> >>>>>>>> v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
> >>>>>>>
> >>>>>>> The tags were really meant only for patch 1, the email that was replied to.
> >>>>>>>
> >>>>>>> But now for patches 2 and 3:
> >>>>>>>
> >>>>>>> Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
> >>>>>>
> >>>>>> Sorry for that, you'd mentioned that pt and bts testing which is
> >>>>>> impacted by more than just patch 1.
> >>>>>>
> >>>>>>> Looking at patches 4 to 25, they do not seem to offer any benefit.
> >>>>>>>
> >>>>>>> Instead of patch 26, presumably perf_tool__fill_defaults() could
> >>>>>>> be moved to __perf_session__new(), which perhaps would allow
> >>>>>>> patch 27 as it is.
> >>>>>>
> >>>>>> What I'm trying to do in the series is make it so that the tool isn't
> >>>>>> mutated during its use by session. Ideally we'd be passing a const
> >>>>>> tool to session_new, that's not possible because there's a hack to fix
> >>>>>> ordered events and pipe mode in session__new:
> >>>>>> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
> >>>>>> Imo, it isn't great to pass a tool to session__new where you say you
> >>>>>> want ordered events and then session just goes to change that for you.
> >>>>>> Altering that behavior was beyond the scope of this clean up, so tool
> >>>>>> is only const after session__new.
> >>>>>
> >>>>> Seems like a separate issue. Since the session is created
> >>>>> by __perf_session__new(), session->tool will always be a pointer
> >>>>> to a const tool once there is:
> >>>>>
> >>>>> diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
> >>>>> index 7f69baeae7fb..7c8dd6956330 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;
> >>>>
> >>>> That's the case after these changes, but not before.
> >>>>
> >>>>>>
> >>>>>> The reason for doing this is to make it so that when I have a tool I
> >>>>>> can reason that nobody is doing things to change it under my feet.
> >>>>>
> >>>>> It still can be changed by the caller of __perf_session__new(), since
> >>>>> the tool itself is not const.
> >>>>>
> >>>>> Anything using container_of() like:
> >>>>>
> >>>>> static int process_sample_event(const struct perf_tool *tool,
> >>>>> union perf_event *event,
> >>>>> struct perf_sample *sample,
> >>>>> struct evsel *evsel,
> >>>>> struct machine *machine)
> >>>>> {
> >>>>> struct perf_script *scr = container_of(tool, struct perf_script, tool);
> >>>>>
> >>>>> can then change scr->tool without even having to cast away const.
> >>>>
> >>>> Agreed, but such things happen in builtin_cmd where the tool is
> >>>> defined and presumably they know what they are doing. My objection is
> >>>> to code in util mutating the tool as I want the tool to have
> >>>> predictable behavior. As callers that take a tool can call fill in
> >>>> defaults (not all) then the tool has to be mutable and I don't want
> >>>> this to be the case.
> >>>>
> >>>>> Really, 'tool' needs to be defined as const in the first place.
> >>>>
> >>>> I'd like this. The problem is initializing all the function pointers
> >>>> and making such initialization robust to extra functions being added
> >>>> to the tool API. It can be done in a long winded way but I couldn't
> >>>> devise macro magic to do it. The other problem is around variables
> >>>> like ordered_events that can't currently be const. The patches move us
> >>>> closer to this being a possibility.
> >>>>
> >>>>>> My
> >>>>>> builtin_cmd is in charge of what the tool is rather than some code
> >>>>>> buried in util that thought it was going to do me a favor. The code is
> >>>>>> a refactor and so the benefit is intended to be for the developer and
> >>>>>> how they reason about the use of tool.
> >>>>>
> >>>>> It creates another question though: since there is a lot of code
> >>>>> before perf_tool__init() is called, does the caller mistakenly
> >>>>> change tool before calling perf_tool__init()
> >>>>
> >>>> If they do this their function pointers will be clobbered and their
> >>>> code won't behave as expected, which I'd expect to be easy to observe.
> >>>> In C++ if you were to initialize memory and then use the memory for a
> >>>> placement new to create an object which would call the constructor,
> >>>> the expected behavior would be that the initialized memory's values
> >>>> would get overridden. I see the use of _init and _exit in the code as
> >>>> being our poor man replacements of constructors and destructors.
> >>>>
> >>>>>> how they reason about the use of tool. We generally use _init
> >>>>>> functions rather than having _fill_defaults, so there is a consistency
> >>>>>> argument.
> >>>>>
> >>>>> The caller does not need the "defaults", so why would it set them up.
> >>>>> The session could just as easily do:
> >>>>>
> >>>>> if (tool->cb)
> >>>>> tool->cb(...);
> >>>>> else
> >>>>> cb_stub(...);
> >>>>
> >>>> Multiplied by every stub, we'd probably need a helper function, how to
> >>>> handle argument passing. There's nothing wrong with this as an idea
> >>>> but I think of this code as trying to create a visitor pattern and
> >>>> this is a visitor pattern with a hard time for the caller.
> >>>>
> >>>>>> I don't expect any impact in terms of performance... Moving
> >>>>>> perf_tool__fill_defaults to __perf_session__new had issues with the
> >>>>>> existing code where NULL would be written over a function pointer
> >>>>>> expecting the later fill_defaults to fix it up, doesn't address coding
> >>>>>> consistency where _init is the norm, and adds another reason the tool
> >>>>>> passed to session__new can't be const.
> >>>>>
> >>>>> perf_tool__init() is not a steeping stone to making 'tool' a
> >>>>> const in the first place.
> >>>>
> >>>> It is because the patch series gets rid of fill in defaults which is
> >>>> why we have a mutable tool passed around. I don't think this is up for
> >>>> debate as the patch series clearly goes from a non-const
> >>>> tool to a const tool at the end. Changing perf_tool__init to make all
> >>>> the function pointers NULL and then making every caller have to do a:
> >>>>
> >>>> if (tool->cb)
> >>>> tool->cb(...);
> >>>> else
> >>>> cb_stub(...);
> >>>>
> >>>> I think it is a less elegant solution at the end, it is also a large
> >>>> and more invasive change. The various refactorings to make tool const,
> >>>> changing the aux use of tool, etc. wouldn't be impacted by such a
> >>>> change but I think it is out of scope for this patch series.
> >>>
> >>> I don't think it's a large and invasive change. The tools are mostly
> >>> zero-initialized so we don't need to reset to NULL. And tool->cb is
> >>> called mostly from two functions: machines__deliver_event() and
> >>> perf_session__process_user_event(). Can we change them to check
> >>> NULL and get rid of perf_tool__fill_defaults() to keep it const?
> >>
> >> As I said above, I don't think that is good style and is out of scope
> >> here. It clearly can be done as follow up, but I don't see how that
> >> fixes the style issue.
> >
> > Just to be clear on what the style issue is. We already have code:
> > https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-record.c?h=perf-tools-next#n1461
> > ```
> > if (rec->buildid_all && !rec->timestamp_boundary)
> > rec->tool.sample = NULL;
> > ```
> > that relies on the special behavior of NULL in a function pointer
> > being changed at dispatch time - a simple reading of that code would
> > be anyone calling the function pointer would get a segv. I'm trying to
> > make it so that NULL isn't magic in the context of tool and you can
> > simply look at the tool to understand what its behavior is, much as a
> > virtual method table would work if we could do proper object-oriented
> > development.
>
> In C, NULL or zero is often used as a special value to mean
> no-value. Optional callbacks that are NULL is also not remarkable.
Sure NULL means optional and has since the dawn of time, I don't see
this adding anything to the conversation. What is remarkable here is
passing around a collection of function pointers, then the caller of
the function pointer is supposed to check whether what you want to be
an optional value (I don't, nor does the existing code) function
pointer is present, and if not present the caller needs to know to
call another special function. This is complicated enough that the
function calls could use helper macros, e.g.:
#define CALL_TOOL_SAMPLE(tool, event, sample, evsel, machine) \
if (tool->sample) \
tool->sample(tool, event, sample, machine); \
else \
process_event_sample_stub(tool, event, sample, machine);
you need about 38 of them. Now if I'm a buildtin_cmd writer wanting to
know what gets called on tool, I need to check, do all callers of the
tool use the macro? Perhaps the user decided to do their own NULL test
and call a different function. Cases like tool->finished_round already
need to go to one of two different stubs. There is clearly more
cognitive load on the tool API user as now they must check all uses of
38 function pointers across the code base to understand the behavior
of the tool, they can't just rely on the function pointer in the
struct being the place the code will go. A simple function pointer
call has imo become spaghetti.
There is also the overhead checking the optional value - I've never
seen a C programmer wanting to do this in 30 years of doing paid C
development, the use of the function pointer was to avoid checking,
etc. But maybe the kernel does this and it's an existing kernel style?
I can't find it.
Thanks,
Ian
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v6 00/27] Constify tool pointers
2024-07-19 16:26 ` Ian Rogers
2024-07-22 8:28 ` Adrian Hunter
@ 2024-08-12 13:53 ` Arnaldo Carvalho de Melo
2024-08-12 18:10 ` Ian Rogers
1 sibling, 1 reply; 39+ messages in thread
From: Arnaldo Carvalho de Melo @ 2024-08-12 13:53 UTC (permalink / raw)
To: Ian Rogers
Cc: Adrian Hunter, Peter Zijlstra, Ingo Molnar, 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, Athira Rajeev, Huacai Chen, Yanteng Si,
Sun Haiyong, linux-kernel, linux-perf-users, linux-arm-kernel
On Fri, Jul 19, 2024 at 09:26:57AM -0700, Ian Rogers wrote:
> On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> >
> > On 18/07/24 03:59, 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.
> > >
> > > v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
> >
> > The tags were really meant only for patch 1, the email that was replied to.
> >
> > But now for patches 2 and 3:
> >
> > Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
Applied 1-3, 4 is not applying, I'll look at it later.
- Arnaldo
>
> Sorry for that, you'd mentioned that pt and bts testing which is
> impacted by more than just patch 1.
>
> > Looking at patches 4 to 25, they do not seem to offer any benefit.
> >
> > Instead of patch 26, presumably perf_tool__fill_defaults() could
> > be moved to __perf_session__new(), which perhaps would allow
> > patch 27 as it is.
>
> What I'm trying to do in the series is make it so that the tool isn't
> mutated during its use by session. Ideally we'd be passing a const
> tool to session_new, that's not possible because there's a hack to fix
> ordered events and pipe mode in session__new:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/session.c?h=perf-tools-next#n275
> Imo, it isn't great to pass a tool to session__new where you say you
> want ordered events and then session just goes to change that for you.
> Altering that behavior was beyond the scope of this clean up, so tool
> is only const after session__new.
>
> The reason for doing this is to make it so that when I have a tool I
> can reason that nobody is doing things to change it under my feet. My
> builtin_cmd is in charge of what the tool is rather than some code
> buried in util that thought it was going to do me a favor. The code is
> a refactor and so the benefit is intended to be for the developer and
> how they reason about the use of tool. We generally use _init
> functions rather than having _fill_defaults, so there is a consistency
> argument. I don't expect any impact in terms of performance... Moving
> perf_tool__fill_defaults to __perf_session__new had issues with the
> existing code where NULL would be written over a function pointer
> expecting the later fill_defaults to fix it up, doesn't address coding
> consistency where _init is the norm, and adds another reason the tool
> passed to session__new can't be const.
>
> Thanks,
> Ian
>
> > > v5: Rebase dropping asan fix merged by Namhyung.
> > > v4: Simplify perf_session__deliver_synth_attr_event following Adrian's
> > > suggestions.
> > > v3: Just remove auxtrace dummy tools [Adrian] and make s390-cpumsf
> > > struct removal its own patch [Adrian].
> > > 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 auxtrace: Remove dummy tools
> > > perf s390-cpumsf: Remove unused struct
> > > 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 | 55 +----
> > > 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 | 39 +--
> > > tools/perf/util/data-convert-bt.c | 34 ++-
> > > tools/perf/util/data-convert-json.c | 47 ++--
> > > 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 | 37 +--
> > > tools/perf/util/intel-pt.c | 30 +--
> > > tools/perf/util/jitdump.c | 4 +-
> > > tools/perf/util/s390-cpumsf.c | 11 +-
> > > tools/perf/util/session.c | 366 +++-------------------------
> > > tools/perf/util/session.h | 9 +-
> > > 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 +-
> > > 53 files changed, 977 insertions(+), 1102 deletions(-)
> > > create mode 100644 tools/perf/util/tool.c
> > >
> >
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v6 00/27] Constify tool pointers
2024-08-12 13:53 ` Arnaldo Carvalho de Melo
@ 2024-08-12 18:10 ` Ian Rogers
2024-08-12 19:54 ` Arnaldo Carvalho de Melo
0 siblings, 1 reply; 39+ messages in thread
From: Ian Rogers @ 2024-08-12 18:10 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo
Cc: Adrian Hunter, Peter Zijlstra, Ingo Molnar, 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, Athira Rajeev, Huacai Chen, Yanteng Si,
Sun Haiyong, linux-kernel, linux-perf-users, linux-arm-kernel
On Mon, Aug 12, 2024 at 6:53 AM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> On Fri, Jul 19, 2024 at 09:26:57AM -0700, Ian Rogers wrote:
> > On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> > >
> > > On 18/07/24 03:59, 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.
> > > >
> > > > v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
> > >
> > > The tags were really meant only for patch 1, the email that was replied to.
> > >
> > > But now for patches 2 and 3:
> > >
> > > Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
>
> Applied 1-3, 4 is not applying, I'll look at it later.
I have a rebase and can resend. I haven't addressed Adrian's feedback
as I prefer callers of the tool function pointers not to have to do
NULL tests, I'm trying to minimize spaghetti. Not sure if you're
applying the whole series here or just the beginning.
Thanks,
Ian
^ permalink raw reply [flat|nested] 39+ messages in thread
* Re: [PATCH v6 00/27] Constify tool pointers
2024-08-12 18:10 ` Ian Rogers
@ 2024-08-12 19:54 ` Arnaldo Carvalho de Melo
0 siblings, 0 replies; 39+ messages in thread
From: Arnaldo Carvalho de Melo @ 2024-08-12 19:54 UTC (permalink / raw)
To: Ian Rogers
Cc: Adrian Hunter, Peter Zijlstra, Ingo Molnar, 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, Athira Rajeev, Huacai Chen, Yanteng Si,
Sun Haiyong, linux-kernel, linux-perf-users, linux-arm-kernel
On Mon, Aug 12, 2024 at 11:10:02AM -0700, Ian Rogers wrote:
> On Mon, Aug 12, 2024 at 6:53 AM Arnaldo Carvalho de Melo
> <acme@kernel.org> wrote:
> >
> > On Fri, Jul 19, 2024 at 09:26:57AM -0700, Ian Rogers wrote:
> > > On Fri, Jul 19, 2024 at 1:51 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
> > > >
> > > > On 18/07/24 03:59, 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.
> > > > >
> > > > > v6: Rebase adding Adrian's reviewed-by/tested-by and Leo's tested-by.
> > > >
> > > > The tags were really meant only for patch 1, the email that was replied to.
> > > >
> > > > But now for patches 2 and 3:
> > > >
> > > > Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
> >
> > Applied 1-3, 4 is not applying, I'll look at it later.
>
> I have a rebase and can resend. I haven't addressed Adrian's feedback
Please resend then.
- Arnaldo
> as I prefer callers of the tool function pointers not to have to do
> NULL tests, I'm trying to minimize spaghetti. Not sure if you're
> applying the whole series here or just the beginning.
>
> Thanks,
> Ian
^ permalink raw reply [flat|nested] 39+ messages in thread