Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * CTF writing support via babeltrace.
4 *
5 * Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com>
6 * Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7 */
8
9#include <errno.h>
10#include <inttypes.h>
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 "asm/bug.h"
23#include "data-convert.h"
24#include "session.h"
25#include "debug.h"
26#include "tool.h"
27#include "evlist.h"
28#include "evsel.h"
29#include "machine.h"
30#include "config.h"
31#include <linux/ctype.h>
32#include <linux/err.h>
33#include <linux/time64.h>
34#include "util.h"
35#include "clockid.h"
36#include "util/sample.h"
37
38#ifdef HAVE_LIBTRACEEVENT
39#include <traceevent/event-parse.h>
40#endif
41
42#define pr_N(n, fmt, ...) \
43 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
44
45#define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
46#define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
47
48#define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
49
50struct evsel_priv {
51 struct bt_ctf_event_class *event_class;
52};
53
54#define MAX_CPUS 4096
55
56struct ctf_stream {
57 struct bt_ctf_stream *stream;
58 int cpu;
59 u32 count;
60};
61
62struct ctf_writer {
63 /* writer primitives */
64 struct bt_ctf_writer *writer;
65 struct ctf_stream **stream;
66 int stream_cnt;
67 struct bt_ctf_stream_class *stream_class;
68 struct bt_ctf_clock *clock;
69
70 /* data types */
71 union {
72 struct {
73 struct bt_ctf_field_type *s64;
74 struct bt_ctf_field_type *u64;
75 struct bt_ctf_field_type *s32;
76 struct bt_ctf_field_type *u32;
77 struct bt_ctf_field_type *string;
78 struct bt_ctf_field_type *u32_hex;
79 struct bt_ctf_field_type *u64_hex;
80 };
81 struct bt_ctf_field_type *array[6];
82 } data;
83 struct bt_ctf_event_class *comm_class;
84 struct bt_ctf_event_class *exit_class;
85 struct bt_ctf_event_class *fork_class;
86 struct bt_ctf_event_class *mmap_class;
87 struct bt_ctf_event_class *mmap2_class;
88};
89
90struct convert {
91 struct perf_tool tool;
92 struct ctf_writer writer;
93
94 u64 events_size;
95 u64 events_count;
96 u64 non_sample_count;
97
98 /* Ordered events configured queue size. */
99 u64 queue_size;
100};
101
102static int value_set(struct bt_ctf_field_type *type,
103 struct bt_ctf_event *event,
104 const char *name, u64 val)
105{
106 struct bt_ctf_field *field;
107 bool sign = bt_ctf_field_type_integer_get_signed(type);
108 int ret;
109
110 field = bt_ctf_field_create(type);
111 if (!field) {
112 pr_err("failed to create a field %s\n", name);
113 return -1;
114 }
115
116 if (sign) {
117 ret = bt_ctf_field_signed_integer_set_value(field, val);
118 if (ret) {
119 pr_err("failed to set field value %s\n", name);
120 goto err;
121 }
122 } else {
123 ret = bt_ctf_field_unsigned_integer_set_value(field, val);
124 if (ret) {
125 pr_err("failed to set field value %s\n", name);
126 goto err;
127 }
128 }
129
130 ret = bt_ctf_event_set_payload(event, name, field);
131 if (ret) {
132 pr_err("failed to set payload %s\n", name);
133 goto err;
134 }
135
136 pr2(" SET [%s = %" PRIu64 "]\n", name, val);
137
138err:
139 bt_ctf_field_put(field);
140 return ret;
141}
142
143#define __FUNC_VALUE_SET(_name, _val_type) \
144static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
145 struct bt_ctf_event *event, \
146 const char *name, \
147 _val_type val) \
148{ \
149 struct bt_ctf_field_type *type = cw->data._name; \
150 return value_set(type, event, name, (u64) val); \
151}
152
153#define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
154
155FUNC_VALUE_SET(s32)
156FUNC_VALUE_SET(u32)
157FUNC_VALUE_SET(s64)
158FUNC_VALUE_SET(u64)
159__FUNC_VALUE_SET(u64_hex, u64)
160
161static int string_set_value(struct bt_ctf_field *field, const char *string);
162static __maybe_unused int
163value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event,
164 const char *name, const char *string)
165{
166 struct bt_ctf_field_type *type = cw->data.string;
167 struct bt_ctf_field *field;
168 int ret = 0;
169
170 field = bt_ctf_field_create(type);
171 if (!field) {
172 pr_err("failed to create a field %s\n", name);
173 return -1;
174 }
175
176 ret = string_set_value(field, string);
177 if (ret) {
178 pr_err("failed to set value %s\n", name);
179 goto err_put_field;
180 }
181
182 ret = bt_ctf_event_set_payload(event, name, field);
183 if (ret)
184 pr_err("failed to set payload %s\n", name);
185
186err_put_field:
187 bt_ctf_field_put(field);
188 return ret;
189}
190
191static struct bt_ctf_field_type*
192get_tracepoint_field_type(struct ctf_writer *cw, struct tep_format_field *field)
193{
194 unsigned long flags = field->flags;
195
196 if (flags & TEP_FIELD_IS_STRING)
197 return cw->data.string;
198
199 if (!(flags & TEP_FIELD_IS_SIGNED)) {
200 /* unsigned long are mostly pointers */
201 if (flags & TEP_FIELD_IS_LONG || flags & TEP_FIELD_IS_POINTER)
202 return cw->data.u64_hex;
203 }
204
205 if (flags & TEP_FIELD_IS_SIGNED) {
206 if (field->size == 8)
207 return cw->data.s64;
208 else
209 return cw->data.s32;
210 }
211
212 if (field->size == 8)
213 return cw->data.u64;
214 else
215 return cw->data.u32;
216}
217
218static unsigned long long adjust_signedness(unsigned long long value_int, int size)
219{
220 unsigned long long value_mask;
221
222 /*
223 * value_mask = (1 << (size * 8 - 1)) - 1.
224 * Directly set value_mask for code readers.
225 */
226 switch (size) {
227 case 1:
228 value_mask = 0x7fULL;
229 break;
230 case 2:
231 value_mask = 0x7fffULL;
232 break;
233 case 4:
234 value_mask = 0x7fffffffULL;
235 break;
236 case 8:
237 /*
238 * For 64 bit value, return it self. There is no need
239 * to fill high bit.
240 */
241 /* Fall through */
242 default:
243 /* BUG! */
244 return value_int;
245 }
246
247 /* If it is a positive value, don't adjust. */
248 if ((value_int & (~0ULL - value_mask)) == 0)
249 return value_int;
250
251 /* Fill upper part of value_int with 1 to make it a negative long long. */
252 return (value_int & value_mask) | ~value_mask;
253}
254
255static int string_set_value(struct bt_ctf_field *field, const char *string)
256{
257 char *buffer = NULL;
258 size_t len = strlen(string), i, p;
259 int err;
260
261 for (i = p = 0; i < len; i++, p++) {
262 if (isprint(string[i])) {
263 if (!buffer)
264 continue;
265 buffer[p] = string[i];
266 } else {
267 char numstr[5];
268
269 snprintf(numstr, sizeof(numstr), "\\x%02x",
270 (unsigned int)(string[i]) & 0xff);
271
272 if (!buffer) {
273 buffer = zalloc(i + (len - i) * 4 + 2);
274 if (!buffer) {
275 pr_err("failed to set unprintable string '%s'\n", string);
276 return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING");
277 }
278 if (i > 0)
279 strncpy(buffer, string, i);
280 }
281 memcpy(buffer + p, numstr, 4);
282 p += 3;
283 }
284 }
285
286 if (!buffer)
287 return bt_ctf_field_string_set_value(field, string);
288 err = bt_ctf_field_string_set_value(field, buffer);
289 free(buffer);
290 return err;
291}
292
293static int add_tracepoint_field_value(struct ctf_writer *cw,
294 struct bt_ctf_event_class *event_class,
295 struct bt_ctf_event *event,
296 struct perf_sample *sample,
297 struct tep_format_field *fmtf)
298{
299 struct bt_ctf_field_type *type;
300 struct bt_ctf_field *array_field;
301 struct bt_ctf_field *field;
302 const char *name = fmtf->name;
303 void *data = sample->raw_data;
304 unsigned long flags = fmtf->flags;
305 unsigned int n_items;
306 unsigned int i;
307 unsigned int offset;
308 unsigned int len;
309 int ret;
310
311 name = fmtf->alias;
312 offset = fmtf->offset;
313 len = fmtf->size;
314 if (flags & TEP_FIELD_IS_STRING)
315 flags &= ~TEP_FIELD_IS_ARRAY;
316
317 if (flags & TEP_FIELD_IS_DYNAMIC) {
318 unsigned long long tmp_val;
319
320 tmp_val = tep_read_number(fmtf->event->tep,
321 data + offset, len);
322 offset = tmp_val;
323 len = offset >> 16;
324 offset &= 0xffff;
325#ifdef HAVE_LIBTRACEEVENT_TEP_FIELD_IS_RELATIVE
326 if (flags & TEP_FIELD_IS_RELATIVE)
327 offset += fmtf->offset + fmtf->size;
328#endif
329 }
330
331 if (flags & TEP_FIELD_IS_ARRAY) {
332
333 type = bt_ctf_event_class_get_field_by_name(
334 event_class, name);
335 array_field = bt_ctf_field_create(type);
336 bt_ctf_field_type_put(type);
337 if (!array_field) {
338 pr_err("Failed to create array type %s\n", name);
339 return -1;
340 }
341
342 len = fmtf->size / fmtf->arraylen;
343 n_items = fmtf->arraylen;
344 } else {
345 n_items = 1;
346 array_field = NULL;
347 }
348
349 type = get_tracepoint_field_type(cw, fmtf);
350
351 for (i = 0; i < n_items; i++) {
352 if (flags & TEP_FIELD_IS_ARRAY)
353 field = bt_ctf_field_array_get_field(array_field, i);
354 else
355 field = bt_ctf_field_create(type);
356
357 if (!field) {
358 pr_err("failed to create a field %s\n", name);
359 return -1;
360 }
361
362 if (flags & TEP_FIELD_IS_STRING)
363 ret = string_set_value(field, data + offset + i * len);
364 else {
365 unsigned long long value_int;
366
367 value_int = tep_read_number(
368 fmtf->event->tep,
369 data + offset + i * len, len);
370
371 if (!(flags & TEP_FIELD_IS_SIGNED))
372 ret = bt_ctf_field_unsigned_integer_set_value(
373 field, value_int);
374 else
375 ret = bt_ctf_field_signed_integer_set_value(
376 field, adjust_signedness(value_int, len));
377 }
378
379 if (ret) {
380 pr_err("failed to set file value %s\n", name);
381 goto err_put_field;
382 }
383 if (!(flags & TEP_FIELD_IS_ARRAY)) {
384 ret = bt_ctf_event_set_payload(event, name, field);
385 if (ret) {
386 pr_err("failed to set payload %s\n", name);
387 goto err_put_field;
388 }
389 }
390 bt_ctf_field_put(field);
391 }
392 if (flags & TEP_FIELD_IS_ARRAY) {
393 ret = bt_ctf_event_set_payload(event, name, array_field);
394 if (ret) {
395 pr_err("Failed add payload array %s\n", name);
396 return -1;
397 }
398 bt_ctf_field_put(array_field);
399 }
400 return 0;
401
402err_put_field:
403 bt_ctf_field_put(field);
404 return -1;
405}
406
407static int add_tracepoint_fields_values(struct ctf_writer *cw,
408 struct bt_ctf_event_class *event_class,
409 struct bt_ctf_event *event,
410 struct tep_format_field *fields,
411 struct perf_sample *sample)
412{
413 struct tep_format_field *field;
414 int ret;
415
416 for (field = fields; field; field = field->next) {
417 ret = add_tracepoint_field_value(cw, event_class, event, sample,
418 field);
419 if (ret)
420 return -1;
421 }
422 return 0;
423}
424
425static int add_tracepoint_values(struct ctf_writer *cw,
426 struct bt_ctf_event_class *event_class,
427 struct bt_ctf_event *event,
428 struct evsel *evsel,
429 struct perf_sample *sample)
430{
431 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
432 struct tep_format_field *fields = evsel->tp_format->format.fields;
433 int ret;
434
435 ret = add_tracepoint_fields_values(cw, event_class, event,
436 common_fields, sample);
437 if (!ret)
438 ret = add_tracepoint_fields_values(cw, event_class, event,
439 fields, sample);
440
441 return ret;
442}
443
444static int
445add_bpf_output_values(struct bt_ctf_event_class *event_class,
446 struct bt_ctf_event *event,
447 struct perf_sample *sample)
448{
449 struct bt_ctf_field_type *len_type, *seq_type;
450 struct bt_ctf_field *len_field, *seq_field;
451 unsigned int raw_size = sample->raw_size;
452 unsigned int nr_elements = raw_size / sizeof(u32);
453 unsigned int i;
454 int ret;
455
456 if (nr_elements * sizeof(u32) != raw_size)
457 pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n",
458 raw_size, nr_elements * sizeof(u32) - raw_size);
459
460 len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len");
461 len_field = bt_ctf_field_create(len_type);
462 if (!len_field) {
463 pr_err("failed to create 'raw_len' for bpf output event\n");
464 ret = -1;
465 goto put_len_type;
466 }
467
468 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
469 if (ret) {
470 pr_err("failed to set field value for raw_len\n");
471 goto put_len_field;
472 }
473 ret = bt_ctf_event_set_payload(event, "raw_len", len_field);
474 if (ret) {
475 pr_err("failed to set payload to raw_len\n");
476 goto put_len_field;
477 }
478
479 seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data");
480 seq_field = bt_ctf_field_create(seq_type);
481 if (!seq_field) {
482 pr_err("failed to create 'raw_data' for bpf output event\n");
483 ret = -1;
484 goto put_seq_type;
485 }
486
487 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
488 if (ret) {
489 pr_err("failed to set length of 'raw_data'\n");
490 goto put_seq_field;
491 }
492
493 for (i = 0; i < nr_elements; i++) {
494 struct bt_ctf_field *elem_field =
495 bt_ctf_field_sequence_get_field(seq_field, i);
496
497 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
498 ((u32 *)(sample->raw_data))[i]);
499
500 bt_ctf_field_put(elem_field);
501 if (ret) {
502 pr_err("failed to set raw_data[%d]\n", i);
503 goto put_seq_field;
504 }
505 }
506
507 ret = bt_ctf_event_set_payload(event, "raw_data", seq_field);
508 if (ret)
509 pr_err("failed to set payload for raw_data\n");
510
511put_seq_field:
512 bt_ctf_field_put(seq_field);
513put_seq_type:
514 bt_ctf_field_type_put(seq_type);
515put_len_field:
516 bt_ctf_field_put(len_field);
517put_len_type:
518 bt_ctf_field_type_put(len_type);
519 return ret;
520}
521
522static int
523add_callchain_output_values(struct bt_ctf_event_class *event_class,
524 struct bt_ctf_event *event,
525 struct ip_callchain *callchain)
526{
527 struct bt_ctf_field_type *len_type, *seq_type;
528 struct bt_ctf_field *len_field, *seq_field;
529 unsigned int nr_elements = callchain->nr;
530 unsigned int i;
531 int ret;
532
533 len_type = bt_ctf_event_class_get_field_by_name(
534 event_class, "perf_callchain_size");
535 len_field = bt_ctf_field_create(len_type);
536 if (!len_field) {
537 pr_err("failed to create 'perf_callchain_size' for callchain output event\n");
538 ret = -1;
539 goto put_len_type;
540 }
541
542 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
543 if (ret) {
544 pr_err("failed to set field value for perf_callchain_size\n");
545 goto put_len_field;
546 }
547 ret = bt_ctf_event_set_payload(event, "perf_callchain_size", len_field);
548 if (ret) {
549 pr_err("failed to set payload to perf_callchain_size\n");
550 goto put_len_field;
551 }
552
553 seq_type = bt_ctf_event_class_get_field_by_name(
554 event_class, "perf_callchain");
555 seq_field = bt_ctf_field_create(seq_type);
556 if (!seq_field) {
557 pr_err("failed to create 'perf_callchain' for callchain output event\n");
558 ret = -1;
559 goto put_seq_type;
560 }
561
562 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
563 if (ret) {
564 pr_err("failed to set length of 'perf_callchain'\n");
565 goto put_seq_field;
566 }
567
568 for (i = 0; i < nr_elements; i++) {
569 struct bt_ctf_field *elem_field =
570 bt_ctf_field_sequence_get_field(seq_field, i);
571
572 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
573 ((u64 *)(callchain->ips))[i]);
574
575 bt_ctf_field_put(elem_field);
576 if (ret) {
577 pr_err("failed to set callchain[%d]\n", i);
578 goto put_seq_field;
579 }
580 }
581
582 ret = bt_ctf_event_set_payload(event, "perf_callchain", seq_field);
583 if (ret)
584 pr_err("failed to set payload for raw_data\n");
585
586put_seq_field:
587 bt_ctf_field_put(seq_field);
588put_seq_type:
589 bt_ctf_field_type_put(seq_type);
590put_len_field:
591 bt_ctf_field_put(len_field);
592put_len_type:
593 bt_ctf_field_type_put(len_type);
594 return ret;
595}
596
597static int add_generic_values(struct ctf_writer *cw,
598 struct bt_ctf_event *event,
599 struct evsel *evsel,
600 struct perf_sample *sample)
601{
602 u64 type = evsel->core.attr.sample_type;
603 int ret;
604
605 /*
606 * missing:
607 * PERF_SAMPLE_TIME - not needed as we have it in
608 * ctf event header
609 * PERF_SAMPLE_READ - TODO
610 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
611 * PERF_SAMPLE_BRANCH_STACK - TODO
612 * PERF_SAMPLE_REGS_USER - TODO
613 * PERF_SAMPLE_STACK_USER - TODO
614 */
615
616 if (type & PERF_SAMPLE_IP) {
617 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
618 if (ret)
619 return -1;
620 }
621
622 if (type & PERF_SAMPLE_TID) {
623 ret = value_set_s32(cw, event, "perf_tid", sample->tid);
624 if (ret)
625 return -1;
626
627 ret = value_set_s32(cw, event, "perf_pid", sample->pid);
628 if (ret)
629 return -1;
630 }
631
632 if ((type & PERF_SAMPLE_ID) ||
633 (type & PERF_SAMPLE_IDENTIFIER)) {
634 ret = value_set_u64(cw, event, "perf_id", sample->id);
635 if (ret)
636 return -1;
637 }
638
639 if (type & PERF_SAMPLE_STREAM_ID) {
640 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
641 if (ret)
642 return -1;
643 }
644
645 if (type & PERF_SAMPLE_PERIOD) {
646 ret = value_set_u64(cw, event, "perf_period", sample->period);
647 if (ret)
648 return -1;
649 }
650
651 if (type & PERF_SAMPLE_WEIGHT) {
652 ret = value_set_u64(cw, event, "perf_weight", sample->weight);
653 if (ret)
654 return -1;
655 }
656
657 if (type & PERF_SAMPLE_DATA_SRC) {
658 ret = value_set_u64(cw, event, "perf_data_src",
659 sample->data_src);
660 if (ret)
661 return -1;
662 }
663
664 if (type & PERF_SAMPLE_TRANSACTION) {
665 ret = value_set_u64(cw, event, "perf_transaction",
666 sample->transaction);
667 if (ret)
668 return -1;
669 }
670
671 return 0;
672}
673
674static int ctf_stream__flush(struct ctf_stream *cs)
675{
676 int err = 0;
677
678 if (cs) {
679 err = bt_ctf_stream_flush(cs->stream);
680 if (err)
681 pr_err("CTF stream %d flush failed\n", cs->cpu);
682
683 pr("Flush stream for cpu %d (%u samples)\n",
684 cs->cpu, cs->count);
685
686 cs->count = 0;
687 }
688
689 return err;
690}
691
692static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
693{
694 struct ctf_stream *cs;
695 struct bt_ctf_field *pkt_ctx = NULL;
696 struct bt_ctf_field *cpu_field = NULL;
697 struct bt_ctf_stream *stream = NULL;
698 int ret;
699
700 cs = zalloc(sizeof(*cs));
701 if (!cs) {
702 pr_err("Failed to allocate ctf stream\n");
703 return NULL;
704 }
705
706 stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
707 if (!stream) {
708 pr_err("Failed to create CTF stream\n");
709 goto out;
710 }
711
712 pkt_ctx = bt_ctf_stream_get_packet_context(stream);
713 if (!pkt_ctx) {
714 pr_err("Failed to obtain packet context\n");
715 goto out;
716 }
717
718 cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
719 bt_ctf_field_put(pkt_ctx);
720 if (!cpu_field) {
721 pr_err("Failed to obtain cpu field\n");
722 goto out;
723 }
724
725 ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
726 if (ret) {
727 pr_err("Failed to update CPU number\n");
728 goto out;
729 }
730
731 bt_ctf_field_put(cpu_field);
732
733 cs->cpu = cpu;
734 cs->stream = stream;
735 return cs;
736
737out:
738 if (cpu_field)
739 bt_ctf_field_put(cpu_field);
740 if (stream)
741 bt_ctf_stream_put(stream);
742
743 free(cs);
744 return NULL;
745}
746
747static void ctf_stream__delete(struct ctf_stream *cs)
748{
749 if (cs) {
750 bt_ctf_stream_put(cs->stream);
751 free(cs);
752 }
753}
754
755static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
756{
757 struct ctf_stream *cs = cw->stream[cpu];
758
759 if (!cs) {
760 cs = ctf_stream__create(cw, cpu);
761 cw->stream[cpu] = cs;
762 }
763
764 return cs;
765}
766
767static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
768 struct evsel *evsel)
769{
770 int cpu = 0;
771
772 if (evsel->core.attr.sample_type & PERF_SAMPLE_CPU)
773 cpu = sample->cpu;
774
775 if (cpu > cw->stream_cnt) {
776 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
777 cpu, cw->stream_cnt);
778 cpu = 0;
779 }
780
781 return cpu;
782}
783
784#define STREAM_FLUSH_COUNT 100000
785
786/*
787 * Currently we have no other way to determine the
788 * time for the stream flush other than keep track
789 * of the number of events and check it against
790 * threshold.
791 */
792static bool is_flush_needed(struct ctf_stream *cs)
793{
794 return cs->count >= STREAM_FLUSH_COUNT;
795}
796
797static int process_sample_event(struct perf_tool *tool,
798 union perf_event *_event,
799 struct perf_sample *sample,
800 struct evsel *evsel,
801 struct machine *machine __maybe_unused)
802{
803 struct convert *c = container_of(tool, struct convert, tool);
804 struct evsel_priv *priv = evsel->priv;
805 struct ctf_writer *cw = &c->writer;
806 struct ctf_stream *cs;
807 struct bt_ctf_event_class *event_class;
808 struct bt_ctf_event *event;
809 int ret;
810 unsigned long type = evsel->core.attr.sample_type;
811
812 if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
813 return 0;
814
815 event_class = priv->event_class;
816
817 /* update stats */
818 c->events_count++;
819 c->events_size += _event->header.size;
820
821 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
822
823 event = bt_ctf_event_create(event_class);
824 if (!event) {
825 pr_err("Failed to create an CTF event\n");
826 return -1;
827 }
828
829 bt_ctf_clock_set_time(cw->clock, sample->time);
830
831 ret = add_generic_values(cw, event, evsel, sample);
832 if (ret)
833 return -1;
834
835 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
836 ret = add_tracepoint_values(cw, event_class, event,
837 evsel, sample);
838 if (ret)
839 return -1;
840 }
841
842 if (type & PERF_SAMPLE_CALLCHAIN) {
843 ret = add_callchain_output_values(event_class,
844 event, sample->callchain);
845 if (ret)
846 return -1;
847 }
848
849 if (evsel__is_bpf_output(evsel)) {
850 ret = add_bpf_output_values(event_class, event, sample);
851 if (ret)
852 return -1;
853 }
854
855 cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel));
856 if (cs) {
857 if (is_flush_needed(cs))
858 ctf_stream__flush(cs);
859
860 cs->count++;
861 bt_ctf_stream_append_event(cs->stream, event);
862 }
863
864 bt_ctf_event_put(event);
865 return cs ? 0 : -1;
866}
867
868#define __NON_SAMPLE_SET_FIELD(_name, _type, _field) \
869do { \
870 ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
871 if (ret) \
872 return -1; \
873} while(0)
874
875#define __FUNC_PROCESS_NON_SAMPLE(_name, body) \
876static int process_##_name##_event(struct perf_tool *tool, \
877 union perf_event *_event, \
878 struct perf_sample *sample, \
879 struct machine *machine) \
880{ \
881 struct convert *c = container_of(tool, struct convert, tool);\
882 struct ctf_writer *cw = &c->writer; \
883 struct bt_ctf_event_class *event_class = cw->_name##_class;\
884 struct bt_ctf_event *event; \
885 struct ctf_stream *cs; \
886 int ret; \
887 \
888 c->non_sample_count++; \
889 c->events_size += _event->header.size; \
890 event = bt_ctf_event_create(event_class); \
891 if (!event) { \
892 pr_err("Failed to create an CTF event\n"); \
893 return -1; \
894 } \
895 \
896 bt_ctf_clock_set_time(cw->clock, sample->time); \
897 body \
898 cs = ctf_stream(cw, 0); \
899 if (cs) { \
900 if (is_flush_needed(cs)) \
901 ctf_stream__flush(cs); \
902 \
903 cs->count++; \
904 bt_ctf_stream_append_event(cs->stream, event); \
905 } \
906 bt_ctf_event_put(event); \
907 \
908 return perf_event__process_##_name(tool, _event, sample, machine);\
909}
910
911__FUNC_PROCESS_NON_SAMPLE(comm,
912 __NON_SAMPLE_SET_FIELD(comm, u32, pid);
913 __NON_SAMPLE_SET_FIELD(comm, u32, tid);
914 __NON_SAMPLE_SET_FIELD(comm, string, comm);
915)
916__FUNC_PROCESS_NON_SAMPLE(fork,
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);
922)
923
924__FUNC_PROCESS_NON_SAMPLE(exit,
925 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
926 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
927 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
928 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
929 __NON_SAMPLE_SET_FIELD(fork, u64, time);
930)
931__FUNC_PROCESS_NON_SAMPLE(mmap,
932 __NON_SAMPLE_SET_FIELD(mmap, u32, pid);
933 __NON_SAMPLE_SET_FIELD(mmap, u32, tid);
934 __NON_SAMPLE_SET_FIELD(mmap, u64_hex, start);
935 __NON_SAMPLE_SET_FIELD(mmap, string, filename);
936)
937__FUNC_PROCESS_NON_SAMPLE(mmap2,
938 __NON_SAMPLE_SET_FIELD(mmap2, u32, pid);
939 __NON_SAMPLE_SET_FIELD(mmap2, u32, tid);
940 __NON_SAMPLE_SET_FIELD(mmap2, u64_hex, start);
941 __NON_SAMPLE_SET_FIELD(mmap2, string, filename);
942)
943#undef __NON_SAMPLE_SET_FIELD
944#undef __FUNC_PROCESS_NON_SAMPLE
945
946/* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
947static char *change_name(char *name, char *orig_name, int dup)
948{
949 char *new_name = NULL;
950 size_t len;
951
952 if (!name)
953 name = orig_name;
954
955 if (dup >= 10)
956 goto out;
957 /*
958 * Add '_' prefix to potential keywork. According to
959 * Mathieu Desnoyers (https://lore.kernel.org/lkml/1074266107.40857.1422045946295.JavaMail.zimbra@efficios.com),
960 * further CTF spec updating may require us to use '$'.
961 */
962 if (dup < 0)
963 len = strlen(name) + sizeof("_");
964 else
965 len = strlen(orig_name) + sizeof("_dupl_X");
966
967 new_name = malloc(len);
968 if (!new_name)
969 goto out;
970
971 if (dup < 0)
972 snprintf(new_name, len, "_%s", name);
973 else
974 snprintf(new_name, len, "%s_dupl_%d", orig_name, dup);
975
976out:
977 if (name != orig_name)
978 free(name);
979 return new_name;
980}
981
982static int event_class_add_field(struct bt_ctf_event_class *event_class,
983 struct bt_ctf_field_type *type,
984 struct tep_format_field *field)
985{
986 struct bt_ctf_field_type *t = NULL;
987 char *name;
988 int dup = 1;
989 int ret;
990
991 /* alias was already assigned */
992 if (field->alias != field->name)
993 return bt_ctf_event_class_add_field(event_class, type,
994 (char *)field->alias);
995
996 name = field->name;
997
998 /* If 'name' is a keywork, add prefix. */
999 if (bt_ctf_validate_identifier(name))
1000 name = change_name(name, field->name, -1);
1001
1002 if (!name) {
1003 pr_err("Failed to fix invalid identifier.");
1004 return -1;
1005 }
1006 while ((t = bt_ctf_event_class_get_field_by_name(event_class, name))) {
1007 bt_ctf_field_type_put(t);
1008 name = change_name(name, field->name, dup++);
1009 if (!name) {
1010 pr_err("Failed to create dup name for '%s'\n", field->name);
1011 return -1;
1012 }
1013 }
1014
1015 ret = bt_ctf_event_class_add_field(event_class, type, name);
1016 if (!ret)
1017 field->alias = name;
1018
1019 return ret;
1020}
1021
1022static int add_tracepoint_fields_types(struct ctf_writer *cw,
1023 struct tep_format_field *fields,
1024 struct bt_ctf_event_class *event_class)
1025{
1026 struct tep_format_field *field;
1027 int ret;
1028
1029 for (field = fields; field; field = field->next) {
1030 struct bt_ctf_field_type *type;
1031 unsigned long flags = field->flags;
1032
1033 pr2(" field '%s'\n", field->name);
1034
1035 type = get_tracepoint_field_type(cw, field);
1036 if (!type)
1037 return -1;
1038
1039 /*
1040 * A string is an array of chars. For this we use the string
1041 * type and don't care that it is an array. What we don't
1042 * support is an array of strings.
1043 */
1044 if (flags & TEP_FIELD_IS_STRING)
1045 flags &= ~TEP_FIELD_IS_ARRAY;
1046
1047 if (flags & TEP_FIELD_IS_ARRAY)
1048 type = bt_ctf_field_type_array_create(type, field->arraylen);
1049
1050 ret = event_class_add_field(event_class, type, field);
1051
1052 if (flags & TEP_FIELD_IS_ARRAY)
1053 bt_ctf_field_type_put(type);
1054
1055 if (ret) {
1056 pr_err("Failed to add field '%s': %d\n",
1057 field->name, ret);
1058 return -1;
1059 }
1060 }
1061
1062 return 0;
1063}
1064
1065static int add_tracepoint_types(struct ctf_writer *cw,
1066 struct evsel *evsel,
1067 struct bt_ctf_event_class *class)
1068{
1069 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
1070 struct tep_format_field *fields = evsel->tp_format->format.fields;
1071 int ret;
1072
1073 ret = add_tracepoint_fields_types(cw, common_fields, class);
1074 if (!ret)
1075 ret = add_tracepoint_fields_types(cw, fields, class);
1076
1077 return ret;
1078}
1079
1080static int add_bpf_output_types(struct ctf_writer *cw,
1081 struct bt_ctf_event_class *class)
1082{
1083 struct bt_ctf_field_type *len_type = cw->data.u32;
1084 struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex;
1085 struct bt_ctf_field_type *seq_type;
1086 int ret;
1087
1088 ret = bt_ctf_event_class_add_field(class, len_type, "raw_len");
1089 if (ret)
1090 return ret;
1091
1092 seq_type = bt_ctf_field_type_sequence_create(seq_base_type, "raw_len");
1093 if (!seq_type)
1094 return -1;
1095
1096 return bt_ctf_event_class_add_field(class, seq_type, "raw_data");
1097}
1098
1099static int add_generic_types(struct ctf_writer *cw, struct evsel *evsel,
1100 struct bt_ctf_event_class *event_class)
1101{
1102 u64 type = evsel->core.attr.sample_type;
1103
1104 /*
1105 * missing:
1106 * PERF_SAMPLE_TIME - not needed as we have it in
1107 * ctf event header
1108 * PERF_SAMPLE_READ - TODO
1109 * PERF_SAMPLE_CALLCHAIN - TODO
1110 * PERF_SAMPLE_RAW - tracepoint fields and BPF output
1111 * are handled separately
1112 * PERF_SAMPLE_BRANCH_STACK - TODO
1113 * PERF_SAMPLE_REGS_USER - TODO
1114 * PERF_SAMPLE_STACK_USER - TODO
1115 */
1116
1117#define ADD_FIELD(cl, t, n) \
1118 do { \
1119 pr2(" field '%s'\n", n); \
1120 if (bt_ctf_event_class_add_field(cl, t, n)) { \
1121 pr_err("Failed to add field '%s';\n", n); \
1122 return -1; \
1123 } \
1124 } while (0)
1125
1126 if (type & PERF_SAMPLE_IP)
1127 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
1128
1129 if (type & PERF_SAMPLE_TID) {
1130 ADD_FIELD(event_class, cw->data.s32, "perf_tid");
1131 ADD_FIELD(event_class, cw->data.s32, "perf_pid");
1132 }
1133
1134 if ((type & PERF_SAMPLE_ID) ||
1135 (type & PERF_SAMPLE_IDENTIFIER))
1136 ADD_FIELD(event_class, cw->data.u64, "perf_id");
1137
1138 if (type & PERF_SAMPLE_STREAM_ID)
1139 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
1140
1141 if (type & PERF_SAMPLE_PERIOD)
1142 ADD_FIELD(event_class, cw->data.u64, "perf_period");
1143
1144 if (type & PERF_SAMPLE_WEIGHT)
1145 ADD_FIELD(event_class, cw->data.u64, "perf_weight");
1146
1147 if (type & PERF_SAMPLE_DATA_SRC)
1148 ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
1149
1150 if (type & PERF_SAMPLE_TRANSACTION)
1151 ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
1152
1153 if (type & PERF_SAMPLE_CALLCHAIN) {
1154 ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size");
1155 ADD_FIELD(event_class,
1156 bt_ctf_field_type_sequence_create(
1157 cw->data.u64_hex, "perf_callchain_size"),
1158 "perf_callchain");
1159 }
1160
1161#undef ADD_FIELD
1162 return 0;
1163}
1164
1165static int add_event(struct ctf_writer *cw, struct evsel *evsel)
1166{
1167 struct bt_ctf_event_class *event_class;
1168 struct evsel_priv *priv;
1169 const char *name = evsel__name(evsel);
1170 int ret;
1171
1172 pr("Adding event '%s' (type %d)\n", name, evsel->core.attr.type);
1173
1174 event_class = bt_ctf_event_class_create(name);
1175 if (!event_class)
1176 return -1;
1177
1178 ret = add_generic_types(cw, evsel, event_class);
1179 if (ret)
1180 goto err;
1181
1182 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
1183 ret = add_tracepoint_types(cw, evsel, event_class);
1184 if (ret)
1185 goto err;
1186 }
1187
1188 if (evsel__is_bpf_output(evsel)) {
1189 ret = add_bpf_output_types(cw, event_class);
1190 if (ret)
1191 goto err;
1192 }
1193
1194 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);
1195 if (ret) {
1196 pr("Failed to add event class into stream.\n");
1197 goto err;
1198 }
1199
1200 priv = malloc(sizeof(*priv));
1201 if (!priv)
1202 goto err;
1203
1204 priv->event_class = event_class;
1205 evsel->priv = priv;
1206 return 0;
1207
1208err:
1209 bt_ctf_event_class_put(event_class);
1210 pr_err("Failed to add event '%s'.\n", name);
1211 return -1;
1212}
1213
1214static int setup_events(struct ctf_writer *cw, struct perf_session *session)
1215{
1216 struct evlist *evlist = session->evlist;
1217 struct evsel *evsel;
1218 int ret;
1219
1220 evlist__for_each_entry(evlist, evsel) {
1221 ret = add_event(cw, evsel);
1222 if (ret)
1223 return ret;
1224 }
1225 return 0;
1226}
1227
1228#define __NON_SAMPLE_ADD_FIELD(t, n) \
1229 do { \
1230 pr2(" field '%s'\n", #n); \
1231 if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\
1232 pr_err("Failed to add field '%s';\n", #n);\
1233 return -1; \
1234 } \
1235 } while(0)
1236
1237#define __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(_name, body) \
1238static int add_##_name##_event(struct ctf_writer *cw) \
1239{ \
1240 struct bt_ctf_event_class *event_class; \
1241 int ret; \
1242 \
1243 pr("Adding "#_name" event\n"); \
1244 event_class = bt_ctf_event_class_create("perf_" #_name);\
1245 if (!event_class) \
1246 return -1; \
1247 body \
1248 \
1249 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\
1250 if (ret) { \
1251 pr("Failed to add event class '"#_name"' into stream.\n");\
1252 return ret; \
1253 } \
1254 \
1255 cw->_name##_class = event_class; \
1256 bt_ctf_event_class_put(event_class); \
1257 return 0; \
1258}
1259
1260__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(comm,
1261 __NON_SAMPLE_ADD_FIELD(u32, pid);
1262 __NON_SAMPLE_ADD_FIELD(u32, tid);
1263 __NON_SAMPLE_ADD_FIELD(string, comm);
1264)
1265
1266__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(fork,
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);
1272)
1273
1274__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(exit,
1275 __NON_SAMPLE_ADD_FIELD(u32, pid);
1276 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1277 __NON_SAMPLE_ADD_FIELD(u32, tid);
1278 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1279 __NON_SAMPLE_ADD_FIELD(u64, time);
1280)
1281
1282__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap,
1283 __NON_SAMPLE_ADD_FIELD(u32, pid);
1284 __NON_SAMPLE_ADD_FIELD(u32, tid);
1285 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1286 __NON_SAMPLE_ADD_FIELD(string, filename);
1287)
1288
1289__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap2,
1290 __NON_SAMPLE_ADD_FIELD(u32, pid);
1291 __NON_SAMPLE_ADD_FIELD(u32, tid);
1292 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1293 __NON_SAMPLE_ADD_FIELD(string, filename);
1294)
1295#undef __NON_SAMPLE_ADD_FIELD
1296#undef __FUNC_ADD_NON_SAMPLE_EVENT_CLASS
1297
1298static int setup_non_sample_events(struct ctf_writer *cw,
1299 struct perf_session *session __maybe_unused)
1300{
1301 int ret;
1302
1303 ret = add_comm_event(cw);
1304 if (ret)
1305 return ret;
1306 ret = add_exit_event(cw);
1307 if (ret)
1308 return ret;
1309 ret = add_fork_event(cw);
1310 if (ret)
1311 return ret;
1312 ret = add_mmap_event(cw);
1313 if (ret)
1314 return ret;
1315 ret = add_mmap2_event(cw);
1316 if (ret)
1317 return ret;
1318 return 0;
1319}
1320
1321static void cleanup_events(struct perf_session *session)
1322{
1323 struct evlist *evlist = session->evlist;
1324 struct evsel *evsel;
1325
1326 evlist__for_each_entry(evlist, evsel) {
1327 struct evsel_priv *priv;
1328
1329 priv = evsel->priv;
1330 bt_ctf_event_class_put(priv->event_class);
1331 zfree(&evsel->priv);
1332 }
1333
1334 evlist__delete(evlist);
1335 session->evlist = NULL;
1336}
1337
1338static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
1339{
1340 struct ctf_stream **stream;
1341 struct perf_header *ph = &session->header;
1342 int ncpus;
1343
1344 /*
1345 * Try to get the number of cpus used in the data file,
1346 * if not present fallback to the MAX_CPUS.
1347 */
1348 ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS;
1349
1350 stream = zalloc(sizeof(*stream) * ncpus);
1351 if (!stream) {
1352 pr_err("Failed to allocate streams.\n");
1353 return -ENOMEM;
1354 }
1355
1356 cw->stream = stream;
1357 cw->stream_cnt = ncpus;
1358 return 0;
1359}
1360
1361static void free_streams(struct ctf_writer *cw)
1362{
1363 int cpu;
1364
1365 for (cpu = 0; cpu < cw->stream_cnt; cpu++)
1366 ctf_stream__delete(cw->stream[cpu]);
1367
1368 zfree(&cw->stream);
1369}
1370
1371static int ctf_writer__setup_env(struct ctf_writer *cw,
1372 struct perf_session *session)
1373{
1374 struct perf_header *header = &session->header;
1375 struct bt_ctf_writer *writer = cw->writer;
1376
1377#define ADD(__n, __v) \
1378do { \
1379 if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \
1380 return -1; \
1381} while (0)
1382
1383 ADD("host", header->env.hostname);
1384 ADD("sysname", "Linux");
1385 ADD("release", header->env.os_release);
1386 ADD("version", header->env.version);
1387 ADD("machine", header->env.arch);
1388 ADD("domain", "kernel");
1389 ADD("tracer_name", "perf");
1390
1391#undef ADD
1392 return 0;
1393}
1394
1395static int ctf_writer__setup_clock(struct ctf_writer *cw,
1396 struct perf_session *session,
1397 bool tod)
1398{
1399 struct bt_ctf_clock *clock = cw->clock;
1400 const char *desc = "perf clock";
1401 int64_t offset = 0;
1402
1403 if (tod) {
1404 struct perf_env *env = &session->header.env;
1405
1406 if (!env->clock.enabled) {
1407 pr_err("Can't provide --tod time, missing clock data. "
1408 "Please record with -k/--clockid option.\n");
1409 return -1;
1410 }
1411
1412 desc = clockid_name(env->clock.clockid);
1413 offset = env->clock.tod_ns - env->clock.clockid_ns;
1414 }
1415
1416#define SET(__n, __v) \
1417do { \
1418 if (bt_ctf_clock_set_##__n(clock, __v)) \
1419 return -1; \
1420} while (0)
1421
1422 SET(frequency, 1000000000);
1423 SET(offset, offset);
1424 SET(description, desc);
1425 SET(precision, 10);
1426 SET(is_absolute, 0);
1427
1428#undef SET
1429 return 0;
1430}
1431
1432static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex)
1433{
1434 struct bt_ctf_field_type *type;
1435
1436 type = bt_ctf_field_type_integer_create(size);
1437 if (!type)
1438 return NULL;
1439
1440 if (sign &&
1441 bt_ctf_field_type_integer_set_signed(type, 1))
1442 goto err;
1443
1444 if (hex &&
1445 bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL))
1446 goto err;
1447
1448#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1449 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_BIG_ENDIAN);
1450#else
1451 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
1452#endif
1453
1454 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
1455 size, sign ? "un" : "", hex ? "hex" : "");
1456 return type;
1457
1458err:
1459 bt_ctf_field_type_put(type);
1460 return NULL;
1461}
1462
1463static void ctf_writer__cleanup_data(struct ctf_writer *cw)
1464{
1465 unsigned int i;
1466
1467 for (i = 0; i < ARRAY_SIZE(cw->data.array); i++)
1468 bt_ctf_field_type_put(cw->data.array[i]);
1469}
1470
1471static int ctf_writer__init_data(struct ctf_writer *cw)
1472{
1473#define CREATE_INT_TYPE(type, size, sign, hex) \
1474do { \
1475 (type) = create_int_type(size, sign, hex); \
1476 if (!(type)) \
1477 goto err; \
1478} while (0)
1479
1480 CREATE_INT_TYPE(cw->data.s64, 64, true, false);
1481 CREATE_INT_TYPE(cw->data.u64, 64, false, false);
1482 CREATE_INT_TYPE(cw->data.s32, 32, true, false);
1483 CREATE_INT_TYPE(cw->data.u32, 32, false, false);
1484 CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true);
1485 CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true);
1486
1487 cw->data.string = bt_ctf_field_type_string_create();
1488 if (cw->data.string)
1489 return 0;
1490
1491err:
1492 ctf_writer__cleanup_data(cw);
1493 pr_err("Failed to create data types.\n");
1494 return -1;
1495}
1496
1497static void ctf_writer__cleanup(struct ctf_writer *cw)
1498{
1499 ctf_writer__cleanup_data(cw);
1500
1501 bt_ctf_clock_put(cw->clock);
1502 free_streams(cw);
1503 bt_ctf_stream_class_put(cw->stream_class);
1504 bt_ctf_writer_put(cw->writer);
1505
1506 /* and NULL all the pointers */
1507 memset(cw, 0, sizeof(*cw));
1508}
1509
1510static int ctf_writer__init(struct ctf_writer *cw, const char *path,
1511 struct perf_session *session, bool tod)
1512{
1513 struct bt_ctf_writer *writer;
1514 struct bt_ctf_stream_class *stream_class;
1515 struct bt_ctf_clock *clock;
1516 struct bt_ctf_field_type *pkt_ctx_type;
1517 int ret;
1518
1519 /* CTF writer */
1520 writer = bt_ctf_writer_create(path);
1521 if (!writer)
1522 goto err;
1523
1524 cw->writer = writer;
1525
1526 /* CTF clock */
1527 clock = bt_ctf_clock_create("perf_clock");
1528 if (!clock) {
1529 pr("Failed to create CTF clock.\n");
1530 goto err_cleanup;
1531 }
1532
1533 cw->clock = clock;
1534
1535 if (ctf_writer__setup_clock(cw, session, tod)) {
1536 pr("Failed to setup CTF clock.\n");
1537 goto err_cleanup;
1538 }
1539
1540 /* CTF stream class */
1541 stream_class = bt_ctf_stream_class_create("perf_stream");
1542 if (!stream_class) {
1543 pr("Failed to create CTF stream class.\n");
1544 goto err_cleanup;
1545 }
1546
1547 cw->stream_class = stream_class;
1548
1549 /* CTF clock stream setup */
1550 if (bt_ctf_stream_class_set_clock(stream_class, clock)) {
1551 pr("Failed to assign CTF clock to stream class.\n");
1552 goto err_cleanup;
1553 }
1554
1555 if (ctf_writer__init_data(cw))
1556 goto err_cleanup;
1557
1558 /* Add cpu_id for packet context */
1559 pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1560 if (!pkt_ctx_type)
1561 goto err_cleanup;
1562
1563 ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id");
1564 bt_ctf_field_type_put(pkt_ctx_type);
1565 if (ret)
1566 goto err_cleanup;
1567
1568 /* CTF clock writer setup */
1569 if (bt_ctf_writer_add_clock(writer, clock)) {
1570 pr("Failed to assign CTF clock to writer.\n");
1571 goto err_cleanup;
1572 }
1573
1574 return 0;
1575
1576err_cleanup:
1577 ctf_writer__cleanup(cw);
1578err:
1579 pr_err("Failed to setup CTF writer.\n");
1580 return -1;
1581}
1582
1583static int ctf_writer__flush_streams(struct ctf_writer *cw)
1584{
1585 int cpu, ret = 0;
1586
1587 for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++)
1588 ret = ctf_stream__flush(cw->stream[cpu]);
1589
1590 return ret;
1591}
1592
1593static int convert__config(const char *var, const char *value, void *cb)
1594{
1595 struct convert *c = cb;
1596
1597 if (!strcmp(var, "convert.queue-size"))
1598 return perf_config_u64(&c->queue_size, var, value);
1599
1600 return 0;
1601}
1602
1603int bt_convert__perf2ctf(const char *input, const char *path,
1604 struct perf_data_convert_opts *opts)
1605{
1606 struct perf_session *session;
1607 struct perf_data data = {
1608 .path = input,
1609 .mode = PERF_DATA_MODE_READ,
1610 .force = opts->force,
1611 };
1612 struct convert c = {
1613 .tool = {
1614 .sample = process_sample_event,
1615 .mmap = perf_event__process_mmap,
1616 .mmap2 = perf_event__process_mmap2,
1617 .comm = perf_event__process_comm,
1618 .exit = perf_event__process_exit,
1619 .fork = perf_event__process_fork,
1620 .lost = perf_event__process_lost,
1621 .tracing_data = perf_event__process_tracing_data,
1622 .build_id = perf_event__process_build_id,
1623 .namespaces = perf_event__process_namespaces,
1624 .ordered_events = true,
1625 .ordering_requires_timestamps = true,
1626 },
1627 };
1628 struct ctf_writer *cw = &c.writer;
1629 int err;
1630
1631 if (opts->all) {
1632 c.tool.comm = process_comm_event;
1633 c.tool.exit = process_exit_event;
1634 c.tool.fork = process_fork_event;
1635 c.tool.mmap = process_mmap_event;
1636 c.tool.mmap2 = process_mmap2_event;
1637 }
1638
1639 err = perf_config(convert__config, &c);
1640 if (err)
1641 return err;
1642
1643 err = -1;
1644 /* perf.data session */
1645 session = perf_session__new(&data, &c.tool);
1646 if (IS_ERR(session))
1647 return PTR_ERR(session);
1648
1649 /* CTF writer */
1650 if (ctf_writer__init(cw, path, session, opts->tod))
1651 goto free_session;
1652
1653 if (c.queue_size) {
1654 ordered_events__set_alloc_size(&session->ordered_events,
1655 c.queue_size);
1656 }
1657
1658 /* CTF writer env/clock setup */
1659 if (ctf_writer__setup_env(cw, session))
1660 goto free_writer;
1661
1662 /* CTF events setup */
1663 if (setup_events(cw, session))
1664 goto free_writer;
1665
1666 if (opts->all && setup_non_sample_events(cw, session))
1667 goto free_writer;
1668
1669 if (setup_streams(cw, session))
1670 goto free_writer;
1671
1672 err = perf_session__process_events(session);
1673 if (!err)
1674 err = ctf_writer__flush_streams(cw);
1675 else
1676 pr_err("Error during conversion.\n");
1677
1678 fprintf(stderr,
1679 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
1680 data.path, path);
1681
1682 fprintf(stderr,
1683 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
1684 (double) c.events_size / 1024.0 / 1024.0,
1685 c.events_count);
1686
1687 if (!c.non_sample_count)
1688 fprintf(stderr, ") ]\n");
1689 else
1690 fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
1691
1692 cleanup_events(session);
1693 perf_session__delete(session);
1694 ctf_writer__cleanup(cw);
1695
1696 return err;
1697
1698free_writer:
1699 ctf_writer__cleanup(cw);
1700free_session:
1701 perf_session__delete(session);
1702 pr_err("Error during conversion setup.\n");
1703 return err;
1704}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * CTF writing support via babeltrace.
4 *
5 * Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com>
6 * Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7 */
8
9#include <errno.h>
10#include <inttypes.h>
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>
23#include "asm/bug.h"
24#include "data-convert-bt.h"
25#include "session.h"
26#include "debug.h"
27#include "tool.h"
28#include "evlist.h"
29#include "evsel.h"
30#include "machine.h"
31#include "config.h"
32#include <linux/ctype.h>
33#include <linux/err.h>
34
35#define pr_N(n, fmt, ...) \
36 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
37
38#define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
39#define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
40
41#define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
42
43struct evsel_priv {
44 struct bt_ctf_event_class *event_class;
45};
46
47#define MAX_CPUS 4096
48
49struct ctf_stream {
50 struct bt_ctf_stream *stream;
51 int cpu;
52 u32 count;
53};
54
55struct ctf_writer {
56 /* writer primitives */
57 struct bt_ctf_writer *writer;
58 struct ctf_stream **stream;
59 int stream_cnt;
60 struct bt_ctf_stream_class *stream_class;
61 struct bt_ctf_clock *clock;
62
63 /* data types */
64 union {
65 struct {
66 struct bt_ctf_field_type *s64;
67 struct bt_ctf_field_type *u64;
68 struct bt_ctf_field_type *s32;
69 struct bt_ctf_field_type *u32;
70 struct bt_ctf_field_type *string;
71 struct bt_ctf_field_type *u32_hex;
72 struct bt_ctf_field_type *u64_hex;
73 };
74 struct bt_ctf_field_type *array[6];
75 } data;
76 struct bt_ctf_event_class *comm_class;
77 struct bt_ctf_event_class *exit_class;
78 struct bt_ctf_event_class *fork_class;
79 struct bt_ctf_event_class *mmap_class;
80 struct bt_ctf_event_class *mmap2_class;
81};
82
83struct convert {
84 struct perf_tool tool;
85 struct ctf_writer writer;
86
87 u64 events_size;
88 u64 events_count;
89 u64 non_sample_count;
90
91 /* Ordered events configured queue size. */
92 u64 queue_size;
93};
94
95static int value_set(struct bt_ctf_field_type *type,
96 struct bt_ctf_event *event,
97 const char *name, u64 val)
98{
99 struct bt_ctf_field *field;
100 bool sign = bt_ctf_field_type_integer_get_signed(type);
101 int ret;
102
103 field = bt_ctf_field_create(type);
104 if (!field) {
105 pr_err("failed to create a field %s\n", name);
106 return -1;
107 }
108
109 if (sign) {
110 ret = bt_ctf_field_signed_integer_set_value(field, val);
111 if (ret) {
112 pr_err("failed to set field value %s\n", name);
113 goto err;
114 }
115 } else {
116 ret = bt_ctf_field_unsigned_integer_set_value(field, val);
117 if (ret) {
118 pr_err("failed to set field value %s\n", name);
119 goto err;
120 }
121 }
122
123 ret = bt_ctf_event_set_payload(event, name, field);
124 if (ret) {
125 pr_err("failed to set payload %s\n", name);
126 goto err;
127 }
128
129 pr2(" SET [%s = %" PRIu64 "]\n", name, val);
130
131err:
132 bt_ctf_field_put(field);
133 return ret;
134}
135
136#define __FUNC_VALUE_SET(_name, _val_type) \
137static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
138 struct bt_ctf_event *event, \
139 const char *name, \
140 _val_type val) \
141{ \
142 struct bt_ctf_field_type *type = cw->data._name; \
143 return value_set(type, event, name, (u64) val); \
144}
145
146#define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
147
148FUNC_VALUE_SET(s32)
149FUNC_VALUE_SET(u32)
150FUNC_VALUE_SET(s64)
151FUNC_VALUE_SET(u64)
152__FUNC_VALUE_SET(u64_hex, u64)
153
154static int string_set_value(struct bt_ctf_field *field, const char *string);
155static __maybe_unused int
156value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event,
157 const char *name, const char *string)
158{
159 struct bt_ctf_field_type *type = cw->data.string;
160 struct bt_ctf_field *field;
161 int ret = 0;
162
163 field = bt_ctf_field_create(type);
164 if (!field) {
165 pr_err("failed to create a field %s\n", name);
166 return -1;
167 }
168
169 ret = string_set_value(field, string);
170 if (ret) {
171 pr_err("failed to set value %s\n", name);
172 goto err_put_field;
173 }
174
175 ret = bt_ctf_event_set_payload(event, name, field);
176 if (ret)
177 pr_err("failed to set payload %s\n", name);
178
179err_put_field:
180 bt_ctf_field_put(field);
181 return ret;
182}
183
184static struct bt_ctf_field_type*
185get_tracepoint_field_type(struct ctf_writer *cw, struct tep_format_field *field)
186{
187 unsigned long flags = field->flags;
188
189 if (flags & TEP_FIELD_IS_STRING)
190 return cw->data.string;
191
192 if (!(flags & TEP_FIELD_IS_SIGNED)) {
193 /* unsigned long are mostly pointers */
194 if (flags & TEP_FIELD_IS_LONG || flags & TEP_FIELD_IS_POINTER)
195 return cw->data.u64_hex;
196 }
197
198 if (flags & TEP_FIELD_IS_SIGNED) {
199 if (field->size == 8)
200 return cw->data.s64;
201 else
202 return cw->data.s32;
203 }
204
205 if (field->size == 8)
206 return cw->data.u64;
207 else
208 return cw->data.u32;
209}
210
211static unsigned long long adjust_signedness(unsigned long long value_int, int size)
212{
213 unsigned long long value_mask;
214
215 /*
216 * value_mask = (1 << (size * 8 - 1)) - 1.
217 * Directly set value_mask for code readers.
218 */
219 switch (size) {
220 case 1:
221 value_mask = 0x7fULL;
222 break;
223 case 2:
224 value_mask = 0x7fffULL;
225 break;
226 case 4:
227 value_mask = 0x7fffffffULL;
228 break;
229 case 8:
230 /*
231 * For 64 bit value, return it self. There is no need
232 * to fill high bit.
233 */
234 /* Fall through */
235 default:
236 /* BUG! */
237 return value_int;
238 }
239
240 /* If it is a positive value, don't adjust. */
241 if ((value_int & (~0ULL - value_mask)) == 0)
242 return value_int;
243
244 /* Fill upper part of value_int with 1 to make it a negative long long. */
245 return (value_int & value_mask) | ~value_mask;
246}
247
248static int string_set_value(struct bt_ctf_field *field, const char *string)
249{
250 char *buffer = NULL;
251 size_t len = strlen(string), i, p;
252 int err;
253
254 for (i = p = 0; i < len; i++, p++) {
255 if (isprint(string[i])) {
256 if (!buffer)
257 continue;
258 buffer[p] = string[i];
259 } else {
260 char numstr[5];
261
262 snprintf(numstr, sizeof(numstr), "\\x%02x",
263 (unsigned int)(string[i]) & 0xff);
264
265 if (!buffer) {
266 buffer = zalloc(i + (len - i) * 4 + 2);
267 if (!buffer) {
268 pr_err("failed to set unprintable string '%s'\n", string);
269 return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING");
270 }
271 if (i > 0)
272 strncpy(buffer, string, i);
273 }
274 memcpy(buffer + p, numstr, 4);
275 p += 3;
276 }
277 }
278
279 if (!buffer)
280 return bt_ctf_field_string_set_value(field, string);
281 err = bt_ctf_field_string_set_value(field, buffer);
282 free(buffer);
283 return err;
284}
285
286static int add_tracepoint_field_value(struct ctf_writer *cw,
287 struct bt_ctf_event_class *event_class,
288 struct bt_ctf_event *event,
289 struct perf_sample *sample,
290 struct tep_format_field *fmtf)
291{
292 struct bt_ctf_field_type *type;
293 struct bt_ctf_field *array_field;
294 struct bt_ctf_field *field;
295 const char *name = fmtf->name;
296 void *data = sample->raw_data;
297 unsigned long flags = fmtf->flags;
298 unsigned int n_items;
299 unsigned int i;
300 unsigned int offset;
301 unsigned int len;
302 int ret;
303
304 name = fmtf->alias;
305 offset = fmtf->offset;
306 len = fmtf->size;
307 if (flags & TEP_FIELD_IS_STRING)
308 flags &= ~TEP_FIELD_IS_ARRAY;
309
310 if (flags & TEP_FIELD_IS_DYNAMIC) {
311 unsigned long long tmp_val;
312
313 tmp_val = tep_read_number(fmtf->event->tep,
314 data + offset, len);
315 offset = tmp_val;
316 len = offset >> 16;
317 offset &= 0xffff;
318 }
319
320 if (flags & TEP_FIELD_IS_ARRAY) {
321
322 type = bt_ctf_event_class_get_field_by_name(
323 event_class, name);
324 array_field = bt_ctf_field_create(type);
325 bt_ctf_field_type_put(type);
326 if (!array_field) {
327 pr_err("Failed to create array type %s\n", name);
328 return -1;
329 }
330
331 len = fmtf->size / fmtf->arraylen;
332 n_items = fmtf->arraylen;
333 } else {
334 n_items = 1;
335 array_field = NULL;
336 }
337
338 type = get_tracepoint_field_type(cw, fmtf);
339
340 for (i = 0; i < n_items; i++) {
341 if (flags & TEP_FIELD_IS_ARRAY)
342 field = bt_ctf_field_array_get_field(array_field, i);
343 else
344 field = bt_ctf_field_create(type);
345
346 if (!field) {
347 pr_err("failed to create a field %s\n", name);
348 return -1;
349 }
350
351 if (flags & TEP_FIELD_IS_STRING)
352 ret = string_set_value(field, data + offset + i * len);
353 else {
354 unsigned long long value_int;
355
356 value_int = tep_read_number(
357 fmtf->event->tep,
358 data + offset + i * len, len);
359
360 if (!(flags & TEP_FIELD_IS_SIGNED))
361 ret = bt_ctf_field_unsigned_integer_set_value(
362 field, value_int);
363 else
364 ret = bt_ctf_field_signed_integer_set_value(
365 field, adjust_signedness(value_int, len));
366 }
367
368 if (ret) {
369 pr_err("failed to set file value %s\n", name);
370 goto err_put_field;
371 }
372 if (!(flags & TEP_FIELD_IS_ARRAY)) {
373 ret = bt_ctf_event_set_payload(event, name, field);
374 if (ret) {
375 pr_err("failed to set payload %s\n", name);
376 goto err_put_field;
377 }
378 }
379 bt_ctf_field_put(field);
380 }
381 if (flags & TEP_FIELD_IS_ARRAY) {
382 ret = bt_ctf_event_set_payload(event, name, array_field);
383 if (ret) {
384 pr_err("Failed add payload array %s\n", name);
385 return -1;
386 }
387 bt_ctf_field_put(array_field);
388 }
389 return 0;
390
391err_put_field:
392 bt_ctf_field_put(field);
393 return -1;
394}
395
396static int add_tracepoint_fields_values(struct ctf_writer *cw,
397 struct bt_ctf_event_class *event_class,
398 struct bt_ctf_event *event,
399 struct tep_format_field *fields,
400 struct perf_sample *sample)
401{
402 struct tep_format_field *field;
403 int ret;
404
405 for (field = fields; field; field = field->next) {
406 ret = add_tracepoint_field_value(cw, event_class, event, sample,
407 field);
408 if (ret)
409 return -1;
410 }
411 return 0;
412}
413
414static int add_tracepoint_values(struct ctf_writer *cw,
415 struct bt_ctf_event_class *event_class,
416 struct bt_ctf_event *event,
417 struct evsel *evsel,
418 struct perf_sample *sample)
419{
420 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
421 struct tep_format_field *fields = evsel->tp_format->format.fields;
422 int ret;
423
424 ret = add_tracepoint_fields_values(cw, event_class, event,
425 common_fields, sample);
426 if (!ret)
427 ret = add_tracepoint_fields_values(cw, event_class, event,
428 fields, sample);
429
430 return ret;
431}
432
433static int
434add_bpf_output_values(struct bt_ctf_event_class *event_class,
435 struct bt_ctf_event *event,
436 struct perf_sample *sample)
437{
438 struct bt_ctf_field_type *len_type, *seq_type;
439 struct bt_ctf_field *len_field, *seq_field;
440 unsigned int raw_size = sample->raw_size;
441 unsigned int nr_elements = raw_size / sizeof(u32);
442 unsigned int i;
443 int ret;
444
445 if (nr_elements * sizeof(u32) != raw_size)
446 pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n",
447 raw_size, nr_elements * sizeof(u32) - raw_size);
448
449 len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len");
450 len_field = bt_ctf_field_create(len_type);
451 if (!len_field) {
452 pr_err("failed to create 'raw_len' for bpf output event\n");
453 ret = -1;
454 goto put_len_type;
455 }
456
457 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
458 if (ret) {
459 pr_err("failed to set field value for raw_len\n");
460 goto put_len_field;
461 }
462 ret = bt_ctf_event_set_payload(event, "raw_len", len_field);
463 if (ret) {
464 pr_err("failed to set payload to raw_len\n");
465 goto put_len_field;
466 }
467
468 seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data");
469 seq_field = bt_ctf_field_create(seq_type);
470 if (!seq_field) {
471 pr_err("failed to create 'raw_data' for bpf output event\n");
472 ret = -1;
473 goto put_seq_type;
474 }
475
476 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
477 if (ret) {
478 pr_err("failed to set length of 'raw_data'\n");
479 goto put_seq_field;
480 }
481
482 for (i = 0; i < nr_elements; i++) {
483 struct bt_ctf_field *elem_field =
484 bt_ctf_field_sequence_get_field(seq_field, i);
485
486 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
487 ((u32 *)(sample->raw_data))[i]);
488
489 bt_ctf_field_put(elem_field);
490 if (ret) {
491 pr_err("failed to set raw_data[%d]\n", i);
492 goto put_seq_field;
493 }
494 }
495
496 ret = bt_ctf_event_set_payload(event, "raw_data", seq_field);
497 if (ret)
498 pr_err("failed to set payload for raw_data\n");
499
500put_seq_field:
501 bt_ctf_field_put(seq_field);
502put_seq_type:
503 bt_ctf_field_type_put(seq_type);
504put_len_field:
505 bt_ctf_field_put(len_field);
506put_len_type:
507 bt_ctf_field_type_put(len_type);
508 return ret;
509}
510
511static int
512add_callchain_output_values(struct bt_ctf_event_class *event_class,
513 struct bt_ctf_event *event,
514 struct ip_callchain *callchain)
515{
516 struct bt_ctf_field_type *len_type, *seq_type;
517 struct bt_ctf_field *len_field, *seq_field;
518 unsigned int nr_elements = callchain->nr;
519 unsigned int i;
520 int ret;
521
522 len_type = bt_ctf_event_class_get_field_by_name(
523 event_class, "perf_callchain_size");
524 len_field = bt_ctf_field_create(len_type);
525 if (!len_field) {
526 pr_err("failed to create 'perf_callchain_size' for callchain output event\n");
527 ret = -1;
528 goto put_len_type;
529 }
530
531 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
532 if (ret) {
533 pr_err("failed to set field value for perf_callchain_size\n");
534 goto put_len_field;
535 }
536 ret = bt_ctf_event_set_payload(event, "perf_callchain_size", len_field);
537 if (ret) {
538 pr_err("failed to set payload to perf_callchain_size\n");
539 goto put_len_field;
540 }
541
542 seq_type = bt_ctf_event_class_get_field_by_name(
543 event_class, "perf_callchain");
544 seq_field = bt_ctf_field_create(seq_type);
545 if (!seq_field) {
546 pr_err("failed to create 'perf_callchain' for callchain output event\n");
547 ret = -1;
548 goto put_seq_type;
549 }
550
551 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
552 if (ret) {
553 pr_err("failed to set length of 'perf_callchain'\n");
554 goto put_seq_field;
555 }
556
557 for (i = 0; i < nr_elements; i++) {
558 struct bt_ctf_field *elem_field =
559 bt_ctf_field_sequence_get_field(seq_field, i);
560
561 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
562 ((u64 *)(callchain->ips))[i]);
563
564 bt_ctf_field_put(elem_field);
565 if (ret) {
566 pr_err("failed to set callchain[%d]\n", i);
567 goto put_seq_field;
568 }
569 }
570
571 ret = bt_ctf_event_set_payload(event, "perf_callchain", seq_field);
572 if (ret)
573 pr_err("failed to set payload for raw_data\n");
574
575put_seq_field:
576 bt_ctf_field_put(seq_field);
577put_seq_type:
578 bt_ctf_field_type_put(seq_type);
579put_len_field:
580 bt_ctf_field_put(len_field);
581put_len_type:
582 bt_ctf_field_type_put(len_type);
583 return ret;
584}
585
586static int add_generic_values(struct ctf_writer *cw,
587 struct bt_ctf_event *event,
588 struct evsel *evsel,
589 struct perf_sample *sample)
590{
591 u64 type = evsel->core.attr.sample_type;
592 int ret;
593
594 /*
595 * missing:
596 * PERF_SAMPLE_TIME - not needed as we have it in
597 * ctf event header
598 * PERF_SAMPLE_READ - TODO
599 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
600 * PERF_SAMPLE_BRANCH_STACK - TODO
601 * PERF_SAMPLE_REGS_USER - TODO
602 * PERF_SAMPLE_STACK_USER - TODO
603 */
604
605 if (type & PERF_SAMPLE_IP) {
606 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
607 if (ret)
608 return -1;
609 }
610
611 if (type & PERF_SAMPLE_TID) {
612 ret = value_set_s32(cw, event, "perf_tid", sample->tid);
613 if (ret)
614 return -1;
615
616 ret = value_set_s32(cw, event, "perf_pid", sample->pid);
617 if (ret)
618 return -1;
619 }
620
621 if ((type & PERF_SAMPLE_ID) ||
622 (type & PERF_SAMPLE_IDENTIFIER)) {
623 ret = value_set_u64(cw, event, "perf_id", sample->id);
624 if (ret)
625 return -1;
626 }
627
628 if (type & PERF_SAMPLE_STREAM_ID) {
629 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
630 if (ret)
631 return -1;
632 }
633
634 if (type & PERF_SAMPLE_PERIOD) {
635 ret = value_set_u64(cw, event, "perf_period", sample->period);
636 if (ret)
637 return -1;
638 }
639
640 if (type & PERF_SAMPLE_WEIGHT) {
641 ret = value_set_u64(cw, event, "perf_weight", sample->weight);
642 if (ret)
643 return -1;
644 }
645
646 if (type & PERF_SAMPLE_DATA_SRC) {
647 ret = value_set_u64(cw, event, "perf_data_src",
648 sample->data_src);
649 if (ret)
650 return -1;
651 }
652
653 if (type & PERF_SAMPLE_TRANSACTION) {
654 ret = value_set_u64(cw, event, "perf_transaction",
655 sample->transaction);
656 if (ret)
657 return -1;
658 }
659
660 return 0;
661}
662
663static int ctf_stream__flush(struct ctf_stream *cs)
664{
665 int err = 0;
666
667 if (cs) {
668 err = bt_ctf_stream_flush(cs->stream);
669 if (err)
670 pr_err("CTF stream %d flush failed\n", cs->cpu);
671
672 pr("Flush stream for cpu %d (%u samples)\n",
673 cs->cpu, cs->count);
674
675 cs->count = 0;
676 }
677
678 return err;
679}
680
681static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
682{
683 struct ctf_stream *cs;
684 struct bt_ctf_field *pkt_ctx = NULL;
685 struct bt_ctf_field *cpu_field = NULL;
686 struct bt_ctf_stream *stream = NULL;
687 int ret;
688
689 cs = zalloc(sizeof(*cs));
690 if (!cs) {
691 pr_err("Failed to allocate ctf stream\n");
692 return NULL;
693 }
694
695 stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
696 if (!stream) {
697 pr_err("Failed to create CTF stream\n");
698 goto out;
699 }
700
701 pkt_ctx = bt_ctf_stream_get_packet_context(stream);
702 if (!pkt_ctx) {
703 pr_err("Failed to obtain packet context\n");
704 goto out;
705 }
706
707 cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
708 bt_ctf_field_put(pkt_ctx);
709 if (!cpu_field) {
710 pr_err("Failed to obtain cpu field\n");
711 goto out;
712 }
713
714 ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
715 if (ret) {
716 pr_err("Failed to update CPU number\n");
717 goto out;
718 }
719
720 bt_ctf_field_put(cpu_field);
721
722 cs->cpu = cpu;
723 cs->stream = stream;
724 return cs;
725
726out:
727 if (cpu_field)
728 bt_ctf_field_put(cpu_field);
729 if (stream)
730 bt_ctf_stream_put(stream);
731
732 free(cs);
733 return NULL;
734}
735
736static void ctf_stream__delete(struct ctf_stream *cs)
737{
738 if (cs) {
739 bt_ctf_stream_put(cs->stream);
740 free(cs);
741 }
742}
743
744static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
745{
746 struct ctf_stream *cs = cw->stream[cpu];
747
748 if (!cs) {
749 cs = ctf_stream__create(cw, cpu);
750 cw->stream[cpu] = cs;
751 }
752
753 return cs;
754}
755
756static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
757 struct evsel *evsel)
758{
759 int cpu = 0;
760
761 if (evsel->core.attr.sample_type & PERF_SAMPLE_CPU)
762 cpu = sample->cpu;
763
764 if (cpu > cw->stream_cnt) {
765 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
766 cpu, cw->stream_cnt);
767 cpu = 0;
768 }
769
770 return cpu;
771}
772
773#define STREAM_FLUSH_COUNT 100000
774
775/*
776 * Currently we have no other way to determine the
777 * time for the stream flush other than keep track
778 * of the number of events and check it against
779 * threshold.
780 */
781static bool is_flush_needed(struct ctf_stream *cs)
782{
783 return cs->count >= STREAM_FLUSH_COUNT;
784}
785
786static int process_sample_event(struct perf_tool *tool,
787 union perf_event *_event,
788 struct perf_sample *sample,
789 struct evsel *evsel,
790 struct machine *machine __maybe_unused)
791{
792 struct convert *c = container_of(tool, struct convert, tool);
793 struct evsel_priv *priv = evsel->priv;
794 struct ctf_writer *cw = &c->writer;
795 struct ctf_stream *cs;
796 struct bt_ctf_event_class *event_class;
797 struct bt_ctf_event *event;
798 int ret;
799 unsigned long type = evsel->core.attr.sample_type;
800
801 if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
802 return 0;
803
804 event_class = priv->event_class;
805
806 /* update stats */
807 c->events_count++;
808 c->events_size += _event->header.size;
809
810 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
811
812 event = bt_ctf_event_create(event_class);
813 if (!event) {
814 pr_err("Failed to create an CTF event\n");
815 return -1;
816 }
817
818 bt_ctf_clock_set_time(cw->clock, sample->time);
819
820 ret = add_generic_values(cw, event, evsel, sample);
821 if (ret)
822 return -1;
823
824 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
825 ret = add_tracepoint_values(cw, event_class, event,
826 evsel, sample);
827 if (ret)
828 return -1;
829 }
830
831 if (type & PERF_SAMPLE_CALLCHAIN) {
832 ret = add_callchain_output_values(event_class,
833 event, sample->callchain);
834 if (ret)
835 return -1;
836 }
837
838 if (perf_evsel__is_bpf_output(evsel)) {
839 ret = add_bpf_output_values(event_class, event, sample);
840 if (ret)
841 return -1;
842 }
843
844 cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel));
845 if (cs) {
846 if (is_flush_needed(cs))
847 ctf_stream__flush(cs);
848
849 cs->count++;
850 bt_ctf_stream_append_event(cs->stream, event);
851 }
852
853 bt_ctf_event_put(event);
854 return cs ? 0 : -1;
855}
856
857#define __NON_SAMPLE_SET_FIELD(_name, _type, _field) \
858do { \
859 ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
860 if (ret) \
861 return -1; \
862} while(0)
863
864#define __FUNC_PROCESS_NON_SAMPLE(_name, body) \
865static int process_##_name##_event(struct perf_tool *tool, \
866 union perf_event *_event, \
867 struct perf_sample *sample, \
868 struct machine *machine) \
869{ \
870 struct convert *c = container_of(tool, struct convert, tool);\
871 struct ctf_writer *cw = &c->writer; \
872 struct bt_ctf_event_class *event_class = cw->_name##_class;\
873 struct bt_ctf_event *event; \
874 struct ctf_stream *cs; \
875 int ret; \
876 \
877 c->non_sample_count++; \
878 c->events_size += _event->header.size; \
879 event = bt_ctf_event_create(event_class); \
880 if (!event) { \
881 pr_err("Failed to create an CTF event\n"); \
882 return -1; \
883 } \
884 \
885 bt_ctf_clock_set_time(cw->clock, sample->time); \
886 body \
887 cs = ctf_stream(cw, 0); \
888 if (cs) { \
889 if (is_flush_needed(cs)) \
890 ctf_stream__flush(cs); \
891 \
892 cs->count++; \
893 bt_ctf_stream_append_event(cs->stream, event); \
894 } \
895 bt_ctf_event_put(event); \
896 \
897 return perf_event__process_##_name(tool, _event, sample, machine);\
898}
899
900__FUNC_PROCESS_NON_SAMPLE(comm,
901 __NON_SAMPLE_SET_FIELD(comm, u32, pid);
902 __NON_SAMPLE_SET_FIELD(comm, u32, tid);
903 __NON_SAMPLE_SET_FIELD(comm, string, comm);
904)
905__FUNC_PROCESS_NON_SAMPLE(fork,
906 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
907 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
908 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
909 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
910 __NON_SAMPLE_SET_FIELD(fork, u64, time);
911)
912
913__FUNC_PROCESS_NON_SAMPLE(exit,
914 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
915 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
916 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
917 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
918 __NON_SAMPLE_SET_FIELD(fork, u64, time);
919)
920__FUNC_PROCESS_NON_SAMPLE(mmap,
921 __NON_SAMPLE_SET_FIELD(mmap, u32, pid);
922 __NON_SAMPLE_SET_FIELD(mmap, u32, tid);
923 __NON_SAMPLE_SET_FIELD(mmap, u64_hex, start);
924 __NON_SAMPLE_SET_FIELD(mmap, string, filename);
925)
926__FUNC_PROCESS_NON_SAMPLE(mmap2,
927 __NON_SAMPLE_SET_FIELD(mmap2, u32, pid);
928 __NON_SAMPLE_SET_FIELD(mmap2, u32, tid);
929 __NON_SAMPLE_SET_FIELD(mmap2, u64_hex, start);
930 __NON_SAMPLE_SET_FIELD(mmap2, string, filename);
931)
932#undef __NON_SAMPLE_SET_FIELD
933#undef __FUNC_PROCESS_NON_SAMPLE
934
935/* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
936static char *change_name(char *name, char *orig_name, int dup)
937{
938 char *new_name = NULL;
939 size_t len;
940
941 if (!name)
942 name = orig_name;
943
944 if (dup >= 10)
945 goto out;
946 /*
947 * Add '_' prefix to potential keywork. According to
948 * Mathieu Desnoyers (https://lkml.org/lkml/2015/1/23/652),
949 * futher CTF spec updating may require us to use '$'.
950 */
951 if (dup < 0)
952 len = strlen(name) + sizeof("_");
953 else
954 len = strlen(orig_name) + sizeof("_dupl_X");
955
956 new_name = malloc(len);
957 if (!new_name)
958 goto out;
959
960 if (dup < 0)
961 snprintf(new_name, len, "_%s", name);
962 else
963 snprintf(new_name, len, "%s_dupl_%d", orig_name, dup);
964
965out:
966 if (name != orig_name)
967 free(name);
968 return new_name;
969}
970
971static int event_class_add_field(struct bt_ctf_event_class *event_class,
972 struct bt_ctf_field_type *type,
973 struct tep_format_field *field)
974{
975 struct bt_ctf_field_type *t = NULL;
976 char *name;
977 int dup = 1;
978 int ret;
979
980 /* alias was already assigned */
981 if (field->alias != field->name)
982 return bt_ctf_event_class_add_field(event_class, type,
983 (char *)field->alias);
984
985 name = field->name;
986
987 /* If 'name' is a keywork, add prefix. */
988 if (bt_ctf_validate_identifier(name))
989 name = change_name(name, field->name, -1);
990
991 if (!name) {
992 pr_err("Failed to fix invalid identifier.");
993 return -1;
994 }
995 while ((t = bt_ctf_event_class_get_field_by_name(event_class, name))) {
996 bt_ctf_field_type_put(t);
997 name = change_name(name, field->name, dup++);
998 if (!name) {
999 pr_err("Failed to create dup name for '%s'\n", field->name);
1000 return -1;
1001 }
1002 }
1003
1004 ret = bt_ctf_event_class_add_field(event_class, type, name);
1005 if (!ret)
1006 field->alias = name;
1007
1008 return ret;
1009}
1010
1011static int add_tracepoint_fields_types(struct ctf_writer *cw,
1012 struct tep_format_field *fields,
1013 struct bt_ctf_event_class *event_class)
1014{
1015 struct tep_format_field *field;
1016 int ret;
1017
1018 for (field = fields; field; field = field->next) {
1019 struct bt_ctf_field_type *type;
1020 unsigned long flags = field->flags;
1021
1022 pr2(" field '%s'\n", field->name);
1023
1024 type = get_tracepoint_field_type(cw, field);
1025 if (!type)
1026 return -1;
1027
1028 /*
1029 * A string is an array of chars. For this we use the string
1030 * type and don't care that it is an array. What we don't
1031 * support is an array of strings.
1032 */
1033 if (flags & TEP_FIELD_IS_STRING)
1034 flags &= ~TEP_FIELD_IS_ARRAY;
1035
1036 if (flags & TEP_FIELD_IS_ARRAY)
1037 type = bt_ctf_field_type_array_create(type, field->arraylen);
1038
1039 ret = event_class_add_field(event_class, type, field);
1040
1041 if (flags & TEP_FIELD_IS_ARRAY)
1042 bt_ctf_field_type_put(type);
1043
1044 if (ret) {
1045 pr_err("Failed to add field '%s': %d\n",
1046 field->name, ret);
1047 return -1;
1048 }
1049 }
1050
1051 return 0;
1052}
1053
1054static int add_tracepoint_types(struct ctf_writer *cw,
1055 struct evsel *evsel,
1056 struct bt_ctf_event_class *class)
1057{
1058 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
1059 struct tep_format_field *fields = evsel->tp_format->format.fields;
1060 int ret;
1061
1062 ret = add_tracepoint_fields_types(cw, common_fields, class);
1063 if (!ret)
1064 ret = add_tracepoint_fields_types(cw, fields, class);
1065
1066 return ret;
1067}
1068
1069static int add_bpf_output_types(struct ctf_writer *cw,
1070 struct bt_ctf_event_class *class)
1071{
1072 struct bt_ctf_field_type *len_type = cw->data.u32;
1073 struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex;
1074 struct bt_ctf_field_type *seq_type;
1075 int ret;
1076
1077 ret = bt_ctf_event_class_add_field(class, len_type, "raw_len");
1078 if (ret)
1079 return ret;
1080
1081 seq_type = bt_ctf_field_type_sequence_create(seq_base_type, "raw_len");
1082 if (!seq_type)
1083 return -1;
1084
1085 return bt_ctf_event_class_add_field(class, seq_type, "raw_data");
1086}
1087
1088static int add_generic_types(struct ctf_writer *cw, struct evsel *evsel,
1089 struct bt_ctf_event_class *event_class)
1090{
1091 u64 type = evsel->core.attr.sample_type;
1092
1093 /*
1094 * missing:
1095 * PERF_SAMPLE_TIME - not needed as we have it in
1096 * ctf event header
1097 * PERF_SAMPLE_READ - TODO
1098 * PERF_SAMPLE_CALLCHAIN - TODO
1099 * PERF_SAMPLE_RAW - tracepoint fields and BPF output
1100 * are handled separately
1101 * PERF_SAMPLE_BRANCH_STACK - TODO
1102 * PERF_SAMPLE_REGS_USER - TODO
1103 * PERF_SAMPLE_STACK_USER - TODO
1104 */
1105
1106#define ADD_FIELD(cl, t, n) \
1107 do { \
1108 pr2(" field '%s'\n", n); \
1109 if (bt_ctf_event_class_add_field(cl, t, n)) { \
1110 pr_err("Failed to add field '%s';\n", n); \
1111 return -1; \
1112 } \
1113 } while (0)
1114
1115 if (type & PERF_SAMPLE_IP)
1116 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
1117
1118 if (type & PERF_SAMPLE_TID) {
1119 ADD_FIELD(event_class, cw->data.s32, "perf_tid");
1120 ADD_FIELD(event_class, cw->data.s32, "perf_pid");
1121 }
1122
1123 if ((type & PERF_SAMPLE_ID) ||
1124 (type & PERF_SAMPLE_IDENTIFIER))
1125 ADD_FIELD(event_class, cw->data.u64, "perf_id");
1126
1127 if (type & PERF_SAMPLE_STREAM_ID)
1128 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
1129
1130 if (type & PERF_SAMPLE_PERIOD)
1131 ADD_FIELD(event_class, cw->data.u64, "perf_period");
1132
1133 if (type & PERF_SAMPLE_WEIGHT)
1134 ADD_FIELD(event_class, cw->data.u64, "perf_weight");
1135
1136 if (type & PERF_SAMPLE_DATA_SRC)
1137 ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
1138
1139 if (type & PERF_SAMPLE_TRANSACTION)
1140 ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
1141
1142 if (type & PERF_SAMPLE_CALLCHAIN) {
1143 ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size");
1144 ADD_FIELD(event_class,
1145 bt_ctf_field_type_sequence_create(
1146 cw->data.u64_hex, "perf_callchain_size"),
1147 "perf_callchain");
1148 }
1149
1150#undef ADD_FIELD
1151 return 0;
1152}
1153
1154static int add_event(struct ctf_writer *cw, struct evsel *evsel)
1155{
1156 struct bt_ctf_event_class *event_class;
1157 struct evsel_priv *priv;
1158 const char *name = perf_evsel__name(evsel);
1159 int ret;
1160
1161 pr("Adding event '%s' (type %d)\n", name, evsel->core.attr.type);
1162
1163 event_class = bt_ctf_event_class_create(name);
1164 if (!event_class)
1165 return -1;
1166
1167 ret = add_generic_types(cw, evsel, event_class);
1168 if (ret)
1169 goto err;
1170
1171 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
1172 ret = add_tracepoint_types(cw, evsel, event_class);
1173 if (ret)
1174 goto err;
1175 }
1176
1177 if (perf_evsel__is_bpf_output(evsel)) {
1178 ret = add_bpf_output_types(cw, event_class);
1179 if (ret)
1180 goto err;
1181 }
1182
1183 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);
1184 if (ret) {
1185 pr("Failed to add event class into stream.\n");
1186 goto err;
1187 }
1188
1189 priv = malloc(sizeof(*priv));
1190 if (!priv)
1191 goto err;
1192
1193 priv->event_class = event_class;
1194 evsel->priv = priv;
1195 return 0;
1196
1197err:
1198 bt_ctf_event_class_put(event_class);
1199 pr_err("Failed to add event '%s'.\n", name);
1200 return -1;
1201}
1202
1203static int setup_events(struct ctf_writer *cw, struct perf_session *session)
1204{
1205 struct evlist *evlist = session->evlist;
1206 struct evsel *evsel;
1207 int ret;
1208
1209 evlist__for_each_entry(evlist, evsel) {
1210 ret = add_event(cw, evsel);
1211 if (ret)
1212 return ret;
1213 }
1214 return 0;
1215}
1216
1217#define __NON_SAMPLE_ADD_FIELD(t, n) \
1218 do { \
1219 pr2(" field '%s'\n", #n); \
1220 if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\
1221 pr_err("Failed to add field '%s';\n", #n);\
1222 return -1; \
1223 } \
1224 } while(0)
1225
1226#define __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(_name, body) \
1227static int add_##_name##_event(struct ctf_writer *cw) \
1228{ \
1229 struct bt_ctf_event_class *event_class; \
1230 int ret; \
1231 \
1232 pr("Adding "#_name" event\n"); \
1233 event_class = bt_ctf_event_class_create("perf_" #_name);\
1234 if (!event_class) \
1235 return -1; \
1236 body \
1237 \
1238 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\
1239 if (ret) { \
1240 pr("Failed to add event class '"#_name"' into stream.\n");\
1241 return ret; \
1242 } \
1243 \
1244 cw->_name##_class = event_class; \
1245 bt_ctf_event_class_put(event_class); \
1246 return 0; \
1247}
1248
1249__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(comm,
1250 __NON_SAMPLE_ADD_FIELD(u32, pid);
1251 __NON_SAMPLE_ADD_FIELD(u32, tid);
1252 __NON_SAMPLE_ADD_FIELD(string, comm);
1253)
1254
1255__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(fork,
1256 __NON_SAMPLE_ADD_FIELD(u32, pid);
1257 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1258 __NON_SAMPLE_ADD_FIELD(u32, tid);
1259 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1260 __NON_SAMPLE_ADD_FIELD(u64, time);
1261)
1262
1263__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(exit,
1264 __NON_SAMPLE_ADD_FIELD(u32, pid);
1265 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1266 __NON_SAMPLE_ADD_FIELD(u32, tid);
1267 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1268 __NON_SAMPLE_ADD_FIELD(u64, time);
1269)
1270
1271__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap,
1272 __NON_SAMPLE_ADD_FIELD(u32, pid);
1273 __NON_SAMPLE_ADD_FIELD(u32, tid);
1274 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1275 __NON_SAMPLE_ADD_FIELD(string, filename);
1276)
1277
1278__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap2,
1279 __NON_SAMPLE_ADD_FIELD(u32, pid);
1280 __NON_SAMPLE_ADD_FIELD(u32, tid);
1281 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1282 __NON_SAMPLE_ADD_FIELD(string, filename);
1283)
1284#undef __NON_SAMPLE_ADD_FIELD
1285#undef __FUNC_ADD_NON_SAMPLE_EVENT_CLASS
1286
1287static int setup_non_sample_events(struct ctf_writer *cw,
1288 struct perf_session *session __maybe_unused)
1289{
1290 int ret;
1291
1292 ret = add_comm_event(cw);
1293 if (ret)
1294 return ret;
1295 ret = add_exit_event(cw);
1296 if (ret)
1297 return ret;
1298 ret = add_fork_event(cw);
1299 if (ret)
1300 return ret;
1301 ret = add_mmap_event(cw);
1302 if (ret)
1303 return ret;
1304 ret = add_mmap2_event(cw);
1305 if (ret)
1306 return ret;
1307 return 0;
1308}
1309
1310static void cleanup_events(struct perf_session *session)
1311{
1312 struct evlist *evlist = session->evlist;
1313 struct evsel *evsel;
1314
1315 evlist__for_each_entry(evlist, evsel) {
1316 struct evsel_priv *priv;
1317
1318 priv = evsel->priv;
1319 bt_ctf_event_class_put(priv->event_class);
1320 zfree(&evsel->priv);
1321 }
1322
1323 evlist__delete(evlist);
1324 session->evlist = NULL;
1325}
1326
1327static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
1328{
1329 struct ctf_stream **stream;
1330 struct perf_header *ph = &session->header;
1331 int ncpus;
1332
1333 /*
1334 * Try to get the number of cpus used in the data file,
1335 * if not present fallback to the MAX_CPUS.
1336 */
1337 ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS;
1338
1339 stream = zalloc(sizeof(*stream) * ncpus);
1340 if (!stream) {
1341 pr_err("Failed to allocate streams.\n");
1342 return -ENOMEM;
1343 }
1344
1345 cw->stream = stream;
1346 cw->stream_cnt = ncpus;
1347 return 0;
1348}
1349
1350static void free_streams(struct ctf_writer *cw)
1351{
1352 int cpu;
1353
1354 for (cpu = 0; cpu < cw->stream_cnt; cpu++)
1355 ctf_stream__delete(cw->stream[cpu]);
1356
1357 zfree(&cw->stream);
1358}
1359
1360static int ctf_writer__setup_env(struct ctf_writer *cw,
1361 struct perf_session *session)
1362{
1363 struct perf_header *header = &session->header;
1364 struct bt_ctf_writer *writer = cw->writer;
1365
1366#define ADD(__n, __v) \
1367do { \
1368 if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \
1369 return -1; \
1370} while (0)
1371
1372 ADD("host", header->env.hostname);
1373 ADD("sysname", "Linux");
1374 ADD("release", header->env.os_release);
1375 ADD("version", header->env.version);
1376 ADD("machine", header->env.arch);
1377 ADD("domain", "kernel");
1378 ADD("tracer_name", "perf");
1379
1380#undef ADD
1381 return 0;
1382}
1383
1384static int ctf_writer__setup_clock(struct ctf_writer *cw)
1385{
1386 struct bt_ctf_clock *clock = cw->clock;
1387
1388 bt_ctf_clock_set_description(clock, "perf clock");
1389
1390#define SET(__n, __v) \
1391do { \
1392 if (bt_ctf_clock_set_##__n(clock, __v)) \
1393 return -1; \
1394} while (0)
1395
1396 SET(frequency, 1000000000);
1397 SET(offset_s, 0);
1398 SET(offset, 0);
1399 SET(precision, 10);
1400 SET(is_absolute, 0);
1401
1402#undef SET
1403 return 0;
1404}
1405
1406static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex)
1407{
1408 struct bt_ctf_field_type *type;
1409
1410 type = bt_ctf_field_type_integer_create(size);
1411 if (!type)
1412 return NULL;
1413
1414 if (sign &&
1415 bt_ctf_field_type_integer_set_signed(type, 1))
1416 goto err;
1417
1418 if (hex &&
1419 bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL))
1420 goto err;
1421
1422#if __BYTE_ORDER == __BIG_ENDIAN
1423 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_BIG_ENDIAN);
1424#else
1425 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
1426#endif
1427
1428 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
1429 size, sign ? "un" : "", hex ? "hex" : "");
1430 return type;
1431
1432err:
1433 bt_ctf_field_type_put(type);
1434 return NULL;
1435}
1436
1437static void ctf_writer__cleanup_data(struct ctf_writer *cw)
1438{
1439 unsigned int i;
1440
1441 for (i = 0; i < ARRAY_SIZE(cw->data.array); i++)
1442 bt_ctf_field_type_put(cw->data.array[i]);
1443}
1444
1445static int ctf_writer__init_data(struct ctf_writer *cw)
1446{
1447#define CREATE_INT_TYPE(type, size, sign, hex) \
1448do { \
1449 (type) = create_int_type(size, sign, hex); \
1450 if (!(type)) \
1451 goto err; \
1452} while (0)
1453
1454 CREATE_INT_TYPE(cw->data.s64, 64, true, false);
1455 CREATE_INT_TYPE(cw->data.u64, 64, false, false);
1456 CREATE_INT_TYPE(cw->data.s32, 32, true, false);
1457 CREATE_INT_TYPE(cw->data.u32, 32, false, false);
1458 CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true);
1459 CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true);
1460
1461 cw->data.string = bt_ctf_field_type_string_create();
1462 if (cw->data.string)
1463 return 0;
1464
1465err:
1466 ctf_writer__cleanup_data(cw);
1467 pr_err("Failed to create data types.\n");
1468 return -1;
1469}
1470
1471static void ctf_writer__cleanup(struct ctf_writer *cw)
1472{
1473 ctf_writer__cleanup_data(cw);
1474
1475 bt_ctf_clock_put(cw->clock);
1476 free_streams(cw);
1477 bt_ctf_stream_class_put(cw->stream_class);
1478 bt_ctf_writer_put(cw->writer);
1479
1480 /* and NULL all the pointers */
1481 memset(cw, 0, sizeof(*cw));
1482}
1483
1484static int ctf_writer__init(struct ctf_writer *cw, const char *path)
1485{
1486 struct bt_ctf_writer *writer;
1487 struct bt_ctf_stream_class *stream_class;
1488 struct bt_ctf_clock *clock;
1489 struct bt_ctf_field_type *pkt_ctx_type;
1490 int ret;
1491
1492 /* CTF writer */
1493 writer = bt_ctf_writer_create(path);
1494 if (!writer)
1495 goto err;
1496
1497 cw->writer = writer;
1498
1499 /* CTF clock */
1500 clock = bt_ctf_clock_create("perf_clock");
1501 if (!clock) {
1502 pr("Failed to create CTF clock.\n");
1503 goto err_cleanup;
1504 }
1505
1506 cw->clock = clock;
1507
1508 if (ctf_writer__setup_clock(cw)) {
1509 pr("Failed to setup CTF clock.\n");
1510 goto err_cleanup;
1511 }
1512
1513 /* CTF stream class */
1514 stream_class = bt_ctf_stream_class_create("perf_stream");
1515 if (!stream_class) {
1516 pr("Failed to create CTF stream class.\n");
1517 goto err_cleanup;
1518 }
1519
1520 cw->stream_class = stream_class;
1521
1522 /* CTF clock stream setup */
1523 if (bt_ctf_stream_class_set_clock(stream_class, clock)) {
1524 pr("Failed to assign CTF clock to stream class.\n");
1525 goto err_cleanup;
1526 }
1527
1528 if (ctf_writer__init_data(cw))
1529 goto err_cleanup;
1530
1531 /* Add cpu_id for packet context */
1532 pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1533 if (!pkt_ctx_type)
1534 goto err_cleanup;
1535
1536 ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id");
1537 bt_ctf_field_type_put(pkt_ctx_type);
1538 if (ret)
1539 goto err_cleanup;
1540
1541 /* CTF clock writer setup */
1542 if (bt_ctf_writer_add_clock(writer, clock)) {
1543 pr("Failed to assign CTF clock to writer.\n");
1544 goto err_cleanup;
1545 }
1546
1547 return 0;
1548
1549err_cleanup:
1550 ctf_writer__cleanup(cw);
1551err:
1552 pr_err("Failed to setup CTF writer.\n");
1553 return -1;
1554}
1555
1556static int ctf_writer__flush_streams(struct ctf_writer *cw)
1557{
1558 int cpu, ret = 0;
1559
1560 for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++)
1561 ret = ctf_stream__flush(cw->stream[cpu]);
1562
1563 return ret;
1564}
1565
1566static int convert__config(const char *var, const char *value, void *cb)
1567{
1568 struct convert *c = cb;
1569
1570 if (!strcmp(var, "convert.queue-size"))
1571 return perf_config_u64(&c->queue_size, var, value);
1572
1573 return 0;
1574}
1575
1576int bt_convert__perf2ctf(const char *input, const char *path,
1577 struct perf_data_convert_opts *opts)
1578{
1579 struct perf_session *session;
1580 struct perf_data data = {
1581 .path = input,
1582 .mode = PERF_DATA_MODE_READ,
1583 .force = opts->force,
1584 };
1585 struct convert c = {
1586 .tool = {
1587 .sample = process_sample_event,
1588 .mmap = perf_event__process_mmap,
1589 .mmap2 = perf_event__process_mmap2,
1590 .comm = perf_event__process_comm,
1591 .exit = perf_event__process_exit,
1592 .fork = perf_event__process_fork,
1593 .lost = perf_event__process_lost,
1594 .tracing_data = perf_event__process_tracing_data,
1595 .build_id = perf_event__process_build_id,
1596 .namespaces = perf_event__process_namespaces,
1597 .ordered_events = true,
1598 .ordering_requires_timestamps = true,
1599 },
1600 };
1601 struct ctf_writer *cw = &c.writer;
1602 int err;
1603
1604 if (opts->all) {
1605 c.tool.comm = process_comm_event;
1606 c.tool.exit = process_exit_event;
1607 c.tool.fork = process_fork_event;
1608 c.tool.mmap = process_mmap_event;
1609 c.tool.mmap2 = process_mmap2_event;
1610 }
1611
1612 err = perf_config(convert__config, &c);
1613 if (err)
1614 return err;
1615
1616 /* CTF writer */
1617 if (ctf_writer__init(cw, path))
1618 return -1;
1619
1620 err = -1;
1621 /* perf.data session */
1622 session = perf_session__new(&data, 0, &c.tool);
1623 if (IS_ERR(session)) {
1624 err = PTR_ERR(session);
1625 goto free_writer;
1626 }
1627
1628 if (c.queue_size) {
1629 ordered_events__set_alloc_size(&session->ordered_events,
1630 c.queue_size);
1631 }
1632
1633 /* CTF writer env/clock setup */
1634 if (ctf_writer__setup_env(cw, session))
1635 goto free_session;
1636
1637 /* CTF events setup */
1638 if (setup_events(cw, session))
1639 goto free_session;
1640
1641 if (opts->all && setup_non_sample_events(cw, session))
1642 goto free_session;
1643
1644 if (setup_streams(cw, session))
1645 goto free_session;
1646
1647 err = perf_session__process_events(session);
1648 if (!err)
1649 err = ctf_writer__flush_streams(cw);
1650 else
1651 pr_err("Error during conversion.\n");
1652
1653 fprintf(stderr,
1654 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
1655 data.path, path);
1656
1657 fprintf(stderr,
1658 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
1659 (double) c.events_size / 1024.0 / 1024.0,
1660 c.events_count);
1661
1662 if (!c.non_sample_count)
1663 fprintf(stderr, ") ]\n");
1664 else
1665 fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
1666
1667 cleanup_events(session);
1668 perf_session__delete(session);
1669 ctf_writer__cleanup(cw);
1670
1671 return err;
1672
1673free_session:
1674 perf_session__delete(session);
1675free_writer:
1676 ctf_writer__cleanup(cw);
1677 pr_err("Error during conversion setup.\n");
1678 return err;
1679}