Skip to content

Commit 739621f

Browse files
Gautam Menghaniacmel
authored andcommitted
perf python: Add evsel read method
Add the evsel read method to enable python to read counter data for the given evsel. Signed-off-by: Gautam Menghani <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Howard Chu <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/linux-perf-users/[email protected]/ Link: https://lore.kernel.org/r/[email protected] [ make the API take a CPU and thread then compute from these the appropriate indices. ] Signed-off-by: Ian Rogers <[email protected]> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 3b4991d commit 739621f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tools/perf/util/python.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,37 @@ static PyObject *pyrf_evsel__threads(struct pyrf_evsel *pevsel)
888888
return (PyObject *)pthread_map;
889889
}
890890

891+
static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel,
892+
PyObject *args, PyObject *kwargs)
893+
{
894+
struct evsel *evsel = &pevsel->evsel;
895+
int cpu = 0, cpu_idx, thread = 0, thread_idx;
896+
struct perf_counts_values counts;
897+
struct pyrf_counts_values *count_values = PyObject_New(struct pyrf_counts_values,
898+
&pyrf_counts_values__type);
899+
900+
if (!count_values)
901+
return NULL;
902+
903+
if (!PyArg_ParseTuple(args, "ii", &cpu, &thread))
904+
return NULL;
905+
906+
cpu_idx = perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){.cpu = cpu});
907+
if (cpu_idx < 0) {
908+
PyErr_Format(PyExc_TypeError, "CPU %d is not part of evsel's CPUs", cpu);
909+
return NULL;
910+
}
911+
thread_idx = perf_thread_map__idx(evsel->core.threads, thread);
912+
if (cpu_idx < 0) {
913+
PyErr_Format(PyExc_TypeError, "Thread %d is not part of evsel's threads",
914+
thread);
915+
return NULL;
916+
}
917+
perf_evsel__read(&(evsel->core), cpu_idx, thread_idx, &counts);
918+
count_values->values = counts;
919+
return (PyObject *)count_values;
920+
}
921+
891922
static PyObject *pyrf_evsel__str(PyObject *self)
892923
{
893924
struct pyrf_evsel *pevsel = (void *)self;
@@ -918,6 +949,12 @@ static PyMethodDef pyrf_evsel__methods[] = {
918949
.ml_flags = METH_NOARGS,
919950
.ml_doc = PyDoc_STR("threads the event is to be used with.")
920951
},
952+
{
953+
.ml_name = "read",
954+
.ml_meth = (PyCFunction)pyrf_evsel__read,
955+
.ml_flags = METH_VARARGS | METH_KEYWORDS,
956+
.ml_doc = PyDoc_STR("read counters")
957+
},
921958
{ .ml_name = NULL, }
922959
};
923960

0 commit comments

Comments
 (0)