0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <errno.h>
0010 #include <inttypes.h>
0011 #include <linux/compiler.h>
0012 #include <linux/kernel.h>
0013 #include <linux/zalloc.h>
0014 #include <babeltrace/ctf-writer/writer.h>
0015 #include <babeltrace/ctf-writer/clock.h>
0016 #include <babeltrace/ctf-writer/stream.h>
0017 #include <babeltrace/ctf-writer/event.h>
0018 #include <babeltrace/ctf-writer/event-types.h>
0019 #include <babeltrace/ctf-writer/event-fields.h>
0020 #include <babeltrace/ctf-ir/utils.h>
0021 #include <babeltrace/ctf/events.h>
0022 #include <traceevent/event-parse.h>
0023 #include "asm/bug.h"
0024 #include "data-convert.h"
0025 #include "session.h"
0026 #include "debug.h"
0027 #include "tool.h"
0028 #include "evlist.h"
0029 #include "evsel.h"
0030 #include "machine.h"
0031 #include "config.h"
0032 #include <linux/ctype.h>
0033 #include <linux/err.h>
0034 #include <linux/time64.h>
0035 #include "util.h"
0036 #include "clockid.h"
0037
0038 #define pr_N(n, fmt, ...) \
0039 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
0040
0041 #define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
0042 #define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
0043
0044 #define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
0045
0046 struct evsel_priv {
0047 struct bt_ctf_event_class *event_class;
0048 };
0049
0050 #define MAX_CPUS 4096
0051
0052 struct ctf_stream {
0053 struct bt_ctf_stream *stream;
0054 int cpu;
0055 u32 count;
0056 };
0057
0058 struct ctf_writer {
0059
0060 struct bt_ctf_writer *writer;
0061 struct ctf_stream **stream;
0062 int stream_cnt;
0063 struct bt_ctf_stream_class *stream_class;
0064 struct bt_ctf_clock *clock;
0065
0066
0067 union {
0068 struct {
0069 struct bt_ctf_field_type *s64;
0070 struct bt_ctf_field_type *u64;
0071 struct bt_ctf_field_type *s32;
0072 struct bt_ctf_field_type *u32;
0073 struct bt_ctf_field_type *string;
0074 struct bt_ctf_field_type *u32_hex;
0075 struct bt_ctf_field_type *u64_hex;
0076 };
0077 struct bt_ctf_field_type *array[6];
0078 } data;
0079 struct bt_ctf_event_class *comm_class;
0080 struct bt_ctf_event_class *exit_class;
0081 struct bt_ctf_event_class *fork_class;
0082 struct bt_ctf_event_class *mmap_class;
0083 struct bt_ctf_event_class *mmap2_class;
0084 };
0085
0086 struct convert {
0087 struct perf_tool tool;
0088 struct ctf_writer writer;
0089
0090 u64 events_size;
0091 u64 events_count;
0092 u64 non_sample_count;
0093
0094
0095 u64 queue_size;
0096 };
0097
0098 static int value_set(struct bt_ctf_field_type *type,
0099 struct bt_ctf_event *event,
0100 const char *name, u64 val)
0101 {
0102 struct bt_ctf_field *field;
0103 bool sign = bt_ctf_field_type_integer_get_signed(type);
0104 int ret;
0105
0106 field = bt_ctf_field_create(type);
0107 if (!field) {
0108 pr_err("failed to create a field %s\n", name);
0109 return -1;
0110 }
0111
0112 if (sign) {
0113 ret = bt_ctf_field_signed_integer_set_value(field, val);
0114 if (ret) {
0115 pr_err("failed to set field value %s\n", name);
0116 goto err;
0117 }
0118 } else {
0119 ret = bt_ctf_field_unsigned_integer_set_value(field, val);
0120 if (ret) {
0121 pr_err("failed to set field value %s\n", name);
0122 goto err;
0123 }
0124 }
0125
0126 ret = bt_ctf_event_set_payload(event, name, field);
0127 if (ret) {
0128 pr_err("failed to set payload %s\n", name);
0129 goto err;
0130 }
0131
0132 pr2(" SET [%s = %" PRIu64 "]\n", name, val);
0133
0134 err:
0135 bt_ctf_field_put(field);
0136 return ret;
0137 }
0138
0139 #define __FUNC_VALUE_SET(_name, _val_type) \
0140 static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
0141 struct bt_ctf_event *event, \
0142 const char *name, \
0143 _val_type val) \
0144 { \
0145 struct bt_ctf_field_type *type = cw->data._name; \
0146 return value_set(type, event, name, (u64) val); \
0147 }
0148
0149 #define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
0150
0151 FUNC_VALUE_SET(s32)
0152 FUNC_VALUE_SET(u32)
0153 FUNC_VALUE_SET(s64)
0154 FUNC_VALUE_SET(u64)
0155 __FUNC_VALUE_SET(u64_hex, u64)
0156
0157 static int string_set_value(struct bt_ctf_field *field, const char *string);
0158 static __maybe_unused int
0159 value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event,
0160 const char *name, const char *string)
0161 {
0162 struct bt_ctf_field_type *type = cw->data.string;
0163 struct bt_ctf_field *field;
0164 int ret = 0;
0165
0166 field = bt_ctf_field_create(type);
0167 if (!field) {
0168 pr_err("failed to create a field %s\n", name);
0169 return -1;
0170 }
0171
0172 ret = string_set_value(field, string);
0173 if (ret) {
0174 pr_err("failed to set value %s\n", name);
0175 goto err_put_field;
0176 }
0177
0178 ret = bt_ctf_event_set_payload(event, name, field);
0179 if (ret)
0180 pr_err("failed to set payload %s\n", name);
0181
0182 err_put_field:
0183 bt_ctf_field_put(field);
0184 return ret;
0185 }
0186
0187 static struct bt_ctf_field_type*
0188 get_tracepoint_field_type(struct ctf_writer *cw, struct tep_format_field *field)
0189 {
0190 unsigned long flags = field->flags;
0191
0192 if (flags & TEP_FIELD_IS_STRING)
0193 return cw->data.string;
0194
0195 if (!(flags & TEP_FIELD_IS_SIGNED)) {
0196
0197 if (flags & TEP_FIELD_IS_LONG || flags & TEP_FIELD_IS_POINTER)
0198 return cw->data.u64_hex;
0199 }
0200
0201 if (flags & TEP_FIELD_IS_SIGNED) {
0202 if (field->size == 8)
0203 return cw->data.s64;
0204 else
0205 return cw->data.s32;
0206 }
0207
0208 if (field->size == 8)
0209 return cw->data.u64;
0210 else
0211 return cw->data.u32;
0212 }
0213
0214 static unsigned long long adjust_signedness(unsigned long long value_int, int size)
0215 {
0216 unsigned long long value_mask;
0217
0218
0219
0220
0221
0222 switch (size) {
0223 case 1:
0224 value_mask = 0x7fULL;
0225 break;
0226 case 2:
0227 value_mask = 0x7fffULL;
0228 break;
0229 case 4:
0230 value_mask = 0x7fffffffULL;
0231 break;
0232 case 8:
0233
0234
0235
0236
0237
0238 default:
0239
0240 return value_int;
0241 }
0242
0243
0244 if ((value_int & (~0ULL - value_mask)) == 0)
0245 return value_int;
0246
0247
0248 return (value_int & value_mask) | ~value_mask;
0249 }
0250
0251 static int string_set_value(struct bt_ctf_field *field, const char *string)
0252 {
0253 char *buffer = NULL;
0254 size_t len = strlen(string), i, p;
0255 int err;
0256
0257 for (i = p = 0; i < len; i++, p++) {
0258 if (isprint(string[i])) {
0259 if (!buffer)
0260 continue;
0261 buffer[p] = string[i];
0262 } else {
0263 char numstr[5];
0264
0265 snprintf(numstr, sizeof(numstr), "\\x%02x",
0266 (unsigned int)(string[i]) & 0xff);
0267
0268 if (!buffer) {
0269 buffer = zalloc(i + (len - i) * 4 + 2);
0270 if (!buffer) {
0271 pr_err("failed to set unprintable string '%s'\n", string);
0272 return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING");
0273 }
0274 if (i > 0)
0275 strncpy(buffer, string, i);
0276 }
0277 memcpy(buffer + p, numstr, 4);
0278 p += 3;
0279 }
0280 }
0281
0282 if (!buffer)
0283 return bt_ctf_field_string_set_value(field, string);
0284 err = bt_ctf_field_string_set_value(field, buffer);
0285 free(buffer);
0286 return err;
0287 }
0288
0289 static int add_tracepoint_field_value(struct ctf_writer *cw,
0290 struct bt_ctf_event_class *event_class,
0291 struct bt_ctf_event *event,
0292 struct perf_sample *sample,
0293 struct tep_format_field *fmtf)
0294 {
0295 struct bt_ctf_field_type *type;
0296 struct bt_ctf_field *array_field;
0297 struct bt_ctf_field *field;
0298 const char *name = fmtf->name;
0299 void *data = sample->raw_data;
0300 unsigned long flags = fmtf->flags;
0301 unsigned int n_items;
0302 unsigned int i;
0303 unsigned int offset;
0304 unsigned int len;
0305 int ret;
0306
0307 name = fmtf->alias;
0308 offset = fmtf->offset;
0309 len = fmtf->size;
0310 if (flags & TEP_FIELD_IS_STRING)
0311 flags &= ~TEP_FIELD_IS_ARRAY;
0312
0313 if (flags & TEP_FIELD_IS_DYNAMIC) {
0314 unsigned long long tmp_val;
0315
0316 tmp_val = tep_read_number(fmtf->event->tep,
0317 data + offset, len);
0318 offset = tmp_val;
0319 len = offset >> 16;
0320 offset &= 0xffff;
0321 if (flags & TEP_FIELD_IS_RELATIVE)
0322 offset += fmtf->offset + fmtf->size;
0323 }
0324
0325 if (flags & TEP_FIELD_IS_ARRAY) {
0326
0327 type = bt_ctf_event_class_get_field_by_name(
0328 event_class, name);
0329 array_field = bt_ctf_field_create(type);
0330 bt_ctf_field_type_put(type);
0331 if (!array_field) {
0332 pr_err("Failed to create array type %s\n", name);
0333 return -1;
0334 }
0335
0336 len = fmtf->size / fmtf->arraylen;
0337 n_items = fmtf->arraylen;
0338 } else {
0339 n_items = 1;
0340 array_field = NULL;
0341 }
0342
0343 type = get_tracepoint_field_type(cw, fmtf);
0344
0345 for (i = 0; i < n_items; i++) {
0346 if (flags & TEP_FIELD_IS_ARRAY)
0347 field = bt_ctf_field_array_get_field(array_field, i);
0348 else
0349 field = bt_ctf_field_create(type);
0350
0351 if (!field) {
0352 pr_err("failed to create a field %s\n", name);
0353 return -1;
0354 }
0355
0356 if (flags & TEP_FIELD_IS_STRING)
0357 ret = string_set_value(field, data + offset + i * len);
0358 else {
0359 unsigned long long value_int;
0360
0361 value_int = tep_read_number(
0362 fmtf->event->tep,
0363 data + offset + i * len, len);
0364
0365 if (!(flags & TEP_FIELD_IS_SIGNED))
0366 ret = bt_ctf_field_unsigned_integer_set_value(
0367 field, value_int);
0368 else
0369 ret = bt_ctf_field_signed_integer_set_value(
0370 field, adjust_signedness(value_int, len));
0371 }
0372
0373 if (ret) {
0374 pr_err("failed to set file value %s\n", name);
0375 goto err_put_field;
0376 }
0377 if (!(flags & TEP_FIELD_IS_ARRAY)) {
0378 ret = bt_ctf_event_set_payload(event, name, field);
0379 if (ret) {
0380 pr_err("failed to set payload %s\n", name);
0381 goto err_put_field;
0382 }
0383 }
0384 bt_ctf_field_put(field);
0385 }
0386 if (flags & TEP_FIELD_IS_ARRAY) {
0387 ret = bt_ctf_event_set_payload(event, name, array_field);
0388 if (ret) {
0389 pr_err("Failed add payload array %s\n", name);
0390 return -1;
0391 }
0392 bt_ctf_field_put(array_field);
0393 }
0394 return 0;
0395
0396 err_put_field:
0397 bt_ctf_field_put(field);
0398 return -1;
0399 }
0400
0401 static int add_tracepoint_fields_values(struct ctf_writer *cw,
0402 struct bt_ctf_event_class *event_class,
0403 struct bt_ctf_event *event,
0404 struct tep_format_field *fields,
0405 struct perf_sample *sample)
0406 {
0407 struct tep_format_field *field;
0408 int ret;
0409
0410 for (field = fields; field; field = field->next) {
0411 ret = add_tracepoint_field_value(cw, event_class, event, sample,
0412 field);
0413 if (ret)
0414 return -1;
0415 }
0416 return 0;
0417 }
0418
0419 static int add_tracepoint_values(struct ctf_writer *cw,
0420 struct bt_ctf_event_class *event_class,
0421 struct bt_ctf_event *event,
0422 struct evsel *evsel,
0423 struct perf_sample *sample)
0424 {
0425 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
0426 struct tep_format_field *fields = evsel->tp_format->format.fields;
0427 int ret;
0428
0429 ret = add_tracepoint_fields_values(cw, event_class, event,
0430 common_fields, sample);
0431 if (!ret)
0432 ret = add_tracepoint_fields_values(cw, event_class, event,
0433 fields, sample);
0434
0435 return ret;
0436 }
0437
0438 static int
0439 add_bpf_output_values(struct bt_ctf_event_class *event_class,
0440 struct bt_ctf_event *event,
0441 struct perf_sample *sample)
0442 {
0443 struct bt_ctf_field_type *len_type, *seq_type;
0444 struct bt_ctf_field *len_field, *seq_field;
0445 unsigned int raw_size = sample->raw_size;
0446 unsigned int nr_elements = raw_size / sizeof(u32);
0447 unsigned int i;
0448 int ret;
0449
0450 if (nr_elements * sizeof(u32) != raw_size)
0451 pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n",
0452 raw_size, nr_elements * sizeof(u32) - raw_size);
0453
0454 len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len");
0455 len_field = bt_ctf_field_create(len_type);
0456 if (!len_field) {
0457 pr_err("failed to create 'raw_len' for bpf output event\n");
0458 ret = -1;
0459 goto put_len_type;
0460 }
0461
0462 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
0463 if (ret) {
0464 pr_err("failed to set field value for raw_len\n");
0465 goto put_len_field;
0466 }
0467 ret = bt_ctf_event_set_payload(event, "raw_len", len_field);
0468 if (ret) {
0469 pr_err("failed to set payload to raw_len\n");
0470 goto put_len_field;
0471 }
0472
0473 seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data");
0474 seq_field = bt_ctf_field_create(seq_type);
0475 if (!seq_field) {
0476 pr_err("failed to create 'raw_data' for bpf output event\n");
0477 ret = -1;
0478 goto put_seq_type;
0479 }
0480
0481 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
0482 if (ret) {
0483 pr_err("failed to set length of 'raw_data'\n");
0484 goto put_seq_field;
0485 }
0486
0487 for (i = 0; i < nr_elements; i++) {
0488 struct bt_ctf_field *elem_field =
0489 bt_ctf_field_sequence_get_field(seq_field, i);
0490
0491 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
0492 ((u32 *)(sample->raw_data))[i]);
0493
0494 bt_ctf_field_put(elem_field);
0495 if (ret) {
0496 pr_err("failed to set raw_data[%d]\n", i);
0497 goto put_seq_field;
0498 }
0499 }
0500
0501 ret = bt_ctf_event_set_payload(event, "raw_data", seq_field);
0502 if (ret)
0503 pr_err("failed to set payload for raw_data\n");
0504
0505 put_seq_field:
0506 bt_ctf_field_put(seq_field);
0507 put_seq_type:
0508 bt_ctf_field_type_put(seq_type);
0509 put_len_field:
0510 bt_ctf_field_put(len_field);
0511 put_len_type:
0512 bt_ctf_field_type_put(len_type);
0513 return ret;
0514 }
0515
0516 static int
0517 add_callchain_output_values(struct bt_ctf_event_class *event_class,
0518 struct bt_ctf_event *event,
0519 struct ip_callchain *callchain)
0520 {
0521 struct bt_ctf_field_type *len_type, *seq_type;
0522 struct bt_ctf_field *len_field, *seq_field;
0523 unsigned int nr_elements = callchain->nr;
0524 unsigned int i;
0525 int ret;
0526
0527 len_type = bt_ctf_event_class_get_field_by_name(
0528 event_class, "perf_callchain_size");
0529 len_field = bt_ctf_field_create(len_type);
0530 if (!len_field) {
0531 pr_err("failed to create 'perf_callchain_size' for callchain output event\n");
0532 ret = -1;
0533 goto put_len_type;
0534 }
0535
0536 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
0537 if (ret) {
0538 pr_err("failed to set field value for perf_callchain_size\n");
0539 goto put_len_field;
0540 }
0541 ret = bt_ctf_event_set_payload(event, "perf_callchain_size", len_field);
0542 if (ret) {
0543 pr_err("failed to set payload to perf_callchain_size\n");
0544 goto put_len_field;
0545 }
0546
0547 seq_type = bt_ctf_event_class_get_field_by_name(
0548 event_class, "perf_callchain");
0549 seq_field = bt_ctf_field_create(seq_type);
0550 if (!seq_field) {
0551 pr_err("failed to create 'perf_callchain' for callchain output event\n");
0552 ret = -1;
0553 goto put_seq_type;
0554 }
0555
0556 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
0557 if (ret) {
0558 pr_err("failed to set length of 'perf_callchain'\n");
0559 goto put_seq_field;
0560 }
0561
0562 for (i = 0; i < nr_elements; i++) {
0563 struct bt_ctf_field *elem_field =
0564 bt_ctf_field_sequence_get_field(seq_field, i);
0565
0566 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
0567 ((u64 *)(callchain->ips))[i]);
0568
0569 bt_ctf_field_put(elem_field);
0570 if (ret) {
0571 pr_err("failed to set callchain[%d]\n", i);
0572 goto put_seq_field;
0573 }
0574 }
0575
0576 ret = bt_ctf_event_set_payload(event, "perf_callchain", seq_field);
0577 if (ret)
0578 pr_err("failed to set payload for raw_data\n");
0579
0580 put_seq_field:
0581 bt_ctf_field_put(seq_field);
0582 put_seq_type:
0583 bt_ctf_field_type_put(seq_type);
0584 put_len_field:
0585 bt_ctf_field_put(len_field);
0586 put_len_type:
0587 bt_ctf_field_type_put(len_type);
0588 return ret;
0589 }
0590
0591 static int add_generic_values(struct ctf_writer *cw,
0592 struct bt_ctf_event *event,
0593 struct evsel *evsel,
0594 struct perf_sample *sample)
0595 {
0596 u64 type = evsel->core.attr.sample_type;
0597 int ret;
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610 if (type & PERF_SAMPLE_IP) {
0611 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
0612 if (ret)
0613 return -1;
0614 }
0615
0616 if (type & PERF_SAMPLE_TID) {
0617 ret = value_set_s32(cw, event, "perf_tid", sample->tid);
0618 if (ret)
0619 return -1;
0620
0621 ret = value_set_s32(cw, event, "perf_pid", sample->pid);
0622 if (ret)
0623 return -1;
0624 }
0625
0626 if ((type & PERF_SAMPLE_ID) ||
0627 (type & PERF_SAMPLE_IDENTIFIER)) {
0628 ret = value_set_u64(cw, event, "perf_id", sample->id);
0629 if (ret)
0630 return -1;
0631 }
0632
0633 if (type & PERF_SAMPLE_STREAM_ID) {
0634 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
0635 if (ret)
0636 return -1;
0637 }
0638
0639 if (type & PERF_SAMPLE_PERIOD) {
0640 ret = value_set_u64(cw, event, "perf_period", sample->period);
0641 if (ret)
0642 return -1;
0643 }
0644
0645 if (type & PERF_SAMPLE_WEIGHT) {
0646 ret = value_set_u64(cw, event, "perf_weight", sample->weight);
0647 if (ret)
0648 return -1;
0649 }
0650
0651 if (type & PERF_SAMPLE_DATA_SRC) {
0652 ret = value_set_u64(cw, event, "perf_data_src",
0653 sample->data_src);
0654 if (ret)
0655 return -1;
0656 }
0657
0658 if (type & PERF_SAMPLE_TRANSACTION) {
0659 ret = value_set_u64(cw, event, "perf_transaction",
0660 sample->transaction);
0661 if (ret)
0662 return -1;
0663 }
0664
0665 return 0;
0666 }
0667
0668 static int ctf_stream__flush(struct ctf_stream *cs)
0669 {
0670 int err = 0;
0671
0672 if (cs) {
0673 err = bt_ctf_stream_flush(cs->stream);
0674 if (err)
0675 pr_err("CTF stream %d flush failed\n", cs->cpu);
0676
0677 pr("Flush stream for cpu %d (%u samples)\n",
0678 cs->cpu, cs->count);
0679
0680 cs->count = 0;
0681 }
0682
0683 return err;
0684 }
0685
0686 static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
0687 {
0688 struct ctf_stream *cs;
0689 struct bt_ctf_field *pkt_ctx = NULL;
0690 struct bt_ctf_field *cpu_field = NULL;
0691 struct bt_ctf_stream *stream = NULL;
0692 int ret;
0693
0694 cs = zalloc(sizeof(*cs));
0695 if (!cs) {
0696 pr_err("Failed to allocate ctf stream\n");
0697 return NULL;
0698 }
0699
0700 stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
0701 if (!stream) {
0702 pr_err("Failed to create CTF stream\n");
0703 goto out;
0704 }
0705
0706 pkt_ctx = bt_ctf_stream_get_packet_context(stream);
0707 if (!pkt_ctx) {
0708 pr_err("Failed to obtain packet context\n");
0709 goto out;
0710 }
0711
0712 cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
0713 bt_ctf_field_put(pkt_ctx);
0714 if (!cpu_field) {
0715 pr_err("Failed to obtain cpu field\n");
0716 goto out;
0717 }
0718
0719 ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
0720 if (ret) {
0721 pr_err("Failed to update CPU number\n");
0722 goto out;
0723 }
0724
0725 bt_ctf_field_put(cpu_field);
0726
0727 cs->cpu = cpu;
0728 cs->stream = stream;
0729 return cs;
0730
0731 out:
0732 if (cpu_field)
0733 bt_ctf_field_put(cpu_field);
0734 if (stream)
0735 bt_ctf_stream_put(stream);
0736
0737 free(cs);
0738 return NULL;
0739 }
0740
0741 static void ctf_stream__delete(struct ctf_stream *cs)
0742 {
0743 if (cs) {
0744 bt_ctf_stream_put(cs->stream);
0745 free(cs);
0746 }
0747 }
0748
0749 static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
0750 {
0751 struct ctf_stream *cs = cw->stream[cpu];
0752
0753 if (!cs) {
0754 cs = ctf_stream__create(cw, cpu);
0755 cw->stream[cpu] = cs;
0756 }
0757
0758 return cs;
0759 }
0760
0761 static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
0762 struct evsel *evsel)
0763 {
0764 int cpu = 0;
0765
0766 if (evsel->core.attr.sample_type & PERF_SAMPLE_CPU)
0767 cpu = sample->cpu;
0768
0769 if (cpu > cw->stream_cnt) {
0770 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
0771 cpu, cw->stream_cnt);
0772 cpu = 0;
0773 }
0774
0775 return cpu;
0776 }
0777
0778 #define STREAM_FLUSH_COUNT 100000
0779
0780
0781
0782
0783
0784
0785
0786 static bool is_flush_needed(struct ctf_stream *cs)
0787 {
0788 return cs->count >= STREAM_FLUSH_COUNT;
0789 }
0790
0791 static int process_sample_event(struct perf_tool *tool,
0792 union perf_event *_event,
0793 struct perf_sample *sample,
0794 struct evsel *evsel,
0795 struct machine *machine __maybe_unused)
0796 {
0797 struct convert *c = container_of(tool, struct convert, tool);
0798 struct evsel_priv *priv = evsel->priv;
0799 struct ctf_writer *cw = &c->writer;
0800 struct ctf_stream *cs;
0801 struct bt_ctf_event_class *event_class;
0802 struct bt_ctf_event *event;
0803 int ret;
0804 unsigned long type = evsel->core.attr.sample_type;
0805
0806 if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
0807 return 0;
0808
0809 event_class = priv->event_class;
0810
0811
0812 c->events_count++;
0813 c->events_size += _event->header.size;
0814
0815 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
0816
0817 event = bt_ctf_event_create(event_class);
0818 if (!event) {
0819 pr_err("Failed to create an CTF event\n");
0820 return -1;
0821 }
0822
0823 bt_ctf_clock_set_time(cw->clock, sample->time);
0824
0825 ret = add_generic_values(cw, event, evsel, sample);
0826 if (ret)
0827 return -1;
0828
0829 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
0830 ret = add_tracepoint_values(cw, event_class, event,
0831 evsel, sample);
0832 if (ret)
0833 return -1;
0834 }
0835
0836 if (type & PERF_SAMPLE_CALLCHAIN) {
0837 ret = add_callchain_output_values(event_class,
0838 event, sample->callchain);
0839 if (ret)
0840 return -1;
0841 }
0842
0843 if (evsel__is_bpf_output(evsel)) {
0844 ret = add_bpf_output_values(event_class, event, sample);
0845 if (ret)
0846 return -1;
0847 }
0848
0849 cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel));
0850 if (cs) {
0851 if (is_flush_needed(cs))
0852 ctf_stream__flush(cs);
0853
0854 cs->count++;
0855 bt_ctf_stream_append_event(cs->stream, event);
0856 }
0857
0858 bt_ctf_event_put(event);
0859 return cs ? 0 : -1;
0860 }
0861
0862 #define __NON_SAMPLE_SET_FIELD(_name, _type, _field) \
0863 do { \
0864 ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
0865 if (ret) \
0866 return -1; \
0867 } while(0)
0868
0869 #define __FUNC_PROCESS_NON_SAMPLE(_name, body) \
0870 static int process_##_name##_event(struct perf_tool *tool, \
0871 union perf_event *_event, \
0872 struct perf_sample *sample, \
0873 struct machine *machine) \
0874 { \
0875 struct convert *c = container_of(tool, struct convert, tool);\
0876 struct ctf_writer *cw = &c->writer; \
0877 struct bt_ctf_event_class *event_class = cw->_name##_class;\
0878 struct bt_ctf_event *event; \
0879 struct ctf_stream *cs; \
0880 int ret; \
0881 \
0882 c->non_sample_count++; \
0883 c->events_size += _event->header.size; \
0884 event = bt_ctf_event_create(event_class); \
0885 if (!event) { \
0886 pr_err("Failed to create an CTF event\n"); \
0887 return -1; \
0888 } \
0889 \
0890 bt_ctf_clock_set_time(cw->clock, sample->time); \
0891 body \
0892 cs = ctf_stream(cw, 0); \
0893 if (cs) { \
0894 if (is_flush_needed(cs)) \
0895 ctf_stream__flush(cs); \
0896 \
0897 cs->count++; \
0898 bt_ctf_stream_append_event(cs->stream, event); \
0899 } \
0900 bt_ctf_event_put(event); \
0901 \
0902 return perf_event__process_##_name(tool, _event, sample, machine);\
0903 }
0904
0905 __FUNC_PROCESS_NON_SAMPLE(comm,
0906 __NON_SAMPLE_SET_FIELD(comm, u32, pid);
0907 __NON_SAMPLE_SET_FIELD(comm, u32, tid);
0908 __NON_SAMPLE_SET_FIELD(comm, string, comm);
0909 )
0910 __FUNC_PROCESS_NON_SAMPLE(fork,
0911 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
0912 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
0913 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
0914 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
0915 __NON_SAMPLE_SET_FIELD(fork, u64, time);
0916 )
0917
0918 __FUNC_PROCESS_NON_SAMPLE(exit,
0919 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
0920 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
0921 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
0922 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
0923 __NON_SAMPLE_SET_FIELD(fork, u64, time);
0924 )
0925 __FUNC_PROCESS_NON_SAMPLE(mmap,
0926 __NON_SAMPLE_SET_FIELD(mmap, u32, pid);
0927 __NON_SAMPLE_SET_FIELD(mmap, u32, tid);
0928 __NON_SAMPLE_SET_FIELD(mmap, u64_hex, start);
0929 __NON_SAMPLE_SET_FIELD(mmap, string, filename);
0930 )
0931 __FUNC_PROCESS_NON_SAMPLE(mmap2,
0932 __NON_SAMPLE_SET_FIELD(mmap2, u32, pid);
0933 __NON_SAMPLE_SET_FIELD(mmap2, u32, tid);
0934 __NON_SAMPLE_SET_FIELD(mmap2, u64_hex, start);
0935 __NON_SAMPLE_SET_FIELD(mmap2, string, filename);
0936 )
0937 #undef __NON_SAMPLE_SET_FIELD
0938 #undef __FUNC_PROCESS_NON_SAMPLE
0939
0940
0941 static char *change_name(char *name, char *orig_name, int dup)
0942 {
0943 char *new_name = NULL;
0944 size_t len;
0945
0946 if (!name)
0947 name = orig_name;
0948
0949 if (dup >= 10)
0950 goto out;
0951
0952
0953
0954
0955
0956 if (dup < 0)
0957 len = strlen(name) + sizeof("_");
0958 else
0959 len = strlen(orig_name) + sizeof("_dupl_X");
0960
0961 new_name = malloc(len);
0962 if (!new_name)
0963 goto out;
0964
0965 if (dup < 0)
0966 snprintf(new_name, len, "_%s", name);
0967 else
0968 snprintf(new_name, len, "%s_dupl_%d", orig_name, dup);
0969
0970 out:
0971 if (name != orig_name)
0972 free(name);
0973 return new_name;
0974 }
0975
0976 static int event_class_add_field(struct bt_ctf_event_class *event_class,
0977 struct bt_ctf_field_type *type,
0978 struct tep_format_field *field)
0979 {
0980 struct bt_ctf_field_type *t = NULL;
0981 char *name;
0982 int dup = 1;
0983 int ret;
0984
0985
0986 if (field->alias != field->name)
0987 return bt_ctf_event_class_add_field(event_class, type,
0988 (char *)field->alias);
0989
0990 name = field->name;
0991
0992
0993 if (bt_ctf_validate_identifier(name))
0994 name = change_name(name, field->name, -1);
0995
0996 if (!name) {
0997 pr_err("Failed to fix invalid identifier.");
0998 return -1;
0999 }
1000 while ((t = bt_ctf_event_class_get_field_by_name(event_class, name))) {
1001 bt_ctf_field_type_put(t);
1002 name = change_name(name, field->name, dup++);
1003 if (!name) {
1004 pr_err("Failed to create dup name for '%s'\n", field->name);
1005 return -1;
1006 }
1007 }
1008
1009 ret = bt_ctf_event_class_add_field(event_class, type, name);
1010 if (!ret)
1011 field->alias = name;
1012
1013 return ret;
1014 }
1015
1016 static int add_tracepoint_fields_types(struct ctf_writer *cw,
1017 struct tep_format_field *fields,
1018 struct bt_ctf_event_class *event_class)
1019 {
1020 struct tep_format_field *field;
1021 int ret;
1022
1023 for (field = fields; field; field = field->next) {
1024 struct bt_ctf_field_type *type;
1025 unsigned long flags = field->flags;
1026
1027 pr2(" field '%s'\n", field->name);
1028
1029 type = get_tracepoint_field_type(cw, field);
1030 if (!type)
1031 return -1;
1032
1033
1034
1035
1036
1037
1038 if (flags & TEP_FIELD_IS_STRING)
1039 flags &= ~TEP_FIELD_IS_ARRAY;
1040
1041 if (flags & TEP_FIELD_IS_ARRAY)
1042 type = bt_ctf_field_type_array_create(type, field->arraylen);
1043
1044 ret = event_class_add_field(event_class, type, field);
1045
1046 if (flags & TEP_FIELD_IS_ARRAY)
1047 bt_ctf_field_type_put(type);
1048
1049 if (ret) {
1050 pr_err("Failed to add field '%s': %d\n",
1051 field->name, ret);
1052 return -1;
1053 }
1054 }
1055
1056 return 0;
1057 }
1058
1059 static int add_tracepoint_types(struct ctf_writer *cw,
1060 struct evsel *evsel,
1061 struct bt_ctf_event_class *class)
1062 {
1063 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
1064 struct tep_format_field *fields = evsel->tp_format->format.fields;
1065 int ret;
1066
1067 ret = add_tracepoint_fields_types(cw, common_fields, class);
1068 if (!ret)
1069 ret = add_tracepoint_fields_types(cw, fields, class);
1070
1071 return ret;
1072 }
1073
1074 static int add_bpf_output_types(struct ctf_writer *cw,
1075 struct bt_ctf_event_class *class)
1076 {
1077 struct bt_ctf_field_type *len_type = cw->data.u32;
1078 struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex;
1079 struct bt_ctf_field_type *seq_type;
1080 int ret;
1081
1082 ret = bt_ctf_event_class_add_field(class, len_type, "raw_len");
1083 if (ret)
1084 return ret;
1085
1086 seq_type = bt_ctf_field_type_sequence_create(seq_base_type, "raw_len");
1087 if (!seq_type)
1088 return -1;
1089
1090 return bt_ctf_event_class_add_field(class, seq_type, "raw_data");
1091 }
1092
1093 static int add_generic_types(struct ctf_writer *cw, struct evsel *evsel,
1094 struct bt_ctf_event_class *event_class)
1095 {
1096 u64 type = evsel->core.attr.sample_type;
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111 #define ADD_FIELD(cl, t, n) \
1112 do { \
1113 pr2(" field '%s'\n", n); \
1114 if (bt_ctf_event_class_add_field(cl, t, n)) { \
1115 pr_err("Failed to add field '%s';\n", n); \
1116 return -1; \
1117 } \
1118 } while (0)
1119
1120 if (type & PERF_SAMPLE_IP)
1121 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
1122
1123 if (type & PERF_SAMPLE_TID) {
1124 ADD_FIELD(event_class, cw->data.s32, "perf_tid");
1125 ADD_FIELD(event_class, cw->data.s32, "perf_pid");
1126 }
1127
1128 if ((type & PERF_SAMPLE_ID) ||
1129 (type & PERF_SAMPLE_IDENTIFIER))
1130 ADD_FIELD(event_class, cw->data.u64, "perf_id");
1131
1132 if (type & PERF_SAMPLE_STREAM_ID)
1133 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
1134
1135 if (type & PERF_SAMPLE_PERIOD)
1136 ADD_FIELD(event_class, cw->data.u64, "perf_period");
1137
1138 if (type & PERF_SAMPLE_WEIGHT)
1139 ADD_FIELD(event_class, cw->data.u64, "perf_weight");
1140
1141 if (type & PERF_SAMPLE_DATA_SRC)
1142 ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
1143
1144 if (type & PERF_SAMPLE_TRANSACTION)
1145 ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
1146
1147 if (type & PERF_SAMPLE_CALLCHAIN) {
1148 ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size");
1149 ADD_FIELD(event_class,
1150 bt_ctf_field_type_sequence_create(
1151 cw->data.u64_hex, "perf_callchain_size"),
1152 "perf_callchain");
1153 }
1154
1155 #undef ADD_FIELD
1156 return 0;
1157 }
1158
1159 static int add_event(struct ctf_writer *cw, struct evsel *evsel)
1160 {
1161 struct bt_ctf_event_class *event_class;
1162 struct evsel_priv *priv;
1163 const char *name = evsel__name(evsel);
1164 int ret;
1165
1166 pr("Adding event '%s' (type %d)\n", name, evsel->core.attr.type);
1167
1168 event_class = bt_ctf_event_class_create(name);
1169 if (!event_class)
1170 return -1;
1171
1172 ret = add_generic_types(cw, evsel, event_class);
1173 if (ret)
1174 goto err;
1175
1176 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
1177 ret = add_tracepoint_types(cw, evsel, event_class);
1178 if (ret)
1179 goto err;
1180 }
1181
1182 if (evsel__is_bpf_output(evsel)) {
1183 ret = add_bpf_output_types(cw, event_class);
1184 if (ret)
1185 goto err;
1186 }
1187
1188 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);
1189 if (ret) {
1190 pr("Failed to add event class into stream.\n");
1191 goto err;
1192 }
1193
1194 priv = malloc(sizeof(*priv));
1195 if (!priv)
1196 goto err;
1197
1198 priv->event_class = event_class;
1199 evsel->priv = priv;
1200 return 0;
1201
1202 err:
1203 bt_ctf_event_class_put(event_class);
1204 pr_err("Failed to add event '%s'.\n", name);
1205 return -1;
1206 }
1207
1208 static int setup_events(struct ctf_writer *cw, struct perf_session *session)
1209 {
1210 struct evlist *evlist = session->evlist;
1211 struct evsel *evsel;
1212 int ret;
1213
1214 evlist__for_each_entry(evlist, evsel) {
1215 ret = add_event(cw, evsel);
1216 if (ret)
1217 return ret;
1218 }
1219 return 0;
1220 }
1221
1222 #define __NON_SAMPLE_ADD_FIELD(t, n) \
1223 do { \
1224 pr2(" field '%s'\n", #n); \
1225 if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\
1226 pr_err("Failed to add field '%s';\n", #n);\
1227 return -1; \
1228 } \
1229 } while(0)
1230
1231 #define __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(_name, body) \
1232 static int add_##_name##_event(struct ctf_writer *cw) \
1233 { \
1234 struct bt_ctf_event_class *event_class; \
1235 int ret; \
1236 \
1237 pr("Adding "#_name" event\n"); \
1238 event_class = bt_ctf_event_class_create("perf_" #_name);\
1239 if (!event_class) \
1240 return -1; \
1241 body \
1242 \
1243 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\
1244 if (ret) { \
1245 pr("Failed to add event class '"#_name"' into stream.\n");\
1246 return ret; \
1247 } \
1248 \
1249 cw->_name##_class = event_class; \
1250 bt_ctf_event_class_put(event_class); \
1251 return 0; \
1252 }
1253
1254 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(comm,
1255 __NON_SAMPLE_ADD_FIELD(u32, pid);
1256 __NON_SAMPLE_ADD_FIELD(u32, tid);
1257 __NON_SAMPLE_ADD_FIELD(string, comm);
1258 )
1259
1260 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(fork,
1261 __NON_SAMPLE_ADD_FIELD(u32, pid);
1262 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1263 __NON_SAMPLE_ADD_FIELD(u32, tid);
1264 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1265 __NON_SAMPLE_ADD_FIELD(u64, time);
1266 )
1267
1268 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(exit,
1269 __NON_SAMPLE_ADD_FIELD(u32, pid);
1270 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1271 __NON_SAMPLE_ADD_FIELD(u32, tid);
1272 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1273 __NON_SAMPLE_ADD_FIELD(u64, time);
1274 )
1275
1276 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap,
1277 __NON_SAMPLE_ADD_FIELD(u32, pid);
1278 __NON_SAMPLE_ADD_FIELD(u32, tid);
1279 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1280 __NON_SAMPLE_ADD_FIELD(string, filename);
1281 )
1282
1283 __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap2,
1284 __NON_SAMPLE_ADD_FIELD(u32, pid);
1285 __NON_SAMPLE_ADD_FIELD(u32, tid);
1286 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1287 __NON_SAMPLE_ADD_FIELD(string, filename);
1288 )
1289 #undef __NON_SAMPLE_ADD_FIELD
1290 #undef __FUNC_ADD_NON_SAMPLE_EVENT_CLASS
1291
1292 static int setup_non_sample_events(struct ctf_writer *cw,
1293 struct perf_session *session __maybe_unused)
1294 {
1295 int ret;
1296
1297 ret = add_comm_event(cw);
1298 if (ret)
1299 return ret;
1300 ret = add_exit_event(cw);
1301 if (ret)
1302 return ret;
1303 ret = add_fork_event(cw);
1304 if (ret)
1305 return ret;
1306 ret = add_mmap_event(cw);
1307 if (ret)
1308 return ret;
1309 ret = add_mmap2_event(cw);
1310 if (ret)
1311 return ret;
1312 return 0;
1313 }
1314
1315 static void cleanup_events(struct perf_session *session)
1316 {
1317 struct evlist *evlist = session->evlist;
1318 struct evsel *evsel;
1319
1320 evlist__for_each_entry(evlist, evsel) {
1321 struct evsel_priv *priv;
1322
1323 priv = evsel->priv;
1324 bt_ctf_event_class_put(priv->event_class);
1325 zfree(&evsel->priv);
1326 }
1327
1328 evlist__delete(evlist);
1329 session->evlist = NULL;
1330 }
1331
1332 static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
1333 {
1334 struct ctf_stream **stream;
1335 struct perf_header *ph = &session->header;
1336 int ncpus;
1337
1338
1339
1340
1341
1342 ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS;
1343
1344 stream = zalloc(sizeof(*stream) * ncpus);
1345 if (!stream) {
1346 pr_err("Failed to allocate streams.\n");
1347 return -ENOMEM;
1348 }
1349
1350 cw->stream = stream;
1351 cw->stream_cnt = ncpus;
1352 return 0;
1353 }
1354
1355 static void free_streams(struct ctf_writer *cw)
1356 {
1357 int cpu;
1358
1359 for (cpu = 0; cpu < cw->stream_cnt; cpu++)
1360 ctf_stream__delete(cw->stream[cpu]);
1361
1362 zfree(&cw->stream);
1363 }
1364
1365 static int ctf_writer__setup_env(struct ctf_writer *cw,
1366 struct perf_session *session)
1367 {
1368 struct perf_header *header = &session->header;
1369 struct bt_ctf_writer *writer = cw->writer;
1370
1371 #define ADD(__n, __v) \
1372 do { \
1373 if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \
1374 return -1; \
1375 } while (0)
1376
1377 ADD("host", header->env.hostname);
1378 ADD("sysname", "Linux");
1379 ADD("release", header->env.os_release);
1380 ADD("version", header->env.version);
1381 ADD("machine", header->env.arch);
1382 ADD("domain", "kernel");
1383 ADD("tracer_name", "perf");
1384
1385 #undef ADD
1386 return 0;
1387 }
1388
1389 static int ctf_writer__setup_clock(struct ctf_writer *cw,
1390 struct perf_session *session,
1391 bool tod)
1392 {
1393 struct bt_ctf_clock *clock = cw->clock;
1394 const char *desc = "perf clock";
1395 int64_t offset = 0;
1396
1397 if (tod) {
1398 struct perf_env *env = &session->header.env;
1399
1400 if (!env->clock.enabled) {
1401 pr_err("Can't provide --tod time, missing clock data. "
1402 "Please record with -k/--clockid option.\n");
1403 return -1;
1404 }
1405
1406 desc = clockid_name(env->clock.clockid);
1407 offset = env->clock.tod_ns - env->clock.clockid_ns;
1408 }
1409
1410 #define SET(__n, __v) \
1411 do { \
1412 if (bt_ctf_clock_set_##__n(clock, __v)) \
1413 return -1; \
1414 } while (0)
1415
1416 SET(frequency, 1000000000);
1417 SET(offset, offset);
1418 SET(description, desc);
1419 SET(precision, 10);
1420 SET(is_absolute, 0);
1421
1422 #undef SET
1423 return 0;
1424 }
1425
1426 static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex)
1427 {
1428 struct bt_ctf_field_type *type;
1429
1430 type = bt_ctf_field_type_integer_create(size);
1431 if (!type)
1432 return NULL;
1433
1434 if (sign &&
1435 bt_ctf_field_type_integer_set_signed(type, 1))
1436 goto err;
1437
1438 if (hex &&
1439 bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL))
1440 goto err;
1441
1442 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1443 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_BIG_ENDIAN);
1444 #else
1445 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
1446 #endif
1447
1448 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
1449 size, sign ? "un" : "", hex ? "hex" : "");
1450 return type;
1451
1452 err:
1453 bt_ctf_field_type_put(type);
1454 return NULL;
1455 }
1456
1457 static void ctf_writer__cleanup_data(struct ctf_writer *cw)
1458 {
1459 unsigned int i;
1460
1461 for (i = 0; i < ARRAY_SIZE(cw->data.array); i++)
1462 bt_ctf_field_type_put(cw->data.array[i]);
1463 }
1464
1465 static int ctf_writer__init_data(struct ctf_writer *cw)
1466 {
1467 #define CREATE_INT_TYPE(type, size, sign, hex) \
1468 do { \
1469 (type) = create_int_type(size, sign, hex); \
1470 if (!(type)) \
1471 goto err; \
1472 } while (0)
1473
1474 CREATE_INT_TYPE(cw->data.s64, 64, true, false);
1475 CREATE_INT_TYPE(cw->data.u64, 64, false, false);
1476 CREATE_INT_TYPE(cw->data.s32, 32, true, false);
1477 CREATE_INT_TYPE(cw->data.u32, 32, false, false);
1478 CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true);
1479 CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true);
1480
1481 cw->data.string = bt_ctf_field_type_string_create();
1482 if (cw->data.string)
1483 return 0;
1484
1485 err:
1486 ctf_writer__cleanup_data(cw);
1487 pr_err("Failed to create data types.\n");
1488 return -1;
1489 }
1490
1491 static void ctf_writer__cleanup(struct ctf_writer *cw)
1492 {
1493 ctf_writer__cleanup_data(cw);
1494
1495 bt_ctf_clock_put(cw->clock);
1496 free_streams(cw);
1497 bt_ctf_stream_class_put(cw->stream_class);
1498 bt_ctf_writer_put(cw->writer);
1499
1500
1501 memset(cw, 0, sizeof(*cw));
1502 }
1503
1504 static int ctf_writer__init(struct ctf_writer *cw, const char *path,
1505 struct perf_session *session, bool tod)
1506 {
1507 struct bt_ctf_writer *writer;
1508 struct bt_ctf_stream_class *stream_class;
1509 struct bt_ctf_clock *clock;
1510 struct bt_ctf_field_type *pkt_ctx_type;
1511 int ret;
1512
1513
1514 writer = bt_ctf_writer_create(path);
1515 if (!writer)
1516 goto err;
1517
1518 cw->writer = writer;
1519
1520
1521 clock = bt_ctf_clock_create("perf_clock");
1522 if (!clock) {
1523 pr("Failed to create CTF clock.\n");
1524 goto err_cleanup;
1525 }
1526
1527 cw->clock = clock;
1528
1529 if (ctf_writer__setup_clock(cw, session, tod)) {
1530 pr("Failed to setup CTF clock.\n");
1531 goto err_cleanup;
1532 }
1533
1534
1535 stream_class = bt_ctf_stream_class_create("perf_stream");
1536 if (!stream_class) {
1537 pr("Failed to create CTF stream class.\n");
1538 goto err_cleanup;
1539 }
1540
1541 cw->stream_class = stream_class;
1542
1543
1544 if (bt_ctf_stream_class_set_clock(stream_class, clock)) {
1545 pr("Failed to assign CTF clock to stream class.\n");
1546 goto err_cleanup;
1547 }
1548
1549 if (ctf_writer__init_data(cw))
1550 goto err_cleanup;
1551
1552
1553 pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1554 if (!pkt_ctx_type)
1555 goto err_cleanup;
1556
1557 ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id");
1558 bt_ctf_field_type_put(pkt_ctx_type);
1559 if (ret)
1560 goto err_cleanup;
1561
1562
1563 if (bt_ctf_writer_add_clock(writer, clock)) {
1564 pr("Failed to assign CTF clock to writer.\n");
1565 goto err_cleanup;
1566 }
1567
1568 return 0;
1569
1570 err_cleanup:
1571 ctf_writer__cleanup(cw);
1572 err:
1573 pr_err("Failed to setup CTF writer.\n");
1574 return -1;
1575 }
1576
1577 static int ctf_writer__flush_streams(struct ctf_writer *cw)
1578 {
1579 int cpu, ret = 0;
1580
1581 for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++)
1582 ret = ctf_stream__flush(cw->stream[cpu]);
1583
1584 return ret;
1585 }
1586
1587 static int convert__config(const char *var, const char *value, void *cb)
1588 {
1589 struct convert *c = cb;
1590
1591 if (!strcmp(var, "convert.queue-size"))
1592 return perf_config_u64(&c->queue_size, var, value);
1593
1594 return 0;
1595 }
1596
1597 int bt_convert__perf2ctf(const char *input, const char *path,
1598 struct perf_data_convert_opts *opts)
1599 {
1600 struct perf_session *session;
1601 struct perf_data data = {
1602 .path = input,
1603 .mode = PERF_DATA_MODE_READ,
1604 .force = opts->force,
1605 };
1606 struct convert c = {
1607 .tool = {
1608 .sample = process_sample_event,
1609 .mmap = perf_event__process_mmap,
1610 .mmap2 = perf_event__process_mmap2,
1611 .comm = perf_event__process_comm,
1612 .exit = perf_event__process_exit,
1613 .fork = perf_event__process_fork,
1614 .lost = perf_event__process_lost,
1615 .tracing_data = perf_event__process_tracing_data,
1616 .build_id = perf_event__process_build_id,
1617 .namespaces = perf_event__process_namespaces,
1618 .ordered_events = true,
1619 .ordering_requires_timestamps = true,
1620 },
1621 };
1622 struct ctf_writer *cw = &c.writer;
1623 int err;
1624
1625 if (opts->all) {
1626 c.tool.comm = process_comm_event;
1627 c.tool.exit = process_exit_event;
1628 c.tool.fork = process_fork_event;
1629 c.tool.mmap = process_mmap_event;
1630 c.tool.mmap2 = process_mmap2_event;
1631 }
1632
1633 err = perf_config(convert__config, &c);
1634 if (err)
1635 return err;
1636
1637 err = -1;
1638
1639 session = perf_session__new(&data, &c.tool);
1640 if (IS_ERR(session))
1641 return PTR_ERR(session);
1642
1643
1644 if (ctf_writer__init(cw, path, session, opts->tod))
1645 goto free_session;
1646
1647 if (c.queue_size) {
1648 ordered_events__set_alloc_size(&session->ordered_events,
1649 c.queue_size);
1650 }
1651
1652
1653 if (ctf_writer__setup_env(cw, session))
1654 goto free_writer;
1655
1656
1657 if (setup_events(cw, session))
1658 goto free_writer;
1659
1660 if (opts->all && setup_non_sample_events(cw, session))
1661 goto free_writer;
1662
1663 if (setup_streams(cw, session))
1664 goto free_writer;
1665
1666 err = perf_session__process_events(session);
1667 if (!err)
1668 err = ctf_writer__flush_streams(cw);
1669 else
1670 pr_err("Error during conversion.\n");
1671
1672 fprintf(stderr,
1673 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
1674 data.path, path);
1675
1676 fprintf(stderr,
1677 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
1678 (double) c.events_size / 1024.0 / 1024.0,
1679 c.events_count);
1680
1681 if (!c.non_sample_count)
1682 fprintf(stderr, ") ]\n");
1683 else
1684 fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
1685
1686 cleanup_events(session);
1687 perf_session__delete(session);
1688 ctf_writer__cleanup(cw);
1689
1690 return err;
1691
1692 free_writer:
1693 ctf_writer__cleanup(cw);
1694 free_session:
1695 perf_session__delete(session);
1696 pr_err("Error during conversion setup.\n");
1697 return err;
1698 }