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