1 // SPDX-License-Identifier: GPL-2.0-only
3 * CTF writing support via babeltrace.
5 * Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com>
6 * Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de>
11 #include <linux/compiler.h>
12 #include <linux/kernel.h>
13 #include <linux/zalloc.h>
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>
21 #include <babeltrace/ctf/events.h>
22 #include <traceevent/event-parse.h>
24 #include "data-convert.h"
32 #include <linux/ctype.h>
33 #include <linux/err.h>
34 #include <linux/time64.h>
38 #define pr_N(n, fmt, ...) \
39 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
41 #define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
42 #define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
44 #define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
47 struct bt_ctf_event_class *event_class;
53 struct bt_ctf_stream *stream;
59 /* writer primitives */
60 struct bt_ctf_writer *writer;
61 struct ctf_stream **stream;
63 struct bt_ctf_stream_class *stream_class;
64 struct bt_ctf_clock *clock;
69 struct bt_ctf_field_type *s64;
70 struct bt_ctf_field_type *u64;
71 struct bt_ctf_field_type *s32;
72 struct bt_ctf_field_type *u32;
73 struct bt_ctf_field_type *string;
74 struct bt_ctf_field_type *u32_hex;
75 struct bt_ctf_field_type *u64_hex;
77 struct bt_ctf_field_type *array[6];
79 struct bt_ctf_event_class *comm_class;
80 struct bt_ctf_event_class *exit_class;
81 struct bt_ctf_event_class *fork_class;
82 struct bt_ctf_event_class *mmap_class;
83 struct bt_ctf_event_class *mmap2_class;
87 struct perf_tool tool;
88 struct ctf_writer writer;
94 /* Ordered events configured queue size. */
98 static int value_set(struct bt_ctf_field_type *type,
99 struct bt_ctf_event *event,
100 const char *name, u64 val)
102 struct bt_ctf_field *field;
103 bool sign = bt_ctf_field_type_integer_get_signed(type);
106 field = bt_ctf_field_create(type);
108 pr_err("failed to create a field %s\n", name);
113 ret = bt_ctf_field_signed_integer_set_value(field, val);
115 pr_err("failed to set field value %s\n", name);
119 ret = bt_ctf_field_unsigned_integer_set_value(field, val);
121 pr_err("failed to set field value %s\n", name);
126 ret = bt_ctf_event_set_payload(event, name, field);
128 pr_err("failed to set payload %s\n", name);
132 pr2(" SET [%s = %" PRIu64 "]\n", name, val);
135 bt_ctf_field_put(field);
139 #define __FUNC_VALUE_SET(_name, _val_type) \
140 static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
141 struct bt_ctf_event *event, \
145 struct bt_ctf_field_type *type = cw->data._name; \
146 return value_set(type, event, name, (u64) val); \
149 #define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
155 __FUNC_VALUE_SET(u64_hex, u64)
157 static int string_set_value(struct bt_ctf_field *field, const char *string);
158 static __maybe_unused int
159 value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event,
160 const char *name, const char *string)
162 struct bt_ctf_field_type *type = cw->data.string;
163 struct bt_ctf_field *field;
166 field = bt_ctf_field_create(type);
168 pr_err("failed to create a field %s\n", name);
172 ret = string_set_value(field, string);
174 pr_err("failed to set value %s\n", name);
178 ret = bt_ctf_event_set_payload(event, name, field);
180 pr_err("failed to set payload %s\n", name);
183 bt_ctf_field_put(field);
187 static struct bt_ctf_field_type*
188 get_tracepoint_field_type(struct ctf_writer *cw, struct tep_format_field *field)
190 unsigned long flags = field->flags;
192 if (flags & TEP_FIELD_IS_STRING)
193 return cw->data.string;
195 if (!(flags & TEP_FIELD_IS_SIGNED)) {
196 /* unsigned long are mostly pointers */
197 if (flags & TEP_FIELD_IS_LONG || flags & TEP_FIELD_IS_POINTER)
198 return cw->data.u64_hex;
201 if (flags & TEP_FIELD_IS_SIGNED) {
202 if (field->size == 8)
208 if (field->size == 8)
214 static unsigned long long adjust_signedness(unsigned long long value_int, int size)
216 unsigned long long value_mask;
219 * value_mask = (1 << (size * 8 - 1)) - 1.
220 * Directly set value_mask for code readers.
224 value_mask = 0x7fULL;
227 value_mask = 0x7fffULL;
230 value_mask = 0x7fffffffULL;
234 * For 64 bit value, return it self. There is no need
243 /* If it is a positive value, don't adjust. */
244 if ((value_int & (~0ULL - value_mask)) == 0)
247 /* Fill upper part of value_int with 1 to make it a negative long long. */
248 return (value_int & value_mask) | ~value_mask;
251 static int string_set_value(struct bt_ctf_field *field, const char *string)
254 size_t len = strlen(string), i, p;
257 for (i = p = 0; i < len; i++, p++) {
258 if (isprint(string[i])) {
261 buffer[p] = string[i];
265 snprintf(numstr, sizeof(numstr), "\\x%02x",
266 (unsigned int)(string[i]) & 0xff);
269 buffer = zalloc(i + (len - i) * 4 + 2);
271 pr_err("failed to set unprintable string '%s'\n", string);
272 return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING");
275 strncpy(buffer, string, i);
277 memcpy(buffer + p, numstr, 4);
283 return bt_ctf_field_string_set_value(field, string);
284 err = bt_ctf_field_string_set_value(field, buffer);
289 static int add_tracepoint_field_value(struct ctf_writer *cw,
290 struct bt_ctf_event_class *event_class,
291 struct bt_ctf_event *event,
292 struct perf_sample *sample,
293 struct tep_format_field *fmtf)
295 struct bt_ctf_field_type *type;
296 struct bt_ctf_field *array_field;
297 struct bt_ctf_field *field;
298 const char *name = fmtf->name;
299 void *data = sample->raw_data;
300 unsigned long flags = fmtf->flags;
301 unsigned int n_items;
308 offset = fmtf->offset;
310 if (flags & TEP_FIELD_IS_STRING)
311 flags &= ~TEP_FIELD_IS_ARRAY;
313 if (flags & TEP_FIELD_IS_DYNAMIC) {
314 unsigned long long tmp_val;
316 tmp_val = tep_read_number(fmtf->event->tep,
323 if (flags & TEP_FIELD_IS_ARRAY) {
325 type = bt_ctf_event_class_get_field_by_name(
327 array_field = bt_ctf_field_create(type);
328 bt_ctf_field_type_put(type);
330 pr_err("Failed to create array type %s\n", name);
334 len = fmtf->size / fmtf->arraylen;
335 n_items = fmtf->arraylen;
341 type = get_tracepoint_field_type(cw, fmtf);
343 for (i = 0; i < n_items; i++) {
344 if (flags & TEP_FIELD_IS_ARRAY)
345 field = bt_ctf_field_array_get_field(array_field, i);
347 field = bt_ctf_field_create(type);
350 pr_err("failed to create a field %s\n", name);
354 if (flags & TEP_FIELD_IS_STRING)
355 ret = string_set_value(field, data + offset + i * len);
357 unsigned long long value_int;
359 value_int = tep_read_number(
361 data + offset + i * len, len);
363 if (!(flags & TEP_FIELD_IS_SIGNED))
364 ret = bt_ctf_field_unsigned_integer_set_value(
367 ret = bt_ctf_field_signed_integer_set_value(
368 field, adjust_signedness(value_int, len));
372 pr_err("failed to set file value %s\n", name);
375 if (!(flags & TEP_FIELD_IS_ARRAY)) {
376 ret = bt_ctf_event_set_payload(event, name, field);
378 pr_err("failed to set payload %s\n", name);
382 bt_ctf_field_put(field);
384 if (flags & TEP_FIELD_IS_ARRAY) {
385 ret = bt_ctf_event_set_payload(event, name, array_field);
387 pr_err("Failed add payload array %s\n", name);
390 bt_ctf_field_put(array_field);
395 bt_ctf_field_put(field);
399 static int add_tracepoint_fields_values(struct ctf_writer *cw,
400 struct bt_ctf_event_class *event_class,
401 struct bt_ctf_event *event,
402 struct tep_format_field *fields,
403 struct perf_sample *sample)
405 struct tep_format_field *field;
408 for (field = fields; field; field = field->next) {
409 ret = add_tracepoint_field_value(cw, event_class, event, sample,
417 static int add_tracepoint_values(struct ctf_writer *cw,
418 struct bt_ctf_event_class *event_class,
419 struct bt_ctf_event *event,
421 struct perf_sample *sample)
423 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
424 struct tep_format_field *fields = evsel->tp_format->format.fields;
427 ret = add_tracepoint_fields_values(cw, event_class, event,
428 common_fields, sample);
430 ret = add_tracepoint_fields_values(cw, event_class, event,
437 add_bpf_output_values(struct bt_ctf_event_class *event_class,
438 struct bt_ctf_event *event,
439 struct perf_sample *sample)
441 struct bt_ctf_field_type *len_type, *seq_type;
442 struct bt_ctf_field *len_field, *seq_field;
443 unsigned int raw_size = sample->raw_size;
444 unsigned int nr_elements = raw_size / sizeof(u32);
448 if (nr_elements * sizeof(u32) != raw_size)
449 pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n",
450 raw_size, nr_elements * sizeof(u32) - raw_size);
452 len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len");
453 len_field = bt_ctf_field_create(len_type);
455 pr_err("failed to create 'raw_len' for bpf output event\n");
460 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
462 pr_err("failed to set field value for raw_len\n");
465 ret = bt_ctf_event_set_payload(event, "raw_len", len_field);
467 pr_err("failed to set payload to raw_len\n");
471 seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data");
472 seq_field = bt_ctf_field_create(seq_type);
474 pr_err("failed to create 'raw_data' for bpf output event\n");
479 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
481 pr_err("failed to set length of 'raw_data'\n");
485 for (i = 0; i < nr_elements; i++) {
486 struct bt_ctf_field *elem_field =
487 bt_ctf_field_sequence_get_field(seq_field, i);
489 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
490 ((u32 *)(sample->raw_data))[i]);
492 bt_ctf_field_put(elem_field);
494 pr_err("failed to set raw_data[%d]\n", i);
499 ret = bt_ctf_event_set_payload(event, "raw_data", seq_field);
501 pr_err("failed to set payload for raw_data\n");
504 bt_ctf_field_put(seq_field);
506 bt_ctf_field_type_put(seq_type);
508 bt_ctf_field_put(len_field);
510 bt_ctf_field_type_put(len_type);
515 add_callchain_output_values(struct bt_ctf_event_class *event_class,
516 struct bt_ctf_event *event,
517 struct ip_callchain *callchain)
519 struct bt_ctf_field_type *len_type, *seq_type;
520 struct bt_ctf_field *len_field, *seq_field;
521 unsigned int nr_elements = callchain->nr;
525 len_type = bt_ctf_event_class_get_field_by_name(
526 event_class, "perf_callchain_size");
527 len_field = bt_ctf_field_create(len_type);
529 pr_err("failed to create 'perf_callchain_size' for callchain output event\n");
534 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
536 pr_err("failed to set field value for perf_callchain_size\n");
539 ret = bt_ctf_event_set_payload(event, "perf_callchain_size", len_field);
541 pr_err("failed to set payload to perf_callchain_size\n");
545 seq_type = bt_ctf_event_class_get_field_by_name(
546 event_class, "perf_callchain");
547 seq_field = bt_ctf_field_create(seq_type);
549 pr_err("failed to create 'perf_callchain' for callchain output event\n");
554 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
556 pr_err("failed to set length of 'perf_callchain'\n");
560 for (i = 0; i < nr_elements; i++) {
561 struct bt_ctf_field *elem_field =
562 bt_ctf_field_sequence_get_field(seq_field, i);
564 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
565 ((u64 *)(callchain->ips))[i]);
567 bt_ctf_field_put(elem_field);
569 pr_err("failed to set callchain[%d]\n", i);
574 ret = bt_ctf_event_set_payload(event, "perf_callchain", seq_field);
576 pr_err("failed to set payload for raw_data\n");
579 bt_ctf_field_put(seq_field);
581 bt_ctf_field_type_put(seq_type);
583 bt_ctf_field_put(len_field);
585 bt_ctf_field_type_put(len_type);
589 static int add_generic_values(struct ctf_writer *cw,
590 struct bt_ctf_event *event,
592 struct perf_sample *sample)
594 u64 type = evsel->core.attr.sample_type;
599 * PERF_SAMPLE_TIME - not needed as we have it in
601 * PERF_SAMPLE_READ - TODO
602 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
603 * PERF_SAMPLE_BRANCH_STACK - TODO
604 * PERF_SAMPLE_REGS_USER - TODO
605 * PERF_SAMPLE_STACK_USER - TODO
608 if (type & PERF_SAMPLE_IP) {
609 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
614 if (type & PERF_SAMPLE_TID) {
615 ret = value_set_s32(cw, event, "perf_tid", sample->tid);
619 ret = value_set_s32(cw, event, "perf_pid", sample->pid);
624 if ((type & PERF_SAMPLE_ID) ||
625 (type & PERF_SAMPLE_IDENTIFIER)) {
626 ret = value_set_u64(cw, event, "perf_id", sample->id);
631 if (type & PERF_SAMPLE_STREAM_ID) {
632 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
637 if (type & PERF_SAMPLE_PERIOD) {
638 ret = value_set_u64(cw, event, "perf_period", sample->period);
643 if (type & PERF_SAMPLE_WEIGHT) {
644 ret = value_set_u64(cw, event, "perf_weight", sample->weight);
649 if (type & PERF_SAMPLE_DATA_SRC) {
650 ret = value_set_u64(cw, event, "perf_data_src",
656 if (type & PERF_SAMPLE_TRANSACTION) {
657 ret = value_set_u64(cw, event, "perf_transaction",
658 sample->transaction);
666 static int ctf_stream__flush(struct ctf_stream *cs)
671 err = bt_ctf_stream_flush(cs->stream);
673 pr_err("CTF stream %d flush failed\n", cs->cpu);
675 pr("Flush stream for cpu %d (%u samples)\n",
684 static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
686 struct ctf_stream *cs;
687 struct bt_ctf_field *pkt_ctx = NULL;
688 struct bt_ctf_field *cpu_field = NULL;
689 struct bt_ctf_stream *stream = NULL;
692 cs = zalloc(sizeof(*cs));
694 pr_err("Failed to allocate ctf stream\n");
698 stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
700 pr_err("Failed to create CTF stream\n");
704 pkt_ctx = bt_ctf_stream_get_packet_context(stream);
706 pr_err("Failed to obtain packet context\n");
710 cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
711 bt_ctf_field_put(pkt_ctx);
713 pr_err("Failed to obtain cpu field\n");
717 ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
719 pr_err("Failed to update CPU number\n");
723 bt_ctf_field_put(cpu_field);
731 bt_ctf_field_put(cpu_field);
733 bt_ctf_stream_put(stream);
739 static void ctf_stream__delete(struct ctf_stream *cs)
742 bt_ctf_stream_put(cs->stream);
747 static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
749 struct ctf_stream *cs = cw->stream[cpu];
752 cs = ctf_stream__create(cw, cpu);
753 cw->stream[cpu] = cs;
759 static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
764 if (evsel->core.attr.sample_type & PERF_SAMPLE_CPU)
767 if (cpu > cw->stream_cnt) {
768 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
769 cpu, cw->stream_cnt);
776 #define STREAM_FLUSH_COUNT 100000
779 * Currently we have no other way to determine the
780 * time for the stream flush other than keep track
781 * of the number of events and check it against
784 static bool is_flush_needed(struct ctf_stream *cs)
786 return cs->count >= STREAM_FLUSH_COUNT;
789 static int process_sample_event(struct perf_tool *tool,
790 union perf_event *_event,
791 struct perf_sample *sample,
793 struct machine *machine __maybe_unused)
795 struct convert *c = container_of(tool, struct convert, tool);
796 struct evsel_priv *priv = evsel->priv;
797 struct ctf_writer *cw = &c->writer;
798 struct ctf_stream *cs;
799 struct bt_ctf_event_class *event_class;
800 struct bt_ctf_event *event;
802 unsigned long type = evsel->core.attr.sample_type;
804 if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
807 event_class = priv->event_class;
811 c->events_size += _event->header.size;
813 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
815 event = bt_ctf_event_create(event_class);
817 pr_err("Failed to create an CTF event\n");
821 bt_ctf_clock_set_time(cw->clock, sample->time);
823 ret = add_generic_values(cw, event, evsel, sample);
827 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
828 ret = add_tracepoint_values(cw, event_class, event,
834 if (type & PERF_SAMPLE_CALLCHAIN) {
835 ret = add_callchain_output_values(event_class,
836 event, sample->callchain);
841 if (evsel__is_bpf_output(evsel)) {
842 ret = add_bpf_output_values(event_class, event, sample);
847 cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel));
849 if (is_flush_needed(cs))
850 ctf_stream__flush(cs);
853 bt_ctf_stream_append_event(cs->stream, event);
856 bt_ctf_event_put(event);
860 #define __NON_SAMPLE_SET_FIELD(_name, _type, _field) \
862 ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
867 #define __FUNC_PROCESS_NON_SAMPLE(_name, body) \
868 static int process_##_name##_event(struct perf_tool *tool, \
869 union perf_event *_event, \
870 struct perf_sample *sample, \
871 struct machine *machine) \
873 struct convert *c = container_of(tool, struct convert, tool);\
874 struct ctf_writer *cw = &c->writer; \
875 struct bt_ctf_event_class *event_class = cw->_name##_class;\
876 struct bt_ctf_event *event; \
877 struct ctf_stream *cs; \
880 c->non_sample_count++; \
881 c->events_size += _event->header.size; \
882 event = bt_ctf_event_create(event_class); \
884 pr_err("Failed to create an CTF event\n"); \
888 bt_ctf_clock_set_time(cw->clock, sample->time); \
890 cs = ctf_stream(cw, 0); \
892 if (is_flush_needed(cs)) \
893 ctf_stream__flush(cs); \
896 bt_ctf_stream_append_event(cs->stream, event); \
898 bt_ctf_event_put(event); \
900 return perf_event__process_##_name(tool, _event, sample, machine);\
903 __FUNC_PROCESS_NON_SAMPLE(comm,
904 __NON_SAMPLE_SET_FIELD(comm, u32, pid);
905 __NON_SAMPLE_SET_FIELD(comm, u32, tid);
906 __NON_SAMPLE_SET_FIELD(comm, string, comm);
908 __FUNC_PROCESS_NON_SAMPLE(fork,
909 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
910 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
911 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
912 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
913 __NON_SAMPLE_SET_FIELD(fork, u64, time);
916 __FUNC_PROCESS_NON_SAMPLE(exit,
917 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
918 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
919 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
920 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
921 __NON_SAMPLE_SET_FIELD(fork, u64, time);
923 __FUNC_PROCESS_NON_SAMPLE(mmap,
924 __NON_SAMPLE_SET_FIELD(mmap, u32, pid);
925 __NON_SAMPLE_SET_FIELD(mmap, u32, tid);
926 __NON_SAMPLE_SET_FIELD(mmap, u64_hex, start);
927 __NON_SAMPLE_SET_FIELD(mmap, string, filename);
929 __FUNC_PROCESS_NON_SAMPLE(mmap2,
930 __NON_SAMPLE_SET_FIELD(mmap2, u32, pid);
931 __NON_SAMPLE_SET_FIELD(mmap2, u32, tid);
932 __NON_SAMPLE_SET_FIELD(mmap2, u64_hex, start);
933 __NON_SAMPLE_SET_FIELD(mmap2, string, filename);
935 #undef __NON_SAMPLE_SET_FIELD
936 #undef __FUNC_PROCESS_NON_SAMPLE
938 /* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
939 static char *change_name(char *name, char *orig_name, int dup)
941 char *new_name = NULL;
950 * Add '_' prefix to potential keywork. According to
951 * Mathieu Desnoyers (https://lore.kernel.org/lkml/1074266107.40857.1422045946295.JavaMail.zimbra@efficios.com),
952 * further CTF spec updating may require us to use '$'.
955 len = strlen(name) + sizeof("_");
957 len = strlen(orig_name) + sizeof("_dupl_X");
959 new_name = malloc(len);
964 snprintf(new_name, len, "_%s", name);
966 snprintf(new_name, len, "%s_dupl_%d", orig_name, dup);
969 if (name != orig_name)
974 static int event_class_add_field(struct bt_ctf_event_class *event_class,
975 struct bt_ctf_field_type *type,
976 struct tep_format_field *field)
978 struct bt_ctf_field_type *t = NULL;
983 /* alias was already assigned */
984 if (field->alias != field->name)
985 return bt_ctf_event_class_add_field(event_class, type,
986 (char *)field->alias);
990 /* If 'name' is a keywork, add prefix. */
991 if (bt_ctf_validate_identifier(name))
992 name = change_name(name, field->name, -1);
995 pr_err("Failed to fix invalid identifier.");
998 while ((t = bt_ctf_event_class_get_field_by_name(event_class, name))) {
999 bt_ctf_field_type_put(t);
1000 name = change_name(name, field->name, dup++);
1002 pr_err("Failed to create dup name for '%s'\n", field->name);
1007 ret = bt_ctf_event_class_add_field(event_class, type, name);
1009 field->alias = name;
1014 static int add_tracepoint_fields_types(struct ctf_writer *cw,
1015 struct tep_format_field *fields,
1016 struct bt_ctf_event_class *event_class)
1018 struct tep_format_field *field;
1021 for (field = fields; field; field = field->next) {
1022 struct bt_ctf_field_type *type;
1023 unsigned long flags = field->flags;
1025 pr2(" field '%s'\n", field->name);
1027 type = get_tracepoint_field_type(cw, field);
1032 * A string is an array of chars. For this we use the string
1033 * type and don't care that it is an array. What we don't
1034 * support is an array of strings.
1036 if (flags & TEP_FIELD_IS_STRING)
1037 flags &= ~TEP_FIELD_IS_ARRAY;
1039 if (flags & TEP_FIELD_IS_ARRAY)
1040 type = bt_ctf_field_type_array_create(type, field->arraylen);
1042 ret = event_class_add_field(event_class, type, field);
1044 if (flags & TEP_FIELD_IS_ARRAY)
1045 bt_ctf_field_type_put(type);
1048 pr_err("Failed to add field '%s': %d\n",
1057 static int add_tracepoint_types(struct ctf_writer *cw,
1058 struct evsel *evsel,
1059 struct bt_ctf_event_class *class)
1061 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
1062 struct tep_format_field *fields = evsel->tp_format->format.fields;
1065 ret = add_tracepoint_fields_types(cw, common_fields, class);
1067 ret = add_tracepoint_fields_types(cw, fields, class);
1072 static int add_bpf_output_types(struct ctf_writer *cw,
1073 struct bt_ctf_event_class *class)
1075 struct bt_ctf_field_type *len_type = cw->data.u32;
1076 struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex;
1077 struct bt_ctf_field_type *seq_type;
1080 ret = bt_ctf_event_class_add_field(class, len_type, "raw_len");
1084 seq_type = bt_ctf_field_type_sequence_create(seq_base_type, "raw_len");
1088 return bt_ctf_event_class_add_field(class, seq_type, "raw_data");
1091 static int add_generic_types(struct ctf_writer *cw, struct evsel *evsel,
1092 struct bt_ctf_event_class *event_class)
1094 u64 type = evsel->core.attr.sample_type;
1098 * PERF_SAMPLE_TIME - not needed as we have it in
1100 * PERF_SAMPLE_READ - TODO
1101 * PERF_SAMPLE_CALLCHAIN - TODO
1102 * PERF_SAMPLE_RAW - tracepoint fields and BPF output
1103 * are handled separately
1104 * PERF_SAMPLE_BRANCH_STACK - TODO
1105 * PERF_SAMPLE_REGS_USER - TODO
1106 * PERF_SAMPLE_STACK_USER - TODO
1109 #define ADD_FIELD(cl, t, n) \
1111 pr2(" field '%s'\n", n); \
1112 if (bt_ctf_event_class_add_field(cl, t, n)) { \
1113 pr_err("Failed to add field '%s';\n", n); \
1118 if (type & PERF_SAMPLE_IP)
1119 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
1121 if (type & PERF_SAMPLE_TID) {
1122 ADD_FIELD(event_class, cw->data.s32, "perf_tid");
1123 ADD_FIELD(event_class, cw->data.s32, "perf_pid");
1126 if ((type & PERF_SAMPLE_ID) ||
1127 (type & PERF_SAMPLE_IDENTIFIER))
1128 ADD_FIELD(event_class, cw->data.u64, "perf_id");
1130 if (type & PERF_SAMPLE_STREAM_ID)
1131 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
1133 if (type & PERF_SAMPLE_PERIOD)
1134 ADD_FIELD(event_class, cw->data.u64, "perf_period");
1136 if (type & PERF_SAMPLE_WEIGHT)
1137 ADD_FIELD(event_class, cw->data.u64, "perf_weight");
1139 if (type & PERF_SAMPLE_DATA_SRC)
1140 ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
1142 if (type & PERF_SAMPLE_TRANSACTION)
1143 ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
1145 if (type & PERF_SAMPLE_CALLCHAIN) {
1146 ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size");
1147 ADD_FIELD(event_class,
1148 bt_ctf_field_type_sequence_create(
1149 cw->data.u64_hex, "perf_callchain_size"),
1157 static int add_event(struct ctf_writer *cw, struct evsel *evsel)
1159 struct bt_ctf_event_class *event_class;
1160 struct evsel_priv *priv;
1161 const char *name = evsel__name(evsel);
1164 pr("Adding event '%s' (type %d)\n", name, evsel->core.attr.type);
1166 event_class = bt_ctf_event_class_create(name);
1170 ret = add_generic_types(cw, evsel, event_class);
1174 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
1175 ret = add_tracepoint_types(cw, evsel, event_class);
1180 if (evsel__is_bpf_output(evsel)) {
1181 ret = add_bpf_output_types(cw, event_class);
1186 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);
1188 pr("Failed to add event class into stream.\n");
1192 priv = malloc(sizeof(*priv));
1196 priv->event_class = event_class;
1201 bt_ctf_event_class_put(event_class);
1202 pr_err("Failed to add event '%s'.\n", name);
1206 static int setup_events(struct ctf_writer *cw, struct perf_session *session)
1208 struct evlist *evlist = session->evlist;
1209 struct evsel *evsel;
1212 evlist__for_each_entry(evlist, evsel) {
1213 ret = add_event(cw, evsel);
1220 #define __NON_SAMPLE_ADD_FIELD(t, n) \
1222 pr2(" field '%s'\n", #n); \
1223 if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\
1224 pr_err("Failed to add field '%s';\n", #n);\
1229 #define __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(_name, body) \
1230 static int add_##_name##_event(struct ctf_writer *cw) \
1232 struct bt_ctf_event_class *event_class; \
1235 pr("Adding "#_name" event\n"); \
1236 event_class = bt_ctf_event_class_create("perf_" #_name);\
1241 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\
1243 pr("Failed to add event class '"#_name"' into stream.\n");\
1247 cw->_name##_class = event_class; \
1248 bt_ctf_event_class_put(event_class); \
1252 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(comm,
1253 __NON_SAMPLE_ADD_FIELD(u32, pid);
1254 __NON_SAMPLE_ADD_FIELD(u32, tid);
1255 __NON_SAMPLE_ADD_FIELD(string, comm);
1258 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(fork,
1259 __NON_SAMPLE_ADD_FIELD(u32, pid);
1260 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1261 __NON_SAMPLE_ADD_FIELD(u32, tid);
1262 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1263 __NON_SAMPLE_ADD_FIELD(u64, time);
1266 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(exit,
1267 __NON_SAMPLE_ADD_FIELD(u32, pid);
1268 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1269 __NON_SAMPLE_ADD_FIELD(u32, tid);
1270 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1271 __NON_SAMPLE_ADD_FIELD(u64, time);
1274 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap,
1275 __NON_SAMPLE_ADD_FIELD(u32, pid);
1276 __NON_SAMPLE_ADD_FIELD(u32, tid);
1277 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1278 __NON_SAMPLE_ADD_FIELD(string, filename);
1281 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap2,
1282 __NON_SAMPLE_ADD_FIELD(u32, pid);
1283 __NON_SAMPLE_ADD_FIELD(u32, tid);
1284 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1285 __NON_SAMPLE_ADD_FIELD(string, filename);
1287 #undef __NON_SAMPLE_ADD_FIELD
1288 #undef __FUNC_ADD_NON_SAMPLE_EVENT_CLASS
1290 static int setup_non_sample_events(struct ctf_writer *cw,
1291 struct perf_session *session __maybe_unused)
1295 ret = add_comm_event(cw);
1298 ret = add_exit_event(cw);
1301 ret = add_fork_event(cw);
1304 ret = add_mmap_event(cw);
1307 ret = add_mmap2_event(cw);
1313 static void cleanup_events(struct perf_session *session)
1315 struct evlist *evlist = session->evlist;
1316 struct evsel *evsel;
1318 evlist__for_each_entry(evlist, evsel) {
1319 struct evsel_priv *priv;
1322 bt_ctf_event_class_put(priv->event_class);
1323 zfree(&evsel->priv);
1326 evlist__delete(evlist);
1327 session->evlist = NULL;
1330 static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
1332 struct ctf_stream **stream;
1333 struct perf_header *ph = &session->header;
1337 * Try to get the number of cpus used in the data file,
1338 * if not present fallback to the MAX_CPUS.
1340 ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS;
1342 stream = zalloc(sizeof(*stream) * ncpus);
1344 pr_err("Failed to allocate streams.\n");
1348 cw->stream = stream;
1349 cw->stream_cnt = ncpus;
1353 static void free_streams(struct ctf_writer *cw)
1357 for (cpu = 0; cpu < cw->stream_cnt; cpu++)
1358 ctf_stream__delete(cw->stream[cpu]);
1363 static int ctf_writer__setup_env(struct ctf_writer *cw,
1364 struct perf_session *session)
1366 struct perf_header *header = &session->header;
1367 struct bt_ctf_writer *writer = cw->writer;
1369 #define ADD(__n, __v) \
1371 if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \
1375 ADD("host", header->env.hostname);
1376 ADD("sysname", "Linux");
1377 ADD("release", header->env.os_release);
1378 ADD("version", header->env.version);
1379 ADD("machine", header->env.arch);
1380 ADD("domain", "kernel");
1381 ADD("tracer_name", "perf");
1387 static int ctf_writer__setup_clock(struct ctf_writer *cw,
1388 struct perf_session *session,
1391 struct bt_ctf_clock *clock = cw->clock;
1392 const char *desc = "perf clock";
1396 struct perf_env *env = &session->header.env;
1398 if (!env->clock.enabled) {
1399 pr_err("Can't provide --tod time, missing clock data. "
1400 "Please record with -k/--clockid option.\n");
1404 desc = clockid_name(env->clock.clockid);
1405 offset = env->clock.tod_ns - env->clock.clockid_ns;
1408 #define SET(__n, __v) \
1410 if (bt_ctf_clock_set_##__n(clock, __v)) \
1414 SET(frequency, 1000000000);
1415 SET(offset, offset);
1416 SET(description, desc);
1418 SET(is_absolute, 0);
1424 static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex)
1426 struct bt_ctf_field_type *type;
1428 type = bt_ctf_field_type_integer_create(size);
1433 bt_ctf_field_type_integer_set_signed(type, 1))
1437 bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL))
1440 #if __BYTE_ORDER == __BIG_ENDIAN
1441 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_BIG_ENDIAN);
1443 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
1446 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
1447 size, sign ? "un" : "", hex ? "hex" : "");
1451 bt_ctf_field_type_put(type);
1455 static void ctf_writer__cleanup_data(struct ctf_writer *cw)
1459 for (i = 0; i < ARRAY_SIZE(cw->data.array); i++)
1460 bt_ctf_field_type_put(cw->data.array[i]);
1463 static int ctf_writer__init_data(struct ctf_writer *cw)
1465 #define CREATE_INT_TYPE(type, size, sign, hex) \
1467 (type) = create_int_type(size, sign, hex); \
1472 CREATE_INT_TYPE(cw->data.s64, 64, true, false);
1473 CREATE_INT_TYPE(cw->data.u64, 64, false, false);
1474 CREATE_INT_TYPE(cw->data.s32, 32, true, false);
1475 CREATE_INT_TYPE(cw->data.u32, 32, false, false);
1476 CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true);
1477 CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true);
1479 cw->data.string = bt_ctf_field_type_string_create();
1480 if (cw->data.string)
1484 ctf_writer__cleanup_data(cw);
1485 pr_err("Failed to create data types.\n");
1489 static void ctf_writer__cleanup(struct ctf_writer *cw)
1491 ctf_writer__cleanup_data(cw);
1493 bt_ctf_clock_put(cw->clock);
1495 bt_ctf_stream_class_put(cw->stream_class);
1496 bt_ctf_writer_put(cw->writer);
1498 /* and NULL all the pointers */
1499 memset(cw, 0, sizeof(*cw));
1502 static int ctf_writer__init(struct ctf_writer *cw, const char *path,
1503 struct perf_session *session, bool tod)
1505 struct bt_ctf_writer *writer;
1506 struct bt_ctf_stream_class *stream_class;
1507 struct bt_ctf_clock *clock;
1508 struct bt_ctf_field_type *pkt_ctx_type;
1512 writer = bt_ctf_writer_create(path);
1516 cw->writer = writer;
1519 clock = bt_ctf_clock_create("perf_clock");
1521 pr("Failed to create CTF clock.\n");
1527 if (ctf_writer__setup_clock(cw, session, tod)) {
1528 pr("Failed to setup CTF clock.\n");
1532 /* CTF stream class */
1533 stream_class = bt_ctf_stream_class_create("perf_stream");
1534 if (!stream_class) {
1535 pr("Failed to create CTF stream class.\n");
1539 cw->stream_class = stream_class;
1541 /* CTF clock stream setup */
1542 if (bt_ctf_stream_class_set_clock(stream_class, clock)) {
1543 pr("Failed to assign CTF clock to stream class.\n");
1547 if (ctf_writer__init_data(cw))
1550 /* Add cpu_id for packet context */
1551 pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1555 ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id");
1556 bt_ctf_field_type_put(pkt_ctx_type);
1560 /* CTF clock writer setup */
1561 if (bt_ctf_writer_add_clock(writer, clock)) {
1562 pr("Failed to assign CTF clock to writer.\n");
1569 ctf_writer__cleanup(cw);
1571 pr_err("Failed to setup CTF writer.\n");
1575 static int ctf_writer__flush_streams(struct ctf_writer *cw)
1579 for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++)
1580 ret = ctf_stream__flush(cw->stream[cpu]);
1585 static int convert__config(const char *var, const char *value, void *cb)
1587 struct convert *c = cb;
1589 if (!strcmp(var, "convert.queue-size"))
1590 return perf_config_u64(&c->queue_size, var, value);
1595 int bt_convert__perf2ctf(const char *input, const char *path,
1596 struct perf_data_convert_opts *opts)
1598 struct perf_session *session;
1599 struct perf_data data = {
1601 .mode = PERF_DATA_MODE_READ,
1602 .force = opts->force,
1604 struct convert c = {
1606 .sample = process_sample_event,
1607 .mmap = perf_event__process_mmap,
1608 .mmap2 = perf_event__process_mmap2,
1609 .comm = perf_event__process_comm,
1610 .exit = perf_event__process_exit,
1611 .fork = perf_event__process_fork,
1612 .lost = perf_event__process_lost,
1613 .tracing_data = perf_event__process_tracing_data,
1614 .build_id = perf_event__process_build_id,
1615 .namespaces = perf_event__process_namespaces,
1616 .ordered_events = true,
1617 .ordering_requires_timestamps = true,
1620 struct ctf_writer *cw = &c.writer;
1624 c.tool.comm = process_comm_event;
1625 c.tool.exit = process_exit_event;
1626 c.tool.fork = process_fork_event;
1627 c.tool.mmap = process_mmap_event;
1628 c.tool.mmap2 = process_mmap2_event;
1631 err = perf_config(convert__config, &c);
1636 /* perf.data session */
1637 session = perf_session__new(&data, 0, &c.tool);
1638 if (IS_ERR(session))
1639 return PTR_ERR(session);
1642 if (ctf_writer__init(cw, path, session, opts->tod))
1646 ordered_events__set_alloc_size(&session->ordered_events,
1650 /* CTF writer env/clock setup */
1651 if (ctf_writer__setup_env(cw, session))
1654 /* CTF events setup */
1655 if (setup_events(cw, session))
1658 if (opts->all && setup_non_sample_events(cw, session))
1661 if (setup_streams(cw, session))
1664 err = perf_session__process_events(session);
1666 err = ctf_writer__flush_streams(cw);
1668 pr_err("Error during conversion.\n");
1671 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
1675 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
1676 (double) c.events_size / 1024.0 / 1024.0,
1679 if (!c.non_sample_count)
1680 fprintf(stderr, ") ]\n");
1682 fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
1684 cleanup_events(session);
1685 perf_session__delete(session);
1686 ctf_writer__cleanup(cw);
1691 ctf_writer__cleanup(cw);
1693 perf_session__delete(session);
1694 pr_err("Error during conversion setup.\n");