2 * Performance counter core code
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
8 * For licensing details see kernel-base/COPYING
13 #include <linux/cpu.h>
14 #include <linux/smp.h>
15 #include <linux/file.h>
16 #include <linux/poll.h>
17 #include <linux/sysfs.h>
18 #include <linux/ptrace.h>
19 #include <linux/percpu.h>
20 #include <linux/vmstat.h>
21 #include <linux/hardirq.h>
22 #include <linux/rculist.h>
23 #include <linux/uaccess.h>
24 #include <linux/syscalls.h>
25 #include <linux/anon_inodes.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/perf_counter.h>
28 #include <linux/dcache.h>
30 #include <asm/irq_regs.h>
33 * Each CPU has a list of per CPU counters:
35 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
37 int perf_max_counters __read_mostly
= 1;
38 static int perf_reserved_percpu __read_mostly
;
39 static int perf_overcommit __read_mostly
= 1;
42 * Mutex for (sysadmin-configurable) counter reservations:
44 static DEFINE_MUTEX(perf_resource_mutex
);
47 * Architecture provided APIs - weak aliases:
49 extern __weak
const struct hw_perf_counter_ops
*
50 hw_perf_counter_init(struct perf_counter
*counter
)
55 u64 __weak
hw_perf_save_disable(void) { return 0; }
56 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
57 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
58 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
59 struct perf_cpu_context
*cpuctx
,
60 struct perf_counter_context
*ctx
, int cpu
)
65 void __weak
perf_counter_print_debug(void) { }
68 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
70 struct perf_counter
*group_leader
= counter
->group_leader
;
73 * Depending on whether it is a standalone or sibling counter,
74 * add it straight to the context's counter list, or to the group
75 * leader's sibling list:
77 if (counter
->group_leader
== counter
)
78 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
80 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
81 group_leader
->nr_siblings
++;
84 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
88 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
90 struct perf_counter
*sibling
, *tmp
;
92 list_del_init(&counter
->list_entry
);
93 list_del_rcu(&counter
->event_entry
);
95 if (counter
->group_leader
!= counter
)
96 counter
->group_leader
->nr_siblings
--;
99 * If this was a group counter with sibling counters then
100 * upgrade the siblings to singleton counters by adding them
101 * to the context list directly:
103 list_for_each_entry_safe(sibling
, tmp
,
104 &counter
->sibling_list
, list_entry
) {
106 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
107 sibling
->group_leader
= sibling
;
112 counter_sched_out(struct perf_counter
*counter
,
113 struct perf_cpu_context
*cpuctx
,
114 struct perf_counter_context
*ctx
)
116 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
119 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
120 counter
->tstamp_stopped
= ctx
->time_now
;
121 counter
->hw_ops
->disable(counter
);
124 if (!is_software_counter(counter
))
125 cpuctx
->active_oncpu
--;
127 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
128 cpuctx
->exclusive
= 0;
132 group_sched_out(struct perf_counter
*group_counter
,
133 struct perf_cpu_context
*cpuctx
,
134 struct perf_counter_context
*ctx
)
136 struct perf_counter
*counter
;
138 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
141 counter_sched_out(group_counter
, cpuctx
, ctx
);
144 * Schedule out siblings (if any):
146 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
147 counter_sched_out(counter
, cpuctx
, ctx
);
149 if (group_counter
->hw_event
.exclusive
)
150 cpuctx
->exclusive
= 0;
154 * Cross CPU call to remove a performance counter
156 * We disable the counter on the hardware level first. After that we
157 * remove it from the context list.
159 static void __perf_counter_remove_from_context(void *info
)
161 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
162 struct perf_counter
*counter
= info
;
163 struct perf_counter_context
*ctx
= counter
->ctx
;
168 * If this is a task context, we need to check whether it is
169 * the current task context of this cpu. If not it has been
170 * scheduled out before the smp call arrived.
172 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
175 curr_rq_lock_irq_save(&flags
);
176 spin_lock(&ctx
->lock
);
178 counter_sched_out(counter
, cpuctx
, ctx
);
180 counter
->task
= NULL
;
184 * Protect the list operation against NMI by disabling the
185 * counters on a global level. NOP for non NMI based counters.
187 perf_flags
= hw_perf_save_disable();
188 list_del_counter(counter
, ctx
);
189 hw_perf_restore(perf_flags
);
193 * Allow more per task counters with respect to the
196 cpuctx
->max_pertask
=
197 min(perf_max_counters
- ctx
->nr_counters
,
198 perf_max_counters
- perf_reserved_percpu
);
201 spin_unlock(&ctx
->lock
);
202 curr_rq_unlock_irq_restore(&flags
);
207 * Remove the counter from a task's (or a CPU's) list of counters.
209 * Must be called with counter->mutex and ctx->mutex held.
211 * CPU counters are removed with a smp call. For task counters we only
212 * call when the task is on a CPU.
214 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
216 struct perf_counter_context
*ctx
= counter
->ctx
;
217 struct task_struct
*task
= ctx
->task
;
221 * Per cpu counters are removed via an smp call and
222 * the removal is always sucessful.
224 smp_call_function_single(counter
->cpu
,
225 __perf_counter_remove_from_context
,
231 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
234 spin_lock_irq(&ctx
->lock
);
236 * If the context is active we need to retry the smp call.
238 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
239 spin_unlock_irq(&ctx
->lock
);
244 * The lock prevents that this context is scheduled in so we
245 * can remove the counter safely, if the call above did not
248 if (!list_empty(&counter
->list_entry
)) {
250 list_del_counter(counter
, ctx
);
251 counter
->task
= NULL
;
253 spin_unlock_irq(&ctx
->lock
);
257 * Get the current time for this context.
258 * If this is a task context, we use the task's task clock,
259 * or for a per-cpu context, we use the cpu clock.
261 static u64
get_context_time(struct perf_counter_context
*ctx
, int update
)
263 struct task_struct
*curr
= ctx
->task
;
266 return cpu_clock(smp_processor_id());
268 return __task_delta_exec(curr
, update
) + curr
->se
.sum_exec_runtime
;
272 * Update the record of the current time in a context.
274 static void update_context_time(struct perf_counter_context
*ctx
, int update
)
276 ctx
->time_now
= get_context_time(ctx
, update
) - ctx
->time_lost
;
280 * Update the total_time_enabled and total_time_running fields for a counter.
282 static void update_counter_times(struct perf_counter
*counter
)
284 struct perf_counter_context
*ctx
= counter
->ctx
;
287 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
288 counter
->total_time_enabled
= ctx
->time_now
-
289 counter
->tstamp_enabled
;
290 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
291 run_end
= counter
->tstamp_stopped
;
293 run_end
= ctx
->time_now
;
294 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
299 * Update total_time_enabled and total_time_running for all counters in a group.
301 static void update_group_times(struct perf_counter
*leader
)
303 struct perf_counter
*counter
;
305 update_counter_times(leader
);
306 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
307 update_counter_times(counter
);
311 * Cross CPU call to disable a performance counter
313 static void __perf_counter_disable(void *info
)
315 struct perf_counter
*counter
= info
;
316 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
317 struct perf_counter_context
*ctx
= counter
->ctx
;
321 * If this is a per-task counter, need to check whether this
322 * counter's task is the current task on this cpu.
324 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
327 curr_rq_lock_irq_save(&flags
);
328 spin_lock(&ctx
->lock
);
331 * If the counter is on, turn it off.
332 * If it is in error state, leave it in error state.
334 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
335 update_context_time(ctx
, 1);
336 update_counter_times(counter
);
337 if (counter
== counter
->group_leader
)
338 group_sched_out(counter
, cpuctx
, ctx
);
340 counter_sched_out(counter
, cpuctx
, ctx
);
341 counter
->state
= PERF_COUNTER_STATE_OFF
;
344 spin_unlock(&ctx
->lock
);
345 curr_rq_unlock_irq_restore(&flags
);
351 static void perf_counter_disable(struct perf_counter
*counter
)
353 struct perf_counter_context
*ctx
= counter
->ctx
;
354 struct task_struct
*task
= ctx
->task
;
358 * Disable the counter on the cpu that it's on
360 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
366 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
368 spin_lock_irq(&ctx
->lock
);
370 * If the counter is still active, we need to retry the cross-call.
372 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
373 spin_unlock_irq(&ctx
->lock
);
378 * Since we have the lock this context can't be scheduled
379 * in, so we can change the state safely.
381 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
382 update_counter_times(counter
);
383 counter
->state
= PERF_COUNTER_STATE_OFF
;
386 spin_unlock_irq(&ctx
->lock
);
390 * Disable a counter and all its children.
392 static void perf_counter_disable_family(struct perf_counter
*counter
)
394 struct perf_counter
*child
;
396 perf_counter_disable(counter
);
399 * Lock the mutex to protect the list of children
401 mutex_lock(&counter
->mutex
);
402 list_for_each_entry(child
, &counter
->child_list
, child_list
)
403 perf_counter_disable(child
);
404 mutex_unlock(&counter
->mutex
);
408 counter_sched_in(struct perf_counter
*counter
,
409 struct perf_cpu_context
*cpuctx
,
410 struct perf_counter_context
*ctx
,
413 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
416 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
417 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
419 * The new state must be visible before we turn it on in the hardware:
423 if (counter
->hw_ops
->enable(counter
)) {
424 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
429 counter
->tstamp_running
+= ctx
->time_now
- counter
->tstamp_stopped
;
431 if (!is_software_counter(counter
))
432 cpuctx
->active_oncpu
++;
435 if (counter
->hw_event
.exclusive
)
436 cpuctx
->exclusive
= 1;
442 * Return 1 for a group consisting entirely of software counters,
443 * 0 if the group contains any hardware counters.
445 static int is_software_only_group(struct perf_counter
*leader
)
447 struct perf_counter
*counter
;
449 if (!is_software_counter(leader
))
452 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
453 if (!is_software_counter(counter
))
460 * Work out whether we can put this counter group on the CPU now.
462 static int group_can_go_on(struct perf_counter
*counter
,
463 struct perf_cpu_context
*cpuctx
,
467 * Groups consisting entirely of software counters can always go on.
469 if (is_software_only_group(counter
))
472 * If an exclusive group is already on, no other hardware
473 * counters can go on.
475 if (cpuctx
->exclusive
)
478 * If this group is exclusive and there are already
479 * counters on the CPU, it can't go on.
481 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
484 * Otherwise, try to add it if all previous groups were able
490 static void add_counter_to_ctx(struct perf_counter
*counter
,
491 struct perf_counter_context
*ctx
)
493 list_add_counter(counter
, ctx
);
495 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
496 counter
->tstamp_enabled
= ctx
->time_now
;
497 counter
->tstamp_running
= ctx
->time_now
;
498 counter
->tstamp_stopped
= ctx
->time_now
;
502 * Cross CPU call to install and enable a performance counter
504 static void __perf_install_in_context(void *info
)
506 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
507 struct perf_counter
*counter
= info
;
508 struct perf_counter_context
*ctx
= counter
->ctx
;
509 struct perf_counter
*leader
= counter
->group_leader
;
510 int cpu
= smp_processor_id();
516 * If this is a task context, we need to check whether it is
517 * the current task context of this cpu. If not it has been
518 * scheduled out before the smp call arrived.
520 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
523 curr_rq_lock_irq_save(&flags
);
524 spin_lock(&ctx
->lock
);
525 update_context_time(ctx
, 1);
528 * Protect the list operation against NMI by disabling the
529 * counters on a global level. NOP for non NMI based counters.
531 perf_flags
= hw_perf_save_disable();
533 add_counter_to_ctx(counter
, ctx
);
536 * Don't put the counter on if it is disabled or if
537 * it is in a group and the group isn't on.
539 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
540 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
544 * An exclusive counter can't go on if there are already active
545 * hardware counters, and no hardware counter can go on if there
546 * is already an exclusive counter on.
548 if (!group_can_go_on(counter
, cpuctx
, 1))
551 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
555 * This counter couldn't go on. If it is in a group
556 * then we have to pull the whole group off.
557 * If the counter group is pinned then put it in error state.
559 if (leader
!= counter
)
560 group_sched_out(leader
, cpuctx
, ctx
);
561 if (leader
->hw_event
.pinned
) {
562 update_group_times(leader
);
563 leader
->state
= PERF_COUNTER_STATE_ERROR
;
567 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
568 cpuctx
->max_pertask
--;
571 hw_perf_restore(perf_flags
);
573 spin_unlock(&ctx
->lock
);
574 curr_rq_unlock_irq_restore(&flags
);
578 * Attach a performance counter to a context
580 * First we add the counter to the list with the hardware enable bit
581 * in counter->hw_config cleared.
583 * If the counter is attached to a task which is on a CPU we use a smp
584 * call to enable it in the task context. The task might have been
585 * scheduled away, but we check this in the smp call again.
587 * Must be called with ctx->mutex held.
590 perf_install_in_context(struct perf_counter_context
*ctx
,
591 struct perf_counter
*counter
,
594 struct task_struct
*task
= ctx
->task
;
598 * Per cpu counters are installed via an smp call and
599 * the install is always sucessful.
601 smp_call_function_single(cpu
, __perf_install_in_context
,
606 counter
->task
= task
;
608 task_oncpu_function_call(task
, __perf_install_in_context
,
611 spin_lock_irq(&ctx
->lock
);
613 * we need to retry the smp call.
615 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
616 spin_unlock_irq(&ctx
->lock
);
621 * The lock prevents that this context is scheduled in so we
622 * can add the counter safely, if it the call above did not
625 if (list_empty(&counter
->list_entry
))
626 add_counter_to_ctx(counter
, ctx
);
627 spin_unlock_irq(&ctx
->lock
);
631 * Cross CPU call to enable a performance counter
633 static void __perf_counter_enable(void *info
)
635 struct perf_counter
*counter
= info
;
636 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
637 struct perf_counter_context
*ctx
= counter
->ctx
;
638 struct perf_counter
*leader
= counter
->group_leader
;
643 * If this is a per-task counter, need to check whether this
644 * counter's task is the current task on this cpu.
646 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
649 curr_rq_lock_irq_save(&flags
);
650 spin_lock(&ctx
->lock
);
651 update_context_time(ctx
, 1);
653 counter
->prev_state
= counter
->state
;
654 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
656 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
657 counter
->tstamp_enabled
= ctx
->time_now
- counter
->total_time_enabled
;
660 * If the counter is in a group and isn't the group leader,
661 * then don't put it on unless the group is on.
663 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
666 if (!group_can_go_on(counter
, cpuctx
, 1))
669 err
= counter_sched_in(counter
, cpuctx
, ctx
,
674 * If this counter can't go on and it's part of a
675 * group, then the whole group has to come off.
677 if (leader
!= counter
)
678 group_sched_out(leader
, cpuctx
, ctx
);
679 if (leader
->hw_event
.pinned
) {
680 update_group_times(leader
);
681 leader
->state
= PERF_COUNTER_STATE_ERROR
;
686 spin_unlock(&ctx
->lock
);
687 curr_rq_unlock_irq_restore(&flags
);
693 static void perf_counter_enable(struct perf_counter
*counter
)
695 struct perf_counter_context
*ctx
= counter
->ctx
;
696 struct task_struct
*task
= ctx
->task
;
700 * Enable the counter on the cpu that it's on
702 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
707 spin_lock_irq(&ctx
->lock
);
708 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
712 * If the counter is in error state, clear that first.
713 * That way, if we see the counter in error state below, we
714 * know that it has gone back into error state, as distinct
715 * from the task having been scheduled away before the
716 * cross-call arrived.
718 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
719 counter
->state
= PERF_COUNTER_STATE_OFF
;
722 spin_unlock_irq(&ctx
->lock
);
723 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
725 spin_lock_irq(&ctx
->lock
);
728 * If the context is active and the counter is still off,
729 * we need to retry the cross-call.
731 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
735 * Since we have the lock this context can't be scheduled
736 * in, so we can change the state safely.
738 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
739 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
740 counter
->tstamp_enabled
= ctx
->time_now
-
741 counter
->total_time_enabled
;
744 spin_unlock_irq(&ctx
->lock
);
747 static void perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
749 atomic_add(refresh
, &counter
->event_limit
);
750 perf_counter_enable(counter
);
754 * Enable a counter and all its children.
756 static void perf_counter_enable_family(struct perf_counter
*counter
)
758 struct perf_counter
*child
;
760 perf_counter_enable(counter
);
763 * Lock the mutex to protect the list of children
765 mutex_lock(&counter
->mutex
);
766 list_for_each_entry(child
, &counter
->child_list
, child_list
)
767 perf_counter_enable(child
);
768 mutex_unlock(&counter
->mutex
);
771 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
772 struct perf_cpu_context
*cpuctx
)
774 struct perf_counter
*counter
;
777 spin_lock(&ctx
->lock
);
779 if (likely(!ctx
->nr_counters
))
781 update_context_time(ctx
, 0);
783 flags
= hw_perf_save_disable();
784 if (ctx
->nr_active
) {
785 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
786 group_sched_out(counter
, cpuctx
, ctx
);
788 hw_perf_restore(flags
);
790 spin_unlock(&ctx
->lock
);
794 * Called from scheduler to remove the counters of the current task,
795 * with interrupts disabled.
797 * We stop each counter and update the counter value in counter->count.
799 * This does not protect us against NMI, but disable()
800 * sets the disabled bit in the control field of counter _before_
801 * accessing the counter control register. If a NMI hits, then it will
802 * not restart the counter.
804 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
806 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
807 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
808 struct pt_regs
*regs
;
810 if (likely(!cpuctx
->task_ctx
))
813 regs
= task_pt_regs(task
);
814 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
);
815 __perf_counter_sched_out(ctx
, cpuctx
);
817 cpuctx
->task_ctx
= NULL
;
820 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
822 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
826 group_sched_in(struct perf_counter
*group_counter
,
827 struct perf_cpu_context
*cpuctx
,
828 struct perf_counter_context
*ctx
,
831 struct perf_counter
*counter
, *partial_group
;
834 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
837 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
839 return ret
< 0 ? ret
: 0;
841 group_counter
->prev_state
= group_counter
->state
;
842 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
846 * Schedule in siblings as one group (if any):
848 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
849 counter
->prev_state
= counter
->state
;
850 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
851 partial_group
= counter
;
860 * Groups can be scheduled in as one unit only, so undo any
861 * partial group before returning:
863 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
864 if (counter
== partial_group
)
866 counter_sched_out(counter
, cpuctx
, ctx
);
868 counter_sched_out(group_counter
, cpuctx
, ctx
);
874 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
875 struct perf_cpu_context
*cpuctx
, int cpu
)
877 struct perf_counter
*counter
;
881 spin_lock(&ctx
->lock
);
883 if (likely(!ctx
->nr_counters
))
887 * Add any time since the last sched_out to the lost time
888 * so it doesn't get included in the total_time_enabled and
889 * total_time_running measures for counters in the context.
891 ctx
->time_lost
= get_context_time(ctx
, 0) - ctx
->time_now
;
893 flags
= hw_perf_save_disable();
896 * First go through the list and put on any pinned groups
897 * in order to give them the best chance of going on.
899 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
900 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
901 !counter
->hw_event
.pinned
)
903 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
906 if (group_can_go_on(counter
, cpuctx
, 1))
907 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
910 * If this pinned group hasn't been scheduled,
911 * put it in error state.
913 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
914 update_group_times(counter
);
915 counter
->state
= PERF_COUNTER_STATE_ERROR
;
919 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
921 * Ignore counters in OFF or ERROR state, and
922 * ignore pinned counters since we did them already.
924 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
925 counter
->hw_event
.pinned
)
929 * Listen to the 'cpu' scheduling filter constraint
932 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
935 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
936 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
940 hw_perf_restore(flags
);
942 spin_unlock(&ctx
->lock
);
946 * Called from scheduler to add the counters of the current task
947 * with interrupts disabled.
949 * We restore the counter value and then enable it.
951 * This does not protect us against NMI, but enable()
952 * sets the enabled bit in the control field of counter _before_
953 * accessing the counter control register. If a NMI hits, then it will
954 * keep the counter running.
956 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
958 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
959 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
961 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
962 cpuctx
->task_ctx
= ctx
;
965 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
967 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
969 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
972 int perf_counter_task_disable(void)
974 struct task_struct
*curr
= current
;
975 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
976 struct perf_counter
*counter
;
981 if (likely(!ctx
->nr_counters
))
984 curr_rq_lock_irq_save(&flags
);
985 cpu
= smp_processor_id();
987 /* force the update of the task clock: */
988 __task_delta_exec(curr
, 1);
990 perf_counter_task_sched_out(curr
, cpu
);
992 spin_lock(&ctx
->lock
);
995 * Disable all the counters:
997 perf_flags
= hw_perf_save_disable();
999 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1000 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
1001 update_group_times(counter
);
1002 counter
->state
= PERF_COUNTER_STATE_OFF
;
1006 hw_perf_restore(perf_flags
);
1008 spin_unlock(&ctx
->lock
);
1010 curr_rq_unlock_irq_restore(&flags
);
1015 int perf_counter_task_enable(void)
1017 struct task_struct
*curr
= current
;
1018 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1019 struct perf_counter
*counter
;
1020 unsigned long flags
;
1024 if (likely(!ctx
->nr_counters
))
1027 curr_rq_lock_irq_save(&flags
);
1028 cpu
= smp_processor_id();
1030 /* force the update of the task clock: */
1031 __task_delta_exec(curr
, 1);
1033 perf_counter_task_sched_out(curr
, cpu
);
1035 spin_lock(&ctx
->lock
);
1038 * Disable all the counters:
1040 perf_flags
= hw_perf_save_disable();
1042 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1043 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1045 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1046 counter
->tstamp_enabled
= ctx
->time_now
-
1047 counter
->total_time_enabled
;
1048 counter
->hw_event
.disabled
= 0;
1050 hw_perf_restore(perf_flags
);
1052 spin_unlock(&ctx
->lock
);
1054 perf_counter_task_sched_in(curr
, cpu
);
1056 curr_rq_unlock_irq_restore(&flags
);
1062 * Round-robin a context's counters:
1064 static void rotate_ctx(struct perf_counter_context
*ctx
)
1066 struct perf_counter
*counter
;
1069 if (!ctx
->nr_counters
)
1072 spin_lock(&ctx
->lock
);
1074 * Rotate the first entry last (works just fine for group counters too):
1076 perf_flags
= hw_perf_save_disable();
1077 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1078 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1081 hw_perf_restore(perf_flags
);
1083 spin_unlock(&ctx
->lock
);
1086 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1088 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1089 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1090 const int rotate_percpu
= 0;
1093 perf_counter_cpu_sched_out(cpuctx
);
1094 perf_counter_task_sched_out(curr
, cpu
);
1097 rotate_ctx(&cpuctx
->ctx
);
1101 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1102 perf_counter_task_sched_in(curr
, cpu
);
1106 * Cross CPU call to read the hardware counter
1108 static void __read(void *info
)
1110 struct perf_counter
*counter
= info
;
1111 struct perf_counter_context
*ctx
= counter
->ctx
;
1112 unsigned long flags
;
1114 curr_rq_lock_irq_save(&flags
);
1116 update_context_time(ctx
, 1);
1117 counter
->hw_ops
->read(counter
);
1118 update_counter_times(counter
);
1119 curr_rq_unlock_irq_restore(&flags
);
1122 static u64
perf_counter_read(struct perf_counter
*counter
)
1125 * If counter is enabled and currently active on a CPU, update the
1126 * value in the counter structure:
1128 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1129 smp_call_function_single(counter
->oncpu
,
1130 __read
, counter
, 1);
1131 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1132 update_counter_times(counter
);
1135 return atomic64_read(&counter
->count
);
1138 static void put_context(struct perf_counter_context
*ctx
)
1141 put_task_struct(ctx
->task
);
1144 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1146 struct perf_cpu_context
*cpuctx
;
1147 struct perf_counter_context
*ctx
;
1148 struct task_struct
*task
;
1151 * If cpu is not a wildcard then this is a percpu counter:
1154 /* Must be root to operate on a CPU counter: */
1155 if (!capable(CAP_SYS_ADMIN
))
1156 return ERR_PTR(-EACCES
);
1158 if (cpu
< 0 || cpu
> num_possible_cpus())
1159 return ERR_PTR(-EINVAL
);
1162 * We could be clever and allow to attach a counter to an
1163 * offline CPU and activate it when the CPU comes up, but
1166 if (!cpu_isset(cpu
, cpu_online_map
))
1167 return ERR_PTR(-ENODEV
);
1169 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1179 task
= find_task_by_vpid(pid
);
1181 get_task_struct(task
);
1185 return ERR_PTR(-ESRCH
);
1187 ctx
= &task
->perf_counter_ctx
;
1190 /* Reuse ptrace permission checks for now. */
1191 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1193 return ERR_PTR(-EACCES
);
1199 static void free_counter_rcu(struct rcu_head
*head
)
1201 struct perf_counter
*counter
;
1203 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1207 static void perf_pending_sync(struct perf_counter
*counter
);
1209 static void free_counter(struct perf_counter
*counter
)
1211 perf_pending_sync(counter
);
1213 if (counter
->destroy
)
1214 counter
->destroy(counter
);
1216 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1220 * Called when the last reference to the file is gone.
1222 static int perf_release(struct inode
*inode
, struct file
*file
)
1224 struct perf_counter
*counter
= file
->private_data
;
1225 struct perf_counter_context
*ctx
= counter
->ctx
;
1227 file
->private_data
= NULL
;
1229 mutex_lock(&ctx
->mutex
);
1230 mutex_lock(&counter
->mutex
);
1232 perf_counter_remove_from_context(counter
);
1234 mutex_unlock(&counter
->mutex
);
1235 mutex_unlock(&ctx
->mutex
);
1237 free_counter(counter
);
1244 * Read the performance counter - simple non blocking version for now
1247 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1253 * Return end-of-file for a read on a counter that is in
1254 * error state (i.e. because it was pinned but it couldn't be
1255 * scheduled on to the CPU at some point).
1257 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1260 mutex_lock(&counter
->mutex
);
1261 values
[0] = perf_counter_read(counter
);
1263 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1264 values
[n
++] = counter
->total_time_enabled
+
1265 atomic64_read(&counter
->child_total_time_enabled
);
1266 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1267 values
[n
++] = counter
->total_time_running
+
1268 atomic64_read(&counter
->child_total_time_running
);
1269 mutex_unlock(&counter
->mutex
);
1271 if (count
< n
* sizeof(u64
))
1273 count
= n
* sizeof(u64
);
1275 if (copy_to_user(buf
, values
, count
))
1282 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1284 struct perf_counter
*counter
= file
->private_data
;
1286 return perf_read_hw(counter
, buf
, count
);
1289 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1291 struct perf_counter
*counter
= file
->private_data
;
1292 struct perf_mmap_data
*data
;
1293 unsigned int events
;
1296 data
= rcu_dereference(counter
->data
);
1298 events
= atomic_xchg(&data
->wakeup
, 0);
1303 poll_wait(file
, &counter
->waitq
, wait
);
1308 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1310 struct perf_counter
*counter
= file
->private_data
;
1314 case PERF_COUNTER_IOC_ENABLE
:
1315 perf_counter_enable_family(counter
);
1317 case PERF_COUNTER_IOC_DISABLE
:
1318 perf_counter_disable_family(counter
);
1320 case PERF_COUNTER_IOC_REFRESH
:
1321 perf_counter_refresh(counter
, arg
);
1330 * Callers need to ensure there can be no nesting of this function, otherwise
1331 * the seqlock logic goes bad. We can not serialize this because the arch
1332 * code calls this from NMI context.
1334 void perf_counter_update_userpage(struct perf_counter
*counter
)
1336 struct perf_mmap_data
*data
;
1337 struct perf_counter_mmap_page
*userpg
;
1340 data
= rcu_dereference(counter
->data
);
1344 userpg
= data
->user_page
;
1347 * Disable preemption so as to not let the corresponding user-space
1348 * spin too long if we get preempted.
1353 userpg
->index
= counter
->hw
.idx
;
1354 userpg
->offset
= atomic64_read(&counter
->count
);
1355 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1356 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1365 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1367 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1368 struct perf_mmap_data
*data
;
1369 int ret
= VM_FAULT_SIGBUS
;
1372 data
= rcu_dereference(counter
->data
);
1376 if (vmf
->pgoff
== 0) {
1377 vmf
->page
= virt_to_page(data
->user_page
);
1379 int nr
= vmf
->pgoff
- 1;
1381 if ((unsigned)nr
> data
->nr_pages
)
1384 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1386 get_page(vmf
->page
);
1394 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1396 struct perf_mmap_data
*data
;
1400 WARN_ON(atomic_read(&counter
->mmap_count
));
1402 size
= sizeof(struct perf_mmap_data
);
1403 size
+= nr_pages
* sizeof(void *);
1405 data
= kzalloc(size
, GFP_KERNEL
);
1409 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1410 if (!data
->user_page
)
1411 goto fail_user_page
;
1413 for (i
= 0; i
< nr_pages
; i
++) {
1414 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1415 if (!data
->data_pages
[i
])
1416 goto fail_data_pages
;
1419 data
->nr_pages
= nr_pages
;
1421 rcu_assign_pointer(counter
->data
, data
);
1426 for (i
--; i
>= 0; i
--)
1427 free_page((unsigned long)data
->data_pages
[i
]);
1429 free_page((unsigned long)data
->user_page
);
1438 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1440 struct perf_mmap_data
*data
= container_of(rcu_head
,
1441 struct perf_mmap_data
, rcu_head
);
1444 free_page((unsigned long)data
->user_page
);
1445 for (i
= 0; i
< data
->nr_pages
; i
++)
1446 free_page((unsigned long)data
->data_pages
[i
]);
1450 static void perf_mmap_data_free(struct perf_counter
*counter
)
1452 struct perf_mmap_data
*data
= counter
->data
;
1454 WARN_ON(atomic_read(&counter
->mmap_count
));
1456 rcu_assign_pointer(counter
->data
, NULL
);
1457 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1460 static void perf_mmap_open(struct vm_area_struct
*vma
)
1462 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1464 atomic_inc(&counter
->mmap_count
);
1467 static void perf_mmap_close(struct vm_area_struct
*vma
)
1469 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1471 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1472 &counter
->mmap_mutex
)) {
1473 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_pages
+ 1;
1474 perf_mmap_data_free(counter
);
1475 mutex_unlock(&counter
->mmap_mutex
);
1479 static struct vm_operations_struct perf_mmap_vmops
= {
1480 .open
= perf_mmap_open
,
1481 .close
= perf_mmap_close
,
1482 .fault
= perf_mmap_fault
,
1485 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1487 struct perf_counter
*counter
= file
->private_data
;
1488 unsigned long vma_size
;
1489 unsigned long nr_pages
;
1490 unsigned long locked
, lock_limit
;
1493 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1496 vma_size
= vma
->vm_end
- vma
->vm_start
;
1497 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1500 * If we have data pages ensure they're a power-of-two number, so we
1501 * can do bitmasks instead of modulo.
1503 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1506 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1509 if (vma
->vm_pgoff
!= 0)
1512 mutex_lock(&counter
->mmap_mutex
);
1513 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1514 if (nr_pages
!= counter
->data
->nr_pages
)
1519 locked
= vma
->vm_mm
->locked_vm
;
1520 locked
+= nr_pages
+ 1;
1522 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1523 lock_limit
>>= PAGE_SHIFT
;
1525 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1530 WARN_ON(counter
->data
);
1531 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1535 atomic_set(&counter
->mmap_count
, 1);
1536 vma
->vm_mm
->locked_vm
+= nr_pages
+ 1;
1538 mutex_unlock(&counter
->mmap_mutex
);
1540 vma
->vm_flags
&= ~VM_MAYWRITE
;
1541 vma
->vm_flags
|= VM_RESERVED
;
1542 vma
->vm_ops
= &perf_mmap_vmops
;
1547 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1549 struct perf_counter
*counter
= filp
->private_data
;
1550 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1553 mutex_lock(&inode
->i_mutex
);
1554 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1555 mutex_unlock(&inode
->i_mutex
);
1563 static const struct file_operations perf_fops
= {
1564 .release
= perf_release
,
1567 .unlocked_ioctl
= perf_ioctl
,
1568 .compat_ioctl
= perf_ioctl
,
1570 .fasync
= perf_fasync
,
1574 * Perf counter wakeup
1576 * If there's data, ensure we set the poll() state and publish everything
1577 * to user-space before waking everybody up.
1580 void perf_counter_wakeup(struct perf_counter
*counter
)
1582 struct perf_mmap_data
*data
;
1585 data
= rcu_dereference(counter
->data
);
1587 atomic_set(&data
->wakeup
, POLL_IN
);
1589 * Ensure all data writes are issued before updating the
1590 * user-space data head information. The matching rmb()
1591 * will be in userspace after reading this value.
1594 data
->user_page
->data_head
= atomic_read(&data
->head
);
1598 wake_up_all(&counter
->waitq
);
1599 kill_fasync(&counter
->fasync
, SIGIO
, POLL_IN
);
1605 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1607 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1608 * single linked list and use cmpxchg() to add entries lockless.
1611 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1613 struct perf_counter
*counter
= container_of(entry
,
1614 struct perf_counter
, pending
);
1616 if (counter
->pending_disable
) {
1617 counter
->pending_disable
= 0;
1618 perf_counter_disable(counter
);
1621 if (counter
->pending_wakeup
) {
1622 counter
->pending_wakeup
= 0;
1623 perf_counter_wakeup(counter
);
1627 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1629 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1633 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1634 void (*func
)(struct perf_pending_entry
*))
1636 struct perf_pending_entry
**head
;
1638 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1643 head
= &get_cpu_var(perf_pending_head
);
1646 entry
->next
= *head
;
1647 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1649 set_perf_counter_pending();
1651 put_cpu_var(perf_pending_head
);
1654 static int __perf_pending_run(void)
1656 struct perf_pending_entry
*list
;
1659 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1660 while (list
!= PENDING_TAIL
) {
1661 void (*func
)(struct perf_pending_entry
*);
1662 struct perf_pending_entry
*entry
= list
;
1669 * Ensure we observe the unqueue before we issue the wakeup,
1670 * so that we won't be waiting forever.
1671 * -- see perf_not_pending().
1682 static inline int perf_not_pending(struct perf_counter
*counter
)
1685 * If we flush on whatever cpu we run, there is a chance we don't
1689 __perf_pending_run();
1693 * Ensure we see the proper queue state before going to sleep
1694 * so that we do not miss the wakeup. -- see perf_pending_handle()
1697 return counter
->pending
.next
== NULL
;
1700 static void perf_pending_sync(struct perf_counter
*counter
)
1702 wait_event(counter
->waitq
, perf_not_pending(counter
));
1705 void perf_counter_do_pending(void)
1707 __perf_pending_run();
1711 * Callchain support -- arch specific
1714 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1723 struct perf_output_handle
{
1724 struct perf_counter
*counter
;
1725 struct perf_mmap_data
*data
;
1726 unsigned int offset
;
1732 static inline void __perf_output_wakeup(struct perf_output_handle
*handle
)
1735 handle
->counter
->pending_wakeup
= 1;
1736 perf_pending_queue(&handle
->counter
->pending
,
1737 perf_pending_counter
);
1739 perf_counter_wakeup(handle
->counter
);
1742 static int perf_output_begin(struct perf_output_handle
*handle
,
1743 struct perf_counter
*counter
, unsigned int size
,
1746 struct perf_mmap_data
*data
;
1747 unsigned int offset
, head
;
1750 data
= rcu_dereference(counter
->data
);
1754 handle
->counter
= counter
;
1757 if (!data
->nr_pages
)
1761 offset
= head
= atomic_read(&data
->head
);
1763 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1765 handle
->data
= data
;
1766 handle
->offset
= offset
;
1767 handle
->head
= head
;
1768 handle
->wakeup
= (offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
);
1773 __perf_output_wakeup(handle
);
1780 static void perf_output_copy(struct perf_output_handle
*handle
,
1781 void *buf
, unsigned int len
)
1783 unsigned int pages_mask
;
1784 unsigned int offset
;
1788 offset
= handle
->offset
;
1789 pages_mask
= handle
->data
->nr_pages
- 1;
1790 pages
= handle
->data
->data_pages
;
1793 unsigned int page_offset
;
1796 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1797 page_offset
= offset
& (PAGE_SIZE
- 1);
1798 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1800 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1807 handle
->offset
= offset
;
1809 WARN_ON_ONCE(handle
->offset
> handle
->head
);
1812 #define perf_output_put(handle, x) \
1813 perf_output_copy((handle), &(x), sizeof(x))
1815 static void perf_output_end(struct perf_output_handle
*handle
)
1817 int wakeup_events
= handle
->counter
->hw_event
.wakeup_events
;
1819 if (wakeup_events
) {
1820 int events
= atomic_inc_return(&handle
->data
->events
);
1821 if (events
>= wakeup_events
) {
1822 atomic_sub(wakeup_events
, &handle
->data
->events
);
1823 __perf_output_wakeup(handle
);
1825 } else if (handle
->wakeup
)
1826 __perf_output_wakeup(handle
);
1830 static void perf_counter_output(struct perf_counter
*counter
,
1831 int nmi
, struct pt_regs
*regs
)
1834 u64 record_type
= counter
->hw_event
.record_type
;
1835 struct perf_output_handle handle
;
1836 struct perf_event_header header
;
1845 struct perf_callchain_entry
*callchain
= NULL
;
1846 int callchain_size
= 0;
1849 header
.type
= PERF_EVENT_COUNTER_OVERFLOW
;
1850 header
.size
= sizeof(header
);
1852 if (record_type
& PERF_RECORD_IP
) {
1853 ip
= instruction_pointer(regs
);
1854 header
.type
|= __PERF_EVENT_IP
;
1855 header
.size
+= sizeof(ip
);
1858 if (record_type
& PERF_RECORD_TID
) {
1859 /* namespace issues */
1860 tid_entry
.pid
= current
->group_leader
->pid
;
1861 tid_entry
.tid
= current
->pid
;
1863 header
.type
|= __PERF_EVENT_TID
;
1864 header
.size
+= sizeof(tid_entry
);
1867 if (record_type
& PERF_RECORD_GROUP
) {
1868 header
.type
|= __PERF_EVENT_GROUP
;
1869 header
.size
+= sizeof(u64
) +
1870 counter
->nr_siblings
* sizeof(group_entry
);
1873 if (record_type
& PERF_RECORD_CALLCHAIN
) {
1874 callchain
= perf_callchain(regs
);
1877 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
1879 header
.type
|= __PERF_EVENT_CALLCHAIN
;
1880 header
.size
+= callchain_size
;
1884 if (record_type
& PERF_RECORD_TIME
) {
1886 * Maybe do better on x86 and provide cpu_clock_nmi()
1888 time
= sched_clock();
1890 header
.type
|= __PERF_EVENT_TIME
;
1891 header
.size
+= sizeof(u64
);
1894 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
);
1898 perf_output_put(&handle
, header
);
1900 if (record_type
& PERF_RECORD_IP
)
1901 perf_output_put(&handle
, ip
);
1903 if (record_type
& PERF_RECORD_TID
)
1904 perf_output_put(&handle
, tid_entry
);
1906 if (record_type
& PERF_RECORD_GROUP
) {
1907 struct perf_counter
*leader
, *sub
;
1908 u64 nr
= counter
->nr_siblings
;
1910 perf_output_put(&handle
, nr
);
1912 leader
= counter
->group_leader
;
1913 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1915 sub
->hw_ops
->read(sub
);
1917 group_entry
.event
= sub
->hw_event
.config
;
1918 group_entry
.counter
= atomic64_read(&sub
->count
);
1920 perf_output_put(&handle
, group_entry
);
1925 perf_output_copy(&handle
, callchain
, callchain_size
);
1927 if (record_type
& PERF_RECORD_TIME
)
1928 perf_output_put(&handle
, time
);
1930 perf_output_end(&handle
);
1937 struct perf_mmap_event
{
1943 struct perf_event_header header
;
1953 static void perf_counter_mmap_output(struct perf_counter
*counter
,
1954 struct perf_mmap_event
*mmap_event
)
1956 struct perf_output_handle handle
;
1957 int size
= mmap_event
->event
.header
.size
;
1958 int ret
= perf_output_begin(&handle
, counter
, size
, 0);
1963 perf_output_put(&handle
, mmap_event
->event
);
1964 perf_output_copy(&handle
, mmap_event
->file_name
,
1965 mmap_event
->file_size
);
1966 perf_output_end(&handle
);
1969 static int perf_counter_mmap_match(struct perf_counter
*counter
,
1970 struct perf_mmap_event
*mmap_event
)
1972 if (counter
->hw_event
.mmap
&&
1973 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
1976 if (counter
->hw_event
.munmap
&&
1977 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
1983 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
1984 struct perf_mmap_event
*mmap_event
)
1986 struct perf_counter
*counter
;
1988 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
1992 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
1993 if (perf_counter_mmap_match(counter
, mmap_event
))
1994 perf_counter_mmap_output(counter
, mmap_event
);
1999 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2001 struct perf_cpu_context
*cpuctx
;
2002 struct file
*file
= mmap_event
->file
;
2009 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2011 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2014 name
= dentry_path(file
->f_dentry
, buf
, PATH_MAX
);
2016 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2020 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2025 size
= ALIGN(strlen(name
), sizeof(u64
));
2027 mmap_event
->file_name
= name
;
2028 mmap_event
->file_size
= size
;
2030 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2032 cpuctx
= &get_cpu_var(perf_cpu_context
);
2033 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2034 put_cpu_var(perf_cpu_context
);
2036 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2041 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2042 unsigned long pgoff
, struct file
*file
)
2044 struct perf_mmap_event mmap_event
= {
2047 .header
= { .type
= PERF_EVENT_MMAP
, },
2048 .pid
= current
->group_leader
->pid
,
2049 .tid
= current
->pid
,
2056 perf_counter_mmap_event(&mmap_event
);
2059 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2060 unsigned long pgoff
, struct file
*file
)
2062 struct perf_mmap_event mmap_event
= {
2065 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2066 .pid
= current
->group_leader
->pid
,
2067 .tid
= current
->pid
,
2074 perf_counter_mmap_event(&mmap_event
);
2078 * Generic counter overflow handling.
2081 int perf_counter_overflow(struct perf_counter
*counter
,
2082 int nmi
, struct pt_regs
*regs
)
2084 int events
= atomic_read(&counter
->event_limit
);
2087 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2090 counter
->pending_disable
= 1;
2091 perf_pending_queue(&counter
->pending
,
2092 perf_pending_counter
);
2094 perf_counter_disable(counter
);
2097 perf_counter_output(counter
, nmi
, regs
);
2102 * Generic software counter infrastructure
2105 static void perf_swcounter_update(struct perf_counter
*counter
)
2107 struct hw_perf_counter
*hwc
= &counter
->hw
;
2112 prev
= atomic64_read(&hwc
->prev_count
);
2113 now
= atomic64_read(&hwc
->count
);
2114 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2119 atomic64_add(delta
, &counter
->count
);
2120 atomic64_sub(delta
, &hwc
->period_left
);
2123 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2125 struct hw_perf_counter
*hwc
= &counter
->hw
;
2126 s64 left
= atomic64_read(&hwc
->period_left
);
2127 s64 period
= hwc
->irq_period
;
2129 if (unlikely(left
<= -period
)) {
2131 atomic64_set(&hwc
->period_left
, left
);
2134 if (unlikely(left
<= 0)) {
2136 atomic64_add(period
, &hwc
->period_left
);
2139 atomic64_set(&hwc
->prev_count
, -left
);
2140 atomic64_set(&hwc
->count
, -left
);
2143 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2145 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2146 struct perf_counter
*counter
;
2147 struct pt_regs
*regs
;
2149 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2150 counter
->hw_ops
->read(counter
);
2152 regs
= get_irq_regs();
2154 * In case we exclude kernel IPs or are somehow not in interrupt
2155 * context, provide the next best thing, the user IP.
2157 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2158 !counter
->hw_event
.exclude_user
)
2159 regs
= task_pt_regs(current
);
2162 if (perf_counter_overflow(counter
, 0, regs
))
2163 ret
= HRTIMER_NORESTART
;
2166 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
2171 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2172 int nmi
, struct pt_regs
*regs
)
2174 perf_swcounter_update(counter
);
2175 perf_swcounter_set_period(counter
);
2176 if (perf_counter_overflow(counter
, nmi
, regs
))
2177 /* soft-disable the counter */
2182 static int perf_swcounter_match(struct perf_counter
*counter
,
2183 enum perf_event_types type
,
2184 u32 event
, struct pt_regs
*regs
)
2186 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2189 if (perf_event_raw(&counter
->hw_event
))
2192 if (perf_event_type(&counter
->hw_event
) != type
)
2195 if (perf_event_id(&counter
->hw_event
) != event
)
2198 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2201 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2207 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2208 int nmi
, struct pt_regs
*regs
)
2210 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2211 if (counter
->hw
.irq_period
&& !neg
)
2212 perf_swcounter_overflow(counter
, nmi
, regs
);
2215 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2216 enum perf_event_types type
, u32 event
,
2217 u64 nr
, int nmi
, struct pt_regs
*regs
)
2219 struct perf_counter
*counter
;
2221 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2225 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2226 if (perf_swcounter_match(counter
, type
, event
, regs
))
2227 perf_swcounter_add(counter
, nr
, nmi
, regs
);
2232 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2235 return &cpuctx
->recursion
[3];
2238 return &cpuctx
->recursion
[2];
2241 return &cpuctx
->recursion
[1];
2243 return &cpuctx
->recursion
[0];
2246 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2247 u64 nr
, int nmi
, struct pt_regs
*regs
)
2249 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2250 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2258 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
, nr
, nmi
, regs
);
2259 if (cpuctx
->task_ctx
) {
2260 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2268 put_cpu_var(perf_cpu_context
);
2271 void perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
)
2273 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
);
2276 static void perf_swcounter_read(struct perf_counter
*counter
)
2278 perf_swcounter_update(counter
);
2281 static int perf_swcounter_enable(struct perf_counter
*counter
)
2283 perf_swcounter_set_period(counter
);
2287 static void perf_swcounter_disable(struct perf_counter
*counter
)
2289 perf_swcounter_update(counter
);
2292 static const struct hw_perf_counter_ops perf_ops_generic
= {
2293 .enable
= perf_swcounter_enable
,
2294 .disable
= perf_swcounter_disable
,
2295 .read
= perf_swcounter_read
,
2299 * Software counter: cpu wall time clock
2302 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2304 int cpu
= raw_smp_processor_id();
2308 now
= cpu_clock(cpu
);
2309 prev
= atomic64_read(&counter
->hw
.prev_count
);
2310 atomic64_set(&counter
->hw
.prev_count
, now
);
2311 atomic64_add(now
- prev
, &counter
->count
);
2314 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2316 struct hw_perf_counter
*hwc
= &counter
->hw
;
2317 int cpu
= raw_smp_processor_id();
2319 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2320 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2321 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2322 if (hwc
->irq_period
) {
2323 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2324 ns_to_ktime(hwc
->irq_period
), 0,
2325 HRTIMER_MODE_REL
, 0);
2331 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2333 hrtimer_cancel(&counter
->hw
.hrtimer
);
2334 cpu_clock_perf_counter_update(counter
);
2337 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2339 cpu_clock_perf_counter_update(counter
);
2342 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
2343 .enable
= cpu_clock_perf_counter_enable
,
2344 .disable
= cpu_clock_perf_counter_disable
,
2345 .read
= cpu_clock_perf_counter_read
,
2349 * Software counter: task time clock
2353 * Called from within the scheduler:
2355 static u64
task_clock_perf_counter_val(struct perf_counter
*counter
, int update
)
2357 struct task_struct
*curr
= counter
->task
;
2360 delta
= __task_delta_exec(curr
, update
);
2362 return curr
->se
.sum_exec_runtime
+ delta
;
2365 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2370 prev
= atomic64_read(&counter
->hw
.prev_count
);
2372 atomic64_set(&counter
->hw
.prev_count
, now
);
2376 atomic64_add(delta
, &counter
->count
);
2379 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2381 struct hw_perf_counter
*hwc
= &counter
->hw
;
2383 atomic64_set(&hwc
->prev_count
, task_clock_perf_counter_val(counter
, 0));
2384 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2385 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2386 if (hwc
->irq_period
) {
2387 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2388 ns_to_ktime(hwc
->irq_period
), 0,
2389 HRTIMER_MODE_REL
, 0);
2395 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2397 hrtimer_cancel(&counter
->hw
.hrtimer
);
2398 task_clock_perf_counter_update(counter
,
2399 task_clock_perf_counter_val(counter
, 0));
2402 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2404 task_clock_perf_counter_update(counter
,
2405 task_clock_perf_counter_val(counter
, 1));
2408 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
2409 .enable
= task_clock_perf_counter_enable
,
2410 .disable
= task_clock_perf_counter_disable
,
2411 .read
= task_clock_perf_counter_read
,
2415 * Software counter: cpu migrations
2418 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2420 struct task_struct
*curr
= counter
->ctx
->task
;
2423 return curr
->se
.nr_migrations
;
2424 return cpu_nr_migrations(smp_processor_id());
2427 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2432 prev
= atomic64_read(&counter
->hw
.prev_count
);
2433 now
= get_cpu_migrations(counter
);
2435 atomic64_set(&counter
->hw
.prev_count
, now
);
2439 atomic64_add(delta
, &counter
->count
);
2442 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2444 cpu_migrations_perf_counter_update(counter
);
2447 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2449 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2450 atomic64_set(&counter
->hw
.prev_count
,
2451 get_cpu_migrations(counter
));
2455 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2457 cpu_migrations_perf_counter_update(counter
);
2460 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
2461 .enable
= cpu_migrations_perf_counter_enable
,
2462 .disable
= cpu_migrations_perf_counter_disable
,
2463 .read
= cpu_migrations_perf_counter_read
,
2466 #ifdef CONFIG_EVENT_PROFILE
2467 void perf_tpcounter_event(int event_id
)
2469 struct pt_regs
*regs
= get_irq_regs();
2472 regs
= task_pt_regs(current
);
2474 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
);
2477 extern int ftrace_profile_enable(int);
2478 extern void ftrace_profile_disable(int);
2480 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2482 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2485 static const struct hw_perf_counter_ops
*
2486 tp_perf_counter_init(struct perf_counter
*counter
)
2488 int event_id
= perf_event_id(&counter
->hw_event
);
2491 ret
= ftrace_profile_enable(event_id
);
2495 counter
->destroy
= tp_perf_counter_destroy
;
2496 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2498 return &perf_ops_generic
;
2501 static const struct hw_perf_counter_ops
*
2502 tp_perf_counter_init(struct perf_counter
*counter
)
2508 static const struct hw_perf_counter_ops
*
2509 sw_perf_counter_init(struct perf_counter
*counter
)
2511 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2512 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
2513 struct hw_perf_counter
*hwc
= &counter
->hw
;
2516 * Software counters (currently) can't in general distinguish
2517 * between user, kernel and hypervisor events.
2518 * However, context switches and cpu migrations are considered
2519 * to be kernel events, and page faults are never hypervisor
2522 switch (perf_event_id(&counter
->hw_event
)) {
2523 case PERF_COUNT_CPU_CLOCK
:
2524 hw_ops
= &perf_ops_cpu_clock
;
2526 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2527 hw_event
->irq_period
= 10000;
2529 case PERF_COUNT_TASK_CLOCK
:
2531 * If the user instantiates this as a per-cpu counter,
2532 * use the cpu_clock counter instead.
2534 if (counter
->ctx
->task
)
2535 hw_ops
= &perf_ops_task_clock
;
2537 hw_ops
= &perf_ops_cpu_clock
;
2539 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2540 hw_event
->irq_period
= 10000;
2542 case PERF_COUNT_PAGE_FAULTS
:
2543 case PERF_COUNT_PAGE_FAULTS_MIN
:
2544 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2545 case PERF_COUNT_CONTEXT_SWITCHES
:
2546 hw_ops
= &perf_ops_generic
;
2548 case PERF_COUNT_CPU_MIGRATIONS
:
2549 if (!counter
->hw_event
.exclude_kernel
)
2550 hw_ops
= &perf_ops_cpu_migrations
;
2555 hwc
->irq_period
= hw_event
->irq_period
;
2561 * Allocate and initialize a counter structure
2563 static struct perf_counter
*
2564 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2566 struct perf_counter_context
*ctx
,
2567 struct perf_counter
*group_leader
,
2570 const struct hw_perf_counter_ops
*hw_ops
;
2571 struct perf_counter
*counter
;
2574 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2576 return ERR_PTR(-ENOMEM
);
2579 * Single counters are their own group leaders, with an
2580 * empty sibling list:
2583 group_leader
= counter
;
2585 mutex_init(&counter
->mutex
);
2586 INIT_LIST_HEAD(&counter
->list_entry
);
2587 INIT_LIST_HEAD(&counter
->event_entry
);
2588 INIT_LIST_HEAD(&counter
->sibling_list
);
2589 init_waitqueue_head(&counter
->waitq
);
2591 mutex_init(&counter
->mmap_mutex
);
2593 INIT_LIST_HEAD(&counter
->child_list
);
2596 counter
->hw_event
= *hw_event
;
2597 counter
->group_leader
= group_leader
;
2598 counter
->hw_ops
= NULL
;
2601 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2602 if (hw_event
->disabled
)
2603 counter
->state
= PERF_COUNTER_STATE_OFF
;
2607 if (perf_event_raw(hw_event
)) {
2608 hw_ops
= hw_perf_counter_init(counter
);
2612 switch (perf_event_type(hw_event
)) {
2613 case PERF_TYPE_HARDWARE
:
2614 hw_ops
= hw_perf_counter_init(counter
);
2617 case PERF_TYPE_SOFTWARE
:
2618 hw_ops
= sw_perf_counter_init(counter
);
2621 case PERF_TYPE_TRACEPOINT
:
2622 hw_ops
= tp_perf_counter_init(counter
);
2629 else if (IS_ERR(hw_ops
))
2630 err
= PTR_ERR(hw_ops
);
2634 return ERR_PTR(err
);
2637 counter
->hw_ops
= hw_ops
;
2643 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2645 * @hw_event_uptr: event type attributes for monitoring/sampling
2648 * @group_fd: group leader counter fd
2650 SYSCALL_DEFINE5(perf_counter_open
,
2651 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2652 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2654 struct perf_counter
*counter
, *group_leader
;
2655 struct perf_counter_hw_event hw_event
;
2656 struct perf_counter_context
*ctx
;
2657 struct file
*counter_file
= NULL
;
2658 struct file
*group_file
= NULL
;
2659 int fput_needed
= 0;
2660 int fput_needed2
= 0;
2663 /* for future expandability... */
2667 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2671 * Get the target context (task or percpu):
2673 ctx
= find_get_context(pid
, cpu
);
2675 return PTR_ERR(ctx
);
2678 * Look up the group leader (we will attach this counter to it):
2680 group_leader
= NULL
;
2681 if (group_fd
!= -1) {
2683 group_file
= fget_light(group_fd
, &fput_needed
);
2685 goto err_put_context
;
2686 if (group_file
->f_op
!= &perf_fops
)
2687 goto err_put_context
;
2689 group_leader
= group_file
->private_data
;
2691 * Do not allow a recursive hierarchy (this new sibling
2692 * becoming part of another group-sibling):
2694 if (group_leader
->group_leader
!= group_leader
)
2695 goto err_put_context
;
2697 * Do not allow to attach to a group in a different
2698 * task or CPU context:
2700 if (group_leader
->ctx
!= ctx
)
2701 goto err_put_context
;
2703 * Only a group leader can be exclusive or pinned
2705 if (hw_event
.exclusive
|| hw_event
.pinned
)
2706 goto err_put_context
;
2709 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2711 ret
= PTR_ERR(counter
);
2712 if (IS_ERR(counter
))
2713 goto err_put_context
;
2715 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2717 goto err_free_put_context
;
2719 counter_file
= fget_light(ret
, &fput_needed2
);
2721 goto err_free_put_context
;
2723 counter
->filp
= counter_file
;
2724 mutex_lock(&ctx
->mutex
);
2725 perf_install_in_context(ctx
, counter
, cpu
);
2726 mutex_unlock(&ctx
->mutex
);
2728 fput_light(counter_file
, fput_needed2
);
2731 fput_light(group_file
, fput_needed
);
2735 err_free_put_context
:
2745 * Initialize the perf_counter context in a task_struct:
2748 __perf_counter_init_context(struct perf_counter_context
*ctx
,
2749 struct task_struct
*task
)
2751 memset(ctx
, 0, sizeof(*ctx
));
2752 spin_lock_init(&ctx
->lock
);
2753 mutex_init(&ctx
->mutex
);
2754 INIT_LIST_HEAD(&ctx
->counter_list
);
2755 INIT_LIST_HEAD(&ctx
->event_list
);
2760 * inherit a counter from parent task to child task:
2762 static struct perf_counter
*
2763 inherit_counter(struct perf_counter
*parent_counter
,
2764 struct task_struct
*parent
,
2765 struct perf_counter_context
*parent_ctx
,
2766 struct task_struct
*child
,
2767 struct perf_counter
*group_leader
,
2768 struct perf_counter_context
*child_ctx
)
2770 struct perf_counter
*child_counter
;
2773 * Instead of creating recursive hierarchies of counters,
2774 * we link inherited counters back to the original parent,
2775 * which has a filp for sure, which we use as the reference
2778 if (parent_counter
->parent
)
2779 parent_counter
= parent_counter
->parent
;
2781 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2782 parent_counter
->cpu
, child_ctx
,
2783 group_leader
, GFP_KERNEL
);
2784 if (IS_ERR(child_counter
))
2785 return child_counter
;
2788 * Link it up in the child's context:
2790 child_counter
->task
= child
;
2791 add_counter_to_ctx(child_counter
, child_ctx
);
2793 child_counter
->parent
= parent_counter
;
2795 * inherit into child's child as well:
2797 child_counter
->hw_event
.inherit
= 1;
2800 * Get a reference to the parent filp - we will fput it
2801 * when the child counter exits. This is safe to do because
2802 * we are in the parent and we know that the filp still
2803 * exists and has a nonzero count:
2805 atomic_long_inc(&parent_counter
->filp
->f_count
);
2808 * Link this into the parent counter's child list
2810 mutex_lock(&parent_counter
->mutex
);
2811 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
2814 * Make the child state follow the state of the parent counter,
2815 * not its hw_event.disabled bit. We hold the parent's mutex,
2816 * so we won't race with perf_counter_{en,dis}able_family.
2818 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
2819 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2821 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
2823 mutex_unlock(&parent_counter
->mutex
);
2825 return child_counter
;
2828 static int inherit_group(struct perf_counter
*parent_counter
,
2829 struct task_struct
*parent
,
2830 struct perf_counter_context
*parent_ctx
,
2831 struct task_struct
*child
,
2832 struct perf_counter_context
*child_ctx
)
2834 struct perf_counter
*leader
;
2835 struct perf_counter
*sub
;
2836 struct perf_counter
*child_ctr
;
2838 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
2839 child
, NULL
, child_ctx
);
2841 return PTR_ERR(leader
);
2842 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
2843 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
2844 child
, leader
, child_ctx
);
2845 if (IS_ERR(child_ctr
))
2846 return PTR_ERR(child_ctr
);
2851 static void sync_child_counter(struct perf_counter
*child_counter
,
2852 struct perf_counter
*parent_counter
)
2854 u64 parent_val
, child_val
;
2856 parent_val
= atomic64_read(&parent_counter
->count
);
2857 child_val
= atomic64_read(&child_counter
->count
);
2860 * Add back the child's count to the parent's count:
2862 atomic64_add(child_val
, &parent_counter
->count
);
2863 atomic64_add(child_counter
->total_time_enabled
,
2864 &parent_counter
->child_total_time_enabled
);
2865 atomic64_add(child_counter
->total_time_running
,
2866 &parent_counter
->child_total_time_running
);
2869 * Remove this counter from the parent's list
2871 mutex_lock(&parent_counter
->mutex
);
2872 list_del_init(&child_counter
->child_list
);
2873 mutex_unlock(&parent_counter
->mutex
);
2876 * Release the parent counter, if this was the last
2879 fput(parent_counter
->filp
);
2883 __perf_counter_exit_task(struct task_struct
*child
,
2884 struct perf_counter
*child_counter
,
2885 struct perf_counter_context
*child_ctx
)
2887 struct perf_counter
*parent_counter
;
2888 struct perf_counter
*sub
, *tmp
;
2891 * If we do not self-reap then we have to wait for the
2892 * child task to unschedule (it will happen for sure),
2893 * so that its counter is at its final count. (This
2894 * condition triggers rarely - child tasks usually get
2895 * off their CPU before the parent has a chance to
2896 * get this far into the reaping action)
2898 if (child
!= current
) {
2899 wait_task_inactive(child
, 0);
2900 list_del_init(&child_counter
->list_entry
);
2901 update_counter_times(child_counter
);
2903 struct perf_cpu_context
*cpuctx
;
2904 unsigned long flags
;
2908 * Disable and unlink this counter.
2910 * Be careful about zapping the list - IRQ/NMI context
2911 * could still be processing it:
2913 curr_rq_lock_irq_save(&flags
);
2914 perf_flags
= hw_perf_save_disable();
2916 cpuctx
= &__get_cpu_var(perf_cpu_context
);
2918 group_sched_out(child_counter
, cpuctx
, child_ctx
);
2919 update_counter_times(child_counter
);
2921 list_del_init(&child_counter
->list_entry
);
2923 child_ctx
->nr_counters
--;
2925 hw_perf_restore(perf_flags
);
2926 curr_rq_unlock_irq_restore(&flags
);
2929 parent_counter
= child_counter
->parent
;
2931 * It can happen that parent exits first, and has counters
2932 * that are still around due to the child reference. These
2933 * counters need to be zapped - but otherwise linger.
2935 if (parent_counter
) {
2936 sync_child_counter(child_counter
, parent_counter
);
2937 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
2940 sync_child_counter(sub
, sub
->parent
);
2944 free_counter(child_counter
);
2949 * When a child task exits, feed back counter values to parent counters.
2951 * Note: we may be running in child context, but the PID is not hashed
2952 * anymore so new counters will not be added.
2954 void perf_counter_exit_task(struct task_struct
*child
)
2956 struct perf_counter
*child_counter
, *tmp
;
2957 struct perf_counter_context
*child_ctx
;
2959 child_ctx
= &child
->perf_counter_ctx
;
2961 if (likely(!child_ctx
->nr_counters
))
2964 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
2966 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
2970 * Initialize the perf_counter context in task_struct
2972 void perf_counter_init_task(struct task_struct
*child
)
2974 struct perf_counter_context
*child_ctx
, *parent_ctx
;
2975 struct perf_counter
*counter
;
2976 struct task_struct
*parent
= current
;
2978 child_ctx
= &child
->perf_counter_ctx
;
2979 parent_ctx
= &parent
->perf_counter_ctx
;
2981 __perf_counter_init_context(child_ctx
, child
);
2984 * This is executed from the parent task context, so inherit
2985 * counters that have been marked for cloning:
2988 if (likely(!parent_ctx
->nr_counters
))
2992 * Lock the parent list. No need to lock the child - not PID
2993 * hashed yet and not running, so nobody can access it.
2995 mutex_lock(&parent_ctx
->mutex
);
2998 * We dont have to disable NMIs - we are only looking at
2999 * the list, not manipulating it:
3001 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
3002 if (!counter
->hw_event
.inherit
)
3005 if (inherit_group(counter
, parent
,
3006 parent_ctx
, child
, child_ctx
))
3010 mutex_unlock(&parent_ctx
->mutex
);
3013 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3015 struct perf_cpu_context
*cpuctx
;
3017 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3018 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3020 mutex_lock(&perf_resource_mutex
);
3021 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3022 mutex_unlock(&perf_resource_mutex
);
3024 hw_perf_counter_setup(cpu
);
3027 #ifdef CONFIG_HOTPLUG_CPU
3028 static void __perf_counter_exit_cpu(void *info
)
3030 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3031 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3032 struct perf_counter
*counter
, *tmp
;
3034 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3035 __perf_counter_remove_from_context(counter
);
3037 static void perf_counter_exit_cpu(int cpu
)
3039 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3040 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3042 mutex_lock(&ctx
->mutex
);
3043 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3044 mutex_unlock(&ctx
->mutex
);
3047 static inline void perf_counter_exit_cpu(int cpu
) { }
3050 static int __cpuinit
3051 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3053 unsigned int cpu
= (long)hcpu
;
3057 case CPU_UP_PREPARE
:
3058 case CPU_UP_PREPARE_FROZEN
:
3059 perf_counter_init_cpu(cpu
);
3062 case CPU_DOWN_PREPARE
:
3063 case CPU_DOWN_PREPARE_FROZEN
:
3064 perf_counter_exit_cpu(cpu
);
3074 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3075 .notifier_call
= perf_cpu_notify
,
3078 static int __init
perf_counter_init(void)
3080 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3081 (void *)(long)smp_processor_id());
3082 register_cpu_notifier(&perf_cpu_nb
);
3086 early_initcall(perf_counter_init
);
3088 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3090 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3094 perf_set_reserve_percpu(struct sysdev_class
*class,
3098 struct perf_cpu_context
*cpuctx
;
3102 err
= strict_strtoul(buf
, 10, &val
);
3105 if (val
> perf_max_counters
)
3108 mutex_lock(&perf_resource_mutex
);
3109 perf_reserved_percpu
= val
;
3110 for_each_online_cpu(cpu
) {
3111 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3112 spin_lock_irq(&cpuctx
->ctx
.lock
);
3113 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3114 perf_max_counters
- perf_reserved_percpu
);
3115 cpuctx
->max_pertask
= mpt
;
3116 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3118 mutex_unlock(&perf_resource_mutex
);
3123 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3125 return sprintf(buf
, "%d\n", perf_overcommit
);
3129 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3134 err
= strict_strtoul(buf
, 10, &val
);
3140 mutex_lock(&perf_resource_mutex
);
3141 perf_overcommit
= val
;
3142 mutex_unlock(&perf_resource_mutex
);
3147 static SYSDEV_CLASS_ATTR(
3150 perf_show_reserve_percpu
,
3151 perf_set_reserve_percpu
3154 static SYSDEV_CLASS_ATTR(
3157 perf_show_overcommit
,
3161 static struct attribute
*perfclass_attrs
[] = {
3162 &attr_reserve_percpu
.attr
,
3163 &attr_overcommit
.attr
,
3167 static struct attribute_group perfclass_attr_group
= {
3168 .attrs
= perfclass_attrs
,
3169 .name
= "perf_counters",
3172 static int __init
perf_counter_sysfs_init(void)
3174 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3175 &perfclass_attr_group
);
3177 device_initcall(perf_counter_sysfs_init
);