提交 621a01ea 编写于 作者: I Ingo Molnar

perf counters: hw driver API

Impact: restructure code, introduce hw_ops driver abstraction

Introduce this abstraction to handle counter details:

 struct hw_perf_counter_ops {
	void (*hw_perf_counter_enable)	(struct perf_counter *counter);
	void (*hw_perf_counter_disable)	(struct perf_counter *counter);
	void (*hw_perf_counter_read)	(struct perf_counter *counter);
 };

This will be useful to support assymetric hw details, and it will also
be useful to implement "software counters". (Counters that count kernel
managed sw events such as pagefaults, context-switches, wall-clock time
or task-local time.)
Signed-off-by: NIngo Molnar <mingo@elte.hu>
上级 ccff286d
...@@ -56,7 +56,7 @@ const int max_intel_perfmon_events = ARRAY_SIZE(intel_perfmon_event_map); ...@@ -56,7 +56,7 @@ const int max_intel_perfmon_events = ARRAY_SIZE(intel_perfmon_event_map);
/* /*
* Setup the hardware configuration for a given hw_event_type * Setup the hardware configuration for a given hw_event_type
*/ */
int hw_perf_counter_init(struct perf_counter *counter) static int __hw_perf_counter_init(struct perf_counter *counter)
{ {
struct perf_counter_hw_event *hw_event = &counter->hw_event; struct perf_counter_hw_event *hw_event = &counter->hw_event;
struct hw_perf_counter *hwc = &counter->hw; struct hw_perf_counter *hwc = &counter->hw;
...@@ -135,7 +135,7 @@ u64 hw_perf_disable_all(void) ...@@ -135,7 +135,7 @@ u64 hw_perf_disable_all(void)
EXPORT_SYMBOL_GPL(hw_perf_disable_all); EXPORT_SYMBOL_GPL(hw_perf_disable_all);
static inline void static inline void
__hw_perf_counter_disable(struct hw_perf_counter *hwc, unsigned int idx) __x86_perf_counter_disable(struct hw_perf_counter *hwc, unsigned int idx)
{ {
wrmsr(hwc->config_base + idx, hwc->config, 0); wrmsr(hwc->config_base + idx, hwc->config, 0);
} }
...@@ -149,13 +149,13 @@ static void __hw_perf_counter_set_period(struct hw_perf_counter *hwc, int idx) ...@@ -149,13 +149,13 @@ static void __hw_perf_counter_set_period(struct hw_perf_counter *hwc, int idx)
wrmsr(hwc->counter_base + idx, hwc->next_count, 0); wrmsr(hwc->counter_base + idx, hwc->next_count, 0);
} }
static void __hw_perf_counter_enable(struct hw_perf_counter *hwc, int idx) static void __x86_perf_counter_enable(struct hw_perf_counter *hwc, int idx)
{ {
wrmsr(hwc->config_base + idx, wrmsr(hwc->config_base + idx,
hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE, 0); hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE, 0);
} }
void hw_perf_counter_enable(struct perf_counter *counter) static void x86_perf_counter_enable(struct perf_counter *counter)
{ {
struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
struct hw_perf_counter *hwc = &counter->hw; struct hw_perf_counter *hwc = &counter->hw;
...@@ -170,12 +170,12 @@ void hw_perf_counter_enable(struct perf_counter *counter) ...@@ -170,12 +170,12 @@ void hw_perf_counter_enable(struct perf_counter *counter)
perf_counters_lapic_init(hwc->nmi); perf_counters_lapic_init(hwc->nmi);
__hw_perf_counter_disable(hwc, idx); __x86_perf_counter_disable(hwc, idx);
cpuc->counters[idx] = counter; cpuc->counters[idx] = counter;
__hw_perf_counter_set_period(hwc, idx); __hw_perf_counter_set_period(hwc, idx);
__hw_perf_counter_enable(hwc, idx); __x86_perf_counter_enable(hwc, idx);
} }
#ifdef CONFIG_X86_64 #ifdef CONFIG_X86_64
...@@ -282,20 +282,20 @@ void perf_counter_print_debug(void) ...@@ -282,20 +282,20 @@ void perf_counter_print_debug(void)
local_irq_enable(); local_irq_enable();
} }
void hw_perf_counter_disable(struct perf_counter *counter) static void x86_perf_counter_disable(struct perf_counter *counter)
{ {
struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
struct hw_perf_counter *hwc = &counter->hw; struct hw_perf_counter *hwc = &counter->hw;
unsigned int idx = hwc->idx; unsigned int idx = hwc->idx;
__hw_perf_counter_disable(hwc, idx); __x86_perf_counter_disable(hwc, idx);
clear_bit(idx, cpuc->used); clear_bit(idx, cpuc->used);
cpuc->counters[idx] = NULL; cpuc->counters[idx] = NULL;
__hw_perf_save_counter(counter, hwc, idx); __hw_perf_save_counter(counter, hwc, idx);
} }
void hw_perf_counter_read(struct perf_counter *counter) static void x86_perf_counter_read(struct perf_counter *counter)
{ {
struct hw_perf_counter *hwc = &counter->hw; struct hw_perf_counter *hwc = &counter->hw;
unsigned long addr = hwc->counter_base + hwc->idx; unsigned long addr = hwc->counter_base + hwc->idx;
...@@ -342,7 +342,7 @@ static void perf_save_and_restart(struct perf_counter *counter) ...@@ -342,7 +342,7 @@ static void perf_save_and_restart(struct perf_counter *counter)
__hw_perf_counter_set_period(hwc, idx); __hw_perf_counter_set_period(hwc, idx);
if (pmc_ctrl & ARCH_PERFMON_EVENTSEL0_ENABLE) if (pmc_ctrl & ARCH_PERFMON_EVENTSEL0_ENABLE)
__hw_perf_counter_enable(hwc, idx); __x86_perf_counter_enable(hwc, idx);
} }
static void static void
...@@ -572,3 +572,20 @@ void __init init_hw_perf_counters(void) ...@@ -572,3 +572,20 @@ void __init init_hw_perf_counters(void)
perf_counters_initialized = true; perf_counters_initialized = true;
} }
static struct hw_perf_counter_ops x86_perf_counter_ops = {
.hw_perf_counter_enable = x86_perf_counter_enable,
.hw_perf_counter_disable = x86_perf_counter_disable,
.hw_perf_counter_read = x86_perf_counter_read,
};
struct hw_perf_counter_ops *hw_perf_counter_init(struct perf_counter *counter)
{
int err;
err = __hw_perf_counter_init(counter);
if (err)
return NULL;
return &x86_perf_counter_ops;
}
...@@ -113,6 +113,17 @@ struct perf_data { ...@@ -113,6 +113,17 @@ struct perf_data {
u8 data[PERF_DATA_BUFLEN]; u8 data[PERF_DATA_BUFLEN];
}; };
struct perf_counter;
/**
* struct hw_perf_counter_ops - performance counter hw ops
*/
struct hw_perf_counter_ops {
void (*hw_perf_counter_enable) (struct perf_counter *counter);
void (*hw_perf_counter_disable) (struct perf_counter *counter);
void (*hw_perf_counter_read) (struct perf_counter *counter);
};
/** /**
* struct perf_counter - performance counter kernel representation: * struct perf_counter - performance counter kernel representation:
*/ */
...@@ -120,6 +131,7 @@ struct perf_counter { ...@@ -120,6 +131,7 @@ struct perf_counter {
struct list_head list_entry; struct list_head list_entry;
struct list_head sibling_list; struct list_head sibling_list;
struct perf_counter *group_leader; struct perf_counter *group_leader;
struct hw_perf_counter_ops *hw_ops;
int active; int active;
#if BITS_PER_LONG == 64 #if BITS_PER_LONG == 64
...@@ -185,6 +197,9 @@ struct perf_cpu_context { ...@@ -185,6 +197,9 @@ struct perf_cpu_context {
extern int perf_max_counters; extern int perf_max_counters;
#ifdef CONFIG_PERF_COUNTERS #ifdef CONFIG_PERF_COUNTERS
extern struct hw_perf_counter_ops *
hw_perf_counter_init(struct perf_counter *counter);
extern void perf_counter_task_sched_in(struct task_struct *task, int cpu); extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
extern void perf_counter_task_sched_out(struct task_struct *task, int cpu); extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
extern void perf_counter_task_tick(struct task_struct *task, int cpu); extern void perf_counter_task_tick(struct task_struct *task, int cpu);
......
...@@ -37,18 +37,15 @@ static DEFINE_MUTEX(perf_resource_mutex); ...@@ -37,18 +37,15 @@ static DEFINE_MUTEX(perf_resource_mutex);
/* /*
* Architecture provided APIs - weak aliases: * Architecture provided APIs - weak aliases:
*/ */
extern __weak struct hw_perf_counter_ops *
int __weak hw_perf_counter_init(struct perf_counter *counter) hw_perf_counter_init(struct perf_counter *counter)
{ {
return -EINVAL; return ERR_PTR(-EINVAL);
} }
void __weak hw_perf_counter_enable(struct perf_counter *counter) { } void __weak hw_perf_disable_all(void) { }
void __weak hw_perf_counter_disable(struct perf_counter *counter) { } void __weak hw_perf_enable_all(void) { }
void __weak hw_perf_counter_read(struct perf_counter *counter) { } void __weak hw_perf_counter_setup(void) { }
void __weak hw_perf_disable_all(void) { }
void __weak hw_perf_enable_all(void) { }
void __weak hw_perf_counter_setup(void) { }
#if BITS_PER_LONG == 64 #if BITS_PER_LONG == 64
...@@ -146,7 +143,7 @@ static void __perf_counter_remove_from_context(void *info) ...@@ -146,7 +143,7 @@ static void __perf_counter_remove_from_context(void *info)
spin_lock(&ctx->lock); spin_lock(&ctx->lock);
if (counter->active) { if (counter->active) {
hw_perf_counter_disable(counter); counter->hw_ops->hw_perf_counter_disable(counter);
counter->active = 0; counter->active = 0;
ctx->nr_active--; ctx->nr_active--;
cpuctx->active_oncpu--; cpuctx->active_oncpu--;
...@@ -257,7 +254,7 @@ static void __perf_install_in_context(void *info) ...@@ -257,7 +254,7 @@ static void __perf_install_in_context(void *info)
ctx->nr_counters++; ctx->nr_counters++;
if (cpuctx->active_oncpu < perf_max_counters) { if (cpuctx->active_oncpu < perf_max_counters) {
hw_perf_counter_enable(counter); counter->hw_ops->hw_perf_counter_enable(counter);
counter->active = 1; counter->active = 1;
counter->oncpu = cpu; counter->oncpu = cpu;
ctx->nr_active++; ctx->nr_active++;
...@@ -333,7 +330,7 @@ counter_sched_out(struct perf_counter *counter, ...@@ -333,7 +330,7 @@ counter_sched_out(struct perf_counter *counter,
if (!counter->active) if (!counter->active)
return; return;
hw_perf_counter_disable(counter); counter->hw_ops->hw_perf_counter_disable(counter);
counter->active = 0; counter->active = 0;
counter->oncpu = -1; counter->oncpu = -1;
...@@ -392,7 +389,7 @@ counter_sched_in(struct perf_counter *counter, ...@@ -392,7 +389,7 @@ counter_sched_in(struct perf_counter *counter,
struct perf_counter_context *ctx, struct perf_counter_context *ctx,
int cpu) int cpu)
{ {
hw_perf_counter_enable(counter); counter->hw_ops->hw_perf_counter_enable(counter);
counter->active = 1; counter->active = 1;
counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */ counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
...@@ -509,7 +506,9 @@ void perf_counter_init_task(struct task_struct *task) ...@@ -509,7 +506,9 @@ void perf_counter_init_task(struct task_struct *task)
*/ */
static void __hw_perf_counter_read(void *info) static void __hw_perf_counter_read(void *info)
{ {
hw_perf_counter_read(info); struct perf_counter *counter = info;
counter->hw_ops->hw_perf_counter_read(counter);
} }
static u64 perf_counter_read(struct perf_counter *counter) static u64 perf_counter_read(struct perf_counter *counter)
...@@ -816,8 +815,10 @@ perf_counter_alloc(struct perf_counter_hw_event *hw_event, ...@@ -816,8 +815,10 @@ perf_counter_alloc(struct perf_counter_hw_event *hw_event,
int cpu, int cpu,
struct perf_counter *group_leader) struct perf_counter *group_leader)
{ {
struct perf_counter *counter = kzalloc(sizeof(*counter), GFP_KERNEL); struct hw_perf_counter_ops *hw_ops;
struct perf_counter *counter;
counter = kzalloc(sizeof(*counter), GFP_KERNEL);
if (!counter) if (!counter)
return NULL; return NULL;
...@@ -839,6 +840,14 @@ perf_counter_alloc(struct perf_counter_hw_event *hw_event, ...@@ -839,6 +840,14 @@ perf_counter_alloc(struct perf_counter_hw_event *hw_event,
counter->hw_event = *hw_event; counter->hw_event = *hw_event;
counter->wakeup_pending = 0; counter->wakeup_pending = 0;
counter->group_leader = group_leader; counter->group_leader = group_leader;
counter->hw_ops = NULL;
hw_ops = hw_perf_counter_init(counter);
if (!hw_ops) {
kfree(counter);
return NULL;
}
counter->hw_ops = hw_ops;
return counter; return counter;
} }
...@@ -908,10 +917,6 @@ asmlinkage int sys_perf_counter_open( ...@@ -908,10 +917,6 @@ asmlinkage int sys_perf_counter_open(
if (!counter) if (!counter)
goto err_put_context; goto err_put_context;
ret = hw_perf_counter_init(counter);
if (ret)
goto err_free_put_context;
perf_install_in_context(ctx, counter, cpu); perf_install_in_context(ctx, counter, cpu);
ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0); ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
...@@ -927,8 +932,6 @@ asmlinkage int sys_perf_counter_open( ...@@ -927,8 +932,6 @@ asmlinkage int sys_perf_counter_open(
mutex_lock(&counter->mutex); mutex_lock(&counter->mutex);
perf_counter_remove_from_context(counter); perf_counter_remove_from_context(counter);
mutex_unlock(&counter->mutex); mutex_unlock(&counter->mutex);
err_free_put_context:
kfree(counter); kfree(counter);
err_put_context: err_put_context:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册