hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/tools/perf/util/scripting-engines/trace-event-python.c
....@@ -31,11 +31,12 @@
3131 #include <linux/compiler.h>
3232 #include <linux/time64.h>
3333
34
-#include "../../perf.h"
34
+#include "../build-id.h"
35
+#include "../counts.h"
3536 #include "../debug.h"
37
+#include "../dso.h"
3638 #include "../callchain.h"
3739 #include "../evsel.h"
38
-#include "../util.h"
3940 #include "../event.h"
4041 #include "../thread.h"
4142 #include "../comm.h"
....@@ -44,8 +45,9 @@
4445 #include "../thread-stack.h"
4546 #include "../trace-event.h"
4647 #include "../call-path.h"
48
+#include "map.h"
49
+#include "symbol.h"
4750 #include "thread_map.h"
48
-#include "cpumap.h"
4951 #include "print_binary.h"
5052 #include "stat.h"
5153 #include "mem-events.h"
....@@ -110,6 +112,8 @@
110112 PyObject *sample_handler;
111113 PyObject *call_path_handler;
112114 PyObject *call_return_handler;
115
+ PyObject *synth_handler;
116
+ PyObject *context_switch_handler;
113117 bool db_export_mode;
114118 };
115119
....@@ -193,7 +197,7 @@
193197 call_object(handler, args, handler_name);
194198 }
195199
196
-static void define_value(enum print_arg_type field_type,
200
+static void define_value(enum tep_print_arg_type field_type,
197201 const char *ev_name,
198202 const char *field_name,
199203 const char *field_value,
....@@ -204,7 +208,7 @@
204208 unsigned long long value;
205209 unsigned n = 0;
206210
207
- if (field_type == PRINT_SYMBOL)
211
+ if (field_type == TEP_PRINT_SYMBOL)
208212 handler_name = "define_symbolic_value";
209213
210214 t = PyTuple_New(4);
....@@ -223,8 +227,8 @@
223227 Py_DECREF(t);
224228 }
225229
226
-static void define_values(enum print_arg_type field_type,
227
- struct print_flag_sym *field,
230
+static void define_values(enum tep_print_arg_type field_type,
231
+ struct tep_print_flag_sym *field,
228232 const char *ev_name,
229233 const char *field_name)
230234 {
....@@ -235,7 +239,7 @@
235239 define_values(field_type, field->next, ev_name, field_name);
236240 }
237241
238
-static void define_field(enum print_arg_type field_type,
242
+static void define_field(enum tep_print_arg_type field_type,
239243 const char *ev_name,
240244 const char *field_name,
241245 const char *delim)
....@@ -244,10 +248,10 @@
244248 PyObject *t;
245249 unsigned n = 0;
246250
247
- if (field_type == PRINT_SYMBOL)
251
+ if (field_type == TEP_PRINT_SYMBOL)
248252 handler_name = "define_symbolic_field";
249253
250
- if (field_type == PRINT_FLAGS)
254
+ if (field_type == TEP_PRINT_FLAGS)
251255 t = PyTuple_New(3);
252256 else
253257 t = PyTuple_New(2);
....@@ -256,7 +260,7 @@
256260
257261 PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
258262 PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
259
- if (field_type == PRINT_FLAGS)
263
+ if (field_type == TEP_PRINT_FLAGS)
260264 PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim));
261265
262266 try_call_object(handler_name, t);
....@@ -264,54 +268,54 @@
264268 Py_DECREF(t);
265269 }
266270
267
-static void define_event_symbols(struct event_format *event,
271
+static void define_event_symbols(struct tep_event *event,
268272 const char *ev_name,
269
- struct print_arg *args)
273
+ struct tep_print_arg *args)
270274 {
271275 if (args == NULL)
272276 return;
273277
274278 switch (args->type) {
275
- case PRINT_NULL:
279
+ case TEP_PRINT_NULL:
276280 break;
277
- case PRINT_ATOM:
278
- define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
281
+ case TEP_PRINT_ATOM:
282
+ define_value(TEP_PRINT_FLAGS, ev_name, cur_field_name, "0",
279283 args->atom.atom);
280284 zero_flag_atom = 0;
281285 break;
282
- case PRINT_FIELD:
286
+ case TEP_PRINT_FIELD:
283287 free(cur_field_name);
284288 cur_field_name = strdup(args->field.name);
285289 break;
286
- case PRINT_FLAGS:
290
+ case TEP_PRINT_FLAGS:
287291 define_event_symbols(event, ev_name, args->flags.field);
288
- define_field(PRINT_FLAGS, ev_name, cur_field_name,
292
+ define_field(TEP_PRINT_FLAGS, ev_name, cur_field_name,
289293 args->flags.delim);
290
- define_values(PRINT_FLAGS, args->flags.flags, ev_name,
294
+ define_values(TEP_PRINT_FLAGS, args->flags.flags, ev_name,
291295 cur_field_name);
292296 break;
293
- case PRINT_SYMBOL:
297
+ case TEP_PRINT_SYMBOL:
294298 define_event_symbols(event, ev_name, args->symbol.field);
295
- define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
296
- define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
299
+ define_field(TEP_PRINT_SYMBOL, ev_name, cur_field_name, NULL);
300
+ define_values(TEP_PRINT_SYMBOL, args->symbol.symbols, ev_name,
297301 cur_field_name);
298302 break;
299
- case PRINT_HEX:
300
- case PRINT_HEX_STR:
303
+ case TEP_PRINT_HEX:
304
+ case TEP_PRINT_HEX_STR:
301305 define_event_symbols(event, ev_name, args->hex.field);
302306 define_event_symbols(event, ev_name, args->hex.size);
303307 break;
304
- case PRINT_INT_ARRAY:
308
+ case TEP_PRINT_INT_ARRAY:
305309 define_event_symbols(event, ev_name, args->int_array.field);
306310 define_event_symbols(event, ev_name, args->int_array.count);
307311 define_event_symbols(event, ev_name, args->int_array.el_size);
308312 break;
309
- case PRINT_STRING:
313
+ case TEP_PRINT_STRING:
310314 break;
311
- case PRINT_TYPE:
315
+ case TEP_PRINT_TYPE:
312316 define_event_symbols(event, ev_name, args->typecast.item);
313317 break;
314
- case PRINT_OP:
318
+ case TEP_PRINT_OP:
315319 if (strcmp(args->op.op, ":") == 0)
316320 zero_flag_atom = 1;
317321 define_event_symbols(event, ev_name, args->op.left);
....@@ -319,11 +323,11 @@
319323 break;
320324 default:
321325 /* gcc warns for these? */
322
- case PRINT_BSTRING:
323
- case PRINT_DYNAMIC_ARRAY:
324
- case PRINT_DYNAMIC_ARRAY_LEN:
325
- case PRINT_FUNC:
326
- case PRINT_BITMASK:
326
+ case TEP_PRINT_BSTRING:
327
+ case TEP_PRINT_DYNAMIC_ARRAY:
328
+ case TEP_PRINT_DYNAMIC_ARRAY_LEN:
329
+ case TEP_PRINT_FUNC:
330
+ case TEP_PRINT_BITMASK:
327331 /* we should warn... */
328332 return;
329333 }
....@@ -332,10 +336,10 @@
332336 define_event_symbols(event, ev_name, args->next);
333337 }
334338
335
-static PyObject *get_field_numeric_entry(struct event_format *event,
336
- struct format_field *field, void *data)
339
+static PyObject *get_field_numeric_entry(struct tep_event *event,
340
+ struct tep_format_field *field, void *data)
337341 {
338
- bool is_array = field->flags & FIELD_IS_ARRAY;
342
+ bool is_array = field->flags & TEP_FIELD_IS_ARRAY;
339343 PyObject *obj = NULL, *list = NULL;
340344 unsigned long long val;
341345 unsigned int item_size, n_items, i;
....@@ -353,7 +357,7 @@
353357
354358 val = read_size(event, data + field->offset + i * item_size,
355359 item_size);
356
- if (field->flags & FIELD_IS_SIGNED) {
360
+ if (field->flags & TEP_FIELD_IS_SIGNED) {
357361 if ((long long)val >= LONG_MIN &&
358362 (long long)val <= LONG_MAX)
359363 obj = _PyLong_FromLong(val);
....@@ -388,7 +392,7 @@
388392 }
389393
390394 static PyObject *python_process_callchain(struct perf_sample *sample,
391
- struct perf_evsel *evsel,
395
+ struct evsel *evsel,
392396 struct addr_location *al)
393397 {
394398 PyObject *pylist;
....@@ -424,24 +428,24 @@
424428 pydict_set_item_string_decref(pyelem, "ip",
425429 PyLong_FromUnsignedLongLong(node->ip));
426430
427
- if (node->sym) {
431
+ if (node->ms.sym) {
428432 PyObject *pysym = PyDict_New();
429433 if (!pysym)
430434 Py_FatalError("couldn't create Python dictionary");
431435 pydict_set_item_string_decref(pysym, "start",
432
- PyLong_FromUnsignedLongLong(node->sym->start));
436
+ PyLong_FromUnsignedLongLong(node->ms.sym->start));
433437 pydict_set_item_string_decref(pysym, "end",
434
- PyLong_FromUnsignedLongLong(node->sym->end));
438
+ PyLong_FromUnsignedLongLong(node->ms.sym->end));
435439 pydict_set_item_string_decref(pysym, "binding",
436
- _PyLong_FromLong(node->sym->binding));
440
+ _PyLong_FromLong(node->ms.sym->binding));
437441 pydict_set_item_string_decref(pysym, "name",
438
- _PyUnicode_FromStringAndSize(node->sym->name,
439
- node->sym->namelen));
442
+ _PyUnicode_FromStringAndSize(node->ms.sym->name,
443
+ node->ms.sym->namelen));
440444 pydict_set_item_string_decref(pyelem, "sym", pysym);
441445 }
442446
443
- if (node->map) {
444
- const char *dsoname = get_dsoname(node->map);
447
+ if (node->ms.map) {
448
+ const char *dsoname = get_dsoname(node->ms.map);
445449
446450 pydict_set_item_string_decref(pyelem, "dso",
447451 _PyUnicode_FromString(dsoname));
....@@ -460,6 +464,7 @@
460464 struct thread *thread)
461465 {
462466 struct branch_stack *br = sample->branch_stack;
467
+ struct branch_entry *entries = perf_sample__branch_entries(sample);
463468 PyObject *pylist;
464469 u64 i;
465470
....@@ -480,28 +485,28 @@
480485 Py_FatalError("couldn't create Python dictionary");
481486
482487 pydict_set_item_string_decref(pyelem, "from",
483
- PyLong_FromUnsignedLongLong(br->entries[i].from));
488
+ PyLong_FromUnsignedLongLong(entries[i].from));
484489 pydict_set_item_string_decref(pyelem, "to",
485
- PyLong_FromUnsignedLongLong(br->entries[i].to));
490
+ PyLong_FromUnsignedLongLong(entries[i].to));
486491 pydict_set_item_string_decref(pyelem, "mispred",
487
- PyBool_FromLong(br->entries[i].flags.mispred));
492
+ PyBool_FromLong(entries[i].flags.mispred));
488493 pydict_set_item_string_decref(pyelem, "predicted",
489
- PyBool_FromLong(br->entries[i].flags.predicted));
494
+ PyBool_FromLong(entries[i].flags.predicted));
490495 pydict_set_item_string_decref(pyelem, "in_tx",
491
- PyBool_FromLong(br->entries[i].flags.in_tx));
496
+ PyBool_FromLong(entries[i].flags.in_tx));
492497 pydict_set_item_string_decref(pyelem, "abort",
493
- PyBool_FromLong(br->entries[i].flags.abort));
498
+ PyBool_FromLong(entries[i].flags.abort));
494499 pydict_set_item_string_decref(pyelem, "cycles",
495
- PyLong_FromUnsignedLongLong(br->entries[i].flags.cycles));
500
+ PyLong_FromUnsignedLongLong(entries[i].flags.cycles));
496501
497502 thread__find_map_fb(thread, sample->cpumode,
498
- br->entries[i].from, &al);
503
+ entries[i].from, &al);
499504 dsoname = get_dsoname(al.map);
500505 pydict_set_item_string_decref(pyelem, "from_dsoname",
501506 _PyUnicode_FromString(dsoname));
502507
503508 thread__find_map_fb(thread, sample->cpumode,
504
- br->entries[i].to, &al);
509
+ entries[i].to, &al);
505510 dsoname = get_dsoname(al.map);
506511 pydict_set_item_string_decref(pyelem, "to_dsoname",
507512 _PyUnicode_FromString(dsoname));
....@@ -557,6 +562,7 @@
557562 struct thread *thread)
558563 {
559564 struct branch_stack *br = sample->branch_stack;
565
+ struct branch_entry *entries = perf_sample__branch_entries(sample);
560566 PyObject *pylist;
561567 u64 i;
562568 char bf[512];
....@@ -577,22 +583,22 @@
577583 Py_FatalError("couldn't create Python dictionary");
578584
579585 thread__find_symbol_fb(thread, sample->cpumode,
580
- br->entries[i].from, &al);
586
+ entries[i].from, &al);
581587 get_symoff(al.sym, &al, true, bf, sizeof(bf));
582588 pydict_set_item_string_decref(pyelem, "from",
583589 _PyUnicode_FromString(bf));
584590
585591 thread__find_symbol_fb(thread, sample->cpumode,
586
- br->entries[i].to, &al);
592
+ entries[i].to, &al);
587593 get_symoff(al.sym, &al, true, bf, sizeof(bf));
588594 pydict_set_item_string_decref(pyelem, "to",
589595 _PyUnicode_FromString(bf));
590596
591
- get_br_mspred(&br->entries[i].flags, bf, sizeof(bf));
597
+ get_br_mspred(&entries[i].flags, bf, sizeof(bf));
592598 pydict_set_item_string_decref(pyelem, "pred",
593599 _PyUnicode_FromString(bf));
594600
595
- if (br->entries[i].flags.in_tx) {
601
+ if (entries[i].flags.in_tx) {
596602 pydict_set_item_string_decref(pyelem, "in_tx",
597603 _PyUnicode_FromString("X"));
598604 } else {
....@@ -600,7 +606,7 @@
600606 _PyUnicode_FromString("-"));
601607 }
602608
603
- if (br->entries[i].flags.abort) {
609
+ if (entries[i].flags.abort) {
604610 pydict_set_item_string_decref(pyelem, "abort",
605611 _PyUnicode_FromString("A"));
606612 } else {
....@@ -630,9 +636,9 @@
630636
631637 static void set_sample_read_in_dict(PyObject *dict_sample,
632638 struct perf_sample *sample,
633
- struct perf_evsel *evsel)
639
+ struct evsel *evsel)
634640 {
635
- u64 read_format = evsel->attr.read_format;
641
+ u64 read_format = evsel->core.attr.read_format;
636642 PyObject *values;
637643 unsigned int i;
638644
....@@ -688,6 +694,9 @@
688694
689695 bf[0] = 0;
690696
697
+ if (!regs || !regs->regs)
698
+ return 0;
699
+
691700 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
692701 u64 val = regs->regs[i++];
693702
....@@ -701,9 +710,9 @@
701710
702711 static void set_regs_in_dict(PyObject *dict,
703712 struct perf_sample *sample,
704
- struct perf_evsel *evsel)
713
+ struct evsel *evsel)
705714 {
706
- struct perf_event_attr *attr = &evsel->attr;
715
+ struct perf_event_attr *attr = &evsel->core.attr;
707716 char bf[512];
708717
709718 regs_map(&sample->intr_regs, attr->sample_regs_intr, bf, sizeof(bf));
....@@ -718,7 +727,7 @@
718727 }
719728
720729 static PyObject *get_perf_sample_dict(struct perf_sample *sample,
721
- struct perf_evsel *evsel,
730
+ struct evsel *evsel,
722731 struct addr_location *al,
723732 PyObject *callchain)
724733 {
....@@ -732,8 +741,8 @@
732741 if (!dict_sample)
733742 Py_FatalError("couldn't create Python dictionary");
734743
735
- pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel)));
736
- pydict_set_item_string_decref(dict, "attr", _PyBytes_FromStringAndSize((const char *)&evsel->attr, sizeof(evsel->attr)));
744
+ pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(evsel__name(evsel)));
745
+ pydict_set_item_string_decref(dict, "attr", _PyBytes_FromStringAndSize((const char *)&evsel->core.attr, sizeof(evsel->core.attr)));
737746
738747 pydict_set_item_string_decref(dict_sample, "pid",
739748 _PyLong_FromLong(sample->pid));
....@@ -786,14 +795,14 @@
786795 }
787796
788797 static void python_process_tracepoint(struct perf_sample *sample,
789
- struct perf_evsel *evsel,
798
+ struct evsel *evsel,
790799 struct addr_location *al)
791800 {
792
- struct event_format *event = evsel->tp_format;
801
+ struct tep_event *event = evsel->tp_format;
793802 PyObject *handler, *context, *t, *obj = NULL, *callchain;
794803 PyObject *dict = NULL, *all_entries_dict = NULL;
795804 static char handler_name[256];
796
- struct format_field *field;
805
+ struct tep_format_field *field;
797806 unsigned long s, ns;
798807 unsigned n = 0;
799808 int pid;
....@@ -805,7 +814,7 @@
805814
806815 if (!event) {
807816 snprintf(handler_name, sizeof(handler_name),
808
- "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
817
+ "ug! no event found for type %" PRIu64, (u64)evsel->core.attr.config);
809818 Py_FatalError(handler_name);
810819 }
811820
....@@ -835,7 +844,7 @@
835844 ns = nsecs - s * NSEC_PER_SEC;
836845
837846 scripting_context->event_data = data;
838
- scripting_context->pevent = evsel->tp_format->pevent;
847
+ scripting_context->pevent = evsel->tp_format->tep;
839848
840849 context = _PyCapsule_New(scripting_context, NULL, NULL);
841850
....@@ -866,22 +875,22 @@
866875 unsigned int offset, len;
867876 unsigned long long val;
868877
869
- if (field->flags & FIELD_IS_ARRAY) {
878
+ if (field->flags & TEP_FIELD_IS_ARRAY) {
870879 offset = field->offset;
871880 len = field->size;
872
- if (field->flags & FIELD_IS_DYNAMIC) {
881
+ if (field->flags & TEP_FIELD_IS_DYNAMIC) {
873882 val = tep_read_number(scripting_context->pevent,
874883 data + offset, len);
875884 offset = val;
876885 len = offset >> 16;
877886 offset &= 0xffff;
878887 }
879
- if (field->flags & FIELD_IS_STRING &&
888
+ if (field->flags & TEP_FIELD_IS_STRING &&
880889 is_printable_array(data + offset, len)) {
881890 obj = _PyUnicode_FromString((char *) data + offset);
882891 } else {
883892 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
884
- field->flags &= ~FIELD_IS_STRING;
893
+ field->flags &= ~TEP_FIELD_IS_STRING;
885894 }
886895 } else { /* FIELD_IS_NUMERIC */
887896 obj = get_field_numeric_entry(event, field, data);
....@@ -925,13 +934,29 @@
925934 return t;
926935 }
927936
928
-static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
937
+static int tuple_set_s64(PyObject *t, unsigned int pos, s64 val)
929938 {
930939 #if BITS_PER_LONG == 64
931940 return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
932941 #endif
933942 #if BITS_PER_LONG == 32
934943 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
944
+#endif
945
+}
946
+
947
+/*
948
+ * Databases support only signed 64-bit numbers, so even though we are
949
+ * exporting a u64, it must be as s64.
950
+ */
951
+#define tuple_set_d64 tuple_set_s64
952
+
953
+static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
954
+{
955
+#if BITS_PER_LONG == 64
956
+ return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLong(val));
957
+#endif
958
+#if BITS_PER_LONG == 32
959
+ return PyTuple_SetItem(t, pos, PyLong_FromUnsignedLongLong(val));
935960 #endif
936961 }
937962
....@@ -945,15 +970,21 @@
945970 return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s));
946971 }
947972
948
-static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
973
+static int tuple_set_bytes(PyObject *t, unsigned int pos, void *bytes,
974
+ unsigned int sz)
975
+{
976
+ return PyTuple_SetItem(t, pos, _PyBytes_FromStringAndSize(bytes, sz));
977
+}
978
+
979
+static int python_export_evsel(struct db_export *dbe, struct evsel *evsel)
949980 {
950981 struct tables *tables = container_of(dbe, struct tables, dbe);
951982 PyObject *t;
952983
953984 t = tuple_new(2);
954985
955
- tuple_set_u64(t, 0, evsel->db_id);
956
- tuple_set_string(t, 1, perf_evsel__name(evsel));
986
+ tuple_set_d64(t, 0, evsel->db_id);
987
+ tuple_set_string(t, 1, evsel__name(evsel));
957988
958989 call_object(tables->evsel_handler, t, "evsel_table");
959990
....@@ -970,7 +1001,7 @@
9701001
9711002 t = tuple_new(3);
9721003
973
- tuple_set_u64(t, 0, machine->db_id);
1004
+ tuple_set_d64(t, 0, machine->db_id);
9741005 tuple_set_s32(t, 1, machine->pid);
9751006 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
9761007
....@@ -989,9 +1020,9 @@
9891020
9901021 t = tuple_new(5);
9911022
992
- tuple_set_u64(t, 0, thread->db_id);
993
- tuple_set_u64(t, 1, machine->db_id);
994
- tuple_set_u64(t, 2, main_thread_db_id);
1023
+ tuple_set_d64(t, 0, thread->db_id);
1024
+ tuple_set_d64(t, 1, machine->db_id);
1025
+ tuple_set_d64(t, 2, main_thread_db_id);
9951026 tuple_set_s32(t, 3, thread->pid_);
9961027 tuple_set_s32(t, 4, thread->tid);
9971028
....@@ -1002,15 +1033,19 @@
10021033 return 0;
10031034 }
10041035
1005
-static int python_export_comm(struct db_export *dbe, struct comm *comm)
1036
+static int python_export_comm(struct db_export *dbe, struct comm *comm,
1037
+ struct thread *thread)
10061038 {
10071039 struct tables *tables = container_of(dbe, struct tables, dbe);
10081040 PyObject *t;
10091041
1010
- t = tuple_new(2);
1042
+ t = tuple_new(5);
10111043
1012
- tuple_set_u64(t, 0, comm->db_id);
1044
+ tuple_set_d64(t, 0, comm->db_id);
10131045 tuple_set_string(t, 1, comm__str(comm));
1046
+ tuple_set_d64(t, 2, thread->db_id);
1047
+ tuple_set_d64(t, 3, comm->start);
1048
+ tuple_set_s32(t, 4, comm->exec);
10141049
10151050 call_object(tables->comm_handler, t, "comm_table");
10161051
....@@ -1027,9 +1062,9 @@
10271062
10281063 t = tuple_new(3);
10291064
1030
- tuple_set_u64(t, 0, db_id);
1031
- tuple_set_u64(t, 1, comm->db_id);
1032
- tuple_set_u64(t, 2, thread->db_id);
1065
+ tuple_set_d64(t, 0, db_id);
1066
+ tuple_set_d64(t, 1, comm->db_id);
1067
+ tuple_set_d64(t, 2, thread->db_id);
10331068
10341069 call_object(tables->comm_thread_handler, t, "comm_thread_table");
10351070
....@@ -1045,12 +1080,12 @@
10451080 char sbuild_id[SBUILD_ID_SIZE];
10461081 PyObject *t;
10471082
1048
- build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1083
+ build_id__sprintf(&dso->bid, sbuild_id);
10491084
10501085 t = tuple_new(5);
10511086
1052
- tuple_set_u64(t, 0, dso->db_id);
1053
- tuple_set_u64(t, 1, machine->db_id);
1087
+ tuple_set_d64(t, 0, dso->db_id);
1088
+ tuple_set_d64(t, 1, machine->db_id);
10541089 tuple_set_string(t, 2, dso->short_name);
10551090 tuple_set_string(t, 3, dso->long_name);
10561091 tuple_set_string(t, 4, sbuild_id);
....@@ -1071,10 +1106,10 @@
10711106
10721107 t = tuple_new(6);
10731108
1074
- tuple_set_u64(t, 0, *sym_db_id);
1075
- tuple_set_u64(t, 1, dso->db_id);
1076
- tuple_set_u64(t, 2, sym->start);
1077
- tuple_set_u64(t, 3, sym->end);
1109
+ tuple_set_d64(t, 0, *sym_db_id);
1110
+ tuple_set_d64(t, 1, dso->db_id);
1111
+ tuple_set_d64(t, 2, sym->start);
1112
+ tuple_set_d64(t, 3, sym->end);
10781113 tuple_set_s32(t, 4, sym->binding);
10791114 tuple_set_string(t, 5, sym->name);
10801115
....@@ -1103,40 +1138,69 @@
11031138 return 0;
11041139 }
11051140
1106
-static int python_export_sample(struct db_export *dbe,
1107
- struct export_sample *es)
1141
+static void python_export_sample_table(struct db_export *dbe,
1142
+ struct export_sample *es)
11081143 {
11091144 struct tables *tables = container_of(dbe, struct tables, dbe);
11101145 PyObject *t;
11111146
1112
- t = tuple_new(22);
1147
+ t = tuple_new(24);
11131148
1114
- tuple_set_u64(t, 0, es->db_id);
1115
- tuple_set_u64(t, 1, es->evsel->db_id);
1116
- tuple_set_u64(t, 2, es->al->machine->db_id);
1117
- tuple_set_u64(t, 3, es->al->thread->db_id);
1118
- tuple_set_u64(t, 4, es->comm_db_id);
1119
- tuple_set_u64(t, 5, es->dso_db_id);
1120
- tuple_set_u64(t, 6, es->sym_db_id);
1121
- tuple_set_u64(t, 7, es->offset);
1122
- tuple_set_u64(t, 8, es->sample->ip);
1123
- tuple_set_u64(t, 9, es->sample->time);
1149
+ tuple_set_d64(t, 0, es->db_id);
1150
+ tuple_set_d64(t, 1, es->evsel->db_id);
1151
+ tuple_set_d64(t, 2, es->al->maps->machine->db_id);
1152
+ tuple_set_d64(t, 3, es->al->thread->db_id);
1153
+ tuple_set_d64(t, 4, es->comm_db_id);
1154
+ tuple_set_d64(t, 5, es->dso_db_id);
1155
+ tuple_set_d64(t, 6, es->sym_db_id);
1156
+ tuple_set_d64(t, 7, es->offset);
1157
+ tuple_set_d64(t, 8, es->sample->ip);
1158
+ tuple_set_d64(t, 9, es->sample->time);
11241159 tuple_set_s32(t, 10, es->sample->cpu);
1125
- tuple_set_u64(t, 11, es->addr_dso_db_id);
1126
- tuple_set_u64(t, 12, es->addr_sym_db_id);
1127
- tuple_set_u64(t, 13, es->addr_offset);
1128
- tuple_set_u64(t, 14, es->sample->addr);
1129
- tuple_set_u64(t, 15, es->sample->period);
1130
- tuple_set_u64(t, 16, es->sample->weight);
1131
- tuple_set_u64(t, 17, es->sample->transaction);
1132
- tuple_set_u64(t, 18, es->sample->data_src);
1160
+ tuple_set_d64(t, 11, es->addr_dso_db_id);
1161
+ tuple_set_d64(t, 12, es->addr_sym_db_id);
1162
+ tuple_set_d64(t, 13, es->addr_offset);
1163
+ tuple_set_d64(t, 14, es->sample->addr);
1164
+ tuple_set_d64(t, 15, es->sample->period);
1165
+ tuple_set_d64(t, 16, es->sample->weight);
1166
+ tuple_set_d64(t, 17, es->sample->transaction);
1167
+ tuple_set_d64(t, 18, es->sample->data_src);
11331168 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
11341169 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
1135
- tuple_set_u64(t, 21, es->call_path_id);
1170
+ tuple_set_d64(t, 21, es->call_path_id);
1171
+ tuple_set_d64(t, 22, es->sample->insn_cnt);
1172
+ tuple_set_d64(t, 23, es->sample->cyc_cnt);
11361173
11371174 call_object(tables->sample_handler, t, "sample_table");
11381175
11391176 Py_DECREF(t);
1177
+}
1178
+
1179
+static void python_export_synth(struct db_export *dbe, struct export_sample *es)
1180
+{
1181
+ struct tables *tables = container_of(dbe, struct tables, dbe);
1182
+ PyObject *t;
1183
+
1184
+ t = tuple_new(3);
1185
+
1186
+ tuple_set_d64(t, 0, es->db_id);
1187
+ tuple_set_d64(t, 1, es->evsel->core.attr.config);
1188
+ tuple_set_bytes(t, 2, es->sample->raw_data, es->sample->raw_size);
1189
+
1190
+ call_object(tables->synth_handler, t, "synth_data");
1191
+
1192
+ Py_DECREF(t);
1193
+}
1194
+
1195
+static int python_export_sample(struct db_export *dbe,
1196
+ struct export_sample *es)
1197
+{
1198
+ struct tables *tables = container_of(dbe, struct tables, dbe);
1199
+
1200
+ python_export_sample_table(dbe, es);
1201
+
1202
+ if (es->evsel->core.attr.type == PERF_TYPE_SYNTH && tables->synth_handler)
1203
+ python_export_synth(dbe, es);
11401204
11411205 return 0;
11421206 }
....@@ -1152,10 +1216,10 @@
11521216
11531217 t = tuple_new(4);
11541218
1155
- tuple_set_u64(t, 0, cp->db_id);
1156
- tuple_set_u64(t, 1, parent_db_id);
1157
- tuple_set_u64(t, 2, sym_db_id);
1158
- tuple_set_u64(t, 3, cp->ip);
1219
+ tuple_set_d64(t, 0, cp->db_id);
1220
+ tuple_set_d64(t, 1, parent_db_id);
1221
+ tuple_set_d64(t, 2, sym_db_id);
1222
+ tuple_set_d64(t, 3, cp->ip);
11591223
11601224 call_object(tables->call_path_handler, t, "call_path_table");
11611225
....@@ -1171,19 +1235,22 @@
11711235 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
11721236 PyObject *t;
11731237
1174
- t = tuple_new(11);
1238
+ t = tuple_new(14);
11751239
1176
- tuple_set_u64(t, 0, cr->db_id);
1177
- tuple_set_u64(t, 1, cr->thread->db_id);
1178
- tuple_set_u64(t, 2, comm_db_id);
1179
- tuple_set_u64(t, 3, cr->cp->db_id);
1180
- tuple_set_u64(t, 4, cr->call_time);
1181
- tuple_set_u64(t, 5, cr->return_time);
1182
- tuple_set_u64(t, 6, cr->branch_count);
1183
- tuple_set_u64(t, 7, cr->call_ref);
1184
- tuple_set_u64(t, 8, cr->return_ref);
1185
- tuple_set_u64(t, 9, cr->cp->parent->db_id);
1240
+ tuple_set_d64(t, 0, cr->db_id);
1241
+ tuple_set_d64(t, 1, cr->thread->db_id);
1242
+ tuple_set_d64(t, 2, comm_db_id);
1243
+ tuple_set_d64(t, 3, cr->cp->db_id);
1244
+ tuple_set_d64(t, 4, cr->call_time);
1245
+ tuple_set_d64(t, 5, cr->return_time);
1246
+ tuple_set_d64(t, 6, cr->branch_count);
1247
+ tuple_set_d64(t, 7, cr->call_ref);
1248
+ tuple_set_d64(t, 8, cr->return_ref);
1249
+ tuple_set_d64(t, 9, cr->cp->parent->db_id);
11861250 tuple_set_s32(t, 10, cr->flags);
1251
+ tuple_set_d64(t, 11, cr->parent_db_id);
1252
+ tuple_set_d64(t, 12, cr->insn_count);
1253
+ tuple_set_d64(t, 13, cr->cyc_count);
11871254
11881255 call_object(tables->call_return_handler, t, "call_return_table");
11891256
....@@ -1192,15 +1259,44 @@
11921259 return 0;
11931260 }
11941261
1195
-static int python_process_call_return(struct call_return *cr, void *data)
1262
+static int python_export_context_switch(struct db_export *dbe, u64 db_id,
1263
+ struct machine *machine,
1264
+ struct perf_sample *sample,
1265
+ u64 th_out_id, u64 comm_out_id,
1266
+ u64 th_in_id, u64 comm_in_id, int flags)
1267
+{
1268
+ struct tables *tables = container_of(dbe, struct tables, dbe);
1269
+ PyObject *t;
1270
+
1271
+ t = tuple_new(9);
1272
+
1273
+ tuple_set_d64(t, 0, db_id);
1274
+ tuple_set_d64(t, 1, machine->db_id);
1275
+ tuple_set_d64(t, 2, sample->time);
1276
+ tuple_set_s32(t, 3, sample->cpu);
1277
+ tuple_set_d64(t, 4, th_out_id);
1278
+ tuple_set_d64(t, 5, comm_out_id);
1279
+ tuple_set_d64(t, 6, th_in_id);
1280
+ tuple_set_d64(t, 7, comm_in_id);
1281
+ tuple_set_s32(t, 8, flags);
1282
+
1283
+ call_object(tables->context_switch_handler, t, "context_switch");
1284
+
1285
+ Py_DECREF(t);
1286
+
1287
+ return 0;
1288
+}
1289
+
1290
+static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
1291
+ void *data)
11961292 {
11971293 struct db_export *dbe = data;
11981294
1199
- return db_export__call_return(dbe, cr);
1295
+ return db_export__call_return(dbe, cr, parent_db_id);
12001296 }
12011297
12021298 static void python_process_general_event(struct perf_sample *sample,
1203
- struct perf_evsel *evsel,
1299
+ struct evsel *evsel,
12041300 struct addr_location *al)
12051301 {
12061302 PyObject *handler, *t, *dict, *callchain;
....@@ -1236,12 +1332,12 @@
12361332
12371333 static void python_process_event(union perf_event *event,
12381334 struct perf_sample *sample,
1239
- struct perf_evsel *evsel,
1335
+ struct evsel *evsel,
12401336 struct addr_location *al)
12411337 {
12421338 struct tables *tables = &tables_global;
12431339
1244
- switch (evsel->attr.type) {
1340
+ switch (evsel->core.attr.type) {
12451341 case PERF_TYPE_TRACEPOINT:
12461342 python_process_tracepoint(sample, evsel, al);
12471343 break;
....@@ -1254,12 +1350,22 @@
12541350 }
12551351 }
12561352
1353
+static void python_process_switch(union perf_event *event,
1354
+ struct perf_sample *sample,
1355
+ struct machine *machine)
1356
+{
1357
+ struct tables *tables = &tables_global;
1358
+
1359
+ if (tables->db_export_mode)
1360
+ db_export__switch(&tables->dbe, event, sample, machine);
1361
+}
1362
+
12571363 static void get_handler_name(char *str, size_t size,
1258
- struct perf_evsel *evsel)
1364
+ struct evsel *evsel)
12591365 {
12601366 char *p = str;
12611367
1262
- scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
1368
+ scnprintf(str, size, "stat__%s", evsel__name(evsel));
12631369
12641370 while ((p = strchr(p, ':'))) {
12651371 *p = '_';
....@@ -1268,7 +1374,7 @@
12681374 }
12691375
12701376 static void
1271
-process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
1377
+process_stat(struct evsel *counter, int cpu, int thread, u64 tstamp,
12721378 struct perf_counts_values *count)
12731379 {
12741380 PyObject *handler, *t;
....@@ -1305,10 +1411,10 @@
13051411 }
13061412
13071413 static void python_process_stat(struct perf_stat_config *config,
1308
- struct perf_evsel *counter, u64 tstamp)
1414
+ struct evsel *counter, u64 tstamp)
13091415 {
1310
- struct thread_map *threads = counter->threads;
1311
- struct cpu_map *cpus = counter->cpus;
1416
+ struct perf_thread_map *threads = counter->core.threads;
1417
+ struct perf_cpu_map *cpus = counter->core.cpus;
13121418 int cpu, thread;
13131419
13141420 if (config->aggr_mode == AGGR_GLOBAL) {
....@@ -1320,7 +1426,7 @@
13201426 for (thread = 0; thread < threads->nr; thread++) {
13211427 for (cpu = 0; cpu < cpus->nr; cpu++) {
13221428 process_stat(counter, cpus->map[cpu],
1323
- thread_map__pid(threads, thread), tstamp,
1429
+ perf_thread_map__pid(threads, thread), tstamp,
13241430 perf_counts(counter->counts, cpu, thread));
13251431 }
13261432 }
....@@ -1469,6 +1575,15 @@
14691575 SET_TABLE_HANDLER(sample);
14701576 SET_TABLE_HANDLER(call_path);
14711577 SET_TABLE_HANDLER(call_return);
1578
+ SET_TABLE_HANDLER(context_switch);
1579
+
1580
+ /*
1581
+ * Synthesized events are samples but with architecture-specific data
1582
+ * stored in sample->raw_data. They are exported via
1583
+ * python_export_sample() and consequently do not need a separate export
1584
+ * callback.
1585
+ */
1586
+ tables->synth_handler = get_handler("synth_data");
14721587 }
14731588
14741589 #if PY_MAJOR_VERSION < 3
....@@ -1567,9 +1682,7 @@
15671682
15681683 static int python_flush_script(void)
15691684 {
1570
- struct tables *tables = &tables_global;
1571
-
1572
- return db_export__flush(&tables->dbe);
1685
+ return 0;
15731686 }
15741687
15751688 /*
....@@ -1592,10 +1705,11 @@
15921705
15931706 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
15941707 {
1595
- struct event_format *event = NULL;
1596
- struct format_field *f;
1708
+ int i, not_first, count, nr_events;
1709
+ struct tep_event **all_events;
1710
+ struct tep_event *event = NULL;
1711
+ struct tep_format_field *f;
15971712 char fname[PATH_MAX];
1598
- int not_first, count;
15991713 FILE *ofp;
16001714
16011715 sprintf(fname, "%s.py", outfile);
....@@ -1640,7 +1754,11 @@
16401754 fprintf(ofp, "def trace_end():\n");
16411755 fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
16421756
1643
- while ((event = trace_find_next_event(pevent, event))) {
1757
+ nr_events = tep_get_events_count(pevent);
1758
+ all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
1759
+
1760
+ for (i = 0; all_events && i < nr_events; i++) {
1761
+ event = all_events[i];
16441762 fprintf(ofp, "def %s__%s(", event->system, event->name);
16451763 fprintf(ofp, "event_name, ");
16461764 fprintf(ofp, "context, ");
....@@ -1688,12 +1806,12 @@
16881806 count++;
16891807
16901808 fprintf(ofp, "%s=", f->name);
1691
- if (f->flags & FIELD_IS_STRING ||
1692
- f->flags & FIELD_IS_FLAG ||
1693
- f->flags & FIELD_IS_ARRAY ||
1694
- f->flags & FIELD_IS_SYMBOLIC)
1809
+ if (f->flags & TEP_FIELD_IS_STRING ||
1810
+ f->flags & TEP_FIELD_IS_FLAG ||
1811
+ f->flags & TEP_FIELD_IS_ARRAY ||
1812
+ f->flags & TEP_FIELD_IS_SYMBOLIC)
16951813 fprintf(ofp, "%%s");
1696
- else if (f->flags & FIELD_IS_SIGNED)
1814
+ else if (f->flags & TEP_FIELD_IS_SIGNED)
16971815 fprintf(ofp, "%%d");
16981816 else
16991817 fprintf(ofp, "%%u");
....@@ -1711,7 +1829,7 @@
17111829 if (++count % 5 == 0)
17121830 fprintf(ofp, "\n\t\t");
17131831
1714
- if (f->flags & FIELD_IS_FLAG) {
1832
+ if (f->flags & TEP_FIELD_IS_FLAG) {
17151833 if ((count - 1) % 5 != 0) {
17161834 fprintf(ofp, "\n\t\t");
17171835 count = 4;
....@@ -1721,7 +1839,7 @@
17211839 event->name);
17221840 fprintf(ofp, "\"%s\", %s)", f->name,
17231841 f->name);
1724
- } else if (f->flags & FIELD_IS_SYMBOLIC) {
1842
+ } else if (f->flags & TEP_FIELD_IS_SYMBOLIC) {
17251843 if ((count - 1) % 5 != 0) {
17261844 fprintf(ofp, "\n\t\t");
17271845 count = 4;
....@@ -1778,6 +1896,7 @@
17781896 .flush_script = python_flush_script,
17791897 .stop_script = python_stop_script,
17801898 .process_event = python_process_event,
1899
+ .process_switch = python_process_switch,
17811900 .process_stat = python_process_stat,
17821901 .process_stat_interval = python_process_stat_interval,
17831902 .generate_script = python_generate_script,