]> www.infradead.org Git - users/hch/misc.git/commitdiff
perf python: Add parse_metrics function
authorIan Rogers <irogers@google.com>
Tue, 19 Aug 2025 01:39:36 +0000 (18:39 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 3 Sep 2025 15:34:54 +0000 (12:34 -0300)
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/])
```

Reviewed-by: Howard Chu <howardchu95@gmail.com>
Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Collin Funk <collin.funk1@gmail.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Gautam Menghani <gautam@linux.ibm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Falcon <thomas.falcon@intel.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Xu Yang <xu.yang_2@nxp.com>
Link: https://lore.kernel.org/r/20250819013941.209033-7-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/python.c

index cf112843502291bf24c2430c08d096a019a615c7..48308ed4e1c73782ba9f0ef651ca340379d180ee 100644 (file)
@@ -1891,6 +1891,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",
@@ -1904,6 +1938,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,