Release 4.10 tools/perf/util/data-convert-bt.c
/*
* CTF writing support via babeltrace.
*
* Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com>
* Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de>
*
* Released under the GPL v2. (and only v2, not any later version)
*/
#include <linux/compiler.h>
#include <babeltrace/ctf-writer/writer.h>
#include <babeltrace/ctf-writer/clock.h>
#include <babeltrace/ctf-writer/stream.h>
#include <babeltrace/ctf-writer/event.h>
#include <babeltrace/ctf-writer/event-types.h>
#include <babeltrace/ctf-writer/event-fields.h>
#include <babeltrace/ctf-ir/utils.h>
#include <babeltrace/ctf/events.h>
#include <traceevent/event-parse.h>
#include "asm/bug.h"
#include "data-convert-bt.h"
#include "session.h"
#include "util.h"
#include "debug.h"
#include "tool.h"
#include "evlist.h"
#include "evsel.h"
#include "machine.h"
#include "config.h"
#define pr_N(n, fmt, ...) \
eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
#define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
#define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
#define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
struct evsel_priv {
struct bt_ctf_event_class *event_class;
};
#define MAX_CPUS 4096
struct ctf_stream {
struct bt_ctf_stream *stream;
int cpu;
u32 count;
};
struct ctf_writer {
/* writer primitives */
struct bt_ctf_writer *writer;
struct ctf_stream **stream;
int stream_cnt;
struct bt_ctf_stream_class *stream_class;
struct bt_ctf_clock *clock;
/* data types */
union {
struct {
struct bt_ctf_field_type *s64;
struct bt_ctf_field_type *u64;
struct bt_ctf_field_type *s32;
struct bt_ctf_field_type *u32;
struct bt_ctf_field_type *string;
struct bt_ctf_field_type *u32_hex;
struct bt_ctf_field_type *u64_hex;
};
struct bt_ctf_field_type *array[6];
}
data;
struct bt_ctf_event_class *comm_class;
struct bt_ctf_event_class *exit_class;
struct bt_ctf_event_class *fork_class;
};
struct convert {
struct perf_tool tool;
struct ctf_writer writer;
u64 events_size;
u64 events_count;
u64 non_sample_count;
/* Ordered events configured queue size. */
u64 queue_size;
};
static int value_set(struct bt_ctf_field_type *type,
struct bt_ctf_event *event,
const char *name, u64 val)
{
struct bt_ctf_field *field;
bool sign = bt_ctf_field_type_integer_get_signed(type);
int ret;
field = bt_ctf_field_create(type);
if (!field) {
pr_err("failed to create a field %s\n", name);
return -1;
}
if (sign) {
ret = bt_ctf_field_signed_integer_set_value(field, val);
if (ret) {
pr_err("failed to set field value %s\n", name);
goto err;
}
} else {
ret = bt_ctf_field_unsigned_integer_set_value(field, val);
if (ret) {
pr_err("failed to set field value %s\n", name);
goto err;
}
}
ret = bt_ctf_event_set_payload(event, name, field);
if (ret) {
pr_err("failed to set payload %s\n", name);
goto err;
}
pr2(" SET [%s = %" PRIu64 "]\n", name, val);
err:
bt_ctf_field_put(field);
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jiri olsa | jiri olsa | 172 | 100.00% | 1 | 100.00% |
| Total | 172 | 100.00% | 1 | 100.00% |
#define __FUNC_VALUE_SET(_name, _val_type) \
static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
struct bt_ctf_event *event, \
const char *name, \
_val_type val) \
{ \
struct bt_ctf_field_type *type = cw->data._name; \
return value_set(type, event, name, (u64) val); \
}
#define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
FUNC_VALUE_SET(s32)
FUNC_VALUE_SET(u32)
FUNC_VALUE_SET(s64)
FUNC_VALUE_SET(u64)
__FUNC_VALUE_SET(u64_hex, u64)
static int string_set_value(struct bt_ctf_field *field, const char *string);
static __maybe_unused int
value_set_string(struct ctf_writer *cw, struct bt_ctf_event *event,
const char *name, const char *string)
{
struct bt_ctf_field_type *type = cw->data.string;
struct bt_ctf_field *field;
int ret = 0;
field = bt_ctf_field_create(type);
if (!field) {
pr_err("failed to create a field %s\n", name);
return -1;
}
ret = string_set_value(field, string);
if (ret) {
pr_err("failed to set value %s\n", name);
goto err_put_field;
}
ret = bt_ctf_event_set_payload(event, name, field);
if (ret)
pr_err("failed to set payload %s\n", name);
err_put_field:
bt_ctf_field_put(field);
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
wang nan | wang nan | 130 | 100.00% | 1 | 100.00% |
| Total | 130 | 100.00% | 1 | 100.00% |
static struct bt_ctf_field_type*
get_tracepoint_field_type(struct ctf_writer *cw, struct format_field *field)
{
unsigned long flags = field->flags;
if (flags & FIELD_IS_STRING)
return cw->data.string;
if (!(flags & FIELD_IS_SIGNED)) {
/* unsigned long are mostly pointers */
if (flags & FIELD_IS_LONG || flags & FIELD_IS_POINTER)
return cw->data.u64_hex;
}
if (flags & FIELD_IS_SIGNED) {
if (field->size == 8)
return cw->data.s64;
else
return cw->data.s32;
}
if (field->size == 8)
return cw->data.u64;
else
return cw->data.u32;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
sebastian andrzej siewior | sebastian andrzej siewior | 122 | 100.00% | 1 | 100.00% |
| Total | 122 | 100.00% | 1 | 100.00% |
static unsigned long long adjust_signedness(unsigned long long value_int, int size)
{
unsigned long long value_mask;
/*
* value_mask = (1 << (size * 8 - 1)) - 1.
* Directly set value_mask for code readers.
*/
switch (size) {
case 1:
value_mask = 0x7fULL;
break;
case 2:
value_mask = 0x7fffULL;
break;
case 4:
value_mask = 0x7fffffffULL;
break;
case 8:
/*
* For 64 bit value, return it self. There is no need
* to fill high bit.
*/
/* Fall through */
default:
/* BUG! */
return value_int;
}
/* If it is a positive value, don't adjust. */
if ((value_int & (~0ULL - value_mask)) == 0)
return value_int;
/* Fill upper part of value_int with 1 to make it a negative long long. */
return (value_int & value_mask) | ~value_mask;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
wang nan | wang nan | 92 | 100.00% | 1 | 100.00% |
| Total | 92 | 100.00% | 1 | 100.00% |
static int string_set_value(struct bt_ctf_field *field, const char *string)
{
char *buffer = NULL;
size_t len = strlen(string), i, p;
int err;
for (i = p = 0; i < len; i++, p++) {
if (isprint(string[i])) {
if (!buffer)
continue;
buffer[p] = string[i];
} else {
char numstr[5];
snprintf(numstr, sizeof(numstr), "\\x%02x",
(unsigned int)(string[i]) & 0xff);
if (!buffer) {
buffer = zalloc(i + (len - i) * 4 + 2);
if (!buffer) {
pr_err("failed to set unprintable string '%s'\n", string);
return bt_ctf_field_string_set_value(field, "UNPRINTABLE-STRING");
}
if (i > 0)
strncpy(buffer, string, i);
}
strncat(buffer + p, numstr, 4);
p += 3;
}
}
if (!buffer)
return bt_ctf_field_string_set_value(field, string);
err = bt_ctf_field_string_set_value(field, buffer);
free(buffer);
return err;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
wang nan | wang nan | 225 | 100.00% | 1 | 100.00% |
| Total | 225 | 100.00% | 1 | 100.00% |
static int add_tracepoint_field_value(struct ctf_writer *cw,
struct bt_ctf_event_class *event_class,
struct bt_ctf_event *event,
struct perf_sample *sample,
struct format_field *fmtf)
{
struct bt_ctf_field_type *type;
struct bt_ctf_field *array_field;
struct bt_ctf_field *field;
const char *name = fmtf->name;
void *data = sample->raw_data;
unsigned long flags = fmtf->flags;
unsigned int n_items;
unsigned int i;
unsigned int offset;
unsigned int len;
int ret;
name = fmtf->alias;
offset = fmtf->offset;
len = fmtf->size;
if (flags & FIELD_IS_STRING)
flags &= ~FIELD_IS_ARRAY;
if (flags & FIELD_IS_DYNAMIC) {
unsigned long long tmp_val;
tmp_val = pevent_read_number(fmtf->event->pevent,
data + offset, len);
offset = tmp_val;
len = offset >> 16;
offset &= 0xffff;
}
if (flags & FIELD_IS_ARRAY) {
type = bt_ctf_event_class_get_field_by_name(
event_class, name);
array_field = bt_ctf_field_create(type);
bt_ctf_field_type_put(type);
if (!array_field) {
pr_err("Failed to create array type %s\n", name);
return -1;
}
len = fmtf->size / fmtf->arraylen;
n_items = fmtf->arraylen;
} else {
n_items = 1;
array_field = NULL;
}
type = get_tracepoint_field_type(cw, fmtf);
for (i = 0; i < n_items; i++) {
if (flags & FIELD_IS_ARRAY)
field = bt_ctf_field_array_get_field(array_field, i);
else
field = bt_ctf_field_create(type);
if (!field) {
pr_err("failed to create a field %s\n", name);
return -1;
}
if (flags & FIELD_IS_STRING)
ret = string_set_value(field, data + offset + i * len);
else {
unsigned long long value_int;
value_int = pevent_read_number(
fmtf->event->pevent,
data + offset + i * len, len);
if (!(flags & FIELD_IS_SIGNED))
ret = bt_ctf_field_unsigned_integer_set_value(
field, value_int);
else
ret = bt_ctf_field_signed_integer_set_value(
field, adjust_signedness(value_int, len));
}
if (ret) {
pr_err("failed to set file value %s\n", name);
goto err_put_field;
}
if (!(flags & FIELD_IS_ARRAY)) {
ret = bt_ctf_event_set_payload(event, name, field);
if (ret) {
pr_err("failed to set payload %s\n", name);
goto err_put_field;
}
}
bt_ctf_field_put(field);
}
if (flags & FIELD_IS_ARRAY) {
ret = bt_ctf_event_set_payload(event, name, array_field);
if (ret) {
pr_err("Failed add payload array %s\n", name);
return -1;
}
bt_ctf_field_put(array_field);
}
return 0;
err_put_field:
bt_ctf_field_put(field);
return -1;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
sebastian andrzej siewior | sebastian andrzej siewior | 458 | 91.78% | 1 | 25.00% |
wang nan | wang nan | 41 | 8.22% | 3 | 75.00% |
| Total | 499 | 100.00% | 4 | 100.00% |
static int add_tracepoint_fields_values(struct ctf_writer *cw,
struct bt_ctf_event_class *event_class,
struct bt_ctf_event *event,
struct format_field *fields,
struct perf_sample *sample)
{
struct format_field *field;
int ret;
for (field = fields; field; field = field->next) {
ret = add_tracepoint_field_value(cw, event_class, event, sample,
field);
if (ret)
return -1;
}
return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
sebastian andrzej siewior | sebastian andrzej siewior | 81 | 100.00% | 1 | 100.00% |
| Total | 81 | 100.00% | 1 | 100.00% |
static int add_tracepoint_values(struct ctf_writer *cw,
struct bt_ctf_event_class *event_class,
struct bt_ctf_event *event,
struct perf_evsel *evsel,
struct perf_sample *sample)
{
struct format_field *common_fields = evsel->tp_format->format.common_fields;
struct format_field *fields = evsel->tp_format->format.fields;
int ret;
ret = add_tracepoint_fields_values(cw, event_class, event,
common_fields, sample);
if (!ret)
ret = add_tracepoint_fields_values(cw, event_class, event,
fields, sample);
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
sebastian andrzej siewior | sebastian andrzej siewior | 98 | 100.00% | 1 | 100.00% |
| Total | 98 | 100.00% | 1 | 100.00% |
static int
add_bpf_output_values(struct bt_ctf_event_class *event_class,
struct bt_ctf_event *event,
struct perf_sample *sample)
{
struct bt_ctf_field_type *len_type, *seq_type;
struct bt_ctf_field *len_field, *seq_field;
unsigned int raw_size = sample->raw_size;
unsigned int nr_elements = raw_size / sizeof(u32);
unsigned int i;
int ret;
if (nr_elements * sizeof(u32) != raw_size)
pr_warning("Incorrect raw_size (%u) in bpf output event, skip %zu bytes\n",
raw_size, nr_elements * sizeof(u32) - raw_size);
len_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_len");
len_field = bt_ctf_field_create(len_type);
if (!len_field) {
pr_err("failed to create 'raw_len' for bpf output event\n");
ret = -1;
goto put_len_type;
}
ret = bt_ctf_field_unsigned_integer_set_value(len_field, nr_elements);
if (ret) {
pr_err("failed to set field value for raw_len\n");
goto put_len_field;
}
ret = bt_ctf_event_set_payload(event, "raw_len", len_field);
if (ret) {
pr_err("failed to set payload to raw_len\n");
goto put_len_field;
}
seq_type = bt_ctf_event_class_get_field_by_name(event_class, "raw_data");
seq_field = bt_ctf_field_create(seq_type);
if (!seq_field) {
pr_err("failed to create 'raw_data' for bpf output event\n");
ret = -1;
goto put_seq_type;
}
ret = bt_ctf_field_sequence_set_length(seq_field, len_field);
if (ret) {
pr_err("failed to set length of 'raw_data'\n");
goto put_seq_field;
}
for (i = 0; i < nr_elements; i++) {
struct bt_ctf_field *elem_field =
bt_ctf_field_sequence_get_field(seq_field, i);
ret = bt_ctf_field_unsigned_integer_set_value(elem_field,
((u32 *)(sample->raw_data))[i]);
bt_ctf_field_put(elem_field);
if (ret) {
pr_err("failed to set raw_data[%d]\n", i);
goto put_seq_field;
}
}
ret = bt_ctf_event_set_payload(event, "raw_data", seq_field);
if (ret)
pr_err("failed to set payload for raw_data\n");
put_seq_field:
bt_ctf_field_put(seq_field);
put_seq_type:
bt_ctf_field_type_put(seq_type);
put_len_field:
bt_ctf_field_put(len_field);
put_len_type:
bt_ctf_field_type_put(len_type);
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
wang nan | wang nan | 354 | 100.00% | 2 | 100.00% |
| Total | 354 | 100.00% | 2 | 100.00% |
static int add_generic_values(struct ctf_writer *cw,
struct bt_ctf_event *event,
struct perf_evsel *evsel,
struct perf_sample *sample)
{
u64 type = evsel->attr.sample_type;
int ret;
/*
* missing:
* PERF_SAMPLE_TIME - not needed as we have it in
* ctf event header
* PERF_SAMPLE_READ - TODO
* PERF_SAMPLE_CALLCHAIN - TODO
* PERF_SAMPLE_RAW - tracepoint fields are handled separately
* PERF_SAMPLE_BRANCH_STACK - TODO
* PERF_SAMPLE_REGS_USER - TODO
* PERF_SAMPLE_STACK_USER - TODO
*/
if (type & PERF_SAMPLE_IP) {
ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
if (ret)
return -1;
}
if (type & PERF_SAMPLE_TID) {
ret = value_set_s32(cw, event, "perf_tid", sample->tid);
if (ret)
return -1;
ret = value_set_s32(cw, event, "perf_pid", sample->pid);
if (ret)
return -1;
}
if ((type & PERF_SAMPLE_ID) ||
(type & PERF_SAMPLE_IDENTIFIER)) {
ret = value_set_u64(cw, event, "perf_id", sample->id);
if (ret)
return -1;
}
if (type & PERF_SAMPLE_STREAM_ID) {
ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
if (ret)
return -1;
}
if (type & PERF_SAMPLE_PERIOD) {
ret = value_set_u64(cw, event, "perf_period", sample->period);
if (ret)
return -1;
}
if (type & PERF_SAMPLE_WEIGHT) {
ret = value_set_u64(cw, event, "perf_weight", sample->weight);
if (ret)
return -1;
}
if (type & PERF_SAMPLE_DATA_SRC) {
ret = value_set_u64(cw, event, "perf_data_src",
sample->data_src);
if (ret)
return -1;
}
if (type & PERF_SAMPLE_TRANSACTION) {
ret = value_set_u64(cw, event, "perf_transaction",
sample->transaction);
if (ret)
return -1;
}
return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
jiri olsa | jiri olsa | 299 | 93.15% | 1 | 33.33% |
sebastian andrzej siewior | sebastian andrzej siewior | 22 | 6.85% | 2 | 66.67% |
| Total | 321 | 100.00% | 3 | 100.00% |
static int ctf_stream__flush(struct ctf_stream *cs)
{
int err = 0;
if (cs) {
err = bt_ctf_stream_flush(cs->stream);
if (err)
pr_err("CTF stream %d flush failed\n", cs->cpu);
pr("Flush stream for cpu %d (%u samples)\n",
cs->cpu, cs->count);
cs->count = 0;
}
return err;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
sebastian andrzej siewior | sebastian andrzej siewior | 51 | 77.27% | 1 | 33.33% |
jiri olsa | jiri olsa | 15 | 22.73% | 2 | 66.67% |
| Total | 66 | 100.00% | 3 | 100.00% |
static struct ctf_stream *ctf_stream__create(struct ctf_writer *cw, int cpu)
{
struct ctf_stream *cs;
struct bt_ctf_field *pkt_ctx = NULL;
struct bt_ctf_field *cpu_field = NULL;
struct bt_ctf_stream *stream = NULL;
int ret;
cs = zalloc(sizeof(*cs));
if (!cs) {
pr_err("Failed to allocate ctf stream\n");
return NULL;
}
stream = bt_ctf_writer_create_stream(cw->writer, cw->stream_class);
if (!stream) {
pr_err("Failed to create CTF stream\n");
goto out;
}
pkt_ctx = bt_ctf_stream_get_packet_context(stream);
if (!pkt_ctx) {
pr_err("Failed to obtain packet context\n");
goto out;
}
cpu_field = bt_ctf_field_structure_get_field(pkt_ctx, "cpu_id");
bt_ctf_field_put(pkt_ctx);
if (!cpu_field) {
pr_err("Failed to obtain cpu field\n");
goto out;
}
ret = bt_ctf_field_unsigned_integer_set_value(cpu_field, (u32) cpu);
if (ret) {
pr_err("Failed to update CPU number\n");
goto out;
}
bt_ctf_field_put(cpu_field);
cs->cpu = cpu;
cs->stream = stream;
return cs;
out:
if (cpu_field)
bt_ctf_field_put(cpu_field);
if (stream)
bt_ctf_stream_put(stream);
free(cs);
return NULL;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
sebastian andrzej siewior | sebastian andrzej siewior | 208 | 92.86% | 1 | 50.00% |
jiri olsa | jiri olsa | 16 | 7.14% | 1 | 50.00% |
| Total | 224 | 100.00% | 2 | 100.00% |
static void ctf_stream__delete(struct ctf_stream *cs)
{
if (cs) {
bt_ctf_stream_put(cs->stream);
free(cs);
}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
sebastian andrzej siewior | sebastian andrzej siewior | 29 | 100.00% | 1 | 100.00% |
| Total | 29 | 100.00% | 1 | 100.00% |
static struct ctf_stream *ctf_stream(struct ctf_writer *cw, int cpu)
{
struct ctf_stream *cs = cw->stream[cpu];
if (!cs) {
cs = ctf_stream__create(cw, cpu);
cw->stream[cpu] = cs;
}
return cs;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
sebastian andrzej siewior | sebastian andrzej siewior | 55 | 98.21% | 1 | 50.00% |
jiri olsa | jiri olsa | 1 | 1.79% | 1 | 50.00% |
| Total | 56 | 100.00% | 2 | 100.00% |
static int get_sample_cpu(struct ctf_writer *cw, struct perf_sample *sample,
struct perf_evsel *evsel)
{
int cpu = 0;
if (evsel->attr.sample_type & PERF_SAMPLE_CPU)
cpu = sample->cpu;
if (cpu > cw->stream_cnt) {
pr_err("Event was recorded for CPU %d, limit is at %d.\n",
cpu, cw->stream_cnt);
cpu = 0