aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/scripting-engines/trace-event-python.c
diff options
context:
space:
mode:
authorRussell King <rmk+kernel@arm.linux.org.uk>2010-08-06 13:13:54 -0400
committerRussell King <rmk+kernel@arm.linux.org.uk>2010-08-06 13:13:54 -0400
commit11e4afb49b7fa1fc8e1ffd850c1806dd86a08204 (patch)
tree9e57efcb106ae912f7bec718feb3f8ec607559bb /tools/perf/util/scripting-engines/trace-event-python.c
parent162500b3a3ff39d941d29db49b41a16667ae44f0 (diff)
parent9b2a606d3898fcb2eedb6faded3bb37549590ac4 (diff)
Merge branches 'gemini' and 'misc' into devel
Diffstat (limited to 'tools/perf/util/scripting-engines/trace-event-python.c')
-rw-r--r--tools/perf/util/scripting-engines/trace-event-python.c67
1 files changed, 44 insertions, 23 deletions
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 33a414bbba3e..33a632523743 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -208,7 +208,7 @@ static void python_process_event(int cpu, void *data,
208 int size __unused, 208 int size __unused,
209 unsigned long long nsecs, char *comm) 209 unsigned long long nsecs, char *comm)
210{ 210{
211 PyObject *handler, *retval, *context, *t; 211 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
212 static char handler_name[256]; 212 static char handler_name[256];
213 struct format_field *field; 213 struct format_field *field;
214 unsigned long long val; 214 unsigned long long val;
@@ -232,6 +232,14 @@ static void python_process_event(int cpu, void *data,
232 232
233 sprintf(handler_name, "%s__%s", event->system, event->name); 233 sprintf(handler_name, "%s__%s", event->system, event->name);
234 234
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 }
235 s = nsecs / NSECS_PER_SEC; 243 s = nsecs / NSECS_PER_SEC;
236 ns = nsecs - s * NSECS_PER_SEC; 244 ns = nsecs - s * NSECS_PER_SEC;
237 245
@@ -242,12 +250,20 @@ static void python_process_event(int cpu, void *data,
242 PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); 250 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
243 PyTuple_SetItem(t, n++, 251 PyTuple_SetItem(t, n++,
244 PyCObject_FromVoidPtr(scripting_context, NULL)); 252 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));
250 253
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 }
251 for (field = event->format.fields; field; field = field->next) { 267 for (field = event->format.fields; field; field = field->next) {
252 if (field->flags & FIELD_IS_STRING) { 268 if (field->flags & FIELD_IS_STRING) {
253 int offset; 269 int offset;
@@ -256,36 +272,47 @@ static void python_process_event(int cpu, void *data,
256 offset &= 0xffff; 272 offset &= 0xffff;
257 } else 273 } else
258 offset = field->offset; 274 offset = field->offset;
259 PyTuple_SetItem(t, n++, 275 obj = PyString_FromString((char *)data + offset);
260 PyString_FromString((char *)data + offset));
261 } else { /* FIELD_IS_NUMERIC */ 276 } else { /* FIELD_IS_NUMERIC */
262 val = read_size(data + field->offset, field->size); 277 val = read_size(data + field->offset, field->size);
263 if (field->flags & FIELD_IS_SIGNED) { 278 if (field->flags & FIELD_IS_SIGNED) {
264 PyTuple_SetItem(t, n++, PyInt_FromLong(val)); 279 if ((long long)val >= LONG_MIN &&
280 (long long)val <= LONG_MAX)
281 obj = PyInt_FromLong(val);
282 else
283 obj = PyLong_FromLongLong(val);
265 } else { 284 } else {
266 PyTuple_SetItem(t, n++, PyInt_FromLong(val)); 285 if (val <= LONG_MAX)
286 obj = PyInt_FromLong(val);
287 else
288 obj = PyLong_FromUnsignedLongLong(val);
267 } 289 }
268 } 290 }
291 if (handler)
292 PyTuple_SetItem(t, n++, obj);
293 else
294 PyDict_SetItemString(dict, field->name, obj);
295
269 } 296 }
297 if (!handler)
298 PyTuple_SetItem(t, n++, dict);
270 299
271 if (_PyTuple_Resize(&t, n) == -1) 300 if (_PyTuple_Resize(&t, n) == -1)
272 Py_FatalError("error resizing Python tuple"); 301 Py_FatalError("error resizing Python tuple");
273 302
274 handler = PyDict_GetItemString(main_dict, handler_name); 303 if (handler) {
275 if (handler && PyCallable_Check(handler)) {
276 retval = PyObject_CallObject(handler, t); 304 retval = PyObject_CallObject(handler, t);
277 if (retval == NULL) 305 if (retval == NULL)
278 handler_call_die(handler_name); 306 handler_call_die(handler_name);
279 } else { 307 } else {
280 handler = PyDict_GetItemString(main_dict, "trace_unhandled"); 308 handler = PyDict_GetItemString(main_dict, "trace_unhandled");
281 if (handler && PyCallable_Check(handler)) { 309 if (handler && PyCallable_Check(handler)) {
282 if (_PyTuple_Resize(&t, N_COMMON_FIELDS) == -1)
283 Py_FatalError("error resizing Python tuple");
284 310
285 retval = PyObject_CallObject(handler, t); 311 retval = PyObject_CallObject(handler, t);
286 if (retval == NULL) 312 if (retval == NULL)
287 handler_call_die("trace_unhandled"); 313 handler_call_die("trace_unhandled");
288 } 314 }
315 Py_DECREF(dict);
289 } 316 }
290 317
291 Py_DECREF(t); 318 Py_DECREF(t);
@@ -367,8 +394,6 @@ static int python_start_script(const char *script, int argc, const char **argv)
367 } 394 }
368 395
369 free(command_line); 396 free(command_line);
370 fprintf(stderr, "perf trace started with Python script %s\n\n",
371 script);
372 397
373 return err; 398 return err;
374error: 399error:
@@ -400,8 +425,6 @@ out:
400 Py_XDECREF(main_module); 425 Py_XDECREF(main_module);
401 Py_Finalize(); 426 Py_Finalize();
402 427
403 fprintf(stderr, "\nperf trace Python script stopped\n");
404
405 return err; 428 return err;
406} 429}
407 430
@@ -545,12 +568,10 @@ static int python_generate_script(const char *outfile)
545 } 568 }
546 569
547 fprintf(ofp, "def trace_unhandled(event_name, context, " 570 fprintf(ofp, "def trace_unhandled(event_name, context, "
548 "common_cpu, common_secs, common_nsecs,\n\t\t" 571 "event_fields_dict):\n");
549 "common_pid, common_comm):\n");
550 572
551 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " 573 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
552 "common_secs, common_nsecs,\n\t\tcommon_pid, " 574 "for k,v in sorted(event_fields_dict.items())])\n\n");
553 "common_comm)\n\n");
554 575
555 fprintf(ofp, "def print_header(" 576 fprintf(ofp, "def print_header("
556 "event_name, cpu, secs, nsecs, pid, comm):\n" 577 "event_name, cpu, secs, nsecs, pid, comm):\n"