trace_hw_branches.c 4.4 KB
Newer Older
1
/*
2
 * h/w branch tracer for x86 based on bts
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * Copyright (C) 2008 Markus Metzger <markus.t.metzger@gmail.com>
 *
 */

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/debugfs.h>
#include <linux/ftrace.h>
#include <linux/kallsyms.h>

#include <asm/ds.h>

#include "trace.h"


#define SIZEOF_BTS (1 << 13)

static DEFINE_PER_CPU(struct bts_tracer *, tracer);
static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);

#define this_tracer per_cpu(tracer, smp_processor_id())
#define this_buffer per_cpu(buffer, smp_processor_id())


static void bts_trace_reset(struct trace_array *tr)
{
	int cpu;

	tr->time_start = ftrace_now(tr->cpu);

	for_each_online_cpu(cpu)
		tracing_reset(tr, cpu);
}

static void bts_trace_start_cpu(void *arg)
{
40 41 42
	if (this_tracer)
		ds_release_bts(this_tracer);

43 44
	this_tracer =
		ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
45 46
			       /* ovfl = */ NULL, /* th = */ (size_t)-1,
			       BTS_KERNEL);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	if (IS_ERR(this_tracer)) {
		this_tracer = NULL;
		return;
	}
}

static void bts_trace_start(struct trace_array *tr)
{
	int cpu;

	bts_trace_reset(tr);

	for_each_cpu_mask(cpu, cpu_possible_map)
		smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
}

static void bts_trace_stop_cpu(void *arg)
{
	if (this_tracer) {
		ds_release_bts(this_tracer);
		this_tracer = NULL;
	}
}

static void bts_trace_stop(struct trace_array *tr)
{
	int cpu;

	for_each_cpu_mask(cpu, cpu_possible_map)
		smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
}

static int bts_trace_init(struct trace_array *tr)
{
	bts_trace_reset(tr);
	bts_trace_start(tr);

	return 0;
}

static void bts_trace_print_header(struct seq_file *m)
{
	seq_puts(m,
		 "# CPU#        FROM                   TO         FUNCTION\n");
	seq_puts(m,
		 "#  |           |                     |             |\n");
}

static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
{
	struct trace_entry *entry = iter->ent;
	struct trace_seq *seq = &iter->seq;
99
	struct hw_branch_entry *it;
100 101 102

	trace_assign_type(it, entry);

103 104 105 106 107 108 109 110 111
	if (entry->type == TRACE_HW_BRANCHES) {
		if (trace_seq_printf(seq, "%4d  ", entry->cpu) &&
		    trace_seq_printf(seq, "0x%016llx -> 0x%016llx ",
				     it->from, it->to) &&
		    (!it->from ||
		     seq_print_ip_sym(seq, it->from, /* sym_flags = */ 0)) &&
		    trace_seq_printf(seq, "\n"))
			return TRACE_TYPE_HANDLED;
		return TRACE_TYPE_PARTIAL_LINE;;
112 113 114 115
	}
	return TRACE_TYPE_UNHANDLED;
}

116
void trace_hw_branch(struct trace_array *tr, u64 from, u64 to)
117 118
{
	struct ring_buffer_event *event;
119
	struct hw_branch_entry *entry;
120 121 122 123 124 125 126
	unsigned long irq;

	event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq);
	if (!event)
		return;
	entry	= ring_buffer_event_data(event);
	tracing_generic_entry_update(&entry->ent, 0, from);
127
	entry->ent.type = TRACE_HW_BRANCHES;
128 129 130 131 132 133
	entry->ent.cpu = smp_processor_id();
	entry->from = from;
	entry->to   = to;
	ring_buffer_unlock_commit(tr->buffer, event, irq);
}

134 135
static void trace_bts_at(struct trace_array *tr,
			 const struct bts_trace *trace, void *at)
136
{
137 138
	struct bts_struct bts;
	int err = 0;
139

140 141
	WARN_ON_ONCE(!trace->read);
	if (!trace->read)
142 143
		return;

144 145 146
	err = trace->read(this_tracer, at, &bts);
	if (err < 0)
		return;
147

148 149 150 151 152
	switch (bts.qualifier) {
	case BTS_BRANCH:
		trace_hw_branch(tr, bts.variant.lbr.from, bts.variant.lbr.to);
		break;
	}
153 154 155 156 157
}

static void trace_bts_cpu(void *arg)
{
	struct trace_array *tr = (struct trace_array *) arg;
158 159
	const struct bts_trace *trace;
	unsigned char *at;
160 161 162 163

	if (!this_tracer)
		return;

164 165 166
	ds_suspend_bts(this_tracer);
	trace = ds_read_bts(this_tracer);
	if (!trace)
167 168
		goto out;

169 170 171
	for (at = trace->ds.top; (void *)at < trace->ds.end;
	     at += trace->ds.size)
		trace_bts_at(tr, trace, at);
172

173 174 175
	for (at = trace->ds.begin; (void *)at < trace->ds.top;
	     at += trace->ds.size)
		trace_bts_at(tr, trace, at);
176 177

out:
178
	ds_resume_bts(this_tracer);
179 180 181 182 183 184 185 186 187 188 189 190
}

static void trace_bts_prepare(struct trace_iterator *iter)
{
	int cpu;

	for_each_cpu_mask(cpu, cpu_possible_map)
		smp_call_function_single(cpu, trace_bts_cpu, iter->tr, 1);
}

struct tracer bts_tracer __read_mostly =
{
191
	.name		= "hw-branch-tracer",
192 193 194 195 196 197 198 199 200 201 202 203 204 205
	.init		= bts_trace_init,
	.reset		= bts_trace_stop,
	.print_header	= bts_trace_print_header,
	.print_line	= bts_trace_print_line,
	.start		= bts_trace_start,
	.stop		= bts_trace_stop,
	.open		= trace_bts_prepare
};

__init static int init_bts_trace(void)
{
	return register_tracer(&bts_tracer);
}
device_initcall(init_bts_trace);