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>
27 * Each CPU has a list of per CPU counters:
29 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
31 int perf_max_counters __read_mostly
= 1;
32 static int perf_reserved_percpu __read_mostly
;
33 static int perf_overcommit __read_mostly
= 1;
36 * Mutex for (sysadmin-configurable) counter reservations:
38 static DEFINE_MUTEX(perf_resource_mutex
);
41 * Architecture provided APIs - weak aliases:
43 extern __weak
const struct hw_perf_counter_ops
*
44 hw_perf_counter_init(struct perf_counter
*counter
)
49 u64 __weak
hw_perf_save_disable(void) { return 0; }
50 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
51 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
52 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
53 struct perf_cpu_context
*cpuctx
,
54 struct perf_counter_context
*ctx
, int cpu
)
59 void __weak
perf_counter_print_debug(void) { }
62 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
64 struct perf_counter
*group_leader
= counter
->group_leader
;
67 * Depending on whether it is a standalone or sibling counter,
68 * add it straight to the context's counter list, or to the group
69 * leader's sibling list:
71 if (counter
->group_leader
== counter
)
72 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
74 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
78 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
80 struct perf_counter
*sibling
, *tmp
;
82 list_del_init(&counter
->list_entry
);
85 * If this was a group counter with sibling counters then
86 * upgrade the siblings to singleton counters by adding them
87 * to the context list directly:
89 list_for_each_entry_safe(sibling
, tmp
,
90 &counter
->sibling_list
, list_entry
) {
92 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
93 sibling
->group_leader
= sibling
;
98 counter_sched_out(struct perf_counter
*counter
,
99 struct perf_cpu_context
*cpuctx
,
100 struct perf_counter_context
*ctx
)
102 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
105 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
106 counter
->hw_ops
->disable(counter
);
109 if (!is_software_counter(counter
))
110 cpuctx
->active_oncpu
--;
112 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
113 cpuctx
->exclusive
= 0;
117 group_sched_out(struct perf_counter
*group_counter
,
118 struct perf_cpu_context
*cpuctx
,
119 struct perf_counter_context
*ctx
)
121 struct perf_counter
*counter
;
123 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
126 counter_sched_out(group_counter
, cpuctx
, ctx
);
129 * Schedule out siblings (if any):
131 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
132 counter_sched_out(counter
, cpuctx
, ctx
);
134 if (group_counter
->hw_event
.exclusive
)
135 cpuctx
->exclusive
= 0;
139 * Cross CPU call to remove a performance counter
141 * We disable the counter on the hardware level first. After that we
142 * remove it from the context list.
144 static void __perf_counter_remove_from_context(void *info
)
146 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
147 struct perf_counter
*counter
= info
;
148 struct perf_counter_context
*ctx
= counter
->ctx
;
153 * If this is a task context, we need to check whether it is
154 * the current task context of this cpu. If not it has been
155 * scheduled out before the smp call arrived.
157 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
160 curr_rq_lock_irq_save(&flags
);
161 spin_lock(&ctx
->lock
);
163 counter_sched_out(counter
, cpuctx
, ctx
);
165 counter
->task
= NULL
;
169 * Protect the list operation against NMI by disabling the
170 * counters on a global level. NOP for non NMI based counters.
172 perf_flags
= hw_perf_save_disable();
173 list_del_counter(counter
, ctx
);
174 hw_perf_restore(perf_flags
);
178 * Allow more per task counters with respect to the
181 cpuctx
->max_pertask
=
182 min(perf_max_counters
- ctx
->nr_counters
,
183 perf_max_counters
- perf_reserved_percpu
);
186 spin_unlock(&ctx
->lock
);
187 curr_rq_unlock_irq_restore(&flags
);
192 * Remove the counter from a task's (or a CPU's) list of counters.
194 * Must be called with counter->mutex and ctx->mutex held.
196 * CPU counters are removed with a smp call. For task counters we only
197 * call when the task is on a CPU.
199 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
201 struct perf_counter_context
*ctx
= counter
->ctx
;
202 struct task_struct
*task
= ctx
->task
;
206 * Per cpu counters are removed via an smp call and
207 * the removal is always sucessful.
209 smp_call_function_single(counter
->cpu
,
210 __perf_counter_remove_from_context
,
216 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
219 spin_lock_irq(&ctx
->lock
);
221 * If the context is active we need to retry the smp call.
223 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
224 spin_unlock_irq(&ctx
->lock
);
229 * The lock prevents that this context is scheduled in so we
230 * can remove the counter safely, if the call above did not
233 if (!list_empty(&counter
->list_entry
)) {
235 list_del_counter(counter
, ctx
);
236 counter
->task
= NULL
;
238 spin_unlock_irq(&ctx
->lock
);
242 * Cross CPU call to disable a performance counter
244 static void __perf_counter_disable(void *info
)
246 struct perf_counter
*counter
= info
;
247 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
248 struct perf_counter_context
*ctx
= counter
->ctx
;
252 * If this is a per-task counter, need to check whether this
253 * counter's task is the current task on this cpu.
255 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
258 curr_rq_lock_irq_save(&flags
);
259 spin_lock(&ctx
->lock
);
262 * If the counter is on, turn it off.
263 * If it is in error state, leave it in error state.
265 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
266 if (counter
== counter
->group_leader
)
267 group_sched_out(counter
, cpuctx
, ctx
);
269 counter_sched_out(counter
, cpuctx
, ctx
);
270 counter
->state
= PERF_COUNTER_STATE_OFF
;
273 spin_unlock(&ctx
->lock
);
274 curr_rq_unlock_irq_restore(&flags
);
280 static void perf_counter_disable(struct perf_counter
*counter
)
282 struct perf_counter_context
*ctx
= counter
->ctx
;
283 struct task_struct
*task
= ctx
->task
;
287 * Disable the counter on the cpu that it's on
289 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
295 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
297 spin_lock_irq(&ctx
->lock
);
299 * If the counter is still active, we need to retry the cross-call.
301 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
302 spin_unlock_irq(&ctx
->lock
);
307 * Since we have the lock this context can't be scheduled
308 * in, so we can change the state safely.
310 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
311 counter
->state
= PERF_COUNTER_STATE_OFF
;
313 spin_unlock_irq(&ctx
->lock
);
317 * Disable a counter and all its children.
319 static void perf_counter_disable_family(struct perf_counter
*counter
)
321 struct perf_counter
*child
;
323 perf_counter_disable(counter
);
326 * Lock the mutex to protect the list of children
328 mutex_lock(&counter
->mutex
);
329 list_for_each_entry(child
, &counter
->child_list
, child_list
)
330 perf_counter_disable(child
);
331 mutex_unlock(&counter
->mutex
);
335 counter_sched_in(struct perf_counter
*counter
,
336 struct perf_cpu_context
*cpuctx
,
337 struct perf_counter_context
*ctx
,
340 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
343 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
344 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
346 * The new state must be visible before we turn it on in the hardware:
350 if (counter
->hw_ops
->enable(counter
)) {
351 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
356 if (!is_software_counter(counter
))
357 cpuctx
->active_oncpu
++;
360 if (counter
->hw_event
.exclusive
)
361 cpuctx
->exclusive
= 1;
367 * Return 1 for a group consisting entirely of software counters,
368 * 0 if the group contains any hardware counters.
370 static int is_software_only_group(struct perf_counter
*leader
)
372 struct perf_counter
*counter
;
374 if (!is_software_counter(leader
))
376 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
377 if (!is_software_counter(counter
))
383 * Work out whether we can put this counter group on the CPU now.
385 static int group_can_go_on(struct perf_counter
*counter
,
386 struct perf_cpu_context
*cpuctx
,
390 * Groups consisting entirely of software counters can always go on.
392 if (is_software_only_group(counter
))
395 * If an exclusive group is already on, no other hardware
396 * counters can go on.
398 if (cpuctx
->exclusive
)
401 * If this group is exclusive and there are already
402 * counters on the CPU, it can't go on.
404 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
407 * Otherwise, try to add it if all previous groups were able
414 * Cross CPU call to install and enable a performance counter
416 static void __perf_install_in_context(void *info
)
418 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
419 struct perf_counter
*counter
= info
;
420 struct perf_counter_context
*ctx
= counter
->ctx
;
421 struct perf_counter
*leader
= counter
->group_leader
;
422 int cpu
= smp_processor_id();
428 * If this is a task context, we need to check whether it is
429 * the current task context of this cpu. If not it has been
430 * scheduled out before the smp call arrived.
432 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
435 curr_rq_lock_irq_save(&flags
);
436 spin_lock(&ctx
->lock
);
439 * Protect the list operation against NMI by disabling the
440 * counters on a global level. NOP for non NMI based counters.
442 perf_flags
= hw_perf_save_disable();
444 list_add_counter(counter
, ctx
);
446 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
449 * Don't put the counter on if it is disabled or if
450 * it is in a group and the group isn't on.
452 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
453 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
457 * An exclusive counter can't go on if there are already active
458 * hardware counters, and no hardware counter can go on if there
459 * is already an exclusive counter on.
461 if (!group_can_go_on(counter
, cpuctx
, 1))
464 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
468 * This counter couldn't go on. If it is in a group
469 * then we have to pull the whole group off.
470 * If the counter group is pinned then put it in error state.
472 if (leader
!= counter
)
473 group_sched_out(leader
, cpuctx
, ctx
);
474 if (leader
->hw_event
.pinned
)
475 leader
->state
= PERF_COUNTER_STATE_ERROR
;
478 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
479 cpuctx
->max_pertask
--;
482 hw_perf_restore(perf_flags
);
484 spin_unlock(&ctx
->lock
);
485 curr_rq_unlock_irq_restore(&flags
);
489 * Attach a performance counter to a context
491 * First we add the counter to the list with the hardware enable bit
492 * in counter->hw_config cleared.
494 * If the counter is attached to a task which is on a CPU we use a smp
495 * call to enable it in the task context. The task might have been
496 * scheduled away, but we check this in the smp call again.
498 * Must be called with ctx->mutex held.
501 perf_install_in_context(struct perf_counter_context
*ctx
,
502 struct perf_counter
*counter
,
505 struct task_struct
*task
= ctx
->task
;
509 * Per cpu counters are installed via an smp call and
510 * the install is always sucessful.
512 smp_call_function_single(cpu
, __perf_install_in_context
,
517 counter
->task
= task
;
519 task_oncpu_function_call(task
, __perf_install_in_context
,
522 spin_lock_irq(&ctx
->lock
);
524 * we need to retry the smp call.
526 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
527 spin_unlock_irq(&ctx
->lock
);
532 * The lock prevents that this context is scheduled in so we
533 * can add the counter safely, if it the call above did not
536 if (list_empty(&counter
->list_entry
)) {
537 list_add_counter(counter
, ctx
);
540 spin_unlock_irq(&ctx
->lock
);
544 * Cross CPU call to enable a performance counter
546 static void __perf_counter_enable(void *info
)
548 struct perf_counter
*counter
= info
;
549 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
550 struct perf_counter_context
*ctx
= counter
->ctx
;
551 struct perf_counter
*leader
= counter
->group_leader
;
556 * If this is a per-task counter, need to check whether this
557 * counter's task is the current task on this cpu.
559 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
562 curr_rq_lock_irq_save(&flags
);
563 spin_lock(&ctx
->lock
);
565 counter
->prev_state
= counter
->state
;
566 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
568 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
571 * If the counter is in a group and isn't the group leader,
572 * then don't put it on unless the group is on.
574 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
577 if (!group_can_go_on(counter
, cpuctx
, 1))
580 err
= counter_sched_in(counter
, cpuctx
, ctx
,
585 * If this counter can't go on and it's part of a
586 * group, then the whole group has to come off.
588 if (leader
!= counter
)
589 group_sched_out(leader
, cpuctx
, ctx
);
590 if (leader
->hw_event
.pinned
)
591 leader
->state
= PERF_COUNTER_STATE_ERROR
;
595 spin_unlock(&ctx
->lock
);
596 curr_rq_unlock_irq_restore(&flags
);
602 static void perf_counter_enable(struct perf_counter
*counter
)
604 struct perf_counter_context
*ctx
= counter
->ctx
;
605 struct task_struct
*task
= ctx
->task
;
609 * Enable the counter on the cpu that it's on
611 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
616 spin_lock_irq(&ctx
->lock
);
617 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
621 * If the counter is in error state, clear that first.
622 * That way, if we see the counter in error state below, we
623 * know that it has gone back into error state, as distinct
624 * from the task having been scheduled away before the
625 * cross-call arrived.
627 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
628 counter
->state
= PERF_COUNTER_STATE_OFF
;
631 spin_unlock_irq(&ctx
->lock
);
632 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
634 spin_lock_irq(&ctx
->lock
);
637 * If the context is active and the counter is still off,
638 * we need to retry the cross-call.
640 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
644 * Since we have the lock this context can't be scheduled
645 * in, so we can change the state safely.
647 if (counter
->state
== PERF_COUNTER_STATE_OFF
)
648 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
650 spin_unlock_irq(&ctx
->lock
);
654 * Enable a counter and all its children.
656 static void perf_counter_enable_family(struct perf_counter
*counter
)
658 struct perf_counter
*child
;
660 perf_counter_enable(counter
);
663 * Lock the mutex to protect the list of children
665 mutex_lock(&counter
->mutex
);
666 list_for_each_entry(child
, &counter
->child_list
, child_list
)
667 perf_counter_enable(child
);
668 mutex_unlock(&counter
->mutex
);
671 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
672 struct perf_cpu_context
*cpuctx
)
674 struct perf_counter
*counter
;
677 spin_lock(&ctx
->lock
);
679 if (likely(!ctx
->nr_counters
))
682 flags
= hw_perf_save_disable();
683 if (ctx
->nr_active
) {
684 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
685 group_sched_out(counter
, cpuctx
, ctx
);
687 hw_perf_restore(flags
);
689 spin_unlock(&ctx
->lock
);
693 * Called from scheduler to remove the counters of the current task,
694 * with interrupts disabled.
696 * We stop each counter and update the counter value in counter->count.
698 * This does not protect us against NMI, but disable()
699 * sets the disabled bit in the control field of counter _before_
700 * accessing the counter control register. If a NMI hits, then it will
701 * not restart the counter.
703 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
705 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
706 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
708 if (likely(!cpuctx
->task_ctx
))
711 __perf_counter_sched_out(ctx
, cpuctx
);
713 cpuctx
->task_ctx
= NULL
;
716 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
718 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
722 group_sched_in(struct perf_counter
*group_counter
,
723 struct perf_cpu_context
*cpuctx
,
724 struct perf_counter_context
*ctx
,
727 struct perf_counter
*counter
, *partial_group
;
730 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
733 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
735 return ret
< 0 ? ret
: 0;
737 group_counter
->prev_state
= group_counter
->state
;
738 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
742 * Schedule in siblings as one group (if any):
744 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
745 counter
->prev_state
= counter
->state
;
746 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
747 partial_group
= counter
;
756 * Groups can be scheduled in as one unit only, so undo any
757 * partial group before returning:
759 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
760 if (counter
== partial_group
)
762 counter_sched_out(counter
, cpuctx
, ctx
);
764 counter_sched_out(group_counter
, cpuctx
, ctx
);
770 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
771 struct perf_cpu_context
*cpuctx
, int cpu
)
773 struct perf_counter
*counter
;
777 spin_lock(&ctx
->lock
);
779 if (likely(!ctx
->nr_counters
))
782 flags
= hw_perf_save_disable();
785 * First go through the list and put on any pinned groups
786 * in order to give them the best chance of going on.
788 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
789 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
790 !counter
->hw_event
.pinned
)
792 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
795 if (group_can_go_on(counter
, cpuctx
, 1))
796 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
799 * If this pinned group hasn't been scheduled,
800 * put it in error state.
802 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
803 counter
->state
= PERF_COUNTER_STATE_ERROR
;
806 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
808 * Ignore counters in OFF or ERROR state, and
809 * ignore pinned counters since we did them already.
811 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
812 counter
->hw_event
.pinned
)
816 * Listen to the 'cpu' scheduling filter constraint
819 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
822 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
823 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
827 hw_perf_restore(flags
);
829 spin_unlock(&ctx
->lock
);
833 * Called from scheduler to add the counters of the current task
834 * with interrupts disabled.
836 * We restore the counter value and then enable it.
838 * This does not protect us against NMI, but enable()
839 * sets the enabled bit in the control field of counter _before_
840 * accessing the counter control register. If a NMI hits, then it will
841 * keep the counter running.
843 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
845 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
846 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
848 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
849 cpuctx
->task_ctx
= ctx
;
852 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
854 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
856 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
859 int perf_counter_task_disable(void)
861 struct task_struct
*curr
= current
;
862 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
863 struct perf_counter
*counter
;
868 if (likely(!ctx
->nr_counters
))
871 curr_rq_lock_irq_save(&flags
);
872 cpu
= smp_processor_id();
874 /* force the update of the task clock: */
875 __task_delta_exec(curr
, 1);
877 perf_counter_task_sched_out(curr
, cpu
);
879 spin_lock(&ctx
->lock
);
882 * Disable all the counters:
884 perf_flags
= hw_perf_save_disable();
886 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
887 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
)
888 counter
->state
= PERF_COUNTER_STATE_OFF
;
891 hw_perf_restore(perf_flags
);
893 spin_unlock(&ctx
->lock
);
895 curr_rq_unlock_irq_restore(&flags
);
900 int perf_counter_task_enable(void)
902 struct task_struct
*curr
= current
;
903 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
904 struct perf_counter
*counter
;
909 if (likely(!ctx
->nr_counters
))
912 curr_rq_lock_irq_save(&flags
);
913 cpu
= smp_processor_id();
915 /* force the update of the task clock: */
916 __task_delta_exec(curr
, 1);
918 perf_counter_task_sched_out(curr
, cpu
);
920 spin_lock(&ctx
->lock
);
923 * Disable all the counters:
925 perf_flags
= hw_perf_save_disable();
927 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
928 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
930 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
931 counter
->hw_event
.disabled
= 0;
933 hw_perf_restore(perf_flags
);
935 spin_unlock(&ctx
->lock
);
937 perf_counter_task_sched_in(curr
, cpu
);
939 curr_rq_unlock_irq_restore(&flags
);
945 * Round-robin a context's counters:
947 static void rotate_ctx(struct perf_counter_context
*ctx
)
949 struct perf_counter
*counter
;
952 if (!ctx
->nr_counters
)
955 spin_lock(&ctx
->lock
);
957 * Rotate the first entry last (works just fine for group counters too):
959 perf_flags
= hw_perf_save_disable();
960 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
961 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
964 hw_perf_restore(perf_flags
);
966 spin_unlock(&ctx
->lock
);
969 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
971 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
972 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
973 const int rotate_percpu
= 0;
976 perf_counter_cpu_sched_out(cpuctx
);
977 perf_counter_task_sched_out(curr
, cpu
);
980 rotate_ctx(&cpuctx
->ctx
);
984 perf_counter_cpu_sched_in(cpuctx
, cpu
);
985 perf_counter_task_sched_in(curr
, cpu
);
989 * Cross CPU call to read the hardware counter
991 static void __read(void *info
)
993 struct perf_counter
*counter
= info
;
996 curr_rq_lock_irq_save(&flags
);
997 counter
->hw_ops
->read(counter
);
998 curr_rq_unlock_irq_restore(&flags
);
1001 static u64
perf_counter_read(struct perf_counter
*counter
)
1004 * If counter is enabled and currently active on a CPU, update the
1005 * value in the counter structure:
1007 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1008 smp_call_function_single(counter
->oncpu
,
1009 __read
, counter
, 1);
1012 return atomic64_read(&counter
->count
);
1016 * Cross CPU call to switch performance data pointers
1018 static void __perf_switch_irq_data(void *info
)
1020 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1021 struct perf_counter
*counter
= info
;
1022 struct perf_counter_context
*ctx
= counter
->ctx
;
1023 struct perf_data
*oldirqdata
= counter
->irqdata
;
1026 * If this is a task context, we need to check whether it is
1027 * the current task context of this cpu. If not it has been
1028 * scheduled out before the smp call arrived.
1031 if (cpuctx
->task_ctx
!= ctx
)
1033 spin_lock(&ctx
->lock
);
1036 /* Change the pointer NMI safe */
1037 atomic_long_set((atomic_long_t
*)&counter
->irqdata
,
1038 (unsigned long) counter
->usrdata
);
1039 counter
->usrdata
= oldirqdata
;
1042 spin_unlock(&ctx
->lock
);
1045 static struct perf_data
*perf_switch_irq_data(struct perf_counter
*counter
)
1047 struct perf_counter_context
*ctx
= counter
->ctx
;
1048 struct perf_data
*oldirqdata
= counter
->irqdata
;
1049 struct task_struct
*task
= ctx
->task
;
1052 smp_call_function_single(counter
->cpu
,
1053 __perf_switch_irq_data
,
1055 return counter
->usrdata
;
1059 spin_lock_irq(&ctx
->lock
);
1060 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
) {
1061 counter
->irqdata
= counter
->usrdata
;
1062 counter
->usrdata
= oldirqdata
;
1063 spin_unlock_irq(&ctx
->lock
);
1066 spin_unlock_irq(&ctx
->lock
);
1067 task_oncpu_function_call(task
, __perf_switch_irq_data
, counter
);
1068 /* Might have failed, because task was scheduled out */
1069 if (counter
->irqdata
== oldirqdata
)
1072 return counter
->usrdata
;
1075 static void put_context(struct perf_counter_context
*ctx
)
1078 put_task_struct(ctx
->task
);
1081 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1083 struct perf_cpu_context
*cpuctx
;
1084 struct perf_counter_context
*ctx
;
1085 struct task_struct
*task
;
1088 * If cpu is not a wildcard then this is a percpu counter:
1091 /* Must be root to operate on a CPU counter: */
1092 if (!capable(CAP_SYS_ADMIN
))
1093 return ERR_PTR(-EACCES
);
1095 if (cpu
< 0 || cpu
> num_possible_cpus())
1096 return ERR_PTR(-EINVAL
);
1099 * We could be clever and allow to attach a counter to an
1100 * offline CPU and activate it when the CPU comes up, but
1103 if (!cpu_isset(cpu
, cpu_online_map
))
1104 return ERR_PTR(-ENODEV
);
1106 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1116 task
= find_task_by_vpid(pid
);
1118 get_task_struct(task
);
1122 return ERR_PTR(-ESRCH
);
1124 ctx
= &task
->perf_counter_ctx
;
1127 /* Reuse ptrace permission checks for now. */
1128 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1130 return ERR_PTR(-EACCES
);
1137 * Called when the last reference to the file is gone.
1139 static int perf_release(struct inode
*inode
, struct file
*file
)
1141 struct perf_counter
*counter
= file
->private_data
;
1142 struct perf_counter_context
*ctx
= counter
->ctx
;
1144 file
->private_data
= NULL
;
1146 mutex_lock(&ctx
->mutex
);
1147 mutex_lock(&counter
->mutex
);
1149 perf_counter_remove_from_context(counter
);
1151 mutex_unlock(&counter
->mutex
);
1152 mutex_unlock(&ctx
->mutex
);
1161 * Read the performance counter - simple non blocking version for now
1164 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1168 if (count
!= sizeof(cntval
))
1172 * Return end-of-file for a read on a counter that is in
1173 * error state (i.e. because it was pinned but it couldn't be
1174 * scheduled on to the CPU at some point).
1176 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1179 mutex_lock(&counter
->mutex
);
1180 cntval
= perf_counter_read(counter
);
1181 mutex_unlock(&counter
->mutex
);
1183 return put_user(cntval
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(cntval
);
1187 perf_copy_usrdata(struct perf_data
*usrdata
, char __user
*buf
, size_t count
)
1192 count
= min(count
, (size_t)usrdata
->len
);
1193 if (copy_to_user(buf
, usrdata
->data
+ usrdata
->rd_idx
, count
))
1196 /* Adjust the counters */
1197 usrdata
->len
-= count
;
1199 usrdata
->rd_idx
= 0;
1201 usrdata
->rd_idx
+= count
;
1207 perf_read_irq_data(struct perf_counter
*counter
,
1212 struct perf_data
*irqdata
, *usrdata
;
1213 DECLARE_WAITQUEUE(wait
, current
);
1216 irqdata
= counter
->irqdata
;
1217 usrdata
= counter
->usrdata
;
1219 if (usrdata
->len
+ irqdata
->len
>= count
)
1225 spin_lock_irq(&counter
->waitq
.lock
);
1226 __add_wait_queue(&counter
->waitq
, &wait
);
1228 set_current_state(TASK_INTERRUPTIBLE
);
1229 if (usrdata
->len
+ irqdata
->len
>= count
)
1232 if (signal_pending(current
))
1235 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1238 spin_unlock_irq(&counter
->waitq
.lock
);
1240 spin_lock_irq(&counter
->waitq
.lock
);
1242 __remove_wait_queue(&counter
->waitq
, &wait
);
1243 __set_current_state(TASK_RUNNING
);
1244 spin_unlock_irq(&counter
->waitq
.lock
);
1246 if (usrdata
->len
+ irqdata
->len
< count
&&
1247 counter
->state
!= PERF_COUNTER_STATE_ERROR
)
1248 return -ERESTARTSYS
;
1250 mutex_lock(&counter
->mutex
);
1252 /* Drain pending data first: */
1253 res
= perf_copy_usrdata(usrdata
, buf
, count
);
1254 if (res
< 0 || res
== count
)
1257 /* Switch irq buffer: */
1258 usrdata
= perf_switch_irq_data(counter
);
1259 res2
= perf_copy_usrdata(usrdata
, buf
+ res
, count
- res
);
1267 mutex_unlock(&counter
->mutex
);
1273 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1275 struct perf_counter
*counter
= file
->private_data
;
1277 switch (counter
->hw_event
.record_type
) {
1278 case PERF_RECORD_SIMPLE
:
1279 return perf_read_hw(counter
, buf
, count
);
1281 case PERF_RECORD_IRQ
:
1282 case PERF_RECORD_GROUP
:
1283 return perf_read_irq_data(counter
, buf
, count
,
1284 file
->f_flags
& O_NONBLOCK
);
1289 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1291 struct perf_counter
*counter
= file
->private_data
;
1292 unsigned int events
= 0;
1293 unsigned long flags
;
1295 poll_wait(file
, &counter
->waitq
, wait
);
1297 spin_lock_irqsave(&counter
->waitq
.lock
, flags
);
1298 if (counter
->usrdata
->len
|| counter
->irqdata
->len
)
1300 spin_unlock_irqrestore(&counter
->waitq
.lock
, flags
);
1305 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1307 struct perf_counter
*counter
= file
->private_data
;
1311 case PERF_COUNTER_IOC_ENABLE
:
1312 perf_counter_enable_family(counter
);
1314 case PERF_COUNTER_IOC_DISABLE
:
1315 perf_counter_disable_family(counter
);
1323 static const struct file_operations perf_fops
= {
1324 .release
= perf_release
,
1327 .unlocked_ioctl
= perf_ioctl
,
1328 .compat_ioctl
= perf_ioctl
,
1332 * Generic software counter infrastructure
1335 static void perf_swcounter_update(struct perf_counter
*counter
)
1337 struct hw_perf_counter
*hwc
= &counter
->hw
;
1342 prev
= atomic64_read(&hwc
->prev_count
);
1343 now
= atomic64_read(&hwc
->count
);
1344 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
1349 atomic64_add(delta
, &counter
->count
);
1350 atomic64_sub(delta
, &hwc
->period_left
);
1353 static void perf_swcounter_set_period(struct perf_counter
*counter
)
1355 struct hw_perf_counter
*hwc
= &counter
->hw
;
1356 s64 left
= atomic64_read(&hwc
->period_left
);
1357 s64 period
= hwc
->irq_period
;
1359 if (unlikely(left
<= -period
)) {
1361 atomic64_set(&hwc
->period_left
, left
);
1364 if (unlikely(left
<= 0)) {
1366 atomic64_add(period
, &hwc
->period_left
);
1369 atomic64_set(&hwc
->prev_count
, -left
);
1370 atomic64_set(&hwc
->count
, -left
);
1373 static void perf_swcounter_save_and_restart(struct perf_counter
*counter
)
1375 perf_swcounter_update(counter
);
1376 perf_swcounter_set_period(counter
);
1379 static void perf_swcounter_store_irq(struct perf_counter
*counter
, u64 data
)
1381 struct perf_data
*irqdata
= counter
->irqdata
;
1383 if (irqdata
->len
> PERF_DATA_BUFLEN
- sizeof(u64
)) {
1386 u64
*p
= (u64
*) &irqdata
->data
[irqdata
->len
];
1389 irqdata
->len
+= sizeof(u64
);
1393 static void perf_swcounter_handle_group(struct perf_counter
*sibling
)
1395 struct perf_counter
*counter
, *group_leader
= sibling
->group_leader
;
1397 list_for_each_entry(counter
, &group_leader
->sibling_list
, list_entry
) {
1398 counter
->hw_ops
->read(counter
);
1399 perf_swcounter_store_irq(sibling
, counter
->hw_event
.type
);
1400 perf_swcounter_store_irq(sibling
, atomic64_read(&counter
->count
));
1404 static void perf_swcounter_interrupt(struct perf_counter
*counter
,
1405 int nmi
, struct pt_regs
*regs
)
1407 switch (counter
->hw_event
.record_type
) {
1408 case PERF_RECORD_SIMPLE
:
1411 case PERF_RECORD_IRQ
:
1412 perf_swcounter_store_irq(counter
, instruction_pointer(regs
));
1415 case PERF_RECORD_GROUP
:
1416 perf_swcounter_handle_group(counter
);
1421 counter
->wakeup_pending
= 1;
1422 set_tsk_thread_flag(current
, TIF_PERF_COUNTERS
);
1424 wake_up(&counter
->waitq
);
1427 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
1429 struct perf_counter
*counter
;
1430 struct pt_regs
*regs
;
1432 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
1433 counter
->hw_ops
->read(counter
);
1435 regs
= get_irq_regs();
1437 * In case we exclude kernel IPs or are somehow not in interrupt
1438 * context, provide the next best thing, the user IP.
1440 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
1441 !counter
->hw_event
.exclude_user
)
1442 regs
= task_pt_regs(current
);
1445 perf_swcounter_interrupt(counter
, 0, regs
);
1447 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
1449 return HRTIMER_RESTART
;
1452 static void perf_swcounter_overflow(struct perf_counter
*counter
,
1453 int nmi
, struct pt_regs
*regs
)
1455 perf_swcounter_save_and_restart(counter
);
1456 perf_swcounter_interrupt(counter
, nmi
, regs
);
1459 static int perf_swcounter_match(struct perf_counter
*counter
,
1460 enum hw_event_types event
,
1461 struct pt_regs
*regs
)
1463 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1466 if (counter
->hw_event
.raw
)
1469 if (counter
->hw_event
.type
!= event
)
1472 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
1475 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
1481 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
1482 int nmi
, struct pt_regs
*regs
)
1484 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
1485 if (counter
->hw
.irq_period
&& !neg
)
1486 perf_swcounter_overflow(counter
, nmi
, regs
);
1489 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
1490 enum hw_event_types event
, u64 nr
,
1491 int nmi
, struct pt_regs
*regs
)
1493 struct perf_counter
*counter
;
1494 unsigned long flags
;
1496 if (list_empty(&ctx
->counter_list
))
1499 spin_lock_irqsave(&ctx
->lock
, flags
);
1502 * XXX: make counter_list RCU safe
1504 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1505 if (perf_swcounter_match(counter
, event
, regs
))
1506 perf_swcounter_add(counter
, nr
, nmi
, regs
);
1509 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1512 void perf_swcounter_event(enum hw_event_types event
, u64 nr
,
1513 int nmi
, struct pt_regs
*regs
)
1515 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
1517 perf_swcounter_ctx_event(&cpuctx
->ctx
, event
, nr
, nmi
, regs
);
1518 if (cpuctx
->task_ctx
)
1519 perf_swcounter_ctx_event(cpuctx
->task_ctx
, event
, nr
, nmi
, regs
);
1521 put_cpu_var(perf_cpu_context
);
1524 static void perf_swcounter_read(struct perf_counter
*counter
)
1526 perf_swcounter_update(counter
);
1529 static int perf_swcounter_enable(struct perf_counter
*counter
)
1531 perf_swcounter_set_period(counter
);
1535 static void perf_swcounter_disable(struct perf_counter
*counter
)
1537 perf_swcounter_update(counter
);
1540 static const struct hw_perf_counter_ops perf_ops_generic
= {
1541 .enable
= perf_swcounter_enable
,
1542 .disable
= perf_swcounter_disable
,
1543 .read
= perf_swcounter_read
,
1547 * Software counter: cpu wall time clock
1550 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
1552 int cpu
= raw_smp_processor_id();
1556 now
= cpu_clock(cpu
);
1557 prev
= atomic64_read(&counter
->hw
.prev_count
);
1558 atomic64_set(&counter
->hw
.prev_count
, now
);
1559 atomic64_add(now
- prev
, &counter
->count
);
1562 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
1564 struct hw_perf_counter
*hwc
= &counter
->hw
;
1565 int cpu
= raw_smp_processor_id();
1567 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
1568 if (hwc
->irq_period
) {
1569 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1570 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
1571 __hrtimer_start_range_ns(&hwc
->hrtimer
,
1572 ns_to_ktime(hwc
->irq_period
), 0,
1573 HRTIMER_MODE_REL
, 0);
1579 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
1581 hrtimer_cancel(&counter
->hw
.hrtimer
);
1582 cpu_clock_perf_counter_update(counter
);
1585 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
1587 cpu_clock_perf_counter_update(counter
);
1590 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
1591 .enable
= cpu_clock_perf_counter_enable
,
1592 .disable
= cpu_clock_perf_counter_disable
,
1593 .read
= cpu_clock_perf_counter_read
,
1597 * Software counter: task time clock
1601 * Called from within the scheduler:
1603 static u64
task_clock_perf_counter_val(struct perf_counter
*counter
, int update
)
1605 struct task_struct
*curr
= counter
->task
;
1608 delta
= __task_delta_exec(curr
, update
);
1610 return curr
->se
.sum_exec_runtime
+ delta
;
1613 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
1618 prev
= atomic64_read(&counter
->hw
.prev_count
);
1620 atomic64_set(&counter
->hw
.prev_count
, now
);
1624 atomic64_add(delta
, &counter
->count
);
1627 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
1629 struct hw_perf_counter
*hwc
= &counter
->hw
;
1631 atomic64_set(&hwc
->prev_count
, task_clock_perf_counter_val(counter
, 0));
1632 if (hwc
->irq_period
) {
1633 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1634 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
1635 __hrtimer_start_range_ns(&hwc
->hrtimer
,
1636 ns_to_ktime(hwc
->irq_period
), 0,
1637 HRTIMER_MODE_REL
, 0);
1643 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
1645 hrtimer_cancel(&counter
->hw
.hrtimer
);
1646 task_clock_perf_counter_update(counter
,
1647 task_clock_perf_counter_val(counter
, 0));
1650 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
1652 task_clock_perf_counter_update(counter
,
1653 task_clock_perf_counter_val(counter
, 1));
1656 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
1657 .enable
= task_clock_perf_counter_enable
,
1658 .disable
= task_clock_perf_counter_disable
,
1659 .read
= task_clock_perf_counter_read
,
1663 * Software counter: context switches
1666 static u64
get_context_switches(struct perf_counter
*counter
)
1668 struct task_struct
*curr
= counter
->ctx
->task
;
1671 return curr
->nvcsw
+ curr
->nivcsw
;
1672 return cpu_nr_switches(smp_processor_id());
1675 static void context_switches_perf_counter_update(struct perf_counter
*counter
)
1680 prev
= atomic64_read(&counter
->hw
.prev_count
);
1681 now
= get_context_switches(counter
);
1683 atomic64_set(&counter
->hw
.prev_count
, now
);
1687 atomic64_add(delta
, &counter
->count
);
1690 static void context_switches_perf_counter_read(struct perf_counter
*counter
)
1692 context_switches_perf_counter_update(counter
);
1695 static int context_switches_perf_counter_enable(struct perf_counter
*counter
)
1697 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
1698 atomic64_set(&counter
->hw
.prev_count
,
1699 get_context_switches(counter
));
1703 static void context_switches_perf_counter_disable(struct perf_counter
*counter
)
1705 context_switches_perf_counter_update(counter
);
1708 static const struct hw_perf_counter_ops perf_ops_context_switches
= {
1709 .enable
= context_switches_perf_counter_enable
,
1710 .disable
= context_switches_perf_counter_disable
,
1711 .read
= context_switches_perf_counter_read
,
1715 * Software counter: cpu migrations
1718 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
1720 struct task_struct
*curr
= counter
->ctx
->task
;
1723 return curr
->se
.nr_migrations
;
1724 return cpu_nr_migrations(smp_processor_id());
1727 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
1732 prev
= atomic64_read(&counter
->hw
.prev_count
);
1733 now
= get_cpu_migrations(counter
);
1735 atomic64_set(&counter
->hw
.prev_count
, now
);
1739 atomic64_add(delta
, &counter
->count
);
1742 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
1744 cpu_migrations_perf_counter_update(counter
);
1747 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
1749 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
1750 atomic64_set(&counter
->hw
.prev_count
,
1751 get_cpu_migrations(counter
));
1755 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
1757 cpu_migrations_perf_counter_update(counter
);
1760 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
1761 .enable
= cpu_migrations_perf_counter_enable
,
1762 .disable
= cpu_migrations_perf_counter_disable
,
1763 .read
= cpu_migrations_perf_counter_read
,
1766 static const struct hw_perf_counter_ops
*
1767 sw_perf_counter_init(struct perf_counter
*counter
)
1769 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
1770 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
1771 struct hw_perf_counter
*hwc
= &counter
->hw
;
1774 * Software counters (currently) can't in general distinguish
1775 * between user, kernel and hypervisor events.
1776 * However, context switches and cpu migrations are considered
1777 * to be kernel events, and page faults are never hypervisor
1780 switch (counter
->hw_event
.type
) {
1781 case PERF_COUNT_CPU_CLOCK
:
1782 hw_ops
= &perf_ops_cpu_clock
;
1784 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
1785 hw_event
->irq_period
= 10000;
1787 case PERF_COUNT_TASK_CLOCK
:
1789 * If the user instantiates this as a per-cpu counter,
1790 * use the cpu_clock counter instead.
1792 if (counter
->ctx
->task
)
1793 hw_ops
= &perf_ops_task_clock
;
1795 hw_ops
= &perf_ops_cpu_clock
;
1797 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
1798 hw_event
->irq_period
= 10000;
1800 case PERF_COUNT_PAGE_FAULTS
:
1801 case PERF_COUNT_PAGE_FAULTS_MIN
:
1802 case PERF_COUNT_PAGE_FAULTS_MAJ
:
1803 hw_ops
= &perf_ops_generic
;
1805 case PERF_COUNT_CONTEXT_SWITCHES
:
1806 if (!counter
->hw_event
.exclude_kernel
)
1807 hw_ops
= &perf_ops_context_switches
;
1809 case PERF_COUNT_CPU_MIGRATIONS
:
1810 if (!counter
->hw_event
.exclude_kernel
)
1811 hw_ops
= &perf_ops_cpu_migrations
;
1818 hwc
->irq_period
= hw_event
->irq_period
;
1824 * Allocate and initialize a counter structure
1826 static struct perf_counter
*
1827 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
1829 struct perf_counter_context
*ctx
,
1830 struct perf_counter
*group_leader
,
1833 const struct hw_perf_counter_ops
*hw_ops
;
1834 struct perf_counter
*counter
;
1836 counter
= kzalloc(sizeof(*counter
), gfpflags
);
1841 * Single counters are their own group leaders, with an
1842 * empty sibling list:
1845 group_leader
= counter
;
1847 mutex_init(&counter
->mutex
);
1848 INIT_LIST_HEAD(&counter
->list_entry
);
1849 INIT_LIST_HEAD(&counter
->sibling_list
);
1850 init_waitqueue_head(&counter
->waitq
);
1852 INIT_LIST_HEAD(&counter
->child_list
);
1854 counter
->irqdata
= &counter
->data
[0];
1855 counter
->usrdata
= &counter
->data
[1];
1857 counter
->hw_event
= *hw_event
;
1858 counter
->wakeup_pending
= 0;
1859 counter
->group_leader
= group_leader
;
1860 counter
->hw_ops
= NULL
;
1863 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1864 if (hw_event
->disabled
)
1865 counter
->state
= PERF_COUNTER_STATE_OFF
;
1868 if (!hw_event
->raw
&& hw_event
->type
< 0)
1869 hw_ops
= sw_perf_counter_init(counter
);
1871 hw_ops
= hw_perf_counter_init(counter
);
1877 counter
->hw_ops
= hw_ops
;
1883 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
1885 * @hw_event_uptr: event type attributes for monitoring/sampling
1888 * @group_fd: group leader counter fd
1890 SYSCALL_DEFINE5(perf_counter_open
,
1891 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
1892 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
1894 struct perf_counter
*counter
, *group_leader
;
1895 struct perf_counter_hw_event hw_event
;
1896 struct perf_counter_context
*ctx
;
1897 struct file
*counter_file
= NULL
;
1898 struct file
*group_file
= NULL
;
1899 int fput_needed
= 0;
1900 int fput_needed2
= 0;
1903 /* for future expandability... */
1907 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
1911 * Get the target context (task or percpu):
1913 ctx
= find_get_context(pid
, cpu
);
1915 return PTR_ERR(ctx
);
1918 * Look up the group leader (we will attach this counter to it):
1920 group_leader
= NULL
;
1921 if (group_fd
!= -1) {
1923 group_file
= fget_light(group_fd
, &fput_needed
);
1925 goto err_put_context
;
1926 if (group_file
->f_op
!= &perf_fops
)
1927 goto err_put_context
;
1929 group_leader
= group_file
->private_data
;
1931 * Do not allow a recursive hierarchy (this new sibling
1932 * becoming part of another group-sibling):
1934 if (group_leader
->group_leader
!= group_leader
)
1935 goto err_put_context
;
1937 * Do not allow to attach to a group in a different
1938 * task or CPU context:
1940 if (group_leader
->ctx
!= ctx
)
1941 goto err_put_context
;
1943 * Only a group leader can be exclusive or pinned
1945 if (hw_event
.exclusive
|| hw_event
.pinned
)
1946 goto err_put_context
;
1950 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
1953 goto err_put_context
;
1955 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
1957 goto err_free_put_context
;
1959 counter_file
= fget_light(ret
, &fput_needed2
);
1961 goto err_free_put_context
;
1963 counter
->filp
= counter_file
;
1964 mutex_lock(&ctx
->mutex
);
1965 perf_install_in_context(ctx
, counter
, cpu
);
1966 mutex_unlock(&ctx
->mutex
);
1968 fput_light(counter_file
, fput_needed2
);
1971 fput_light(group_file
, fput_needed
);
1975 err_free_put_context
:
1985 * Initialize the perf_counter context in a task_struct:
1988 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1989 struct task_struct
*task
)
1991 memset(ctx
, 0, sizeof(*ctx
));
1992 spin_lock_init(&ctx
->lock
);
1993 mutex_init(&ctx
->mutex
);
1994 INIT_LIST_HEAD(&ctx
->counter_list
);
1999 * inherit a counter from parent task to child task:
2001 static struct perf_counter
*
2002 inherit_counter(struct perf_counter
*parent_counter
,
2003 struct task_struct
*parent
,
2004 struct perf_counter_context
*parent_ctx
,
2005 struct task_struct
*child
,
2006 struct perf_counter
*group_leader
,
2007 struct perf_counter_context
*child_ctx
)
2009 struct perf_counter
*child_counter
;
2012 * Instead of creating recursive hierarchies of counters,
2013 * we link inherited counters back to the original parent,
2014 * which has a filp for sure, which we use as the reference
2017 if (parent_counter
->parent
)
2018 parent_counter
= parent_counter
->parent
;
2020 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2021 parent_counter
->cpu
, child_ctx
,
2022 group_leader
, GFP_KERNEL
);
2027 * Link it up in the child's context:
2029 child_counter
->task
= child
;
2030 list_add_counter(child_counter
, child_ctx
);
2031 child_ctx
->nr_counters
++;
2033 child_counter
->parent
= parent_counter
;
2035 * inherit into child's child as well:
2037 child_counter
->hw_event
.inherit
= 1;
2040 * Get a reference to the parent filp - we will fput it
2041 * when the child counter exits. This is safe to do because
2042 * we are in the parent and we know that the filp still
2043 * exists and has a nonzero count:
2045 atomic_long_inc(&parent_counter
->filp
->f_count
);
2048 * Link this into the parent counter's child list
2050 mutex_lock(&parent_counter
->mutex
);
2051 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
2054 * Make the child state follow the state of the parent counter,
2055 * not its hw_event.disabled bit. We hold the parent's mutex,
2056 * so we won't race with perf_counter_{en,dis}able_family.
2058 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
2059 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2061 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
2063 mutex_unlock(&parent_counter
->mutex
);
2065 return child_counter
;
2068 static int inherit_group(struct perf_counter
*parent_counter
,
2069 struct task_struct
*parent
,
2070 struct perf_counter_context
*parent_ctx
,
2071 struct task_struct
*child
,
2072 struct perf_counter_context
*child_ctx
)
2074 struct perf_counter
*leader
;
2075 struct perf_counter
*sub
;
2077 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
2078 child
, NULL
, child_ctx
);
2081 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
2082 if (!inherit_counter(sub
, parent
, parent_ctx
,
2083 child
, leader
, child_ctx
))
2089 static void sync_child_counter(struct perf_counter
*child_counter
,
2090 struct perf_counter
*parent_counter
)
2092 u64 parent_val
, child_val
;
2094 parent_val
= atomic64_read(&parent_counter
->count
);
2095 child_val
= atomic64_read(&child_counter
->count
);
2098 * Add back the child's count to the parent's count:
2100 atomic64_add(child_val
, &parent_counter
->count
);
2103 * Remove this counter from the parent's list
2105 mutex_lock(&parent_counter
->mutex
);
2106 list_del_init(&child_counter
->child_list
);
2107 mutex_unlock(&parent_counter
->mutex
);
2110 * Release the parent counter, if this was the last
2113 fput(parent_counter
->filp
);
2117 __perf_counter_exit_task(struct task_struct
*child
,
2118 struct perf_counter
*child_counter
,
2119 struct perf_counter_context
*child_ctx
)
2121 struct perf_counter
*parent_counter
;
2122 struct perf_counter
*sub
, *tmp
;
2125 * If we do not self-reap then we have to wait for the
2126 * child task to unschedule (it will happen for sure),
2127 * so that its counter is at its final count. (This
2128 * condition triggers rarely - child tasks usually get
2129 * off their CPU before the parent has a chance to
2130 * get this far into the reaping action)
2132 if (child
!= current
) {
2133 wait_task_inactive(child
, 0);
2134 list_del_init(&child_counter
->list_entry
);
2136 struct perf_cpu_context
*cpuctx
;
2137 unsigned long flags
;
2141 * Disable and unlink this counter.
2143 * Be careful about zapping the list - IRQ/NMI context
2144 * could still be processing it:
2146 curr_rq_lock_irq_save(&flags
);
2147 perf_flags
= hw_perf_save_disable();
2149 cpuctx
= &__get_cpu_var(perf_cpu_context
);
2151 group_sched_out(child_counter
, cpuctx
, child_ctx
);
2153 list_del_init(&child_counter
->list_entry
);
2155 child_ctx
->nr_counters
--;
2157 hw_perf_restore(perf_flags
);
2158 curr_rq_unlock_irq_restore(&flags
);
2161 parent_counter
= child_counter
->parent
;
2163 * It can happen that parent exits first, and has counters
2164 * that are still around due to the child reference. These
2165 * counters need to be zapped - but otherwise linger.
2167 if (parent_counter
) {
2168 sync_child_counter(child_counter
, parent_counter
);
2169 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
2172 sync_child_counter(sub
, sub
->parent
);
2176 kfree(child_counter
);
2181 * When a child task exits, feed back counter values to parent counters.
2183 * Note: we may be running in child context, but the PID is not hashed
2184 * anymore so new counters will not be added.
2186 void perf_counter_exit_task(struct task_struct
*child
)
2188 struct perf_counter
*child_counter
, *tmp
;
2189 struct perf_counter_context
*child_ctx
;
2191 child_ctx
= &child
->perf_counter_ctx
;
2193 if (likely(!child_ctx
->nr_counters
))
2196 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
2198 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
2202 * Initialize the perf_counter context in task_struct
2204 void perf_counter_init_task(struct task_struct
*child
)
2206 struct perf_counter_context
*child_ctx
, *parent_ctx
;
2207 struct perf_counter
*counter
;
2208 struct task_struct
*parent
= current
;
2210 child_ctx
= &child
->perf_counter_ctx
;
2211 parent_ctx
= &parent
->perf_counter_ctx
;
2213 __perf_counter_init_context(child_ctx
, child
);
2216 * This is executed from the parent task context, so inherit
2217 * counters that have been marked for cloning:
2220 if (likely(!parent_ctx
->nr_counters
))
2224 * Lock the parent list. No need to lock the child - not PID
2225 * hashed yet and not running, so nobody can access it.
2227 mutex_lock(&parent_ctx
->mutex
);
2230 * We dont have to disable NMIs - we are only looking at
2231 * the list, not manipulating it:
2233 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
2234 if (!counter
->hw_event
.inherit
)
2237 if (inherit_group(counter
, parent
,
2238 parent_ctx
, child
, child_ctx
))
2242 mutex_unlock(&parent_ctx
->mutex
);
2245 static void __cpuinit
perf_counter_init_cpu(int cpu
)
2247 struct perf_cpu_context
*cpuctx
;
2249 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2250 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
2252 mutex_lock(&perf_resource_mutex
);
2253 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
2254 mutex_unlock(&perf_resource_mutex
);
2256 hw_perf_counter_setup(cpu
);
2259 #ifdef CONFIG_HOTPLUG_CPU
2260 static void __perf_counter_exit_cpu(void *info
)
2262 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
2263 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
2264 struct perf_counter
*counter
, *tmp
;
2266 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
2267 __perf_counter_remove_from_context(counter
);
2269 static void perf_counter_exit_cpu(int cpu
)
2271 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2272 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
2274 mutex_lock(&ctx
->mutex
);
2275 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
2276 mutex_unlock(&ctx
->mutex
);
2279 static inline void perf_counter_exit_cpu(int cpu
) { }
2282 static int __cpuinit
2283 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
2285 unsigned int cpu
= (long)hcpu
;
2289 case CPU_UP_PREPARE
:
2290 case CPU_UP_PREPARE_FROZEN
:
2291 perf_counter_init_cpu(cpu
);
2294 case CPU_DOWN_PREPARE
:
2295 case CPU_DOWN_PREPARE_FROZEN
:
2296 perf_counter_exit_cpu(cpu
);
2306 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
2307 .notifier_call
= perf_cpu_notify
,
2310 static int __init
perf_counter_init(void)
2312 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
2313 (void *)(long)smp_processor_id());
2314 register_cpu_notifier(&perf_cpu_nb
);
2318 early_initcall(perf_counter_init
);
2320 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
2322 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
2326 perf_set_reserve_percpu(struct sysdev_class
*class,
2330 struct perf_cpu_context
*cpuctx
;
2334 err
= strict_strtoul(buf
, 10, &val
);
2337 if (val
> perf_max_counters
)
2340 mutex_lock(&perf_resource_mutex
);
2341 perf_reserved_percpu
= val
;
2342 for_each_online_cpu(cpu
) {
2343 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2344 spin_lock_irq(&cpuctx
->ctx
.lock
);
2345 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
2346 perf_max_counters
- perf_reserved_percpu
);
2347 cpuctx
->max_pertask
= mpt
;
2348 spin_unlock_irq(&cpuctx
->ctx
.lock
);
2350 mutex_unlock(&perf_resource_mutex
);
2355 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
2357 return sprintf(buf
, "%d\n", perf_overcommit
);
2361 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
2366 err
= strict_strtoul(buf
, 10, &val
);
2372 mutex_lock(&perf_resource_mutex
);
2373 perf_overcommit
= val
;
2374 mutex_unlock(&perf_resource_mutex
);
2379 static SYSDEV_CLASS_ATTR(
2382 perf_show_reserve_percpu
,
2383 perf_set_reserve_percpu
2386 static SYSDEV_CLASS_ATTR(
2389 perf_show_overcommit
,
2393 static struct attribute
*perfclass_attrs
[] = {
2394 &attr_reserve_percpu
.attr
,
2395 &attr_overcommit
.attr
,
2399 static struct attribute_group perfclass_attr_group
= {
2400 .attrs
= perfclass_attrs
,
2401 .name
= "perf_counters",
2404 static int __init
perf_counter_sysfs_init(void)
2406 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
2407 &perfclass_attr_group
);
2409 device_initcall(perf_counter_sysfs_init
);