perf script python: Add sample_read to dict
authorArun Kalyanasundaram <arunkaly@google.com>
Fri, 21 Jul 2017 22:04:20 +0000 (15:04 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 26 Jul 2017 01:43:19 +0000 (22:43 -0300)
Provide time_enabled, time_running and counter value in the perf_sample
dict.

Signed-off-by: Arun Kalyanasundaram <arunkaly@google.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Carrillo-Cisneros <davidcc@google.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Seongjae Park <sj38.park@gmail.com>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/20170721220422.63962-4-arunkaly@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/scripting-engines/trace-event-python.c

index 69d1b6db96f6cb52c47ceffd2c67b782d1586c51..55a45784c9109bd6d34e4c93e0e0d7cfbfe35e95 100644 (file)
@@ -391,6 +391,56 @@ exit:
        return pylist;
 }
 
+static PyObject *get_sample_value_as_tuple(struct sample_read_value *value)
+{
+       PyObject *t;
+
+       t = PyTuple_New(2);
+       if (!t)
+               Py_FatalError("couldn't create Python tuple");
+       PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id));
+       PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value));
+       return t;
+}
+
+static void set_sample_read_in_dict(PyObject *dict_sample,
+                                        struct perf_sample *sample,
+                                        struct perf_evsel *evsel)
+{
+       u64 read_format = evsel->attr.read_format;
+       PyObject *values;
+       unsigned int i;
+
+       if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
+               pydict_set_item_string_decref(dict_sample, "time_enabled",
+                       PyLong_FromUnsignedLongLong(sample->read.time_enabled));
+       }
+
+       if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
+               pydict_set_item_string_decref(dict_sample, "time_running",
+                       PyLong_FromUnsignedLongLong(sample->read.time_running));
+       }
+
+       if (read_format & PERF_FORMAT_GROUP)
+               values = PyList_New(sample->read.group.nr);
+       else
+               values = PyList_New(1);
+
+       if (!values)
+               Py_FatalError("couldn't create Python list");
+
+       if (read_format & PERF_FORMAT_GROUP) {
+               for (i = 0; i < sample->read.group.nr; i++) {
+                       PyObject *t = get_sample_value_as_tuple(&sample->read.group.values[i]);
+                       PyList_SET_ITEM(values, i, t);
+               }
+       } else {
+               PyObject *t = get_sample_value_as_tuple(&sample->read.one);
+               PyList_SET_ITEM(values, 0, t);
+       }
+       pydict_set_item_string_decref(dict_sample, "values", values);
+}
+
 static PyObject *get_perf_sample_dict(struct perf_sample *sample,
                                         struct perf_evsel *evsel,
                                         struct addr_location *al,
@@ -422,6 +472,7 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
                        PyLong_FromUnsignedLongLong(sample->time));
        pydict_set_item_string_decref(dict_sample, "period",
                        PyLong_FromUnsignedLongLong(sample->period));
+       set_sample_read_in_dict(dict_sample, sample, evsel);
        pydict_set_item_string_decref(dict, "sample", dict_sample);
 
        pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(