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

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-5-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/python.c

index 6f9728d365aeda4d0895df4d7a7a99e8c0d12245..cf112843502291bf24c2430c08d096a019a615c7 100644 (file)
@@ -670,6 +670,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;
@@ -680,6 +745,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("Returns a sequence of events encoded as a dictionaries.")
+       },
        {
                .ml_name  = "name",
                .ml_meth  = (PyCFunction)pyrf_pmu__name,