perf data: Fix duplicate field names and avoid reserved keywords
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / data-convert-bt.c
1 /*
2  * CTF writing support via babeltrace.
3  *
4  * Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com>
5  * Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de>
6  *
7  * Released under the GPL v2. (and only v2, not any later version)
8  */
9
10 #include <linux/compiler.h>
11 #include <babeltrace/ctf-writer/writer.h>
12 #include <babeltrace/ctf-writer/clock.h>
13 #include <babeltrace/ctf-writer/stream.h>
14 #include <babeltrace/ctf-writer/event.h>
15 #include <babeltrace/ctf-writer/event-types.h>
16 #include <babeltrace/ctf-writer/event-fields.h>
17 #include <babeltrace/ctf-ir/utils.h>
18 #include <babeltrace/ctf/events.h>
19 #include <traceevent/event-parse.h>
20 #include "asm/bug.h"
21 #include "data-convert-bt.h"
22 #include "session.h"
23 #include "util.h"
24 #include "debug.h"
25 #include "tool.h"
26 #include "evlist.h"
27 #include "evsel.h"
28 #include "machine.h"
29
30 #define pr_N(n, fmt, ...) \
31         eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
32
33 #define pr(fmt, ...)  pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
34 #define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
35
36 #define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
37
38 struct evsel_priv {
39         struct bt_ctf_event_class *event_class;
40 };
41
42 #define MAX_CPUS        4096
43
44 struct ctf_stream {
45         struct bt_ctf_stream *stream;
46         int cpu;
47         u32 count;
48 };
49
50 struct ctf_writer {
51         /* writer primitives */
52         struct bt_ctf_writer             *writer;
53         struct ctf_stream               **stream;
54         int                               stream_cnt;
55         struct bt_ctf_stream_class       *stream_class;
56         struct bt_ctf_clock              *clock;
57
58         /* data types */
59         union {
60                 struct {
61                         struct bt_ctf_field_type        *s64;
62                         struct bt_ctf_field_type        *u64;
63                         struct bt_ctf_field_type        *s32;
64                         struct bt_ctf_field_type        *u32;
65                         struct bt_ctf_field_type        *string;
66                         struct bt_ctf_field_type        *u64_hex;
67                 };
68                 struct bt_ctf_field_type *array[6];
69         } data;
70 };
71
72 struct convert {
73         struct perf_tool        tool;
74         struct ctf_writer       writer;
75
76         u64                     events_size;
77         u64                     events_count;
78
79         /* Ordered events configured queue size. */
80         u64                     queue_size;
81 };
82
83 static int value_set(struct bt_ctf_field_type *type,
84                      struct bt_ctf_event *event,
85                      const char *name, u64 val)
86 {
87         struct bt_ctf_field *field;
88         bool sign = bt_ctf_field_type_integer_get_signed(type);
89         int ret;
90
91         field = bt_ctf_field_create(type);
92         if (!field) {
93                 pr_err("failed to create a field %s\n", name);
94                 return -1;
95         }
96
97         if (sign) {
98                 ret = bt_ctf_field_signed_integer_set_value(field, val);
99                 if (ret) {
100                         pr_err("failed to set field value %s\n", name);
101                         goto err;
102                 }
103         } else {
104                 ret = bt_ctf_field_unsigned_integer_set_value(field, val);
105                 if (ret) {
106                         pr_err("failed to set field value %s\n", name);
107                         goto err;
108                 }
109         }
110
111         ret = bt_ctf_event_set_payload(event, name, field);
112         if (ret) {
113                 pr_err("failed to set payload %s\n", name);
114                 goto err;
115         }
116
117         pr2("  SET [%s = %" PRIu64 "]\n", name, val);
118
119 err:
120         bt_ctf_field_put(field);
121         return ret;
122 }
123
124 #define __FUNC_VALUE_SET(_name, _val_type)                              \
125 static __maybe_unused int value_set_##_name(struct ctf_writer *cw,      \
126                              struct bt_ctf_event *event,                \
127                              const char *name,                          \
128                              _val_type val)                             \
129 {                                                                       \
130         struct bt_ctf_field_type *type = cw->data._name;                \
131         return value_set(type, event, name, (u64) val);                 \
132 }
133
134 #define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
135
136 FUNC_VALUE_SET(s32)
137 FUNC_VALUE_SET(u32)
138 FUNC_VALUE_SET(s64)
139 FUNC_VALUE_SET(u64)
140 __FUNC_VALUE_SET(u64_hex, u64)
141
142 static struct bt_ctf_field_type*
143 get_tracepoint_field_type(struct ctf_writer *cw, struct format_field *field)
144 {
145         unsigned long flags = field->flags;
146
147         if (flags & FIELD_IS_STRING)
148                 return cw->data.string;
149
150         if (!(flags & FIELD_IS_SIGNED)) {
151                 /* unsigned long are mostly pointers */
152                 if (flags & FIELD_IS_LONG || flags & FIELD_IS_POINTER)
153                         return cw->data.u64_hex;
154         }
155
156         if (flags & FIELD_IS_SIGNED) {
157                 if (field->size == 8)
158                         return cw->data.s64;
159                 else
160                         return cw->data.s32;
161         }
162
163         if (field->size == 8)
164                 return cw->data.u64;
165         else
166                 return cw->data.u32;
167 }
168
169 static int add_tracepoint_field_value(struct ctf_writer *cw,
170                                       struct bt_ctf_event_class *event_class,
171                                       struct bt_ctf_event *event,
172                                       struct perf_sample *sample,
173                                       struct format_field *fmtf)
174 {
175         struct bt_ctf_field_type *type;
176         struct bt_ctf_field *array_field;
177         struct bt_ctf_field *field;
178         const char *name = fmtf->name;
179         void *data = sample->raw_data;
180         unsigned long long value_int;
181         unsigned long flags = fmtf->flags;
182         unsigned int n_items;
183         unsigned int i;
184         unsigned int offset;
185         unsigned int len;
186         int ret;
187
188         name = fmtf->alias;
189         offset = fmtf->offset;
190         len = fmtf->size;
191         if (flags & FIELD_IS_STRING)
192                 flags &= ~FIELD_IS_ARRAY;
193
194         if (flags & FIELD_IS_DYNAMIC) {
195                 unsigned long long tmp_val;
196
197                 tmp_val = pevent_read_number(fmtf->event->pevent,
198                                 data + offset, len);
199                 offset = tmp_val;
200                 len = offset >> 16;
201                 offset &= 0xffff;
202         }
203
204         if (flags & FIELD_IS_ARRAY) {
205
206                 type = bt_ctf_event_class_get_field_by_name(
207                                 event_class, name);
208                 array_field = bt_ctf_field_create(type);
209                 bt_ctf_field_type_put(type);
210                 if (!array_field) {
211                         pr_err("Failed to create array type %s\n", name);
212                         return -1;
213                 }
214
215                 len = fmtf->size / fmtf->arraylen;
216                 n_items = fmtf->arraylen;
217         } else {
218                 n_items = 1;
219                 array_field = NULL;
220         }
221
222         type = get_tracepoint_field_type(cw, fmtf);
223
224         for (i = 0; i < n_items; i++) {
225                 if (!(flags & FIELD_IS_STRING))
226                         value_int = pevent_read_number(
227                                         fmtf->event->pevent,
228                                         data + offset + i * len, len);
229
230                 if (flags & FIELD_IS_ARRAY)
231                         field = bt_ctf_field_array_get_field(array_field, i);
232                 else
233                         field = bt_ctf_field_create(type);
234
235                 if (!field) {
236                         pr_err("failed to create a field %s\n", name);
237                         return -1;
238                 }
239
240                 if (flags & FIELD_IS_STRING)
241                         ret = bt_ctf_field_string_set_value(field,
242                                         data + offset + i * len);
243                 else if (!(flags & FIELD_IS_SIGNED))
244                         ret = bt_ctf_field_unsigned_integer_set_value(
245                                         field, value_int);
246                 else
247                         ret = bt_ctf_field_signed_integer_set_value(
248                                         field, value_int);
249                 if (ret) {
250                         pr_err("failed to set file value %s\n", name);
251                         goto err_put_field;
252                 }
253                 if (!(flags & FIELD_IS_ARRAY)) {
254                         ret = bt_ctf_event_set_payload(event, name, field);
255                         if (ret) {
256                                 pr_err("failed to set payload %s\n", name);
257                                 goto err_put_field;
258                         }
259                 }
260                 bt_ctf_field_put(field);
261         }
262         if (flags & FIELD_IS_ARRAY) {
263                 ret = bt_ctf_event_set_payload(event, name, array_field);
264                 if (ret) {
265                         pr_err("Failed add payload array %s\n", name);
266                         return -1;
267                 }
268                 bt_ctf_field_put(array_field);
269         }
270         return 0;
271
272 err_put_field:
273         bt_ctf_field_put(field);
274         return -1;
275 }
276
277 static int add_tracepoint_fields_values(struct ctf_writer *cw,
278                                         struct bt_ctf_event_class *event_class,
279                                         struct bt_ctf_event *event,
280                                         struct format_field *fields,
281                                         struct perf_sample *sample)
282 {
283         struct format_field *field;
284         int ret;
285
286         for (field = fields; field; field = field->next) {
287                 ret = add_tracepoint_field_value(cw, event_class, event, sample,
288                                 field);
289                 if (ret)
290                         return -1;
291         }
292         return 0;
293 }
294
295 static int add_tracepoint_values(struct ctf_writer *cw,
296                                  struct bt_ctf_event_class *event_class,
297                                  struct bt_ctf_event *event,
298                                  struct perf_evsel *evsel,
299                                  struct perf_sample *sample)
300 {
301         struct format_field *common_fields = evsel->tp_format->format.common_fields;
302         struct format_field *fields        = evsel->tp_format->format.fields;
303         int ret;
304
305         ret = add_tracepoint_fields_values(cw, event_class, event,
306                                            common_fields, sample);
307         if (!ret)
308                 ret = add_tracepoint_fields_values(cw, event_class, event,
309                                                    fields, sample);
310
311         return ret;
312 }
313
314 static int add_generic_values(struct ctf_writer *cw,
315                               struct bt_ctf_event *event,
316                               struct perf_evsel *evsel,
317                               struct perf_sample *sample)
318 {
319         u64 type = evsel->attr.sample_type;
320         int ret;
321
322         /*
323          * missing:
324          *   PERF_SAMPLE_TIME         - not needed as we have it in
325          *                              ctf event header
326          *   PERF_SAMPLE_READ         - TODO
327          *   PERF_SAMPLE_CALLCHAIN    - TODO
328          *   PERF_SAMPLE_RAW          - tracepoint fields are handled separately
329          *   PERF_SAMPLE_BRANCH_STACK - TODO
330          *   PERF_SAMPLE_REGS_USER    - TODO
331          *   PERF_SAMPLE_STACK_USER   - TODO
332          */
333
334         if (type & PERF_SAMPLE_IP) {
335                 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
336                 if (ret)
337                         return -1;
338         }
339
340         if (type & PERF_SAMPLE_TID) {
341                 ret = value_set_s32(cw, event, "perf_tid", sample->tid);
342                 if (ret)
343                         return -1;
344
345                 ret = value_set_s32(cw, event, "perf_pid", sample->pid);
346                 if (ret)
347                         return -1;
348         }
349
350         if ((type & PERF_SAMPLE_ID) ||
351             (type & PERF_SAMPLE_IDENTIFIER)) {
352                 ret = value_set_u64(cw, event, "perf_id", sample->id);
353                 if (ret)
354                         return -1;
355         }
356
357         if (type & PERF_SAMPLE_STREAM_ID) {
358                 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
359                 if (ret)
360                         return -1;
361         }
362
363         if (type & PERF_SAMPLE_PERIOD) {
364                 ret = value_set_u64(cw, event, "perf_period", sample->period);
365                 if (ret)
366                         return -1;
367         }
368
369         if (type & PERF_SAMPLE_WEIGHT) {
370                 ret = value_set_u64(cw, event, "perf_weight", sample->weight);
371                 if (ret)
372                         return -1;
373         }
374
375         if (type & PERF_SAMPLE_DATA_SRC) {
376                 ret = value_set_u64(cw, event, "perf_data_src",
377                                 sample->data_src);
378                 if (ret)
379                         return -1;
380         }
381
382         if (type & PERF_SAMPLE_TRANSACTION) {
383                 ret = value_set_u64(cw, event, "perf_transaction",
384                                 sample->transaction);
385                 if (ret)
386                         return -1;
387         }
388
389         return 0;
390 }
391
392 static int ctf_stream__flush(struct ctf_stream *cs)
393 {
394         int err = 0;
395
396         if (cs) {
397                 err = bt_ctf_stream_flush(cs->stream);
398                 if (err)
399                         pr_err("CTF stream %d flush failed\n", cs->cpu);
400
401                 pr("Flush stream for cpu %d (%u samples)\n",
402                    cs->cpu, cs->count);
403
404                 cs->count = 0;
405         }
406
407         return err;
408 }
409
410 static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
411 {
412         struct ctf_stream *cs;
413         struct bt_ctf_field *pkt_ctx   = NULL;
414         struct bt_ctf_field *cpu_field = NULL;
415         struct bt_ctf_stream *stream   = NULL;
416         int ret;
417
418         cs = zalloc(sizeof(*cs));
419         if (!cs) {
420                 pr_err("Failed to allocate ctf stream\n");
421                 return NULL;
422         }
423
424         stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
425         if (!stream) {
426                 pr_err("Failed to create CTF stream\n");
427                 goto out;
428         }
429
430         pkt_ctx = bt_ctf_stream_get_packet_context(stream);
431         if (!pkt_ctx) {
432                 pr_err("Failed to obtain packet context\n");
433                 goto out;
434         }
435
436         cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
437         bt_ctf_field_put(pkt_ctx);
438         if (!cpu_field) {
439                 pr_err("Failed to obtain cpu field\n");
440                 goto out;
441         }
442
443         ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
444         if (ret) {
445                 pr_err("Failed to update CPU number\n");
446                 goto out;
447         }
448
449         bt_ctf_field_put(cpu_field);
450
451         cs->cpu    = cpu;
452         cs->stream = stream;
453         return cs;
454
455 out:
456         if (cpu_field)
457                 bt_ctf_field_put(cpu_field);
458         if (stream)
459                 bt_ctf_stream_put(stream);
460
461         free(cs);
462         return NULL;
463 }
464
465 static void ctf_stream__delete(struct ctf_stream *cs)
466 {
467         if (cs) {
468                 bt_ctf_stream_put(cs->stream);
469                 free(cs);
470         }
471 }
472
473 static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
474 {
475         struct ctf_stream *cs = cw->stream[cpu];
476
477         if (!cs) {
478                 cs = ctf_stream__create(cw, cpu);
479                 cw->stream[cpu] = cs;
480         }
481
482         return cs;
483 }
484
485 static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
486                           struct perf_evsel *evsel)
487 {
488         int cpu = 0;
489
490         if (evsel->attr.sample_type & PERF_SAMPLE_CPU)
491                 cpu = sample->cpu;
492
493         if (cpu > cw->stream_cnt) {
494                 pr_err("Event was recorded for CPU %d, limit is at %d.\n",
495                         cpu, cw->stream_cnt);
496                 cpu = 0;
497         }
498
499         return cpu;
500 }
501
502 #define STREAM_FLUSH_COUNT 100000
503
504 /*
505  * Currently we have no other way to determine the
506  * time for the stream flush other than keep track
507  * of the number of events and check it against
508  * threshold.
509  */
510 static bool is_flush_needed(struct ctf_stream *cs)
511 {
512         return cs->count >= STREAM_FLUSH_COUNT;
513 }
514
515 static int process_sample_event(struct perf_tool *tool,
516                                 union perf_event *_event __maybe_unused,
517                                 struct perf_sample *sample,
518                                 struct perf_evsel *evsel,
519                                 struct machine *machine __maybe_unused)
520 {
521         struct convert *c = container_of(tool, struct convert, tool);
522         struct evsel_priv *priv = evsel->priv;
523         struct ctf_writer *cw = &c->writer;
524         struct ctf_stream *cs;
525         struct bt_ctf_event_class *event_class;
526         struct bt_ctf_event *event;
527         int ret;
528
529         if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
530                 return 0;
531
532         event_class = priv->event_class;
533
534         /* update stats */
535         c->events_count++;
536         c->events_size += _event->header.size;
537
538         pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
539
540         event = bt_ctf_event_create(event_class);
541         if (!event) {
542                 pr_err("Failed to create an CTF event\n");
543                 return -1;
544         }
545
546         bt_ctf_clock_set_time(cw->clock, sample->time);
547
548         ret = add_generic_values(cw, event, evsel, sample);
549         if (ret)
550                 return -1;
551
552         if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
553                 ret = add_tracepoint_values(cw, event_class, event,
554                                             evsel, sample);
555                 if (ret)
556                         return -1;
557         }
558
559         cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel));
560         if (cs) {
561                 if (is_flush_needed(cs))
562                         ctf_stream__flush(cs);
563
564                 cs->count++;
565                 bt_ctf_stream_append_event(cs->stream, event);
566         }
567
568         bt_ctf_event_put(event);
569         return cs ? 0 : -1;
570 }
571
572 /* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
573 static char *change_name(char *name, char *orig_name, int dup)
574 {
575         char *new_name = NULL;
576         size_t len;
577
578         if (!name)
579                 name = orig_name;
580
581         if (dup >= 10)
582                 goto out;
583         /*
584          * Add '_' prefix to potential keywork.  According to
585          * Mathieu Desnoyers (https://lkml.org/lkml/2015/1/23/652),
586          * futher CTF spec updating may require us to use '$'.
587          */
588         if (dup < 0)
589                 len = strlen(name) + sizeof("_");
590         else
591                 len = strlen(orig_name) + sizeof("_dupl_X");
592
593         new_name = malloc(len);
594         if (!new_name)
595                 goto out;
596
597         if (dup < 0)
598                 snprintf(new_name, len, "_%s", name);
599         else
600                 snprintf(new_name, len, "%s_dupl_%d", orig_name, dup);
601
602 out:
603         if (name != orig_name)
604                 free(name);
605         return new_name;
606 }
607
608 static int event_class_add_field(struct bt_ctf_event_class *event_class,
609                 struct bt_ctf_field_type *type,
610                 struct format_field *field)
611 {
612         struct bt_ctf_field_type *t = NULL;
613         char *name;
614         int dup = 1;
615         int ret;
616
617         /* alias was already assigned */
618         if (field->alias != field->name)
619                 return bt_ctf_event_class_add_field(event_class, type,
620                                 (char *)field->alias);
621
622         name = field->name;
623
624         /* If 'name' is a keywork, add prefix. */
625         if (bt_ctf_validate_identifier(name))
626                 name = change_name(name, field->name, -1);
627
628         if (!name) {
629                 pr_err("Failed to fix invalid identifier.");
630                 return -1;
631         }
632         while ((t = bt_ctf_event_class_get_field_by_name(event_class, name))) {
633                 bt_ctf_field_type_put(t);
634                 name = change_name(name, field->name, dup++);
635                 if (!name) {
636                         pr_err("Failed to create dup name for '%s'\n", field->name);
637                         return -1;
638                 }
639         }
640
641         ret = bt_ctf_event_class_add_field(event_class, type, name);
642         if (!ret)
643                 field->alias = name;
644
645         return ret;
646 }
647
648 static int add_tracepoint_fields_types(struct ctf_writer *cw,
649                                        struct format_field *fields,
650                                        struct bt_ctf_event_class *event_class)
651 {
652         struct format_field *field;
653         int ret;
654
655         for (field = fields; field; field = field->next) {
656                 struct bt_ctf_field_type *type;
657                 unsigned long flags = field->flags;
658
659                 pr2("  field '%s'\n", field->name);
660
661                 type = get_tracepoint_field_type(cw, field);
662                 if (!type)
663                         return -1;
664
665                 /*
666                  * A string is an array of chars. For this we use the string
667                  * type and don't care that it is an array. What we don't
668                  * support is an array of strings.
669                  */
670                 if (flags & FIELD_IS_STRING)
671                         flags &= ~FIELD_IS_ARRAY;
672
673                 if (flags & FIELD_IS_ARRAY)
674                         type = bt_ctf_field_type_array_create(type, field->arraylen);
675
676                 ret = event_class_add_field(event_class, type, field);
677
678                 if (flags & FIELD_IS_ARRAY)
679                         bt_ctf_field_type_put(type);
680
681                 if (ret) {
682                         pr_err("Failed to add field '%s': %d\n",
683                                         field->name, ret);
684                         return -1;
685                 }
686         }
687
688         return 0;
689 }
690
691 static int add_tracepoint_types(struct ctf_writer *cw,
692                                 struct perf_evsel *evsel,
693                                 struct bt_ctf_event_class *class)
694 {
695         struct format_field *common_fields = evsel->tp_format->format.common_fields;
696         struct format_field *fields        = evsel->tp_format->format.fields;
697         int ret;
698
699         ret = add_tracepoint_fields_types(cw, common_fields, class);
700         if (!ret)
701                 ret = add_tracepoint_fields_types(cw, fields, class);
702
703         return ret;
704 }
705
706 static int add_generic_types(struct ctf_writer *cw, struct perf_evsel *evsel,
707                              struct bt_ctf_event_class *event_class)
708 {
709         u64 type = evsel->attr.sample_type;
710
711         /*
712          * missing:
713          *   PERF_SAMPLE_TIME         - not needed as we have it in
714          *                              ctf event header
715          *   PERF_SAMPLE_READ         - TODO
716          *   PERF_SAMPLE_CALLCHAIN    - TODO
717          *   PERF_SAMPLE_RAW          - tracepoint fields are handled separately
718          *   PERF_SAMPLE_BRANCH_STACK - TODO
719          *   PERF_SAMPLE_REGS_USER    - TODO
720          *   PERF_SAMPLE_STACK_USER   - TODO
721          */
722
723 #define ADD_FIELD(cl, t, n)                                             \
724         do {                                                            \
725                 pr2("  field '%s'\n", n);                               \
726                 if (bt_ctf_event_class_add_field(cl, t, n)) {           \
727                         pr_err("Failed to add field '%s';\n", n);       \
728                         return -1;                                      \
729                 }                                                       \
730         } while (0)
731
732         if (type & PERF_SAMPLE_IP)
733                 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
734
735         if (type & PERF_SAMPLE_TID) {
736                 ADD_FIELD(event_class, cw->data.s32, "perf_tid");
737                 ADD_FIELD(event_class, cw->data.s32, "perf_pid");
738         }
739
740         if ((type & PERF_SAMPLE_ID) ||
741             (type & PERF_SAMPLE_IDENTIFIER))
742                 ADD_FIELD(event_class, cw->data.u64, "perf_id");
743
744         if (type & PERF_SAMPLE_STREAM_ID)
745                 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
746
747         if (type & PERF_SAMPLE_PERIOD)
748                 ADD_FIELD(event_class, cw->data.u64, "perf_period");
749
750         if (type & PERF_SAMPLE_WEIGHT)
751                 ADD_FIELD(event_class, cw->data.u64, "perf_weight");
752
753         if (type & PERF_SAMPLE_DATA_SRC)
754                 ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
755
756         if (type & PERF_SAMPLE_TRANSACTION)
757                 ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
758
759 #undef ADD_FIELD
760         return 0;
761 }
762
763 static int add_event(struct ctf_writer *cw, struct perf_evsel *evsel)
764 {
765         struct bt_ctf_event_class *event_class;
766         struct evsel_priv *priv;
767         const char *name = perf_evsel__name(evsel);
768         int ret;
769
770         pr("Adding event '%s' (type %d)\n", name, evsel->attr.type);
771
772         event_class = bt_ctf_event_class_create(name);
773         if (!event_class)
774                 return -1;
775
776         ret = add_generic_types(cw, evsel, event_class);
777         if (ret)
778                 goto err;
779
780         if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
781                 ret = add_tracepoint_types(cw, evsel, event_class);
782                 if (ret)
783                         goto err;
784         }
785
786         ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);
787         if (ret) {
788                 pr("Failed to add event class into stream.\n");
789                 goto err;
790         }
791
792         priv = malloc(sizeof(*priv));
793         if (!priv)
794                 goto err;
795
796         priv->event_class = event_class;
797         evsel->priv       = priv;
798         return 0;
799
800 err:
801         bt_ctf_event_class_put(event_class);
802         pr_err("Failed to add event '%s'.\n", name);
803         return -1;
804 }
805
806 static int setup_events(struct ctf_writer *cw, struct perf_session *session)
807 {
808         struct perf_evlist *evlist = session->evlist;
809         struct perf_evsel *evsel;
810         int ret;
811
812         evlist__for_each(evlist, evsel) {
813                 ret = add_event(cw, evsel);
814                 if (ret)
815                         return ret;
816         }
817         return 0;
818 }
819
820 static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
821 {
822         struct ctf_stream **stream;
823         struct perf_header *ph = &session->header;
824         int ncpus;
825
826         /*
827          * Try to get the number of cpus used in the data file,
828          * if not present fallback to the MAX_CPUS.
829          */
830         ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS;
831
832         stream = zalloc(sizeof(*stream) * ncpus);
833         if (!stream) {
834                 pr_err("Failed to allocate streams.\n");
835                 return -ENOMEM;
836         }
837
838         cw->stream     = stream;
839         cw->stream_cnt = ncpus;
840         return 0;
841 }
842
843 static void free_streams(struct ctf_writer *cw)
844 {
845         int cpu;
846
847         for (cpu = 0; cpu < cw->stream_cnt; cpu++)
848                 ctf_stream__delete(cw->stream[cpu]);
849
850         free(cw->stream);
851 }
852
853 static int ctf_writer__setup_env(struct ctf_writer *cw,
854                                  struct perf_session *session)
855 {
856         struct perf_header *header = &session->header;
857         struct bt_ctf_writer *writer = cw->writer;
858
859 #define ADD(__n, __v)                                                   \
860 do {                                                                    \
861         if (bt_ctf_writer_add_environment_field(writer, __n, __v))      \
862                 return -1;                                              \
863 } while (0)
864
865         ADD("host",    header->env.hostname);
866         ADD("sysname", "Linux");
867         ADD("release", header->env.os_release);
868         ADD("version", header->env.version);
869         ADD("machine", header->env.arch);
870         ADD("domain", "kernel");
871         ADD("tracer_name", "perf");
872
873 #undef ADD
874         return 0;
875 }
876
877 static int ctf_writer__setup_clock(struct ctf_writer *cw)
878 {
879         struct bt_ctf_clock *clock = cw->clock;
880
881         bt_ctf_clock_set_description(clock, "perf clock");
882
883 #define SET(__n, __v)                           \
884 do {                                            \
885         if (bt_ctf_clock_set_##__n(clock, __v)) \
886                 return -1;                      \
887 } while (0)
888
889         SET(frequency,   1000000000);
890         SET(offset_s,    0);
891         SET(offset,      0);
892         SET(precision,   10);
893         SET(is_absolute, 0);
894
895 #undef SET
896         return 0;
897 }
898
899 static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex)
900 {
901         struct bt_ctf_field_type *type;
902
903         type = bt_ctf_field_type_integer_create(size);
904         if (!type)
905                 return NULL;
906
907         if (sign &&
908             bt_ctf_field_type_integer_set_signed(type, 1))
909                 goto err;
910
911         if (hex &&
912             bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL))
913                 goto err;
914
915         pr2("Created type: INTEGER %d-bit %ssigned %s\n",
916             size, sign ? "un" : "", hex ? "hex" : "");
917         return type;
918
919 err:
920         bt_ctf_field_type_put(type);
921         return NULL;
922 }
923
924 static void ctf_writer__cleanup_data(struct ctf_writer *cw)
925 {
926         unsigned int i;
927
928         for (i = 0; i < ARRAY_SIZE(cw->data.array); i++)
929                 bt_ctf_field_type_put(cw->data.array[i]);
930 }
931
932 static int ctf_writer__init_data(struct ctf_writer *cw)
933 {
934 #define CREATE_INT_TYPE(type, size, sign, hex)          \
935 do {                                                    \
936         (type) = create_int_type(size, sign, hex);      \
937         if (!(type))                                    \
938                 goto err;                               \
939 } while (0)
940
941         CREATE_INT_TYPE(cw->data.s64, 64, true,  false);
942         CREATE_INT_TYPE(cw->data.u64, 64, false, false);
943         CREATE_INT_TYPE(cw->data.s32, 32, true,  false);
944         CREATE_INT_TYPE(cw->data.u32, 32, false, false);
945         CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true);
946
947         cw->data.string  = bt_ctf_field_type_string_create();
948         if (cw->data.string)
949                 return 0;
950
951 err:
952         ctf_writer__cleanup_data(cw);
953         pr_err("Failed to create data types.\n");
954         return -1;
955 }
956
957 static void ctf_writer__cleanup(struct ctf_writer *cw)
958 {
959         ctf_writer__cleanup_data(cw);
960
961         bt_ctf_clock_put(cw->clock);
962         free_streams(cw);
963         bt_ctf_stream_class_put(cw->stream_class);
964         bt_ctf_writer_put(cw->writer);
965
966         /* and NULL all the pointers */
967         memset(cw, 0, sizeof(*cw));
968 }
969
970 static int ctf_writer__init(struct ctf_writer *cw, const char *path)
971 {
972         struct bt_ctf_writer            *writer;
973         struct bt_ctf_stream_class      *stream_class;
974         struct bt_ctf_clock             *clock;
975         struct bt_ctf_field_type        *pkt_ctx_type;
976         int                             ret;
977
978         /* CTF writer */
979         writer = bt_ctf_writer_create(path);
980         if (!writer)
981                 goto err;
982
983         cw->writer = writer;
984
985         /* CTF clock */
986         clock = bt_ctf_clock_create("perf_clock");
987         if (!clock) {
988                 pr("Failed to create CTF clock.\n");
989                 goto err_cleanup;
990         }
991
992         cw->clock = clock;
993
994         if (ctf_writer__setup_clock(cw)) {
995                 pr("Failed to setup CTF clock.\n");
996                 goto err_cleanup;
997         }
998
999         /* CTF stream class */
1000         stream_class = bt_ctf_stream_class_create("perf_stream");
1001         if (!stream_class) {
1002                 pr("Failed to create CTF stream class.\n");
1003                 goto err_cleanup;
1004         }
1005
1006         cw->stream_class = stream_class;
1007
1008         /* CTF clock stream setup */
1009         if (bt_ctf_stream_class_set_clock(stream_class, clock)) {
1010                 pr("Failed to assign CTF clock to stream class.\n");
1011                 goto err_cleanup;
1012         }
1013
1014         if (ctf_writer__init_data(cw))
1015                 goto err_cleanup;
1016
1017         /* Add cpu_id for packet context */
1018         pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1019         if (!pkt_ctx_type)
1020                 goto err_cleanup;
1021
1022         ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id");
1023         bt_ctf_field_type_put(pkt_ctx_type);
1024         if (ret)
1025                 goto err_cleanup;
1026
1027         /* CTF clock writer setup */
1028         if (bt_ctf_writer_add_clock(writer, clock)) {
1029                 pr("Failed to assign CTF clock to writer.\n");
1030                 goto err_cleanup;
1031         }
1032
1033         return 0;
1034
1035 err_cleanup:
1036         ctf_writer__cleanup(cw);
1037 err:
1038         pr_err("Failed to setup CTF writer.\n");
1039         return -1;
1040 }
1041
1042 static int ctf_writer__flush_streams(struct ctf_writer *cw)
1043 {
1044         int cpu, ret = 0;
1045
1046         for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++)
1047                 ret = ctf_stream__flush(cw->stream[cpu]);
1048
1049         return ret;
1050 }
1051
1052 static int convert__config(const char *var, const char *value, void *cb)
1053 {
1054         struct convert *c = cb;
1055
1056         if (!strcmp(var, "convert.queue-size")) {
1057                 c->queue_size = perf_config_u64(var, value);
1058                 return 0;
1059         }
1060
1061         return perf_default_config(var, value, cb);
1062 }
1063
1064 int bt_convert__perf2ctf(const char *input, const char *path, bool force)
1065 {
1066         struct perf_session *session;
1067         struct perf_data_file file = {
1068                 .path = input,
1069                 .mode = PERF_DATA_MODE_READ,
1070                 .force = force,
1071         };
1072         struct convert c = {
1073                 .tool = {
1074                         .sample          = process_sample_event,
1075                         .mmap            = perf_event__process_mmap,
1076                         .mmap2           = perf_event__process_mmap2,
1077                         .comm            = perf_event__process_comm,
1078                         .exit            = perf_event__process_exit,
1079                         .fork            = perf_event__process_fork,
1080                         .lost            = perf_event__process_lost,
1081                         .tracing_data    = perf_event__process_tracing_data,
1082                         .build_id        = perf_event__process_build_id,
1083                         .ordered_events  = true,
1084                         .ordering_requires_timestamps = true,
1085                 },
1086         };
1087         struct ctf_writer *cw = &c.writer;
1088         int err = -1;
1089
1090         perf_config(convert__config, &c);
1091
1092         /* CTF writer */
1093         if (ctf_writer__init(cw, path))
1094                 return -1;
1095
1096         /* perf.data session */
1097         session = perf_session__new(&file, 0, &c.tool);
1098         if (!session)
1099                 goto free_writer;
1100
1101         if (c.queue_size) {
1102                 ordered_events__set_alloc_size(&session->ordered_events,
1103                                                c.queue_size);
1104         }
1105
1106         /* CTF writer env/clock setup  */
1107         if (ctf_writer__setup_env(cw, session))
1108                 goto free_session;
1109
1110         /* CTF events setup */
1111         if (setup_events(cw, session))
1112                 goto free_session;
1113
1114         if (setup_streams(cw, session))
1115                 goto free_session;
1116
1117         err = perf_session__process_events(session);
1118         if (!err)
1119                 err = ctf_writer__flush_streams(cw);
1120         else
1121                 pr_err("Error during conversion.\n");
1122
1123         fprintf(stderr,
1124                 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
1125                 file.path, path);
1126
1127         fprintf(stderr,
1128                 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
1129                 (double) c.events_size / 1024.0 / 1024.0,
1130                 c.events_count);
1131
1132         perf_session__delete(session);
1133         ctf_writer__cleanup(cw);
1134
1135         return err;
1136
1137 free_session:
1138         perf_session__delete(session);
1139 free_writer:
1140         ctf_writer__cleanup(cw);
1141         pr_err("Error during conversion setup.\n");
1142         return err;
1143 }