Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 121098
b: refs/heads/master
c: 1f0d69a
h: refs/heads/master
v: v3
  • Loading branch information
Steven Rostedt authored and Ingo Molnar committed Nov 12, 2008
1 parent 4f7c086 commit 1d11b3a
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 4 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: cb9382e5a94e54d0356d730954396c746ae66d6e
refs/heads/master: 1f0d69a9fc815db82f15722bf05227190b1d714d
8 changes: 8 additions & 0 deletions trunk/arch/x86/kernel/vsyscall_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
* want per guest time just set the kernel.vsyscall64 sysctl to 0.
*/

/* Protect userspace from profiling */
#ifdef CONFIG_TRACE_UNLIKELY_PROFILE
# undef likely
# undef unlikely
# define likely(x) likely_notrace(x)
# define unlikely(x) unlikely_notrace(x)
#endif

#include <linux/time.h>
#include <linux/init.h>
#include <linux/kernel.h>
Expand Down
14 changes: 13 additions & 1 deletion trunk/include/asm-generic/vmlinux.lds.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
#define MCOUNT_REC()
#endif

#ifdef CONFIG_TRACE_UNLIKELY_PROFILE
#define LIKELY_PROFILE() VMLINUX_SYMBOL(__start_likely_profile) = .; \
*(_ftrace_likely) \
VMLINUX_SYMBOL(__stop_likely_profile) = .; \
VMLINUX_SYMBOL(__start_unlikely_profile) = .; \
*(_ftrace_unlikely) \
VMLINUX_SYMBOL(__stop_unlikely_profile) = .;
#else
#define LIKELY_PROFILE()
#endif

/* .data section */
#define DATA_DATA \
*(.data) \
Expand All @@ -62,7 +73,8 @@
VMLINUX_SYMBOL(__stop___markers) = .; \
VMLINUX_SYMBOL(__start___tracepoints) = .; \
*(__tracepoints) \
VMLINUX_SYMBOL(__stop___tracepoints) = .;
VMLINUX_SYMBOL(__stop___tracepoints) = .; \
LIKELY_PROFILE()

#define RO_DATA(align) \
. = ALIGN((align)); \
Expand Down
61 changes: 59 additions & 2 deletions trunk/include/linux/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,65 @@ extern void __chk_io_ptr(const volatile void __iomem *);
* specific implementations come from the above header files
*/

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#ifdef CONFIG_TRACE_UNLIKELY_PROFILE
struct ftrace_likely_data {
const char *func;
const char *file;
unsigned line;
unsigned long correct;
unsigned long incorrect;
};
void ftrace_likely_update(struct ftrace_likely_data *f, int val, int expect);

#define likely_notrace(x) __builtin_expect(!!(x), 1)
#define unlikely_notrace(x) __builtin_expect(!!(x), 0)

#define likely_check(x) ({ \
int ______r; \
static struct ftrace_likely_data \
__attribute__((__aligned__(4))) \
__attribute__((section("_ftrace_likely"))) \
______f = { \
.func = __func__, \
.file = __FILE__, \
.line = __LINE__, \
}; \
______f.line = __LINE__; \
______r = likely_notrace(x); \
ftrace_likely_update(&______f, ______r, 1); \
______r; \
})
#define unlikely_check(x) ({ \
int ______r; \
static struct ftrace_likely_data \
__attribute__((__aligned__(4))) \
__attribute__((section("_ftrace_unlikely"))) \
______f = { \
.func = __func__, \
.file = __FILE__, \
.line = __LINE__, \
}; \
______f.line = __LINE__; \
______r = unlikely_notrace(x); \
ftrace_likely_update(&______f, ______r, 0); \
______r; \
})

/*
* Using __builtin_constant_p(x) to ignore cases where the return
* value is always the same. This idea is taken from a similar patch
* written by Daniel Walker.
*/
# ifndef likely
# define likely(x) (__builtin_constant_p(x) ? !!(x) : likely_check(x))
# endif
# ifndef unlikely
# define unlikely(x) (__builtin_constant_p(x) ? !!(x) : unlikely_check(x))
# endif
#else
# define likely(x) __builtin_expect(!!(x), 1)
# define unlikely(x) __builtin_expect(!!(x), 0)
#endif

/* Optimization barrier */
#ifndef barrier
Expand Down
16 changes: 16 additions & 0 deletions trunk/kernel/trace/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ config BOOT_TRACER
selected, because the self-tests are an initcall as well and that
would invalidate the boot trace. )

config TRACE_UNLIKELY_PROFILE
bool "Trace likely/unlikely profiler"
depends on DEBUG_KERNEL
select TRACING
help
This tracer profiles all the the likely and unlikely macros
in the kernel. It will display the results in:

