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
12 #include <linux/cpu.h>
13 #include <linux/smp.h>
14 #include <linux/file.h>
15 #include <linux/poll.h>
16 #include <linux/sysfs.h>
17 #include <linux/ptrace.h>
18 #include <linux/percpu.h>
19 #include <linux/uaccess.h>
20 #include <linux/syscalls.h>
21 #include <linux/anon_inodes.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/perf_counter.h>
25 #include <linux/vmstat.h>
26 #include <linux/rculist.h>
27 #include <linux/hardirq.h>
29 #include <asm/irq_regs.h>
32 * Each CPU has a list of per CPU counters:
34 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
36 int perf_max_counters __read_mostly
= 1;
37 static int perf_reserved_percpu __read_mostly
;
38 static int perf_overcommit __read_mostly
= 1;
41 * Mutex for (sysadmin-configurable) counter reservations:
43 static DEFINE_MUTEX(perf_resource_mutex
);
46 * Architecture provided APIs - weak aliases:
48 extern __weak
const struct hw_perf_counter_ops
*
49 hw_perf_counter_init(struct perf_counter
*counter
)
54 u64 __weak
hw_perf_save_disable(void) { return 0; }
55 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
56 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
57 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
58 struct perf_cpu_context
*cpuctx
,
59 struct perf_counter_context
*ctx
, int cpu
)
64 void __weak
perf_counter_print_debug(void) { }
67 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
69 struct perf_counter
*group_leader
= counter
->group_leader
;
72 * Depending on whether it is a standalone or sibling counter,
73 * add it straight to the context's counter list, or to the group
74 * leader's sibling list:
76 if (counter
->group_leader
== counter
)
77 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
79 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
81 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
85 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
87 struct perf_counter
*sibling
, *tmp
;
89 list_del_init(&counter
->list_entry
);
90 list_del_rcu(&counter
->event_entry
);
93 * If this was a group counter with sibling counters then
94 * upgrade the siblings to singleton counters by adding them
95 * to the context list directly:
97 list_for_each_entry_safe(sibling
, tmp
,
98 &counter
->sibling_list
, list_entry
) {
100 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
101 sibling
->group_leader
= sibling
;
106 counter_sched_out(struct perf_counter
*counter
,
107 struct perf_cpu_context
*cpuctx
,
108 struct perf_counter_context
*ctx
)
110 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
113 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
114 counter
->hw_ops
->disable(counter
);
117 if (!is_software_counter(counter
))
118 cpuctx
->active_oncpu
--;
120 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
121 cpuctx
->exclusive
= 0;
125 group_sched_out(struct perf_counter
*group_counter
,
126 struct perf_cpu_context
*cpuctx
,
127 struct perf_counter_context
*ctx
)
129 struct perf_counter
*counter
;
131 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
134 counter_sched_out(group_counter
, cpuctx
, ctx
);
137 * Schedule out siblings (if any):
139 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
140 counter_sched_out(counter
, cpuctx
, ctx
);
142 if (group_counter
->hw_event
.exclusive
)
143 cpuctx
->exclusive
= 0;
147 * Cross CPU call to remove a performance counter
149 * We disable the counter on the hardware level first. After that we
150 * remove it from the context list.
152 static void __perf_counter_remove_from_context(void *info
)
154 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
155 struct perf_counter
*counter
= info
;
156 struct perf_counter_context
*ctx
= counter
->ctx
;
161 * If this is a task context, we need to check whether it is
162 * the current task context of this cpu. If not it has been
163 * scheduled out before the smp call arrived.
165 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
168 curr_rq_lock_irq_save(&flags
);
169 spin_lock(&ctx
->lock
);
171 counter_sched_out(counter
, cpuctx
, ctx
);
173 counter
->task
= NULL
;
177 * Protect the list operation against NMI by disabling the
178 * counters on a global level. NOP for non NMI based counters.
180 perf_flags
= hw_perf_save_disable();
181 list_del_counter(counter
, ctx
);
182 hw_perf_restore(perf_flags
);
186 * Allow more per task counters with respect to the
189 cpuctx
->max_pertask
=
190 min(perf_max_counters
- ctx
->nr_counters
,
191 perf_max_counters
- perf_reserved_percpu
);
194 spin_unlock(&ctx
->lock
);
195 curr_rq_unlock_irq_restore(&flags
);
200 * Remove the counter from a task's (or a CPU's) list of counters.
202 * Must be called with counter->mutex and ctx->mutex held.
204 * CPU counters are removed with a smp call. For task counters we only
205 * call when the task is on a CPU.
207 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
209 struct perf_counter_context
*ctx
= counter
->ctx
;
210 struct task_struct
*task
= ctx
->task
;
214 * Per cpu counters are removed via an smp call and
215 * the removal is always sucessful.
217 smp_call_function_single(counter
->cpu
,
218 __perf_counter_remove_from_context
,
224 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
227 spin_lock_irq(&ctx
->lock
);
229 * If the context is active we need to retry the smp call.
231 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
232 spin_unlock_irq(&ctx
->lock
);
237 * The lock prevents that this context is scheduled in so we
238 * can remove the counter safely, if the call above did not
241 if (!list_empty(&counter
->list_entry
)) {
243 list_del_counter(counter
, ctx
);
244 counter
->task
= NULL
;
246 spin_unlock_irq(&ctx
->lock
);
250 * Cross CPU call to disable a performance counter
252 static void __perf_counter_disable(void *info
)
254 struct perf_counter
*counter
= info
;
255 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
256 struct perf_counter_context
*ctx
= counter
->ctx
;
260 * If this is a per-task counter, need to check whether this
261 * counter's task is the current task on this cpu.
263 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
266 curr_rq_lock_irq_save(&flags
);
267 spin_lock(&ctx
->lock
);
270 * If the counter is on, turn it off.
271 * If it is in error state, leave it in error state.
273 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
274 if (counter
== counter
->group_leader
)
275 group_sched_out(counter
, cpuctx
, ctx
);
277 counter_sched_out(counter
, cpuctx
, ctx
);
278 counter
->state
= PERF_COUNTER_STATE_OFF
;
281 spin_unlock(&ctx
->lock
);
282 curr_rq_unlock_irq_restore(&flags
);
288 static void perf_counter_disable(struct perf_counter
*counter
)
290 struct perf_counter_context
*ctx
= counter
->ctx
;
291 struct task_struct
*task
= ctx
->task
;
295 * Disable the counter on the cpu that it's on
297 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
303 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
305 spin_lock_irq(&ctx
->lock
);
307 * If the counter is still active, we need to retry the cross-call.
309 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
310 spin_unlock_irq(&ctx
->lock
);
315 * Since we have the lock this context can't be scheduled
316 * in, so we can change the state safely.
318 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
319 counter
->state
= PERF_COUNTER_STATE_OFF
;
321 spin_unlock_irq(&ctx
->lock
);
325 * Disable a counter and all its children.
327 static void perf_counter_disable_family(struct perf_counter
*counter
)
329 struct perf_counter
*child
;
331 perf_counter_disable(counter
);
334 * Lock the mutex to protect the list of children
336 mutex_lock(&counter
->mutex
);
337 list_for_each_entry(child
, &counter
->child_list
, child_list
)
338 perf_counter_disable(child
);
339 mutex_unlock(&counter
->mutex
);
343 counter_sched_in(struct perf_counter
*counter
,
344 struct perf_cpu_context
*cpuctx
,
345 struct perf_counter_context
*ctx
,
348 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
351 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
352 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
354 * The new state must be visible before we turn it on in the hardware:
358 if (counter
->hw_ops
->enable(counter
)) {
359 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
364 if (!is_software_counter(counter
))
365 cpuctx
->active_oncpu
++;
368 if (counter
->hw_event
.exclusive
)
369 cpuctx
->exclusive
= 1;
375 * Return 1 for a group consisting entirely of software counters,
376 * 0 if the group contains any hardware counters.
378 static int is_software_only_group(struct perf_counter
*leader
)
380 struct perf_counter
*counter
;
382 if (!is_software_counter(leader
))
384 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
385 if (!is_software_counter(counter
))
391 * Work out whether we can put this counter group on the CPU now.
393 static int group_can_go_on(struct perf_counter
*counter
,
394 struct perf_cpu_context
*cpuctx
,
398 * Groups consisting entirely of software counters can always go on.
400 if (is_software_only_group(counter
))
403 * If an exclusive group is already on, no other hardware
404 * counters can go on.
406 if (cpuctx
->exclusive
)
409 * If this group is exclusive and there are already
410 * counters on the CPU, it can't go on.
412 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
415 * Otherwise, try to add it if all previous groups were able
422 * Cross CPU call to install and enable a performance counter
424 static void __perf_install_in_context(void *info
)
426 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
427 struct perf_counter
*counter
= info
;
428 struct perf_counter_context
*ctx
= counter
->ctx
;
429 struct perf_counter
*leader
= counter
->group_leader
;
430 int cpu
= smp_processor_id();
436 * If this is a task context, we need to check whether it is
437 * the current task context of this cpu. If not it has been
438 * scheduled out before the smp call arrived.
440 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
443 curr_rq_lock_irq_save(&flags
);
444 spin_lock(&ctx
->lock
);
447 * Protect the list operation against NMI by disabling the
448 * counters on a global level. NOP for non NMI based counters.
450 perf_flags
= hw_perf_save_disable();
452 list_add_counter(counter
, ctx
);
454 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
457 * Don't put the counter on if it is disabled or if
458 * it is in a group and the group isn't on.
460 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
461 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
465 * An exclusive counter can't go on if there are already active
466 * hardware counters, and no hardware counter can go on if there
467 * is already an exclusive counter on.
469 if (!group_can_go_on(counter
, cpuctx
, 1))
472 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
476 * This counter couldn't go on. If it is in a group
477 * then we have to pull the whole group off.
478 * If the counter group is pinned then put it in error state.
480 if (leader
!= counter
)
481 group_sched_out(leader
, cpuctx
, ctx
);
482 if (leader
->hw_event
.pinned
)
483 leader
->state
= PERF_COUNTER_STATE_ERROR
;
486 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
487 cpuctx
->max_pertask
--;
490 hw_perf_restore(perf_flags
);
492 spin_unlock(&ctx
->lock
);
493 curr_rq_unlock_irq_restore(&flags
);
497 * Attach a performance counter to a context
499 * First we add the counter to the list with the hardware enable bit
500 * in counter->hw_config cleared.
502 * If the counter is attached to a task which is on a CPU we use a smp
503 * call to enable it in the task context. The task might have been
504 * scheduled away, but we check this in the smp call again.
506 * Must be called with ctx->mutex held.
509 perf_install_in_context(struct perf_counter_context
*ctx
,
510 struct perf_counter
*counter
,
513 struct task_struct
*task
= ctx
->task
;
517 * Per cpu counters are installed via an smp call and
518 * the install is always sucessful.
520 smp_call_function_single(cpu
, __perf_install_in_context
,
525 counter
->task
= task
;
527 task_oncpu_function_call(task
, __perf_install_in_context
,
530 spin_lock_irq(&ctx
->lock
);
532 * we need to retry the smp call.
534 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
535 spin_unlock_irq(&ctx
->lock
);
540 * The lock prevents that this context is scheduled in so we
541 * can add the counter safely, if it the call above did not
544 if (list_empty(&counter
->list_entry
)) {
545 list_add_counter(counter
, ctx
);
548 spin_unlock_irq(&ctx
->lock
);
552 * Cross CPU call to enable a performance counter
554 static void __perf_counter_enable(void *info
)
556 struct perf_counter
*counter
= info
;
557 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
558 struct perf_counter_context
*ctx
= counter
->ctx
;
559 struct perf_counter
*leader
= counter
->group_leader
;
564 * If this is a per-task counter, need to check whether this
565 * counter's task is the current task on this cpu.
567 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
570 curr_rq_lock_irq_save(&flags
);
571 spin_lock(&ctx
->lock
);
573 counter
->prev_state
= counter
->state
;
574 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
576 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
579 * If the counter is in a group and isn't the group leader,
580 * then don't put it on unless the group is on.
582 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
585 if (!group_can_go_on(counter
, cpuctx
, 1))
588 err
= counter_sched_in(counter
, cpuctx
, ctx
,
593 * If this counter can't go on and it's part of a
594 * group, then the whole group has to come off.
596 if (leader
!= counter
)
597 group_sched_out(leader
, cpuctx
, ctx
);
598 if (leader
->hw_event
.pinned
)
599 leader
->state
= PERF_COUNTER_STATE_ERROR
;
603 spin_unlock(&ctx
->lock
);
604 curr_rq_unlock_irq_restore(&flags
);
610 static void perf_counter_enable(struct perf_counter
*counter
)
612 struct perf_counter_context
*ctx
= counter
->ctx
;
613 struct task_struct
*task
= ctx
->task
;
617 * Enable the counter on the cpu that it's on
619 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
624 spin_lock_irq(&ctx
->lock
);
625 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
629 * If the counter is in error state, clear that first.
630 * That way, if we see the counter in error state below, we
631 * know that it has gone back into error state, as distinct
632 * from the task having been scheduled away before the
633 * cross-call arrived.
635 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
636 counter
->state
= PERF_COUNTER_STATE_OFF
;
639 spin_unlock_irq(&ctx
->lock
);
640 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
642 spin_lock_irq(&ctx
->lock
);
645 * If the context is active and the counter is still off,
646 * we need to retry the cross-call.
648 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
652 * Since we have the lock this context can't be scheduled
653 * in, so we can change the state safely.
655 if (counter
->state
== PERF_COUNTER_STATE_OFF
)
656 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
658 spin_unlock_irq(&ctx
->lock
);
662 * Enable a counter and all its children.
664 static void perf_counter_enable_family(struct perf_counter
*counter
)
666 struct perf_counter
*child
;
668 perf_counter_enable(counter
);
671 * Lock the mutex to protect the list of children
673 mutex_lock(&counter
->mutex
);
674 list_for_each_entry(child
, &counter
->child_list
, child_list
)
675 perf_counter_enable(child
);
676 mutex_unlock(&counter
->mutex
);
679 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
680 struct perf_cpu_context
*cpuctx
)
682 struct perf_counter
*counter
;
685 spin_lock(&ctx
->lock
);
687 if (likely(!ctx
->nr_counters
))
690 flags
= hw_perf_save_disable();
691 if (ctx
->nr_active
) {
692 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
693 group_sched_out(counter
, cpuctx
, ctx
);
695 hw_perf_restore(flags
);
697 spin_unlock(&ctx
->lock
);
701 * Called from scheduler to remove the counters of the current task,
702 * with interrupts disabled.
704 * We stop each counter and update the counter value in counter->count.
706 * This does not protect us against NMI, but disable()
707 * sets the disabled bit in the control field of counter _before_
708 * accessing the counter control register. If a NMI hits, then it will
709 * not restart the counter.
711 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
713 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
714 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
715 struct pt_regs
*regs
;
717 if (likely(!cpuctx
->task_ctx
))
720 regs
= task_pt_regs(task
);
721 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
);
722 __perf_counter_sched_out(ctx
, cpuctx
);
724 cpuctx
->task_ctx
= NULL
;
727 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
729 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
733 group_sched_in(struct perf_counter
*group_counter
,
734 struct perf_cpu_context
*cpuctx
,
735 struct perf_counter_context
*ctx
,
738 struct perf_counter
*counter
, *partial_group
;
741 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
744 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
746 return ret
< 0 ? ret
: 0;
748 group_counter
->prev_state
= group_counter
->state
;
749 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
753 * Schedule in siblings as one group (if any):
755 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
756 counter
->prev_state
= counter
->state
;
757 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
758 partial_group
= counter
;
767 * Groups can be scheduled in as one unit only, so undo any
768 * partial group before returning:
770 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
771 if (counter
== partial_group
)
773 counter_sched_out(counter
, cpuctx
, ctx
);
775 counter_sched_out(group_counter
, cpuctx
, ctx
);
781 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
782 struct perf_cpu_context
*cpuctx
, int cpu
)
784 struct perf_counter
*counter
;
788 spin_lock(&ctx
->lock
);
790 if (likely(!ctx
->nr_counters
))
793 flags
= hw_perf_save_disable();
796 * First go through the list and put on any pinned groups
797 * in order to give them the best chance of going on.
799 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
800 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
801 !counter
->hw_event
.pinned
)
803 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
806 if (group_can_go_on(counter
, cpuctx
, 1))
807 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
810 * If this pinned group hasn't been scheduled,
811 * put it in error state.
813 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
814 counter
->state
= PERF_COUNTER_STATE_ERROR
;
817 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
819 * Ignore counters in OFF or ERROR state, and
820 * ignore pinned counters since we did them already.
822 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
823 counter
->hw_event
.pinned
)
827 * Listen to the 'cpu' scheduling filter constraint
830 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
833 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
834 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
838 hw_perf_restore(flags
);
840 spin_unlock(&ctx
->lock
);
844 * Called from scheduler to add the counters of the current task
845 * with interrupts disabled.
847 * We restore the counter value and then enable it.
849 * This does not protect us against NMI, but enable()
850 * sets the enabled bit in the control field of counter _before_
851 * accessing the counter control register. If a NMI hits, then it will
852 * keep the counter running.
854 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
856 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
857 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
859 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
860 cpuctx
->task_ctx
= ctx
;
863 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
865 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
867 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
870 int perf_counter_task_disable(void)
872 struct task_struct
*curr
= current
;
873 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
874 struct perf_counter
*counter
;
879 if (likely(!ctx
->nr_counters
))
882 curr_rq_lock_irq_save(&flags
);
883 cpu
= smp_processor_id();
885 /* force the update of the task clock: */
886 __task_delta_exec(curr
, 1);
888 perf_counter_task_sched_out(curr
, cpu
);
890 spin_lock(&ctx
->lock
);
893 * Disable all the counters:
895 perf_flags
= hw_perf_save_disable();
897 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
898 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
)
899 counter
->state
= PERF_COUNTER_STATE_OFF
;
902 hw_perf_restore(perf_flags
);
904 spin_unlock(&ctx
->lock
);
906 curr_rq_unlock_irq_restore(&flags
);
911 int perf_counter_task_enable(void)
913 struct task_struct
*curr
= current
;
914 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
915 struct perf_counter
*counter
;
920 if (likely(!ctx
->nr_counters
))
923 curr_rq_lock_irq_save(&flags
);
924 cpu
= smp_processor_id();
926 /* force the update of the task clock: */
927 __task_delta_exec(curr
, 1);
929 perf_counter_task_sched_out(curr
, cpu
);
931 spin_lock(&ctx
->lock
);
934 * Disable all the counters:
936 perf_flags
= hw_perf_save_disable();
938 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
939 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
941 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
942 counter
->hw_event
.disabled
= 0;
944 hw_perf_restore(perf_flags
);
946 spin_unlock(&ctx
->lock
);
948 perf_counter_task_sched_in(curr
, cpu
);
950 curr_rq_unlock_irq_restore(&flags
);
956 * Round-robin a context's counters:
958 static void rotate_ctx(struct perf_counter_context
*ctx
)
960 struct perf_counter
*counter
;
963 if (!ctx
->nr_counters
)
966 spin_lock(&ctx
->lock
);
968 * Rotate the first entry last (works just fine for group counters too):
970 perf_flags
= hw_perf_save_disable();
971 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
972 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
975 hw_perf_restore(perf_flags
);
977 spin_unlock(&ctx
->lock
);
980 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
982 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
983 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
984 const int rotate_percpu
= 0;
987 perf_counter_cpu_sched_out(cpuctx
);
988 perf_counter_task_sched_out(curr
, cpu
);
991 rotate_ctx(&cpuctx
->ctx
);
995 perf_counter_cpu_sched_in(cpuctx
, cpu
);
996 perf_counter_task_sched_in(curr
, cpu
);
1000 * Cross CPU call to read the hardware counter
1002 static void __read(void *info
)
1004 struct perf_counter
*counter
= info
;
1005 unsigned long flags
;
1007 curr_rq_lock_irq_save(&flags
);
1008 counter
->hw_ops
->read(counter
);
1009 curr_rq_unlock_irq_restore(&flags
);
1012 static u64
perf_counter_read(struct perf_counter
*counter
)
1015 * If counter is enabled and currently active on a CPU, update the
1016 * value in the counter structure:
1018 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1019 smp_call_function_single(counter
->oncpu
,
1020 __read
, counter
, 1);
1023 return atomic64_read(&counter
->count
);
1026 static void put_context(struct perf_counter_context
*ctx
)
1029 put_task_struct(ctx
->task
);
1032 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1034 struct perf_cpu_context
*cpuctx
;
1035 struct perf_counter_context
*ctx
;
1036 struct task_struct
*task
;
1039 * If cpu is not a wildcard then this is a percpu counter:
1042 /* Must be root to operate on a CPU counter: */
1043 if (!capable(CAP_SYS_ADMIN
))
1044 return ERR_PTR(-EACCES
);
1046 if (cpu
< 0 || cpu
> num_possible_cpus())
1047 return ERR_PTR(-EINVAL
);
1050 * We could be clever and allow to attach a counter to an
1051 * offline CPU and activate it when the CPU comes up, but
1054 if (!cpu_isset(cpu
, cpu_online_map
))
1055 return ERR_PTR(-ENODEV
);
1057 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1067 task
= find_task_by_vpid(pid
);
1069 get_task_struct(task
);
1073 return ERR_PTR(-ESRCH
);
1075 ctx
= &task
->perf_counter_ctx
;
1078 /* Reuse ptrace permission checks for now. */
1079 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1081 return ERR_PTR(-EACCES
);
1087 static void free_counter_rcu(struct rcu_head
*head
)
1089 struct perf_counter
*counter
;
1091 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1095 static void free_counter(struct perf_counter
*counter
)
1097 if (counter
->destroy
)
1098 counter
->destroy(counter
);
1100 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1104 * Called when the last reference to the file is gone.
1106 static int perf_release(struct inode
*inode
, struct file
*file
)
1108 struct perf_counter
*counter
= file
->private_data
;
1109 struct perf_counter_context
*ctx
= counter
->ctx
;
1111 file
->private_data
= NULL
;
1113 mutex_lock(&ctx
->mutex
);
1114 mutex_lock(&counter
->mutex
);
1116 perf_counter_remove_from_context(counter
);
1118 mutex_unlock(&counter
->mutex
);
1119 mutex_unlock(&ctx
->mutex
);
1121 free_counter(counter
);
1128 * Read the performance counter - simple non blocking version for now
1131 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1135 if (count
< sizeof(cntval
))
1139 * Return end-of-file for a read on a counter that is in
1140 * error state (i.e. because it was pinned but it couldn't be
1141 * scheduled on to the CPU at some point).
1143 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1146 mutex_lock(&counter
->mutex
);
1147 cntval
= perf_counter_read(counter
);
1148 mutex_unlock(&counter
->mutex
);
1150 return put_user(cntval
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(cntval
);
1154 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1156 struct perf_counter
*counter
= file
->private_data
;
1158 return perf_read_hw(counter
, buf
, count
);
1161 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1163 struct perf_counter
*counter
= file
->private_data
;
1164 unsigned int events
= POLLIN
;
1166 poll_wait(file
, &counter
->waitq
, wait
);
1171 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1173 struct perf_counter
*counter
= file
->private_data
;
1177 case PERF_COUNTER_IOC_ENABLE
:
1178 perf_counter_enable_family(counter
);
1180 case PERF_COUNTER_IOC_DISABLE
:
1181 perf_counter_disable_family(counter
);
1189 static void __perf_counter_update_userpage(struct perf_counter
*counter
,
1190 struct perf_mmap_data
*data
)
1192 struct perf_counter_mmap_page
*userpg
= data
->user_page
;
1195 * Disable preemption so as to not let the corresponding user-space
1196 * spin too long if we get preempted.
1201 userpg
->index
= counter
->hw
.idx
;
1202 userpg
->offset
= atomic64_read(&counter
->count
);
1203 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1204 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1206 userpg
->data_head
= atomic_read(&data
->head
);
1212 void perf_counter_update_userpage(struct perf_counter
*counter
)
1214 struct perf_mmap_data
*data
;
1217 data
= rcu_dereference(counter
->data
);
1219 __perf_counter_update_userpage(counter
, data
);
1223 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1225 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1226 struct perf_mmap_data
*data
;
1227 int ret
= VM_FAULT_SIGBUS
;
1230 data
= rcu_dereference(counter
->data
);
1234 if (vmf
->pgoff
== 0) {
1235 vmf
->page
= virt_to_page(data
->user_page
);
1237 int nr
= vmf
->pgoff
- 1;
1239 if ((unsigned)nr
> data
->nr_pages
)
1242 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1244 get_page(vmf
->page
);
1252 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1254 struct perf_mmap_data
*data
;
1258 WARN_ON(atomic_read(&counter
->mmap_count
));
1260 size
= sizeof(struct perf_mmap_data
);
1261 size
+= nr_pages
* sizeof(void *);
1263 data
= kzalloc(size
, GFP_KERNEL
);
1267 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1268 if (!data
->user_page
)
1269 goto fail_user_page
;
1271 for (i
= 0; i
< nr_pages
; i
++) {
1272 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1273 if (!data
->data_pages
[i
])
1274 goto fail_data_pages
;
1277 data
->nr_pages
= nr_pages
;
1279 rcu_assign_pointer(counter
->data
, data
);
1284 for (i
--; i
>= 0; i
--)
1285 free_page((unsigned long)data
->data_pages
[i
]);
1287 free_page((unsigned long)data
->user_page
);
1296 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1298 struct perf_mmap_data
*data
= container_of(rcu_head
,
1299 struct perf_mmap_data
, rcu_head
);
1302 free_page((unsigned long)data
->user_page
);
1303 for (i
= 0; i
< data
->nr_pages
; i
++)
1304 free_page((unsigned long)data
->data_pages
[i
]);
1308 static void perf_mmap_data_free(struct perf_counter
*counter
)
1310 struct perf_mmap_data
*data
= counter
->data
;
1312 WARN_ON(atomic_read(&counter
->mmap_count
));
1314 rcu_assign_pointer(counter
->data
, NULL
);
1315 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1318 static void perf_mmap_open(struct vm_area_struct
*vma
)
1320 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1322 atomic_inc(&counter
->mmap_count
);
1325 static void perf_mmap_close(struct vm_area_struct
*vma
)
1327 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1329 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1330 &counter
->mmap_mutex
)) {
1331 perf_mmap_data_free(counter
);
1332 mutex_unlock(&counter
->mmap_mutex
);
1336 static struct vm_operations_struct perf_mmap_vmops
= {
1337 .open
= perf_mmap_open
,
1338 .close
= perf_mmap_close
,
1339 .fault
= perf_mmap_fault
,
1342 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1344 struct perf_counter
*counter
= file
->private_data
;
1345 unsigned long vma_size
;
1346 unsigned long nr_pages
;
1347 unsigned long locked
, lock_limit
;
1350 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1353 vma_size
= vma
->vm_end
- vma
->vm_start
;
1354 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1356 if (nr_pages
== 0 || !is_power_of_2(nr_pages
))
1359 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1362 if (vma
->vm_pgoff
!= 0)
1365 locked
= vma_size
>> PAGE_SHIFT
;
1366 locked
+= vma
->vm_mm
->locked_vm
;
1368 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1369 lock_limit
>>= PAGE_SHIFT
;
1371 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
))
1374 mutex_lock(&counter
->mmap_mutex
);
1375 if (atomic_inc_not_zero(&counter
->mmap_count
))
1378 WARN_ON(counter
->data
);
1379 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1381 atomic_set(&counter
->mmap_count
, 1);
1383 mutex_unlock(&counter
->mmap_mutex
);
1385 vma
->vm_flags
&= ~VM_MAYWRITE
;
1386 vma
->vm_flags
|= VM_RESERVED
;
1387 vma
->vm_ops
= &perf_mmap_vmops
;
1392 static const struct file_operations perf_fops
= {
1393 .release
= perf_release
,
1396 .unlocked_ioctl
= perf_ioctl
,
1397 .compat_ioctl
= perf_ioctl
,
1405 static int perf_output_write(struct perf_counter
*counter
, int nmi
,
1406 void *buf
, ssize_t size
)
1408 struct perf_mmap_data
*data
;
1409 unsigned int offset
, head
, nr
;
1415 data
= rcu_dereference(counter
->data
);
1419 if (!data
->nr_pages
)
1423 if (size
> PAGE_SIZE
)
1427 offset
= head
= atomic_read(&data
->head
);
1428 head
+= sizeof(u64
);
1429 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1431 wakeup
= (offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
);
1433 nr
= (offset
>> PAGE_SHIFT
) & (data
->nr_pages
- 1);
1434 offset
&= PAGE_SIZE
- 1;
1436 len
= min_t(unsigned int, PAGE_SIZE
- offset
, size
);
1437 memcpy(data
->data_pages
[nr
] + offset
, buf
, len
);
1441 nr
= (nr
+ 1) & (data
->nr_pages
- 1);
1442 memcpy(data
->data_pages
[nr
], buf
+ len
, size
);
1446 * generate a poll() wakeup for every page boundary crossed
1449 __perf_counter_update_userpage(counter
, data
);
1451 counter
->wakeup_pending
= 1;
1452 set_perf_counter_pending();
1454 wake_up(&counter
->waitq
);
1463 static void perf_output_simple(struct perf_counter
*counter
,
1464 int nmi
, struct pt_regs
*regs
)
1468 entry
= instruction_pointer(regs
);
1470 perf_output_write(counter
, nmi
, &entry
, sizeof(entry
));
1473 struct group_entry
{
1478 static void perf_output_group(struct perf_counter
*counter
, int nmi
)
1480 struct perf_counter
*leader
, *sub
;
1482 leader
= counter
->group_leader
;
1483 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1484 struct group_entry entry
;
1487 sub
->hw_ops
->read(sub
);
1489 entry
.event
= sub
->hw_event
.config
;
1490 entry
.counter
= atomic64_read(&sub
->count
);
1492 perf_output_write(counter
, nmi
, &entry
, sizeof(entry
));
1496 void perf_counter_output(struct perf_counter
*counter
,
1497 int nmi
, struct pt_regs
*regs
)
1499 switch (counter
->hw_event
.record_type
) {
1500 case PERF_RECORD_SIMPLE
:
1503 case PERF_RECORD_IRQ
:
1504 perf_output_simple(counter
, nmi
, regs
);
1507 case PERF_RECORD_GROUP
:
1508 perf_output_group(counter
, nmi
);
1514 * Generic software counter infrastructure
1517 static void perf_swcounter_update(struct perf_counter
*counter
)
1519 struct hw_perf_counter
*hwc
= &counter
->hw
;
1524 prev
= atomic64_read(&hwc
->prev_count
);
1525 now
= atomic64_read(&hwc
->count
);
1526 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
1531 atomic64_add(delta
, &counter
->count
);
1532 atomic64_sub(delta
, &hwc
->period_left
);
1535 static void perf_swcounter_set_period(struct perf_counter
*counter
)
1537 struct hw_perf_counter
*hwc
= &counter
->hw
;
1538 s64 left
= atomic64_read(&hwc
->period_left
);
1539 s64 period
= hwc
->irq_period
;
1541 if (unlikely(left
<= -period
)) {
1543 atomic64_set(&hwc
->period_left
, left
);
1546 if (unlikely(left
<= 0)) {
1548 atomic64_add(period
, &hwc
->period_left
);
1551 atomic64_set(&hwc
->prev_count
, -left
);
1552 atomic64_set(&hwc
->count
, -left
);
1555 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
1557 struct perf_counter
*counter
;
1558 struct pt_regs
*regs
;
1560 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
1561 counter
->hw_ops
->read(counter
);
1563 regs
= get_irq_regs();
1565 * In case we exclude kernel IPs or are somehow not in interrupt
1566 * context, provide the next best thing, the user IP.
1568 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
1569 !counter
->hw_event
.exclude_user
)
1570 regs
= task_pt_regs(current
);
1573 perf_counter_output(counter
, 0, regs
);
1575 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
1577 return HRTIMER_RESTART
;
1580 static void perf_swcounter_overflow(struct perf_counter
*counter
,
1581 int nmi
, struct pt_regs
*regs
)
1583 perf_swcounter_update(counter
);
1584 perf_swcounter_set_period(counter
);
1585 perf_counter_output(counter
, nmi
, regs
);
1588 static int perf_swcounter_match(struct perf_counter
*counter
,
1589 enum perf_event_types type
,
1590 u32 event
, struct pt_regs
*regs
)
1592 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1595 if (perf_event_raw(&counter
->hw_event
))
1598 if (perf_event_type(&counter
->hw_event
) != type
)
1601 if (perf_event_id(&counter
->hw_event
) != event
)
1604 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
1607 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
1613 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
1614 int nmi
, struct pt_regs
*regs
)
1616 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
1617 if (counter
->hw
.irq_period
&& !neg
)
1618 perf_swcounter_overflow(counter
, nmi
, regs
);
1621 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
1622 enum perf_event_types type
, u32 event
,
1623 u64 nr
, int nmi
, struct pt_regs
*regs
)
1625 struct perf_counter
*counter
;
1627 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
1631 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
1632 if (perf_swcounter_match(counter
, type
, event
, regs
))
1633 perf_swcounter_add(counter
, nr
, nmi
, regs
);
1638 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
1641 return &cpuctx
->recursion
[3];
1644 return &cpuctx
->recursion
[2];
1647 return &cpuctx
->recursion
[1];
1649 return &cpuctx
->recursion
[0];
1652 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
1653 u64 nr
, int nmi
, struct pt_regs
*regs
)
1655 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
1656 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
1664 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
, nr
, nmi
, regs
);
1665 if (cpuctx
->task_ctx
) {
1666 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
1674 put_cpu_var(perf_cpu_context
);
1677 void perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
)
1679 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
);
1682 static void perf_swcounter_read(struct perf_counter
*counter
)
1684 perf_swcounter_update(counter
);
1687 static int perf_swcounter_enable(struct perf_counter
*counter
)
1689 perf_swcounter_set_period(counter
);
1693 static void perf_swcounter_disable(struct perf_counter
*counter
)
1695 perf_swcounter_update(counter
);
1698 static const struct hw_perf_counter_ops perf_ops_generic
= {
1699 .enable
= perf_swcounter_enable
,
1700 .disable
= perf_swcounter_disable
,
1701 .read
= perf_swcounter_read
,
1705 * Software counter: cpu wall time clock
1708 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
1710 int cpu
= raw_smp_processor_id();
1714 now
= cpu_clock(cpu
);
1715 prev
= atomic64_read(&counter
->hw
.prev_count
);
1716 atomic64_set(&counter
->hw
.prev_count
, now
);
1717 atomic64_add(now
- prev
, &counter
->count
);
1720 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
1722 struct hw_perf_counter
*hwc
= &counter
->hw
;
1723 int cpu
= raw_smp_processor_id();
1725 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
1726 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1727 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
1728 if (hwc
->irq_period
) {
1729 __hrtimer_start_range_ns(&hwc
->hrtimer
,
1730 ns_to_ktime(hwc
->irq_period
), 0,
1731 HRTIMER_MODE_REL
, 0);
1737 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
1739 hrtimer_cancel(&counter
->hw
.hrtimer
);
1740 cpu_clock_perf_counter_update(counter
);
1743 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
1745 cpu_clock_perf_counter_update(counter
);
1748 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
1749 .enable
= cpu_clock_perf_counter_enable
,
1750 .disable
= cpu_clock_perf_counter_disable
,
1751 .read
= cpu_clock_perf_counter_read
,
1755 * Software counter: task time clock
1759 * Called from within the scheduler:
1761 static u64
task_clock_perf_counter_val(struct perf_counter
*counter
, int update
)
1763 struct task_struct
*curr
= counter
->task
;
1766 delta
= __task_delta_exec(curr
, update
);
1768 return curr
->se
.sum_exec_runtime
+ delta
;
1771 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
1776 prev
= atomic64_read(&counter
->hw
.prev_count
);
1778 atomic64_set(&counter
->hw
.prev_count
, now
);
1782 atomic64_add(delta
, &counter
->count
);
1785 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
1787 struct hw_perf_counter
*hwc
= &counter
->hw
;
1789 atomic64_set(&hwc
->prev_count
, task_clock_perf_counter_val(counter
, 0));
1790 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1791 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
1792 if (hwc
->irq_period
) {
1793 __hrtimer_start_range_ns(&hwc
->hrtimer
,
1794 ns_to_ktime(hwc
->irq_period
), 0,
1795 HRTIMER_MODE_REL
, 0);
1801 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
1803 hrtimer_cancel(&counter
->hw
.hrtimer
);
1804 task_clock_perf_counter_update(counter
,
1805 task_clock_perf_counter_val(counter
, 0));
1808 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
1810 task_clock_perf_counter_update(counter
,
1811 task_clock_perf_counter_val(counter
, 1));
1814 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
1815 .enable
= task_clock_perf_counter_enable
,
1816 .disable
= task_clock_perf_counter_disable
,
1817 .read
= task_clock_perf_counter_read
,
1821 * Software counter: cpu migrations
1824 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
1826 struct task_struct
*curr
= counter
->ctx
->task
;
1829 return curr
->se
.nr_migrations
;
1830 return cpu_nr_migrations(smp_processor_id());
1833 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
1838 prev
= atomic64_read(&counter
->hw
.prev_count
);
1839 now
= get_cpu_migrations(counter
);
1841 atomic64_set(&counter
->hw
.prev_count
, now
);
1845 atomic64_add(delta
, &counter
->count
);
1848 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
1850 cpu_migrations_perf_counter_update(counter
);
1853 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
1855 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
1856 atomic64_set(&counter
->hw
.prev_count
,
1857 get_cpu_migrations(counter
));
1861 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
1863 cpu_migrations_perf_counter_update(counter
);
1866 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
1867 .enable
= cpu_migrations_perf_counter_enable
,
1868 .disable
= cpu_migrations_perf_counter_disable
,
1869 .read
= cpu_migrations_perf_counter_read
,
1872 #ifdef CONFIG_EVENT_PROFILE
1873 void perf_tpcounter_event(int event_id
)
1875 struct pt_regs
*regs
= get_irq_regs();
1878 regs
= task_pt_regs(current
);
1880 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
);
1883 extern int ftrace_profile_enable(int);
1884 extern void ftrace_profile_disable(int);
1886 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
1888 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
1891 static const struct hw_perf_counter_ops
*
1892 tp_perf_counter_init(struct perf_counter
*counter
)
1894 int event_id
= perf_event_id(&counter
->hw_event
);
1897 ret
= ftrace_profile_enable(event_id
);
1901 counter
->destroy
= tp_perf_counter_destroy
;
1902 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
1904 return &perf_ops_generic
;
1907 static const struct hw_perf_counter_ops
*
1908 tp_perf_counter_init(struct perf_counter
*counter
)
1914 static const struct hw_perf_counter_ops
*
1915 sw_perf_counter_init(struct perf_counter
*counter
)
1917 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
1918 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
1919 struct hw_perf_counter
*hwc
= &counter
->hw
;
1922 * Software counters (currently) can't in general distinguish
1923 * between user, kernel and hypervisor events.
1924 * However, context switches and cpu migrations are considered
1925 * to be kernel events, and page faults are never hypervisor
1928 switch (perf_event_id(&counter
->hw_event
)) {
1929 case PERF_COUNT_CPU_CLOCK
:
1930 hw_ops
= &perf_ops_cpu_clock
;
1932 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
1933 hw_event
->irq_period
= 10000;
1935 case PERF_COUNT_TASK_CLOCK
:
1937 * If the user instantiates this as a per-cpu counter,
1938 * use the cpu_clock counter instead.
1940 if (counter
->ctx
->task
)
1941 hw_ops
= &perf_ops_task_clock
;
1943 hw_ops
= &perf_ops_cpu_clock
;
1945 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
1946 hw_event
->irq_period
= 10000;
1948 case PERF_COUNT_PAGE_FAULTS
:
1949 case PERF_COUNT_PAGE_FAULTS_MIN
:
1950 case PERF_COUNT_PAGE_FAULTS_MAJ
:
1951 case PERF_COUNT_CONTEXT_SWITCHES
:
1952 hw_ops
= &perf_ops_generic
;
1954 case PERF_COUNT_CPU_MIGRATIONS
:
1955 if (!counter
->hw_event
.exclude_kernel
)
1956 hw_ops
= &perf_ops_cpu_migrations
;
1961 hwc
->irq_period
= hw_event
->irq_period
;
1967 * Allocate and initialize a counter structure
1969 static struct perf_counter
*
1970 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
1972 struct perf_counter_context
*ctx
,
1973 struct perf_counter
*group_leader
,
1976 const struct hw_perf_counter_ops
*hw_ops
;
1977 struct perf_counter
*counter
;
1979 counter
= kzalloc(sizeof(*counter
), gfpflags
);
1984 * Single counters are their own group leaders, with an
1985 * empty sibling list:
1988 group_leader
= counter
;
1990 mutex_init(&counter
->mutex
);
1991 INIT_LIST_HEAD(&counter
->list_entry
);
1992 INIT_LIST_HEAD(&counter
->event_entry
);
1993 INIT_LIST_HEAD(&counter
->sibling_list
);
1994 init_waitqueue_head(&counter
->waitq
);
1996 mutex_init(&counter
->mmap_mutex
);
1998 INIT_LIST_HEAD(&counter
->child_list
);
2001 counter
->hw_event
= *hw_event
;
2002 counter
->wakeup_pending
= 0;
2003 counter
->group_leader
= group_leader
;
2004 counter
->hw_ops
= NULL
;
2007 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2008 if (hw_event
->disabled
)
2009 counter
->state
= PERF_COUNTER_STATE_OFF
;
2013 if (perf_event_raw(hw_event
)) {
2014 hw_ops
= hw_perf_counter_init(counter
);
2018 switch (perf_event_type(hw_event
)) {
2019 case PERF_TYPE_HARDWARE
:
2020 hw_ops
= hw_perf_counter_init(counter
);
2023 case PERF_TYPE_SOFTWARE
:
2024 hw_ops
= sw_perf_counter_init(counter
);
2027 case PERF_TYPE_TRACEPOINT
:
2028 hw_ops
= tp_perf_counter_init(counter
);
2037 counter
->hw_ops
= hw_ops
;
2043 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2045 * @hw_event_uptr: event type attributes for monitoring/sampling
2048 * @group_fd: group leader counter fd
2050 SYSCALL_DEFINE5(perf_counter_open
,
2051 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2052 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2054 struct perf_counter
*counter
, *group_leader
;
2055 struct perf_counter_hw_event hw_event
;
2056 struct perf_counter_context
*ctx
;
2057 struct file
*counter_file
= NULL
;
2058 struct file
*group_file
= NULL
;
2059 int fput_needed
= 0;
2060 int fput_needed2
= 0;
2063 /* for future expandability... */
2067 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2071 * Get the target context (task or percpu):
2073 ctx
= find_get_context(pid
, cpu
);
2075 return PTR_ERR(ctx
);
2078 * Look up the group leader (we will attach this counter to it):
2080 group_leader
= NULL
;
2081 if (group_fd
!= -1) {
2083 group_file
= fget_light(group_fd
, &fput_needed
);
2085 goto err_put_context
;
2086 if (group_file
->f_op
!= &perf_fops
)
2087 goto err_put_context
;
2089 group_leader
= group_file
->private_data
;
2091 * Do not allow a recursive hierarchy (this new sibling
2092 * becoming part of another group-sibling):
2094 if (group_leader
->group_leader
!= group_leader
)
2095 goto err_put_context
;
2097 * Do not allow to attach to a group in a different
2098 * task or CPU context:
2100 if (group_leader
->ctx
!= ctx
)
2101 goto err_put_context
;
2103 * Only a group leader can be exclusive or pinned
2105 if (hw_event
.exclusive
|| hw_event
.pinned
)
2106 goto err_put_context
;
2110 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2113 goto err_put_context
;
2115 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2117 goto err_free_put_context
;
2119 counter_file
= fget_light(ret
, &fput_needed2
);
2121 goto err_free_put_context
;
2123 counter
->filp
= counter_file
;
2124 mutex_lock(&ctx
->mutex
);
2125 perf_install_in_context(ctx
, counter
, cpu
);
2126 mutex_unlock(&ctx
->mutex
);
2128 fput_light(counter_file
, fput_needed2
);
2131 fput_light(group_file
, fput_needed
);
2135 err_free_put_context
:
2145 * Initialize the perf_counter context in a task_struct:
2148 __perf_counter_init_context(struct perf_counter_context
*ctx
,
2149 struct task_struct
*task
)
2151 memset(ctx
, 0, sizeof(*ctx
));
2152 spin_lock_init(&ctx
->lock
);
2153 mutex_init(&ctx
->mutex
);
2154 INIT_LIST_HEAD(&ctx
->counter_list
);
2155 INIT_LIST_HEAD(&ctx
->event_list
);
2160 * inherit a counter from parent task to child task:
2162 static struct perf_counter
*
2163 inherit_counter(struct perf_counter
*parent_counter
,
2164 struct task_struct
*parent
,
2165 struct perf_counter_context
*parent_ctx
,
2166 struct task_struct
*child
,
2167 struct perf_counter
*group_leader
,
2168 struct perf_counter_context
*child_ctx
)
2170 struct perf_counter
*child_counter
;
2173 * Instead of creating recursive hierarchies of counters,
2174 * we link inherited counters back to the original parent,
2175 * which has a filp for sure, which we use as the reference
2178 if (parent_counter
->parent
)
2179 parent_counter
= parent_counter
->parent
;
2181 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2182 parent_counter
->cpu
, child_ctx
,
2183 group_leader
, GFP_KERNEL
);
2188 * Link it up in the child's context:
2190 child_counter
->task
= child
;
2191 list_add_counter(child_counter
, child_ctx
);
2192 child_ctx
->nr_counters
++;
2194 child_counter
->parent
= parent_counter
;
2196 * inherit into child's child as well:
2198 child_counter
->hw_event
.inherit
= 1;
2201 * Get a reference to the parent filp - we will fput it
2202 * when the child counter exits. This is safe to do because
2203 * we are in the parent and we know that the filp still
2204 * exists and has a nonzero count:
2206 atomic_long_inc(&parent_counter
->filp
->f_count
);
2209 * Link this into the parent counter's child list
2211 mutex_lock(&parent_counter
->mutex
);
2212 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
2215 * Make the child state follow the state of the parent counter,
2216 * not its hw_event.disabled bit. We hold the parent's mutex,
2217 * so we won't race with perf_counter_{en,dis}able_family.
2219 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
2220 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2222 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
2224 mutex_unlock(&parent_counter
->mutex
);
2226 return child_counter
;
2229 static int inherit_group(struct perf_counter
*parent_counter
,
2230 struct task_struct
*parent
,
2231 struct perf_counter_context
*parent_ctx
,
2232 struct task_struct
*child
,
2233 struct perf_counter_context
*child_ctx
)
2235 struct perf_counter
*leader
;
2236 struct perf_counter
*sub
;
2238 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
2239 child
, NULL
, child_ctx
);
2242 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
2243 if (!inherit_counter(sub
, parent
, parent_ctx
,
2244 child
, leader
, child_ctx
))
2250 static void sync_child_counter(struct perf_counter
*child_counter
,
2251 struct perf_counter
*parent_counter
)
2253 u64 parent_val
, child_val
;
2255 parent_val
= atomic64_read(&parent_counter
->count
);
2256 child_val
= atomic64_read(&child_counter
->count
);
2259 * Add back the child's count to the parent's count:
2261 atomic64_add(child_val
, &parent_counter
->count
);
2264 * Remove this counter from the parent's list
2266 mutex_lock(&parent_counter
->mutex
);
2267 list_del_init(&child_counter
->child_list
);
2268 mutex_unlock(&parent_counter
->mutex
);
2271 * Release the parent counter, if this was the last
2274 fput(parent_counter
->filp
);
2278 __perf_counter_exit_task(struct task_struct
*child
,
2279 struct perf_counter
*child_counter
,
2280 struct perf_counter_context
*child_ctx
)
2282 struct perf_counter
*parent_counter
;
2283 struct perf_counter
*sub
, *tmp
;
2286 * If we do not self-reap then we have to wait for the
2287 * child task to unschedule (it will happen for sure),
2288 * so that its counter is at its final count. (This
2289 * condition triggers rarely - child tasks usually get
2290 * off their CPU before the parent has a chance to
2291 * get this far into the reaping action)
2293 if (child
!= current
) {
2294 wait_task_inactive(child
, 0);
2295 list_del_init(&child_counter
->list_entry
);
2297 struct perf_cpu_context
*cpuctx
;
2298 unsigned long flags
;
2302 * Disable and unlink this counter.
2304 * Be careful about zapping the list - IRQ/NMI context
2305 * could still be processing it:
2307 curr_rq_lock_irq_save(&flags
);
2308 perf_flags
= hw_perf_save_disable();
2310 cpuctx
= &__get_cpu_var(perf_cpu_context
);
2312 group_sched_out(child_counter
, cpuctx
, child_ctx
);
2314 list_del_init(&child_counter
->list_entry
);
2316 child_ctx
->nr_counters
--;
2318 hw_perf_restore(perf_flags
);
2319 curr_rq_unlock_irq_restore(&flags
);
2322 parent_counter
= child_counter
->parent
;
2324 * It can happen that parent exits first, and has counters
2325 * that are still around due to the child reference. These
2326 * counters need to be zapped - but otherwise linger.
2328 if (parent_counter
) {
2329 sync_child_counter(child_counter
, parent_counter
);
2330 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
2333 sync_child_counter(sub
, sub
->parent
);
2337 free_counter(child_counter
);
2342 * When a child task exits, feed back counter values to parent counters.
2344 * Note: we may be running in child context, but the PID is not hashed
2345 * anymore so new counters will not be added.
2347 void perf_counter_exit_task(struct task_struct
*child
)
2349 struct perf_counter
*child_counter
, *tmp
;
2350 struct perf_counter_context
*child_ctx
;
2352 child_ctx
= &child
->perf_counter_ctx
;
2354 if (likely(!child_ctx
->nr_counters
))
2357 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
2359 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
2363 * Initialize the perf_counter context in task_struct
2365 void perf_counter_init_task(struct task_struct
*child
)
2367 struct perf_counter_context
*child_ctx
, *parent_ctx
;
2368 struct perf_counter
*counter
;
2369 struct task_struct
*parent
= current
;
2371 child_ctx
= &child
->perf_counter_ctx
;
2372 parent_ctx
= &parent
->perf_counter_ctx
;
2374 __perf_counter_init_context(child_ctx
, child
);
2377 * This is executed from the parent task context, so inherit
2378 * counters that have been marked for cloning:
2381 if (likely(!parent_ctx
->nr_counters
))
2385 * Lock the parent list. No need to lock the child - not PID
2386 * hashed yet and not running, so nobody can access it.
2388 mutex_lock(&parent_ctx
->mutex
);
2391 * We dont have to disable NMIs - we are only looking at
2392 * the list, not manipulating it:
2394 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
2395 if (!counter
->hw_event
.inherit
)
2398 if (inherit_group(counter
, parent
,
2399 parent_ctx
, child
, child_ctx
))
2403 mutex_unlock(&parent_ctx
->mutex
);
2406 static void __cpuinit
perf_counter_init_cpu(int cpu
)
2408 struct perf_cpu_context
*cpuctx
;
2410 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2411 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
2413 mutex_lock(&perf_resource_mutex
);
2414 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
2415 mutex_unlock(&perf_resource_mutex
);
2417 hw_perf_counter_setup(cpu
);
2420 #ifdef CONFIG_HOTPLUG_CPU
2421 static void __perf_counter_exit_cpu(void *info
)
2423 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
2424 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
2425 struct perf_counter
*counter
, *tmp
;
2427 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
2428 __perf_counter_remove_from_context(counter
);
2430 static void perf_counter_exit_cpu(int cpu
)
2432 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2433 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
2435 mutex_lock(&ctx
->mutex
);
2436 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
2437 mutex_unlock(&ctx
->mutex
);
2440 static inline void perf_counter_exit_cpu(int cpu
) { }
2443 static int __cpuinit
2444 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
2446 unsigned int cpu
= (long)hcpu
;
2450 case CPU_UP_PREPARE
:
2451 case CPU_UP_PREPARE_FROZEN
:
2452 perf_counter_init_cpu(cpu
);
2455 case CPU_DOWN_PREPARE
:
2456 case CPU_DOWN_PREPARE_FROZEN
:
2457 perf_counter_exit_cpu(cpu
);
2467 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
2468 .notifier_call
= perf_cpu_notify
,
2471 static int __init
perf_counter_init(void)
2473 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
2474 (void *)(long)smp_processor_id());
2475 register_cpu_notifier(&perf_cpu_nb
);
2479 early_initcall(perf_counter_init
);
2481 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
2483 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
2487 perf_set_reserve_percpu(struct sysdev_class
*class,
2491 struct perf_cpu_context
*cpuctx
;
2495 err
= strict_strtoul(buf
, 10, &val
);
2498 if (val
> perf_max_counters
)
2501 mutex_lock(&perf_resource_mutex
);
2502 perf_reserved_percpu
= val
;
2503 for_each_online_cpu(cpu
) {
2504 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2505 spin_lock_irq(&cpuctx
->ctx
.lock
);
2506 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
2507 perf_max_counters
- perf_reserved_percpu
);
2508 cpuctx
->max_pertask
= mpt
;
2509 spin_unlock_irq(&cpuctx
->ctx
.lock
);
2511 mutex_unlock(&perf_resource_mutex
);
2516 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
2518 return sprintf(buf
, "%d\n", perf_overcommit
);
2522 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
2527 err
= strict_strtoul(buf
, 10, &val
);
2533 mutex_lock(&perf_resource_mutex
);
2534 perf_overcommit
= val
;
2535 mutex_unlock(&perf_resource_mutex
);
2540 static SYSDEV_CLASS_ATTR(
2543 perf_show_reserve_percpu
,
2544 perf_set_reserve_percpu
2547 static SYSDEV_CLASS_ATTR(
2550 perf_show_overcommit
,
2554 static struct attribute
*perfclass_attrs
[] = {
2555 &attr_reserve_percpu
.attr
,
2556 &attr_overcommit
.attr
,
2560 static struct attribute_group perfclass_attr_group
= {
2561 .attrs
= perfclass_attrs
,
2562 .name
= "perf_counters",
2565 static int __init
perf_counter_sysfs_init(void)
2567 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
2568 &perfclass_attr_group
);
2570 device_initcall(perf_counter_sysfs_init
);