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>,
John Garry <john.g.garry@oracle.com>,
"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
Howard Chu <howardchu95@gmail.com>,
Weilin Wang <weilin.wang@intel.com>,
Thomas Richter <tmricht@linux.ibm.com>,
Andi Kleen <ak@linux.intel.com>,
Tiezhu Yang <yangtiezhu@loongson.cn>,
Gautam Menghani <gautam@linux.ibm.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: [PATCH v4 07/15] perf python: Add function returning dictionary of all events on a PMU
Date: Fri, 27 Jun 2025 17:09:21 -0700 [thread overview]
Message-ID: <20250628000929.230406-8-irogers@google.com> (raw)
In-Reply-To: <20250628000929.230406-1-irogers@google.com>
Allow all events on a PMU to be gathered, similar to how perf list
gathers event information.
An example usage:
```
$ python
Python 3.12.9 (main, Feb 5 2025, 01:31:18) [GCC 14.2.0] on linux
>>> import perf
>>> for pmu in perf.pmus():
... print(pmu.events())
...
[{'name': 'mem_load_retired.l3_hit', 'desc': 'Retired load instructions...
```
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 67 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 67 insertions(+)
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 33b23d56dfb1..f86a82548636 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -648,6 +648,67 @@ static PyObject *pyrf_pmu__name(PyObject *self)
return PyUnicode_FromString(ppmu->pmu->name);
}
+static bool add_to_dict(PyObject *dict, const char *key, const char *value)
+{
+ PyObject *pkey, *pvalue;
+ bool ret;
+
+ if (value == NULL)
+ return true;
+
+ pkey = PyUnicode_FromString(key);
+ pvalue = PyUnicode_FromString(value);
+
+ ret = pkey && pvalue && PyDict_SetItem(dict, pkey, pvalue) == 0;
+ Py_XDECREF(pkey);
+ Py_XDECREF(pvalue);
+ return ret;
+}
+
+static int pyrf_pmu__events_cb(void *state, struct pmu_event_info *info)
+{
+ PyObject *py_list = state;
+ PyObject *dict = PyDict_New();
+
+ if (!dict)
+ return -ENOMEM;
+
+ if (!add_to_dict(dict, "name", info->name) ||
+ !add_to_dict(dict, "alias", info->alias) ||
+ !add_to_dict(dict, "scale_unit", info->scale_unit) ||
+ !add_to_dict(dict, "desc", info->desc) ||
+ !add_to_dict(dict, "long_desc", info->long_desc) ||
+ !add_to_dict(dict, "encoding_desc", info->encoding_desc) ||
+ !add_to_dict(dict, "topic", info->topic) ||
+ !add_to_dict(dict, "event_type_desc", info->event_type_desc) ||
+ !add_to_dict(dict, "str", info->str) ||
+ !add_to_dict(dict, "deprecated", info->deprecated ? "deprecated" : NULL) ||
+ PyList_Append(py_list, dict) != 0) {
+ Py_DECREF(dict);
+ return -ENOMEM;
+ }
+ Py_DECREF(dict);
+ return 0;
+}
+
+static PyObject *pyrf_pmu__events(PyObject *self)
+{
+ struct pyrf_pmu *ppmu = (void *)self;
+ PyObject *py_list = PyList_New(0);
+
+ if (!py_list)
+ return NULL;
+
+ if (perf_pmu__for_each_event(ppmu->pmu,
+ /*skip_duplicate_pmus=*/false,
+ py_list,
+ pyrf_pmu__events_cb) != 0) {
+ Py_DECREF(py_list);
+ return NULL;
+ }
+ return py_list;
+}
+
static PyObject *pyrf_pmu__repr(PyObject *self)
{
struct pyrf_pmu *ppmu = (void *)self;
@@ -658,6 +719,12 @@ static PyObject *pyrf_pmu__repr(PyObject *self)
static const char pyrf_pmu__doc[] = PyDoc_STR("perf Performance Monitoring Unit (PMU) object.");
static PyMethodDef pyrf_pmu__methods[] = {
+ {
+ .ml_name = "events",
+ .ml_meth = (PyCFunction)pyrf_pmu__events,
+ .ml_flags = METH_NOARGS,
+ .ml_doc = PyDoc_STR("Name of the PMU including suffixes.")
+ },
{
.ml_name = "name",
.ml_meth = (PyCFunction)pyrf_pmu__name,
--
2.50.0.727.gbf7dc18ff4-goog
next prev parent reply other threads:[~2025-06-28 0:09 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-28 0:09 [PATCH v4 00/15] New perf ilist app Ian Rogers
2025-06-28 0:09 ` [PATCH v4 01/15] perf hwmon_pmu: Avoid shortening hwmon PMU name Ian Rogers
2025-06-28 0:09 ` [PATCH v4 02/15] perf parse-events: Minor tidy up of event_type helper Ian Rogers
2025-06-28 0:09 ` [PATCH v4 03/15] perf python: In str(evsel) use the evsel__pmu_name helper Ian Rogers
2025-06-28 0:09 ` [PATCH v4 04/15] perf python: Fix thread check in pyrf_evsel__read Ian Rogers
2025-06-28 0:09 ` [PATCH v4 05/15] perf python: Correct pyrf_evsel__read for tool PMUs Ian Rogers
2025-06-28 0:09 ` [PATCH v4 06/15] perf python: Add basic PMU abstraction and pmus sequence Ian Rogers
2025-06-28 0:09 ` Ian Rogers [this message]
2025-06-28 0:09 ` [PATCH v4 08/15] perf jevents: If the long_desc and desc are identical then drop the long_desc Ian Rogers
2025-06-28 0:09 ` [PATCH v4 09/15] perf jevents: Add common software event json Ian Rogers
2025-06-28 0:09 ` [PATCH v4 10/15] perf pmu: Tolerate failure to read the type for wellknown PMUs Ian Rogers
2025-06-28 0:09 ` [PATCH v4 11/15] perf parse-events: Remove non-json software events Ian Rogers
2025-06-28 0:09 ` [PATCH v4 12/15] perf tp_pmu: Factor existing tracepoint logic to new file Ian Rogers
2025-06-28 0:09 ` [PATCH v4 13/15] perf tp_pmu: Add event APIs Ian Rogers
2025-06-28 0:09 ` [PATCH v4 14/15] perf list: Remove tracepoint printing code Ian Rogers
2025-06-28 0:09 ` [PATCH v4 15/15] perf ilist: Add new python ilist command Ian Rogers
2025-07-09 16:35 ` [PATCH v4 00/15] New perf ilist app Namhyung Kim
2025-07-09 17:13 ` Ian Rogers
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=20250628000929.230406-8-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=gautam@linux.ibm.com \
--cc=howardchu95@gmail.com \
--cc=james.clark@linaro.org \
--cc=john.g.garry@oracle.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.