/debugfs/tracing/profile_likely
/debugfs/tracing/profile_unlikely

Note: this will add a significant overhead, only turn this
on if you need to profile the system's use of these macros.

Say N if unsure.

config STACK_TRACER
bool "Trace max stack"
depends on HAVE_FUNCTION_TRACER
Expand Down
1 change: 1 addition & 0 deletions trunk/kernel/trace/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ obj-$(CONFIG_STACK_TRACER) += trace_stack.o
obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
obj-$(CONFIG_BOOT_TRACER) += trace_boot.o
obj-$(CONFIG_FUNCTION_RET_TRACER) += trace_functions_return.o
obj-$(CONFIG_TRACE_UNLIKELY_PROFILE) += trace_unlikely.o

libftrace-y := ftrace.o
164 changes: 164 additions & 0 deletions trunk/kernel/trace/trace_unlikely.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* unlikely profiler
*
* Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
*/
#include <linux/kallsyms.h>
#include <linux/seq_file.h>
#include <linux/spinlock.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/ftrace.h>
#include <linux/hash.h>
#include <linux/fs.h>
#include <asm/local.h>
#include "trace.h"

void ftrace_likely_update(struct ftrace_likely_data *f, int val, int expect)
{
/* FIXME: Make this atomic! */
if (val == expect)
f->correct++;
else
f->incorrect++;
}
EXPORT_SYMBOL(ftrace_likely_update);

struct ftrace_pointer {
void *start;
void *stop;
};

static void *
t_next(struct seq_file *m, void *v, loff_t *pos)
{
struct ftrace_pointer *f = m->private;
struct ftrace_likely_data *p = v;

(*pos)++;

if (v == (void *)1)
return f->start;

++p;

if ((void *)p >= (void *)f->stop)
return NULL;

return p;
}

static void *t_start(struct seq_file *m, loff_t *pos)
{
void *t = (void *)1;
loff_t l = 0;

for (; t && l < *pos; t = t_next(m, t, &l))
;

return t;
}

static void t_stop(struct seq_file *m, void *p)
{
}

static int t_show(struct seq_file *m, void *v)
{
struct ftrace_likely_data *p = v;
const char *f;
unsigned long percent;

if (v == (void *)1) {
seq_printf(m, " correct incorrect %% "
" Function "
" File Line\n"
" ------- --------- - "
" -------- "
" ---- ----\n");
return 0;
}

/* Only print the file, not the path */
f = p->file + strlen(p->file);
while (f >= p->file && *f != '/')
f--;
f++;

if (p->correct) {
percent = p->incorrect * 100;
percent /= p->correct + p->incorrect;
} else
percent = p->incorrect ? 100 : 0;

seq_printf(m, "%8lu %8lu %3lu ", p->correct, p->incorrect, percent);
seq_printf(m, "%-30.30s %-20.20s %d\n", p->func, f, p->line);
return 0;
}

static struct seq_operations tracing_likely_seq_ops = {
.start = t_start,
.next = t_next,
.stop = t_stop,
.show = t_show,
};

static int tracing_likely_open(struct inode *inode, struct file *file)
{
int ret;

ret = seq_open(file, &tracing_likely_seq_ops);
if (!ret) {
struct seq_file *m = file->private_data;
m->private = (void *)inode->i_private;
}

return ret;
}

static struct file_operations tracing_likely_fops = {
.open = tracing_likely_open,
.read = seq_read,
.llseek = seq_lseek,
};

extern unsigned long __start_likely_profile[];
extern unsigned long __stop_likely_profile[];
extern unsigned long __start_unlikely_profile[];
extern unsigned long __stop_unlikely_profile[];

static struct ftrace_pointer ftrace_likely_pos = {
.start = __start_likely_profile,
.stop = __stop_likely_profile,
};

static struct ftrace_pointer ftrace_unlikely_pos = {
.start = __start_unlikely_profile,
.stop = __stop_unlikely_profile,
};

static __init int ftrace_unlikely_init(void)
{
struct dentry *d_tracer;
struct dentry *entry;

d_tracer = tracing_init_dentry();

entry = debugfs_create_file("profile_likely", 0444, d_tracer,
&ftrace_likely_pos,
&tracing_likely_fops);
if (!entry)
pr_warning("Could not create debugfs 'profile_likely' entry\n");

entry = debugfs_create_file("profile_unlikely", 0444, d_tracer,
&ftrace_unlikely_pos,
&tracing_likely_fops);
if (!entry)
pr_warning("Could not create debugfs"
" 'profile_unlikely' entry\n");

return 0;
}

device_initcall(ftrace_unlikely_init);

0 comments on commit 1d11b3a

Please sign in to comment.