Skip to content

Commit c025148

Browse files
tardypacmel
authored andcommitted
perf scripts python: Give field dict to unhandled callback
trace_unhandled() callback does not allow to access event fields, this patch resolves the problem. It can also been used as a more pythonic and flexible way for script writters to demux event types This will for example greatly simplify pytimechart event demux. Acked-by: Frederic Weisbecker <fweisbec@gmail.com> Acked-by: Tom Zanussi <tzanussi@gmail.com> Cc: Ingo Molnar <mingo@elte.hu>, Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Tom Zanussi <tzanussi@gmail.com> LKML-Reference: <1275340329-2397-1-git-send-email-tardyp@gmail.com> Signed-off-by: Pierre Tardy <tardyp@gmail.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 75d9ef1 commit c025148

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

‎tools/perf/scripts/python/check-perf-trace.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def kmem__kmalloc(event_name, context, common_cpu,
5151

5252
flag_str("kmem__kmalloc", "gfp_flags", gfp_flags)),
5353

54-
def trace_unhandled(event_name, context, common_cpu, common_secs, common_nsecs,
55-
common_pid, common_comm):
54+
def trace_unhandled(event_name, context, event_fields_dict):
5655
try:
5756
unhandled[event_name] += 1
5857
except TypeError:

‎tools/perf/util/scripting-engines/trace-event-python.c‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static void python_process_event(int cpu, void *data,
208208
int size __unused,
209209
unsigned long long nsecs, char *comm)
210210
{
211-
PyObject *handler, *retval, *context, *t, *obj;
211+
PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
212212
static char handler_name[256];
213213
struct format_field *field;
214214
unsigned long long val;
@@ -232,6 +232,14 @@ static void python_process_event(int cpu, void *data,
232232

233233
sprintf(handler_name, "%s__%s", event->system, event->name);
234234

235+
handler = PyDict_GetItemString(main_dict, handler_name);
236+
if (handler && !PyCallable_Check(handler))
237+
handler = NULL;
238+
if (!handler) {
239+
dict = PyDict_New();
240+
if (!dict)
241+
Py_FatalError("couldn't create Python dict");
242+
}
235243
s = nsecs / NSECS_PER_SEC;
236244
ns = nsecs - s * NSECS_PER_SEC;
237245

@@ -242,12 +250,20 @@ static void python_process_event(int cpu, void *data,
242250
PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
243251
PyTuple_SetItem(t, n++,
244252
PyCObject_FromVoidPtr(scripting_context, NULL));
245-
PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
246-
PyTuple_SetItem(t, n++, PyInt_FromLong(s));
247-
PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
248-
PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
249-
PyTuple_SetItem(t, n++, PyString_FromString(comm));
250253

254+
if (handler) {
255+
PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
256+
PyTuple_SetItem(t, n++, PyInt_FromLong(s));
257+
PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
258+
PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
259+
PyTuple_SetItem(t, n++, PyString_FromString(comm));
260+
} else {
261+
PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
262+
PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
263+
PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
264+
PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
265+
PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
266+
}
251267
for (field = event->format.fields; field; field = field->next) {
252268
if (field->flags & FIELD_IS_STRING) {
253269
int offset;
@@ -272,27 +288,31 @@ static void python_process_event(int cpu, void *data,
272288
obj = PyLong_FromUnsignedLongLong(val);
273289
}
274290
}
275-
PyTuple_SetItem(t, n++, obj);
291+
if (handler)
292+
PyTuple_SetItem(t, n++, obj);
293+
else
294+
PyDict_SetItemString(dict, field->name, obj);
295+
276296
}
297+
if (!handler)
298+
PyTuple_SetItem(t, n++, dict);
277299

278300
if (_PyTuple_Resize(&t, n) == -1)
279301
Py_FatalError("error resizing Python tuple");
280302

281-
handler = PyDict_GetItemString(main_dict, handler_name);
282-
if (handler && PyCallable_Check(handler)) {
303+
if (handler) {
283304
retval = PyObject_CallObject(handler, t);
284305
if (retval == NULL)
285306
handler_call_die(handler_name);
286307
} else {
287308
handler = PyDict_GetItemString(main_dict, "trace_unhandled");
288309
if (handler && PyCallable_Check(handler)) {
289-
if (_PyTuple_Resize(&t, N_COMMON_FIELDS) == -1)
290-
Py_FatalError("error resizing Python tuple");
291310

292311
retval = PyObject_CallObject(handler, t);
293312
if (retval == NULL)
294313
handler_call_die("trace_unhandled");
295314
}
315+
Py_DECREF(dict);
296316
}
297317

298318
Py_DECREF(t);
@@ -548,12 +568,10 @@ static int python_generate_script(const char *outfile)
548568
}
549569

550570
fprintf(ofp, "def trace_unhandled(event_name, context, "
551-
"common_cpu, common_secs, common_nsecs,\n\t\t"
552-
"common_pid, common_comm):\n");
571+
"event_fields_dict):\n");
553572

554-
fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
555-
"common_secs, common_nsecs,\n\t\tcommon_pid, "
556-
"common_comm)\n\n");
573+
fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
574+
"for k,v in sorted(event_fields_dict.items())])\n\n");
557575

558576
fprintf(ofp, "def print_header("
559577
"event_name, cpu, secs, nsecs, pid, comm):\n"

0 commit comments

Comments
 (0)