Hello,
On Fri, Jul 25, 2025 at 11:52 AM Ian Rogers <irogers@google.com> wrote:
>
> 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>
> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
> tools/perf/util/python.c | 71 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 71 insertions(+)
>
> diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
> index ddcb5d40ff14..d49b4401ab7e 100644
> --- a/tools/perf/util/python.c
> +++ b/tools/perf/util/python.c
> @@ -660,6 +660,71 @@ 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);
> + int ret;
> +
> + if (!py_list)
> + return NULL;
> +
> + ret = perf_pmu__for_each_event(ppmu->pmu,
> + /*skip_duplicate_pmus=*/false,
> + py_list,
> + pyrf_pmu__events_cb);
> + if (ret) {
> + Py_DECREF(py_list);
> + errno = -ret;
> + PyErr_SetFromErrno(PyExc_OSError);
> + return NULL;
> + }
> + return py_list;
> +}
> +
> static PyObject *pyrf_pmu__repr(PyObject *self)
> {
> struct pyrf_pmu *ppmu = (void *)self;
> @@ -670,6 +735,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.")
Pretty sure it's "General information about the PMU"
Thanks,
Howard