2 * Performance counter core code
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
7 * For licencing details see kernel-base/COPYING
11 #include <linux/cpu.h>
12 #include <linux/smp.h>
13 #include <linux/file.h>
14 #include <linux/poll.h>
15 #include <linux/sysfs.h>
16 #include <linux/ptrace.h>
17 #include <linux/percpu.h>
18 #include <linux/uaccess.h>
19 #include <linux/syscalls.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/perf_counter.h>
24 #include <linux/vmstat.h>
25 #include <linux/rculist.h>
27 #include <asm/irq_regs.h>
30 * Each CPU has a list of per CPU counters:
32 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
34 int perf_max_counters __read_mostly
= 1;
35 static int perf_reserved_percpu __read_mostly
;
36 static int perf_overcommit __read_mostly
= 1;
39 * Mutex for (sysadmin-configurable) counter reservations:
41 static DEFINE_MUTEX(perf_resource_mutex
);
44 * Architecture provided APIs - weak aliases:
46 extern __weak
const struct hw_perf_counter_ops
*
47 hw_perf_counter_init(struct perf_counter
*counter
)
52 u64 __weak
hw_perf_save_disable(void) { return 0; }
53 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
54 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
55 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
56 struct perf_cpu_context
*cpuctx
,
57 struct perf_counter_context
*ctx
, int cpu
)
62 void __weak
perf_counter_print_debug(void) { }
65 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
67 struct perf_counter
*group_leader
= counter
->group_leader
;
70 * Depending on whether it is a standalone or sibling counter,
71 * add it straight to the context's counter list, or to the group
72 * leader's sibling list:
74 if (counter
->group_leader
== counter
)
75 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
77 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
79 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
83 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
85 struct perf_counter
*sibling
, *tmp
;
87 list_del_init(&counter
->list_entry
);
88 list_del_rcu(&counter
->event_entry
);
91 * If this was a group counter with sibling counters then
92 * upgrade the siblings to singleton counters by adding them
93 * to the context list directly:
95 list_for_each_entry_safe(sibling
, tmp
,
96 &counter
->sibling_list
, list_entry
) {
98 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
99 sibling
->group_leader
= sibling
;
104 counter_sched_out(struct perf_counter
*counter
,
105 struct perf_cpu_context
*cpuctx
,
106 struct perf_counter_context
*ctx
)
108 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
111 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
112 counter
->hw_ops
->disable(counter
);
115 if (!is_software_counter(counter
))
116 cpuctx
->active_oncpu
--;
118 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
119 cpuctx
->exclusive
= 0;
123 group_sched_out(struct perf_counter
*group_counter
,
124 struct perf_cpu_context
*cpuctx
,
125 struct perf_counter_context
*ctx
)
127 struct perf_counter
*counter
;
129 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
132 counter_sched_out(group_counter
, cpuctx
, ctx
);
135 * Schedule out siblings (if any):
137 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
138 counter_sched_out(counter
, cpuctx
, ctx
);
140 if (group_counter
->hw_event
.exclusive
)
141 cpuctx
->exclusive
= 0;
145 * Cross CPU call to remove a performance counter
147 * We disable the counter on the hardware level first. After that we
148 * remove it from the context list.
150 static void __perf_counter_remove_from_context(void *info
)
152 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
153 struct perf_counter
*counter
= info
;
154 struct perf_counter_context
*ctx
= counter
->ctx
;
159 * If this is a task context, we need to check whether it is
160 * the current task context of this cpu. If not it has been
161 * scheduled out before the smp call arrived.
163 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
166 curr_rq_lock_irq_save(&flags
);
167 spin_lock(&ctx
->lock
);
169 counter_sched_out(counter
, cpuctx
, ctx
);
171 counter
->task
= NULL
;
175 * Protect the list operation against NMI by disabling the
176 * counters on a global level. NOP for non NMI based counters.
178 perf_flags
= hw_perf_save_disable();
179 list_del_counter(counter
, ctx
);
180 hw_perf_restore(perf_flags
);
184 * Allow more per task counters with respect to the
187 cpuctx
->max_pertask
=
188 min(perf_max_counters
- ctx
->nr_counters
,
189 perf_max_counters
- perf_reserved_percpu
);
192 spin_unlock(&ctx
->lock
);
193 curr_rq_unlock_irq_restore(&flags
);
198 * Remove the counter from a task's (or a CPU's) list of counters.
200 * Must be called with counter->mutex and ctx->mutex held.
202 * CPU counters are removed with a smp call. For task counters we only
203 * call when the task is on a CPU.
205 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
207 struct perf_counter_context
*ctx
= counter
->ctx
;
208 struct task_struct
*task
= ctx
->task
;
212 * Per cpu counters are removed via an smp call and
213 * the removal is always sucessful.
215 smp_call_function_single(counter
->cpu
,
216 __perf_counter_remove_from_context
,
222 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
225 spin_lock_irq(&ctx
->lock
);
227 * If the context is active we need to retry the smp call.
229 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
230 spin_unlock_irq(&ctx
->lock
);
235 * The lock prevents that this context is scheduled in so we
236 * can remove the counter safely, if the call above did not
239 if (!list_empty(&counter
->list_entry
)) {
241 list_del_counter(counter
, ctx
);
242 counter
->task
= NULL
;
244 spin_unlock_irq(&ctx
->lock
);
248 * Cross CPU call to disable a performance counter
250 static void __perf_counter_disable(void *info
)
252 struct perf_counter
*counter
= info
;
253 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
254 struct perf_counter_context
*ctx
= counter
->ctx
;
258 * If this is a per-task counter, need to check whether this
259 * counter's task is the current task on this cpu.
261 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
264 curr_rq_lock_irq_save(&flags
);
265 spin_lock(&ctx
->lock
);
268 * If the counter is on, turn it off.
269 * If it is in error state, leave it in error state.
271 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
272 if (counter
== counter
->group_leader
)
273 group_sched_out(counter
, cpuctx
, ctx
);
275 counter_sched_out(counter
, cpuctx
, ctx
);
276 counter
->state
= PERF_COUNTER_STATE_OFF
;
279 spin_unlock(&ctx
->lock
);
280 curr_rq_unlock_irq_restore(&flags
);
286 static void perf_counter_disable(struct perf_counter
*counter
)
288 struct perf_counter_context
*ctx
= counter
->ctx
;
289 struct task_struct
*task
= ctx
->task
;
293 * Disable the counter on the cpu that it's on
295 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
301 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
303 spin_lock_irq(&ctx
->lock
);
305 * If the counter is still active, we need to retry the cross-call.
307 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
308 spin_unlock_irq(&ctx
->lock
);
313 * Since we have the lock this context can't be scheduled
314 * in, so we can change the state safely.
316 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
317 counter
->state
= PERF_COUNTER_STATE_OFF
;
319 spin_unlock_irq(&ctx
->lock
);
323 * Disable a counter and all its children.
325 static void perf_counter_disable_family(struct perf_counter
*counter
)
327 struct perf_counter
*child
;
329 perf_counter_disable(counter
);
332 * Lock the mutex to protect the list of children
334 mutex_lock(&counter
->mutex
);
335 list_for_each_entry(child
, &counter
->child_list
, child_list
)
336 perf_counter_disable(child
);
337 mutex_unlock(&counter
->mutex
);
341 counter_sched_in(struct perf_counter
*counter
,
342 struct perf_cpu_context
*cpuctx
,
343 struct perf_counter_context
*ctx
,
346 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
349 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
350 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
352 * The new state must be visible before we turn it on in the hardware:
356 if (counter
->hw_ops
->enable(counter
)) {
357 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
362 if (!is_software_counter(counter
))
363 cpuctx
->active_oncpu
++;
366 if (counter
->hw_event
.exclusive
)
367 cpuctx
->exclusive
= 1;
373 * Return 1 for a group consisting entirely of software counters,
374 * 0 if the group contains any hardware counters.
376 static int is_software_only_group(struct perf_counter
*leader
)
378 struct perf_counter
*counter
;
380 if (!is_software_counter(leader
))
382 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
383 if (!is_software_counter(counter
))
389 * Work out whether we can put this counter group on the CPU now.
391 static int group_can_go_on(struct perf_counter
*counter
,
392 struct perf_cpu_context
*cpuctx
,
396 * Groups consisting entirely of software counters can always go on.
398 if (is_software_only_group(counter
))
401 * If an exclusive group is already on, no other hardware
402 * counters can go on.
404 if (cpuctx
->exclusive
)
407 * If this group is exclusive and there are already
408 * counters on the CPU, it can't go on.
410 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
413 * Otherwise, try to add it if all previous groups were able
420 * Cross CPU call to install and enable a performance counter
422 static void __perf_install_in_context(void *info
)
424 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
425 struct perf_counter
*counter
= info
;
426 struct perf_counter_context
*ctx
= counter
->ctx
;
427 struct perf_counter
*leader
= counter
->group_leader
;
428 int cpu
= smp_processor_id();
434 * If this is a task context, we need to check whether it is
435 * the current task context of this cpu. If not it has been
436 * scheduled out before the smp call arrived.
438 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
441 curr_rq_lock_irq_save(&flags
);
442 spin_lock(&ctx
->lock
);
445 * Protect the list operation against NMI by disabling the
446 * counters on a global level. NOP for non NMI based counters.
448 perf_flags
= hw_perf_save_disable();
450 list_add_counter(counter
, ctx
);
452 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
455 * Don't put the counter on if it is disabled or if
456 * it is in a group and the group isn't on.
458 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
459 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
463 * An exclusive counter can't go on if there are already active
464 * hardware counters, and no hardware counter can go on if there
465 * is already an exclusive counter on.
467 if (!group_can_go_on(counter
, cpuctx
, 1))
470 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
474 * This counter couldn't go on. If it is in a group
475 * then we have to pull the whole group off.
476 * If the counter group is pinned then put it in error state.
478 if (leader
!= counter
)
479 group_sched_out(leader
, cpuctx
, ctx
);
480 if (leader
->hw_event
.pinned
)
481 leader
->state
= PERF_COUNTER_STATE_ERROR
;
484 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
485 cpuctx
->max_pertask
--;
488 hw_perf_restore(perf_flags
);
490 spin_unlock(&ctx
->lock
);
491 curr_rq_unlock_irq_restore(&flags
);
495 * Attach a performance counter to a context
497 * First we add the counter to the list with the hardware enable bit
498 * in counter->hw_config cleared.
500 * If the counter is attached to a task which is on a CPU we use a smp
501 * call to enable it in the task context. The task might have been
502 * scheduled away, but we check this in the smp call again.
504 * Must be called with ctx->mutex held.
507 perf_install_in_context(struct perf_counter_context
*ctx
,
508 struct perf_counter
*counter
,
511 struct task_struct
*task
= ctx
->task
;
515 * Per cpu counters are installed via an smp call and
516 * the install is always sucessful.
518 smp_call_function_single(cpu
, __perf_install_in_context
,
523 counter
->task
= task
;
525 task_oncpu_function_call(task
, __perf_install_in_context
,
528 spin_lock_irq(&ctx
->lock
);
530 * we need to retry the smp call.
532 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
533 spin_unlock_irq(&ctx
->lock
);
538 * The lock prevents that this context is scheduled in so we
539 * can add the counter safely, if it the call above did not
542 if (list_empty(&counter
->list_entry
)) {
543 list_add_counter(counter
, ctx
);
546 spin_unlock_irq(&ctx
->lock
);
550 * Cross CPU call to enable a performance counter
552 static void __perf_counter_enable(void *info
)
554 struct perf_counter
*counter
= info
;
555 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
556 struct perf_counter_context
*ctx
= counter
->ctx
;
557 struct perf_counter
*leader
= counter
->group_leader
;
562 * If this is a per-task counter, need to check whether this
563 * counter's task is the current task on this cpu.
565 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
568 curr_rq_lock_irq_save(&flags
);
569 spin_lock(&ctx
->lock
);
571 counter
->prev_state
= counter
->state
;
572 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
574 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
577 * If the counter is in a group and isn't the group leader,
578 * then don't put it on unless the group is on.
580 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
583 if (!group_can_go_on(counter
, cpuctx
, 1))
586 err
= counter_sched_in(counter
, cpuctx
, ctx
,
591 * If this counter can't go on and it's part of a
592 * group, then the whole group has to come off.
594 if (leader
!= counter
)
595 group_sched_out(leader
, cpuctx
, ctx
);
596 if (leader
->hw_event
.pinned
)
597 leader
->state
= PERF_COUNTER_STATE_ERROR
;
601 spin_unlock(&ctx
->lock
);
602 curr_rq_unlock_irq_restore(&flags
);
608 static void perf_counter_enable(struct perf_counter
*counter
)
610 struct perf_counter_context
*ctx
= counter
->ctx
;
611 struct task_struct
*task
= ctx
->task
;
615 * Enable the counter on the cpu that it's on
617 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
622 spin_lock_irq(&ctx
->lock
);
623 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
627 * If the counter is in error state, clear that first.
628 * That way, if we see the counter in error state below, we
629 * know that it has gone back into error state, as distinct
630 * from the task having been scheduled away before the
631 * cross-call arrived.
633 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
634 counter
->state
= PERF_COUNTER_STATE_OFF
;
637 spin_unlock_irq(&ctx
->lock
);
638 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
640 spin_lock_irq(&ctx
->lock
);
643 * If the context is active and the counter is still off,
644 * we need to retry the cross-call.
646 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
650 * Since we have the lock this context can't be scheduled
651 * in, so we can change the state safely.
653 if (counter
->state
== PERF_COUNTER_STATE_OFF
)
654 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
656 spin_unlock_irq(&ctx
->lock
);
660 * Enable a counter and all its children.
662 static void perf_counter_enable_family(struct perf_counter
*counter
)
664 struct perf_counter
*child
;
666 perf_counter_enable(counter
);
669 * Lock the mutex to protect the list of children
671 mutex_lock(&counter
->mutex
);
672 list_for_each_entry(child
, &counter
->child_list
, child_list
)
673 perf_counter_enable(child
);
674 mutex_unlock(&counter
->mutex
);
677 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
678 struct perf_cpu_context
*cpuctx
)
680 struct perf_counter
*counter
;
683 spin_lock(&ctx
->lock
);
685 if (likely(!ctx
->nr_counters
))
688 flags
= hw_perf_save_disable();
689 if (ctx
->nr_active
) {
690 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
691 group_sched_out(counter
, cpuctx
, ctx
);
693 hw_perf_restore(flags
);
695 spin_unlock(&ctx
->lock
);
699 * Called from scheduler to remove the counters of the current task,
700 * with interrupts disabled.
702 * We stop each counter and update the counter value in counter->count.
704 * This does not protect us against NMI, but disable()
705 * sets the disabled bit in the control field of counter _before_
706 * accessing the counter control register. If a NMI hits, then it will
707 * not restart the counter.
709 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
711 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
712 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
713 struct pt_regs
*regs
;
715 if (likely(!cpuctx
->task_ctx
))
718 regs
= task_pt_regs(task
);
719 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
);
720 __perf_counter_sched_out(ctx
, cpuctx
);
722 cpuctx
->task_ctx
= NULL
;
725 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
727 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
731 group_sched_in(struct perf_counter
*group_counter
,
732 struct perf_cpu_context
*cpuctx
,
733 struct perf_counter_context
*ctx
,
736 struct perf_counter
*counter
, *partial_group
;
739 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
742 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
744 return ret
< 0 ? ret
: 0;
746 group_counter
->prev_state
= group_counter
->state
;
747 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
751 * Schedule in siblings as one group (if any):
753 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
754 counter
->prev_state
= counter
->state
;
755 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
756 partial_group
= counter
;
765 * Groups can be scheduled in as one unit only, so undo any
766 * partial group before returning:
768 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
769 if (counter
== partial_group
)
771 counter_sched_out(counter
, cpuctx
, ctx
);
773 counter_sched_out(group_counter
, cpuctx
, ctx
);
779 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
780 struct perf_cpu_context
*cpuctx
, int cpu
)
782 struct perf_counter
*counter
;
786 spin_lock(&ctx
->lock
);
788 if (likely(!ctx
->nr_counters
))
791 flags
= hw_perf_save_disable();
794 * First go through the list and put on any pinned groups
795 * in order to give them the best chance of going on.
797 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
798 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
799 !counter
->hw_event
.pinned
)
801 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
804 if (group_can_go_on(counter
, cpuctx
, 1))
805 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
808 * If this pinned group hasn't been scheduled,
809 * put it in error state.
811 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
812 counter
->state
= PERF_COUNTER_STATE_ERROR
;
815 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
817 * Ignore counters in OFF or ERROR state, and
818 * ignore pinned counters since we did them already.
820 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
821 counter
->hw_event
.pinned
)
825 * Listen to the 'cpu' scheduling filter constraint
828 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
831 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
832 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
836 hw_perf_restore(flags
);
838 spin_unlock(&ctx
->lock
);
842 * Called from scheduler to add the counters of the current task
843 * with interrupts disabled.
845 * We restore the counter value and then enable it.
847 * This does not protect us against NMI, but enable()
848 * sets the enabled bit in the control field of counter _before_
849 * accessing the counter control register. If a NMI hits, then it will
850 * keep the counter running.
852 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
854 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
855 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
857 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
858 cpuctx
->task_ctx
= ctx
;
861 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
863 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
865 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
868 int perf_counter_task_disable(void)
870 struct task_struct
*curr
= current
;
871 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
872 struct perf_counter
*counter
;
877 if (likely(!ctx
->nr_counters
))
880 curr_rq_lock_irq_save(&flags
);
881 cpu
= smp_processor_id();
883 /* force the update of the task clock: */
884 __task_delta_exec(curr
, 1);
886 perf_counter_task_sched_out(curr
, cpu
);
888 spin_lock(&ctx
->lock
);
891 * Disable all the counters:
893 perf_flags
= hw_perf_save_disable();
895 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
896 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
)
897 counter
->state
= PERF_COUNTER_STATE_OFF
;
900 hw_perf_restore(perf_flags
);
902 spin_unlock(&ctx
->lock
);
904 curr_rq_unlock_irq_restore(&flags
);
909 int perf_counter_task_enable(void)
911 struct task_struct
*curr
= current
;
912 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
913 struct perf_counter
*counter
;
918 if (likely(!ctx
->nr_counters
))
921 curr_rq_lock_irq_save(&flags
);
922 cpu
= smp_processor_id();
924 /* force the update of the task clock: */
925 __task_delta_exec(curr
, 1);
927 perf_counter_task_sched_out(curr
, cpu
);
929 spin_lock(&ctx
->lock
);
932 * Disable all the counters:
934 perf_flags
= hw_perf_save_disable();
936 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
937 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
939 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
940 counter
->hw_event
.disabled
= 0;
942 hw_perf_restore(perf_flags
);
944 spin_unlock(&ctx
->lock
);
946 perf_counter_task_sched_in(curr
, cpu
);
948 curr_rq_unlock_irq_restore(&flags
);
954 * Round-robin a context's counters:
956 static void rotate_ctx(struct perf_counter_context
*ctx
)
958 struct perf_counter
*counter
;
961 if (!ctx
->nr_counters
)
964 spin_lock(&ctx
->lock
);
966 * Rotate the first entry last (works just fine for group counters too):
968 perf_flags
= hw_perf_save_disable();
969 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
970 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
973 hw_perf_restore(perf_flags
);
975 spin_unlock(&ctx
->lock
);
978 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
980 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
981 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
982 const int rotate_percpu
= 0;
985 perf_counter_cpu_sched_out(cpuctx
);
986 perf_counter_task_sched_out(curr
, cpu
);
989 rotate_ctx(&cpuctx
->ctx
);
993 perf_counter_cpu_sched_in(cpuctx
, cpu
);
994 perf_counter_task_sched_in(curr
, cpu
);
998 * Cross CPU call to read the hardware counter
1000 static void __read(void *info
)
1002 struct perf_counter
*counter
= info
;
1003 unsigned long flags
;
1005 curr_rq_lock_irq_save(&flags
);
1006 counter
->hw_ops
->read(counter
);
1007 curr_rq_unlock_irq_restore(&flags
);
1010 static u64
perf_counter_read(struct perf_counter
*counter
)
1013 * If counter is enabled and currently active on a CPU, update the
1014 * value in the counter structure:
1016 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1017 smp_call_function_single(counter
->oncpu
,
1018 __read
, counter
, 1);
1021 return atomic64_read(&counter
->count
);
1025 * Cross CPU call to switch performance data pointers
1027 static void __perf_switch_irq_data(void *info
)
1029 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1030 struct perf_counter
*counter
= info
;
1031 struct perf_counter_context
*ctx
= counter
->ctx
;
1032 struct perf_data
*oldirqdata
= counter
->irqdata
;
1035 * If this is a task context, we need to check whether it is
1036 * the current task context of this cpu. If not it has been
1037 * scheduled out before the smp call arrived.
1040 if (cpuctx
->task_ctx
!= ctx
)
1042 spin_lock(&ctx
->lock
);
1045 /* Change the pointer NMI safe */
1046 atomic_long_set((atomic_long_t
*)&counter
->irqdata
,
1047 (unsigned long) counter
->usrdata
);
1048 counter
->usrdata
= oldirqdata
;
1051 spin_unlock(&ctx
->lock
);
1054 static struct perf_data
*perf_switch_irq_data(struct perf_counter
*counter
)
1056 struct perf_counter_context
*ctx
= counter
->ctx
;
1057 struct perf_data
*oldirqdata
= counter
->irqdata
;
1058 struct task_struct
*task
= ctx
->task
;
1061 smp_call_function_single(counter
->cpu
,
1062 __perf_switch_irq_data
,
1064 return counter
->usrdata
;
1068 spin_lock_irq(&ctx
->lock
);
1069 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
) {
1070 counter
->irqdata
= counter
->usrdata
;
1071 counter
->usrdata
= oldirqdata
;
1072 spin_unlock_irq(&ctx
->lock
);
1075 spin_unlock_irq(&ctx
->lock
);
1076 task_oncpu_function_call(task
, __perf_switch_irq_data
, counter
);
1077 /* Might have failed, because task was scheduled out */
1078 if (counter
->irqdata
== oldirqdata
)
1081 return counter
->usrdata
;
1084 static void put_context(struct perf_counter_context
*ctx
)
1087 put_task_struct(ctx
->task
);
1090 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1092 struct perf_cpu_context
*cpuctx
;
1093 struct perf_counter_context
*ctx
;
1094 struct task_struct
*task
;
1097 * If cpu is not a wildcard then this is a percpu counter:
1100 /* Must be root to operate on a CPU counter: */
1101 if (!capable(CAP_SYS_ADMIN
))
1102 return ERR_PTR(-EACCES
);
1104 if (cpu
< 0 || cpu
> num_possible_cpus())
1105 return ERR_PTR(-EINVAL
);
1108 * We could be clever and allow to attach a counter to an
1109 * offline CPU and activate it when the CPU comes up, but
1112 if (!cpu_isset(cpu
, cpu_online_map
))
1113 return ERR_PTR(-ENODEV
);
1115 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1125 task
= find_task_by_vpid(pid
);
1127 get_task_struct(task
);
1131 return ERR_PTR(-ESRCH
);
1133 ctx
= &task
->perf_counter_ctx
;
1136 /* Reuse ptrace permission checks for now. */
1137 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1139 return ERR_PTR(-EACCES
);
1145 static void free_counter_rcu(struct rcu_head
*head
)
1147 struct perf_counter
*counter
;
1149 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1153 static void free_counter(struct perf_counter
*counter
)
1155 if (counter
->destroy
)
1156 counter
->destroy(counter
);
1158 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1162 * Called when the last reference to the file is gone.
1164 static int perf_release(struct inode
*inode
, struct file
*file
)
1166 struct perf_counter
*counter
= file
->private_data
;
1167 struct perf_counter_context
*ctx
= counter
->ctx
;
1169 file
->private_data
= NULL
;
1171 mutex_lock(&ctx
->mutex
);
1172 mutex_lock(&counter
->mutex
);
1174 perf_counter_remove_from_context(counter
);
1176 mutex_unlock(&counter
->mutex
);
1177 mutex_unlock(&ctx
->mutex
);
1179 free_counter(counter
);
1186 * Read the performance counter - simple non blocking version for now
1189 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1193 if (count
!= sizeof(cntval
))
1197 * Return end-of-file for a read on a counter that is in
1198 * error state (i.e. because it was pinned but it couldn't be
1199 * scheduled on to the CPU at some point).
1201 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1204 mutex_lock(&counter
->mutex
);
1205 cntval
= perf_counter_read(counter
);
1206 mutex_unlock(&counter
->mutex
);
1208 return put_user(cntval
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(cntval
);
1212 perf_copy_usrdata(struct perf_data
*usrdata
, char __user
*buf
, size_t count
)
1217 count
= min(count
, (size_t)usrdata
->len
);
1218 if (copy_to_user(buf
, usrdata
->data
+ usrdata
->rd_idx
, count
))
1221 /* Adjust the counters */
1222 usrdata
->len
-= count
;
1224 usrdata
->rd_idx
= 0;
1226 usrdata
->rd_idx
+= count
;
1232 perf_read_irq_data(struct perf_counter
*counter
,
1237 struct perf_data
*irqdata
, *usrdata
;
1238 DECLARE_WAITQUEUE(wait
, current
);
1241 irqdata
= counter
->irqdata
;
1242 usrdata
= counter
->usrdata
;
1244 if (usrdata
->len
+ irqdata
->len
>= count
)
1250 spin_lock_irq(&counter
->waitq
.lock
);
1251 __add_wait_queue(&counter
->waitq
, &wait
);
1253 set_current_state(TASK_INTERRUPTIBLE
);
1254 if (usrdata
->len
+ irqdata
->len
>= count
)
1257 if (signal_pending(current
))
1260 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1263 spin_unlock_irq(&counter
->waitq
.lock
);
1265 spin_lock_irq(&counter
->waitq
.lock
);
1267 __remove_wait_queue(&counter
->waitq
, &wait
);
1268 __set_current_state(TASK_RUNNING
);
1269 spin_unlock_irq(&counter
->waitq
.lock
);
1271 if (usrdata
->len
+ irqdata
->len
< count
&&
1272 counter
->state
!= PERF_COUNTER_STATE_ERROR
)
1273 return -ERESTARTSYS
;
1275 mutex_lock(&counter
->mutex
);
1277 /* Drain pending data first: */
1278 res
= perf_copy_usrdata(usrdata
, buf
, count
);
1279 if (res
< 0 || res
== count
)
1282 /* Switch irq buffer: */
1283 usrdata
= perf_switch_irq_data(counter
);
1284 res2
= perf_copy_usrdata(usrdata
, buf
+ res
, count
- res
);
1292 mutex_unlock(&counter
->mutex
);
1298 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1300 struct perf_counter
*counter
= file
->private_data
;
1302 switch (counter
->hw_event
.record_type
) {
1303 case PERF_RECORD_SIMPLE
:
1304 return perf_read_hw(counter
, buf
, count
);
1306 case PERF_RECORD_IRQ
:
1307 case PERF_RECORD_GROUP
:
1308 return perf_read_irq_data(counter
, buf
, count
,
1309 file
->f_flags
& O_NONBLOCK
);
1314 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1316 struct perf_counter
*counter
= file
->private_data
;
1317 unsigned int events
= 0;
1318 unsigned long flags
;
1320 poll_wait(file
, &counter
->waitq
, wait
);
1322 spin_lock_irqsave(&counter
->waitq
.lock
, flags
);
1323 if (counter
->usrdata
->len
|| counter
->irqdata
->len
)
1325 spin_unlock_irqrestore(&counter
->waitq
.lock
, flags
);
1330 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1332 struct perf_counter
*counter
= file
->private_data
;
1336 case PERF_COUNTER_IOC_ENABLE
:
1337 perf_counter_enable_family(counter
);
1339 case PERF_COUNTER_IOC_DISABLE
:
1340 perf_counter_disable_family(counter
);
1348 static const struct file_operations perf_fops
= {
1349 .release
= perf_release
,
1352 .unlocked_ioctl
= perf_ioctl
,
1353 .compat_ioctl
= perf_ioctl
,
1360 static void perf_counter_store_irq(struct perf_counter
*counter
, u64 data
)
1362 struct perf_data
*irqdata
= counter
->irqdata
;
1364 if (irqdata
->len
> PERF_DATA_BUFLEN
- sizeof(u64
)) {
1367 u64
*p
= (u64
*) &irqdata
->data
[irqdata
->len
];
1370 irqdata
->len
+= sizeof(u64
);
1374 static void perf_counter_handle_group(struct perf_counter
*counter
)
1376 struct perf_counter
*leader
, *sub
;
1378 leader
= counter
->group_leader
;
1379 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1381 sub
->hw_ops
->read(sub
);
1382 perf_counter_store_irq(counter
, sub
->hw_event
.config
);
1383 perf_counter_store_irq(counter
, atomic64_read(&sub
->count
));
1387 void perf_counter_output(struct perf_counter
*counter
,
1388 int nmi
, struct pt_regs
*regs
)
1390 switch (counter
->hw_event
.record_type
) {
1391 case PERF_RECORD_SIMPLE
:
1394 case PERF_RECORD_IRQ
:
1395 perf_counter_store_irq(counter
, instruction_pointer(regs
));
1398 case PERF_RECORD_GROUP
:
1399 perf_counter_handle_group(counter
);
1404 counter
->wakeup_pending
= 1;
1405 set_perf_counter_pending();
1407 wake_up(&counter
->waitq
);
1411 * Generic software counter infrastructure
1414 static void perf_swcounter_update(struct perf_counter
*counter
)
1416 struct hw_perf_counter
*hwc
= &counter
->hw
;
1421 prev
= atomic64_read(&hwc
->prev_count
);
1422 now
= atomic64_read(&hwc
->count
);
1423 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
1428 atomic64_add(delta
, &counter
->count
);
1429 atomic64_sub(delta
, &hwc
->period_left
);
1432 static void perf_swcounter_set_period(struct perf_counter
*counter
)
1434 struct hw_perf_counter
*hwc
= &counter
->hw
;
1435 s64 left
= atomic64_read(&hwc
->period_left
);
1436 s64 period
= hwc
->irq_period
;
1438 if (unlikely(left
<= -period
)) {
1440 atomic64_set(&hwc
->period_left
, left
);
1443 if (unlikely(left
<= 0)) {
1445 atomic64_add(period
, &hwc
->period_left
);
1448 atomic64_set(&hwc
->prev_count
, -left
);
1449 atomic64_set(&hwc
->count
, -left
);
1452 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
1454 struct perf_counter
*counter
;
1455 struct pt_regs
*regs
;
1457 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
1458 counter
->hw_ops
->read(counter
);
1460 regs
= get_irq_regs();
1462 * In case we exclude kernel IPs or are somehow not in interrupt
1463 * context, provide the next best thing, the user IP.
1465 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
1466 !counter
->hw_event
.exclude_user
)
1467 regs
= task_pt_regs(current
);
1470 perf_counter_output(counter
, 0, regs
);
1472 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
1474 return HRTIMER_RESTART
;
1477 static void perf_swcounter_overflow(struct perf_counter
*counter
,
1478 int nmi
, struct pt_regs
*regs
)
1480 perf_swcounter_update(counter
);
1481 perf_swcounter_set_period(counter
);
1482 perf_counter_output(counter
, nmi
, regs
);
1485 static int perf_swcounter_match(struct perf_counter
*counter
,
1486 enum perf_event_types type
,
1487 u32 event
, struct pt_regs
*regs
)
1489 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1492 if (perf_event_raw(&counter
->hw_event
))
1495 if (perf_event_type(&counter
->hw_event
) != type
)
1498 if (perf_event_id(&counter
->hw_event
) != event
)
1501 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
1504 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
1510 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
1511 int nmi
, struct pt_regs
*regs
)
1513 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
1514 if (counter
->hw
.irq_period
&& !neg
)
1515 perf_swcounter_overflow(counter
, nmi
, regs
);
1518 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
1519 enum perf_event_types type
, u32 event
,
1520 u64 nr
, int nmi
, struct pt_regs
*regs
)
1522 struct perf_counter
*counter
;
1524 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
1528 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
1529 if (perf_swcounter_match(counter
, type
, event
, regs
))
1530 perf_swcounter_add(counter
, nr
, nmi
, regs
);
1535 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
1536 u64 nr
, int nmi
, struct pt_regs
*regs
)
1538 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
1540 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
, nr
, nmi
, regs
);
1541 if (cpuctx
->task_ctx
) {
1542 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
1546 put_cpu_var(perf_cpu_context
);
1549 void perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
)
1551 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
);
1554 static void perf_swcounter_read(struct perf_counter
*counter
)
1556 perf_swcounter_update(counter
);
1559 static int perf_swcounter_enable(struct perf_counter
*counter
)
1561 perf_swcounter_set_period(counter
);
1565 static void perf_swcounter_disable(struct perf_counter
*counter
)
1567 perf_swcounter_update(counter
);
1570 static const struct hw_perf_counter_ops perf_ops_generic
= {
1571 .enable
= perf_swcounter_enable
,
1572 .disable
= perf_swcounter_disable
,
1573 .read
= perf_swcounter_read
,
1577 * Software counter: cpu wall time clock
1580 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
1582 int cpu
= raw_smp_processor_id();
1586 now
= cpu_clock(cpu
);
1587 prev
= atomic64_read(&counter
->hw
.prev_count
);
1588 atomic64_set(&counter
->hw
.prev_count
, now
);
1589 atomic64_add(now
- prev
, &counter
->count
);
1592 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
1594 struct hw_perf_counter
*hwc
= &counter
->hw
;
1595 int cpu
= raw_smp_processor_id();
1597 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
1598 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1599 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
1600 if (hwc
->irq_period
) {
1601 __hrtimer_start_range_ns(&hwc
->hrtimer
,
1602 ns_to_ktime(hwc
->irq_period
), 0,
1603 HRTIMER_MODE_REL
, 0);
1609 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
1611 hrtimer_cancel(&counter
->hw
.hrtimer
);
1612 cpu_clock_perf_counter_update(counter
);
1615 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
1617 cpu_clock_perf_counter_update(counter
);
1620 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
1621 .enable
= cpu_clock_perf_counter_enable
,
1622 .disable
= cpu_clock_perf_counter_disable
,
1623 .read
= cpu_clock_perf_counter_read
,
1627 * Software counter: task time clock
1631 * Called from within the scheduler:
1633 static u64
task_clock_perf_counter_val(struct perf_counter
*counter
, int update
)
1635 struct task_struct
*curr
= counter
->task
;
1638 delta
= __task_delta_exec(curr
, update
);
1640 return curr
->se
.sum_exec_runtime
+ delta
;
1643 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
1648 prev
= atomic64_read(&counter
->hw
.prev_count
);
1650 atomic64_set(&counter
->hw
.prev_count
, now
);
1654 atomic64_add(delta
, &counter
->count
);
1657 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
1659 struct hw_perf_counter
*hwc
= &counter
->hw
;
1661 atomic64_set(&hwc
->prev_count
, task_clock_perf_counter_val(counter
, 0));
1662 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1663 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
1664 if (hwc
->irq_period
) {
1665 __hrtimer_start_range_ns(&hwc
->hrtimer
,
1666 ns_to_ktime(hwc
->irq_period
), 0,
1667 HRTIMER_MODE_REL
, 0);
1673 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
1675 hrtimer_cancel(&counter
->hw
.hrtimer
);
1676 task_clock_perf_counter_update(counter
,
1677 task_clock_perf_counter_val(counter
, 0));
1680 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
1682 task_clock_perf_counter_update(counter
,
1683 task_clock_perf_counter_val(counter
, 1));
1686 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
1687 .enable
= task_clock_perf_counter_enable
,
1688 .disable
= task_clock_perf_counter_disable
,
1689 .read
= task_clock_perf_counter_read
,
1693 * Software counter: cpu migrations
1696 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
1698 struct task_struct
*curr
= counter
->ctx
->task
;
1701 return curr
->se
.nr_migrations
;
1702 return cpu_nr_migrations(smp_processor_id());
1705 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
1710 prev
= atomic64_read(&counter
->hw
.prev_count
);
1711 now
= get_cpu_migrations(counter
);
1713 atomic64_set(&counter
->hw
.prev_count
, now
);
1717 atomic64_add(delta
, &counter
->count
);
1720 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
1722 cpu_migrations_perf_counter_update(counter
);
1725 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
1727 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
1728 atomic64_set(&counter
->hw
.prev_count
,
1729 get_cpu_migrations(counter
));
1733 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
1735 cpu_migrations_perf_counter_update(counter
);
1738 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
1739 .enable
= cpu_migrations_perf_counter_enable
,
1740 .disable
= cpu_migrations_perf_counter_disable
,
1741 .read
= cpu_migrations_perf_counter_read
,
1744 #ifdef CONFIG_EVENT_PROFILE
1745 void perf_tpcounter_event(int event_id
)
1747 struct pt_regs
*regs
= get_irq_regs();
1750 regs
= task_pt_regs(current
);
1752 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
);
1755 extern int ftrace_profile_enable(int);
1756 extern void ftrace_profile_disable(int);
1758 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
1760 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
1763 static const struct hw_perf_counter_ops
*
1764 tp_perf_counter_init(struct perf_counter
*counter
)
1766 int event_id
= perf_event_id(&counter
->hw_event
);
1769 ret
= ftrace_profile_enable(event_id
);
1773 counter
->destroy
= tp_perf_counter_destroy
;
1774 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
1776 return &perf_ops_generic
;
1779 static const struct hw_perf_counter_ops
*
1780 tp_perf_counter_init(struct perf_counter
*counter
)
1786 static const struct hw_perf_counter_ops
*
1787 sw_perf_counter_init(struct perf_counter
*counter
)
1789 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
1790 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
1791 struct hw_perf_counter
*hwc
= &counter
->hw
;
1794 * Software counters (currently) can't in general distinguish
1795 * between user, kernel and hypervisor events.
1796 * However, context switches and cpu migrations are considered
1797 * to be kernel events, and page faults are never hypervisor
1800 switch (perf_event_id(&counter
->hw_event
)) {
1801 case PERF_COUNT_CPU_CLOCK
:
1802 hw_ops
= &perf_ops_cpu_clock
;
1804 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
1805 hw_event
->irq_period
= 10000;
1807 case PERF_COUNT_TASK_CLOCK
:
1809 * If the user instantiates this as a per-cpu counter,
1810 * use the cpu_clock counter instead.
1812 if (counter
->ctx
->task
)
1813 hw_ops
= &perf_ops_task_clock
;
1815 hw_ops
= &perf_ops_cpu_clock
;
1817 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
1818 hw_event
->irq_period
= 10000;
1820 case PERF_COUNT_PAGE_FAULTS
:
1821 case PERF_COUNT_PAGE_FAULTS_MIN
:
1822 case PERF_COUNT_PAGE_FAULTS_MAJ
:
1823 case PERF_COUNT_CONTEXT_SWITCHES
:
1824 hw_ops
= &perf_ops_generic
;
1826 case PERF_COUNT_CPU_MIGRATIONS
:
1827 if (!counter
->hw_event
.exclude_kernel
)
1828 hw_ops
= &perf_ops_cpu_migrations
;
1833 hwc
->irq_period
= hw_event
->irq_period
;
1839 * Allocate and initialize a counter structure
1841 static struct perf_counter
*
1842 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
1844 struct perf_counter_context
*ctx
,
1845 struct perf_counter
*group_leader
,
1848 const struct hw_perf_counter_ops
*hw_ops
;
1849 struct perf_counter
*counter
;
1851 counter
= kzalloc(sizeof(*counter
), gfpflags
);
1856 * Single counters are their own group leaders, with an
1857 * empty sibling list:
1860 group_leader
= counter
;
1862 mutex_init(&counter
->mutex
);
1863 INIT_LIST_HEAD(&counter
->list_entry
);
1864 INIT_LIST_HEAD(&counter
->event_entry
);
1865 INIT_LIST_HEAD(&counter
->sibling_list
);
1866 init_waitqueue_head(&counter
->waitq
);
1868 INIT_LIST_HEAD(&counter
->child_list
);
1870 counter
->irqdata
= &counter
->data
[0];
1871 counter
->usrdata
= &counter
->data
[1];
1873 counter
->hw_event
= *hw_event
;
1874 counter
->wakeup_pending
= 0;
1875 counter
->group_leader
= group_leader
;
1876 counter
->hw_ops
= NULL
;
1879 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1880 if (hw_event
->disabled
)
1881 counter
->state
= PERF_COUNTER_STATE_OFF
;
1885 if (perf_event_raw(hw_event
)) {
1886 hw_ops
= hw_perf_counter_init(counter
);
1890 switch (perf_event_type(hw_event
)) {
1891 case PERF_TYPE_HARDWARE
:
1892 hw_ops
= hw_perf_counter_init(counter
);
1895 case PERF_TYPE_SOFTWARE
:
1896 hw_ops
= sw_perf_counter_init(counter
);
1899 case PERF_TYPE_TRACEPOINT
:
1900 hw_ops
= tp_perf_counter_init(counter
);
1909 counter
->hw_ops
= hw_ops
;
1915 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
1917 * @hw_event_uptr: event type attributes for monitoring/sampling
1920 * @group_fd: group leader counter fd
1922 SYSCALL_DEFINE5(perf_counter_open
,
1923 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
1924 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
1926 struct perf_counter
*counter
, *group_leader
;
1927 struct perf_counter_hw_event hw_event
;
1928 struct perf_counter_context
*ctx
;
1929 struct file
*counter_file
= NULL
;
1930 struct file
*group_file
= NULL
;
1931 int fput_needed
= 0;
1932 int fput_needed2
= 0;
1935 /* for future expandability... */
1939 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
1943 * Get the target context (task or percpu):
1945 ctx
= find_get_context(pid
, cpu
);
1947 return PTR_ERR(ctx
);
1950 * Look up the group leader (we will attach this counter to it):
1952 group_leader
= NULL
;
1953 if (group_fd
!= -1) {
1955 group_file
= fget_light(group_fd
, &fput_needed
);
1957 goto err_put_context
;
1958 if (group_file
->f_op
!= &perf_fops
)
1959 goto err_put_context
;
1961 group_leader
= group_file
->private_data
;
1963 * Do not allow a recursive hierarchy (this new sibling
1964 * becoming part of another group-sibling):
1966 if (group_leader
->group_leader
!= group_leader
)
1967 goto err_put_context
;
1969 * Do not allow to attach to a group in a different
1970 * task or CPU context:
1972 if (group_leader
->ctx
!= ctx
)
1973 goto err_put_context
;
1975 * Only a group leader can be exclusive or pinned
1977 if (hw_event
.exclusive
|| hw_event
.pinned
)
1978 goto err_put_context
;
1982 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
1985 goto err_put_context
;
1987 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
1989 goto err_free_put_context
;
1991 counter_file
= fget_light(ret
, &fput_needed2
);
1993 goto err_free_put_context
;
1995 counter
->filp
= counter_file
;
1996 mutex_lock(&ctx
->mutex
);
1997 perf_install_in_context(ctx
, counter
, cpu
);
1998 mutex_unlock(&ctx
->mutex
);
2000 fput_light(counter_file
, fput_needed2
);
2003 fput_light(group_file
, fput_needed
);
2007 err_free_put_context
:
2017 * Initialize the perf_counter context in a task_struct:
2020 __perf_counter_init_context(struct perf_counter_context
*ctx
,
2021 struct task_struct
*task
)
2023 memset(ctx
, 0, sizeof(*ctx
));
2024 spin_lock_init(&ctx
->lock
);
2025 mutex_init(&ctx
->mutex
);
2026 INIT_LIST_HEAD(&ctx
->counter_list
);
2027 INIT_LIST_HEAD(&ctx
->event_list
);
2032 * inherit a counter from parent task to child task:
2034 static struct perf_counter
*
2035 inherit_counter(struct perf_counter
*parent_counter
,
2036 struct task_struct
*parent
,
2037 struct perf_counter_context
*parent_ctx
,
2038 struct task_struct
*child
,
2039 struct perf_counter
*group_leader
,
2040 struct perf_counter_context
*child_ctx
)
2042 struct perf_counter
*child_counter
;
2045 * Instead of creating recursive hierarchies of counters,
2046 * we link inherited counters back to the original parent,
2047 * which has a filp for sure, which we use as the reference
2050 if (parent_counter
->parent
)
2051 parent_counter
= parent_counter
->parent
;
2053 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2054 parent_counter
->cpu
, child_ctx
,
2055 group_leader
, GFP_KERNEL
);
2060 * Link it up in the child's context:
2062 child_counter
->task
= child
;
2063 list_add_counter(child_counter
, child_ctx
);
2064 child_ctx
->nr_counters
++;
2066 child_counter
->parent
= parent_counter
;
2068 * inherit into child's child as well:
2070 child_counter
->hw_event
.inherit
= 1;
2073 * Get a reference to the parent filp - we will fput it
2074 * when the child counter exits. This is safe to do because
2075 * we are in the parent and we know that the filp still
2076 * exists and has a nonzero count:
2078 atomic_long_inc(&parent_counter
->filp
->f_count
);
2081 * Link this into the parent counter's child list
2083 mutex_lock(&parent_counter
->mutex
);
2084 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
2087 * Make the child state follow the state of the parent counter,
2088 * not its hw_event.disabled bit. We hold the parent's mutex,
2089 * so we won't race with perf_counter_{en,dis}able_family.
2091 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
2092 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2094 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
2096 mutex_unlock(&parent_counter
->mutex
);
2098 return child_counter
;
2101 static int inherit_group(struct perf_counter
*parent_counter
,
2102 struct task_struct
*parent
,
2103 struct perf_counter_context
*parent_ctx
,
2104 struct task_struct
*child
,
2105 struct perf_counter_context
*child_ctx
)
2107 struct perf_counter
*leader
;
2108 struct perf_counter
*sub
;
2110 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
2111 child
, NULL
, child_ctx
);
2114 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
2115 if (!inherit_counter(sub
, parent
, parent_ctx
,
2116 child
, leader
, child_ctx
))
2122 static void sync_child_counter(struct perf_counter
*child_counter
,
2123 struct perf_counter
*parent_counter
)
2125 u64 parent_val
, child_val
;
2127 parent_val
= atomic64_read(&parent_counter
->count
);
2128 child_val
= atomic64_read(&child_counter
->count
);
2131 * Add back the child's count to the parent's count:
2133 atomic64_add(child_val
, &parent_counter
->count
);
2136 * Remove this counter from the parent's list
2138 mutex_lock(&parent_counter
->mutex
);
2139 list_del_init(&child_counter
->child_list
);
2140 mutex_unlock(&parent_counter
->mutex
);
2143 * Release the parent counter, if this was the last
2146 fput(parent_counter
->filp
);
2150 __perf_counter_exit_task(struct task_struct
*child
,
2151 struct perf_counter
*child_counter
,
2152 struct perf_counter_context
*child_ctx
)
2154 struct perf_counter
*parent_counter
;
2155 struct perf_counter
*sub
, *tmp
;
2158 * If we do not self-reap then we have to wait for the
2159 * child task to unschedule (it will happen for sure),
2160 * so that its counter is at its final count. (This
2161 * condition triggers rarely - child tasks usually get
2162 * off their CPU before the parent has a chance to
2163 * get this far into the reaping action)
2165 if (child
!= current
) {
2166 wait_task_inactive(child
, 0);
2167 list_del_init(&child_counter
->list_entry
);
2169 struct perf_cpu_context
*cpuctx
;
2170 unsigned long flags
;
2174 * Disable and unlink this counter.
2176 * Be careful about zapping the list - IRQ/NMI context
2177 * could still be processing it:
2179 curr_rq_lock_irq_save(&flags
);
2180 perf_flags
= hw_perf_save_disable();
2182 cpuctx
= &__get_cpu_var(perf_cpu_context
);
2184 group_sched_out(child_counter
, cpuctx
, child_ctx
);
2186 list_del_init(&child_counter
->list_entry
);
2188 child_ctx
->nr_counters
--;
2190 hw_perf_restore(perf_flags
);
2191 curr_rq_unlock_irq_restore(&flags
);
2194 parent_counter
= child_counter
->parent
;
2196 * It can happen that parent exits first, and has counters
2197 * that are still around due to the child reference. These
2198 * counters need to be zapped - but otherwise linger.
2200 if (parent_counter
) {
2201 sync_child_counter(child_counter
, parent_counter
);
2202 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
2205 sync_child_counter(sub
, sub
->parent
);
2209 free_counter(child_counter
);
2214 * When a child task exits, feed back counter values to parent counters.
2216 * Note: we may be running in child context, but the PID is not hashed
2217 * anymore so new counters will not be added.
2219 void perf_counter_exit_task(struct task_struct
*child
)
2221 struct perf_counter
*child_counter
, *tmp
;
2222 struct perf_counter_context
*child_ctx
;
2224 child_ctx
= &child
->perf_counter_ctx
;
2226 if (likely(!child_ctx
->nr_counters
))
2229 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
2231 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
2235 * Initialize the perf_counter context in task_struct
2237 void perf_counter_init_task(struct task_struct
*child
)
2239 struct perf_counter_context
*child_ctx
, *parent_ctx
;
2240 struct perf_counter
*counter
;
2241 struct task_struct
*parent
= current
;
2243 child_ctx
= &child
->perf_counter_ctx
;
2244 parent_ctx
= &parent
->perf_counter_ctx
;
2246 __perf_counter_init_context(child_ctx
, child
);
2249 * This is executed from the parent task context, so inherit
2250 * counters that have been marked for cloning:
2253 if (likely(!parent_ctx
->nr_counters
))
2257 * Lock the parent list. No need to lock the child - not PID
2258 * hashed yet and not running, so nobody can access it.
2260 mutex_lock(&parent_ctx
->mutex
);
2263 * We dont have to disable NMIs - we are only looking at
2264 * the list, not manipulating it:
2266 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
2267 if (!counter
->hw_event
.inherit
)
2270 if (inherit_group(counter
, parent
,
2271 parent_ctx
, child
, child_ctx
))
2275 mutex_unlock(&parent_ctx
->mutex
);
2278 static void __cpuinit
perf_counter_init_cpu(int cpu
)
2280 struct perf_cpu_context
*cpuctx
;
2282 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2283 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
2285 mutex_lock(&perf_resource_mutex
);
2286 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
2287 mutex_unlock(&perf_resource_mutex
);
2289 hw_perf_counter_setup(cpu
);
2292 #ifdef CONFIG_HOTPLUG_CPU
2293 static void __perf_counter_exit_cpu(void *info
)
2295 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
2296 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
2297 struct perf_counter
*counter
, *tmp
;
2299 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
2300 __perf_counter_remove_from_context(counter
);
2302 static void perf_counter_exit_cpu(int cpu
)
2304 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2305 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
2307 mutex_lock(&ctx
->mutex
);
2308 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
2309 mutex_unlock(&ctx
->mutex
);
2312 static inline void perf_counter_exit_cpu(int cpu
) { }
2315 static int __cpuinit
2316 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
2318 unsigned int cpu
= (long)hcpu
;
2322 case CPU_UP_PREPARE
:
2323 case CPU_UP_PREPARE_FROZEN
:
2324 perf_counter_init_cpu(cpu
);
2327 case CPU_DOWN_PREPARE
:
2328 case CPU_DOWN_PREPARE_FROZEN
:
2329 perf_counter_exit_cpu(cpu
);
2339 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
2340 .notifier_call
= perf_cpu_notify
,
2343 static int __init
perf_counter_init(void)
2345 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
2346 (void *)(long)smp_processor_id());
2347 register_cpu_notifier(&perf_cpu_nb
);
2351 early_initcall(perf_counter_init
);
2353 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
2355 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
2359 perf_set_reserve_percpu(struct sysdev_class
*class,
2363 struct perf_cpu_context
*cpuctx
;
2367 err
= strict_strtoul(buf
, 10, &val
);
2370 if (val
> perf_max_counters
)
2373 mutex_lock(&perf_resource_mutex
);
2374 perf_reserved_percpu
= val
;
2375 for_each_online_cpu(cpu
) {
2376 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2377 spin_lock_irq(&cpuctx
->ctx
.lock
);
2378 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
2379 perf_max_counters
- perf_reserved_percpu
);
2380 cpuctx
->max_pertask
= mpt
;
2381 spin_unlock_irq(&cpuctx
->ctx
.lock
);
2383 mutex_unlock(&perf_resource_mutex
);
2388 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
2390 return sprintf(buf
, "%d\n", perf_overcommit
);
2394 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
2399 err
= strict_strtoul(buf
, 10, &val
);
2405 mutex_lock(&perf_resource_mutex
);
2406 perf_overcommit
= val
;
2407 mutex_unlock(&perf_resource_mutex
);
2412 static SYSDEV_CLASS_ATTR(
2415 perf_show_reserve_percpu
,
2416 perf_set_reserve_percpu
2419 static SYSDEV_CLASS_ATTR(
2422 perf_show_overcommit
,
2426 static struct attribute
*perfclass_attrs
[] = {
2427 &attr_reserve_percpu
.attr
,
2428 &attr_overcommit
.attr
,
2432 static struct attribute_group perfclass_attr_group
= {
2433 .attrs
= perfclass_attrs
,
2434 .name
= "perf_counters",
2437 static int __init
perf_counter_sysfs_init(void)
2439 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
2440 &perfclass_attr_group
);
2442 device_initcall(perf_counter_sysfs_init
);