[PATCH V3 1/6] perf tools: pass thread info to process function

From: kan . liang
Date: Fri Oct 20 2017 - 16:11:40 EST


From: Kan Liang <Kan.liang@xxxxxxxxx>

For multithreading, the process function needs to know the thread
related information. E.g. saving the process result to the buffer or
file which belongs to specific thread.

Add struct thread_info parameter for process function.
Currently, it only includes thread index.

perf_event__repipe is shared by process function and event_op of
perf_tool in builtin-inject.c. Add dedicated process function
perf_event__repipe_threads.

No functional change.

Signed-off-by: Kan Liang <Kan.liang@xxxxxxxxx>
---
tools/perf/arch/x86/util/tsc.c | 2 +-
tools/perf/builtin-inject.c | 12 +++++++++++-
tools/perf/builtin-record.c | 3 ++-
tools/perf/builtin-sched.c | 12 ++++++++----
tools/perf/builtin-stat.c | 3 ++-
tools/perf/builtin-trace.c | 3 ++-
tools/perf/tests/cpumap.c | 6 ++++--
tools/perf/tests/dwarf-unwind.c | 3 ++-
tools/perf/tests/event_update.c | 12 ++++++++----
tools/perf/tests/stat.c | 9 ++++++---
tools/perf/tests/thread-map.c | 3 ++-
tools/perf/util/auxtrace.c | 2 +-
tools/perf/util/event.c | 15 ++++++++-------
tools/perf/util/event.h | 10 ++++++++--
tools/perf/util/header.c | 16 ++++++++--------
tools/perf/util/intel-bts.c | 3 ++-
tools/perf/util/intel-pt.c | 3 ++-
tools/perf/util/session.c | 4 ++--
18 files changed, 79 insertions(+), 42 deletions(-)

diff --git a/tools/perf/arch/x86/util/tsc.c b/tools/perf/arch/x86/util/tsc.c
index 2e5567c..0affc0f 100644
--- a/tools/perf/arch/x86/util/tsc.c
+++ b/tools/perf/arch/x86/util/tsc.c
@@ -76,5 +76,5 @@ int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc,
event.time_conv.time_shift = tc.time_shift;
event.time_conv.time_zero = tc.time_zero;

- return process(tool, &event, NULL, machine);
+ return process(tool, &event, NULL, machine, NULL);
}
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 2b80329..67a6701 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -191,6 +191,15 @@ static int perf_event__repipe(struct perf_tool *tool,
return perf_event__repipe_synth(tool, event);
}

