1edbe9817SJiri Olsa /* 2edbe9817SJiri Olsa * CTF writing support via babeltrace. 3edbe9817SJiri Olsa * 4edbe9817SJiri Olsa * Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com> 5edbe9817SJiri Olsa * Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de> 6edbe9817SJiri Olsa * 7edbe9817SJiri Olsa * Released under the GPL v2. (and only v2, not any later version) 8edbe9817SJiri Olsa */ 9edbe9817SJiri Olsa 10a43783aeSArnaldo Carvalho de Melo #include <errno.h> 11fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 12edbe9817SJiri Olsa #include <linux/compiler.h> 13877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.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 "util.h" 27edbe9817SJiri Olsa #include "debug.h" 28edbe9817SJiri Olsa #include "tool.h" 29edbe9817SJiri Olsa #include "evlist.h" 30edbe9817SJiri Olsa #include "evsel.h" 31edbe9817SJiri Olsa #include "machine.h" 32f6c12a00SJiri Olsa #include "config.h" 333d689ed6SArnaldo Carvalho de Melo #include "sane_ctype.h" 34edbe9817SJiri Olsa 35edbe9817SJiri Olsa #define pr_N(n, fmt, ...) \ 36edbe9817SJiri Olsa eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__) 37edbe9817SJiri Olsa 38edbe9817SJiri Olsa #define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__) 39edbe9817SJiri Olsa #define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__) 40edbe9817SJiri Olsa 41edbe9817SJiri Olsa #define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__) 42edbe9817SJiri Olsa 43edbe9817SJiri Olsa struct evsel_priv { 44edbe9817SJiri Olsa struct bt_ctf_event_class *event_class; 45edbe9817SJiri Olsa }; 46edbe9817SJiri Olsa 4790e129ffSSebastian Andrzej Siewior #define MAX_CPUS 4096 4890e129ffSSebastian Andrzej Siewior 4990e129ffSSebastian Andrzej Siewior struct ctf_stream { 5090e129ffSSebastian Andrzej Siewior struct bt_ctf_stream *stream; 5190e129ffSSebastian Andrzej Siewior int cpu; 5289e5fa88SJiri Olsa u32 count; 5390e129ffSSebastian Andrzej Siewior }; 5490e129ffSSebastian Andrzej Siewior 55edbe9817SJiri Olsa struct ctf_writer { 56edbe9817SJiri Olsa /* writer primitives */ 57edbe9817SJiri Olsa struct bt_ctf_writer *writer; 5890e129ffSSebastian Andrzej Siewior struct ctf_stream **stream; 5990e129ffSSebastian Andrzej Siewior int stream_cnt; 60edbe9817SJiri Olsa struct bt_ctf_stream_class *stream_class; 61edbe9817SJiri Olsa struct bt_ctf_clock *clock; 62edbe9817SJiri Olsa 63edbe9817SJiri Olsa /* data types */ 64edbe9817SJiri Olsa union { 65edbe9817SJiri Olsa struct { 66edbe9817SJiri Olsa struct bt_ctf_field_type *s64; 67edbe9817SJiri Olsa struct bt_ctf_field_type *u64; 68edbe9817SJiri Olsa struct bt_ctf_field_type *s32; 69edbe9817SJiri Olsa struct bt_ctf_field_type *u32; 70edbe9817SJiri Olsa struct bt_ctf_field_type *string; 7126812d46SWang Nan struct bt_ctf_field_type *u32_hex; 72edbe9817SJiri Olsa struct bt_ctf_field_type *u64_hex; 73edbe9817SJiri Olsa }; 74edbe9817SJiri Olsa struct bt_ctf_field_type *array[6]; 75edbe9817SJiri Olsa } data; 76f5a08cedSWang Nan struct bt_ctf_event_class *comm_class; 77ebccba3fSWang Nan struct bt_ctf_event_class *exit_class; 78ebccba3fSWang Nan struct bt_ctf_event_class *fork_class; 79edbe9817SJiri Olsa }; 80edbe9817SJiri Olsa 81edbe9817SJiri Olsa struct convert { 82edbe9817SJiri Olsa struct perf_tool tool; 83edbe9817SJiri Olsa struct ctf_writer writer; 84edbe9817SJiri Olsa 85edbe9817SJiri Olsa u64 events_size; 86edbe9817SJiri Olsa u64 events_count; 878ee4c46cSWang Nan u64 non_sample_count; 888fa46753SJiri Olsa 898fa46753SJiri Olsa /* Ordered events configured queue size. */ 908fa46753SJiri Olsa u64 queue_size; 91edbe9817SJiri Olsa }; 92edbe9817SJiri Olsa 93edbe9817SJiri Olsa static int value_set(struct bt_ctf_field_type *type, 94edbe9817SJiri Olsa struct bt_ctf_event *event, 95edbe9817SJiri Olsa const char *name, u64 val) 96edbe9817SJiri Olsa { 97edbe9817SJiri Olsa struct bt_ctf_field *field; 98edbe9817SJiri Olsa bool sign = bt_ctf_field_type_integer_get_signed(type); 99edbe9817SJiri Olsa int ret; 100edbe9817SJiri Olsa 101edbe9817SJiri Olsa field = bt_ctf_field_create(type); 102edbe9817SJiri Olsa if (!field) { 103edbe9817SJiri Olsa pr_err("failed to create a field %s\n", name); 104edbe9817SJiri Olsa return -1; 105edbe9817SJiri Olsa } 106edbe9817SJiri Olsa 107edbe9817SJiri Olsa if (sign) { 108edbe9817SJiri Olsa ret = bt_ctf_field_signed_integer_set_value(field, val); 109edbe9817SJiri Olsa if (ret) { 110edbe9817SJiri Olsa pr_err("failed to set field value %s\n", name); 111edbe9817SJiri Olsa goto err; 112edbe9817SJiri Olsa } 113edbe9817SJiri Olsa } else { 114edbe9817SJiri Olsa ret = bt_ctf_field_unsigned_integer_set_value(field, val); 115edbe9817SJiri Olsa if (ret) { 116edbe9817SJiri Olsa pr_err("failed to set field value %s\n", name); 117edbe9817SJiri Olsa goto err; 118edbe9817SJiri Olsa } 119edbe9817SJiri Olsa } 120edbe9817SJiri Olsa 121edbe9817SJiri Olsa ret = bt_ctf_event_set_payload(event, name, field); 122edbe9817SJiri Olsa if (ret) { 123edbe9817SJiri Olsa pr_err("failed to set payload %s\n", name); 124edbe9817SJiri Olsa goto err; 125edbe9817SJiri Olsa } 126edbe9817SJiri Olsa 127edbe9817SJiri Olsa pr2(" SET [%s = %" PRIu64 "]\n", name, val); 128edbe9817SJiri Olsa 129edbe9817SJiri Olsa err: 130edbe9817SJiri Olsa bt_ctf_field_put(field); 131edbe9817SJiri Olsa return ret; 132edbe9817SJiri Olsa } 133edbe9817SJiri Olsa 134edbe9817SJiri Olsa #define __FUNC_VALUE_SET(_name, _val_type) \ 135edbe9817SJiri Olsa static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \ 136edbe9817SJiri Olsa struct bt_ctf_event *event, \ 137edbe9817SJiri Olsa const char *name, \ 138edbe9817SJiri Olsa _val_type val) \ 139edbe9817SJiri Olsa { \ 140edbe9817SJiri Olsa struct bt_ctf_field_type *type = cw->data._name; \ 141edbe9817SJiri Olsa return value_set(type, event, name, (u64) val); \ 142edbe9817SJiri Olsa } 143edbe9817SJiri Olsa 144edbe9817SJiri Olsa #define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name) 145edbe9817SJiri Olsa 146edbe9817SJiri Olsa FUNC_VALUE_SET(s32) 147edbe9817SJiri Olsa FUNC_VALUE_SET(u32) 148edbe9817SJiri Olsa FUNC_VALUE_SET(s64) 149edbe9817SJiri Olsa FUNC_VALUE_SET(u64) 150edbe9817SJiri Olsa __FUNC_VALUE_SET(u64_hex, u64) 151edbe9817SJiri Olsa 152069ee5c4SWang Nan static int string_set_value(struct bt_ctf_field *field, const char *string); 153069ee5c4SWang Nan static __maybe_unused int 154069ee5c4SWang Nan value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event, 155069ee5c4SWang Nan const char *name, const char *string) 156069ee5c4SWang Nan { 157069ee5c4SWang Nan struct bt_ctf_field_type *type = cw->data.string; 158069ee5c4SWang Nan struct bt_ctf_field *field; 159069ee5c4SWang Nan int ret = 0; 160069ee5c4SWang Nan 161069ee5c4SWang Nan field = bt_ctf_field_create(type); 162069ee5c4SWang Nan if (!field) { 163069ee5c4SWang Nan pr_err("failed to create a field %s\n", name); 164069ee5c4SWang Nan return -1; 165069ee5c4SWang Nan } 166069ee5c4SWang Nan 167069ee5c4SWang Nan ret = string_set_value(field, string); 168069ee5c4SWang Nan if (ret) { 169069ee5c4SWang Nan pr_err("failed to set value %s\n", name); 170069ee5c4SWang Nan goto err_put_field; 171069ee5c4SWang Nan } 172069ee5c4SWang Nan 173069ee5c4SWang Nan ret = bt_ctf_event_set_payload(event, name, field); 174069ee5c4SWang Nan if (ret) 175069ee5c4SWang Nan pr_err("failed to set payload %s\n", name); 176069ee5c4SWang Nan 177069ee5c4SWang Nan err_put_field: 178069ee5c4SWang Nan bt_ctf_field_put(field); 179069ee5c4SWang Nan return ret; 180069ee5c4SWang Nan } 181069ee5c4SWang Nan 18269364727SSebastian Andrzej Siewior static struct bt_ctf_field_type* 18369364727SSebastian Andrzej Siewior get_tracepoint_field_type(struct ctf_writer *cw, struct format_field *field) 18469364727SSebastian Andrzej Siewior { 18569364727SSebastian Andrzej Siewior unsigned long flags = field->flags; 18669364727SSebastian Andrzej Siewior 18769364727SSebastian Andrzej Siewior if (flags & FIELD_IS_STRING) 18869364727SSebastian Andrzej Siewior return cw->data.string; 18969364727SSebastian Andrzej Siewior 19069364727SSebastian Andrzej Siewior if (!(flags & FIELD_IS_SIGNED)) { 19169364727SSebastian Andrzej Siewior /* unsigned long are mostly pointers */ 19269364727SSebastian Andrzej Siewior if (flags & FIELD_IS_LONG || flags & FIELD_IS_POINTER) 19369364727SSebastian Andrzej Siewior return cw->data.u64_hex; 19469364727SSebastian Andrzej Siewior } 19569364727SSebastian Andrzej Siewior 19669364727SSebastian Andrzej Siewior if (flags & FIELD_IS_SIGNED) { 19769364727SSebastian Andrzej Siewior if (field->size == 8) 19869364727SSebastian Andrzej Siewior return cw->data.s64; 19969364727SSebastian Andrzej Siewior else 20069364727SSebastian Andrzej Siewior return cw->data.s32; 20169364727SSebastian Andrzej Siewior } 20269364727SSebastian Andrzej Siewior 20369364727SSebastian Andrzej Siewior if (field->size == 8) 20469364727SSebastian Andrzej Siewior return cw->data.u64; 20569364727SSebastian Andrzej Siewior else 20669364727SSebastian Andrzej Siewior return cw->data.u32; 20769364727SSebastian Andrzej Siewior } 20869364727SSebastian Andrzej Siewior 209d4ae4213SWang Nan static unsigned long long adjust_signedness(unsigned long long value_int, int size) 210d4ae4213SWang Nan { 211d4ae4213SWang Nan unsigned long long value_mask; 212d4ae4213SWang Nan 213d4ae4213SWang Nan /* 214d4ae4213SWang Nan * value_mask = (1 << (size * 8 - 1)) - 1. 215d4ae4213SWang Nan * Directly set value_mask for code readers. 216d4ae4213SWang Nan */ 217d4ae4213SWang Nan switch (size) { 218d4ae4213SWang Nan case 1: 219d4ae4213SWang Nan value_mask = 0x7fULL; 220d4ae4213SWang Nan break; 221d4ae4213SWang Nan case 2: 222d4ae4213SWang Nan value_mask = 0x7fffULL; 223d4ae4213SWang Nan break; 224d4ae4213SWang Nan case 4: 225d4ae4213SWang Nan value_mask = 0x7fffffffULL; 226d4ae4213SWang Nan break; 227d4ae4213SWang Nan case 8: 228d4ae4213SWang Nan /* 229d4ae4213SWang Nan * For 64 bit value, return it self. There is no need 230d4ae4213SWang Nan * to fill high bit. 231d4ae4213SWang Nan */ 232d4ae4213SWang Nan /* Fall through */ 233d4ae4213SWang Nan default: 234d4ae4213SWang Nan /* BUG! */ 235d4ae4213SWang Nan return value_int; 236d4ae4213SWang Nan } 237d4ae4213SWang Nan 238d4ae4213SWang Nan /* If it is a positive value, don't adjust. */ 239d4ae4213SWang Nan if ((value_int & (~0ULL - value_mask)) == 0) 240d4ae4213SWang Nan return value_int; 241d4ae4213SWang Nan 242d4ae4213SWang Nan /* Fill upper part of value_int with 1 to make it a negative long long. */ 243d4ae4213SWang Nan return (value_int & value_mask) | ~value_mask; 244d4ae4213SWang Nan } 245d4ae4213SWang Nan 2465ea5888bSWang Nan static int string_set_value(struct bt_ctf_field *field, const char *string) 2475ea5888bSWang Nan { 2485ea5888bSWang Nan char *buffer = NULL; 2495ea5888bSWang Nan size_t len = strlen(string), i, p; 2505ea5888bSWang Nan int err; 2515ea5888bSWang Nan 2525ea5888bSWang Nan for (i = p = 0; i < len; i++, p++) { 2535ea5888bSWang Nan if (isprint(string[i])) { 2545ea5888bSWang Nan if (!buffer) 2555ea5888bSWang Nan continue; 2565ea5888bSWang Nan buffer[p] = string[i]; 2575ea5888bSWang Nan } else { 2585ea5888bSWang Nan char numstr[5]; 2595ea5888bSWang Nan 2605ea5888bSWang Nan snprintf(numstr, sizeof(numstr), "\\x%02x", 2615ea5888bSWang Nan (unsigned int)(string[i]) & 0xff); 2625ea5888bSWang Nan 2635ea5888bSWang Nan if (!buffer) { 2645ea5888bSWang Nan buffer = zalloc(i + (len - i) * 4 + 2); 2655ea5888bSWang Nan if (!buffer) { 2665ea5888bSWang Nan pr_err("failed to set unprintable string '%s'\n", string); 2675ea5888bSWang Nan return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING"); 2685ea5888bSWang Nan } 2695ea5888bSWang Nan if (i > 0) 2705ea5888bSWang Nan strncpy(buffer, string, i); 2715ea5888bSWang Nan } 2725ea5888bSWang Nan strncat(buffer + p, numstr, 4); 2735ea5888bSWang Nan p += 3; 2745ea5888bSWang Nan } 2755ea5888bSWang Nan } 2765ea5888bSWang Nan 2775ea5888bSWang Nan if (!buffer) 2785ea5888bSWang Nan return bt_ctf_field_string_set_value(field, string); 2795ea5888bSWang Nan err = bt_ctf_field_string_set_value(field, buffer); 2805ea5888bSWang Nan free(buffer); 2815ea5888bSWang Nan return err; 2825ea5888bSWang Nan } 2835ea5888bSWang Nan 28469364727SSebastian Andrzej Siewior static int add_tracepoint_field_value(struct ctf_writer *cw, 28569364727SSebastian Andrzej Siewior struct bt_ctf_event_class *event_class, 28669364727SSebastian Andrzej Siewior struct bt_ctf_event *event, 28769364727SSebastian Andrzej Siewior struct perf_sample *sample, 28869364727SSebastian Andrzej Siewior struct format_field *fmtf) 28969364727SSebastian Andrzej Siewior { 29069364727SSebastian Andrzej Siewior struct bt_ctf_field_type *type; 29169364727SSebastian Andrzej Siewior struct bt_ctf_field *array_field; 29269364727SSebastian Andrzej Siewior struct bt_ctf_field *field; 29369364727SSebastian Andrzej Siewior const char *name = fmtf->name; 29469364727SSebastian Andrzej Siewior void *data = sample->raw_data; 29569364727SSebastian Andrzej Siewior unsigned long flags = fmtf->flags; 29669364727SSebastian Andrzej Siewior unsigned int n_items; 29769364727SSebastian Andrzej Siewior unsigned int i; 29869364727SSebastian Andrzej Siewior unsigned int offset; 29969364727SSebastian Andrzej Siewior unsigned int len; 30069364727SSebastian Andrzej Siewior int ret; 30169364727SSebastian Andrzej Siewior 302e0a7cce5SWang Nan name = fmtf->alias; 30369364727SSebastian Andrzej Siewior offset = fmtf->offset; 30469364727SSebastian Andrzej Siewior len = fmtf->size; 30569364727SSebastian Andrzej Siewior if (flags & FIELD_IS_STRING) 30669364727SSebastian Andrzej Siewior flags &= ~FIELD_IS_ARRAY; 30769364727SSebastian Andrzej Siewior 30869364727SSebastian Andrzej Siewior if (flags & FIELD_IS_DYNAMIC) { 30969364727SSebastian Andrzej Siewior unsigned long long tmp_val; 31069364727SSebastian Andrzej Siewior 31169364727SSebastian Andrzej Siewior tmp_val = pevent_read_number(fmtf->event->pevent, 31269364727SSebastian Andrzej Siewior data + offset, len); 31369364727SSebastian Andrzej Siewior offset = tmp_val; 31469364727SSebastian Andrzej Siewior len = offset >> 16; 31569364727SSebastian Andrzej Siewior offset &= 0xffff; 31669364727SSebastian Andrzej Siewior } 31769364727SSebastian Andrzej Siewior 31869364727SSebastian Andrzej Siewior if (flags & FIELD_IS_ARRAY) { 31969364727SSebastian Andrzej Siewior 32069364727SSebastian Andrzej Siewior type = bt_ctf_event_class_get_field_by_name( 32169364727SSebastian Andrzej Siewior event_class, name); 32269364727SSebastian Andrzej Siewior array_field = bt_ctf_field_create(type); 32369364727SSebastian Andrzej Siewior bt_ctf_field_type_put(type); 32469364727SSebastian Andrzej Siewior if (!array_field) { 32569364727SSebastian Andrzej Siewior pr_err("Failed to create array type %s\n", name); 32669364727SSebastian Andrzej Siewior return -1; 32769364727SSebastian Andrzej Siewior } 32869364727SSebastian Andrzej Siewior 32969364727SSebastian Andrzej Siewior len = fmtf->size / fmtf->arraylen; 33069364727SSebastian Andrzej Siewior n_items = fmtf->arraylen; 33169364727SSebastian Andrzej Siewior } else { 33269364727SSebastian Andrzej Siewior n_items = 1; 33369364727SSebastian Andrzej Siewior array_field = NULL; 33469364727SSebastian Andrzej Siewior } 33569364727SSebastian Andrzej Siewior 33669364727SSebastian Andrzej Siewior type = get_tracepoint_field_type(cw, fmtf); 33769364727SSebastian Andrzej Siewior 33869364727SSebastian Andrzej Siewior for (i = 0; i < n_items; i++) { 33969364727SSebastian Andrzej Siewior if (flags & FIELD_IS_ARRAY) 34069364727SSebastian Andrzej Siewior field = bt_ctf_field_array_get_field(array_field, i); 34169364727SSebastian Andrzej Siewior else 34269364727SSebastian Andrzej Siewior field = bt_ctf_field_create(type); 34369364727SSebastian Andrzej Siewior 34469364727SSebastian Andrzej Siewior if (!field) { 34569364727SSebastian Andrzej Siewior pr_err("failed to create a field %s\n", name); 34669364727SSebastian Andrzej Siewior return -1; 34769364727SSebastian Andrzej Siewior } 34869364727SSebastian Andrzej Siewior 34969364727SSebastian Andrzej Siewior if (flags & FIELD_IS_STRING) 3505ea5888bSWang Nan ret = string_set_value(field, data + offset + i * len); 351d4ae4213SWang Nan else { 352d4ae4213SWang Nan unsigned long long value_int; 353d4ae4213SWang Nan 354d4ae4213SWang Nan value_int = pevent_read_number( 355d4ae4213SWang Nan fmtf->event->pevent, 356d4ae4213SWang Nan data + offset + i * len, len); 357d4ae4213SWang Nan 358d4ae4213SWang Nan if (!(flags & FIELD_IS_SIGNED)) 35969364727SSebastian Andrzej Siewior ret = bt_ctf_field_unsigned_integer_set_value( 36069364727SSebastian Andrzej Siewior field, value_int); 36169364727SSebastian Andrzej Siewior else 36269364727SSebastian Andrzej Siewior ret = bt_ctf_field_signed_integer_set_value( 363d4ae4213SWang Nan field, adjust_signedness(value_int, len)); 364d4ae4213SWang Nan } 365d4ae4213SWang Nan 36669364727SSebastian Andrzej Siewior if (ret) { 36769364727SSebastian Andrzej Siewior pr_err("failed to set file value %s\n", name); 36869364727SSebastian Andrzej Siewior goto err_put_field; 36969364727SSebastian Andrzej Siewior } 37069364727SSebastian Andrzej Siewior if (!(flags & FIELD_IS_ARRAY)) { 37169364727SSebastian Andrzej Siewior ret = bt_ctf_event_set_payload(event, name, field); 37269364727SSebastian Andrzej Siewior if (ret) { 37369364727SSebastian Andrzej Siewior pr_err("failed to set payload %s\n", name); 37469364727SSebastian Andrzej Siewior goto err_put_field; 37569364727SSebastian Andrzej Siewior } 37669364727SSebastian Andrzej Siewior } 37769364727SSebastian Andrzej Siewior bt_ctf_field_put(field); 37869364727SSebastian Andrzej Siewior } 37969364727SSebastian Andrzej Siewior if (flags & FIELD_IS_ARRAY) { 38069364727SSebastian Andrzej Siewior ret = bt_ctf_event_set_payload(event, name, array_field); 38169364727SSebastian Andrzej Siewior if (ret) { 38269364727SSebastian Andrzej Siewior pr_err("Failed add payload array %s\n", name); 38369364727SSebastian Andrzej Siewior return -1; 38469364727SSebastian Andrzej Siewior } 38569364727SSebastian Andrzej Siewior bt_ctf_field_put(array_field); 38669364727SSebastian Andrzej Siewior } 38769364727SSebastian Andrzej Siewior return 0; 38869364727SSebastian Andrzej Siewior 38969364727SSebastian Andrzej Siewior err_put_field: 39069364727SSebastian Andrzej Siewior bt_ctf_field_put(field); 39169364727SSebastian Andrzej Siewior return -1; 39269364727SSebastian Andrzej Siewior } 39369364727SSebastian Andrzej Siewior 39469364727SSebastian Andrzej Siewior static int add_tracepoint_fields_values(struct ctf_writer *cw, 39569364727SSebastian Andrzej Siewior struct bt_ctf_event_class *event_class, 39669364727SSebastian Andrzej Siewior struct bt_ctf_event *event, 39769364727SSebastian Andrzej Siewior struct format_field *fields, 39869364727SSebastian Andrzej Siewior struct perf_sample *sample) 39969364727SSebastian Andrzej Siewior { 40069364727SSebastian Andrzej Siewior struct format_field *field; 40169364727SSebastian Andrzej Siewior int ret; 40269364727SSebastian Andrzej Siewior 40369364727SSebastian Andrzej Siewior for (field = fields; field; field = field->next) { 40469364727SSebastian Andrzej Siewior ret = add_tracepoint_field_value(cw, event_class, event, sample, 40569364727SSebastian Andrzej Siewior field); 40669364727SSebastian Andrzej Siewior if (ret) 40769364727SSebastian Andrzej Siewior return -1; 40869364727SSebastian Andrzej Siewior } 40969364727SSebastian Andrzej Siewior return 0; 41069364727SSebastian Andrzej Siewior } 41169364727SSebastian Andrzej Siewior 41269364727SSebastian Andrzej Siewior static int add_tracepoint_values(struct ctf_writer *cw, 41369364727SSebastian Andrzej Siewior struct bt_ctf_event_class *event_class, 41469364727SSebastian Andrzej Siewior struct bt_ctf_event *event, 41569364727SSebastian Andrzej Siewior struct perf_evsel *evsel, 41669364727SSebastian Andrzej Siewior struct perf_sample *sample) 41769364727SSebastian Andrzej Siewior { 41869364727SSebastian Andrzej Siewior struct format_field *common_fields = evsel->tp_format->format.common_fields; 41969364727SSebastian Andrzej Siewior struct format_field *fields = evsel->tp_format->format.fields; 42069364727SSebastian Andrzej Siewior int ret; 42169364727SSebastian Andrzej Siewior 42269364727SSebastian Andrzej Siewior ret = add_tracepoint_fields_values(cw, event_class, event, 42369364727SSebastian Andrzej Siewior common_fields, sample); 42469364727SSebastian Andrzej Siewior if (!ret) 42569364727SSebastian Andrzej Siewior ret = add_tracepoint_fields_values(cw, event_class, event, 42669364727SSebastian Andrzej Siewior fields, sample); 42769364727SSebastian Andrzej Siewior 42869364727SSebastian Andrzej Siewior return ret; 42969364727SSebastian Andrzej Siewior } 43069364727SSebastian Andrzej Siewior 4316122d57eSWang Nan static int 4326122d57eSWang Nan add_bpf_output_values(struct bt_ctf_event_class *event_class, 4336122d57eSWang Nan struct bt_ctf_event *event, 4346122d57eSWang Nan struct perf_sample *sample) 4356122d57eSWang Nan { 4366122d57eSWang Nan struct bt_ctf_field_type *len_type, *seq_type; 4376122d57eSWang Nan struct bt_ctf_field *len_field, *seq_field; 4386122d57eSWang Nan unsigned int raw_size = sample->raw_size; 4396122d57eSWang Nan unsigned int nr_elements = raw_size / sizeof(u32); 4406122d57eSWang Nan unsigned int i; 4416122d57eSWang Nan int ret; 4426122d57eSWang Nan 4436122d57eSWang Nan if (nr_elements * sizeof(u32) != raw_size) 444f2c8852eSWang Nan pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n", 4456122d57eSWang Nan raw_size, nr_elements * sizeof(u32) - raw_size); 4466122d57eSWang Nan 4476122d57eSWang Nan len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len"); 4486122d57eSWang Nan len_field = bt_ctf_field_create(len_type); 4496122d57eSWang Nan if (!len_field) { 4506122d57eSWang Nan pr_err("failed to create 'raw_len' for bpf output event\n"); 4516122d57eSWang Nan ret = -1; 4526122d57eSWang Nan goto put_len_type; 4536122d57eSWang Nan } 4546122d57eSWang Nan 4556122d57eSWang Nan ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements); 4566122d57eSWang Nan if (ret) { 4576122d57eSWang Nan pr_err("failed to set field value for raw_len\n"); 4586122d57eSWang Nan goto put_len_field; 4596122d57eSWang Nan } 4606122d57eSWang Nan ret = bt_ctf_event_set_payload(event, "raw_len", len_field); 4616122d57eSWang Nan if (ret) { 4626122d57eSWang Nan pr_err("failed to set payload to raw_len\n"); 4636122d57eSWang Nan goto put_len_field; 4646122d57eSWang Nan } 4656122d57eSWang Nan 4666122d57eSWang Nan seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data"); 4676122d57eSWang Nan seq_field = bt_ctf_field_create(seq_type); 4686122d57eSWang Nan if (!seq_field) { 4696122d57eSWang Nan pr_err("failed to create 'raw_data' for bpf output event\n"); 4706122d57eSWang Nan ret = -1; 4716122d57eSWang Nan goto put_seq_type; 4726122d57eSWang Nan } 4736122d57eSWang Nan 4746122d57eSWang Nan ret = bt_ctf_field_sequence_set_length(seq_field, len_field); 4756122d57eSWang Nan if (ret) { 4766122d57eSWang Nan pr_err("failed to set length of 'raw_data'\n"); 4776122d57eSWang Nan goto put_seq_field; 4786122d57eSWang Nan } 4796122d57eSWang Nan 4806122d57eSWang Nan for (i = 0; i < nr_elements; i++) { 4816122d57eSWang Nan struct bt_ctf_field *elem_field = 4826122d57eSWang Nan bt_ctf_field_sequence_get_field(seq_field, i); 4836122d57eSWang Nan 4846122d57eSWang Nan ret = bt_ctf_field_unsigned_integer_set_value(elem_field, 4856122d57eSWang Nan ((u32 *)(sample->raw_data))[i]); 4866122d57eSWang Nan 4876122d57eSWang Nan bt_ctf_field_put(elem_field); 4886122d57eSWang Nan if (ret) { 4896122d57eSWang Nan pr_err("failed to set raw_data[%d]\n", i); 4906122d57eSWang Nan goto put_seq_field; 4916122d57eSWang Nan } 4926122d57eSWang Nan } 4936122d57eSWang Nan 4946122d57eSWang Nan ret = bt_ctf_event_set_payload(event, "raw_data", seq_field); 4956122d57eSWang Nan if (ret) 4966122d57eSWang Nan pr_err("failed to set payload for raw_data\n"); 4976122d57eSWang Nan 4986122d57eSWang Nan put_seq_field: 4996122d57eSWang Nan bt_ctf_field_put(seq_field); 5006122d57eSWang Nan put_seq_type: 5016122d57eSWang Nan bt_ctf_field_type_put(seq_type); 5026122d57eSWang Nan put_len_field: 5036122d57eSWang Nan bt_ctf_field_put(len_field); 5046122d57eSWang Nan put_len_type: 5056122d57eSWang Nan bt_ctf_field_type_put(len_type); 5066122d57eSWang Nan return ret; 5076122d57eSWang Nan } 5086122d57eSWang Nan 509*a3073c8eSGeneviève Bastien static int 510*a3073c8eSGeneviève Bastien add_callchain_output_values(struct bt_ctf_event_class *event_class, 511*a3073c8eSGeneviève Bastien struct bt_ctf_event *event, 512*a3073c8eSGeneviève Bastien struct ip_callchain *callchain) 513*a3073c8eSGeneviève Bastien { 514*a3073c8eSGeneviève Bastien struct bt_ctf_field_type *len_type, *seq_type; 515*a3073c8eSGeneviève Bastien struct bt_ctf_field *len_field, *seq_field; 516*a3073c8eSGeneviève Bastien unsigned int nr_elements = callchain->nr; 517*a3073c8eSGeneviève Bastien unsigned int i; 518*a3073c8eSGeneviève Bastien int ret; 519*a3073c8eSGeneviève Bastien 520*a3073c8eSGeneviève Bastien len_type = bt_ctf_event_class_get_field_by_name( 521*a3073c8eSGeneviève Bastien event_class, "perf_callchain_size"); 522*a3073c8eSGeneviève Bastien len_field = bt_ctf_field_create(len_type); 523*a3073c8eSGeneviève Bastien if (!len_field) { 524*a3073c8eSGeneviève Bastien pr_err("failed to create 'perf_callchain_size' for callchain output event\n"); 525*a3073c8eSGeneviève Bastien ret = -1; 526*a3073c8eSGeneviève Bastien goto put_len_type; 527*a3073c8eSGeneviève Bastien } 528*a3073c8eSGeneviève Bastien 529*a3073c8eSGeneviève Bastien ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements); 530*a3073c8eSGeneviève Bastien if (ret) { 531*a3073c8eSGeneviève Bastien pr_err("failed to set field value for perf_callchain_size\n"); 532*a3073c8eSGeneviève Bastien goto put_len_field; 533*a3073c8eSGeneviève Bastien } 534*a3073c8eSGeneviève Bastien ret = bt_ctf_event_set_payload(event, "perf_callchain_size", len_field); 535*a3073c8eSGeneviève Bastien if (ret) { 536*a3073c8eSGeneviève Bastien pr_err("failed to set payload to perf_callchain_size\n"); 537*a3073c8eSGeneviève Bastien goto put_len_field; 538*a3073c8eSGeneviève Bastien } 539*a3073c8eSGeneviève Bastien 540*a3073c8eSGeneviève Bastien seq_type = bt_ctf_event_class_get_field_by_name( 541*a3073c8eSGeneviève Bastien event_class, "perf_callchain"); 542*a3073c8eSGeneviève Bastien seq_field = bt_ctf_field_create(seq_type); 543*a3073c8eSGeneviève Bastien if (!seq_field) { 544*a3073c8eSGeneviève Bastien pr_err("failed to create 'perf_callchain' for callchain output event\n"); 545*a3073c8eSGeneviève Bastien ret = -1; 546*a3073c8eSGeneviève Bastien goto put_seq_type; 547*a3073c8eSGeneviève Bastien } 548*a3073c8eSGeneviève Bastien 549*a3073c8eSGeneviève Bastien ret = bt_ctf_field_sequence_set_length(seq_field, len_field); 550*a3073c8eSGeneviève Bastien if (ret) { 551*a3073c8eSGeneviève Bastien pr_err("failed to set length of 'perf_callchain'\n"); 552*a3073c8eSGeneviève Bastien goto put_seq_field; 553*a3073c8eSGeneviève Bastien } 554*a3073c8eSGeneviève Bastien 555*a3073c8eSGeneviève Bastien for (i = 0; i < nr_elements; i++) { 556*a3073c8eSGeneviève Bastien struct bt_ctf_field *elem_field = 557*a3073c8eSGeneviève Bastien bt_ctf_field_sequence_get_field(seq_field, i); 558*a3073c8eSGeneviève Bastien 559*a3073c8eSGeneviève Bastien ret = bt_ctf_field_unsigned_integer_set_value(elem_field, 560*a3073c8eSGeneviève Bastien ((u64 *)(callchain->ips))[i]); 561*a3073c8eSGeneviève Bastien 562*a3073c8eSGeneviève Bastien bt_ctf_field_put(elem_field); 563*a3073c8eSGeneviève Bastien if (ret) { 564*a3073c8eSGeneviève Bastien pr_err("failed to set callchain[%d]\n", i); 565*a3073c8eSGeneviève Bastien goto put_seq_field; 566*a3073c8eSGeneviève Bastien } 567*a3073c8eSGeneviève Bastien } 568*a3073c8eSGeneviève Bastien 569*a3073c8eSGeneviève Bastien ret = bt_ctf_event_set_payload(event, "perf_callchain", seq_field); 570*a3073c8eSGeneviève Bastien if (ret) 571*a3073c8eSGeneviève Bastien pr_err("failed to set payload for raw_data\n"); 572*a3073c8eSGeneviève Bastien 573*a3073c8eSGeneviève Bastien put_seq_field: 574*a3073c8eSGeneviève Bastien bt_ctf_field_put(seq_field); 575*a3073c8eSGeneviève Bastien put_seq_type: 576*a3073c8eSGeneviève Bastien bt_ctf_field_type_put(seq_type); 577*a3073c8eSGeneviève Bastien put_len_field: 578*a3073c8eSGeneviève Bastien bt_ctf_field_put(len_field); 579*a3073c8eSGeneviève Bastien put_len_type: 580*a3073c8eSGeneviève Bastien bt_ctf_field_type_put(len_type); 581*a3073c8eSGeneviève Bastien return ret; 582*a3073c8eSGeneviève Bastien } 583*a3073c8eSGeneviève Bastien 584edbe9817SJiri Olsa static int add_generic_values(struct ctf_writer *cw, 585edbe9817SJiri Olsa struct bt_ctf_event *event, 586edbe9817SJiri Olsa struct perf_evsel *evsel, 587edbe9817SJiri Olsa struct perf_sample *sample) 588edbe9817SJiri Olsa { 589edbe9817SJiri Olsa u64 type = evsel->attr.sample_type; 590edbe9817SJiri Olsa int ret; 591edbe9817SJiri Olsa 592edbe9817SJiri Olsa /* 593edbe9817SJiri Olsa * missing: 594edbe9817SJiri Olsa * PERF_SAMPLE_TIME - not needed as we have it in 595edbe9817SJiri Olsa * ctf event header 596edbe9817SJiri Olsa * PERF_SAMPLE_READ - TODO 597edbe9817SJiri Olsa * PERF_SAMPLE_RAW - tracepoint fields are handled separately 598edbe9817SJiri Olsa * PERF_SAMPLE_BRANCH_STACK - TODO 599edbe9817SJiri Olsa * PERF_SAMPLE_REGS_USER - TODO 600edbe9817SJiri Olsa * PERF_SAMPLE_STACK_USER - TODO 601edbe9817SJiri Olsa */ 602edbe9817SJiri Olsa 603edbe9817SJiri Olsa if (type & PERF_SAMPLE_IP) { 60454cf776aSSebastian Andrzej Siewior ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip); 605edbe9817SJiri Olsa if (ret) 606edbe9817SJiri Olsa return -1; 607edbe9817SJiri Olsa } 608edbe9817SJiri Olsa 609edbe9817SJiri Olsa if (type & PERF_SAMPLE_TID) { 61054cf776aSSebastian Andrzej Siewior ret = value_set_s32(cw, event, "perf_tid", sample->tid); 611edbe9817SJiri Olsa if (ret) 612edbe9817SJiri Olsa return -1; 613edbe9817SJiri Olsa 61454cf776aSSebastian Andrzej Siewior ret = value_set_s32(cw, event, "perf_pid", sample->pid); 615edbe9817SJiri Olsa if (ret) 616edbe9817SJiri Olsa return -1; 617edbe9817SJiri Olsa } 618edbe9817SJiri Olsa 619edbe9817SJiri Olsa if ((type & PERF_SAMPLE_ID) || 620edbe9817SJiri Olsa (type & PERF_SAMPLE_IDENTIFIER)) { 62154cf776aSSebastian Andrzej Siewior ret = value_set_u64(cw, event, "perf_id", sample->id); 622edbe9817SJiri Olsa if (ret) 623edbe9817SJiri Olsa return -1; 624edbe9817SJiri Olsa } 625edbe9817SJiri Olsa 626edbe9817SJiri Olsa if (type & PERF_SAMPLE_STREAM_ID) { 62754cf776aSSebastian Andrzej Siewior ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id); 628edbe9817SJiri Olsa if (ret) 629edbe9817SJiri Olsa return -1; 630edbe9817SJiri Olsa } 631edbe9817SJiri Olsa 632edbe9817SJiri Olsa if (type & PERF_SAMPLE_PERIOD) { 63354cf776aSSebastian Andrzej Siewior ret = value_set_u64(cw, event, "perf_period", sample->period); 634edbe9817SJiri Olsa if (ret) 635edbe9817SJiri Olsa return -1; 636edbe9817SJiri Olsa } 637edbe9817SJiri Olsa 638edbe9817SJiri Olsa if (type & PERF_SAMPLE_WEIGHT) { 63954cf776aSSebastian Andrzej Siewior ret = value_set_u64(cw, event, "perf_weight", sample->weight); 640edbe9817SJiri Olsa if (ret) 641edbe9817SJiri Olsa return -1; 642edbe9817SJiri Olsa } 643edbe9817SJiri Olsa 644edbe9817SJiri Olsa if (type & PERF_SAMPLE_DATA_SRC) { 64554cf776aSSebastian Andrzej Siewior ret = value_set_u64(cw, event, "perf_data_src", 64654cf776aSSebastian Andrzej Siewior sample->data_src); 647edbe9817SJiri Olsa if (ret) 648edbe9817SJiri Olsa return -1; 649edbe9817SJiri Olsa } 650edbe9817SJiri Olsa 651edbe9817SJiri Olsa if (type & PERF_SAMPLE_TRANSACTION) { 65254cf776aSSebastian Andrzej Siewior ret = value_set_u64(cw, event, "perf_transaction", 65354cf776aSSebastian Andrzej Siewior sample->transaction); 654edbe9817SJiri Olsa if (ret) 655edbe9817SJiri Olsa return -1; 656edbe9817SJiri Olsa } 657edbe9817SJiri Olsa 658edbe9817SJiri Olsa return 0; 659edbe9817SJiri Olsa } 660edbe9817SJiri Olsa 66190e129ffSSebastian Andrzej Siewior static int ctf_stream__flush(struct ctf_stream *cs) 66290e129ffSSebastian Andrzej Siewior { 66390e129ffSSebastian Andrzej Siewior int err = 0; 66490e129ffSSebastian Andrzej Siewior 66590e129ffSSebastian Andrzej Siewior if (cs) { 66690e129ffSSebastian Andrzej Siewior err = bt_ctf_stream_flush(cs->stream); 66790e129ffSSebastian Andrzej Siewior if (err) 66890e129ffSSebastian Andrzej Siewior pr_err("CTF stream %d flush failed\n", cs->cpu); 66990e129ffSSebastian Andrzej Siewior 67089e5fa88SJiri Olsa pr("Flush stream for cpu %d (%u samples)\n", 67189e5fa88SJiri Olsa cs->cpu, cs->count); 67289e5fa88SJiri Olsa 67389e5fa88SJiri Olsa cs->count = 0; 67490e129ffSSebastian Andrzej Siewior } 67590e129ffSSebastian Andrzej Siewior 67690e129ffSSebastian Andrzej Siewior return err; 67790e129ffSSebastian Andrzej Siewior } 67890e129ffSSebastian Andrzej Siewior 67990e129ffSSebastian Andrzej Siewior static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu) 68090e129ffSSebastian Andrzej Siewior { 68190e129ffSSebastian Andrzej Siewior struct ctf_stream *cs; 68290e129ffSSebastian Andrzej Siewior struct bt_ctf_field *pkt_ctx = NULL; 68390e129ffSSebastian Andrzej Siewior struct bt_ctf_field *cpu_field = NULL; 68490e129ffSSebastian Andrzej Siewior struct bt_ctf_stream *stream = NULL; 68590e129ffSSebastian Andrzej Siewior int ret; 68690e129ffSSebastian Andrzej Siewior 68790e129ffSSebastian Andrzej Siewior cs = zalloc(sizeof(*cs)); 68890e129ffSSebastian Andrzej Siewior if (!cs) { 68990e129ffSSebastian Andrzej Siewior pr_err("Failed to allocate ctf stream\n"); 69090e129ffSSebastian Andrzej Siewior return NULL; 69190e129ffSSebastian Andrzej Siewior } 69290e129ffSSebastian Andrzej Siewior 69390e129ffSSebastian Andrzej Siewior stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class); 69490e129ffSSebastian Andrzej Siewior if (!stream) { 69590e129ffSSebastian Andrzej Siewior pr_err("Failed to create CTF stream\n"); 69690e129ffSSebastian Andrzej Siewior goto out; 69790e129ffSSebastian Andrzej Siewior } 69890e129ffSSebastian Andrzej Siewior 69990e129ffSSebastian Andrzej Siewior pkt_ctx = bt_ctf_stream_get_packet_context(stream); 70090e129ffSSebastian Andrzej Siewior if (!pkt_ctx) { 70190e129ffSSebastian Andrzej Siewior pr_err("Failed to obtain packet context\n"); 70290e129ffSSebastian Andrzej Siewior goto out; 70390e129ffSSebastian Andrzej Siewior } 70490e129ffSSebastian Andrzej Siewior 70590e129ffSSebastian Andrzej Siewior cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id"); 70690e129ffSSebastian Andrzej Siewior bt_ctf_field_put(pkt_ctx); 70790e129ffSSebastian Andrzej Siewior if (!cpu_field) { 70890e129ffSSebastian Andrzej Siewior pr_err("Failed to obtain cpu field\n"); 70990e129ffSSebastian Andrzej Siewior goto out; 71090e129ffSSebastian Andrzej Siewior } 71190e129ffSSebastian Andrzej Siewior 71290e129ffSSebastian Andrzej Siewior ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu); 71390e129ffSSebastian Andrzej Siewior if (ret) { 71490e129ffSSebastian Andrzej Siewior pr_err("Failed to update CPU number\n"); 71590e129ffSSebastian Andrzej Siewior goto out; 71690e129ffSSebastian Andrzej Siewior } 71790e129ffSSebastian Andrzej Siewior 71890e129ffSSebastian Andrzej Siewior bt_ctf_field_put(cpu_field); 71990e129ffSSebastian Andrzej Siewior 72090e129ffSSebastian Andrzej Siewior cs->cpu = cpu; 72190e129ffSSebastian Andrzej Siewior cs->stream = stream; 72290e129ffSSebastian Andrzej Siewior return cs; 72390e129ffSSebastian Andrzej Siewior 72490e129ffSSebastian Andrzej Siewior out: 72590e129ffSSebastian Andrzej Siewior if (cpu_field) 72690e129ffSSebastian Andrzej Siewior bt_ctf_field_put(cpu_field); 72790e129ffSSebastian Andrzej Siewior if (stream) 72890e129ffSSebastian Andrzej Siewior bt_ctf_stream_put(stream); 72990e129ffSSebastian Andrzej Siewior 73090e129ffSSebastian Andrzej Siewior free(cs); 73190e129ffSSebastian Andrzej Siewior return NULL; 73290e129ffSSebastian Andrzej Siewior } 73390e129ffSSebastian Andrzej Siewior 73490e129ffSSebastian Andrzej Siewior static void ctf_stream__delete(struct ctf_stream *cs) 73590e129ffSSebastian Andrzej Siewior { 73690e129ffSSebastian Andrzej Siewior if (cs) { 73790e129ffSSebastian Andrzej Siewior bt_ctf_stream_put(cs->stream); 73890e129ffSSebastian Andrzej Siewior free(cs); 73990e129ffSSebastian Andrzej Siewior } 74090e129ffSSebastian Andrzej Siewior } 74190e129ffSSebastian Andrzej Siewior 74290e129ffSSebastian Andrzej Siewior static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu) 74390e129ffSSebastian Andrzej Siewior { 74490e129ffSSebastian Andrzej Siewior struct ctf_stream *cs = cw->stream[cpu]; 74590e129ffSSebastian Andrzej Siewior 74690e129ffSSebastian Andrzej Siewior if (!cs) { 74790e129ffSSebastian Andrzej Siewior cs = ctf_stream__create(cw, cpu); 74890e129ffSSebastian Andrzej Siewior cw->stream[cpu] = cs; 74990e129ffSSebastian Andrzej Siewior } 75090e129ffSSebastian Andrzej Siewior 75190e129ffSSebastian Andrzej Siewior return cs; 75290e129ffSSebastian Andrzej Siewior } 75390e129ffSSebastian Andrzej Siewior 75490e129ffSSebastian Andrzej Siewior static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample, 75590e129ffSSebastian Andrzej Siewior struct perf_evsel *evsel) 75690e129ffSSebastian Andrzej Siewior { 75790e129ffSSebastian Andrzej Siewior int cpu = 0; 75890e129ffSSebastian Andrzej Siewior 75990e129ffSSebastian Andrzej Siewior if (evsel->attr.sample_type & PERF_SAMPLE_CPU) 76090e129ffSSebastian Andrzej Siewior cpu = sample->cpu; 76190e129ffSSebastian Andrzej Siewior 76290e129ffSSebastian Andrzej Siewior if (cpu > cw->stream_cnt) { 76390e129ffSSebastian Andrzej Siewior pr_err("Event was recorded for CPU %d, limit is at %d.\n", 76490e129ffSSebastian Andrzej Siewior cpu, cw->stream_cnt); 76590e129ffSSebastian Andrzej Siewior cpu = 0; 76690e129ffSSebastian Andrzej Siewior } 76790e129ffSSebastian Andrzej Siewior 76890e129ffSSebastian Andrzej Siewior return cpu; 76990e129ffSSebastian Andrzej Siewior } 77090e129ffSSebastian Andrzej Siewior 77189e5fa88SJiri Olsa #define STREAM_FLUSH_COUNT 100000 77289e5fa88SJiri Olsa 77389e5fa88SJiri Olsa /* 77489e5fa88SJiri Olsa * Currently we have no other way to determine the 77589e5fa88SJiri Olsa * time for the stream flush other than keep track 77689e5fa88SJiri Olsa * of the number of events and check it against 77789e5fa88SJiri Olsa * threshold. 77889e5fa88SJiri Olsa */ 77989e5fa88SJiri Olsa static bool is_flush_needed(struct ctf_stream *cs) 78089e5fa88SJiri Olsa { 78189e5fa88SJiri Olsa return cs->count >= STREAM_FLUSH_COUNT; 78289e5fa88SJiri Olsa } 78389e5fa88SJiri Olsa 784edbe9817SJiri Olsa static int process_sample_event(struct perf_tool *tool, 785b8f8eb84SArnaldo Carvalho de Melo union perf_event *_event, 786edbe9817SJiri Olsa struct perf_sample *sample, 787edbe9817SJiri Olsa struct perf_evsel *evsel, 788edbe9817SJiri Olsa struct machine *machine __maybe_unused) 789edbe9817SJiri Olsa { 790edbe9817SJiri Olsa struct convert *c = container_of(tool, struct convert, tool); 791edbe9817SJiri Olsa struct evsel_priv *priv = evsel->priv; 792edbe9817SJiri Olsa struct ctf_writer *cw = &c->writer; 79390e129ffSSebastian Andrzej Siewior struct ctf_stream *cs; 794edbe9817SJiri Olsa struct bt_ctf_event_class *event_class; 795edbe9817SJiri Olsa struct bt_ctf_event *event; 796edbe9817SJiri Olsa int ret; 797*a3073c8eSGeneviève Bastien unsigned long type = evsel->attr.sample_type; 798edbe9817SJiri Olsa 799edbe9817SJiri Olsa if (WARN_ONCE(!priv, "Failed to setup all events.\n")) 800edbe9817SJiri Olsa return 0; 801edbe9817SJiri Olsa 802edbe9817SJiri Olsa event_class = priv->event_class; 803edbe9817SJiri Olsa 804edbe9817SJiri Olsa /* update stats */ 805edbe9817SJiri Olsa c->events_count++; 806edbe9817SJiri Olsa c->events_size += _event->header.size; 807edbe9817SJiri Olsa 808edbe9817SJiri Olsa pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count); 809edbe9817SJiri Olsa 810edbe9817SJiri Olsa event = bt_ctf_event_create(event_class); 811edbe9817SJiri Olsa if (!event) { 812edbe9817SJiri Olsa pr_err("Failed to create an CTF event\n"); 813edbe9817SJiri Olsa return -1; 814edbe9817SJiri Olsa } 815edbe9817SJiri Olsa 816edbe9817SJiri Olsa bt_ctf_clock_set_time(cw->clock, sample->time); 817edbe9817SJiri Olsa 818edbe9817SJiri Olsa ret = add_generic_values(cw, event, evsel, sample); 819edbe9817SJiri Olsa if (ret) 820edbe9817SJiri Olsa return -1; 821edbe9817SJiri Olsa 82269364727SSebastian Andrzej Siewior if (evsel->attr.type == PERF_TYPE_TRACEPOINT) { 82369364727SSebastian Andrzej Siewior ret = add_tracepoint_values(cw, event_class, event, 82469364727SSebastian Andrzej Siewior evsel, sample); 82569364727SSebastian Andrzej Siewior if (ret) 82669364727SSebastian Andrzej Siewior return -1; 82769364727SSebastian Andrzej Siewior } 82869364727SSebastian Andrzej Siewior 829*a3073c8eSGeneviève Bastien if (type & PERF_SAMPLE_CALLCHAIN) { 830*a3073c8eSGeneviève Bastien ret = add_callchain_output_values(event_class, 831*a3073c8eSGeneviève Bastien event, sample->callchain); 832*a3073c8eSGeneviève Bastien if (ret) 833*a3073c8eSGeneviève Bastien return -1; 834*a3073c8eSGeneviève Bastien } 835*a3073c8eSGeneviève Bastien 8366122d57eSWang Nan if (perf_evsel__is_bpf_output(evsel)) { 8376122d57eSWang Nan ret = add_bpf_output_values(event_class, event, sample); 8386122d57eSWang Nan if (ret) 8396122d57eSWang Nan return -1; 8406122d57eSWang Nan } 8416122d57eSWang Nan 84290e129ffSSebastian Andrzej Siewior cs = ctf_stream(cw, get_sample_cpu(cw, sample, evsel)); 84389e5fa88SJiri Olsa if (cs) { 84489e5fa88SJiri Olsa if (is_flush_needed(cs)) 84589e5fa88SJiri Olsa ctf_stream__flush(cs); 84689e5fa88SJiri Olsa 84789e5fa88SJiri Olsa cs->count++; 84890e129ffSSebastian Andrzej Siewior bt_ctf_stream_append_event(cs->stream, event); 84989e5fa88SJiri Olsa } 85090e129ffSSebastian Andrzej Siewior 851edbe9817SJiri Olsa bt_ctf_event_put(event); 85290e129ffSSebastian Andrzej Siewior return cs ? 0 : -1; 853edbe9817SJiri Olsa } 854edbe9817SJiri Olsa 855f5a08cedSWang Nan #define __NON_SAMPLE_SET_FIELD(_name, _type, _field) \ 856f5a08cedSWang Nan do { \ 857f5a08cedSWang Nan ret = value_set_##_type(cw, event, #_field, _event->_name._field);\ 858f5a08cedSWang Nan if (ret) \ 859f5a08cedSWang Nan return -1; \ 860f5a08cedSWang Nan } while(0) 861f5a08cedSWang Nan 862f5a08cedSWang Nan #define __FUNC_PROCESS_NON_SAMPLE(_name, body) \ 863f5a08cedSWang Nan static int process_##_name##_event(struct perf_tool *tool, \ 864f5a08cedSWang Nan union perf_event *_event, \ 865f5a08cedSWang Nan struct perf_sample *sample, \ 866f5a08cedSWang Nan struct machine *machine) \ 867f5a08cedSWang Nan { \ 868f5a08cedSWang Nan struct convert *c = container_of(tool, struct convert, tool);\ 869f5a08cedSWang Nan struct ctf_writer *cw = &c->writer; \ 870f5a08cedSWang Nan struct bt_ctf_event_class *event_class = cw->_name##_class;\ 871f5a08cedSWang Nan struct bt_ctf_event *event; \ 872f5a08cedSWang Nan struct ctf_stream *cs; \ 873f5a08cedSWang Nan int ret; \ 874f5a08cedSWang Nan \ 875f5a08cedSWang Nan c->non_sample_count++; \ 876f5a08cedSWang Nan c->events_size += _event->header.size; \ 877f5a08cedSWang Nan event = bt_ctf_event_create(event_class); \ 878f5a08cedSWang Nan if (!event) { \ 879f5a08cedSWang Nan pr_err("Failed to create an CTF event\n"); \ 880f5a08cedSWang Nan return -1; \ 881f5a08cedSWang Nan } \ 882f5a08cedSWang Nan \ 883f5a08cedSWang Nan bt_ctf_clock_set_time(cw->clock, sample->time); \ 884f5a08cedSWang Nan body \ 885f5a08cedSWang Nan cs = ctf_stream(cw, 0); \ 886f5a08cedSWang Nan if (cs) { \ 887f5a08cedSWang Nan if (is_flush_needed(cs)) \ 888f5a08cedSWang Nan ctf_stream__flush(cs); \ 889f5a08cedSWang Nan \ 890f5a08cedSWang Nan cs->count++; \ 891f5a08cedSWang Nan bt_ctf_stream_append_event(cs->stream, event); \ 892f5a08cedSWang Nan } \ 893f5a08cedSWang Nan bt_ctf_event_put(event); \ 894f5a08cedSWang Nan \ 895f5a08cedSWang Nan return perf_event__process_##_name(tool, _event, sample, machine);\ 896f5a08cedSWang Nan } 897f5a08cedSWang Nan 898f5a08cedSWang Nan __FUNC_PROCESS_NON_SAMPLE(comm, 899f5a08cedSWang Nan __NON_SAMPLE_SET_FIELD(comm, u32, pid); 900f5a08cedSWang Nan __NON_SAMPLE_SET_FIELD(comm, u32, tid); 901f5a08cedSWang Nan __NON_SAMPLE_SET_FIELD(comm, string, comm); 902f5a08cedSWang Nan ) 903ebccba3fSWang Nan __FUNC_PROCESS_NON_SAMPLE(fork, 904ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u32, pid); 905ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u32, ppid); 906ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u32, tid); 907ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u32, ptid); 908ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u64, time); 909ebccba3fSWang Nan ) 910ebccba3fSWang Nan 911ebccba3fSWang Nan __FUNC_PROCESS_NON_SAMPLE(exit, 912ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u32, pid); 913ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u32, ppid); 914ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u32, tid); 915ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u32, ptid); 916ebccba3fSWang Nan __NON_SAMPLE_SET_FIELD(fork, u64, time); 917ebccba3fSWang Nan ) 918f5a08cedSWang Nan #undef __NON_SAMPLE_SET_FIELD 919f5a08cedSWang Nan #undef __FUNC_PROCESS_NON_SAMPLE 920f5a08cedSWang Nan 921e0a7cce5SWang Nan /* If dup < 0, add a prefix. Else, add _dupl_X suffix. */ 922e0a7cce5SWang Nan static char *change_name(char *name, char *orig_name, int dup) 923e0a7cce5SWang Nan { 924e0a7cce5SWang Nan char *new_name = NULL; 925e0a7cce5SWang Nan size_t len; 926e0a7cce5SWang Nan 927e0a7cce5SWang Nan if (!name) 928e0a7cce5SWang Nan name = orig_name; 929e0a7cce5SWang Nan 930e0a7cce5SWang Nan if (dup >= 10) 931e0a7cce5SWang Nan goto out; 932e0a7cce5SWang Nan /* 933e0a7cce5SWang Nan * Add '_' prefix to potential keywork. According to 934e0a7cce5SWang Nan * Mathieu Desnoyers (https://lkml.org/lkml/2015/1/23/652), 935e0a7cce5SWang Nan * futher CTF spec updating may require us to use '$'. 936e0a7cce5SWang Nan */ 937e0a7cce5SWang Nan if (dup < 0) 938e0a7cce5SWang Nan len = strlen(name) + sizeof("_"); 939e0a7cce5SWang Nan else 940e0a7cce5SWang Nan len = strlen(orig_name) + sizeof("_dupl_X"); 941e0a7cce5SWang Nan 942e0a7cce5SWang Nan new_name = malloc(len); 943e0a7cce5SWang Nan if (!new_name) 944e0a7cce5SWang Nan goto out; 945e0a7cce5SWang Nan 946e0a7cce5SWang Nan if (dup < 0) 947e0a7cce5SWang Nan snprintf(new_name, len, "_%s", name); 948e0a7cce5SWang Nan else 949e0a7cce5SWang Nan snprintf(new_name, len, "%s_dupl_%d", orig_name, dup); 950e0a7cce5SWang Nan 951e0a7cce5SWang Nan out: 952e0a7cce5SWang Nan if (name != orig_name) 953e0a7cce5SWang Nan free(name); 954e0a7cce5SWang Nan return new_name; 955e0a7cce5SWang Nan } 956e0a7cce5SWang Nan 957e0a7cce5SWang Nan static int event_class_add_field(struct bt_ctf_event_class *event_class, 958e0a7cce5SWang Nan struct bt_ctf_field_type *type, 959e0a7cce5SWang Nan struct format_field *field) 960e0a7cce5SWang Nan { 961e0a7cce5SWang Nan struct bt_ctf_field_type *t = NULL; 962e0a7cce5SWang Nan char *name; 963e0a7cce5SWang Nan int dup = 1; 964e0a7cce5SWang Nan int ret; 965e0a7cce5SWang Nan 966e0a7cce5SWang Nan /* alias was already assigned */ 967e0a7cce5SWang Nan if (field->alias != field->name) 968e0a7cce5SWang Nan return bt_ctf_event_class_add_field(event_class, type, 969e0a7cce5SWang Nan (char *)field->alias); 970e0a7cce5SWang Nan 971e0a7cce5SWang Nan name = field->name; 972e0a7cce5SWang Nan 973e0a7cce5SWang Nan /* If 'name' is a keywork, add prefix. */ 974e0a7cce5SWang Nan if (bt_ctf_validate_identifier(name)) 975e0a7cce5SWang Nan name = change_name(name, field->name, -1); 976e0a7cce5SWang Nan 977e0a7cce5SWang Nan if (!name) { 978e0a7cce5SWang Nan pr_err("Failed to fix invalid identifier."); 979e0a7cce5SWang Nan return -1; 980e0a7cce5SWang Nan } 981e0a7cce5SWang Nan while ((t = bt_ctf_event_class_get_field_by_name(event_class, name))) { 982e0a7cce5SWang Nan bt_ctf_field_type_put(t); 983e0a7cce5SWang Nan name = change_name(name, field->name, dup++); 984e0a7cce5SWang Nan if (!name) { 985e0a7cce5SWang Nan pr_err("Failed to create dup name for '%s'\n", field->name); 986e0a7cce5SWang Nan return -1; 987e0a7cce5SWang Nan } 988e0a7cce5SWang Nan } 989e0a7cce5SWang Nan 990e0a7cce5SWang Nan ret = bt_ctf_event_class_add_field(event_class, type, name); 991e0a7cce5SWang Nan if (!ret) 992e0a7cce5SWang Nan field->alias = name; 993e0a7cce5SWang Nan 994e0a7cce5SWang Nan return ret; 995e0a7cce5SWang Nan } 996e0a7cce5SWang Nan 99769364727SSebastian Andrzej Siewior static int add_tracepoint_fields_types(struct ctf_writer *cw, 99869364727SSebastian Andrzej Siewior struct format_field *fields, 99969364727SSebastian Andrzej Siewior struct bt_ctf_event_class *event_class) 100069364727SSebastian Andrzej Siewior { 100169364727SSebastian Andrzej Siewior struct format_field *field; 100269364727SSebastian Andrzej Siewior int ret; 100369364727SSebastian Andrzej Siewior 100469364727SSebastian Andrzej Siewior for (field = fields; field; field = field->next) { 100569364727SSebastian Andrzej Siewior struct bt_ctf_field_type *type; 100669364727SSebastian Andrzej Siewior unsigned long flags = field->flags; 100769364727SSebastian Andrzej Siewior 100869364727SSebastian Andrzej Siewior pr2(" field '%s'\n", field->name); 100969364727SSebastian Andrzej Siewior 101069364727SSebastian Andrzej Siewior type = get_tracepoint_field_type(cw, field); 101169364727SSebastian Andrzej Siewior if (!type) 101269364727SSebastian Andrzej Siewior return -1; 101369364727SSebastian Andrzej Siewior 101469364727SSebastian Andrzej Siewior /* 101569364727SSebastian Andrzej Siewior * A string is an array of chars. For this we use the string 101669364727SSebastian Andrzej Siewior * type and don't care that it is an array. What we don't 101769364727SSebastian Andrzej Siewior * support is an array of strings. 101869364727SSebastian Andrzej Siewior */ 101969364727SSebastian Andrzej Siewior if (flags & FIELD_IS_STRING) 102069364727SSebastian Andrzej Siewior flags &= ~FIELD_IS_ARRAY; 102169364727SSebastian Andrzej Siewior 102269364727SSebastian Andrzej Siewior if (flags & FIELD_IS_ARRAY) 102369364727SSebastian Andrzej Siewior type = bt_ctf_field_type_array_create(type, field->arraylen); 102469364727SSebastian Andrzej Siewior 1025e0a7cce5SWang Nan ret = event_class_add_field(event_class, type, field); 102669364727SSebastian Andrzej Siewior 102769364727SSebastian Andrzej Siewior if (flags & FIELD_IS_ARRAY) 102869364727SSebastian Andrzej Siewior bt_ctf_field_type_put(type); 102969364727SSebastian Andrzej Siewior 103069364727SSebastian Andrzej Siewior if (ret) { 1031e0a7cce5SWang Nan pr_err("Failed to add field '%s': %d\n", 1032e0a7cce5SWang Nan field->name, ret); 103369364727SSebastian Andrzej Siewior return -1; 103469364727SSebastian Andrzej Siewior } 103569364727SSebastian Andrzej Siewior } 103669364727SSebastian Andrzej Siewior 103769364727SSebastian Andrzej Siewior return 0; 103869364727SSebastian Andrzej Siewior } 103969364727SSebastian Andrzej Siewior 104069364727SSebastian Andrzej Siewior static int add_tracepoint_types(struct ctf_writer *cw, 104169364727SSebastian Andrzej Siewior struct perf_evsel *evsel, 104269364727SSebastian Andrzej Siewior struct bt_ctf_event_class *class) 104369364727SSebastian Andrzej Siewior { 104469364727SSebastian Andrzej Siewior struct format_field *common_fields = evsel->tp_format->format.common_fields; 104569364727SSebastian Andrzej Siewior struct format_field *fields = evsel->tp_format->format.fields; 104669364727SSebastian Andrzej Siewior int ret; 104769364727SSebastian Andrzej Siewior 104869364727SSebastian Andrzej Siewior ret = add_tracepoint_fields_types(cw, common_fields, class); 104969364727SSebastian Andrzej Siewior if (!ret) 105069364727SSebastian Andrzej Siewior ret = add_tracepoint_fields_types(cw, fields, class); 105169364727SSebastian Andrzej Siewior 105269364727SSebastian Andrzej Siewior return ret; 105369364727SSebastian Andrzej Siewior } 105469364727SSebastian Andrzej Siewior 10556122d57eSWang Nan static int add_bpf_output_types(struct ctf_writer *cw, 10566122d57eSWang Nan struct bt_ctf_event_class *class) 10576122d57eSWang Nan { 10586122d57eSWang Nan struct bt_ctf_field_type *len_type = cw->data.u32; 10596122d57eSWang Nan struct bt_ctf_field_type *seq_base_type = cw->data.u32_hex; 10606122d57eSWang Nan struct bt_ctf_field_type *seq_type; 10616122d57eSWang Nan int ret; 10626122d57eSWang Nan 10636122d57eSWang Nan ret = bt_ctf_event_class_add_field(class, len_type, "raw_len"); 10646122d57eSWang Nan if (ret) 10656122d57eSWang Nan return ret; 10666122d57eSWang Nan 10676122d57eSWang Nan seq_type = bt_ctf_field_type_sequence_create(seq_base_type, "raw_len"); 10686122d57eSWang Nan if (!seq_type) 10696122d57eSWang Nan return -1; 10706122d57eSWang Nan 10716122d57eSWang Nan return bt_ctf_event_class_add_field(class, seq_type, "raw_data"); 10726122d57eSWang Nan } 10736122d57eSWang Nan 1074edbe9817SJiri Olsa static int add_generic_types(struct ctf_writer *cw, struct perf_evsel *evsel, 1075edbe9817SJiri Olsa struct bt_ctf_event_class *event_class) 1076edbe9817SJiri Olsa { 1077edbe9817SJiri Olsa u64 type = evsel->attr.sample_type; 1078edbe9817SJiri Olsa 1079edbe9817SJiri Olsa /* 1080edbe9817SJiri Olsa * missing: 1081edbe9817SJiri Olsa * PERF_SAMPLE_TIME - not needed as we have it in 1082edbe9817SJiri Olsa * ctf event header 1083edbe9817SJiri Olsa * PERF_SAMPLE_READ - TODO 1084edbe9817SJiri Olsa * PERF_SAMPLE_CALLCHAIN - TODO 10856122d57eSWang Nan * PERF_SAMPLE_RAW - tracepoint fields and BPF output 10866122d57eSWang Nan * are handled separately 1087edbe9817SJiri Olsa * PERF_SAMPLE_BRANCH_STACK - TODO 1088edbe9817SJiri Olsa * PERF_SAMPLE_REGS_USER - TODO 1089edbe9817SJiri Olsa * PERF_SAMPLE_STACK_USER - TODO 1090edbe9817SJiri Olsa */ 1091edbe9817SJiri Olsa 1092edbe9817SJiri Olsa #define ADD_FIELD(cl, t, n) \ 1093edbe9817SJiri Olsa do { \ 1094edbe9817SJiri Olsa pr2(" field '%s'\n", n); \ 1095edbe9817SJiri Olsa if (bt_ctf_event_class_add_field(cl, t, n)) { \ 1096e0a7cce5SWang Nan pr_err("Failed to add field '%s';\n", n); \ 1097edbe9817SJiri Olsa return -1; \ 1098edbe9817SJiri Olsa } \ 1099edbe9817SJiri Olsa } while (0) 1100edbe9817SJiri Olsa 1101edbe9817SJiri Olsa if (type & PERF_SAMPLE_IP) 110254cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip"); 1103edbe9817SJiri Olsa 1104edbe9817SJiri Olsa if (type & PERF_SAMPLE_TID) { 110554cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.s32, "perf_tid"); 110654cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.s32, "perf_pid"); 1107edbe9817SJiri Olsa } 1108edbe9817SJiri Olsa 1109edbe9817SJiri Olsa if ((type & PERF_SAMPLE_ID) || 1110edbe9817SJiri Olsa (type & PERF_SAMPLE_IDENTIFIER)) 111154cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.u64, "perf_id"); 1112edbe9817SJiri Olsa 1113edbe9817SJiri Olsa if (type & PERF_SAMPLE_STREAM_ID) 111454cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.u64, "perf_stream_id"); 1115edbe9817SJiri Olsa 1116edbe9817SJiri Olsa if (type & PERF_SAMPLE_PERIOD) 111754cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.u64, "perf_period"); 1118edbe9817SJiri Olsa 1119edbe9817SJiri Olsa if (type & PERF_SAMPLE_WEIGHT) 112054cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.u64, "perf_weight"); 1121edbe9817SJiri Olsa 1122edbe9817SJiri Olsa if (type & PERF_SAMPLE_DATA_SRC) 112354cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.u64, "perf_data_src"); 1124edbe9817SJiri Olsa 1125edbe9817SJiri Olsa if (type & PERF_SAMPLE_TRANSACTION) 112654cf776aSSebastian Andrzej Siewior ADD_FIELD(event_class, cw->data.u64, "perf_transaction"); 1127edbe9817SJiri Olsa 1128*a3073c8eSGeneviève Bastien if (type & PERF_SAMPLE_CALLCHAIN) { 1129*a3073c8eSGeneviève Bastien ADD_FIELD(event_class, cw->data.u32, "perf_callchain_size"); 1130*a3073c8eSGeneviève Bastien ADD_FIELD(event_class, 1131*a3073c8eSGeneviève Bastien bt_ctf_field_type_sequence_create( 1132*a3073c8eSGeneviève Bastien cw->data.u64_hex, "perf_callchain_size"), 1133*a3073c8eSGeneviève Bastien "perf_callchain"); 1134*a3073c8eSGeneviève Bastien } 1135*a3073c8eSGeneviève Bastien 1136edbe9817SJiri Olsa #undef ADD_FIELD 1137edbe9817SJiri Olsa return 0; 1138edbe9817SJiri Olsa } 1139edbe9817SJiri Olsa 1140edbe9817SJiri Olsa static int add_event(struct ctf_writer *cw, struct perf_evsel *evsel) 1141edbe9817SJiri Olsa { 1142edbe9817SJiri Olsa struct bt_ctf_event_class *event_class; 1143edbe9817SJiri Olsa struct evsel_priv *priv; 1144edbe9817SJiri Olsa const char *name = perf_evsel__name(evsel); 1145edbe9817SJiri Olsa int ret; 1146edbe9817SJiri Olsa 1147edbe9817SJiri Olsa pr("Adding event '%s' (type %d)\n", name, evsel->attr.type); 1148edbe9817SJiri Olsa 1149edbe9817SJiri Olsa event_class = bt_ctf_event_class_create(name); 1150edbe9817SJiri Olsa if (!event_class) 1151edbe9817SJiri Olsa return -1; 1152edbe9817SJiri Olsa 1153edbe9817SJiri Olsa ret = add_generic_types(cw, evsel, event_class); 1154edbe9817SJiri Olsa if (ret) 1155edbe9817SJiri Olsa goto err; 1156edbe9817SJiri Olsa 115769364727SSebastian Andrzej Siewior if (evsel->attr.type == PERF_TYPE_TRACEPOINT) { 115869364727SSebastian Andrzej Siewior ret = add_tracepoint_types(cw, evsel, event_class); 115969364727SSebastian Andrzej Siewior if (ret) 116069364727SSebastian Andrzej Siewior goto err; 116169364727SSebastian Andrzej Siewior } 116269364727SSebastian Andrzej Siewior 11636122d57eSWang Nan if (perf_evsel__is_bpf_output(evsel)) { 11646122d57eSWang Nan ret = add_bpf_output_types(cw, event_class); 11656122d57eSWang Nan if (ret) 11666122d57eSWang Nan goto err; 11676122d57eSWang Nan } 11686122d57eSWang Nan 1169edbe9817SJiri Olsa ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class); 1170edbe9817SJiri Olsa if (ret) { 1171edbe9817SJiri Olsa pr("Failed to add event class into stream.\n"); 1172edbe9817SJiri Olsa goto err; 1173edbe9817SJiri Olsa } 1174edbe9817SJiri Olsa 1175edbe9817SJiri Olsa priv = malloc(sizeof(*priv)); 1176edbe9817SJiri Olsa if (!priv) 1177edbe9817SJiri Olsa goto err; 1178edbe9817SJiri Olsa 1179edbe9817SJiri Olsa priv->event_class = event_class; 1180edbe9817SJiri Olsa evsel->priv = priv; 1181edbe9817SJiri Olsa return 0; 1182edbe9817SJiri Olsa 1183edbe9817SJiri Olsa err: 1184edbe9817SJiri Olsa bt_ctf_event_class_put(event_class); 1185edbe9817SJiri Olsa pr_err("Failed to add event '%s'.\n", name); 1186edbe9817SJiri Olsa return -1; 1187edbe9817SJiri Olsa } 1188edbe9817SJiri Olsa 1189edbe9817SJiri Olsa static int setup_events(struct ctf_writer *cw, struct perf_session *session) 1190edbe9817SJiri Olsa { 1191edbe9817SJiri Olsa struct perf_evlist *evlist = session->evlist; 1192edbe9817SJiri Olsa struct perf_evsel *evsel; 1193edbe9817SJiri Olsa int ret; 1194edbe9817SJiri Olsa 1195e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1196edbe9817SJiri Olsa ret = add_event(cw, evsel); 1197edbe9817SJiri Olsa if (ret) 1198edbe9817SJiri Olsa return ret; 1199edbe9817SJiri Olsa } 1200edbe9817SJiri Olsa return 0; 1201edbe9817SJiri Olsa } 1202edbe9817SJiri Olsa 1203f5a08cedSWang Nan #define __NON_SAMPLE_ADD_FIELD(t, n) \ 1204f5a08cedSWang Nan do { \ 1205f5a08cedSWang Nan pr2(" field '%s'\n", #n); \ 1206f5a08cedSWang Nan if (bt_ctf_event_class_add_field(event_class, cw->data.t, #n)) {\ 1207f5a08cedSWang Nan pr_err("Failed to add field '%s';\n", #n);\ 1208f5a08cedSWang Nan return -1; \ 1209f5a08cedSWang Nan } \ 1210f5a08cedSWang Nan } while(0) 1211f5a08cedSWang Nan 1212f5a08cedSWang Nan #define __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(_name, body) \ 1213f5a08cedSWang Nan static int add_##_name##_event(struct ctf_writer *cw) \ 1214f5a08cedSWang Nan { \ 1215f5a08cedSWang Nan struct bt_ctf_event_class *event_class; \ 1216f5a08cedSWang Nan int ret; \ 1217f5a08cedSWang Nan \ 1218f5a08cedSWang Nan pr("Adding "#_name" event\n"); \ 1219f5a08cedSWang Nan event_class = bt_ctf_event_class_create("perf_" #_name);\ 1220f5a08cedSWang Nan if (!event_class) \ 1221f5a08cedSWang Nan return -1; \ 1222f5a08cedSWang Nan body \ 1223f5a08cedSWang Nan \ 1224f5a08cedSWang Nan ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);\ 1225f5a08cedSWang Nan if (ret) { \ 1226f5a08cedSWang Nan pr("Failed to add event class '"#_name"' into stream.\n");\ 1227f5a08cedSWang Nan return ret; \ 1228f5a08cedSWang Nan } \ 1229f5a08cedSWang Nan \ 1230f5a08cedSWang Nan cw->_name##_class = event_class; \ 1231f5a08cedSWang Nan bt_ctf_event_class_put(event_class); \ 1232f5a08cedSWang Nan return 0; \ 1233f5a08cedSWang Nan } 1234f5a08cedSWang Nan 1235f5a08cedSWang Nan __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(comm, 1236f5a08cedSWang Nan __NON_SAMPLE_ADD_FIELD(u32, pid); 1237f5a08cedSWang Nan __NON_SAMPLE_ADD_FIELD(u32, tid); 1238f5a08cedSWang Nan __NON_SAMPLE_ADD_FIELD(string, comm); 1239f5a08cedSWang Nan ) 1240f5a08cedSWang Nan 1241ebccba3fSWang Nan __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(fork, 1242ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u32, pid); 1243ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u32, ppid); 1244ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u32, tid); 1245ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u32, ptid); 1246ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u64, time); 1247ebccba3fSWang Nan ) 1248ebccba3fSWang Nan 1249ebccba3fSWang Nan __FUNC_ADD_NON_SAMPLE_EVENT_CLASS(exit, 1250ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u32, pid); 1251ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u32, ppid); 1252ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u32, tid); 1253ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u32, ptid); 1254ebccba3fSWang Nan __NON_SAMPLE_ADD_FIELD(u64, time); 1255ebccba3fSWang Nan ) 1256ebccba3fSWang Nan 1257f5a08cedSWang Nan #undef __NON_SAMPLE_ADD_FIELD 1258f5a08cedSWang Nan #undef __FUNC_ADD_NON_SAMPLE_EVENT_CLASS 1259f5a08cedSWang Nan 1260f5a08cedSWang Nan static int setup_non_sample_events(struct ctf_writer *cw, 1261f5a08cedSWang Nan struct perf_session *session __maybe_unused) 1262f5a08cedSWang Nan { 1263f5a08cedSWang Nan int ret; 1264f5a08cedSWang Nan 1265f5a08cedSWang Nan ret = add_comm_event(cw); 1266f5a08cedSWang Nan if (ret) 1267f5a08cedSWang Nan return ret; 1268ebccba3fSWang Nan ret = add_exit_event(cw); 1269ebccba3fSWang Nan if (ret) 1270ebccba3fSWang Nan return ret; 1271ebccba3fSWang Nan ret = add_fork_event(cw); 1272ebccba3fSWang Nan if (ret) 1273ebccba3fSWang Nan return ret; 1274f5a08cedSWang Nan return 0; 1275f5a08cedSWang Nan } 1276f5a08cedSWang Nan 12775141d735SWang Nan static void cleanup_events(struct perf_session *session) 12785141d735SWang Nan { 12795141d735SWang Nan struct perf_evlist *evlist = session->evlist; 12805141d735SWang Nan struct perf_evsel *evsel; 12815141d735SWang Nan 1282e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 12835141d735SWang Nan struct evsel_priv *priv; 12845141d735SWang Nan 12855141d735SWang Nan priv = evsel->priv; 12865141d735SWang Nan bt_ctf_event_class_put(priv->event_class); 12875141d735SWang Nan zfree(&evsel->priv); 12885141d735SWang Nan } 12895141d735SWang Nan 12905141d735SWang Nan perf_evlist__delete(evlist); 12915141d735SWang Nan session->evlist = NULL; 12925141d735SWang Nan } 12935141d735SWang Nan 129490e129ffSSebastian Andrzej Siewior static int setup_streams(struct ctf_writer *cw, struct perf_session *session) 129590e129ffSSebastian Andrzej Siewior { 129690e129ffSSebastian Andrzej Siewior struct ctf_stream **stream; 129790e129ffSSebastian Andrzej Siewior struct perf_header *ph = &session->header; 129890e129ffSSebastian Andrzej Siewior int ncpus; 129990e129ffSSebastian Andrzej Siewior 130090e129ffSSebastian Andrzej Siewior /* 130190e129ffSSebastian Andrzej Siewior * Try to get the number of cpus used in the data file, 130290e129ffSSebastian Andrzej Siewior * if not present fallback to the MAX_CPUS. 130390e129ffSSebastian Andrzej Siewior */ 130490e129ffSSebastian Andrzej Siewior ncpus = ph->env.nr_cpus_avail ?: MAX_CPUS; 130590e129ffSSebastian Andrzej Siewior 130690e129ffSSebastian Andrzej Siewior stream = zalloc(sizeof(*stream) * ncpus); 130790e129ffSSebastian Andrzej Siewior if (!stream) { 130890e129ffSSebastian Andrzej Siewior pr_err("Failed to allocate streams.\n"); 130990e129ffSSebastian Andrzej Siewior return -ENOMEM; 131090e129ffSSebastian Andrzej Siewior } 131190e129ffSSebastian Andrzej Siewior 131290e129ffSSebastian Andrzej Siewior cw->stream = stream; 131390e129ffSSebastian Andrzej Siewior cw->stream_cnt = ncpus; 131490e129ffSSebastian Andrzej Siewior return 0; 131590e129ffSSebastian Andrzej Siewior } 131690e129ffSSebastian Andrzej Siewior 131790e129ffSSebastian Andrzej Siewior static void free_streams(struct ctf_writer *cw) 131890e129ffSSebastian Andrzej Siewior { 131990e129ffSSebastian Andrzej Siewior int cpu; 132090e129ffSSebastian Andrzej Siewior 132190e129ffSSebastian Andrzej Siewior for (cpu = 0; cpu < cw->stream_cnt; cpu++) 132290e129ffSSebastian Andrzej Siewior ctf_stream__delete(cw->stream[cpu]); 132390e129ffSSebastian Andrzej Siewior 132490e129ffSSebastian Andrzej Siewior free(cw->stream); 132590e129ffSSebastian Andrzej Siewior } 132690e129ffSSebastian Andrzej Siewior 1327edbe9817SJiri Olsa static int ctf_writer__setup_env(struct ctf_writer *cw, 1328edbe9817SJiri Olsa struct perf_session *session) 1329edbe9817SJiri Olsa { 1330edbe9817SJiri Olsa struct perf_header *header = &session->header; 1331edbe9817SJiri Olsa struct bt_ctf_writer *writer = cw->writer; 1332edbe9817SJiri Olsa 1333edbe9817SJiri Olsa #define ADD(__n, __v) \ 1334edbe9817SJiri Olsa do { \ 1335edbe9817SJiri Olsa if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \ 1336edbe9817SJiri Olsa return -1; \ 1337edbe9817SJiri Olsa } while (0) 1338edbe9817SJiri Olsa 1339edbe9817SJiri Olsa ADD("host", header->env.hostname); 1340edbe9817SJiri Olsa ADD("sysname", "Linux"); 1341edbe9817SJiri Olsa ADD("release", header->env.os_release); 1342edbe9817SJiri Olsa ADD("version", header->env.version); 1343edbe9817SJiri Olsa ADD("machine", header->env.arch); 1344edbe9817SJiri Olsa ADD("domain", "kernel"); 1345edbe9817SJiri Olsa ADD("tracer_name", "perf"); 1346edbe9817SJiri Olsa 1347edbe9817SJiri Olsa #undef ADD 1348edbe9817SJiri Olsa return 0; 1349edbe9817SJiri Olsa } 1350edbe9817SJiri Olsa 1351edbe9817SJiri Olsa static int ctf_writer__setup_clock(struct ctf_writer *cw) 1352edbe9817SJiri Olsa { 1353edbe9817SJiri Olsa struct bt_ctf_clock *clock = cw->clock; 1354edbe9817SJiri Olsa 1355edbe9817SJiri Olsa bt_ctf_clock_set_description(clock, "perf clock"); 1356edbe9817SJiri Olsa 1357edbe9817SJiri Olsa #define SET(__n, __v) \ 1358edbe9817SJiri Olsa do { \ 1359edbe9817SJiri Olsa if (bt_ctf_clock_set_##__n(clock, __v)) \ 1360edbe9817SJiri Olsa return -1; \ 1361edbe9817SJiri Olsa } while (0) 1362edbe9817SJiri Olsa 1363edbe9817SJiri Olsa SET(frequency, 1000000000); 1364edbe9817SJiri Olsa SET(offset_s, 0); 1365edbe9817SJiri Olsa SET(offset, 0); 1366edbe9817SJiri Olsa SET(precision, 10); 1367edbe9817SJiri Olsa SET(is_absolute, 0); 1368edbe9817SJiri Olsa 1369edbe9817SJiri Olsa #undef SET 1370edbe9817SJiri Olsa return 0; 1371edbe9817SJiri Olsa } 1372edbe9817SJiri Olsa 1373edbe9817SJiri Olsa static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex) 1374edbe9817SJiri Olsa { 1375edbe9817SJiri Olsa struct bt_ctf_field_type *type; 1376edbe9817SJiri Olsa 1377edbe9817SJiri Olsa type = bt_ctf_field_type_integer_create(size); 1378edbe9817SJiri Olsa if (!type) 1379edbe9817SJiri Olsa return NULL; 1380edbe9817SJiri Olsa 1381edbe9817SJiri Olsa if (sign && 1382edbe9817SJiri Olsa bt_ctf_field_type_integer_set_signed(type, 1)) 1383edbe9817SJiri Olsa goto err; 1384edbe9817SJiri Olsa 1385edbe9817SJiri Olsa if (hex && 1386edbe9817SJiri Olsa bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL)) 1387edbe9817SJiri Olsa goto err; 1388edbe9817SJiri Olsa 1389f8dd2d5fSWang Nan #if __BYTE_ORDER == __BIG_ENDIAN 1390f8dd2d5fSWang Nan bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_BIG_ENDIAN); 1391f8dd2d5fSWang Nan #else 1392f8dd2d5fSWang Nan bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN); 1393f8dd2d5fSWang Nan #endif 1394f8dd2d5fSWang Nan 1395edbe9817SJiri Olsa pr2("Created type: INTEGER %d-bit %ssigned %s\n", 1396edbe9817SJiri Olsa size, sign ? "un" : "", hex ? "hex" : ""); 1397edbe9817SJiri Olsa return type; 1398edbe9817SJiri Olsa 1399edbe9817SJiri Olsa err: 1400edbe9817SJiri Olsa bt_ctf_field_type_put(type); 1401edbe9817SJiri Olsa return NULL; 1402edbe9817SJiri Olsa } 1403edbe9817SJiri Olsa 1404edbe9817SJiri Olsa static void ctf_writer__cleanup_data(struct ctf_writer *cw) 1405edbe9817SJiri Olsa { 1406edbe9817SJiri Olsa unsigned int i; 1407edbe9817SJiri Olsa 1408edbe9817SJiri Olsa for (i = 0; i < ARRAY_SIZE(cw->data.array); i++) 1409edbe9817SJiri Olsa bt_ctf_field_type_put(cw->data.array[i]); 1410edbe9817SJiri Olsa } 1411edbe9817SJiri Olsa 1412edbe9817SJiri Olsa static int ctf_writer__init_data(struct ctf_writer *cw) 1413edbe9817SJiri Olsa { 1414edbe9817SJiri Olsa #define CREATE_INT_TYPE(type, size, sign, hex) \ 1415edbe9817SJiri Olsa do { \ 1416edbe9817SJiri Olsa (type) = create_int_type(size, sign, hex); \ 1417edbe9817SJiri Olsa if (!(type)) \ 1418edbe9817SJiri Olsa goto err; \ 1419edbe9817SJiri Olsa } while (0) 1420edbe9817SJiri Olsa 1421edbe9817SJiri Olsa CREATE_INT_TYPE(cw->data.s64, 64, true, false); 1422edbe9817SJiri Olsa CREATE_INT_TYPE(cw->data.u64, 64, false, false); 1423edbe9817SJiri Olsa CREATE_INT_TYPE(cw->data.s32, 32, true, false); 1424edbe9817SJiri Olsa CREATE_INT_TYPE(cw->data.u32, 32, false, false); 142526812d46SWang Nan CREATE_INT_TYPE(cw->data.u32_hex, 32, false, true); 1426edbe9817SJiri Olsa CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true); 1427edbe9817SJiri Olsa 1428edbe9817SJiri Olsa cw->data.string = bt_ctf_field_type_string_create(); 1429edbe9817SJiri Olsa if (cw->data.string) 1430edbe9817SJiri Olsa return 0; 1431edbe9817SJiri Olsa 1432edbe9817SJiri Olsa err: 1433edbe9817SJiri Olsa ctf_writer__cleanup_data(cw); 1434edbe9817SJiri Olsa pr_err("Failed to create data types.\n"); 1435edbe9817SJiri Olsa return -1; 1436edbe9817SJiri Olsa } 1437edbe9817SJiri Olsa 1438edbe9817SJiri Olsa static void ctf_writer__cleanup(struct ctf_writer *cw) 1439edbe9817SJiri Olsa { 1440edbe9817SJiri Olsa ctf_writer__cleanup_data(cw); 1441edbe9817SJiri Olsa 1442edbe9817SJiri Olsa bt_ctf_clock_put(cw->clock); 144390e129ffSSebastian Andrzej Siewior free_streams(cw); 1444edbe9817SJiri Olsa bt_ctf_stream_class_put(cw->stream_class); 1445edbe9817SJiri Olsa bt_ctf_writer_put(cw->writer); 1446edbe9817SJiri Olsa 1447edbe9817SJiri Olsa /* and NULL all the pointers */ 1448edbe9817SJiri Olsa memset(cw, 0, sizeof(*cw)); 1449edbe9817SJiri Olsa } 1450edbe9817SJiri Olsa 1451edbe9817SJiri Olsa static int ctf_writer__init(struct ctf_writer *cw, const char *path) 1452edbe9817SJiri Olsa { 1453edbe9817SJiri Olsa struct bt_ctf_writer *writer; 1454edbe9817SJiri Olsa struct bt_ctf_stream_class *stream_class; 1455edbe9817SJiri Olsa struct bt_ctf_clock *clock; 145690e129ffSSebastian Andrzej Siewior struct bt_ctf_field_type *pkt_ctx_type; 145790e129ffSSebastian Andrzej Siewior int ret; 1458edbe9817SJiri Olsa 1459edbe9817SJiri Olsa /* CTF writer */ 1460edbe9817SJiri Olsa writer = bt_ctf_writer_create(path); 1461edbe9817SJiri Olsa if (!writer) 1462edbe9817SJiri Olsa goto err; 1463edbe9817SJiri Olsa 1464edbe9817SJiri Olsa cw->writer = writer; 1465edbe9817SJiri Olsa 1466edbe9817SJiri Olsa /* CTF clock */ 1467edbe9817SJiri Olsa clock = bt_ctf_clock_create("perf_clock"); 1468edbe9817SJiri Olsa if (!clock) { 1469edbe9817SJiri Olsa pr("Failed to create CTF clock.\n"); 1470edbe9817SJiri Olsa goto err_cleanup; 1471edbe9817SJiri Olsa } 1472edbe9817SJiri Olsa 1473edbe9817SJiri Olsa cw->clock = clock; 1474edbe9817SJiri Olsa 1475edbe9817SJiri Olsa if (ctf_writer__setup_clock(cw)) { 1476edbe9817SJiri Olsa pr("Failed to setup CTF clock.\n"); 1477edbe9817SJiri Olsa goto err_cleanup; 1478edbe9817SJiri Olsa } 1479edbe9817SJiri Olsa 1480edbe9817SJiri Olsa /* CTF stream class */ 1481edbe9817SJiri Olsa stream_class = bt_ctf_stream_class_create("perf_stream"); 1482edbe9817SJiri Olsa if (!stream_class) { 1483edbe9817SJiri Olsa pr("Failed to create CTF stream class.\n"); 1484edbe9817SJiri Olsa goto err_cleanup; 1485edbe9817SJiri Olsa } 1486edbe9817SJiri Olsa 1487edbe9817SJiri Olsa cw->stream_class = stream_class; 1488edbe9817SJiri Olsa 1489edbe9817SJiri Olsa /* CTF clock stream setup */ 1490edbe9817SJiri Olsa if (bt_ctf_stream_class_set_clock(stream_class, clock)) { 1491edbe9817SJiri Olsa pr("Failed to assign CTF clock to stream class.\n"); 1492edbe9817SJiri Olsa goto err_cleanup; 1493edbe9817SJiri Olsa } 1494edbe9817SJiri Olsa 1495edbe9817SJiri Olsa if (ctf_writer__init_data(cw)) 1496edbe9817SJiri Olsa goto err_cleanup; 1497edbe9817SJiri Olsa 149890e129ffSSebastian Andrzej Siewior /* Add cpu_id for packet context */ 149990e129ffSSebastian Andrzej Siewior pkt_ctx_type = bt_ctf_stream_class_get_packet_context_type(stream_class); 150090e129ffSSebastian Andrzej Siewior if (!pkt_ctx_type) 1501edbe9817SJiri Olsa goto err_cleanup; 1502edbe9817SJiri Olsa 150390e129ffSSebastian Andrzej Siewior ret = bt_ctf_field_type_structure_add_field(pkt_ctx_type, cw->data.u32, "cpu_id"); 150490e129ffSSebastian Andrzej Siewior bt_ctf_field_type_put(pkt_ctx_type); 150590e129ffSSebastian Andrzej Siewior if (ret) 150690e129ffSSebastian Andrzej Siewior goto err_cleanup; 1507edbe9817SJiri Olsa 1508edbe9817SJiri Olsa /* CTF clock writer setup */ 1509edbe9817SJiri Olsa if (bt_ctf_writer_add_clock(writer, clock)) { 1510edbe9817SJiri Olsa pr("Failed to assign CTF clock to writer.\n"); 1511edbe9817SJiri Olsa goto err_cleanup; 1512edbe9817SJiri Olsa } 1513edbe9817SJiri Olsa 1514edbe9817SJiri Olsa return 0; 1515edbe9817SJiri Olsa 1516edbe9817SJiri Olsa err_cleanup: 1517edbe9817SJiri Olsa ctf_writer__cleanup(cw); 1518edbe9817SJiri Olsa err: 1519edbe9817SJiri Olsa pr_err("Failed to setup CTF writer.\n"); 1520edbe9817SJiri Olsa return -1; 1521edbe9817SJiri Olsa } 1522edbe9817SJiri Olsa 152390e129ffSSebastian Andrzej Siewior static int ctf_writer__flush_streams(struct ctf_writer *cw) 152490e129ffSSebastian Andrzej Siewior { 152590e129ffSSebastian Andrzej Siewior int cpu, ret = 0; 152690e129ffSSebastian Andrzej Siewior 152790e129ffSSebastian Andrzej Siewior for (cpu = 0; cpu < cw->stream_cnt && !ret; cpu++) 152890e129ffSSebastian Andrzej Siewior ret = ctf_stream__flush(cw->stream[cpu]); 152990e129ffSSebastian Andrzej Siewior 153090e129ffSSebastian Andrzej Siewior return ret; 153190e129ffSSebastian Andrzej Siewior } 153290e129ffSSebastian Andrzej Siewior 15338fa46753SJiri Olsa static int convert__config(const char *var, const char *value, void *cb) 15348fa46753SJiri Olsa { 15358fa46753SJiri Olsa struct convert *c = cb; 15368fa46753SJiri Olsa 153725ce4bb8SArnaldo Carvalho de Melo if (!strcmp(var, "convert.queue-size")) 153825ce4bb8SArnaldo Carvalho de Melo return perf_config_u64(&c->queue_size, var, value); 15398fa46753SJiri Olsa 1540b8cbb349SWang Nan return 0; 15418fa46753SJiri Olsa } 15428fa46753SJiri Olsa 15433275f68eSWang Nan int bt_convert__perf2ctf(const char *input, const char *path, 15443275f68eSWang Nan struct perf_data_convert_opts *opts) 1545edbe9817SJiri Olsa { 1546edbe9817SJiri Olsa struct perf_session *session; 1547edbe9817SJiri Olsa struct perf_data_file file = { 1548edbe9817SJiri Olsa .path = input, 1549edbe9817SJiri Olsa .mode = PERF_DATA_MODE_READ, 15503275f68eSWang Nan .force = opts->force, 1551edbe9817SJiri Olsa }; 1552edbe9817SJiri Olsa struct convert c = { 1553edbe9817SJiri Olsa .tool = { 1554edbe9817SJiri Olsa .sample = process_sample_event, 1555edbe9817SJiri Olsa .mmap = perf_event__process_mmap, 1556edbe9817SJiri Olsa .mmap2 = perf_event__process_mmap2, 1557edbe9817SJiri Olsa .comm = perf_event__process_comm, 1558edbe9817SJiri Olsa .exit = perf_event__process_exit, 1559edbe9817SJiri Olsa .fork = perf_event__process_fork, 1560edbe9817SJiri Olsa .lost = perf_event__process_lost, 1561edbe9817SJiri Olsa .tracing_data = perf_event__process_tracing_data, 1562edbe9817SJiri Olsa .build_id = perf_event__process_build_id, 1563f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 1564edbe9817SJiri Olsa .ordered_events = true, 1565edbe9817SJiri Olsa .ordering_requires_timestamps = true, 1566edbe9817SJiri Olsa }, 1567edbe9817SJiri Olsa }; 1568edbe9817SJiri Olsa struct ctf_writer *cw = &c.writer; 1569ecc4c561SArnaldo Carvalho de Melo int err; 1570edbe9817SJiri Olsa 1571ebccba3fSWang Nan if (opts->all) { 1572f5a08cedSWang Nan c.tool.comm = process_comm_event; 1573ebccba3fSWang Nan c.tool.exit = process_exit_event; 1574ebccba3fSWang Nan c.tool.fork = process_fork_event; 1575ebccba3fSWang Nan } 1576f5a08cedSWang Nan 1577ecc4c561SArnaldo Carvalho de Melo err = perf_config(convert__config, &c); 1578ecc4c561SArnaldo Carvalho de Melo if (err) 1579ecc4c561SArnaldo Carvalho de Melo return err; 15808fa46753SJiri Olsa 1581edbe9817SJiri Olsa /* CTF writer */ 1582edbe9817SJiri Olsa if (ctf_writer__init(cw, path)) 1583edbe9817SJiri Olsa return -1; 1584edbe9817SJiri Olsa 1585ecc4c561SArnaldo Carvalho de Melo err = -1; 1586edbe9817SJiri Olsa /* perf.data session */ 1587b7b61cbeSArnaldo Carvalho de Melo session = perf_session__new(&file, 0, &c.tool); 1588edbe9817SJiri Olsa if (!session) 1589edbe9817SJiri Olsa goto free_writer; 1590edbe9817SJiri Olsa 15918fa46753SJiri Olsa if (c.queue_size) { 15928fa46753SJiri Olsa ordered_events__set_alloc_size(&session->ordered_events, 15938fa46753SJiri Olsa c.queue_size); 15948fa46753SJiri Olsa } 15958fa46753SJiri Olsa 1596edbe9817SJiri Olsa /* CTF writer env/clock setup */ 1597edbe9817SJiri Olsa if (ctf_writer__setup_env(cw, session)) 1598edbe9817SJiri Olsa goto free_session; 1599edbe9817SJiri Olsa 1600edbe9817SJiri Olsa /* CTF events setup */ 1601edbe9817SJiri Olsa if (setup_events(cw, session)) 1602edbe9817SJiri Olsa goto free_session; 1603edbe9817SJiri Olsa 1604f5a08cedSWang Nan if (opts->all && setup_non_sample_events(cw, session)) 1605f5a08cedSWang Nan goto free_session; 1606f5a08cedSWang Nan 160790e129ffSSebastian Andrzej Siewior if (setup_streams(cw, session)) 160890e129ffSSebastian Andrzej Siewior goto free_session; 160990e129ffSSebastian Andrzej Siewior 1610b7b61cbeSArnaldo Carvalho de Melo err = perf_session__process_events(session); 1611edbe9817SJiri Olsa if (!err) 161290e129ffSSebastian Andrzej Siewior err = ctf_writer__flush_streams(cw); 1613c2141055SHe Kuang else 1614c2141055SHe Kuang pr_err("Error during conversion.\n"); 1615edbe9817SJiri Olsa 1616edbe9817SJiri Olsa fprintf(stderr, 1617edbe9817SJiri Olsa "[ perf data convert: Converted '%s' into CTF data '%s' ]\n", 1618edbe9817SJiri Olsa file.path, path); 1619edbe9817SJiri Olsa 1620edbe9817SJiri Olsa fprintf(stderr, 16218ee4c46cSWang Nan "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples", 1622edbe9817SJiri Olsa (double) c.events_size / 1024.0 / 1024.0, 1623edbe9817SJiri Olsa c.events_count); 1624edbe9817SJiri Olsa 16258ee4c46cSWang Nan if (!c.non_sample_count) 16268ee4c46cSWang Nan fprintf(stderr, ") ]\n"); 16278ee4c46cSWang Nan else 16288ee4c46cSWang Nan fprintf(stderr, ", %" PRIu64 " non-samples) ]\n", c.non_sample_count); 16298ee4c46cSWang Nan 16305141d735SWang Nan cleanup_events(session); 1631c2141055SHe Kuang perf_session__delete(session); 1632c2141055SHe Kuang ctf_writer__cleanup(cw); 1633c2141055SHe Kuang 1634c2141055SHe Kuang return err; 1635c2141055SHe Kuang 1636edbe9817SJiri Olsa free_session: 1637edbe9817SJiri Olsa perf_session__delete(session); 1638edbe9817SJiri Olsa free_writer: 1639edbe9817SJiri Olsa ctf_writer__cleanup(cw); 1640c2141055SHe Kuang pr_err("Error during conversion setup.\n"); 1641edbe9817SJiri Olsa return err; 1642edbe9817SJiri Olsa } 1643