static int perf_overcommit __read_mostly = 1;
static atomic_t nr_counters __read_mostly;
-static atomic_t nr_mmap_tracking __read_mostly;
-static atomic_t nr_munmap_tracking __read_mostly;
-static atomic_t nr_comm_tracking __read_mostly;
+static atomic_t nr_mmap_counters __read_mostly;
+static atomic_t nr_munmap_counters __read_mostly;
+static atomic_t nr_comm_counters __read_mostly;
int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
+static atomic64_t perf_counter_id;
+
/*
* Lock for (sysadmin-configurable) counter reservations:
*/
if (!is_software_counter(counter))
cpuctx->active_oncpu--;
ctx->nr_active--;
- if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
+ if (counter->attr.exclusive || !cpuctx->active_oncpu)
cpuctx->exclusive = 0;
}
list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
counter_sched_out(counter, cpuctx, ctx);
- if (group_counter->hw_event.exclusive)
+ if (group_counter->attr.exclusive)
cpuctx->exclusive = 0;
}
cpuctx->active_oncpu++;
ctx->nr_active++;
- if (counter->hw_event.exclusive)
+ if (counter->attr.exclusive)
cpuctx->exclusive = 1;
return 0;
if (ret)
return ret < 0 ? ret : 0;
- group_counter->prev_state = group_counter->state;
if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
return -EAGAIN;
* Schedule in siblings as one group (if any):
*/
list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
- counter->prev_state = counter->state;
if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
partial_group = counter;
goto group_error;
* If this group is exclusive and there are already
* counters on the CPU, it can't go on.
*/
- if (counter->hw_event.exclusive && cpuctx->active_oncpu)
+ if (counter->attr.exclusive && cpuctx->active_oncpu)
return 0;
/*
* Otherwise, try to add it if all previous groups were able
struct perf_counter_context *ctx)
{
list_add_counter(counter, ctx);
- counter->prev_state = PERF_COUNTER_STATE_OFF;
counter->tstamp_enabled = ctx->time;
counter->tstamp_running = ctx->time;
counter->tstamp_stopped = ctx->time;
*/
if (leader != counter)
group_sched_out(leader, cpuctx, ctx);
- if (leader->hw_event.pinned) {
+ if (leader->attr.pinned) {
update_group_times(leader);
leader->state = PERF_COUNTER_STATE_ERROR;
}
ctx->is_active = 1;
update_context_time(ctx);
- counter->prev_state = counter->state;
if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
goto unlock;
counter->state = PERF_COUNTER_STATE_INACTIVE;
*/
if (leader != counter)
group_sched_out(leader, cpuctx, ctx);
- if (leader->hw_event.pinned) {
+ if (leader->attr.pinned) {
update_group_times(leader);
leader->state = PERF_COUNTER_STATE_ERROR;
}
/*
* not supported on inherited counters
*/
- if (counter->hw_event.inherit)
+ if (counter->attr.inherit)
return -EINVAL;
atomic_add(refresh, &counter->event_limit);
*/
list_for_each_entry(counter, &ctx->counter_list, list_entry) {
if (counter->state <= PERF_COUNTER_STATE_OFF ||
- !counter->hw_event.pinned)
+ !counter->attr.pinned)
continue;
if (counter->cpu != -1 && counter->cpu != cpu)
continue;
* ignore pinned counters since we did them already.
*/
if (counter->state <= PERF_COUNTER_STATE_OFF ||
- counter->hw_event.pinned)
+ counter->attr.pinned)
continue;
/*
static void perf_adjust_freq(struct perf_counter_context *ctx)
{
struct perf_counter *counter;
- u64 interrupts, irq_period;
+ u64 interrupts, sample_period;
u64 events, period;
s64 delta;
interrupts = 2*sysctl_perf_counter_limit/HZ;
}
- if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
+ if (!counter->attr.freq || !counter->attr.sample_freq)
continue;
- events = HZ * interrupts * counter->hw.irq_period;
- period = div64_u64(events, counter->hw_event.irq_freq);
+ events = HZ * interrupts * counter->hw.sample_period;
+ period = div64_u64(events, counter->attr.sample_freq);
- delta = (s64)(1 + period - counter->hw.irq_period);
+ delta = (s64)(1 + period - counter->hw.sample_period);
delta >>= 1;
- irq_period = counter->hw.irq_period + delta;
+ sample_period = counter->hw.sample_period + delta;
- if (!irq_period)
- irq_period = 1;
+ if (!sample_period)
+ sample_period = 1;
- perf_log_period(counter, irq_period);
+ perf_log_period(counter, sample_period);
- counter->hw.irq_period = irq_period;
+ counter->hw.sample_period = sample_period;
}
spin_unlock(&ctx->lock);
}
struct perf_counter *counter;
counter = container_of(head, struct perf_counter, rcu_head);
+ if (counter->ns)
+ put_pid_ns(counter->ns);
kfree(counter);
}
perf_pending_sync(counter);
atomic_dec(&nr_counters);
- if (counter->hw_event.mmap)
- atomic_dec(&nr_mmap_tracking);
- if (counter->hw_event.munmap)
- atomic_dec(&nr_munmap_tracking);
- if (counter->hw_event.comm)
- atomic_dec(&nr_comm_tracking);
+ if (counter->attr.mmap)
+ atomic_dec(&nr_mmap_counters);
+ if (counter->attr.munmap)
+ atomic_dec(&nr_munmap_counters);
+ if (counter->attr.comm)
+ atomic_dec(&nr_comm_counters);
if (counter->destroy)
counter->destroy(counter);
mutex_lock(&counter->child_mutex);
values[0] = perf_counter_read(counter);
n = 1;
- if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
+ if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
values[n++] = counter->total_time_enabled +
atomic64_read(&counter->child_total_time_enabled);
- if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
+ if (counter->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
values[n++] = counter->total_time_running +
atomic64_read(&counter->child_total_time_running);
+ if (counter->attr.read_format & PERF_FORMAT_ID)
+ values[n++] = counter->id;
mutex_unlock(&counter->child_mutex);
if (count < n * sizeof(u64))
mutex_unlock(&counter->child_mutex);
}
+static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
+{
+ struct perf_counter_context *ctx = counter->ctx;
+ unsigned long size;
+ int ret = 0;
+ u64 value;
+
+ if (!counter->attr.sample_period)
+ return -EINVAL;
+
+ size = copy_from_user(&value, arg, sizeof(value));
+ if (size != sizeof(value))
+ return -EFAULT;
+
+ if (!value)
+ return -EINVAL;
+
+ spin_lock_irq(&ctx->lock);
+ if (counter->attr.freq) {
+ if (value > sysctl_perf_counter_limit) {
+ ret = -EINVAL;
+ goto unlock;
+ }
+
+ counter->attr.sample_freq = value;
+ } else {
+ counter->attr.sample_period = value;
+ counter->hw.sample_period = value;
+
+ perf_log_period(counter, value);
+ }
+unlock:
+ spin_unlock_irq(&ctx->lock);
+
+ return ret;
+}
+
static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct perf_counter *counter = file->private_data;
case PERF_COUNTER_IOC_REFRESH:
return perf_counter_refresh(counter, arg);
+
+ case PERF_COUNTER_IOC_PERIOD:
+ return perf_counter_period(counter, (u64 __user *)arg);
+
default:
return -ENOTTY;
}
struct perf_output_handle {
struct perf_counter *counter;
struct perf_mmap_data *data;
- unsigned int offset;
- unsigned int head;
+ unsigned long head;
+ unsigned long offset;
int nmi;
int overflow;
int locked;
static void perf_output_unlock(struct perf_output_handle *handle)
{
struct perf_mmap_data *data = handle->data;
- int head, cpu;
+ unsigned long head;
+ int cpu;
data->done_head = data->head;
* before we publish the new head, matched by a rmb() in userspace when
* reading this position.
*/
- while ((head = atomic_xchg(&data->done_head, 0)))
+ while ((head = atomic_long_xchg(&data->done_head, 0)))
data->user_page->data_head = head;
/*
/*
* Therefore we have to validate we did not indeed do so.
*/
- if (unlikely(atomic_read(&data->done_head))) {
+ if (unlikely(atomic_long_read(&data->done_head))) {
/*
* Since we had it locked, we can lock it again.
*/
do {
offset = head = atomic_read(&data->head);
head += size;
- } while (atomic_cmpxchg(&data->head, offset, head) != offset);
+ } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
handle->offset = offset;
handle->head = head;
* Check we didn't copy past our reservation window, taking the
* possible unsigned int wrap into account.
*/
- WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
+ WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
}
#define perf_output_put(handle, x) \
struct perf_counter *counter = handle->counter;
struct perf_mmap_data *data = handle->data;
- int wakeup_events = counter->hw_event.wakeup_events;
+ int wakeup_events = counter->attr.wakeup_events;
if (handle->overflow && wakeup_events) {
int events = atomic_inc_return(&data->events);
rcu_read_unlock();
}
+static u32 perf_counter_pid(struct perf_counter *counter, struct task_struct *p)
+{
+ /*
+ * only top level counters have the pid namespace they were created in
+ */
+ if (counter->parent)
+ counter = counter->parent;
+
+ return task_tgid_nr_ns(p, counter->ns);
+}
+
+static u32 perf_counter_tid(struct perf_counter *counter, struct task_struct *p)
+{
+ /*
+ * only top level counters have the pid namespace they were created in
+ */
+ if (counter->parent)
+ counter = counter->parent;
+
+ return task_pid_nr_ns(p, counter->ns);
+}
+
static void perf_counter_output(struct perf_counter *counter,
int nmi, struct pt_regs *regs, u64 addr)
{
int ret;
- u64 record_type = counter->hw_event.record_type;
+ u64 sample_type = counter->attr.sample_type;
struct perf_output_handle handle;
struct perf_event_header header;
u64 ip;
u32 pid, tid;
} tid_entry;
struct {
- u64 event;
+ u64 id;
u64 counter;
} group_entry;
struct perf_callchain_entry *callchain = NULL;
header.misc = PERF_EVENT_MISC_OVERFLOW;
header.misc |= perf_misc_flags(regs);
- if (record_type & PERF_RECORD_IP) {
+ if (sample_type & PERF_SAMPLE_IP) {
ip = perf_instruction_pointer(regs);
- header.type |= PERF_RECORD_IP;
+ header.type |= PERF_SAMPLE_IP;
header.size += sizeof(ip);
}
- if (record_type & PERF_RECORD_TID) {
+ if (sample_type & PERF_SAMPLE_TID) {
/* namespace issues */
- tid_entry.pid = current->group_leader->pid;
- tid_entry.tid = current->pid;
+ tid_entry.pid = perf_counter_pid(counter, current);
+ tid_entry.tid = perf_counter_tid(counter, current);
- header.type |= PERF_RECORD_TID;
+ header.type |= PERF_SAMPLE_TID;
header.size += sizeof(tid_entry);
}
- if (record_type & PERF_RECORD_TIME) {
+ if (sample_type & PERF_SAMPLE_TIME) {
/*
* Maybe do better on x86 and provide cpu_clock_nmi()
*/
time = sched_clock();
- header.type |= PERF_RECORD_TIME;
+ header.type |= PERF_SAMPLE_TIME;
header.size += sizeof(u64);
}
- if (record_type & PERF_RECORD_ADDR) {
- header.type |= PERF_RECORD_ADDR;
+ if (sample_type & PERF_SAMPLE_ADDR) {
+ header.type |= PERF_SAMPLE_ADDR;
header.size += sizeof(u64);
}
- if (record_type & PERF_RECORD_CONFIG) {
- header.type |= PERF_RECORD_CONFIG;
+ if (sample_type & PERF_SAMPLE_CONFIG) {
+ header.type |= PERF_SAMPLE_CONFIG;
header.size += sizeof(u64);
}
- if (record_type & PERF_RECORD_CPU) {
- header.type |= PERF_RECORD_CPU;
+ if (sample_type & PERF_SAMPLE_CPU) {
+ header.type |= PERF_SAMPLE_CPU;
header.size += sizeof(cpu_entry);
cpu_entry.cpu = raw_smp_processor_id();
}
- if (record_type & PERF_RECORD_GROUP) {
- header.type |= PERF_RECORD_GROUP;
+ if (sample_type & PERF_SAMPLE_GROUP) {
+ header.type |= PERF_SAMPLE_GROUP;
header.size += sizeof(u64) +
counter->nr_siblings * sizeof(group_entry);
}
- if (record_type & PERF_RECORD_CALLCHAIN) {
+ if (sample_type & PERF_SAMPLE_CALLCHAIN) {
callchain = perf_callchain(regs);
if (callchain) {
callchain_size = (1 + callchain->nr) * sizeof(u64);
- header.type |= PERF_RECORD_CALLCHAIN;
+ header.type |= PERF_SAMPLE_CALLCHAIN;
header.size += callchain_size;
}
}
perf_output_put(&handle, header);
- if (record_type & PERF_RECORD_IP)
+ if (sample_type & PERF_SAMPLE_IP)
perf_output_put(&handle, ip);
- if (record_type & PERF_RECORD_TID)
+ if (sample_type & PERF_SAMPLE_TID)
perf_output_put(&handle, tid_entry);
- if (record_type & PERF_RECORD_TIME)
+ if (sample_type & PERF_SAMPLE_TIME)
perf_output_put(&handle, time);
- if (record_type & PERF_RECORD_ADDR)
+ if (sample_type & PERF_SAMPLE_ADDR)
perf_output_put(&handle, addr);
- if (record_type & PERF_RECORD_CONFIG)
- perf_output_put(&handle, counter->hw_event.config);
+ if (sample_type & PERF_SAMPLE_CONFIG)
+ perf_output_put(&handle, counter->attr.config);
- if (record_type & PERF_RECORD_CPU)
+ if (sample_type & PERF_SAMPLE_CPU)
perf_output_put(&handle, cpu_entry);
/*
- * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
+ * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
*/
- if (record_type & PERF_RECORD_GROUP) {
+ if (sample_type & PERF_SAMPLE_GROUP) {
struct perf_counter *leader, *sub;
u64 nr = counter->nr_siblings;
if (sub != counter)
sub->pmu->read(sub);
- group_entry.event = sub->hw_event.config;
+ group_entry.id = sub->id;
group_entry.counter = atomic64_read(&sub->count);
perf_output_put(&handle, group_entry);
perf_output_end(&handle);
}
+/*
+ * fork tracking
+ */
+
+struct perf_fork_event {
+ struct task_struct *task;
+
+ struct {
+ struct perf_event_header header;
+
+ u32 pid;
+ u32 ppid;
+ } event;
+};
+
+static void perf_counter_fork_output(struct perf_counter *counter,
+ struct perf_fork_event *fork_event)
+{
+ struct perf_output_handle handle;
+ int size = fork_event->event.header.size;
+ struct task_struct *task = fork_event->task;
+ int ret = perf_output_begin(&handle, counter, size, 0, 0);
+
+ if (ret)
+ return;
+
+ fork_event->event.pid = perf_counter_pid(counter, task);
+ fork_event->event.ppid = perf_counter_pid(counter, task->real_parent);
+
+ perf_output_put(&handle, fork_event->event);
+ perf_output_end(&handle);
+}
+
+static int perf_counter_fork_match(struct perf_counter *counter)
+{
+ if (counter->attr.comm || counter->attr.mmap || counter->attr.munmap)
+ return 1;
+
+ return 0;
+}
+
+static void perf_counter_fork_ctx(struct perf_counter_context *ctx,
+ struct perf_fork_event *fork_event)
+{
+ struct perf_counter *counter;
+
+ if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
+ return;
+
+ rcu_read_lock();
+ list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
+ if (perf_counter_fork_match(counter))
+ perf_counter_fork_output(counter, fork_event);
+ }
+ rcu_read_unlock();
+}
+
+static void perf_counter_fork_event(struct perf_fork_event *fork_event)
+{
+ struct perf_cpu_context *cpuctx;
+ struct perf_counter_context *ctx;
+
+ cpuctx = &get_cpu_var(perf_cpu_context);
+ perf_counter_fork_ctx(&cpuctx->ctx, fork_event);
+ put_cpu_var(perf_cpu_context);
+
+ rcu_read_lock();
+ /*
+ * doesn't really matter which of the child contexts the
+ * events ends up in.
+ */
+ ctx = rcu_dereference(current->perf_counter_ctxp);
+ if (ctx)
+ perf_counter_fork_ctx(ctx, fork_event);
+ rcu_read_unlock();
+}
+
+void perf_counter_fork(struct task_struct *task)
+{
+ struct perf_fork_event fork_event;
+
+ if (!atomic_read(&nr_comm_counters) &&
+ !atomic_read(&nr_mmap_counters) &&
+ !atomic_read(&nr_munmap_counters))
+ return;
+
+ fork_event = (struct perf_fork_event){
+ .task = task,
+ .event = {
+ .header = {
+ .type = PERF_EVENT_FORK,
+ .size = sizeof(fork_event.event),
+ },
+ },
+ };
+
+ perf_counter_fork_event(&fork_event);
+}
+
/*
* comm tracking
*/
if (ret)
return;
+ comm_event->event.pid = perf_counter_pid(counter, comm_event->task);
+ comm_event->event.tid = perf_counter_tid(counter, comm_event->task);
+
perf_output_put(&handle, comm_event->event);
perf_output_copy(&handle, comm_event->comm,
comm_event->comm_size);
perf_output_end(&handle);
}
-static int perf_counter_comm_match(struct perf_counter *counter,
- struct perf_comm_event *comm_event)
+static int perf_counter_comm_match(struct perf_counter *counter)
{
- if (counter->hw_event.comm &&
- comm_event->event.header.type == PERF_EVENT_COMM)
+ if (counter->attr.comm)
return 1;
return 0;
rcu_read_lock();
list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
- if (perf_counter_comm_match(counter, comm_event))
+ if (perf_counter_comm_match(counter))
perf_counter_comm_output(counter, comm_event);
}
rcu_read_unlock();
{
struct perf_comm_event comm_event;
- if (!atomic_read(&nr_comm_tracking))
+ if (!atomic_read(&nr_comm_counters))
return;
comm_event = (struct perf_comm_event){
.task = task,
.event = {
.header = { .type = PERF_EVENT_COMM, },
- .pid = task->group_leader->pid,
- .tid = task->pid,
},
};
if (ret)
return;
+ mmap_event->event.pid = perf_counter_pid(counter, current);
+ mmap_event->event.tid = perf_counter_tid(counter, current);
+
perf_output_put(&handle, mmap_event->event);
perf_output_copy(&handle, mmap_event->file_name,
mmap_event->file_size);
static int perf_counter_mmap_match(struct perf_counter *counter,
struct perf_mmap_event *mmap_event)
{
- if (counter->hw_event.mmap &&
+ if (counter->attr.mmap &&
mmap_event->event.header.type == PERF_EVENT_MMAP)
return 1;
- if (counter->hw_event.munmap &&
+ if (counter->attr.munmap &&
mmap_event->event.header.type == PERF_EVENT_MUNMAP)
return 1;
{
struct perf_mmap_event mmap_event;
- if (!atomic_read(&nr_mmap_tracking))
+ if (!atomic_read(&nr_mmap_counters))
return;
mmap_event = (struct perf_mmap_event){
.file = file,
.event = {
.header = { .type = PERF_EVENT_MMAP, },
- .pid = current->group_leader->pid,
- .tid = current->pid,
.start = addr,
.len = len,
.pgoff = pgoff,
{
struct perf_mmap_event mmap_event;
- if (!atomic_read(&nr_munmap_tracking))
+ if (!atomic_read(&nr_munmap_counters))
return;
mmap_event = (struct perf_mmap_event){
.file = file,
.event = {
.header = { .type = PERF_EVENT_MUNMAP, },
- .pid = current->group_leader->pid,
- .tid = current->pid,
.start = addr,
.len = len,
.pgoff = pgoff,
}
/*
- * Log irq_period changes so that analyzing tools can re-normalize the
+ * Log sample_period changes so that analyzing tools can re-normalize the
* event flow.
*/
.period = period,
};
- if (counter->hw.irq_period == period)
+ if (counter->hw.sample_period == period)
return;
ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
if (!throttle) {
counter->hw.interrupts++;
- } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
- counter->hw.interrupts++;
- if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
- counter->hw.interrupts = MAX_INTERRUPTS;
- perf_log_throttle(counter, 0);
+ } else {
+ if (counter->hw.interrupts != MAX_INTERRUPTS) {
+ counter->hw.interrupts++;
+ if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
+ counter->hw.interrupts = MAX_INTERRUPTS;
+ perf_log_throttle(counter, 0);
+ ret = 1;
+ }
+ } else {
+ /*
+ * Keep re-disabling counters even though on the previous
+ * pass we disabled it - just in case we raced with a
+ * sched-in and the counter got enabled again:
+ */
ret = 1;
}
}
{
struct hw_perf_counter *hwc = &counter->hw;
s64 left = atomic64_read(&hwc->period_left);
- s64 period = hwc->irq_period;
+ s64 period = hwc->sample_period;
if (unlikely(left <= -period)) {
left = period;
* In case we exclude kernel IPs or are somehow not in interrupt
* context, provide the next best thing, the user IP.
*/
- if ((counter->hw_event.exclude_kernel || !regs) &&
- !counter->hw_event.exclude_user)
+ if ((counter->attr.exclude_kernel || !regs) &&
+ !counter->attr.exclude_user)
regs = task_pt_regs(current);
if (regs) {
ret = HRTIMER_NORESTART;
}
- period = max_t(u64, 10000, counter->hw.irq_period);
+ period = max_t(u64, 10000, counter->hw.sample_period);
hrtimer_forward_now(hrtimer, ns_to_ktime(period));
return ret;
if (!perf_swcounter_is_counting(counter))
return 0;
- if (counter->hw_event.config != event_config)
+ if (counter->attr.config != event_config)
return 0;
- if (counter->hw_event.exclude_user && user_mode(regs))
- return 0;
+ if (regs) {
+ if (counter->attr.exclude_user && user_mode(regs))
+ return 0;
- if (counter->hw_event.exclude_kernel && !user_mode(regs))
- return 0;
+ if (counter->attr.exclude_kernel && !user_mode(regs))
+ return 0;
+ }
return 1;
}
{
int neg = atomic64_add_negative(nr, &counter->hw.count);
- if (counter->hw.irq_period && !neg)
+ if (counter->hw.sample_period && !neg && regs)
perf_swcounter_overflow(counter, nmi, regs, addr);
}
atomic64_set(&hwc->prev_count, cpu_clock(cpu));
hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
hwc->hrtimer.function = perf_swcounter_hrtimer;
- if (hwc->irq_period) {
- u64 period = max_t(u64, 10000, hwc->irq_period);
+ if (hwc->sample_period) {
+ u64 period = max_t(u64, 10000, hwc->sample_period);
__hrtimer_start_range_ns(&hwc->hrtimer,
ns_to_ktime(period), 0,
HRTIMER_MODE_REL, 0);
static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
{
- if (counter->hw.irq_period)
+ if (counter->hw.sample_period)
hrtimer_cancel(&counter->hw.hrtimer);
cpu_clock_perf_counter_update(counter);
}
atomic64_set(&hwc->prev_count, now);
hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
hwc->hrtimer.function = perf_swcounter_hrtimer;
- if (hwc->irq_period) {
- u64 period = max_t(u64, 10000, hwc->irq_period);
+ if (hwc->sample_period) {
+ u64 period = max_t(u64, 10000, hwc->sample_period);
__hrtimer_start_range_ns(&hwc->hrtimer,
ns_to_ktime(period), 0,
HRTIMER_MODE_REL, 0);
static void task_clock_perf_counter_disable(struct perf_counter *counter)
{
- if (counter->hw.irq_period)
+ if (counter->hw.sample_period)
hrtimer_cancel(&counter->hw.hrtimer);
task_clock_perf_counter_update(counter, counter->ctx->time);
/*
* Software counter: cpu migrations
*/
-
-static inline u64 get_cpu_migrations(struct perf_counter *counter)
+void perf_counter_task_migration(struct task_struct *task, int cpu)
{
- struct task_struct *curr = counter->ctx->task;
-
- if (curr)
- return curr->se.nr_migrations;
- return cpu_nr_migrations(smp_processor_id());
-}
-
-static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
-{
- u64 prev, now;
- s64 delta;
-
- prev = atomic64_read(&counter->hw.prev_count);
- now = get_cpu_migrations(counter);
-
- atomic64_set(&counter->hw.prev_count, now);
-
- delta = now - prev;
-
- atomic64_add(delta, &counter->count);
-}
-
-static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
-{
- cpu_migrations_perf_counter_update(counter);
-}
+ struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
+ struct perf_counter_context *ctx;
-static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
-{
- if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
- atomic64_set(&counter->hw.prev_count,
- get_cpu_migrations(counter));
- return 0;
-}
+ perf_swcounter_ctx_event(&cpuctx->ctx, PERF_TYPE_SOFTWARE,
+ PERF_COUNT_CPU_MIGRATIONS,
+ 1, 1, NULL, 0);
-static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
-{
- cpu_migrations_perf_counter_update(counter);
+ ctx = perf_pin_task_context(task);
+ if (ctx) {
+ perf_swcounter_ctx_event(ctx, PERF_TYPE_SOFTWARE,
+ PERF_COUNT_CPU_MIGRATIONS,
+ 1, 1, NULL, 0);
+ perf_unpin_context(ctx);
+ }
}
-static const struct pmu perf_ops_cpu_migrations = {
- .enable = cpu_migrations_perf_counter_enable,
- .disable = cpu_migrations_perf_counter_disable,
- .read = cpu_migrations_perf_counter_read,
-};
-
#ifdef CONFIG_EVENT_PROFILE
void perf_tpcounter_event(int event_id)
{
static void tp_perf_counter_destroy(struct perf_counter *counter)
{
- ftrace_profile_disable(perf_event_id(&counter->hw_event));
+ ftrace_profile_disable(perf_event_id(&counter->attr));
}
static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
{
- int event_id = perf_event_id(&counter->hw_event);
+ int event_id = perf_event_id(&counter->attr);
int ret;
ret = ftrace_profile_enable(event_id);
return NULL;
counter->destroy = tp_perf_counter_destroy;
- counter->hw.irq_period = counter->hw_event.irq_period;
+ counter->hw.sample_period = counter->attr.sample_period;
return &perf_ops_generic;
}
* to be kernel events, and page faults are never hypervisor
* events.
*/
- switch (perf_event_id(&counter->hw_event)) {
+ switch (perf_event_id(&counter->attr)) {
case PERF_COUNT_CPU_CLOCK:
pmu = &perf_ops_cpu_clock;
case PERF_COUNT_PAGE_FAULTS_MIN:
case PERF_COUNT_PAGE_FAULTS_MAJ:
case PERF_COUNT_CONTEXT_SWITCHES:
- pmu = &perf_ops_generic;
- break;
case PERF_COUNT_CPU_MIGRATIONS:
- if (!counter->hw_event.exclude_kernel)
- pmu = &perf_ops_cpu_migrations;
+ pmu = &perf_ops_generic;
break;
}
* Allocate and initialize a counter structure
*/
static struct perf_counter *
-perf_counter_alloc(struct perf_counter_hw_event *hw_event,
+perf_counter_alloc(struct perf_counter_attr *attr,
int cpu,
struct perf_counter_context *ctx,
struct perf_counter *group_leader,
mutex_init(&counter->mmap_mutex);
- counter->cpu = cpu;
- counter->hw_event = *hw_event;
- counter->group_leader = group_leader;
- counter->pmu = NULL;
- counter->ctx = ctx;
- counter->oncpu = -1;
+ counter->cpu = cpu;
+ counter->attr = *attr;
+ counter->group_leader = group_leader;
+ counter->pmu = NULL;
+ counter->ctx = ctx;
+ counter->oncpu = -1;
- counter->state = PERF_COUNTER_STATE_INACTIVE;
- if (hw_event->disabled)
+ counter->ns = get_pid_ns(current->nsproxy->pid_ns);
+ counter->id = atomic64_inc_return(&perf_counter_id);
+
+ counter->state = PERF_COUNTER_STATE_INACTIVE;
+
+ if (attr->disabled)
counter->state = PERF_COUNTER_STATE_OFF;
pmu = NULL;
hwc = &counter->hw;
- if (hw_event->freq && hw_event->irq_freq)
- hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
+ if (attr->freq && attr->sample_freq)
+ hwc->sample_period = div64_u64(TICK_NSEC, attr->sample_freq);
else
- hwc->irq_period = hw_event->irq_period;
+ hwc->sample_period = attr->sample_period;
/*
- * we currently do not support PERF_RECORD_GROUP on inherited counters
+ * we currently do not support PERF_SAMPLE_GROUP on inherited counters
*/
- if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
+ if (attr->inherit && (attr->sample_type & PERF_SAMPLE_GROUP))
goto done;
- if (perf_event_raw(hw_event)) {
+ if (perf_event_raw(attr)) {
pmu = hw_perf_counter_init(counter);
goto done;
}
- switch (perf_event_type(hw_event)) {
+ switch (perf_event_type(attr)) {
case PERF_TYPE_HARDWARE:
pmu = hw_perf_counter_init(counter);
break;
err = PTR_ERR(pmu);
if (err) {
+ if (counter->ns)
+ put_pid_ns(counter->ns);
kfree(counter);
return ERR_PTR(err);
}
counter->pmu = pmu;
atomic_inc(&nr_counters);
- if (counter->hw_event.mmap)
- atomic_inc(&nr_mmap_tracking);
- if (counter->hw_event.munmap)
- atomic_inc(&nr_munmap_tracking);
- if (counter->hw_event.comm)
- atomic_inc(&nr_comm_tracking);
+ if (counter->attr.mmap)
+ atomic_inc(&nr_mmap_counters);
+ if (counter->attr.munmap)
+ atomic_inc(&nr_munmap_counters);
+ if (counter->attr.comm)
+ atomic_inc(&nr_comm_counters);
return counter;
}
/**
* sys_perf_counter_open - open a performance counter, associate it to a task/cpu
*
- * @hw_event_uptr: event type attributes for monitoring/sampling
+ * @attr_uptr: event type attributes for monitoring/sampling
* @pid: target pid
* @cpu: target cpu
* @group_fd: group leader counter fd
*/
SYSCALL_DEFINE5(perf_counter_open,
- const struct perf_counter_hw_event __user *, hw_event_uptr,
+ const struct perf_counter_attr __user *, attr_uptr,
pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
{
struct perf_counter *counter, *group_leader;
- struct perf_counter_hw_event hw_event;
+ struct perf_counter_attr attr;
struct perf_counter_context *ctx;
struct file *counter_file = NULL;
struct file *group_file = NULL;
if (flags)
return -EINVAL;
- if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
+ if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
return -EFAULT;
/*
/*
* Only a group leader can be exclusive or pinned
*/
- if (hw_event.exclusive || hw_event.pinned)
+ if (attr.exclusive || attr.pinned)
goto err_put_context;
}
- counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
+ counter = perf_counter_alloc(&attr, cpu, ctx, group_leader,
GFP_KERNEL);
ret = PTR_ERR(counter);
if (IS_ERR(counter))
if (parent_counter->parent)
parent_counter = parent_counter->parent;
- child_counter = perf_counter_alloc(&parent_counter->hw_event,
+ child_counter = perf_counter_alloc(&parent_counter->attr,
parent_counter->cpu, child_ctx,
group_leader, GFP_KERNEL);
if (IS_ERR(child_counter))
/*
* Make the child state follow the state of the parent counter,
- * not its hw_event.disabled bit. We hold the parent's mutex,
+ * not its attr.disabled bit. We hold the parent's mutex,
* so we won't race with perf_counter_{en, dis}able_family.
*/
if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
/*
* inherit into child's child as well:
*/
- child_counter->hw_event.inherit = 1;
+ child_counter->attr.inherit = 1;
/*
* Get a reference to the parent filp - we will fput it
if (counter != counter->group_leader)
continue;
- if (!counter->hw_event.inherit) {
+ if (!counter->attr.inherit) {
inherited_all = 0;
continue;
}
return NOTIFY_OK;
}
+/*
+ * This has to have a higher priority than migration_notifier in sched.c.
+ */
static struct notifier_block __cpuinitdata perf_cpu_nb = {
.notifier_call = perf_cpu_notify,
+ .priority = 20,
};
void __init perf_counter_init(void)