Lines Matching +full:convert +full:- +full:sample +full:- +full:format

1 // SPDX-License-Identifier: GPL-2.0-only
14 #include <babeltrace/ctf-writer/writer.h>
15 #include <babeltrace/ctf-writer/clock.h>
16 #include <babeltrace/ctf-writer/stream.h>
17 #include <babeltrace/ctf-writer/event.h>
18 #include <babeltrace/ctf-writer/event-types.h>
19 #include <babeltrace/ctf-writer/event-fields.h>
20 #include <babeltrace/ctf-ir/utils.h>
23 #include "data-convert.h"
36 #include "util/sample.h"
39 #include <event-parse.h>
90 struct convert { struct
113 return -1; in value_set()
149 struct bt_ctf_field_type *type = cw->data._name; \
166 struct bt_ctf_field_type *type = cw->data.string; in value_set_string()
173 return -1; in value_set_string()
194 unsigned long flags = field->flags; in get_tracepoint_field_type()
197 return cw->data.string; in get_tracepoint_field_type()
202 return cw->data.u64_hex; in get_tracepoint_field_type()
206 if (field->size == 8) in get_tracepoint_field_type()
207 return cw->data.s64; in get_tracepoint_field_type()
209 return cw->data.s32; in get_tracepoint_field_type()
212 if (field->size == 8) in get_tracepoint_field_type()
213 return cw->data.u64; in get_tracepoint_field_type()
215 return cw->data.u32; in get_tracepoint_field_type()
223 * value_mask = (1 << (size * 8 - 1)) - 1. in adjust_signedness()
248 if ((value_int & (~0ULL - value_mask)) == 0) in adjust_signedness()
273 buffer = zalloc(i + (len - i) * 4 + 2); in string_set_value()
276 return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING"); in string_set_value()
296 struct perf_sample *sample, in add_tracepoint_field_value() argument
302 const char *name = fmtf->name; in add_tracepoint_field_value()
303 void *data = sample->raw_data; in add_tracepoint_field_value()
304 unsigned long flags = fmtf->flags; in add_tracepoint_field_value()
311 name = fmtf->alias; in add_tracepoint_field_value()
312 offset = fmtf->offset; in add_tracepoint_field_value()
313 len = fmtf->size; in add_tracepoint_field_value()
320 tmp_val = tep_read_number(fmtf->event->tep, in add_tracepoint_field_value()
326 offset += fmtf->offset + fmtf->size; in add_tracepoint_field_value()
337 return -1; in add_tracepoint_field_value()
340 len = fmtf->size / fmtf->arraylen; in add_tracepoint_field_value()
341 n_items = fmtf->arraylen; in add_tracepoint_field_value()
357 return -1; in add_tracepoint_field_value()
366 fmtf->event->tep, in add_tracepoint_field_value()
394 return -1; in add_tracepoint_field_value()
402 return -1; in add_tracepoint_field_value()
409 struct perf_sample *sample) in add_tracepoint_fields_values() argument
414 for (field = fields; field; field = field->next) { in add_tracepoint_fields_values()
415 ret = add_tracepoint_field_value(cw, event_class, event, sample, in add_tracepoint_fields_values()
418 return -1; in add_tracepoint_fields_values()
427 struct perf_sample *sample) in add_tracepoint_values() argument
430 struct tep_format_field *common_fields = tp_format->format.common_fields; in add_tracepoint_values()
431 struct tep_format_field *fields = tp_format->format.fields; in add_tracepoint_values()
435 common_fields, sample); in add_tracepoint_values()
438 fields, sample); in add_tracepoint_values()
446 struct perf_sample *sample) in add_bpf_output_values() argument
450 unsigned int raw_size = sample->raw_size; in add_bpf_output_values()
457 raw_size, nr_elements * sizeof(u32) - raw_size); in add_bpf_output_values()
463 ret = -1; in add_bpf_output_values()
482 ret = -1; in add_bpf_output_values()
497 ((u32 *)(sample->raw_data))[i]); in add_bpf_output_values()
528 unsigned int nr_elements = callchain->nr; in add_callchain_output_values()
537 ret = -1; in add_callchain_output_values()
557 ret = -1; in add_callchain_output_values()
572 ((u64 *)(callchain->ips))[i]); in add_callchain_output_values()
599 struct perf_sample *sample) in add_generic_values() argument
601 u64 type = evsel->core.attr.sample_type; in add_generic_values()
606 * PERF_SAMPLE_TIME - not needed as we have it in in add_generic_values()
608 * PERF_SAMPLE_READ - TODO in add_generic_values()
609 * PERF_SAMPLE_RAW - tracepoint fields are handled separately in add_generic_values()
610 * PERF_SAMPLE_BRANCH_STACK - TODO in add_generic_values()
611 * PERF_SAMPLE_REGS_USER - TODO in add_generic_values()
612 * PERF_SAMPLE_STACK_USER - TODO in add_generic_values()
616 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip); in add_generic_values()
618 return -1; in add_generic_values()
622 ret = value_set_s32(cw, event, "perf_tid", sample->tid); in add_generic_values()
624 return -1; in add_generic_values()
626 ret = value_set_s32(cw, event, "perf_pid", sample->pid); in add_generic_values()
628 return -1; in add_generic_values()
633 ret = value_set_u64(cw, event, "perf_id", sample->id); in add_generic_values()
635 return -1; in add_generic_values()
639 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id); in add_generic_values()
641 return -1; in add_generic_values()
645 ret = value_set_u64(cw, event, "perf_period", sample->period); in add_generic_values()
647 return -1; in add_generic_values()
651 ret = value_set_u64(cw, event, "perf_weight", sample->weight); in add_generic_values()
653 return -1; in add_generic_values()
658 sample->data_src); in add_generic_values()
660 return -1; in add_generic_values()
665 sample->transaction); in add_generic_values()
667 return -1; in add_generic_values()
678 err = bt_ctf_stream_flush(cs->stream); in ctf_stream__flush()
680 pr_err("CTF stream %d flush failed\n", cs->cpu); in ctf_stream__flush()
683 cs->cpu, cs->count); in ctf_stream__flush()
685 cs->count = 0; in ctf_stream__flush()
705 stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class); in ctf_stream__create()
732 cs->cpu = cpu; in ctf_stream__create()
733 cs->stream = stream; in ctf_stream__create()
749 bt_ctf_stream_put(cs->stream); in ctf_stream__delete()
756 struct ctf_stream *cs = cw->stream[cpu]; in ctf_stream()
760 cw->stream[cpu] = cs; in ctf_stream()
766 static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample, in get_sample_cpu() argument
771 if (evsel->core.attr.sample_type & PERF_SAMPLE_CPU) in get_sample_cpu()
772 cpu = sample->cpu; in get_sample_cpu()
774 if (cpu > cw->stream_cnt) { in get_sample_cpu()
776 cpu, cw->stream_cnt); in get_sample_cpu()
793 return cs->count >= STREAM_FLUSH_COUNT; in is_flush_needed()
798 struct perf_sample *sample, in process_sample_event() argument
802 struct convert *c = container_of(tool, struct convert, tool); in process_sample_event()
803 struct evsel_priv *priv = evsel->priv; in process_sample_event()
804 struct ctf_writer *cw = &c->writer; in process_sample_event()
809 unsigned long type = evsel->core.attr.sample_type; in process_sample_event()
814 event_class = priv->event_class; in process_sample_event()
817 c->events_count++; in process_sample_event()
818 c->events_size += _event->header.size; in process_sample_event()
820 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count); in process_sample_event()
825 return -1; in process_sample_event()
828 bt_ctf_clock_set_time(cw->clock, sample->time); in process_sample_event()
830 ret = add_generic_values(cw, event, evsel, sample); in process_sample_event()
832 return -1; in process_sample_event()
834 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { in process_sample_event()
836 evsel, sample); in process_sample_event()
838 return -1; in process_sample_event()
843 event, sample->callchain); in process_sample_event()
845 return -1; in process_sample_event()
849 ret = add_bpf_output_values(event_class, event, sample); in process_sample_event()
851 return -1; in process_sample_event()
854 cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel)); in process_sample_event()
859 cs->count++; in process_sample_event()
860 bt_ctf_stream_append_event(cs->stream, event); in process_sample_event()
864 return cs ? 0 : -1; in process_sample_event()
869 ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
871 return -1; \
877 struct perf_sample *sample, \
880 struct convert *c = container_of(tool, struct convert, tool);\
881 struct ctf_writer *cw = &c->writer; \
882 struct bt_ctf_event_class *event_class = cw->_name##_class;\
887 c->non_sample_count++; \
888 c->events_size += _event->header.size; \
892 return -1; \
895 bt_ctf_clock_set_time(cw->clock, sample->time); \
902 cs->count++; \
903 bt_ctf_stream_append_event(cs->stream, event); \
907 return perf_event__process_##_name(tool, _event, sample, machine);\
991 if (field->alias != field->name) in event_class_add_field()
993 (char *)field->alias); in event_class_add_field()
995 name = field->name; in event_class_add_field()
999 name = change_name(name, field->name, -1); in event_class_add_field()
1003 return -1; in event_class_add_field()
1007 name = change_name(name, field->name, dup++); in event_class_add_field()
1009 pr_err("Failed to create dup name for '%s'\n", field->name); in event_class_add_field()
1010 return -1; in event_class_add_field()
1016 field->alias = name; in event_class_add_field()
1028 for (field = fields; field; field = field->next) { in add_tracepoint_fields_types()
1030 unsigned long flags = field->flags; in add_tracepoint_fields_types()
1032 pr2(" field '%s'\n", field->name); in add_tracepoint_fields_types()
1036 return -1; in add_tracepoint_fields_types()
1047 type = bt_ctf_field_type_array_create(type, field->arraylen); in add_tracepoint_fields_types()
1056 field->name, ret); in add_tracepoint_fields_types()
1057 return -1; in add_tracepoint_fields_types()
1069 struct tep_format_field *common_fields = tp_format ? tp_format->format.common_fields : NULL; in add_tracepoint_types()
1070 struct tep_format_field *fields = tp_format ? tp_format->format.fields : NULL; in add_tracepoint_types()
1083 struct bt_ctf_field_type *len_type = cw->data.u32; in add_bpf_output_types()
1084 struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex; in add_bpf_output_types()
1094 return -1; in add_bpf_output_types()
1102 u64 type = evsel->core.attr.sample_type; in add_generic_types()
1106 * PERF_SAMPLE_TIME - not needed as we have it in in add_generic_types()
1108 * PERF_SAMPLE_READ - TODO in add_generic_types()
1109 * PERF_SAMPLE_CALLCHAIN - TODO in add_generic_types()
1110 * PERF_SAMPLE_RAW - tracepoint fields and BPF output in add_generic_types()
1112 * PERF_SAMPLE_BRANCH_STACK - TODO in add_generic_types()
1113 * PERF_SAMPLE_REGS_USER - TODO in add_generic_types()
1114 * PERF_SAMPLE_STACK_USER - TODO in add_generic_types()
1122 return -1; \ in add_generic_types()
1127 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip"); in add_generic_types()
1130 ADD_FIELD(event_class, cw->data.s32, "perf_tid"); in add_generic_types()
1131 ADD_FIELD(event_class, cw->data.s32, "perf_pid"); in add_generic_types()
1136 ADD_FIELD(event_class, cw->data.u64, "perf_id"); in add_generic_types()
1139 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id"); in add_generic_types()
1142 ADD_FIELD(event_class, cw->data.u64, "perf_period"); in add_generic_types()
1145 ADD_FIELD(event_class, cw->data.u64, "perf_weight"); in add_generic_types()
1148 ADD_FIELD(event_class, cw->data.u64, "perf_data_src"); in add_generic_types()
1151 ADD_FIELD(event_class, cw->data.u64, "perf_transaction"); in add_generic_types()
1154 ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size"); in add_generic_types()
1157 cw->data.u64_hex, "perf_callchain_size"), in add_generic_types()
1172 pr("Adding event '%s' (type %d)\n", name, evsel->core.attr.type); in add_event()
1176 return -1; in add_event()
1182 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { in add_event()
1194 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class); in add_event()
1204 priv->event_class = event_class; in add_event()
1205 evsel->priv = priv; in add_event()
1211 return -1; in add_event()
1216 struct evlist *evlist = session->evlist; in setup_events()
1231 if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\
1233 return -1; \
1246 return -1; \
1249 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\
1255 cw->_name##_class = event_class; \
1323 struct evlist *evlist = session->evlist; in cleanup_events()
1329 priv = evsel->priv; in cleanup_events()
1330 bt_ctf_event_class_put(priv->event_class); in cleanup_events()
1331 zfree(&evsel->priv); in cleanup_events()
1335 session->evlist = NULL; in cleanup_events()
1341 struct perf_header *ph = &session->header; in setup_streams()
1348 ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS; in setup_streams()
1353 return -ENOMEM; in setup_streams()
1356 cw->stream = stream; in setup_streams()
1357 cw->stream_cnt = ncpus; in setup_streams()
1365 for (cpu = 0; cpu < cw->stream_cnt; cpu++) in free_streams()
1366 ctf_stream__delete(cw->stream[cpu]); in free_streams()
1368 zfree(&cw->stream); in free_streams()
1374 struct perf_header *header = &session->header; in ctf_writer__setup_env()
1375 struct bt_ctf_writer *writer = cw->writer; in ctf_writer__setup_env()
1380 return -1; \ in ctf_writer__setup_env()
1383 ADD("host", header->env.hostname); in ctf_writer__setup_env()
1385 ADD("release", header->env.os_release); in ctf_writer__setup_env()
1386 ADD("version", header->env.version); in ctf_writer__setup_env()
1387 ADD("machine", header->env.arch); in ctf_writer__setup_env()
1399 struct bt_ctf_clock *clock = cw->clock; in ctf_writer__setup_clock()
1404 struct perf_env *env = &session->header.env; in ctf_writer__setup_clock()
1406 if (!env->clock.enabled) { in ctf_writer__setup_clock()
1407 pr_err("Can't provide --tod time, missing clock data. " in ctf_writer__setup_clock()
1408 "Please record with -k/--clockid option.\n"); in ctf_writer__setup_clock()
1409 return -1; in ctf_writer__setup_clock()
1412 desc = clockid_name(env->clock.clockid); in ctf_writer__setup_clock()
1413 offset = env->clock.tod_ns - env->clock.clockid_ns; in ctf_writer__setup_clock()
1419 return -1; \ in ctf_writer__setup_clock()
1454 pr2("Created type: INTEGER %d-bit %ssigned %s\n", in create_int_type()
1467 for (i = 0; i < ARRAY_SIZE(cw->data.array); i++) in ctf_writer__cleanup_data()
1468 bt_ctf_field_type_put(cw->data.array[i]); in ctf_writer__cleanup_data()
1480 CREATE_INT_TYPE(cw->data.s64, 64, true, false); in ctf_writer__init_data()
1481 CREATE_INT_TYPE(cw->data.u64, 64, false, false); in ctf_writer__init_data()
1482 CREATE_INT_TYPE(cw->data.s32, 32, true, false); in ctf_writer__init_data()
1483 CREATE_INT_TYPE(cw->data.u32, 32, false, false); in ctf_writer__init_data()
1484 CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true); in ctf_writer__init_data()
1485 CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true); in ctf_writer__init_data()
1487 cw->data.string = bt_ctf_field_type_string_create(); in ctf_writer__init_data()
1488 if (cw->data.string) in ctf_writer__init_data()
1494 return -1; in ctf_writer__init_data()
1501 bt_ctf_clock_put(cw->clock); in ctf_writer__cleanup()
1503 bt_ctf_stream_class_put(cw->stream_class); in ctf_writer__cleanup()
1504 bt_ctf_writer_put(cw->writer); in ctf_writer__cleanup()
1524 cw->writer = writer; in ctf_writer__init()
1533 cw->clock = clock; in ctf_writer__init()
1547 cw->stream_class = stream_class; in ctf_writer__init()
1563 ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id"); in ctf_writer__init()
1580 return -1; in ctf_writer__init()
1587 for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++) in ctf_writer__flush_streams()
1588 ret = ctf_stream__flush(cw->stream[cpu]); in ctf_writer__flush_streams()
1595 struct convert *c = cb; in convert__config()
1597 if (!strcmp(var, "convert.queue-size")) in convert__config()
1598 return perf_config_u64(&c->queue_size, var, value); in convert__config()
1610 .force = opts->force, in bt_convert__perf2ctf()
1612 struct convert c = {}; in bt_convert__perf2ctf()
1617 c.tool.sample = process_sample_event; in bt_convert__perf2ctf()
1629 if (opts->all) { in bt_convert__perf2ctf()
1641 err = -1; in bt_convert__perf2ctf()
1648 if (ctf_writer__init(cw, path, session, opts->tod)) in bt_convert__perf2ctf()
1652 ordered_events__set_alloc_size(&session->ordered_events, in bt_convert__perf2ctf()
1664 if (opts->all && setup_non_sample_events(cw, session)) in bt_convert__perf2ctf()
1677 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n", in bt_convert__perf2ctf()
1681 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples", in bt_convert__perf2ctf()
1688 fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count); in bt_convert__perf2ctf()