Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf python: Add metrics function

The metrics function returns a list dictionaries describing metrics as
strings mapping to strings, except for metric groups that are a string
mapping to a list of strings. For example:
```
>>> import perf
>>> perf.metrics()[0]
{'MetricGroup': ['Power'], 'MetricName': 'C10_Pkg_Residency',
'PMU': 'default_core', 'MetricExpr': 'cstate_pkg@c10\\-residency@ / TSC',
'ScaleUnit': '100%', 'BriefDescription': 'C10 residency percent per package'}
```

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-10-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
47b3e957 064647d6

+86
+86
tools/perf/util/python.c
··· 2083 2083 return result; 2084 2084 } 2085 2085 2086 + static PyObject *pyrf__metrics_groups(const struct pmu_metric *pm) 2087 + { 2088 + PyObject *groups = PyList_New(/*len=*/0); 2089 + const char *mg = pm->metric_group; 2090 + 2091 + if (!groups) 2092 + return NULL; 2093 + 2094 + while (mg) { 2095 + PyObject *val = NULL; 2096 + const char *sep = strchr(mg, ';'); 2097 + size_t len = sep ? (size_t)(sep - mg) : strlen(mg); 2098 + 2099 + if (len > 0) { 2100 + val = PyUnicode_FromStringAndSize(mg, len); 2101 + if (val) 2102 + PyList_Append(groups, val); 2103 + 2104 + Py_XDECREF(val); 2105 + } 2106 + mg = sep ? sep + 1 : NULL; 2107 + } 2108 + return groups; 2109 + } 2110 + 2111 + static int pyrf__metrics_cb(const struct pmu_metric *pm, 2112 + const struct pmu_metrics_table *table __maybe_unused, 2113 + void *vdata) 2114 + { 2115 + PyObject *py_list = vdata; 2116 + PyObject *dict = PyDict_New(); 2117 + PyObject *key = dict ? PyUnicode_FromString("MetricGroup") : NULL; 2118 + PyObject *value = key ? pyrf__metrics_groups(pm) : NULL; 2119 + 2120 + if (!value || PyDict_SetItem(dict, key, value) != 0) { 2121 + Py_XDECREF(key); 2122 + Py_XDECREF(value); 2123 + Py_XDECREF(dict); 2124 + return -ENOMEM; 2125 + } 2126 + 2127 + if (!add_to_dict(dict, "MetricName", pm->metric_name) || 2128 + !add_to_dict(dict, "PMU", pm->pmu) || 2129 + !add_to_dict(dict, "MetricExpr", pm->metric_expr) || 2130 + !add_to_dict(dict, "MetricThreshold", pm->metric_threshold) || 2131 + !add_to_dict(dict, "ScaleUnit", pm->unit) || 2132 + !add_to_dict(dict, "Compat", pm->compat) || 2133 + !add_to_dict(dict, "BriefDescription", pm->desc) || 2134 + !add_to_dict(dict, "PublicDescription", pm->long_desc) || 2135 + PyList_Append(py_list, dict) != 0) { 2136 + Py_DECREF(dict); 2137 + return -ENOMEM; 2138 + } 2139 + Py_DECREF(dict); 2140 + return 0; 2141 + } 2142 + 2143 + static PyObject *pyrf__metrics(PyObject *self, PyObject *args) 2144 + { 2145 + const struct pmu_metrics_table *table = pmu_metrics_table__find(); 2146 + PyObject *list = PyList_New(/*len=*/0); 2147 + int ret; 2148 + 2149 + if (!list) 2150 + return NULL; 2151 + 2152 + ret = pmu_metrics_table__for_each_metric(table, pyrf__metrics_cb, list); 2153 + if (!ret) 2154 + ret = pmu_for_each_sys_metric(pyrf__metrics_cb, list); 2155 + 2156 + if (ret) { 2157 + Py_DECREF(list); 2158 + errno = -ret; 2159 + PyErr_SetFromErrno(PyExc_OSError); 2160 + return NULL; 2161 + } 2162 + return list; 2163 + } 2164 + 2086 2165 static PyMethodDef perf__methods[] = { 2166 + { 2167 + .ml_name = "metrics", 2168 + .ml_meth = (PyCFunction) pyrf__metrics, 2169 + .ml_flags = METH_NOARGS, 2170 + .ml_doc = PyDoc_STR( 2171 + "Returns a list of metrics represented as string values in dictionaries.") 2172 + }, 2087 2173 { 2088 2174 .ml_name = "tracepoint", 2089 2175 .ml_meth = (PyCFunction) pyrf__tracepoint,