2008-11-25 16:24:15 +08:00
|
|
|
/*
|
2009-03-13 18:54:40 +08:00
|
|
|
* h/w branch tracer for x86 based on BTS
|
2008-11-25 16:24:15 +08:00
|
|
|
*
|
2009-01-19 17:26:53 +08:00
|
|
|
* Copyright (C) 2008-2009 Intel Corporation.
|
|
|
|
* Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
|
2008-11-25 16:24:15 +08:00
|
|
|
*/
|
2009-02-25 15:40:09 +08:00
|
|
|
#include <linux/kallsyms.h>
|
2008-11-25 16:24:15 +08:00
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/ftrace.h>
|
2009-02-25 15:40:09 +08:00
|
|
|
#include <linux/module.h>
|
2009-01-19 17:26:53 +08:00
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/smp.h>
|
2009-02-25 15:40:09 +08:00
|
|
|
#include <linux/fs.h>
|
2008-11-25 16:24:15 +08:00
|
|
|
|
|
|
|
#include <asm/ds.h>
|
|
|
|
|
2008-12-24 12:24:12 +08:00
|
|
|
#include "trace_output.h"
|
2009-03-13 18:54:40 +08:00
|
|
|
#include "trace.h"
|
2008-11-25 16:24:15 +08:00
|
|
|
|
|
|
|
|
2009-03-13 17:48:52 +08:00
|
|
|
#define BTS_BUFFER_SIZE (1 << 13)
|
2008-11-25 16:24:15 +08:00
|
|
|
|
|
|
|
static DEFINE_PER_CPU(struct bts_tracer *, tracer);
|
2009-03-13 17:48:52 +08:00
|
|
|
static DEFINE_PER_CPU(unsigned char[BTS_BUFFER_SIZE], buffer);
|
2008-11-25 16:24:15 +08:00
|
|
|
|
|
|
|
#define this_tracer per_cpu(tracer, smp_processor_id())
|
|
|
|
|
2009-03-13 17:48:52 +08:00
|
|
|
static int trace_hw_branches_enabled __read_mostly;
|
|
|
|
static int trace_hw_branches_suspended __read_mostly;
|
2009-01-19 17:31:01 +08:00
|
|
|
static struct trace_array *hw_branch_trace __read_mostly;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2009-01-19 17:26:53 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
static void bts_trace_init_cpu(int cpu)
|
2008-11-25 16:24:15 +08:00
|
|
|
{
|
2009-04-03 22:43:40 +08:00
|
|
|
per_cpu(tracer, cpu) =
|
|
|
|
ds_request_bts_cpu(cpu, per_cpu(buffer, cpu), BTS_BUFFER_SIZE,
|
|
|
|
NULL, (size_t)-1, BTS_KERNEL);
|
2008-12-11 20:53:26 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
if (IS_ERR(per_cpu(tracer, cpu)))
|
|
|
|
per_cpu(tracer, cpu) = NULL;
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
2009-03-13 17:48:52 +08:00
|
|
|
static int bts_trace_init(struct trace_array *tr)
|
2008-11-25 16:24:15 +08:00
|
|
|
{
|
2009-04-03 22:43:40 +08:00
|
|
|
int cpu;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2009-03-13 17:48:52 +08:00
|
|
|
hw_branch_trace = tr;
|
2009-04-03 22:43:40 +08:00
|
|
|
trace_hw_branches_enabled = 0;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
get_online_cpus();
|
|
|
|
for_each_online_cpu(cpu) {
|
|
|
|
bts_trace_init_cpu(cpu);
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
if (likely(per_cpu(tracer, cpu)))
|
|
|
|
trace_hw_branches_enabled = 1;
|
|
|
|
}
|
2009-03-13 17:48:52 +08:00
|
|
|
trace_hw_branches_suspended = 0;
|
2009-04-03 22:43:40 +08:00
|
|
|
put_online_cpus();
|
2009-03-13 17:48:52 +08:00
|
|
|
|
|
|
|
/* If we could not enable tracing on a single cpu, we fail. */
|
2009-04-03 22:43:40 +08:00
|
|
|
return trace_hw_branches_enabled ? 0 : -EOPNOTSUPP;
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
2009-03-13 17:48:52 +08:00
|
|
|
static void bts_trace_reset(struct trace_array *tr)
|
2008-11-25 16:24:15 +08:00
|
|
|
{
|
2009-04-03 22:43:40 +08:00
|
|
|
int cpu;
|
2009-01-19 17:26:53 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
get_online_cpus();
|
|
|
|
for_each_online_cpu(cpu) {
|
|
|
|
if (likely(per_cpu(tracer, cpu))) {
|
|
|
|
ds_release_bts(per_cpu(tracer, cpu));
|
|
|
|
per_cpu(tracer, cpu) = NULL;
|
|
|
|
}
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
2009-01-19 17:26:53 +08:00
|
|
|
trace_hw_branches_enabled = 0;
|
2009-03-13 17:48:52 +08:00
|
|
|
trace_hw_branches_suspended = 0;
|
2009-04-03 22:43:40 +08:00
|
|
|
put_online_cpus();
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
2009-03-13 17:48:52 +08:00
|
|
|
static void bts_trace_start(struct trace_array *tr)
|
2008-11-25 16:24:15 +08:00
|
|
|
{
|
2009-04-03 22:43:40 +08:00
|
|
|
int cpu;
|
2009-01-19 17:26:53 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
get_online_cpus();
|
|
|
|
for_each_online_cpu(cpu)
|
|
|
|
if (likely(per_cpu(tracer, cpu)))
|
|
|
|
ds_resume_bts(per_cpu(tracer, cpu));
|
2009-03-13 17:48:52 +08:00
|
|
|
trace_hw_branches_suspended = 0;
|
2009-04-03 22:43:40 +08:00
|
|
|
put_online_cpus();
|
2009-03-13 17:48:52 +08:00
|
|
|
}
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2009-03-13 17:48:52 +08:00
|
|
|
static void bts_trace_stop(struct trace_array *tr)
|
|
|
|
{
|
2009-04-03 22:43:40 +08:00
|
|
|
int cpu;
|
2009-03-13 17:48:52 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
get_online_cpus();
|
|
|
|
for_each_online_cpu(cpu)
|
|
|
|
if (likely(per_cpu(tracer, cpu)))
|
|
|
|
ds_suspend_bts(per_cpu(tracer, cpu));
|
2009-03-13 17:48:52 +08:00
|
|
|
trace_hw_branches_suspended = 1;
|
2009-04-03 22:43:40 +08:00
|
|
|
put_online_cpus();
|
2009-01-19 17:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
|
|
|
|
unsigned long action, void *hcpu)
|
|
|
|
{
|
2009-04-03 22:43:40 +08:00
|
|
|
int cpu = (long)hcpu;
|
2009-01-19 17:26:53 +08:00
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case CPU_ONLINE:
|
|
|
|
case CPU_DOWN_FAILED:
|
2009-04-03 22:43:40 +08:00
|
|
|
/* The notification is sent with interrupts enabled. */
|
|
|
|
if (trace_hw_branches_enabled) {
|
|
|
|
bts_trace_init_cpu(cpu);
|
|
|
|
|
|
|
|
if (trace_hw_branches_suspended &&
|
|
|
|
likely(per_cpu(tracer, cpu)))
|
|
|
|
ds_suspend_bts(per_cpu(tracer, cpu));
|
|
|
|
}
|
2009-01-19 17:26:53 +08:00
|
|
|
break;
|
2009-04-03 22:43:40 +08:00
|
|
|
|
2009-01-19 17:26:53 +08:00
|
|
|
case CPU_DOWN_PREPARE:
|
2009-04-03 22:43:40 +08:00
|
|
|
/* The notification is sent with interrupts enabled. */
|
|
|
|
if (likely(per_cpu(tracer, cpu))) {
|
|
|
|
ds_release_bts(per_cpu(tracer, cpu));
|
|
|
|
per_cpu(tracer, cpu) = NULL;
|
|
|
|
}
|
2009-01-19 17:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
2009-01-19 17:26:53 +08:00
|
|
|
static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
|
|
|
|
.notifier_call = bts_hotcpu_handler
|
|
|
|
};
|
|
|
|
|
2008-11-25 16:24:15 +08:00
|
|
|
static void bts_trace_print_header(struct seq_file *m)
|
|
|
|
{
|
2009-01-19 17:29:16 +08:00
|
|
|
seq_puts(m, "# CPU# TO <- FROM\n");
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
|
|
|
|
{
|
2009-03-13 18:54:40 +08:00
|
|
|
unsigned long symflags = TRACE_ITER_SYM_OFFSET;
|
2008-11-25 16:24:15 +08:00
|
|
|
struct trace_entry *entry = iter->ent;
|
|
|
|
struct trace_seq *seq = &iter->seq;
|
2008-12-11 20:53:26 +08:00
|
|
|
struct hw_branch_entry *it;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
|
|
|
trace_assign_type(it, entry);
|
|
|
|
|
2008-12-11 20:53:26 +08:00
|
|
|
if (entry->type == TRACE_HW_BRANCHES) {
|
2009-02-08 08:38:43 +08:00
|
|
|
if (trace_seq_printf(seq, "%4d ", iter->cpu) &&
|
2009-01-19 17:29:16 +08:00
|
|
|
seq_print_ip_sym(seq, it->to, symflags) &&
|
|
|
|
trace_seq_printf(seq, "\t <- ") &&
|
|
|
|
seq_print_ip_sym(seq, it->from, symflags) &&
|
2008-12-11 20:53:26 +08:00
|
|
|
trace_seq_printf(seq, "\n"))
|
|
|
|
return TRACE_TYPE_HANDLED;
|
|
|
|
return TRACE_TYPE_PARTIAL_LINE;;
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
return TRACE_TYPE_UNHANDLED;
|
|
|
|
}
|
|
|
|
|
2009-01-19 17:31:01 +08:00
|
|
|
void trace_hw_branch(u64 from, u64 to)
|
2008-11-25 16:24:15 +08:00
|
|
|
{
|
2009-03-31 13:48:49 +08:00
|
|
|
struct ftrace_event_call *call = &event_hw_branch;
|
2009-01-19 17:31:01 +08:00
|
|
|
struct trace_array *tr = hw_branch_trace;
|
2008-11-25 16:24:15 +08:00
|
|
|
struct ring_buffer_event *event;
|
2008-12-11 20:53:26 +08:00
|
|
|
struct hw_branch_entry *entry;
|
2009-02-06 02:12:56 +08:00
|
|
|
unsigned long irq1;
|
2009-01-19 17:26:53 +08:00
|
|
|
int cpu;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2009-01-19 17:26:53 +08:00
|
|
|
if (unlikely(!tr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (unlikely(!trace_hw_branches_enabled))
|
2008-11-25 16:24:15 +08:00
|
|
|
return;
|
2009-01-19 17:26:53 +08:00
|
|
|
|
|
|
|
local_irq_save(irq1);
|
|
|
|
cpu = raw_smp_processor_id();
|
|
|
|
if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
|
|
|
|
goto out;
|
|
|
|
|
tracing: Introduce trace_buffer_{lock_reserve,unlock_commit}
Impact: new API
These new functions do what previously was being open coded, reducing
the number of details ftrace plugin writers have to worry about.
It also standardizes the handling of stacktrace, userstacktrace and
other trace options we may introduce in the future.
With this patch, for instance, the blk tracer (and some others already
in the tree) can use the "userstacktrace" /d/tracing/trace_options
facility.
$ codiff /tmp/vmlinux.before /tmp/vmlinux.after
linux-2.6-tip/kernel/trace/trace.c:
trace_vprintk | -5
trace_graph_return | -22
trace_graph_entry | -26
trace_function | -45
__ftrace_trace_stack | -27
ftrace_trace_userstack | -29
tracing_sched_switch_trace | -66
tracing_stop | +1
trace_seq_to_user | -1
ftrace_trace_special | -63
ftrace_special | +1
tracing_sched_wakeup_trace | -70
tracing_reset_online_cpus | -1
13 functions changed, 2 bytes added, 355 bytes removed, diff: -353
linux-2.6-tip/block/blktrace.c:
__blk_add_trace | -58
1 function changed, 58 bytes removed, diff: -58
linux-2.6-tip/kernel/trace/trace.c:
trace_buffer_lock_reserve | +88
trace_buffer_unlock_commit | +86
2 functions changed, 174 bytes added, diff: +174
/tmp/vmlinux.after:
16 functions changed, 176 bytes added, 413 bytes removed, diff: -237
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Frédéric Weisbecker <fweisbec@gmail.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2009-02-06 02:14:13 +08:00
|
|
|
event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
|
|
|
|
sizeof(*entry), 0, 0);
|
2009-01-19 17:26:53 +08:00
|
|
|
if (!event)
|
|
|
|
goto out;
|
2008-11-25 16:24:15 +08:00
|
|
|
entry = ring_buffer_event_data(event);
|
|
|
|
tracing_generic_entry_update(&entry->ent, 0, from);
|
2008-12-11 20:53:26 +08:00
|
|
|
entry->ent.type = TRACE_HW_BRANCHES;
|
2008-11-25 16:24:15 +08:00
|
|
|
entry->from = from;
|
|
|
|
entry->to = to;
|
2009-04-08 16:15:54 +08:00
|
|
|
if (!filter_check_discard(call, entry, tr->buffer, event))
|
|
|
|
trace_buffer_unlock_commit(tr, event, 0, 0);
|
2009-01-19 17:26:53 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
atomic_dec(&tr->data[cpu]->disabled);
|
|
|
|
local_irq_restore(irq1);
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
2009-01-19 17:31:01 +08:00
|
|
|
static void trace_bts_at(const struct bts_trace *trace, void *at)
|
2008-11-25 16:24:15 +08:00
|
|
|
{
|
2008-12-11 20:53:26 +08:00
|
|
|
struct bts_struct bts;
|
|
|
|
int err = 0;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2008-12-11 20:53:26 +08:00
|
|
|
WARN_ON_ONCE(!trace->read);
|
|
|
|
if (!trace->read)
|
2008-11-25 16:24:15 +08:00
|
|
|
return;
|
|
|
|
|
2008-12-11 20:53:26 +08:00
|
|
|
err = trace->read(this_tracer, at, &bts);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2008-12-11 20:53:26 +08:00
|
|
|
switch (bts.qualifier) {
|
|
|
|
case BTS_BRANCH:
|
2009-01-19 17:31:01 +08:00
|
|
|
trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
|
2008-12-11 20:53:26 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
2009-01-19 17:26:53 +08:00
|
|
|
/*
|
|
|
|
* Collect the trace on the current cpu and write it into the ftrace buffer.
|
|
|
|
*
|
2009-04-03 22:43:40 +08:00
|
|
|
* pre: tracing must be suspended on the current cpu
|
2009-01-19 17:26:53 +08:00
|
|
|
*/
|
2008-11-25 16:24:15 +08:00
|
|
|
static void trace_bts_cpu(void *arg)
|
|
|
|
{
|
2009-03-13 17:48:52 +08:00
|
|
|
struct trace_array *tr = (struct trace_array *)arg;
|
2008-12-11 20:53:26 +08:00
|
|
|
const struct bts_trace *trace;
|
|
|
|
unsigned char *at;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2009-01-19 17:31:01 +08:00
|
|
|
if (unlikely(!tr))
|
2008-11-25 16:24:15 +08:00
|
|
|
return;
|
|
|
|
|
2009-01-19 17:26:53 +08:00
|
|
|
if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
|
|
|
|
return;
|
|
|
|
|
2009-01-19 17:31:01 +08:00
|
|
|
if (unlikely(!this_tracer))
|
|
|
|
return;
|
|
|
|
|
2008-12-11 20:53:26 +08:00
|
|
|
trace = ds_read_bts(this_tracer);
|
|
|
|
if (!trace)
|
2009-04-03 22:43:40 +08:00
|
|
|
return;
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2008-12-11 20:53:26 +08:00
|
|
|
for (at = trace->ds.top; (void *)at < trace->ds.end;
|
|
|
|
at += trace->ds.size)
|
2009-01-19 17:31:01 +08:00
|
|
|
trace_bts_at(trace, at);
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2008-12-11 20:53:26 +08:00
|
|
|
for (at = trace->ds.begin; (void *)at < trace->ds.top;
|
|
|
|
at += trace->ds.size)
|
2009-01-19 17:31:01 +08:00
|
|
|
trace_bts_at(trace, at);
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void trace_bts_prepare(struct trace_iterator *iter)
|
|
|
|
{
|
2009-04-03 22:43:40 +08:00
|
|
|
int cpu;
|
2009-01-19 17:26:53 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
get_online_cpus();
|
|
|
|
for_each_online_cpu(cpu)
|
|
|
|
if (likely(per_cpu(tracer, cpu)))
|
|
|
|
ds_suspend_bts(per_cpu(tracer, cpu));
|
|
|
|
/*
|
|
|
|
* We need to collect the trace on the respective cpu since ftrace
|
|
|
|
* implicitly adds the record for the current cpu.
|
|
|
|
* Once that is more flexible, we could collect the data from any cpu.
|
|
|
|
*/
|
2009-01-19 17:26:53 +08:00
|
|
|
on_each_cpu(trace_bts_cpu, iter->tr, 1);
|
2008-11-25 16:24:15 +08:00
|
|
|
|
2009-04-03 22:43:40 +08:00
|
|
|
for_each_online_cpu(cpu)
|
|
|
|
if (likely(per_cpu(tracer, cpu)))
|
|
|
|
ds_resume_bts(per_cpu(tracer, cpu));
|
|
|
|
put_online_cpus();
|
2008-11-25 16:24:15 +08:00
|
|
|
}
|
|
|
|
|
2009-01-19 17:33:31 +08:00
|
|
|
static void trace_bts_close(struct trace_iterator *iter)
|
|
|
|
{
|
|
|
|
tracing_reset_online_cpus(iter->tr);
|
|
|
|
}
|
|
|
|
|
2009-01-19 17:31:01 +08:00
|
|
|
void trace_hw_branch_oops(void)
|
|
|
|
{
|
2009-04-03 22:43:40 +08:00
|
|
|
if (this_tracer) {
|
|
|
|
ds_suspend_bts_noirq(this_tracer);
|
2009-03-13 17:48:52 +08:00
|
|
|
trace_bts_cpu(hw_branch_trace);
|
2009-04-03 22:43:40 +08:00
|
|
|
ds_resume_bts_noirq(this_tracer);
|
|
|
|
}
|
2009-01-19 17:31:01 +08:00
|
|
|
}
|
|
|
|
|
2008-11-25 16:24:15 +08:00
|
|
|
struct tracer bts_tracer __read_mostly =
|
|
|
|
{
|
2008-12-11 20:53:26 +08:00
|
|
|
.name = "hw-branch-tracer",
|
2008-11-25 16:24:15 +08:00
|
|
|
.init = bts_trace_init,
|
2009-01-19 17:26:53 +08:00
|
|
|
.reset = bts_trace_reset,
|
2008-11-25 16:24:15 +08:00
|
|
|
.print_header = bts_trace_print_header,
|
|
|
|
.print_line = bts_trace_print_line,
|
|
|
|
.start = bts_trace_start,
|
|
|
|
.stop = bts_trace_stop,
|
2009-01-19 17:33:31 +08:00
|
|
|
.open = trace_bts_prepare,
|
2009-03-13 17:50:27 +08:00
|
|
|
.close = trace_bts_close,
|
|
|
|
#ifdef CONFIG_FTRACE_SELFTEST
|
|
|
|
.selftest = trace_selftest_startup_hw_branches,
|
|
|
|
#endif /* CONFIG_FTRACE_SELFTEST */
|
2008-11-25 16:24:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
__init static int init_bts_trace(void)
|
|
|
|
{
|
2009-02-24 20:55:18 +08:00
|
|
|
register_hotcpu_notifier(&bts_hotcpu_notifier);
|
2008-11-25 16:24:15 +08:00
|
|
|
return register_tracer(&bts_tracer);
|
|
|
|
}
|
|
|
|
device_initcall(init_bts_trace);
|