xref: /linux/tools/perf/util/data-convert-bt.c (revision 63503dba87acfab49280d3b05df6705a6f327e8a)
191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2edbe9817SJiri Olsa /*
3edbe9817SJiri Olsa  * CTF writing support via babeltrace.
4edbe9817SJiri Olsa  *
5edbe9817SJiri Olsa  * Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com>
6edbe9817SJiri Olsa  * Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7edbe9817SJiri Olsa  */
8edbe9817SJiri Olsa 
9a43783aeSArnaldo Carvalho de Melo #include <errno.h>
10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
11edbe9817SJiri Olsa #include <linux/compiler.h>
12877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
137f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
14edbe9817SJiri Olsa #include <babeltrace/ctf-writer/writer.h>
15edbe9817SJiri Olsa #include <babeltrace/ctf-writer/clock.h>
16edbe9817SJiri Olsa #include <babeltrace/ctf-writer/stream.h>
17edbe9817SJiri Olsa #include <babeltrace/ctf-writer/event.h>
18edbe9817SJiri Olsa #include <babeltrace/ctf-writer/event-types.h>
19edbe9817SJiri Olsa #include <babeltrace/ctf-writer/event-fields.h>
20e0a7cce5SWang Nan #include <babeltrace/ctf-ir/utils.h>
21edbe9817SJiri Olsa #include <babeltrace/ctf/events.h>
22edbe9817SJiri Olsa #include <traceevent/event-parse.h>
23edbe9817SJiri Olsa #include "asm/bug.h"
24edbe9817SJiri Olsa #include "data-convert-bt.h"
25edbe9817SJiri Olsa #include "session.h"
26edbe9817SJiri Olsa #include "debug.h"
27edbe9817SJiri Olsa #include "tool.h"
28edbe9817SJiri Olsa #include "evlist.h"
29edbe9817SJiri Olsa #include "evsel.h"
30edbe9817SJiri Olsa #include "machine.h"
31f6c12a00SJiri Olsa #include "config.h"
323052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h>
33edbe9817SJiri Olsa 
34edbe9817SJiri Olsa #define pr_N(n, fmt, ...) \
35edbe9817SJiri Olsa 	eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
36edbe9817SJiri Olsa 
37edbe9817SJiri Olsa #define pr(fmt, ...)  pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
38edbe9817SJiri Olsa #define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
39edbe9817SJiri Olsa 
40edbe9817SJiri Olsa #define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
41edbe9817SJiri Olsa 
42edbe9817SJiri Olsa struct evsel_priv {
43edbe9817SJiri Olsa 	struct bt_ctf_event_class *event_class;
44edbe9817SJiri Olsa };
45edbe9817SJiri Olsa 
4690e129ffSSebastian Andrzej Siewior #define MAX_CPUS	4096
4790e129ffSSebastian Andrzej Siewior 
4890e129ffSSebastian Andrzej Siewior struct ctf_stream {
4990e129ffSSebastian Andrzej Siewior 	struct bt_ctf_stream *stream;
5090e129ffSSebastian Andrzej Siewior 	int cpu;
5189e5fa88SJiri Olsa 	u32 count;
5290e129ffSSebastian Andrzej Siewior };
5390e129ffSSebastian Andrzej Siewior 
54edbe9817SJiri Olsa struct ctf_writer {
55edbe9817SJiri Olsa 	/* writer primitives */
56edbe9817SJiri Olsa 	struct bt_ctf_writer		 *writer;
5790e129ffSSebastian Andrzej Siewior 	struct ctf_stream		**stream;
5890e129ffSSebastian Andrzej Siewior 	int				  stream_cnt;
59edbe9817SJiri Olsa 	struct bt_ctf_stream_class	 *stream_class;
60edbe9817SJiri Olsa 	struct bt_ctf_clock		 *clock;
61edbe9817SJiri Olsa 
62edbe9817SJiri Olsa 	/* data types */
63edbe9817SJiri Olsa 	union {
64edbe9817SJiri Olsa 		struct {
65edbe9817SJiri Olsa 			struct bt_ctf_field_type	*s64;
66edbe9817SJiri Olsa 			struct bt_ctf_field_type	*u64;
67edbe9817SJiri Olsa 			struct bt_ctf_field_type	*s32;
68edbe9817SJiri Olsa 			struct bt_ctf_field_type	*u32;
69edbe9817SJiri Olsa 			struct bt_ctf_field_type	*string;
7026812d46SWang Nan 			struct bt_ctf_field_type	*u32_hex;
71edbe9817SJiri Olsa 			struct bt_ctf_field_type	*u64_hex;
72edbe9817SJiri Olsa 		};
73edbe9817SJiri Olsa 		struct bt_ctf_field_type *array[6];
74edbe9817SJiri Olsa 	} data;
75f5a08cedSWang Nan 	struct bt_ctf_event_class	*comm_class;
76ebccba3fSWang Nan 	struct bt_ctf_event_class	*exit_class;
77ebccba3fSWang Nan 	struct bt_ctf_event_class	*fork_class;
78f9f6f2a9SGeneviève Bastien 	struct bt_ctf_event_class	*mmap_class;
79f9f6f2a9SGeneviève Bastien 	struct bt_ctf_event_class	*mmap2_class;
80edbe9817SJiri Olsa };
81edbe9817SJiri Olsa 
82edbe9817SJiri Olsa struct convert {
83edbe9817SJiri Olsa 	struct perf_tool	tool;
84edbe9817SJiri Olsa 	struct ctf_writer	writer;
85edbe9817SJiri Olsa 
86edbe9817SJiri Olsa 	u64			events_size;
87edbe9817SJiri Olsa 	u64			events_count;
888ee4c46cSWang Nan 	u64			non_sample_count;
898fa46753SJiri Olsa 
908fa46753SJiri Olsa 	/* Ordered events configured queue size. */
918fa46753SJiri Olsa 	u64			queue_size;
92edbe9817SJiri Olsa };
93edbe9817SJiri Olsa 
94edbe9817SJiri Olsa static int value_set(struct bt_ctf_field_type *type,
95edbe9817SJiri Olsa 		     struct bt_ctf_event *event,
96edbe9817SJiri Olsa 		     const char *name, u64 val)
97edbe9817SJiri Olsa {
98edbe9817SJiri Olsa 	struct bt_ctf_field *field;
99edbe9817SJiri Olsa 	bool sign = bt_ctf_field_type_integer_get_signed(type);
100edbe9817SJiri Olsa 	int ret;
101edbe9817SJiri Olsa 
102edbe9817SJiri Olsa 	field = bt_ctf_field_create(type);
103edbe9817SJiri Olsa 	if (!field) {
104edbe9817SJiri Olsa 		pr_err("failed to create a field %s\n", name);
105edbe9817SJiri Olsa 		return -1;
106edbe9817SJiri Olsa 	}
107edbe9817SJiri Olsa 
108edbe9817SJiri Olsa 	if (sign) {
109edbe9817SJiri Olsa 		ret = bt_ctf_field_signed_integer_set_value(field, val);
110edbe9817SJiri Olsa 		if (ret) {
111edbe9817SJiri Olsa 			pr_err("failed to set field value %s\n", name);
112edbe9817SJiri Olsa 			goto err;
113edbe9817SJiri Olsa 		}
114edbe9817SJiri Olsa 	} else {
115edbe9817SJiri Olsa 		ret = bt_ctf_field_unsigned_integer_set_value(field, val);
116edbe9817SJiri Olsa 		if (ret) {
117edbe9817SJiri Olsa 			pr_err("failed to set field value %s\n", name);
118edbe9817SJiri Olsa 			goto err;
119edbe9817SJiri Olsa 		}
120edbe9817SJiri Olsa 	}
121edbe9817SJiri Olsa 
122edbe9817SJiri Olsa 	ret = bt_ctf_event_set_payload(event, name, field);
123edbe9817SJiri Olsa 	if (ret) {
124edbe9817SJiri Olsa 		pr_err("failed to set payload %s\n", name);
125edbe9817SJiri Olsa 		goto err;
126edbe9817SJiri Olsa 	}
127edbe9817SJiri Olsa 
128edbe9817SJiri Olsa 	pr2("  SET [%s = %" PRIu64 "]\n", name, val);
129edbe9817SJiri Olsa 
130edbe9817SJiri Olsa err:
131edbe9817SJiri Olsa 	bt_ctf_field_put(field);
132edbe9817SJiri Olsa 	return ret;
133edbe9817SJiri Olsa }
134edbe9817SJiri Olsa 
135edbe9817SJiri Olsa #define __FUNC_VALUE_SET(_name, _val_type)				\
136edbe9817SJiri Olsa static __maybe_unused int value_set_##_name(struct ctf_writer *cw,	\
137edbe9817SJiri Olsa 			     struct bt_ctf_event *event,		\
138edbe9817SJiri Olsa 			     const char *name,				\
139edbe9817SJiri Olsa 			     _val_type val)				\
140edbe9817SJiri Olsa {									\
141edbe9817SJiri Olsa 	struct bt_ctf_field_type *type = cw->data._name;		\
142edbe9817SJiri Olsa 	return value_set(type, event, name, (u64) val);			\
143edbe9817SJiri Olsa }
144edbe9817SJiri Olsa 
145edbe9817SJiri Olsa #define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
146edbe9817SJiri Olsa 
147edbe9817SJiri Olsa FUNC_VALUE_SET(s32)
148edbe9817SJiri Olsa FUNC_VALUE_SET(u32)
149edbe9817SJiri Olsa FUNC_VALUE_SET(s64)
150edbe9817SJiri Olsa FUNC_VALUE_SET(u64)
151edbe9817SJiri Olsa __FUNC_VALUE_SET(u64_hex, u64)
152edbe9817SJiri Olsa 
153069ee5c4SWang Nan static int string_set_value(struct bt_ctf_field *field, const char *string);
154069ee5c4SWang Nan static __maybe_unused int
155069ee5c4SWang Nan value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event,
156069ee5c4SWang Nan 		 const char *name, const char *string)
157069ee5c4SWang Nan {
158069ee5c4SWang Nan 	struct bt_ctf_field_type *type = cw->data.string;
159069ee5c4SWang Nan 	struct bt_ctf_field *field;
160069ee5c4SWang Nan 	int ret = 0;
161069ee5c4SWang Nan 
162069ee5c4SWang Nan 	field = bt_ctf_field_create(type);
163069ee5c4SWang Nan 	if (!field) {
164069ee5c4SWang Nan 		pr_err("failed to create a field %s\n", name);
165069ee5c4SWang Nan 		return -1;
166069ee5c4SWang Nan 	}
167069ee5c4SWang Nan 
168069ee5c4SWang Nan 	ret = string_set_value(field, string);
169069ee5c4SWang Nan 	if (ret) {
170069ee5c4SWang Nan 		pr_err("failed to set value %s\n", name);
171069ee5c4SWang Nan 		goto err_put_field;
172069ee5c4SWang Nan 	}
173069ee5c4SWang Nan 
174069ee5c4SWang Nan 	ret = bt_ctf_event_set_payload(event, name, field);
175069ee5c4SWang Nan 	if (ret)
176069ee5c4SWang Nan 		pr_err("failed to set payload %s\n", name);
177069ee5c4SWang Nan 
178069ee5c4SWang Nan err_put_field:
179069ee5c4SWang Nan 	bt_ctf_field_put(field);
180069ee5c4SWang Nan 	return ret;
181069ee5c4SWang Nan }
182069ee5c4SWang Nan 
18369364727SSebastian Andrzej Siewior static struct bt_ctf_field_type*
1842c92f982STzvetomir Stoyanov (VMware) get_tracepoint_field_type(struct ctf_writer *cw, struct tep_format_field *field)
18569364727SSebastian Andrzej Siewior {
18669364727SSebastian Andrzej Siewior 	unsigned long flags = field->flags;
18769364727SSebastian Andrzej Siewior 
188bb39ccb2STzvetomir Stoyanov (VMware) 	if (flags & TEP_FIELD_IS_STRING)
18969364727SSebastian Andrzej Siewior 		return cw->data.string;
19069364727SSebastian Andrzej Siewior 
191bb39ccb2STzvetomir Stoyanov (VMware) 	if (!(flags & TEP_FIELD_IS_SIGNED)) {
19269364727SSebastian Andrzej Siewior 		/* unsigned long are mostly pointers */
193bb39ccb2STzvetomir Stoyanov (VMware) 		if (flags & TEP_FIELD_IS_LONG || flags & TEP_FIELD_IS_POINTER)
19469364727SSebastian Andrzej Siewior 			return cw->data.u64_hex;
19569364727SSebastian Andrzej Siewior 	}
19669364727SSebastian Andrzej Siewior 
197bb39ccb2STzvetomir Stoyanov (VMware) 	if (flags & TEP_FIELD_IS_SIGNED) {
19869364727SSebastian Andrzej Siewior 		if (field->size == 8)
19969364727SSebastian Andrzej Siewior 			return cw->data.s64;
20069364727SSebastian Andrzej Siewior 		else
20169364727SSebastian Andrzej Siewior 			return cw->data.s32;
20269364727SSebastian Andrzej Siewior 	}
20369364727SSebastian Andrzej Siewior 
20469364727SSebastian Andrzej Siewior 	if (field->size == 8)
20569364727SSebastian Andrzej Siewior 		return cw->data.u64;
20669364727SSebastian Andrzej Siewior 	else
20769364727SSebastian Andrzej Siewior 		return cw->data.u32;
20869364727SSebastian Andrzej Siewior }
20969364727SSebastian Andrzej Siewior 
210d4ae4213SWang Nan static unsigned long long adjust_signedness(unsigned long long value_int, int size)
211d4ae4213SWang Nan {
212d4ae4213SWang Nan 	unsigned long long value_mask;
213d4ae4213SWang Nan 
214d4ae4213SWang Nan 	/*
215d4ae4213SWang Nan 	 * value_mask = (1 << (size * 8 - 1)) - 1.
216d4ae4213SWang Nan 	 * Directly set value_mask for code readers.
217d4ae4213SWang Nan 	 */
218d4ae4213SWang Nan 	switch (size) {
219d4ae4213SWang Nan 	case 1:
220d4ae4213SWang Nan 		value_mask = 0x7fULL;
221d4ae4213SWang Nan 		break;
222d4ae4213SWang Nan 	case 2:
223d4ae4213SWang Nan 		value_mask = 0x7fffULL;
224d4ae4213SWang Nan 		break;
225d4ae4213SWang Nan 	case 4:
226d4ae4213SWang Nan 		value_mask = 0x7fffffffULL;
227d4ae4213SWang Nan 		break;
228d4ae4213SWang Nan 	case 8:
229d4ae4213SWang Nan 		/*
230d4ae4213SWang Nan 		 * For 64 bit value, return it self. There is no need
231d4ae4213SWang Nan 		 * to fill high bit.
232d4ae4213SWang Nan 		 */
233d4ae4213SWang Nan 		/* Fall through */
234d4ae4213SWang Nan 	default:
235d4ae4213SWang Nan 		/* BUG! */
236d4ae4213SWang Nan 		return value_int;
237d4ae4213SWang Nan 	}
238d4ae4213SWang Nan 
239d4ae4213SWang Nan 	/* If it is a positive value, don't adjust. */
240d4ae4213SWang Nan 	if ((value_int & (~0ULL - value_mask)) == 0)
241d4ae4213SWang Nan 		return value_int;
242d4ae4213SWang Nan 
243d4ae4213SWang Nan 	/* Fill upper part of value_int with 1 to make it a negative long long. */
244d4ae4213SWang Nan 	return (value_int & value_mask) | ~value_mask;
245d4ae4213SWang Nan }
246d4ae4213SWang Nan 
2475ea5888bSWang Nan static int string_set_value(struct bt_ctf_field *field, const char *string)
2485ea5888bSWang Nan {
2495ea5888bSWang Nan 	char *buffer = NULL;
2505ea5888bSWang Nan 	size_t len = strlen(string), i, p;
2515ea5888bSWang Nan 	int err;
2525ea5888bSWang Nan 
2535ea5888bSWang Nan 	for (i = p = 0; i < len; i++, p++) {
2545ea5888bSWang Nan 		if (isprint(string[i])) {
2555ea5888bSWang Nan 			if (!buffer)
2565ea5888bSWang Nan 				continue;
2575ea5888bSWang Nan 			buffer[p] = string[i];
2585ea5888bSWang Nan 		} else {
2595ea5888bSWang Nan 			char numstr[5];
2605ea5888bSWang Nan 
2615ea5888bSWang Nan 			snprintf(numstr, sizeof(numstr), "\\x%02x",
2625ea5888bSWang Nan 				 (unsigned int)(string[i]) & 0xff);
2635ea5888bSWang Nan 
2645ea5888bSWang Nan 			if (!buffer) {
2655ea5888bSWang Nan 				buffer = zalloc(i + (len - i) * 4 + 2);
2665ea5888bSWang Nan 				if (!buffer) {
2675ea5888bSWang Nan 					pr_err("failed to set unprintable string '%s'\n", string);
2685ea5888bSWang Nan 					return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING");
2695ea5888bSWang Nan 				}
2705ea5888bSWang Nan 				if (i > 0)
2715ea5888bSWang Nan 					strncpy(buffer, string, i);
2725ea5888bSWang Nan 			}
27397acec7dSShawn Landden 			memcpy(buffer + p, numstr, 4);
2745ea5888bSWang Nan 			p += 3;
2755ea5888bSWang Nan 		}
2765ea5888bSWang Nan 	}
2775ea5888bSWang Nan 
2785ea5888bSWang Nan 	if (!buffer)
2795ea5888bSWang Nan 		return bt_ctf_field_string_set_value(field, string);
2805ea5888bSWang Nan 	err = bt_ctf_field_string_set_value(field, buffer);
2815ea5888bSWang Nan 	free(buffer);
2825ea5888bSWang Nan 	return err;
2835ea5888bSWang Nan }
2845ea5888bSWang Nan 
28569364727SSebastian Andrzej Siewior static int add_tracepoint_field_value(struct ctf_writer *cw,
28669364727SSebastian Andrzej Siewior 				      struct bt_ctf_event_class *event_class,
28769364727SSebastian Andrzej Siewior 				      struct bt_ctf_event *event,
28869364727SSebastian Andrzej Siewior 				      struct perf_sample *sample,
2892c92f982STzvetomir Stoyanov (VMware) 				      struct tep_format_field *fmtf)
29069364727SSebastian Andrzej Siewior {
29169364727SSebastian Andrzej Siewior 	struct bt_ctf_field_type *type;
29269364727SSebastian Andrzej Siewior 	struct bt_ctf_field *array_field;
29369364727SSebastian Andrzej Siewior 	struct bt_ctf_field *field;
29469364727SSebastian Andrzej Siewior 	const char *name = fmtf->name;
29569364727SSebastian Andrzej Siewior 	void *data = sample->raw_data;
29669364727SSebastian Andrzej Siewior 	unsigned long flags = fmtf->flags;
29769364727SSebastian Andrzej Siewior 	unsigned int n_items;
29869364727SSebastian Andrzej Siewior 	unsigned int i;
29969364727SSebastian Andrzej Siewior 	unsigned int offset;
30069364727SSebastian Andrzej Siewior 	unsigned int len;
30169364727SSebastian Andrzej Siewior 	int ret;
30269364727SSebastian Andrzej Siewior 
303e0a7cce5SWang Nan 	name = fmtf->alias;
30469364727SSebastian Andrzej Siewior 	offset = fmtf->offset;
30569364727SSebastian Andrzej Siewior 	len = fmtf->size;
306bb39ccb2STzvetomir Stoyanov (VMware) 	if (flags & TEP_FIELD_IS_STRING)
307bb39ccb2STzvetomir Stoyanov (VMware) 		flags &= ~TEP_FIELD_IS_ARRAY;
30869364727SSebastian Andrzej Siewior 
309bb39ccb2STzvetomir Stoyanov (VMware) 	if (flags & TEP_FIELD_IS_DYNAMIC) {
31069364727SSebastian Andrzej Siewior 		unsigned long long tmp_val;
31169364727SSebastian Andrzej Siewior 
31269769ce1STzvetomir Stoyanov 		tmp_val = tep_read_number(fmtf->event->tep,
31369364727SSebastian Andrzej Siewior 					  data + offset, len);
31469364727SSebastian Andrzej Siewior 		offset = tmp_val;
31569364727SSebastian Andrzej Siewior 		len = offset >> 16;
31669364727SSebastian Andrzej Siewior 		offset &= 0xffff;
31769364727SSebastian Andrzej Siewior 	}
31869364727SSebastian Andrzej Siewior 
319bb39ccb2STzvetomir Stoyanov (VMware) 	if (flags & TEP_FIELD_IS_ARRAY) {
32069364727SSebastian Andrzej Siewior 
32169364727SSebastian Andrzej Siewior 		type = bt_ctf_event_class_get_field_by_name(
32269364727SSebastian Andrzej Siewior 				event_class, name);
32369364727SSebastian Andrzej Siewior 		array_field = bt_ctf_field_create(type);
32469364727SSebastian Andrzej Siewior 		bt_ctf_field_type_put(type);
32569364727SSebastian Andrzej Siewior 		if (!array_field) {
32669364727SSebastian Andrzej Siewior 			pr_err("Failed to create array type %s\n", name);
32769364727SSebastian Andrzej Siewior 			return -1;
32869364727SSebastian Andrzej Siewior 		}
32969364727SSebastian Andrzej Siewior 
33069364727SSebastian Andrzej Siewior 		len = fmtf->size / fmtf->arraylen;
33169364727SSebastian Andrzej Siewior 		n_items = fmtf->arraylen;
33269364727SSebastian Andrzej Siewior 	} else {
33369364727SSebastian Andrzej Siewior 		n_items = 1;
33469364727SSebastian Andrzej Siewior 		array_field = NULL;
33569364727SSebastian Andrzej Siewior 	}
33669364727SSebastian Andrzej Siewior 
33769364727SSebastian Andrzej Siewior 	type = get_tracepoint_field_type(cw, fmtf);
33869364727SSebastian Andrzej Siewior 
33969364727SSebastian Andrzej Siewior 	for (i = 0; i < n_items; i++) {
340bb39ccb2STzvetomir Stoyanov (VMware) 		if (flags & TEP_FIELD_IS_ARRAY)
34169364727SSebastian Andrzej Siewior 			field = bt_ctf_field_array_get_field(array_field, i);
34269364727SSebastian Andrzej Siewior 		else
34369364727SSebastian Andrzej Siewior 			field = bt_ctf_field_create(type);
34469364727SSebastian Andrzej Siewior 
34569364727SSebastian Andrzej Siewior 		if (!field) {
34669364727SSebastian Andrzej Siewior 			pr_err("failed to create a field %s\n", name);
34769364727SSebastian Andrzej Siewior 			return -1;
34869364727SSebastian Andrzej Siewior 		}
34969364727SSebastian Andrzej Siewior 
350bb39ccb2STzvetomir Stoyanov (VMware) 		if (flags & TEP_FIELD_IS_STRING)
3515ea5888bSWang Nan 			ret = string_set_value(field, data + offset + i * len);
352d4ae4213SWang Nan 		else {
353d4ae4213SWang Nan 			unsigned long long value_int;
354d4ae4213SWang Nan 
35559c1baeeSTzvetomir Stoyanov (VMware) 			value_int = tep_read_number(
35669769ce1STzvetomir Stoyanov 					fmtf->event->tep,
357d4ae4213SWang Nan 					data + offset + i * len, len);
358d4ae4213SWang Nan 
359bb39ccb2STzvetomir Stoyanov (VMware) 			if (!(flags & TEP_FIELD_IS_SIGNED))
36069364727SSebastian Andrzej Siewior 				ret = bt_ctf_field_unsigned_integer_set_value(
36169364727SSebastian Andrzej Siewior 						field, value_int);
36269364727SSebastian Andrzej Siewior 			else
36369364727SSebastian Andrzej Siewior 				ret = bt_ctf_field_signed_integer_set_value(
364d4ae4213SWang Nan 						field, adjust_signedness(value_int, len));
365d4ae4213SWang Nan 		}
366d4ae4213SWang Nan 
36769364727SSebastian Andrzej Siewior 		if (ret) {
36869364727SSebastian Andrzej Siewior 			pr_err("failed to set file value %s\n", name);
36969364727SSebastian Andrzej Siewior 			goto err_put_field;
37069364727SSebastian Andrzej Siewior 		}
371bb39ccb2STzvetomir Stoyanov (VMware) 		if (!(flags & TEP_FIELD_IS_ARRAY)) {
37269364727SSebastian Andrzej Siewior 			ret = bt_ctf_event_set_payload(event, name, field);
37369364727SSebastian Andrzej Siewior 			if (ret) {
37469364727SSebastian Andrzej Siewior 				pr_err("failed to set payload %s\n", name);
37569364727SSebastian Andrzej Siewior 				goto err_put_field;
37669364727SSebastian Andrzej Siewior 			}
37769364727SSebastian Andrzej Siewior 		}
37869364727SSebastian Andrzej Siewior 		bt_ctf_field_put(field);
37969364727SSebastian Andrzej Siewior 	}
380bb39ccb2STzvetomir Stoyanov (VMware) 	if (flags & TEP_FIELD_IS_ARRAY) {
38169364727SSebastian Andrzej Siewior 		ret = bt_ctf_event_set_payload(event, name, array_field);
38269364727SSebastian Andrzej Siewior 		if (ret) {
38369364727SSebastian Andrzej Siewior 			pr_err("Failed add payload array %s\n", name);
38469364727SSebastian Andrzej Siewior 			return -1;
38569364727SSebastian Andrzej Siewior 		}
38669364727SSebastian Andrzej Siewior 		bt_ctf_field_put(array_field);
38769364727SSebastian Andrzej Siewior 	}
38869364727SSebastian Andrzej Siewior 	return 0;
38969364727SSebastian Andrzej Siewior 
39069364727SSebastian Andrzej Siewior err_put_field:
39169364727SSebastian Andrzej Siewior 	bt_ctf_field_put(field);
39269364727SSebastian Andrzej Siewior 	return -1;
39369364727SSebastian Andrzej Siewior }
39469364727SSebastian Andrzej Siewior 
39569364727SSebastian Andrzej Siewior static int add_tracepoint_fields_values(struct ctf_writer *cw,
39669364727SSebastian Andrzej Siewior 					struct bt_ctf_event_class *event_class,
39769364727SSebastian Andrzej Siewior 					struct bt_ctf_event *event,
3982c92f982STzvetomir Stoyanov (VMware) 					struct tep_format_field *fields,
39969364727SSebastian Andrzej Siewior 					struct perf_sample *sample)
40069364727SSebastian Andrzej Siewior {
4012c92f982STzvetomir Stoyanov (VMware) 	struct tep_format_field *field;
40269364727SSebastian Andrzej Siewior 	int ret;
40369364727SSebastian Andrzej Siewior 
40469364727SSebastian Andrzej Siewior 	for (field = fields; field; field = field->next) {
40569364727SSebastian Andrzej Siewior 		ret = add_tracepoint_field_value(cw, event_class, event, sample,
40669364727SSebastian Andrzej Siewior 				field);
40769364727SSebastian Andrzej Siewior 		if (ret)
40869364727SSebastian Andrzej Siewior 			return -1;
40969364727SSebastian Andrzej Siewior 	}
41069364727SSebastian Andrzej Siewior 	return 0;
41169364727SSebastian Andrzej Siewior }
41269364727SSebastian Andrzej Siewior 
41369364727SSebastian Andrzej Siewior static int add_tracepoint_values(struct ctf_writer *cw,
41469364727SSebastian Andrzej Siewior 				 struct bt_ctf_event_class *event_class,
41569364727SSebastian Andrzej Siewior 				 struct bt_ctf_event *event,
41632dcd021SJiri Olsa 				 struct evsel *evsel,
41769364727SSebastian Andrzej Siewior 				 struct perf_sample *sample)
41869364727SSebastian Andrzej Siewior {
4192c92f982STzvetomir Stoyanov (VMware) 	struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
4202c92f982STzvetomir Stoyanov (VMware) 	struct tep_format_field *fields        = evsel->tp_format->format.fields;
42169364727SSebastian Andrzej Siewior 	int ret;
42269364727SSebastian Andrzej Siewior 
42369364727SSebastian Andrzej Siewior 	ret = add_tracepoint_fields_values(cw, event_class, event,
42469364727SSebastian Andrzej Siewior 					   common_fields, sample);
42569364727SSebastian Andrzej Siewior 	if (!ret)
42669364727SSebastian Andrzej Siewior 		ret = add_tracepoint_fields_values(cw, event_class, event,
42769364727SSebastian Andrzej Siewior 						   fields, sample);
42869364727SSebastian Andrzej Siewior 
42969364727SSebastian Andrzej Siewior 	return ret;
43069364727SSebastian Andrzej Siewior }
43169364727SSebastian Andrzej Siewior 
4326122d57eSWang Nan static int
4336122d57eSWang Nan add_bpf_output_values(struct bt_ctf_event_class *event_class,
4346122d57eSWang Nan 		      struct bt_ctf_event *event,
4356122d57eSWang Nan 		      struct perf_sample *sample)
4366122d57eSWang Nan {
4376122d57eSWang Nan 	struct bt_ctf_field_type *len_type, *seq_type;
4386122d57eSWang Nan 	struct bt_ctf_field *len_field, *seq_field;
4396122d57eSWang Nan 	unsigned int raw_size = sample->raw_size;
4406122d57eSWang Nan 	unsigned int nr_elements = raw_size / sizeof(u32);
4416122d57eSWang Nan 	unsigned int i;
4426122d57eSWang Nan 	int ret;
4436122d57eSWang Nan 
4446122d57eSWang Nan 	if (nr_elements * sizeof(u32) != raw_size)
445f2c8852eSWang Nan 		pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n",
4466122d57eSWang Nan 			   raw_size, nr_elements * sizeof(u32) - raw_size);
4476122d57eSWang Nan 
4486122d57eSWang Nan 	len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len");
4496122d57eSWang Nan 	len_field = bt_ctf_field_create(len_type);
4506122d57eSWang Nan 	if (!len_field) {
4516122d57eSWang Nan 		pr_err("failed to create 'raw_len' for bpf output event\n");
4526122d57eSWang Nan 		ret = -1;
4536122d57eSWang Nan 		goto put_len_type;
4546122d57eSWang Nan 	}
4556122d57eSWang Nan 
4566122d57eSWang Nan 	ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
4576122d57eSWang Nan 	if (ret) {
4586122d57eSWang Nan 		pr_err("failed to set field value for raw_len\n");
4596122d57eSWang Nan 		goto put_len_field;
4606122d57eSWang Nan 	}
4616122d57eSWang Nan 	ret = bt_ctf_event_set_payload(event, "raw_len", len_field);
4626122d57eSWang Nan 	if (ret) {
4636122d57eSWang Nan 		pr_err("failed to set payload to raw_len\n");
4646122d57eSWang Nan 		goto put_len_field;
4656122d57eSWang Nan 	}
4666122d57eSWang Nan 
4676122d57eSWang Nan 	seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data");
4686122d57eSWang Nan 	seq_field = bt_ctf_field_create(seq_type);
4696122d57eSWang Nan 	if (!seq_field) {
4706122d57eSWang Nan 		pr_err("failed to create 'raw_data' for bpf output event\n");
4716122d57eSWang Nan 		ret = -1;
4726122d57eSWang Nan 		goto put_seq_type;
4736122d57eSWang Nan 	}
4746122d57eSWang Nan 
4756122d57eSWang Nan 	ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
4766122d57eSWang Nan 	if (ret) {
4776122d57eSWang Nan 		pr_err("failed to set length of 'raw_data'\n");
4786122d57eSWang Nan 		goto put_seq_field;
4796122d57eSWang Nan 	}
4806122d57eSWang Nan 
4816122d57eSWang Nan 	for (i = 0; i < nr_elements; i++) {
4826122d57eSWang Nan 		struct bt_ctf_field *elem_field =
4836122d57eSWang Nan 			bt_ctf_field_sequence_get_field(seq_field, i);
4846122d57eSWang Nan 
4856122d57eSWang Nan 		ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
4866122d57eSWang Nan 				((u32 *)(sample->raw_data))[i]);
4876122d57eSWang Nan 
4886122d57eSWang Nan 		bt_ctf_field_put(elem_field);
4896122d57eSWang Nan 		if (ret) {
4906122d57eSWang Nan 			pr_err("failed to set raw_data[%d]\n", i);
4916122d57eSWang Nan 			goto put_seq_field;
4926122d57eSWang Nan 		}
4936122d57eSWang Nan 	}
4946122d57eSWang Nan 
4956122d57eSWang Nan 	ret = bt_ctf_event_set_payload(event, "raw_data", seq_field);
4966122d57eSWang Nan 	if (ret)
4976122d57eSWang Nan 		pr_err("failed to set payload for raw_data\n");
4986122d57eSWang Nan 
4996122d57eSWang Nan put_seq_field:
5006122d57eSWang Nan 	bt_ctf_field_put(seq_field);
5016122d57eSWang Nan put_seq_type:
5026122d57eSWang Nan 	bt_ctf_field_type_put(seq_type);
5036122d57eSWang Nan put_len_field:
5046122d57eSWang Nan 	bt_ctf_field_put(len_field);
5056122d57eSWang Nan put_len_type:
5066122d57eSWang Nan 	bt_ctf_field_type_put(len_type);
5076122d57eSWang Nan 	return ret;
5086122d57eSWang Nan }
5096122d57eSWang Nan 
510a3073c8eSGeneviève Bastien static int
511a3073c8eSGeneviève Bastien add_callchain_output_values(struct bt_ctf_event_class *event_class,
512a3073c8eSGeneviève Bastien 		      struct bt_ctf_event *event,
513a3073c8eSGeneviève Bastien 		      struct ip_callchain *callchain)
514a3073c8eSGeneviève Bastien {
515a3073c8eSGeneviève Bastien 	struct bt_ctf_field_type *len_type, *seq_type;
516a3073c8eSGeneviève Bastien 	struct bt_ctf_field *len_field, *seq_field;
517a3073c8eSGeneviève Bastien 	unsigned int nr_elements = callchain->nr;
518a3073c8eSGeneviève Bastien 	unsigned int i;
519a3073c8eSGeneviève Bastien 	int ret;
520a3073c8eSGeneviève Bastien 
521a3073c8eSGeneviève Bastien 	len_type = bt_ctf_event_class_get_field_by_name(
522a3073c8eSGeneviève Bastien 			event_class, "perf_callchain_size");
523a3073c8eSGeneviève Bastien 	len_field = bt_ctf_field_create(len_type);
524a3073c8eSGeneviève Bastien 	if (!len_field) {
525a3073c8eSGeneviève Bastien 		pr_err("failed to create 'perf_callchain_size' for callchain output event\n");
526a3073c8eSGeneviève Bastien 		ret = -1;
527a3073c8eSGeneviève Bastien 		goto put_len_type;
528a3073c8eSGeneviève Bastien 	}
529a3073c8eSGeneviève Bastien 
530a3073c8eSGeneviève Bastien 	ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
531a3073c8eSGeneviève Bastien 	if (ret) {
532a3073c8eSGeneviève Bastien 		pr_err("failed to set field value for perf_callchain_size\n");
533a3073c8eSGeneviève Bastien 		goto put_len_field;
534a3073c8eSGeneviève Bastien 	}
535a3073c8eSGeneviève Bastien 	ret = bt_ctf_event_set_payload(event, "perf_callchain_size", len_field);
536a3073c8eSGeneviève Bastien 	if (ret) {
537a3073c8eSGeneviève Bastien 		pr_err("failed to set payload to perf_callchain_size\n");
538a3073c8eSGeneviève Bastien 		goto put_len_field;
539a3073c8eSGeneviève Bastien 	}
540a3073c8eSGeneviève Bastien 
541a3073c8eSGeneviève Bastien 	seq_type = bt_ctf_event_class_get_field_by_name(
542a3073c8eSGeneviève Bastien 			event_class, "perf_callchain");
543a3073c8eSGeneviève Bastien 	seq_field = bt_ctf_field_create(seq_type);
544a3073c8eSGeneviève Bastien 	if (!seq_field) {
545a3073c8eSGeneviève Bastien 		pr_err("failed to create 'perf_callchain' for callchain output event\n");
546a3073c8eSGeneviève Bastien 		ret = -1;
547a3073c8eSGeneviève Bastien 		goto put_seq_type;
548a3073c8eSGeneviève Bastien 	}
549a3073c8eSGeneviève Bastien 
550a3073c8eSGeneviève Bastien 	ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
551a3073c8eSGeneviève Bastien 	if (ret) {
552a3073c8eSGeneviève Bastien 		pr_err("failed to set length of 'perf_callchain'\n");
553a3073c8eSGeneviève Bastien 		goto put_seq_field;
554a3073c8eSGeneviève Bastien 	}
555a3073c8eSGeneviève Bastien 
556a3073c8eSGeneviève Bastien 	for (i = 0; i < nr_elements; i++) {
557a3073c8eSGeneviève Bastien 		struct bt_ctf_field *elem_field =
558a3073c8eSGeneviève Bastien 			bt_ctf_field_sequence_get_field(seq_field, i);
559a3073c8eSGeneviève Bastien 
560a3073c8eSGeneviève Bastien 		ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
561a3073c8eSGeneviève Bastien 				((u64 *)(callchain->ips))[i]);
562a3073c8eSGeneviève Bastien 
563a3073c8eSGeneviève Bastien 		bt_ctf_field_put(elem_field);
564a3073c8eSGeneviève Bastien 		if (ret) {
565a3073c8eSGeneviève Bastien 			pr_err("failed to set callchain[%d]\n", i);
566a3073c8eSGeneviève Bastien 			goto put_seq_field;
567a3073c8eSGeneviève Bastien 		}
568a3073c8eSGeneviève Bastien 	}
569a3073c8eSGeneviève Bastien 
570a3073c8eSGeneviève Bastien 	ret = bt_ctf_event_set_payload(event, "perf_callchain", seq_field);
571a3073c8eSGeneviève Bastien 	if (ret)
572a3073c8eSGeneviève Bastien 		pr_err("failed to set payload for raw_data\n");
573a3073c8eSGeneviève Bastien 
574a3073c8eSGeneviève Bastien put_seq_field:
575a3073c8eSGeneviève Bastien 	bt_ctf_field_put(seq_field);
576a3073c8eSGeneviève Bastien put_seq_type:
577a3073c8eSGeneviève Bastien 	bt_ctf_field_type_put(seq_type);
578a3073c8eSGeneviève Bastien put_len_field:
579a3073c8eSGeneviève Bastien 	bt_ctf_field_put(len_field);
580a3073c8eSGeneviève Bastien put_len_type:
581a3073c8eSGeneviève Bastien 	bt_ctf_field_type_put(len_type);
582a3073c8eSGeneviève Bastien 	return ret;
583a3073c8eSGeneviève Bastien }
584a3073c8eSGeneviève Bastien 
585edbe9817SJiri Olsa static int add_generic_values(struct ctf_writer *cw,
586edbe9817SJiri Olsa 			      struct bt_ctf_event *event,
58732dcd021SJiri Olsa 			      struct evsel *evsel,
588edbe9817SJiri Olsa 			      struct perf_sample *sample)
589edbe9817SJiri Olsa {
590edbe9817SJiri Olsa 	u64 type = evsel->attr.sample_type;
591edbe9817SJiri Olsa 	int ret;
592edbe9817SJiri Olsa 
593edbe9817SJiri Olsa 	/*
594edbe9817SJiri Olsa 	 * missing:
595edbe9817SJiri Olsa 	 *   PERF_SAMPLE_TIME         - not needed as we have it in
596edbe9817SJiri Olsa 	 *                              ctf event header
597edbe9817SJiri Olsa 	 *   PERF_SAMPLE_READ         - TODO
598edbe9817SJiri Olsa 	 *   PERF_SAMPLE_RAW          - tracepoint fields are handled separately
599edbe9817SJiri Olsa 	 *   PERF_SAMPLE_BRANCH_STACK - TODO
600edbe9817SJiri Olsa 	 *   PERF_SAMPLE_REGS_USER    - TODO
601edbe9817SJiri Olsa 	 *   PERF_SAMPLE_STACK_USER   - TODO
602edbe9817SJiri Olsa 	 */
603edbe9817SJiri Olsa 
604edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_IP) {
60554cf776aSSebastian Andrzej Siewior 		ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
606edbe9817SJiri Olsa 		if (ret)
607edbe9817SJiri Olsa 			return -1;
608edbe9817SJiri Olsa 	}
609edbe9817SJiri Olsa 
610edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_TID) {
61154cf776aSSebastian Andrzej Siewior 		ret = value_set_s32(cw, event, "perf_tid", sample->tid);
612edbe9817SJiri Olsa 		if (ret)
613edbe9817SJiri Olsa 			return -1;
614edbe9817SJiri Olsa 
61554cf776aSSebastian Andrzej Siewior 		ret = value_set_s32(cw, event, "perf_pid", sample->pid);
616edbe9817SJiri Olsa 		if (ret)
617edbe9817SJiri Olsa 			return -1;
618edbe9817SJiri Olsa 	}
619edbe9817SJiri Olsa 
620edbe9817SJiri Olsa 	if ((type & PERF_SAMPLE_ID) ||
621edbe9817SJiri Olsa 	    (type & PERF_SAMPLE_IDENTIFIER)) {
62254cf776aSSebastian Andrzej Siewior 		ret = value_set_u64(cw, event, "perf_id", sample->id);
623edbe9817SJiri Olsa 		if (ret)
624edbe9817SJiri Olsa 			return -1;
625edbe9817SJiri Olsa 	}
626edbe9817SJiri Olsa 
627edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_STREAM_ID) {
62854cf776aSSebastian Andrzej Siewior 		ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
629edbe9817SJiri Olsa 		if (ret)
630edbe9817SJiri Olsa 			return -1;
631edbe9817SJiri Olsa 	}
632edbe9817SJiri Olsa 
633edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_PERIOD) {
63454cf776aSSebastian Andrzej Siewior 		ret = value_set_u64(cw, event, "perf_period", sample->period);
635edbe9817SJiri Olsa 		if (ret)
636edbe9817SJiri Olsa 			return -1;
637edbe9817SJiri Olsa 	}
638edbe9817SJiri Olsa 
639edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_WEIGHT) {
64054cf776aSSebastian Andrzej Siewior 		ret = value_set_u64(cw, event, "perf_weight", sample->weight);
641edbe9817SJiri Olsa 		if (ret)
642edbe9817SJiri Olsa 			return -1;
643edbe9817SJiri Olsa 	}
644edbe9817SJiri Olsa 
645edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_DATA_SRC) {
64654cf776aSSebastian Andrzej Siewior 		ret = value_set_u64(cw, event, "perf_data_src",
64754cf776aSSebastian Andrzej Siewior 				sample->data_src);
648edbe9817SJiri Olsa 		if (ret)
649edbe9817SJiri Olsa 			return -1;
650edbe9817SJiri Olsa 	}
651edbe9817SJiri Olsa 
652edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_TRANSACTION) {
65354cf776aSSebastian Andrzej Siewior 		ret = value_set_u64(cw, event, "perf_transaction",
65454cf776aSSebastian Andrzej Siewior 				sample->transaction);
655edbe9817SJiri Olsa 		if (ret)
656edbe9817SJiri Olsa 			return -1;
657edbe9817SJiri Olsa 	}
658edbe9817SJiri Olsa 
659edbe9817SJiri Olsa 	return 0;
660edbe9817SJiri Olsa }
661edbe9817SJiri Olsa 
66290e129ffSSebastian Andrzej Siewior static int ctf_stream__flush(struct ctf_stream *cs)
66390e129ffSSebastian Andrzej Siewior {
66490e129ffSSebastian Andrzej Siewior 	int err = 0;
66590e129ffSSebastian Andrzej Siewior 
66690e129ffSSebastian Andrzej Siewior 	if (cs) {
66790e129ffSSebastian Andrzej Siewior 		err = bt_ctf_stream_flush(cs->stream);
66890e129ffSSebastian Andrzej Siewior 		if (err)
66990e129ffSSebastian Andrzej Siewior 			pr_err("CTF stream %d flush failed\n", cs->cpu);
67090e129ffSSebastian Andrzej Siewior 
67189e5fa88SJiri Olsa 		pr("Flush stream for cpu %d (%u samples)\n",
67289e5fa88SJiri Olsa 		   cs->cpu, cs->count);
67389e5fa88SJiri Olsa 
67489e5fa88SJiri Olsa 		cs->count = 0;
67590e129ffSSebastian Andrzej Siewior 	}
67690e129ffSSebastian Andrzej Siewior 
67790e129ffSSebastian Andrzej Siewior 	return err;
67890e129ffSSebastian Andrzej Siewior }
67990e129ffSSebastian Andrzej Siewior 
68090e129ffSSebastian Andrzej Siewior static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
68190e129ffSSebastian Andrzej Siewior {
68290e129ffSSebastian Andrzej Siewior 	struct ctf_stream *cs;
68390e129ffSSebastian Andrzej Siewior 	struct bt_ctf_field *pkt_ctx   = NULL;
68490e129ffSSebastian Andrzej Siewior 	struct bt_ctf_field *cpu_field = NULL;
68590e129ffSSebastian Andrzej Siewior 	struct bt_ctf_stream *stream   = NULL;
68690e129ffSSebastian Andrzej Siewior 	int ret;
68790e129ffSSebastian Andrzej Siewior 
68890e129ffSSebastian Andrzej Siewior 	cs = zalloc(sizeof(*cs));
68990e129ffSSebastian Andrzej Siewior 	if (!cs) {
69090e129ffSSebastian Andrzej Siewior 		pr_err("Failed to allocate ctf stream\n");
69190e129ffSSebastian Andrzej Siewior 		return NULL;
69290e129ffSSebastian Andrzej Siewior 	}
69390e129ffSSebastian Andrzej Siewior 
69490e129ffSSebastian Andrzej Siewior 	stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
69590e129ffSSebastian Andrzej Siewior 	if (!stream) {
69690e129ffSSebastian Andrzej Siewior 		pr_err("Failed to create CTF stream\n");
69790e129ffSSebastian Andrzej Siewior 		goto out;
69890e129ffSSebastian Andrzej Siewior 	}
69990e129ffSSebastian Andrzej Siewior 
70090e129ffSSebastian Andrzej Siewior 	pkt_ctx = bt_ctf_stream_get_packet_context(stream);
70190e129ffSSebastian Andrzej Siewior 	if (!pkt_ctx) {
70290e129ffSSebastian Andrzej Siewior 		pr_err("Failed to obtain packet context\n");
70390e129ffSSebastian Andrzej Siewior 		goto out;
70490e129ffSSebastian Andrzej Siewior 	}
70590e129ffSSebastian Andrzej Siewior 
70690e129ffSSebastian Andrzej Siewior 	cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
70790e129ffSSebastian Andrzej Siewior 	bt_ctf_field_put(pkt_ctx);
70890e129ffSSebastian Andrzej Siewior 	if (!cpu_field) {
70990e129ffSSebastian Andrzej Siewior 		pr_err("Failed to obtain cpu field\n");
71090e129ffSSebastian Andrzej Siewior 		goto out;
71190e129ffSSebastian Andrzej Siewior 	}
71290e129ffSSebastian Andrzej Siewior 
71390e129ffSSebastian Andrzej Siewior 	ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
71490e129ffSSebastian Andrzej Siewior 	if (ret) {
71590e129ffSSebastian Andrzej Siewior 		pr_err("Failed to update CPU number\n");
71690e129ffSSebastian Andrzej Siewior 		goto out;
71790e129ffSSebastian Andrzej Siewior 	}
71890e129ffSSebastian Andrzej Siewior 
71990e129ffSSebastian Andrzej Siewior 	bt_ctf_field_put(cpu_field);
72090e129ffSSebastian Andrzej Siewior 
72190e129ffSSebastian Andrzej Siewior 	cs->cpu    = cpu;
72290e129ffSSebastian Andrzej Siewior 	cs->stream = stream;
72390e129ffSSebastian Andrzej Siewior 	return cs;
72490e129ffSSebastian Andrzej Siewior 
72590e129ffSSebastian Andrzej Siewior out:
72690e129ffSSebastian Andrzej Siewior 	if (cpu_field)
72790e129ffSSebastian Andrzej Siewior 		bt_ctf_field_put(cpu_field);
72890e129ffSSebastian Andrzej Siewior 	if (stream)
72990e129ffSSebastian Andrzej Siewior 		bt_ctf_stream_put(stream);
73090e129ffSSebastian Andrzej Siewior 
73190e129ffSSebastian Andrzej Siewior 	free(cs);
73290e129ffSSebastian Andrzej Siewior 	return NULL;
73390e129ffSSebastian Andrzej Siewior }
73490e129ffSSebastian Andrzej Siewior 
73590e129ffSSebastian Andrzej Siewior static void ctf_stream__delete(struct ctf_stream *cs)
73690e129ffSSebastian Andrzej Siewior {
73790e129ffSSebastian Andrzej Siewior 	if (cs) {
73890e129ffSSebastian Andrzej Siewior 		bt_ctf_stream_put(cs->stream);
73990e129ffSSebastian Andrzej Siewior 		free(cs);
74090e129ffSSebastian Andrzej Siewior 	}
74190e129ffSSebastian Andrzej Siewior }
74290e129ffSSebastian Andrzej Siewior 
74390e129ffSSebastian Andrzej Siewior static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
74490e129ffSSebastian Andrzej Siewior {
74590e129ffSSebastian Andrzej Siewior 	struct ctf_stream *cs = cw->stream[cpu];
74690e129ffSSebastian Andrzej Siewior 
74790e129ffSSebastian Andrzej Siewior 	if (!cs) {
74890e129ffSSebastian Andrzej Siewior 		cs = ctf_stream__create(cw, cpu);
74990e129ffSSebastian Andrzej Siewior 		cw->stream[cpu] = cs;
75090e129ffSSebastian Andrzej Siewior 	}
75190e129ffSSebastian Andrzej Siewior 
75290e129ffSSebastian Andrzej Siewior 	return cs;
75390e129ffSSebastian Andrzej Siewior }
75490e129ffSSebastian Andrzej Siewior 
75590e129ffSSebastian Andrzej Siewior static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
75632dcd021SJiri Olsa 			  struct evsel *evsel)
75790e129ffSSebastian Andrzej Siewior {
75890e129ffSSebastian Andrzej Siewior 	int cpu = 0;
75990e129ffSSebastian Andrzej Siewior 
76090e129ffSSebastian Andrzej Siewior 	if (evsel->attr.sample_type & PERF_SAMPLE_CPU)
76190e129ffSSebastian Andrzej Siewior 		cpu = sample->cpu;
76290e129ffSSebastian Andrzej Siewior 
76390e129ffSSebastian Andrzej Siewior 	if (cpu > cw->stream_cnt) {
76490e129ffSSebastian Andrzej Siewior 		pr_err("Event was recorded for CPU %d, limit is at %d.\n",
76590e129ffSSebastian Andrzej Siewior 			cpu, cw->stream_cnt);
76690e129ffSSebastian Andrzej Siewior 		cpu = 0;
76790e129ffSSebastian Andrzej Siewior 	}
76890e129ffSSebastian Andrzej Siewior 
76990e129ffSSebastian Andrzej Siewior 	return cpu;
77090e129ffSSebastian Andrzej Siewior }
77190e129ffSSebastian Andrzej Siewior 
77289e5fa88SJiri Olsa #define STREAM_FLUSH_COUNT 100000
77389e5fa88SJiri Olsa 
77489e5fa88SJiri Olsa /*
77589e5fa88SJiri Olsa  * Currently we have no other way to determine the
77689e5fa88SJiri Olsa  * time for the stream flush other than keep track
77789e5fa88SJiri Olsa  * of the number of events and check it against
77889e5fa88SJiri Olsa  * threshold.
77989e5fa88SJiri Olsa  */
78089e5fa88SJiri Olsa static bool is_flush_needed(struct ctf_stream *cs)
78189e5fa88SJiri Olsa {
78289e5fa88SJiri Olsa 	return cs->count >= STREAM_FLUSH_COUNT;
78389e5fa88SJiri Olsa }
78489e5fa88SJiri Olsa 
785edbe9817SJiri Olsa static int process_sample_event(struct perf_tool *tool,
786b8f8eb84SArnaldo Carvalho de Melo 				union perf_event *_event,
787edbe9817SJiri Olsa 				struct perf_sample *sample,
78832dcd021SJiri Olsa 				struct evsel *evsel,
789edbe9817SJiri Olsa 				struct machine *machine __maybe_unused)
790edbe9817SJiri Olsa {
791edbe9817SJiri Olsa 	struct convert *c = container_of(tool, struct convert, tool);
792edbe9817SJiri Olsa 	struct evsel_priv *priv = evsel->priv;
793edbe9817SJiri Olsa 	struct ctf_writer *cw = &c->writer;
79490e129ffSSebastian Andrzej Siewior 	struct ctf_stream *cs;
795edbe9817SJiri Olsa 	struct bt_ctf_event_class *event_class;
796edbe9817SJiri Olsa 	struct bt_ctf_event *event;
797edbe9817SJiri Olsa 	int ret;
798a3073c8eSGeneviève Bastien 	unsigned long type = evsel->attr.sample_type;
799edbe9817SJiri Olsa 
800edbe9817SJiri Olsa 	if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
801edbe9817SJiri Olsa 		return 0;
802edbe9817SJiri Olsa 
803edbe9817SJiri Olsa 	event_class = priv->event_class;
804edbe9817SJiri Olsa 
805edbe9817SJiri Olsa 	/* update stats */
806edbe9817SJiri Olsa 	c->events_count++;
807edbe9817SJiri Olsa 	c->events_size += _event->header.size;
808edbe9817SJiri Olsa 
809edbe9817SJiri Olsa 	pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
810edbe9817SJiri Olsa 
811edbe9817SJiri Olsa 	event = bt_ctf_event_create(event_class);
812edbe9817SJiri Olsa 	if (!event) {
813edbe9817SJiri Olsa 		pr_err("Failed to create an CTF event\n");
814edbe9817SJiri Olsa 		return -1;
815edbe9817SJiri Olsa 	}
816edbe9817SJiri Olsa 
817edbe9817SJiri Olsa 	bt_ctf_clock_set_time(cw->clock, sample->time);
818edbe9817SJiri Olsa 
819edbe9817SJiri Olsa 	ret = add_generic_values(cw, event, evsel, sample);
820edbe9817SJiri Olsa 	if (ret)
821edbe9817SJiri Olsa 		return -1;
822edbe9817SJiri Olsa 
82369364727SSebastian Andrzej Siewior 	if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
82469364727SSebastian Andrzej Siewior 		ret = add_tracepoint_values(cw, event_class, event,
82569364727SSebastian Andrzej Siewior 					    evsel, sample);
82669364727SSebastian Andrzej Siewior 		if (ret)
82769364727SSebastian Andrzej Siewior 			return -1;
82869364727SSebastian Andrzej Siewior 	}
82969364727SSebastian Andrzej Siewior 
830a3073c8eSGeneviève Bastien 	if (type & PERF_SAMPLE_CALLCHAIN) {
831a3073c8eSGeneviève Bastien 		ret = add_callchain_output_values(event_class,
832a3073c8eSGeneviève Bastien 				event, sample->callchain);
833a3073c8eSGeneviève Bastien 		if (ret)
834a3073c8eSGeneviève Bastien 			return -1;
835a3073c8eSGeneviève Bastien 	}
836a3073c8eSGeneviève Bastien 
8376122d57eSWang Nan 	if (perf_evsel__is_bpf_output(evsel)) {
8386122d57eSWang Nan 		ret = add_bpf_output_values(event_class, event, sample);
8396122d57eSWang Nan 		if (ret)
8406122d57eSWang Nan 			return -1;
8416122d57eSWang Nan 	}
8426122d57eSWang Nan 
84390e129ffSSebastian Andrzej Siewior 	cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel));
84489e5fa88SJiri Olsa 	if (cs) {
84589e5fa88SJiri Olsa 		if (is_flush_needed(cs))
84689e5fa88SJiri Olsa 			ctf_stream__flush(cs);
84789e5fa88SJiri Olsa 
84889e5fa88SJiri Olsa 		cs->count++;
84990e129ffSSebastian Andrzej Siewior 		bt_ctf_stream_append_event(cs->stream, event);
85089e5fa88SJiri Olsa 	}
85190e129ffSSebastian Andrzej Siewior 
852edbe9817SJiri Olsa 	bt_ctf_event_put(event);
85390e129ffSSebastian Andrzej Siewior 	return cs ? 0 : -1;
854edbe9817SJiri Olsa }
855edbe9817SJiri Olsa 
856f5a08cedSWang Nan #define __NON_SAMPLE_SET_FIELD(_name, _type, _field) 	\
857f5a08cedSWang Nan do {							\
858f5a08cedSWang Nan 	ret = value_set_##_type(cw, event, #_field, _event->_name._field);\
859f5a08cedSWang Nan 	if (ret)					\
860f5a08cedSWang Nan 		return -1;				\
861f5a08cedSWang Nan } while(0)
862f5a08cedSWang Nan 
863f5a08cedSWang Nan #define __FUNC_PROCESS_NON_SAMPLE(_name, body) 	\
864f5a08cedSWang Nan static int process_##_name##_event(struct perf_tool *tool,	\
865f5a08cedSWang Nan 				   union perf_event *_event,	\
866f5a08cedSWang Nan 				   struct perf_sample *sample,	\
867f5a08cedSWang Nan 				   struct machine *machine)	\
868f5a08cedSWang Nan {								\
869f5a08cedSWang Nan 	struct convert *c = container_of(tool, struct convert, tool);\
870f5a08cedSWang Nan 	struct ctf_writer *cw = &c->writer;			\
871f5a08cedSWang Nan 	struct bt_ctf_event_class *event_class = cw->_name##_class;\
872f5a08cedSWang Nan 	struct bt_ctf_event *event;				\
873f5a08cedSWang Nan 	struct ctf_stream *cs;					\
874f5a08cedSWang Nan 	int ret;						\
875f5a08cedSWang Nan 								\
876f5a08cedSWang Nan 	c->non_sample_count++;					\
877f5a08cedSWang Nan 	c->events_size += _event->header.size;			\
878f5a08cedSWang Nan 	event = bt_ctf_event_create(event_class);		\
879f5a08cedSWang Nan 	if (!event) {						\
880f5a08cedSWang Nan 		pr_err("Failed to create an CTF event\n");	\
881f5a08cedSWang Nan 		return -1;					\
882f5a08cedSWang Nan 	}							\
883f5a08cedSWang Nan 								\
884f5a08cedSWang Nan 	bt_ctf_clock_set_time(cw->clock, sample->time);		\
885f5a08cedSWang Nan 	body							\
886f5a08cedSWang Nan 	cs = ctf_stream(cw, 0);					\
887f5a08cedSWang Nan 	if (cs) {						\
888f5a08cedSWang Nan 		if (is_flush_needed(cs))			\
889f5a08cedSWang Nan 			ctf_stream__flush(cs);			\
890f5a08cedSWang Nan 								\
891f5a08cedSWang Nan 		cs->count++;					\
892f5a08cedSWang Nan 		bt_ctf_stream_append_event(cs->stream, event);	\
893f5a08cedSWang Nan 	}							\
894f5a08cedSWang Nan 	bt_ctf_event_put(event);				\
895f5a08cedSWang Nan 								\
896f5a08cedSWang Nan 	return perf_event__process_##_name(tool, _event, sample, machine);\
897f5a08cedSWang Nan }
898f5a08cedSWang Nan 
899f5a08cedSWang Nan __FUNC_PROCESS_NON_SAMPLE(comm,
900f5a08cedSWang Nan 	__NON_SAMPLE_SET_FIELD(comm, u32, pid);
901f5a08cedSWang Nan 	__NON_SAMPLE_SET_FIELD(comm, u32, tid);
902f5a08cedSWang Nan 	__NON_SAMPLE_SET_FIELD(comm, string, comm);
903f5a08cedSWang Nan )
904ebccba3fSWang Nan __FUNC_PROCESS_NON_SAMPLE(fork,
905ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u32, pid);
906ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u32, ppid);
907ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u32, tid);
908ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u32, ptid);
909ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u64, time);
910ebccba3fSWang Nan )
911ebccba3fSWang Nan 
912ebccba3fSWang Nan __FUNC_PROCESS_NON_SAMPLE(exit,
913ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u32, pid);
914ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u32, ppid);
915ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u32, tid);
916ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u32, ptid);
917ebccba3fSWang Nan 	__NON_SAMPLE_SET_FIELD(fork, u64, time);
918ebccba3fSWang Nan )
919f9f6f2a9SGeneviève Bastien __FUNC_PROCESS_NON_SAMPLE(mmap,
920f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_SET_FIELD(mmap, u32, pid);
921f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_SET_FIELD(mmap, u32, tid);
922f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_SET_FIELD(mmap, u64_hex, start);
923f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_SET_FIELD(mmap, string, filename);
924f9f6f2a9SGeneviève Bastien )
925f9f6f2a9SGeneviève Bastien __FUNC_PROCESS_NON_SAMPLE(mmap2,
926f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_SET_FIELD(mmap2, u32, pid);
927f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_SET_FIELD(mmap2, u32, tid);
928f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_SET_FIELD(mmap2, u64_hex, start);
929f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_SET_FIELD(mmap2, string, filename);
930f9f6f2a9SGeneviève Bastien )
931f5a08cedSWang Nan #undef __NON_SAMPLE_SET_FIELD
932f5a08cedSWang Nan #undef __FUNC_PROCESS_NON_SAMPLE
933f5a08cedSWang Nan 
934e0a7cce5SWang Nan /* If dup < 0, add a prefix. Else, add _dupl_X suffix. */
935e0a7cce5SWang Nan static char *change_name(char *name, char *orig_name, int dup)
936e0a7cce5SWang Nan {
937e0a7cce5SWang Nan 	char *new_name = NULL;
938e0a7cce5SWang Nan 	size_t len;
939e0a7cce5SWang Nan 
940e0a7cce5SWang Nan 	if (!name)
941e0a7cce5SWang Nan 		name = orig_name;
942e0a7cce5SWang Nan 
943e0a7cce5SWang Nan 	if (dup >= 10)
944e0a7cce5SWang Nan 		goto out;
945e0a7cce5SWang Nan 	/*
946e0a7cce5SWang Nan 	 * Add '_' prefix to potential keywork.  According to
947e0a7cce5SWang Nan 	 * Mathieu Desnoyers (https://lkml.org/lkml/2015/1/23/652),
948e0a7cce5SWang Nan 	 * futher CTF spec updating may require us to use '$'.
949e0a7cce5SWang Nan 	 */
950e0a7cce5SWang Nan 	if (dup < 0)
951e0a7cce5SWang Nan 		len = strlen(name) + sizeof("_");
952e0a7cce5SWang Nan 	else
953e0a7cce5SWang Nan 		len = strlen(orig_name) + sizeof("_dupl_X");
954e0a7cce5SWang Nan 
955e0a7cce5SWang Nan 	new_name = malloc(len);
956e0a7cce5SWang Nan 	if (!new_name)
957e0a7cce5SWang Nan 		goto out;
958e0a7cce5SWang Nan 
959e0a7cce5SWang Nan 	if (dup < 0)
960e0a7cce5SWang Nan 		snprintf(new_name, len, "_%s", name);
961e0a7cce5SWang Nan 	else
962e0a7cce5SWang Nan 		snprintf(new_name, len, "%s_dupl_%d", orig_name, dup);
963e0a7cce5SWang Nan 
964e0a7cce5SWang Nan out:
965e0a7cce5SWang Nan 	if (name != orig_name)
966e0a7cce5SWang Nan 		free(name);
967e0a7cce5SWang Nan 	return new_name;
968e0a7cce5SWang Nan }
969e0a7cce5SWang Nan 
970e0a7cce5SWang Nan static int event_class_add_field(struct bt_ctf_event_class *event_class,
971e0a7cce5SWang Nan 		struct bt_ctf_field_type *type,
9722c92f982STzvetomir Stoyanov (VMware) 		struct tep_format_field *field)
973e0a7cce5SWang Nan {
974e0a7cce5SWang Nan 	struct bt_ctf_field_type *t = NULL;
975e0a7cce5SWang Nan 	char *name;
976e0a7cce5SWang Nan 	int dup = 1;
977e0a7cce5SWang Nan 	int ret;
978e0a7cce5SWang Nan 
979e0a7cce5SWang Nan 	/* alias was already assigned */
980e0a7cce5SWang Nan 	if (field->alias != field->name)
981e0a7cce5SWang Nan 		return bt_ctf_event_class_add_field(event_class, type,
982e0a7cce5SWang Nan 				(char *)field->alias);
983e0a7cce5SWang Nan 
984e0a7cce5SWang Nan 	name = field->name;
985e0a7cce5SWang Nan 
986e0a7cce5SWang Nan 	/* If 'name' is a keywork, add prefix. */
987e0a7cce5SWang Nan 	if (bt_ctf_validate_identifier(name))
988e0a7cce5SWang Nan 		name = change_name(name, field->name, -1);
989e0a7cce5SWang Nan 
990e0a7cce5SWang Nan 	if (!name) {
991e0a7cce5SWang Nan 		pr_err("Failed to fix invalid identifier.");
992e0a7cce5SWang Nan 		return -1;
993e0a7cce5SWang Nan 	}
994e0a7cce5SWang Nan 	while ((t = bt_ctf_event_class_get_field_by_name(event_class, name))) {
995e0a7cce5SWang Nan 		bt_ctf_field_type_put(t);
996e0a7cce5SWang Nan 		name = change_name(name, field->name, dup++);
997e0a7cce5SWang Nan 		if (!name) {
998e0a7cce5SWang Nan 			pr_err("Failed to create dup name for '%s'\n", field->name);
999e0a7cce5SWang Nan 			return -1;
1000e0a7cce5SWang Nan 		}
1001e0a7cce5SWang Nan 	}
1002e0a7cce5SWang Nan 
1003e0a7cce5SWang Nan 	ret = bt_ctf_event_class_add_field(event_class, type, name);
1004e0a7cce5SWang Nan 	if (!ret)
1005e0a7cce5SWang Nan 		field->alias = name;
1006e0a7cce5SWang Nan 
1007e0a7cce5SWang Nan 	return ret;
1008e0a7cce5SWang Nan }
1009e0a7cce5SWang Nan 
101069364727SSebastian Andrzej Siewior static int add_tracepoint_fields_types(struct ctf_writer *cw,
10112c92f982STzvetomir Stoyanov (VMware) 				       struct tep_format_field *fields,
101269364727SSebastian Andrzej Siewior 				       struct bt_ctf_event_class *event_class)
101369364727SSebastian Andrzej Siewior {
10142c92f982STzvetomir Stoyanov (VMware) 	struct tep_format_field *field;
101569364727SSebastian Andrzej Siewior 	int ret;
101669364727SSebastian Andrzej Siewior 
101769364727SSebastian Andrzej Siewior 	for (field = fields; field; field = field->next) {
101869364727SSebastian Andrzej Siewior 		struct bt_ctf_field_type *type;
101969364727SSebastian Andrzej Siewior 		unsigned long flags = field->flags;
102069364727SSebastian Andrzej Siewior 
102169364727SSebastian Andrzej Siewior 		pr2("  field '%s'\n", field->name);
102269364727SSebastian Andrzej Siewior 
102369364727SSebastian Andrzej Siewior 		type = get_tracepoint_field_type(cw, field);
102469364727SSebastian Andrzej Siewior 		if (!type)
102569364727SSebastian Andrzej Siewior 			return -1;
102669364727SSebastian Andrzej Siewior 
102769364727SSebastian Andrzej Siewior 		/*
102869364727SSebastian Andrzej Siewior 		 * A string is an array of chars. For this we use the string
102969364727SSebastian Andrzej Siewior 		 * type and don't care that it is an array. What we don't
103069364727SSebastian Andrzej Siewior 		 * support is an array of strings.
103169364727SSebastian Andrzej Siewior 		 */
1032bb39ccb2STzvetomir Stoyanov (VMware) 		if (flags & TEP_FIELD_IS_STRING)
1033bb39ccb2STzvetomir Stoyanov (VMware) 			flags &= ~TEP_FIELD_IS_ARRAY;
103469364727SSebastian Andrzej Siewior 
1035bb39ccb2STzvetomir Stoyanov (VMware) 		if (flags & TEP_FIELD_IS_ARRAY)
103669364727SSebastian Andrzej Siewior 			type = bt_ctf_field_type_array_create(type, field->arraylen);
103769364727SSebastian Andrzej Siewior 
1038e0a7cce5SWang Nan 		ret = event_class_add_field(event_class, type, field);
103969364727SSebastian Andrzej Siewior 
1040bb39ccb2STzvetomir Stoyanov (VMware) 		if (flags & TEP_FIELD_IS_ARRAY)
104169364727SSebastian Andrzej Siewior 			bt_ctf_field_type_put(type);
104269364727SSebastian Andrzej Siewior 
104369364727SSebastian Andrzej Siewior 		if (ret) {
1044e0a7cce5SWang Nan 			pr_err("Failed to add field '%s': %d\n",
1045e0a7cce5SWang Nan 					field->name, ret);
104669364727SSebastian Andrzej Siewior 			return -1;
104769364727SSebastian Andrzej Siewior 		}
104869364727SSebastian Andrzej Siewior 	}
104969364727SSebastian Andrzej Siewior 
105069364727SSebastian Andrzej Siewior 	return 0;
105169364727SSebastian Andrzej Siewior }
105269364727SSebastian Andrzej Siewior 
105369364727SSebastian Andrzej Siewior static int add_tracepoint_types(struct ctf_writer *cw,
105432dcd021SJiri Olsa 				struct evsel *evsel,
105569364727SSebastian Andrzej Siewior 				struct bt_ctf_event_class *class)
105669364727SSebastian Andrzej Siewior {
10572c92f982STzvetomir Stoyanov (VMware) 	struct tep_format_field *common_fields = evsel->tp_format->format.common_fields;
10582c92f982STzvetomir Stoyanov (VMware) 	struct tep_format_field *fields        = evsel->tp_format->format.fields;
105969364727SSebastian Andrzej Siewior 	int ret;
106069364727SSebastian Andrzej Siewior 
106169364727SSebastian Andrzej Siewior 	ret = add_tracepoint_fields_types(cw, common_fields, class);
106269364727SSebastian Andrzej Siewior 	if (!ret)
106369364727SSebastian Andrzej Siewior 		ret = add_tracepoint_fields_types(cw, fields, class);
106469364727SSebastian Andrzej Siewior 
106569364727SSebastian Andrzej Siewior 	return ret;
106669364727SSebastian Andrzej Siewior }
106769364727SSebastian Andrzej Siewior 
10686122d57eSWang Nan static int add_bpf_output_types(struct ctf_writer *cw,
10696122d57eSWang Nan 				struct bt_ctf_event_class *class)
10706122d57eSWang Nan {
10716122d57eSWang Nan 	struct bt_ctf_field_type *len_type = cw->data.u32;
10726122d57eSWang Nan 	struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex;
10736122d57eSWang Nan 	struct bt_ctf_field_type *seq_type;
10746122d57eSWang Nan 	int ret;
10756122d57eSWang Nan 
10766122d57eSWang Nan 	ret = bt_ctf_event_class_add_field(class, len_type, "raw_len");
10776122d57eSWang Nan 	if (ret)
10786122d57eSWang Nan 		return ret;
10796122d57eSWang Nan 
10806122d57eSWang Nan 	seq_type = bt_ctf_field_type_sequence_create(seq_base_type, "raw_len");
10816122d57eSWang Nan 	if (!seq_type)
10826122d57eSWang Nan 		return -1;
10836122d57eSWang Nan 
10846122d57eSWang Nan 	return bt_ctf_event_class_add_field(class, seq_type, "raw_data");
10856122d57eSWang Nan }
10866122d57eSWang Nan 
108732dcd021SJiri Olsa static int add_generic_types(struct ctf_writer *cw, struct evsel *evsel,
1088edbe9817SJiri Olsa 			     struct bt_ctf_event_class *event_class)
1089edbe9817SJiri Olsa {
1090edbe9817SJiri Olsa 	u64 type = evsel->attr.sample_type;
1091edbe9817SJiri Olsa 
1092edbe9817SJiri Olsa 	/*
1093edbe9817SJiri Olsa 	 * missing:
1094edbe9817SJiri Olsa 	 *   PERF_SAMPLE_TIME         - not needed as we have it in
1095edbe9817SJiri Olsa 	 *                              ctf event header
1096edbe9817SJiri Olsa 	 *   PERF_SAMPLE_READ         - TODO
1097edbe9817SJiri Olsa 	 *   PERF_SAMPLE_CALLCHAIN    - TODO
10986122d57eSWang Nan 	 *   PERF_SAMPLE_RAW          - tracepoint fields and BPF output
10996122d57eSWang Nan 	 *                              are handled separately
1100edbe9817SJiri Olsa 	 *   PERF_SAMPLE_BRANCH_STACK - TODO
1101edbe9817SJiri Olsa 	 *   PERF_SAMPLE_REGS_USER    - TODO
1102edbe9817SJiri Olsa 	 *   PERF_SAMPLE_STACK_USER   - TODO
1103edbe9817SJiri Olsa 	 */
1104edbe9817SJiri Olsa 
1105edbe9817SJiri Olsa #define ADD_FIELD(cl, t, n)						\
1106edbe9817SJiri Olsa 	do {								\
1107edbe9817SJiri Olsa 		pr2("  field '%s'\n", n);				\
1108edbe9817SJiri Olsa 		if (bt_ctf_event_class_add_field(cl, t, n)) {		\
1109e0a7cce5SWang Nan 			pr_err("Failed to add field '%s';\n", n);	\
1110edbe9817SJiri Olsa 			return -1;					\
1111edbe9817SJiri Olsa 		}							\
1112edbe9817SJiri Olsa 	} while (0)
1113edbe9817SJiri Olsa 
1114edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_IP)
111554cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
1116edbe9817SJiri Olsa 
1117edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_TID) {
111854cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.s32, "perf_tid");
111954cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.s32, "perf_pid");
1120edbe9817SJiri Olsa 	}
1121edbe9817SJiri Olsa 
1122edbe9817SJiri Olsa 	if ((type & PERF_SAMPLE_ID) ||
1123edbe9817SJiri Olsa 	    (type & PERF_SAMPLE_IDENTIFIER))
112454cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.u64, "perf_id");
1125edbe9817SJiri Olsa 
1126edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_STREAM_ID)
112754cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
1128edbe9817SJiri Olsa 
1129edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_PERIOD)
113054cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.u64, "perf_period");
1131edbe9817SJiri Olsa 
1132edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_WEIGHT)
113354cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.u64, "perf_weight");
1134edbe9817SJiri Olsa 
1135edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_DATA_SRC)
113654cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
1137edbe9817SJiri Olsa 
1138edbe9817SJiri Olsa 	if (type & PERF_SAMPLE_TRANSACTION)
113954cf776aSSebastian Andrzej Siewior 		ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
1140edbe9817SJiri Olsa 
1141a3073c8eSGeneviève Bastien 	if (type & PERF_SAMPLE_CALLCHAIN) {
1142a3073c8eSGeneviève Bastien 		ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size");
1143a3073c8eSGeneviève Bastien 		ADD_FIELD(event_class,
1144a3073c8eSGeneviève Bastien 			bt_ctf_field_type_sequence_create(
1145a3073c8eSGeneviève Bastien 				cw->data.u64_hex, "perf_callchain_size"),
1146a3073c8eSGeneviève Bastien 			"perf_callchain");
1147a3073c8eSGeneviève Bastien 	}
1148a3073c8eSGeneviève Bastien 
1149edbe9817SJiri Olsa #undef ADD_FIELD
1150edbe9817SJiri Olsa 	return 0;
1151edbe9817SJiri Olsa }
1152edbe9817SJiri Olsa 
115332dcd021SJiri Olsa static int add_event(struct ctf_writer *cw, struct evsel *evsel)
1154edbe9817SJiri Olsa {
1155edbe9817SJiri Olsa 	struct bt_ctf_event_class *event_class;
1156edbe9817SJiri Olsa 	struct evsel_priv *priv;
1157edbe9817SJiri Olsa 	const char *name = perf_evsel__name(evsel);
1158edbe9817SJiri Olsa 	int ret;
1159edbe9817SJiri Olsa 
1160edbe9817SJiri Olsa 	pr("Adding event '%s' (type %d)\n", name, evsel->attr.type);
1161edbe9817SJiri Olsa 
1162edbe9817SJiri Olsa 	event_class = bt_ctf_event_class_create(name);
1163edbe9817SJiri Olsa 	if (!event_class)
1164edbe9817SJiri Olsa 		return -1;
1165edbe9817SJiri Olsa 
1166edbe9817SJiri Olsa 	ret = add_generic_types(cw, evsel, event_class);
1167edbe9817SJiri Olsa 	if (ret)
1168edbe9817SJiri Olsa 		goto err;
1169edbe9817SJiri Olsa 
117069364727SSebastian Andrzej Siewior 	if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
117169364727SSebastian Andrzej Siewior 		ret = add_tracepoint_types(cw, evsel, event_class);
117269364727SSebastian Andrzej Siewior 		if (ret)
117369364727SSebastian Andrzej Siewior 			goto err;
117469364727SSebastian Andrzej Siewior 	}
117569364727SSebastian Andrzej Siewior 
11766122d57eSWang Nan 	if (perf_evsel__is_bpf_output(evsel)) {
11776122d57eSWang Nan 		ret = add_bpf_output_types(cw, event_class);
11786122d57eSWang Nan 		if (ret)
11796122d57eSWang Nan 			goto err;
11806122d57eSWang Nan 	}
11816122d57eSWang Nan 
1182edbe9817SJiri Olsa 	ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);
1183edbe9817SJiri Olsa 	if (ret) {
1184edbe9817SJiri Olsa 		pr("Failed to add event class into stream.\n");
1185edbe9817SJiri Olsa 		goto err;
1186edbe9817SJiri Olsa 	}
1187edbe9817SJiri Olsa 
1188edbe9817SJiri Olsa 	priv = malloc(sizeof(*priv));
1189edbe9817SJiri Olsa 	if (!priv)
1190edbe9817SJiri Olsa 		goto err;
1191edbe9817SJiri Olsa 
1192edbe9817SJiri Olsa 	priv->event_class = event_class;
1193edbe9817SJiri Olsa 	evsel->priv       = priv;
1194edbe9817SJiri Olsa 	return 0;
1195edbe9817SJiri Olsa 
1196edbe9817SJiri Olsa err:
1197edbe9817SJiri Olsa 	bt_ctf_event_class_put(event_class);
1198edbe9817SJiri Olsa 	pr_err("Failed to add event '%s'.\n", name);
1199edbe9817SJiri Olsa 	return -1;
1200edbe9817SJiri Olsa }
1201edbe9817SJiri Olsa 
1202edbe9817SJiri Olsa static int setup_events(struct ctf_writer *cw, struct perf_session *session)
1203edbe9817SJiri Olsa {
1204*63503dbaSJiri Olsa 	struct evlist *evlist = session->evlist;
120532dcd021SJiri Olsa 	struct evsel *evsel;
1206edbe9817SJiri Olsa 	int ret;
1207edbe9817SJiri Olsa 
1208e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1209edbe9817SJiri Olsa 		ret = add_event(cw, evsel);
1210edbe9817SJiri Olsa 		if (ret)
1211edbe9817SJiri Olsa 			return ret;
1212edbe9817SJiri Olsa 	}
1213edbe9817SJiri Olsa 	return 0;
1214edbe9817SJiri Olsa }
1215edbe9817SJiri Olsa 
1216f5a08cedSWang Nan #define __NON_SAMPLE_ADD_FIELD(t, n)						\
1217f5a08cedSWang Nan 	do {							\
1218f5a08cedSWang Nan 		pr2("  field '%s'\n", #n);			\
1219f5a08cedSWang Nan 		if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\
1220f5a08cedSWang Nan 			pr_err("Failed to add field '%s';\n", #n);\
1221f5a08cedSWang Nan 			return -1;				\
1222f5a08cedSWang Nan 		}						\
1223f5a08cedSWang Nan 	} while(0)
1224f5a08cedSWang Nan 
1225f5a08cedSWang Nan #define __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(_name, body) 		\
1226f5a08cedSWang Nan static int add_##_name##_event(struct ctf_writer *cw)		\
1227f5a08cedSWang Nan {								\
1228f5a08cedSWang Nan 	struct bt_ctf_event_class *event_class;			\
1229f5a08cedSWang Nan 	int ret;						\
1230f5a08cedSWang Nan 								\
1231f5a08cedSWang Nan 	pr("Adding "#_name" event\n");				\
1232f5a08cedSWang Nan 	event_class = bt_ctf_event_class_create("perf_" #_name);\
1233f5a08cedSWang Nan 	if (!event_class)					\
1234f5a08cedSWang Nan 		return -1;					\
1235f5a08cedSWang Nan 	body							\
1236f5a08cedSWang Nan 								\
1237f5a08cedSWang Nan 	ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\
1238f5a08cedSWang Nan 	if (ret) {						\
1239f5a08cedSWang Nan 		pr("Failed to add event class '"#_name"' into stream.\n");\
1240f5a08cedSWang Nan 		return ret;					\
1241f5a08cedSWang Nan 	}							\
1242f5a08cedSWang Nan 								\
1243f5a08cedSWang Nan 	cw->_name##_class = event_class;			\
1244f5a08cedSWang Nan 	bt_ctf_event_class_put(event_class);			\
1245f5a08cedSWang Nan 	return 0;						\
1246f5a08cedSWang Nan }
1247f5a08cedSWang Nan 
1248f5a08cedSWang Nan __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(comm,
1249f5a08cedSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, pid);
1250f5a08cedSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, tid);
1251f5a08cedSWang Nan 	__NON_SAMPLE_ADD_FIELD(string, comm);
1252f5a08cedSWang Nan )
1253f5a08cedSWang Nan 
1254ebccba3fSWang Nan __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(fork,
1255ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, pid);
1256ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, ppid);
1257ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, tid);
1258ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, ptid);
1259ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u64, time);
1260ebccba3fSWang Nan )
1261ebccba3fSWang Nan 
1262ebccba3fSWang Nan __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(exit,
1263ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, pid);
1264ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, ppid);
1265ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, tid);
1266ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u32, ptid);
1267ebccba3fSWang Nan 	__NON_SAMPLE_ADD_FIELD(u64, time);
1268ebccba3fSWang Nan )
1269ebccba3fSWang Nan 
1270f9f6f2a9SGeneviève Bastien __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap,
1271f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_ADD_FIELD(u32, pid);
1272f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_ADD_FIELD(u32, tid);
1273f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_ADD_FIELD(u64_hex, start);
1274f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_ADD_FIELD(string, filename);
1275f9f6f2a9SGeneviève Bastien )
1276f9f6f2a9SGeneviève Bastien 
1277f9f6f2a9SGeneviève Bastien __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(mmap2,
1278f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_ADD_FIELD(u32, pid);
1279f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_ADD_FIELD(u32, tid);
1280f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_ADD_FIELD(u64_hex, start);
1281f9f6f2a9SGeneviève Bastien 	__NON_SAMPLE_ADD_FIELD(string, filename);
1282f9f6f2a9SGeneviève Bastien )
1283f5a08cedSWang Nan #undef __NON_SAMPLE_ADD_FIELD
1284f5a08cedSWang Nan #undef __FUNC_ADD_NON_SAMPLE_EVENT_CLASS
1285f5a08cedSWang Nan 
1286f5a08cedSWang Nan static int setup_non_sample_events(struct ctf_writer *cw,
1287f5a08cedSWang Nan 				   struct perf_session *session __maybe_unused)
1288f5a08cedSWang Nan {
1289f5a08cedSWang Nan 	int ret;
1290f5a08cedSWang Nan 
1291f5a08cedSWang Nan 	ret = add_comm_event(cw);
1292f5a08cedSWang Nan 	if (ret)
1293f5a08cedSWang Nan 		return ret;
1294ebccba3fSWang Nan 	ret = add_exit_event(cw);
1295ebccba3fSWang Nan 	if (ret)
1296ebccba3fSWang Nan 		return ret;
1297ebccba3fSWang Nan 	ret = add_fork_event(cw);
1298ebccba3fSWang Nan 	if (ret)
1299ebccba3fSWang Nan 		return ret;
1300f9f6f2a9SGeneviève Bastien 	ret = add_mmap_event(cw);
1301f9f6f2a9SGeneviève Bastien 	if (ret)
1302f9f6f2a9SGeneviève Bastien 		return ret;
1303f9f6f2a9SGeneviève Bastien 	ret = add_mmap2_event(cw);
1304f9f6f2a9SGeneviève Bastien 	if (ret)
1305f9f6f2a9SGeneviève Bastien 		return ret;
1306f5a08cedSWang Nan 	return 0;
1307f5a08cedSWang Nan }
1308f5a08cedSWang Nan 
13095141d735SWang Nan static void cleanup_events(struct perf_session *session)
13105141d735SWang Nan {
1311*63503dbaSJiri Olsa 	struct evlist *evlist = session->evlist;
131232dcd021SJiri Olsa 	struct evsel *evsel;
13135141d735SWang Nan 
1314e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
13155141d735SWang Nan 		struct evsel_priv *priv;
13165141d735SWang Nan 
13175141d735SWang Nan 		priv = evsel->priv;
13185141d735SWang Nan 		bt_ctf_event_class_put(priv->event_class);
13195141d735SWang Nan 		zfree(&evsel->priv);
13205141d735SWang Nan 	}
13215141d735SWang Nan 
13225141d735SWang Nan 	perf_evlist__delete(evlist);
13235141d735SWang Nan 	session->evlist = NULL;
13245141d735SWang Nan }
13255141d735SWang Nan 
132690e129ffSSebastian Andrzej Siewior static int setup_streams(struct ctf_writer *cw, struct perf_session *session)
132790e129ffSSebastian Andrzej Siewior {
132890e129ffSSebastian Andrzej Siewior 	struct ctf_stream **stream;
132990e129ffSSebastian Andrzej Siewior 	struct perf_header *ph = &session->header;
133090e129ffSSebastian Andrzej Siewior 	int ncpus;
133190e129ffSSebastian Andrzej Siewior 
133290e129ffSSebastian Andrzej Siewior 	/*
133390e129ffSSebastian Andrzej Siewior 	 * Try to get the number of cpus used in the data file,
133490e129ffSSebastian Andrzej Siewior 	 * if not present fallback to the MAX_CPUS.
133590e129ffSSebastian Andrzej Siewior 	 */
133690e129ffSSebastian Andrzej Siewior 	ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS;
133790e129ffSSebastian Andrzej Siewior 
133890e129ffSSebastian Andrzej Siewior 	stream = zalloc(sizeof(*stream) * ncpus);
133990e129ffSSebastian Andrzej Siewior 	if (!stream) {
134090e129ffSSebastian Andrzej Siewior 		pr_err("Failed to allocate streams.\n");
134190e129ffSSebastian Andrzej Siewior 		return -ENOMEM;
134290e129ffSSebastian Andrzej Siewior 	}
134390e129ffSSebastian Andrzej Siewior 
134490e129ffSSebastian Andrzej Siewior 	cw->stream     = stream;
134590e129ffSSebastian Andrzej Siewior 	cw->stream_cnt = ncpus;
134690e129ffSSebastian Andrzej Siewior 	return 0;
134790e129ffSSebastian Andrzej Siewior }
134890e129ffSSebastian Andrzej Siewior 
134990e129ffSSebastian Andrzej Siewior static void free_streams(struct ctf_writer *cw)
135090e129ffSSebastian Andrzej Siewior {
135190e129ffSSebastian Andrzej Siewior 	int cpu;
135290e129ffSSebastian Andrzej Siewior 
135390e129ffSSebastian Andrzej Siewior 	for (cpu = 0; cpu < cw->stream_cnt; cpu++)
135490e129ffSSebastian Andrzej Siewior 		ctf_stream__delete(cw->stream[cpu]);
135590e129ffSSebastian Andrzej Siewior 
1356d8f9da24SArnaldo Carvalho de Melo 	zfree(&cw->stream);
135790e129ffSSebastian Andrzej Siewior }
135890e129ffSSebastian Andrzej Siewior 
1359edbe9817SJiri Olsa static int ctf_writer__setup_env(struct ctf_writer *cw,
1360edbe9817SJiri Olsa 				 struct perf_session *session)
1361edbe9817SJiri Olsa {
1362edbe9817SJiri Olsa 	struct perf_header *header = &session->header;
1363edbe9817SJiri Olsa 	struct bt_ctf_writer *writer = cw->writer;
1364edbe9817SJiri Olsa 
1365edbe9817SJiri Olsa #define ADD(__n, __v)							\
1366edbe9817SJiri Olsa do {									\
1367edbe9817SJiri Olsa 	if (bt_ctf_writer_add_environment_field(writer, __n, __v))	\
1368edbe9817SJiri Olsa 		return -1;						\
1369edbe9817SJiri Olsa } while (0)
1370edbe9817SJiri Olsa 
1371edbe9817SJiri Olsa 	ADD("host",    header->env.hostname);
1372edbe9817SJiri Olsa 	ADD("sysname", "Linux");
1373edbe9817SJiri Olsa 	ADD("release", header->env.os_release);
1374edbe9817SJiri Olsa 	ADD("version", header->env.version);
1375edbe9817SJiri Olsa 	ADD("machine", header->env.arch);
1376edbe9817SJiri Olsa 	ADD("domain", "kernel");
1377edbe9817SJiri Olsa 	ADD("tracer_name", "perf");
1378edbe9817SJiri Olsa 
1379edbe9817SJiri Olsa #undef ADD
1380edbe9817SJiri Olsa 	return 0;
1381edbe9817SJiri Olsa }
1382edbe9817SJiri Olsa 
1383edbe9817SJiri Olsa static int ctf_writer__setup_clock(struct ctf_writer *cw)
1384edbe9817SJiri Olsa {
1385edbe9817SJiri Olsa 	struct bt_ctf_clock *clock = cw->clock;
1386edbe9817SJiri Olsa 
1387edbe9817SJiri Olsa 	bt_ctf_clock_set_description(clock, "perf clock");
1388edbe9817SJiri Olsa 
1389edbe9817SJiri Olsa #define SET(__n, __v)				\
1390edbe9817SJiri Olsa do {						\
1391edbe9817SJiri Olsa 	if (bt_ctf_clock_set_##__n(clock, __v))	\
1392edbe9817SJiri Olsa 		return -1;			\
1393edbe9817SJiri Olsa } while (0)
1394edbe9817SJiri Olsa 
1395edbe9817SJiri Olsa 	SET(frequency,   1000000000);
1396edbe9817SJiri Olsa 	SET(offset_s,    0);
1397edbe9817SJiri Olsa 	SET(offset,      0);
1398edbe9817SJiri Olsa 	SET(precision,   10);
1399edbe9817SJiri Olsa 	SET(is_absolute, 0);
1400edbe9817SJiri Olsa 
1401edbe9817SJiri Olsa #undef SET
1402edbe9817SJiri Olsa 	return 0;
1403edbe9817SJiri Olsa }
1404edbe9817SJiri Olsa 
1405edbe9817SJiri Olsa static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex)
1406edbe9817SJiri Olsa {
1407edbe9817SJiri Olsa 	struct bt_ctf_field_type *type;
1408edbe9817SJiri Olsa 
1409edbe9817SJiri Olsa 	type = bt_ctf_field_type_integer_create(size);
1410edbe9817SJiri Olsa 	if (!type)
1411edbe9817SJiri Olsa 		return NULL;
1412edbe9817SJiri Olsa 
1413edbe9817SJiri Olsa 	if (sign &&
1414edbe9817SJiri Olsa 	    bt_ctf_field_type_integer_set_signed(type, 1))
1415edbe9817SJiri Olsa 		goto err;
1416edbe9817SJiri Olsa 
1417edbe9817SJiri Olsa 	if (hex &&
1418edbe9817SJiri Olsa 	    bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL))
1419edbe9817SJiri Olsa 		goto err;
1420edbe9817SJiri Olsa 
1421f8dd2d5fSWang Nan #if __BYTE_ORDER == __BIG_ENDIAN
1422f8dd2d5fSWang Nan 	bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_BIG_ENDIAN);
1423f8dd2d5fSWang Nan #else
1424f8dd2d5fSWang Nan 	bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
1425f8dd2d5fSWang Nan #endif
1426f8dd2d5fSWang Nan 
1427edbe9817SJiri Olsa 	pr2("Created type: INTEGER %d-bit %ssigned %s\n",
1428edbe9817SJiri Olsa 	    size, sign ? "un" : "", hex ? "hex" : "");
1429edbe9817SJiri Olsa 	return type;
1430edbe9817SJiri Olsa 
1431edbe9817SJiri Olsa err:
1432edbe9817SJiri Olsa 	bt_ctf_field_type_put(type);
1433edbe9817SJiri Olsa 	return NULL;
1434edbe9817SJiri Olsa }
1435edbe9817SJiri Olsa 
1436edbe9817SJiri Olsa static void ctf_writer__cleanup_data(struct ctf_writer *cw)
1437edbe9817SJiri Olsa {
1438edbe9817SJiri Olsa 	unsigned int i;
1439edbe9817SJiri Olsa 
1440edbe9817SJiri Olsa 	for (i = 0; i < ARRAY_SIZE(cw->data.array); i++)
1441edbe9817SJiri Olsa 		bt_ctf_field_type_put(cw->data.array[i]);
1442edbe9817SJiri Olsa }
1443edbe9817SJiri Olsa 
1444edbe9817SJiri Olsa static int ctf_writer__init_data(struct ctf_writer *cw)
1445edbe9817SJiri Olsa {
1446edbe9817SJiri Olsa #define CREATE_INT_TYPE(type, size, sign, hex)		\
1447edbe9817SJiri Olsa do {							\
1448edbe9817SJiri Olsa 	(type) = create_int_type(size, sign, hex);	\
1449edbe9817SJiri Olsa 	if (!(type))					\
1450edbe9817SJiri Olsa 		goto err;				\
1451edbe9817SJiri Olsa } while (0)
1452edbe9817SJiri Olsa 
1453edbe9817SJiri Olsa 	CREATE_INT_TYPE(cw->data.s64, 64, true,  false);
1454edbe9817SJiri Olsa 	CREATE_INT_TYPE(cw->data.u64, 64, false, false);
1455edbe9817SJiri Olsa 	CREATE_INT_TYPE(cw->data.s32, 32, true,  false);
1456edbe9817SJiri Olsa 	CREATE_INT_TYPE(cw->data.u32, 32, false, false);
145726812d46SWang Nan 	CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true);
1458edbe9817SJiri Olsa 	CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true);
1459edbe9817SJiri Olsa 
1460edbe9817SJiri Olsa 	cw->data.string  = bt_ctf_field_type_string_create();
1461edbe9817SJiri Olsa 	if (cw->data.string)
1462edbe9817SJiri Olsa 		return 0;
1463edbe9817SJiri Olsa 
1464edbe9817SJiri Olsa err:
1465edbe9817SJiri Olsa 	ctf_writer__cleanup_data(cw);
1466edbe9817SJiri Olsa 	pr_err("Failed to create data types.\n");
1467edbe9817SJiri Olsa 	return -1;
1468edbe9817SJiri Olsa }
1469edbe9817SJiri Olsa 
1470edbe9817SJiri Olsa static void ctf_writer__cleanup(struct ctf_writer *cw)
1471edbe9817SJiri Olsa {
1472edbe9817SJiri Olsa 	ctf_writer__cleanup_data(cw);
1473edbe9817SJiri Olsa 
1474edbe9817SJiri Olsa 	bt_ctf_clock_put(cw->clock);
147590e129ffSSebastian Andrzej Siewior 	free_streams(cw);
1476edbe9817SJiri Olsa 	bt_ctf_stream_class_put(cw->stream_class);
1477edbe9817SJiri Olsa 	bt_ctf_writer_put(cw->writer);
1478edbe9817SJiri Olsa 
1479edbe9817SJiri Olsa 	/* and NULL all the pointers */
1480edbe9817SJiri Olsa 	memset(cw, 0, sizeof(*cw));
1481edbe9817SJiri Olsa }
1482edbe9817SJiri Olsa 
1483edbe9817SJiri Olsa static int ctf_writer__init(struct ctf_writer *cw, const char *path)
1484edbe9817SJiri Olsa {
1485edbe9817SJiri Olsa 	struct bt_ctf_writer		*writer;
1486edbe9817SJiri Olsa 	struct bt_ctf_stream_class	*stream_class;
1487edbe9817SJiri Olsa 	struct bt_ctf_clock		*clock;
148890e129ffSSebastian Andrzej Siewior 	struct bt_ctf_field_type	*pkt_ctx_type;
148990e129ffSSebastian Andrzej Siewior 	int				ret;
1490edbe9817SJiri Olsa 
1491edbe9817SJiri Olsa 	/* CTF writer */
1492edbe9817SJiri Olsa 	writer = bt_ctf_writer_create(path);
1493edbe9817SJiri Olsa 	if (!writer)
1494edbe9817SJiri Olsa 		goto err;
1495edbe9817SJiri Olsa 
1496edbe9817SJiri Olsa 	cw->writer = writer;
1497edbe9817SJiri Olsa 
1498edbe9817SJiri Olsa 	/* CTF clock */
1499edbe9817SJiri Olsa 	clock = bt_ctf_clock_create("perf_clock");
1500edbe9817SJiri Olsa 	if (!clock) {
1501edbe9817SJiri Olsa 		pr("Failed to create CTF clock.\n");
1502edbe9817SJiri Olsa 		goto err_cleanup;
1503edbe9817SJiri Olsa 	}
1504edbe9817SJiri Olsa 
1505edbe9817SJiri Olsa 	cw->clock = clock;
1506edbe9817SJiri Olsa 
1507edbe9817SJiri Olsa 	if (ctf_writer__setup_clock(cw)) {
1508edbe9817SJiri Olsa 		pr("Failed to setup CTF clock.\n");
1509edbe9817SJiri Olsa 		goto err_cleanup;
1510edbe9817SJiri Olsa 	}
1511edbe9817SJiri Olsa 
1512edbe9817SJiri Olsa 	/* CTF stream class */
1513edbe9817SJiri Olsa 	stream_class = bt_ctf_stream_class_create("perf_stream");
1514edbe9817SJiri Olsa 	if (!stream_class) {
1515edbe9817SJiri Olsa 		pr("Failed to create CTF stream class.\n");
1516edbe9817SJiri Olsa 		goto err_cleanup;
1517edbe9817SJiri Olsa 	}
1518edbe9817SJiri Olsa 
1519edbe9817SJiri Olsa 	cw->stream_class = stream_class;
1520edbe9817SJiri Olsa 
1521edbe9817SJiri Olsa 	/* CTF clock stream setup */
1522edbe9817SJiri Olsa 	if (bt_ctf_stream_class_set_clock(stream_class, clock)) {
1523edbe9817SJiri Olsa 		pr("Failed to assign CTF clock to stream class.\n");
1524edbe9817SJiri Olsa 		goto err_cleanup;
1525edbe9817SJiri Olsa 	}
1526edbe9817SJiri Olsa 
1527edbe9817SJiri Olsa 	if (ctf_writer__init_data(cw))
1528edbe9817SJiri Olsa 		goto err_cleanup;
1529edbe9817SJiri Olsa 
153090e129ffSSebastian Andrzej Siewior 	/* Add cpu_id for packet context */
153190e129ffSSebastian Andrzej Siewior 	pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
153290e129ffSSebastian Andrzej Siewior 	if (!pkt_ctx_type)
1533edbe9817SJiri Olsa 		goto err_cleanup;
1534edbe9817SJiri Olsa 
153590e129ffSSebastian Andrzej Siewior 	ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id");
153690e129ffSSebastian Andrzej Siewior 	bt_ctf_field_type_put(pkt_ctx_type);
153790e129ffSSebastian Andrzej Siewior 	if (ret)
153890e129ffSSebastian Andrzej Siewior 		goto err_cleanup;
1539edbe9817SJiri Olsa 
1540edbe9817SJiri Olsa 	/* CTF clock writer setup */
1541edbe9817SJiri Olsa 	if (bt_ctf_writer_add_clock(writer, clock)) {
1542edbe9817SJiri Olsa 		pr("Failed to assign CTF clock to writer.\n");
1543edbe9817SJiri Olsa 		goto err_cleanup;
1544edbe9817SJiri Olsa 	}
1545edbe9817SJiri Olsa 
1546edbe9817SJiri Olsa 	return 0;
1547edbe9817SJiri Olsa 
1548edbe9817SJiri Olsa err_cleanup:
1549edbe9817SJiri Olsa 	ctf_writer__cleanup(cw);
1550edbe9817SJiri Olsa err:
1551edbe9817SJiri Olsa 	pr_err("Failed to setup CTF writer.\n");
1552edbe9817SJiri Olsa 	return -1;
1553edbe9817SJiri Olsa }
1554edbe9817SJiri Olsa 
155590e129ffSSebastian Andrzej Siewior static int ctf_writer__flush_streams(struct ctf_writer *cw)
155690e129ffSSebastian Andrzej Siewior {
155790e129ffSSebastian Andrzej Siewior 	int cpu, ret = 0;
155890e129ffSSebastian Andrzej Siewior 
155990e129ffSSebastian Andrzej Siewior 	for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++)
156090e129ffSSebastian Andrzej Siewior 		ret = ctf_stream__flush(cw->stream[cpu]);
156190e129ffSSebastian Andrzej Siewior 
156290e129ffSSebastian Andrzej Siewior 	return ret;
156390e129ffSSebastian Andrzej Siewior }
156490e129ffSSebastian Andrzej Siewior 
15658fa46753SJiri Olsa static int convert__config(const char *var, const char *value, void *cb)
15668fa46753SJiri Olsa {
15678fa46753SJiri Olsa 	struct convert *c = cb;
15688fa46753SJiri Olsa 
156925ce4bb8SArnaldo Carvalho de Melo 	if (!strcmp(var, "convert.queue-size"))
157025ce4bb8SArnaldo Carvalho de Melo 		return perf_config_u64(&c->queue_size, var, value);
15718fa46753SJiri Olsa 
1572b8cbb349SWang Nan 	return 0;
15738fa46753SJiri Olsa }
15748fa46753SJiri Olsa 
15753275f68eSWang Nan int bt_convert__perf2ctf(const char *input, const char *path,
15763275f68eSWang Nan 			 struct perf_data_convert_opts *opts)
1577edbe9817SJiri Olsa {
1578edbe9817SJiri Olsa 	struct perf_session *session;
15798ceb41d7SJiri Olsa 	struct perf_data data = {
15802d4f2799SJiri Olsa 		.path	   = input,
1581edbe9817SJiri Olsa 		.mode      = PERF_DATA_MODE_READ,
15823275f68eSWang Nan 		.force     = opts->force,
1583edbe9817SJiri Olsa 	};
1584edbe9817SJiri Olsa 	struct convert c = {
1585edbe9817SJiri Olsa 		.tool = {
1586edbe9817SJiri Olsa 			.sample          = process_sample_event,
1587edbe9817SJiri Olsa 			.mmap            = perf_event__process_mmap,
1588edbe9817SJiri Olsa 			.mmap2           = perf_event__process_mmap2,
1589edbe9817SJiri Olsa 			.comm            = perf_event__process_comm,
1590edbe9817SJiri Olsa 			.exit            = perf_event__process_exit,
1591edbe9817SJiri Olsa 			.fork            = perf_event__process_fork,
1592edbe9817SJiri Olsa 			.lost            = perf_event__process_lost,
1593edbe9817SJiri Olsa 			.tracing_data    = perf_event__process_tracing_data,
1594edbe9817SJiri Olsa 			.build_id        = perf_event__process_build_id,
1595f3b3614aSHari Bathini 			.namespaces      = perf_event__process_namespaces,
1596edbe9817SJiri Olsa 			.ordered_events  = true,
1597edbe9817SJiri Olsa 			.ordering_requires_timestamps = true,
1598edbe9817SJiri Olsa 		},
1599edbe9817SJiri Olsa 	};
1600edbe9817SJiri Olsa 	struct ctf_writer *cw = &c.writer;
1601ecc4c561SArnaldo Carvalho de Melo 	int err;
1602edbe9817SJiri Olsa 
1603ebccba3fSWang Nan 	if (opts->all) {
1604f5a08cedSWang Nan 		c.tool.comm = process_comm_event;
1605ebccba3fSWang Nan 		c.tool.exit = process_exit_event;
1606ebccba3fSWang Nan 		c.tool.fork = process_fork_event;
1607f9f6f2a9SGeneviève Bastien 		c.tool.mmap = process_mmap_event;
1608f9f6f2a9SGeneviève Bastien 		c.tool.mmap2 = process_mmap2_event;
1609ebccba3fSWang Nan 	}
1610f5a08cedSWang Nan 
1611ecc4c561SArnaldo Carvalho de Melo 	err = perf_config(convert__config, &c);
1612ecc4c561SArnaldo Carvalho de Melo 	if (err)
1613ecc4c561SArnaldo Carvalho de Melo 		return err;
16148fa46753SJiri Olsa 
1615edbe9817SJiri Olsa 	/* CTF writer */
1616edbe9817SJiri Olsa 	if (ctf_writer__init(cw, path))
1617edbe9817SJiri Olsa 		return -1;
1618edbe9817SJiri Olsa 
1619ecc4c561SArnaldo Carvalho de Melo 	err = -1;
1620edbe9817SJiri Olsa 	/* perf.data session */
16218ceb41d7SJiri Olsa 	session = perf_session__new(&data, 0, &c.tool);
1622edbe9817SJiri Olsa 	if (!session)
1623edbe9817SJiri Olsa 		goto free_writer;
1624edbe9817SJiri Olsa 
16258fa46753SJiri Olsa 	if (c.queue_size) {
16268fa46753SJiri Olsa 		ordered_events__set_alloc_size(&session->ordered_events,
16278fa46753SJiri Olsa 					       c.queue_size);
16288fa46753SJiri Olsa 	}
16298fa46753SJiri Olsa 
1630edbe9817SJiri Olsa 	/* CTF writer env/clock setup  */
1631edbe9817SJiri Olsa 	if (ctf_writer__setup_env(cw, session))
1632edbe9817SJiri Olsa 		goto free_session;
1633edbe9817SJiri Olsa 
1634edbe9817SJiri Olsa 	/* CTF events setup */
1635edbe9817SJiri Olsa 	if (setup_events(cw, session))
1636edbe9817SJiri Olsa 		goto free_session;
1637edbe9817SJiri Olsa 
1638f5a08cedSWang Nan 	if (opts->all && setup_non_sample_events(cw, session))
1639f5a08cedSWang Nan 		goto free_session;
1640f5a08cedSWang Nan 
164190e129ffSSebastian Andrzej Siewior 	if (setup_streams(cw, session))
164290e129ffSSebastian Andrzej Siewior 		goto free_session;
164390e129ffSSebastian Andrzej Siewior 
1644b7b61cbeSArnaldo Carvalho de Melo 	err = perf_session__process_events(session);
1645edbe9817SJiri Olsa 	if (!err)
164690e129ffSSebastian Andrzej Siewior 		err = ctf_writer__flush_streams(cw);
1647c2141055SHe Kuang 	else
1648c2141055SHe Kuang 		pr_err("Error during conversion.\n");
1649edbe9817SJiri Olsa 
1650edbe9817SJiri Olsa 	fprintf(stderr,
1651edbe9817SJiri Olsa 		"[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
16522d4f2799SJiri Olsa 		data.path, path);
1653edbe9817SJiri Olsa 
1654edbe9817SJiri Olsa 	fprintf(stderr,
16558ee4c46cSWang Nan 		"[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
1656edbe9817SJiri Olsa 		(double) c.events_size / 1024.0 / 1024.0,
1657edbe9817SJiri Olsa 		c.events_count);
1658edbe9817SJiri Olsa 
16598ee4c46cSWang Nan 	if (!c.non_sample_count)
16608ee4c46cSWang Nan 		fprintf(stderr, ") ]\n");
16618ee4c46cSWang Nan 	else
16628ee4c46cSWang Nan 		fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count);
16638ee4c46cSWang Nan 
16645141d735SWang Nan 	cleanup_events(session);
1665c2141055SHe Kuang 	perf_session__delete(session);
1666c2141055SHe Kuang 	ctf_writer__cleanup(cw);
1667c2141055SHe Kuang 
1668c2141055SHe Kuang 	return err;
1669c2141055SHe Kuang 
1670edbe9817SJiri Olsa free_session:
1671edbe9817SJiri Olsa 	perf_session__delete(session);
1672edbe9817SJiri Olsa free_writer:
1673edbe9817SJiri Olsa 	ctf_writer__cleanup(cw);
1674c2141055SHe Kuang 	pr_err("Error during conversion setup.\n");
1675edbe9817SJiri Olsa 	return err;
1676edbe9817SJiri Olsa }
1677