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
*u32_hex
;
67 struct bt_ctf_field_type
*u64_hex
;
69 struct bt_ctf_field_type
*array
[6];
74 struct perf_tool tool
;
75 struct ctf_writer writer
;
80 /* Ordered events configured queue size. */
84 static int value_set(struct bt_ctf_field_type
*type
,
85 struct bt_ctf_event
*event
,
86 const char *name
, u64 val
)
88 struct bt_ctf_field
*field
;
89 bool sign
= bt_ctf_field_type_integer_get_signed(type
);
92 field
= bt_ctf_field_create(type
);
94 pr_err("failed to create a field %s\n", name
);
99 ret
= bt_ctf_field_signed_integer_set_value(field
, val
);
101 pr_err("failed to set field value %s\n", name
);
105 ret
= bt_ctf_field_unsigned_integer_set_value(field
, val
);
107 pr_err("failed to set field value %s\n", name
);
112 ret
= bt_ctf_event_set_payload(event
, name
, field
);
114 pr_err("failed to set payload %s\n", name
);
118 pr2(" SET [%s = %" PRIu64
"]\n", name
, val
);
121 bt_ctf_field_put(field
);
125 #define __FUNC_VALUE_SET(_name, _val_type) \
126 static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
127 struct bt_ctf_event *event, \
131 struct bt_ctf_field_type *type = cw->data._name; \
132 return value_set(type, event, name, (u64) val); \
135 #define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
141 __FUNC_VALUE_SET(u64_hex
, u64
)
143 static struct bt_ctf_field_type
*
144 get_tracepoint_field_type(struct ctf_writer
*cw
, struct format_field
*field
)
146 unsigned long flags
= field
->flags
;
148 if (flags
& FIELD_IS_STRING
)
149 return cw
->data
.string
;
151 if (!(flags
& FIELD_IS_SIGNED
)) {
152 /* unsigned long are mostly pointers */
153 if (flags
& FIELD_IS_LONG
|| flags
& FIELD_IS_POINTER
)
154 return cw
->data
.u64_hex
;
157 if (flags
& FIELD_IS_SIGNED
) {
158 if (field
->size
== 8)
164 if (field
->size
== 8)
170 static unsigned long long adjust_signedness(unsigned long long value_int
, int size
)
172 unsigned long long value_mask
;
175 * value_mask = (1 << (size * 8 - 1)) - 1.
176 * Directly set value_mask for code readers.
180 value_mask
= 0x7fULL
;
183 value_mask
= 0x7fffULL
;
186 value_mask
= 0x7fffffffULL
;
190 * For 64 bit value, return it self. There is no need
199 /* If it is a positive value, don't adjust. */
200 if ((value_int
& (~0ULL - value_mask
)) == 0)
203 /* Fill upper part of value_int with 1 to make it a negative long long. */
204 return (value_int
& value_mask
) | ~value_mask
;
207 static int add_tracepoint_field_value(struct ctf_writer
*cw
,
208 struct bt_ctf_event_class
*event_class
,
209 struct bt_ctf_event
*event
,
210 struct perf_sample
*sample
,
211 struct format_field
*fmtf
)
213 struct bt_ctf_field_type
*type
;
214 struct bt_ctf_field
*array_field
;
215 struct bt_ctf_field
*field
;
216 const char *name
= fmtf
->name
;
217 void *data
= sample
->raw_data
;
218 unsigned long flags
= fmtf
->flags
;
219 unsigned int n_items
;
226 offset
= fmtf
->offset
;
228 if (flags
& FIELD_IS_STRING
)
229 flags
&= ~FIELD_IS_ARRAY
;
231 if (flags
& FIELD_IS_DYNAMIC
) {
232 unsigned long long tmp_val
;
234 tmp_val
= pevent_read_number(fmtf
->event
->pevent
,
241 if (flags
& FIELD_IS_ARRAY
) {
243 type
= bt_ctf_event_class_get_field_by_name(
245 array_field
= bt_ctf_field_create(type
);
246 bt_ctf_field_type_put(type
);
248 pr_err("Failed to create array type %s\n", name
);
252 len
= fmtf
->size
/ fmtf
->arraylen
;
253 n_items
= fmtf
->arraylen
;
259 type
= get_tracepoint_field_type(cw
, fmtf
);
261 for (i
= 0; i
< n_items
; i
++) {
262 if (flags
& FIELD_IS_ARRAY
)
263 field
= bt_ctf_field_array_get_field(array_field
, i
);
265 field
= bt_ctf_field_create(type
);
268 pr_err("failed to create a field %s\n", name
);
272 if (flags
& FIELD_IS_STRING
)
273 ret
= bt_ctf_field_string_set_value(field
,
274 data
+ offset
+ i
* len
);
276 unsigned long long value_int
;
278 value_int
= pevent_read_number(
280 data
+ offset
+ i
* len
, len
);
282 if (!(flags
& FIELD_IS_SIGNED
))
283 ret
= bt_ctf_field_unsigned_integer_set_value(
286 ret
= bt_ctf_field_signed_integer_set_value(
287 field
, adjust_signedness(value_int
, len
));
291 pr_err("failed to set file value %s\n", name
);
294 if (!(flags
& FIELD_IS_ARRAY
)) {
295 ret
= bt_ctf_event_set_payload(event
, name
, field
);
297 pr_err("failed to set payload %s\n", name
);
301 bt_ctf_field_put(field
);
303 if (flags
& FIELD_IS_ARRAY
) {
304 ret
= bt_ctf_event_set_payload(event
, name
, array_field
);
306 pr_err("Failed add payload array %s\n", name
);
309 bt_ctf_field_put(array_field
);
314 bt_ctf_field_put(field
);
318 static int add_tracepoint_fields_values(struct ctf_writer
*cw
,
319 struct bt_ctf_event_class
*event_class
,
320 struct bt_ctf_event
*event
,
321 struct format_field
*fields
,
322 struct perf_sample
*sample
)
324 struct format_field
*field
;
327 for (field
= fields
; field
; field
= field
->next
) {
328 ret
= add_tracepoint_field_value(cw
, event_class
, event
, sample
,
336 static int add_tracepoint_values(struct ctf_writer
*cw
,
337 struct bt_ctf_event_class
*event_class
,
338 struct bt_ctf_event
*event
,
339 struct perf_evsel
*evsel
,
340 struct perf_sample
*sample
)
342 struct format_field
*common_fields
= evsel
->tp_format
->format
.common_fields
;
343 struct format_field
*fields
= evsel
->tp_format
->format
.fields
;
346 ret
= add_tracepoint_fields_values(cw
, event_class
, event
,
347 common_fields
, sample
);
349 ret
= add_tracepoint_fields_values(cw
, event_class
, event
,
355 static int add_generic_values(struct ctf_writer
*cw
,
356 struct bt_ctf_event
*event
,
357 struct perf_evsel
*evsel
,
358 struct perf_sample
*sample
)
360 u64 type
= evsel
->attr
.sample_type
;
365 * PERF_SAMPLE_TIME - not needed as we have it in
367 * PERF_SAMPLE_READ - TODO
368 * PERF_SAMPLE_CALLCHAIN - TODO
369 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
370 * PERF_SAMPLE_BRANCH_STACK - TODO
371 * PERF_SAMPLE_REGS_USER - TODO
372 * PERF_SAMPLE_STACK_USER - TODO
375 if (type
& PERF_SAMPLE_IP
) {
376 ret
= value_set_u64_hex(cw
, event
, "perf_ip", sample
->ip
);
381 if (type
& PERF_SAMPLE_TID
) {
382 ret
= value_set_s32(cw
, event
, "perf_tid", sample
->tid
);
386 ret
= value_set_s32(cw
, event
, "perf_pid", sample
->pid
);
391 if ((type
& PERF_SAMPLE_ID
) ||
392 (type
& PERF_SAMPLE_IDENTIFIER
)) {
393 ret
= value_set_u64(cw
, event
, "perf_id", sample
->id
);
398 if (type
& PERF_SAMPLE_STREAM_ID
) {
399 ret
= value_set_u64(cw
, event
, "perf_stream_id", sample
->stream_id
);
404 if (type
& PERF_SAMPLE_PERIOD
) {
405 ret
= value_set_u64(cw
, event
, "perf_period", sample
->period
);
410 if (type
& PERF_SAMPLE_WEIGHT
) {
411 ret
= value_set_u64(cw
, event
, "perf_weight", sample
->weight
);
416 if (type
& PERF_SAMPLE_DATA_SRC
) {
417 ret
= value_set_u64(cw
, event
, "perf_data_src",
423 if (type
& PERF_SAMPLE_TRANSACTION
) {
424 ret
= value_set_u64(cw
, event
, "perf_transaction",
425 sample
->transaction
);
433 static int ctf_stream__flush(struct ctf_stream
*cs
)
438 err
= bt_ctf_stream_flush(cs
->stream
);
440 pr_err("CTF stream %d flush failed\n", cs
->cpu
);
442 pr("Flush stream for cpu %d (%u samples)\n",
451 static struct ctf_stream
*ctf_stream__create(struct ctf_writer
*cw
, int cpu
)
453 struct ctf_stream
*cs
;
454 struct bt_ctf_field
*pkt_ctx
= NULL
;
455 struct bt_ctf_field
*cpu_field
= NULL
;
456 struct bt_ctf_stream
*stream
= NULL
;
459 cs
= zalloc(sizeof(*cs
));
461 pr_err("Failed to allocate ctf stream\n");
465 stream
= bt_ctf_writer_create_stream(cw
->writer
, cw
->stream_class
);
467 pr_err("Failed to create CTF stream\n");
471 pkt_ctx
= bt_ctf_stream_get_packet_context(stream
);
473 pr_err("Failed to obtain packet context\n");
477 cpu_field
= bt_ctf_field_structure_get_field(pkt_ctx
, "cpu_id");
478 bt_ctf_field_put(pkt_ctx
);
480 pr_err("Failed to obtain cpu field\n");
484 ret
= bt_ctf_field_unsigned_integer_set_value(cpu_field
, (u32
) cpu
);
486 pr_err("Failed to update CPU number\n");
490 bt_ctf_field_put(cpu_field
);
498 bt_ctf_field_put(cpu_field
);
500 bt_ctf_stream_put(stream
);
506 static void ctf_stream__delete(struct ctf_stream
*cs
)
509 bt_ctf_stream_put(cs
->stream
);
514 static struct ctf_stream
*ctf_stream(struct ctf_writer
*cw
, int cpu
)
516 struct ctf_stream
*cs
= cw
->stream
[cpu
];
519 cs
= ctf_stream__create(cw
, cpu
);
520 cw
->stream
[cpu
] = cs
;
526 static int get_sample_cpu(struct ctf_writer
*cw
, struct perf_sample
*sample
,
527 struct perf_evsel
*evsel
)
531 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CPU
)
534 if (cpu
> cw
->stream_cnt
) {
535 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
536 cpu
, cw
->stream_cnt
);
543 #define STREAM_FLUSH_COUNT 100000
546 * Currently we have no other way to determine the
547 * time for the stream flush other than keep track
548 * of the number of events and check it against
551 static bool is_flush_needed(struct ctf_stream
*cs
)
553 return cs
->count
>= STREAM_FLUSH_COUNT
;
556 static int process_sample_event(struct perf_tool
*tool
,
557 union perf_event
*_event __maybe_unused
,
558 struct perf_sample
*sample
,
559 struct perf_evsel
*evsel
,
560 struct machine
*machine __maybe_unused
)
562 struct convert
*c
= container_of(tool
, struct convert
, tool
);
563 struct evsel_priv
*priv
= evsel
->priv
;
564 struct ctf_writer
*cw
= &c
->writer
;
565 struct ctf_stream
*cs
;
566 struct bt_ctf_event_class
*event_class
;
567 struct bt_ctf_event
*event
;
570 if (WARN_ONCE(!priv
, "Failed to setup all events.\n"))
573 event_class
= priv
->event_class
;
577 c
->events_size
+= _event
->header
.size
;
579 pr_time2(sample
->time
, "sample %" PRIu64
"\n", c
->events_count
);
581 event
= bt_ctf_event_create(event_class
);
583 pr_err("Failed to create an CTF event\n");
587 bt_ctf_clock_set_time(cw
->clock
, sample
->time
);
589 ret
= add_generic_values(cw
, event
, evsel
, sample
);
593 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) {
594 ret
= add_tracepoint_values(cw
, event_class
, event
,
600 cs
= ctf_stream(cw
, get_sample_cpu(cw
, sample
, evsel
));
602 if (is_flush_needed(cs
))
603 ctf_stream__flush(cs
);
606 bt_ctf_stream_append_event(cs
->stream
, event
);
609 bt_ctf_event_put(event
);
613 /* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
614 static char *change_name(char *name
, char *orig_name
, int dup
)
616 char *new_name
= NULL
;
625 * Add '_' prefix to potential keywork. According to
626 * Mathieu Desnoyers (https://lkml.org/lkml/2015/1/23/652),
627 * futher CTF spec updating may require us to use '$'.
630 len
= strlen(name
) + sizeof("_");
632 len
= strlen(orig_name
) + sizeof("_dupl_X");
634 new_name
= malloc(len
);
639 snprintf(new_name
, len
, "_%s", name
);
641 snprintf(new_name
, len
, "%s_dupl_%d", orig_name
, dup
);
644 if (name
!= orig_name
)
649 static int event_class_add_field(struct bt_ctf_event_class
*event_class
,
650 struct bt_ctf_field_type
*type
,
651 struct format_field
*field
)
653 struct bt_ctf_field_type
*t
= NULL
;
658 /* alias was already assigned */
659 if (field
->alias
!= field
->name
)
660 return bt_ctf_event_class_add_field(event_class
, type
,
661 (char *)field
->alias
);
665 /* If 'name' is a keywork, add prefix. */
666 if (bt_ctf_validate_identifier(name
))
667 name
= change_name(name
, field
->name
, -1);
670 pr_err("Failed to fix invalid identifier.");
673 while ((t
= bt_ctf_event_class_get_field_by_name(event_class
, name
))) {
674 bt_ctf_field_type_put(t
);
675 name
= change_name(name
, field
->name
, dup
++);
677 pr_err("Failed to create dup name for '%s'\n", field
->name
);
682 ret
= bt_ctf_event_class_add_field(event_class
, type
, name
);
689 static int add_tracepoint_fields_types(struct ctf_writer
*cw
,
690 struct format_field
*fields
,
691 struct bt_ctf_event_class
*event_class
)
693 struct format_field
*field
;
696 for (field
= fields
; field
; field
= field
->next
) {
697 struct bt_ctf_field_type
*type
;
698 unsigned long flags
= field
->flags
;
700 pr2(" field '%s'\n", field
->name
);
702 type
= get_tracepoint_field_type(cw
, field
);
707 * A string is an array of chars. For this we use the string
708 * type and don't care that it is an array. What we don't
709 * support is an array of strings.
711 if (flags
& FIELD_IS_STRING
)
712 flags
&= ~FIELD_IS_ARRAY
;
714 if (flags
& FIELD_IS_ARRAY
)
715 type
= bt_ctf_field_type_array_create(type
, field
->arraylen
);
717 ret
= event_class_add_field(event_class
, type
, field
);
719 if (flags
& FIELD_IS_ARRAY
)
720 bt_ctf_field_type_put(type
);
723 pr_err("Failed to add field '%s': %d\n",
732 static int add_tracepoint_types(struct ctf_writer
*cw
,
733 struct perf_evsel
*evsel
,
734 struct bt_ctf_event_class
*class)
736 struct format_field
*common_fields
= evsel
->tp_format
->format
.common_fields
;
737 struct format_field
*fields
= evsel
->tp_format
->format
.fields
;
740 ret
= add_tracepoint_fields_types(cw
, common_fields
, class);
742 ret
= add_tracepoint_fields_types(cw
, fields
, class);
747 static int add_generic_types(struct ctf_writer
*cw
, struct perf_evsel
*evsel
,
748 struct bt_ctf_event_class
*event_class
)
750 u64 type
= evsel
->attr
.sample_type
;
754 * PERF_SAMPLE_TIME - not needed as we have it in
756 * PERF_SAMPLE_READ - TODO
757 * PERF_SAMPLE_CALLCHAIN - TODO
758 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
759 * PERF_SAMPLE_BRANCH_STACK - TODO
760 * PERF_SAMPLE_REGS_USER - TODO
761 * PERF_SAMPLE_STACK_USER - TODO
764 #define ADD_FIELD(cl, t, n) \
766 pr2(" field '%s'\n", n); \
767 if (bt_ctf_event_class_add_field(cl, t, n)) { \
768 pr_err("Failed to add field '%s';\n", n); \
773 if (type
& PERF_SAMPLE_IP
)
774 ADD_FIELD(event_class
, cw
->data
.u64_hex
, "perf_ip");
776 if (type
& PERF_SAMPLE_TID
) {
777 ADD_FIELD(event_class
, cw
->data
.s32
, "perf_tid");
778 ADD_FIELD(event_class
, cw
->data
.s32
, "perf_pid");
781 if ((type
& PERF_SAMPLE_ID
) ||
782 (type
& PERF_SAMPLE_IDENTIFIER
))
783 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_id");
785 if (type
& PERF_SAMPLE_STREAM_ID
)
786 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_stream_id");
788 if (type
& PERF_SAMPLE_PERIOD
)
789 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_period");
791 if (type
& PERF_SAMPLE_WEIGHT
)
792 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_weight");
794 if (type
& PERF_SAMPLE_DATA_SRC
)
795 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_data_src");
797 if (type
& PERF_SAMPLE_TRANSACTION
)
798 ADD_FIELD(event_class
, cw
->data
.u64
, "perf_transaction");
804 static int add_event(struct ctf_writer
*cw
, struct perf_evsel
*evsel
)
806 struct bt_ctf_event_class
*event_class
;
807 struct evsel_priv
*priv
;
808 const char *name
= perf_evsel__name(evsel
);
811 pr("Adding event '%s' (type %d)\n", name
, evsel
->attr
.type
);
813 event_class
= bt_ctf_event_class_create(name
);
817 ret
= add_generic_types(cw
, evsel
, event_class
);
821 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) {
822 ret
= add_tracepoint_types(cw
, evsel
, event_class
);
827 ret
= bt_ctf_stream_class_add_event_class(cw
->stream_class
, event_class
);
829 pr("Failed to add event class into stream.\n");
833 priv
= malloc(sizeof(*priv
));
837 priv
->event_class
= event_class
;
842 bt_ctf_event_class_put(event_class
);
843 pr_err("Failed to add event '%s'.\n", name
);
847 static int setup_events(struct ctf_writer
*cw
, struct perf_session
*session
)
849 struct perf_evlist
*evlist
= session
->evlist
;
850 struct perf_evsel
*evsel
;
853 evlist__for_each(evlist
, evsel
) {
854 ret
= add_event(cw
, evsel
);
861 static int setup_streams(struct ctf_writer
*cw
, struct perf_session
*session
)
863 struct ctf_stream
**stream
;
864 struct perf_header
*ph
= &session
->header
;
868 * Try to get the number of cpus used in the data file,
869 * if not present fallback to the MAX_CPUS.
871 ncpus
= ph
->env
.nr_cpus_avail
?: MAX_CPUS
;
873 stream
= zalloc(sizeof(*stream
) * ncpus
);
875 pr_err("Failed to allocate streams.\n");
880 cw
->stream_cnt
= ncpus
;
884 static void free_streams(struct ctf_writer
*cw
)
888 for (cpu
= 0; cpu
< cw
->stream_cnt
; cpu
++)
889 ctf_stream__delete(cw
->stream
[cpu
]);
894 static int ctf_writer__setup_env(struct ctf_writer
*cw
,
895 struct perf_session
*session
)
897 struct perf_header
*header
= &session
->header
;
898 struct bt_ctf_writer
*writer
= cw
->writer
;
900 #define ADD(__n, __v) \
902 if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \
906 ADD("host", header
->env
.hostname
);
907 ADD("sysname", "Linux");
908 ADD("release", header
->env
.os_release
);
909 ADD("version", header
->env
.version
);
910 ADD("machine", header
->env
.arch
);
911 ADD("domain", "kernel");
912 ADD("tracer_name", "perf");
918 static int ctf_writer__setup_clock(struct ctf_writer
*cw
)
920 struct bt_ctf_clock
*clock
= cw
->clock
;
922 bt_ctf_clock_set_description(clock
, "perf clock");
924 #define SET(__n, __v) \
926 if (bt_ctf_clock_set_##__n(clock, __v)) \
930 SET(frequency
, 1000000000);
940 static struct bt_ctf_field_type
*create_int_type(int size
, bool sign
, bool hex
)
942 struct bt_ctf_field_type
*type
;
944 type
= bt_ctf_field_type_integer_create(size
);
949 bt_ctf_field_type_integer_set_signed(type
, 1))
953 bt_ctf_field_type_integer_set_base(type
, BT_CTF_INTEGER_BASE_HEXADECIMAL
))
956 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
957 size
, sign
? "un" : "", hex
? "hex" : "");
961 bt_ctf_field_type_put(type
);
965 static void ctf_writer__cleanup_data(struct ctf_writer
*cw
)
969 for (i
= 0; i
< ARRAY_SIZE(cw
->data
.array
); i
++)
970 bt_ctf_field_type_put(cw
->data
.array
[i
]);
973 static int ctf_writer__init_data(struct ctf_writer
*cw
)
975 #define CREATE_INT_TYPE(type, size, sign, hex) \
977 (type) = create_int_type(size, sign, hex); \
982 CREATE_INT_TYPE(cw
->data
.s64
, 64, true, false);
983 CREATE_INT_TYPE(cw
->data
.u64
, 64, false, false);
984 CREATE_INT_TYPE(cw
->data
.s32
, 32, true, false);
985 CREATE_INT_TYPE(cw
->data
.u32
, 32, false, false);
986 CREATE_INT_TYPE(cw
->data
.u32_hex
, 32, false, true);
987 CREATE_INT_TYPE(cw
->data
.u64_hex
, 64, false, true);
989 cw
->data
.string
= bt_ctf_field_type_string_create();
994 ctf_writer__cleanup_data(cw
);
995 pr_err("Failed to create data types.\n");
999 static void ctf_writer__cleanup(struct ctf_writer
*cw
)
1001 ctf_writer__cleanup_data(cw
);
1003 bt_ctf_clock_put(cw
->clock
);
1005 bt_ctf_stream_class_put(cw
->stream_class
);
1006 bt_ctf_writer_put(cw
->writer
);
1008 /* and NULL all the pointers */
1009 memset(cw
, 0, sizeof(*cw
));
1012 static int ctf_writer__init(struct ctf_writer
*cw
, const char *path
)
1014 struct bt_ctf_writer
*writer
;
1015 struct bt_ctf_stream_class
*stream_class
;
1016 struct bt_ctf_clock
*clock
;
1017 struct bt_ctf_field_type
*pkt_ctx_type
;
1021 writer
= bt_ctf_writer_create(path
);
1025 cw
->writer
= writer
;
1028 clock
= bt_ctf_clock_create("perf_clock");
1030 pr("Failed to create CTF clock.\n");
1036 if (ctf_writer__setup_clock(cw
)) {
1037 pr("Failed to setup CTF clock.\n");
1041 /* CTF stream class */
1042 stream_class
= bt_ctf_stream_class_create("perf_stream");
1043 if (!stream_class
) {
1044 pr("Failed to create CTF stream class.\n");
1048 cw
->stream_class
= stream_class
;
1050 /* CTF clock stream setup */
1051 if (bt_ctf_stream_class_set_clock(stream_class
, clock
)) {
1052 pr("Failed to assign CTF clock to stream class.\n");
1056 if (ctf_writer__init_data(cw
))
1059 /* Add cpu_id for packet context */
1060 pkt_ctx_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
1064 ret
= bt_ctf_field_type_structure_add_field(pkt_ctx_type
, cw
->data
.u32
, "cpu_id");
1065 bt_ctf_field_type_put(pkt_ctx_type
);
1069 /* CTF clock writer setup */
1070 if (bt_ctf_writer_add_clock(writer
, clock
)) {
1071 pr("Failed to assign CTF clock to writer.\n");
1078 ctf_writer__cleanup(cw
);
1080 pr_err("Failed to setup CTF writer.\n");
1084 static int ctf_writer__flush_streams(struct ctf_writer
*cw
)
1088 for (cpu
= 0; cpu
< cw
->stream_cnt
&& !ret
; cpu
++)
1089 ret
= ctf_stream__flush(cw
->stream
[cpu
]);
1094 static int convert__config(const char *var
, const char *value
, void *cb
)
1096 struct convert
*c
= cb
;
1098 if (!strcmp(var
, "convert.queue-size")) {
1099 c
->queue_size
= perf_config_u64(var
, value
);
1103 return perf_default_config(var
, value
, cb
);
1106 int bt_convert__perf2ctf(const char *input
, const char *path
, bool force
)
1108 struct perf_session
*session
;
1109 struct perf_data_file file
= {
1111 .mode
= PERF_DATA_MODE_READ
,
1114 struct convert c
= {
1116 .sample
= process_sample_event
,
1117 .mmap
= perf_event__process_mmap
,
1118 .mmap2
= perf_event__process_mmap2
,
1119 .comm
= perf_event__process_comm
,
1120 .exit
= perf_event__process_exit
,
1121 .fork
= perf_event__process_fork
,
1122 .lost
= perf_event__process_lost
,
1123 .tracing_data
= perf_event__process_tracing_data
,
1124 .build_id
= perf_event__process_build_id
,
1125 .ordered_events
= true,
1126 .ordering_requires_timestamps
= true,
1129 struct ctf_writer
*cw
= &c
.writer
;
1132 perf_config(convert__config
, &c
);
1135 if (ctf_writer__init(cw
, path
))
1138 /* perf.data session */
1139 session
= perf_session__new(&file
, 0, &c
.tool
);
1144 ordered_events__set_alloc_size(&session
->ordered_events
,
1148 /* CTF writer env/clock setup */
1149 if (ctf_writer__setup_env(cw
, session
))
1152 /* CTF events setup */
1153 if (setup_events(cw
, session
))
1156 if (setup_streams(cw
, session
))
1159 err
= perf_session__process_events(session
);
1161 err
= ctf_writer__flush_streams(cw
);
1163 pr_err("Error during conversion.\n");
1166 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
1170 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64
" samples) ]\n",
1171 (double) c
.events_size
/ 1024.0 / 1024.0,
1174 perf_session__delete(session
);
1175 ctf_writer__cleanup(cw
);
1180 perf_session__delete(session
);
1182 ctf_writer__cleanup(cw
);
1183 pr_err("Error during conversion setup.\n");