From nobody Tue Jun 23 13:16:26 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0FE42C4321E for ; Mon, 28 Mar 2022 06:24:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235004AbiC1G0X (ORCPT ); Mon, 28 Mar 2022 02:26:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236694AbiC1G0G (ORCPT ); Mon, 28 Mar 2022 02:26:06 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 789564EA26 for ; Sun, 27 Mar 2022 23:24:24 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id b16-20020a253410000000b00633b9e71eecso10135732yba.14 for ; Sun, 27 Mar 2022 23:24:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=DsGPUblhs6jgawOdjH7k+Ssh4HZcDt3nLqv+p577Wb4=; b=eZkm6L1DipR0tE3RTEJnRvsu3BnrSNidljK1XHaeA1XZ7JoUtJVffDmKKyYFYMJ4q8 IMopLcE2U2i0992lngXzYSGXe6rnWUymiRAwLvtxkrZZMZU1Z3A95aWH3MwyJjkfR3G/ KK3gqHF3T/9D3JY7BHVMdVbcoS2Oc4MBQ1PsJi4B4DoSs3ihMyQQm8oSXErEuoW+HteF N26U7EhKemq0BdhbuNKe9mnvjOB3aguR9owFwNZS92XIL2Y1nIM+kcydC7ymw6Hl+psr Rag0fbB1VvMsEDiIGoyvseywMY9Y246EXCzwcUcYHEj2zSNoqLjSaM4lK2xKBRHw3gOY BgHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=DsGPUblhs6jgawOdjH7k+Ssh4HZcDt3nLqv+p577Wb4=; b=YZiHJqSWd5+w71pCmnLVDixolr3unr+dGe7jxN0DiBSxGKtb8LHtNuKMQeT7lICoEn aKoZLbzJPwKMo6c7gaVhDr0z8Jm+0IIZlKLcqCchPnEK/bHpXfSrcHIQl6kbmUow0IcL /sPHBDjO6i9wyeaX4DzgS46ChOmzpdL8Lgz3sRHiJ5ofIYCigrWUFih/ttmtXgqGDjF8 pIV+PlaZYfyx4XL/TU19qINcXorL+Zft25XEw/VHGumDzT5yf8aRhhPPiBHSFqCC9b5p KcJbziSXF31qg1apa4jUB8Sc7ofN1cEyuuKJQjft80d6ZiYUm0j2J1kdN9r+mR58Mvxh GpOw== X-Gm-Message-State: AOAM5328TMAD9kAgYBs3aWDk5qFHKQ3RQxsqhgdBqx5o7BfUkQdIR4Md cj8EUcGDFaXBnls5jkElZp5AYnaCGyxJ X-Google-Smtp-Source: ABdhPJw73Kv3NBNNWXSKzmHSm18phLJ3zwJLTRUyYZoaRW3OKqDL8lGu6vLpWujhaO3V7sYsTlpJnGyuxuiM X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ef08:ed1b:261f:77fa]) (user=irogers job=sendgmr) by 2002:a25:a541:0:b0:628:75d5:1982 with SMTP id h59-20020a25a541000000b0062875d51982mr20773011ybi.520.1648448663656; Sun, 27 Mar 2022 23:24:23 -0700 (PDT) Date: Sun, 27 Mar 2022 23:24:10 -0700 In-Reply-To: <20220328062414.1893550-1-irogers@google.com> Message-Id: <20220328062414.1893550-2-irogers@google.com> Mime-Version: 1.0 References: <20220328062414.1893550-1-irogers@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH 1/5] perf evlist: Rename cpus to user_cpus From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Kajol Jain , James Clark , German Gomez , Adrian Hunter , Riccardo Mancini , Andi Kleen , Alexey Bayduraev , Alexander Antonov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" evlist contains cpus and all_cpus. all_cpus is the union of the cpu maps of all evsels. cpus is set to be cpus required from the command line, defaulting to all online cpus if no cpus are specified. For something like an uncore event, all_cpus may just be CPU 0, however, all_cpus may be every online CPU. This causes all_cpus to have fewer values than the cpus variable which is confusing given the 'all' in the name. To try to make the behavior clearer, rename cpus to user_cpus and add comments on the two struct variables. Signed-off-by: Ian Rogers --- tools/lib/perf/evlist.c | 28 ++++++++++++------------ tools/lib/perf/include/internal/evlist.h | 4 +++- tools/perf/arch/arm/util/cs-etm.c | 8 +++---- tools/perf/arch/arm64/util/arm-spe.c | 2 +- tools/perf/arch/x86/util/intel-bts.c | 2 +- tools/perf/arch/x86/util/intel-pt.c | 4 ++-- tools/perf/bench/evlist-open-close.c | 2 +- tools/perf/builtin-ftrace.c | 2 +- tools/perf/builtin-record.c | 6 ++--- tools/perf/builtin-stat.c | 8 +++---- tools/perf/builtin-top.c | 2 +- tools/perf/util/auxtrace.c | 2 +- tools/perf/util/bpf_ftrace.c | 4 ++-- tools/perf/util/evlist.c | 14 ++++++------ tools/perf/util/record.c | 6 ++--- tools/perf/util/sideband_evlist.c | 2 +- tools/perf/util/stat-display.c | 2 +- tools/perf/util/synthetic-events.c | 2 +- tools/perf/util/top.c | 7 +++--- 19 files changed, 55 insertions(+), 52 deletions(-) diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c index 9a770bfdc804..e29dc229768a 100644 --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -41,10 +41,10 @@ static void __perf_evlist__propagate_maps(struct perf_e= vlist *evlist, */ if (!evsel->own_cpus || evlist->has_user_cpus) { perf_cpu_map__put(evsel->cpus); - evsel->cpus =3D perf_cpu_map__get(evlist->cpus); - } else if (!evsel->system_wide && perf_cpu_map__empty(evlist->cpus)) { + evsel->cpus =3D perf_cpu_map__get(evlist->user_cpus); + } else if (!evsel->system_wide && perf_cpu_map__empty(evlist->user_cpus))= { perf_cpu_map__put(evsel->cpus); - evsel->cpus =3D perf_cpu_map__get(evlist->cpus); + evsel->cpus =3D perf_cpu_map__get(evlist->user_cpus); } else if (evsel->cpus !=3D evsel->own_cpus) { perf_cpu_map__put(evsel->cpus); evsel->cpus =3D perf_cpu_map__get(evsel->own_cpus); @@ -123,10 +123,10 @@ static void perf_evlist__purge(struct perf_evlist *ev= list) =20 void perf_evlist__exit(struct perf_evlist *evlist) { - perf_cpu_map__put(evlist->cpus); + perf_cpu_map__put(evlist->user_cpus); perf_cpu_map__put(evlist->all_cpus); perf_thread_map__put(evlist->threads); - evlist->cpus =3D NULL; + evlist->user_cpus =3D NULL; evlist->all_cpus =3D NULL; evlist->threads =3D NULL; fdarray__exit(&evlist->pollfd); @@ -155,9 +155,9 @@ void perf_evlist__set_maps(struct perf_evlist *evlist, * original reference count of 1. If that is not the case it is up to * the caller to increase the reference count. */ - if (cpus !=3D evlist->cpus) { - perf_cpu_map__put(evlist->cpus); - evlist->cpus =3D perf_cpu_map__get(cpus); + if (cpus !=3D evlist->user_cpus) { + perf_cpu_map__put(evlist->user_cpus); + evlist->user_cpus =3D perf_cpu_map__get(cpus); } =20 if (threads !=3D evlist->threads) { @@ -294,7 +294,7 @@ int perf_evlist__id_add_fd(struct perf_evlist *evlist, =20 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) { - int nr_cpus =3D perf_cpu_map__nr(evlist->cpus); + int nr_cpus =3D perf_cpu_map__nr(evlist->user_cpus); int nr_threads =3D perf_thread_map__nr(evlist->threads); int nfds =3D 0; struct perf_evsel *evsel; @@ -426,7 +426,7 @@ mmap_per_evsel(struct perf_evlist *evlist, struct perf_= evlist_mmap_ops *ops, int idx, struct perf_mmap_param *mp, int cpu_idx, int thread, int *_output, int *_output_overwrite) { - struct perf_cpu evlist_cpu =3D perf_cpu_map__cpu(evlist->cpus, cpu_idx); + struct perf_cpu evlist_cpu =3D perf_cpu_map__cpu(evlist->user_cpus, cpu_i= dx); struct perf_evsel *evsel; int revent; =20 @@ -536,7 +536,7 @@ mmap_per_cpu(struct perf_evlist *evlist, struct perf_ev= list_mmap_ops *ops, struct perf_mmap_param *mp) { int nr_threads =3D perf_thread_map__nr(evlist->threads); - int nr_cpus =3D perf_cpu_map__nr(evlist->cpus); + int nr_cpus =3D perf_cpu_map__nr(evlist->user_cpus); int cpu, thread; =20 for (cpu =3D 0; cpu < nr_cpus; cpu++) { @@ -564,8 +564,8 @@ static int perf_evlist__nr_mmaps(struct perf_evlist *ev= list) { int nr_mmaps; =20 - nr_mmaps =3D perf_cpu_map__nr(evlist->cpus); - if (perf_cpu_map__empty(evlist->cpus)) + nr_mmaps =3D perf_cpu_map__nr(evlist->user_cpus); + if (perf_cpu_map__empty(evlist->user_cpus)) nr_mmaps =3D perf_thread_map__nr(evlist->threads); =20 return nr_mmaps; @@ -576,7 +576,7 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist, struct perf_mmap_param *mp) { struct perf_evsel *evsel; - const struct perf_cpu_map *cpus =3D evlist->cpus; + const struct perf_cpu_map *cpus =3D evlist->user_cpus; const struct perf_thread_map *threads =3D evlist->threads; =20 if (!ops || !ops->get || !ops->mmap) diff --git a/tools/lib/perf/include/internal/evlist.h b/tools/lib/perf/incl= ude/internal/evlist.h index 4cefade540bd..5f95672662ae 100644 --- a/tools/lib/perf/include/internal/evlist.h +++ b/tools/lib/perf/include/internal/evlist.h @@ -19,7 +19,9 @@ struct perf_evlist { int nr_entries; int nr_groups; bool has_user_cpus; - struct perf_cpu_map *cpus; + /** The list of cpus passed from the command line. */ + struct perf_cpu_map *user_cpus; + /** The union of all evsel cpu maps. */ struct perf_cpu_map *all_cpus; struct perf_thread_map *threads; int nr_mmaps; diff --git a/tools/perf/arch/arm/util/cs-etm.c b/tools/perf/arch/arm/util/c= s-etm.c index cbc555245959..405d58903d84 100644 --- a/tools/perf/arch/arm/util/cs-etm.c +++ b/tools/perf/arch/arm/util/cs-etm.c @@ -199,7 +199,7 @@ static int cs_etm_set_option(struct auxtrace_record *it= r, struct evsel *evsel, u32 option) { int i, err =3D -EINVAL; - struct perf_cpu_map *event_cpus =3D evsel->evlist->core.cpus; + struct perf_cpu_map *event_cpus =3D evsel->evlist->core.user_cpus; struct perf_cpu_map *online_cpus =3D perf_cpu_map__new(NULL); =20 /* Set option of each CPU we have */ @@ -299,7 +299,7 @@ static int cs_etm_recording_options(struct auxtrace_rec= ord *itr, container_of(itr, struct cs_etm_recording, itr); struct perf_pmu *cs_etm_pmu =3D ptr->cs_etm_pmu; struct evsel *evsel, *cs_etm_evsel =3D NULL; - struct perf_cpu_map *cpus =3D evlist->core.cpus; + struct perf_cpu_map *cpus =3D evlist->core.user_cpus; bool privileged =3D perf_event_paranoid_check(-1); int err =3D 0; =20 @@ -522,7 +522,7 @@ cs_etm_info_priv_size(struct auxtrace_record *itr __may= be_unused, { int i; int etmv3 =3D 0, etmv4 =3D 0, ete =3D 0; - struct perf_cpu_map *event_cpus =3D evlist->core.cpus; + struct perf_cpu_map *event_cpus =3D evlist->core.user_cpus; struct perf_cpu_map *online_cpus =3D perf_cpu_map__new(NULL); =20 /* cpu map is not empty, we have specific CPUs to work with */ @@ -713,7 +713,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 =3D session->evlist->core.cpus; + struct perf_cpu_map *event_cpus =3D session->evlist->core.user_cpus; struct perf_cpu_map *online_cpus =3D perf_cpu_map__new(NULL); struct cs_etm_recording *ptr =3D container_of(itr, struct cs_etm_recording, itr); diff --git a/tools/perf/arch/arm64/util/arm-spe.c b/tools/perf/arch/arm64/u= til/arm-spe.c index 5860bbaea95a..83ad05613321 100644 --- a/tools/perf/arch/arm64/util/arm-spe.c +++ b/tools/perf/arch/arm64/util/arm-spe.c @@ -144,7 +144,7 @@ static int arm_spe_recording_options(struct auxtrace_re= cord *itr, container_of(itr, struct arm_spe_recording, itr); struct perf_pmu *arm_spe_pmu =3D sper->arm_spe_pmu; struct evsel *evsel, *arm_spe_evsel =3D NULL; - struct perf_cpu_map *cpus =3D evlist->core.cpus; + struct perf_cpu_map *cpus =3D evlist->core.user_cpus; bool privileged =3D perf_event_paranoid_check(-1); struct evsel *tracking_evsel; int err; diff --git a/tools/perf/arch/x86/util/intel-bts.c b/tools/perf/arch/x86/uti= l/intel-bts.c index 4a76d49d25d6..c9d73ecfd795 100644 --- a/tools/perf/arch/x86/util/intel-bts.c +++ b/tools/perf/arch/x86/util/intel-bts.c @@ -110,7 +110,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 =3D btsr->intel_bts_pmu; struct evsel *evsel, *intel_bts_evsel =3D NULL; - const struct perf_cpu_map *cpus =3D evlist->core.cpus; + const struct perf_cpu_map *cpus =3D evlist->core.user_cpus; bool privileged =3D perf_event_paranoid_check(-1); =20 if (opts->auxtrace_sample_mode) { diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util= /intel-pt.c index 8c31578d6f4a..58bf24960273 100644 --- a/tools/perf/arch/x86/util/intel-pt.c +++ b/tools/perf/arch/x86/util/intel-pt.c @@ -382,7 +382,7 @@ static int intel_pt_info_fill(struct auxtrace_record *i= tr, ui__warning("Intel Processor Trace: TSC not available\n"); } =20 - per_cpu_mmaps =3D !perf_cpu_map__empty(session->evlist->core.cpus); + per_cpu_mmaps =3D !perf_cpu_map__empty(session->evlist->core.user_cpus); =20 auxtrace_info->type =3D PERF_AUXTRACE_INTEL_PT; auxtrace_info->priv[INTEL_PT_PMU_TYPE] =3D intel_pt_pmu->type; @@ -632,7 +632,7 @@ static int intel_pt_recording_options(struct auxtrace_r= ecord *itr, struct perf_pmu *intel_pt_pmu =3D ptr->intel_pt_pmu; bool have_timing_info, need_immediate =3D false; struct evsel *evsel, *intel_pt_evsel =3D NULL; - const struct perf_cpu_map *cpus =3D evlist->core.cpus; + const struct perf_cpu_map *cpus =3D evlist->core.user_cpus; bool privileged =3D perf_event_paranoid_check(-1); u64 tsc_bit; int err; diff --git a/tools/perf/bench/evlist-open-close.c b/tools/perf/bench/evlist= -open-close.c index de56601f69ee..5bdc6b476a4d 100644 --- a/tools/perf/bench/evlist-open-close.c +++ b/tools/perf/bench/evlist-open-close.c @@ -151,7 +151,7 @@ static int bench_evlist_open_close__run(char *evstr) =20 init_stats(&time_stats); =20 - printf(" Number of cpus:\t%d\n", perf_cpu_map__nr(evlist->core.cpus)); + printf(" Number of cpus:\t%d\n", perf_cpu_map__nr(evlist->core.user_cpus= )); printf(" Number of threads:\t%d\n", evlist->core.threads->nr); printf(" Number of events:\t%d (%d fds)\n", evlist->core.nr_entries, evlist__count_evsel_fds(evlist)); diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c index ad9ce1bfffa1..642cbc6fdfc5 100644 --- a/tools/perf/builtin-ftrace.c +++ b/tools/perf/builtin-ftrace.c @@ -301,7 +301,7 @@ static int set_tracing_cpumask(struct perf_cpu_map *cpu= map) =20 static int set_tracing_cpu(struct perf_ftrace *ftrace) { - struct perf_cpu_map *cpumap =3D ftrace->evlist->core.cpus; + struct perf_cpu_map *cpumap =3D ftrace->evlist->core.user_cpus; =20 if (!target__has_cpu(&ftrace->target)) return 0; diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 0b4abed555d8..28ab3866802c 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -987,7 +987,7 @@ static int record__thread_data_init_maps(struct record_= thread *thread_data, stru int m, tm, nr_mmaps =3D evlist->core.nr_mmaps; struct mmap *mmap =3D evlist->mmap; struct mmap *overwrite_mmap =3D evlist->overwrite_mmap; - struct perf_cpu_map *cpus =3D evlist->core.cpus; + struct perf_cpu_map *cpus =3D evlist->core.user_cpus; =20 thread_data->nr_mmaps =3D bitmap_weight(thread_data->mask->maps.bits, thread_data->mask->maps.nbits); @@ -1881,7 +1881,7 @@ static int record__synthesize(struct record *rec, boo= l tail) return err; } =20 - err =3D perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.cpus, + err =3D perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->core.user= _cpus, process_synthesized_event, NULL); if (err < 0) { pr_err("Couldn't synthesize cpu map.\n"); @@ -3675,7 +3675,7 @@ static int record__init_thread_default_masks(struct r= ecord *rec, struct perf_cpu static int record__init_thread_masks(struct record *rec) { int ret =3D 0; - struct perf_cpu_map *cpus =3D rec->evlist->core.cpus; + struct perf_cpu_map *cpus =3D rec->evlist->core.user_cpus; =20 if (!record__threads_enabled(rec)) return record__init_thread_default_masks(rec, cpus); diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 4ee40de698a4..5bee529f7656 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -804,7 +804,7 @@ static int __run_perf_stat(int argc, const char **argv,= int run_idx) if (group) evlist__set_leader(evsel_list); =20 - if (!cpu_map__is_dummy(evsel_list->core.cpus)) { + if (!cpu_map__is_dummy(evsel_list->core.user_cpus)) { if (affinity__setup(&saved_affinity) < 0) return -1; affinity =3D &saved_affinity; @@ -1458,7 +1458,7 @@ static int perf_stat_init_aggr_mode(void) aggr_cpu_id_get_t get_id =3D aggr_mode__get_aggr(stat_config.aggr_mode); =20 if (get_id) { - stat_config.aggr_map =3D cpu_aggr_map__new(evsel_list->core.cpus, + stat_config.aggr_map =3D cpu_aggr_map__new(evsel_list->core.user_cpus, get_id, /*data=3D*/NULL); if (!stat_config.aggr_map) { pr_err("cannot build %s map", aggr_mode__string[stat_config.aggr_mode]); @@ -1472,7 +1472,7 @@ static int perf_stat_init_aggr_mode(void) * taking the highest cpu number to be the size of * the aggregation translate cpumap. */ - nr =3D perf_cpu_map__max(evsel_list->core.cpus).cpu; + nr =3D perf_cpu_map__max(evsel_list->core.user_cpus).cpu; stat_config.cpus_aggr_map =3D cpu_aggr_map__empty_new(nr + 1); return stat_config.cpus_aggr_map ? 0 : -ENOMEM; } @@ -1627,7 +1627,7 @@ static int perf_stat_init_aggr_mode_file(struct perf_= stat *st) if (!get_id) return 0; =20 - stat_config.aggr_map =3D cpu_aggr_map__new(evsel_list->core.cpus, get_id,= env); + stat_config.aggr_map =3D cpu_aggr_map__new(evsel_list->core.user_cpus, ge= t_id, env); if (!stat_config.aggr_map) { pr_err("cannot build %s map", aggr_mode__string[stat_config.aggr_mode]); return -1; diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 9b08e44a31d9..4cfa112292d0 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1021,7 +1021,7 @@ static int perf_top__start_counters(struct perf_top *= top) =20 evlist__for_each_entry(evlist, counter) { try_again: - if (evsel__open(counter, top->evlist->core.cpus, + if (evsel__open(counter, top->evlist->core.user_cpus, top->evlist->core.threads) < 0) { =20 /* diff --git a/tools/perf/util/auxtrace.c b/tools/perf/util/auxtrace.c index 9e48652662d4..b138dd6bdefc 100644 --- a/tools/perf/util/auxtrace.c +++ b/tools/perf/util/auxtrace.c @@ -174,7 +174,7 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap= _params *mp, mp->idx =3D idx; =20 if (per_cpu) { - mp->cpu =3D perf_cpu_map__cpu(evlist->core.cpus, idx); + mp->cpu =3D perf_cpu_map__cpu(evlist->core.user_cpus, idx); if (evlist->core.threads) mp->tid =3D perf_thread_map__pid(evlist->core.threads, 0); else diff --git a/tools/perf/util/bpf_ftrace.c b/tools/perf/util/bpf_ftrace.c index 4f4d3aaff37c..69481b28b885 100644 --- a/tools/perf/util/bpf_ftrace.c +++ b/tools/perf/util/bpf_ftrace.c @@ -38,7 +38,7 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *= ftrace) =20 /* don't need to set cpu filter for system-wide mode */ if (ftrace->target.cpu_list) { - ncpus =3D perf_cpu_map__nr(ftrace->evlist->core.cpus); + ncpus =3D perf_cpu_map__nr(ftrace->evlist->core.user_cpus); bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus); } =20 @@ -63,7 +63,7 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *= ftrace) fd =3D bpf_map__fd(skel->maps.cpu_filter); =20 for (i =3D 0; i < ncpus; i++) { - cpu =3D perf_cpu_map__cpu(ftrace->evlist->core.cpus, i).cpu; + cpu =3D perf_cpu_map__cpu(ftrace->evlist->core.user_cpus, i).cpu; bpf_map_update_elem(fd, &cpu, &val, BPF_ANY); } } diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 9bb79e049957..d335fb713f5e 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -440,7 +440,7 @@ static void __evlist__disable(struct evlist *evlist, ch= ar *evsel_name) bool has_imm =3D false; =20 // See explanation in evlist__close() - if (!cpu_map__is_dummy(evlist->core.cpus)) { + if (!cpu_map__is_dummy(evlist->core.user_cpus)) { if (affinity__setup(&saved_affinity) < 0) return; affinity =3D &saved_affinity; @@ -500,7 +500,7 @@ static void __evlist__enable(struct evlist *evlist, cha= r *evsel_name) struct affinity saved_affinity, *affinity =3D NULL; =20 // See explanation in evlist__close() - if (!cpu_map__is_dummy(evlist->core.cpus)) { + if (!cpu_map__is_dummy(evlist->core.user_cpus)) { if (affinity__setup(&saved_affinity) < 0) return; affinity =3D &saved_affinity; @@ -565,7 +565,7 @@ static int evlist__enable_event_cpu(struct evlist *evli= st, struct evsel *evsel, static int evlist__enable_event_thread(struct evlist *evlist, struct evsel= *evsel, int thread) { int cpu; - int nr_cpus =3D perf_cpu_map__nr(evlist->core.cpus); + int nr_cpus =3D perf_cpu_map__nr(evlist->core.user_cpus); =20 if (!evsel->core.fd) return -EINVAL; @@ -580,7 +580,7 @@ static int evlist__enable_event_thread(struct evlist *e= vlist, struct evsel *evse =20 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, i= nt idx) { - bool per_cpu_mmaps =3D !perf_cpu_map__empty(evlist->core.cpus); + bool per_cpu_mmaps =3D !perf_cpu_map__empty(evlist->core.user_cpus); =20 if (per_cpu_mmaps) return evlist__enable_event_cpu(evlist, evsel, idx); @@ -1301,10 +1301,10 @@ void evlist__close(struct evlist *evlist) struct affinity affinity; =20 /* - * With perf record core.cpus is usually NULL. + * With perf record core.user_cpus is usually NULL. * Use the old method to handle this for now. */ - if (!evlist->core.cpus || cpu_map__is_dummy(evlist->core.cpus)) { + if (!evlist->core.user_cpus || cpu_map__is_dummy(evlist->core.user_cpus))= { evlist__for_each_entry_reverse(evlist, evsel) evsel__close(evsel); return; @@ -1367,7 +1367,7 @@ int evlist__open(struct evlist *evlist) * Default: one fd per CPU, all threads, aka systemwide * as sys_perf_event_open(cpu =3D -1, thread =3D -1) is EINVAL */ - if (evlist->core.threads =3D=3D NULL && evlist->core.cpus =3D=3D NULL) { + if (evlist->core.threads =3D=3D NULL && evlist->core.user_cpus =3D=3D NUL= L) { err =3D evlist__create_syswide_maps(evlist); if (err < 0) goto out_err; diff --git a/tools/perf/util/record.c b/tools/perf/util/record.c index 007a64681416..ff326eba084f 100644 --- a/tools/perf/util/record.c +++ b/tools/perf/util/record.c @@ -106,7 +106,7 @@ void evlist__config(struct evlist *evlist, struct recor= d_opts *opts, struct call if (opts->group) evlist__set_leader(evlist); =20 - if (perf_cpu_map__cpu(evlist->core.cpus, 0).cpu < 0) + if (perf_cpu_map__cpu(evlist->core.user_cpus, 0).cpu < 0) opts->no_inherit =3D true; =20 use_comm_exec =3D perf_can_comm_exec(); @@ -244,7 +244,7 @@ bool evlist__can_select_event(struct evlist *evlist, co= nst char *str) =20 evsel =3D evlist__last(temp_evlist); =20 - if (!evlist || perf_cpu_map__empty(evlist->core.cpus)) { + if (!evlist || perf_cpu_map__empty(evlist->core.user_cpus)) { struct perf_cpu_map *cpus =3D perf_cpu_map__new(NULL); =20 if (cpus) @@ -252,7 +252,7 @@ bool evlist__can_select_event(struct evlist *evlist, co= nst char *str) =20 perf_cpu_map__put(cpus); } else { - cpu =3D perf_cpu_map__cpu(evlist->core.cpus, 0); + cpu =3D perf_cpu_map__cpu(evlist->core.user_cpus, 0); } =20 while (1) { diff --git a/tools/perf/util/sideband_evlist.c b/tools/perf/util/sideband_e= vlist.c index 748371ac22be..9f58c68a25f7 100644 --- a/tools/perf/util/sideband_evlist.c +++ b/tools/perf/util/sideband_evlist.c @@ -114,7 +114,7 @@ int evlist__start_sb_thread(struct evlist *evlist, stru= ct target *target) } =20 evlist__for_each_entry(evlist, counter) { - if (evsel__open(counter, evlist->core.cpus, evlist->core.threads) < 0) + if (evsel__open(counter, evlist->core.user_cpus, evlist->core.threads) <= 0) goto out_delete_evlist; } =20 diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index 9cbe351b141f..634dd9ea2b35 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -929,7 +929,7 @@ static void print_no_aggr_metric(struct perf_stat_confi= g *config, int all_idx; struct perf_cpu cpu; =20 - perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.cpus) { + perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_cpus) { struct evsel *counter; bool first =3D true; =20 diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index b654de0841f8..591afc6c607b 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -2127,7 +2127,7 @@ int perf_event__synthesize_stat_events(struct perf_st= at_config *config, struct p return err; } =20 - err =3D perf_event__synthesize_cpu_map(tool, evlist->core.cpus, process, = NULL); + err =3D perf_event__synthesize_cpu_map(tool, evlist->core.user_cpus, proc= ess, NULL); if (err < 0) { pr_err("Couldn't synthesize thread map.\n"); return err; diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c index c1ebfc5d2e0c..e98422f3ff17 100644 --- a/tools/perf/util/top.c +++ b/tools/perf/util/top.c @@ -95,15 +95,16 @@ size_t perf_top__header_snprintf(struct perf_top *top, = char *bf, size_t size) =20 if (target->cpu_list) ret +=3D SNPRINTF(bf + ret, size - ret, ", CPU%s: %s)", - perf_cpu_map__nr(top->evlist->core.cpus) > 1 ? "s" : "", + perf_cpu_map__nr(top->evlist->core.user_cpus) > 1 ? "s" : "", target->cpu_list); else { if (target->tid) ret +=3D SNPRINTF(bf + ret, size - ret, ")"); else ret +=3D SNPRINTF(bf + ret, size - ret, ", %d CPU%s)", - perf_cpu_map__nr(top->evlist->core.cpus), - perf_cpu_map__nr(top->evlist->core.cpus) > 1 ? "s" : ""); + perf_cpu_map__nr(top->evlist->core.user_cpus), + perf_cpu_map__nr(top->evlist->core.user_cpus) > 1 + ? "s" : ""); } =20 perf_top__reset_sample_counters(top); --=20 2.35.1.1021.g381101b075-goog From nobody Tue Jun 23 13:16:26 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8593BC433F5 for ; Mon, 28 Mar 2022 06:24:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234946AbiC1G01 (ORCPT ); Mon, 28 Mar 2022 02:26:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238409AbiC1G0G (ORCPT ); Mon, 28 Mar 2022 02:26:06 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CBD34ECC7 for ; Sun, 27 Mar 2022 23:24:26 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-2e6d04210b3so80571667b3.10 for ; Sun, 27 Mar 2022 23:24:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=YZdCpDo2MwsKjb0WbdxKDFQz4ii0U4OJCI/h5Uxfyc8=; b=hVa/7ihqPsZym2uTsY9dt+yok+r2vNttS+kECPUz8hlfd3jStwkXswPdBeCrSKu+8r QeU5/PriYcQc4cgKtYeuqB+cy1x97REfIUo0tC2tFZO4gXeIS/xcS05fs5cuywbioJnn dptX2c3gxaqTVY27IxEJg86seWOY2oqTzL9/HTiP5pi8uDw/SFsbF0HjegJ7qMvBkPpR Tb3CYk2hqKAcsnVo+uJYoHlxxOo6v1WKZhiKvPpnMIB8kTTUbg3sdPpLNwbIU7oZ6r3k wnckyfAkBl3jtmyGjoDKqwQt5IoJGvKr37Zhaq02trVr8xMkQL0hE58dHckjoxQCLiWN W71w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=YZdCpDo2MwsKjb0WbdxKDFQz4ii0U4OJCI/h5Uxfyc8=; b=A0W8ZazCkFCFimmw5/9CZXn78S2uTOxE/3iOQoWdcl0sBhZ80knvjASKfp99KmXkjX CaRaa7f+1t/9y8I6Hd57wyOBeXv/X6//0h6AFA3PwJCXWLzNi5B7aBb15UPtGxsNDCTH a7w2ZylmXK5R8noC94tLfiXIzjMzkcGYL6Tmn6qXuO+CCGv/+GJw6c3oP8PLBZSTSt5T d/R+fUbgPcibc6l1DYWENVQLwVN46Pd/PH4lGRHdbpMivWzMyfywi8cGah0XKqYcJMbb HYAJ5+Z9+Icnm6gOR0oGV+Fr4HH3sSoqpmjbRwkjbWt/VNVOgJSvLoGCJ3n1cog5yK99 Nr4A== X-Gm-Message-State: AOAM530gi0BQ/IdS8rv2IEuc1DTVhShmcrvEMfchs1z8I/jlSj67Kica 0XLiDcQq61FnnUIp4GrzbXjL8TMtiiZ6 X-Google-Smtp-Source: ABdhPJzQSAN+iwF+0ELfjnRELyBsh17NWxruCk/wZoBkE5XSh7FJRsS5lOFGmt2jB/qkes1g2fsc2qElJ/xK X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ef08:ed1b:261f:77fa]) (user=irogers job=sendgmr) by 2002:a05:6902:150b:b0:639:f81:8179 with SMTP id q11-20020a056902150b00b006390f818179mr17151751ybu.31.1648448665698; Sun, 27 Mar 2022 23:24:25 -0700 (PDT) Date: Sun, 27 Mar 2022 23:24:11 -0700 In-Reply-To: <20220328062414.1893550-1-irogers@google.com> Message-Id: <20220328062414.1893550-3-irogers@google.com> Mime-Version: 1.0 References: <20220328062414.1893550-1-irogers@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH 2/5] perf cpumap: More cpu map reuse by merge. From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Kajol Jain , James Clark , German Gomez , Adrian Hunter , Riccardo Mancini , Andi Kleen , Alexey Bayduraev , Alexander Antonov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf_cpu_map__merge will reuse one of its arguments if they are equal or the other argument is NULL. The arguments could be reused if it is known one set of values is a subset of the other. For example, a map of 0-1 and a map of just 0 when merged yields the map of 0-1. Currently a new map is created rather than adding a reference count to the original 0-1 map. Signed-off-by: Ian Rogers --- tools/lib/perf/cpumap.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c index ee66760f1e63..953bc50b0e41 100644 --- a/tools/lib/perf/cpumap.c +++ b/tools/lib/perf/cpumap.c @@ -319,6 +319,29 @@ struct perf_cpu perf_cpu_map__max(struct perf_cpu_map = *map) return map->nr > 0 ? map->map[map->nr - 1] : result; } =20 +/** Is 'b' a subset of 'a'. */ +static bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, + const struct perf_cpu_map *b) +{ + int i, j; + + if (a =3D=3D b || !b) + return true; + if (!a || b->nr > a->nr) + return false; + j =3D 0; + for (i =3D 0; i < a->nr; i++) { + if (a->map[i].cpu > b->map[j].cpu) + return false; + if (a->map[i].cpu =3D=3D b->map[j].cpu) { + j++; + if (j =3D=3D b->nr) + return true; + } + } + return false; +} + /* * Merge two cpumaps * @@ -335,17 +358,12 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_= cpu_map *orig, int i, j, k; struct perf_cpu_map *merged; =20 - if (!orig && !other) - return NULL; - if (!orig) { - perf_cpu_map__get(other); - return other; - } - if (!other) - return orig; - if (orig->nr =3D=3D other->nr && - !memcmp(orig->map, other->map, orig->nr * sizeof(struct perf_cpu))) + if (perf_cpu_map__is_subset(orig, other)) return orig; + if (perf_cpu_map__is_subset(other, orig)) { + perf_cpu_map__put(orig); + return perf_cpu_map__get(other); + } =20 tmp_len =3D orig->nr + other->nr; tmp_cpus =3D malloc(tmp_len * sizeof(struct perf_cpu)); --=20 2.35.1.1021.g381101b075-goog From nobody Tue Jun 23 13:16:26 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94846C433EF for ; Mon, 28 Mar 2022 06:24:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238466AbiC1G0c (ORCPT ); Mon, 28 Mar 2022 02:26:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238428AbiC1G0U (ORCPT ); Mon, 28 Mar 2022 02:26:20 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B083351E73 for ; Sun, 27 Mar 2022 23:24:28 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2e68c93bb30so110963757b3.18 for ; Sun, 27 Mar 2022 23:24:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=YAhIdIX1NbY8GJ/Fxc7tPQp5MnsWrJ6b1ZKA2cSf7J0=; b=BmBfuq5dDJ8FyY++eKZzoNfnofz30yeVLbgg6C06RDaHI+2/on7h2TcF3d5wM4fa4v 4bk4KMagZlk4nj19qtiaEbo1M4KSqHCCoSG28PmBhOeZ55PNWY+H93zGymXKK5231X2m KZtd7Vd97HDsSx/TyGhWx0qCCkgggvpS4Beh5fSGE8tfMMFU8D1fm5/iLShTGjpys2a+ Ro5/rm+LPdNcLq+0L3HrEDbV5NW1nCqM4h+PDD0jVJ0KBaAHhCIuNar/9RgYSjRjtnLL aT1mG0BBsu3ZXQTCANlrv09w07cbBxpDrXfARHxCKPef7Jb7jNIUufL44QKXW6flpj3O tNNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=YAhIdIX1NbY8GJ/Fxc7tPQp5MnsWrJ6b1ZKA2cSf7J0=; b=YdaYAvGJp1+l5fMMA5ZEN2c4K8FiSr+mWNWW9gjLbUvv+ukVap+rUkbo6WwxnsASlE cKfc+ArbK1eJ0lwIy7d/qA58jgi97vpamDsYD+OhsQMkUyvhxxto8DxkNnByImyKyZPH b8OPBi9RRNGgji2IryuVCT4qFs8rgpurboq3C7gImgB94yEVUANsRpv4GmQiekNUASwW N6NNZU0KiKsBYotPQ3hj8DgRhoYtREn4wrgToM3TAQGgS1AmhnnWOKLiLMXUz5//lI7A sgbJ3k57l8TVwGQRMLvgpbAAb9UImslzyuQj6KA1f+bXOUy5Kq3cKkXhNWPjFLB6XfYi p5jA== X-Gm-Message-State: AOAM532REJxWwhUUzd4PXLOEd0vZ5aq+bqMbiIx4cdiQ0zrcBjECqDFx kIIa9dJK1wJckJst7oXZy6yHlvnDhac8 X-Google-Smtp-Source: ABdhPJzgJ3tnHPRL/ZoEIaIEfGlgiRAfkH5m8p1TAIooj8UqZqfQygDqtd6JOglW1lVfIhZfwaS+hnJN01ZZ X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ef08:ed1b:261f:77fa]) (user=irogers job=sendgmr) by 2002:a05:6902:124e:b0:634:619e:4114 with SMTP id t14-20020a056902124e00b00634619e4114mr21938385ybu.181.1648448667764; Sun, 27 Mar 2022 23:24:27 -0700 (PDT) Date: Sun, 27 Mar 2022 23:24:12 -0700 In-Reply-To: <20220328062414.1893550-1-irogers@google.com> Message-Id: <20220328062414.1893550-4-irogers@google.com> Mime-Version: 1.0 References: <20220328062414.1893550-1-irogers@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH 3/5] perf cpumap: Add intersect function. From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Kajol Jain , James Clark , German Gomez , Adrian Hunter , Riccardo Mancini , Andi Kleen , Alexey Bayduraev , Alexander Antonov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The merge function gives the union of two cpu maps. Add an intersect function which will be used in the next change. Signed-off-by: Ian Rogers --- tools/lib/perf/cpumap.c | 38 ++++++++++++++++++++++++++++ tools/lib/perf/include/perf/cpumap.h | 2 ++ 2 files changed, 40 insertions(+) diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c index 953bc50b0e41..56b4d213039f 100644 --- a/tools/lib/perf/cpumap.c +++ b/tools/lib/perf/cpumap.c @@ -393,3 +393,41 @@ struct perf_cpu_map *perf_cpu_map__merge(struct perf_c= pu_map *orig, perf_cpu_map__put(orig); return merged; } + +struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig, + struct perf_cpu_map *other) +{ + struct perf_cpu *tmp_cpus; + int tmp_len; + int i, j, k; + struct perf_cpu_map *merged =3D NULL; + + if (perf_cpu_map__is_subset(other, orig)) + return orig; + if (perf_cpu_map__is_subset(orig, other)) { + perf_cpu_map__put(orig); + return perf_cpu_map__get(other); + } + + tmp_len =3D max(orig->nr, other->nr); + tmp_cpus =3D malloc(tmp_len * sizeof(struct perf_cpu)); + if (!tmp_cpus) + return NULL; + + i =3D j =3D k =3D 0; + while (i < orig->nr && j < other->nr) { + if (orig->map[i].cpu < other->map[j].cpu) + i++; + else if (orig->map[i].cpu > other->map[j].cpu) + j++; + else { + j++; + tmp_cpus[k++] =3D orig->map[i++]; + } + } + if (k) + merged =3D cpu_map__trim_new(k, tmp_cpus); + free(tmp_cpus); + perf_cpu_map__put(orig); + return merged; +} diff --git a/tools/lib/perf/include/perf/cpumap.h b/tools/lib/perf/include/= perf/cpumap.h index 4a2edbdb5e2b..a2a7216c0b78 100644 --- a/tools/lib/perf/include/perf/cpumap.h +++ b/tools/lib/perf/include/perf/cpumap.h @@ -19,6 +19,8 @@ LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE = *file); LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *ma= p); LIBPERF_API struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *= orig, struct perf_cpu_map *other); +LIBPERF_API struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_m= ap *orig, + struct perf_cpu_map *other); LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map); LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *c= pus, int idx); LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus); --=20 2.35.1.1021.g381101b075-goog From nobody Tue Jun 23 13:16:26 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29F50C4332F for ; Mon, 28 Mar 2022 06:25:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238488AbiC1G0q (ORCPT ); Mon, 28 Mar 2022 02:26:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238433AbiC1G0U (ORCPT ); Mon, 28 Mar 2022 02:26:20 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CC1D522C2 for ; Sun, 27 Mar 2022 23:24:30 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id b11-20020a5b008b000000b00624ea481d55so10185643ybp.19 for ; Sun, 27 Mar 2022 23:24:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=4TQ73I0e25XkGF6JYmSar5D4w+BV0D8V/TiHYSAl5KU=; b=rAbswlnujk8SJmxLde3al3arUpI/R7w91Rzm7Im3FFbuzIfrwxc+ukwRg2ubEFbel6 u2xn9Xt+8UqLmVxVZDCy5Nby8rLxqJEd/CoGCk9U++oFpUs7RKCCFqA/YaPX4/GZKU5U w4zDDyta0qWgGM0toE5CfiZ51VV7n22L7WUbX6ZP1pmQMpV/LmnyXyL6MsHIdCuXlyuX z+SQAkei8tjNWFHynWVB1nY+gTAKs8lordMKynfqoAtLVgafdcegbc5TSy2occ4xSrIB kBaLqdF9P69Scb/0cyCCr6sUxseIuHLDaguZgdN1KxkbJ9A16IZ+UtqU0aR+e7aNrEl8 vWTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=4TQ73I0e25XkGF6JYmSar5D4w+BV0D8V/TiHYSAl5KU=; b=e2dCR0WWvz3kjcI2lQJJ0JuMoKhSjcybi88gGpsfHlaQBv7K9wcvJQ7z2TJjEOY+DV JnXMrmUy/T0Yd5GA/G48ATRyGe2u9RlNdU8UX5dXK1sOYkn0FeYoZq9GxvzMFUrcxo+3 6WsYrjYPBnnfO7oSZR7pSYk8jb6Z3Zi1CK0DnUgn6wAf4zGORZj5knoCUFlicg95N+Xa REf0Zb7BgENu1Af5o83Il7e147ZIjRI4y7XjCOQtnMeYPQP/STPLYFQxWBdA3LXOvFfH 1Jf8Z8wSAChlFW+xu6lMQeKvoOwdG7ImGpHhia9ALSfsh6suwPV+ElXTse1bMfUjQwsv PSdw== X-Gm-Message-State: AOAM531z3DXB1iRM5jYSA3boXhM+FdrIWmlomtvTEAcqNuER3KziCXbe NR+Ic1Bpb3Ig7HOEpyIdHjKu+TF8Z3Ys X-Google-Smtp-Source: ABdhPJzzFACczXPpjcFkdPBG8L2RXqeBU42ZTsEkDl97YgxNOBZWCpXE1hi/ATJnf1wHmPGu6AqFFy8JjyPU X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ef08:ed1b:261f:77fa]) (user=irogers job=sendgmr) by 2002:a81:3a52:0:b0:2d7:549a:50fc with SMTP id h79-20020a813a52000000b002d7549a50fcmr24269495ywa.85.1648448669800; Sun, 27 Mar 2022 23:24:29 -0700 (PDT) Date: Sun, 27 Mar 2022 23:24:13 -0700 In-Reply-To: <20220328062414.1893550-1-irogers@google.com> Message-Id: <20220328062414.1893550-5-irogers@google.com> Mime-Version: 1.0 References: <20220328062414.1893550-1-irogers@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH 4/5] perf stat: Avoid segv if core.user_cpus isn't set. From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Kajol Jain , James Clark , German Gomez , Adrian Hunter , Riccardo Mancini , Andi Kleen , Alexey Bayduraev , Alexander Antonov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Passing null to perf_cpu_map__max doesn't make sense as there is no valid max. Avoid this problem by null checking in perf_stat_init_aggr_mode. Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 5bee529f7656..ecd5cf4fd872 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -1472,7 +1472,10 @@ static int perf_stat_init_aggr_mode(void) * taking the highest cpu number to be the size of * the aggregation translate cpumap. */ - nr =3D perf_cpu_map__max(evsel_list->core.user_cpus).cpu; + if (evsel_list->core.user_cpus) + nr =3D perf_cpu_map__max(evsel_list->core.user_cpus).cpu; + else + nr =3D 0; stat_config.cpus_aggr_map =3D cpu_aggr_map__empty_new(nr + 1); return stat_config.cpus_aggr_map ? 0 : -ENOMEM; } --=20 2.35.1.1021.g381101b075-goog From nobody Tue Jun 23 13:16:26 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E12B8C433F5 for ; Mon, 28 Mar 2022 06:25:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238434AbiC1G0y (ORCPT ); Mon, 28 Mar 2022 02:26:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238452AbiC1G0V (ORCPT ); Mon, 28 Mar 2022 02:26:21 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B005522D3 for ; Sun, 27 Mar 2022 23:24:33 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-2e642be1a51so109966827b3.21 for ; Sun, 27 Mar 2022 23:24:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=9DdBX2SnvA0xmL5taueohrnj4hozmwadyqsEuZMg/sA=; b=JExcflpahLFCp46tCO+bVKhfqtpGsdrFCJs+A4n7LEj0S0oyM28aQpiH9N2I4o3EFq qv6yOpmEB4FHmzEJDFtrlj5POsxwk7ymhkoAZGKLNsnrUMmG7PhVKT+rNPkl7WrEnsRG r+DGYQwjH6ShFQ0BJbAPo3ulaNKd7bAJJW2F8mb0pcjRDO3zlwYg6zLaYLbBc17pLs+e 1PgB6LDqxrfexhWFD9UWMtQIlMqoh3dCxy7XUyejsjPNhj1KacYCvXPRwTBhF+BOCrYj K0AonuWZtGaodLfoc3NG1ln7v73zS22G4J7e++zg1bmud/pqhRfzzj1pMmNSlD2pY85i UZsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=9DdBX2SnvA0xmL5taueohrnj4hozmwadyqsEuZMg/sA=; b=qg6viFJx8BTLrAmFgbiztWGj12Z0jnvaX6dXv9jIsYO8oZv2a6yYhe8LuWZ128xnVf oUm4qVH9QJaXHEa0PjNF4kFJA1hDeMEP4hOjXAzLbrppKtv5I51p5ya2PU4I0SIrdRtp 4pwiAQOUg+dVPgT1GZqWANDFUUoLA50tJIxqnA+3OhGyU7fLOf4OrabQmlCU6fz6W4YI Sk+ejecZao3zQ7+MqEMrw0o3njBd95zrfIi5Lj1a2Bi+r+7rKpB6FkIq6A9vsF2+qf5L jSBi9Nf2noSrZ/kVwkjexKOLnjBlJjzgBmBe129NtYqTBkpwKcAe8No2OfPIQljGQrLz cUEA== X-Gm-Message-State: AOAM531sylaUIQnQeTWcUUD+xO+uaxkXW08Y3eXowUWpVyBQZthYa5j+ km3oP1d1ME+44uR3H0/dK6sNOMWNzqvT X-Google-Smtp-Source: ABdhPJxPf/g6psmSnbUpwgPJRs5+zV2W469Tco/QieQGsDIBSkUvRLgEjG1Y+RiHh3KzGowWDsrlmJTLCwDJ X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ef08:ed1b:261f:77fa]) (user=irogers job=sendgmr) by 2002:a25:ec08:0:b0:633:9106:b10b with SMTP id j8-20020a25ec08000000b006339106b10bmr22130772ybh.62.1648448672235; Sun, 27 Mar 2022 23:24:32 -0700 (PDT) Date: Sun, 27 Mar 2022 23:24:14 -0700 In-Reply-To: <20220328062414.1893550-1-irogers@google.com> Message-Id: <20220328062414.1893550-6-irogers@google.com> Mime-Version: 1.0 References: <20220328062414.1893550-1-irogers@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH 5/5] perf evlist: Respect all_cpus when setting user_cpus From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Kajol Jain , James Clark , German Gomez , Adrian Hunter , Riccardo Mancini , Andi Kleen , Alexey Bayduraev , Alexander Antonov , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If all_cpus is calculated it represents the merge/union of all evsel cpu maps. By default user_cpus is computed to be the online CPUs. For uncore events, it is often the case currently that all_cpus is a subset of user_cpus. Metrics printed without aggregation and with metric-only, in print_no_aggr_metric, iterate over user_cpus assuming every CPU has a metric to print. For each CPU the prefix is printed, but then if the evsel's cpus doesn't contain anything you get an empty line like the following on a SkylakeX: ``` $ perf stat -A -M DRAM_BW_Use -a --metric-only -I 1000 1.000453137 CPU0 0.00 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 CPU18 0.00 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 1.000453137 2.003717143 CPU0 0.00 ... ``` While it is possible to be lazier in printing the prefix and trailing newline, having user_cpus not be a subset of all_cpus is preferential so that wasted work isn't done elsewhere user_cpus is used. The change modifies user_cpus to be the intersection of user specified CPUs, or default all online CPUs, with the CPUs computed through the merge of all evsel cpu maps. New behavior: ``` $ perf stat -A -M DRAM_BW_Use -a --metric-only -I 1000 1.001086325 CPU0 0.00 1.001086325 CPU18 0.00 2.003671291 CPU0 0.00 2.003671291 CPU18 0.00 ... ``` Signed-off-by: Ian Rogers --- tools/perf/util/evlist.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index d335fb713f5e..91bbb66b7e9a 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -1036,6 +1036,8 @@ int evlist__create_maps(struct evlist *evlist, struct= target *target) if (!cpus) goto out_delete_threads; =20 + if (evlist->core.all_cpus) + cpus =3D perf_cpu_map__intersect(cpus, evlist->core.all_cpus); evlist->core.has_user_cpus =3D !!target->cpu_list && !target->hybrid; =20 perf_evlist__set_maps(&evlist->core, cpus, threads); --=20 2.35.1.1021.g381101b075-goog