perf: Reorganize scripting support
Subsystems (4)
Ian Rogers
reviewerPATCH v20 7/21 perf evlist: Add reference count checking
In reply to Ian RogersNow the evlist is reference counted, add reference count checking so that gets and puts are paired and easy to debug. Reference count checking is documented here:
https://perfwiki.github.io/main/reference-count-checking/
This large patch is adding accessors to evlist functions and switching to their use. There was some minor renaming as evlist__mmap is now an accessor to the mmap variable, and the original evlist__mmap is renamed to evlist__do_mmap.
--- tools/perf/arch/arm/util/cs-etm.c | 10 +- tools/perf/arch/arm64/util/arm-spe.c | 8 +- tools/perf/arch/arm64/util/hisi-ptt.c | 2 +- tools/perf/arch/x86/tests/hybrid.c | 20 +- tools/perf/arch/x86/util/auxtrace.c | 2 +- tools/perf/arch/x86/util/intel-bts.c | 6 +- tools/perf/arch/x86/util/intel-pt.c | 9 +- tools/perf/arch/x86/util/iostat.c | 12 +- tools/perf/bench/evlist-open-close.c | 11 +- tools/perf/builtin-annotate.c | 7 +- tools/perf/builtin-ftrace.c | 6 +- tools/perf/builtin-inject.c | 4 +- tools/perf/builtin-kvm.c | 10 +- tools/perf/builtin-kwork.c | 8 +- tools/perf/builtin-lock.c | 2 +- tools/perf/builtin-record.c | 91 ++--- tools/perf/builtin-report.c | 6 +- tools/perf/builtin-sched.c | 24 +- tools/perf/builtin-script.c | 13 +- tools/perf/builtin-stat.c | 73 ++-- tools/perf/builtin-top.c | 52 +-- tools/perf/builtin-trace.c | 22 +- tools/perf/tests/backward-ring-buffer.c | 8 +- tools/perf/tests/code-reading.c | 10 +- tools/perf/tests/event-times.c | 2 +- tools/perf/tests/event_update.c | 2 +- tools/perf/tests/expand-cgroup.c | 4 +- tools/perf/tests/hwmon_pmu.c | 5 +- tools/perf/tests/keep-tracking.c | 8 +- tools/perf/tests/mmap-basic.c | 6 +- tools/perf/tests/openat-syscall-tp-fields.c | 8 +- tools/perf/tests/parse-events.c | 135 +++---- tools/perf/tests/parse-metric.c | 4 +- tools/perf/tests/perf-record.c | 22 +- tools/perf/tests/perf-time-to-tsc.c | 10 +- tools/perf/tests/pfm.c | 8 +- tools/perf/tests/pmu-events.c | 5 +- tools/perf/tests/sample-parsing.c | 45 +-- tools/perf/tests/sw-clock.c | 6 +- tools/perf/tests/switch-tracking.c | 9 +- tools/perf/tests/task-exit.c | 6 +- tools/perf/tests/time-utils-test.c | 14 +- tools/perf/tests/tool_pmu.c | 5 +- tools/perf/tests/topology.c | 2 +- tools/perf/tests/uncore-event-sorting.c | 4 +- tools/perf/ui/browsers/annotate.c | 2 +- tools/perf/ui/browsers/hists.c | 22 +- tools/perf/util/amd-sample-raw.c | 2 +- tools/perf/util/annotate-data.c | 2 +- tools/perf/util/annotate.c | 10 +- tools/perf/util/auxtrace.c | 14 +- tools/perf/util/block-info.c | 4 +- tools/perf/util/bpf_counter.c | 2 +- tools/perf/util/bpf_counter_cgroup.c | 12 +- tools/perf/util/bpf_ftrace.c | 9 +- tools/perf/util/bpf_lock_contention.c | 12 +- tools/perf/util/bpf_off_cpu.c | 14 +- tools/perf/util/cgroup.c | 20 +- tools/perf/util/cs-etm.c | 5 +- tools/perf/util/evlist.c | 395 ++++++++++++-------- tools/perf/util/evlist.h | 251 ++++++++++++- tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 4 +- tools/perf/util/header.c | 47 +-- tools/perf/util/header.h | 2 +- tools/perf/util/intel-tpebs.c | 7 +- tools/perf/util/iostat.c | 2 +- tools/perf/util/iostat.h | 2 +- tools/perf/util/metricgroup.c | 6 +- tools/perf/util/parse-events.c | 6 +- tools/perf/util/pfm.c | 2 +- tools/perf/util/python.c | 90 +++-- tools/perf/util/record.c | 9 +- tools/perf/util/sample-raw.c | 4 +- tools/perf/util/session.c | 43 ++- tools/perf/util/sideband_evlist.c | 24 +- tools/perf/util/sort.c | 2 +- tools/perf/util/stat-display.c | 6 +- tools/perf/util/stat-shadow.c | 4 +- tools/perf/util/stat.c | 4 +- tools/perf/util/stream.c | 4 +- tools/perf/util/synthetic-events.c | 11 +- tools/perf/util/time-utils.c | 12 +- tools/perf/util/top.c | 4 +- 84 files changed, 1085 insertions(+), 718 deletions(-)
tools/perf/arch/arm/util/cs-etm.c +5 -5
@@ -201,7 +201,7 @@ static int cs_etm_validate_config(struct perf_pmu *cs_etm_pmu, {unsigned int idx;int err =0 ; -struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus; +struct perf_cpu_map *event_cpus =evlist__core (evsel->evlist)->user_requested_cpus;struct perf_cpu_map *intersect_cpus;struct perf_cpu cpu;
@@ -325,7 +325,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr, container_of(itr, struct cs_etm_recording, itr);struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;struct evsel *evsel, *cs_etm_evsel =NULL ; -struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;bool privileged =perf_event_paranoid_check (-1 );int err =0 ;
@@ -551,7 +551,7 @@ cs_etm_info_priv_size(struct auxtrace_record *itr, {unsigned int idx;int etmv3 =0 , etmv4 =0 , ete =0 ; -struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *event_cpus =evlist__core (evlist)->user_requested_cpus;struct perf_cpu_map *intersect_cpus;struct perf_cpu cpu;struct perf_pmu *cs_etm_pmu =cs_etm_get_pmu (itr);
@@ -790,7 +790,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,u32 offset;u64 nr_cpu, type;struct perf_cpu_map *cpu_map; -struct perf_cpu_map *event_cpus = session->evlist->core.user_requested_cpus; +struct perf_cpu_map *event_cpus =evlist__core (session->evlist)->user_requested_cpus;struct perf_cpu_map *online_cpus =perf_cpu_map__new_online_cpus ();struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr);
@@ -800,7 +800,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,if (priv_size !=cs_etm_info_priv_size (itr, session->evlist))return -EINVAL ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ;/* If the cpu_map has the "any" CPU all online CPUs are involved */
tools/perf/arch/arm64/util/arm-spe.c +4 -4
@@ -60,7 +60,7 @@ static bool arm_spe_is_set_freq(struct evsel *evsel)*/ static struct perf_cpu_map *arm_spe_find_cpus (struct evlist *evlist) { -struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *event_cpus =evlist__core (evlist)->user_requested_cpus;struct perf_cpu_map *online_cpus =perf_cpu_map__new_online_cpus ();struct perf_cpu_map *intersect_cpus;
@@ -157,7 +157,7 @@ static int arm_spe_info_fill(struct auxtrace_record *itr,if (priv_size !=arm_spe_info_priv_size (itr, session->evlist))return -EINVAL ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ; cpu_map =arm_spe_find_cpus (session->evlist);
@@ -363,7 +363,7 @@ static int arm_spe_setup_tracking_event(struct evlist *evlist, {int err;struct evsel *tracking_evsel; -struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;/* Add dummy event to keep tracking */ err =parse_event (evlist,"dummy:u" );
@@ -396,7 +396,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,struct arm_spe_recording *sper = container_of(itr, struct arm_spe_recording, itr);struct evsel *evsel, *tmp; -struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;bool discard = false;int err;u64 discard_bit;
tools/perf/arch/arm64/util/hisi-ptt.c +1 -1
@@ -53,7 +53,7 @@ static int hisi_ptt_info_fill(struct auxtrace_record *itr,if (priv_size !=HISI_PTT_AUXTRACE_PRIV_SIZE )return -EINVAL ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ; auxtrace_info->type =PERF_AUXTRACE_HISI_PTT ;
tools/perf/arch/x86/tests/hybrid.c +10 -10
@@ -26,7 +26,7 @@ static int test__hybrid_hw_event_with_pmu(struct evlist *evlist) {struct evsel *evsel =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,1 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,1 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
@@ -38,7 +38,7 @@ static int test__hybrid_hw_group_event(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
@@ -57,7 +57,7 @@ static int test__hybrid_sw_hw_group_event(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_SOFTWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong leader" ,evsel__has_leader (evsel, leader));
@@ -74,7 +74,7 @@ static int test__hybrid_hw_sw_group_event(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
@@ -91,7 +91,7 @@ static int test__hybrid_group_modifier1(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
@@ -113,7 +113,7 @@ static int test__hybrid_raw1(struct evlist *evlist) {struct perf_evsel *evsel; -perf_evlist__for_each_evsel (&evlist->core, evsel) { +perf_evlist__for_each_evsel (evlist__core (evlist), evsel) {struct perf_pmu *pmu =perf_pmus__find_by_type (evsel->attr.type);TEST_ASSERT_VAL ("missing pmu" , pmu);
@@ -127,7 +127,7 @@ static int test__hybrid_raw2(struct evlist *evlist) {struct evsel *evsel =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,1 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,1 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_RAW == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,0x1a ));return TEST_OK ;
@@ -137,7 +137,7 @@ static int test__hybrid_cache_event(struct evlist *evlist) {struct evsel *evsel =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,1 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,1 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HW_CACHE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong config" ,0x2 == (evsel->core.attr.config &0xffffffff ));return TEST_OK ;
@@ -148,7 +148,7 @@ static int test__checkevent_pmu(struct evlist *evlist)struct evsel *evsel =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,1 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,1 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_RAW == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong config" ,10 == evsel->core.attr.config);TEST_ASSERT_VAL ("wrong config1" ,1 == evsel->core.attr.config1);
@@ -168,7 +168,7 @@ static int test__hybrid_hw_group_event_2(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
tools/perf/arch/x86/util/auxtrace.c +1 -1
@@ -55,7 +55,7 @@ struct auxtrace_record *auxtrace_record__init(struct evlist *evlist, int *err) {char buffer[64 ]; -struct perf_cpu cpu =perf_cpu_map__min (evlist->core.all_cpus); +struct perf_cpu cpu =perf_cpu_map__min (evlist__core (evlist)->all_cpus);int ret; *err =0 ;
tools/perf/arch/x86/util/intel-bts.c +3 -3
@@ -79,10 +79,10 @@ static int intel_bts_info_fill(struct auxtrace_record *itr,if (priv_size !=INTEL_BTS_AUXTRACE_PRIV_SIZE )return -EINVAL ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ; - pc = session->evlist->mmap[0 ].core.base; + pc =evlist__mmap (session->evlist)[0 ].core.base;if (pc) { err =perf_read_tsc_conversion (pc, &tc);if (err) {
@@ -114,7 +114,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr, container_of(itr, struct intel_bts_recording, itr);struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;struct evsel *evsel, *intel_bts_evsel =NULL ; -const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +const struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;bool privileged =perf_event_paranoid_check (-1 );if (opts->auxtrace_sample_mode) {
tools/perf/arch/x86/util/intel-pt.c +5 -4
@@ -360,10 +360,10 @@ static int intel_pt_info_fill(struct auxtrace_record *itr, filter =intel_pt_find_filter (session->evlist, ptr->intel_pt_pmu); filter_str_len = filter ?strlen (filter) :0 ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ; - pc = session->evlist->mmap[0 ].core.base; + pc =evlist__mmap (session->evlist)[0 ].core.base;if (pc) { err =perf_read_tsc_conversion (pc, &tc);if (err) {
@@ -376,7 +376,8 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,ui__warning ("Intel Processor Trace: TSC not available\n" ); } - per_cpu_mmaps = !perf_cpu_map__is_any_cpu_or_is_empty (session->evlist->core.user_requested_cpus); + per_cpu_mmaps = !perf_cpu_map__is_any_cpu_or_is_empty ( +evlist__core (session->evlist)->user_requested_cpus); auxtrace_info->type =PERF_AUXTRACE_INTEL_PT ; auxtrace_info->priv[INTEL_PT_PMU_TYPE ] = intel_pt_pmu->type;
@@ -621,7 +622,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;bool have_timing_info, need_immediate = false;struct evsel *evsel, *intel_pt_evsel =NULL ; -const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +const struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;bool privileged =perf_event_paranoid_check (-1 );u64 tsc_bit;int err;
tools/perf/arch/x86/util/iostat.c +7 -5
@@ -332,13 +332,15 @@ static int iostat_event_group(struct evlist *evl,return ret; } -int iostat_prepare (struct evlist *evlist,struct perf_stat_config *config) +int iostat_prepare (struct evlist **evlist_ptr,struct perf_stat_config *config) { -if (evlist->core.nr_entries >0 ) { +struct evlist *evlist = *evlist_ptr; + +if (evlist__nr_entries (evlist) >0 ) {pr_warning ("The -e and -M options are not supported." "All chosen events/metrics will be dropped\n" );evlist__put (evlist); - evlist =evlist__new (); + *evlist_ptr = evlist =evlist__new ();if (!evlist)return -ENOMEM ; }
@@ -400,7 +402,7 @@ void iostat_prefix(struct evlist *evlist,struct perf_stat_config *config, char *prefix, struct timespec *ts) { - struct iio_root_port *rp = evlist->selected->priv; + struct iio_root_port *rp = evlist__selected(evlist)->priv;if (rp) {/*
@@ -463,7 +465,7 @@ void iostat_print_counters(struct evlist *evlist,iostat_prefix (evlist, config, prefix, ts);fprintf (config->output,"%s" , prefix);evlist__for_each_entry (evlist, counter) { - perf_device = evlist->selected->priv; + perf_device =evlist__selected (evlist)->priv;if (perf_device && perf_device != counter->priv) {evlist__set_selected (evlist, counter);iostat_prefix (evlist, config, prefix, ts);
tools/perf/bench/evlist-open-close.c +6 -5
@@ -116,7 +116,7 @@ static int bench__do_evlist_open_close(struct evlist *evlist)return err; } - err =evlist__mmap (evlist, opts.mmap_pages); + err =evlist__do_mmap (evlist, opts.mmap_pages);if (err <0 ) {pr_err ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));return err;
@@ -124,7 +124,7 @@ static int bench__do_evlist_open_close(struct evlist *evlist)evlist__enable (evlist);evlist__disable (evlist); -evlist__munmap (evlist); +evlist__do_munmap (evlist);evlist__close (evlist);return 0 ;
@@ -145,10 +145,11 @@ static int bench_evlist_open_close__run(char *evstr, const char *uid_str)init_stats (&time_stats); -printf (" Number of cpus:\t%d\n" ,perf_cpu_map__nr (evlist->core.user_requested_cpus)); -printf (" Number of threads:\t%d\n" , evlist->core.threads->nr); +printf (" Number of cpus:\t%d\n" , +perf_cpu_map__nr (evlist__core (evlist)->user_requested_cpus)); +printf (" Number of threads:\t%d\n" ,evlist__core (evlist)->threads->nr);printf (" Number of events:\t%d (%d fds)\n" , - evlist->core.nr_entries,evlist__count_evsel_fds (evlist)); +evlist__nr_entries (evlist),evlist__count_evsel_fds (evlist));printf (" Number of iterations:\t%d\n" , iterations);evlist__put (evlist);
tools/perf/builtin-annotate.c +5 -2
@@ -562,7 +562,7 @@ static int __cmd_annotate(struct perf_annotate *ann) goto out;if ((use_browser ==1 || ann->use_stdio2) && ann->has_br_stack) -if (session->evlist->nr_br_cntr >0 ) +if (evlist__nr_br_cntr (session->evlist) >0 ) annotate_opts.show_br_cntr = true;if (dump_trace) {
@@ -928,8 +928,11 @@ int cmd_annotate(int argc, const char **argv)* branch counters, if the corresponding branch info is available * in the perf data in the TUI mode. */ -if ((use_browser ==1 || annotate.use_stdio2) && annotate.has_br_stack) +if ((use_browser ==1 || annotate.use_stdio2) && annotate.has_br_stack) { sort__mode =SORT_MODE__BRANCH ; +if (evlist__nr_br_cntr (annotate.session->evlist) >0 ) + annotate_opts.show_br_cntr = true; + }if (setup_sorting (/*evlist=*/ NULL ,perf_session__env (annotate.session)) <0 )usage_with_options (annotate_usage, options);
tools/perf/builtin-ftrace.c +3 -3
@@ -377,9 +377,9 @@ static int set_tracing_pid(struct perf_ftrace *ftrace)if (target__has_cpu (&ftrace->target))return 0 ; -for (i =0 ; i <perf_thread_map__nr (ftrace->evlist->core.threads); i++) { +for (i =0 ; i <perf_thread_map__nr (evlist__core (ftrace->evlist)->threads); i++) {scnprintf (buf,sizeof (buf),"%d" , -perf_thread_map__pid (ftrace->evlist->core.threads, i)); +perf_thread_map__pid (evlist__core (ftrace->evlist)->threads, i));if (append_tracing_file ("set_ftrace_pid" , buf) <0 )return -1 ; }
@@ -413,7 +413,7 @@ static int set_tracing_cpumask(struct perf_cpu_map *cpumap)static int set_tracing_cpu (struct perf_ftrace *ftrace) { -struct perf_cpu_map *cpumap = ftrace->evlist->core.user_requested_cpus; +struct perf_cpu_map *cpumap =evlist__core (ftrace->evlist)->user_requested_cpus;if (!target__has_cpu (&ftrace->target))return 0 ;
tools/perf/builtin-inject.c +2 -2
@@ -1496,7 +1496,7 @@ static int synthesize_id_index(struct perf_inject *inject, size_t new_cnt)struct perf_session *session = inject->session;struct evlist *evlist = session->evlist;struct machine *machine = &session->machines.host; -size_t from = evlist->core.nr_entries - new_cnt; +size_t from =evlist__nr_entries (evlist) - new_cnt;return __perf_event__synthesize_id_index (&inject->tool, perf_event__repipe, evlist, machine, from);
@@ -2031,7 +2031,7 @@ static int host__finished_init(const struct perf_tool *tool, struct perf_sessionif (ret)return ret; - ret =synthesize_id_index (inject, gs->session->evlist->core.nr_entries); + ret =synthesize_id_index (inject,evlist__nr_entries (gs->session->evlist));if (ret) {pr_err ("Failed to synthesize id_index\n" );return ret;
tools/perf/builtin-kvm.c +5 -5
@@ -1221,7 +1221,7 @@ static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,int err; *mmap_time =ULLONG_MAX ; - md = &evlist->mmap[idx]; + md = &evlist__mmap (evlist)[idx]; err =perf_mmap__read_init (&md->core);if (err <0 )return (err == -EAGAIN ) ?0 :-1 ;
@@ -1266,7 +1266,7 @@ static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)s64 n, ntotal =0 ;u64 flush_time =ULLONG_MAX , mmap_time; -for (i =0 ; i < kvm->evlist->core.nr_mmaps; i++) { +for (i =0 ; i <evlist__core (kvm->evlist)->nr_mmaps; i++) { n =perf_kvm__mmap_read_idx (kvm, i, &mmap_time);if (n <0 )return -1 ;
@@ -1449,7 +1449,7 @@ static int kvm_events_live_report(struct perf_kvm_stat *kvm)evlist__enable (kvm->evlist);while (!done) { -struct fdarray *fda = &kvm->evlist->core.pollfd; +struct fdarray *fda = &evlist__core (kvm->evlist)->pollfd;int rc; rc =perf_kvm__mmap_read (kvm);
@@ -1531,7 +1531,7 @@ static int kvm_live_open_events(struct perf_kvm_stat *kvm) goto out; } -if (evlist__mmap (evlist, kvm->opts.mmap_pages) <0 ) { +if (evlist__do_mmap (evlist, kvm->opts.mmap_pages) <0 ) {ui__error ("Failed to mmap the events: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));evlist__close (evlist);
@@ -1931,7 +1931,7 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,perf_session__set_id_hdr_size (kvm->session);ordered_events__set_copy_on_queue (&kvm->session->ordered_events, true);machine__synthesize_threads (&kvm->session->machines.host, &kvm->opts.target, - kvm->evlist->core.threads, true, false,1 ); +evlist__core (kvm->evlist)->threads, true, false,1 ); err =kvm_live_open_events (kvm);if (err) goto out;
tools/perf/builtin-kwork.c +4 -4
@@ -1814,7 +1814,7 @@ static int perf_kwork__check_config(struct perf_kwork *kwork, } } -list_for_each_entry (evsel, &session->evlist->core.entries, core.node) { +list_for_each_entry (evsel, &evlist__core (session->evlist)->entries, core.node) {if (kwork->show_callchain && !evsel__has_callchain (evsel)) {pr_debug ("Samples do not have callchains\n" ); kwork->show_callchain =0 ;
@@ -1864,9 +1864,9 @@ static int perf_kwork__read_events(struct perf_kwork *kwork) goto out_delete; } - kwork->nr_events = session->evlist->stats.nr_events[0 ]; - kwork->nr_lost_events = session->evlist->stats.total_lost; - kwork->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST ]; + kwork->nr_events =evlist__stats (session->evlist)->nr_events[0 ]; + kwork->nr_lost_events =evlist__stats (session->evlist)->total_lost; + kwork->nr_lost_chunks =evlist__stats (session->evlist)->nr_events[PERF_RECORD_LOST ]; out_delete:perf_session__delete (session);
tools/perf/builtin-lock.c +1 -1
@@ -2129,7 +2129,7 @@ static int __cmd_contention(int argc, const char **argv)evlist__start_workload (con.evlist);while (!done) { -if (argc &&waitpid (con.evlist->workload.pid,NULL ,WNOHANG ) >0 ) +if (argc &&waitpid (evlist__workload_pid (con.evlist),NULL ,WNOHANG ) >0 )break ;sleep (1 ); }
tools/perf/builtin-record.c +46 -45
268 lines not on this page · raw message
tools/perf/builtin-report.c +3 -3
@@ -561,7 +561,7 @@ static int evlist__tty_browse_hists(struct evlist *evlist, struct report *rep, cif (!quiet) {fprintf (stdout,"#\n# Total Lost Samples: %" PRIu64"\n#\n" , - evlist->stats.total_lost_samples); +evlist__stats (evlist)->total_lost_samples); }evlist__for_each_entry (evlist, pos) {
@@ -1156,7 +1156,7 @@ static int __cmd_report(struct report *rep) PERF_HPP_REPORT__BLOCK_AVG_CYCLES, }; -if (session->evlist->nr_br_cntr >0 ) +if (evlist__nr_br_cntr (session->evlist) >0 ) block_hpps[nr_hpps++] =PERF_HPP_REPORT__BLOCK_BRANCH_COUNTER ; block_hpps[nr_hpps++] =PERF_HPP_REPORT__BLOCK_RANGE ;
@@ -1291,7 +1291,7 @@ static int process_attr(const struct perf_tool *tool __maybe_unused,* on events sample_type. */ sample_type =evlist__combined_sample_type (*pevlist); - session = (*pevlist)->session; + session =evlist__session (*pevlist);callchain_param_setup (sample_type,perf_session__e_machine (session,/*e_flags=*/ NULL ));return 0 ; }
tools/perf/builtin-sched.c +12 -12
@@ -2014,9 +2014,9 @@ static int perf_sched__read_events(struct perf_sched *sched) goto out_delete; } - sched->nr_events = session->evlist->stats.nr_events[0 ]; - sched->nr_lost_events = session->evlist->stats.total_lost; - sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST ]; + sched->nr_events =evlist__stats (session->evlist)->nr_events[0 ]; + sched->nr_lost_events =evlist__stats (session->evlist)->total_lost; + sched->nr_lost_chunks =evlist__stats (session->evlist)->nr_events[PERF_RECORD_LOST ]; } rc =0 ;
@@ -3292,7 +3292,7 @@ static int timehist_check_attr(struct perf_sched *sched,struct evsel *evsel;struct evsel_runtime *er; -list_for_each_entry (evsel, &evlist->core.entries, core.node) { +list_for_each_entry (evsel, &evlist__core (evlist)->entries, core.node) { er =evsel__get_runtime (evsel);if (er ==NULL ) {pr_err ("Failed to allocate memory for evsel runtime data\n" );
@@ -3464,9 +3464,9 @@ static int perf_sched__timehist(struct perf_sched *sched) goto out; } - sched->nr_events = evlist->stats.nr_events[0 ]; - sched->nr_lost_events = evlist->stats.total_lost; - sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST ]; + sched->nr_events =evlist__stats (evlist)->nr_events[0 ]; + sched->nr_lost_events =evlist__stats (evlist)->total_lost; + sched->nr_lost_chunks =evlist__stats (evlist)->nr_events[PERF_RECORD_LOST ];if (sched->summary)timehist_print_summary (sched, session);
@@ -3971,7 +3971,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,if (err <0 ) goto out; - user_requested_cpus = evlist->core.user_requested_cpus; + user_requested_cpus =evlist__core (evlist)->user_requested_cpus; err = perf_event__synthesize_schedstat(&(sched->tool), process_synthesized_schedstat_event,
@@ -3987,7 +3987,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,evlist__start_workload (evlist);while (!done) { -if (argc &&waitpid (evlist->workload.pid,NULL ,WNOHANG ) >0 ) +if (argc &&waitpid (evlist__workload_pid (evlist),NULL ,WNOHANG ) >0 )break ;sleep (1 ); }
@@ -4688,7 +4688,7 @@ static int perf_sched__schedstat_report(struct perf_sched *sched)if (err <0 ) goto out; - user_requested_cpus = session->evlist->core.user_requested_cpus; + user_requested_cpus =evlist__core (session->evlist)->user_requested_cpus; err =perf_session__process_events (session);
@@ -4864,7 +4864,7 @@ static int perf_sched__schedstat_live(struct perf_sched *sched,if (err <0 ) goto out; - user_requested_cpus = evlist->core.user_requested_cpus; + user_requested_cpus =evlist__core (evlist)->user_requested_cpus; err = perf_event__synthesize_schedstat(&(sched->tool), process_synthesized_event_live,
@@ -4880,7 +4880,7 @@ static int perf_sched__schedstat_live(struct perf_sched *sched,evlist__start_workload (evlist);while (!done) { -if (argc &&waitpid (evlist->workload.pid,NULL ,WNOHANG ) >0 ) +if (argc &&waitpid (evlist__workload_pid (evlist),NULL ,WNOHANG ) >0 )break ;sleep (1 ); }
tools/perf/builtin-script.c +7 -6
@@ -2229,9 +2229,10 @@ static int script_find_metrics(const struct pmu_metric *pm,evlist__for_each_entry (metric_evlist, metric_evsel) {struct evsel *script_evsel =map_metric_evsel_to_script_evsel (script_evlist, metric_evsel); -struct metric_event *metric_me =metricgroup__lookup (&metric_evlist->metric_events, - metric_evsel, -/*create=*/ false); +struct metric_event *metric_me = +metricgroup__lookup (evlist__metric_events (metric_evlist), + metric_evsel, +/*create=*/ false);if (script_evsel->metric_id ==NULL ) { script_evsel->metric_id = metric_evsel->metric_id;
@@ -2251,7 +2252,7 @@ static int script_find_metrics(const struct pmu_metric *pm,if (metric_me) {struct metric_expr *expr;struct metric_event *script_me = -metricgroup__lookup (&script_evlist->metric_events, +metricgroup__lookup (evlist__metric_events (script_evlist), script_evsel,/*create=*/ true);
@@ -2321,7 +2322,7 @@ static void perf_sample__fprint_metric(struct thread *thread,assert (stat_config.aggr_mode ==AGGR_GLOBAL ); stat_config.aggr_get_id = script_aggr_cpu_id_get; stat_config.aggr_map = -cpu_aggr_map__new (evsel->evlist->core.user_requested_cpus, +cpu_aggr_map__new (evlist__core (evsel->evlist)->user_requested_cpus, aggr_cpu_id__global,/*data=*/ NULL ,/*needs_sort=*/ false); }
@@ -3909,7 +3910,7 @@ static int set_maps(struct perf_script *script)if (WARN_ONCE (script->allocated,"stats double allocation\n" ))return -EINVAL ; -perf_evlist__set_maps (&evlist->core, script->cpus, script->threads); +perf_evlist__set_maps (evlist__core (evlist), script->cpus, script->threads);if (evlist__alloc_stats (&stat_config, evlist,/*alloc_raw=*/ true))return -ENOMEM ;
tools/perf/builtin-stat.c +39 -34
206 lines not on this page · raw message
tools/perf/builtin-top.c +26 -26
175 lines not on this page · raw message
tools/perf/builtin-trace.c +11 -11
@@ -2023,7 +2023,7 @@ static int trace__symbols_init(struct trace *trace, int argc, const char **argv, goto out; err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target, - evlist->core.threads, trace__tool_process, + evlist__core(evlist)->threads, trace__tool_process, /*needs_mmap=*/callchain_param.enabled && !trace->summary_only, /*mmap_data=*/false,
@@ -4195,7 +4195,7 @@ static int trace__set_filter_pids(struct trace *trace) err =augmented_syscalls__set_filter_pids (trace->filter_pids.nr, trace->filter_pids.entries); } - }else if (perf_thread_map__pid (trace->evlist->core.threads,0 ) ==-1 ) { + }else if (perf_thread_map__pid (evlist__core (trace->evlist)->threads,0 ) ==-1 ) { err =trace__set_filter_loop_pids (trace); }
@@ -4509,7 +4509,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)fprintf (trace->output,"Couldn't run the workload!\n" ); goto out_put_evlist; } - workload_pid = evlist->workload.pid; + workload_pid =evlist__workload_pid (evlist); } err =evlist__open (evlist);
@@ -4561,7 +4561,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv) goto out_error_apply_filters;if (!trace->summary_only || !trace->summary_bpf) { - err =evlist__mmap (evlist, trace->opts.mmap_pages); + err =evlist__do_mmap (evlist, trace->opts.mmap_pages);if (err <0 ) goto out_error_mmap; }
@@ -4580,8 +4580,8 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (trace->summary_bpf)trace_start_bpf_summary (); - trace->multiple_threads =perf_thread_map__pid (evlist->core.threads,0 ) ==-1 || -perf_thread_map__nr (evlist->core.threads) >1 || + trace->multiple_threads =perf_thread_map__pid (evlist__core (evlist)->threads,0 ) ==-1 || +perf_thread_map__nr (evlist__core (evlist)->threads) >1 ||evlist__first (evlist)->core.attr.inherit;/*
@@ -4598,11 +4598,11 @@ static int trace__run(struct trace *trace, int argc, const char **argv) again: before = trace->nr_events; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) {union perf_event *event;struct mmap *md; - md = &evlist->mmap[i]; + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
@@ -5304,7 +5304,7 @@ static int trace__parse_cgroups(const struct option *opt, const char *str, int u {struct trace *trace = opt->value; -if (!list_empty (&trace->evlist->core.entries)) { +if (!list_empty (&evlist__core (trace->evlist)->entries)) {struct option o = { .value = &trace->evlist, };
@@ -5578,7 +5578,7 @@ int cmd_trace(int argc, const char **argv)* .perfconfig trace.add_events, and filter those out. */ if (!trace.trace_syscalls && !trace.trace_pgfaults && - trace.evlist->core.nr_entries ==0 /* Was --events used? */ ) { +evlist__nr_entries (trace.evlist) ==0 /* Was --events used? */ ) { trace.trace_syscalls = true; }/*
@@ -5664,7 +5664,7 @@ int cmd_trace(int argc, const char **argv) symbol_conf.use_callchain = true; } -if (trace.evlist->core.nr_entries >0 ) { +if (evlist__nr_entries (trace.evlist) >0 ) {bool use_btf = false;evlist__set_default_evsel_handler (trace.evlist, trace__event_handler);
tools/perf/tests/backward-ring-buffer.c +4 -4
@@ -34,8 +34,8 @@ static int count_samples(struct evlist *evlist, int *sample_count, {int i; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { -struct mmap *map = &evlist->overwrite_mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { +struct mmap *map = &evlist__overwrite_mmap (evlist)[i];union perf_event *event;perf_mmap__read_init (&map->core);
@@ -65,7 +65,7 @@ static int do_test(struct evlist *evlist, int mmap_pages,int err;char sbuf[STRERR_BUFSIZE ]; - err =evlist__mmap (evlist, mmap_pages); + err =evlist__do_mmap (evlist, mmap_pages);if (err <0 ) {pr_debug ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -77,7 +77,7 @@ static int do_test(struct evlist *evlist, int mmap_pages,evlist__disable (evlist); err =count_samples (evlist, sample_count, comm_count); -evlist__munmap (evlist); +evlist__do_munmap (evlist);return err; }
tools/perf/tests/code-reading.c +5 -5
@@ -592,8 +592,8 @@ static int process_events(struct machine *machine, struct evlist *evlist,struct mmap *md;int i, ret; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { - md = &evlist->mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
@@ -781,7 +781,7 @@ static int do_test_code_reading(bool try_kcore) goto out_put; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads); str = events[evidx];pr_debug ("Parsing event '%s'\n" , str);
@@ -809,7 +809,7 @@ static int do_test_code_reading(bool try_kcore)pr_debug ("perf_evlist__open() failed!\n%s\n" , errbuf); } -perf_evlist__set_maps (&evlist->core,NULL ,NULL ); +perf_evlist__set_maps (evlist__core (evlist),NULL ,NULL );evlist__put (evlist); evlist =NULL ;continue ;
@@ -820,7 +820,7 @@ static int do_test_code_reading(bool try_kcore)if (events[evidx] ==NULL ) goto out_put; - ret =evlist__mmap (evlist,UINT_MAX ); + ret =evlist__do_mmap (evlist,UINT_MAX );if (ret <0 ) {pr_debug ("evlist__mmap failed\n" ); goto out_put;
tools/perf/tests/event-times.c +1 -1
@@ -50,7 +50,7 @@ static int attach__enable_on_exec(struct evlist *evlist)static int detach__enable_on_exec (struct evlist *evlist) { -waitpid (evlist->workload.pid,NULL ,0 ); +waitpid (evlist__workload_pid (evlist),NULL ,0 );return 0 ; }
tools/perf/tests/event_update.c +1 -1
@@ -92,7 +92,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtesTEST_ASSERT_VAL ("failed to allocate ids" , !perf_evsel__alloc_id (&evsel->core,1 ,1 )); -perf_evlist__id_add (&evlist->core, &evsel->core,0 ,0 ,123 ); +perf_evlist__id_add (evlist__core (evlist), &evsel->core,0 ,0 ,123 );free ((char *)evsel->unit); evsel->unit =strdup ("KRAVA" );
tools/perf/tests/expand-cgroup.c +2 -2
@@ -28,7 +28,7 @@ static int test_expand_events(struct evlist *evlist)TEST_ASSERT_VAL ("evlist is empty" , !evlist__empty (evlist)); - nr_events = evlist->core.nr_entries; + nr_events =evlist__nr_entries (evlist); ev_name =calloc (nr_events,sizeof (*ev_name));if (ev_name ==NULL ) {pr_debug ("memory allocation failure\n" );
@@ -54,7 +54,7 @@ static int test_expand_events(struct evlist *evlist) } ret =TEST_FAIL ; -if (evlist->core.nr_entries != nr_events * nr_cgrps) { +if (evlist__nr_entries (evlist) != nr_events * nr_cgrps) {pr_debug ("event count doesn't match\n" ); goto out; }
tools/perf/tests/hwmon_pmu.c +3 -2
@@ -184,9 +184,10 @@ static int do_test(size_t i, bool with_pmu, bool with_alias) } ret =TEST_OK ; -if (with_pmu ? (evlist->core.nr_entries !=1 ) : (evlist->core.nr_entries <1 )) { +if (with_pmu ? (evlist__nr_entries (evlist) !=1 ) + : (evlist__nr_entries (evlist) <1 )) {pr_debug ("FAILED %s:%d Unexpected number of events for '%s' of %d\n" , - __FILE__, __LINE__, str, evlist->core.nr_entries); + __FILE__, __LINE__, str,evlist__nr_entries (evlist)); ret =TEST_FAIL ; goto out; }
tools/perf/tests/keep-tracking.c +4 -4
@@ -37,8 +37,8 @@ static int find_comm(struct evlist *evlist, const char *comm)int i, found; found =0 ; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { - md = &evlist->mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;while ((event =perf_mmap__read_event (&md->core)) !=NULL ) {
@@ -87,7 +87,7 @@ static int test__keep_tracking(struct test_suite *test __maybe_unused, int subte evlist =evlist__new ();CHECK_NOT_NULL__ (evlist); -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);CHECK__ (parse_event (evlist,"dummy:u" ));CHECK__ (parse_event (evlist,"cpu-cycles:u" ));
@@ -106,7 +106,7 @@ static int test__keep_tracking(struct test_suite *test __maybe_unused, int subte goto out_err; } -CHECK__ (evlist__mmap (evlist,UINT_MAX )); +CHECK__ (evlist__do_mmap (evlist,UINT_MAX ));/* * First, test that a 'comm' event can be found when the event is
tools/perf/tests/mmap-basic.c +3 -3
@@ -81,7 +81,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest goto out_free_cpus; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);for (i =0 ; i < nsyscalls; ++i) {char name[64 ];
@@ -113,7 +113,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest expected_nr_events[i] =1 +rand () %127 ; } -if (evlist__mmap (evlist,128 ) <0 ) { +if (evlist__do_mmap (evlist,128 ) <0 ) {pr_debug ("failed to mmap events: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf))); goto out_put_evlist;
@@ -124,7 +124,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest syscalls[i](); } - md = &evlist->mmap[0 ]; + md = &evlist__mmap (evlist)[0 ];if (perf_mmap__read_init (&md->core) <0 ) goto out_init;
tools/perf/tests/openat-syscall-tp-fields.c +4 -4
@@ -64,7 +64,7 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unusedevsel__config (evsel, &opts,NULL ); -perf_thread_map__set_pid (evlist->core.threads,0 ,getpid ()); +perf_thread_map__set_pid (evlist__core (evlist)->threads,0 ,getpid ()); err =evlist__open (evlist);if (err <0 ) {
@@ -73,7 +73,7 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unused goto out_put_evlist; } - err =evlist__mmap (evlist,UINT_MAX ); + err =evlist__do_mmap (evlist,UINT_MAX );if (err <0 ) {pr_debug ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -90,11 +90,11 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unusedwhile (1 ) {int before = nr_events; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) {union perf_event *event;struct mmap *md; - md = &evlist->mmap[i]; + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
tools/perf/tests/parse-events.c +70 -65
519 lines not on this page · raw message
tools/perf/tests/parse-metric.c +2 -2
@@ -53,7 +53,7 @@ static double compute_single(struct evlist *evlist, const char *name)struct evsel *evsel;evlist__for_each_entry (evlist, evsel) { - me =metricgroup__lookup (&evlist->metric_events, evsel, false); + me =metricgroup__lookup (evlist__metric_events (evlist), evsel, false);if (me !=NULL ) { list_for_each_entry (mexp, &me->head, nd) {if (strcmp (mexp->metric_name, name))
@@ -88,7 +88,7 @@ static int __compute_metric(const char *name, struct value *vals,return -ENOMEM ; } -perf_evlist__set_maps (&evlist->core, cpus,NULL ); +perf_evlist__set_maps (evlist__core (evlist), cpus,NULL );/* Parse the metric into metric_events list. */ pme_test =find_core_metrics_table ("testarch" ,"testcpu" );
tools/perf/tests/perf-record.c +11 -11
@@ -129,7 +129,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestevsel__set_sample_bit (evsel,TIME );evlist__config (evlist, &opts,NULL ); - err =sched__get_first_possible_cpu (evlist->workload.pid, cpu_mask); + err =sched__get_first_possible_cpu (evlist__workload_pid (evlist), cpu_mask);if (err <0 ) {pr_debug ("sched__get_first_possible_cpu: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -142,7 +142,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest/* * So that we can check perf_sample.cpu on all the samples. */ -if (sched_setaffinity (evlist->workload.pid, cpu_mask_size, cpu_mask) <0 ) { +if (sched_setaffinity (evlist__workload_pid (evlist), cpu_mask_size, cpu_mask) <0 ) {pr_debug ("sched_setaffinity: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));evlist__cancel_workload (evlist);
@@ -166,7 +166,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest* fds in the same CPU to be injected in the same mmap ring buffer * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)). */ - err =evlist__mmap (evlist, opts.mmap_pages); + err =evlist__do_mmap (evlist, opts.mmap_pages);if (err <0 ) {pr_debug ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -188,11 +188,11 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestwhile (1 ) {int before = total_events; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) {union perf_event *event;struct mmap *md; - md = &evlist->mmap[i]; + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
@@ -231,15 +231,15 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest ++errs; } -if ((pid_t )sample.pid != evlist->workload.pid) { +if ((pid_t )sample.pid !=evlist__workload_pid (evlist)) {pr_debug ("%s with unexpected pid, expected %d, got %d\n" , - name, evlist->workload.pid, sample.pid); + name,evlist__workload_pid (evlist), sample.pid); ++errs; } -if ((pid_t )sample.tid != evlist->workload.pid) { +if ((pid_t )sample.tid !=evlist__workload_pid (evlist)) {pr_debug ("%s with unexpected tid, expected %d, got %d\n" , - name, evlist->workload.pid, sample.tid); + name,evlist__workload_pid (evlist), sample.tid); ++errs; }
@@ -248,7 +248,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest type == PERF_RECORD_MMAP2 || type == PERF_RECORD_FORK || type == PERF_RECORD_EXIT) && - (pid_t)event->comm.pid != evlist->workload.pid) { + (pid_t)event->comm.pid != evlist__workload_pid(evlist)) {pr_debug ("%s with unexpected pid/tid\n" , name); ++errs; }
@@ -352,9 +352,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest } out_put_evlist:CPU_FREE (cpu_mask); -evlist__put (evlist); out:perf_sample__exit (&sample); +evlist__put (evlist);if (err == -EACCES )return TEST_SKIP ;if (err <0 || errs !=0 )
tools/perf/tests/perf-time-to-tsc.c +5 -5
@@ -99,7 +99,7 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su evlist =evlist__new ();CHECK_NOT_NULL__ (evlist); -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);CHECK__ (parse_event (evlist,"cpu-cycles:u" ));
@@ -121,9 +121,9 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su goto out_err; } -CHECK__ (evlist__mmap (evlist,UINT_MAX )); +CHECK__ (evlist__do_mmap (evlist,UINT_MAX )); - pc = evlist->mmap[0 ].core.base; + pc =evlist__mmap (evlist)[0 ].core.base; ret =perf_read_tsc_conversion (pc, &tc);if (ret) {if (ret == -EOPNOTSUPP ) {
@@ -145,8 +145,8 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int suevlist__disable (evlist); -for (i =0 ; i < evlist->core.nr_mmaps; i++) { - md = &evlist->mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
tools/perf/tests/pfm.c +4 -4
@@ -69,12 +69,12 @@ static int test__pfm_events(struct test_suite *test __maybe_unused,if (evlist ==NULL )return -ENOMEM ; - opt.value = evlist; + opt.value = &evlist;parse_libpfm_events_option (&opt, table[i].events,0 );TEST_ASSERT_EQUAL (table[i].events, -count_pfm_events (&evlist->core), +count_pfm_events (evlist__core (evlist)), table[i].nr_events); TEST_ASSERT_EQUAL(table[i].events, evlist__nr_groups(evlist),
@@ -154,12 +154,12 @@ static int test__pfm_group(struct test_suite *test __maybe_unused,if (evlist ==NULL )return -ENOMEM ; - opt.value = evlist; + opt.value = &evlist;parse_libpfm_events_option (&opt, table[i].events,0 );TEST_ASSERT_EQUAL (table[i].events, -count_pfm_events (&evlist->core), +count_pfm_events (evlist__core (evlist)), table[i].nr_events); TEST_ASSERT_EQUAL(table[i].events, evlist__nr_groups(evlist),
tools/perf/tests/pmu-events.c +3 -2
@@ -869,7 +869,7 @@ static int test__parsing_callback(const struct pmu_metric *pm,return -ENOMEM ; } -perf_evlist__set_maps (&evlist->core, cpus,NULL ); +perf_evlist__set_maps (evlist__core (evlist), cpus,NULL ); err =metricgroup__parse_groups_test (evlist, table, pm->metric_name);if (err) {
@@ -895,7 +895,8 @@ static int test__parsing_callback(const struct pmu_metric *pm, k++; }evlist__for_each_entry (evlist, evsel) { -struct metric_event *me =metricgroup__lookup (&evlist->metric_events, evsel, false); +struct metric_event *me =metricgroup__lookup (evlist__metric_events (evlist), + evsel, false);if (me !=NULL ) {struct metric_expr *mexp;
tools/perf/tests/sample-parsing.c +24 -21
112 lines not on this page · raw message
tools/perf/tests/sw-clock.c +3 -3
@@ -71,7 +71,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) goto out_put_evlist; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);if (evlist__open (evlist)) {const char *knob ="/proc/sys/kernel/perf_event_max_sample_rate" ;
@@ -83,7 +83,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) goto out_put_evlist; } - err =evlist__mmap (evlist,128 ); + err =evlist__do_mmap (evlist,128 );if (err <0 ) {pr_debug ("failed to mmap event: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -98,7 +98,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)evlist__disable (evlist); - md = &evlist->mmap[0 ]; + md = &evlist__mmap (evlist)[0 ];if (perf_mmap__read_init (&md->core) <0 ) goto out_init;
tools/perf/tests/switch-tracking.c +5 -4
@@ -237,6 +237,7 @@ static int add_event(struct evlist *evlist, struct list_head *events,if (evlist__parse_sample (evlist, event, &sample)) {pr_debug ("evlist__parse_sample failed\n" ); +perf_sample__exit (&sample);return -1 ; }
@@ -282,8 +283,8 @@ static int process_events(struct evlist *evlist,struct mmap *md;int i, ret; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { - md = &evlist->mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
@@ -374,7 +375,7 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub goto out_err; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);/* First event */ err =parse_event (evlist,"cpu-clock:u" );
@@ -471,7 +472,7 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub goto out; } - err =evlist__mmap (evlist,UINT_MAX ); + err =evlist__do_mmap (evlist,UINT_MAX );if (err) {pr_debug ("evlist__mmap failed!\n" ); goto out_err;
tools/perf/tests/task-exit.c +3 -3
@@ -77,7 +77,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _ goto out_put_evlist; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads); err =evlist__prepare_workload (evlist, &target, argv, false, workload_exec_failed_signal);if (err <0 ) {
@@ -104,7 +104,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _ goto out_put_evlist; } -if (evlist__mmap (evlist,128 ) <0 ) { +if (evlist__do_mmap (evlist,128 ) <0 ) {pr_debug ("failed to mmap events: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf))); err =-1 ;
@@ -114,7 +114,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _evlist__start_workload (evlist); retry: - md = &evlist->mmap[0 ]; + md = &evlist__mmap (evlist)[0 ];if (perf_mmap__read_init (&md->core) <0 ) goto out_init;
tools/perf/tests/time-utils-test.c +9 -5
@@ -69,16 +69,19 @@ struct test_data {static bool test__perf_time__parse_for_ranges (struct test_data *d) { -struct evlist evlist = { - .first_sample_time = d->first, - .last_sample_time = d->last, - }; -struct perf_session session = { .evlist = &evlist }; +struct evlist *evlist =evlist__new (); +struct perf_session session = { .evlist = evlist };struct perf_time_interval *ptime =NULL ;int range_size, range_num;bool pass = false;int i, err; +if (!evlist) { +pr_debug ("Missing evlist\n" ); +return false; + } +evlist__set_first_sample_time (evlist, d->first); +evlist__set_last_sample_time (evlist, d->last);pr_debug ("\nperf_time__parse_for_ranges(\"%s\")\n" , d->str);if (strchr (d->str,'%' ))
@@ -127,6 +130,7 @@ static bool test__perf_time__parse_for_ranges(struct test_data *d) pass = true; out: +evlist__put (evlist);free (ptime);return pass; }
tools/perf/tests/tool_pmu.c +3 -2
@@ -40,9 +40,10 @@ static int do_test(enum tool_pmu_event ev, bool with_pmu) } ret =TEST_OK ; -if (with_pmu ? (evlist->core.nr_entries !=1 ) : (evlist->core.nr_entries <1 )) { +if (with_pmu ? (evlist__nr_entries (evlist) !=1 ) + : (evlist__nr_entries (evlist) <1 )) {pr_debug ("FAILED %s:%d Unexpected number of events for '%s' of %d\n" , - __FILE__, __LINE__, str, evlist->core.nr_entries); + __FILE__, __LINE__, str,evlist__nr_entries (evlist)); ret =TEST_FAIL ; goto out; }
tools/perf/tests/topology.c +1 -1
@@ -46,7 +46,7 @@ static int session_write_header(char *path) session->evlist =evlist__new_default (&target,/*sample_callchains=*/ false);TEST_ASSERT_VAL ("can't get evlist" , session->evlist); - session->evlist->session = session; +evlist__set_session (session->evlist, session);perf_header__set_feat (&session->header,HEADER_CPU_TOPOLOGY );perf_header__set_feat (&session->header,HEADER_NRCPUS );
tools/perf/tests/uncore-event-sorting.c +2 -2
@@ -147,8 +147,8 @@ static int test__uncore_event_sorting(struct test_suite *test __maybe_unused, goto out_err; } -CHECK_COND (evlist->core.nr_entries >=4 ,"Number of events is >= 4" ); -CHECK_EQUAL (evlist->core.nr_entries %2 ,0 ,"Number of events is a multiple of 2" ); +CHECK_COND (evlist__nr_entries (evlist) >=4 ,"Number of events is >= 4" ); +CHECK_EQUAL (evlist__nr_entries (evlist) %2 ,0 ,"Number of events is a multiple of 2" );evlist__for_each_entry (evlist, evsel) {struct evsel *next;
tools/perf/ui/browsers/annotate.c +1 -1
@@ -597,7 +597,7 @@ static bool annotate_browser__callq(struct annotate_browser *browser, notes =symbol__annotation (dl->ops.target.sym);annotation__lock (notes); -if (!symbol__hists (dl->ops.target.sym, evsel->evlist->core.nr_entries)) { +if (!symbol__hists (dl->ops.target.sym,evlist__nr_entries (evsel->evlist))) {annotation__unlock (notes);ui__warning ("Not enough memory for annotating '%s' symbol!\n" , dl->ops.target.sym->name);
tools/perf/ui/browsers/hists.c +11 -11
@@ -688,10 +688,10 @@ static int hist_browser__handle_hotkey(struct hist_browser *browser, bool warn_lui_browser__update_nr_entries (&browser->b, nr_entries);if (warn_lost_event && - (evsel->evlist->stats.nr_lost_warned != - evsel->evlist->stats.nr_events[PERF_RECORD_LOST ])) { - evsel->evlist->stats.nr_lost_warned = - evsel->evlist->stats.nr_events[PERF_RECORD_LOST ]; + (evlist__stats (evsel->evlist)->nr_lost_warned != +evlist__stats (evsel->evlist)->nr_events[PERF_RECORD_LOST ])) { +evlist__stats (evsel->evlist)->nr_lost_warned = +evlist__stats (evsel->evlist)->nr_events[PERF_RECORD_LOST ];ui_browser__warn_lost_events (&browser->b); }
@@ -3321,7 +3321,7 @@ static int evsel__hists_browse(struct evsel *evsel, int nr_events, const char *h* No need to refresh, resort/decay histogram * entries if we are not collecting samples: */ -if (top->evlist->enabled) { +if (evlist__enabled (top->evlist)) { helpline ="Press 'f' to disable the events or 'h' to see other hotkeys" ; hbt->refresh = delay_secs; }else {
@@ -3493,7 +3493,7 @@ static void perf_evsel_menu__write(struct ui_browser *browser, unit, unit == ' ' ? "" : " ", ev_name);ui_browser__printf (browser,"%s" , bf); - nr_events = evsel->evlist->stats.nr_events[PERF_RECORD_LOST ]; + nr_events =evlist__stats (evsel->evlist)->nr_events[PERF_RECORD_LOST ];if (nr_events !=0 ) { menu->lost_events = true;if (!current_entry)
@@ -3559,13 +3559,13 @@ static int perf_evsel_menu__run(struct evsel_menu *menu,ui_browser__show_title (&menu->b, title);switch (key) {case K_TAB : -if (pos->core.node.next == &evlist->core.entries) +if (pos->core.node.next == &evlist__core (evlist)->entries) pos =evlist__first (evlist);else pos =evsel__next (pos); goto browse_hists;case K_UNTAB : -if (pos->core.node.prev == &evlist->core.entries) +if (pos->core.node.prev == &evlist__core (evlist)->entries) pos =evlist__last (evlist);else pos =evsel__prev (pos);
@@ -3618,7 +3618,7 @@ static int __evlist__tui_browse_hists(struct evlist *evlist, int nr_entries, construct evsel *pos;struct evsel_menu menu = { .b = { - .entries = &evlist->core.entries, + .entries = &evlist__core (evlist)->entries, .refresh = ui_browser__list_head_refresh, .seek = ui_browser__list_head_seek, .write = perf_evsel_menu__write,
@@ -3646,7 +3646,7 @@ static int __evlist__tui_browse_hists(struct evlist *evlist, int nr_entries, constatic bool evlist__single_entry (struct evlist *evlist) { -int nr_entries = evlist->core.nr_entries; +int nr_entries =evlist__nr_entries (evlist);if (nr_entries ==1 )return true;
@@ -3664,7 +3664,7 @@ static bool evlist__single_entry(struct evlist *evlist)int evlist__tui_browse_hists (struct evlist *evlist,const char *help,struct hist_browser_timer *hbt,float min_pcnt,struct perf_env *env,bool warn_lost_event) { -int nr_entries = evlist->core.nr_entries; +int nr_entries =evlist__nr_entries (evlist);if (evlist__single_entry (evlist)) { single_entry: {
tools/perf/util/amd-sample-raw.c +1 -1
@@ -421,7 +421,7 @@ static void parse_cpuid(struct perf_env *env)*/ bool evlist__has_amd_ibs (struct evlist *evlist) { -struct perf_env *env =perf_session__env (evlist->session); +struct perf_env *env =perf_session__env (evlist__session (evlist));int ret, nr_pmu_mappings =perf_env__nr_pmu_mappings (env);const char *pmu_mapping =perf_env__pmu_mappings (env);char name[sizeof ("ibs_fetch" )];
tools/perf/util/annotate-data.c +1 -1
@@ -1829,7 +1829,7 @@ int annotated_data_type__update_samples(struct annotated_data_type *adt,return 0 ;if (adt->histograms ==NULL ) { -int nr = evsel->evlist->core.nr_entries; +int nr =evlist__nr_entries (evsel->evlist);if (alloc_data_type_histograms (adt, nr) <0 )return -1 ;
tools/perf/util/annotate.c +5 -5
@@ -328,7 +328,7 @@ static int symbol__inc_addr_samples(struct map_symbol *ms,if (sym ==NULL )return 0 ; - src =symbol__hists (sym, sample->evsel->evlist->core.nr_entries); + src =symbol__hists (sym,evlist__nr_entries (sample->evsel->evlist));return src ?__symbol__inc_addr_samples (ms, src, addr, sample) :0 ; }
@@ -339,7 +339,7 @@ static int symbol__account_br_cntr(struct annotated_branch *branch, {unsigned int br_cntr_nr =evsel__leader (evsel)->br_cntr_nr;unsigned int base =evsel__leader (evsel)->br_cntr_idx; -unsigned int off = offset * evsel->evlist->nr_br_cntr; +unsigned int off = offset *evlist__nr_br_cntr (evsel->evlist);u64 *branch_br_cntr = branch->br_cntr;unsigned int i, mask, width;
@@ -369,7 +369,7 @@ static int symbol__account_cycles(u64 addr, u64 start, struct symbol *sym,if (sym ==NULL )return 0 ; - branch =symbol__find_branch_hist (sym, evsel->evlist->nr_br_cntr); + branch =symbol__find_branch_hist (sym,evlist__nr_br_cntr (evsel->evlist));if (!branch)return -ENOMEM ;if (addr < sym->start || addr >= sym->end)
@@ -511,7 +511,7 @@ static void annotation__count_and_fill(struct annotation *notes, u64 start, u64static int annotation__compute_ipc (struct annotation *notes,size_t size,struct evsel *evsel) { -unsigned int br_cntr_nr = evsel->evlist->nr_br_cntr; +unsigned int br_cntr_nr =evlist__nr_br_cntr (evsel->evlist);int err =0 ;s64 offset;
@@ -1813,7 +1813,7 @@ int annotation_br_cntr_abbr_list(char **str, struct evsel *evsel, bool header)struct evsel *pos;struct strbuf sb; -if (evsel->evlist->nr_br_cntr <=0 ) +if (evlist__nr_br_cntr (evsel->evlist) <=0 )return -ENOTSUP ;strbuf_init (&sb,/*hint=*/ 0 );
tools/perf/util/auxtrace.c +7 -7
@@ -191,7 +191,7 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,struct evlist *evlist, struct evsel *evsel, int idx) { - bool per_cpu = !perf_cpu_map__has_any_cpu(evlist->core.user_requested_cpus); + bool per_cpu = !perf_cpu_map__has_any_cpu(evlist__core(evlist)->user_requested_cpus); mp->mmap_needed = evsel->needs_auxtrace_mmap;
@@ -201,11 +201,11 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp, mp->idx = idx;if (per_cpu) { - mp->cpu =perf_cpu_map__cpu (evlist->core.all_cpus, idx); - mp->tid =perf_thread_map__pid (evlist->core.threads,0 ); + mp->cpu =perf_cpu_map__cpu (evlist__core (evlist)->all_cpus, idx); + mp->tid =perf_thread_map__pid (evlist__core (evlist)->threads,0 ); }else { mp->cpu.cpu =-1 ; - mp->tid =perf_thread_map__pid (evlist->core.threads, idx); + mp->tid =perf_thread_map__pid (evlist__core (evlist)->threads, idx); } }
@@ -668,10 +668,10 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,static int evlist__enable_event_idx (struct evlist *evlist,struct evsel *evsel,int idx) { -bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu (evlist->core.user_requested_cpus); +bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu (evlist__core (evlist)->user_requested_cpus);if (per_cpu_mmaps) { -struct perf_cpu evlist_cpu =perf_cpu_map__cpu (evlist->core.all_cpus, idx); +struct perf_cpu evlist_cpu =perf_cpu_map__cpu (evlist__core (evlist)->all_cpus, idx);int cpu_map_idx =perf_cpu_map__idx (evsel->core.cpus, evlist_cpu);if (cpu_map_idx ==-1 )
@@ -1838,7 +1838,7 @@ void perf_session__auxtrace_error_inc(struct perf_session *session,struct perf_record_auxtrace_error *e = &event->auxtrace_error;if (e->type <PERF_AUXTRACE_ERROR_MAX ) - session->evlist->stats.nr_auxtrace_errors[e->type] +=1 ; +evlist__stats (session->evlist)->nr_auxtrace_errors[e->type] +=1 ; }void events_stats__auxtrace_error_warn (const struct events_stats *stats)
tools/perf/util/block-info.c +2 -2
@@ -472,7 +472,7 @@ struct block_report *block_info__create_report(struct evlist *evlist, int *nr_reps) {struct block_report *block_reports; -int nr_hists = evlist->core.nr_entries, i =0 ; +int nr_hists =evlist__nr_entries (evlist), i =0 ;struct evsel *pos; block_reports =calloc (nr_hists,sizeof (struct block_report ));
@@ -483,7 +483,7 @@ struct block_report *block_info__create_report(struct evlist *evlist,struct hists *hists =evsel__hists (pos);process_block_report (hists, &block_reports[i], total_cycles, - block_hpps, nr_hpps, evlist->nr_br_cntr); + block_hpps, nr_hpps,evlist__nr_br_cntr (evlist)); i++; }
tools/perf/util/bpf_counter.c +1 -1
@@ -443,7 +443,7 @@ static int bperf_check_target(struct evsel *evsel, }else if (target->tid) { *filter_type =BPERF_FILTER_PID ; *filter_entry_cnt =perf_thread_map__nr (evsel->core.threads); - }else if (target->pid || evsel->evlist->workload.pid !=-1 ) { + }else if (target->pid ||evlist__workload_pid (evsel->evlist) !=-1 ) { *filter_type =BPERF_FILTER_TGID ; *filter_entry_cnt =perf_thread_map__nr (evsel->core.threads); }else {
tools/perf/util/bpf_counter_cgroup.c +6 -6
@@ -104,7 +104,7 @@ static int bperf_load_program(struct evlist *evlist)set_max_rlimit (); -if (nr_cgroups ==0 || evlist->core.nr_entries % nr_cgroups !=0 ) { +if (nr_cgroups ==0 ||evlist__nr_entries (evlist) % nr_cgroups !=0 ) {pr_err ("Invalid cgroup or event count\n" );return -EINVAL ; }
@@ -116,7 +116,7 @@ static int bperf_load_program(struct evlist *evlist)pr_err ("Failed to open cgroup skeleton\n" );return -1 ; } -setup_rodata (skel, evlist->core.nr_entries); +setup_rodata (skel,evlist__nr_entries (evlist)); err =bperf_cgroup_bpf__load (skel);if (err) {
@@ -127,12 +127,12 @@ static int bperf_load_program(struct evlist *evlist) err =-1 ; cgrp_switch =evsel__new (&cgrp_switch_attr); -if (evsel__open_per_cpu (cgrp_switch, evlist->core.all_cpus,-1 ) <0 ) { +if (evsel__open_per_cpu (cgrp_switch,evlist__core (evlist)->all_cpus,-1 ) <0 ) {pr_err ("Failed to open cgroup switches event\n" ); goto out; } -perf_cpu_map__for_each_cpu (cpu, i, evlist->core.all_cpus) { +perf_cpu_map__for_each_cpu (cpu, i,evlist__core (evlist)->all_cpus) { link =bpf_program__attach_perf_event (skel->progs.on_cgrp_switch,FD (cgrp_switch, i));if (IS_ERR (link)) {
@@ -197,7 +197,7 @@ static int bperf_load_program(struct evlist *evlist)*/ {struct evsel *leader; -int num_events = evlist->core.nr_entries / nr_cgroups; +int num_events =evlist__nr_entries (evlist) / nr_cgroups;evlist__for_each_entry (evlist, evsel) { leader =evlist__find_evsel (evlist, evsel->core.idx % num_events);
@@ -258,7 +258,7 @@ static int bperf_cgrp__sync_counters(struct evlist *evlist)unsigned int idx;int prog_fd =bpf_program__fd (skel->progs.trigger_read); -perf_cpu_map__for_each_cpu (cpu, idx, evlist->core.all_cpus) +perf_cpu_map__for_each_cpu (cpu, idx,evlist__core (evlist)->all_cpus)bperf_trigger_reading (prog_fd, cpu.cpu);return 0 ;
tools/perf/util/bpf_ftrace.c +5 -4
@@ -59,13 +59,13 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace)/* don't need to set cpu filter for system-wide mode */ if (ftrace->target.cpu_list) { - ncpus =perf_cpu_map__nr (ftrace->evlist->core.user_requested_cpus); + ncpus =perf_cpu_map__nr (evlist__core (ftrace->evlist)->user_requested_cpus);bpf_map__set_max_entries (skel->maps.cpu_filter, ncpus); skel->rodata->has_cpu =1 ; }if (target__has_task (&ftrace->target) ||target__none (&ftrace->target)) { - ntasks =perf_thread_map__nr (ftrace->evlist->core.threads); + ntasks =perf_thread_map__nr (evlist__core (ftrace->evlist)->threads);bpf_map__set_max_entries (skel->maps.task_filter, ntasks); skel->rodata->has_task =1 ; }
@@ -87,7 +87,8 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace) fd =bpf_map__fd (skel->maps.cpu_filter);for (i =0 ; i < ncpus; i++) { - cpu =perf_cpu_map__cpu (ftrace->evlist->core.user_requested_cpus, i).cpu; + cpu =perf_cpu_map__cpu ( +evlist__core (ftrace->evlist)->user_requested_cpus, i).cpu;bpf_map_update_elem (fd, &cpu, &val,BPF_ANY ); } }
@@ -99,7 +100,7 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace) fd =bpf_map__fd (skel->maps.task_filter);for (i =0 ; i < ntasks; i++) { - pid =perf_thread_map__pid (ftrace->evlist->core.threads, i); + pid =perf_thread_map__pid (evlist__core (ftrace->evlist)->threads, i);bpf_map_update_elem (fd, &pid, &val,BPF_ANY ); } }
tools/perf/util/bpf_lock_contention.c +6 -6
@@ -223,11 +223,11 @@ int lock_contention_prepare(struct lock_contention *con)if (target__has_cpu (target)) { skel->rodata->has_cpu =1 ; - ncpus =perf_cpu_map__nr (evlist->core.user_requested_cpus); + ncpus =perf_cpu_map__nr (evlist__core (evlist)->user_requested_cpus); }if (target__has_task (target)) { skel->rodata->has_task =1 ; - ntasks =perf_thread_map__nr (evlist->core.threads); + ntasks =perf_thread_map__nr (evlist__core (evlist)->threads); }if (con->filters->nr_types) { skel->rodata->has_type =1 ;
@@ -334,7 +334,7 @@ int lock_contention_prepare(struct lock_contention *con) fd =bpf_map__fd (skel->maps.cpu_filter);for (i =0 ; i < ncpus; i++) { - cpu =perf_cpu_map__cpu (evlist->core.user_requested_cpus, i).cpu; + cpu =perf_cpu_map__cpu (evlist__core (evlist)->user_requested_cpus, i).cpu;bpf_map_update_elem (fd, &cpu, &val,BPF_ANY ); } }
@@ -346,13 +346,13 @@ int lock_contention_prepare(struct lock_contention *con) fd =bpf_map__fd (skel->maps.task_filter);for (i =0 ; i < ntasks; i++) { - pid =perf_thread_map__pid (evlist->core.threads, i); + pid =perf_thread_map__pid (evlist__core (evlist)->threads, i);bpf_map_update_elem (fd, &pid, &val,BPF_ANY ); } } -if (target__none (target) && evlist->workload.pid >0 ) { -u32 pid = evlist->workload.pid; +if (target__none (target) &&evlist__workload_pid (evlist) >0 ) { +u32 pid =evlist__workload_pid (evlist);u8 val =1 ; fd =bpf_map__fd (skel->maps.task_filter);
tools/perf/util/bpf_off_cpu.c +7 -7
@@ -73,13 +73,13 @@ static void off_cpu_start(void *arg)/* update task filter for the given workload */ if (skel->rodata->has_task && skel->rodata->uses_tgid && -perf_thread_map__pid (evlist->core.threads,0 ) !=-1 ) { +perf_thread_map__pid (evlist__core (evlist)->threads,0 ) !=-1 ) {int fd;u32 pid;u8 val =1 ; fd =bpf_map__fd (skel->maps.task_filter); - pid =perf_thread_map__pid (evlist->core.threads,0 ); + pid =perf_thread_map__pid (evlist__core (evlist)->threads,0 );bpf_map_update_elem (fd, &pid, &val,BPF_ANY ); }
@@ -168,7 +168,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target,/* don't need to set cpu filter for system-wide mode */ if (target->cpu_list) { - ncpus =perf_cpu_map__nr (evlist->core.user_requested_cpus); + ncpus =perf_cpu_map__nr (evlist__core (evlist)->user_requested_cpus);bpf_map__set_max_entries (skel->maps.cpu_filter, ncpus); skel->rodata->has_cpu =1 ; }
@@ -199,7 +199,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, skel->rodata->has_task =1 ; skel->rodata->uses_tgid =1 ; }else if (target__has_task (target)) { - ntasks =perf_thread_map__nr (evlist->core.threads); + ntasks =perf_thread_map__nr (evlist__core (evlist)->threads);bpf_map__set_max_entries (skel->maps.task_filter, ntasks); skel->rodata->has_task =1 ; }else if (target__none (target)) {
@@ -209,7 +209,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, }if (evlist__first (evlist)->cgrp) { - ncgrps = evlist->core.nr_entries -1 ;/* excluding a dummy */ + ncgrps =evlist__nr_entries (evlist) -1 ;/* excluding a dummy */ bpf_map__set_max_entries (skel->maps.cgroup_filter, ncgrps);if (!cgroup_is_v2 ("perf_event" ))
@@ -240,7 +240,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, fd =bpf_map__fd (skel->maps.cpu_filter);for (i =0 ; i < ncpus; i++) { - cpu =perf_cpu_map__cpu (evlist->core.user_requested_cpus, i).cpu; + cpu =perf_cpu_map__cpu (evlist__core (evlist)->user_requested_cpus, i).cpu;bpf_map_update_elem (fd, &cpu, &val,BPF_ANY ); } }
@@ -269,7 +269,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, fd =bpf_map__fd (skel->maps.task_filter);for (i =0 ; i < ntasks; i++) { - pid =perf_thread_map__pid (evlist->core.threads, i); + pid =perf_thread_map__pid (evlist__core (evlist)->threads, i);bpf_map_update_elem (fd, &pid, &val,BPF_ANY ); } }
tools/perf/util/cgroup.c +10 -10
@@ -367,7 +367,7 @@ int parse_cgroups(const struct option *opt, const char *str,char *s;int ret, i; -if (list_empty (&evlist->core.entries)) { +if (list_empty (&evlist__core (evlist)->entries)) {fprintf (stderr,"must define events before cgroups\n" );return -1 ; }
@@ -423,7 +423,7 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgroint ret =-1 ;int prefix_len; -if (evlist->core.nr_entries ==0 ) { +if (evlist__nr_entries (evlist) ==0 ) {fprintf (stderr,"must define events before cgroups\n" );return -EINVAL ; }
@@ -436,11 +436,11 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgro }/* save original events and init evlist */ -evlist__splice_list_tail (orig_list, &evlist->core.entries); - evlist->core.nr_entries =0 ; +evlist__splice_list_tail (orig_list, &evlist__core (evlist)->entries); +evlist__core (evlist)->nr_entries =0 ; - orig_metric_events = evlist->metric_events; -metricgroup__rblist_init (&evlist->metric_events); + orig_metric_events = *evlist__metric_events (evlist); +metricgroup__rblist_init (evlist__metric_events (evlist));if (has_pattern_string (str)) prefix_len =match_cgroups (str);
@@ -503,15 +503,15 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgro nr_cgroups++;if (metricgroup__copy_metric_events (tmp_list, cgrp, - &evlist->metric_events, +evlist__metric_events (evlist), &orig_metric_events) <0 ) goto out_err; -evlist__splice_list_tail (evlist, &tmp_list->core.entries); - tmp_list->core.nr_entries =0 ; +evlist__splice_list_tail (evlist, &evlist__core (tmp_list)->entries); +evlist__core (tmp_list)->nr_entries =0 ; } -if (list_empty (&evlist->core.entries)) { +if (list_empty (&evlist__core (evlist)->entries)) {fprintf (stderr,"no cgroup matched: %s\n" , str); goto out_err; }
tools/perf/util/cs-etm.c +4 -1
@@ -1672,8 +1672,9 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq, {int ret =0 ;struct cs_etm_auxtrace *etm = etmq->etm; -struct perf_sample sample = {.ip =0 ,}; +struct perf_sample sample;union perf_event *event = tidq->event_buf; +struct dummy_branch_stack {u64 nr;u64 hw_idx;
@@ -1681,6 +1682,7 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq, } dummy_bs;u64 ip; +perf_sample__init (&sample,/*all=*/ true); ip =cs_etm__last_executed_instr (tidq->prev_packet); event->sample.header.type =PERF_RECORD_SAMPLE ;
@@ -1733,6 +1735,7 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,"CS ETM Trace: failed to deliver instruction event, error %d\n" , ret); +perf_sample__exit (&sample);return ret; }
tools/perf/util/evlist.c +233 -162
172 lines not on this page · raw message
tools/perf/util/evlist.h +235 -16
251 lines not on this page · raw message
tools/perf/util/header.c +24 -23
47 lines not on this page · raw message
tools/perf/util/python.c +64 -26
90 lines not on this page · raw message
tools/perf/util/session.c +21 -22
43 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Part of the diff is not shown here. View the raw message.
[email protected]
AI reviewRe: [PATCH v20 07/21] perf evlist: Add reference count checking
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Lockless cycle-breaker algorithm in `evlist__put` is vulnerable to a race condition that can…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Lockless cycle-breaker algorithm in evlist__put is vulnerable to a race condition that can result in a double free of the evlist.
- [High] Circular reference between evlist and evsel causes a permanent memory leak if an evsel outlives its parent evlist. --
commit bf56f6ea798f2473fd24a71487153a7749cf533e
Author: Ian Rogers <[email protected]>
perf evlist: Add reference count checking
Now the evlist is reference counted, add reference count checking so that gets and puts are paired and easy to debug. This large patch is adding accessors to evlist functions and switching to their use.
Quoted 19 lines
tools/perf/util/evlist.c +6
-2@@ -211,26 +222,46 @@ static void evlist__exit(struct evlist *evlist)void evlist__put (struct evlist *evlist) { +struct evsel *evsel; +unsigned int count, ref_cnt; +if (evlist ==NULL )return ; -if (!refcount_dec_and_test (&evlist->refcnt)) -return ; +if (refcount_dec_and_test (evlist__refcnt (evlist))) + goto out_delete; +
tools/perf/util/evlist.c static void evlist__exit(struct evlist *evlist) · line 243
Quoted 10 lines
tools/perf/util/evlist.c +10
-0+retry: + count =refcount_read (evlist__refcnt (evlist)); + ref_cnt = count; +evlist__for_each_entry (evlist, evsel) { +if (RC_CHK_EQUAL (evsel->evlist, evlist) && count && +refcount_read (&evsel->refcnt) ==1 ) + count--; + } +if (refcount_read (evlist__refcnt (evlist)) != ref_cnt) + goto retry;
[Severity: Critical]
Can the lockless cycle-breaker algorithm in evlist__put() lead to a double free here?
If two threads call evlist__put() at the same time and neither hits 0 in refcount_dec_and_test(), they could both reach the retry loop. They could both read the same ref_cnt from refcount_read(), evaluate count to 0, pass the validation check, and simultaneously proceed to out_delete.
tools/perf/util/evlist.c static void evlist__purge(struct evlist *evlist) · line 256
Quoted 9 lines
tools/perf/util/evlist.c +8
-0+if (count !=0 ) { +/* +* Not the last reference except for back references from +* evsels. +*/ +RC_CHK_PUT (evlist); +return ; + }
[Severity: High]
Does this early return prevent the cycle from ever being broken if an external user holds a reference to an evsel?
Since evlist__add() takes a reference on the evlist below, this introduces a circular reference.
If an external user holds an extra reference to an evsel (so its refcnt is >= 2), the loop above won't decrement count for it, and we return early here.
Later, when the external user drops the evsel reference via evsel__put(), the evsel's refcnt drops to 1. But evsel__put() doesn't have a reciprocal cycle-breaking mechanism like this one. Will this leave both the evlist and the evsel leaked permanently?
Quoted 18 lines
tools/perf/util/evlist.c +6
-4+out_delete:evlist__free_stats (evlist); -evlist__munmap (evlist); +evlist__do_munmap (evlist);evlist__close (evlist);evlist__purge (evlist);evlist__exit (evlist); -free (evlist); +RC_CHK_FREE (evlist); }void evlist__add (struct evlist *evlist,struct evsel *entry) { -perf_evlist__add (&evlist->core, &entry->core); - entry->evlist = evlist; +perf_evlist__add (evlist__core (evlist), &entry->core); +evlist__put (entry->evlist); + entry->evlist =evlist__get (evlist);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerPATCH v20 8/21 perf python: Use evsel in sample in pyrf_event
In reply to Ian RogersAvoid a duplicated evsel by using the one in sample. Add evsel__get/put to the evsel in perf_sample.
--- tools/perf/builtin-inject.c | 5 ++++- tools/perf/builtin-trace.c | 5 ++++- tools/perf/util/evsel.c | 2 +- tools/perf/util/intel-pt.c | 8 +++++--- tools/perf/util/python.c | 10 +++------- tools/perf/util/s390-sample-raw.c | 1 + tools/perf/util/sample.c | 17 ++++++++++++----- tools/perf/util/session.c | 19 ++++++++++++++++--- 8 files changed, 46 insertions(+), 21 deletions(-)
tools/perf/builtin-inject.c +4 -1
@@ -702,8 +702,11 @@ static int perf_event__repipe_common_mmap(const struct perf_tool *tool, }if (dso && !dso__hit (dso)) { -if (!sample->evsel) +if (!sample->evsel) { sample->evsel =evlist__event2evsel (inject->session->evlist, event); +if (sample->evsel) +evsel__get (sample->evsel); + }if (sample->evsel) {dso__set_hit (dso);
tools/perf/builtin-trace.c +4 -1
@@ -3657,8 +3657,11 @@ static void trace__handle_event(struct trace *trace, union perf_event *event, streturn ; } -if (sample->evsel ==NULL ) +if (sample->evsel ==NULL ) { sample->evsel =evlist__id2evsel (trace->evlist, sample->id); +if (sample->evsel) +evsel__get (sample->evsel); + }if (sample->evsel ==NULL ) {fprintf (trace->output,"Unknown tp ID %" PRIu64", skipping...\n" , sample->id);
tools/perf/util/evsel.c +1 -1
@@ -3365,7 +3365,7 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event,union u64_swap u;perf_sample__init (data,/*all=*/ true); - data->evsel = evsel; + data->evsel =evsel__get (evsel); data->cpu = data->pid = data->tid =-1 ; data->stream_id = data->id = data->time =-1ULL ; data->period = evsel->core.attr.sample_period;
tools/perf/util/intel-pt.c +5 -3
@@ -1830,9 +1830,9 @@ static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq) ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt; } -perf_sample__exit (&sample); ret =intel_pt_deliver_synth_event (pt, event, &sample, pt->branches_sample_type); +perf_sample__exit (&sample);return ret; }
@@ -1967,6 +1967,7 @@ static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)union perf_event *event = ptq->event_buf;struct perf_sample sample = { .ip =0 , };struct perf_synth_intel_ptwrite raw; +int ret;if (intel_pt_skip_event (pt))return 0 ;
@@ -1983,8 +1984,9 @@ static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq) sample.raw_size =perf_synth__raw_size (raw); sample.raw_data =perf_synth__raw_data (&raw); -return intel_pt_deliver_synth_event (pt, event, &sample, - pt->ptwrites_sample_type); + ret =intel_pt_deliver_synth_event (pt, event, &sample, pt->ptwrites_sample_type); +perf_sample__exit (&sample); +return ret; }static int intel_pt_synth_cbr_sample (struct intel_pt_queue *ptq)
tools/perf/util/python.c +3 -7
@@ -63,7 +63,6 @@ PyMODINIT_FUNC PyInit_perf(void);struct pyrf_event {PyObject_HEAD - struct evsel *evsel; struct perf_sample sample;union perf_event event; };
@@ -294,7 +293,6 @@ static PyMemberDef pyrf_sample_event__members[] = {static void pyrf_sample_event__delete (struct pyrf_event *pevent) { -evsel__put (pevent->evsel);perf_sample__exit (&pevent->sample);Py_TYPE (pevent)->tp_free ((PyObject *)pevent); }
@@ -316,7 +314,7 @@ static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent)#ifdef HAVE_LIBTRACEEVENT static bool is_tracepoint (const struct pyrf_event *pevent) { -return pevent->evsel->core.attr.type ==PERF_TYPE_TRACEPOINT ; +return pevent->sample.evsel->core.attr.type ==PERF_TYPE_TRACEPOINT ; } static PyObject*
@@ -363,7 +361,7 @@ tracepoint_field(const struct pyrf_event *pe, struct tep_format_field *field)static PyObject *get_tracepoint_field (struct pyrf_event *pevent,PyObject *attr_name) { -struct evsel *evsel = pevent->evsel; +struct evsel *evsel = pevent->sample.evsel;struct tep_event *tp_format =evsel__tp_format (evsel);struct tep_format_field *field;
@@ -529,7 +527,7 @@ static PyObject *pyrf_event__new(const union perf_event *event) pevent = PyObject_New(struct pyrf_event, ptype);if (pevent !=NULL ) {memcpy (&pevent->event, event, event->header.size); - pevent->evsel =NULL ; +perf_sample__init (&pevent->sample,/*all=*/ false); }return (PyObject *)pevent; }
@@ -1907,8 +1905,6 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,return Py_None; } - pevent->evsel =evsel__get (evsel); -perf_mmap__consume (&md->core); err =evsel__parse_sample (evsel, &pevent->event, &pevent->sample);
tools/perf/util/s390-sample-raw.c +1 -0
@@ -343,6 +343,7 @@ void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event, sample->evsel =evlist__event2evsel (evlist, event);if (!sample->evsel)return ; +evsel__get (sample->evsel); }/* Check for raw data in sample */
tools/perf/util/sample.c +12 -5
@@ -1,18 +1,23 @@/* SPDX-License-Identifier: GPL-2.0 */ #include "sample.h" + +#include <stdlib.h> +#include <string.h> + +#include <elf.h> +#include <linux/zalloc.h> + +#include "../../arch/x86/include/asm/insn.h" #include "debug.h" +#include "evsel.h" #include "thread.h" -#include <elf.h> +#ifndef EM_CSKY #define EM_CSKY 252#endif #ifndef EM_LOONGARCH #define EM_LOONGARCH 258#endif -#include <linux/zalloc.h> -#include <stdlib.h> -#include <string.h> -#include "../../arch/x86/include/asm/insn.h" void perf_sample__init (struct perf_sample *sample,bool all) {
@@ -29,6 +34,8 @@ void perf_sample__init(struct perf_sample *sample, bool all)void perf_sample__exit (struct perf_sample *sample) { +evsel__put (sample->evsel); + sample->evsel =NULL ;zfree (&sample->user_regs);zfree (&sample->intr_regs);if (sample->merged_callchain) {
tools/perf/util/session.c +16 -3
@@ -1854,6 +1854,7 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,list_for_each_entry_safe (de, tmp,evlist__deferred_samples (evlist), list) {struct perf_sample orig_sample; +struct evsel *new_evsel;perf_sample__init (&orig_sample,/*all=*/ false); ret =evlist__parse_sample (evlist, de->event, &orig_sample);
@@ -1874,7 +1875,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,else orig_sample.deferred_callchain = false; - orig_sample.evsel =evlist__id2evsel (evlist, orig_sample.id); + new_evsel =evlist__id2evsel (evlist, orig_sample.id); +if (new_evsel != orig_sample.evsel) { +evsel__put (orig_sample.evsel); + orig_sample.evsel =evsel__get (new_evsel); + } ret =evlist__deliver_sample (evlist, tool, de->event, &orig_sample, machine);
@@ -1903,6 +1908,7 @@ static int session__flush_deferred_samples(struct perf_session *session,list_for_each_entry_safe (de, tmp,evlist__deferred_samples (evlist), list) {struct perf_sample sample; +struct evsel *new_evsel;perf_sample__init (&sample,/*all=*/ false); ret =evlist__parse_sample (evlist, de->event, &sample);
@@ -1913,7 +1919,11 @@ static int session__flush_deferred_samples(struct perf_session *session, } sample.file_offset = de->file_offset; - sample.evsel =evlist__id2evsel (evlist, sample.id); + new_evsel =evlist__id2evsel (evlist, sample.id); +if (new_evsel != sample.evsel) { +evsel__put (sample.evsel); + sample.evsel =evsel__get (new_evsel); + } ret =evlist__deliver_sample (evlist, tool, de->event, &sample, machine);
@@ -1959,8 +1969,11 @@ static int machines__deliver_event(struct machines *machines,dump_event (evlist, event, file_offset, sample, file_path); -if (!sample->evsel) +if (!sample->evsel) { sample->evsel =evlist__id2evsel (evlist, sample->id); +if (sample->evsel) + sample->evsel =evsel__get (sample->evsel); + }else assert (sample->evsel ==evlist__id2evsel (evlist, sample->id)); machine =machines__find_for_cpumode (machines, event, sample);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
[email protected]
AI reviewRe: [PATCH v20 08/21] perf python: Use evsel in sample in pyrf_event
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Unchecked NULL pointer passed to evsel__get() causes a segmentation fault on malformed…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Unchecked NULL pointer passed to evsel__get() causes a segmentation fault on malformed perf.data.
- [Medium] Memory leak of evsel reference and dynamic perf_sample fields on the error path in test__perf_time_to_tsc. --
commit 84354b1d17171465e121f0b606e0dbe0206da819
Author: Ian Rogers <[email protected]>
perf python: Use evsel in sample in pyrf_event
Avoid a duplicated evsel by using the one in sample. Add evsel__get/put to the evsel in perf_sample.
tools/perf/util/evsel.c int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, · line 3368
Quoted 10 lines
tools/perf/util/evsel.c +1
-1@@ -3365,7 +3365,7 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,union u64_swap u;perf_sample__init (data,/*all=*/ true); - data->evsel = evsel; + data->evsel =evsel__get (evsel);
[Severity: Medium]
Does acquiring the reference this early introduce a leak on error paths?
If evsel__parse_sample() fails later in this function (e.g., when jumping to out_efault), the caller receives an error code but the evsel reference has already been taken.
For example, in tools/perf/tests/perf-time-to-tsc.c:test__perf_time_to_tsc():
CHECK__(evsel__parse_sample(evsel, event, &sample));
...
out_err:
evlist__put(evlist);If parsing fails, it exits without calling perf_sample__exit(). Could this leave a valid evsel reference inside the sample and leak it?
Quoted 3 lines
tools/perf/util/evsel.c +0
-0data->cpu = data->pid = data->tid =-1 ; data->stream_id = data->id = data->time =-1ULL ; data->period = evsel->core.attr.sample_period;
[ ... ]
Quoted 13 lines
tools/perf/util/session.c +1
-0@@ -1854,6 +1854,7 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,list_for_each_entry_safe (de, tmp,evlist__deferred_samples (evlist), list) {struct perf_sample orig_sample; +struct evsel *new_evsel;perf_sample__init (&orig_sample,/*all=*/ false); ret =evlist__parse_sample (evlist, de->event, &orig_sample);
tools/perf/util/session.c static int evlist__deliver_deferred_callchain(struct evlist *evlist, · line 1881
Quoted 8 lines
tools/perf/util/session.c +4
-1@@ -1874,7 +1875,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,else orig_sample.deferred_callchain = false; - orig_sample.evsel =evlist__id2evsel (evlist, orig_sample.id); + new_evsel =evlist__id2evsel (evlist, orig_sample.id); +if (new_evsel != orig_sample.evsel) { +evsel__put (orig_sample.evsel); + orig_sample.evsel =evsel__get (new_evsel);
[Severity: Medium]
Can new_evsel be NULL here?
If a perf.data file is crafted with a malformed sample ID, evlist__id2evsel() will return NULL. Will passing this NULL pointer to evsel__get() cause a segmentation fault when it attempts to increment the reference count?
Quoted 13 lines
tools/perf/util/session.c +2
-0+ } ret =evlist__deliver_sample (evlist, tool, de->event, &orig_sample, machine);@@ -1903,6 +1908,7 @@ static int session__flush_deferred_samples(struct perf_session *session,list_for_each_entry_safe (de, tmp,evlist__deferred_samples (evlist), list) {struct perf_sample sample; +struct evsel *new_evsel;perf_sample__init (&sample,/*all=*/ false); ret =evlist__parse_sample (evlist, de->event, &sample);
tools/perf/util/session.c static int session__flush_deferred_samples(struct perf_session *session, · line 1925
Quoted 8 lines
tools/perf/util/session.c +4
-1@@ -1913,7 +1919,11 @@ static int session__flush_deferred_samples(struct perf_session *session, } sample.file_offset = de->file_offset; - sample.evsel =evlist__id2evsel (evlist, sample.id); + new_evsel =evlist__id2evsel (evlist, sample.id); +if (new_evsel != sample.evsel) { +evsel__put (sample.evsel); + sample.evsel =evsel__get (new_evsel);
[Severity: Medium]
Does this have the same NULL pointer vulnerability as the previous location?
If evlist__id2evsel() returns NULL for an invalid sample ID, could calling evsel__get() without a NULL check crash the perf tool?
Quoted 4 lines
tools/perf/util/session.c +1
-0+ } ret =evlist__deliver_sample (evlist, tool, de->event, &sample, machine);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerPATCH v20 9/21 perf python: Add wrapper for perf_data file abstraction
In reply to Ian RogersThe perf_data struct is needed for session support.
--- tools/perf/util/python.c | 123 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-)
tools/perf/util/python.c +122 -1
@@ -6,6 +6,7 @@#include <linux/err.h> #include <poll.h> +#include <unistd.h> #include <internal/lib.h> #include <perf/cpumap.h>
@@ -14,6 +15,7 @@#include "callchain.h" #include "counts.h" +#include "data.h" #include "event.h" #include "evlist.h" #include "evsel.h"
@@ -2462,6 +2464,121 @@ static PyObject *pyrf__metrics(PyObject *self, PyObject *args)return list; } +struct pyrf_data { +PyObject_HEAD + + struct perf_data data; +}; + +static int pyrf_data__init (struct pyrf_data *pdata,PyObject *args,PyObject *kwargs) +{ +static char *kwlist[] = {"path" ,"fd" ,NULL }; +char *path =NULL ; +int fd =-1 ; + +if (!PyArg_ParseTupleAndKeywords (args, kwargs,"|si" , kwlist, &path, &fd)) +return -1 ; + +if (pdata->data.open) +perf_data__close (&pdata->data); +free ((char *)pdata->data.path); + pdata->data.path =NULL ; + +if (fd !=-1 ) { +struct stat st; + +if (fstat (fd, &st) <0 || !S_ISFIFO (st.st_mode)) { +PyErr_SetString (PyExc_ValueError, +"fd argument is only supported for pipes" ); +return -1 ; + } +if (!path) + path ="-" ; +else if (strcmp (path,"-" ) !=0 ) { +PyErr_SetString (PyExc_ValueError, +"path must be '-' when fd is provided" ); +return -1 ; + } + fd =dup (fd); +if (fd <0 ) { +PyErr_SetFromErrno (PyExc_OSError); +return -1 ; + } + }else if (path &&strcmp (path,"-" ) ==0 ) { + fd =dup (0 ); +if (fd <0 ) { +PyErr_SetFromErrno (PyExc_OSError); +return -1 ; + } + } + +if (!path) + path ="perf.data" ; + + pdata->data.path =strdup (path); +if (!pdata->data.path) { +if (fd !=-1 ) +close (fd); +PyErr_NoMemory (); +return -1 ; + } + + pdata->data.mode =PERF_DATA_MODE_READ ; + pdata->data.file.fd = fd; +if (perf_data__open (&pdata->data) <0 ) { +PyErr_Format (PyExc_IOError,"Failed to open perf data: %s" , + pdata->data.path ? pdata->data.path :"perf.data" ); +return -1 ; + } +return 0 ; +} + +static void pyrf_data__delete (struct pyrf_data *pdata) +{ +perf_data__close (&pdata->data); +free ((char *)pdata->data.path); +Py_TYPE (pdata)->tp_free ((PyObject *)pdata); +} + +static PyObject *pyrf_data__str (PyObject *self) +{ +const struct pyrf_data *pdata = (const struct pyrf_data *)self; + +if (!pdata->data.path) +return PyUnicode_FromString ("[uninitialized]" ); +return PyUnicode_FromString (pdata->data.path); +} + +static PyObject *pyrf_data__new (PyTypeObject *type,PyObject *args,PyObject *kwargs) +{ +struct pyrf_data *pdata; + + pdata = (struct pyrf_data *)PyType_GenericNew (type, args, kwargs); +if (pdata) +memset (&pdata->data,0 ,sizeof (pdata->data)); +return (PyObject *)pdata; +} + +static const char pyrf_data__doc[] =PyDoc_STR ("perf data file object." ); + +static PyTypeObject pyrf_data__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.data" , + .tp_basicsize =sizeof (struct pyrf_data ), + .tp_dealloc = (destructor )pyrf_data__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = pyrf_data__doc, + .tp_init = (initproc )pyrf_data__init, + .tp_repr = pyrf_data__str, + .tp_str = pyrf_data__str, +}; + +static int pyrf_data__setup_types (void ) +{ + pyrf_data__type.tp_new = pyrf_data__new; +return PyType_Ready (&pyrf_data__type); +} +static PyMethodDef perf__methods[] = { { .ml_name ="metrics" ,
@@ -2524,7 +2641,8 @@ PyMODINIT_FUNC PyInit_perf(void)pyrf_cpu_map__setup_types () <0 ||pyrf_pmu_iterator__setup_types () <0 ||pyrf_pmu__setup_types () <0 || - pyrf_counts_values__setup_types() < 0) +pyrf_counts_values__setup_types () <0 || + pyrf_data__setup_types() < 0) return module;/* The page_size is placed in util object. */
@@ -2572,6 +2690,9 @@ PyMODINIT_FUNC PyInit_perf(void)Py_INCREF (&pyrf_counts_values__type);PyModule_AddObject (module,"counts_values" , (PyObject *)&pyrf_counts_values__type); +Py_INCREF (&pyrf_data__type); +PyModule_AddObject (module,"data" , (PyObject *)&pyrf_data__type); + dict =PyModule_GetDict (module);if (dict ==NULL ) goto error;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v20 10/21 perf python: Add python session abstraction wrapping perf's session
In reply to Ian RogersSessions are necessary to be able to use perf.data files within a tool. Add a wrapper python type that incorporates the tool. Allow a sample callback to be passed when creating the session. When process_events is run this callback will be called, if supplied, for sample events.
An example use looks like:
```
$ perf record -e cycles,instructions -a sleep 3
$ PYTHONPATH=..../perf/python python3
Python 3.13.7 (main, Aug 20 2025, 22:17:40) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import perf
>>> count=0
... def handle_sample(x):
... global count
... if count < 3:
... print(dir(x))
... count = count + 1
... perf.session(perf.data("perf.data"),sample=handle_sample).process_events()
...
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'sample_addr', 'sample_cpu', 'sample_id', 'sample_ip', 'sample_period', 'sample_pid', 'sample_stream_id', 'sample_tid', 'sample_time', 'type']
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'sample_addr', 'sample_cpu', 'sample_id', 'sample_ip', 'sample_period', 'sample_pid', 'sample_stream_id', 'sample_tid', 'sample_time', 'type']
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'sample_addr', 'sample_cpu', 'sample_id', 'sample_ip', 'sample_period', 'sample_pid', 'sample_stream_id', 'sample_tid', 'sample_time', 'type']
```Also, add the ability to get the thread associated with a session. For threads, allow the comm string to be retrieved. This can be useful for filtering threads. Connect up some of the standard event handling in psession->tool to better support queries of the machine. Also connect up the symbols.
--- tools/perf/util/python.c | 357 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 351 insertions(+), 6 deletions(-)
tools/perf/util/python.c +351 -6
@@ -14,8 +14,10 @@#include <structmember.h> #include "callchain.h" +#include "comm.h" #include "counts.h" #include "data.h" +#include "debug.h" #include "event.h" #include "evlist.h" #include "evsel.h"
@@ -25,8 +27,14 @@#include "pmus.h" #include "print_binary.h" #include "record.h" +#include "session.h" #include "strbuf.h" +#include "symbol.h" +#include "stat.h" +#include "header.h" +#include "thread.h" #include "thread_map.h" +#include "tool.h" #include "tp_pmu.h" #include "trace-event.h" #include "util/sample.h"
@@ -2579,6 +2587,332 @@ static int pyrf_data__setup_types(void)return PyType_Ready (&pyrf_data__type); } +struct pyrf_thread { +PyObject_HEAD + + struct thread *thread; +}; + +static void pyrf_thread__delete (struct pyrf_thread *pthread) +{ +thread__put (pthread->thread); +Py_TYPE (pthread)->tp_free ((PyObject *)pthread); +} + +static PyObject *pyrf_thread__comm (PyObject *obj) +{ +struct pyrf_thread *pthread = (void *)obj; +const char *str; + +CHECK_INITIALIZED (pthread->thread,"perf.thread" ); + + str =thread__comm_str (pthread->thread); + +if (!str) + Py_RETURN_NONE; + +return PyUnicode_FromString (str); +} + +static PyMethodDef pyrf_thread__methods[] = { + { + .ml_name ="comm" , + .ml_meth = (PyCFunction )pyrf_thread__comm, + .ml_flags =METH_NOARGS , + .ml_doc =PyDoc_STR ("Comm(and) associated with this thread." ) + }, + { .ml_name =NULL , } +}; + +static PyObject *pyrf_thread__get_pid (struct pyrf_thread *pthread,void *closure__maybe_unused ) +{ +return PyLong_FromLong (thread__pid (pthread->thread)); +} + +static PyObject *pyrf_thread__get_tid (struct pyrf_thread *pthread,void *closure__maybe_unused ) +{ +return PyLong_FromLong (thread__tid (pthread->thread)); +} + +static PyObject *pyrf_thread__get_ppid (struct pyrf_thread *pthread,void *closure__maybe_unused ) +{ +return PyLong_FromLong (thread__ppid (pthread->thread)); +} + +static PyObject *pyrf_thread__get_cpu (struct pyrf_thread *pthread,void *closure__maybe_unused ) +{ +return PyLong_FromLong (thread__cpu (pthread->thread)); +} + +static PyGetSetDef pyrf_thread__getset[] = { + { .name ="pid" , .get = (getter )pyrf_thread__get_pid, .doc ="process ID" }, + { .name ="tid" , .get = (getter )pyrf_thread__get_tid, .doc ="thread ID" }, + { .name ="ppid" , .get = (getter )pyrf_thread__get_ppid, .doc ="parent process ID" }, + { .name ="cpu" , .get = (getter )pyrf_thread__get_cpu, .doc ="cpu number" }, + { .name =NULL } +}; + +static const char pyrf_thread__doc[] =PyDoc_STR ("perf thread object." ); + +static PyTypeObject pyrf_thread__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.thread" , + .tp_basicsize =sizeof (struct pyrf_thread ), + .tp_dealloc = (destructor )pyrf_thread__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_methods = pyrf_thread__methods, + .tp_getset = pyrf_thread__getset, + .tp_doc = pyrf_thread__doc, +}; + +static int pyrf_thread__setup_types (void ) +{ +return PyType_Ready (&pyrf_thread__type); +} + +static PyObject *pyrf_thread__from_thread (struct thread *thread) +{ + struct pyrf_thread *pthread = PyObject_New(struct pyrf_thread, &pyrf_thread__type); + +if (!pthread) +return NULL ; + + pthread->thread =thread__get (thread); +return (PyObject *)pthread; +} + +struct pyrf_session { +PyObject_HEAD + + struct perf_session *session; +struct perf_tool tool; +struct pyrf_data *pdata; +PyObject *sample; +}; + +static int pyrf_session_tool__sample (const struct perf_tool *tool, +union perf_event *event, +struct perf_sample *sample, +struct machine *machine__maybe_unused ) +{ + struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); +PyObject *pyevent =pyrf_event__new (event); +struct pyrf_event *pevent = (struct pyrf_event *)pyevent; +PyObject *ret; +bool needs_swap; + +if (pyevent ==NULL ) +return -ENOMEM ; + + needs_swap = sample->evsel->needs_swap; + +memcpy (&pevent->event, event, event->header.size); +/* The event is already swapped, disable swapping temporarily */ + sample->evsel->needs_swap = false; +if (evsel__parse_sample (sample->evsel, &pevent->event, &pevent->sample) <0 ) { + sample->evsel->needs_swap = needs_swap; +Py_DECREF (pyevent); +return -1 ; + } + sample->evsel->needs_swap = needs_swap; + + ret =PyObject_CallFunction (psession->sample,"O" , pyevent); +if (!ret) { +Py_DECREF (pyevent); +return -1 ; + } +Py_DECREF (ret); +Py_DECREF (pyevent); +return 0 ; +} + +static PyObject *pyrf_session__find_thread (struct pyrf_session *psession,PyObject *args) +{ +struct machine *machine; +struct thread *thread =NULL ; +PyObject *result; +int pid; + +CHECK_INITIALIZED (psession->session,"session" ); + +if (!PyArg_ParseTuple (args,"i" , &pid)) +return NULL ; + + machine = &psession->session->machines.host; + thread =machine__find_thread (machine, pid, pid); + +if (!thread) { + machine =perf_session__find_machine (psession->session, pid); +if (machine) + thread =machine__find_thread (machine, pid, pid); + } + +if (!thread) { +PyErr_Format (PyExc_TypeError,"Failed to find thread %d" , pid); +return NULL ; + } + result =pyrf_thread__from_thread (thread); +thread__put (thread); +return result; +} + +static PyObject *pyrf_session__new (PyTypeObject *type,PyObject *args,PyObject *kwargs) +{ +struct pyrf_data *pdata; +PyObject *sample =NULL ; +static char *kwlist[] = {"data" ,"sample" ,NULL }; +struct pyrf_session *psession; +struct perf_session *session; + +if (!PyArg_ParseTupleAndKeywords (args, kwargs,"O!|O" , kwlist, &pyrf_data__type, &pdata, + &sample)) +return NULL ; + + psession = PyObject_New(struct pyrf_session, type); +if (!psession) +return NULL ; + + psession->session =NULL ; + psession->sample =NULL ; + psession->pdata =NULL ; + +Py_INCREF (pdata); + psession->pdata = pdata; + +perf_tool__init (&psession->tool,/*ordered_events=*/ true); + psession->tool.ordering_requires_timestamps = true; + +#define ADD_TOOL (name) \ + do { \ + if (name) { \ + if (!PyCallable_Check(name)) { \ + PyErr_SetString(PyExc_TypeError, #name " must be callable"); \ + goto err_out; \ + } \ + psession->tool.name = pyrf_session_tool__##name; \ + Py_INCREF(name); \ + psession->name = name; \ + } \ + } while (0) + +ADD_TOOL (sample); +#undef ADD_TOOL + + psession->tool.comm = perf_event__process_comm; + psession->tool.mmap = perf_event__process_mmap; + psession->tool.mmap2 = perf_event__process_mmap2; + psession->tool.namespaces = perf_event__process_namespaces; + psession->tool.cgroup = perf_event__process_cgroup; + psession->tool.exit = perf_event__process_exit; + psession->tool.fork = perf_event__process_fork; + psession->tool.ksymbol = perf_event__process_ksymbol; + psession->tool.text_poke = perf_event__process_text_poke; + psession->tool.build_id = perf_event__process_build_id; + psession->tool.attr = perf_event__process_attr; + psession->tool.feature = perf_event__process_feature; + psession->tool.stat = perf_event__process_stat_event; + session =perf_session__new (&pdata->data, &psession->tool); +if (IS_ERR (session)) { +PyErr_Format (PyExc_IOError,"failed to create session: %ld" ,PTR_ERR (session)); + goto err_out; + } + psession->session = session; + +if (symbol__init (perf_session__env (session)) <0 ) { +PyErr_SetString (PyExc_OSError,"perf: symbol__init failed" ); + goto err_out; + } + + + +return (PyObject *)psession; +err_out: +Py_DECREF (psession); +return NULL ; +} + +static void pyrf_session__delete (struct pyrf_session *psession) +{ +perf_session__delete (psession->session); +Py_XDECREF (psession->pdata); +Py_XDECREF (psession->sample); +Py_TYPE (psession)->tp_free ((PyObject *)psession); +} + +static PyObject *pyrf_session__find_thread_events (struct pyrf_session *psession) +{ +int err; + +CHECK_INITIALIZED (psession->session,"session" ); + + err =perf_session__process_events (psession->session); + +if (PyErr_Occurred ()) +return NULL ; + +if (err <0 ) { +PyErr_Format (PyExc_OSError,"Process events failed: %d" , err); +return NULL ; + } + + Py_RETURN_NONE; +} + +static PyMethodDef pyrf_session__methods[] = { + { + .ml_name ="process_events" , + .ml_meth = (PyCFunction )pyrf_session__find_thread_events, + .ml_flags =METH_NOARGS , + .ml_doc =PyDoc_STR ("Iterate and process events." ) + }, + { + .ml_name ="find_thread" , + .ml_meth = (PyCFunction )pyrf_session__find_thread, + .ml_flags =METH_VARARGS , + .ml_doc =PyDoc_STR ("Returns the thread associated with a pid." ) + }, + { .ml_name =NULL , } +}; + +static const char pyrf_session__doc[] =PyDoc_STR ("perf session object." ); + +static PyObject *pyrf_session__getattro (struct pyrf_session *psession,PyObject *attr_name) +{ +if (!psession->session) { +PyErr_SetString (PyExc_ValueError,"session not initialized" ); +return NULL ; + } +return PyObject_GenericGetAttr ((PyObject *) psession, attr_name); +} + +static int pyrf_session__setattro (struct pyrf_session *psession,PyObject *attr_name, +PyObject *value) +{ +if (!psession->session) { +PyErr_SetString (PyExc_ValueError,"session not initialized" ); +return -1 ; + } +return PyObject_GenericSetAttr ((PyObject *) psession, attr_name, value); +} + +static PyTypeObject pyrf_session__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.session" , + .tp_basicsize =sizeof (struct pyrf_session ), + .tp_dealloc = (destructor )pyrf_session__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_methods = pyrf_session__methods, + .tp_doc = pyrf_session__doc, + .tp_new = pyrf_session__new, + .tp_getattro = (getattrofunc ) pyrf_session__getattro, + .tp_setattro = (setattrofunc ) pyrf_session__setattro, +}; + +static int pyrf_session__setup_types (void ) +{ +return PyType_Ready (&pyrf_session__type); +} +static PyMethodDef perf__methods[] = { { .ml_name ="metrics" ,
@@ -2633,8 +2967,10 @@ PyMODINIT_FUNC PyInit_perf(void) };PyObject *module =PyModule_Create (&moduledef); - if (module == NULL || - pyrf_event__setup_types() < 0 || +if (module ==NULL ) +return NULL ; + + if (pyrf_event__setup_types() < 0 || pyrf_evlist__setup_types() < 0 || pyrf_evsel__setup_types() < 0 || pyrf_thread_map__setup_types() < 0 ||
@@ -2642,8 +2978,12 @@ PyMODINIT_FUNC PyInit_perf(void) pyrf_pmu_iterator__setup_types() < 0 || pyrf_pmu__setup_types() < 0 || pyrf_counts_values__setup_types() < 0 || - pyrf_data__setup_types() < 0) - return module; + pyrf_data__setup_types() < 0 || + pyrf_session__setup_types() < 0 || + pyrf_thread__setup_types() < 0) { +Py_DECREF (module); +return NULL ; + }/* The page_size is placed in util object. */ page_size =sysconf (_SC_PAGE_SIZE);
@@ -2693,6 +3033,9 @@ PyMODINIT_FUNC PyInit_perf(void)Py_INCREF (&pyrf_data__type);PyModule_AddObject (module,"data" , (PyObject *)&pyrf_data__type); +Py_INCREF (&pyrf_session__type); +PyModule_AddObject (module,"session" , (PyObject *)&pyrf_session__type); + dict =PyModule_GetDict (module);if (dict ==NULL ) goto error;
@@ -2706,7 +3049,9 @@ PyMODINIT_FUNC PyInit_perf(void) } error: -if (PyErr_Occurred ()) -PyErr_SetString (PyExc_ImportError,"perf: Init failed!" ); +if (PyErr_Occurred ()) { +Py_XDECREF (module); +return NULL ; + }return module; }
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
[email protected]
AI reviewRe: [PATCH v20 10/21] perf python: Add python session abstraction wrapping perf's session
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Disabling `needs_swap` in `pyrf_session_tool__sample` breaks parsing of 32-bit fields (like `pid`…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Disabling needs_swap in pyrf_session_tool__sample breaks parsing of 32-bit fields (like pid, tid, cpu) for cross-endian perf.data files. --
commit bcc392a3d23443fffd50461d98d652ca9e98f59b
Author: Ian Rogers <[email protected]>
perf python: Add python session abstraction wrapping perf's session
This commit adds a wrapper Python type for perf sessions, allowing the use of perf.data files within Python tools. It allows a sample callback to be passed when creating a session and includes support for retrieving thread associations for filtering.
Quoted 4 lines
tools/perf/util/python.c +0
-0
[ ... ]
Quoted 4 lines
tools/perf/util/python.c +0
-0@@ -2579,6 +2587,332 @@ static int pyrf_data__setup_types(void)return PyType_Ready (&pyrf_data__type); }
[ ... ]
Quoted 10 lines
tools/perf/util/python.c +10
-0+static int pyrf_session_tool__sample (const struct perf_tool *tool, +union perf_event *event, +struct perf_sample *sample, +struct machine *machine__maybe_unused ) +{ + struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); +PyObject *pyevent =pyrf_event__new (event); +struct pyrf_event *pevent = (struct pyrf_event *)pyevent; +PyObject *ret; +bool needs_swap;
tools/perf/util/python.c static int pyrf_data__setup_types(void) · line 2712
Quoted 10 lines
tools/perf/util/python.c +10
-0+ +if (pyevent ==NULL ) +return -ENOMEM ; + + needs_swap = sample->evsel->needs_swap; + +memcpy (&pevent->event, event, event->header.size); +/* The event is already swapped, disable swapping temporarily */ + sample->evsel->needs_swap = false; +if (evsel__parse_sample (sample->evsel, &pevent->event, &pevent->sample) <0 ) {
[Severity: High]
Does disabling the needs_swap flag here break the parsing of 32-bit fields like pid, tid, and cpu for cross-endian perf.data files?
When perf_session__process_events() processes a cross-endian file, it first swaps the event payload in 64-bit chunks. The evsel__parse_sample() function relies on the needs_swap flag to correctly undo this 64-bit swap and apply 32-bit swaps for fields like pid and tid.
By temporarily forcing sample->evsel->needs_swap = false before calling evsel__parse_sample() on a copy of the event, won't the 32-bit fixup logic be skipped?
This could cause pevent->sample.pid and pevent->sample.tid to receive byte-swapped values, and break raw_size extraction, which might misalign subsequent parsed fields resulting in data corruption.
Although this logic is moved to pyrf_event__new() later in the series, the needs_swap override appears to persist. Could this be adjusted to ensure cross-endian data is handled correctly?
Quoted 5 lines
tools/perf/util/python.c +5
-0+ sample->evsel->needs_swap = needs_swap; +Py_DECREF (pyevent); +return -1 ; + } + sample->evsel->needs_swap = needs_swap;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Ian Rogers
reviewerPATCH v20 11/21 perf python: Refactor and add accessors to sample event
In reply to Ian RogersAdd common evsel field for events and move sample specific fields to only be present in sample events. Add accessors for sample events. Ensure offsets are within the bounds of the event. Allocate just enough memory for the copied event, don't make the maximum event size each time.
--- tools/perf/util/python.c | 519 +++++++++++++++++++++++++++++++------- tools/perf/util/session.c | 2 +- tools/perf/util/session.h | 2 + 3 files changed, 429 insertions(+), 94 deletions(-)
tools/perf/util/python.c +426 -93
@@ -13,21 +13,28 @@#include <perf/mmap.h> #include <structmember.h> +#include "addr_location.h" +#include "build-id.h" #include "callchain.h" #include "comm.h" #include "counts.h" #include "data.h" #include "debug.h" +#include "dso.h" #include "event.h" #include "evlist.h" #include "evsel.h" #include "expr.h" +#include "map.h" #include "metricgroup.h" #include "mmap.h" #include "pmus.h" #include "print_binary.h" #include "record.h" +#include "sample.h" #include "session.h" +#include "srccode.h" +#include "srcline.h" #include "strbuf.h" #include "symbol.h" #include "stat.h"
@@ -37,7 +44,6 @@#include "tool.h" #include "tp_pmu.h" #include "trace-event.h" -#include "util/sample.h" #ifdef HAVE_LIBTRACEEVENT #include <event-parse.h>
@@ -45,6 +51,8 @@PyMODINIT_FUNC PyInit_perf (void ); +static PyObject *pyrf_evsel__from_evsel (struct evsel *evsel); +#define member_def (type, member, ptype, help) \ { #member, ptype, \ offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
@@ -73,21 +81,53 @@ PyMODINIT_FUNC PyInit_perf(void);struct pyrf_event {PyObject_HEAD +/** @sample: The parsed sample from the event. */ struct perf_sample sample; -union perf_event event; +/** @al: The address location from machine__resolve, lazily computed. */ +struct addr_location al; +/** @al_resolved: True when machine__resolve been called. */ +bool al_resolved; +/** @event: The underlying perf_event that may be in a file or ring buffer. */ +union perf_event event; };#define sample_members \ - sample_member_def(sample_ip, ip, T_ULONGLONG, "event ip"), \ sample_member_def(sample_pid, pid, T_INT, "event pid"), \ sample_member_def(sample_tid, tid, T_INT, "event tid"), \ sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \ - sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \ sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \ sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \ sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \ sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"), +static PyObject *pyrf_event__get_evsel (PyObject *self,void *closure__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; + +if (!pevent->sample.evsel) + Py_RETURN_NONE; + +return pyrf_evsel__from_evsel (pevent->sample.evsel); +} + +static PyGetSetDef pyrf_event__getset[] = { + { + .name ="evsel" , + .get = pyrf_event__get_evsel, + .set =NULL , + .doc ="tracking event." , + }, + { .name =NULL , }, +}; + +static void pyrf_event__delete (struct pyrf_event *pevent) +{ +if (pevent->al_resolved) +addr_location__exit (&pevent->al); +perf_sample__exit (&pevent->sample); +Py_TYPE (pevent)->tp_free ((PyObject *)pevent); +} +static const char pyrf_mmap_event__doc[] =PyDoc_STR ("perf mmap event object." );static PyMemberDef pyrf_mmap_event__members[] = {
@@ -126,9 +166,11 @@ static PyTypeObject pyrf_mmap_event__type = {PyVarObject_HEAD_INIT (NULL ,0 ) .tp_name = "perf.mmap_event", .tp_basicsize = sizeof(struct pyrf_event), + .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_mmap_event__doc, .tp_members = pyrf_mmap_event__members, + .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_mmap_event__repr, };
@@ -161,9 +203,11 @@ static PyTypeObject pyrf_task_event__type = {PyVarObject_HEAD_INIT (NULL ,0 ) .tp_name = "perf.task_event", .tp_basicsize = sizeof(struct pyrf_event), + .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_task_event__doc, .tp_members = pyrf_task_event__members, + .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_task_event__repr, };
@@ -190,9 +234,11 @@ static PyTypeObject pyrf_comm_event__type = {PyVarObject_HEAD_INIT (NULL ,0 ) .tp_name = "perf.comm_event", .tp_basicsize = sizeof(struct pyrf_event), + .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_comm_event__doc, .tp_members = pyrf_comm_event__members, + .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_comm_event__repr, };
@@ -222,9 +268,11 @@ static PyTypeObject pyrf_throttle_event__type = {PyVarObject_HEAD_INIT (NULL ,0 ) .tp_name = "perf.throttle_event", .tp_basicsize = sizeof(struct pyrf_event), + .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_throttle_event__doc, .tp_members = pyrf_throttle_event__members, + .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_throttle_event__repr, };
@@ -232,6 +280,7 @@ static const char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");static PyMemberDef pyrf_lost_event__members[] = { sample_members +member_def (perf_event_header, type,T_UINT ,"event type" ),member_def (perf_record_lost, id,T_ULONGLONG ,"event id" ),member_def (perf_record_lost, lost,T_ULONGLONG ,"number of lost events" ), { .name =NULL , },
@@ -257,9 +306,11 @@ static PyTypeObject pyrf_lost_event__type = {PyVarObject_HEAD_INIT (NULL ,0 ) .tp_name = "perf.lost_event", .tp_basicsize = sizeof(struct pyrf_event), + .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_lost_event__doc, .tp_members = pyrf_lost_event__members, + .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_lost_event__repr, };
@@ -267,6 +318,7 @@ static const char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");static PyMemberDef pyrf_read_event__members[] = { sample_members +member_def (perf_event_header, type,T_UINT ,"event type" ),member_def (perf_record_read, pid,T_UINT ,"event pid" ),member_def (perf_record_read, tid,T_UINT ,"event tid" ), { .name =NULL , },
@@ -287,9 +339,11 @@ static PyTypeObject pyrf_read_event__type = {PyVarObject_HEAD_INIT (NULL ,0 ) .tp_name = "perf.read_event", .tp_basicsize = sizeof(struct pyrf_event), + .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_read_event__doc, .tp_members = pyrf_read_event__members, + .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_read_event__repr, };
@@ -297,16 +351,17 @@ static const char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event objectstatic PyMemberDef pyrf_sample_event__members[] = { sample_members +sample_member_def (sample_ip, ip,T_ULONGLONG ,"event ip" ), +sample_member_def (sample_addr, addr,T_ULONGLONG ,"event addr" ), +sample_member_def (sample_phys_addr, phys_addr,T_ULONGLONG ,"event physical addr" ), +sample_member_def (sample_weight, weight,T_ULONGLONG ,"event weight" ), +sample_member_def (sample_data_src, data_src,T_ULONGLONG ,"event data source" ), +sample_member_def (sample_insn_count, insn_cnt,T_ULONGLONG ,"event instruction count" ), +sample_member_def (sample_cyc_count, cyc_cnt,T_ULONGLONG ,"event cycle count" ),member_def (perf_event_header, type,T_UINT ,"event type" ), { .name =NULL , }, }; -static void pyrf_sample_event__delete (struct pyrf_event *pevent) -{ -perf_sample__exit (&pevent->sample); -Py_TYPE (pevent)->tp_free ((PyObject *)pevent); -} -static PyObject *pyrf_sample_event__repr (const struct pyrf_event *pevent) {PyObject *ret;
@@ -324,6 +379,8 @@ static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent)#ifdef HAVE_LIBTRACEEVENT static bool is_tracepoint (const struct pyrf_event *pevent) { +if (!pevent->sample.evsel) +return false;return pevent->sample.evsel->core.attr.type ==PERF_TYPE_TRACEPOINT ; }
@@ -394,6 +451,199 @@ get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) }#endif /* HAVE_LIBTRACEEVENT */ +static int pyrf_sample_event__resolve_al (struct pyrf_event *pevent) +{ +struct evsel *evsel = pevent->sample.evsel; +struct evlist *evlist = evsel ? evsel->evlist :NULL ; +struct perf_session *session = evlist ?evlist__session (evlist) :NULL ; + +if (pevent->al_resolved) +return 0 ; + +if (!session) +return -1 ; + +addr_location__init (&pevent->al); +if (machine__resolve (&session->machines.host, &pevent->al, &pevent->sample) <0 ) { +addr_location__exit (&pevent->al); +return -1 ; + } + + pevent->al_resolved = true; +return 0 ; +} + +static PyObject *pyrf_sample_event__get_dso (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyUnicode_FromString (dso__name (map__dso (pevent->al.map))); +} + +static PyObject *pyrf_sample_event__get_dso_long_name (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyUnicode_FromString (dso__long_name (map__dso (pevent->al.map))); +} + +static PyObject *pyrf_sample_event__get_dso_bid (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +char sbuild_id[SBUILD_ID_SIZE ]; + +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +build_id__snprintf (dso__bid (map__dso (pevent->al.map)), sbuild_id,sizeof (sbuild_id)); +return PyUnicode_FromString (sbuild_id); +} + +static PyObject *pyrf_sample_event__get_map_start (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLong (map__start (pevent->al.map)); +} + +static PyObject *pyrf_sample_event__get_map_end (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLong (map__end (pevent->al.map)); +} + +static PyObject *pyrf_sample_event__get_map_pgoff (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLongLong (map__pgoff (pevent->al.map)); +} + +static PyObject *pyrf_sample_event__get_symbol (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.sym) + Py_RETURN_NONE; + +return PyUnicode_FromString (pevent->al.sym->name); +} + +static PyObject *pyrf_sample_event__get_sym_start (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.sym) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLongLong (pevent->al.sym->start); +} + +static PyObject *pyrf_sample_event__get_sym_end (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.sym) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLongLong (pevent->al.sym->end); +} + +static PyObject *pyrf_sample_event__get_raw_buf (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pevent->event.header.type !=PERF_RECORD_SAMPLE ) + Py_RETURN_NONE; + +return PyBytes_FromStringAndSize ((const char *)pevent->sample.raw_data, + pevent->sample.raw_size); +} + +static PyObject *pyrf_sample_event__srccode (PyObject *self,PyObject *args) +{ +struct pyrf_event *pevent = (void *)self; +u64 addr = pevent->sample.ip; +char *srcfile =NULL ; +char *srccode =NULL ; +unsigned int line =0 ; +int len =0 ; +PyObject *result; +struct addr_location al; + +if (!PyArg_ParseTuple (args,"|K" , &addr)) +return NULL ; + +if (pyrf_sample_event__resolve_al (pevent) <0 ) + Py_RETURN_NONE; + +if (addr != pevent->sample.ip) { +addr_location__init (&al); +thread__find_symbol_fb (pevent->al.thread, pevent->sample.cpumode, addr, &al); + }else { +addr_location__init (&al); + al.thread =thread__get (pevent->al.thread); + al.map =map__get (pevent->al.map); + al.sym = pevent->al.sym; + al.addr = pevent->al.addr; + } + +if (al.map) { +struct dso *dso =map__dso (al.map); + +if (dso) { + srcfile =get_srcline_split (dso,map__rip_2objdump (al.map, addr), + &line); + } + } +addr_location__exit (&al); + +if (srcfile) { + srccode =find_sourceline (srcfile, line, &len); + result =Py_BuildValue ("(sIs#)" , srcfile, line, srccode, (Py_ssize_t )len); +free (srcfile); + }else { + result =Py_BuildValue ("(sIs#)" ,NULL ,0 ,NULL , (Py_ssize_t )0 ); + } + +return result; +} + +static PyObject *pyrf_sample_event__insn (PyObject *self,PyObject *args__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; +struct thread *thread; +struct machine *machine; + +if (pyrf_sample_event__resolve_al (pevent) <0 ) + Py_RETURN_NONE; + + thread = pevent->al.thread; + +if (!thread || !thread__maps (thread)) + Py_RETURN_NONE; + + machine =maps__machine (thread__maps (thread)); +if (!machine) + Py_RETURN_NONE; + +if (pevent->sample.ip && !pevent->sample.insn_len) +perf_sample__fetch_insn (&pevent->sample, thread, machine); + +if (!pevent->sample.insn_len) + Py_RETURN_NONE; + +return PyBytes_FromStringAndSize ((const char *)pevent->sample.insn, + pevent->sample.insn_len); +} +static PyObject *pyrf_sample_event__getattro (struct pyrf_event *pevent,PyObject *attr_name) {
@@ -407,13 +657,102 @@ pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)return obj ?:PyObject_GenericGetAttr ((PyObject *) pevent, attr_name); } +static PyGetSetDef pyrf_sample_event__getset[] = { + { + .name ="raw_buf" , + .get = (getter )pyrf_sample_event__get_raw_buf, + .set =NULL , + .doc ="event raw buffer." , + }, + { + .name ="evsel" , + .get = pyrf_event__get_evsel, + .set =NULL , + .doc ="tracking event." , + }, + { + .name ="dso" , + .get = (getter )pyrf_sample_event__get_dso, + .set =NULL , + .doc ="event dso short name." , + }, + { + .name ="dso_long_name" , + .get = (getter )pyrf_sample_event__get_dso_long_name, + .set =NULL , + .doc ="event dso long name." , + }, + { + .name ="dso_bid" , + .get = (getter )pyrf_sample_event__get_dso_bid, + .set =NULL , + .doc ="event dso build id." , + }, + { + .name ="map_start" , + .get = (getter )pyrf_sample_event__get_map_start, + .set =NULL , + .doc ="event map start address." , + }, + { + .name ="map_end" , + .get = (getter )pyrf_sample_event__get_map_end, + .set =NULL , + .doc ="event map end address." , + }, + { + .name ="map_pgoff" , + .get = (getter )pyrf_sample_event__get_map_pgoff, + .set =NULL , + .doc ="event map page offset." , + }, + { + .name ="symbol" , + .get = (getter )pyrf_sample_event__get_symbol, + .set =NULL , + .doc ="event symbol name." , + }, + { + .name ="sym_start" , + .get = (getter )pyrf_sample_event__get_sym_start, + .set =NULL , + .doc ="event symbol start address." , + }, + { + .name ="sym_end" , + .get = (getter )pyrf_sample_event__get_sym_end, + .set =NULL , + .doc ="event symbol end address." , + }, + { .name =NULL , }, +}; + +static PyMethodDef pyrf_sample_event__methods[] = { + { + .ml_name ="srccode" , + .ml_meth = (PyCFunction )pyrf_sample_event__srccode, + .ml_flags =METH_VARARGS , + .ml_doc =PyDoc_STR ("Get source code for an address." ) + }, + { + .ml_name ="insn" , + .ml_meth = (PyCFunction )pyrf_sample_event__insn, + .ml_flags =METH_NOARGS , + .ml_doc =PyDoc_STR ("Get instruction bytes for a sample." ) + }, + { .ml_name =NULL , } +}; +static PyTypeObject pyrf_sample_event__type = {PyVarObject_HEAD_INIT (NULL ,0 ) .tp_name ="perf.sample_event" , .tp_basicsize =sizeof (struct pyrf_event ), + .tp_dealloc = (destructor )pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_sample_event__doc, .tp_members = pyrf_sample_event__members, + .tp_getset = pyrf_sample_event__getset, + .tp_methods = pyrf_sample_event__methods, .tp_repr = (reprfunc )pyrf_sample_event__repr, .tp_getattro = (getattrofunc ) pyrf_sample_event__getattro, };
@@ -449,25 +788,17 @@ static PyTypeObject pyrf_context_switch_event__type = {PyVarObject_HEAD_INIT (NULL ,0 ) .tp_name = "perf.context_switch_event", .tp_basicsize = sizeof(struct pyrf_event), + .tp_dealloc = (destructor)pyrf_event__delete, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, .tp_doc = pyrf_context_switch_event__doc, .tp_members = pyrf_context_switch_event__members, + .tp_getset = pyrf_event__getset, .tp_repr = (reprfunc)pyrf_context_switch_event__repr, };static int pyrf_event__setup_types (void ) {int err; - pyrf_mmap_event__type.tp_new = - pyrf_task_event__type.tp_new = - pyrf_comm_event__type.tp_new = - pyrf_lost_event__type.tp_new = - pyrf_read_event__type.tp_new = - pyrf_sample_event__type.tp_new = - pyrf_context_switch_event__type.tp_new = - pyrf_throttle_event__type.tp_new = PyType_GenericNew; - - pyrf_sample_event__type.tp_dealloc = (destructor )pyrf_sample_event__delete, err =PyType_Ready (&pyrf_mmap_event__type);if (err <0 )
@@ -511,33 +842,57 @@ static PyTypeObject *pyrf_event__type[] = { [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, }; -static PyObject *pyrf_event__new (const union perf_event *event) +static PyObject *pyrf_event__new (const union perf_event *event,struct evsel *evsel) {struct pyrf_event *pevent; -PyTypeObject *ptype; +int err; +u32 min_size; +bool needs_swap; -if ((event->header.type <PERF_RECORD_MMAP || - event->header.type >PERF_RECORD_SAMPLE ) && - !(event->header.type ==PERF_RECORD_SWITCH || - event->header.type ==PERF_RECORD_SWITCH_CPU_WIDE )) { -PyErr_Format (PyExc_TypeError,"Unexpected header type %u" , +if (event->header.type >=ARRAY_SIZE (pyrf_event__type) || + pyrf_event__type[event->header.type] ==NULL ) { +return PyErr_Format (PyExc_TypeError,"Unexpected header type %u" , event->header.type); -return NULL ; } -// FIXME this better be dynamic or we need to parse everything -// before calling perf_mmap__consume(), including tracepoint fields. -if (sizeof (pevent->event) < event->header.size) { -PyErr_Format (PyExc_TypeError,"Unexpected event size: %zd < %u" , -sizeof (pevent->event), event->header.size); -return NULL ; +if (perf_event__too_small (event, &min_size)) { +return PyErr_Format (PyExc_ValueError,"Event size %u too small for type %u" , + event->header.size, event->header.type); } - ptype = pyrf_event__type[event->header.type]; - pevent = PyObject_New(struct pyrf_event, ptype); -if (pevent !=NULL ) { -memcpy (&pevent->event, event, event->header.size); -perf_sample__init (&pevent->sample,/*all=*/ false); +size_t copy_size = event->header.size; + +if (copy_size >sizeof (pevent->event)) { +return PyErr_Format (PyExc_TypeError,"Unexpected event size: %zd < %zu" , +sizeof (pevent->event), copy_size); + } + + pevent = PyObject_New(struct pyrf_event, pyrf_event__type[event->header.type]); +if (pevent ==NULL ) +return PyErr_NoMemory (); + +/* Copy the event for memory safety and initialize variables. */ +memcpy (&pevent->event, event, copy_size); +if (copy_size <sizeof (pevent->event)) +memset ((char *)&pevent->event + copy_size,0 ,sizeof (pevent->event) - copy_size); + +perf_sample__init (&pevent->sample,/*all=*/ true); + pevent->al_resolved = false; +addr_location__init (&pevent->al); + +if (!evsel) +return (PyObject *)pevent; + +/* Parse the sample again so that pointers are within the copied event. */ + needs_swap = evsel->needs_swap; + + evsel->needs_swap = false; + err =evsel__parse_sample (evsel, &pevent->event, &pevent->sample); + evsel->needs_swap = needs_swap; +if (err <0 ) { +Py_DECREF (pevent); +return PyErr_Format (PyExc_OSError, +"perf: can't parse sample, err=%d" , err); }return (PyObject *)pevent; }
@@ -1244,7 +1599,7 @@ static PyObject *pyrf_evsel__str(PyObject *self)if (!evsel)return PyUnicode_FromString ("evsel(uninitialized)" ); -return PyUnicode_FromFormat ("evsel(%s/%s/)" ,evsel__pmu_name (evsel),evsel__name (evsel)); +return PyUnicode_FromFormat ("evsel(%s)" ,evsel__name (evsel)); }static PyMethodDef pyrf_evsel__methods[] = {
@@ -1878,9 +2233,11 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, {struct evlist *evlist;union perf_event *event; +struct evsel *evsel;int sample_id_all =1 , cpu;static char *kwlist[] = {"cpu" ,"sample_id_all" ,NULL };struct mmap *md; +PyObject *pyevent;int err;CHECK_INITIALIZED (pevlist->evlist,"evlist" );
@@ -1891,44 +2248,33 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,return NULL ; md =get_md (evlist, cpu); -if (!md) { -PyErr_Format (PyExc_TypeError,"Unknown CPU '%d'" , cpu); -return NULL ; - } +if (!md) +return PyErr_Format (PyExc_TypeError,"Unknown CPU '%d'" , cpu); -if (perf_mmap__read_init (&md->core) <0 ) - goto end; + err =perf_mmap__read_init (&md->core); +if (err <0 ) { +if (err == -EAGAIN ) + Py_RETURN_NONE; +return PyErr_Format (PyExc_OSError, +"perf: error mmap read init, err=%d" , err); + } event =perf_mmap__read_event (&md->core); -if (event !=NULL ) { -PyObject *pyevent =pyrf_event__new (event); -struct pyrf_event *pevent = (struct pyrf_event *)pyevent; -struct evsel *evsel; - -if (pyevent ==NULL ) -return PyErr_NoMemory (); - - evsel =evlist__event2evsel (evlist, event); -if (!evsel) { -Py_DECREF (pyevent); -Py_INCREF (Py_None); -return Py_None; - } +if (event ==NULL ) + Py_RETURN_NONE; + evsel =evlist__event2evsel (evlist, event); +if (!evsel) { +/* Unknown evsel. */ perf_mmap__consume (&md->core); - - err =evsel__parse_sample (evsel, &pevent->event, &pevent->sample); -if (err) { -Py_DECREF (pyevent); -return PyErr_Format (PyExc_OSError, -"perf: can't parse sample, err=%d" , err); - } - -return pyevent; + Py_RETURN_NONE; } -end: -Py_INCREF (Py_None); -return Py_None; + pyevent =pyrf_event__new (event, evsel); +perf_mmap__consume (&md->core); +if (pyevent ==NULL ) +return PyErr_Occurred () ?NULL :PyErr_NoMemory (); + +return pyevent; } static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
@@ -2140,10 +2486,7 @@ static PyObject *pyrf_evlist__str(PyObject *self)evlist__for_each_entry (pevlist->evlist, pos) {if (!first)strbuf_addch (&sb,',' ); -if (!pos->pmu) -strbuf_addstr (&sb,evsel__name (pos)); -else -strbuf_addf (&sb,"%s/%s/" , pos->pmu->name,evsel__name (pos)); +strbuf_addstr (&sb,evsel__name (pos)); first = false; }strbuf_addstr (&sb,"])" );
@@ -2225,6 +2568,7 @@ static const struct perf_constant perf__constants[] = {PERF_CONST (TYPE_BREAKPOINT ),PERF_CONST (COUNT_HW_CPU_CYCLES ), +PERF_CONST (COUNT_HW_REF_CPU_CYCLES ),PERF_CONST (COUNT_HW_INSTRUCTIONS ),PERF_CONST (COUNT_HW_CACHE_REFERENCES ), PERF_CONST(COUNT_HW_CACHE_MISSES),
@@ -2696,26 +3040,12 @@ static int pyrf_session_tool__sample(const struct perf_tool *tool, struct machine *machine __maybe_unused) { struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); -PyObject *pyevent =pyrf_event__new (event); -struct pyrf_event *pevent = (struct pyrf_event *)pyevent; +PyObject *pyevent =pyrf_event__new (event, sample->evsel);PyObject *ret; -bool needs_swap;if (pyevent ==NULL )return -ENOMEM ; - needs_swap = sample->evsel->needs_swap; - -memcpy (&pevent->event, event, event->header.size); -/* The event is already swapped, disable swapping temporarily */ - sample->evsel->needs_swap = false; -if (evsel__parse_sample (sample->evsel, &pevent->event, &pevent->sample) <0 ) { - sample->evsel->needs_swap = needs_swap; -Py_DECREF (pyevent); -return -1 ; - } - sample->evsel->needs_swap = needs_swap; - ret =PyObject_CallFunction (psession->sample,"O" , pyevent);if (!ret) {Py_DECREF (pyevent);
@@ -2989,10 +3319,13 @@ PyMODINIT_FUNC PyInit_perf(void) page_size =sysconf (_SC_PAGE_SIZE);Py_INCREF (&pyrf_evlist__type); -PyModule_AddObject (module,"evlist" , (PyObject *)&pyrf_evlist__type); +PyModule_AddObject (module,"evlist" , (PyObject *)&pyrf_evlist__type);Py_INCREF (&pyrf_evsel__type); -PyModule_AddObject (module,"evsel" , (PyObject *)&pyrf_evsel__type); +PyModule_AddObject (module,"evsel" , (PyObject *)&pyrf_evsel__type); + +Py_INCREF (&pyrf_thread__type); +PyModule_AddObject (module,"thread" , (PyObject *)&pyrf_thread__type);Py_INCREF (&pyrf_mmap_event__type);PyModule_AddObject (module,"mmap_event" , (PyObject *)&pyrf_mmap_event__type);
tools/perf/util/session.c +1 -1
@@ -2672,7 +2672,7 @@ static const u32 perf_event__min_size[PERF_RECORD_HEADER_MAX] = {* Caller must ensure event->header.type < PERF_RECORD_HEADER_MAX. * If min is non-NULL, stores the required minimum on failure. */ -static bool perf_event__too_small (const union perf_event *event,u32 *min) +bool perf_event__too_small (const union perf_event *event,u32 *min) {u32 min_sz = perf_event__min_size[event->header.type];
tools/perf/util/session.h +2 -0
@@ -122,6 +122,8 @@ void perf_session__delete(struct perf_session *session);void perf_event_header__bswap (struct perf_event_header *hdr); +bool perf_event__too_small (const union perf_event *event,u32 *min); + int perf_session__peek_event(struct perf_session *session, off_t file_offset, void *buf, size_t buf_sz, union perf_event **event_ptr,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
[email protected]
AI reviewRe: [PATCH v20 11/21] perf python: Refactor and add accessors to sample event
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Accessor functions for `dso` blindly dereference the result of `map__dso()` which can be NULL.…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Accessor functions for dso blindly dereference the result of map__dso() which can be NULL.
- [Medium] 64-bit map start and end addresses are truncated to 32 bits when perf is compiled on a 32-bit host. --
commit 18ea80c7f1771915bbf09a1482be621af384623d
Author: Ian Rogers <[email protected]>
perf python: Refactor and add accessors to sample event
Add common evsel field for events and move sample specific fields to only be present in sample events. Add accessors for sample events. Ensure offsets are within the bounds of the event. Allocate just enough memory for the copied event, don't make the maximum event size each time.
Quoted 4 lines
tools/perf/util/python.c +0
-0
[ ... ]
Quoted 6 lines
tools/perf/util/python.c +2
-0@@ -394,6 +451,199 @@ get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) }#endif /* HAVE_LIBTRACEEVENT */ +static int pyrf_sample_event__resolve_al (struct pyrf_event *pevent) +{
[ ... ]
tools/perf/util/python.c get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) · line 482
Quoted 7 lines
tools/perf/util/python.c +7
-0+static PyObject *pyrf_sample_event__get_dso (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyUnicode_FromString (dso__name (map__dso (pevent->al.map)));
[Severity: High]
Does this code risk a NULL pointer dereference? map__dso() can return NULL for certain mappings, and passing it unconditionally to dso__name() would crash the python process.
tools/perf/util/python.c get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) · line 491
Quoted 9 lines
tools/perf/util/python.c +9
-0+} + +static PyObject *pyrf_sample_event__get_dso_long_name (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyUnicode_FromString (dso__long_name (map__dso (pevent->al.map)));
[Severity: High]
Can map__dso() return NULL here as well, leading to a NULL pointer dereference in dso__long_name()?
tools/perf/util/python.c get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) · line 502
Quoted 11 lines
tools/perf/util/python.c +11
-0+} + +static PyObject *pyrf_sample_event__get_dso_bid (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +char sbuild_id[SBUILD_ID_SIZE ]; + +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +build_id__snprintf (dso__bid (map__dso (pevent->al.map)), sbuild_id,sizeof (sbuild_id));
[Severity: High]
Similarly, if map__dso() returns NULL, will this cause a crash when passed to dso__bid()?
tools/perf/util/python.c get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) · line 512
Quoted 10 lines
tools/perf/util/python.c +10
-0+return PyUnicode_FromString (sbuild_id); +} + +static PyObject *pyrf_sample_event__get_map_start (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLong (map__start (pevent->al.map));
[Severity: Medium]
Since map__start() returns a u64, will PyLong_FromUnsignedLong() truncate the upper 32 bits of the address on 32-bit hosts? This might break cross-platform analysis when analyzing 64-bit perf.data files. Should this use PyLong_FromUnsignedLongLong() instead?
tools/perf/util/python.c get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) · line 521
Quoted 9 lines
tools/perf/util/python.c +9
-0+} + +static PyObject *pyrf_sample_event__get_map_end (struct pyrf_event *pevent, +void *closure__maybe_unused ) +{ +if (pyrf_sample_event__resolve_al (pevent) <0 || !pevent->al.map) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLong (map__end (pevent->al.map));
[Severity: Medium]
Will this also truncate the 64-bit end address on 32-bit hosts?
Quoted 3 lines
tools/perf/util/python.c +3
-0+} + +static PyObject *pyrf_sample_event__get_map_pgoff(struct pyrf_event *pevent,
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Ian Rogers
reviewerPATCH v20 12/21 perf python: Add mmap2 event
In reply to Ian RogersIf mmap is handled so should mmap2 events. Add support as a distinct python event type.
--- tools/perf/util/python.c | 153 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 152 insertions(+), 1 deletion(-)
tools/perf/util/python.c +152 -1
@@ -133,7 +133,7 @@ static const char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");static PyMemberDef pyrf_mmap_event__members[] = { sample_membersmember_def (perf_event_header, type,T_UINT ,"event type" ), -member_def (perf_event_header, misc,T_UINT ,"event misc" ), +member_def (perf_event_header, misc,T_USHORT ,"event misc" ),member_def (perf_record_mmap, pid,T_UINT ,"event pid" ),member_def (perf_record_mmap, tid,T_UINT ,"event tid" ),member_def (perf_record_mmap, start,T_ULONGLONG ,"start of the map" ),
@@ -174,6 +174,147 @@ static PyTypeObject pyrf_mmap_event__type = { .tp_repr = (reprfunc)pyrf_mmap_event__repr, }; +static const char pyrf_mmap2_event__doc[] =PyDoc_STR ("perf mmap2 event object." ); + +static PyObject *pyrf_mmap2_event__get_maj (PyObject *self,void *closure__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; + +if (pevent->event.header.misc &PERF_RECORD_MISC_MMAP_BUILD_ID ) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLong (pevent->event.mmap2.maj); +} + +static PyObject *pyrf_mmap2_event__get_min (PyObject *self,void *closure__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; + +if (pevent->event.header.misc &PERF_RECORD_MISC_MMAP_BUILD_ID ) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLong (pevent->event.mmap2.min); +} + +static PyObject *pyrf_mmap2_event__get_ino (PyObject *self,void *closure__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; + +if (pevent->event.header.misc &PERF_RECORD_MISC_MMAP_BUILD_ID ) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLongLong (pevent->event.mmap2.ino); +} + +static PyObject *pyrf_mmap2_event__get_ino_generation (PyObject *self,void *closure__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; + +if (pevent->event.header.misc &PERF_RECORD_MISC_MMAP_BUILD_ID ) + Py_RETURN_NONE; + +return PyLong_FromUnsignedLongLong (pevent->event.mmap2.ino_generation); +} + +static PyObject *pyrf_mmap2_event__get_build_id (PyObject *self,void *closure__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; + +if (!(pevent->event.header.misc &PERF_RECORD_MISC_MMAP_BUILD_ID )) + Py_RETURN_NONE; + +int size = pevent->event.mmap2.build_id_size; + +if (size >20 ) + size =20 ; + +return PyBytes_FromStringAndSize ((const char *)pevent->event.mmap2.build_id, size); +} + +static PyGetSetDef pyrf_mmap2_event__getset[] = { + { + .name ="evsel" , + .get = pyrf_event__get_evsel, + .set =NULL , + .doc ="tracking event." , + }, + { + .name ="maj" , + .get = pyrf_mmap2_event__get_maj, + .set =NULL , + .doc ="major number." , + }, + { + .name ="min" , + .get = pyrf_mmap2_event__get_min, + .set =NULL , + .doc ="minor number." , + }, + { + .name ="ino" , + .get = pyrf_mmap2_event__get_ino, + .set =NULL , + .doc ="inode number." , + }, + { + .name ="ino_generation" , + .get = pyrf_mmap2_event__get_ino_generation, + .set =NULL , + .doc ="inode generation." , + }, + { + .name ="build_id" , + .get = pyrf_mmap2_event__get_build_id, + .set =NULL , + .doc ="binary build ID." , + }, + { .name =NULL , }, +}; + +static PyMemberDef pyrf_mmap2_event__members[] = { + sample_members +member_def (perf_event_header, type,T_UINT ,"event type" ), +member_def (perf_event_header, misc,T_USHORT ,"event misc" ), +member_def (perf_record_mmap2, pid,T_UINT ,"event pid" ), +member_def (perf_record_mmap2, tid,T_UINT ,"event tid" ), +member_def (perf_record_mmap2, start,T_ULONGLONG ,"start of the map" ), +member_def (perf_record_mmap2, len,T_ULONGLONG ,"map length" ), +member_def (perf_record_mmap2, pgoff,T_ULONGLONG ,"page offset" ), +member_def (perf_record_mmap2, prot,T_UINT ,"protection" ), +member_def (perf_record_mmap2, flags,T_UINT ,"flags" ), +member_def (perf_record_mmap2, filename,T_STRING_INPLACE ,"backing store" ), + { .name =NULL , }, +}; + +static PyObject *pyrf_mmap2_event__repr (const struct pyrf_event *pevent) +{ +PyObject *ret; +char *s; + +if (asprintf (&s,"{ type: mmap2, pid: %u, tid: %u, start: %#" PRI_lx64", length: %#" PRI_lx64", offset: %#" PRI_lx64", flags: %#x, prot: %#x, filename: %s }" , + pevent->event.mmap2.pid, pevent->event.mmap2.tid, + pevent->event.mmap2.start, pevent->event.mmap2.len, + pevent->event.mmap2.pgoff, pevent->event.mmap2.flags, + pevent->event.mmap2.prot, pevent->event.mmap2.filename) <0 ) +return PyErr_NoMemory (); + + ret =PyUnicode_FromString (s); +free (s); +return ret; +} + +static PyTypeObject pyrf_mmap2_event__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.mmap2_event" , + .tp_basicsize =sizeof (struct pyrf_event ), + .tp_dealloc = (destructor )pyrf_event__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = pyrf_mmap2_event__doc, + .tp_members = pyrf_mmap2_event__members, + .tp_getset = pyrf_mmap2_event__getset, + .tp_repr = (reprfunc )pyrf_mmap2_event__repr, +}; +static const char pyrf_task_event__doc[] =PyDoc_STR ("perf task (fork/exit) event object." );static PyMemberDef pyrf_task_event__members[] = {
@@ -801,6 +942,9 @@ static int pyrf_event__setup_types(void)int err; err =PyType_Ready (&pyrf_mmap_event__type); +if (err <0 ) + goto out; + err =PyType_Ready (&pyrf_mmap2_event__type);if (err <0 ) goto out; err =PyType_Ready (&pyrf_lost_event__type);
@@ -830,6 +974,7 @@ static int pyrf_event__setup_types(void)static PyTypeObject *pyrf_event__type[] = { [PERF_RECORD_MMAP ] = &pyrf_mmap_event__type, + [PERF_RECORD_MMAP2 ] = &pyrf_mmap2_event__type, [PERF_RECORD_LOST ] = &pyrf_lost_event__type, [PERF_RECORD_COMM ] = &pyrf_comm_event__type, [PERF_RECORD_EXIT ] = &pyrf_task_event__type,
@@ -876,6 +1021,9 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evif (copy_size <sizeof (pevent->event))memset ((char *)&pevent->event + copy_size,0 ,sizeof (pevent->event) - copy_size); +if (event->header.type ==PERF_RECORD_MMAP2 ) + pevent->event.mmap2.filename[sizeof (pevent->event.mmap2.filename) -1 ] ='\0' ; +perf_sample__init (&pevent->sample,/*all=*/ true); pevent->al_resolved = false;addr_location__init (&pevent->al);
@@ -3330,6 +3478,9 @@ PyMODINIT_FUNC PyInit_perf(void)Py_INCREF (&pyrf_mmap_event__type);PyModule_AddObject (module,"mmap_event" , (PyObject *)&pyrf_mmap_event__type); +Py_INCREF (&pyrf_mmap2_event__type); +PyModule_AddObject (module,"mmap2_event" , (PyObject *)&pyrf_mmap2_event__type); +Py_INCREF (&pyrf_lost_event__type);PyModule_AddObject (module,"lost_event" , (PyObject *)&pyrf_lost_event__type);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v20 13/21 perf python: Add callchain support
In reply to Ian RogersImplement pyrf_callchain_node and pyrf_callchain types for lazy iteration over callchain frames. Add callchain property to sample_event.
--- tools/perf/util/python.c | 221 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 217 insertions(+), 4 deletions(-)
tools/perf/util/python.c +217 -4
@@ -87,6 +87,8 @@ struct pyrf_event {struct addr_location al;/** @al_resolved: True when machine__resolve been called. */ bool al_resolved; +/** @callchain: Resolved callchain, eagerly computed if requested. */ +PyObject *callchain;/** @event: The underlying perf_event that may be in a file or ring buffer. */ union perf_event event; };
@@ -124,6 +126,7 @@ static void pyrf_event__delete(struct pyrf_event *pevent) {if (pevent->al_resolved)addr_location__exit (&pevent->al); +Py_XDECREF (pevent->callchain);perf_sample__exit (&pevent->sample);Py_TYPE (pevent)->tp_free ((PyObject *)pevent); }
@@ -785,6 +788,144 @@ static PyObject *pyrf_sample_event__insn(PyObject *self, PyObject *args __maybe_ pevent->sample.insn_len); } +struct pyrf_callchain_node { +PyObject_HEAD + u64 ip; +struct map *map; +struct symbol *sym; +}; + +static void pyrf_callchain_node__delete (struct pyrf_callchain_node *pnode) +{ +map__put (pnode->map); +Py_TYPE (pnode)->tp_free ((PyObject *)pnode); +} + +static PyObject *pyrf_callchain_node__get_ip (struct pyrf_callchain_node *pnode, +void *closure__maybe_unused ) +{ +return PyLong_FromUnsignedLongLong (pnode->ip); +} + +static PyObject *pyrf_callchain_node__get_symbol (struct pyrf_callchain_node *pnode, +void *closure__maybe_unused ) +{ +if (pnode->sym) +return PyUnicode_FromString (pnode->sym->name); +return PyUnicode_FromString ("[unknown]" ); +} + +static PyObject *pyrf_callchain_node__get_dso (struct pyrf_callchain_node *pnode, +void *closure__maybe_unused ) +{ +const char *dsoname ="[unknown]" ; + +if (pnode->map) { +struct dso *dso =map__dso (pnode->map); + +if (dso) { +if (symbol_conf.show_kernel_path &&dso__long_name (dso)) + dsoname =dso__long_name (dso); +else + dsoname =dso__name (dso); + } + } +return PyUnicode_FromString (dsoname); +} + +static PyGetSetDef pyrf_callchain_node__getset[] = { + { .name ="ip" , .get = (getter )pyrf_callchain_node__get_ip, }, + { .name ="symbol" , .get = (getter )pyrf_callchain_node__get_symbol, }, + { .name ="dso" , .get = (getter )pyrf_callchain_node__get_dso, }, + { .name =NULL , }, +}; + +static PyTypeObject pyrf_callchain_node__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.callchain_node" , + .tp_basicsize =sizeof (struct pyrf_callchain_node ), + .tp_dealloc = (destructor )pyrf_callchain_node__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc ="perf callchain node object." , + .tp_getset = pyrf_callchain_node__getset, +}; + +struct pyrf_callchain_frame { +u64 ip; +struct map *map; +struct symbol *sym; +}; + +struct pyrf_callchain { +PyObject_HEAD + struct pyrf_callchain_frame *frames; +u64 nr_frames; +}; + +static void pyrf_callchain__delete (struct pyrf_callchain *pchain) +{ +if (pchain->frames) { +for (u64 i =0 ; i < pchain->nr_frames; i++) +map__put (pchain->frames[i].map); +free (pchain->frames); + } +Py_TYPE (pchain)->tp_free ((PyObject *)pchain); +} + +static Py_ssize_t pyrf_callchain__length (PyObject *obj) +{ +struct pyrf_callchain *pchain = (void *)obj; + +return pchain->nr_frames; +} + +static PyObject *pyrf_callchain__item (PyObject *obj,Py_ssize_t i) +{ +struct pyrf_callchain *pchain = (void *)obj; +struct pyrf_callchain_node *pnode; + +if (i <0 || i >= (Py_ssize_t )pchain->nr_frames) { +PyErr_SetString (PyExc_IndexError,"Index out of range" ); +return NULL ; + } + + pnode = PyObject_New(struct pyrf_callchain_node, &pyrf_callchain_node__type); +if (!pnode) +return NULL ; + + pnode->ip = pchain->frames[i].ip; + pnode->map =map__get (pchain->frames[i].map); + pnode->sym = pchain->frames[i].sym; + +return (PyObject *)pnode; +} + +static PySequenceMethods pyrf_callchain__sequence_methods = { + .sq_length = pyrf_callchain__length, + .sq_item = pyrf_callchain__item, +}; + +static PyTypeObject pyrf_callchain__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.callchain" , + .tp_basicsize =sizeof (struct pyrf_callchain ), + .tp_dealloc = (destructor )pyrf_callchain__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc ="perf callchain object." , + .tp_as_sequence = &pyrf_callchain__sequence_methods, +}; + +static PyObject *pyrf_sample_event__get_callchain (PyObject *self,void *closure__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; + +if (!pevent->callchain) + Py_RETURN_NONE; + +Py_INCREF (pevent->callchain); +return pevent->callchain; +} +static PyObject *pyrf_sample_event__getattro (struct pyrf_event *pevent,PyObject *attr_name) {
@@ -799,6 +940,12 @@ pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) }static PyGetSetDef pyrf_sample_event__getset[] = { + { + .name ="callchain" , + .get = pyrf_sample_event__get_callchain, + .set =NULL , + .doc ="event callchain." , + }, { .name ="raw_buf" , .get = (getter )pyrf_sample_event__get_raw_buf,
@@ -968,6 +1115,12 @@ static int pyrf_event__setup_types(void) err =PyType_Ready (&pyrf_context_switch_event__type);if (err <0 ) goto out; + err =PyType_Ready (&pyrf_callchain_node__type); +if (err <0 ) + goto out; + err =PyType_Ready (&pyrf_callchain__type); +if (err <0 ) + goto out; out:return err; }
@@ -987,13 +1140,19 @@ static PyTypeObject *pyrf_event__type[] = { [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, }; -static PyObject *pyrf_event__new (const union perf_event *event,struct evsel *evsel) +static PyObject *pyrf_event__new (const union perf_event *event,struct evsel *evsel, +struct perf_session *session, +struct machine *machine) {struct pyrf_event *pevent; +struct perf_sample *sample;int err;u32 min_size;bool needs_swap; +if (!machine) + machine = session ? &session->machines.host :NULL ; +if (event->header.type >=ARRAY_SIZE (pyrf_event__type) || pyrf_event__type[event->header.type] ==NULL ) { return PyErr_Format(PyExc_TypeError, "Unexpected header type %u",
@@ -1025,6 +1184,7 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev pevent->event.mmap2.filename[sizeof (pevent->event.mmap2.filename) -1 ] ='\0' ;perf_sample__init (&pevent->sample,/*all=*/ true); + pevent->callchain =NULL ; pevent->al_resolved = false;addr_location__init (&pevent->al);
@@ -1042,6 +1202,50 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evreturn PyErr_Format (PyExc_OSError,"perf: can't parse sample, err=%d" , err); } + sample = &pevent->sample; +if (machine && sample->callchain) { +struct addr_location al; +struct callchain_cursor *cursor; +u64 i; +struct pyrf_callchain *pchain; + +addr_location__init (&al); +if (machine__resolve (machine, &al, sample) >=0 ) { + cursor =get_tls_callchain_cursor (); +if (thread__resolve_callchain (al.thread, cursor, sample, +NULL ,NULL ,PERF_MAX_STACK_DEPTH ) ==0 ) { +callchain_cursor_commit (cursor); + + pchain = PyObject_New(struct pyrf_callchain, &pyrf_callchain__type); +if (!pchain) { +addr_location__exit (&al); +Py_DECREF (pevent); +return NULL ; + } + pchain->nr_frames = cursor->nr; + pchain->frames =calloc (pchain->nr_frames, +sizeof (*pchain->frames)); +if (!pchain->frames) { +Py_DECREF (pchain); +addr_location__exit (&al); +Py_DECREF (pevent); +return PyErr_NoMemory (); + } +struct callchain_cursor_node *node; + +for (i =0 ; i < pchain->nr_frames; i++) { + node =callchain_cursor_current (cursor); + pchain->frames[i].ip = node->ip; + pchain->frames[i].map = +map__get (node->ms.map); + pchain->frames[i].sym = node->ms.sym; +callchain_cursor_advance (cursor); + } + pevent->callchain = (PyObject *)pchain; + } +addr_location__exit (&al); + } + }return (PyObject *)pevent; }
@@ -2417,7 +2621,7 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,perf_mmap__consume (&md->core); Py_RETURN_NONE; } - pyevent =pyrf_event__new (event, evsel); + pyevent =pyrf_event__new (event, evsel,evlist__session (evlist),/*machine=*/ NULL );perf_mmap__consume (&md->core);if (pyevent ==NULL )return PyErr_Occurred () ?NULL :PyErr_NoMemory ();
@@ -3185,10 +3389,10 @@ struct pyrf_session {static int pyrf_session_tool__sample (const struct perf_tool *tool,union perf_event *event,struct perf_sample *sample, -struct machine *machine__maybe_unused ) +struct machine *machine) { struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); -PyObject *pyevent =pyrf_event__new (event, sample->evsel); +PyObject *pyevent =pyrf_event__new (event, sample->evsel, psession->session, machine);PyObject *ret;if (pyevent ==NULL )
@@ -3296,6 +3500,9 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject } psession->session = session; + symbol_conf.use_callchain = true; + symbol_conf.show_kernel_path = true; + symbol_conf.inline_name = false;if (symbol__init (perf_session__env (session)) <0 ) {PyErr_SetString (PyExc_OSError,"perf: symbol__init failed" ); goto err_out;
@@ -3475,6 +3682,12 @@ PyMODINIT_FUNC PyInit_perf(void)Py_INCREF (&pyrf_thread__type);PyModule_AddObject (module,"thread" , (PyObject *)&pyrf_thread__type); +Py_INCREF (&pyrf_callchain__type); +PyModule_AddObject (module,"callchain" , (PyObject *)&pyrf_callchain__type); + +Py_INCREF (&pyrf_callchain_node__type); +PyModule_AddObject (module,"callchain_node" , (PyObject *)&pyrf_callchain_node__type); +Py_INCREF (&pyrf_mmap_event__type);PyModule_AddObject (module,"mmap_event" , (PyObject *)&pyrf_mmap_event__type);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v20 14/21 perf python: Extend API for stat events in python.c
In reply to Ian RogersAdd stat information to the session. Add call backs for stat events.
--- tools/perf/util/python.c | 189 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 184 insertions(+), 5 deletions(-)
tools/perf/util/python.c +184 -5
@@ -458,6 +458,77 @@ static PyTypeObject pyrf_lost_event__type = { .tp_repr = (reprfunc)pyrf_lost_event__repr, }; +static const char pyrf_stat_event__doc[] =PyDoc_STR ("perf stat event object." ); + +static PyMemberDef pyrf_stat_event__members[] = { + sample_members +member_def (perf_event_header, type,T_UINT ,"event type" ), +member_def (perf_record_stat, id,T_ULONGLONG ,"event id" ), +member_def (perf_record_stat, cpu,T_UINT ,"event cpu" ), +member_def (perf_record_stat, thread,T_UINT ,"event thread" ), +member_def (perf_record_stat, val,T_ULONGLONG ,"counter value" ), +member_def (perf_record_stat, ena,T_ULONGLONG ,"enabled time" ), +member_def (perf_record_stat, run,T_ULONGLONG ,"running time" ), + { .name =NULL , }, +}; + +static PyObject *pyrf_stat_event__repr (const struct pyrf_event *pevent) +{ +return PyUnicode_FromFormat ( +"{ type: stat, id: %llu, cpu: %u, thread: %u, val: %llu, ena: %llu, run: %llu }" , + pevent->event.stat.id, + pevent->event.stat.cpu, + pevent->event.stat.thread, + pevent->event.stat.val, + pevent->event.stat.ena, + pevent->event.stat.run); +} + +static PyTypeObject pyrf_stat_event__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.stat_event" , + .tp_basicsize =sizeof (struct pyrf_event ), + .tp_new = PyType_GenericNew, + .tp_dealloc = (destructor )pyrf_event__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = pyrf_stat_event__doc, + .tp_members = pyrf_stat_event__members, + .tp_getset = pyrf_event__getset, + .tp_repr = (reprfunc )pyrf_stat_event__repr, +}; + +static const char pyrf_stat_round_event__doc[] =PyDoc_STR ("perf stat round event object." ); + +static PyMemberDef pyrf_stat_round_event__members[] = { + sample_members +member_def (perf_event_header, type,T_UINT ,"event type" ), + { .name ="stat_round_type" , .type =T_ULONGLONG , + .offset = offsetof(struct pyrf_event , event) + offsetof(struct perf_record_stat_round , type), + .doc ="round type" }, +member_def (perf_record_stat_round, time,T_ULONGLONG ,"round time" ), + { .name =NULL , }, +}; + +static PyObject *pyrf_stat_round_event__repr (const struct pyrf_event *pevent) +{ +return PyUnicode_FromFormat ("{ type: stat_round, type: %llu, time: %llu }" , + pevent->event.stat_round.type, + pevent->event.stat_round.time); +} + +static PyTypeObject pyrf_stat_round_event__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.stat_round_event" , + .tp_basicsize =sizeof (struct pyrf_event ), + .tp_new = PyType_GenericNew, + .tp_dealloc = (destructor )pyrf_event__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = pyrf_stat_round_event__doc, + .tp_members = pyrf_stat_round_event__members, + .tp_getset = pyrf_event__getset, + .tp_repr = (reprfunc )pyrf_stat_round_event__repr, +}; +static const char pyrf_read_event__doc[] =PyDoc_STR ("perf read event object." );static PyMemberDef pyrf_read_event__members[] = {
@@ -1113,6 +1184,12 @@ static int pyrf_event__setup_types(void)if (err <0 ) goto out; err =PyType_Ready (&pyrf_context_switch_event__type); +if (err <0 ) + goto out; + err =PyType_Ready (&pyrf_stat_event__type); +if (err <0 ) + goto out; + err =PyType_Ready (&pyrf_stat_round_event__type);if (err <0 ) goto out; err =PyType_Ready (&pyrf_callchain_node__type);
@@ -1138,6 +1215,8 @@ static PyTypeObject *pyrf_event__type[] = { [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type, [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type, [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, + [PERF_RECORD_STAT] = &pyrf_stat_event__type, + [PERF_RECORD_STAT_ROUND] = &pyrf_stat_round_event__type, }; static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evsel,
@@ -2132,7 +2211,45 @@ static PyObject *pyrf_evsel__get_attr_wakeup_events(PyObject *self, void *closurreturn PyLong_FromUnsignedLong (pevsel->evsel->core.attr.wakeup_events); } +static PyObject *pyrf_evsel__get_ids (struct pyrf_evsel *pevsel,void *closure__maybe_unused ) +{ +struct evsel *evsel; +PyObject *list; + +CHECK_INITIALIZED (pevsel->evsel,"evsel" ); + + evsel = pevsel->evsel; + list =PyList_New (0 ); + +if (!list) +return NULL ; + +for (u32 i =0 ; i < evsel->core.ids; i++) { +PyObject *id =PyLong_FromUnsignedLongLong (evsel->core.id[i]); +int ret; + +if (!id) { +Py_DECREF (list); +return NULL ; + } + ret =PyList_Append (list, id); +Py_DECREF (id); +if (ret <0 ) { +Py_DECREF (list); +return NULL ; + } + } + +return list; +} +static PyGetSetDef pyrf_evsel__getset[] = { + { + .name ="ids" , + .get = (getter )pyrf_evsel__get_ids, + .set =NULL , + .doc ="event IDs." , + }, { .name ="tracking" , .get = pyrf_evsel__get_tracking,
@@ -2985,6 +3102,8 @@ static const struct perf_constant perf__constants[] = {PERF_CONST (RECORD_LOST_SAMPLES ),PERF_CONST (RECORD_SWITCH ),PERF_CONST (RECORD_SWITCH_CPU_WIDE ), +PERF_CONST (RECORD_STAT ), +PERF_CONST (RECORD_STAT_ROUND ),PERF_CONST (RECORD_MISC_SWITCH_OUT ), { .name = NULL, },
@@ -3384,6 +3503,7 @@ struct pyrf_session {struct perf_tool tool;struct pyrf_data *pdata;PyObject *sample; +PyObject *stat; }; static int pyrf_session_tool__sample(const struct perf_tool *tool,
@@ -3408,6 +3528,52 @@ static int pyrf_session_tool__sample(const struct perf_tool *tool,return 0 ; } +static int pyrf_session_tool__stat (const struct perf_tool *tool, +struct perf_session *session, +union perf_event *event) +{ + struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); +struct evsel *evsel =evlist__id2evsel (session->evlist, event->stat.id); +PyObject *pyevent =pyrf_event__new (event,/*evsel=*/ NULL , psession->session, +/*machine=*/ NULL ); +const char *name = evsel ?evsel__name (evsel) :"unknown" ; +PyObject *ret; + +if (pyevent ==NULL ) +return -ENOMEM ; + + ret =PyObject_CallFunction (psession->stat,"Oz" , pyevent, name); +if (!ret) { +Py_DECREF (pyevent); +return -1 ; + } +Py_DECREF (ret); +Py_DECREF (pyevent); +return 0 ; +} + +static int pyrf_session_tool__stat_round (const struct perf_tool *tool, +struct perf_session *session__maybe_unused , +union perf_event *event) +{ + struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); +PyObject *pyevent =pyrf_event__new (event,/*evsel=*/ NULL , psession->session, +/*machine=*/ NULL ); +PyObject *ret; + +if (pyevent ==NULL ) +return -ENOMEM ; + + ret =PyObject_CallFunction (psession->stat,"Oz" , pyevent,NULL ); +if (!ret) { +Py_DECREF (pyevent); +return -1 ; + } +Py_DECREF (ret); +Py_DECREF (pyevent); +return 0 ; +} +static PyObject *pyrf_session__find_thread (struct pyrf_session *psession,PyObject *args) {struct machine *machine;
@@ -3441,13 +3607,13 @@ static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObjestatic PyObject *pyrf_session__new (PyTypeObject *type,PyObject *args,PyObject *kwargs) {struct pyrf_data *pdata; -PyObject *sample =NULL ; -static char *kwlist[] = {"data" ,"sample" ,NULL }; +PyObject *sample =NULL , *stat =NULL ; +static char *kwlist[] = {"data" ,"sample" ,"stat" ,NULL };struct pyrf_session *psession;struct perf_session *session; -if (!PyArg_ParseTupleAndKeywords (args, kwargs,"O!|O" , kwlist, &pyrf_data__type, &pdata, - &sample)) +if (!PyArg_ParseTupleAndKeywords (args, kwargs,"O!|OO" , kwlist, &pyrf_data__type, &pdata, + &sample, &stat))return NULL ; psession = PyObject_New(struct pyrf_session, type);
@@ -3456,6 +3622,7 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject psession->session =NULL ; psession->sample =NULL ; + psession->stat =NULL ; psession->pdata =NULL ;Py_INCREF (pdata);
@@ -3478,8 +3645,13 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject }while (0 )ADD_TOOL (sample); +ADD_TOOL (stat);#undef ADD_TOOL +if (stat) + psession->tool.stat_round = pyrf_session_tool__stat_round; + + psession->tool.comm = perf_event__process_comm; psession->tool.mmap = perf_event__process_mmap; psession->tool.mmap2 = perf_event__process_mmap2;
@@ -3492,7 +3664,7 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject psession->tool.build_id = perf_event__process_build_id; psession->tool.attr = perf_event__process_attr; psession->tool.feature = perf_event__process_feature; - psession->tool.stat = perf_event__process_stat_event; + session =perf_session__new (&pdata->data, &psession->tool);if (IS_ERR (session)) {PyErr_Format (PyExc_IOError,"failed to create session: %ld" ,PTR_ERR (session));
@@ -3521,6 +3693,7 @@ static void pyrf_session__delete(struct pyrf_session *psession)perf_session__delete (psession->session);Py_XDECREF (psession->pdata);Py_XDECREF (psession->sample); +Py_XDECREF (psession->stat);Py_TYPE (psession)->tp_free ((PyObject *)psession); }
@@ -3718,6 +3891,12 @@ PyMODINIT_FUNC PyInit_perf(void)Py_INCREF (&pyrf_context_switch_event__type);PyModule_AddObject (module,"switch_event" , (PyObject *)&pyrf_context_switch_event__type); +Py_INCREF (&pyrf_stat_event__type); +PyModule_AddObject (module,"stat_event" , (PyObject *)&pyrf_stat_event__type); + +Py_INCREF (&pyrf_stat_round_event__type); +PyModule_AddObject (module,"stat_round_event" , (PyObject *)&pyrf_stat_round_event__type); +Py_INCREF (&pyrf_thread_map__type);PyModule_AddObject (module,"thread_map" , (PyObject *)&pyrf_thread_map__type);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v20 15/21 perf python: Expose brstack in sample event
In reply to Ian RogersImplement pyrf_branch_entry and pyrf_branch_stack for lazy iteration over branch stack entries.
--- tools/perf/util/python.c | 198 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+)
tools/perf/util/python.c +198 -0
@@ -22,6 +22,7 @@#include "debug.h" #include "dso.h" #include "event.h" +#include "branch.h" #include "evlist.h" #include "evsel.h" #include "expr.h"
@@ -89,6 +90,8 @@ struct pyrf_event {bool al_resolved;/** @callchain: Resolved callchain, eagerly computed if requested. */ PyObject *callchain; +/** @brstack: Resolved branch stack, eagerly computed if requested. */ +PyObject *brstack;/** @event: The underlying perf_event that may be in a file or ring buffer. */ union perf_event event; };
@@ -127,6 +130,7 @@ static void pyrf_event__delete(struct pyrf_event *pevent)if (pevent->al_resolved)addr_location__exit (&pevent->al);Py_XDECREF (pevent->callchain); +Py_XDECREF (pevent->brstack);perf_sample__exit (&pevent->sample);Py_TYPE (pevent)->tp_free ((PyObject *)pevent); }
@@ -997,6 +1001,154 @@ static PyObject *pyrf_sample_event__get_callchain(PyObject *self, void *closurereturn pevent->callchain; } +struct pyrf_branch_entry { +PyObject_HEAD + u64 from; +u64 to; +struct branch_flags flags; +}; + +static void pyrf_branch_entry__delete (struct pyrf_branch_entry *pentry) +{ +Py_TYPE (pentry)->tp_free ((PyObject *)pentry); +} + +static PyObject *pyrf_branch_entry__get_from (struct pyrf_branch_entry *pentry, +void *closure__maybe_unused ) +{ +return PyLong_FromUnsignedLongLong (pentry->from); +} + +static PyObject *pyrf_branch_entry__get_to (struct pyrf_branch_entry *pentry, +void *closure__maybe_unused ) +{ +return PyLong_FromUnsignedLongLong (pentry->to); +} + +static PyObject *pyrf_branch_entry__get_mispred (struct pyrf_branch_entry *pentry, +void *closure__maybe_unused ) +{ +return PyBool_FromLong (pentry->flags.mispred); +} + +static PyObject *pyrf_branch_entry__get_predicted (struct pyrf_branch_entry *pentry, +void *closure__maybe_unused ) +{ +return PyBool_FromLong (pentry->flags.predicted); +} + +static PyObject *pyrf_branch_entry__get_in_tx (struct pyrf_branch_entry *pentry, +void *closure__maybe_unused ) +{ +return PyBool_FromLong (pentry->flags.in_tx); +} + +static PyObject *pyrf_branch_entry__get_abort (struct pyrf_branch_entry *pentry, +void *closure__maybe_unused ) +{ +return PyBool_FromLong (pentry->flags.abort); +} + +static PyObject *pyrf_branch_entry__get_cycles (struct pyrf_branch_entry *pentry, +void *closure__maybe_unused ) +{ +return PyLong_FromUnsignedLongLong (pentry->flags.cycles); +} + +static PyObject *pyrf_branch_entry__get_type (struct pyrf_branch_entry *pentry, +void *closure__maybe_unused ) +{ +return PyLong_FromUnsignedLongLong ((unsigned long long )pentry->flags.type); +} + +static PyGetSetDef pyrf_branch_entry__getset[] = { + { .name ="from_ip" , .get = (getter )pyrf_branch_entry__get_from, }, + { .name ="to_ip" , .get = (getter )pyrf_branch_entry__get_to, }, + { .name ="mispred" , .get = (getter )pyrf_branch_entry__get_mispred, }, + { .name ="predicted" , .get = (getter )pyrf_branch_entry__get_predicted, }, + { .name ="in_tx" , .get = (getter )pyrf_branch_entry__get_in_tx, }, + { .name ="abort" , .get = (getter )pyrf_branch_entry__get_abort, }, + { .name ="cycles" , .get = (getter )pyrf_branch_entry__get_cycles, }, + { .name ="type" , .get = (getter )pyrf_branch_entry__get_type, }, + { .name =NULL , }, +}; + +static PyTypeObject pyrf_branch_entry__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.branch_entry" , + .tp_basicsize =sizeof (struct pyrf_branch_entry ), + .tp_dealloc = (destructor )pyrf_branch_entry__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc ="perf branch entry object." , + .tp_getset = pyrf_branch_entry__getset, +}; + +struct pyrf_branch_stack { +PyObject_HEAD + struct branch_entry *entries; +u64 nr; +}; + +static void pyrf_branch_stack__delete (struct pyrf_branch_stack *pstack) +{ +free (pstack->entries); +Py_TYPE (pstack)->tp_free ((PyObject *)pstack); +} + +static Py_ssize_t pyrf_branch_stack__length (PyObject *obj) +{ +struct pyrf_branch_stack *pstack = (void *)obj; + +return pstack->nr; +} + +static PyObject *pyrf_branch_stack__item (PyObject *obj,Py_ssize_t i) +{ +struct pyrf_branch_stack *pstack = (void *)obj; +struct pyrf_branch_entry *pentry; + +if (i <0 || i >= (Py_ssize_t )pstack->nr) { +PyErr_SetString (PyExc_IndexError,"Index out of range" ); +return NULL ; + } + + pentry = PyObject_New(struct pyrf_branch_entry, &pyrf_branch_entry__type); +if (!pentry) +return NULL ; + + pentry->from = pstack->entries[i].from; + pentry->to = pstack->entries[i].to; + pentry->flags = pstack->entries[i].flags; + +return (PyObject *)pentry; +} + +static PySequenceMethods pyrf_branch_stack__sequence_methods = { + .sq_length = pyrf_branch_stack__length, + .sq_item = pyrf_branch_stack__item, +}; + +static PyTypeObject pyrf_branch_stack__type = { +PyVarObject_HEAD_INIT (NULL ,0 ) + .tp_name ="perf.branch_stack" , + .tp_basicsize =sizeof (struct pyrf_branch_stack ), + .tp_dealloc = (destructor )pyrf_branch_stack__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc ="perf branch stack object." , + .tp_as_sequence = &pyrf_branch_stack__sequence_methods, +}; + +static PyObject *pyrf_sample_event__get_brstack (PyObject *self,void *closure__maybe_unused ) +{ +struct pyrf_event *pevent = (void *)self; + +if (!pevent->brstack) + Py_RETURN_NONE; + +Py_INCREF (pevent->brstack); +return pevent->brstack; +} +static PyObject *pyrf_sample_event__getattro (struct pyrf_event *pevent,PyObject *attr_name) {
@@ -1017,6 +1169,12 @@ static PyGetSetDef pyrf_sample_event__getset[] = { .set = NULL, .doc = "event callchain.", }, + { + .name = "brstack", + .get = pyrf_sample_event__get_brstack, + .set = NULL, + .doc = "event branch stack.", + }, { .name = "raw_buf", .get = (getter)pyrf_sample_event__get_raw_buf,
@@ -1198,6 +1356,12 @@ static int pyrf_event__setup_types(void) err =PyType_Ready (&pyrf_callchain__type);if (err <0 ) goto out; + err =PyType_Ready (&pyrf_branch_entry__type); +if (err <0 ) + goto out; + err =PyType_Ready (&pyrf_branch_stack__type); +if (err <0 ) + goto out; out:return err; }
@@ -1264,6 +1428,7 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evperf_sample__init (&pevent->sample,/*all=*/ true); pevent->callchain =NULL ; + pevent->brstack =NULL ; pevent->al_resolved = false;addr_location__init (&pevent->al);
@@ -1325,6 +1490,27 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evaddr_location__exit (&al); } } +if (sample->branch_stack) { +struct branch_stack *bs = sample->branch_stack; +struct branch_entry *entries =perf_sample__branch_entries (sample); +struct pyrf_branch_stack *pstack; + + pstack = PyObject_New(struct pyrf_branch_stack, &pyrf_branch_stack__type); +if (!pstack) { +Py_DECREF (pevent); +return NULL ; + } + pstack->nr = bs->nr; + pstack->entries =calloc (bs->nr,sizeof (struct branch_entry )); +if (!pstack->entries) { +Py_DECREF (pstack); +Py_DECREF (pevent); +return PyErr_NoMemory (); + } +memcpy (pstack->entries, entries, + bs->nr *sizeof (struct branch_entry )); + pevent->brstack = (PyObject *)pstack; + }return (PyObject *)pevent; }
@@ -3912,6 +4098,18 @@ PyMODINIT_FUNC PyInit_perf(void)Py_INCREF (&pyrf_session__type);PyModule_AddObject (module,"session" , (PyObject *)&pyrf_session__type); +Py_INCREF (&pyrf_branch_entry__type); +if (PyModule_AddObject (module,"branch_entry" , (PyObject *)&pyrf_branch_entry__type) <0 ) { +Py_DECREF (&pyrf_branch_entry__type); + goto error; + } + +Py_INCREF (&pyrf_branch_stack__type); +if (PyModule_AddObject (module,"branch_stack" , (PyObject *)&pyrf_branch_stack__type) <0 ) { +Py_DECREF (&pyrf_branch_stack__type); + goto error; + } + dict =PyModule_GetDict (module);if (dict ==NULL ) goto error;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v20 16/21 perf python: Add syscall name/id to convert syscall number and name
In reply to Ian RogersUse perf's syscalltbl support to convert syscall number to name assuming the number is for the host machine. This avoids python libaudit support as tools/perf/scripts/python/syscall-counts.py requires.
--- tools/perf/util/Build | 1 - tools/perf/util/python.c | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-)
tools/perf/util/Build +0 -1
@@ -75,7 +75,6 @@ perf-util-y += sample.o perf-util-y += sample-raw.o perf-util-y += s390-sample-raw.o perf-util-y += amd-sample-raw.o - perf-util-y += ordered-events.o perf-util-y += namespaces.o perf-util-y += comm.o
tools/perf/util/python.c +48 -0
72 lines not on this page · raw message
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v20 17/21 perf python: Add config file access
In reply to Ian RogersAdd perf.config_get(name) to expose the perf configuration system.
--- tools/perf/util/python.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
tools/perf/util/python.c +20 -0
@@ -17,6 +17,7 @@#include "build-id.h" #include "callchain.h" #include "comm.h" +#include "config.h" #include "counts.h" #include "data.h" #include "debug.h"
@@ -3993,7 +3994,26 @@ static PyObject *pyrf__syscall_id(PyObject *self, PyObject *args, PyObject *kwarreturn PyLong_FromLong (id); } +static PyObject *pyrf__config_get (PyObject *self,PyObject *args) +{ +const char *config_name, *val; + +if (!PyArg_ParseTuple (args,"s" , &config_name)) +return NULL ; + + val =perf_config_get (config_name); +if (!val) + Py_RETURN_NONE; +return PyUnicode_FromString (val); +} +static PyMethodDef perf__methods[] = { + { + .ml_name ="config_get" , + .ml_meth = (PyCFunction ) pyrf__config_get, + .ml_flags =METH_VARARGS , + .ml_doc =PyDoc_STR ("Get a perf config value." ) + }, { .ml_name ="metrics" , .ml_meth = (PyCFunction ) pyrf__metrics,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v20 18/21 perf python: Handle Py_None for thread and cpu maps
In reply to Ian RogersThe python stubs allow passing None for threads and cpus to the perf.parse_events() and perf.parse_metrics() bindings. However, PyArg_ParseTuple parses None into a Py_None object, which is not a NULL pointer. Because the C code lacked an explicit check for Py_None, it would cast Py_None to a pyrf_thread_map/pyrf_cpu_map struct pointer and dereference it, causing a memory corruption crash.
Fix this pre-existing issue by explicitly checking for Py_None alongside NULL in pyrf__parse_events, pyrf__parse_metrics, and pyrf_evsel__open.
--- tools/perf/util/python.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
tools/perf/util/python.c +10 -6
@@ -2091,10 +2091,10 @@ static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, &pcpus, &pthreads, &group, &inherit))return NULL ; -if (pthreads !=NULL ) +if (pthreads !=NULL && pthreads != Py_None) threads = ((struct pyrf_thread_map *)pthreads)->threads; -if (pcpus !=NULL ) +if (pcpus !=NULL && pcpus != Py_None) cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; evsel->core.attr.inherit = inherit;
@@ -3341,8 +3341,10 @@ static PyObject *pyrf__parse_events(PyObject *self, PyObject *args)return NULL ; } - threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads :NULL ; - cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus :NULL ; + threads = (pthreads && pthreads != Py_None) ? + ((struct pyrf_thread_map *)pthreads)->threads :NULL ; + cpus = (pcpus && pcpus != Py_None) ? + ((struct pyrf_cpu_map *)pcpus)->cpus :NULL ;parse_events_error__init (&err);perf_evlist__set_maps (evlist__core (evlist), cpus, threads);
@@ -3375,8 +3377,10 @@ static PyObject *pyrf__parse_metrics(PyObject *self, PyObject *args)return NULL ; } - threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads :NULL ; - cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus :NULL ; + threads = (pthreads && pthreads != Py_None) ? + ((struct pyrf_thread_map *)pthreads)->threads :NULL ; + cpus = (pcpus && pcpus != Py_None) ? + ((struct pyrf_cpu_map *)pcpus)->cpus :NULL ;perf_evlist__set_maps (evlist__core (evlist), cpus, threads); ret = metricgroup__parse_groups(evlist, pmu ?: "all", input,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
[email protected]
AI reviewRe: [PATCH v20 18/21] perf python: Handle Py_None for thread and cpu maps
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing PyObject_TypeCheck validation in pyrf_evsel__open allows arbitrary Python objects to be unsafely cast to internal C structs, leading to type confusion and memory corruption. --
commit d7732c71209049d1fdcc532224b91ad794512101
Author: Ian Rogers <[email protected]>
perf python: Handle Py_None for thread and cpu maps
This commit fixes memory corruption crashes by explicitly checking for Py_None before casting python objects to internal thread and cpu map structures. Previously, parsing None into Py_None bypassed NULL checks and resulted in invalid pointer dereferences.
Quoted 4 lines
tools/perf/util/python.c static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, · line 2098
Quoted 10 lines
tools/perf/util/python.c +2
-2@@ -2091,10 +2091,10 @@ static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, &pcpus, &pthreads, &group, &inherit))return NULL ; -if (pthreads !=NULL ) +if (pthreads !=NULL && pthreads != Py_None) threads = ((struct pyrf_thread_map *)pthreads)->threads; -if (pcpus !=NULL ) +if (pcpus !=NULL && pcpus != Py_None) cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
[Severity: High]
This is a pre-existing issue, but missing PyObject_TypeCheck validation here allows arbitrary Python objects to be unsafely cast to internal C structs.
Since pcpus and pthreads are parsed as generic objects in pyrf_evsel__open(), what happens if a user passes an incompatible type like an integer or a string instead of Py_None?
It looks like the extension will blindly cast the generic PyObject to a struct pyrf_cpu_map or struct pyrf_thread_map and dereference it to read the cpus or threads field.
Could this lead to type confusion and memory corruption if we try to access arbitrary memory offsets on the wrong python object type?
I noticed that pyrf__parse_events() and pyrf__parse_metrics() both use PyObject_TypeCheck() to validate the types before casting. Would it make sense to add those same type checks here in pyrf_evsel__open()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
Ian Rogers
reviewerPATCH v20 19/21 perf python: Add type checking for parse_events/parse_metrics
In reply to Ian RogersThe threads and cpus parameters in parse_events and parse_metrics are parsed with the 'O' format specifier but blindly casted in the C extension. If a user passes an invalid object type, this leads to memory corruption when dereferencing the expected struct.
Add runtime PyObject_TypeCheck validations in python.c to safely raise a TypeError if an invalid object is passed.
--- tools/perf/util/python.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
tools/perf/util/python.c +28 -0
@@ -3341,6 +3341,20 @@ static PyObject *pyrf__parse_events(PyObject *self, PyObject *args)return NULL ; } +if (pthreads && pthreads != Py_None && + !PyObject_TypeCheck (pthreads, &pyrf_thread_map__type)) { +PyErr_SetString (PyExc_TypeError,"threads must be a perf.thread_map or None" ); +evlist__put (evlist); +return NULL ; + } + +if (pcpus && pcpus != Py_None && + !PyObject_TypeCheck (pcpus, &pyrf_cpu_map__type)) { +PyErr_SetString (PyExc_TypeError,"cpus must be a perf.cpu_map or None" ); +evlist__put (evlist); +return NULL ; + } + threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads :NULL ; cpus = (pcpus && pcpus != Py_None) ?
@@ -3377,6 +3391,20 @@ static PyObject *pyrf__parse_metrics(PyObject *self, PyObject *args)return NULL ; } +if (pthreads && pthreads != Py_None && + !PyObject_TypeCheck (pthreads, &pyrf_thread_map__type)) { +PyErr_SetString (PyExc_TypeError,"threads must be a perf.thread_map or None" ); +evlist__put (evlist); +return NULL ; + } + +if (pcpus && pcpus != Py_None && + !PyObject_TypeCheck (pcpus, &pyrf_cpu_map__type)) { +PyErr_SetString (PyExc_TypeError,"cpus must be a perf.cpu_map or None" ); +evlist__put (evlist); +return NULL ; + } + threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads :NULL ; cpus = (pcpus && pcpus != Py_None) ?
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
[email protected]
AI reviewRe: [PATCH v20 19/21] perf python: Add type checking for parse_events/parse_metrics
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing type checks for Python objects blindly cast to internal map structs in pyrf_evsel__open. --
commit fe6a29ea73634479b123e57801dc3fa83e09bec2
Author: Ian Rogers <[email protected]>
perf python: Add type checking for parse_events/parse_metrics
The threads and cpus parameters in parse_events and parse_metrics are parsed with the 'O' format specifier but blindly casted in the C extension. If a user passes an invalid object type, this leads to memory corruption when dereferencing the expected struct.
Quoted 14 lines
tools/perf/util/python.c +6
-0@@ -3341,6 +3341,20 @@ static PyObject *pyrf__parse_events(PyObject *self, PyObject *args)return NULL ; } +if (pthreads && pthreads != Py_None && + !PyObject_TypeCheck (pthreads, &pyrf_thread_map__type)) { +PyErr_SetString (PyExc_TypeError,"threads must be a perf.thread_map or None" ); +evlist__put (evlist); +return NULL ; + }
tools/perf/util/python.c static PyObject *pyrf__parse_events(PyObject *self, PyObject *args) · line 3359
Quoted 10 lines
tools/perf/util/python.c +8
-0+ +if (pcpus && pcpus != Py_None && + !PyObject_TypeCheck (pcpus, &pyrf_cpu_map__type)) { +PyErr_SetString (PyExc_TypeError,"cpus must be a perf.cpu_map or None" ); +evlist__put (evlist); +return NULL ; + } + threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads :NULL ;
[Severity: High]
This isn't a bug introduced by this patch, but does pyrf_evsel__open() need these same validations?
Looking at pyrf_evsel__open() in tools/perf/util/python.c, it appears to parse the exact same cpus and threads arguments without checking their type before casting:
tools/perf/util/python.c:pyrf_evsel__open() {
...
if (pthreads != NULL && pthreads != Py_None)
threads = ((struct pyrf_thread_map *)pthreads)->threads;
if (pcpus != NULL && pcpus != Py_None)
cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
...
}Could a user passing an invalid object type to evsel.open(cpus=...) trigger the same memory corruption when dereferencing the expected struct?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
Ian Rogers
reviewerPATCH v20 20/21 perf python: Add perf.pyi stubs file
In reply to Ian RogersAdd Python type stubs for the perf module to improve IDE support and static analysis. Includes docstrings for classes, methods, and constants derived from C source and JSON definitions.
--- tools/perf/Makefile.perf | 7 +- tools/perf/python/perf.pyi | 672 +++++++++++++++++++++++++++++++++++++ tools/perf/util/setup.py | 5 + 3 files changed, 682 insertions(+), 2 deletions(-) create mode 100644 tools/perf/python/perf.pyi
tools/perf/Makefile.perf +5 -2
@@ -517,12 +517,15 @@ all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) # Create python binding output directory if not already present $(shell [ -d '$(OUTPUT)python' ] || mkdir -p '$(OUTPUT)python') -$(OUTPUT)python/perf$(PYTHON_EXTENSION_SUFFIX): util/python.c util/setup.py $(PERFLIBS_PY) +$(OUTPUT)python/perf$(PYTHON_EXTENSION_SUFFIX): util/python.c util/setup.py python/perf.pyi $(PERFLIBS_PY) $(QUIET_GEN)LDSHARED="$(CC) -pthread -shared" \ CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS) $(LIBS_PY)' \ $(PYTHON_WORD) util/setup.py \ $(python_setup_quiet) build_ext; \ - cp $(PYTHON_EXTBUILD_LIB)perf*.so $(OUTPUT)python/ + cp $(PYTHON_EXTBUILD_LIB)perf*.so $(OUTPUT)python/; \ + if [ "$(abspath python/perf.pyi)" != "$(abspath $(OUTPUT)python/perf.pyi)" ]; then \ + cp python/perf.pyi $(OUTPUT)python/; \ + fi python_perf_target: @echo "Target is: $(OUTPUT)python/perf$(PYTHON_EXTENSION_SUFFIX)"
tools/perf/python/perf.pyi new file +672 -0
672 lines not on this page · raw message
tools/perf/util/setup.py +5 -0
@@ -2,6 +2,7 @@ from os import getenv, path from subprocess import Popen, PIPE from re import sub import shlex +import shutil cc = getenv("CC") assert cc, "Environment variable CC not set"
@@ -73,6 +74,10 @@ class install_lib(_install_lib): _install_lib.finalize_options(self) self.build_dir = build_lib + def run(self): + _install_lib.run(self) + shutil.copy2(f'{src_perf}/python/perf.pyi', self.install_dir) + # switch off several checks (need to be at the end of cflags list) extra_cflags = ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls' ]
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
[email protected]
AI reviewRe: [PATCH v20 20/21] perf python: Add perf.pyi stubs file
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Incorrect type definitions in newly added Python type stubs (perf.pyi) --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incorrect type definitions in newly added Python type stubs (perf.pyi) --
commit 291ae14f89c5a2b4229164393082ddfb015deb9e
Author: Ian Rogers <[email protected]>
perf python: Add perf.pyi stubs file
This commit adds Python type stubs for the perf module to improve IDE support and static analysis. It includes docstrings for classes, methods, and constants derived from C source and JSON definitions.
Quoted 5 lines
tools/perf/python/perf.pyi new file +0
-0
[ ... ]
Quoted 2 lines
tools/perf/python/perf.pyi new file +2
-0+class sample_event(_sample_members): + """Represents a sample event from perf."""
[ ... ]
tools/perf/python/perf.pyi line 202
Quoted 4 lines
tools/perf/python/perf.pyi new file +4
-0+ raw_buf: bytes + dso: str + dso_long_name: str + dso_bid: Optional[bytes]
^^^^^^^^^^^^^^^
[Severity: Medium]
Does the C implementation return bytes for dso_bid? Looking at the C code in tools/perf/util/python.c, it appears to return an Optional[str] via PyUnicode_FromString after formatting the build ID.
Quoted 1 line
+
map_start: int
[ ... ]
tools/perf/python/perf.pyi line 211
Quoted 4 lines
tools/perf/python/perf.pyi new file +4
-0+ sym_end: int + brstack: Optional['branch_stack'] + callchain: Optional['callchain'] + def srccode(self) -> str: ...
^^^^^^^^^^^^^^^^^^^^^
[Severity: Medium]
Does this correctly match the C implementation of srccode()? The extension appears to accept an optional addr integer and returns a tuple of (srcfile, line, srccode) rather than a str.
Quoted 1 line
+ def insn(self) -> str: ...
^^^^^^^^^^^^^^^^
[Severity: Medium]
Does insn() return a str? The underlying C code seems to return an Optional[bytes] object using PyBytes_FromStringAndSize.
Quoted 1 line
+ def
__getattr__(self, name: str)-> Any: ...
[ ... ]
Quoted 1 line
+class evlist:
[ ... ]
tools/perf/python/perf.pyi line 378
Quoted 11 lines
tools/perf/python/perf.pyi new file +11
-0+ def poll(self, timeout: int) -> int: + """Poll for events. + + Args: + timeout: Timeout in milliseconds. + + Returns: + Number of events ready. + """ + ... + def read_on_cpu(self, cpu: int) -> Optional[Any]:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[Severity: Medium]
Is read_on_cpu() missing its optional sample_id_all parameter? The C method signature ("i|i") suggests it accepts a second integer argument.
Quoted 2 lines
tools/perf/python/perf.pyi new file +2
-0+ """Read a sample event from a specific CPU. +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
Ian Rogers
reviewerPATCH v20 21/21 perf python: Add LiveSession helper
In reply to Ian RogersAdd LiveSession class in tools/perf/python/perf_live.py to support live event collection using perf.evlist and perf.parse_events, avoiding the need to fork a separate perf record process.
--- tools/perf/python/perf_live.py | 59 ++++++++++++++++++++++ tools/perf/tests/shell/lib/setup_python.sh | 13 +++++ 2 files changed, 72 insertions(+) create mode 100755 tools/perf/python/perf_live.py
tools/perf/python/perf_live.py new file +59 -0
@@ -0,0 +1,59 @@ +# SPDX-License-Identifier: GPL-2.0 +""" +Live event session helper using perf.evlist. + +This module provides a LiveSession class that allows running a callback +for each event collected live from the system, similar to perf.session +but without requiring a perf.data file. +""" + +import perf + + +class LiveSession: + """Represents a live event collection session.""" + + def __init__(self, event_string: str, sample_callback): + self.event_string = event_string + self.sample_callback = sample_callback + # Create a cpu map for all online CPUs + self.cpus = perf.cpu_map() + # Parse events and set maps + self.evlist = perf.parse_events(self.event_string, self.cpus) + self.evlist.config() + + def run(self): + """Run the live session.""" + try: + self.evlist.open() + self.evlist.mmap() + self.evlist.enable() + + while True: + # Poll for events with 100ms timeout + try: + self.evlist.poll(100) + except InterruptedError: + continue + for cpu in self.cpus: + for _ in range(1000): # Limit to 1000 events per CPU per poll to prevent starvation + try: + event = self.evlist.read_on_cpu(cpu) + except TypeError as e: + if "Unknown CPU" in str(e): + # CPU might be unmapped or offline, wait for mmap event + break + if "Unexpected header type" in str(e): + # Ignore valid but unsupported event types + continue + raise + + if event is None: + break + + if event.type == perf.RECORD_SAMPLE: + self.sample_callback(event) + except KeyboardInterrupt: + pass + finally: + self.evlist.close()
tools/perf/tests/shell/lib/setup_python.sh +13 -0
@@ -14,3 +14,16 @@ then echo Skipping test, python not detected please set environment variable PYTHON. exit 2 fi + +# Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-wide perf.so +if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then + PYTHONPATH_DIR="$PERF_EXEC_PATH/python" +elif [ -d "$(dirname "$0")/../../python" ]; then + PYTHONPATH_DIR="$(dirname "$0")/../../python" +elif [ -d "$(dirname "$0")/../python" ]; then + PYTHONPATH_DIR="$(dirname "$0")/../python" +fi + +if [ -n "$PYTHONPATH_DIR" ]; then + export PYTHONPATH="$PYTHONPATH_DIR${PYTHONPATH:+:$PYTHONPATH}" +fi
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerv21 cover Add additional python API support
In reply to Ian RogersThe perf script command has long supported running Python and Perl scripts by embedding libpython and libperl. This approach has several drawbacks:
- overhead by creating Python dictionaries for every event (whether used or not),
- complex build dependencies on specific Python/Perl versions,
- complications with threading due to perf being the interpreter,
- no clear way to run standalone scripts like ilist.py.
This series takes a different approach with some initial implementation posted as an RFC last October:
https://lore.kernel.org/linux-perf-users/[email protected]/
It builds the python extension as part of the normal build. The extension is able to read perf.data files. The event callbacks are converted to have a python evsel/evlist/sample passed to them.
To make the review process more manageable, the original 58-patch series has been split. This v18 series represents "Phase 1: API & Infrastructure" (20 patches). The first 4 patches of Phase 1 (cleanups and arch-specific header sorting) have already been merged upstream.
This remaining set contains:
1. Missed explicit dependency cleanups and header sorting for util/ and python.
2. Crucial core safety infrastructure (reference counting for evlist/evsel) to support safe lifecycle management in garbage-collected Python.
3. The core Python API extensions (session wrappers, perf_data wrappers, sample accessors, stubs, and LiveSession helper).
Phase 2 ("Script Porting & Tool Migration") will migrate the remaining 35+ existing Python/Perl scripts to the new API (which yields up to 35x speedups as demonstrated previously) and the final removal of embedded interpreters.
Addressing v20 Review Feedback:
The v21 patches merely update a commit message. Sashiko has correctly identified several structural limitations and edge cases. Almost all of these points correspond to known limitations that we have explicitly mapped out and planned for "Phase 2" of this Python binding refactor. Our primary goal with this v21 series (Phase 1) is to establish the baseline abstractions, fix immediate memory corruption bugs, and provide the foundational wrappers.
- Patch 4 (perf data: Add open flag): The reported issue with perf_data__switch failing to close thread-local files during directory mode rotation is a pre-existing bug in the C core. This patch strictly fixes the boolean data->open state tracking for Python integration.
- Patch 7 (perf evlist: Add reference count checking): evlist is fundamentally not thread-safe for concurrent destruction in the C core. The asymmetric leak (leaving cycles intact if evlist is dropped before evsel) is a known limitation. Implementing a thread-safe, symmetrical global GC lifecycle for evlist/evsel is planned for Phase 2.
- Patch 10 (perf python: Add python session abstraction):
1) perf.thread cannot cause a NULL dereference because it does not implement tp_new/tp_init, so it cannot be instantiated directly from Python. 2) Proper cross-endian analysis of packed 32-bit fields and the implementation of the remaining tool callbacks are explicitly deferred to Phase 2.
- Patch 11 (perf python: Refactor and add accessors to sample event):
1) Session UAF due to tracking the topological lifetime of the session relative to events requires a major lifecycle overhaul, slated for Phase 2. 2) The commit message previously inaccurately claimed we only allocate the strictly necessary copy size. This has been corrected in v21. We use PyObject_New with the static tp_basicsize; optimizing the dynamic allocation size is a future cleanup.
- Patch 16 (perf python: Add syscall name/id): The legacy scripts remain untouched in this series. Migrating the existing Python/Perl scripts to use the new C-API and dropping libaudit entirely is the core objective of Phase 2.
- Patch 20 (perf python: Add perf.pyi stubs file): While the stub is currently loosely typed as Any for threads, the segmentation fault is no longer possible since Patch 19 added runtime type validation (PyObject_TypeCheck) to the C implementation. We will tighten the stub typing in a follow-up patch.
Addressing v19 Review Feedback:
- Patch 19: Added PyObject_TypeCheck runtime validations to parse_events and parse_metrics to prevent memory corruption when invalid objects are passed from Python, resolving the blind C cast vulnerability.
- Patch 20: Updated perf.pyi stubs to properly type the threads parameter as Optional['thread_map'] instead of Optional[Any] to catch invalid types during static analysis.
Note: Other architectural limitations raised in the v19 review (e.g. TOCTOU cycle races, asymmetric cycle leaks, cross-endian needs_swap bypass, and session object dangling pointers) are explicitly acknowledged as limitations of this transitional patch set. As noted previously, implementing thread-safe, symmetrical GC for the Python bindings and hardening the C-API boundary are the primary focus of the upcoming Phase 2 series.
Addressing v18 Review Feedback:
- Patch 10 (perf.thread initialization): Added missing CHECK_INITIALIZED() in pyrf_thread__comm to prevent NULL dereference when instantiated directly.
- Patch 14 (STAT events): Passed NULL to pyrf_event__new for STAT events to prevent unconditional evsel__parse_sample and potential out-of-bounds reads.
- Patch 20 (LiveSession timeout): Broadened exception handling to explicitly ignore "Unexpected header type" for valid but unsupported events.
Note: Other feedback items raised in v18 (TOCTOU cycle races, asymmetric cycle collection, cross-endian data handling, guest machine symbol resolution, and pre-existing memory leaks/uninitialized variables) are acknowledged as limitations of the current implementation and will be addressed in Phase 2 or separate cleanup patches.
Addressing v17 Review Feedback:
- Patch 8: Added missing perf_sample__exit(&sample) to intel_pt_synth_ptwrite_sample() to fix an evsel reference leak.
- Patch 10: Fixed a bug in pyrf_session_tool__sample() that caused double byte-swapping on foreign-endian files by temporarily disabling needs_swap during re-parsing instead of assigning *sample.
- Patch 11: (Missed fixing address resolution for guest samples - will fix in next spin or Phase 2).
- Patch 19: Prevented python-clean from deleting tracked source file python/perf.pyi when building in tree. Also explicitly exported COUNT_HW_REF_CPU_CYCLES, thread, callchain, and callchain_node types/constants to the module via PyInit_perf.
- Patch 20: Narrowed except TypeError in LiveSession.run() to explicitly check for "Unknown CPU" so legitimate event parsing failures aren't swallowed.
Addressing v16 Review Feedback:
- Patch 10: Removed unconditional perf_session__create_kernel_maps to prevent corrupting cross-platform offline analysis.
- Patch 11: Corrected inaccurate commit message regarding memory allocation sizing.
- Patch 19: Fixed numerous type inconsistencies, missing properties, and incorrect return types in the perf.pyi stubs file.
- Patch 20: Cleaned up unused import errno in perf_live.py.
Note: Several issues spotted in v16/v17 review (e.g. pyrf_evsel__init format string type mismatch, evlist lockless double free, asymmetric memory leaks, missing Py_None type checks, and lack of NUL-termination for COMM/MMAP) are pre-existing limitations in the codebase or side-effects of the transitional cycle-breaking design. As discussed previously, these structurally complex or pre-existing bugs are deliberately deferred to the Phase 2 series.
Addressing v15 Review Feedback:
- Patch 2 (buffer overflow & type checks): The buffer overflow in pyrf_event__new() has been resolved by verifying event->header.size against the event struct size.
- Patch 2 (PyObject_HEAD_INIT): The initialization macros have been corrected to use the proper Python 3 compatibility approach.
- Patch 5 (Memory leak & RC validation): Applied extensive structural fixes using refcount_t semantics. Added validation wrapper structures to statically verify memory access safety in the lockless cycles.
- Patch 8 (evlist.open memory leak): Restructured lifecycle management for mmap buffers using do_munmap() hooks in evlist__put().
- Patch 16 (perf.pyi stubs): Corrected return types (Optional and proper object properties) and missing documentation strings in type stubs.
- Patch 20 (perf_live.py timeout): Adjusted poll timeout from 10000ms back to 100ms, replacing the tight exception loop.
Ian Rogers (21):
perf util: Sort includes and add missed explicit dependencies perf python: Add missed explicit dependencies
perf evsel/evlist: Avoid unnecessary #includes
perf data: Add open flag
perf evlist: Add reference count
perf evsel: Add reference count
perf evlist: Add reference count checking
perf python: Use evsel in sample in pyrf_event
perf python: Add wrapper for perf_data file abstraction perf python: Add python session abstraction wrapping perf's session perf python: Refactor and add accessors to sample event perf python: Add mmap2 event
perf python: Add callchain support
perf python: Extend API for stat events in python.c
perf python: Expose brstack in sample event
perf python: Add syscall name/id to convert syscall number and name perf python: Add config file access
perf python: Handle Py_None for thread and cpu maps
perf python: Add type checking for parse_events/parse_metrics perf python: Add perf.pyi stubs file
perf python: Add LiveSession helper
tools/perf/Makefile.perf | 7 +- tools/perf/arch/arm/util/cs-etm.c | 10 +- tools/perf/arch/arm64/util/arm-spe.c | 8 +- tools/perf/arch/arm64/util/hisi-ptt.c | 2 +- tools/perf/arch/x86/tests/hybrid.c | 22 +- tools/perf/arch/x86/tests/topdown.c | 4 +- tools/perf/arch/x86/util/auxtrace.c | 2 +- tools/perf/arch/x86/util/intel-bts.c | 6 +- tools/perf/arch/x86/util/intel-pt.c | 9 +- tools/perf/arch/x86/util/iostat.c | 14 +- tools/perf/bench/evlist-open-close.c | 29 +- tools/perf/builtin-annotate.c | 7 +- tools/perf/builtin-ftrace.c | 14 +- tools/perf/builtin-inject.c | 9 +- tools/perf/builtin-kvm.c | 14 +- tools/perf/builtin-kwork.c | 8 +- tools/perf/builtin-lock.c | 4 +- tools/perf/builtin-record.c | 95 +- tools/perf/builtin-report.c | 6 +- tools/perf/builtin-sched.c | 30 +- tools/perf/builtin-script.c | 15 +- tools/perf/builtin-stat.c | 83 +- tools/perf/builtin-top.c | 104 +- tools/perf/builtin-trace.c | 65 +- tools/perf/python/perf.pyi | 672 +++++ tools/perf/python/perf_live.py | 59 + tools/perf/tests/backward-ring-buffer.c | 26 +- tools/perf/tests/code-reading.c | 14 +- tools/perf/tests/event-times.c | 6 +- tools/perf/tests/event_update.c | 4 +- tools/perf/tests/evsel-roundtrip-name.c | 8 +- tools/perf/tests/evsel-tp-sched.c | 4 +- tools/perf/tests/expand-cgroup.c | 12 +- tools/perf/tests/hists_cumulate.c | 2 +- tools/perf/tests/hists_filter.c | 2 +- tools/perf/tests/hists_link.c | 2 +- tools/perf/tests/hists_output.c | 2 +- tools/perf/tests/hwmon_pmu.c | 7 +- tools/perf/tests/keep-tracking.c | 10 +- tools/perf/tests/mmap-basic.c | 24 +- tools/perf/tests/openat-syscall-all-cpus.c | 6 +- tools/perf/tests/openat-syscall-tp-fields.c | 26 +- tools/perf/tests/openat-syscall.c | 6 +- tools/perf/tests/parse-events.c | 139 +- tools/perf/tests/parse-metric.c | 8 +- tools/perf/tests/parse-no-sample-id-all.c | 2 +- tools/perf/tests/perf-record.c | 38 +- tools/perf/tests/perf-time-to-tsc.c | 12 +- tools/perf/tests/pfm.c | 12 +- tools/perf/tests/pmu-events.c | 11 +- tools/perf/tests/pmu.c | 4 +- tools/perf/tests/sample-parsing.c | 45 +- tools/perf/tests/shell/lib/setup_python.sh | 13 + tools/perf/tests/sw-clock.c | 20 +- tools/perf/tests/switch-tracking.c | 11 +- tools/perf/tests/task-exit.c | 20 +- tools/perf/tests/time-utils-test.c | 14 +- tools/perf/tests/tool_pmu.c | 7 +- tools/perf/tests/topology.c | 4 +- tools/perf/tests/uncore-event-sorting.c | 6 +- tools/perf/ui/browsers/annotate.c | 2 +- tools/perf/ui/browsers/hists.c | 22 +- tools/perf/util/Build | 1 - tools/perf/util/amd-sample-raw.c | 2 +- tools/perf/util/annotate-data.c | 2 +- tools/perf/util/annotate.c | 10 +- tools/perf/util/auxtrace.c | 14 +- tools/perf/util/block-info.c | 4 +- tools/perf/util/bpf_counter.c | 2 +- tools/perf/util/bpf_counter_cgroup.c | 14 +- tools/perf/util/bpf_ftrace.c | 9 +- tools/perf/util/bpf_lock_contention.c | 12 +- tools/perf/util/bpf_off_cpu.c | 44 +- tools/perf/util/bpf_trace_augment.c | 8 +- tools/perf/util/cgroup.c | 26 +- tools/perf/util/cs-etm.c | 5 +- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/data.c | 27 +- tools/perf/util/data.h | 4 +- tools/perf/util/evlist.c | 496 ++-- tools/perf/util/evlist.h | 273 +- tools/perf/util/evsel.c | 39 +- tools/perf/util/evsel.h | 40 +- tools/perf/util/expr.c | 2 +- tools/perf/util/header.c | 69 +- tools/perf/util/header.h | 2 +- tools/perf/util/intel-pt.c | 8 +- tools/perf/util/intel-tpebs.c | 7 +- tools/perf/util/iostat.c | 2 +- tools/perf/util/iostat.h | 2 +- tools/perf/util/map.h | 9 +- tools/perf/util/metricgroup.c | 12 +- tools/perf/util/parse-events.c | 10 +- tools/perf/util/parse-events.y | 2 +- tools/perf/util/perf_api_probe.c | 20 +- tools/perf/util/pfm.c | 4 +- tools/perf/util/print-events.c | 2 +- tools/perf/util/python.c | 2846 ++++++++++++++++--- tools/perf/util/record.c | 11 +- tools/perf/util/s390-sample-raw.c | 20 +- tools/perf/util/sample-raw.c | 4 +- tools/perf/util/sample.c | 17 +- tools/perf/util/session.c | 69 +- tools/perf/util/session.h | 2 + tools/perf/util/setup.py | 5 + tools/perf/util/sideband_evlist.c | 40 +- tools/perf/util/sort.c | 2 +- tools/perf/util/stat-display.c | 6 +- tools/perf/util/stat-shadow.c | 24 +- tools/perf/util/stat.c | 20 +- tools/perf/util/stream.c | 4 +- tools/perf/util/synthetic-events.c | 11 +- tools/perf/util/time-utils.c | 12 +- tools/perf/util/top.c | 4 +- 114 files changed, 4602 insertions(+), 1529 deletions(-) create mode 100644 tools/perf/python/perf.pyi create mode 100755 tools/perf/python/perf_live.py
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v21 1/21 perf util: Sort includes and add missed explicit dependencies
Fix missing includes found while cleaning the evsel/evlist header files. Sort the remaining header files for consistency with the rest of the code.
--- tools/perf/util/bpf_off_cpu.c | 30 +++++----- tools/perf/util/bpf_trace_augment.c | 8 +-- tools/perf/util/evlist.c | 91 +++++++++++++++-------------- tools/perf/util/map.h | 9 ++- tools/perf/util/perf_api_probe.c | 18 +++--- tools/perf/util/s390-sample-raw.c | 19 +++--- tools/perf/util/stat-shadow.c | 20 ++++--- tools/perf/util/stat.c | 16 +++-- 8 files changed, 113 insertions(+), 98 deletions(-)
tools/perf/util/bpf_off_cpu.c +16 -14
@@ -1,23 +1,25 @@// SPDX-License-Identifier: GPL-2.0 -#include "util/bpf_counter.h" -#include "util/debug.h" -#include "util/evsel.h" -#include "util/evlist.h" -#include "util/off_cpu.h" -#include "util/perf-hooks.h" -#include "util/record.h" -#include "util/session.h" -#include "util/target.h" -#include "util/cpumap.h" -#include "util/thread_map.h" -#include "util/cgroup.h" -#include "util/strlist.h" +#include <linux/time64.h> +#include <bpf/bpf.h> #include <bpf/btf.h> #include <internal/xyarray.h> -#include <linux/time64.h> +#include "bpf_counter.h" #include "bpf_skel/off_cpu.skel.h" +#include "cgroup.h" +#include "cpumap.h" +#include "debug.h" +#include "evlist.h" +#include "evsel.h" +#include "off_cpu.h" +#include "parse-events.h" +#include "perf-hooks.h" +#include "record.h" +#include "session.h" +#include "strlist.h" +#include "target.h" +#include "thread_map.h" #define MAX_STACKS 32#define MAX_PROC 4096
tools/perf/util/bpf_trace_augment.c +4 -4
@@ -1,11 +1,11 @@#include <bpf/libbpf.h> #include <internal/xyarray.h> -#include "util/debug.h" -#include "util/evlist.h" -#include "util/trace_augment.h" -#include "bpf_skel/augmented_raw_syscalls.skel.h" +#include "debug.h" +#include "evlist.h" +#include "parse-events.h" +#include "trace_augment.h" static struct augmented_raw_syscalls_bpf *skel;static struct evsel *bpf_output;
tools/perf/util/evlist.c +46 -45
113 lines not on this page · raw message
tools/perf/util/map.h +4 -5
@@ -2,14 +2,13 @@#ifndef __PERF_MAP_H#define __PERF_MAP_H -#include <linux/refcount.h> -#include <linux/compiler.h> -#include <linux/list.h> -#include <linux/rbtree.h> +#include <stdbool.h> #include <stdio.h> #include <string.h> -#include <stdbool.h> + +#include <linux/refcount.h> #include <linux/types.h> +#include <internal/rc_check.h> struct dso ;
tools/perf/util/perf_api_probe.c +11 -7
@@ -1,14 +1,18 @@/* SPDX-License-Identifier: GPL-2.0 */ +#include "perf_api_probe.h" -#include "perf-sys.h" -#include "util/cloexec.h" -#include "util/evlist.h" -#include "util/evsel.h" -#include "util/parse-events.h" -#include "util/perf_api_probe.h" -#include <perf/cpumap.h> #include <errno.h> +#include <perf/cpumap.h> + +#include "cloexec.h" +#include "evlist.h" +#include "evsel.h" +#include "parse-events.h" +#include "perf-sys.h" +#include "pmu.h" +#include "pmus.h" +typedef void (*setup_probe_fn_t )(struct evsel *evsel);static int perf_do_probe_api (setup_probe_fn_t fn,struct perf_cpu cpu,const char *str)
tools/perf/util/s390-sample-raw.c +10 -9
@@ -12,25 +12,26 @@* sample was taken from. */ -#include <unistd.h> +#include <inttypes.h> #include <stdio.h> #include <string.h> -#include <inttypes.h> -#include <sys/stat.h> +#include <asm/byteorder.h> #include <linux/compiler.h> #include <linux/err.h> -#include <asm/byteorder.h> +#include <sys/stat.h> +#include <unistd.h> +#include "color.h" #include "debug.h" -#include "session.h" #include "evlist.h" -#include "color.h" #include "hashmap.h" -#include "sample-raw.h" +#include "pmu.h" +#include "pmus.h" #include "s390-cpumcf-kernel.h" -#include "util/pmu.h" -#include "util/sample.h" +#include "sample-raw.h" +#include "sample.h" +#include "session.h" static size_t ctrset_size (struct cf_ctrset_entry *set) {
tools/perf/util/stat-shadow.c +12 -8
@@ -2,20 +2,24 @@#include <errno.h> #include <math.h> #include <stdio.h> -#include "evsel.h" -#include "stat.h" + +#include <linux/zalloc.h> + +#include "cgroup.h" #include "color.h" #include "debug.h" -#include "pmu.h" -#include "rblist.h" #include "evlist.h" +#include "evsel.h" #include "expr.h" -#include "metricgroup.h" -#include "cgroup.h" -#include "units.h" +#include "hashmap.h" #include "iostat.h" -#include "util/hashmap.h" +#include "metricgroup.h" +#include "pmu.h" +#include "pmus.h" +#include "rblist.h" +#include "stat.h" #include "tool_pmu.h" +#include "units.h" static bool tool_pmu__is_time_event (const struct perf_stat_config *config,const struct evsel *evsel,int *tool_aggr_idx)
tools/perf/util/stat.c +10 -6
@@ -1,21 +1,25 @@// SPDX-License-Identifier: GPL-2.0 +#include "stat.h" +#include <errno.h> -#include <linux/err.h> #include <inttypes.h> #include <math.h> #include <string.h> + +#include <linux/err.h> +#include <linux/zalloc.h> +#include "counts.h" #include "cpumap.h" #include "debug.h" +#include "evlist.h" +#include "evsel.h" +#include "hashmap.h" #include "header.h" -#include "stat.h" +#include "pmu.h" #include "session.h" #include "target.h" -#include "evlist.h" -#include "evsel.h" #include "thread_map.h" -#include "util/hashmap.h" -#include <linux/zalloc.h> void update_stats (struct stats *stats,u64 val) {
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v21 2/21 perf python: Add missed explicit dependencies
In reply to Ian RogersFix missing #include of pmus.h found while cleaning the evsel/evlist header files.
Also define PY_SSIZE_T_CLEAN before including Python.h to comply with modern Python 3 C-API requirements, and introduce CHECK_INITIALIZED and CHECK_INITIALIZED_INT safety macros to be used by subsequent patches.
--- tools/perf/util/python.c | 41 +++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-)
tools/perf/util/python.c +32 -9
@@ -1,30 +1,37 @@// SPDX-License-Identifier: GPL-2.0 +#define PY_SSIZE_T_CLEAN #include <Python.h> -#include <structmember.h> +#include <inttypes.h> -#include <poll.h> +#include <linux/err.h> +#include <poll.h> + +#include <internal/lib.h> #include <perf/cpumap.h> -#ifdef HAVE_LIBTRACEEVENT -#include <event-parse.h> -#endif #include <perf/mmap.h> +#include <structmember.h> +#include "callchain.h" #include "counts.h" +#include "event.h" #include "evlist.h" #include "evsel.h" -#include "event.h" #include "expr.h" +#include "metricgroup.h" +#include "mmap.h" +#include "pmus.h" #include "print_binary.h" #include "record.h" #include "strbuf.h" #include "thread_map.h" #include "tp_pmu.h" #include "trace-event.h" -#include "metricgroup.h" -#include "mmap.h" #include "util/sample.h" -#include <internal/lib.h> + +#ifdef HAVE_LIBTRACEEVENT +#include <event-parse.h> +#endif PyMODINIT_FUNC PyInit_perf (void );
@@ -38,6 +45,22 @@ PyMODINIT_FUNC PyInit_perf(void); offsetof(struct pyrf_event , sample) + offsetof(struct perf_sample , member), \ 0, help } +#define CHECK_INITIALIZED (ptr, msg) \ + do { \ + if (!(ptr)) { \ + PyErr_SetString(PyExc_ValueError, msg " not initialized"); \ + return NULL; \ + } \ + } while (0) + +#define CHECK_INITIALIZED_INT (ptr, msg) \ + do { \ + if (!(ptr)) { \ + PyErr_SetString(PyExc_ValueError, msg " not initialized"); \ + return -1; \ + } \ + } while (0) +struct pyrf_event {PyObject_HEAD struct evsel *evsel;
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v21 3/21 perf evsel/evlist: Avoid unnecessary #includes
In reply to Ian RogersUse forward declarations and remove unnecessary #includes in evsel.h. Sort the forward declarations in evsel.h and evlist.h. Move some PMU code into evsel.c.
--- tools/perf/util/evlist.h | 15 +++++++++------ tools/perf/util/evsel.c | 5 +++++ tools/perf/util/evsel.h | 25 ++++++++++++------------- 3 files changed, 26 insertions(+), 19 deletions(-)
tools/perf/util/evlist.h +9 -6
@@ -2,29 +2,32 @@#ifndef __PERF_EVLIST_H#define __PERF_EVLIST_H 1 +#include <signal.h> +#include <linux/compiler.h> #include <linux/kernel.h> -#include <linux/refcount.h> #include <linux/list.h> +#include <linux/refcount.h> +#include <pthread.h> +#include <unistd.h> +#include <api/fd/array.h> #include <internal/evlist.h> #include <internal/evsel.h> #include <perf/evlist.h> +#include "affinity.h" #include "events_stats.h" #include "evsel.h" #include "rblist.h" -#include <pthread.h> -#include <signal.h> -#include <unistd.h> -struct pollfd ; -struct thread_map ;struct perf_cpu_map ;struct perf_stat_config ; +struct pollfd ;struct record_opts ;struct strbuf ;struct target ; +struct thread_map ;/* * State machine of bkw_mmap_state:
tools/perf/util/evsel.c +5 -0
@@ -370,6 +370,11 @@ void evsel__set_sample_id(struct evsel *evsel, evsel->core.attr.read_format |=PERF_FORMAT_ID ; } +bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel) +{ +return evsel->pmu && evsel->pmu->type >PERF_PMU_TYPE_PE_END ; +} +/** * evsel__is_function_event - Return whether given evsel is a function * trace event
tools/perf/util/evsel.h +12 -13
@@ -2,28 +2,30 @@#ifndef __PERF_EVSEL_H#define __PERF_EVSEL_H 1 -#include <linux/list.h> #include <stdbool.h> -#include <sys/types.h> + +#include <linux/list.h> #include <linux/perf_event.h> #include <linux/types.h> +#include <sys/types.h> +#include <internal/evsel.h> #include <perf/evsel.h> +#include "symbol_conf.h" -#include "pmus.h" -#include "pmu.h" +struct bperf_follower_bpf ; +struct bperf_leader_bpf ; +struct bpf_counter_ops ;struct bpf_object ;struct cgroup ; +struct hashmap ;struct perf_counts ; +struct perf_pmu ;struct perf_stat_config ;struct perf_stat_evsel ; -union perf_event ; -struct bpf_counter_ops ;struct target ; -struct hashmap ; -struct bperf_leader_bpf ; -struct bperf_follower_bpf ; +union perf_event ;typedef int (evsel__sb_cb_t )(union perf_event *event,void *data);
@@ -356,10 +358,6 @@ void arch_evsel__apply_ratio_to_prev(struct evsel *evsel, struct perf_event_attrint evsel__set_filter (struct evsel *evsel,const char *filter);int evsel__append_tp_filter (struct evsel *evsel,const char *filter);int evsel__append_addr_filter (struct evsel *evsel,const char *filter); -static inline bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel) -{ -return evsel->pmu && evsel->pmu->type >PERF_PMU_TYPE_PE_END ; -}int evsel__enable_cpu (struct evsel *evsel,int cpu_map_idx);int evsel__enable (struct evsel *evsel);
@@ -478,6 +476,7 @@ static inline bool evsel__is_group_event(struct evsel *evsel)return evsel__is_group_leader (evsel) && evsel->core.nr_members >1 ; } +bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel);bool evsel__is_function_event (struct evsel *evsel);static inline bool evsel__is_bpf_output (struct evsel *evsel)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v21 03/21] perf evsel/evlist: Avoid unnecessary #includes
Quoted 6 lines · Ian Rogers
Use forward declarations and remove unnecessary #includes in
evsel.h. Sort the forward declarations inevsel.handevlist.h. Move some PMU code intoevsel.c.Signed-off-by Ian Rogers <[email protected]>Acked-by Namhyung Kim <[email protected]>
⬢ [acme@toolbx perf-tools-next2]$ git log --oneline -5ce69f4f6d7467ef4 (HEAD) perf evsel/evlist: Avoid unnecessary #includes23dce143f5ed0557 perf python: Add missed explicit dependenciesca74d08280ad690f perf util: Sort includes and add missed explicit dependencies776bd9ac834556ab (x1/perf-tools-next, perf-tools-next.korg/tmp.perf-tools-next, perf-tools-next.korg/perf-tools-next, origin/perf-tools-next) perf bpf: Fix up build failure due to change of btf_vlen() return type 522ae978ac720a91 (origin/perf/hardening-9) perf dso: Set standard errno on decompression failure
⬢ [acme@toolbx perf-tools-next2]$
LD /tmp/build/perf-tools-next2/util/scripting-engines/perf-util-in.o
make[3]: *** [/home/acme/git/perf-tools-next2/tools/build/Makefile.build:158: util] Error 2 make[2]: *** [Makefile.perf:573: /tmp/build/perf-tools-next2/perf-util-in.o] Error 2 make[2]: *** Waiting for unfinished jobs....
LD /tmp/build/perf-tools-next2/tests/perf-test-in.o LD /tmp/build/perf-tools-next2/perf-test-in.o LD /tmp/build/perf-tools-next2/perf-in.o
make[1]: *** [Makefile.perf:288: sub-make] Error 2
make: *** [Makefile:122: install-bin] Error 2
make: Leaving directory '/home/acme/git/perf-tools-next2/tools/perf' ⬢ [acme@toolbx perf-tools-next2]$
I added this to fix the build:
⬢ [acme@toolbx perf-tools-next2]$ git diff
tools/perf/util/aslr.c +1 -0
@@ -13,6 +13,7 @@#include "session.h" #include "data.h" #include "dso.h" +#include "pmu.h" #include "pmus.h" #include <internal/lib.h> /* page_size */
⬢ [acme@toolbx perf-tools-next2]$
Applied the series, now testing.
- Arnaldo
Quoted 130 lines
--- tools/perf/util/evlist.h | 15 +++++++++------ tools/perf/util/evsel.c | 5 +++++ tools/perf/util/evsel.h | 25 ++++++++++++------------- 3 files changed, 26 insertions(+), 19 deletions(-)tools/perf/util/evlist.h +9
-6@@ -2,29 +2,32 @@#ifndef __PERF_EVLIST_H#define __PERF_EVLIST_H 1 +#include <signal.h> +#include <linux/compiler.h> #include <linux/kernel.h> -#include <linux/refcount.h> #include <linux/list.h> +#include <linux/refcount.h> +#include <pthread.h> +#include <unistd.h> +#include <api/fd/array.h> #include <internal/evlist.h> #include <internal/evsel.h> #include <perf/evlist.h> +#include "affinity.h" #include "events_stats.h" #include "evsel.h" #include "rblist.h" -#include <pthread.h> -#include <signal.h> -#include <unistd.h> -struct pollfd ; -struct thread_map ;struct perf_cpu_map ;struct perf_stat_config ; +struct pollfd ;struct record_opts ;struct strbuf ;struct target ; +struct thread_map ;/* * State machine of bkw_mmap_state: tools/perf/util/evsel.c +5
-0@@ -370,6 +370,11 @@ void evsel__set_sample_id(struct evsel *evsel, evsel->core.attr.read_format |=PERF_FORMAT_ID ; } +bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel) +{ +return evsel->pmu && evsel->pmu->type >PERF_PMU_TYPE_PE_END ; +} +/** * evsel__is_function_event - Return whether given evsel is a function * trace event tools/perf/util/evsel.h +12
-13@@ -2,28 +2,30 @@#ifndef __PERF_EVSEL_H#define __PERF_EVSEL_H 1 -#include <linux/list.h> #include <stdbool.h> -#include <sys/types.h> + +#include <linux/list.h> #include <linux/perf_event.h> #include <linux/types.h> +#include <sys/types.h> +#include <internal/evsel.h> #include <perf/evsel.h> +#include "symbol_conf.h" -#include "pmus.h" -#include "pmu.h" +struct bperf_follower_bpf ; +struct bperf_leader_bpf ; +struct bpf_counter_ops ;struct bpf_object ;struct cgroup ; +struct hashmap ;struct perf_counts ; +struct perf_pmu ;struct perf_stat_config ;struct perf_stat_evsel ; -union perf_event ; -struct bpf_counter_ops ;struct target ; -struct hashmap ; -struct bperf_leader_bpf ; -struct bperf_follower_bpf ; +union perf_event ;typedef int (evsel__sb_cb_t )(union perf_event *event,void *data);@@ -356,10 +358,6 @@ void arch_evsel__apply_ratio_to_prev(struct evsel *evsel, struct perf_event_attrint evsel__set_filter (struct evsel *evsel,const char *filter);int evsel__append_tp_filter (struct evsel *evsel,const char *filter);int evsel__append_addr_filter (struct evsel *evsel,const char *filter); -static inline bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel) -{ -return evsel->pmu && evsel->pmu->type >PERF_PMU_TYPE_PE_END ; -}int evsel__enable_cpu (struct evsel *evsel,int cpu_map_idx);int evsel__enable (struct evsel *evsel);@@ -478,6 +476,7 @@ static inline bool evsel__is_group_event(struct evsel *evsel)return evsel__is_group_leader (evsel) && evsel->core.nr_members >1 ; } +bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel);bool evsel__is_function_event (struct evsel *evsel);static inline bool evsel__is_bpf_output (struct evsel *evsel)Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerRe: [PATCH v21 03/21] perf evsel/evlist: Avoid unnecessary #includes
Quoted 46 lines · Arnaldo Carvalho de Melo
Quoted 6 lines · Ian Rogers
Use forward declarations and remove unnecessary #includes in
evsel.h. Sort the forward declarations inevsel.handevlist.h. Move some PMU code intoevsel.c.Signed-off-by Ian Rogers <[email protected]>Acked-by Namhyung Kim <[email protected]>⬢ [acme@toolbx perf-tools-next2]$ git log --oneline -5
ce69f4f6d7467ef4(HEAD) perf evsel/evlist: Avoid unnecessary #includes23dce143f5ed0557perf python: Add missed explicit dependenciesca74d08280ad690fperf util: Sort includes and add missed explicit dependencies776bd9ac834556ab(x1/perf-tools-next, perf-tools-next.korg/tmp.perf-tools-next, perf-tools-next.korg/perf-tools-next, origin/perf-tools-next) perf bpf: Fix up build failure due to change ofbtf_vlen()return type522ae978ac720a91(origin/perf/hardening-9) perf dso: Set standard errno on decompression failure
⬢ [acme@toolbx perf-tools-next2]$LD
/tmp/build/perf-tools-next2/util/scripting-engines/perf-util-in.o
make[3]: *** [/home/acme/git/perf-tools-next2/tools/build/Makefile.build:158: util] Error 2 make[2]: *** [Makefile.perf:573:/tmp/build/perf-tools-next2/perf-util-in.o] Error 2 make[2]: *** Waiting for unfinished jobs....LD /tmp/build/perf-tools-next2/tests/perf-test-in.o LD /tmp/build/perf-tools-next2/perf-test-in.o LD /tmp/build/perf-tools-next2/perf-in.omake[1]: *** [Makefile.perf:288: sub-make] Error 2
make: *** [Makefile:122: install-bin] Error 2
make: Leaving directory '/home/acme/git/perf-tools-next2/tools/perf' ⬢ [acme@toolbx perf-tools-next2]$I added this to fix the build:
⬢ [acme@toolbx perf-tools-next2]$ git diff
tools/perf/util/aslr.c +1
-0@@ -13,6 +13,7 @@#include "session.h" #include "data.h" #include "dso.h" +#include "pmu.h" #include "pmus.h" #include <internal/lib.h> /* page_size */ ⬢ [acme@toolbx perf-tools-next2]$
Quoted 1 line
Applied the series, now testing.
Thanks, I'd found the same aslr issue but was testing with fixing the wider issues you raised. Presumably the aslr code was depending on the the #include via evsel.h and so cleaning those up broke the build as the aslr code needed a fix.
Thanks for testing, I can rebase my fixes on top of the v21 changes assuming testing goes ok.
Ian
Quoted 133 lines
- Arnaldo
Quoted 130 lines
--- tools/perf/util/evlist.h | 15 +++++++++------ tools/perf/util/evsel.c | 5 +++++ tools/perf/util/evsel.h | 25 ++++++++++++------------- 3 files changed, 26 insertions(+), 19 deletions(-)tools/perf/util/evlist.h +9
-6@@ -2,29 +2,32 @@#ifndef __PERF_EVLIST_H#define __PERF_EVLIST_H 1 +#include <signal.h> +#include <linux/compiler.h> #include <linux/kernel.h> -#include <linux/refcount.h> #include <linux/list.h> +#include <linux/refcount.h> +#include <pthread.h> +#include <unistd.h> +#include <api/fd/array.h> #include <internal/evlist.h> #include <internal/evsel.h> #include <perf/evlist.h> +#include "affinity.h" #include "events_stats.h" #include "evsel.h" #include "rblist.h" -#include <pthread.h> -#include <signal.h> -#include <unistd.h> -struct pollfd ; -struct thread_map ;struct perf_cpu_map ;struct perf_stat_config ; +struct pollfd ;struct record_opts ;struct strbuf ;struct target ; +struct thread_map ;/* * State machine of bkw_mmap_state: tools/perf/util/evsel.c +5
-0@@ -370,6 +370,11 @@ void evsel__set_sample_id(struct evsel *evsel, evsel->core.attr.read_format |=PERF_FORMAT_ID ; } +bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel) +{ +return evsel->pmu && evsel->pmu->type >PERF_PMU_TYPE_PE_END ; +} +/** * evsel__is_function_event - Return whether given evsel is a function * trace event tools/perf/util/evsel.h +12
-14@@ -2,28 +2,30 @@#ifndef __PERF_EVSEL_H#define __PERF_EVSEL_H 1 -#include <linux/list.h> #include <stdbool.h> -#include <sys/types.h> + +#include <linux/list.h> #include <linux/perf_event.h> #include <linux/types.h> +#include <sys/types.h> +#include <internal/evsel.h> #include <perf/evsel.h> +#include "symbol_conf.h" -#include "pmus.h" -#include "pmu.h" +struct bperf_follower_bpf ; +struct bperf_leader_bpf ; +struct bpf_counter_ops ;struct bpf_object ;struct cgroup ; +struct hashmap ;struct perf_counts ; +struct perf_pmu ;struct perf_stat_config ;struct perf_stat_evsel ; -union perf_event ; -struct bpf_counter_ops ;struct target ; -struct hashmap ; -struct bperf_leader_bpf ; -struct bperf_follower_bpf ; +union perf_event ;typedef int (evsel__sb_cb_t )(union perf_event *event,void *data);@@ -356,10 +358,6 @@ void arch_evsel__apply_ratio_to_prev(struct evsel *evsel, struct perf_event_attrint evsel__set_filter (struct evsel *evsel,const char *filter);int evsel__append_tp_filter (struct evsel *evsel,const char *filter);int evsel__append_addr_filter (struct evsel *evsel,const char *filter); -static inline bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel) -{ -return evsel->pmu && evsel->pmu->type >PERF_PMU_TYPE_PE_END ; -}int evsel__enable_cpu (struct evsel *evsel,int cpu_map_idx);int evsel__enable (struct evsel *evsel);@@ -478,6 +476,7 @@ static inline bool evsel__is_group_event(struct evsel *evsel)return evsel__is_group_leader (evsel) && evsel->core.nr_members >1 ; } +bool evsel__is_non_perf_event_open_pmu (const struct evsel *evsel);bool evsel__is_function_event (struct evsel *evsel);static inline bool evsel__is_bpf_output (struct evsel *evsel) --2.54.0.1136.gdb2ca164c4-goog
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v21 03/21] perf evsel/evlist: Avoid unnecessary #includes
Quoted 55 lines · Ian Rogers
Quoted 46 lines · Arnaldo Carvalho de Melo
Quoted 6 lines · Ian Rogers
Use forward declarations and remove unnecessary #includes in
evsel.h. Sort the forward declarations inevsel.handevlist.h. Move some PMU code intoevsel.c.Signed-off-by Ian Rogers <[email protected]>Acked-by Namhyung Kim <[email protected]>⬢ [acme@toolbx perf-tools-next2]$ git log --oneline -5
ce69f4f6d7467ef4(HEAD) perf evsel/evlist: Avoid unnecessary #includes23dce143f5ed0557perf python: Add missed explicit dependenciesca74d08280ad690fperf util: Sort includes and add missed explicit dependencies776bd9ac834556ab(x1/perf-tools-next, perf-tools-next.korg/tmp.perf-tools-next, perf-tools-next.korg/perf-tools-next, origin/perf-tools-next) perf bpf: Fix up build failure due to change ofbtf_vlen()return type522ae978ac720a91(origin/perf/hardening-9) perf dso: Set standard errno on decompression failure
⬢ [acme@toolbx perf-tools-next2]$LD
/tmp/build/perf-tools-next2/util/scripting-engines/perf-util-in.o
make[3]: *** [/home/acme/git/perf-tools-next2/tools/build/Makefile.build:158: util] Error 2 make[2]: *** [Makefile.perf:573:/tmp/build/perf-tools-next2/perf-util-in.o] Error 2 make[2]: *** Waiting for unfinished jobs....LD /tmp/build/perf-tools-next2/tests/perf-test-in.o LD /tmp/build/perf-tools-next2/perf-test-in.o LD /tmp/build/perf-tools-next2/perf-in.omake[1]: *** [Makefile.perf:288: sub-make] Error 2
make: *** [Makefile:122: install-bin] Error 2
make: Leaving directory '/home/acme/git/perf-tools-next2/tools/perf' ⬢ [acme@toolbx perf-tools-next2]$I added this to fix the build:
⬢ [acme@toolbx perf-tools-next2]$ git diff
tools/perf/util/aslr.c +1
-0@@ -13,6 +13,7 @@#include "session.h" #include "data.h" #include "dso.h" +#include "pmu.h" #include "pmus.h" #include <internal/lib.h> /* page_size */ ⬢ [acme@toolbx perf-tools-next2]$
Quoted 1 line
Applied the series, now testing.
Thanks, I'd found the same aslr issue but was testing with fixing the wider issues you raised. Presumably the aslr code was depending on the the #include via
evsel.hand so cleaning those up broke the build as the aslr code needed a fix.
Quoted 2 lines
Thanks for testing, I can rebase my fixes on top of the v21 changes assuming testing goes ok.
Since 7.1 was already released I'm inclined to push what I have, your v21 series + this simple fix, so that it can sit in linux-next for a few days and you would send fixes on top of it, ok?
I'm basically stopping merging patch series and switching to processing just fixes so that I can send the PR to Linus by the end of this week.
I'll push what I have to tmp.perf-tools-next now.
- Arnaldo
Ian Rogers
reviewerPATCH v21 4/21 perf data: Add open flag
In reply to Ian RogersAvoid double opens and ensure only open files are closed. This addresses some issues with python integration where the data file wants to be opened before being given to a session.
--- tools/perf/util/data.c | 27 ++++++++++++++++++++++----- tools/perf/util/data.h | 4 +++- 2 files changed, 25 insertions(+), 6 deletions(-)
tools/perf/util/data.c +22 -5
@@ -345,9 +345,10 @@ static int open_dir(struct perf_data *data)if (asprintf (&data->file.path,"%s/data" , data->path) <0 )return -1 ; -if (perf_data__is_write (data) && -mkdir (data->path,S_IRWXU ) <0 ) +if (perf_data__is_write (data) &&mkdir (data->path,0700 ) <0 ) { +zfree (&data->file.path);return -1 ; + } ret =open_file (data);
@@ -360,9 +361,16 @@ static int open_dir(struct perf_data *data)int perf_data__open (struct perf_data *data) { -if (check_pipe (data)) +int ret; + +if (data->open)return 0 ; +if (check_pipe (data)) { + data->open = true; +return 0 ; + } +/* currently it allows stdio for pipe only */ data->file.use_stdio = false;
@@ -375,16 +383,24 @@ int perf_data__open(struct perf_data *data)if (perf_data__is_read (data)) data->is_dir =is_dir (data); -return perf_data__is_dir (data) ? -open_dir (data) :open_file_dup (data); + ret =perf_data__is_dir (data) ?open_dir (data) :open_file_dup (data); + +if (!ret) + data->open = true; + +return ret; }void perf_data__close (struct perf_data *data) { +if (!data->open) +return ; +if (perf_data__is_dir (data))perf_data__close_dir (data);perf_data_file__close (&data->file); + data->open = false; }static ssize_t perf_data_file__read (struct perf_data_file *file,void *buf,size_t size)
@@ -457,6 +473,7 @@ int perf_data__switch(struct perf_data *data,if (!at_exit) {perf_data_file__close (&data->file); + data->open = false; ret =perf_data__open (data);if (ret <0 ) goto out;
tools/perf/util/data.h +3 -1
@@ -50,6 +50,8 @@ struct perf_data {const char *path;/** @file: Underlying file to be used. */ struct perf_data_file file; +/** @open: Has the file or directory been opened. */ +bool open;/** @is_pipe: Underlying file is a pipe. */ bool is_pipe;/** @is_dir: Underlying file is a directory. */
@@ -59,7 +61,7 @@ struct perf_data {/** @in_place_update: A file opened for reading but will be written to. */ bool in_place_update;/** @mode: Read or write mode. */ -enum perf_data_mode mode; + enum perf_data_mode mode:8;struct {/** @version: perf_dir_version. */
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v21 5/21 perf evlist: Add reference count
In reply to Ian RogersThis a no-op for most of the perf tool. The reference count is set to 1 at allocation, the put will see the 1, decrement it and perform the delete. The purpose for adding the reference count is for the python code. Prior to this change the python code would clone evlists, but this has issues if events are opened, etc. This change adds a reference count for the evlists and a later change will add it to evsels. The combination is needed for the python code to operate correctly (not hit asserts in the evsel clone), but the changes are broken apart for the sake of smaller patches.
--- tools/perf/arch/x86/tests/hybrid.c | 2 +- tools/perf/arch/x86/tests/topdown.c | 4 +- tools/perf/arch/x86/util/iostat.c | 2 +- tools/perf/bench/evlist-open-close.c | 18 +- tools/perf/builtin-ftrace.c | 8 +- tools/perf/builtin-kvm.c | 4 +- tools/perf/builtin-lock.c | 2 +- tools/perf/builtin-record.c | 4 +- tools/perf/builtin-sched.c | 6 +- tools/perf/builtin-script.c | 2 +- tools/perf/builtin-stat.c | 10 +- tools/perf/builtin-top.c | 52 ++--- tools/perf/builtin-trace.c | 26 +-- tools/perf/tests/backward-ring-buffer.c | 18 +- tools/perf/tests/code-reading.c | 4 +- tools/perf/tests/event-times.c | 4 +- tools/perf/tests/event_update.c | 2 +- tools/perf/tests/evsel-roundtrip-name.c | 8 +- tools/perf/tests/expand-cgroup.c | 8 +- tools/perf/tests/hists_cumulate.c | 2 +- tools/perf/tests/hists_filter.c | 2 +- tools/perf/tests/hists_link.c | 2 +- tools/perf/tests/hists_output.c | 2 +- tools/perf/tests/hwmon_pmu.c | 2 +- tools/perf/tests/keep-tracking.c | 2 +- tools/perf/tests/mmap-basic.c | 18 +- tools/perf/tests/openat-syscall-tp-fields.c | 18 +- tools/perf/tests/parse-events.c | 4 +- tools/perf/tests/parse-metric.c | 4 +- tools/perf/tests/parse-no-sample-id-all.c | 2 +- tools/perf/tests/perf-record.c | 18 +- tools/perf/tests/perf-time-to-tsc.c | 2 +- tools/perf/tests/pfm.c | 4 +- tools/perf/tests/pmu-events.c | 6 +- tools/perf/tests/pmu.c | 4 +- tools/perf/tests/sw-clock.c | 14 +- tools/perf/tests/switch-tracking.c | 2 +- tools/perf/tests/task-exit.c | 14 +- tools/perf/tests/tool_pmu.c | 2 +- tools/perf/tests/topology.c | 2 +- tools/perf/tests/uncore-event-sorting.c | 2 +- tools/perf/util/cgroup.c | 4 +- tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/evlist.c | 20 +- tools/perf/util/evlist.h | 7 +- tools/perf/util/expr.c | 2 +- tools/perf/util/header.c | 22 +- tools/perf/util/metricgroup.c | 6 +- tools/perf/util/parse-events.c | 4 +- tools/perf/util/perf_api_probe.c | 2 +- tools/perf/util/python.c | 242 +++++++++----------- tools/perf/util/record.c | 2 +- tools/perf/util/session.c | 2 +- tools/perf/util/sideband_evlist.c | 16 +- 54 files changed, 315 insertions(+), 328 deletions(-)
tools/perf/arch/x86/tests/hybrid.c +1 -1
@@ -268,7 +268,7 @@ static int test_event(const struct evlist_test *e) ret = e->check (evlist); }parse_events_error__exit (&err); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
tools/perf/arch/x86/tests/topdown.c +2 -2
@@ -56,7 +56,7 @@ static int event_cb(void *state, struct pmu_event_info *info) *ret =TEST_FAIL ; } } -evlist__delete (evlist); +evlist__put (evlist);return 0 ; }
@@ -174,7 +174,7 @@ static int test_sort(const char *str, int expected_slots_group_size,CHECK_COND (slots_seen,"slots seen" ); ret =TEST_OK ; out_err: -evlist__delete (evlist); +evlist__put (evlist);parse_events_error__exit (&err);return ret; }
tools/perf/arch/x86/util/iostat.c +1 -1
@@ -337,7 +337,7 @@ int iostat_prepare(struct evlist *evlist, struct perf_stat_config *config)if (evlist->core.nr_entries >0 ) {pr_warning ("The -e and -M options are not supported." "All chosen events/metrics will be dropped\n" ); -evlist__delete (evlist); +evlist__put (evlist); evlist =evlist__new ();if (!evlist)return -ENOMEM ;
tools/perf/bench/evlist-open-close.c +9 -9
@@ -76,7 +76,7 @@ static struct evlist *bench__create_evlist(char *evstr, const char *uid_str)parse_events_error__exit (&err);pr_err ("Run 'perf list' for a list of valid events\n" ); ret =1 ; - goto out_delete_evlist; + goto out_put_evlist; }parse_events_error__exit (&err);if (uid_str) {
@@ -85,24 +85,24 @@ static struct evlist *bench__create_evlist(char *evstr, const char *uid_str)if (uid ==UINT_MAX ) {pr_err ("Invalid User: %s" , uid_str); ret = -EINVAL ; - goto out_delete_evlist; + goto out_put_evlist; } ret =parse_uid_filter (evlist, uid);if (ret) - goto out_delete_evlist; + goto out_put_evlist; } ret =evlist__create_maps (evlist, &opts.target);if (ret <0 ) {pr_err ("Not enough memory to create thread/cpu maps\n" ); - goto out_delete_evlist; + goto out_put_evlist; }evlist__config (evlist, &opts,NULL );return evlist; -out_delete_evlist: -evlist__delete (evlist); +out_put_evlist: +evlist__put (evlist);return NULL ; }
@@ -151,7 +151,7 @@ static int bench_evlist_open_close__run(char *evstr, const char *uid_str) evlist->core.nr_entries, evlist__count_evsel_fds(evlist));printf (" Number of iterations:\t%d\n" , iterations); -evlist__delete (evlist); +evlist__put (evlist);for (i =0 ; i < iterations; i++) {pr_debug ("Started iteration %d\n" , i);
@@ -162,7 +162,7 @@ static int bench_evlist_open_close__run(char *evstr, const char *uid_str)gettimeofday (&start,NULL ); err =bench__do_evlist_open_close (evlist);if (err) { -evlist__delete (evlist); +evlist__put (evlist);return err; }
@@ -171,7 +171,7 @@ static int bench_evlist_open_close__run(char *evstr, const char *uid_str) runtime_us =timeval2usec (&diff);update_stats (&time_stats, runtime_us); -evlist__delete (evlist); +evlist__put (evlist);pr_debug ("Iteration %d took:\t%" PRIu64"us\n" , i, runtime_us); }
tools/perf/builtin-ftrace.c +4 -4
@@ -1999,20 +1999,20 @@ int cmd_ftrace(int argc, const char **argv) ret =evlist__create_maps (ftrace.evlist, &ftrace.target);if (ret <0 ) - goto out_delete_evlist; + goto out_put_evlist;if (argc) { ret =evlist__prepare_workload (ftrace.evlist, &ftrace.target, argv, false, ftrace__workload_exec_failed_signal);if (ret <0 ) - goto out_delete_evlist; + goto out_put_evlist; } ret =cmd_func (&ftrace); -out_delete_evlist: -evlist__delete (ftrace.evlist); +out_put_evlist: +evlist__put (ftrace.evlist); out_delete_filters:delete_filter_func (&ftrace.filters);
tools/perf/builtin-kvm.c +2 -2
@@ -1810,7 +1810,7 @@ static struct evlist *kvm_live_event_list(void) out:if (err) { -evlist__delete (evlist); +evlist__put (evlist); evlist =NULL ; }
@@ -1941,7 +1941,7 @@ static int kvm_events_live(struct perf_kvm_stat *kvm, out:perf_session__delete (kvm->session); kvm->session =NULL ; -evlist__delete (kvm->evlist); +evlist__put (kvm->evlist);return err; }
tools/perf/builtin-lock.c +1 -1
@@ -2149,7 +2149,7 @@ static int __cmd_contention(int argc, const char **argv) out_delete:lock_filter_finish (); -evlist__delete (con.evlist); +evlist__put (con.evlist);lock_contention_finish (&con);perf_session__delete (session);perf_env__exit (&host_env);
tools/perf/builtin-record.c +2 -2
@@ -4291,7 +4291,7 @@ int cmd_record(int argc, const char **argv) goto out;evlist__splice_list_tail (rec->evlist, &def_evlist->core.entries); -evlist__delete (def_evlist); +evlist__put (def_evlist); }if (rec->opts.target.tid && !rec->opts.no_inherit_set)
@@ -4401,7 +4401,7 @@ int cmd_record(int argc, const char **argv)auxtrace_record__free (rec->itr); out_opts:evlist__close_control (rec->opts.ctl_fd, rec->opts.ctl_fd_ack, &rec->opts.ctl_fd_close); -evlist__delete (rec->evlist); +evlist__put (rec->evlist);return err; }
tools/perf/builtin-sched.c +3 -3
@@ -3913,7 +3913,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched, session =perf_session__new (&data, &sched->tool);if (IS_ERR (session)) {pr_err ("Perf session creation failed.\n" ); -evlist__delete (evlist); +evlist__put (evlist);return PTR_ERR (session); }
@@ -4012,7 +4012,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,else fprintf (stderr,"[ perf sched stats: Failed !! ]\n" ); -evlist__delete (evlist); +evlist__put (evlist);close (fd);return err; }
@@ -4916,7 +4916,7 @@ static int perf_sched__schedstat_live(struct perf_sched *sched,free_cpu_domain_info (cd_map, sv, nr); out:free_schedstat (&cpu_head); -evlist__delete (evlist); +evlist__put (evlist);return err; }
tools/perf/builtin-script.c +1 -1
@@ -2274,7 +2274,7 @@ static int script_find_metrics(const struct pmu_metric *pm, }pr_debug ("Found metric '%s' whose evsels match those of in the perf data\n" , pm->metric_name); -evlist__delete (metric_evlist); +evlist__put (metric_evlist); out:return 0 ; }
tools/perf/builtin-stat.c +5 -5
@@ -2119,7 +2119,7 @@ static int add_default_events(void) stat_config.user_requested_cpu_list, stat_config.system_wide, stat_config.hardware_aware_grouping) < 0) { -evlist__delete (metric_evlist); +evlist__put (metric_evlist); ret =-1 ;break ; }
@@ -2131,7 +2131,7 @@ static int add_default_events(void)metricgroup__copy_metric_events (evlist,/*cgrp=*/ NULL , &evlist->metric_events, &metric_evlist->metric_events); -evlist__delete (metric_evlist); +evlist__put (metric_evlist); }list_sort (/*priv=*/ NULL , &evlist->core.entries, default_evlist_evsel_cmp);
@@ -2152,7 +2152,7 @@ static int add_default_events(void)metricgroup__copy_metric_events (evsel_list,/*cgrp=*/ NULL , &evsel_list->metric_events, &evlist->metric_events); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
@@ -2387,7 +2387,7 @@ static int __cmd_report(int argc, const char **argv) perf_stat.session = session; stat_config.output = stderr; -evlist__delete (evsel_list); +evlist__put (evsel_list); evsel_list = session->evlist; ret =perf_session__process_events (session);
@@ -3066,7 +3066,7 @@ int cmd_stat(int argc, const char **argv)if (smi_cost && smi_reset)sysfs__write_int (FREEZE_ON_SMI_PATH ,0 ); -evlist__delete (evsel_list); +evlist__put (evsel_list);evlist__close_control (stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
tools/perf/builtin-top.c +26 -26
184 lines not on this page · raw message
tools/perf/builtin-trace.c +13 -13
@@ -4418,7 +4418,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (trace->summary_bpf) {if (trace_prepare_bpf_summary (trace->summary_mode) <0 ) - goto out_delete_evlist; + goto out_put_evlist;if (trace->summary_only) goto create_maps;
@@ -4486,19 +4486,19 @@ static int trace__run(struct trace *trace, int argc, const char **argv) err =evlist__create_maps (evlist, &trace->opts.target);if (err <0 ) {fprintf (trace->output,"Problems parsing the target to trace, check your options!\n" ); - goto out_delete_evlist; + goto out_put_evlist; } err =trace__symbols_init (trace, argc, argv, evlist);if (err <0 ) {fprintf (trace->output,"Problems initializing symbol libraries!\n" ); - goto out_delete_evlist; + goto out_put_evlist; }if (trace->summary_mode ==SUMMARY__BY_TOTAL && !trace->summary_bpf) { trace->syscall_stats =alloc_syscall_stats ();if (!trace->syscall_stats) - goto out_delete_evlist; + goto out_put_evlist; }evlist__config (evlist, &trace->opts, &callchain_param);
@@ -4507,7 +4507,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv) err =evlist__prepare_workload (evlist, &trace->opts.target, argv, false,NULL );if (err <0 ) {fprintf (trace->output,"Couldn't run the workload!\n" ); - goto out_delete_evlist; + goto out_put_evlist; } workload_pid = evlist->workload.pid; }
@@ -4555,7 +4555,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv) err =trace__expand_filters (trace, &evsel);if (err) - goto out_delete_evlist; + goto out_put_evlist; err =evlist__apply_filters (evlist, &evsel, &trace->opts.target);if (err <0 ) goto out_error_apply_filters;
@@ -4672,12 +4672,12 @@ static int trace__run(struct trace *trace, int argc, const char **argv) } } -out_delete_evlist: +out_put_evlist:trace_cleanup_bpf_summary ();delete_syscall_stats (trace->syscall_stats);trace__symbols__exit (trace);evlist__free_syscall_tp_fields (evlist); -evlist__delete (evlist); +evlist__put (evlist);cgroup__put (trace->cgroup); trace->evlist =NULL ; trace->live = false;
@@ -4702,21 +4702,21 @@ static int trace__run(struct trace *trace, int argc, const char **argv) out_error:fprintf (trace->output,"%s\n" , errbuf); - goto out_delete_evlist; + goto out_put_evlist; out_error_apply_filters:fprintf (trace->output,"Failed to set filter \"%s\" on event %s: %m\n" , evsel->filter,evsel__name (evsel)); - goto out_delete_evlist; + goto out_put_evlist; } out_error_mem:fprintf (trace->output,"Not enough memory to run!\n" ); - goto out_delete_evlist; + goto out_put_evlist; out_errno:fprintf (trace->output,"%m\n" ); - goto out_delete_evlist; + goto out_put_evlist; }static int trace__replay (struct trace *trace)
@@ -5396,7 +5396,7 @@ static void trace__exit(struct trace *trace)zfree (&trace->syscalls.table); }zfree (&trace->perfconfig_events); -evlist__delete (trace->evlist); +evlist__put (trace->evlist); trace->evlist =NULL ;ordered_events__free (&trace->oe.data);#ifdef HAVE_LIBBPF_SUPPORT
tools/perf/tests/backward-ring-buffer.c +9 -9
@@ -111,7 +111,7 @@ static int test__backward_ring_buffer(struct test_suite *test __maybe_unused, in err =evlist__create_maps (evlist, &opts.target);if (err <0 ) {pr_debug ("Not enough memory to create thread/cpu maps\n" ); - goto out_delete_evlist; + goto out_put_evlist; }parse_events_error__init (&parse_error);
@@ -124,7 +124,7 @@ static int test__backward_ring_buffer(struct test_suite *test __maybe_unused, inif (err) {pr_debug ("Failed to parse tracepoint event, try use root\n" ); ret =TEST_SKIP ; - goto out_delete_evlist; + goto out_put_evlist; }evlist__config (evlist, &opts,NULL );
@@ -133,19 +133,19 @@ static int test__backward_ring_buffer(struct test_suite *test __maybe_unused, inif (err <0 ) {pr_debug ("perf_evlist__open: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf))); - goto out_delete_evlist; + goto out_put_evlist; } ret =TEST_FAIL ; err =do_test (evlist, opts.mmap_pages, &sample_count, &comm_count);if (err !=TEST_OK ) - goto out_delete_evlist; + goto out_put_evlist;if ((sample_count !=NR_ITERS ) || (comm_count !=NR_ITERS )) {pr_err ("Unexpected counter: sample_count=%d, comm_count=%d\n" , sample_count, comm_count); - goto out_delete_evlist; + goto out_put_evlist; }evlist__close (evlist);
@@ -154,16 +154,16 @@ static int test__backward_ring_buffer(struct test_suite *test __maybe_unused, inif (err <0 ) {pr_debug ("perf_evlist__open: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf))); - goto out_delete_evlist; + goto out_put_evlist; } err =do_test (evlist,1 , &sample_count, &comm_count);if (err !=TEST_OK ) - goto out_delete_evlist; + goto out_put_evlist; ret =TEST_OK ; -out_delete_evlist: -evlist__delete (evlist); +out_put_evlist: +evlist__put (evlist);return ret; }
tools/perf/tests/code-reading.c +2 -2
@@ -810,7 +810,7 @@ static int do_test_code_reading(bool try_kcore) }perf_evlist__set_maps (&evlist->core,NULL ,NULL ); -evlist__delete (evlist); +evlist__put (evlist); evlist =NULL ;continue ; }
@@ -847,7 +847,7 @@ static int do_test_code_reading(bool try_kcore) out_put:thread__put (thread); out_err: -evlist__delete (evlist); +evlist__put (evlist);perf_cpu_map__put (cpus);perf_thread_map__put (threads);machine__delete (machine);
tools/perf/tests/event-times.c +2 -2
@@ -186,7 +186,7 @@ static int test_times(int (attach)(struct evlist *), err =attach (evlist);if (err ==TEST_SKIP ) {pr_debug (" SKIP : not enough rights\n" ); -evlist__delete (evlist); +evlist__put (evlist);return err; }
@@ -205,7 +205,7 @@ static int test_times(int (attach)(struct evlist *), count.ena, count.run); out_err: -evlist__delete (evlist); +evlist__put (evlist);return !err ?TEST_OK :TEST_FAIL ; }
tools/perf/tests/event_update.c +1 -1
@@ -117,7 +117,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtesTEST_ASSERT_VAL ("failed to synthesize attr update cpus" , !perf_event__synthesize_event_update_cpus (&tmp.tool, evsel, process_event_cpus)); -evlist__delete (evlist); +evlist__put (evlist);return 0 ; }
tools/perf/tests/evsel-roundtrip-name.c +4 -4
@@ -33,7 +33,7 @@ static int perf_evsel__roundtrip_cache_name_test(void)if (err) {pr_debug ("Failure to parse cache event '%s' possibly as PMUs don't support it" , name); -evlist__delete (evlist); +evlist__put (evlist);continue ; }evlist__for_each_entry (evlist, evsel) {
@@ -42,7 +42,7 @@ static int perf_evsel__roundtrip_cache_name_test(void) ret =TEST_FAIL ; } } -evlist__delete (evlist); +evlist__put (evlist); } } }
@@ -66,7 +66,7 @@ static int perf_evsel__name_array_test(const char *const names[], int nr_names)if (err) {pr_debug ("failed to parse event '%s', err %d\n" , names[i], err); -evlist__delete (evlist); +evlist__put (evlist); ret =TEST_FAIL ;continue ; }
@@ -76,7 +76,7 @@ static int perf_evsel__name_array_test(const char *const names[], int nr_names) ret =TEST_FAIL ; } } -evlist__delete (evlist); +evlist__put (evlist); }return ret; }
tools/perf/tests/expand-cgroup.c +4 -4
@@ -106,7 +106,7 @@ static int expand_default_events(void)TEST_ASSERT_VAL ("failed to get evlist" , evlist); ret =test_expand_events (evlist); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
@@ -133,7 +133,7 @@ static int expand_group_events(void) ret =test_expand_events (evlist); out:parse_events_error__exit (&err); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
@@ -164,7 +164,7 @@ static int expand_libpfm_events(void) ret =test_expand_events (evlist); out: -evlist__delete (evlist); +evlist__put (evlist);return ret; }
@@ -188,7 +188,7 @@ static int expand_metric_events(void) ret =test_expand_events (evlist); out: -evlist__delete (evlist); +evlist__put (evlist);return ret; }
tools/perf/tests/hists_cumulate.c +1 -1
@@ -743,7 +743,7 @@ static int test__hists_cumulate(struct test_suite *test __maybe_unused, int subt out:/* tear down everything */ -evlist__delete (evlist); +evlist__put (evlist);machines__exit (&machines);put_fake_samples ();
tools/perf/tests/hists_filter.c +1 -1
@@ -331,7 +331,7 @@ static int test__hists_filter(struct test_suite *test __maybe_unused, int subtes out:/* tear down everything */ -evlist__delete (evlist); +evlist__put (evlist);reset_output_field ();machines__exit (&machines);put_fake_samples ();
tools/perf/tests/hists_link.c +1 -1
@@ -352,7 +352,7 @@ static int test__hists_link(struct test_suite *test __maybe_unused, int subtest out:/* tear down everything */ -evlist__delete (evlist); +evlist__put (evlist);reset_output_field ();machines__exit (&machines);put_fake_samples ();
tools/perf/tests/hists_output.c +1 -1
@@ -630,7 +630,7 @@ static int test__hists_output(struct test_suite *test __maybe_unused, int subtes out:/* tear down everything */ -evlist__delete (evlist); +evlist__put (evlist);machines__exit (&machines);put_fake_samples ();
tools/perf/tests/hwmon_pmu.c +1 -1
@@ -215,7 +215,7 @@ static int do_test(size_t i, bool with_pmu, bool with_alias) out:parse_events_error__exit (&err); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
tools/perf/tests/keep-tracking.c +1 -1
@@ -153,7 +153,7 @@ static int test__keep_tracking(struct test_suite *test __maybe_unused, int subte out_err:if (evlist) {evlist__disable (evlist); -evlist__delete (evlist); +evlist__put (evlist); }perf_cpu_map__put (cpus);perf_thread_map__put (threads);
tools/perf/tests/mmap-basic.c +9 -9
@@ -94,7 +94,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest/* Permissions failure, flag the failure as a skip. */ err =TEST_SKIP ; } - goto out_delete_evlist; + goto out_put_evlist; } evsels[i]->core.attr.wakeup_events =1 ;
@@ -106,7 +106,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtestpr_debug ("failed to open counter: %s, " "tweak /proc/sys/kernel/perf_event_paranoid?\n" ,str_error_r (errno, sbuf,sizeof (sbuf))); - goto out_delete_evlist; + goto out_put_evlist; } nr_events[i] =0 ;
@@ -116,7 +116,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtestif (evlist__mmap (evlist,128 ) <0 ) {pr_debug ("failed to mmap events: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf))); - goto out_delete_evlist; + goto out_put_evlist; }for (i =0 ; i < nsyscalls; ++i)
@@ -134,7 +134,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtestif (event->header.type !=PERF_RECORD_SAMPLE ) {pr_debug ("unexpected %s event\n" ,perf_event__name (event->header.type)); - goto out_delete_evlist; + goto out_put_evlist; }perf_sample__init (&sample,/*all=*/ false);
@@ -142,7 +142,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtestif (err) {pr_err ("Can't parse sample, err = %d\n" , err);perf_sample__exit (&sample); - goto out_delete_evlist; + goto out_put_evlist; } err =-1 ;
@@ -153,7 +153,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtestif (evsel ==NULL ) {pr_debug ("event with id %" PRIu64" doesn't map to an evsel\n" , sample.id); - goto out_delete_evlist; + goto out_put_evlist; } nr_events[evsel->core.idx]++;perf_mmap__consume (&md->core);
@@ -168,12 +168,12 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest expected_nr_events[evsel->core.idx], evsel__name(evsel), nr_events[evsel->core.idx]); err =-1 ; - goto out_delete_evlist; + goto out_put_evlist; } } -out_delete_evlist: -evlist__delete (evlist); +out_put_evlist: +evlist__put (evlist); out_free_cpus:perf_cpu_map__put (cpus); out_free_threads:
tools/perf/tests/openat-syscall-tp-fields.c +9 -9
@@ -51,7 +51,7 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unusedif (IS_ERR (evsel)) {pr_debug ("%s: evsel__newtp\n" , __func__); ret =PTR_ERR (evsel) == -EACCES ?TEST_SKIP :TEST_FAIL ; - goto out_delete_evlist; + goto out_put_evlist; }evlist__add (evlist, evsel);
@@ -59,7 +59,7 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unused err =evlist__create_maps (evlist, &opts.target);if (err <0 ) {pr_debug ("%s: evlist__create_maps\n" , __func__); - goto out_delete_evlist; + goto out_put_evlist; }evsel__config (evsel, &opts,NULL );
@@ -70,14 +70,14 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unusedif (err <0 ) {pr_debug ("perf_evlist__open: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf))); - goto out_delete_evlist; + goto out_put_evlist; } err =evlist__mmap (evlist,UINT_MAX );if (err <0 ) {pr_debug ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf))); - goto out_delete_evlist; + goto out_put_evlist; }evlist__enable (evlist);
@@ -115,7 +115,7 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unusedif (err) {pr_debug ("Can't parse sample, err = %d\n" , err);perf_sample__exit (&sample); - goto out_delete_evlist; + goto out_put_evlist; } tp_flags =perf_sample__intval (&sample,"flags" );
@@ -126,7 +126,7 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unused (tp_flags & flags) != flags) {pr_debug ("%s: Expected flags=%#x, got %#x\n" , __func__, flags, tp_flags); - goto out_delete_evlist; + goto out_put_evlist; } goto out_ok;
@@ -139,13 +139,13 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unusedif (++nr_polls >5 ) {pr_debug ("%s: no events!\n" , __func__); - goto out_delete_evlist; + goto out_put_evlist; } } out_ok: ret =TEST_OK ; -out_delete_evlist: -evlist__delete (evlist); +out_put_evlist: +evlist__put (evlist); out:return ret; }
tools/perf/tests/parse-events.c +2 -2
@@ -2568,7 +2568,7 @@ static int test_event(const struct evlist_test *e) ret = e->check (evlist); }parse_events_error__exit (&err); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
@@ -2594,7 +2594,7 @@ static int test_event_fake_pmu(const char *str) }parse_events_error__exit (&err); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
tools/perf/tests/parse-metric.c +2 -2
@@ -84,7 +84,7 @@ static int __compute_metric(const char *name, struct value *vals, cpus =perf_cpu_map__new ("0" );if (!cpus) { -evlist__delete (evlist); +evlist__put (evlist);return -ENOMEM ; }
@@ -113,7 +113,7 @@ static int __compute_metric(const char *name, struct value *vals,/* ... cleanup. */ evlist__free_stats (evlist);perf_cpu_map__put (cpus); -evlist__delete (evlist); +evlist__put (evlist);return err; }
tools/perf/tests/parse-no-sample-id-all.c +1 -1
@@ -49,7 +49,7 @@ static int process_events(union perf_event **events, size_t count)for (i =0 ; i < count && !err; i++) err =process_event (&evlist, events[i]); -evlist__delete (evlist); +evlist__put (evlist);return err; }
tools/perf/tests/perf-record.c +9 -9
@@ -105,7 +105,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest err =evlist__create_maps (evlist, &opts.target);if (err <0 ) {pr_debug ("Not enough memory to create thread/cpu maps\n" ); - goto out_delete_evlist; + goto out_put_evlist; }/*
@@ -117,7 +117,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest err =evlist__prepare_workload (evlist, &opts.target, argv, false,NULL );if (err <0 ) {pr_debug ("Couldn't run the workload!\n" ); - goto out_delete_evlist; + goto out_put_evlist; }/*
@@ -134,7 +134,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestpr_debug ("sched__get_first_possible_cpu: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));evlist__cancel_workload (evlist); - goto out_delete_evlist; + goto out_put_evlist; } cpu = err;
@@ -146,7 +146,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestpr_debug ("sched_setaffinity: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));evlist__cancel_workload (evlist); - goto out_delete_evlist; + goto out_put_evlist; }/*
@@ -158,7 +158,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestpr_debug ("perf_evlist__open: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));evlist__cancel_workload (evlist); - goto out_delete_evlist; + goto out_put_evlist; }/*
@@ -171,7 +171,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestpr_debug ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));evlist__cancel_workload (evlist); - goto out_delete_evlist; + goto out_put_evlist; }/*
@@ -209,7 +209,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestif (verbose >0 )perf_event__fprintf (event,NULL , stderr);pr_debug ("Couldn't parse sample\n" ); - goto out_delete_evlist; + goto out_put_evlist; }if (verbose >0 ) {
@@ -350,9 +350,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestpr_debug ("PERF_RECORD_MMAP for %s missing!\n" ,"[vdso]" ); ++errs; } -out_delete_evlist: +out_put_evlist:CPU_FREE (cpu_mask); -evlist__delete (evlist); +evlist__put (evlist); out:perf_sample__exit (&sample);if (err == -EACCES )
tools/perf/tests/perf-time-to-tsc.c +1 -1
@@ -201,7 +201,7 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su err =TEST_OK ; out_err: -evlist__delete (evlist); +evlist__put (evlist);perf_cpu_map__put (cpus);perf_thread_map__put (threads);return err;
tools/perf/tests/pfm.c +2 -2
@@ -80,7 +80,7 @@ static int test__pfm_events(struct test_suite *test __maybe_unused,evlist__nr_groups (evlist), 0); -evlist__delete (evlist); +evlist__put (evlist); }return 0 ; }
@@ -165,7 +165,7 @@ static int test__pfm_group(struct test_suite *test __maybe_unused,evlist__nr_groups (evlist), table[i].nr_groups); -evlist__delete (evlist); +evlist__put (evlist); }return 0 ; }
tools/perf/tests/pmu-events.c +3 -3
@@ -798,7 +798,7 @@ static int check_parse_id(const char *id, struct parse_events_error *error) /*warn_if_reordered=*/true, /*fake_tp=*/false);free (dup); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
@@ -865,7 +865,7 @@ static int test__parsing_callback(const struct pmu_metric *pm, cpus =perf_cpu_map__new ("0" );if (!cpus) { -evlist__delete (evlist); +evlist__put (evlist);return -ENOMEM ; }
@@ -919,7 +919,7 @@ static int test__parsing_callback(const struct pmu_metric *pm,/* ... cleanup. */ evlist__free_stats (evlist);perf_cpu_map__put (cpus); -evlist__delete (evlist); +evlist__put (evlist);return err; }
tools/perf/tests/pmu.c +2 -2
@@ -294,7 +294,7 @@ static int test__pmu_config_helpers(struct test_suite *test __maybe_unused, ret =TEST_OK ; err_out:parse_events_terms__exit (&terms); -evlist__delete (evlist); +evlist__put (evlist);test_pmu_put (dir, pmu);return ret; }
@@ -346,7 +346,7 @@ static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest ret =TEST_OK ; err_out:parse_events_error__exit (&err); -evlist__delete (evlist); +evlist__put (evlist);test_pmu_put (dir, pmu);return ret; }
tools/perf/tests/sw-clock.c +7 -7
@@ -59,7 +59,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) evsel =evsel__new (&attr);if (evsel ==NULL ) {pr_debug ("evsel__new\n" ); - goto out_delete_evlist; + goto out_put_evlist; }evlist__add (evlist, evsel);
@@ -68,7 +68,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)if (!cpus || !threads) { err = -ENOMEM ;pr_debug ("Not enough memory to create thread/cpu maps\n" ); - goto out_delete_evlist; + goto out_put_evlist; }perf_evlist__set_maps (&evlist->core, cpus, threads);
@@ -80,14 +80,14 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)pr_debug ("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64" in this test.\n" ,str_error_r (errno, sbuf,sizeof (sbuf)), knob, (u64 )attr.sample_freq); - goto out_delete_evlist; + goto out_put_evlist; } err =evlist__mmap (evlist,128 );if (err <0 ) {pr_debug ("failed to mmap event: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf))); - goto out_delete_evlist; + goto out_put_evlist; }evlist__enable (evlist);
@@ -113,7 +113,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)if (err <0 ) {pr_debug ("Error during parse sample\n" );perf_sample__exit (&sample); - goto out_delete_evlist; + goto out_put_evlist; } total_periods += sample.period;
@@ -131,10 +131,10 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) err =-1 ; } -out_delete_evlist: +out_put_evlist:perf_cpu_map__put (cpus);perf_thread_map__put (threads); -evlist__delete (evlist); +evlist__put (evlist);return err; }
tools/perf/tests/switch-tracking.c +1 -1
@@ -582,7 +582,7 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub out:if (evlist) {evlist__disable (evlist); -evlist__delete (evlist); +evlist__put (evlist); }perf_cpu_map__put (cpus);perf_thread_map__put (threads);
tools/perf/tests/task-exit.c +7 -7
@@ -74,7 +74,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _if (!cpus || !threads) { err = -ENOMEM ;pr_debug ("Not enough memory to create thread/cpu maps\n" ); - goto out_delete_evlist; + goto out_put_evlist; }perf_evlist__set_maps (&evlist->core, cpus, threads);
@@ -82,7 +82,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _ err =evlist__prepare_workload (evlist, &target, argv, false, workload_exec_failed_signal);if (err <0 ) {pr_debug ("Couldn't run the workload!\n" ); - goto out_delete_evlist; + goto out_put_evlist; } evsel =evlist__first (evlist);
@@ -101,14 +101,14 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _if (err <0 ) {pr_debug ("Couldn't open the evlist: %s\n" ,str_error_r (-err, sbuf,sizeof (sbuf))); - goto out_delete_evlist; + goto out_put_evlist; }if (evlist__mmap (evlist,128 ) <0 ) {pr_debug ("failed to mmap events: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf))); err =-1 ; - goto out_delete_evlist; + goto out_put_evlist; }evlist__start_workload (evlist);
@@ -133,7 +133,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _if (retry_count++ >1000 ) {pr_debug ("Failed after retrying 1000 times\n" ); err =-1 ; - goto out_delete_evlist; + goto out_put_evlist; } goto retry;
@@ -144,10 +144,10 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _ err =-1 ; } -out_delete_evlist: +out_put_evlist:perf_cpu_map__put (cpus);perf_thread_map__put (threads); -evlist__delete (evlist); +evlist__put (evlist);return err; }
tools/perf/tests/tool_pmu.c +1 -1
@@ -67,7 +67,7 @@ static int do_test(enum tool_pmu_event ev, bool with_pmu) out:parse_events_error__exit (&err); -evlist__delete (evlist); +evlist__put (evlist);return ret; }
tools/perf/tests/topology.c +1 -1
@@ -58,7 +58,7 @@ static int session_write_header(char *path) !perf_session__write_header (session, session->evlist, perf_data__fd(&data), true)); -evlist__delete (session->evlist); +evlist__put (session->evlist);perf_session__delete (session);return 0 ;
tools/perf/tests/uncore-event-sorting.c +1 -1
@@ -165,7 +165,7 @@ static int test__uncore_event_sorting(struct test_suite *test __maybe_unused, ret =TEST_OK ; out_err: -evlist__delete (evlist); +evlist__put (evlist);parse_events_error__exit (&err);zfree (&pmu_prefix);zfree (&m.event1);
tools/perf/util/cgroup.c +2 -2
@@ -520,8 +520,8 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgro cgrp_event_expanded = true; out_err: -evlist__delete (orig_list); -evlist__delete (tmp_list); +evlist__put (orig_list); +evlist__put (tmp_list);metricgroup__rblist_exit (&orig_metric_events);release_cgroup_list ();
tools/perf/util/data-convert-bt.c +1 -1
@@ -1362,7 +1362,7 @@ static void cleanup_events(struct perf_session *session)zfree (&evsel->priv); } -evlist__delete (evlist); +evlist__put (evlist); session->evlist =NULL ; }
tools/perf/util/evlist.c +15 -5
@@ -75,7 +75,7 @@ int sigqueue(pid_t pid, int sig, const union sigval value);#define FD (e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))#define SID (e, x, y) xyarray__entry(e->core.sample_id, x, y) -void evlist__init (struct evlist *evlist,struct perf_cpu_map *cpus, +static void evlist__init (struct evlist *evlist,struct perf_cpu_map *cpus,struct perf_thread_map *threads) {perf_evlist__init (&evlist->core);
@@ -88,6 +88,7 @@ void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, evlist->nr_br_cntr =-1 ;metricgroup__rblist_init (&evlist->metric_events);INIT_LIST_HEAD (&evlist->deferred_samples); +refcount_set (&evlist->refcnt,1 ); }struct evlist *evlist__new (void )
@@ -139,7 +140,7 @@ struct evlist *evlist__new_default(const struct target *target, bool sample_callreturn evlist; out_err: -evlist__delete (evlist); +evlist__put (evlist);return NULL ; }
@@ -148,13 +149,19 @@ struct evlist *evlist__new_dummy(void)struct evlist *evlist =evlist__new ();if (evlist &&evlist__add_dummy (evlist)) { -evlist__delete (evlist); +evlist__put (evlist); evlist =NULL ; }return evlist; } +struct evlist *evlist__get (struct evlist *evlist) +{ +refcount_inc (&evlist->refcnt); +return evlist; +} +/** * evlist__set_id_pos - set the positions of event ids. * @evlist: selected event list
@@ -193,7 +200,7 @@ static void evlist__purge(struct evlist *evlist) evlist->core.nr_entries =0 ; } -void evlist__exit (struct evlist *evlist) +static void evlist__exit (struct evlist *evlist) {metricgroup__rblist_exit (&evlist->metric_events);event_enable_timer__exit (&evlist->eet);
@@ -202,11 +209,14 @@ void evlist__exit(struct evlist *evlist)perf_evlist__exit (&evlist->core); } -void evlist__delete (struct evlist *evlist) +void evlist__put (struct evlist *evlist) {if (evlist ==NULL )return ; +if (!refcount_dec_and_test (&evlist->refcnt)) +return ; +evlist__free_stats (evlist);evlist__munmap (evlist);evlist__close (evlist);
tools/perf/util/evlist.h +3 -4
@@ -61,6 +61,7 @@ struct event_enable_timer;struct evlist {struct perf_evlist core; +refcount_t refcnt;bool enabled;bool no_affinity;int id_pos;
@@ -109,10 +110,8 @@ struct evsel_str_handler {struct evlist *evlist__new (void );struct evlist *evlist__new_default (const struct target *target,bool sample_callchains);struct evlist *evlist__new_dummy (void ); -void evlist__init (struct evlist *evlist,struct perf_cpu_map *cpus, -struct perf_thread_map *threads); -void evlist__exit (struct evlist *evlist); -void evlist__delete (struct evlist *evlist); +struct evlist *evlist__get (struct evlist *evlist); +void evlist__put (struct evlist *evlist);void evlist__add (struct evlist *evlist,struct evsel *entry);void evlist__remove (struct evlist *evlist,struct evsel *evsel);
tools/perf/util/expr.c +1 -1
@@ -468,7 +468,7 @@ double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const ret =parse_event (tmp, id) ?0 :1 ; } out: -evlist__delete (tmp); +evlist__put (tmp);return ret; }
tools/perf/util/header.c +11 -11
@@ -5187,7 +5187,7 @@ int perf_session__read_header(struct perf_session *session)pr_err ("Invalid ids section size %" PRIu64" for attr %d, not aligned to u64\n" , f_attr.ids.size, i); err = -EINVAL ; - goto out_delete_evlist; + goto out_put_evlist; }/*
@@ -5200,7 +5200,7 @@ int perf_session__read_header(struct perf_session *session)pr_err ("Invalid ids section size %" PRIu64" for attr %d, too many IDs\n" , f_attr.ids.size, i); err = -EINVAL ; - goto out_delete_evlist; + goto out_put_evlist; }/*
@@ -5213,19 +5213,19 @@ int perf_session__read_header(struct perf_session *session)pr_err ("Invalid ids section for attr %d: offset=%" PRIu64" size=%" PRIu64" exceeds file size %" PRIu64"\n" , i, f_attr.ids.offset, f_attr.ids.size, (u64 )input_stat.st_size); err = -EINVAL ; - goto out_delete_evlist; + goto out_put_evlist; } tmp =lseek (fd,0 ,SEEK_CUR ); evsel =evsel__new (&f_attr.attr);if (evsel ==NULL ) - goto out_delete_evlist; + goto out_put_evlist; evsel->needs_swap = header->needs_swap;/* * Do it before so that if perf_evsel__alloc_id fails, this -* entry gets purged too at evlist__delete(). +* entry gets purged too at evlist__put(). */ evlist__add (session->evlist, evsel);
@@ -5236,7 +5236,7 @@ int perf_session__read_header(struct perf_session *session)* hattr->ids threads. */ if (perf_evsel__alloc_id (&evsel->core,1 , nr_ids)) - goto out_delete_evlist; + goto out_put_evlist;lseek (fd, f_attr.ids.offset,SEEK_SET );
@@ -5266,18 +5266,18 @@ int perf_session__read_header(struct perf_session *session) err =perf_header__process_sections (header, fd, &session->tevent, perf_file_section__process);if (err <0 ) - goto out_delete_evlist; + goto out_put_evlist;if (evlist__prepare_tracepoint_events (session->evlist, session->tevent.pevent)) { err = -ENOMEM ; - goto out_delete_evlist; + goto out_put_evlist; }#else err =perf_header__process_sections (header, fd,NULL , perf_file_section__process);if (err <0 ) - goto out_delete_evlist; + goto out_put_evlist;#endif }
@@ -5303,8 +5303,8 @@ int perf_session__read_header(struct perf_session *session) out_errno:return -errno; -out_delete_evlist: -evlist__delete (session->evlist); +out_put_evlist: +evlist__put (session->evlist); session->evlist =NULL ;return err; }
tools/perf/util/metricgroup.c +3 -3
@@ -214,7 +214,7 @@ static void metric__free(struct metric *m)zfree (&m->metric_refs);expr__ctx_free (m->pctx);zfree (&m->modifier); -evlist__delete (m->evlist); +evlist__put (m->evlist);free (m); }
@@ -1331,7 +1331,7 @@ static int parse_ids(bool metric_no_merge, bool fake_pmu, parsed_evlist =NULL ; err_out:parse_events_error__exit (&parse_error); -evlist__delete (parsed_evlist); +evlist__put (parsed_evlist);strbuf_release (&events);return ret; }
@@ -1542,7 +1542,7 @@ static int parse_groups(struct evlist *perf_evlist,if (combined_evlist) {evlist__splice_list_tail (perf_evlist, &combined_evlist->core.entries); -evlist__delete (combined_evlist); +evlist__put (combined_evlist); }list_for_each_entry (m, &metric_list, nd) {
tools/perf/util/parse-events.c +2 -2
@@ -2343,7 +2343,7 @@ int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filte/* * There are 2 users - builtin-record and builtin-test objects. -* Both call evlist__delete in case of error, so we dont +* Both call evlist__put in case of error, so we dont * need to bother. */ return ret;
@@ -2546,7 +2546,7 @@ int parse_events_option_new_evlist(const struct option *opt, const char *str, in } ret =parse_events_option (opt, str, unset);if (ret) { -evlist__delete (*args->evlistp); +evlist__put (*args->evlistp); *args->evlistp =NULL ; }
tools/perf/util/perf_api_probe.c +1 -1
@@ -57,7 +57,7 @@ static int perf_do_probe_api(setup_probe_fn_t fn, struct perf_cpu cpu, const cha err =0 ; out_delete: -evlist__delete (evlist); +evlist__put (evlist);return err; }
tools/perf/util/python.c +110 -132
451 lines not on this page · raw message
tools/perf/util/record.c +1 -1
@@ -264,7 +264,7 @@ bool evlist__can_select_event(struct evlist *evlist, const char *str) ret = true; out_delete: -evlist__delete (temp_evlist); +evlist__put (temp_evlist);return ret; }
tools/perf/util/session.c +1 -1
@@ -264,7 +264,7 @@ void perf_session__delete(struct perf_session *session)machines__exit (&session->machines);if (session->data) {if (perf_data__is_read (session->data)) -evlist__delete (session->evlist); +evlist__put (session->evlist);perf_data__close (session->data); }#ifdef HAVE_LIBTRACEEVENT
tools/perf/util/sideband_evlist.c +8 -8
@@ -102,7 +102,7 @@ int evlist__start_sb_thread(struct evlist *evlist, struct target *target)return 0 ;if (evlist__create_maps (evlist, target)) - goto out_delete_evlist; + goto out_put_evlist;if (evlist->core.nr_entries >1 ) {bool can_sample_identifier =perf_can_sample_identifier ();
@@ -116,25 +116,25 @@ int evlist__start_sb_thread(struct evlist *evlist, struct target *target)evlist__for_each_entry (evlist, counter) {if (evsel__open (counter, evlist->core.user_requested_cpus, evlist->core.threads) <0 ) - goto out_delete_evlist; + goto out_put_evlist; }if (evlist__mmap (evlist,UINT_MAX )) - goto out_delete_evlist; + goto out_put_evlist;evlist__for_each_entry (evlist, counter) {if (evsel__enable (counter)) - goto out_delete_evlist; + goto out_put_evlist; } evlist->thread.done =0 ;if (pthread_create (&evlist->thread.th,NULL , perf_evlist__poll_thread, evlist)) - goto out_delete_evlist; + goto out_put_evlist;return 0 ; -out_delete_evlist: -evlist__delete (evlist); +out_put_evlist: +evlist__put (evlist); evlist =NULL ;return -1 ; }
@@ -145,5 +145,5 @@ void evlist__stop_sb_thread(struct evlist *evlist)return ; evlist->thread.done =1 ;pthread_join (evlist->thread.th,NULL ); -evlist__delete (evlist); +evlist__put (evlist); }
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v21 6/21 perf evsel: Add reference count
In reply to Ian RogersAs with evlist this a no-op for most of the perf tool. The reference count is set to 1 at allocation, the put will see the 1, decrement it and perform the delete. The purpose for adding the reference count is for the python code. Prior to this change the python code would clone evsels, but this has issues if events are opened, etc. leading to assertion failures. With a reference count the same evsel can be used and the reference count incremented for the python usage. To not change the python evsel API getset functions are added for the evsel members, no set function is provided for size as it doesn't make sense to alter this.
--- tools/perf/builtin-trace.c | 12 +- tools/perf/tests/evsel-tp-sched.c | 4 +- tools/perf/tests/openat-syscall-all-cpus.c | 6 +- tools/perf/tests/openat-syscall.c | 6 +- tools/perf/util/bpf_counter_cgroup.c | 2 +- tools/perf/util/cgroup.c | 2 +- tools/perf/util/evlist.c | 2 +- tools/perf/util/evsel.c | 26 +- tools/perf/util/evsel.h | 11 +- tools/perf/util/parse-events.y | 2 +- tools/perf/util/pfm.c | 2 +- tools/perf/util/print-events.c | 2 +- tools/perf/util/python.c | 325 +++++++++++++++++---- tools/perf/util/session.c | 3 + 14 files changed, 321 insertions(+), 84 deletions(-)
tools/perf/builtin-trace.c +6 -6
@@ -460,10 +460,10 @@ static int evsel__init_tp_ptr_field(struct evsel *evsel, struct tp_field *field, ({ struct syscall_tp *sc = __evsel__syscall_tp(evsel);\ evsel__init_tp_ptr_field(evsel, &sc->name, #name); }) -static void evsel__delete_priv(struct evsel *evsel) +static void evsel__put_and_free_priv(struct evsel *evsel) { zfree(&evsel->priv); - evsel__delete(evsel); + evsel__put(evsel); } static int evsel__init_syscall_tp(struct evsel *evsel)
@@ -543,7 +543,7 @@ static struct evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *return evsel; out_delete: -evsel__delete_priv (evsel); +evsel__put_and_free_priv (evsel);return NULL ; }
@@ -3612,7 +3612,7 @@ static bool evlist__add_vfs_getname(struct evlist *evlist)list_del_init (&evsel->core.node); evsel->evlist =NULL ; -evsel__delete (evsel); +evsel__put (evsel); }return found;
@@ -3728,9 +3728,9 @@ static int trace__add_syscall_newtp(struct trace *trace)return ret; out_delete_sys_exit: -evsel__delete_priv (sys_exit); +evsel__put_and_free_priv (sys_exit); out_delete_sys_enter: -evsel__delete_priv (sys_enter); +evsel__put_and_free_priv (sys_enter); goto out; }
tools/perf/tests/evsel-tp-sched.c +2 -2
@@ -64,7 +64,7 @@ static int test__perf_evsel__tp_sched_test(struct test_suite *test __maybe_unuseif (evsel__test_field (evsel,"next_prio" ,4 , true)) ret =TEST_FAIL ; -evsel__delete (evsel); +evsel__put (evsel); evsel =evsel__newtp ("sched" ,"sched_wakeup" );
@@ -85,7 +85,7 @@ static int test__perf_evsel__tp_sched_test(struct test_suite *test __maybe_unuseif (evsel__test_field (evsel,"target_cpu" ,4 , true)) ret =TEST_FAIL ; -evsel__delete (evsel); +evsel__put (evsel);return ret; }
tools/perf/tests/openat-syscall-all-cpus.c +3 -3
@@ -59,7 +59,7 @@ static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __mayb"tweak /proc/sys/kernel/perf_event_paranoid?\n" , str_error_r(errno, sbuf, sizeof(sbuf))); err =TEST_SKIP ; - goto out_evsel_delete; + goto out_evsel_put; }perf_cpu_map__for_each_cpu (cpu, idx, cpus) {
@@ -116,8 +116,8 @@ static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __maybevsel__free_counts (evsel); out_close_fd:perf_evsel__close_fd (&evsel->core); -out_evsel_delete: -evsel__delete (evsel); +out_evsel_put: +evsel__put (evsel); out_cpu_map_delete:perf_cpu_map__put (cpus); out_thread_map_delete:
tools/perf/tests/openat-syscall.c +3 -3
@@ -42,7 +42,7 @@ static int test__openat_syscall_event(struct test_suite *test __maybe_unused,"tweak /proc/sys/kernel/perf_event_paranoid?\n" , str_error_r(errno, sbuf, sizeof(sbuf))); err =TEST_SKIP ; - goto out_evsel_delete; + goto out_evsel_put; }for (i =0 ; i < nr_openat_calls; ++i) {
@@ -64,8 +64,8 @@ static int test__openat_syscall_event(struct test_suite *test __maybe_unused, err =TEST_OK ; out_close_fd:perf_evsel__close_fd (&evsel->core); -out_evsel_delete: -evsel__delete (evsel); +out_evsel_put: +evsel__put (evsel); out_thread_map_delete:perf_thread_map__put (threads);return err;
tools/perf/util/bpf_counter_cgroup.c +1 -1
@@ -336,7 +336,7 @@ static int bperf_cgrp__destroy(struct evsel *evsel)return 0 ;bperf_cgroup_bpf__destroy (skel); -evsel__delete (cgrp_switch);// it'll destroy on_switch progs too +evsel__put (cgrp_switch);// it'll destroy on_switch progs too return 0 ; }
tools/perf/util/cgroup.c +1 -1
@@ -469,7 +469,7 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgro/* copy the list and set to the new cgroup. */ evlist__for_each_entry (orig_list, pos) { -struct evsel *evsel =evsel__clone (/*dest=*/ NULL , pos); +struct evsel *evsel =evsel__clone (pos);if (evsel ==NULL ) goto out_err;
tools/perf/util/evlist.c +1 -1
@@ -194,7 +194,7 @@ static void evlist__purge(struct evlist *evlist)evlist__for_each_entry_safe (evlist, n, pos) {list_del_init (&pos->core.node); pos->evlist =NULL ; -evsel__delete (pos); +evsel__put (pos); } evlist->core.nr_entries =0 ;
tools/perf/util/evsel.c +16 -10
@@ -393,10 +393,11 @@ bool evsel__is_function_event(struct evsel *evsel)#undef FUNCTION_EVENT } -void evsel__init (struct evsel *evsel, +static void evsel__init (struct evsel *evsel,struct perf_event_attr *attr,int idx) {perf_evsel__init (&evsel->core, attr, idx); +refcount_set (&evsel->refcnt,1 ); evsel->tracking = !idx; evsel->unit =strdup ("" ); evsel->scale =1.0 ;
@@ -477,7 +478,7 @@ static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src)* The assumption is that @orig is not configured nor opened yet. * So we only care about the attributes that can be set while it's parsed. */ -struct evsel *evsel__clone (struct evsel *dest,struct evsel *orig) +struct evsel *evsel__clone (struct evsel *orig) {struct evsel *evsel;
@@ -490,11 +491,7 @@ struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig)if (orig->bpf_obj)return NULL ; -if (dest) - evsel = dest; -else - evsel =evsel__new (&orig->core.attr); - + evsel =evsel__new (&orig->core.attr);if (evsel ==NULL )return NULL ;
@@ -579,7 +576,7 @@ struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig)return evsel; out_err: -evsel__delete (evsel); +evsel__put (evsel);return NULL ; }
@@ -638,6 +635,12 @@ struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx, boolreturn ERR_PTR (err); } +struct evsel *evsel__get (struct evsel *evsel) +{ +refcount_inc (&evsel->refcnt); +return evsel; +} +#ifdef HAVE_LIBTRACEEVENT struct tep_event *evsel__tp_format (struct evsel *evsel) {
@@ -1976,7 +1979,7 @@ void evsel__set_priv_destructor(void (*destructor)(void *priv)) evsel__priv_destructor = destructor; } -void evsel__exit (struct evsel *evsel) +static void evsel__exit (struct evsel *evsel) {assert (list_empty (&evsel->core.node));assert (evsel->evlist ==NULL );
@@ -2013,11 +2016,14 @@ void evsel__exit(struct evsel *evsel) } } -void evsel__delete (struct evsel *evsel) +void evsel__put (struct evsel *evsel) {if (!evsel)return ; +if (!refcount_dec_and_test (&evsel->refcnt)) +return ; +evsel__exit (evsel);free (evsel); }
tools/perf/util/evsel.h +6 -5
@@ -6,6 +6,7 @@#include <linux/list.h> #include <linux/perf_event.h> +#include <linux/refcount.h> #include <linux/types.h> #include <sys/types.h>
@@ -47,6 +48,7 @@ typedef int (evsel__sb_cb_t)(union perf_event *event, void *data);struct evsel {struct perf_evsel core;struct evlist *evlist; +refcount_t refcnt;off_t id_offset;int id_pos;int is_pos;
@@ -268,7 +270,7 @@ static inline struct evsel *evsel__new(struct perf_event_attr *attr)return evsel__new_idx (attr,0 ); } -struct evsel *evsel__clone (struct evsel *dest,struct evsel *orig); +struct evsel *evsel__clone (struct evsel *orig);int copy_config_terms (struct list_head *dst,struct list_head *src);void free_config_terms (struct list_head *config_terms);
@@ -283,14 +285,13 @@ static inline struct evsel *evsel__newtp(const char *sys, const char *name)return evsel__newtp_idx (sys, name,0 , true); } +struct evsel *evsel__get (struct evsel *evsel); +void evsel__put (struct evsel *evsel); +#ifdef HAVE_LIBTRACEEVENT struct tep_event *evsel__tp_format (struct evsel *evsel);#endif -void evsel__init (struct evsel *evsel,struct perf_event_attr *attr,int idx); -void evsel__exit (struct evsel *evsel); -void evsel__delete (struct evsel *evsel); -void evsel__set_priv_destructor (void (*destructor)(void *priv));struct callchain_param ;
tools/perf/util/parse-events.y +1 -1
@@ -47,7 +47,7 @@ static void free_list_evsel(struct list_head* list_evsel) list_for_each_entry_safe(evsel, tmp, list_evsel, core.node) { list_del_init(&evsel->core.node); - evsel__delete(evsel); + evsel__put(evsel); } free(list_evsel); }
tools/perf/util/pfm.c +1 -1
@@ -159,7 +159,7 @@ static bool is_libpfm_event_supported(const char *name, struct perf_cpu_map *cpu result = false;evsel__close (evsel); -evsel__delete (evsel); +evsel__put (evsel);return result; }
tools/perf/util/print-events.c +1 -1
@@ -174,7 +174,7 @@ bool is_event_supported(u8 type, u64 config) }evsel__close (evsel); -evsel__delete (evsel); +evsel__put (evsel); }perf_thread_map__put (tmap);
tools/perf/util/python.c +276 -49
460 lines not on this page · raw message
tools/perf/util/session.c +3 -0
@@ -1844,7 +1844,10 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,struct evsel *saved_evsel = sample->evsel; sample->evsel =evlist__id2evsel (evlist, sample->id); +if (sample->evsel) + sample->evsel =evsel__get (sample->evsel); ret = tool->callchain_deferred (tool, event, sample, machine); +evsel__put (sample->evsel); sample->evsel = saved_evsel;return ret; }
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Ian Rogers
reviewerPATCH v21 7/21 perf evlist: Add reference count checking
In reply to Ian RogersNow the evlist is reference counted, add reference count checking so that gets and puts are paired and easy to debug. Reference count checking is documented here:
https://perfwiki.github.io/main/reference-count-checking/
This large patch is adding accessors to evlist functions and switching to their use. There was some minor renaming as evlist__mmap is now an accessor to the mmap variable, and the original evlist__mmap is renamed to evlist__do_mmap.
--- tools/perf/arch/arm/util/cs-etm.c | 10 +- tools/perf/arch/arm64/util/arm-spe.c | 8 +- tools/perf/arch/arm64/util/hisi-ptt.c | 2 +- tools/perf/arch/x86/tests/hybrid.c | 20 +- tools/perf/arch/x86/util/auxtrace.c | 2 +- tools/perf/arch/x86/util/intel-bts.c | 6 +- tools/perf/arch/x86/util/intel-pt.c | 9 +- tools/perf/arch/x86/util/iostat.c | 12 +- tools/perf/bench/evlist-open-close.c | 11 +- tools/perf/builtin-annotate.c | 7 +- tools/perf/builtin-ftrace.c | 6 +- tools/perf/builtin-inject.c | 4 +- tools/perf/builtin-kvm.c | 10 +- tools/perf/builtin-kwork.c | 8 +- tools/perf/builtin-lock.c | 2 +- tools/perf/builtin-record.c | 91 ++--- tools/perf/builtin-report.c | 6 +- tools/perf/builtin-sched.c | 24 +- tools/perf/builtin-script.c | 13 +- tools/perf/builtin-stat.c | 73 ++-- tools/perf/builtin-top.c | 52 +-- tools/perf/builtin-trace.c | 22 +- tools/perf/tests/backward-ring-buffer.c | 8 +- tools/perf/tests/code-reading.c | 10 +- tools/perf/tests/event-times.c | 2 +- tools/perf/tests/event_update.c | 2 +- tools/perf/tests/expand-cgroup.c | 4 +- tools/perf/tests/hwmon_pmu.c | 5 +- tools/perf/tests/keep-tracking.c | 8 +- tools/perf/tests/mmap-basic.c | 6 +- tools/perf/tests/openat-syscall-tp-fields.c | 8 +- tools/perf/tests/parse-events.c | 135 +++---- tools/perf/tests/parse-metric.c | 4 +- tools/perf/tests/perf-record.c | 22 +- tools/perf/tests/perf-time-to-tsc.c | 10 +- tools/perf/tests/pfm.c | 8 +- tools/perf/tests/pmu-events.c | 5 +- tools/perf/tests/sample-parsing.c | 45 +-- tools/perf/tests/sw-clock.c | 6 +- tools/perf/tests/switch-tracking.c | 9 +- tools/perf/tests/task-exit.c | 6 +- tools/perf/tests/time-utils-test.c | 14 +- tools/perf/tests/tool_pmu.c | 5 +- tools/perf/tests/topology.c | 2 +- tools/perf/tests/uncore-event-sorting.c | 4 +- tools/perf/ui/browsers/annotate.c | 2 +- tools/perf/ui/browsers/hists.c | 22 +- tools/perf/util/amd-sample-raw.c | 2 +- tools/perf/util/annotate-data.c | 2 +- tools/perf/util/annotate.c | 10 +- tools/perf/util/auxtrace.c | 14 +- tools/perf/util/block-info.c | 4 +- tools/perf/util/bpf_counter.c | 2 +- tools/perf/util/bpf_counter_cgroup.c | 12 +- tools/perf/util/bpf_ftrace.c | 9 +- tools/perf/util/bpf_lock_contention.c | 12 +- tools/perf/util/bpf_off_cpu.c | 14 +- tools/perf/util/cgroup.c | 20 +- tools/perf/util/cs-etm.c | 5 +- tools/perf/util/evlist.c | 395 ++++++++++++-------- tools/perf/util/evlist.h | 251 ++++++++++++- tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 4 +- tools/perf/util/header.c | 47 +-- tools/perf/util/header.h | 2 +- tools/perf/util/intel-tpebs.c | 7 +- tools/perf/util/iostat.c | 2 +- tools/perf/util/iostat.h | 2 +- tools/perf/util/metricgroup.c | 6 +- tools/perf/util/parse-events.c | 6 +- tools/perf/util/pfm.c | 2 +- tools/perf/util/python.c | 90 +++-- tools/perf/util/record.c | 9 +- tools/perf/util/sample-raw.c | 4 +- tools/perf/util/session.c | 43 ++- tools/perf/util/sideband_evlist.c | 24 +- tools/perf/util/sort.c | 2 +- tools/perf/util/stat-display.c | 6 +- tools/perf/util/stat-shadow.c | 4 +- tools/perf/util/stat.c | 4 +- tools/perf/util/stream.c | 4 +- tools/perf/util/synthetic-events.c | 11 +- tools/perf/util/time-utils.c | 12 +- tools/perf/util/top.c | 4 +- 84 files changed, 1085 insertions(+), 718 deletions(-)
tools/perf/arch/arm/util/cs-etm.c +5 -5
@@ -201,7 +201,7 @@ static int cs_etm_validate_config(struct perf_pmu *cs_etm_pmu, {unsigned int idx;int err =0 ; -struct perf_cpu_map *event_cpus = evsel->evlist->core.user_requested_cpus; +struct perf_cpu_map *event_cpus =evlist__core (evsel->evlist)->user_requested_cpus;struct perf_cpu_map *intersect_cpus;struct perf_cpu cpu;
@@ -325,7 +325,7 @@ static int cs_etm_recording_options(struct auxtrace_record *itr, container_of(itr, struct cs_etm_recording, itr);struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;struct evsel *evsel, *cs_etm_evsel =NULL ; -struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;bool privileged =perf_event_paranoid_check (-1 );int err =0 ;
@@ -551,7 +551,7 @@ cs_etm_info_priv_size(struct auxtrace_record *itr, {unsigned int idx;int etmv3 =0 , etmv4 =0 , ete =0 ; -struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *event_cpus =evlist__core (evlist)->user_requested_cpus;struct perf_cpu_map *intersect_cpus;struct perf_cpu cpu;struct perf_pmu *cs_etm_pmu =cs_etm_get_pmu (itr);
@@ -790,7 +790,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,u32 offset;u64 nr_cpu, type;struct perf_cpu_map *cpu_map; -struct perf_cpu_map *event_cpus = session->evlist->core.user_requested_cpus; +struct perf_cpu_map *event_cpus =evlist__core (session->evlist)->user_requested_cpus;struct perf_cpu_map *online_cpus =perf_cpu_map__new_online_cpus ();struct cs_etm_recording *ptr = container_of(itr, struct cs_etm_recording, itr);
@@ -800,7 +800,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,if (priv_size !=cs_etm_info_priv_size (itr, session->evlist))return -EINVAL ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ;/* If the cpu_map has the "any" CPU all online CPUs are involved */
tools/perf/arch/arm64/util/arm-spe.c +4 -4
@@ -60,7 +60,7 @@ static bool arm_spe_is_set_freq(struct evsel *evsel)*/ static struct perf_cpu_map *arm_spe_find_cpus (struct evlist *evlist) { -struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *event_cpus =evlist__core (evlist)->user_requested_cpus;struct perf_cpu_map *online_cpus =perf_cpu_map__new_online_cpus ();struct perf_cpu_map *intersect_cpus;
@@ -157,7 +157,7 @@ static int arm_spe_info_fill(struct auxtrace_record *itr,if (priv_size !=arm_spe_info_priv_size (itr, session->evlist))return -EINVAL ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ; cpu_map =arm_spe_find_cpus (session->evlist);
@@ -363,7 +363,7 @@ static int arm_spe_setup_tracking_event(struct evlist *evlist, {int err;struct evsel *tracking_evsel; -struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;/* Add dummy event to keep tracking */ err =parse_event (evlist,"dummy:u" );
@@ -396,7 +396,7 @@ static int arm_spe_recording_options(struct auxtrace_record *itr,struct arm_spe_recording *sper = container_of(itr, struct arm_spe_recording, itr);struct evsel *evsel, *tmp; -struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;bool discard = false;int err;u64 discard_bit;
tools/perf/arch/arm64/util/hisi-ptt.c +1 -1
@@ -53,7 +53,7 @@ static int hisi_ptt_info_fill(struct auxtrace_record *itr,if (priv_size !=HISI_PTT_AUXTRACE_PRIV_SIZE )return -EINVAL ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ; auxtrace_info->type =PERF_AUXTRACE_HISI_PTT ;
tools/perf/arch/x86/tests/hybrid.c +10 -10
@@ -26,7 +26,7 @@ static int test__hybrid_hw_event_with_pmu(struct evlist *evlist) {struct evsel *evsel =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,1 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,1 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
@@ -38,7 +38,7 @@ static int test__hybrid_hw_group_event(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
@@ -57,7 +57,7 @@ static int test__hybrid_sw_hw_group_event(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_SOFTWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong leader" ,evsel__has_leader (evsel, leader));
@@ -74,7 +74,7 @@ static int test__hybrid_hw_sw_group_event(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
@@ -91,7 +91,7 @@ static int test__hybrid_group_modifier1(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
@@ -113,7 +113,7 @@ static int test__hybrid_raw1(struct evlist *evlist) {struct perf_evsel *evsel; -perf_evlist__for_each_evsel (&evlist->core, evsel) { +perf_evlist__for_each_evsel (evlist__core (evlist), evsel) {struct perf_pmu *pmu =perf_pmus__find_by_type (evsel->attr.type);TEST_ASSERT_VAL ("missing pmu" , pmu);
@@ -127,7 +127,7 @@ static int test__hybrid_raw2(struct evlist *evlist) {struct evsel *evsel =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,1 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,1 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_RAW == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,0x1a ));return TEST_OK ;
@@ -137,7 +137,7 @@ static int test__hybrid_cache_event(struct evlist *evlist) {struct evsel *evsel =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,1 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,1 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HW_CACHE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong config" ,0x2 == (evsel->core.attr.config &0xffffffff ));return TEST_OK ;
@@ -148,7 +148,7 @@ static int test__checkevent_pmu(struct evlist *evlist)struct evsel *evsel =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,1 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,1 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_RAW == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong config" ,10 == evsel->core.attr.config);TEST_ASSERT_VAL ("wrong config1" ,1 == evsel->core.attr.config1);
@@ -168,7 +168,7 @@ static int test__hybrid_hw_group_event_2(struct evlist *evlist)struct evsel *evsel, *leader; evsel = leader =evlist__first (evlist); -TEST_ASSERT_VAL ("wrong number of entries" ,2 == evlist->core.nr_entries); +TEST_ASSERT_VAL ("wrong number of entries" ,2 ==evlist__nr_entries (evlist));TEST_ASSERT_VAL ("wrong type" ,PERF_TYPE_HARDWARE == evsel->core.attr.type);TEST_ASSERT_VAL ("wrong hybrid type" ,test_hybrid_type (evsel,PERF_TYPE_RAW ));TEST_ASSERT_VAL ("wrong config" ,test_config (evsel,PERF_COUNT_HW_CPU_CYCLES ));
tools/perf/arch/x86/util/auxtrace.c +1 -1
@@ -55,7 +55,7 @@ struct auxtrace_record *auxtrace_record__init(struct evlist *evlist, int *err) {char buffer[64 ]; -struct perf_cpu cpu =perf_cpu_map__min (evlist->core.all_cpus); +struct perf_cpu cpu =perf_cpu_map__min (evlist__core (evlist)->all_cpus);int ret; *err =0 ;
tools/perf/arch/x86/util/intel-bts.c +3 -3
@@ -79,10 +79,10 @@ static int intel_bts_info_fill(struct auxtrace_record *itr,if (priv_size !=INTEL_BTS_AUXTRACE_PRIV_SIZE )return -EINVAL ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ; - pc = session->evlist->mmap[0 ].core.base; + pc =evlist__mmap (session->evlist)[0 ].core.base;if (pc) { err =perf_read_tsc_conversion (pc, &tc);if (err) {
@@ -114,7 +114,7 @@ static int intel_bts_recording_options(struct auxtrace_record *itr, container_of(itr, struct intel_bts_recording, itr);struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;struct evsel *evsel, *intel_bts_evsel =NULL ; -const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +const struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;bool privileged =perf_event_paranoid_check (-1 );if (opts->auxtrace_sample_mode) {
tools/perf/arch/x86/util/intel-pt.c +5 -4
@@ -360,10 +360,10 @@ static int intel_pt_info_fill(struct auxtrace_record *itr, filter =intel_pt_find_filter (session->evlist, ptr->intel_pt_pmu); filter_str_len = filter ?strlen (filter) :0 ; -if (!session->evlist->core.nr_mmaps) +if (!evlist__core (session->evlist)->nr_mmaps)return -EINVAL ; - pc = session->evlist->mmap[0 ].core.base; + pc =evlist__mmap (session->evlist)[0 ].core.base;if (pc) { err =perf_read_tsc_conversion (pc, &tc);if (err) {
@@ -376,7 +376,8 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,ui__warning ("Intel Processor Trace: TSC not available\n" ); } - per_cpu_mmaps = !perf_cpu_map__is_any_cpu_or_is_empty (session->evlist->core.user_requested_cpus); + per_cpu_mmaps = !perf_cpu_map__is_any_cpu_or_is_empty ( +evlist__core (session->evlist)->user_requested_cpus); auxtrace_info->type =PERF_AUXTRACE_INTEL_PT ; auxtrace_info->priv[INTEL_PT_PMU_TYPE ] = intel_pt_pmu->type;
@@ -621,7 +622,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;bool have_timing_info, need_immediate = false;struct evsel *evsel, *intel_pt_evsel =NULL ; -const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus; +const struct perf_cpu_map *cpus =evlist__core (evlist)->user_requested_cpus;bool privileged =perf_event_paranoid_check (-1 );u64 tsc_bit;int err;
tools/perf/arch/x86/util/iostat.c +7 -5
@@ -332,13 +332,15 @@ static int iostat_event_group(struct evlist *evl,return ret; } -int iostat_prepare (struct evlist *evlist,struct perf_stat_config *config) +int iostat_prepare (struct evlist **evlist_ptr,struct perf_stat_config *config) { -if (evlist->core.nr_entries >0 ) { +struct evlist *evlist = *evlist_ptr; + +if (evlist__nr_entries (evlist) >0 ) {pr_warning ("The -e and -M options are not supported." "All chosen events/metrics will be dropped\n" );evlist__put (evlist); - evlist =evlist__new (); + *evlist_ptr = evlist =evlist__new ();if (!evlist)return -ENOMEM ; }
@@ -400,7 +402,7 @@ void iostat_prefix(struct evlist *evlist,struct perf_stat_config *config, char *prefix, struct timespec *ts) { - struct iio_root_port *rp = evlist->selected->priv; + struct iio_root_port *rp = evlist__selected(evlist)->priv;if (rp) {/*
@@ -463,7 +465,7 @@ void iostat_print_counters(struct evlist *evlist,iostat_prefix (evlist, config, prefix, ts);fprintf (config->output,"%s" , prefix);evlist__for_each_entry (evlist, counter) { - perf_device = evlist->selected->priv; + perf_device =evlist__selected (evlist)->priv;if (perf_device && perf_device != counter->priv) {evlist__set_selected (evlist, counter);iostat_prefix (evlist, config, prefix, ts);
tools/perf/bench/evlist-open-close.c +6 -5
@@ -116,7 +116,7 @@ static int bench__do_evlist_open_close(struct evlist *evlist)return err; } - err =evlist__mmap (evlist, opts.mmap_pages); + err =evlist__do_mmap (evlist, opts.mmap_pages);if (err <0 ) {pr_err ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));return err;
@@ -124,7 +124,7 @@ static int bench__do_evlist_open_close(struct evlist *evlist)evlist__enable (evlist);evlist__disable (evlist); -evlist__munmap (evlist); +evlist__do_munmap (evlist);evlist__close (evlist);return 0 ;
@@ -145,10 +145,11 @@ static int bench_evlist_open_close__run(char *evstr, const char *uid_str)init_stats (&time_stats); -printf (" Number of cpus:\t%d\n" ,perf_cpu_map__nr (evlist->core.user_requested_cpus)); -printf (" Number of threads:\t%d\n" , evlist->core.threads->nr); +printf (" Number of cpus:\t%d\n" , +perf_cpu_map__nr (evlist__core (evlist)->user_requested_cpus)); +printf (" Number of threads:\t%d\n" ,evlist__core (evlist)->threads->nr);printf (" Number of events:\t%d (%d fds)\n" , - evlist->core.nr_entries,evlist__count_evsel_fds (evlist)); +evlist__nr_entries (evlist),evlist__count_evsel_fds (evlist));printf (" Number of iterations:\t%d\n" , iterations);evlist__put (evlist);
tools/perf/builtin-annotate.c +5 -2
@@ -562,7 +562,7 @@ static int __cmd_annotate(struct perf_annotate *ann) goto out;if ((use_browser ==1 || ann->use_stdio2) && ann->has_br_stack) -if (session->evlist->nr_br_cntr >0 ) +if (evlist__nr_br_cntr (session->evlist) >0 ) annotate_opts.show_br_cntr = true;if (dump_trace) {
@@ -928,8 +928,11 @@ int cmd_annotate(int argc, const char **argv)* branch counters, if the corresponding branch info is available * in the perf data in the TUI mode. */ -if ((use_browser ==1 || annotate.use_stdio2) && annotate.has_br_stack) +if ((use_browser ==1 || annotate.use_stdio2) && annotate.has_br_stack) { sort__mode =SORT_MODE__BRANCH ; +if (evlist__nr_br_cntr (annotate.session->evlist) >0 ) + annotate_opts.show_br_cntr = true; + }if (setup_sorting (/*evlist=*/ NULL ,perf_session__env (annotate.session)) <0 )usage_with_options (annotate_usage, options);
tools/perf/builtin-ftrace.c +3 -3
@@ -377,9 +377,9 @@ static int set_tracing_pid(struct perf_ftrace *ftrace)if (target__has_cpu (&ftrace->target))return 0 ; -for (i =0 ; i <perf_thread_map__nr (ftrace->evlist->core.threads); i++) { +for (i =0 ; i <perf_thread_map__nr (evlist__core (ftrace->evlist)->threads); i++) {scnprintf (buf,sizeof (buf),"%d" , -perf_thread_map__pid (ftrace->evlist->core.threads, i)); +perf_thread_map__pid (evlist__core (ftrace->evlist)->threads, i));if (append_tracing_file ("set_ftrace_pid" , buf) <0 )return -1 ; }
@@ -413,7 +413,7 @@ static int set_tracing_cpumask(struct perf_cpu_map *cpumap)static int set_tracing_cpu (struct perf_ftrace *ftrace) { -struct perf_cpu_map *cpumap = ftrace->evlist->core.user_requested_cpus; +struct perf_cpu_map *cpumap =evlist__core (ftrace->evlist)->user_requested_cpus;if (!target__has_cpu (&ftrace->target))return 0 ;
tools/perf/builtin-inject.c +2 -2
@@ -1496,7 +1496,7 @@ static int synthesize_id_index(struct perf_inject *inject, size_t new_cnt)struct perf_session *session = inject->session;struct evlist *evlist = session->evlist;struct machine *machine = &session->machines.host; -size_t from = evlist->core.nr_entries - new_cnt; +size_t from =evlist__nr_entries (evlist) - new_cnt;return __perf_event__synthesize_id_index (&inject->tool, perf_event__repipe, evlist, machine, from);
@@ -2031,7 +2031,7 @@ static int host__finished_init(const struct perf_tool *tool, struct perf_sessionif (ret)return ret; - ret =synthesize_id_index (inject, gs->session->evlist->core.nr_entries); + ret =synthesize_id_index (inject,evlist__nr_entries (gs->session->evlist));if (ret) {pr_err ("Failed to synthesize id_index\n" );return ret;
tools/perf/builtin-kvm.c +5 -5
@@ -1221,7 +1221,7 @@ static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,int err; *mmap_time =ULLONG_MAX ; - md = &evlist->mmap[idx]; + md = &evlist__mmap (evlist)[idx]; err =perf_mmap__read_init (&md->core);if (err <0 )return (err == -EAGAIN ) ?0 :-1 ;
@@ -1266,7 +1266,7 @@ static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)s64 n, ntotal =0 ;u64 flush_time =ULLONG_MAX , mmap_time; -for (i =0 ; i < kvm->evlist->core.nr_mmaps; i++) { +for (i =0 ; i <evlist__core (kvm->evlist)->nr_mmaps; i++) { n =perf_kvm__mmap_read_idx (kvm, i, &mmap_time);if (n <0 )return -1 ;
@@ -1449,7 +1449,7 @@ static int kvm_events_live_report(struct perf_kvm_stat *kvm)evlist__enable (kvm->evlist);while (!done) { -struct fdarray *fda = &kvm->evlist->core.pollfd; +struct fdarray *fda = &evlist__core (kvm->evlist)->pollfd;int rc; rc =perf_kvm__mmap_read (kvm);
@@ -1531,7 +1531,7 @@ static int kvm_live_open_events(struct perf_kvm_stat *kvm) goto out; } -if (evlist__mmap (evlist, kvm->opts.mmap_pages) <0 ) { +if (evlist__do_mmap (evlist, kvm->opts.mmap_pages) <0 ) {ui__error ("Failed to mmap the events: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));evlist__close (evlist);
@@ -1931,7 +1931,7 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,perf_session__set_id_hdr_size (kvm->session);ordered_events__set_copy_on_queue (&kvm->session->ordered_events, true);machine__synthesize_threads (&kvm->session->machines.host, &kvm->opts.target, - kvm->evlist->core.threads, true, false,1 ); +evlist__core (kvm->evlist)->threads, true, false,1 ); err =kvm_live_open_events (kvm);if (err) goto out;
tools/perf/builtin-kwork.c +4 -4
@@ -1814,7 +1814,7 @@ static int perf_kwork__check_config(struct perf_kwork *kwork, } } -list_for_each_entry (evsel, &session->evlist->core.entries, core.node) { +list_for_each_entry (evsel, &evlist__core (session->evlist)->entries, core.node) {if (kwork->show_callchain && !evsel__has_callchain (evsel)) {pr_debug ("Samples do not have callchains\n" ); kwork->show_callchain =0 ;
@@ -1864,9 +1864,9 @@ static int perf_kwork__read_events(struct perf_kwork *kwork) goto out_delete; } - kwork->nr_events = session->evlist->stats.nr_events[0 ]; - kwork->nr_lost_events = session->evlist->stats.total_lost; - kwork->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST ]; + kwork->nr_events =evlist__stats (session->evlist)->nr_events[0 ]; + kwork->nr_lost_events =evlist__stats (session->evlist)->total_lost; + kwork->nr_lost_chunks =evlist__stats (session->evlist)->nr_events[PERF_RECORD_LOST ]; out_delete:perf_session__delete (session);
tools/perf/builtin-lock.c +1 -1
@@ -2129,7 +2129,7 @@ static int __cmd_contention(int argc, const char **argv)evlist__start_workload (con.evlist);while (!done) { -if (argc &&waitpid (con.evlist->workload.pid,NULL ,WNOHANG ) >0 ) +if (argc &&waitpid (evlist__workload_pid (con.evlist),NULL ,WNOHANG ) >0 )break ;sleep (1 ); }
tools/perf/builtin-record.c +46 -45
268 lines not on this page · raw message
tools/perf/builtin-report.c +3 -3
@@ -561,7 +561,7 @@ static int evlist__tty_browse_hists(struct evlist *evlist, struct report *rep, cif (!quiet) {fprintf (stdout,"#\n# Total Lost Samples: %" PRIu64"\n#\n" , - evlist->stats.total_lost_samples); +evlist__stats (evlist)->total_lost_samples); }evlist__for_each_entry (evlist, pos) {
@@ -1156,7 +1156,7 @@ static int __cmd_report(struct report *rep) PERF_HPP_REPORT__BLOCK_AVG_CYCLES, }; -if (session->evlist->nr_br_cntr >0 ) +if (evlist__nr_br_cntr (session->evlist) >0 ) block_hpps[nr_hpps++] =PERF_HPP_REPORT__BLOCK_BRANCH_COUNTER ; block_hpps[nr_hpps++] =PERF_HPP_REPORT__BLOCK_RANGE ;
@@ -1291,7 +1291,7 @@ static int process_attr(const struct perf_tool *tool __maybe_unused,* on events sample_type. */ sample_type =evlist__combined_sample_type (*pevlist); - session = (*pevlist)->session; + session =evlist__session (*pevlist);callchain_param_setup (sample_type,perf_session__e_machine (session,/*e_flags=*/ NULL ));return 0 ; }
tools/perf/builtin-sched.c +12 -12
@@ -2014,9 +2014,9 @@ static int perf_sched__read_events(struct perf_sched *sched) goto out_delete; } - sched->nr_events = session->evlist->stats.nr_events[0 ]; - sched->nr_lost_events = session->evlist->stats.total_lost; - sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST ]; + sched->nr_events =evlist__stats (session->evlist)->nr_events[0 ]; + sched->nr_lost_events =evlist__stats (session->evlist)->total_lost; + sched->nr_lost_chunks =evlist__stats (session->evlist)->nr_events[PERF_RECORD_LOST ]; } rc =0 ;
@@ -3292,7 +3292,7 @@ static int timehist_check_attr(struct perf_sched *sched,struct evsel *evsel;struct evsel_runtime *er; -list_for_each_entry (evsel, &evlist->core.entries, core.node) { +list_for_each_entry (evsel, &evlist__core (evlist)->entries, core.node) { er =evsel__get_runtime (evsel);if (er ==NULL ) {pr_err ("Failed to allocate memory for evsel runtime data\n" );
@@ -3464,9 +3464,9 @@ static int perf_sched__timehist(struct perf_sched *sched) goto out; } - sched->nr_events = evlist->stats.nr_events[0 ]; - sched->nr_lost_events = evlist->stats.total_lost; - sched->nr_lost_chunks = evlist->stats.nr_events[PERF_RECORD_LOST ]; + sched->nr_events =evlist__stats (evlist)->nr_events[0 ]; + sched->nr_lost_events =evlist__stats (evlist)->total_lost; + sched->nr_lost_chunks =evlist__stats (evlist)->nr_events[PERF_RECORD_LOST ];if (sched->summary)timehist_print_summary (sched, session);
@@ -3971,7 +3971,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,if (err <0 ) goto out; - user_requested_cpus = evlist->core.user_requested_cpus; + user_requested_cpus =evlist__core (evlist)->user_requested_cpus; err = perf_event__synthesize_schedstat(&(sched->tool), process_synthesized_schedstat_event,
@@ -3987,7 +3987,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,evlist__start_workload (evlist);while (!done) { -if (argc &&waitpid (evlist->workload.pid,NULL ,WNOHANG ) >0 ) +if (argc &&waitpid (evlist__workload_pid (evlist),NULL ,WNOHANG ) >0 )break ;sleep (1 ); }
@@ -4688,7 +4688,7 @@ static int perf_sched__schedstat_report(struct perf_sched *sched)if (err <0 ) goto out; - user_requested_cpus = session->evlist->core.user_requested_cpus; + user_requested_cpus =evlist__core (session->evlist)->user_requested_cpus; err =perf_session__process_events (session);
@@ -4864,7 +4864,7 @@ static int perf_sched__schedstat_live(struct perf_sched *sched,if (err <0 ) goto out; - user_requested_cpus = evlist->core.user_requested_cpus; + user_requested_cpus =evlist__core (evlist)->user_requested_cpus; err = perf_event__synthesize_schedstat(&(sched->tool), process_synthesized_event_live,
@@ -4880,7 +4880,7 @@ static int perf_sched__schedstat_live(struct perf_sched *sched,evlist__start_workload (evlist);while (!done) { -if (argc &&waitpid (evlist->workload.pid,NULL ,WNOHANG ) >0 ) +if (argc &&waitpid (evlist__workload_pid (evlist),NULL ,WNOHANG ) >0 )break ;sleep (1 ); }
tools/perf/builtin-script.c +7 -6
@@ -2229,9 +2229,10 @@ static int script_find_metrics(const struct pmu_metric *pm,evlist__for_each_entry (metric_evlist, metric_evsel) {struct evsel *script_evsel =map_metric_evsel_to_script_evsel (script_evlist, metric_evsel); -struct metric_event *metric_me =metricgroup__lookup (&metric_evlist->metric_events, - metric_evsel, -/*create=*/ false); +struct metric_event *metric_me = +metricgroup__lookup (evlist__metric_events (metric_evlist), + metric_evsel, +/*create=*/ false);if (script_evsel->metric_id ==NULL ) { script_evsel->metric_id = metric_evsel->metric_id;
@@ -2251,7 +2252,7 @@ static int script_find_metrics(const struct pmu_metric *pm,if (metric_me) {struct metric_expr *expr;struct metric_event *script_me = -metricgroup__lookup (&script_evlist->metric_events, +metricgroup__lookup (evlist__metric_events (script_evlist), script_evsel,/*create=*/ true);
@@ -2321,7 +2322,7 @@ static void perf_sample__fprint_metric(struct thread *thread,assert (stat_config.aggr_mode ==AGGR_GLOBAL ); stat_config.aggr_get_id = script_aggr_cpu_id_get; stat_config.aggr_map = -cpu_aggr_map__new (evsel->evlist->core.user_requested_cpus, +cpu_aggr_map__new (evlist__core (evsel->evlist)->user_requested_cpus, aggr_cpu_id__global,/*data=*/ NULL ,/*needs_sort=*/ false); }
@@ -3909,7 +3910,7 @@ static int set_maps(struct perf_script *script)if (WARN_ONCE (script->allocated,"stats double allocation\n" ))return -EINVAL ; -perf_evlist__set_maps (&evlist->core, script->cpus, script->threads); +perf_evlist__set_maps (evlist__core (evlist), script->cpus, script->threads);if (evlist__alloc_stats (&stat_config, evlist,/*alloc_raw=*/ true))return -ENOMEM ;
tools/perf/builtin-stat.c +39 -34
206 lines not on this page · raw message
tools/perf/builtin-top.c +26 -26
175 lines not on this page · raw message
tools/perf/builtin-trace.c +11 -11
@@ -2023,7 +2023,7 @@ static int trace__symbols_init(struct trace *trace, int argc, const char **argv, goto out; err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target, - evlist->core.threads, trace__tool_process, + evlist__core(evlist)->threads, trace__tool_process, /*needs_mmap=*/callchain_param.enabled && !trace->summary_only, /*mmap_data=*/false,
@@ -4195,7 +4195,7 @@ static int trace__set_filter_pids(struct trace *trace) err =augmented_syscalls__set_filter_pids (trace->filter_pids.nr, trace->filter_pids.entries); } - }else if (perf_thread_map__pid (trace->evlist->core.threads,0 ) ==-1 ) { + }else if (perf_thread_map__pid (evlist__core (trace->evlist)->threads,0 ) ==-1 ) { err =trace__set_filter_loop_pids (trace); }
@@ -4509,7 +4509,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)fprintf (trace->output,"Couldn't run the workload!\n" ); goto out_put_evlist; } - workload_pid = evlist->workload.pid; + workload_pid =evlist__workload_pid (evlist); } err =evlist__open (evlist);
@@ -4561,7 +4561,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv) goto out_error_apply_filters;if (!trace->summary_only || !trace->summary_bpf) { - err =evlist__mmap (evlist, trace->opts.mmap_pages); + err =evlist__do_mmap (evlist, trace->opts.mmap_pages);if (err <0 ) goto out_error_mmap; }
@@ -4580,8 +4580,8 @@ static int trace__run(struct trace *trace, int argc, const char **argv)if (trace->summary_bpf)trace_start_bpf_summary (); - trace->multiple_threads =perf_thread_map__pid (evlist->core.threads,0 ) ==-1 || -perf_thread_map__nr (evlist->core.threads) >1 || + trace->multiple_threads =perf_thread_map__pid (evlist__core (evlist)->threads,0 ) ==-1 || +perf_thread_map__nr (evlist__core (evlist)->threads) >1 ||evlist__first (evlist)->core.attr.inherit;/*
@@ -4598,11 +4598,11 @@ static int trace__run(struct trace *trace, int argc, const char **argv) again: before = trace->nr_events; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) {union perf_event *event;struct mmap *md; - md = &evlist->mmap[i]; + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
@@ -5304,7 +5304,7 @@ static int trace__parse_cgroups(const struct option *opt, const char *str, int u {struct trace *trace = opt->value; -if (!list_empty (&trace->evlist->core.entries)) { +if (!list_empty (&evlist__core (trace->evlist)->entries)) {struct option o = { .value = &trace->evlist, };
@@ -5578,7 +5578,7 @@ int cmd_trace(int argc, const char **argv)* .perfconfig trace.add_events, and filter those out. */ if (!trace.trace_syscalls && !trace.trace_pgfaults && - trace.evlist->core.nr_entries ==0 /* Was --events used? */ ) { +evlist__nr_entries (trace.evlist) ==0 /* Was --events used? */ ) { trace.trace_syscalls = true; }/*
@@ -5664,7 +5664,7 @@ int cmd_trace(int argc, const char **argv) symbol_conf.use_callchain = true; } -if (trace.evlist->core.nr_entries >0 ) { +if (evlist__nr_entries (trace.evlist) >0 ) {bool use_btf = false;evlist__set_default_evsel_handler (trace.evlist, trace__event_handler);
tools/perf/tests/backward-ring-buffer.c +4 -4
@@ -34,8 +34,8 @@ static int count_samples(struct evlist *evlist, int *sample_count, {int i; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { -struct mmap *map = &evlist->overwrite_mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { +struct mmap *map = &evlist__overwrite_mmap (evlist)[i];union perf_event *event;perf_mmap__read_init (&map->core);
@@ -65,7 +65,7 @@ static int do_test(struct evlist *evlist, int mmap_pages,int err;char sbuf[STRERR_BUFSIZE ]; - err =evlist__mmap (evlist, mmap_pages); + err =evlist__do_mmap (evlist, mmap_pages);if (err <0 ) {pr_debug ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -77,7 +77,7 @@ static int do_test(struct evlist *evlist, int mmap_pages,evlist__disable (evlist); err =count_samples (evlist, sample_count, comm_count); -evlist__munmap (evlist); +evlist__do_munmap (evlist);return err; }
tools/perf/tests/code-reading.c +5 -5
@@ -592,8 +592,8 @@ static int process_events(struct machine *machine, struct evlist *evlist,struct mmap *md;int i, ret; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { - md = &evlist->mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
@@ -781,7 +781,7 @@ static int do_test_code_reading(bool try_kcore) goto out_put; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads); str = events[evidx];pr_debug ("Parsing event '%s'\n" , str);
@@ -809,7 +809,7 @@ static int do_test_code_reading(bool try_kcore)pr_debug ("perf_evlist__open() failed!\n%s\n" , errbuf); } -perf_evlist__set_maps (&evlist->core,NULL ,NULL ); +perf_evlist__set_maps (evlist__core (evlist),NULL ,NULL );evlist__put (evlist); evlist =NULL ;continue ;
@@ -820,7 +820,7 @@ static int do_test_code_reading(bool try_kcore)if (events[evidx] ==NULL ) goto out_put; - ret =evlist__mmap (evlist,UINT_MAX ); + ret =evlist__do_mmap (evlist,UINT_MAX );if (ret <0 ) {pr_debug ("evlist__mmap failed\n" ); goto out_put;
tools/perf/tests/event-times.c +1 -1
@@ -50,7 +50,7 @@ static int attach__enable_on_exec(struct evlist *evlist)static int detach__enable_on_exec (struct evlist *evlist) { -waitpid (evlist->workload.pid,NULL ,0 ); +waitpid (evlist__workload_pid (evlist),NULL ,0 );return 0 ; }
tools/perf/tests/event_update.c +1 -1
@@ -92,7 +92,7 @@ static int test__event_update(struct test_suite *test __maybe_unused, int subtesTEST_ASSERT_VAL ("failed to allocate ids" , !perf_evsel__alloc_id (&evsel->core,1 ,1 )); -perf_evlist__id_add (&evlist->core, &evsel->core,0 ,0 ,123 ); +perf_evlist__id_add (evlist__core (evlist), &evsel->core,0 ,0 ,123 );free ((char *)evsel->unit); evsel->unit =strdup ("KRAVA" );
tools/perf/tests/expand-cgroup.c +2 -2
@@ -28,7 +28,7 @@ static int test_expand_events(struct evlist *evlist)TEST_ASSERT_VAL ("evlist is empty" , !evlist__empty (evlist)); - nr_events = evlist->core.nr_entries; + nr_events =evlist__nr_entries (evlist); ev_name =calloc (nr_events,sizeof (*ev_name));if (ev_name ==NULL ) {pr_debug ("memory allocation failure\n" );
@@ -54,7 +54,7 @@ static int test_expand_events(struct evlist *evlist) } ret =TEST_FAIL ; -if (evlist->core.nr_entries != nr_events * nr_cgrps) { +if (evlist__nr_entries (evlist) != nr_events * nr_cgrps) {pr_debug ("event count doesn't match\n" ); goto out; }
tools/perf/tests/hwmon_pmu.c +3 -2
@@ -184,9 +184,10 @@ static int do_test(size_t i, bool with_pmu, bool with_alias) } ret =TEST_OK ; -if (with_pmu ? (evlist->core.nr_entries !=1 ) : (evlist->core.nr_entries <1 )) { +if (with_pmu ? (evlist__nr_entries (evlist) !=1 ) + : (evlist__nr_entries (evlist) <1 )) {pr_debug ("FAILED %s:%d Unexpected number of events for '%s' of %d\n" , - __FILE__, __LINE__, str, evlist->core.nr_entries); + __FILE__, __LINE__, str,evlist__nr_entries (evlist)); ret =TEST_FAIL ; goto out; }
tools/perf/tests/keep-tracking.c +4 -4
@@ -37,8 +37,8 @@ static int find_comm(struct evlist *evlist, const char *comm)int i, found; found =0 ; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { - md = &evlist->mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;while ((event =perf_mmap__read_event (&md->core)) !=NULL ) {
@@ -87,7 +87,7 @@ static int test__keep_tracking(struct test_suite *test __maybe_unused, int subte evlist =evlist__new ();CHECK_NOT_NULL__ (evlist); -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);CHECK__ (parse_event (evlist,"dummy:u" ));CHECK__ (parse_event (evlist,"cpu-cycles:u" ));
@@ -106,7 +106,7 @@ static int test__keep_tracking(struct test_suite *test __maybe_unused, int subte goto out_err; } -CHECK__ (evlist__mmap (evlist,UINT_MAX )); +CHECK__ (evlist__do_mmap (evlist,UINT_MAX ));/* * First, test that a 'comm' event can be found when the event is
tools/perf/tests/mmap-basic.c +3 -3
@@ -81,7 +81,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest goto out_free_cpus; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);for (i =0 ; i < nsyscalls; ++i) {char name[64 ];
@@ -113,7 +113,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest expected_nr_events[i] =1 +rand () %127 ; } -if (evlist__mmap (evlist,128 ) <0 ) { +if (evlist__do_mmap (evlist,128 ) <0 ) {pr_debug ("failed to mmap events: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf))); goto out_put_evlist;
@@ -124,7 +124,7 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest syscalls[i](); } - md = &evlist->mmap[0 ]; + md = &evlist__mmap (evlist)[0 ];if (perf_mmap__read_init (&md->core) <0 ) goto out_init;
tools/perf/tests/openat-syscall-tp-fields.c +4 -4
@@ -64,7 +64,7 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unusedevsel__config (evsel, &opts,NULL ); -perf_thread_map__set_pid (evlist->core.threads,0 ,getpid ()); +perf_thread_map__set_pid (evlist__core (evlist)->threads,0 ,getpid ()); err =evlist__open (evlist);if (err <0 ) {
@@ -73,7 +73,7 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unused goto out_put_evlist; } - err =evlist__mmap (evlist,UINT_MAX ); + err =evlist__do_mmap (evlist,UINT_MAX );if (err <0 ) {pr_debug ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -90,11 +90,11 @@ static int test__syscall_openat_tp_fields(struct test_suite *test __maybe_unusedwhile (1 ) {int before = nr_events; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) {union perf_event *event;struct mmap *md; - md = &evlist->mmap[i]; + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
tools/perf/tests/parse-events.c +70 -65
519 lines not on this page · raw message
tools/perf/tests/parse-metric.c +2 -2
@@ -53,7 +53,7 @@ static double compute_single(struct evlist *evlist, const char *name)struct evsel *evsel;evlist__for_each_entry (evlist, evsel) { - me =metricgroup__lookup (&evlist->metric_events, evsel, false); + me =metricgroup__lookup (evlist__metric_events (evlist), evsel, false);if (me !=NULL ) { list_for_each_entry (mexp, &me->head, nd) {if (strcmp (mexp->metric_name, name))
@@ -88,7 +88,7 @@ static int __compute_metric(const char *name, struct value *vals,return -ENOMEM ; } -perf_evlist__set_maps (&evlist->core, cpus,NULL ); +perf_evlist__set_maps (evlist__core (evlist), cpus,NULL );/* Parse the metric into metric_events list. */ pme_test =find_core_metrics_table ("testarch" ,"testcpu" );
tools/perf/tests/perf-record.c +11 -11
@@ -129,7 +129,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestevsel__set_sample_bit (evsel,TIME );evlist__config (evlist, &opts,NULL ); - err =sched__get_first_possible_cpu (evlist->workload.pid, cpu_mask); + err =sched__get_first_possible_cpu (evlist__workload_pid (evlist), cpu_mask);if (err <0 ) {pr_debug ("sched__get_first_possible_cpu: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -142,7 +142,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest/* * So that we can check perf_sample.cpu on all the samples. */ -if (sched_setaffinity (evlist->workload.pid, cpu_mask_size, cpu_mask) <0 ) { +if (sched_setaffinity (evlist__workload_pid (evlist), cpu_mask_size, cpu_mask) <0 ) {pr_debug ("sched_setaffinity: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));evlist__cancel_workload (evlist);
@@ -166,7 +166,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest* fds in the same CPU to be injected in the same mmap ring buffer * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)). */ - err =evlist__mmap (evlist, opts.mmap_pages); + err =evlist__do_mmap (evlist, opts.mmap_pages);if (err <0 ) {pr_debug ("evlist__mmap: %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -188,11 +188,11 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtestwhile (1 ) {int before = total_events; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) {union perf_event *event;struct mmap *md; - md = &evlist->mmap[i]; + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
@@ -231,15 +231,15 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest ++errs; } -if ((pid_t )sample.pid != evlist->workload.pid) { +if ((pid_t )sample.pid !=evlist__workload_pid (evlist)) {pr_debug ("%s with unexpected pid, expected %d, got %d\n" , - name, evlist->workload.pid, sample.pid); + name,evlist__workload_pid (evlist), sample.pid); ++errs; } -if ((pid_t )sample.tid != evlist->workload.pid) { +if ((pid_t )sample.tid !=evlist__workload_pid (evlist)) {pr_debug ("%s with unexpected tid, expected %d, got %d\n" , - name, evlist->workload.pid, sample.tid); + name,evlist__workload_pid (evlist), sample.tid); ++errs; }
@@ -248,7 +248,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest type == PERF_RECORD_MMAP2 || type == PERF_RECORD_FORK || type == PERF_RECORD_EXIT) && - (pid_t)event->comm.pid != evlist->workload.pid) { + (pid_t)event->comm.pid != evlist__workload_pid(evlist)) {pr_debug ("%s with unexpected pid/tid\n" , name); ++errs; }
@@ -352,9 +352,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest } out_put_evlist:CPU_FREE (cpu_mask); -evlist__put (evlist); out:perf_sample__exit (&sample); +evlist__put (evlist);if (err == -EACCES )return TEST_SKIP ;if (err <0 || errs !=0 )
tools/perf/tests/perf-time-to-tsc.c +5 -5
@@ -99,7 +99,7 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su evlist =evlist__new ();CHECK_NOT_NULL__ (evlist); -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);CHECK__ (parse_event (evlist,"cpu-cycles:u" ));
@@ -121,9 +121,9 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su goto out_err; } -CHECK__ (evlist__mmap (evlist,UINT_MAX )); +CHECK__ (evlist__do_mmap (evlist,UINT_MAX )); - pc = evlist->mmap[0 ].core.base; + pc =evlist__mmap (evlist)[0 ].core.base; ret =perf_read_tsc_conversion (pc, &tc);if (ret) {if (ret == -EOPNOTSUPP ) {
@@ -145,8 +145,8 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int suevlist__disable (evlist); -for (i =0 ; i < evlist->core.nr_mmaps; i++) { - md = &evlist->mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
tools/perf/tests/pfm.c +4 -4
@@ -69,12 +69,12 @@ static int test__pfm_events(struct test_suite *test __maybe_unused,if (evlist ==NULL )return -ENOMEM ; - opt.value = evlist; + opt.value = &evlist;parse_libpfm_events_option (&opt, table[i].events,0 );TEST_ASSERT_EQUAL (table[i].events, -count_pfm_events (&evlist->core), +count_pfm_events (evlist__core (evlist)), table[i].nr_events); TEST_ASSERT_EQUAL(table[i].events, evlist__nr_groups(evlist),
@@ -154,12 +154,12 @@ static int test__pfm_group(struct test_suite *test __maybe_unused,if (evlist ==NULL )return -ENOMEM ; - opt.value = evlist; + opt.value = &evlist;parse_libpfm_events_option (&opt, table[i].events,0 );TEST_ASSERT_EQUAL (table[i].events, -count_pfm_events (&evlist->core), +count_pfm_events (evlist__core (evlist)), table[i].nr_events); TEST_ASSERT_EQUAL(table[i].events, evlist__nr_groups(evlist),
tools/perf/tests/pmu-events.c +3 -2
@@ -869,7 +869,7 @@ static int test__parsing_callback(const struct pmu_metric *pm,return -ENOMEM ; } -perf_evlist__set_maps (&evlist->core, cpus,NULL ); +perf_evlist__set_maps (evlist__core (evlist), cpus,NULL ); err =metricgroup__parse_groups_test (evlist, table, pm->metric_name);if (err) {
@@ -895,7 +895,8 @@ static int test__parsing_callback(const struct pmu_metric *pm, k++; }evlist__for_each_entry (evlist, evsel) { -struct metric_event *me =metricgroup__lookup (&evlist->metric_events, evsel, false); +struct metric_event *me =metricgroup__lookup (evlist__metric_events (evlist), + evsel, false);if (me !=NULL ) {struct metric_expr *mexp;
tools/perf/tests/sample-parsing.c +24 -21
112 lines not on this page · raw message
tools/perf/tests/sw-clock.c +3 -3
@@ -71,7 +71,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) goto out_put_evlist; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);if (evlist__open (evlist)) {const char *knob ="/proc/sys/kernel/perf_event_max_sample_rate" ;
@@ -83,7 +83,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) goto out_put_evlist; } - err =evlist__mmap (evlist,128 ); + err =evlist__do_mmap (evlist,128 );if (err <0 ) {pr_debug ("failed to mmap event: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf)));
@@ -98,7 +98,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)evlist__disable (evlist); - md = &evlist->mmap[0 ]; + md = &evlist__mmap (evlist)[0 ];if (perf_mmap__read_init (&md->core) <0 ) goto out_init;
tools/perf/tests/switch-tracking.c +5 -4
@@ -237,6 +237,7 @@ static int add_event(struct evlist *evlist, struct list_head *events,if (evlist__parse_sample (evlist, event, &sample)) {pr_debug ("evlist__parse_sample failed\n" ); +perf_sample__exit (&sample);return -1 ; }
@@ -282,8 +283,8 @@ static int process_events(struct evlist *evlist,struct mmap *md;int i, ret; -for (i =0 ; i < evlist->core.nr_mmaps; i++) { - md = &evlist->mmap[i]; +for (i =0 ; i <evlist__core (evlist)->nr_mmaps; i++) { + md = &evlist__mmap (evlist)[i];if (perf_mmap__read_init (&md->core) <0 )continue ;
@@ -374,7 +375,7 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub goto out_err; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads);/* First event */ err =parse_event (evlist,"cpu-clock:u" );
@@ -471,7 +472,7 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub goto out; } - err =evlist__mmap (evlist,UINT_MAX ); + err =evlist__do_mmap (evlist,UINT_MAX );if (err) {pr_debug ("evlist__mmap failed!\n" ); goto out_err;
tools/perf/tests/task-exit.c +3 -3
@@ -77,7 +77,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _ goto out_put_evlist; } -perf_evlist__set_maps (&evlist->core, cpus, threads); +perf_evlist__set_maps (evlist__core (evlist), cpus, threads); err =evlist__prepare_workload (evlist, &target, argv, false, workload_exec_failed_signal);if (err <0 ) {
@@ -104,7 +104,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _ goto out_put_evlist; } -if (evlist__mmap (evlist,128 ) <0 ) { +if (evlist__do_mmap (evlist,128 ) <0 ) {pr_debug ("failed to mmap events: %d (%s)\n" , errno,str_error_r (errno, sbuf,sizeof (sbuf))); err =-1 ;
@@ -114,7 +114,7 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _evlist__start_workload (evlist); retry: - md = &evlist->mmap[0 ]; + md = &evlist__mmap (evlist)[0 ];if (perf_mmap__read_init (&md->core) <0 ) goto out_init;
tools/perf/tests/time-utils-test.c +9 -5
@@ -69,16 +69,19 @@ struct test_data {static bool test__perf_time__parse_for_ranges (struct test_data *d) { -struct evlist evlist = { - .first_sample_time = d->first, - .last_sample_time = d->last, - }; -struct perf_session session = { .evlist = &evlist }; +struct evlist *evlist =evlist__new (); +struct perf_session session = { .evlist = evlist };struct perf_time_interval *ptime =NULL ;int range_size, range_num;bool pass = false;int i, err; +if (!evlist) { +pr_debug ("Missing evlist\n" ); +return false; + } +evlist__set_first_sample_time (evlist, d->first); +evlist__set_last_sample_time (evlist, d->last);pr_debug ("\nperf_time__parse_for_ranges(\"%s\")\n" , d->str);if (strchr (d->str,'%' ))
@@ -127,6 +130,7 @@ static bool test__perf_time__parse_for_ranges(struct test_data *d) pass = true; out: +evlist__put (evlist);free (ptime);return pass; }
tools/perf/tests/tool_pmu.c +3 -2
@@ -40,9 +40,10 @@ static int do_test(enum tool_pmu_event ev, bool with_pmu) } ret =TEST_OK ; -if (with_pmu ? (evlist->core.nr_entries !=1 ) : (evlist->core.nr_entries <1 )) { +if (with_pmu ? (evlist__nr_entries (evlist) !=1 ) + : (evlist__nr_entries (evlist) <1 )) {pr_debug ("FAILED %s:%d Unexpected number of events for '%s' of %d\n" , - __FILE__, __LINE__, str, evlist->core.nr_entries); + __FILE__, __LINE__, str,evlist__nr_entries (evlist)); ret =TEST_FAIL ; goto out; }
tools/perf/tests/topology.c +1 -1
@@ -46,7 +46,7 @@ static int session_write_header(char *path) session->evlist =evlist__new_default (&target,/*sample_callchains=*/ false);TEST_ASSERT_VAL ("can't get evlist" , session->evlist); - session->evlist->session = session; +evlist__set_session (session->evlist, session);perf_header__set_feat (&session->header,HEADER_CPU_TOPOLOGY );perf_header__set_feat (&session->header,HEADER_NRCPUS );
tools/perf/tests/uncore-event-sorting.c +2 -2
@@ -147,8 +147,8 @@ static int test__uncore_event_sorting(struct test_suite *test __maybe_unused, goto out_err; } -CHECK_COND (evlist->core.nr_entries >=4 ,"Number of events is >= 4" ); -CHECK_EQUAL (evlist->core.nr_entries %2 ,0 ,"Number of events is a multiple of 2" ); +CHECK_COND (evlist__nr_entries (evlist) >=4 ,"Number of events is >= 4" ); +CHECK_EQUAL (evlist__nr_entries (evlist) %2 ,0 ,"Number of events is a multiple of 2" );evlist__for_each_entry (evlist, evsel) {struct evsel *next;
tools/perf/ui/browsers/annotate.c +1 -1
@@ -597,7 +597,7 @@ static bool annotate_browser__callq(struct annotate_browser *browser, notes =symbol__annotation (dl->ops.target.sym);annotation__lock (notes); -if (!symbol__hists (dl->ops.target.sym, evsel->evlist->core.nr_entries)) { +if (!symbol__hists (dl->ops.target.sym,evlist__nr_entries (evsel->evlist))) {annotation__unlock (notes);ui__warning ("Not enough memory for annotating '%s' symbol!\n" , dl->ops.target.sym->name);
tools/perf/ui/browsers/hists.c +11 -11
@@ -688,10 +688,10 @@ static int hist_browser__handle_hotkey(struct hist_browser *browser, bool warn_lui_browser__update_nr_entries (&browser->b, nr_entries);if (warn_lost_event && - (evsel->evlist->stats.nr_lost_warned != - evsel->evlist->stats.nr_events[PERF_RECORD_LOST ])) { - evsel->evlist->stats.nr_lost_warned = - evsel->evlist->stats.nr_events[PERF_RECORD_LOST ]; + (evlist__stats (evsel->evlist)->nr_lost_warned != +evlist__stats (evsel->evlist)->nr_events[PERF_RECORD_LOST ])) { +evlist__stats (evsel->evlist)->nr_lost_warned = +evlist__stats (evsel->evlist)->nr_events[PERF_RECORD_LOST ];ui_browser__warn_lost_events (&browser->b); }
@@ -3321,7 +3321,7 @@ static int evsel__hists_browse(struct evsel *evsel, int nr_events, const char *h* No need to refresh, resort/decay histogram * entries if we are not collecting samples: */ -if (top->evlist->enabled) { +if (evlist__enabled (top->evlist)) { helpline ="Press 'f' to disable the events or 'h' to see other hotkeys" ; hbt->refresh = delay_secs; }else {
@@ -3493,7 +3493,7 @@ static void perf_evsel_menu__write(struct ui_browser *browser, unit, unit == ' ' ? "" : " ", ev_name);ui_browser__printf (browser,"%s" , bf); - nr_events = evsel->evlist->stats.nr_events[PERF_RECORD_LOST ]; + nr_events =evlist__stats (evsel->evlist)->nr_events[PERF_RECORD_LOST ];if (nr_events !=0 ) { menu->lost_events = true;if (!current_entry)
@@ -3559,13 +3559,13 @@ static int perf_evsel_menu__run(struct evsel_menu *menu,ui_browser__show_title (&menu->b, title);switch (key) {case K_TAB : -if (pos->core.node.next == &evlist->core.entries) +if (pos->core.node.next == &evlist__core (evlist)->entries) pos =evlist__first (evlist);else pos =evsel__next (pos); goto browse_hists;case K_UNTAB : -if (pos->core.node.prev == &evlist->core.entries) +if (pos->core.node.prev == &evlist__core (evlist)->entries) pos =evlist__last (evlist);else pos =evsel__prev (pos);
@@ -3618,7 +3618,7 @@ static int __evlist__tui_browse_hists(struct evlist *evlist, int nr_entries, construct evsel *pos;struct evsel_menu menu = { .b = { - .entries = &evlist->core.entries, + .entries = &evlist__core (evlist)->entries, .refresh = ui_browser__list_head_refresh, .seek = ui_browser__list_head_seek, .write = perf_evsel_menu__write,
@@ -3646,7 +3646,7 @@ static int __evlist__tui_browse_hists(struct evlist *evlist, int nr_entries, constatic bool evlist__single_entry (struct evlist *evlist) { -int nr_entries = evlist->core.nr_entries; +int nr_entries =evlist__nr_entries (evlist);if (nr_entries ==1 )return true;
@@ -3664,7 +3664,7 @@ static bool evlist__single_entry(struct evlist *evlist)int evlist__tui_browse_hists (struct evlist *evlist,const char *help,struct hist_browser_timer *hbt,float min_pcnt,struct perf_env *env,bool warn_lost_event) { -int nr_entries = evlist->core.nr_entries; +int nr_entries =evlist__nr_entries (evlist);if (evlist__single_entry (evlist)) { single_entry: {
tools/perf/util/amd-sample-raw.c +1 -1
@@ -421,7 +421,7 @@ static void parse_cpuid(struct perf_env *env)*/ bool evlist__has_amd_ibs (struct evlist *evlist) { -struct perf_env *env =perf_session__env (evlist->session); +struct perf_env *env =perf_session__env (evlist__session (evlist));int ret, nr_pmu_mappings =perf_env__nr_pmu_mappings (env);const char *pmu_mapping =perf_env__pmu_mappings (env);char name[sizeof ("ibs_fetch" )];
tools/perf/util/annotate-data.c +1 -1
@@ -1829,7 +1829,7 @@ int annotated_data_type__update_samples(struct annotated_data_type *adt,return 0 ;if (adt->histograms ==NULL ) { -int nr = evsel->evlist->core.nr_entries; +int nr =evlist__nr_entries (evsel->evlist);if (alloc_data_type_histograms (adt, nr) <0 )return -1 ;
tools/perf/util/annotate.c +5 -5
@@ -328,7 +328,7 @@ static int symbol__inc_addr_samples(struct map_symbol *ms,if (sym ==NULL )return 0 ; - src =symbol__hists (sym, sample->evsel->evlist->core.nr_entries); + src =symbol__hists (sym,evlist__nr_entries (sample->evsel->evlist));return src ?__symbol__inc_addr_samples (ms, src, addr, sample) :0 ; }
@@ -339,7 +339,7 @@ static int symbol__account_br_cntr(struct annotated_branch *branch, {unsigned int br_cntr_nr =evsel__leader (evsel)->br_cntr_nr;unsigned int base =evsel__leader (evsel)->br_cntr_idx; -unsigned int off = offset * evsel->evlist->nr_br_cntr; +unsigned int off = offset *evlist__nr_br_cntr (evsel->evlist);u64 *branch_br_cntr = branch->br_cntr;unsigned int i, mask, width;
@@ -369,7 +369,7 @@ static int symbol__account_cycles(u64 addr, u64 start, struct symbol *sym,if (sym ==NULL )return 0 ; - branch =symbol__find_branch_hist (sym, evsel->evlist->nr_br_cntr); + branch =symbol__find_branch_hist (sym,evlist__nr_br_cntr (evsel->evlist));if (!branch)return -ENOMEM ;if (addr < sym->start || addr >= sym->end)
@@ -511,7 +511,7 @@ static void annotation__count_and_fill(struct annotation *notes, u64 start, u64static int annotation__compute_ipc (struct annotation *notes,size_t size,struct evsel *evsel) { -unsigned int br_cntr_nr = evsel->evlist->nr_br_cntr; +unsigned int br_cntr_nr =evlist__nr_br_cntr (evsel->evlist);int err =0 ;s64 offset;
@@ -1813,7 +1813,7 @@ int annotation_br_cntr_abbr_list(char **str, struct evsel *evsel, bool header)struct evsel *pos;struct strbuf sb; -if (evsel->evlist->nr_br_cntr <=0 ) +if (evlist__nr_br_cntr (evsel->evlist) <=0 )return -ENOTSUP ;strbuf_init (&sb,/*hint=*/ 0 );
tools/perf/util/auxtrace.c +7 -7
@@ -191,7 +191,7 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,struct evlist *evlist, struct evsel *evsel, int idx) { - bool per_cpu = !perf_cpu_map__has_any_cpu(evlist->core.user_requested_cpus); + bool per_cpu = !perf_cpu_map__has_any_cpu(evlist__core(evlist)->user_requested_cpus); mp->mmap_needed = evsel->needs_auxtrace_mmap;
@@ -201,11 +201,11 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp, mp->idx = idx;if (per_cpu) { - mp->cpu =perf_cpu_map__cpu (evlist->core.all_cpus, idx); - mp->tid =perf_thread_map__pid (evlist->core.threads,0 ); + mp->cpu =perf_cpu_map__cpu (evlist__core (evlist)->all_cpus, idx); + mp->tid =perf_thread_map__pid (evlist__core (evlist)->threads,0 ); }else { mp->cpu.cpu =-1 ; - mp->tid =perf_thread_map__pid (evlist->core.threads, idx); + mp->tid =perf_thread_map__pid (evlist__core (evlist)->threads, idx); } }
@@ -668,10 +668,10 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,static int evlist__enable_event_idx (struct evlist *evlist,struct evsel *evsel,int idx) { -bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu (evlist->core.user_requested_cpus); +bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu (evlist__core (evlist)->user_requested_cpus);if (per_cpu_mmaps) { -struct perf_cpu evlist_cpu =perf_cpu_map__cpu (evlist->core.all_cpus, idx); +struct perf_cpu evlist_cpu =perf_cpu_map__cpu (evlist__core (evlist)->all_cpus, idx);int cpu_map_idx =perf_cpu_map__idx (evsel->core.cpus, evlist_cpu);if (cpu_map_idx ==-1 )
@@ -1838,7 +1838,7 @@ void perf_session__auxtrace_error_inc(struct perf_session *session,struct perf_record_auxtrace_error *e = &event->auxtrace_error;if (e->type <PERF_AUXTRACE_ERROR_MAX ) - session->evlist->stats.nr_auxtrace_errors[e->type] +=1 ; +evlist__stats (session->evlist)->nr_auxtrace_errors[e->type] +=1 ; }void events_stats__auxtrace_error_warn (const struct events_stats *stats)
tools/perf/util/block-info.c +2 -2
@@ -472,7 +472,7 @@ struct block_report *block_info__create_report(struct evlist *evlist, int *nr_reps) {struct block_report *block_reports; -int nr_hists = evlist->core.nr_entries, i =0 ; +int nr_hists =evlist__nr_entries (evlist), i =0 ;struct evsel *pos; block_reports =calloc (nr_hists,sizeof (struct block_report ));
@@ -483,7 +483,7 @@ struct block_report *block_info__create_report(struct evlist *evlist,struct hists *hists =evsel__hists (pos);process_block_report (hists, &block_reports[i], total_cycles, - block_hpps, nr_hpps, evlist->nr_br_cntr); + block_hpps, nr_hpps,evlist__nr_br_cntr (evlist)); i++; }
tools/perf/util/bpf_counter.c +1 -1
@@ -443,7 +443,7 @@ static int bperf_check_target(struct evsel *evsel, }else if (target->tid) { *filter_type =BPERF_FILTER_PID ; *filter_entry_cnt =perf_thread_map__nr (evsel->core.threads); - }else if (target->pid || evsel->evlist->workload.pid !=-1 ) { + }else if (target->pid ||evlist__workload_pid (evsel->evlist) !=-1 ) { *filter_type =BPERF_FILTER_TGID ; *filter_entry_cnt =perf_thread_map__nr (evsel->core.threads); }else {
tools/perf/util/bpf_counter_cgroup.c +6 -6
@@ -104,7 +104,7 @@ static int bperf_load_program(struct evlist *evlist)set_max_rlimit (); -if (nr_cgroups ==0 || evlist->core.nr_entries % nr_cgroups !=0 ) { +if (nr_cgroups ==0 ||evlist__nr_entries (evlist) % nr_cgroups !=0 ) {pr_err ("Invalid cgroup or event count\n" );return -EINVAL ; }
@@ -116,7 +116,7 @@ static int bperf_load_program(struct evlist *evlist)pr_err ("Failed to open cgroup skeleton\n" );return -1 ; } -setup_rodata (skel, evlist->core.nr_entries); +setup_rodata (skel,evlist__nr_entries (evlist)); err =bperf_cgroup_bpf__load (skel);if (err) {
@@ -127,12 +127,12 @@ static int bperf_load_program(struct evlist *evlist) err =-1 ; cgrp_switch =evsel__new (&cgrp_switch_attr); -if (evsel__open_per_cpu (cgrp_switch, evlist->core.all_cpus,-1 ) <0 ) { +if (evsel__open_per_cpu (cgrp_switch,evlist__core (evlist)->all_cpus,-1 ) <0 ) {pr_err ("Failed to open cgroup switches event\n" ); goto out; } -perf_cpu_map__for_each_cpu (cpu, i, evlist->core.all_cpus) { +perf_cpu_map__for_each_cpu (cpu, i,evlist__core (evlist)->all_cpus) { link =bpf_program__attach_perf_event (skel->progs.on_cgrp_switch,FD (cgrp_switch, i));if (IS_ERR (link)) {
@@ -197,7 +197,7 @@ static int bperf_load_program(struct evlist *evlist)*/ {struct evsel *leader; -int num_events = evlist->core.nr_entries / nr_cgroups; +int num_events =evlist__nr_entries (evlist) / nr_cgroups;evlist__for_each_entry (evlist, evsel) { leader =evlist__find_evsel (evlist, evsel->core.idx % num_events);
@@ -258,7 +258,7 @@ static int bperf_cgrp__sync_counters(struct evlist *evlist)unsigned int idx;int prog_fd =bpf_program__fd (skel->progs.trigger_read); -perf_cpu_map__for_each_cpu (cpu, idx, evlist->core.all_cpus) +perf_cpu_map__for_each_cpu (cpu, idx,evlist__core (evlist)->all_cpus)bperf_trigger_reading (prog_fd, cpu.cpu);return 0 ;
tools/perf/util/bpf_ftrace.c +5 -4
@@ -59,13 +59,13 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace)/* don't need to set cpu filter for system-wide mode */ if (ftrace->target.cpu_list) { - ncpus =perf_cpu_map__nr (ftrace->evlist->core.user_requested_cpus); + ncpus =perf_cpu_map__nr (evlist__core (ftrace->evlist)->user_requested_cpus);bpf_map__set_max_entries (skel->maps.cpu_filter, ncpus); skel->rodata->has_cpu =1 ; }if (target__has_task (&ftrace->target) ||target__none (&ftrace->target)) { - ntasks =perf_thread_map__nr (ftrace->evlist->core.threads); + ntasks =perf_thread_map__nr (evlist__core (ftrace->evlist)->threads);bpf_map__set_max_entries (skel->maps.task_filter, ntasks); skel->rodata->has_task =1 ; }
@@ -87,7 +87,8 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace) fd =bpf_map__fd (skel->maps.cpu_filter);for (i =0 ; i < ncpus; i++) { - cpu =perf_cpu_map__cpu (ftrace->evlist->core.user_requested_cpus, i).cpu; + cpu =perf_cpu_map__cpu ( +evlist__core (ftrace->evlist)->user_requested_cpus, i).cpu;bpf_map_update_elem (fd, &cpu, &val,BPF_ANY ); } }
@@ -99,7 +100,7 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace) fd =bpf_map__fd (skel->maps.task_filter);for (i =0 ; i < ntasks; i++) { - pid =perf_thread_map__pid (ftrace->evlist->core.threads, i); + pid =perf_thread_map__pid (evlist__core (ftrace->evlist)->threads, i);bpf_map_update_elem (fd, &pid, &val,BPF_ANY ); } }
tools/perf/util/bpf_lock_contention.c +6 -6
@@ -223,11 +223,11 @@ int lock_contention_prepare(struct lock_contention *con)if (target__has_cpu (target)) { skel->rodata->has_cpu =1 ; - ncpus =perf_cpu_map__nr (evlist->core.user_requested_cpus); + ncpus =perf_cpu_map__nr (evlist__core (evlist)->user_requested_cpus); }if (target__has_task (target)) { skel->rodata->has_task =1 ; - ntasks =perf_thread_map__nr (evlist->core.threads); + ntasks =perf_thread_map__nr (evlist__core (evlist)->threads); }if (con->filters->nr_types) { skel->rodata->has_type =1 ;
@@ -334,7 +334,7 @@ int lock_contention_prepare(struct lock_contention *con) fd =bpf_map__fd (skel->maps.cpu_filter);for (i =0 ; i < ncpus; i++) { - cpu =perf_cpu_map__cpu (evlist->core.user_requested_cpus, i).cpu; + cpu =perf_cpu_map__cpu (evlist__core (evlist)->user_requested_cpus, i).cpu;bpf_map_update_elem (fd, &cpu, &val,BPF_ANY ); } }
@@ -346,13 +346,13 @@ int lock_contention_prepare(struct lock_contention *con) fd =bpf_map__fd (skel->maps.task_filter);for (i =0 ; i < ntasks; i++) { - pid =perf_thread_map__pid (evlist->core.threads, i); + pid =perf_thread_map__pid (evlist__core (evlist)->threads, i);bpf_map_update_elem (fd, &pid, &val,BPF_ANY ); } } -if (target__none (target) && evlist->workload.pid >0 ) { -u32 pid = evlist->workload.pid; +if (target__none (target) &&evlist__workload_pid (evlist) >0 ) { +u32 pid =evlist__workload_pid (evlist);u8 val =1 ; fd =bpf_map__fd (skel->maps.task_filter);
tools/perf/util/bpf_off_cpu.c +7 -7
@@ -73,13 +73,13 @@ static void off_cpu_start(void *arg)/* update task filter for the given workload */ if (skel->rodata->has_task && skel->rodata->uses_tgid && -perf_thread_map__pid (evlist->core.threads,0 ) !=-1 ) { +perf_thread_map__pid (evlist__core (evlist)->threads,0 ) !=-1 ) {int fd;u32 pid;u8 val =1 ; fd =bpf_map__fd (skel->maps.task_filter); - pid =perf_thread_map__pid (evlist->core.threads,0 ); + pid =perf_thread_map__pid (evlist__core (evlist)->threads,0 );bpf_map_update_elem (fd, &pid, &val,BPF_ANY ); }
@@ -168,7 +168,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target,/* don't need to set cpu filter for system-wide mode */ if (target->cpu_list) { - ncpus =perf_cpu_map__nr (evlist->core.user_requested_cpus); + ncpus =perf_cpu_map__nr (evlist__core (evlist)->user_requested_cpus);bpf_map__set_max_entries (skel->maps.cpu_filter, ncpus); skel->rodata->has_cpu =1 ; }
@@ -199,7 +199,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, skel->rodata->has_task =1 ; skel->rodata->uses_tgid =1 ; }else if (target__has_task (target)) { - ntasks =perf_thread_map__nr (evlist->core.threads); + ntasks =perf_thread_map__nr (evlist__core (evlist)->threads);bpf_map__set_max_entries (skel->maps.task_filter, ntasks); skel->rodata->has_task =1 ; }else if (target__none (target)) {
@@ -209,7 +209,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, }if (evlist__first (evlist)->cgrp) { - ncgrps = evlist->core.nr_entries -1 ;/* excluding a dummy */ + ncgrps =evlist__nr_entries (evlist) -1 ;/* excluding a dummy */ bpf_map__set_max_entries (skel->maps.cgroup_filter, ncgrps);if (!cgroup_is_v2 ("perf_event" ))
@@ -240,7 +240,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, fd =bpf_map__fd (skel->maps.cpu_filter);for (i =0 ; i < ncpus; i++) { - cpu =perf_cpu_map__cpu (evlist->core.user_requested_cpus, i).cpu; + cpu =perf_cpu_map__cpu (evlist__core (evlist)->user_requested_cpus, i).cpu;bpf_map_update_elem (fd, &cpu, &val,BPF_ANY ); } }
@@ -269,7 +269,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, fd =bpf_map__fd (skel->maps.task_filter);for (i =0 ; i < ntasks; i++) { - pid =perf_thread_map__pid (evlist->core.threads, i); + pid =perf_thread_map__pid (evlist__core (evlist)->threads, i);bpf_map_update_elem (fd, &pid, &val,BPF_ANY ); } }
tools/perf/util/cgroup.c +10 -10
@@ -367,7 +367,7 @@ int parse_cgroups(const struct option *opt, const char *str,char *s;int ret, i; -if (list_empty (&evlist->core.entries)) { +if (list_empty (&evlist__core (evlist)->entries)) {fprintf (stderr,"must define events before cgroups\n" );return -1 ; }
@@ -423,7 +423,7 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgroint ret =-1 ;int prefix_len; -if (evlist->core.nr_entries ==0 ) { +if (evlist__nr_entries (evlist) ==0 ) {fprintf (stderr,"must define events before cgroups\n" );return -EINVAL ; }
@@ -436,11 +436,11 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgro }/* save original events and init evlist */ -evlist__splice_list_tail (orig_list, &evlist->core.entries); - evlist->core.nr_entries =0 ; +evlist__splice_list_tail (orig_list, &evlist__core (evlist)->entries); +evlist__core (evlist)->nr_entries =0 ; - orig_metric_events = evlist->metric_events; -metricgroup__rblist_init (&evlist->metric_events); + orig_metric_events = *evlist__metric_events (evlist); +metricgroup__rblist_init (evlist__metric_events (evlist));if (has_pattern_string (str)) prefix_len =match_cgroups (str);
@@ -503,15 +503,15 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgro nr_cgroups++;if (metricgroup__copy_metric_events (tmp_list, cgrp, - &evlist->metric_events, +evlist__metric_events (evlist), &orig_metric_events) <0 ) goto out_err; -evlist__splice_list_tail (evlist, &tmp_list->core.entries); - tmp_list->core.nr_entries =0 ; +evlist__splice_list_tail (evlist, &evlist__core (tmp_list)->entries); +evlist__core (tmp_list)->nr_entries =0 ; } -if (list_empty (&evlist->core.entries)) { +if (list_empty (&evlist__core (evlist)->entries)) {fprintf (stderr,"no cgroup matched: %s\n" , str); goto out_err; }
tools/perf/util/cs-etm.c +4 -1
@@ -1672,8 +1672,9 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq, {int ret =0 ;struct cs_etm_auxtrace *etm = etmq->etm; -struct perf_sample sample = {.ip =0 ,}; +struct perf_sample sample;union perf_event *event = tidq->event_buf; +struct dummy_branch_stack {u64 nr;u64 hw_idx;
@@ -1681,6 +1682,7 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq, } dummy_bs;u64 ip; +perf_sample__init (&sample,/*all=*/ true); ip =cs_etm__last_executed_instr (tidq->prev_packet); event->sample.header.type =PERF_RECORD_SAMPLE ;
@@ -1733,6 +1735,7 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,"CS ETM Trace: failed to deliver instruction event, error %d\n" , ret); +perf_sample__exit (&sample);return ret; }
tools/perf/util/evlist.c +233 -162
172 lines not on this page · raw message
tools/perf/util/evlist.h +235 -16
251 lines not on this page · raw message
tools/perf/util/header.c +24 -23
47 lines not on this page · raw message
tools/perf/util/python.c +64 -26
90 lines not on this page · raw message
tools/perf/util/session.c +21 -22
43 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
Part of the diff is not shown here. View the raw message.
[email protected]
AI reviewRe: [PATCH v21 07/21] perf evlist: Add reference count checking
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] Race condition in `evlist__put` lockless cycle-detection leading to double-free. - [High]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] Race condition in evlist__put lockless cycle-detection leading to double-free.
- [High] Order-dependent memory leak of evlist and evsel objects due to asymmetric cycle-breaking logic.
- [Low] Missing accessor replacement causes build failure on s390 when REFCNT_CHECKING is enabled. --
commit 90f010720902ca448ae8e2550b479cb3776be988
Author: Ian Rogers <[email protected]>
Subject: perf evlist: Add reference count checking
Now the evlist is reference counted, add reference count checking so that gets and puts are paired and easy to debug. Reference count checking is documented here:
https://perfwiki.github.io/main/reference-count-checking/
Quoted 2 lines
tools/perf/util/evlist.h +0
-0
[ ... ]
tools/perf/util/evlist.h enum bkw_mmap_state { · line 66
Quoted 9 lines
tools/perf/util/evlist.h +1
-1@@ -59,7 +60,7 @@ enum bkw_mmap_state {struct event_enable_timer ; -struct evlist { +DECLARE_RC_STRUCT (evlist) {struct perf_evlist core;refcount_t refcnt;bool enabled;
[Severity: Low]
Is a direct access to evlist->core.nr_entries missing an accessor replacement?
With REFCNT_CHECKING enabled, struct evlist is replaced with this opaque DECLARE_RC_STRUCT(evlist) wrapper. This requires all direct member accesses to be migrated to accessor macros like evlist__nr_entries(). However, auxtrace_record__init() in tools/perf/arch/s390/util/auxtrace.c still accesses it directly:
if (evlist->core.nr_entries == 0)
return NULL;Will this cause a build failure on s390 when REFCNT_CHECKING is enabled?
Quoted 2 lines
tools/perf/util/evlist.c +0
-0
[ ... ]
Quoted 23 lines
void
evlist__put(struct evlist *evlist)
{tools/perf/util/evlist.c +16
-2+struct evsel *evsel; +unsigned int count, ref_cnt; +if (evlist ==NULL )return ; -if (!refcount_dec_and_test (&evlist->refcnt)) -return ; +if (refcount_dec_and_test (evlist__refcnt (evlist))) + goto out_delete; + +retry: + count =refcount_read (evlist__refcnt (evlist)); + ref_cnt = count; +evlist__for_each_entry (evlist, evsel) { +if (RC_CHK_EQUAL (evsel->evlist, evlist) && count && +refcount_read (&evsel->refcnt) ==1 ) + count--; + } +if (refcount_read (evlist__refcnt (evlist)) != ref_cnt) + goto retry;
tools/perf/util/evlist.c static void evlist__purge(struct evlist *evlist) · line 257
Quoted 10 lines
tools/perf/util/evlist.c +9
-0+if (count !=0 ) { +/* +* Not the last reference except for back references from +* evsels. +*/ +RC_CHK_PUT (evlist); +return ; + } +out_delete:
[Severity: Critical]
Can this race when executed concurrently, leading to a double-free?
Looking at evlist__put() in tools/perf/util/evlist.c, if multiple threads concurrently drop external references to the same evlist, they can both decrement the atomic refcount without reaching zero. They would then concurrently execute this fallback verification loop, read the exact same evlist refcount, and independently subtract the internal cyclic references.
Could both threads calculate that the list should be destroyed and simultaneously enter out_delete, freeing the evlist and all its evsel structures twice?
Quoted 2 lines
tools/perf/util/python.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/python.c +0
-0@@ -1811,28 +1830,33 @@ static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
[ ... ]
tools/perf/util/python.c static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist, · line 1852
Quoted 10 lines
tools/perf/util/python.c +4
-2evsel = ((struct pyrf_evsel *)pevsel)->evsel; - evsel->core.idx = evlist->core.nr_entries; +CHECK_INITIALIZED (evsel,"evsel" ); + + evsel->core.idx =evlist__nr_entries (evlist);evlist__add (evlist,evsel__get (evsel)); -return Py_BuildValue ("i" , evlist->core.nr_entries); +return Py_BuildValue ("i" ,evlist__nr_entries (evlist)); }
[Severity: High]
Does this code create an order-dependent memory leak of the evlist and evsel objects?
In Python bindings (pyrf_evlist__add), an explicit external reference is added to the evsel via evsel__get(evsel), making its refcnt == 2. Because the Python garbage collector reclaims objects non-deterministically, the evlist might be finalized before the evsel.
If evlist__put() runs first, it sees evsel->refcnt == 2, determines there are external references, and skips teardown. Later, when the evsel is finalized, evsel__put() simply drops the refcount to 1 and returns, as it lacks any cycle-breaking logic. Does this permanently leak the evlist and all its evsel structures since neither reaches 0?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Ian Rogers
reviewerPATCH v21 8/21 perf python: Use evsel in sample in pyrf_event
In reply to Ian RogersAvoid a duplicated evsel by using the one in sample. Add evsel__get/put to the evsel in perf_sample.
--- tools/perf/builtin-inject.c | 5 ++++- tools/perf/builtin-trace.c | 5 ++++- tools/perf/util/evsel.c | 2 +- tools/perf/util/intel-pt.c | 8 +++++--- tools/perf/util/python.c | 10 +++------- tools/perf/util/s390-sample-raw.c | 1 + tools/perf/util/sample.c | 17 ++++++++++++----- tools/perf/util/session.c | 19 ++++++++++++++++--- 8 files changed, 46 insertions(+), 21 deletions(-)
tools/perf/builtin-inject.c +4 -1
@@ -702,8 +702,11 @@ static int perf_event__repipe_common_mmap(const struct perf_tool *tool, }if (dso && !dso__hit (dso)) { -if (!sample->evsel) +if (!sample->evsel) { sample->evsel =evlist__event2evsel (inject->session->evlist, event); +if (sample->evsel) +evsel__get (sample->evsel); + }if (sample->evsel) {dso__set_hit (dso);
tools/perf/builtin-trace.c +4 -1
@@ -3657,8 +3657,11 @@ static void trace__handle_event(struct trace *trace, union perf_event *event, streturn ; } -if (sample->evsel ==NULL ) +if (sample->evsel ==NULL ) { sample->evsel =evlist__id2evsel (trace->evlist, sample->id); +if (sample->evsel) +evsel__get (sample->evsel); + }if (sample->evsel ==NULL ) {fprintf (trace->output,"Unknown tp ID %" PRIu64", skipping...\n" , sample->id);
tools/perf/util/evsel.c +1 -1
@@ -3365,7 +3365,7 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event,union u64_swap u;perf_sample__init (data,/*all=*/ true); - data->evsel = evsel; + data->evsel =evsel__get (evsel); data->cpu = data->pid = data->tid =-1 ; data->stream_id = data->id = data->time =-1ULL ; data->period = evsel->core.attr.sample_period;
tools/perf/util/intel-pt.c +5 -3
@@ -1830,9 +1830,9 @@ static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq) ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt; } -perf_sample__exit (&sample); ret =intel_pt_deliver_synth_event (pt, event, &sample, pt->branches_sample_type); +perf_sample__exit (&sample);return ret; }
@@ -1967,6 +1967,7 @@ static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)union perf_event *event = ptq->event_buf;struct perf_sample sample = { .ip =0 , };struct perf_synth_intel_ptwrite raw; +int ret;if (intel_pt_skip_event (pt))return 0 ;
@@ -1983,8 +1984,9 @@ static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq) sample.raw_size =perf_synth__raw_size (raw); sample.raw_data =perf_synth__raw_data (&raw); -return intel_pt_deliver_synth_event (pt, event, &sample, - pt->ptwrites_sample_type); + ret =intel_pt_deliver_synth_event (pt, event, &sample, pt->ptwrites_sample_type); +perf_sample__exit (&sample); +return ret; }static int intel_pt_synth_cbr_sample (struct intel_pt_queue *ptq)
tools/perf/util/python.c +3 -7
@@ -63,7 +63,6 @@ PyMODINIT_FUNC PyInit_perf(void);struct pyrf_event {PyObject_HEAD - struct evsel *evsel; struct perf_sample sample;union perf_event event; };
@@ -294,7 +293,6 @@ static PyMemberDef pyrf_sample_event__members[] = {static void pyrf_sample_event__delete (struct pyrf_event *pevent) { -evsel__put (pevent->evsel);perf_sample__exit (&pevent->sample);Py_TYPE (pevent)->tp_free ((PyObject *)pevent); }
@@ -316,7 +314,7 @@ static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent)#ifdef HAVE_LIBTRACEEVENT static bool is_tracepoint (const struct pyrf_event *pevent) { -return pevent->evsel->core.attr.type ==PERF_TYPE_TRACEPOINT ; +return pevent->sample.evsel->core.attr.type ==PERF_TYPE_TRACEPOINT ; } static PyObject*
@@ -363,7 +361,7 @@ tracepoint_field(const struct pyrf_event *pe, struct tep_format_field *field)static PyObject *get_tracepoint_field (struct pyrf_event *pevent,PyObject *attr_name) { -struct evsel *evsel = pevent->evsel; +struct evsel *evsel = pevent->sample.evsel;struct tep_event *tp_format =evsel__tp_format (evsel);struct tep_format_field *field;
@@ -529,7 +527,7 @@ static PyObject *pyrf_event__new(const union perf_event *event) pevent = PyObject_New(struct pyrf_event, ptype);if (pevent !=NULL ) {memcpy (&pevent->event, event, event->header.size); - pevent->evsel =NULL ; +perf_sample__init (&pevent->sample,/*all=*/ false); }return (PyObject *)pevent; }
@@ -1907,8 +1905,6 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,return Py_None; } - pevent->evsel =evsel__get (evsel); -perf_mmap__consume (&md->core); err =evsel__parse_sample (evsel, &pevent->event, &pevent->sample);
tools/perf/util/s390-sample-raw.c +1 -0
@@ -343,6 +343,7 @@ void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event, sample->evsel =evlist__event2evsel (evlist, event);if (!sample->evsel)return ; +evsel__get (sample->evsel); }/* Check for raw data in sample */
tools/perf/util/sample.c +12 -5
@@ -1,18 +1,23 @@/* SPDX-License-Identifier: GPL-2.0 */ #include "sample.h" + +#include <stdlib.h> +#include <string.h> + +#include <elf.h> +#include <linux/zalloc.h> + +#include "../../arch/x86/include/asm/insn.h" #include "debug.h" +#include "evsel.h" #include "thread.h" -#include <elf.h> +#ifndef EM_CSKY #define EM_CSKY 252#endif #ifndef EM_LOONGARCH #define EM_LOONGARCH 258#endif -#include <linux/zalloc.h> -#include <stdlib.h> -#include <string.h> -#include "../../arch/x86/include/asm/insn.h" void perf_sample__init (struct perf_sample *sample,bool all) {
@@ -29,6 +34,8 @@ void perf_sample__init(struct perf_sample *sample, bool all)void perf_sample__exit (struct perf_sample *sample) { +evsel__put (sample->evsel); + sample->evsel =NULL ;zfree (&sample->user_regs);zfree (&sample->intr_regs);if (sample->merged_callchain) {
tools/perf/util/session.c +16 -3
@@ -1854,6 +1854,7 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,list_for_each_entry_safe (de, tmp,evlist__deferred_samples (evlist), list) {struct perf_sample orig_sample; +struct evsel *new_evsel;perf_sample__init (&orig_sample,/*all=*/ false); ret =evlist__parse_sample (evlist, de->event, &orig_sample);
@@ -1874,7 +1875,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,else orig_sample.deferred_callchain = false; - orig_sample.evsel =evlist__id2evsel (evlist, orig_sample.id); + new_evsel =evlist__id2evsel (evlist, orig_sample.id); +if (new_evsel != orig_sample.evsel) { +evsel__put (orig_sample.evsel); + orig_sample.evsel =evsel__get (new_evsel); + } ret =evlist__deliver_sample (evlist, tool, de->event, &orig_sample, machine);
@@ -1903,6 +1908,7 @@ static int session__flush_deferred_samples(struct perf_session *session,list_for_each_entry_safe (de, tmp,evlist__deferred_samples (evlist), list) {struct perf_sample sample; +struct evsel *new_evsel;perf_sample__init (&sample,/*all=*/ false); ret =evlist__parse_sample (evlist, de->event, &sample);
@@ -1913,7 +1919,11 @@ static int session__flush_deferred_samples(struct perf_session *session, } sample.file_offset = de->file_offset; - sample.evsel =evlist__id2evsel (evlist, sample.id); + new_evsel =evlist__id2evsel (evlist, sample.id); +if (new_evsel != sample.evsel) { +evsel__put (sample.evsel); + sample.evsel =evsel__get (new_evsel); + } ret =evlist__deliver_sample (evlist, tool, de->event, &sample, machine);
@@ -1959,8 +1969,11 @@ static int machines__deliver_event(struct machines *machines,dump_event (evlist, event, file_offset, sample, file_path); -if (!sample->evsel) +if (!sample->evsel) { sample->evsel =evlist__id2evsel (evlist, sample->id); +if (sample->evsel) + sample->evsel =evsel__get (sample->evsel); + }else assert (sample->evsel ==evlist__id2evsel (evlist, sample->id)); machine =machines__find_for_cpumode (machines, event, sample);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
[email protected]
AI reviewRe: [PATCH v21 08/21] perf python: Use evsel in sample in pyrf_event
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Missing NULL check before calling evsel__get() on the result of evlist__id2evsel() leads to a…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Missing NULL check before calling evsel__get() on the result of evlist__id2evsel() leads to a potential crash.
- [Medium] Missing perf_sample__exit() on the error path of guest_session__fetch() leaks the evsel reference.
- [Medium] Missing perf_sample__exit() on the error path out of perf_mmap__read_event() loop in test__perf_time_to_tsc leaks the evsel reference. --
commit c23db56d8fc8d40b59596916ce6b0537514263aa
Author: Ian Rogers <[email protected]>
perf python: Use evsel in sample in pyrf_event
Avoid a duplicated evsel by using the one in sample. Add evsel__get/put to the evsel in perf_sample.
tools/perf/util/evsel.c int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, · line 3368
Quoted 10 lines
tools/perf/util/evsel.c +1
-1@@ -3365,7 +3365,7 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,union u64_swap u;perf_sample__init (data,/*all=*/ true); - data->evsel = evsel; + data->evsel =evsel__get (evsel);
[Severity: Medium]
Does this new reference acquisition cause memory leaks in existing callers that do not expect sample.evsel to hold a reference on early errors?
For example, in tools/perf/builtin-inject.c:guest_session__inject_events(), if guest_session__fetch() fails, it returns an error but the cleanup block checking if (ret && gs->fetched) prevents perf_sample__exit() from running, leaking the evsel reference.
Similarly, in tools/perf/tests/perf-time-to-tsc.c:test__perf_time_to_tsc(), the CHECK__(evsel__parse_sample(...)) macro jumps to out_err on failure, bypassing perf_sample__exit(&sample) and permanently leaking the newly acquired evsel.
Quoted 3 lines
tools/perf/util/evsel.c +0
-0data->cpu = data->pid = data->tid =-1 ; data->stream_id = data->id = data->time =-1ULL ; data->period = evsel->core.attr.sample_period;
[ ... ]
Quoted 5 lines
tools/perf/util/session.c +0
-0
tools/perf/util/session.c static int evlist__deliver_deferred_callchain(struct evlist *evlist, · line 1882
Quoted 9 lines
tools/perf/util/session.c +5
-1@@ -1874,7 +1875,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,else orig_sample.deferred_callchain = false; - orig_sample.evsel =evlist__id2evsel (evlist, orig_sample.id); + new_evsel =evlist__id2evsel (evlist, orig_sample.id); +if (new_evsel != orig_sample.evsel) { +evsel__put (orig_sample.evsel); + orig_sample.evsel =evsel__get (new_evsel); + }
[Severity: High]
Can this cause a segmentation fault if the sample ID is unknown or corrupted?
The evlist__id2evsel() function can return NULL for malformed perf.data files. If new_evsel is NULL, passing it directly to evsel__get(new_evsel) will dereference the NULL pointer when trying to increment its reference count.
Could we add a NULL check here before calling evsel__get()?
tools/perf/util/session.c static int session__flush_deferred_samples(struct perf_session *session, · line 1926
Quoted 13 lines
tools/perf/util/session.c +5
-1ret =evlist__deliver_sample (evlist, tool, de->event, &orig_sample, machine);@@ -1913,7 +1919,11 @@ static int session__flush_deferred_samples(struct perf_session *session, } sample.file_offset = de->file_offset; - sample.evsel =evlist__id2evsel (evlist, sample.id); + new_evsel =evlist__id2evsel (evlist, sample.id); +if (new_evsel != sample.evsel) { +evsel__put (sample.evsel); + sample.evsel =evsel__get (new_evsel); + }
[Severity: High]
Similarly, could evlist__id2evsel() return NULL here and cause a crash when passed to evsel__get()?
Quoted 3 lines
tools/perf/util/session.c +0
-0ret =evlist__deliver_sample (evlist, tool, de->event, &sample, machine);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v21 08/21] perf python: Use evsel in sample in pyrf_event
Quoted 21 lines · On Tue, Jun 16, 2026 at 01:52:16AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Missing NULL check before callingevsel__get()on the result ofevlist__id2evsel()leads to a potential crash.
- [Medium] Missingperf_sample__exit()on the error path ofguest_session__fetch()leaks the evsel reference.
- [Medium] Missingperf_sample__exit()on the error path out ofperf_mmap__read_event()loop intest__perf_time_to_tscleaks the evsel reference. --commit
c23db56d8fc8d40b59596916ce6b0537514263aa
Author: Ian Rogers <[email protected]>perf python: Use evsel in sample in
pyrf_eventAvoid a duplicated evsel by using the one in sample. Add evsel__get/put to the evsel in
perf_sample.Quoted 7 lines
tools/perf/util/evsel.c +0
-0@@ -3365,7 +3365,7 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,union u64_swap u;
Quoted 7 lines
tools/perf/util/evsel.c int evsel__parse_sample(struct evsel *evsel, union perf_event *event, · line 3368
Quoted 3 lines
tools/perf/util/evsel.c +1
-1perf_sample__init (data,/*all=*/ true); - data->evsel = evsel; + data->evsel =evsel__get (evsel);[Severity: Medium]
Does this new reference acquisition cause memory leaks in existing callers that do not expect sample.evsel to hold a reference on early errors?
So this was something I wasn't expecting: patches for tools/perf/util/ inside a python binding series :-\
Quoted 4 lines
For example, in tools/perf/builtin-inject.c:guest_session__inject_events(), if
guest_session__fetch()fails, it returns an error but the cleanup block checking if (ret &&gs->fetched) preventsperf_sample__exit()from running, leaking the evsel reference.
this looks valid, no?
And while looking at it, there is another bug in that function, but now I'm looking like sashiko ;-)
Namely:
tools/perf/builtin-inject.c
╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
1970 if (sample->cpu >= gs->vcpu_cnt) {
1971 pr_err("Guest event with unknown VCPU %u\n",
1972 sample->cpu);
1973 - return -EINVAL;
1973 + ret = -EINVAL;
1974 + break;
1975 }
1976 /* Change to host CPU instead of guest VCPU */
1977 sample->cpu = gs->vcpu[sample->cpu].cpu;But the comment for your patch seems actionable, i.e. it is an issue introduced by your series, by calling that evsel__get(), no?
Quoted 59 lines
Similarly, in tools/perf/tests/perf-time-to-tsc.c:test__perf_time_to_tsc(), the CHECK__(evsel__parse_sample(...)) macro jumps to
out_erron failure, bypassingperf_sample__exit(&sample)and permanently leaking the newly acquired evsel.Quoted 3 lines
tools/perf/util/evsel.c +0
-0data->cpu = data->pid = data->tid =-1 ; data->stream_id = data->id = data->time =-1ULL ; data->period = evsel->core.attr.sample_period;[ ... ]
Quoted 5 lines
tools/perf/util/session.c +0
-0tools/perf/util/session.c static int evlist__deliver_deferred_callchain(struct evlist *evlist, · line 1882
Quoted 9 lines
tools/perf/util/session.c +5
-1@@ -1874,7 +1875,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,else orig_sample.deferred_callchain = false; - orig_sample.evsel =evlist__id2evsel (evlist, orig_sample.id); + new_evsel =evlist__id2evsel (evlist, orig_sample.id); +if (new_evsel != orig_sample.evsel) { +evsel__put (orig_sample.evsel); + orig_sample.evsel =evsel__get (new_evsel); + }[Severity: High]
Can this cause a segmentation fault if the sample ID is unknown or corrupted?The
evlist__id2evsel()function can return NULL for malformed perf.data files. Ifnew_evselis NULL, passing it directly toevsel__get(new_evsel)will dereference the NULL pointer when trying to increment its reference count.Could we add a NULL check here before calling
evsel__get()?tools/perf/util/session.c static int session__flush_deferred_samples(struct perf_session *session, · line 1926
Quoted 13 lines
tools/perf/util/session.c +5
-1ret =evlist__deliver_sample (evlist, tool, de->event, &orig_sample, machine);@@ -1913,7 +1919,11 @@ static int session__flush_deferred_samples(struct perf_session *session, } sample.file_offset = de->file_offset; - sample.evsel =evlist__id2evsel (evlist, sample.id); + new_evsel =evlist__id2evsel (evlist, sample.id); +if (new_evsel != sample.evsel) { +evsel__put (sample.evsel); + sample.evsel =evsel__get (new_evsel); + }[Severity: High]
Similarly, couldevlist__id2evsel()return NULL here and cause a crash when passed toevsel__get()?Quoted 3 lines
tools/perf/util/session.c +0
-0ret =evlist__deliver_sample (evlist, tool, de->event, &sample, machine);Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Ian Rogers
reviewerRe: [PATCH v21 08/21] perf python: Use evsel in sample in pyrf_event
Quoted 31 lines · Arnaldo Carvalho de Melo
Quoted 21 lines · On Tue, Jun 16, 2026 at 01:52:16AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Missing NULL check before callingevsel__get()on the result ofevlist__id2evsel()leads to a potential crash.
- [Medium] Missingperf_sample__exit()on the error path ofguest_session__fetch()leaks the evsel reference.
- [Medium] Missingperf_sample__exit()on the error path out ofperf_mmap__read_event()loop intest__perf_time_to_tscleaks the evsel reference. --commit
c23db56d8fc8d40b59596916ce6b0537514263aa
Author: Ian Rogers <[email protected]>perf python: Use evsel in sample in
pyrf_eventAvoid a duplicated evsel by using the one in sample. Add evsel__get/put to the evsel in
perf_sample.Quoted 7 lines
tools/perf/util/evsel.c +0
-0@@ -3365,7 +3365,7 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,union u64_swap u;Quoted 7 lines
tools/perf/util/evsel.c int evsel__parse_sample(struct evsel *evsel, union perf_event *event, · line 3368
Quoted 3 lines
tools/perf/util/evsel.c +1
-1perf_sample__init (data,/*all=*/ true); - data->evsel = evsel; + data->evsel =evsel__get (evsel);[Severity: Medium]
Does this new reference acquisition cause memory leaks in existing callers that do not expect sample.evsel to hold a reference on early errors?
Quoted 2 lines
So this was something I wasn't expecting: patches for
tools/perf/util/inside a python binding series :-\
So this was the point of sending the RFC early:
https://lore.kernel.org/linux-perf-users/[email protected]/
When working with the session API, copying evsels and evlists becomes painful; it's easier to hand references to the parts of the system that need them instead of copying and ensuring the copying is correct.
Quoted 24 lines
Quoted 4 lines
For example, in tools/perf/builtin-inject.c:guest_session__inject_events(), if
guest_session__fetch()fails, it returns an error but the cleanup block checking if (ret &&gs->fetched) preventsperf_sample__exit()from running, leaking the evsel reference.this looks valid, no?
And while looking at it, there is another bug in that function, but now I'm looking like sashiko ;-)
Namely:
tools/perf/builtin-inject.c╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ 1970 if (sample->cpu >= gs->vcpu_cnt) { 1971 pr_err("Guest event with unknown VCPU %u\n", 1972 sample->cpu); 1973 - return -EINVAL; 1973 + ret = -EINVAL; 1974 + break; 1975 } 1976 /* Change to host CPU instead of guest VCPU */ 1977 sample->cpu = gs->vcpu[sample->cpu].cpu;
Quoted 2 lines
But the comment for your patch seems actionable, i.e. it is an issue introduced by your series, by calling that
evsel__get(), no?
Hmm.. I checked that the tests were clean using leak sanitizer and reference count checking. There's no reference count checking on evsels as the scope of the fix is so large for that. I'll try to find other issues, and make a v22, but much of the Sashiko feedback has proven to cover things that are either pre-existing, or addressed later in this series. In this case the missing perf_sample__exit was pre-existing and we could have been leaking other values like sampled registers.
Thanks,
Ian
Quoted 59 lines
Quoted 59 lines
Similarly, in tools/perf/tests/perf-time-to-tsc.c:test__perf_time_to_tsc(), the CHECK__(evsel__parse_sample(...)) macro jumps to
out_erron failure, bypassingperf_sample__exit(&sample)and permanently leaking the newly acquired evsel.Quoted 3 lines
tools/perf/util/evsel.c +0
-0data->cpu = data->pid = data->tid =-1 ; data->stream_id = data->id = data->time =-1ULL ; data->period = evsel->core.attr.sample_period;[ ... ]
Quoted 5 lines
tools/perf/util/session.c +0
-0tools/perf/util/session.c static int evlist__deliver_deferred_callchain(struct evlist *evlist, · line 1882
Quoted 9 lines
tools/perf/util/session.c +5
-1@@ -1874,7 +1875,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,else orig_sample.deferred_callchain = false; - orig_sample.evsel =evlist__id2evsel (evlist, orig_sample.id); + new_evsel =evlist__id2evsel (evlist, orig_sample.id); +if (new_evsel != orig_sample.evsel) { +evsel__put (orig_sample.evsel); + orig_sample.evsel =evsel__get (new_evsel); + }[Severity: High]
Can this cause a segmentation fault if the sample ID is unknown or corrupted?The
evlist__id2evsel()function can return NULL for malformed perf.data files. Ifnew_evselis NULL, passing it directly toevsel__get(new_evsel)will dereference the NULL pointer when trying to increment its reference count.Could we add a NULL check here before calling
evsel__get()?tools/perf/util/session.c static int session__flush_deferred_samples(struct perf_session *session, · line 1926
Quoted 13 lines
tools/perf/util/session.c +5
-1ret =evlist__deliver_sample (evlist, tool, de->event, &orig_sample, machine);@@ -1913,7 +1919,11 @@ static int session__flush_deferred_samples(struct perf_session *session, } sample.file_offset = de->file_offset; - sample.evsel =evlist__id2evsel (evlist, sample.id); + new_evsel =evlist__id2evsel (evlist, sample.id); +if (new_evsel != sample.evsel) { +evsel__put (sample.evsel); + sample.evsel =evsel__get (new_evsel); + }[Severity: High]
Similarly, couldevlist__id2evsel()return NULL here and cause a crash when passed toevsel__get()?tools/perf/util/session.c static int session__flush_deferred_samples(struct perf_session *session, · line 1929
Quoted 3 lines
tools/perf/util/session.c +0
-0ret =evlist__deliver_sample (evlist, tool, de->event, &sample, machine);--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v21 08/21] perf python: Use evsel in sample in pyrf_event
Quoted 33 lines · Ian Rogers
Quoted 31 lines · Arnaldo Carvalho de Melo
Quoted 28 lines · On Tue, Jun 16, 2026 at 01:52:16AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Missing NULL check before callingevsel__get()on the result ofevlist__id2evsel()leads to a potential crash.
- [Medium] Missingperf_sample__exit()on the error path ofguest_session__fetch()leaks the evsel reference.
- [Medium] Missingperf_sample__exit()on the error path out ofperf_mmap__read_event()loop intest__perf_time_to_tscleaks the evsel reference. --commit
c23db56d8fc8d40b59596916ce6b0537514263aa
Author: Ian Rogers <[email protected]>perf python: Use evsel in sample in
pyrf_eventAvoid a duplicated evsel by using the one in sample. Add evsel__get/put to the evsel in
perf_sample.tools/perf/util/evsel.c int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, · line 3368
Quoted 10 lines
tools/perf/util/evsel.c +1
-1@@ -3365,7 +3365,7 @@ int __evsel__parse_sample(struct evsel *evsel, union perf_event *event,union u64_swap u;perf_sample__init (data,/*all=*/ true); - data->evsel = evsel; + data->evsel =evsel__get (evsel);[Severity: Medium]
Does this new reference acquisition cause memory leaks in existing callers that do not expect sample.evsel to hold a reference on early errors?
Quoted 8 lines
Quoted 2 lines
So this was something I wasn't expecting: patches for
tools/perf/util/inside a python binding series :-\So this was the point of sending the RFC early:
https://lore.kernel.org/linux-perf-users/[email protected]/
When working with the session API, copying evsels and evlists becomes painful; it's easier to hand references to the parts of the system that need them instead of copying and ensuring the copying is correct.
I like the whole series, its just that when coming back to look at it I was expecting to see it separated into a "prep the env" followed to a "use what was done before", but nah, I'll try merging it now.
Quoted 27 lines
Quoted 24 lines
Quoted 4 lines
For example, in tools/perf/builtin-inject.c:guest_session__inject_events(), if
guest_session__fetch()fails, it returns an error but the cleanup block checking if (ret &&gs->fetched) preventsperf_sample__exit()from running, leaking the evsel reference.this looks valid, no?
And while looking at it, there is another bug in that function, but now I'm looking like sashiko ;-)
Namely:
tools/perf/builtin-inject.c╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ 1970 if (sample->cpu >= gs->vcpu_cnt) { 1971 pr_err("Guest event with unknown VCPU %u\n", 1972 sample->cpu); 1973 - return -EINVAL; 1973 + ret = -EINVAL; 1974 + break; 1975 } 1976 /* Change to host CPU instead of guest VCPU */ 1977 sample->cpu = gs->vcpu[sample->cpu].cpu;Quoted 2 lines
But the comment for your patch seems actionable, i.e. it is an issue introduced by your series, by calling that
evsel__get(), no?
Quoted 8 lines
Hmm.. I checked that the tests were clean using leak sanitizer and reference count checking. There's no reference count checking on evsels as the scope of the fix is so large for that. I'll try to find other issues, and make a v22, but much of the Sashiko feedback has proven to cover things that are either pre-existing, or addressed later in this series. In this case the missing
perf_sample__exitwas pre-existing and we could have been leaking other values like sampled registers.
Fair enough, I'll ask Claude to track this for us, so that we don't lose track of it and fix it eventually.
- Arnaldo
Quoted 62 lines
Thanks,
IanQuoted 59 lines
Quoted 59 lines
Similarly, in tools/perf/tests/perf-time-to-tsc.c:test__perf_time_to_tsc(), the CHECK__(evsel__parse_sample(...)) macro jumps to
out_erron failure, bypassingperf_sample__exit(&sample)and permanently leaking the newly acquired evsel.Quoted 3 lines
tools/perf/util/evsel.c +0
-0data->cpu = data->pid = data->tid =-1 ; data->stream_id = data->id = data->time =-1ULL ; data->period = evsel->core.attr.sample_period;[ ... ]
Quoted 5 lines
tools/perf/util/session.c +0
-0tools/perf/util/session.c static int evlist__deliver_deferred_callchain(struct evlist *evlist, · line 1882
Quoted 9 lines
tools/perf/util/session.c +5
-1@@ -1874,7 +1875,11 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,else orig_sample.deferred_callchain = false; - orig_sample.evsel =evlist__id2evsel (evlist, orig_sample.id); + new_evsel =evlist__id2evsel (evlist, orig_sample.id); +if (new_evsel != orig_sample.evsel) { +evsel__put (orig_sample.evsel); + orig_sample.evsel =evsel__get (new_evsel); + }[Severity: High]
Can this cause a segmentation fault if the sample ID is unknown or corrupted?The
evlist__id2evsel()function can return NULL for malformed perf.data files. Ifnew_evselis NULL, passing it directly toevsel__get(new_evsel)will dereference the NULL pointer when trying to increment its reference count.Could we add a NULL check here before calling
evsel__get()?tools/perf/util/session.c static int session__flush_deferred_samples(struct perf_session *session, · line 1926
Quoted 13 lines
tools/perf/util/session.c +5
-1ret =evlist__deliver_sample (evlist, tool, de->event, &orig_sample, machine);@@ -1913,7 +1919,11 @@ static int session__flush_deferred_samples(struct perf_session *session, } sample.file_offset = de->file_offset; - sample.evsel =evlist__id2evsel (evlist, sample.id); + new_evsel =evlist__id2evsel (evlist, sample.id); +if (new_evsel != sample.evsel) { +evsel__put (sample.evsel); + sample.evsel =evsel__get (new_evsel); + }[Severity: High]
Similarly, couldevlist__id2evsel()return NULL here and cause a crash when passed toevsel__get()?tools/perf/util/session.c static int session__flush_deferred_samples(struct perf_session *session, · line 1929
Quoted 3 lines
tools/perf/util/session.c +0
-0ret =evlist__deliver_sample (evlist, tool, de->event, &sample, machine);--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8