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 <traceevent/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
429 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields; in add_tracepoint_values()
430 struct tep_format_field *fields = evsel->tp_format->format.fields; in add_tracepoint_values()
434 common_fields, sample); in add_tracepoint_values()
437 fields, sample); in add_tracepoint_values()
445 struct perf_sample *sample) in add_bpf_output_values() argument
449 unsigned int raw_size = sample->raw_size; in add_bpf_output_values()
456 raw_size, nr_elements * sizeof(u32) - raw_size); in add_bpf_output_values()
462 ret = -1; in add_bpf_output_values()
481 ret = -1; in add_bpf_output_values()
496 ((u32 *)(sample->raw_data))[i]); in add_bpf_output_values()
527 unsigned int nr_elements = callchain->nr; in add_callchain_output_values()
536 ret = -1; in add_callchain_output_values()
556 ret = -1; in add_callchain_output_values()
571 ((u64 *)(callchain->ips))[i]); in add_callchain_output_values()
598 struct perf_sample *sample) in add_generic_values() argument
600 u64 type = evsel->core.attr.sample_type; in add_generic_values()
605 * PERF_SAMPLE_TIME - not needed as we have it in in add_generic_values()
607 * PERF_SAMPLE_READ - TODO in add_generic_values()
608 * PERF_SAMPLE_RAW - tracepoint fields are handled separately in add_generic_values()
609 * PERF_SAMPLE_BRANCH_STACK - TODO in add_generic_values()
610 * PERF_SAMPLE_REGS_USER - TODO in add_generic_values()
611 * PERF_SAMPLE_STACK_USER - TODO in add_generic_values()
615 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip); in add_generic_values()
617 return -1; in add_generic_values()
621 ret = value_set_s32(cw, event, "perf_tid", sample->tid); in add_generic_values()
623 return -1; in add_generic_values()
625 ret = value_set_s32(cw, event, "perf_pid", sample->pid); in add_generic_values()
627 return -1; in add_generic_values()
632 ret = value_set_u64(cw, event, "perf_id", sample->id); in add_generic_values()
634 return -1; in add_generic_values()
638 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id); in add_generic_values()
640 return -1; in add_generic_values()
644 ret = value_set_u64(cw, event, "perf_period", sample->period); in add_generic_values()
646 return -1; in add_generic_values()
650 ret = value_set_u64(cw, event, "perf_weight", sample->weight); in add_generic_values()
652 return -1; in add_generic_values()
657 sample->data_src); in add_generic_values()
659 return -1; in add_generic_values()
664 sample->transaction); in add_generic_values()
666 return -1; in add_generic_values()
677 err = bt_ctf_stream_flush(cs->stream); in ctf_stream__flush()
679 pr_err("CTF stream %d flush failed\n", cs->cpu); in ctf_stream__flush()
682 cs->cpu, cs->count); in ctf_stream__flush()
684 cs->count = 0; in ctf_stream__flush()
704 stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class); in ctf_stream__create()
731 cs->cpu = cpu; in ctf_stream__create()
732 cs->stream = stream; in ctf_stream__create()
748 bt_ctf_stream_put(cs->stream); in ctf_stream__delete()
755 struct ctf_stream *cs = cw->stream[cpu]; in ctf_stream()
759 cw->stream[cpu] = cs; in ctf_stream()
765 static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample, in get_sample_cpu() argument
770 if (evsel->core.attr.sample_type & PERF_SAMPLE_CPU) in get_sample_cpu()
771 cpu = sample->cpu; in get_sample_cpu()
773 if (cpu > cw->stream_cnt) { in get_sample_cpu()
775 cpu, cw->stream_cnt); in get_sample_cpu()
792 return cs->count >= STREAM_FLUSH_COUNT; in is_flush_needed()
797 struct perf_sample *sample, in process_sample_event() argument
801 struct convert *c = container_of(tool, struct convert, tool); in process_sample_event()
802 struct evsel_priv *priv = evsel->priv; in process_sample_event()
803 struct ctf_writer *cw = &c->writer; in process_sample_event()
808 unsigned long type = evsel->core.attr.sample_type; in process_sample_event()
813 event_class = priv->event_class; in process_sample_event()
816 c->events_count++; in process_sample_event()
817 c->events_size += _event->header.size; in process_sample_event()
819 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count); in process_sample_event()
824 return -1; in process_sample_event()
827 bt_ctf_clock_set_time(cw->clock, sample->time); in process_sample_event()
829 ret = add_generic_values(cw, event, evsel, sample); in process_sample_event()
831 return -1; in process_sample_event()
833 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { in process_sample_event()
835 evsel, sample); in process_sample_event()
837 return -1; in process_sample_event()
842 event, sample->callchain); in process_sample_event()
844 return -1; in process_sample_event()
848 ret = add_bpf_output_values(event_class, event, sample); in process_sample_event()
850 return -1; in process_sample_event()
853 cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel)); in process_sample_event()
858 cs->count++; in process_sample_event()
859 bt_ctf_stream_append_event(cs->stream, event); in process_sample_event()
863 return cs ? 0 : -1; in process_sample_event()
868 ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
870 return -1; \
876 struct perf_sample *sample, \
879 struct convert *c = container_of(tool, struct convert, tool);\
880 struct ctf_writer *cw = &c->writer; \
881 struct bt_ctf_event_class *event_class = cw->_name##_class;\
886 c->non_sample_count++; \
887 c->events_size += _event->header.size; \
891 return -1; \
894 bt_ctf_clock_set_time(cw->clock, sample->time); \
901 cs->count++; \
902 bt_ctf_stream_append_event(cs->stream, event); \
906 return perf_event__process_##_name(tool, _event, sample, machine);\
990 if (field->alias != field->name) in event_class_add_field()
992 (char *)field->alias); in event_class_add_field()
994 name = field->name; in event_class_add_field()
998 name = change_name(name, field->name, -1); in event_class_add_field()
1002 return -1; in event_class_add_field()
1006 name = change_name(name, field->name, dup++); in event_class_add_field()
1008 pr_err("Failed to create dup name for '%s'\n", field->name); in event_class_add_field()
1009 return -1; in event_class_add_field()
1015 field->alias = name; in event_class_add_field()
1027 for (field = fields; field; field = field->next) { in add_tracepoint_fields_types()
1029 unsigned long flags = field->flags; in add_tracepoint_fields_types()
1031 pr2(" field '%s'\n", field->name); in add_tracepoint_fields_types()
1035 return -1; in add_tracepoint_fields_types()
1046 type = bt_ctf_field_type_array_create(type, field->arraylen); in add_tracepoint_fields_types()
1055 field->name, ret); in add_tracepoint_fields_types()
1056 return -1; in add_tracepoint_fields_types()
1067 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields; in add_tracepoint_types()
1068 struct tep_format_field *fields = evsel->tp_format->format.fields; in add_tracepoint_types()
1081 struct bt_ctf_field_type *len_type = cw->data.u32; in add_bpf_output_types()
1082 struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex; in add_bpf_output_types()
1092 return -1; in add_bpf_output_types()
1100 u64 type = evsel->core.attr.sample_type; in add_generic_types()
1104 * PERF_SAMPLE_TIME - not needed as we have it in in add_generic_types()
1106 * PERF_SAMPLE_READ - TODO in add_generic_types()
1107 * PERF_SAMPLE_CALLCHAIN - TODO in add_generic_types()
1108 * PERF_SAMPLE_RAW - tracepoint fields and BPF output in add_generic_types()
1110 * PERF_SAMPLE_BRANCH_STACK - TODO in add_generic_types()
1111 * PERF_SAMPLE_REGS_USER - TODO in add_generic_types()
1112 * PERF_SAMPLE_STACK_USER - TODO in add_generic_types()
1120 return -1; \ in add_generic_types()
1125 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip"); in add_generic_types()
1128 ADD_FIELD(event_class, cw->data.s32, "perf_tid"); in add_generic_types()
1129 ADD_FIELD(event_class, cw->data.s32, "perf_pid"); in add_generic_types()
1134 ADD_FIELD(event_class, cw->data.u64, "perf_id"); in add_generic_types()
1137 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id"); in add_generic_types()
1140 ADD_FIELD(event_class, cw->data.u64, "perf_period"); in add_generic_types()
1143 ADD_FIELD(event_class, cw->data.u64, "perf_weight"); in add_generic_types()
1146 ADD_FIELD(event_class, cw->data.u64, "perf_data_src"); in add_generic_types()
1149 ADD_FIELD(event_class, cw->data.u64, "perf_transaction"); in add_generic_types()
1152 ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size"); in add_generic_types()
1155 cw->data.u64_hex, "perf_callchain_size"), in add_generic_types()
1170 pr("Adding event '%s' (type %d)\n", name, evsel->core.attr.type); in add_event()
1174 return -1; in add_event()
1180 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { in add_event()
1192 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class); in add_event()
1202 priv->event_class = event_class; in add_event()
1203 evsel->priv = priv; in add_event()
1209 return -1; in add_event()
1214 struct evlist *evlist = session->evlist; in setup_events()
1229 if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\
1231 return -1; \
1244 return -1; \
1247 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\
1253 cw->_name##_class = event_class; \
1321 struct evlist *evlist = session->evlist; in cleanup_events()
1327 priv = evsel->priv; in cleanup_events()
1328 bt_ctf_event_class_put(priv->event_class); in cleanup_events()
1329 zfree(&evsel->priv); in cleanup_events()
1333 session->evlist = NULL; in cleanup_events()
1339 struct perf_header *ph = &session->header; in setup_streams()
1346 ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS; in setup_streams()
1351 return -ENOMEM; in setup_streams()
1354 cw->stream = stream; in setup_streams()
1355 cw->stream_cnt = ncpus; in setup_streams()
1363 for (cpu = 0; cpu < cw->stream_cnt; cpu++) in free_streams()
1364 ctf_stream__delete(cw->stream[cpu]); in free_streams()
1366 zfree(&cw->stream); in free_streams()
1372 struct perf_header *header = &session->header; in ctf_writer__setup_env()
1373 struct bt_ctf_writer *writer = cw->writer; in ctf_writer__setup_env()
1378 return -1; \ in ctf_writer__setup_env()
1381 ADD("host", header->env.hostname); in ctf_writer__setup_env()
1383 ADD("release", header->env.os_release); in ctf_writer__setup_env()
1384 ADD("version", header->env.version); in ctf_writer__setup_env()
1385 ADD("machine", header->env.arch); in ctf_writer__setup_env()
1397 struct bt_ctf_clock *clock = cw->clock; in ctf_writer__setup_clock()
1402 struct perf_env *env = &session->header.env; in ctf_writer__setup_clock()
1404 if (!env->clock.enabled) { in ctf_writer__setup_clock()
1405 pr_err("Can't provide --tod time, missing clock data. " in ctf_writer__setup_clock()
1406 "Please record with -k/--clockid option.\n"); in ctf_writer__setup_clock()
1407 return -1; in ctf_writer__setup_clock()
1410 desc = clockid_name(env->clock.clockid); in ctf_writer__setup_clock()
1411 offset = env->clock.tod_ns - env->clock.clockid_ns; in ctf_writer__setup_clock()
1417 return -1; \ in ctf_writer__setup_clock()
1452 pr2("Created type: INTEGER %d-bit %ssigned %s\n", in create_int_type()
1465 for (i = 0; i < ARRAY_SIZE(cw->data.array); i++) in ctf_writer__cleanup_data()
1466 bt_ctf_field_type_put(cw->data.array[i]); in ctf_writer__cleanup_data()
1478 CREATE_INT_TYPE(cw->data.s64, 64, true, false); in ctf_writer__init_data()
1479 CREATE_INT_TYPE(cw->data.u64, 64, false, false); in ctf_writer__init_data()
1480 CREATE_INT_TYPE(cw->data.s32, 32, true, false); in ctf_writer__init_data()
1481 CREATE_INT_TYPE(cw->data.u32, 32, false, false); in ctf_writer__init_data()
1482 CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true); in ctf_writer__init_data()
1483 CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true); in ctf_writer__init_data()
1485 cw->data.string = bt_ctf_field_type_string_create(); in ctf_writer__init_data()
1486 if (cw->data.string) in ctf_writer__init_data()
1492 return -1; in ctf_writer__init_data()
1499 bt_ctf_clock_put(cw->clock); in ctf_writer__cleanup()
1501 bt_ctf_stream_class_put(cw->stream_class); in ctf_writer__cleanup()
1502 bt_ctf_writer_put(cw->writer); in ctf_writer__cleanup()
1522 cw->writer = writer; in ctf_writer__init()
1531 cw->clock = clock; in ctf_writer__init()
1545 cw->stream_class = stream_class; in ctf_writer__init()
1561 ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id"); in ctf_writer__init()
1578 return -1; in ctf_writer__init()
1585 for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++) in ctf_writer__flush_streams()
1586 ret = ctf_stream__flush(cw->stream[cpu]); in ctf_writer__flush_streams()
1593 struct convert *c = cb; in convert__config()
1595 if (!strcmp(var, "convert.queue-size")) in convert__config()
1596 return perf_config_u64(&c->queue_size, var, value); in convert__config()
1608 .force = opts->force, in bt_convert__perf2ctf()
1610 struct convert c = {}; in bt_convert__perf2ctf()
1615 c.tool.sample = process_sample_event; in bt_convert__perf2ctf()
1627 if (opts->all) { in bt_convert__perf2ctf()
1639 err = -1; in bt_convert__perf2ctf()
1646 if (ctf_writer__init(cw, path, session, opts->tod)) in bt_convert__perf2ctf()
1650 ordered_events__set_alloc_size(&session->ordered_events, in bt_convert__perf2ctf()
1662 if (opts->all && setup_non_sample_events(cw, session)) in bt_convert__perf2ctf()
1675 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n", in bt_convert__perf2ctf()
1679 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples", in bt_convert__perf2ctf()
1686 fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count); in bt_convert__perf2ctf()