blob: 9e0aee276df8e1ddc35e7ba1a42815ca971823e8 [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"
Nicholas Fraserd0713d42021-04-26 10:47:16 -040024#include "data-convert.h"
Jiri Olsaedbe9812015-02-20 23:17:00 +010025#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>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053033#include <linux/err.h>
Jiri Olsa88371c52020-08-05 11:34:42 +020034#include <linux/time64.h>
35#include "util.h"
36#include "clockid.h"
Jiri Olsaedbe9812015-02-20 23:17:00 +010037
38#define pr_N(n, fmt, ...) \
39 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
40
41#define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
42#define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
43
44#define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
45
46struct evsel_priv {
47 struct bt_ctf_event_class *event_class;
48};
49
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +020050#define MAX_CPUS 4096
51
52struct ctf_stream {
53 struct bt_ctf_stream *stream;
54 int cpu;
Jiri Olsa89e5fa82015-04-18 17:50:16 +020055 u32 count;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +020056};
57
Jiri Olsaedbe9812015-02-20 23:17:00 +010058struct ctf_writer {
59 /* writer primitives */
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +020060 struct bt_ctf_writer *writer;
61 struct ctf_stream **stream;
62 int stream_cnt;
63 struct bt_ctf_stream_class *stream_class;
64 struct bt_ctf_clock *clock;
Jiri Olsaedbe9812015-02-20 23:17:00 +010065
66 /* data types */
67 union {
68 struct {
69 struct bt_ctf_field_type *s64;
70 struct bt_ctf_field_type *u64;
71 struct bt_ctf_field_type *s32;
72 struct bt_ctf_field_type *u32;
73 struct bt_ctf_field_type *string;
Wang Nan26812d42015-12-08 02:25:39 +000074 struct bt_ctf_field_type *u32_hex;
Jiri Olsaedbe9812015-02-20 23:17:00 +010075 struct bt_ctf_field_type *u64_hex;
76 };
77 struct bt_ctf_field_type *array[6];
78 } data;
Wang Nanf5a08ce2016-06-24 11:22:10 +000079 struct bt_ctf_event_class *comm_class;
Wang Nanebccba32016-06-24 11:22:12 +000080 struct bt_ctf_event_class *exit_class;
81 struct bt_ctf_event_class *fork_class;
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -040082 struct bt_ctf_event_class *mmap_class;
83 struct bt_ctf_event_class *mmap2_class;
Jiri Olsaedbe9812015-02-20 23:17:00 +010084};
85
86struct convert {
87 struct perf_tool tool;
88 struct ctf_writer writer;
89
90 u64 events_size;
91 u64 events_count;
Wang Nan8ee4c462016-06-24 11:22:09 +000092 u64 non_sample_count;
Jiri Olsa8fa46752015-04-18 17:50:17 +020093
94 /* Ordered events configured queue size. */
95 u64 queue_size;
Jiri Olsaedbe9812015-02-20 23:17:00 +010096};
97
98static int value_set(struct bt_ctf_field_type *type,
99 struct bt_ctf_event *event,
100 const char *name, u64 val)
101{
102 struct bt_ctf_field *field;
103 bool sign = bt_ctf_field_type_integer_get_signed(type);
104 int ret;
105
106 field = bt_ctf_field_create(type);
107 if (!field) {
108 pr_err("failed to create a field %s\n", name);
109 return -1;
110 }
111
112 if (sign) {
113 ret = bt_ctf_field_signed_integer_set_value(field, val);
114 if (ret) {
115 pr_err("failed to set field value %s\n", name);
116 goto err;
117 }
118 } else {
119 ret = bt_ctf_field_unsigned_integer_set_value(field, val);
120 if (ret) {
121 pr_err("failed to set field value %s\n", name);
122 goto err;
123 }
124 }
125
126 ret = bt_ctf_event_set_payload(event, name, field);
127 if (ret) {
128 pr_err("failed to set payload %s\n", name);
129 goto err;
130 }
131
132 pr2(" SET [%s = %" PRIu64 "]\n", name, val);
133
134err:
135 bt_ctf_field_put(field);
136 return ret;
137}
138
139#define __FUNC_VALUE_SET(_name, _val_type) \
140static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
141 struct bt_ctf_event *event, \
142 const char *name, \
143 _val_type val) \
144{ \
145 struct bt_ctf_field_type *type = cw->data._name; \
146 return value_set(type, event, name, (u64) val); \
147}
148
149#define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
150
151FUNC_VALUE_SET(s32)
152FUNC_VALUE_SET(u32)
153FUNC_VALUE_SET(s64)
154FUNC_VALUE_SET(u64)
155__FUNC_VALUE_SET(u64_hex, u64)
156
Wang Nan069ee5c2016-06-24 11:22:06 +0000157static int string_set_value(struct bt_ctf_field *field, const char *string);
158static __maybe_unused int
159value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event,
160 const char *name, const char *string)
161{
162 struct bt_ctf_field_type *type = cw->data.string;
163 struct bt_ctf_field *field;
164 int ret = 0;
165
166 field = bt_ctf_field_create(type);
167 if (!field) {
168 pr_err("failed to create a field %s\n", name);
169 return -1;
170 }
171
172 ret = string_set_value(field, string);
173 if (ret) {
174 pr_err("failed to set value %s\n", name);
175 goto err_put_field;
176 }
177
178 ret = bt_ctf_event_set_payload(event, name, field);
179 if (ret)
180 pr_err("failed to set payload %s\n", name);
181
182err_put_field:
183 bt_ctf_field_put(field);
184 return ret;
185}
186
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100187static struct bt_ctf_field_type*
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400188get_tracepoint_field_type(struct ctf_writer *cw, struct tep_format_field *field)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100189{
190 unsigned long flags = field->flags;
191
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400192 if (flags & TEP_FIELD_IS_STRING)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100193 return cw->data.string;
194
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400195 if (!(flags & TEP_FIELD_IS_SIGNED)) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100196 /* unsigned long are mostly pointers */
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400197 if (flags & TEP_FIELD_IS_LONG || flags & TEP_FIELD_IS_POINTER)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100198 return cw->data.u64_hex;
199 }
200
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400201 if (flags & TEP_FIELD_IS_SIGNED) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100202 if (field->size == 8)
203 return cw->data.s64;
204 else
205 return cw->data.s32;
206 }
207
208 if (field->size == 8)
209 return cw->data.u64;
210 else
211 return cw->data.u32;
212}
213
Wang Nand4ae4212015-04-18 17:50:20 +0200214static unsigned long long adjust_signedness(unsigned long long value_int, int size)
215{
216 unsigned long long value_mask;
217
218 /*
219 * value_mask = (1 << (size * 8 - 1)) - 1.
220 * Directly set value_mask for code readers.
221 */
222 switch (size) {
223 case 1:
224 value_mask = 0x7fULL;
225 break;
226 case 2:
227 value_mask = 0x7fffULL;
228 break;
229 case 4:
230 value_mask = 0x7fffffffULL;
231 break;
232 case 8:
233 /*
234 * For 64 bit value, return it self. There is no need
235 * to fill high bit.
236 */
237 /* Fall through */
238 default:
239 /* BUG! */
240 return value_int;
241 }
242
243 /* If it is a positive value, don't adjust. */
244 if ((value_int & (~0ULL - value_mask)) == 0)
245 return value_int;
246
247 /* Fill upper part of value_int with 1 to make it a negative long long. */
248 return (value_int & value_mask) | ~value_mask;
249}
250
Wang Nan5ea58882016-05-27 11:35:51 +0000251static int string_set_value(struct bt_ctf_field *field, const char *string)
252{
253 char *buffer = NULL;
254 size_t len = strlen(string), i, p;
255 int err;
256
257 for (i = p = 0; i < len; i++, p++) {
258 if (isprint(string[i])) {
259 if (!buffer)
260 continue;
261 buffer[p] = string[i];
262 } else {
263 char numstr[5];
264
265 snprintf(numstr, sizeof(numstr), "\\x%02x",
266 (unsigned int)(string[i]) & 0xff);
267
268 if (!buffer) {
269 buffer = zalloc(i + (len - i) * 4 + 2);
270 if (!buffer) {
271 pr_err("failed to set unprintable string '%s'\n", string);
272 return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING");
273 }
274 if (i > 0)
275 strncpy(buffer, string, i);
276 }
Shawn Landden97acec72019-05-18 15:32:38 -0300277 memcpy(buffer + p, numstr, 4);
Wang Nan5ea58882016-05-27 11:35:51 +0000278 p += 3;
279 }
280 }
281
282 if (!buffer)
283 return bt_ctf_field_string_set_value(field, string);
284 err = bt_ctf_field_string_set_value(field, buffer);
285 free(buffer);
286 return err;
287}
288
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100289static int add_tracepoint_field_value(struct ctf_writer *cw,
290 struct bt_ctf_event_class *event_class,
291 struct bt_ctf_event *event,
292 struct perf_sample *sample,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400293 struct tep_format_field *fmtf)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100294{
295 struct bt_ctf_field_type *type;
296 struct bt_ctf_field *array_field;
297 struct bt_ctf_field *field;
298 const char *name = fmtf->name;
299 void *data = sample->raw_data;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100300 unsigned long flags = fmtf->flags;
301 unsigned int n_items;
302 unsigned int i;
303 unsigned int offset;
304 unsigned int len;
305 int ret;
306
Wang Nane0a7cce2015-04-18 17:50:19 +0200307 name = fmtf->alias;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100308 offset = fmtf->offset;
309 len = fmtf->size;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400310 if (flags & TEP_FIELD_IS_STRING)
311 flags &= ~TEP_FIELD_IS_ARRAY;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100312
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400313 if (flags & TEP_FIELD_IS_DYNAMIC) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100314 unsigned long long tmp_val;
315
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -0400316 tmp_val = tep_read_number(fmtf->event->tep,
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400317 data + offset, len);
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100318 offset = tmp_val;
319 len = offset >> 16;
320 offset &= 0xffff;
Masami Hiramatsu7c689c82021-11-22 18:30:48 +0900321 if (flags & TEP_FIELD_IS_RELATIVE)
322 offset += fmtf->offset + fmtf->size;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100323 }
324
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400325 if (flags & TEP_FIELD_IS_ARRAY) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100326
327 type = bt_ctf_event_class_get_field_by_name(
328 event_class, name);
329 array_field = bt_ctf_field_create(type);
330 bt_ctf_field_type_put(type);
331 if (!array_field) {
332 pr_err("Failed to create array type %s\n", name);
333 return -1;
334 }
335
336 len = fmtf->size / fmtf->arraylen;
337 n_items = fmtf->arraylen;
338 } else {
339 n_items = 1;
340 array_field = NULL;
341 }
342
343 type = get_tracepoint_field_type(cw, fmtf);
344
345 for (i = 0; i < n_items; i++) {
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400346 if (flags & TEP_FIELD_IS_ARRAY)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100347 field = bt_ctf_field_array_get_field(array_field, i);
348 else
349 field = bt_ctf_field_create(type);
350
351 if (!field) {
352 pr_err("failed to create a field %s\n", name);
353 return -1;
354 }
355
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400356 if (flags & TEP_FIELD_IS_STRING)
Wang Nan5ea58882016-05-27 11:35:51 +0000357 ret = string_set_value(field, data + offset + i * len);
Wang Nand4ae4212015-04-18 17:50:20 +0200358 else {
359 unsigned long long value_int;
360
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400361 value_int = tep_read_number(
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -0400362 fmtf->event->tep,
Wang Nand4ae4212015-04-18 17:50:20 +0200363 data + offset + i * len, len);
364
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400365 if (!(flags & TEP_FIELD_IS_SIGNED))
Wang Nand4ae4212015-04-18 17:50:20 +0200366 ret = bt_ctf_field_unsigned_integer_set_value(
367 field, value_int);
368 else
369 ret = bt_ctf_field_signed_integer_set_value(
370 field, adjust_signedness(value_int, len));
371 }
372
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100373 if (ret) {
374 pr_err("failed to set file value %s\n", name);
375 goto err_put_field;
376 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400377 if (!(flags & TEP_FIELD_IS_ARRAY)) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100378 ret = bt_ctf_event_set_payload(event, name, field);
379 if (ret) {
380 pr_err("failed to set payload %s\n", name);
381 goto err_put_field;
382 }
383 }
384 bt_ctf_field_put(field);
385 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400386 if (flags & TEP_FIELD_IS_ARRAY) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100387 ret = bt_ctf_event_set_payload(event, name, array_field);
388 if (ret) {
389 pr_err("Failed add payload array %s\n", name);
390 return -1;
391 }
392 bt_ctf_field_put(array_field);
393 }
394 return 0;
395
396err_put_field:
397 bt_ctf_field_put(field);
398 return -1;
399}
400
401static int add_tracepoint_fields_values(struct ctf_writer *cw,
402 struct bt_ctf_event_class *event_class,
403 struct bt_ctf_event *event,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400404 struct tep_format_field *fields,
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100405 struct perf_sample *sample)
406{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400407 struct tep_format_field *field;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100408 int ret;
409
410 for (field = fields; field; field = field->next) {
411 ret = add_tracepoint_field_value(cw, event_class, event, sample,
412 field);
413 if (ret)
414 return -1;
415 }
416 return 0;
417}
418
419static int add_tracepoint_values(struct ctf_writer *cw,
420 struct bt_ctf_event_class *event_class,
421 struct bt_ctf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200422 struct evsel *evsel,
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100423 struct perf_sample *sample)
424{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400425 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
426 struct tep_format_field *fields = evsel->tp_format->format.fields;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100427 int ret;
428
429 ret = add_tracepoint_fields_values(cw, event_class, event,
430 common_fields, sample);
431 if (!ret)
432 ret = add_tracepoint_fields_values(cw, event_class, event,
433 fields, sample);
434
435 return ret;
436}
437
Wang Nan6122d572016-02-26 09:31:56 +0000438static int
439add_bpf_output_values(struct bt_ctf_event_class *event_class,
440 struct bt_ctf_event *event,
441 struct perf_sample *sample)
442{
443 struct bt_ctf_field_type *len_type, *seq_type;
444 struct bt_ctf_field *len_field, *seq_field;
445 unsigned int raw_size = sample->raw_size;
446 unsigned int nr_elements = raw_size / sizeof(u32);
447 unsigned int i;
448 int ret;
449
450 if (nr_elements * sizeof(u32) != raw_size)
Wang Nanf2c88522016-09-28 03:58:46 +0000451 pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n",
Wang Nan6122d572016-02-26 09:31:56 +0000452 raw_size, nr_elements * sizeof(u32) - raw_size);
453
454 len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len");
455 len_field = bt_ctf_field_create(len_type);
456 if (!len_field) {
457 pr_err("failed to create 'raw_len' for bpf output event\n");
458 ret = -1;
459 goto put_len_type;
460 }
461
462 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
463 if (ret) {
464 pr_err("failed to set field value for raw_len\n");
465 goto put_len_field;
466 }
467 ret = bt_ctf_event_set_payload(event, "raw_len", len_field);
468 if (ret) {
469 pr_err("failed to set payload to raw_len\n");
470 goto put_len_field;
471 }
472
473 seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data");
474 seq_field = bt_ctf_field_create(seq_type);
475 if (!seq_field) {
476 pr_err("failed to create 'raw_data' for bpf output event\n");
477 ret = -1;
478 goto put_seq_type;
479 }
480
481 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
482 if (ret) {
483 pr_err("failed to set length of 'raw_data'\n");
484 goto put_seq_field;
485 }
486
487 for (i = 0; i < nr_elements; i++) {
488 struct bt_ctf_field *elem_field =
489 bt_ctf_field_sequence_get_field(seq_field, i);
490
491 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
492 ((u32 *)(sample->raw_data))[i]);
493
494 bt_ctf_field_put(elem_field);
495 if (ret) {
496 pr_err("failed to set raw_data[%d]\n", i);
497 goto put_seq_field;
498 }
499 }
500
501 ret = bt_ctf_event_set_payload(event, "raw_data", seq_field);
502 if (ret)
503 pr_err("failed to set payload for raw_data\n");
504
505put_seq_field:
506 bt_ctf_field_put(seq_field);
507put_seq_type:
508 bt_ctf_field_type_put(seq_type);
509put_len_field:
510 bt_ctf_field_put(len_field);
511put_len_type:
512 bt_ctf_field_type_put(len_type);
513 return ret;
514}
515
Geneviève Bastiena3073c82017-07-27 14:12:03 -0400516static int
517add_callchain_output_values(struct bt_ctf_event_class *event_class,
518 struct bt_ctf_event *event,
519 struct ip_callchain *callchain)
520{
521 struct bt_ctf_field_type *len_type, *seq_type;
522 struct bt_ctf_field *len_field, *seq_field;
523 unsigned int nr_elements = callchain->nr;
524 unsigned int i;
525 int ret;
526
527 len_type = bt_ctf_event_class_get_field_by_name(
528 event_class, "perf_callchain_size");
529 len_field = bt_ctf_field_create(len_type);
530 if (!len_field) {
531 pr_err("failed to create 'perf_callchain_size' for callchain output event\n");
532 ret = -1;
533 goto put_len_type;
534 }
535
536 ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
537 if (ret) {
538 pr_err("failed to set field value for perf_callchain_size\n");
539 goto put_len_field;
540 }
541 ret = bt_ctf_event_set_payload(event, "perf_callchain_size", len_field);
542 if (ret) {
543 pr_err("failed to set payload to perf_callchain_size\n");
544 goto put_len_field;
545 }
546
547 seq_type = bt_ctf_event_class_get_field_by_name(
548 event_class, "perf_callchain");
549 seq_field = bt_ctf_field_create(seq_type);
550 if (!seq_field) {
551 pr_err("failed to create 'perf_callchain' for callchain output event\n");
552 ret = -1;
553 goto put_seq_type;
554 }
555
556 ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
557 if (ret) {
558 pr_err("failed to set length of 'perf_callchain'\n");
559 goto put_seq_field;
560 }
561
562 for (i = 0; i < nr_elements; i++) {
563 struct bt_ctf_field *elem_field =
564 bt_ctf_field_sequence_get_field(seq_field, i);
565
566 ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
567 ((u64 *)(callchain->ips))[i]);
568
569 bt_ctf_field_put(elem_field);
570 if (ret) {
571 pr_err("failed to set callchain[%d]\n", i);
572 goto put_seq_field;
573 }
574 }
575
576 ret = bt_ctf_event_set_payload(event, "perf_callchain", seq_field);
577 if (ret)
578 pr_err("failed to set payload for raw_data\n");
579
580put_seq_field:
581 bt_ctf_field_put(seq_field);
582put_seq_type:
583 bt_ctf_field_type_put(seq_type);
584put_len_field:
585 bt_ctf_field_put(len_field);
586put_len_type:
587 bt_ctf_field_type_put(len_type);
588 return ret;
589}
590
Jiri Olsaedbe9812015-02-20 23:17:00 +0100591static int add_generic_values(struct ctf_writer *cw,
592 struct bt_ctf_event *event,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200593 struct evsel *evsel,
Jiri Olsaedbe9812015-02-20 23:17:00 +0100594 struct perf_sample *sample)
595{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200596 u64 type = evsel->core.attr.sample_type;
Jiri Olsaedbe9812015-02-20 23:17:00 +0100597 int ret;
598
599 /*
600 * missing:
601 * PERF_SAMPLE_TIME - not needed as we have it in
602 * ctf event header
603 * PERF_SAMPLE_READ - TODO
Jiri Olsaedbe9812015-02-20 23:17:00 +0100604 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
605 * PERF_SAMPLE_BRANCH_STACK - TODO
606 * PERF_SAMPLE_REGS_USER - TODO
607 * PERF_SAMPLE_STACK_USER - TODO
608 */
609
610 if (type & PERF_SAMPLE_IP) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100611 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100612 if (ret)
613 return -1;
614 }
615
616 if (type & PERF_SAMPLE_TID) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100617 ret = value_set_s32(cw, event, "perf_tid", sample->tid);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100618 if (ret)
619 return -1;
620
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100621 ret = value_set_s32(cw, event, "perf_pid", sample->pid);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100622 if (ret)
623 return -1;
624 }
625
626 if ((type & PERF_SAMPLE_ID) ||
627 (type & PERF_SAMPLE_IDENTIFIER)) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100628 ret = value_set_u64(cw, event, "perf_id", sample->id);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100629 if (ret)
630 return -1;
631 }
632
633 if (type & PERF_SAMPLE_STREAM_ID) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100634 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100635 if (ret)
636 return -1;
637 }
638
Jiri Olsaedbe9812015-02-20 23:17:00 +0100639 if (type & PERF_SAMPLE_PERIOD) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100640 ret = value_set_u64(cw, event, "perf_period", sample->period);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100641 if (ret)
642 return -1;
643 }
644
645 if (type & PERF_SAMPLE_WEIGHT) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100646 ret = value_set_u64(cw, event, "perf_weight", sample->weight);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100647 if (ret)
648 return -1;
649 }
650
651 if (type & PERF_SAMPLE_DATA_SRC) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100652 ret = value_set_u64(cw, event, "perf_data_src",
653 sample->data_src);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100654 if (ret)
655 return -1;
656 }
657
658 if (type & PERF_SAMPLE_TRANSACTION) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +0100659 ret = value_set_u64(cw, event, "perf_transaction",
660 sample->transaction);
Jiri Olsaedbe9812015-02-20 23:17:00 +0100661 if (ret)
662 return -1;
663 }
664
665 return 0;
666}
667
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200668static int ctf_stream__flush(struct ctf_stream *cs)
669{
670 int err = 0;
671
672 if (cs) {
673 err = bt_ctf_stream_flush(cs->stream);
674 if (err)
675 pr_err("CTF stream %d flush failed\n", cs->cpu);
676
Jiri Olsa89e5fa82015-04-18 17:50:16 +0200677 pr("Flush stream for cpu %d (%u samples)\n",
678 cs->cpu, cs->count);
679
680 cs->count = 0;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200681 }
682
683 return err;
684}
685
686static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
687{
688 struct ctf_stream *cs;
689 struct bt_ctf_field *pkt_ctx = NULL;
690 struct bt_ctf_field *cpu_field = NULL;
691 struct bt_ctf_stream *stream = NULL;
692 int ret;
693
694 cs = zalloc(sizeof(*cs));
695 if (!cs) {
696 pr_err("Failed to allocate ctf stream\n");
697 return NULL;
698 }
699
700 stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
701 if (!stream) {
702 pr_err("Failed to create CTF stream\n");
703 goto out;
704 }
705
706 pkt_ctx = bt_ctf_stream_get_packet_context(stream);
707 if (!pkt_ctx) {
708 pr_err("Failed to obtain packet context\n");
709 goto out;
710 }
711
712 cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
713 bt_ctf_field_put(pkt_ctx);
714 if (!cpu_field) {
715 pr_err("Failed to obtain cpu field\n");
716 goto out;
717 }
718
719 ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
720 if (ret) {
721 pr_err("Failed to update CPU number\n");
722 goto out;
723 }
724
725 bt_ctf_field_put(cpu_field);
726
727 cs->cpu = cpu;
728 cs->stream = stream;
729 return cs;
730
731out:
732 if (cpu_field)
733 bt_ctf_field_put(cpu_field);
734 if (stream)
735 bt_ctf_stream_put(stream);
736
737 free(cs);
738 return NULL;
739}
740
741static void ctf_stream__delete(struct ctf_stream *cs)
742{
743 if (cs) {
744 bt_ctf_stream_put(cs->stream);
745 free(cs);
746 }
747}
748
749static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
750{
751 struct ctf_stream *cs = cw->stream[cpu];
752
753 if (!cs) {
754 cs = ctf_stream__create(cw, cpu);
755 cw->stream[cpu] = cs;
756 }
757
758 return cs;
759}
760
761static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200762 struct evsel *evsel)
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200763{
764 int cpu = 0;
765
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200766 if (evsel->core.attr.sample_type & PERF_SAMPLE_CPU)
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200767 cpu = sample->cpu;
768
769 if (cpu > cw->stream_cnt) {
770 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
771 cpu, cw->stream_cnt);
772 cpu = 0;
773 }
774
775 return cpu;
776}
777
Jiri Olsa89e5fa82015-04-18 17:50:16 +0200778#define STREAM_FLUSH_COUNT 100000
779
780/*
781 * Currently we have no other way to determine the
782 * time for the stream flush other than keep track
783 * of the number of events and check it against
784 * threshold.
785 */
786static bool is_flush_needed(struct ctf_stream *cs)
787{
788 return cs->count >= STREAM_FLUSH_COUNT;
789}
790
Jiri Olsaedbe9812015-02-20 23:17:00 +0100791static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300792 union perf_event *_event,
Jiri Olsaedbe9812015-02-20 23:17:00 +0100793 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200794 struct evsel *evsel,
Jiri Olsaedbe9812015-02-20 23:17:00 +0100795 struct machine *machine __maybe_unused)
796{
797 struct convert *c = container_of(tool, struct convert, tool);
798 struct evsel_priv *priv = evsel->priv;
799 struct ctf_writer *cw = &c->writer;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200800 struct ctf_stream *cs;
Jiri Olsaedbe9812015-02-20 23:17:00 +0100801 struct bt_ctf_event_class *event_class;
802 struct bt_ctf_event *event;
803 int ret;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200804 unsigned long type = evsel->core.attr.sample_type;
Jiri Olsaedbe9812015-02-20 23:17:00 +0100805
806 if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
807 return 0;
808
809 event_class = priv->event_class;
810
811 /* update stats */
812 c->events_count++;
813 c->events_size += _event->header.size;
814
815 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
816
817 event = bt_ctf_event_create(event_class);
818 if (!event) {
819 pr_err("Failed to create an CTF event\n");
820 return -1;
821 }
822
823 bt_ctf_clock_set_time(cw->clock, sample->time);
824
825 ret = add_generic_values(cw, event, evsel, sample);
826 if (ret)
827 return -1;
828
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200829 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +0100830 ret = add_tracepoint_values(cw, event_class, event,
831 evsel, sample);
832 if (ret)
833 return -1;
834 }
835
Geneviève Bastiena3073c82017-07-27 14:12:03 -0400836 if (type & PERF_SAMPLE_CALLCHAIN) {
837 ret = add_callchain_output_values(event_class,
838 event, sample->callchain);
839 if (ret)
840 return -1;
841 }
842
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300843 if (evsel__is_bpf_output(evsel)) {
Wang Nan6122d572016-02-26 09:31:56 +0000844 ret = add_bpf_output_values(event_class, event, sample);
845 if (ret)
846 return -1;
847 }
848
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200849 cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel));
Jiri Olsa89e5fa82015-04-18 17:50:16 +0200850 if (cs) {
851 if (is_flush_needed(cs))
852 ctf_stream__flush(cs);
853
854 cs->count++;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200855 bt_ctf_stream_append_event(cs->stream, event);
Jiri Olsa89e5fa82015-04-18 17:50:16 +0200856 }
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200857
Jiri Olsaedbe9812015-02-20 23:17:00 +0100858 bt_ctf_event_put(event);
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +0200859 return cs ? 0 : -1;
Jiri Olsaedbe9812015-02-20 23:17:00 +0100860}
861
Wang Nanf5a08ce2016-06-24 11:22:10 +0000862#define __NON_SAMPLE_SET_FIELD(_name, _type, _field) \
863do { \
864 ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
865 if (ret) \
866 return -1; \
867} while(0)
868
869#define __FUNC_PROCESS_NON_SAMPLE(_name, body) \
870static int process_##_name##_event(struct perf_tool *tool, \
871 union perf_event *_event, \
872 struct perf_sample *sample, \
873 struct machine *machine) \
874{ \
875 struct convert *c = container_of(tool, struct convert, tool);\
876 struct ctf_writer *cw = &c->writer; \
877 struct bt_ctf_event_class *event_class = cw->_name##_class;\
878 struct bt_ctf_event *event; \
879 struct ctf_stream *cs; \
880 int ret; \
881 \
882 c->non_sample_count++; \
883 c->events_size += _event->header.size; \
884 event = bt_ctf_event_create(event_class); \
885 if (!event) { \
886 pr_err("Failed to create an CTF event\n"); \
887 return -1; \
888 } \
889 \
890 bt_ctf_clock_set_time(cw->clock, sample->time); \
891 body \
892 cs = ctf_stream(cw, 0); \
893 if (cs) { \
894 if (is_flush_needed(cs)) \
895 ctf_stream__flush(cs); \
896 \
897 cs->count++; \
898 bt_ctf_stream_append_event(cs->stream, event); \
899 } \
900 bt_ctf_event_put(event); \
901 \
902 return perf_event__process_##_name(tool, _event, sample, machine);\
903}
904
905__FUNC_PROCESS_NON_SAMPLE(comm,
906 __NON_SAMPLE_SET_FIELD(comm, u32, pid);
907 __NON_SAMPLE_SET_FIELD(comm, u32, tid);
908 __NON_SAMPLE_SET_FIELD(comm, string, comm);
909)
Wang Nanebccba32016-06-24 11:22:12 +0000910__FUNC_PROCESS_NON_SAMPLE(fork,
911 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
912 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
913 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
914 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
915 __NON_SAMPLE_SET_FIELD(fork, u64, time);
916)
917
918__FUNC_PROCESS_NON_SAMPLE(exit,
919 __NON_SAMPLE_SET_FIELD(fork, u32, pid);
920 __NON_SAMPLE_SET_FIELD(fork, u32, ppid);
921 __NON_SAMPLE_SET_FIELD(fork, u32, tid);
922 __NON_SAMPLE_SET_FIELD(fork, u32, ptid);
923 __NON_SAMPLE_SET_FIELD(fork, u64, time);
924)
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -0400925__FUNC_PROCESS_NON_SAMPLE(mmap,
926 __NON_SAMPLE_SET_FIELD(mmap, u32, pid);
927 __NON_SAMPLE_SET_FIELD(mmap, u32, tid);
928 __NON_SAMPLE_SET_FIELD(mmap, u64_hex, start);
929 __NON_SAMPLE_SET_FIELD(mmap, string, filename);
930)
931__FUNC_PROCESS_NON_SAMPLE(mmap2,
932 __NON_SAMPLE_SET_FIELD(mmap2, u32, pid);
933 __NON_SAMPLE_SET_FIELD(mmap2, u32, tid);
934 __NON_SAMPLE_SET_FIELD(mmap2, u64_hex, start);
935 __NON_SAMPLE_SET_FIELD(mmap2, string, filename);
936)
Wang Nanf5a08ce2016-06-24 11:22:10 +0000937#undef __NON_SAMPLE_SET_FIELD
938#undef __FUNC_PROCESS_NON_SAMPLE
939
Wang Nane0a7cce2015-04-18 17:50:19 +0200940/* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
941static char *change_name(char *name, char *orig_name, int dup)
942{
943 char *new_name = NULL;
944 size_t len;
945
946 if (!name)
947 name = orig_name;
948
949 if (dup >= 10)
950 goto out;
951 /*
952 * Add '_' prefix to potential keywork. According to
Kees Cook6edfd0e2021-02-10 15:42:19 -0800953 * Mathieu Desnoyers (https://lore.kernel.org/lkml/1074266107.40857.1422045946295.JavaMail.zimbra@efficios.com),
Ingo Molnar4d39c892021-03-23 17:09:15 +0100954 * further CTF spec updating may require us to use '$'.
Wang Nane0a7cce2015-04-18 17:50:19 +0200955 */
956 if (dup < 0)
957 len = strlen(name) + sizeof("_");
958 else
959 len = strlen(orig_name) + sizeof("_dupl_X");
960
961 new_name = malloc(len);
962 if (!new_name)
963 goto out;
964
965 if (dup < 0)
966 snprintf(new_name, len, "_%s", name);
967 else
968 snprintf(new_name, len, "%s_dupl_%d", orig_name, dup);
969
970out:
971 if (name != orig_name)
972 free(name);
973 return new_name;
974}
975
976static int event_class_add_field(struct bt_ctf_event_class *event_class,
977 struct bt_ctf_field_type *type,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400978 struct tep_format_field *field)
Wang Nane0a7cce2015-04-18 17:50:19 +0200979{
980 struct bt_ctf_field_type *t = NULL;
981 char *name;
982 int dup = 1;
983 int ret;
984
985 /* alias was already assigned */
986 if (field->alias != field->name)
987 return bt_ctf_event_class_add_field(event_class, type,
988 (char *)field->alias);
989
990 name = field->name;
991
992 /* If 'name' is a keywork, add prefix. */
993 if (bt_ctf_validate_identifier(name))
994 name = change_name(name, field->name, -1);
995
996 if (!name) {
997 pr_err("Failed to fix invalid identifier.");
998 return -1;
999 }
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
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001016static int add_tracepoint_fields_types(struct ctf_writer *cw,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001017 struct tep_format_field *fields,
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001018 struct bt_ctf_event_class *event_class)
1019{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001020 struct tep_format_field *field;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001021 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 * A string is an array of chars. For this we use the string
1035 * type and don't care that it is an array. What we don't
1036 * support is an array of strings.
1037 */
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001038 if (flags & TEP_FIELD_IS_STRING)
1039 flags &= ~TEP_FIELD_IS_ARRAY;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001040
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001041 if (flags & TEP_FIELD_IS_ARRAY)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001042 type = bt_ctf_field_type_array_create(type, field->arraylen);
1043
Wang Nane0a7cce2015-04-18 17:50:19 +02001044 ret = event_class_add_field(event_class, type, field);
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001045
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001046 if (flags & TEP_FIELD_IS_ARRAY)
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001047 bt_ctf_field_type_put(type);
1048
1049 if (ret) {
Wang Nane0a7cce2015-04-18 17:50:19 +02001050 pr_err("Failed to add field '%s': %d\n",
1051 field->name, ret);
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001052 return -1;
1053 }
1054 }
1055
1056 return 0;
1057}
1058
1059static int add_tracepoint_types(struct ctf_writer *cw,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001060 struct evsel *evsel,
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001061 struct bt_ctf_event_class *class)
1062{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001063 struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
1064 struct tep_format_field *fields = evsel->tp_format->format.fields;
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001065 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
Wang Nan6122d572016-02-26 09:31:56 +00001074static 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
Jiri Olsa32dcd022019-07-21 13:23:51 +02001093static int add_generic_types(struct ctf_writer *cw, struct evsel *evsel,
Jiri Olsaedbe9812015-02-20 23:17:00 +01001094 struct bt_ctf_event_class *event_class)
1095{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001096 u64 type = evsel->core.attr.sample_type;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001097
1098 /*
1099 * missing:
1100 * PERF_SAMPLE_TIME - not needed as we have it in
1101 * ctf event header
1102 * PERF_SAMPLE_READ - TODO
1103 * PERF_SAMPLE_CALLCHAIN - TODO
Wang Nan6122d572016-02-26 09:31:56 +00001104 * PERF_SAMPLE_RAW - tracepoint fields and BPF output
1105 * are handled separately
Jiri Olsaedbe9812015-02-20 23:17:00 +01001106 * PERF_SAMPLE_BRANCH_STACK - TODO
1107 * PERF_SAMPLE_REGS_USER - TODO
1108 * PERF_SAMPLE_STACK_USER - TODO
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)) { \
Wang Nane0a7cce2015-04-18 17:50:19 +02001115 pr_err("Failed to add field '%s';\n", n); \
Jiri Olsaedbe9812015-02-20 23:17:00 +01001116 return -1; \
1117 } \
1118 } while (0)
1119
1120 if (type & PERF_SAMPLE_IP)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001121 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001122
1123 if (type & PERF_SAMPLE_TID) {
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001124 ADD_FIELD(event_class, cw->data.s32, "perf_tid");
1125 ADD_FIELD(event_class, cw->data.s32, "perf_pid");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001126 }
1127
1128 if ((type & PERF_SAMPLE_ID) ||
1129 (type & PERF_SAMPLE_IDENTIFIER))
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001130 ADD_FIELD(event_class, cw->data.u64, "perf_id");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001131
1132 if (type & PERF_SAMPLE_STREAM_ID)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001133 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001134
Jiri Olsaedbe9812015-02-20 23:17:00 +01001135 if (type & PERF_SAMPLE_PERIOD)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001136 ADD_FIELD(event_class, cw->data.u64, "perf_period");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001137
1138 if (type & PERF_SAMPLE_WEIGHT)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001139 ADD_FIELD(event_class, cw->data.u64, "perf_weight");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001140
1141 if (type & PERF_SAMPLE_DATA_SRC)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001142 ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001143
1144 if (type & PERF_SAMPLE_TRANSACTION)
Sebastian Andrzej Siewior54cf7762015-02-20 23:17:01 +01001145 ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001146
Geneviève Bastiena3073c82017-07-27 14:12:03 -04001147 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
Jiri Olsaedbe9812015-02-20 23:17:00 +01001155#undef ADD_FIELD
1156 return 0;
1157}
1158
Jiri Olsa32dcd022019-07-21 13:23:51 +02001159static int add_event(struct ctf_writer *cw, struct evsel *evsel)
Jiri Olsaedbe9812015-02-20 23:17:00 +01001160{
1161 struct bt_ctf_event_class *event_class;
1162 struct evsel_priv *priv;
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03001163 const char *name = evsel__name(evsel);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001164 int ret;
1165
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001166 pr("Adding event '%s' (type %d)\n", name, evsel->core.attr.type);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001167
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
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001176 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
Sebastian Andrzej Siewior69364722015-02-20 23:17:02 +01001177 ret = add_tracepoint_types(cw, evsel, event_class);
1178 if (ret)
1179 goto err;
1180 }
1181
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001182 if (evsel__is_bpf_output(evsel)) {
Wang Nan6122d572016-02-26 09:31:56 +00001183 ret = add_bpf_output_types(cw, event_class);
1184 if (ret)
1185 goto err;
1186 }
1187
Jiri Olsaedbe9812015-02-20 23:17:00 +01001188 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
1202err:
1203 bt_ctf_event_class_put(event_class);
1204 pr_err("Failed to add event '%s'.\n", name);
1205 return -1;
1206}
1207
1208static int setup_events(struct ctf_writer *cw, struct perf_session *session)
1209{
Jiri Olsa63503db2019-07-21 13:23:52 +02001210 struct evlist *evlist = session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001211 struct evsel *evsel;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001212 int ret;
1213
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001214 evlist__for_each_entry(evlist, evsel) {
Jiri Olsaedbe9812015-02-20 23:17:00 +01001215 ret = add_event(cw, evsel);
1216 if (ret)
1217 return ret;
1218 }
1219 return 0;
1220}
1221
Wang Nanf5a08ce2016-06-24 11:22:10 +00001222#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) \
1232static 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
Wang Nanebccba32016-06-24 11:22:12 +00001260__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
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -04001276__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)
Wang Nanf5a08ce2016-06-24 11:22:10 +00001289#undef __NON_SAMPLE_ADD_FIELD
1290#undef __FUNC_ADD_NON_SAMPLE_EVENT_CLASS
1291
1292static 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;
Wang Nanebccba32016-06-24 11:22:12 +00001300 ret = add_exit_event(cw);
1301 if (ret)
1302 return ret;
1303 ret = add_fork_event(cw);
1304 if (ret)
1305 return ret;
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -04001306 ret = add_mmap_event(cw);
1307 if (ret)
1308 return ret;
1309 ret = add_mmap2_event(cw);
1310 if (ret)
1311 return ret;
Wang Nanf5a08ce2016-06-24 11:22:10 +00001312 return 0;
1313}
1314
Wang Nan5141d732016-02-05 14:01:30 +00001315static void cleanup_events(struct perf_session *session)
1316{
Jiri Olsa63503db2019-07-21 13:23:52 +02001317 struct evlist *evlist = session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001318 struct evsel *evsel;
Wang Nan5141d732016-02-05 14:01:30 +00001319
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001320 evlist__for_each_entry(evlist, evsel) {
Wang Nan5141d732016-02-05 14:01:30 +00001321 struct evsel_priv *priv;
1322
1323 priv = evsel->priv;
1324 bt_ctf_event_class_put(priv->event_class);
1325 zfree(&evsel->priv);
1326 }
1327
Jiri Olsac12995a2019-07-21 13:23:56 +02001328 evlist__delete(evlist);
Wang Nan5141d732016-02-05 14:01:30 +00001329 session->evlist = NULL;
1330}
1331
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001332static 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 * Try to get the number of cpus used in the data file,
1340 * if not present fallback to the MAX_CPUS.
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
1355static 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
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001362 zfree(&cw->stream);
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001363}
1364
Jiri Olsaedbe9812015-02-20 23:17:00 +01001365static 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) \
1372do { \
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
Jiri Olsa88371c52020-08-05 11:34:42 +02001389static int ctf_writer__setup_clock(struct ctf_writer *cw,
1390 struct perf_session *session,
1391 bool tod)
Jiri Olsaedbe9812015-02-20 23:17:00 +01001392{
1393 struct bt_ctf_clock *clock = cw->clock;
Jiri Olsa88371c52020-08-05 11:34:42 +02001394 const char *desc = "perf clock";
1395 int64_t offset = 0;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001396
Jiri Olsa88371c52020-08-05 11:34:42 +02001397 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 }
Jiri Olsaedbe9812015-02-20 23:17:00 +01001409
1410#define SET(__n, __v) \
1411do { \
1412 if (bt_ctf_clock_set_##__n(clock, __v)) \
1413 return -1; \
1414} while (0)
1415
1416 SET(frequency, 1000000000);
Jiri Olsa88371c52020-08-05 11:34:42 +02001417 SET(offset, offset);
1418 SET(description, desc);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001419 SET(precision, 10);
1420 SET(is_absolute, 0);
1421
1422#undef SET
1423 return 0;
1424}
1425
1426static 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
Ilya Leoshkevich4e881182021-11-04 14:23:11 +01001442#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
Wang Nanf8dd2d52016-02-26 09:31:57 +00001443 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
Jiri Olsaedbe9812015-02-20 23:17:00 +01001448 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
1449 size, sign ? "un" : "", hex ? "hex" : "");
1450 return type;
1451
1452err:
1453 bt_ctf_field_type_put(type);
1454 return NULL;
1455}
1456
1457static 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
1465static int ctf_writer__init_data(struct ctf_writer *cw)
1466{
1467#define CREATE_INT_TYPE(type, size, sign, hex) \
1468do { \
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);
Wang Nan26812d42015-12-08 02:25:39 +00001478 CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001479 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
1485err:
1486 ctf_writer__cleanup_data(cw);
1487 pr_err("Failed to create data types.\n");
1488 return -1;
1489}
1490
1491static void ctf_writer__cleanup(struct ctf_writer *cw)
1492{
1493 ctf_writer__cleanup_data(cw);
1494
1495 bt_ctf_clock_put(cw->clock);
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001496 free_streams(cw);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001497 bt_ctf_stream_class_put(cw->stream_class);
1498 bt_ctf_writer_put(cw->writer);
1499
1500 /* and NULL all the pointers */
1501 memset(cw, 0, sizeof(*cw));
1502}
1503
Jiri Olsa88371c52020-08-05 11:34:42 +02001504static int ctf_writer__init(struct ctf_writer *cw, const char *path,
1505 struct perf_session *session, bool tod)
Jiri Olsaedbe9812015-02-20 23:17:00 +01001506{
1507 struct bt_ctf_writer *writer;
1508 struct bt_ctf_stream_class *stream_class;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001509 struct bt_ctf_clock *clock;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001510 struct bt_ctf_field_type *pkt_ctx_type;
1511 int ret;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001512
1513 /* CTF writer */
1514 writer = bt_ctf_writer_create(path);
1515 if (!writer)
1516 goto err;
1517
1518 cw->writer = writer;
1519
1520 /* CTF clock */
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
Jiri Olsa88371c52020-08-05 11:34:42 +02001529 if (ctf_writer__setup_clock(cw, session, tod)) {
Jiri Olsaedbe9812015-02-20 23:17:00 +01001530 pr("Failed to setup CTF clock.\n");
1531 goto err_cleanup;
1532 }
1533
1534 /* CTF stream class */
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 /* CTF clock stream setup */
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
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001552 /* Add cpu_id for packet context */
1553 pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1554 if (!pkt_ctx_type)
Jiri Olsaedbe9812015-02-20 23:17:00 +01001555 goto err_cleanup;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001556
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001557 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;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001561
1562 /* CTF clock writer setup */
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
1570err_cleanup:
1571 ctf_writer__cleanup(cw);
1572err:
1573 pr_err("Failed to setup CTF writer.\n");
1574 return -1;
1575}
1576
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001577static 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
Jiri Olsa8fa46752015-04-18 17:50:17 +02001587static int convert__config(const char *var, const char *value, void *cb)
1588{
1589 struct convert *c = cb;
1590
Arnaldo Carvalho de Melo25ce4bb2017-06-27 11:44:58 -03001591 if (!strcmp(var, "convert.queue-size"))
1592 return perf_config_u64(&c->queue_size, var, value);
Jiri Olsa8fa46752015-04-18 17:50:17 +02001593
Wang Nanb8cbb342016-02-26 09:31:51 +00001594 return 0;
Jiri Olsa8fa46752015-04-18 17:50:17 +02001595}
1596
Wang Nan3275f682016-06-24 11:22:07 +00001597int bt_convert__perf2ctf(const char *input, const char *path,
1598 struct perf_data_convert_opts *opts)
Jiri Olsaedbe9812015-02-20 23:17:00 +01001599{
1600 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001601 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001602 .path = input,
Jiri Olsaeae8ad82017-01-23 22:25:41 +01001603 .mode = PERF_DATA_MODE_READ,
1604 .force = opts->force,
Jiri Olsaedbe9812015-02-20 23:17:00 +01001605 };
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,
Hari Bathinif3b36142017-03-08 02:11:43 +05301617 .namespaces = perf_event__process_namespaces,
Jiri Olsaedbe9812015-02-20 23:17:00 +01001618 .ordered_events = true,
1619 .ordering_requires_timestamps = true,
1620 },
1621 };
1622 struct ctf_writer *cw = &c.writer;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001623 int err;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001624
Wang Nanebccba32016-06-24 11:22:12 +00001625 if (opts->all) {
Wang Nanf5a08ce2016-06-24 11:22:10 +00001626 c.tool.comm = process_comm_event;
Wang Nanebccba32016-06-24 11:22:12 +00001627 c.tool.exit = process_exit_event;
1628 c.tool.fork = process_fork_event;
Geneviève Bastienf9f6f2a2017-07-27 14:12:04 -04001629 c.tool.mmap = process_mmap_event;
1630 c.tool.mmap2 = process_mmap2_event;
Wang Nanebccba32016-06-24 11:22:12 +00001631 }
Wang Nanf5a08ce2016-06-24 11:22:10 +00001632
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001633 err = perf_config(convert__config, &c);
1634 if (err)
1635 return err;
Jiri Olsa8fa46752015-04-18 17:50:17 +02001636
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03001637 err = -1;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001638 /* perf.data session */
Namhyung Kim2681bd82021-07-19 15:31:49 -07001639 session = perf_session__new(&data, &c.tool);
Jiri Olsa88371c52020-08-05 11:34:42 +02001640 if (IS_ERR(session))
1641 return PTR_ERR(session);
1642
1643 /* CTF writer */
1644 if (ctf_writer__init(cw, path, session, opts->tod))
1645 goto free_session;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001646
Jiri Olsa8fa46752015-04-18 17:50:17 +02001647 if (c.queue_size) {
1648 ordered_events__set_alloc_size(&session->ordered_events,
1649 c.queue_size);
1650 }
1651
Jiri Olsaedbe9812015-02-20 23:17:00 +01001652 /* CTF writer env/clock setup */
1653 if (ctf_writer__setup_env(cw, session))
Jiri Olsa88371c52020-08-05 11:34:42 +02001654 goto free_writer;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001655
1656 /* CTF events setup */
1657 if (setup_events(cw, session))
Jiri Olsa88371c52020-08-05 11:34:42 +02001658 goto free_writer;
Jiri Olsaedbe9812015-02-20 23:17:00 +01001659
Wang Nanf5a08ce2016-06-24 11:22:10 +00001660 if (opts->all && setup_non_sample_events(cw, session))
Jiri Olsa88371c52020-08-05 11:34:42 +02001661 goto free_writer;
Wang Nanf5a08ce2016-06-24 11:22:10 +00001662
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001663 if (setup_streams(cw, session))
Jiri Olsa88371c52020-08-05 11:34:42 +02001664 goto free_writer;
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001665
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001666 err = perf_session__process_events(session);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001667 if (!err)
Sebastian Andrzej Siewior90e129f2015-04-18 17:50:15 +02001668 err = ctf_writer__flush_streams(cw);
He Kuangc2141052015-04-18 17:50:14 +02001669 else
1670 pr_err("Error during conversion.\n");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001671
1672 fprintf(stderr,
1673 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001674 data.path, path);
Jiri Olsaedbe9812015-02-20 23:17:00 +01001675
1676 fprintf(stderr,
Wang Nan8ee4c462016-06-24 11:22:09 +00001677 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
Jiri Olsaedbe9812015-02-20 23:17:00 +01001678 (double) c.events_size / 1024.0 / 1024.0,
1679 c.events_count);
1680
Wang Nan8ee4c462016-06-24 11:22:09 +00001681 if (!c.non_sample_count)
1682 fprintf(stderr, ") ]\n");
1683 else
1684 fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
1685
Wang Nan5141d732016-02-05 14:01:30 +00001686 cleanup_events(session);
He Kuangc2141052015-04-18 17:50:14 +02001687 perf_session__delete(session);
1688 ctf_writer__cleanup(cw);
1689
1690 return err;
1691
Jiri Olsaedbe9812015-02-20 23:17:00 +01001692free_writer:
1693 ctf_writer__cleanup(cw);
Jiri Olsa88371c52020-08-05 11:34:42 +02001694free_session:
1695 perf_session__delete(session);
He Kuangc2141052015-04-18 17:50:14 +02001696 pr_err("Error during conversion setup.\n");
Jiri Olsaedbe9812015-02-20 23:17:00 +01001697 return err;
1698}