From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Kan Liang <kan.liang@linux.intel.com>,
James Clark <james.clark@linaro.org>,
Xu Yang <xu.yang_2@nxp.com>,
"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
Collin Funk <collin.funk1@gmail.com>,
Howard Chu <howardchu95@gmail.com>,
Weilin Wang <weilin.wang@intel.com>,
Andi Kleen <ak@linux.intel.com>,
"Dr. David Alan Gilbert" <linux@treblig.org>,
Thomas Richter <tmricht@linux.ibm.com>,
Tiezhu Yang <yangtiezhu@loongson.cn>,
Gautam Menghani <gautam@linux.ibm.com>,
Thomas Falcon <thomas.falcon@intel.com>,
Chun-Tse Shao <ctshao@google.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH v9 12/16] perf python: Add parse_metrics function
Date: Fri, 25 Jul 2025 11:51:58 -0700 [thread overview]
Message-ID: <20250725185202.68671-13-irogers@google.com> (raw)
In-Reply-To: <20250725185202.68671-1-irogers@google.com>
Add parse_metrics function that takes a string of metrics and/or
metric groups and returns the evlist containing the events and
metrics.
For example:
```
>>> import perf
>>> perf.parse_metrics("TopdownL1")
evlist([cpu/TOPDOWN.SLOTS/,cpu/topdown-retiring/,cpu/topdown-fe-bound/,
cpu/topdown-be-bound/,cpu/topdown-bad-spec/,cpu/INT_MISC.CLEARS_COUNT/,
cpu/INT_MISC.UOP_DROPPING/])
```
Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/util/python.c | 41 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index d49b4401ab7e..3a58080bab24 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -1881,6 +1881,40 @@ static PyObject *pyrf__parse_events(PyObject *self, PyObject *args)
return result;
}
+static PyObject *pyrf__parse_metrics(PyObject *self, PyObject *args)
+{
+ const char *input;
+ struct evlist evlist = {};
+ PyObject *result;
+ PyObject *pcpus = NULL, *pthreads = NULL;
+ struct perf_cpu_map *cpus;
+ struct perf_thread_map *threads;
+ int ret;
+
+ if (!PyArg_ParseTuple(args, "s|OO", &input, &pcpus, &pthreads))
+ return NULL;
+
+ threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads : NULL;
+ cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL;
+
+ evlist__init(&evlist, cpus, threads);
+ ret = metricgroup__parse_groups(&evlist, /*pmu=*/"all", input,
+ /*metric_no_group=*/ false,
+ /*metric_no_merge=*/ false,
+ /*metric_no_threshold=*/ true,
+ /*user_requested_cpu_list=*/ NULL,
+ /*system_wide=*/true,
+ /*hardware_aware_grouping=*/ false);
+ if (ret) {
+ errno = -ret;
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ result = pyrf_evlist__from_evlist(&evlist);
+ evlist__exit(&evlist);
+ return result;
+}
+
static PyMethodDef perf__methods[] = {
{
.ml_name = "tracepoint",
@@ -1894,6 +1928,13 @@ static PyMethodDef perf__methods[] = {
.ml_flags = METH_VARARGS,
.ml_doc = PyDoc_STR("Parse a string of events and return an evlist.")
},
+ {
+ .ml_name = "parse_metrics",
+ .ml_meth = (PyCFunction) pyrf__parse_metrics,
+ .ml_flags = METH_VARARGS,
+ .ml_doc = PyDoc_STR(
+ "Parse a string of metics or metric groups and return an evlist.")
+ },
{
.ml_name = "pmus",
.ml_meth = (PyCFunction) pyrf__pmus,
--
2.50.1.552.g942d659e1b-goog
next prev parent reply other threads:[~2025-07-25 18:52 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-25 18:51 [PATCH v9 00/16] New perf ilist app Ian Rogers
2025-07-25 18:51 ` [PATCH v9 01/16] perf python: Add more exceptions on error paths Ian Rogers
2025-07-25 18:51 ` [PATCH v9 02/16] perf jevents: Add common software event json Ian Rogers
2025-07-25 18:51 ` [PATCH v9 03/16] perf parse-events: Remove non-json software events Ian Rogers
2025-07-25 18:51 ` [PATCH v9 04/16] perf tp_pmu: Factor existing tracepoint logic to new file Ian Rogers
2025-08-05 22:57 ` Howard Chu
2025-07-25 18:51 ` [PATCH v9 05/16] perf tp_pmu: Add event APIs Ian Rogers
2025-07-25 18:51 ` [PATCH v9 06/16] perf list: Remove tracepoint printing code Ian Rogers
2025-07-25 18:51 ` [PATCH v9 07/16] perf list: Skip ABI PMUs when printing pmu values Ian Rogers
2025-07-25 18:51 ` [PATCH v9 08/16] perf python: Improve the tracepoint function if no libtraceevent Ian Rogers
2025-07-25 18:51 ` [PATCH v9 09/16] perf python: Add basic PMU abstraction and pmus sequence Ian Rogers
2025-07-25 18:51 ` [PATCH v9 10/16] perf python: Add function returning dictionary of all events on a PMU Ian Rogers
2025-08-05 22:58 ` Howard Chu
2025-07-25 18:51 ` [PATCH v9 11/16] perf ilist: Add new python ilist command Ian Rogers
2025-08-05 23:05 ` Howard Chu
2025-08-18 22:13 ` Ian Rogers
2025-07-25 18:51 ` Ian Rogers [this message]
2025-07-25 18:51 ` [PATCH v9 13/16] perf python: Add evlist metrics function Ian Rogers
2025-07-25 18:52 ` [PATCH v9 14/16] perf python: Add evlist compute_metric Ian Rogers
2025-07-25 18:52 ` [PATCH v9 15/16] perf python: Add metrics function Ian Rogers
2025-07-25 18:52 ` [PATCH v9 16/16] perf ilist: Add support for metrics Ian Rogers
2025-07-26 23:12 ` [PATCH v9 00/16] New perf ilist app Namhyung Kim
2025-07-26 23:22 ` Namhyung Kim
2025-07-28 18:04 ` Namhyung Kim
2025-08-05 23:25 ` Howard Chu
2025-08-06 3:15 ` Howard Chu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250725185202.68671-13-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=acme@redhat.com \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=collin.funk1@gmail.com \
--cc=ctshao@google.com \
--cc=gautam@linux.ibm.com \
--cc=howardchu95@gmail.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux@treblig.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=thomas.falcon@intel.com \
--cc=tmricht@linux.ibm.com \
--cc=weilin.wang@intel.com \
--cc=xu.yang_2@nxp.com \
--cc=yangtiezhu@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).