2 * CTF writing support via babeltrace.
4 * Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com>
5 * Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7 * Released under the GPL v2. (and only v2, not any later version)
10 #include <linux/compiler.h>
11 #include <babeltrace/ctf-writer/writer.h>
12 #include <babeltrace/ctf-writer/clock.h>
13 #include <babeltrace/ctf-writer/stream.h>
14 #include <babeltrace/ctf-writer/event.h>
15 #include <babeltrace/ctf-writer/event-types.h>
16 #include <babeltrace/ctf-writer/event-fields.h>
17 #include <babeltrace/ctf-ir/utils.h>
18 #include <babeltrace/ctf/events.h>
19 #include <traceevent/event-parse.h>
21 #include "data-convert-bt.h"
30 #define pr_N(n, fmt, ...) \
31 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
33 #define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
34 #define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
36 #define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
39 struct bt_ctf_event_class
*event_class
;
45 struct bt_ctf_stream
*stream
;
51 /* writer primitives */
52 struct bt_ctf_writer
*writer
;
53 struct ctf_stream
**stream
;
55 struct bt_ctf_stream_class
*stream_class
;
56 struct bt_ctf_clock
*clock
;
61 struct bt_ctf_field_type
*s64
;
62 struct bt_ctf_field_type
*u64
;
63 struct bt_ctf_field_type
*s32
;
64 struct bt_ctf_field_type
*u32
;
65 struct bt_ctf_field_type
*string
;
66 struct bt_ctf_field_type
*u64_hex
;
68 struct bt_ctf_field_type
*array
[6];
73 struct perf_tool tool
;
74 struct ctf_writer writer
;
79 /* Ordered events configured queue size. */
83 static int value_set(struct bt_ctf_field_type
*type
,
84 struct bt_ctf_event
*event
,
85 const char *name
, u64 val
)
87 struct bt_ctf_field
*field
;
88 bool sign
= bt_ctf_field_type_integer_get_signed(type
);
91 field
= bt_ctf_field_create(type
);
93 pr_err("failed to create a field %s\n", name
);
98 ret
= bt_ctf_field_signed_integer_set_value(field
, val
);
100 pr_err("failed to set field value %s\n", name
);
104 ret
= bt_ctf_field_unsigned_integer_set_value(field
, val
);
106 pr_err("failed to set field value %s\n", name
);
111 ret
= bt_ctf_event_set_payload(event
, name
, field
);
113 pr_err("failed to set payload %s\n", name
);
117 pr2(" SET [%s = %" PRIu64
"]\n", name
, val
);
120 bt_ctf_field_put(field
);
124 #define __FUNC_VALUE_SET(_name, _val_type) \
125 static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
126 struct bt_ctf_event *event, \
130 struct bt_ctf_field_type *type = cw->data._name; \
131 return value_set(type, event, name, (u64) val); \
134 #define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
140 __FUNC_VALUE_SET(u64_hex
, u64
)
142 static struct bt_ctf_field_type
*
143 get_tracepoint_field_type(struct ctf_writer
*cw
, struct format_field
*field
)
145 unsigned long flags
= field
->flags
;
147 if (flags
& FIELD_IS_STRING
)
148 return cw
->data
.string
;
150 if (!(flags
& FIELD_IS_SIGNED
)) {
151 /* unsigned long are mostly pointers */
152 if (flags
& FIELD_IS_LONG
|| flags
& FIELD_IS_POINTER
)
153 return cw
->data
.u64_hex
;
156 if (flags
& FIELD_IS_SIGNED
) {
157 if (field
->size
== 8)
163 if (field
->size
== 8)
169 static unsigned long long adjust_signedness(unsigned long long value_int
, int size
)
171 unsigned long long value_mask
;
174 * value_mask = (1 << (size * 8 - 1)) - 1.
175 * Directly set value_mask for code readers.
179 value_mask
= 0x7fULL
;
182 value_mask
= 0x7fffULL
;
185 value_mask
= 0x7fffffffULL
;
189 * For 64 bit value, return it self. There is no need
198 /* If it is a positive value, don't adjust. */
199 if ((value_int
& (~0ULL - value_mask
)) == 0)
202 /* Fill upper part of value_int with 1 to make it a negative long long. */
203 return (value_int
& value_mask
) | ~value_mask
;
206 static int add_tracepoint_field_value(struct ctf_writer
*cw
,
207 struct bt_ctf_event_class
*event_class
,
208 struct bt_ctf_event
*event
,
209 struct perf_sample
*sample
,
210 struct format_field
*fmtf
)
212 struct bt_ctf_field_type
*type
;
213 struct bt_ctf_field
*array_field
;
214 struct bt_ctf_field
*field
;
215 const char *name
= fmtf
->name
;
216 void *data
= sample
->raw_data
;
217 unsigned long flags
= fmtf
->flags
;
218 unsigned int n_items
;
225 offset
= fmtf
->offset
;
227 if (flags
& FIELD_IS_STRING
)
228 flags
&= ~FIELD_IS_ARRAY
;
230 if (flags
& FIELD_IS_DYNAMIC
) {
231 unsigned long long tmp_val
;
233 tmp_val
= pevent_read_number(fmtf
->event
->pevent
,
240 if (flags
& FIELD_IS_ARRAY
) {
242 type
= bt_ctf_event_class_get_field_by_name(
244 array_field
= bt_ctf_field_create(type
);
245 bt_ctf_field_type_put(type
);
247 pr_err("Failed to create array type %s\n", name
);
251 len
= fmtf
->size
/ fmtf
->arraylen
;
252 n_items
= fmtf
->arraylen
;
258 type
= get_tracepoint_field_type(cw
, fmtf
);
260 for (i
= 0; i
< n_items
; i
++) {
261 if (flags
& FIELD_IS_ARRAY
)
262 field
= bt_ctf_field_array_get_field(array_field
, i
);
264 field
= bt_ctf_field_create(type
);
267 pr_err("failed to create a field %s\n", name
);
271 if (flags
& FIELD_IS_STRING
)
272 ret
= bt_ctf_field_string_set_value(field
,
273 data
+ offset
+ i
* len
);
275 unsigned long long value_int
;
277 value_int
= pevent_read_number(
279 data
+ offset
+ i
* len
, len
);
281 if (!(flags
& FIELD_IS_SIGNED
))
282 ret
= bt_ctf_field_unsigned_integer_set_value(
285 ret
= bt_ctf_field_signed_integer_set_value(
286 field
, adjust_signedness(value_int
, len
));
290 pr_err("failed to set file value %s\n", name
);
293 if (!(flags
& FIELD_IS_ARRAY
)) {
294 ret
= bt_ctf_event_set_payload(event
, name
, field
);
296 pr_err("failed to set payload %s\n", name
);
300 bt_ctf_field_put(field
);
302 if (flags
& FIELD_IS_ARRAY
) {
303 ret
= bt_ctf_event_set_payload(event
, name
, array_field
);
305 pr_err("Failed add payload array %s\n", name
);
308 bt_ctf_field_put(array_field
);
313 bt_ctf_field_put(field
);
317 static int add_tracepoint_fields_values(struct ctf_writer
*cw
,
318 struct bt_ctf_event_class
*event_class
,
319 struct bt_ctf_event
*event
,
320 struct format_field
*fields
,
321 struct perf_sample
*sample
)
323 struct format_field
*field
;
326 for (field
= fields
; field
; field
= field
->next
) {
327 ret
= add_tracepoint_field_value(cw
, event_class
, event
, sample
,
335 static int add_tracepoint_values(struct ctf_writer
*cw
,
336 struct bt_ctf_event_class
*event_class
,
337 struct bt_ctf_event
*event
,
338 struct perf_evsel
*evsel
,
339 struct perf_sample
*sample
)
341 struct format_field
*common_fields
= evsel
->tp_format
->format
.common_fields
;
342 struct format_field
*fields
= evsel
->tp_format
->format
.fields
;
345 ret
= add_tracepoint_fields_values(cw
, event_class
, event
,
346 common_fields
, sample
);
348 ret
= add_tracepoint_fields_values(cw
, event_class
, event
,
354 static int add_generic_values(struct ctf_writer
*cw
,
355 struct bt_ctf_event
*event
,
356 struct perf_evsel
*evsel
,
357 struct perf_sample
*sample
)
359 u64 type
= evsel
->attr
.sample_type
;
364 * PERF_SAMPLE_TIME - not needed as we have it in
366 * PERF_SAMPLE_READ - TODO
367 * PERF_SAMPLE_CALLCHAIN - TODO
368 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
369 * PERF_SAMPLE_BRANCH_STACK - TODO
370 * PERF_SAMPLE_REGS_USER - TODO
371 * PERF_SAMPLE_STACK_USER - TODO
374 if (type
& PERF_SAMPLE_IP
) {
375 ret
= value_set_u64_hex(cw
, event
, "perf_ip", sample
->ip
);
380 if (type
& PERF_SAMPLE_TID
) {
381 ret
= value_set_s32(cw
, event
, "perf_tid", sample
->tid
);
385 ret
= value_set_s32(cw
, event
, "perf_pid", sample
->pid
);
390 if ((type
& PERF_SAMPLE_ID
) ||
391 (type
& PERF_SAMPLE_IDENTIFIER
)) {
392 ret
= value_set_u64(cw
, event
, "perf_id", sample
->id
);
397 if (type
& PERF_SAMPLE_STREAM_ID
) {
398 ret
= value_set_u64(cw
, event
, "perf_stream_id", sample
->stream_id
);
403 if (type
& PERF_SAMPLE_PERIOD
) {
404 ret
= value_set_u64(cw
, event
, "perf_period", sample
->period
);
409 if (type
& PERF_SAMPLE_WEIGHT
) {
410 ret
= value_set_u64(cw
, event
, "perf_weight", sample
->weight
);
415 if (type
& PERF_SAMPLE_DATA_SRC
) {
416 ret
= value_set_u64(cw
, event
, "perf_data_src",
422 if (type
& PERF_SAMPLE_TRANSACTION
) {
423 ret
= value_set_u64(cw
, event
, "perf_transaction",
424 sample
->transaction
);
432 static int ctf_stream__flush(struct ctf_stream
*cs
)
437 err
= bt_ctf_stream_flush(cs
->stream
);
439 pr_err("CTF stream %d flush failed\n", cs
->cpu
);
441 pr("Flush stream for cpu %d (%u samples)\n",
450 static struct ctf_stream
*ctf_stream__create(struct ctf_writer
*cw
, int cpu
)
452 struct ctf_stream
*cs
;
453 struct bt_ctf_field
*pkt_ctx
= NULL
;
454 struct bt_ctf_field
*cpu_field
= NULL
;
455 struct bt_ctf_stream
*stream
= NULL
;
458 cs
= zalloc(sizeof(*cs
));
460 pr_err("Failed to allocate ctf stream\n");
464 stream
= bt_ctf_writer_create_stream(cw
->writer
, cw
->stream_class
);
466 pr_err("Failed to create CTF stream\n");
470 pkt_ctx
= bt_ctf_stream_get_packet_context(stream
);
472 pr_err("Failed to obtain packet context\n");
476 cpu_field
= bt_ctf_field_structure_get_field(pkt_ctx
, "cpu_id");
477 bt_ctf_field_put(pkt_ctx
);
479 pr_err("Failed to obtain cpu field\n");
483 ret
= bt_ctf_field_unsigned_integer_set_value(cpu_field
, (u32
) cpu
);
485 pr_err("Failed to update CPU number\n");
489 bt_ctf_field_put(cpu_field
);
497 bt_ctf_field_put(cpu_field
);
499 bt_ctf_stream_put(stream
);
505 static void ctf_stream__delete(struct ctf_stream
*cs
)
508 bt_ctf_stream_put(cs
->stream
);
513 static struct ctf_stream
*ctf_stream(struct ctf_writer
*cw
, int cpu
)
515 struct ctf_stream
*cs
= cw
->stream
[cpu
];
518 cs
= ctf_stream__create(cw
, cpu
);
519 cw
->stream
[cpu
] = cs
;
525 static int get_sample_cpu(struct ctf_writer
*cw
, struct perf_sample
*sample
,
526 struct perf_evsel
*evsel
)
530 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CPU
)
533 if (cpu
> cw
->stream_cnt
) {
534 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
535 cpu
, cw
->stream_cnt
);
542 #define STREAM_FLUSH_COUNT 100000
545 * Currently we have no other way to determine the
546 * time for the stream flush other than keep track
547 * of the number of events and check it against
550 static bool is_flush_needed(struct ctf_stream
*cs
)
552 return cs
->count
>= STREAM_FLUSH_COUNT
;
555 static int process_sample_event(struct perf_tool
*tool
,
556 union perf_event
*_event __maybe_unused
,
557 struct perf_sample
*sample
,
558 struct perf_evsel
*evsel
,
559 struct machine
*machine __maybe_unused
)
561 struct convert
*c
= container_of(tool
, struct convert
, tool
);
562 struct evsel_priv
*priv
= evsel
->priv
;
563 struct ctf_writer
*cw
= &c
->writer
;
564 struct ctf_stream
*cs
;
565 struct bt_ctf_event_class
*event_class
;
566 struct bt_ctf_event
*event
;
569 if (WARN_ONCE(!priv
, "Failed to setup all events.\n"))
572 event_class
= priv
->event_class
;
576 c
->events_size
+= _event
->header
.size
;
578 pr_time2(sample
->time
, "sample %" PRIu64
"\n", c
->events_count
);
580 event
= bt_ctf_event_create(event_class
);
582 pr_err("Failed to create an CTF event\n");
586 bt_ctf_clock_set_time(cw
->clock
, sample
->time
);
588 ret
= add_generic_values(cw
, event
, evsel
, sample
);
592 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) {
593 ret
= add_tracepoint_values(cw
, event_class
, event
,
599 cs
= ctf_stream(cw
, get_sample_cpu(cw
, sample
, evsel
));
601 if (is_flush_needed(cs
))
602 ctf_stream__flush(cs
);
605 bt_ctf_stream_append_event(cs
->stream
, event
);
608 bt_ctf_event_put(event
);
612 /* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
613 static char *change_name(char *name
, char *orig_name
, int dup
)
615 char *new_name
= NULL
;
624 * Add '_' prefix to potential keywork. According to
625 * Mathieu Desnoyers (https://lkml.org/lkml/2015/1/23/652),
626 * futher CTF spec updating may require us to use '$'.
629 len
= strlen(name
) + sizeof("_");
631 len
= strlen(orig_name
) + sizeof("_dupl_X");
633 new_name
= malloc(len
);
638 snprintf(new_name
, len
, "_%s", name
);
640 snprintf(new_name
, len
, "%s_dupl_%d", orig_name
, dup
);
643 if (name
!= orig_name
)
648 static int event_class_add_field(struct bt_ctf_event_class
*event_class
,
649 struct bt_ctf_field_type
*type
,
650 struct format_field
*field
)
652 struct bt_ctf_field_type
*t
= NULL
;
657 /* alias was already assigned */
658 if (field
->alias
!= field
->name
)
659 return bt_ctf_event_class_add_field(event_class
, type
,
660 (char *)field
->alias
);
664 /* If 'name' is a keywork, add prefix. */
665 if (bt_ctf_validate_identifier(name
))
666 name
= change_name(name
, field
->name
, -1);
669 pr_err("Failed to fix invalid identifier.");
672 while ((t
= bt_ctf_event_class_get_field_by_name(event_class
, name
))) {
673 bt_ctf_field_type_put(t
);
674 name
= change_name(name
, field
->name
, dup
++);
676 pr_err("Failed to create dup name for '%s'\n", field
->name
);
681 ret
= bt_ctf_event_class_add_field(event_class
, type
, name
);
688 static int add_tracepoint_fields_types(struct ctf_writer
*cw
,
689 struct format_field
*fields
,
690 struct bt_ctf_event_class
*event_class
)
692 struct format_field
*field
;
695 for (field
= fields
; field
; field
= field
->next
) {
696 struct bt_ctf_field_type
*type
;
697 unsigned long flags
= field
->flags
;
699 pr2(" field '%s'\n", field
->name
);
701 type
= get_tracepoint_field_type(cw
, field
);
706 * A string is an array of chars. For this we use the string
707 * type and don't care that it is an array. What we don't
708 * support is an array of strings.
710 if (flags
& FIELD_IS_STRING
)
711 flags
&= ~FIELD_IS_ARRAY
;
713 if (flags
& FIELD_IS_ARRAY
)
714 type
= bt_ctf_field_type_array_create(type
, field
->arraylen
);
716 ret
= event_class_add_field(event_class
, type
, field
);
718 if (flags
& FIELD_IS_ARRAY
)
719 bt_ctf_field_type_put(type
);
722 pr_err("Failed to add field '%s': %d\n",
731 static int add_tracepoint_types(struct ctf_writer
*cw
,
732 struct perf_evsel
*evsel
,
733 struct bt_ctf_event_class
*class)
735 struct format_field
*common_fields
= evsel
->tp_format
->format
.common_fields
;
736 struct format_field
*fields
= evsel
->tp_format
->format
.fields
;
739 ret
= add_tracepoint_fields_types(cw
, common_fields
, class);
741 ret
= add_tracepoint_fields_types(cw
, fields
, class);
746 static int add_generic_types(struct ctf_writer
*cw
, struct perf_evsel
*evsel
,
747 struct bt_ctf_event_class
*event_class
)
749 u64 type
= evsel
->attr
.sample_type
;
753 * PERF_SAMPLE_TIME - not needed as we have it in
755 * PERF_SAMPLE_READ - TODO
756 * PERF_SAMPLE_CALLCHAIN - TODO
757 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
758 * PERF_SAMPLE_BRANCH_STACK - TODO
759 * PERF_SAMPLE_REGS_USER - TODO
760 * PERF_SAMPLE_STACK_USER - TODO
763 #define ADD_FIELD(cl, t, n) \
765 pr2(" field '%s'\n", n); \
766 if (bt_ctf_event_class_add_field(cl, t, n)) { \
767 pr_err("Failed to add field '%s';\n", n); \
772 if (type
& PERF_SAMPLE_IP
)
773 ADD_FIELD(event_class
, cw
->data
.u64_hex
, "perf_ip");
775 if (type
& PERF_SAMPLE_TID
) {
776 ADD_FIELD(event_class
, cw
->data
.s32
, "perf_tid");
777 ADD_FIELD(event_class
, cw
->data
.s32
, "perf_pid");
780 if ((type
& PERF_SAMPLE_ID
) ||
781 (type
& PERF_SAMPLE_IDENTIFIER
))
782 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_id");
784 if (type
& PERF_SAMPLE_STREAM_ID
)
785 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_stream_id");
787 if (type
& PERF_SAMPLE_PERIOD
)
788 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_period");
790 if (type
& PERF_SAMPLE_WEIGHT
)
791 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_weight");
793 if (type
& PERF_SAMPLE_DATA_SRC
)
794 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_data_src");
796 if (type
& PERF_SAMPLE_TRANSACTION
)
797 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_transaction");
803 static int add_event(struct ctf_writer
*cw
, struct perf_evsel
*evsel
)
805 struct bt_ctf_event_class
*event_class
;
806 struct evsel_priv
*priv
;
807 const char *name
= perf_evsel__name(evsel
);
810 pr("Adding event '%s' (type %d)\n", name
, evsel
->attr
.type
);
812 event_class
= bt_ctf_event_class_create(name
);
816 ret
= add_generic_types(cw
, evsel
, event_class
);
820 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) {
821 ret
= add_tracepoint_types(cw
, evsel
, event_class
);
826 ret
= bt_ctf_stream_class_add_event_class(cw
->stream_class
, event_class
);
828 pr("Failed to add event class into stream.\n");
832 priv
= malloc(sizeof(*priv
));
836 priv
->event_class
= event_class
;
841 bt_ctf_event_class_put(event_class
);
842 pr_err("Failed to add event '%s'.\n", name
);
846 static int setup_events(struct ctf_writer
*cw
, struct perf_session
*session
)
848 struct perf_evlist
*evlist
= session
->evlist
;
849 struct perf_evsel
*evsel
;
852 evlist__for_each(evlist
, evsel
) {
853 ret
= add_event(cw
, evsel
);
860 static int setup_streams(struct ctf_writer
*cw
, struct perf_session
*session
)
862 struct ctf_stream
**stream
;
863 struct perf_header
*ph
= &session
->header
;
867 * Try to get the number of cpus used in the data file,
868 * if not present fallback to the MAX_CPUS.
870 ncpus
= ph
->env
.nr_cpus_avail
?: MAX_CPUS
;
872 stream
= zalloc(sizeof(*stream
) * ncpus
);
874 pr_err("Failed to allocate streams.\n");
879 cw
->stream_cnt
= ncpus
;
883 static void free_streams(struct ctf_writer
*cw
)
887 for (cpu
= 0; cpu
< cw
->stream_cnt
; cpu
++)
888 ctf_stream__delete(cw
->stream
[cpu
]);
893 static int ctf_writer__setup_env(struct ctf_writer
*cw
,
894 struct perf_session
*session
)
896 struct perf_header
*header
= &session
->header
;
897 struct bt_ctf_writer
*writer
= cw
->writer
;
899 #define ADD(__n, __v) \
901 if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \
905 ADD("host", header
->env
.hostname
);
906 ADD("sysname", "Linux");
907 ADD("release", header
->env
.os_release
);
908 ADD("version", header
->env
.version
);
909 ADD("machine", header
->env
.arch
);
910 ADD("domain", "kernel");
911 ADD("tracer_name", "perf");
917 static int ctf_writer__setup_clock(struct ctf_writer
*cw
)
919 struct bt_ctf_clock
*clock
= cw
->clock
;
921 bt_ctf_clock_set_description(clock
, "perf clock");
923 #define SET(__n, __v) \
925 if (bt_ctf_clock_set_##__n(clock, __v)) \
929 SET(frequency
, 1000000000);
939 static struct bt_ctf_field_type
*create_int_type(int size
, bool sign
, bool hex
)
941 struct bt_ctf_field_type
*type
;
943 type
= bt_ctf_field_type_integer_create(size
);
948 bt_ctf_field_type_integer_set_signed(type
, 1))
952 bt_ctf_field_type_integer_set_base(type
, BT_CTF_INTEGER_BASE_HEXADECIMAL
))
955 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
956 size
, sign
? "un" : "", hex
? "hex" : "");
960 bt_ctf_field_type_put(type
);
964 static void ctf_writer__cleanup_data(struct ctf_writer
*cw
)
968 for (i
= 0; i
< ARRAY_SIZE(cw
->data
.array
); i
++)
969 bt_ctf_field_type_put(cw
->data
.array
[i
]);
972 static int ctf_writer__init_data(struct ctf_writer
*cw
)
974 #define CREATE_INT_TYPE(type, size, sign, hex) \
976 (type) = create_int_type(size, sign, hex); \
981 CREATE_INT_TYPE(cw
->data
.s64
, 64, true, false);
982 CREATE_INT_TYPE(cw
->data
.u64
, 64, false, false);
983 CREATE_INT_TYPE(cw
->data
.s32
, 32, true, false);
984 CREATE_INT_TYPE(cw
->data
.u32
, 32, false, false);
985 CREATE_INT_TYPE(cw
->data
.u64_hex
, 64, false, true);
987 cw
->data
.string
= bt_ctf_field_type_string_create();
992 ctf_writer__cleanup_data(cw
);
993 pr_err("Failed to create data types.\n");
997 static void ctf_writer__cleanup(struct ctf_writer
*cw
)
999 ctf_writer__cleanup_data(cw
);
1001 bt_ctf_clock_put(cw
->clock
);
1003 bt_ctf_stream_class_put(cw
->stream_class
);
1004 bt_ctf_writer_put(cw
->writer
);
1006 /* and NULL all the pointers */
1007 memset(cw
, 0, sizeof(*cw
));
1010 static int ctf_writer__init(struct ctf_writer
*cw
, const char *path
)
1012 struct bt_ctf_writer
*writer
;
1013 struct bt_ctf_stream_class
*stream_class
;
1014 struct bt_ctf_clock
*clock
;
1015 struct bt_ctf_field_type
*pkt_ctx_type
;
1019 writer
= bt_ctf_writer_create(path
);
1023 cw
->writer
= writer
;
1026 clock
= bt_ctf_clock_create("perf_clock");
1028 pr("Failed to create CTF clock.\n");
1034 if (ctf_writer__setup_clock(cw
)) {
1035 pr("Failed to setup CTF clock.\n");
1039 /* CTF stream class */
1040 stream_class
= bt_ctf_stream_class_create("perf_stream");
1041 if (!stream_class
) {
1042 pr("Failed to create CTF stream class.\n");
1046 cw
->stream_class
= stream_class
;
1048 /* CTF clock stream setup */
1049 if (bt_ctf_stream_class_set_clock(stream_class
, clock
)) {
1050 pr("Failed to assign CTF clock to stream class.\n");
1054 if (ctf_writer__init_data(cw
))
1057 /* Add cpu_id for packet context */
1058 pkt_ctx_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
1062 ret
= bt_ctf_field_type_structure_add_field(pkt_ctx_type
, cw
->data
.u32
, "cpu_id");
1063 bt_ctf_field_type_put(pkt_ctx_type
);
1067 /* CTF clock writer setup */
1068 if (bt_ctf_writer_add_clock(writer
, clock
)) {
1069 pr("Failed to assign CTF clock to writer.\n");
1076 ctf_writer__cleanup(cw
);
1078 pr_err("Failed to setup CTF writer.\n");
1082 static int ctf_writer__flush_streams(struct ctf_writer
*cw
)
1086 for (cpu
= 0; cpu
< cw
->stream_cnt
&& !ret
; cpu
++)
1087 ret
= ctf_stream__flush(cw
->stream
[cpu
]);
1092 static int convert__config(const char *var
, const char *value
, void *cb
)
1094 struct convert
*c
= cb
;
1096 if (!strcmp(var
, "convert.queue-size")) {
1097 c
->queue_size
= perf_config_u64(var
, value
);
1101 return perf_default_config(var
, value
, cb
);
1104 int bt_convert__perf2ctf(const char *input
, const char *path
, bool force
)
1106 struct perf_session
*session
;
1107 struct perf_data_file file
= {
1109 .mode
= PERF_DATA_MODE_READ
,
1112 struct convert c
= {
1114 .sample
= process_sample_event
,
1115 .mmap
= perf_event__process_mmap
,
1116 .mmap2
= perf_event__process_mmap2
,
1117 .comm
= perf_event__process_comm
,
1118 .exit
= perf_event__process_exit
,
1119 .fork
= perf_event__process_fork
,
1120 .lost
= perf_event__process_lost
,
1121 .tracing_data
= perf_event__process_tracing_data
,
1122 .build_id
= perf_event__process_build_id
,
1123 .ordered_events
= true,
1124 .ordering_requires_timestamps
= true,
1127 struct ctf_writer
*cw
= &c
.writer
;
1130 perf_config(convert__config
, &c
);
1133 if (ctf_writer__init(cw
, path
))
1136 /* perf.data session */
1137 session
= perf_session__new(&file
, 0, &c
.tool
);
1142 ordered_events__set_alloc_size(&session
->ordered_events
,
1146 /* CTF writer env/clock setup */
1147 if (ctf_writer__setup_env(cw
, session
))
1150 /* CTF events setup */
1151 if (setup_events(cw
, session
))
1154 if (setup_streams(cw
, session
))
1157 err
= perf_session__process_events(session
);
1159 err
= ctf_writer__flush_streams(cw
);
1161 pr_err("Error during conversion.\n");
1164 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
1168 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64
" samples) ]\n",
1169 (double) c
.events_size
/ 1024.0 / 1024.0,
1172 perf_session__delete(session
);
1173 ctf_writer__cleanup(cw
);
1178 perf_session__delete(session
);
1180 ctf_writer__cleanup(cw
);
1181 pr_err("Error during conversion setup.\n");