+static int perf_event__repipe_threads(struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample,
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused)
+{
+ return perf_event__repipe(tool, event, sample, machine);
+}
+
static int perf_event__drop(struct perf_tool *tool __maybe_unused,
union perf_event *event __maybe_unused,
struct perf_sample *sample __maybe_unused,
@@ -413,7 +422,8 @@ static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,
if (dso->kernel)
misc = PERF_RECORD_MISC_KERNEL;

- err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,
+ err = perf_event__synthesize_build_id(tool, dso, misc,
+ perf_event__repipe_threads,
machine);
if (err) {
pr_err("Can't synthesize build_id event for %s\n", dso->long_name);
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index a6cbf16..f53c1163 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -123,7 +123,8 @@ static int record__write(struct record *rec, void *bf, size_t size)
static int process_synthesized_event(struct perf_tool *tool,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct record *rec = container_of(tool, struct record, tool);
return record__write(rec, event, event->header.size);
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index b7e8812..ed34a14 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -1432,7 +1432,8 @@ static void perf_sched__sort_lat(struct perf_sched *sched)
static int process_sched_wakeup_event(struct perf_tool *tool,
struct perf_evsel *evsel,
struct perf_sample *sample,
- struct machine *machine)
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused)
{
struct perf_sched *sched = container_of(tool, struct perf_sched, tool);

@@ -1603,7 +1604,8 @@ static int map_switch_event(struct perf_sched *sched, struct perf_evsel *evsel,
static int process_sched_switch_event(struct perf_tool *tool,
struct perf_evsel *evsel,
struct perf_sample *sample,
- struct machine *machine)
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused)
{
struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
int this_cpu = sample->cpu, err = 0;
@@ -1629,7 +1631,8 @@ static int process_sched_switch_event(struct perf_tool *tool,
static int process_sched_runtime_event(struct perf_tool *tool,
struct perf_evsel *evsel,
struct perf_sample *sample,
- struct machine *machine)
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused)
{
struct perf_sched *sched = container_of(tool, struct perf_sched, tool);

@@ -1659,7 +1662,8 @@ static int perf_sched__process_fork_event(struct perf_tool *tool,
static int process_sched_migrate_task_event(struct perf_tool *tool,
struct perf_evsel *evsel,
struct perf_sample *sample,
- struct machine *machine)
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused)
{
struct perf_sched *sched = container_of(tool, struct perf_sched, tool);

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index dd52541..80d5add 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -293,7 +293,8 @@ static inline int nsec_counter(struct perf_evsel *evsel)
static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
pr_err("failed to write perf data, error: %m\n");
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index afef6fe..f737416 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -1091,7 +1091,8 @@ static int trace__process_event(struct trace *trace, struct machine *machine,
static int trace__tool_process(struct perf_tool *tool,
union perf_event *event,
struct perf_sample *sample,
- struct machine *machine)
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused)
{
struct trace *trace = container_of(tool, struct trace, tool);
return trace__process_event(trace, machine, event, sample);
diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c
index 1997022..fec51c7 100644
--- a/tools/perf/tests/cpumap.c
+++ b/tools/perf/tests/cpumap.c
@@ -11,7 +11,8 @@ struct machine;
static int process_event_mask(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct cpu_map_event *map_event = &event->cpu_map;
struct cpu_map_mask *mask;
@@ -45,7 +46,8 @@ static int process_event_mask(struct perf_tool *tool __maybe_unused,
static int process_event_cpus(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct cpu_map_event *map_event = &event->cpu_map;
struct cpu_map_entries *cpus;
diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwind.c
index 9ba1d21..5ed2271 100644
--- a/tools/perf/tests/dwarf-unwind.c
+++ b/tools/perf/tests/dwarf-unwind.c
@@ -22,7 +22,8 @@
static int mmap_handler(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample,
- struct machine *machine)
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused)
{
return machine__process_mmap2_event(machine, event, sample);
}
diff --git a/tools/perf/tests/event_update.c b/tools/perf/tests/event_update.c
index 9484da2..b5f4ab1 100644
--- a/tools/perf/tests/event_update.c
+++ b/tools/perf/tests/event_update.c
@@ -8,7 +8,8 @@
static int process_event_unit(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct event_update_event *ev = (struct event_update_event *) event;

@@ -21,7 +22,8 @@ static int process_event_unit(struct perf_tool *tool __maybe_unused,
static int process_event_scale(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct event_update_event *ev = (struct event_update_event *) event;
struct event_update_event_scale *ev_data;
@@ -42,7 +44,8 @@ struct event_name {
static int process_event_name(struct perf_tool *tool,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct event_name *tmp = container_of(tool, struct event_name, tool);
struct event_update_event *ev = (struct event_update_event*) event;
@@ -56,7 +59,8 @@ static int process_event_name(struct perf_tool *tool,
static int process_event_cpus(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct event_update_event *ev = (struct event_update_event*) event;
struct event_update_event_cpus *ev_data;
diff --git a/tools/perf/tests/stat.c b/tools/perf/tests/stat.c
index 7f988a9..846cbec 100644
--- a/tools/perf/tests/stat.c
+++ b/tools/perf/tests/stat.c
@@ -22,7 +22,8 @@ static bool has_term(struct stat_config_event *config,
static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct stat_config_event *config = &event->stat_config;
struct perf_stat_config stat_config;
@@ -62,7 +63,8 @@ int test__synthesize_stat_config(struct test *test __maybe_unused, int subtest _
static int process_stat_event(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct stat_event *st = &event->stat;

@@ -92,7 +94,8 @@ int test__synthesize_stat(struct test *test __maybe_unused, int subtest __maybe_
static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct stat_round_event *stat_round = &event->stat_round;

diff --git a/tools/perf/tests/thread-map.c b/tools/perf/tests/thread-map.c
index b3423c7..d2f42ad 100644
--- a/tools/perf/tests/thread-map.c
+++ b/tools/perf/tests/thread-map.c
@@ -52,7 +52,8 @@ int test__thread_map(struct test *test __maybe_unused, int subtest __maybe_unuse
static int process_event(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
- struct machine *machine __maybe_unused)
+ struct machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct thread_map_event *map = &event->thread_map;
struct thread_map *threads;
diff --git a/tools/perf/util/auxtrace.c b/tools/perf/util/auxtrace.c
index 5547457..c4ab2c8 100644
--- a/tools/perf/util/auxtrace.c
+++ b/tools/perf/util/auxtrace.c
@@ -887,7 +887,7 @@ int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
if (err)
goto out_free;

- err = process(tool, ev, NULL, NULL);
+ err = process(tool, ev, NULL, NULL, NULL);
out_free:
free(ev);
return err;
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 47eff47..fd523ca7 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -102,7 +102,7 @@ static int perf_tool__process_synth_event(struct perf_tool *tool,
.cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
};

- return process(tool, event, &synth_sample, machine);
+ return process(tool, event, &synth_sample, machine, NULL);
};

/*
@@ -976,7 +976,7 @@ int perf_event__synthesize_thread_map2(struct perf_tool *tool,
strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
}

- err = process(tool, event, NULL, machine);
+ err = process(tool, event, NULL, machine, NULL);

free(event);
return err;
@@ -1107,7 +1107,7 @@ int perf_event__synthesize_cpu_map(struct perf_tool *tool,
if (!event)
return -ENOMEM;

- err = process(tool, (union perf_event *) event, NULL, machine);
+ err = process(tool, (union perf_event *) event, NULL, machine, NULL);

free(event);
return err;
@@ -1145,7 +1145,7 @@ int perf_event__synthesize_stat_config(struct perf_tool *tool,
"stat config terms unbalanced\n");
#undef ADD

- err = process(tool, (union perf_event *) event, NULL, machine);
+ err = process(tool, (union perf_event *) event, NULL, machine, NULL);

free(event);
return err;
@@ -1170,7 +1170,7 @@ int perf_event__synthesize_stat(struct perf_tool *tool,
event.ena = count->ena;
event.run = count->run;

- return process(tool, (union perf_event *) &event, NULL, machine);
+ return process(tool, (union perf_event *) &event, NULL, machine, NULL);
}

int perf_event__synthesize_stat_round(struct perf_tool *tool,
@@ -1187,7 +1187,7 @@ int perf_event__synthesize_stat_round(struct perf_tool *tool,
event.time = evtime;
event.type = type;

- return process(tool, (union perf_event *) &event, NULL, machine);
+ return process(tool, (union perf_event *) &event, NULL, machine, NULL);
}

void perf_event__read_stat_config(struct perf_stat_config *config,
@@ -1476,7 +1476,8 @@ size_t perf_event__fprintf(union perf_event *event, FILE *fp)
int perf_event__process(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample,
- struct machine *machine)
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused)
{
return machine__process_event(machine, event, sample);
}
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index d6cbb0a..200f3f8 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -659,10 +659,15 @@ struct cpu_map;
struct perf_stat_config;
struct perf_counts_values;

+struct thread_info {
+ int idx;
+};
+
typedef int (*perf_event__handler_t)(struct perf_tool *tool,
union perf_event *event,
struct perf_sample *sample,
- struct machine *machine);
+ struct machine *machine,
+ struct thread_info *thread);

int perf_event__synthesize_thread_map(struct perf_tool *tool,
struct thread_map *threads,
@@ -751,7 +756,8 @@ int perf_event__process_exit(struct perf_tool *tool,
int perf_event__process(struct perf_tool *tool,
union perf_event *event,
struct perf_sample *sample,
- struct machine *machine);
+ struct machine *machine,
+ struct thread_info *thread __maybe_unused);

struct addr_location;

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 605bbd5..c0183fb 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -2986,7 +2986,7 @@ int perf_event__synthesize_attr(struct perf_tool *tool,
ev->attr.header.size = (u16)size;

if (ev->attr.header.size == size)
- err = process(tool, ev, NULL, NULL);
+ err = process(tool, ev, NULL, NULL, NULL);
else
err = -E2BIG;

@@ -3040,7 +3040,7 @@ int perf_event__synthesize_features(struct perf_tool *tool,
fe->header.type = PERF_RECORD_HEADER_FEATURE;
fe->header.size = ff.offset;

- ret = process(tool, ff.buf, NULL, NULL);
+ ret = process(tool, ff.buf, NULL, NULL, NULL);
if (ret) {
free(ff.buf);
return ret;
@@ -3124,7 +3124,7 @@ perf_event__synthesize_event_update_unit(struct perf_tool *tool,
return -ENOMEM;

strncpy(ev->data, evsel->unit, size);
- err = process(tool, (union perf_event *)ev, NULL, NULL);
+ err = process(tool, (union perf_event *)ev, NULL, NULL, NULL);
free(ev);
return err;
}
@@ -3144,7 +3144,7 @@ perf_event__synthesize_event_update_scale(struct perf_tool *tool,

ev_data = (struct event_update_event_scale *) ev->data;
ev_data->scale = evsel->scale;
- err = process(tool, (union perf_event*) ev, NULL, NULL);
+ err = process(tool, (union perf_event *) ev, NULL, NULL, NULL);
free(ev);
return err;
}
@@ -3163,7 +3163,7 @@ perf_event__synthesize_event_update_name(struct perf_tool *tool,
return -ENOMEM;

strncpy(ev->data, evsel->name, len);
- err = process(tool, (union perf_event*) ev, NULL, NULL);
+ err = process(tool, (union perf_event *) ev, NULL, NULL, NULL);
free(ev);
return err;
}
@@ -3194,7 +3194,7 @@ perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
evsel->own_cpus,
type, max);

- err = process(tool, (union perf_event*) ev, NULL, NULL);
+ err = process(tool, (union perf_event *) ev, NULL, NULL, NULL);
free(ev);
return err;
}
@@ -3377,7 +3377,7 @@ int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
ev.tracing_data.header.size = sizeof(ev.tracing_data);
ev.tracing_data.size = aligned_size;

- process(tool, &ev, NULL, NULL);
+ process(tool, &ev, NULL, NULL, NULL);

/*
* The put function will copy all the tracing data
@@ -3455,7 +3455,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool,
ev.build_id.header.size = sizeof(ev.build_id) + len;
memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);

- err = process(tool, &ev, NULL, machine);
+ err = process(tool, &ev, NULL, machine, NULL);

return err;
}
diff --git a/tools/perf/util/intel-bts.c b/tools/perf/util/intel-bts.c
index 218ee2b..8c15309 100644
--- a/tools/perf/util/intel-bts.c
+++ b/tools/perf/util/intel-bts.c
@@ -755,7 +755,8 @@ struct intel_bts_synth {
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 machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct intel_bts_synth *intel_bts_synth =
container_of(tool, struct intel_bts_synth, dummy_tool);
diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index b58f9fd..4858634 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -2121,7 +2121,8 @@ struct intel_pt_synth {
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 machine *machine __maybe_unused,
+ struct thread_info *thread __maybe_unused)
{
struct intel_pt_synth *intel_pt_synth =
container_of(tool, struct intel_pt_synth, dummy_tool);
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index ceac084..f044bad 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -2211,7 +2211,7 @@ int perf_event__synthesize_id_index(struct perf_tool *tool,
struct perf_sample_id *sid;

if (i >= n) {
- err = process(tool, ev, NULL, machine);
+ err = process(tool, ev, NULL, machine, NULL);
if (err)
goto out_err;
nr -= n;
@@ -2238,7 +2238,7 @@ int perf_event__synthesize_id_index(struct perf_tool *tool,
ev->id_index.header.size = sz;
ev->id_index.nr = nr;

- err = process(tool, ev, NULL, machine);
+ err = process(tool, ev, NULL, machine, NULL);
out_err:
free(ev);

--
2.7.4