From nobody Fri Nov 29 16:29:10 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBE1C1CB53E for ; Wed, 18 Sep 2024 22:01:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726696919; cv=none; b=CVwIRC16wl/PrcZX5bx7bwO4L8oGh5tslEcLcm51aEWPV6rXtuOtMOLUpK4e4FTGq7k73o3k74xAqJ4Ah/IhsDdcl1JkiRvNHB3wX75tMtL1bSTNRyyTB43eeGSBs5c3PYBObAMN//R+sFFXYar9kXczF6JSdIpjaWoUW4tJHtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726696919; c=relaxed/simple; bh=0HmX3+oDarBi9YsEUyCZhTasjdJ8E7Vq2wgPDApLjP8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=qmUaW6Xj+TmGWfQ7q7CZsSeUdrtliwEMQd2KS/I8kadhujIZu5h3SM6LLnrUqeXYVZS0uuJbSb49XOIY9HHY8hymCzFqnUppBypVjZkU+XE38f4JYQuTHAdw9bQoyImFfHDb7skvJifIftYBiAwqg/jLDbqww84Si0SuuuHTt0U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mk7ahGZZ; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mk7ahGZZ" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6d9e31e66eeso3801537b3.1 for ; Wed, 18 Sep 2024 15:01:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726696916; x=1727301716; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=NcM+VU+CnvqLlwnAxi53/iARGP0HqFfg/cKQ7ZCX6g4=; b=mk7ahGZZUqh2tK4Ie+OcZx47JFujrBSP+fnUI0Ks/0wm3D05K0ckcVBCbYgHcEkf2B 9KxxM4ds9kFUGcjzB+9AeXcoJxZJeGPF900R/l00UnM1Hjlgnl9vFVi1OR6+WTke3FGv 91Sznm/zyj/VtZ+oOMzds/DdStgSumpnOgTwfq2LEMCfJaaUENRkb3l54ulLZqoH3f/3 Gsi50m3A9ctar0bp6M4uS7rs7u60SalEYb13+Ov3k96/bLo1mzvahqD/2avXUru+df+e MM0R3Due0HLr/JAaBMCVFAH+hTvs8b2BYZcfG9bI7uoTesdaQ98H7Ri8VSoxxqxR2XMw sU1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726696916; x=1727301716; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NcM+VU+CnvqLlwnAxi53/iARGP0HqFfg/cKQ7ZCX6g4=; b=fE5OIKW9qTz54AI2wSkmDXJEjD2j9YyQ9GfaWpm0xC/gMwBR/hnNIRl3Wy3PYxayNx LiadnXX1tuz6f8h01sNx23CGQW6tUybLw27JnllehoTb+YcZg6Miv6SNAjyIIQv3sXag 8u5j12RNqgR43lFRYENL0v43mTmgO3sFkhT8l+/73HFEZpLy3jVzNZ/HdOQ9aPc/GUbu BgIxRrEnix+UYqY6z/+yHxcUmKMVVG6S7WD5MgxTPMEoI9uEi3F/5BjzkpmFgZ236UPY WiUXC4VfckLgomcc/JATIJtgWcvu6L8FMfgApJ9UpECcESmtzmNdZcq/jTXke+fLWS+n /8sg== X-Forwarded-Encrypted: i=1; AJvYcCXMXyv/z24y8QH5Q4XNZqZjzPvHs+2THgl6jri20QVTgVZ95X65aZt8/fR1oYvDXWLfyHsJDV+IdEmDpEM=@vger.kernel.org X-Gm-Message-State: AOJu0YzHIC6NE7T/ltNF2aPiHPGpzYw4Bn0omuh8svlIpdgj6x8hFgS3 4oiogoIPxfZZDlgRIR5Ai5/J6jpauF0Ff2NFvdxg44ImxKXCnxggbZc2Pc2EWi/7aTBadFp+aqL GQwSIfA== X-Google-Smtp-Source: AGHT+IHmgM+Bh5rGiZ5/+XTrkIvNyuS76NKjY1Cy+4xkAJ6JRB0yVCQnlNFZ0Erd2sOSpjTf0arogTVX/3Gh X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:cb6b:1e62:cfd8:bd50]) (user=irogers job=sendgmr) by 2002:a25:ab8e:0:b0:e11:5b8c:f9c with SMTP id 3f1490d57ef6-e1dafd96ef0mr48042276.0.1726696915876; Wed, 18 Sep 2024 15:01:55 -0700 (PDT) Date: Thu, 19 Sep 2024 00:01:33 +0200 In-Reply-To: <20240918220133.102964-1-irogers@google.com> Message-Id: <20240918220133.102964-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240918220133.102964-1-irogers@google.com> X-Mailer: git-send-email 2.46.0.662.g92d0881bb0-goog Subject: [PATCH v3 3/3] perf parse-events: Add "cpu" term to set the CPU an event is recorded on From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Ravi Bangoria , Weilin Wang , Dominique Martinet , Jing Zhang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The -C option allows the CPUs for a list of events to be specified but its not possible to set the CPU for a single event. Add a term to allow this. The term isn't a general CPU list due to ',' already being a special character in event parsing instead multiple cpu=3D terms may be provided and they will be merged/unioned together. An example of mixing different types of events counted on different CPUs: ``` $ perf stat -A -C 0,4-5,8 -e "instructions/cpu=3D0/,l1d-misses/cpu=3D4,cpu= =3D5/,inst_retired.any/cpu=3D8/,cycles" -a sleep 0.1 Performance counter stats for 'system wide': CPU0 368,647 instructions/cpu=3D0/ # 0.26= insn per cycle CPU4 instructions/cpu=3D0/ CPU5 instructions/cpu=3D0/ CPU8 instructions/cpu=3D0/ CPU0 l1d-misses [cpu] CPU4 203,377 l1d-misses [cpu] CPU5 138,231 l1d-misses [cpu] CPU8 l1d-misses [cpu] CPU0 cpu/cpu=3D8/ CPU4 cpu/cpu=3D8/ CPU5 cpu/cpu=3D8/ CPU8 943,861 cpu/cpu=3D8/ CPU0 1,412,071 cycles CPU4 20,362,900 cycles CPU5 10,172,725 cycles CPU8 2,406,081 cycles 0.102925309 seconds time elapsed ``` Note, the event name of inst_retired.any is missing, reported as cpu/cpu=3D8/, as there are unmerged uniquify fixes: https://lore.kernel.org/lkml/20240510053705.2462258-3-irogers@google.com/ Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-list.txt | 9 ++++ tools/perf/util/evsel_config.h | 1 + tools/perf/util/parse-events.c | 71 ++++++++++++++++++++++---- tools/perf/util/parse-events.h | 3 +- tools/perf/util/parse-events.l | 1 + tools/perf/util/pmu.c | 1 + 6 files changed, 74 insertions(+), 12 deletions(-) diff --git a/tools/perf/Documentation/perf-list.txt b/tools/perf/Documentat= ion/perf-list.txt index dea005410ec0..e0cd9bb8283e 100644 --- a/tools/perf/Documentation/perf-list.txt +++ b/tools/perf/Documentation/perf-list.txt @@ -274,6 +274,15 @@ Sums up the event counts for all hardware threads in a= core, e.g.: =20 perf stat -e cpu/event=3D0,umask=3D0x3,percore=3D1/ =20 +cpu: + +Specifies the CPU to open the event upon. The value may be repeated to +specify opening the event on multiple CPUs: + + + perf stat -e instructions/cpu=3D0,cpu=3D2/,cycles/cpu=3D1,cpu=3D2/ -a sl= eep 1 + perf stat -e data_read/cpu=3D0/,data_write/cpu=3D1/ -a sleep 1 + =20 EVENT GROUPS ------------ diff --git a/tools/perf/util/evsel_config.h b/tools/perf/util/evsel_config.h index aee6f808b512..9630c4a24721 100644 --- a/tools/perf/util/evsel_config.h +++ b/tools/perf/util/evsel_config.h @@ -47,6 +47,7 @@ struct evsel_config_term { u32 aux_sample_size; u64 cfg_chg; char *str; + int cpu; } val; bool weak; }; diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 017d31d51ea4..63c31cfdd79b 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -7,6 +7,7 @@ #include #include #include +#include "cpumap.h" #include "term.h" #include "env.h" #include "evlist.h" @@ -178,6 +179,26 @@ static char *get_config_name(const struct parse_events= _terms *head_terms) return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME); } =20 +static struct perf_cpu_map *get_config_cpu(const struct parse_events_terms= *head_terms) +{ + struct parse_events_term *term; + struct perf_cpu_map *cpus =3D NULL; + + if (!head_terms) + return NULL; + + list_for_each_entry(term, &head_terms->terms, list) { + if (term->type_term =3D=3D PARSE_EVENTS__TERM_TYPE_CPU) { + struct perf_cpu_map *cpu =3D perf_cpu_map__new_int(term->val.num); + + cpus =3D perf_cpu_map__merge(cpus, cpu); + perf_cpu_map__put(cpu); + } + } + + return cpus; +} + /** * fix_raw - For each raw term see if there is an event (aka alias) in pmu= that * matches the raw's string value. If the string value matches an @@ -469,11 +490,12 @@ int parse_events_add_cache(struct list_head *list, in= t *idx, const char *name, bool found_supported =3D false; const char *config_name =3D get_config_name(parsed_terms); const char *metric_id =3D get_config_metric_id(parsed_terms); + struct perf_cpu_map *cpus =3D get_config_cpu(parsed_terms); + int ret =3D 0; =20 while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { LIST_HEAD(config_terms); struct perf_event_attr attr; - int ret; =20 if (parse_events__filter_pmu(parse_state, pmu)) continue; @@ -487,7 +509,7 @@ int parse_events_add_cache(struct list_head *list, int = *idx, const char *name, parsed_terms, perf_pmu__auto_merge_stats(pmu)); if (ret) - return ret; + goto out_err; continue; } =20 @@ -507,20 +529,27 @@ int parse_events_add_cache(struct list_head *list, in= t *idx, const char *name, =20 if (parsed_terms) { if (config_attr(&attr, parsed_terms, parse_state->error, - config_term_common)) - return -EINVAL; - - if (get_config_terms(parsed_terms, &config_terms)) - return -ENOMEM; + config_term_common)) { + ret =3D -EINVAL; + goto out_err; + } + if (get_config_terms(parsed_terms, &config_terms)) { + ret =3D -ENOMEM; + goto out_err; + } } =20 if (__add_event(list, idx, &attr, /*init_attr*/true, config_name ?: name, metric_id, pmu, &config_terms, /*auto_merge_stats=3D*/false, - /*cpu_list=3D*/NULL) =3D=3D NULL) - return -ENOMEM; + cpus) =3D=3D NULL) + ret =3D -ENOMEM; =20 free_config_terms(&config_terms); + if (ret) + goto out_err; } +out_err: + perf_cpu_map__put(cpus); return found_supported ? 0 : -EINVAL; } =20 @@ -835,6 +864,7 @@ static const char *config_term_name(enum parse_events__= term_type term_type) [PARSE_EVENTS__TERM_TYPE_RAW] =3D "raw", [PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE] =3D "legacy-cache", [PARSE_EVENTS__TERM_TYPE_HARDWARE] =3D "hardware", + [PARSE_EVENTS__TERM_TYPE_CPU] =3D "cpu", }; if ((unsigned int)term_type >=3D __PARSE_EVENTS__TERM_TYPE_NR) return "unknown term"; @@ -864,6 +894,7 @@ config_term_avail(enum parse_events__term_type term_typ= e, struct parse_events_er case PARSE_EVENTS__TERM_TYPE_METRIC_ID: case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: case PARSE_EVENTS__TERM_TYPE_PERCORE: + case PARSE_EVENTS__TERM_TYPE_CPU: return true; case PARSE_EVENTS__TERM_TYPE_USER: case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: @@ -1007,6 +1038,15 @@ do { \ return -EINVAL; } break; + case PARSE_EVENTS__TERM_TYPE_CPU: + CHECK_TYPE_VAL(NUM); + if (term->val.num >=3D (u64)cpu__max_present_cpu().cpu) { + parse_events_error__handle(err, term->err_val, + strdup("too big"), + NULL); + return -EINVAL; + } + break; case PARSE_EVENTS__TERM_TYPE_DRV_CFG: case PARSE_EVENTS__TERM_TYPE_USER: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: @@ -1133,6 +1173,7 @@ static int config_term_tracepoint(struct perf_event_a= ttr *attr, case PARSE_EVENTS__TERM_TYPE_RAW: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: + case PARSE_EVENTS__TERM_TYPE_CPU: default: if (err) { parse_events_error__handle(err, term->err_term, @@ -1264,6 +1305,7 @@ do { \ case PARSE_EVENTS__TERM_TYPE_RAW: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: + case PARSE_EVENTS__TERM_TYPE_CPU: default: break; } @@ -1317,6 +1359,7 @@ static int get_config_chgs(struct perf_pmu *pmu, stru= ct parse_events_terms *head case PARSE_EVENTS__TERM_TYPE_RAW: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: case PARSE_EVENTS__TERM_TYPE_HARDWARE: + case PARSE_EVENTS__TERM_TYPE_CPU: default: break; } @@ -1371,6 +1414,7 @@ static int __parse_events_add_numeric(struct parse_ev= ents_state *parse_state, struct perf_event_attr attr; LIST_HEAD(config_terms); const char *name, *metric_id; + struct perf_cpu_map *cpus; int ret; =20 memset(&attr, 0, sizeof(attr)); @@ -1392,9 +1436,11 @@ static int __parse_events_add_numeric(struct parse_e= vents_state *parse_state, =20 name =3D get_config_name(head_config); metric_id =3D get_config_metric_id(head_config); + cpus =3D get_config_cpu(head_config); ret =3D __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, na= me, metric_id, pmu, &config_terms, /*auto_merge_stats=3D*/false, - /*cpu_list=3D*/NULL) ? 0 : -ENOMEM; + cpus) ? 0 : -ENOMEM; + perf_cpu_map__put(cpus); free_config_terms(&config_terms); return ret; } @@ -1461,6 +1507,7 @@ static int parse_events_add_pmu(struct parse_events_s= tate *parse_state, LIST_HEAD(config_terms); struct parse_events_terms parsed_terms; bool alias_rewrote_terms =3D false; + struct perf_cpu_map *term_cpu =3D NULL; =20 if (verbose > 1) { struct strbuf sb; @@ -1552,10 +1599,12 @@ static int parse_events_add_pmu(struct parse_events= _state *parse_state, return -EINVAL; } =20 + term_cpu =3D get_config_cpu(&parsed_terms); evsel =3D __add_event(list, &parse_state->idx, &attr, /*init_attr=3D*/tru= e, get_config_name(&parsed_terms), get_config_metric_id(&parsed_terms), pmu, - &config_terms, auto_merge_stats, /*cpu_list=3D*/NULL); + &config_terms, auto_merge_stats, term_cpu); + perf_cpu_map__put(term_cpu); if (!evsel) { parse_events_terms__exit(&parsed_terms); return -ENOMEM; diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index 10cc9c433116..2532c81d4f9a 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -79,7 +79,8 @@ enum parse_events__term_type { PARSE_EVENTS__TERM_TYPE_RAW, PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE, PARSE_EVENTS__TERM_TYPE_HARDWARE, -#define __PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_HARDWARE + 1) + PARSE_EVENTS__TERM_TYPE_CPU, +#define __PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_CPU + 1) }; =20 struct parse_events_term { diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l index 5a0bcd7f166a..635d216632d7 100644 --- a/tools/perf/util/parse-events.l +++ b/tools/perf/util/parse-events.l @@ -331,6 +331,7 @@ percore { return term(yyscanner, PARSE_EVENTS__TERM_T= YPE_PERCORE); } aux-output { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); } aux-sample-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMP= LE_SIZE); } metric-id { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); } +cpu { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CPU); } cpu-cycles|cycles { return hw_term(yyscanner, PERF_COUNT_HW_CPU_CYCLES)= ; } stalled-cycles-frontend|idle-cycles-frontend { return hw_term(yyscanner, P= ERF_COUNT_HW_STALLED_CYCLES_FRONTEND); } stalled-cycles-backend|idle-cycles-backend { return hw_term(yyscanner, PER= F_COUNT_HW_STALLED_CYCLES_BACKEND); } diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 61bdda01a05a..4f68026a97bb 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1738,6 +1738,7 @@ int perf_pmu__for_each_format(struct perf_pmu *pmu, v= oid *state, pmu_format_call "percore", "aux-output", "aux-sample-size=3Dnumber", + "cpu=3Dnumber", }; struct perf_pmu_format *format; int ret; --=20 2.46.0.662.g92d0881bb0-goog