blob: 083101ae7b7725f8bfc5e55dfe0f63ef33edafd5 [file] [log] [blame]
Thomas Gleixner91007042019-05-29 07:12:25 -07001// SPDX-License-Identifier: GPL-2.0-only
Jiri Olsaedbe9812015-02-20 23:17:00 +01002/*
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>
Jiri Olsaedbe9812015-02-20 23:17:00 +01007 */
8
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03009#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030010#include <inttypes.h>
Jiri Olsaedbe9812015-02-20 23:17:00 +010011#include <linux/compiler.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030012#include <linux/kernel.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030013#include <linux/zalloc.h>
Jiri Olsaedbe9812015-02-20 23:17:00 +010014#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>
Wang Nane0a7cce2015-04-18 17:50:19 +020020#include <babeltrace/ctf-ir/utils.h>
Jiri Olsaedbe9812015-02-20 23:17:00 +010021#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"
Jiri Olsaedbe9812015-02-20 23:17:00 +010026#include "debug.h"
27#include "tool.h"
28#include "evlist.h"
29#include "evsel.h"
30#include "machine.h"
Jiri Olsaf6c12a02016-06-24 14:40:24 +020031#include "config.h"
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030032#include <linux/ctype.h>
Jiri Olsaedbe9812015-02-20 23:17:00 +010033
34#define pr_N(n, fmt, ...) \
35 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
36
37#define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
38#define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
39
40#define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
41
42struct evsel_priv {
43 struct bt_ctf_event_class *event_class;
44};
45
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +020046#define MAX_CPUS 4096
47
48struct ctf_stream {
49 struct bt_ctf_stream *stream;
50 int cpu;
Jiri Olsa89e5fa82015-04-18 17:50:16 +020051 u32 count;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +020052};
53
Jiri Olsaedbe9812015-02-20 23:17:00 +010054struct ctf_writer {
55 /* writer primitives */
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +020056 struct bt_ctf_writer *writer;
57 struct ctf_stream **stream;
58 int stream_cnt;
59 struct bt_ctf_stream_class *stream_class;
60 struct bt_ctf_clock *clock;
Jiri Olsaedbe9812015-02-20 23:17:00 +010061
62 /* data types */
63 union {
64 struct {
65 struct bt_ctf_field_type *s64;
66 struct bt_ctf_field_type *u64;
67 struct bt_ctf_field_type *s32;
68 struct bt_ctf_field_type *u32;
69 struct bt_ctf_field_type *string;
Wang Nan26812d42015-12-08 02:25:39 +000070 struct bt_ctf_field_type *u32_hex;
Jiri Olsaedbe9812015-02-20 23:17:00 +010071 struct bt_ctf_field_type *u64_hex;
72 };
73 struct bt_ctf_field_type *array[6];
74 } data;
Wang Nanf5a08ce2016-06-24 11:22:10 +000075 struct bt_ctf_event_class *comm_class;
Wang Nanebccba32016-06-24 11:22:12 +000076 struct bt_ctf_event_class *exit_class;
77 struct bt_ctf_event_class *fork_class;
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -040078 struct bt_ctf_event_class *mmap_class;
79 struct bt_ctf_event_class *mmap2_class;
Jiri Olsaedbe9812015-02-20 23:17:00 +010080};
81
82struct convert {
83 struct perf_tool tool;
84 struct ctf_writer writer;
85
86 u64 events_size;
87 u64 events_count;
Wang Nan8ee4c462016-06-24 11:22:09 +000088 u64 non_sample_count;
Jiri Olsa8fa46752015-04-18 17:50:17 +020089
90 /* Ordered events configured queue size. */
91 u64 queue_size;
Jiri Olsaedbe9812015-02-20 23:17:00 +010092};
93
94static int value_set(struct bt_ctf_field_type *type,
95 struct bt_ctf_event *event,
96 const char *name, u64 val)
97{
98 struct bt_ctf_field *field;
99 bool sign = bt_ctf_field_type_integer_get_signed(type);
100 int ret;
101
102 field = bt_ctf_field_create(type);
103 if (!field) {
104 pr_err("failed to create a field %s\n", name);
105 return -1;
106 }
107
108 if (sign) {
109 ret = bt_ctf_field_signed_integer_set_value(field, val);
110 if (ret) {
111 pr_err("failed to set field value %s\n", name);
112 goto err;
113 }
114 } else {
115 ret = bt_ctf_field_unsigned_integer_set_value(field, val);
116 if (ret) {
117 pr_err("failed to set field value %s\n", name);
118 goto err;
119 }
120 }
121
122 ret = bt_ctf_event_set_payload(event, name, field);
123 if (ret) {
124 pr_err("failed to set payload %s\n", name);
125 goto err;
126 }
127
128 pr2(" SET [%s = %" PRIu64 "]\n", name, val);
129
130err:
131 bt_ctf_field_put(field);
132 return ret;
133}
134
135#define __FUNC_VALUE_SET(_name, _val_type) \
136static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
137 struct bt_ctf_event *event, \
138 const char *name, \
139 _val_type val) \
140{ \
141 struct bt_ctf_field_type *type = cw->data._name; \
142 return value_set(type, event, name, (u64) val); \
143}
144
145#define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
146
147FUNC_VALUE_SET(s32)
148FUNC_VALUE_SET(u32)
149FUNC_VALUE_SET(s64)
150FUNC_VALUE_SET(u64)
151__FUNC_VALUE_SET(u64_hex, u64)
152
Wang Nan069ee5c2016-06-24 11:22:06 +0000153static int string_set_value(struct bt_ctf_field *field, const char *string);
154static __maybe_unused int
155value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event,
156 const char *name, const char *string)
157{
158 struct bt_ctf_field_type *type = cw->data.string;
159 struct bt_ctf_field *field;
160 int ret = 0;
161
162 field = bt_ctf_field_create(type);
163 if (!field) {
164 pr_err("failed to create a field %s\n", name);
165 return -1;
166 }
167
168 ret = string_set_value(field, string);
169 if (ret) {
170 pr_err("failed to set value %s\n", name);
171 goto err_put_field;
172 }
173
174 ret = bt_ctf_event_set_payload(event, name, field);
175 if (ret)
176 pr_err("failed to set payload %s\n", name);
177
178err_put_field:
179 bt_ctf_field_put(field);
180 return ret;
181}
182
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100183static struct bt_ctf_field_type*
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400184get_tracepoint_field_type(struct ctf_writer *cw, struct tep_format_field *field)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100185{
186 unsigned long flags = field->flags;
187
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400188 if (flags & TEP_FIELD_IS_STRING)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100189 return cw->data.string;
190
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400191 if (!(flags & TEP_FIELD_IS_SIGNED)) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100192 /* unsigned long are mostly pointers */
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400193 if (flags & TEP_FIELD_IS_LONG || flags & TEP_FIELD_IS_POINTER)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100194 return cw->data.u64_hex;
195 }
196
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400197 if (flags & TEP_FIELD_IS_SIGNED) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100198 if (field->size == 8)
199 return cw->data.s64;
200 else
201 return cw->data.s32;
202 }
203
204 if (field->size == 8)
205 return cw->data.u64;
206 else
207 return cw->data.u32;
208}
209
Wang Nand4ae4212015-04-18 17:50:20 +0200210static unsigned long long adjust_signedness(unsigned long long value_int, int size)
211{
212 unsigned long long value_mask;
213
214 /*
215 * value_mask = (1 << (size * 8 - 1)) - 1.
216 * Directly set value_mask for code readers.
217 */
218 switch (size) {
219 case 1:
220 value_mask = 0x7fULL;
221 break;
222 case 2:
223 value_mask = 0x7fffULL;
224 break;
225 case 4:
226 value_mask = 0x7fffffffULL;
227 break;
228 case 8:
229 /*
230 * For 64 bit value, return it self. There is no need
231 * to fill high bit.
232 */
233 /* Fall through */
234 default:
235 /* BUG! */
236 return value_int;
237 }
238
239 /* If it is a positive value, don't adjust. */
240 if ((value_int & (~0ULL - value_mask)) == 0)
241 return value_int;
242
243 /* Fill upper part of value_int with 1 to make it a negative long long. */
244 return (value_int & value_mask) | ~value_mask;
245}
246
Wang Nan5ea58882016-05-27 11:35:51 +0000247static int string_set_value(struct bt_ctf_field *field, const char *string)
248{
249 char *buffer = NULL;
250 size_t len = strlen(string), i, p;
251 int err;
252
253 for (i = p = 0; i < len; i++, p++) {
254 if (isprint(string[i])) {
255 if (!buffer)
256 continue;
257 buffer[p] = string[i];
258 } else {
259 char numstr[5];
260
261 snprintf(numstr, sizeof(numstr), "\\x%02x",
262 (unsigned int)(string[i]) & 0xff);
263
264 if (!buffer) {
265 buffer = zalloc(i + (len - i) * 4 + 2);
266 if (!buffer) {
267 pr_err("failed to set unprintable string '%s'\n", string);
268 return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING");
269 }
270 if (i > 0)
271 strncpy(buffer, string, i);
272 }
Shawn Landden97acec72019-05-18 15:32:38 -0300273 memcpy(buffer + p, numstr, 4);
Wang Nan5ea58882016-05-27 11:35:51 +0000274 p += 3;
275 }
276 }
277
278 if (!buffer)
279 return bt_ctf_field_string_set_value(field, string);
280 err = bt_ctf_field_string_set_value(field, buffer);
281 free(buffer);
282 return err;
283}
284
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100285static int add_tracepoint_field_value(struct ctf_writer *cw,
286 struct bt_ctf_event_class *event_class,
287 struct bt_ctf_event *event,
288 struct perf_sample *sample,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400289 struct tep_format_field *fmtf)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100290{
291 struct bt_ctf_field_type *type;
292 struct bt_ctf_field *array_field;
293 struct bt_ctf_field *field;
294 const char *name = fmtf->name;
295 void *data = sample->raw_data;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100296 unsigned long flags = fmtf->flags;
297 unsigned int n_items;
298 unsigned int i;
299 unsigned int offset;
300 unsigned int len;
301 int ret;
302
Wang Nane0a7cce2015-04-18 17:50:19 +0200303 name = fmtf->alias;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100304 offset = fmtf->offset;
305 len = fmtf->size;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400306 if (flags & TEP_FIELD_IS_STRING)
307 flags &= ~TEP_FIELD_IS_ARRAY;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100308
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400309 if (flags & TEP_FIELD_IS_DYNAMIC) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100310 unsigned long long tmp_val;
311
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -0400312 tmp_val = tep_read_number(fmtf->event->tep,
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400313 data + offset, len);
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100314 offset = tmp_val;
315 len = offset >> 16;
316 offset &= 0xffff;
317 }
318
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400319 if (flags & TEP_FIELD_IS_ARRAY) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100320
321 type = bt_ctf_event_class_get_field_by_name(
322 event_class, name);
323 array_field = bt_ctf_field_create(type);
324 bt_ctf_field_type_put(type);
325 if (!array_field) {
326 pr_err("Failed to create array type %s\n", name);
327 return -1;
328 }
329
330 len = fmtf->size / fmtf->arraylen;
331 n_items = fmtf->arraylen;
332 } else {
333 n_items = 1;
334 array_field = NULL;
335 }
336
337 type = get_tracepoint_field_type(cw, fmtf);
338
339 for (i = 0; i < n_items; i++) {
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400340 if (flags & TEP_FIELD_IS_ARRAY)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100341 field = bt_ctf_field_array_get_field(array_field, i);
342 else
343 field = bt_ctf_field_create(type);
344
345 if (!field) {
346 pr_err("failed to create a field %s\n", name);
347 return -1;
348 }
349
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400350 if (flags & TEP_FIELD_IS_STRING)
Wang Nan5ea58882016-05-27 11:35:51 +0000351 ret = string_set_value(field, data + offset + i * len);
Wang Nand4ae4212015-04-18 17:50:20 +0200352 else {
353 unsigned long long value_int;
354
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400355 value_int = tep_read_number(
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -0400356 fmtf->event->tep,
Wang Nand4ae4212015-04-18 17:50:20 +0200357 data + offset + i * len, len);
358
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400359 if (!(flags & TEP_FIELD_IS_SIGNED))
Wang Nand4ae4212015-04-18 17:50:20 +0200360 ret = bt_ctf_field_unsigned_integer_set_value(
361 field, value_int);
362 else
363 ret = bt_ctf_field_signed_integer_set_value(
364 field, adjust_signedness(value_int, len));
365 }
366
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100367 if (ret) {
368 pr_err("failed to set file value %s\n", name);
369 goto err_put_field;
370 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400371 if (!(flags & TEP_FIELD_IS_ARRAY)) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100372 ret = bt_ctf_event_set_payload(event, name, field);
373 if (ret) {
374 pr_err("failed to set payload %s\n", name);
375 goto err_put_field;
376 }
377 }
378 bt_ctf_field_put(field);
379 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400380 if (flags & TEP_FIELD_IS_ARRAY) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100381 ret = bt_ctf_event_set_payload(event, name, array_field);
382 if (ret) {
383 pr_err("Failed add payload array %s\n", name);
384 return -1;
385 }
386 bt_ctf_field_put(array_field);
387 }
388 return 0;
389
390err_put_field:
391 bt_ctf_field_put(field);
392 return -1;
393}
394
395static int add_tracepoint_fields_values(struct ctf_writer *cw,
396 struct bt_ctf_event_class *event_class,
397 struct bt_ctf_event *event,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400398 struct tep_format_field *fields,
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100399 struct perf_sample *sample)
400{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400401 struct tep_format_field *field;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100402 int ret;
403
404 for (field = fields; field; field = field->next) {
405 ret = add_tracepoint_field_value(cw, event_class, event, sample,
406 field);
407 if (ret)
408 return -1;
409 }
410 return 0;
411}
412
413static int add_tracepoint_values(struct ctf_writer *cw,
414 struct bt_ctf_event_class *event_class,
415 struct bt_ctf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200416 struct evsel *evsel,
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100417 struct perf_sample *sample)
418{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400419 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
420 struct tep_format_field *fields = evsel->tp_format->format.fields;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100421 int ret;
422
423 ret = add_tracepoint_fields_values(cw, event_class, event,
424 common_fields, sample);
425 if (!ret)
426 ret = add_tracepoint_fields_values(cw, event_class, event,
427 fields, sample);
428
429 return ret;
430}
431
Wang Nan6122d572016-02-26 09:31:56 +0000432static int
433add_bpf_output_values(struct bt_ctf_event_class *event_class,
434 struct bt_ctf_event *event,
435 struct perf_sample *sample)
436{
437 struct bt_ctf_field_type *len_type, *seq_type;
438 struct bt_ctf_field *len_field, *seq_field;
439 unsigned int raw_size = sample->raw_size;
440 unsigned int nr_elements = raw_size / sizeof(u32);
441 unsigned int i;
442 int ret;
443
444 if (nr_elements * sizeof(u32) != raw_size)
Wang Nanf2c88522016-09-28 03:58:46 +0000445 pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n",
Wang Nan6122d572016-02-26 09:31:56 +0000446 raw_size, nr_elements * sizeof(u32) - raw_size);
447
448 len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len");
449 len_field = bt_ctf_field_create(len_type);
450 if (!len_field) {
451 pr_err("failed to create 'raw_len' for bpf output event\n");
452 ret = -1;
453 goto put_len_type;
454 }
455
456 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
457 if (ret) {
458 pr_err("failed to set field value for raw_len\n");
459 goto put_len_field;
460 }
461 ret = bt_ctf_event_set_payload(event, "raw_len", len_field);
462 if (ret) {
463 pr_err("failed to set payload to raw_len\n");
464 goto put_len_field;
465 }
466
467 seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data");
468 seq_field = bt_ctf_field_create(seq_type);
469 if (!seq_field) {
470 pr_err("failed to create 'raw_data' for bpf output event\n");
471 ret = -1;
472 goto put_seq_type;
473 }
474
475 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
476 if (ret) {
477 pr_err("failed to set length of 'raw_data'\n");
478 goto put_seq_field;
479 }
480
481 for (i = 0; i < nr_elements; i++) {
482 struct bt_ctf_field *elem_field =
483 bt_ctf_field_sequence_get_field(seq_field, i);
484
485 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
486 ((u32 *)(sample->raw_data))[i]);
487
488 bt_ctf_field_put(elem_field);
489 if (ret) {
490 pr_err("failed to set raw_data[%d]\n", i);
491 goto put_seq_field;
492 }
493 }
494
495 ret = bt_ctf_event_set_payload(event, "raw_data", seq_field);
496 if (ret)
497 pr_err("failed to set payload for raw_data\n");
498
499put_seq_field:
500 bt_ctf_field_put(seq_field);
501put_seq_type:
502 bt_ctf_field_type_put(seq_type);
503put_len_field:
504 bt_ctf_field_put(len_field);
505put_len_type:
506 bt_ctf_field_type_put(len_type);
507 return ret;
508}
509
Geneviève Bastiena3073c82017-07-27 14:12:03 -0400510static int
511add_callchain_output_values(struct bt_ctf_event_class *event_class,
512 struct bt_ctf_event *event,
513 struct ip_callchain *callchain)
514{
515 struct bt_ctf_field_type *len_type, *seq_type;
516 struct bt_ctf_field *len_field, *seq_field;
517 unsigned int nr_elements = callchain->nr;
518 unsigned int i;
519 int ret;
520
521 len_type = bt_ctf_event_class_get_field_by_name(
522 event_class, "perf_callchain_size");
523 len_field = bt_ctf_field_create(len_type);
524 if (!len_field) {
525 pr_err("failed to create 'perf_callchain_size' for callchain output event\n");
526 ret = -1;
527 goto put_len_type;
528 }
529
530 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
531 if (ret) {
532 pr_err("failed to set field value for perf_callchain_size\n");
533 goto put_len_field;
534 }
535 ret = bt_ctf_event_set_payload(event, "perf_callchain_size", len_field);
536 if (ret) {
537 pr_err("failed to set payload to perf_callchain_size\n");
538 goto put_len_field;
539 }
540
541 seq_type = bt_ctf_event_class_get_field_by_name(
542 event_class, "perf_callchain");
543 seq_field = bt_ctf_field_create(seq_type);
544 if (!seq_field) {
545 pr_err("failed to create 'perf_callchain' for callchain output event\n");
546 ret = -1;
547 goto put_seq_type;
548 }
549
550 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
551 if (ret) {
552 pr_err("failed to set length of 'perf_callchain'\n");
553 goto put_seq_field;
554 }
555
556 for (i = 0; i < nr_elements; i++) {
557 struct bt_ctf_field *elem_field =
558 bt_ctf_field_sequence_get_field(seq_field, i);
559
560 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
561 ((u64 *)(callchain->ips))[i]);
562
563 bt_ctf_field_put(elem_field);
564 if (ret) {
565 pr_err("failed to set callchain[%d]\n", i);
566 goto put_seq_field;
567 }
568 }
569
570 ret = bt_ctf_event_set_payload(event, "perf_callchain", seq_field);
571 if (ret)
572 pr_err("failed to set payload for raw_data\n");
573
574put_seq_field:
575 bt_ctf_field_put(seq_field);
576put_seq_type:
577 bt_ctf_field_type_put(seq_type);
578put_len_field:
579 bt_ctf_field_put(len_field);
580put_len_type:
581 bt_ctf_field_type_put(len_type);
582 return ret;
583}
584
Jiri Olsaedbe9812015-02-20 23:17:00 +0100585static int add_generic_values(struct ctf_writer *cw,
586 struct bt_ctf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200587 struct evsel *evsel,
Jiri Olsaedbe9812015-02-20 23:17:00 +0100588 struct perf_sample *sample)
589{
590 u64 type = evsel->attr.sample_type;
591 int ret;
592
593 /*
594 * missing:
595 * PERF_SAMPLE_TIME - not needed as we have it in
596 * ctf event header
597 * PERF_SAMPLE_READ - TODO
Jiri Olsaedbe9812015-02-20 23:17:00 +0100598 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
599 * PERF_SAMPLE_BRANCH_STACK - TODO
600 * PERF_SAMPLE_REGS_USER - TODO
601 * PERF_SAMPLE_STACK_USER - TODO
602 */
603
604 if (type & PERF_SAMPLE_IP) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100605 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100606 if (ret)
607 return -1;
608 }
609
610 if (type & PERF_SAMPLE_TID) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100611 ret = value_set_s32(cw, event, "perf_tid", sample->tid);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100612 if (ret)
613 return -1;
614
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100615 ret = value_set_s32(cw, event, "perf_pid", sample->pid);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100616 if (ret)
617 return -1;
618 }
619
620 if ((type & PERF_SAMPLE_ID) ||
621 (type & PERF_SAMPLE_IDENTIFIER)) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100622 ret = value_set_u64(cw, event, "perf_id", sample->id);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100623 if (ret)
624 return -1;
625 }
626
627 if (type & PERF_SAMPLE_STREAM_ID) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100628 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100629 if (ret)
630 return -1;
631 }
632
Jiri Olsaedbe9812015-02-20 23:17:00 +0100633 if (type & PERF_SAMPLE_PERIOD) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100634 ret = value_set_u64(cw, event, "perf_period", sample->period);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100635 if (ret)
636 return -1;
637 }
638
639 if (type & PERF_SAMPLE_WEIGHT) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100640 ret = value_set_u64(cw, event, "perf_weight", sample->weight);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100641 if (ret)
642 return -1;
643 }
644
645 if (type & PERF_SAMPLE_DATA_SRC) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100646 ret = value_set_u64(cw, event, "perf_data_src",
647 sample->data_src);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100648 if (ret)
649 return -1;
650 }
651
652 if (type & PERF_SAMPLE_TRANSACTION) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100653 ret = value_set_u64(cw, event, "perf_transaction",
654 sample->transaction);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100655 if (ret)
656 return -1;
657 }
658
659 return 0;
660}
661
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200662static int ctf_stream__flush(struct ctf_stream *cs)
663{
664 int err = 0;
665
666 if (cs) {
667 err = bt_ctf_stream_flush(cs->stream);
668 if (err)
669 pr_err("CTF stream %d flush failed\n", cs->cpu);
670
Jiri Olsa89e5fa82015-04-18 17:50:16 +0200671 pr("Flush stream for cpu %d (%u samples)\n",
672 cs->cpu, cs->count);
673
674 cs->count = 0;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200675 }
676
677 return err;
678}
679
680static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
681{
682 struct ctf_stream *cs;
683 struct bt_ctf_field *pkt_ctx = NULL;
684 struct bt_ctf_field *cpu_field = NULL;
685 struct bt_ctf_stream *stream = NULL;
686 int ret;
687
688 cs = zalloc(sizeof(*cs));
689 if (!cs) {
690 pr_err("Failed to allocate ctf stream\n");
691 return NULL;
692 }
693
694 stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
695 if (!stream) {
696 pr_err("Failed to create CTF stream\n");
697 goto out;
698 }
699
700 pkt_ctx = bt_ctf_stream_get_packet_context(stream);
701 if (!pkt_ctx) {
702 pr_err("Failed to obtain packet context\n");
703 goto out;
704 }
705
706 cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
707 bt_ctf_field_put(pkt_ctx);
708 if (!cpu_field) {
709 pr_err("Failed to obtain cpu field\n");
710 goto out;
711 }
712
713 ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
714 if (ret) {
715 pr_err("Failed to update CPU number\n");
716 goto out;
717 }
718
719 bt_ctf_field_put(cpu_field);
720
721 cs->cpu = cpu;
722 cs->stream = stream;
723 return cs;
724
725out:
726 if (cpu_field)
727 bt_ctf_field_put(cpu_field);
728 if (stream)
729 bt_ctf_stream_put(stream);
730
731 free(cs);
732 return NULL;
733}
734
735static void ctf_stream__delete(struct ctf_stream *cs)
736{
737 if (cs) {
738 bt_ctf_stream_put(cs->stream);
739 free(cs);
740 }
741}
742
743static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
744{
745 struct ctf_stream *cs = cw->stream[cpu];
746
747 if (!cs) {
748 cs = ctf_stream__create(cw, cpu);
749 cw->stream[cpu] = cs;
750 }
751
752 return cs;
753}
754
755static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200756 struct evsel *evsel)
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200757{
758 int cpu = 0;
759
760 if (evsel->attr.sample_type & PERF_SAMPLE_CPU)
761 cpu = sample->cpu;
762
763 if (cpu > cw->stream_cnt) {
764 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
765 cpu, cw->stream_cnt);
766 cpu = 0;
767 }
768
769 return cpu;
770}
771
Jiri Olsa89e5fa82015-04-18 17:50:16 +0200772#define STREAM_FLUSH_COUNT 100000
773
774/*
775 * Currently we have no other way to determine the
776 * time for the stream flush other than keep track
777 * of the number of events and check it against
778 * threshold.
779 */
780static bool is_flush_needed(struct ctf_stream *cs)
781{
782 return cs->count >= STREAM_FLUSH_COUNT;
783}
784
Jiri Olsaedbe9812015-02-20 23:17:00 +0100785static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300786 union perf_event *_event,
Jiri Olsaedbe9812015-02-20 23:17:00 +0100787 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200788 struct evsel *evsel,
Jiri Olsaedbe9812015-02-20 23:17:00 +0100789 struct machine *machine __maybe_unused)
790{
791 struct convert *c = container_of(tool, struct convert, tool);
792 struct evsel_priv *priv = evsel->priv;
793 struct ctf_writer *cw = &c->writer;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200794 struct ctf_stream *cs;
Jiri Olsaedbe9812015-02-20 23:17:00 +0100795 struct bt_ctf_event_class *event_class;
796 struct bt_ctf_event *event;
797 int ret;
Geneviève Bastiena3073c82017-07-27 14:12:03 -0400798 unsigned long type = evsel->attr.sample_type;
Jiri Olsaedbe9812015-02-20 23:17:00 +0100799
800 if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
801 return 0;
802
803 event_class = priv->event_class;
804
805 /* update stats */
806 c->events_count++;
807 c->events_size += _event->header.size;
808
809 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
810
811 event = bt_ctf_event_create(event_class);
812 if (!event) {
813 pr_err("Failed to create an CTF event\n");
814 return -1;
815 }
816
817 bt_ctf_clock_set_time(cw->clock, sample->time);
818
819 ret = add_generic_values(cw, event, evsel, sample);
820 if (ret)
821 return -1;
822
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100823 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
824 ret = add_tracepoint_values(cw, event_class, event,
825 evsel, sample);
826 if (ret)
827 return -1;
828 }
829
Geneviève Bastiena3073c82017-07-27 14:12:03 -0400830 if (type & PERF_SAMPLE_CALLCHAIN) {
831 ret = add_callchain_output_values(event_class,
832 event, sample->callchain);
833 if (ret)
834 return -1;
835 }
836
Wang Nan6122d572016-02-26 09:31:56 +0000837 if (perf_evsel__is_bpf_output(evsel)) {
838 ret = add_bpf_output_values(event_class, event, sample);
839 if (ret)
840 return -1;
841 }
842
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200843 cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel));
Jiri Olsa89e5fa82015-04-18 17:50:16 +0200844 if (cs) {
845 if (is_flush_needed(cs))
846 ctf_stream__flush(cs);
847
848 cs->count++;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200849 bt_ctf_stream_append_event(cs->stream, event);
Jiri Olsa89e5fa82015-04-18 17:50:16 +0200850 }
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200851
Jiri Olsaedbe9812015-02-20 23:17:00 +0100852 bt_ctf_event_put(event);
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200853 return cs ? 0 : -1;
Jiri Olsaedbe9812015-02-20 23:17:00 +0100854}
855
Wang Nanf5a08ce2016-06-24 11:22:10 +0000856#define __NON_SAMPLE_SET_FIELD(_name, _type, _field) \
857do { \
858 ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
859 if (ret) \
860 return -1; \
861} while(0)
862
863#define __FUNC_PROCESS_NON_SAMPLE(_name, body) \
864static int process_##_name##_event(struct perf_tool *tool, \
865 union perf_event *_event, \
866 struct perf_sample *sample, \
867 struct machine *machine) \
868{ \
869 struct convert *c = container_of(tool, struct convert, tool);\
870 struct ctf_writer *cw = &c->writer; \
871 struct bt_ctf_event_class *event_class = cw->_name##_class;\
872 struct bt_ctf_event *event; \
873 struct ctf_stream *cs; \
874 int ret; \
875 \
876 c->non_sample_count++; \
877 c->events_size += _event->header.size; \
878 event = bt_ctf_event_create(event_class); \
879 if (!event) { \
880 pr_err("Failed to create an CTF event\n"); \
881 return -1; \
882 } \
883 \
884 bt_ctf_clock_set_time(cw->clock, sample->time); \
885 body \
886 cs = ctf_stream(cw, 0); \
887 if (cs) { \
888 if (is_flush_needed(cs)) \
889 ctf_stream__flush(cs); \
890 \
891 cs->count++; \
892 bt_ctf_stream_append_event(cs->stream, event); \
893 } \
894 bt_ctf_event_put(event); \
895 \
896 return perf_event__process_##_name(tool, _event, sample, machine);\
897}
898
899__FUNC_PROCESS_NON_SAMPLE(comm,
900 __NON_SAMPLE_SET_FIELD(comm, u32, pid);
901 __NON_SAMPLE_SET_FIELD(comm, u32, tid);
902 __NON_SAMPLE_SET_FIELD(comm, string, comm);
903)
Wang Nanebccba32016-06-24 11:22:12 +0000904__FUNC_PROCESS_NON_SAMPLE(fork,
905 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
906 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
907 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
908 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
909 __NON_SAMPLE_SET_FIELD(fork, u64, time);
910)
911
912__FUNC_PROCESS_NON_SAMPLE(exit,
913 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
914 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
915 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
916 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
917 __NON_SAMPLE_SET_FIELD(fork, u64, time);
918)
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -0400919__FUNC_PROCESS_NON_SAMPLE(mmap,
920 __NON_SAMPLE_SET_FIELD(mmap, u32, pid);
921 __NON_SAMPLE_SET_FIELD(mmap, u32, tid);
922 __NON_SAMPLE_SET_FIELD(mmap, u64_hex, start);
923 __NON_SAMPLE_SET_FIELD(mmap, string, filename);
924)
925__FUNC_PROCESS_NON_SAMPLE(mmap2,
926 __NON_SAMPLE_SET_FIELD(mmap2, u32, pid);
927 __NON_SAMPLE_SET_FIELD(mmap2, u32, tid);
928 __NON_SAMPLE_SET_FIELD(mmap2, u64_hex, start);
929 __NON_SAMPLE_SET_FIELD(mmap2, string, filename);
930)
Wang Nanf5a08ce2016-06-24 11:22:10 +0000931#undef __NON_SAMPLE_SET_FIELD
932#undef __FUNC_PROCESS_NON_SAMPLE
933
Wang Nane0a7cce2015-04-18 17:50:19 +0200934/* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
935static char *change_name(char *name, char *orig_name, int dup)
936{
937 char *new_name = NULL;
938 size_t len;
939
940 if (!name)
941 name = orig_name;
942
943 if (dup >= 10)
944 goto out;
945 /*
946 * Add '_' prefix to potential keywork. According to
947 * Mathieu Desnoyers (https://lkml.org/lkml/2015/1/23/652),
948 * futher CTF spec updating may require us to use '$'.
949 */
950 if (dup < 0)
951 len = strlen(name) + sizeof("_");
952 else
953 len = strlen(orig_name) + sizeof("_dupl_X");
954
955 new_name = malloc(len);
956 if (!new_name)
957 goto out;
958
959 if (dup < 0)
960 snprintf(new_name, len, "_%s", name);
961 else
962 snprintf(new_name, len, "%s_dupl_%d", orig_name, dup);
963
964out:
965 if (name != orig_name)
966 free(name);
967 return new_name;
968}
969
970static int event_class_add_field(struct bt_ctf_event_class *event_class,
971 struct bt_ctf_field_type *type,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400972 struct tep_format_field *field)
Wang Nane0a7cce2015-04-18 17:50:19 +0200973{
974 struct bt_ctf_field_type *t = NULL;
975 char *name;
976 int dup = 1;
977 int ret;
978
979 /* alias was already assigned */
980 if (field->alias != field->name)
981 return bt_ctf_event_class_add_field(event_class, type,
982 (char *)field->alias);
983
984 name = field->name;
985
986 /* If 'name' is a keywork, add prefix. */
987 if (bt_ctf_validate_identifier(name))
988 name = change_name(name, field->name, -1);
989
990 if (!name) {
991 pr_err("Failed to fix invalid identifier.");
992 return -1;
993 }
994 while ((t = bt_ctf_event_class_get_field_by_name(event_class, name))) {
995 bt_ctf_field_type_put(t);
996 name = change_name(name, field->name, dup++);
997 if (!name) {
998 pr_err("Failed to create dup name for '%s'\n", field->name);
999 return -1;
1000 }
1001 }
1002
1003 ret = bt_ctf_event_class_add_field(event_class, type, name);
1004 if (!ret)
1005 field->alias = name;
1006
1007 return ret;
1008}
1009
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001010static int add_tracepoint_fields_types(struct ctf_writer *cw,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001011 struct tep_format_field *fields,
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001012 struct bt_ctf_event_class *event_class)
1013{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001014 struct tep_format_field *field;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001015 int ret;
1016
1017 for (field = fields; field; field = field->next) {
1018 struct bt_ctf_field_type *type;
1019 unsigned long flags = field->flags;
1020
1021 pr2(" field '%s'\n", field->name);
1022
1023 type = get_tracepoint_field_type(cw, field);
1024 if (!type)
1025 return -1;
1026
1027 /*
1028 * A string is an array of chars. For this we use the string
1029 * type and don't care that it is an array. What we don't
1030 * support is an array of strings.
1031 */
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001032 if (flags & TEP_FIELD_IS_STRING)
1033 flags &= ~TEP_FIELD_IS_ARRAY;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001034
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001035 if (flags & TEP_FIELD_IS_ARRAY)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001036 type = bt_ctf_field_type_array_create(type, field->arraylen);
1037
Wang Nane0a7cce2015-04-18 17:50:19 +02001038 ret = event_class_add_field(event_class, type, field);
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001039
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001040 if (flags & TEP_FIELD_IS_ARRAY)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001041 bt_ctf_field_type_put(type);
1042
1043 if (ret) {
Wang Nane0a7cce2015-04-18 17:50:19 +02001044 pr_err("Failed to add field '%s': %d\n",
1045 field->name, ret);
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001046 return -1;
1047 }
1048 }
1049
1050 return 0;
1051}
1052
1053static int add_tracepoint_types(struct ctf_writer *cw,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001054 struct evsel *evsel,
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001055 struct bt_ctf_event_class *class)
1056{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001057 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
1058 struct tep_format_field *fields = evsel->tp_format->format.fields;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001059 int ret;
1060
1061 ret = add_tracepoint_fields_types(cw, common_fields, class);
1062 if (!ret)
1063 ret = add_tracepoint_fields_types(cw, fields, class);
1064
1065 return ret;
1066}
1067
Wang Nan6122d572016-02-26 09:31:56 +00001068static int add_bpf_output_types(struct ctf_writer *cw,
1069 struct bt_ctf_event_class *class)
1070{
1071 struct bt_ctf_field_type *len_type = cw->data.u32;
1072 struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex;
1073 struct bt_ctf_field_type *seq_type;
1074 int ret;
1075
1076 ret = bt_ctf_event_class_add_field(class, len_type, "raw_len");
1077 if (ret)
1078 return ret;
1079
1080 seq_type = bt_ctf_field_type_sequence_create(seq_base_type, "raw_len");
1081 if (!seq_type)
1082 return -1;
1083
1084 return bt_ctf_event_class_add_field(class, seq_type, "raw_data");
1085}
1086
Jiri Olsa32dcd022019-07-21 13:23:51 +02001087static int add_generic_types(struct ctf_writer *cw, struct evsel *evsel,
Jiri Olsaedbe9812015-02-20 23:17:00 +01001088 struct bt_ctf_event_class *event_class)
1089{
1090 u64 type = evsel->attr.sample_type;
1091
1092 /*
1093 * missing:
1094 * PERF_SAMPLE_TIME - not needed as we have it in
1095 * ctf event header
1096 * PERF_SAMPLE_READ - TODO
1097 * PERF_SAMPLE_CALLCHAIN - TODO
Wang Nan6122d572016-02-26 09:31:56 +00001098 * PERF_SAMPLE_RAW - tracepoint fields and BPF output
1099 * are handled separately
Jiri Olsaedbe9812015-02-20 23:17:00 +01001100 * PERF_SAMPLE_BRANCH_STACK - TODO
1101 * PERF_SAMPLE_REGS_USER - TODO
1102 * PERF_SAMPLE_STACK_USER - TODO
1103 */
1104
1105#define ADD_FIELD(cl, t, n) \
1106 do { \
1107 pr2(" field '%s'\n", n); \
1108 if (bt_ctf_event_class_add_field(cl, t, n)) { \
Wang Nane0a7cce2015-04-18 17:50:19 +02001109 pr_err("Failed to add field '%s';\n", n); \
Jiri Olsaedbe9812015-02-20 23:17:00 +01001110 return -1; \
1111 } \
1112 } while (0)
1113
1114 if (type & PERF_SAMPLE_IP)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001115 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001116
1117 if (type & PERF_SAMPLE_TID) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001118 ADD_FIELD(event_class, cw->data.s32, "perf_tid");
1119 ADD_FIELD(event_class, cw->data.s32, "perf_pid");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001120 }
1121
1122 if ((type & PERF_SAMPLE_ID) ||
1123 (type & PERF_SAMPLE_IDENTIFIER))
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001124 ADD_FIELD(event_class, cw->data.u64, "perf_id");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001125
1126 if (type & PERF_SAMPLE_STREAM_ID)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001127 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001128
Jiri Olsaedbe9812015-02-20 23:17:00 +01001129 if (type & PERF_SAMPLE_PERIOD)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001130 ADD_FIELD(event_class, cw->data.u64, "perf_period");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001131
1132 if (type & PERF_SAMPLE_WEIGHT)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001133 ADD_FIELD(event_class, cw->data.u64, "perf_weight");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001134
1135 if (type & PERF_SAMPLE_DATA_SRC)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001136 ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001137
1138 if (type & PERF_SAMPLE_TRANSACTION)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001139 ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001140
Geneviève Bastiena3073c82017-07-27 14:12:03 -04001141 if (type & PERF_SAMPLE_CALLCHAIN) {
1142 ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size");
1143 ADD_FIELD(event_class,
1144 bt_ctf_field_type_sequence_create(
1145 cw->data.u64_hex, "perf_callchain_size"),
1146 "perf_callchain");
1147 }
1148
Jiri Olsaedbe9812015-02-20 23:17:00 +01001149#undef ADD_FIELD
1150 return 0;
1151}
1152
Jiri Olsa32dcd022019-07-21 13:23:51 +02001153static int add_event(struct ctf_writer *cw, struct evsel *evsel)
Jiri Olsaedbe9812015-02-20 23:17:00 +01001154{
1155 struct bt_ctf_event_class *event_class;
1156 struct evsel_priv *priv;
1157 const char *name = perf_evsel__name(evsel);
1158 int ret;
1159
1160 pr("Adding event '%s' (type %d)\n", name, evsel->attr.type);
1161
1162 event_class = bt_ctf_event_class_create(name);
1163 if (!event_class)
1164 return -1;
1165
1166 ret = add_generic_types(cw, evsel, event_class);
1167 if (ret)
1168 goto err;
1169
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001170 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
1171 ret = add_tracepoint_types(cw, evsel, event_class);
1172 if (ret)
1173 goto err;
1174 }
1175
Wang Nan6122d572016-02-26 09:31:56 +00001176 if (perf_evsel__is_bpf_output(evsel)) {
1177 ret = add_bpf_output_types(cw, event_class);
1178 if (ret)
1179 goto err;
1180 }
1181
Jiri Olsaedbe9812015-02-20 23:17:00 +01001182 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);
1183 if (ret) {
1184 pr("Failed to add event class into stream.\n");
1185 goto err;
1186 }
1187
1188 priv = malloc(sizeof(*priv));
1189 if (!priv)
1190 goto err;
1191
1192 priv->event_class = event_class;
1193 evsel->priv = priv;
1194 return 0;
1195
1196err:
1197 bt_ctf_event_class_put(event_class);
1198 pr_err("Failed to add event '%s'.\n", name);
1199 return -1;
1200}
1201
1202static int setup_events(struct ctf_writer *cw, struct perf_session *session)
1203{
Jiri Olsa63503db2019-07-21 13:23:52 +02001204 struct evlist *evlist = session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001205 struct evsel *evsel;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001206 int ret;
1207
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001208 evlist__for_each_entry(evlist, evsel) {
Jiri Olsaedbe9812015-02-20 23:17:00 +01001209 ret = add_event(cw, evsel);
1210 if (ret)
1211 return ret;
1212 }
1213 return 0;
1214}
1215
Wang Nanf5a08ce2016-06-24 11:22:10 +00001216#define __NON_SAMPLE_ADD_FIELD(t, n) \
1217 do { \
1218 pr2(" field '%s'\n", #n); \
1219 if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\
1220 pr_err("Failed to add field '%s';\n", #n);\
1221 return -1; \
1222 } \
1223 } while(0)
1224
1225#define __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(_name, body) \
1226static int add_##_name##_event(struct ctf_writer *cw) \
1227{ \
1228 struct bt_ctf_event_class *event_class; \
1229 int ret; \
1230 \
1231 pr("Adding "#_name" event\n"); \
1232 event_class = bt_ctf_event_class_create("perf_" #_name);\
1233 if (!event_class) \
1234 return -1; \
1235 body \
1236 \
1237 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\
1238 if (ret) { \
1239 pr("Failed to add event class '"#_name"' into stream.\n");\
1240 return ret; \
1241 } \
1242 \
1243 cw->_name##_class = event_class; \
1244 bt_ctf_event_class_put(event_class); \
1245 return 0; \
1246}
1247
1248__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(comm,
1249 __NON_SAMPLE_ADD_FIELD(u32, pid);
1250 __NON_SAMPLE_ADD_FIELD(u32, tid);
1251 __NON_SAMPLE_ADD_FIELD(string, comm);
1252)
1253
Wang Nanebccba32016-06-24 11:22:12 +00001254__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(fork,
1255 __NON_SAMPLE_ADD_FIELD(u32, pid);
1256 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1257 __NON_SAMPLE_ADD_FIELD(u32, tid);
1258 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1259 __NON_SAMPLE_ADD_FIELD(u64, time);
1260)
1261
1262__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(exit,
1263 __NON_SAMPLE_ADD_FIELD(u32, pid);
1264 __NON_SAMPLE_ADD_FIELD(u32, ppid);
1265 __NON_SAMPLE_ADD_FIELD(u32, tid);
1266 __NON_SAMPLE_ADD_FIELD(u32, ptid);
1267 __NON_SAMPLE_ADD_FIELD(u64, time);
1268)
1269
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -04001270__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap,
1271 __NON_SAMPLE_ADD_FIELD(u32, pid);
1272 __NON_SAMPLE_ADD_FIELD(u32, tid);
1273 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1274 __NON_SAMPLE_ADD_FIELD(string, filename);
1275)
1276
1277__FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap2,
1278 __NON_SAMPLE_ADD_FIELD(u32, pid);
1279 __NON_SAMPLE_ADD_FIELD(u32, tid);
1280 __NON_SAMPLE_ADD_FIELD(u64_hex, start);
1281 __NON_SAMPLE_ADD_FIELD(string, filename);
1282)
Wang Nanf5a08ce2016-06-24 11:22:10 +00001283#undef __NON_SAMPLE_ADD_FIELD
1284#undef __FUNC_ADD_NON_SAMPLE_EVENT_CLASS
1285
1286static int setup_non_sample_events(struct ctf_writer *cw,
1287 struct perf_session *session __maybe_unused)
1288{
1289 int ret;
1290
1291 ret = add_comm_event(cw);
1292 if (ret)
1293 return ret;
Wang Nanebccba32016-06-24 11:22:12 +00001294 ret = add_exit_event(cw);
1295 if (ret)
1296 return ret;
1297 ret = add_fork_event(cw);
1298 if (ret)
1299 return ret;
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -04001300 ret = add_mmap_event(cw);
1301 if (ret)
1302 return ret;
1303 ret = add_mmap2_event(cw);
1304 if (ret)
1305 return ret;
Wang Nanf5a08ce2016-06-24 11:22:10 +00001306 return 0;
1307}
1308
Wang Nan5141d732016-02-05 14:01:30 +00001309static void cleanup_events(struct perf_session *session)
1310{
Jiri Olsa63503db2019-07-21 13:23:52 +02001311 struct evlist *evlist = session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001312 struct evsel *evsel;
Wang Nan5141d732016-02-05 14:01:30 +00001313
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001314 evlist__for_each_entry(evlist, evsel) {
Wang Nan5141d732016-02-05 14:01:30 +00001315 struct evsel_priv *priv;
1316
1317 priv = evsel->priv;
1318 bt_ctf_event_class_put(priv->event_class);
1319 zfree(&evsel->priv);
1320 }
1321
1322 perf_evlist__delete(evlist);
1323 session->evlist = NULL;
1324}
1325
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001326static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
1327{
1328 struct ctf_stream **stream;
1329 struct perf_header *ph = &session->header;
1330 int ncpus;
1331
1332 /*
1333 * Try to get the number of cpus used in the data file,
1334 * if not present fallback to the MAX_CPUS.
1335 */
1336 ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS;
1337
1338 stream = zalloc(sizeof(*stream) * ncpus);
1339 if (!stream) {
1340 pr_err("Failed to allocate streams.\n");
1341 return -ENOMEM;
1342 }
1343
1344 cw->stream = stream;
1345 cw->stream_cnt = ncpus;
1346 return 0;
1347}
1348
1349static void free_streams(struct ctf_writer *cw)
1350{
1351 int cpu;
1352
1353 for (cpu = 0; cpu < cw->stream_cnt; cpu++)
1354 ctf_stream__delete(cw->stream[cpu]);
1355
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001356 zfree(&cw->stream);
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001357}
1358
Jiri Olsaedbe9812015-02-20 23:17:00 +01001359static int ctf_writer__setup_env(struct ctf_writer *cw,
1360 struct perf_session *session)
1361{
1362 struct perf_header *header = &session->header;
1363 struct bt_ctf_writer *writer = cw->writer;
1364
1365#define ADD(__n, __v) \
1366do { \
1367 if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \
1368 return -1; \
1369} while (0)
1370
1371 ADD("host", header->env.hostname);
1372 ADD("sysname", "Linux");
1373 ADD("release", header->env.os_release);
1374 ADD("version", header->env.version);
1375 ADD("machine", header->env.arch);
1376 ADD("domain", "kernel");
1377 ADD("tracer_name", "perf");
1378
1379#undef ADD
1380 return 0;
1381}
1382
1383static int ctf_writer__setup_clock(struct ctf_writer *cw)
1384{
1385 struct bt_ctf_clock *clock = cw->clock;
1386
1387 bt_ctf_clock_set_description(clock, "perf clock");
1388
1389#define SET(__n, __v) \
1390do { \
1391 if (bt_ctf_clock_set_##__n(clock, __v)) \
1392 return -1; \
1393} while (0)
1394
1395 SET(frequency, 1000000000);
1396 SET(offset_s, 0);
1397 SET(offset, 0);
1398 SET(precision, 10);
1399 SET(is_absolute, 0);
1400
1401#undef SET
1402 return 0;
1403}
1404
1405static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex)
1406{
1407 struct bt_ctf_field_type *type;
1408
1409 type = bt_ctf_field_type_integer_create(size);
1410 if (!type)
1411 return NULL;
1412
1413 if (sign &&
1414 bt_ctf_field_type_integer_set_signed(type, 1))
1415 goto err;
1416
1417 if (hex &&
1418 bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL))
1419 goto err;
1420
Wang Nanf8dd2d52016-02-26 09:31:57 +00001421#if __BYTE_ORDER == __BIG_ENDIAN
1422 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_BIG_ENDIAN);
1423#else
1424 bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
1425#endif
1426
Jiri Olsaedbe9812015-02-20 23:17:00 +01001427 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
1428 size, sign ? "un" : "", hex ? "hex" : "");
1429 return type;
1430
1431err:
1432 bt_ctf_field_type_put(type);
1433 return NULL;
1434}
1435
1436static void ctf_writer__cleanup_data(struct ctf_writer *cw)
1437{
1438 unsigned int i;
1439
1440 for (i = 0; i < ARRAY_SIZE(cw->data.array); i++)
1441 bt_ctf_field_type_put(cw->data.array[i]);
1442}
1443
1444static int ctf_writer__init_data(struct ctf_writer *cw)
1445{
1446#define CREATE_INT_TYPE(type, size, sign, hex) \
1447do { \
1448 (type) = create_int_type(size, sign, hex); \
1449 if (!(type)) \
1450 goto err; \
1451} while (0)
1452
1453 CREATE_INT_TYPE(cw->data.s64, 64, true, false);
1454 CREATE_INT_TYPE(cw->data.u64, 64, false, false);
1455 CREATE_INT_TYPE(cw->data.s32, 32, true, false);
1456 CREATE_INT_TYPE(cw->data.u32, 32, false, false);
Wang Nan26812d42015-12-08 02:25:39 +00001457 CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001458 CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true);
1459
1460 cw->data.string = bt_ctf_field_type_string_create();
1461 if (cw->data.string)
1462 return 0;
1463
1464err:
1465 ctf_writer__cleanup_data(cw);
1466 pr_err("Failed to create data types.\n");
1467 return -1;
1468}
1469
1470static void ctf_writer__cleanup(struct ctf_writer *cw)
1471{
1472 ctf_writer__cleanup_data(cw);
1473
1474 bt_ctf_clock_put(cw->clock);
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001475 free_streams(cw);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001476 bt_ctf_stream_class_put(cw->stream_class);
1477 bt_ctf_writer_put(cw->writer);
1478
1479 /* and NULL all the pointers */
1480 memset(cw, 0, sizeof(*cw));
1481}
1482
1483static int ctf_writer__init(struct ctf_writer *cw, const char *path)
1484{
1485 struct bt_ctf_writer *writer;
1486 struct bt_ctf_stream_class *stream_class;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001487 struct bt_ctf_clock *clock;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001488 struct bt_ctf_field_type *pkt_ctx_type;
1489 int ret;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001490
1491 /* CTF writer */
1492 writer = bt_ctf_writer_create(path);
1493 if (!writer)
1494 goto err;
1495
1496 cw->writer = writer;
1497
1498 /* CTF clock */
1499 clock = bt_ctf_clock_create("perf_clock");
1500 if (!clock) {
1501 pr("Failed to create CTF clock.\n");
1502 goto err_cleanup;
1503 }
1504
1505 cw->clock = clock;
1506
1507 if (ctf_writer__setup_clock(cw)) {
1508 pr("Failed to setup CTF clock.\n");
1509 goto err_cleanup;
1510 }
1511
1512 /* CTF stream class */
1513 stream_class = bt_ctf_stream_class_create("perf_stream");
1514 if (!stream_class) {
1515 pr("Failed to create CTF stream class.\n");
1516 goto err_cleanup;
1517 }
1518
1519 cw->stream_class = stream_class;
1520
1521 /* CTF clock stream setup */
1522 if (bt_ctf_stream_class_set_clock(stream_class, clock)) {
1523 pr("Failed to assign CTF clock to stream class.\n");
1524 goto err_cleanup;
1525 }
1526
1527 if (ctf_writer__init_data(cw))
1528 goto err_cleanup;
1529
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001530 /* Add cpu_id for packet context */
1531 pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1532 if (!pkt_ctx_type)
Jiri Olsaedbe9812015-02-20 23:17:00 +01001533 goto err_cleanup;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001534
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001535 ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id");
1536 bt_ctf_field_type_put(pkt_ctx_type);
1537 if (ret)
1538 goto err_cleanup;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001539
1540 /* CTF clock writer setup */
1541 if (bt_ctf_writer_add_clock(writer, clock)) {
1542 pr("Failed to assign CTF clock to writer.\n");
1543 goto err_cleanup;
1544 }
1545
1546 return 0;
1547
1548err_cleanup:
1549 ctf_writer__cleanup(cw);
1550err:
1551 pr_err("Failed to setup CTF writer.\n");
1552 return -1;
1553}
1554
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001555static int ctf_writer__flush_streams(struct ctf_writer *cw)
1556{
1557 int cpu, ret = 0;
1558
1559 for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++)
1560 ret = ctf_stream__flush(cw->stream[cpu]);
1561
1562 return ret;
1563}
1564
Jiri Olsa8fa46752015-04-18 17:50:17 +02001565static int convert__config(const char *var, const char *value, void *cb)
1566{
1567 struct convert *c = cb;
1568
Arnaldo Carvalho de Melo25ce4bb2017-06-27 11:44:58 -03001569 if (!strcmp(var, "convert.queue-size"))
1570 return perf_config_u64(&c->queue_size, var, value);
Jiri Olsa8fa46752015-04-18 17:50:17 +02001571
Wang Nanb8cbb342016-02-26 09:31:51 +00001572 return 0;
Jiri Olsa8fa46752015-04-18 17:50:17 +02001573}
1574
Wang Nan3275f682016-06-24 11:22:07 +00001575int bt_convert__perf2ctf(const char *input, const char *path,
1576 struct perf_data_convert_opts *opts)
Jiri Olsaedbe9812015-02-20 23:17:00 +01001577{
1578 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001579 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001580 .path = input,
Jiri Olsaeae8ad82017-01-23 22:25:41 +01001581 .mode = PERF_DATA_MODE_READ,
1582 .force = opts->force,
Jiri Olsaedbe9812015-02-20 23:17:00 +01001583 };
1584 struct convert c = {
1585 .tool = {
1586 .sample = process_sample_event,
1587 .mmap = perf_event__process_mmap,
1588 .mmap2 = perf_event__process_mmap2,
1589 .comm = perf_event__process_comm,
1590 .exit = perf_event__process_exit,
1591 .fork = perf_event__process_fork,
1592 .lost = perf_event__process_lost,
1593 .tracing_data = perf_event__process_tracing_data,
1594 .build_id = perf_event__process_build_id,
Hari Bathinif3b36142017-03-08 02:11:43 +05301595 .namespaces = perf_event__process_namespaces,
Jiri Olsaedbe9812015-02-20 23:17:00 +01001596 .ordered_events = true,
1597 .ordering_requires_timestamps = true,
1598 },
1599 };
1600 struct ctf_writer *cw = &c.writer;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001601 int err;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001602
Wang Nanebccba32016-06-24 11:22:12 +00001603 if (opts->all) {
Wang Nanf5a08ce2016-06-24 11:22:10 +00001604 c.tool.comm = process_comm_event;
Wang Nanebccba32016-06-24 11:22:12 +00001605 c.tool.exit = process_exit_event;
1606 c.tool.fork = process_fork_event;
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -04001607 c.tool.mmap = process_mmap_event;
1608 c.tool.mmap2 = process_mmap2_event;
Wang Nanebccba32016-06-24 11:22:12 +00001609 }
Wang Nanf5a08ce2016-06-24 11:22:10 +00001610
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001611 err = perf_config(convert__config, &c);
1612 if (err)
1613 return err;
Jiri Olsa8fa46752015-04-18 17:50:17 +02001614
Jiri Olsaedbe9812015-02-20 23:17:00 +01001615 /* CTF writer */
1616 if (ctf_writer__init(cw, path))
1617 return -1;
1618
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001619 err = -1;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001620 /* perf.data session */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001621 session = perf_session__new(&data, 0, &c.tool);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001622 if (!session)
1623 goto free_writer;
1624
Jiri Olsa8fa46752015-04-18 17:50:17 +02001625 if (c.queue_size) {
1626 ordered_events__set_alloc_size(&session->ordered_events,
1627 c.queue_size);
1628 }
1629
Jiri Olsaedbe9812015-02-20 23:17:00 +01001630 /* CTF writer env/clock setup */
1631 if (ctf_writer__setup_env(cw, session))
1632 goto free_session;
1633
1634 /* CTF events setup */
1635 if (setup_events(cw, session))
1636 goto free_session;
1637
Wang Nanf5a08ce2016-06-24 11:22:10 +00001638 if (opts->all && setup_non_sample_events(cw, session))
1639 goto free_session;
1640
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001641 if (setup_streams(cw, session))
1642 goto free_session;
1643
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001644 err = perf_session__process_events(session);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001645 if (!err)
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001646 err = ctf_writer__flush_streams(cw);
He Kuangc2141052015-04-18 17:50:14 +02001647 else
1648 pr_err("Error during conversion.\n");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001649
1650 fprintf(stderr,
1651 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001652 data.path, path);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001653
1654 fprintf(stderr,
Wang Nan8ee4c462016-06-24 11:22:09 +00001655 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
Jiri Olsaedbe9812015-02-20 23:17:00 +01001656 (double) c.events_size / 1024.0 / 1024.0,
1657 c.events_count);
1658
Wang Nan8ee4c462016-06-24 11:22:09 +00001659 if (!c.non_sample_count)
1660 fprintf(stderr, ") ]\n");
1661 else
1662 fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
1663
Wang Nan5141d732016-02-05 14:01:30 +00001664 cleanup_events(session);
He Kuangc2141052015-04-18 17:50:14 +02001665 perf_session__delete(session);
1666 ctf_writer__cleanup(cw);
1667
1668 return err;
1669
Jiri Olsaedbe9812015-02-20 23:17:00 +01001670free_session:
1671 perf_session__delete(session);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001672free_writer:
1673 ctf_writer__cleanup(cw);
He Kuangc2141052015-04-18 17:50:14 +02001674 pr_err("Error during conversion setup.\n");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001675 return err;
1676}