2 * Performance counter core code
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
8 * For licensing details see kernel-base/COPYING
13 #include <linux/cpu.h>
14 #include <linux/smp.h>
15 #include <linux/file.h>
16 #include <linux/poll.h>
17 #include <linux/sysfs.h>
18 #include <linux/ptrace.h>
19 #include <linux/percpu.h>
20 #include <linux/vmstat.h>
21 #include <linux/hardirq.h>
22 #include <linux/rculist.h>
23 #include <linux/uaccess.h>
24 #include <linux/syscalls.h>
25 #include <linux/anon_inodes.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/perf_counter.h>
29 #include <asm/irq_regs.h>
32 * Each CPU has a list of per CPU counters:
34 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
36 int perf_max_counters __read_mostly
= 1;
37 static int perf_reserved_percpu __read_mostly
;
38 static int perf_overcommit __read_mostly
= 1;
41 * Mutex for (sysadmin-configurable) counter reservations:
43 static DEFINE_MUTEX(perf_resource_mutex
);
46 * Architecture provided APIs - weak aliases:
48 extern __weak
const struct hw_perf_counter_ops
*
49 hw_perf_counter_init(struct perf_counter
*counter
)
54 u64 __weak
hw_perf_save_disable(void) { return 0; }
55 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
56 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
57 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
58 struct perf_cpu_context
*cpuctx
,
59 struct perf_counter_context
*ctx
, int cpu
)
64 void __weak
perf_counter_print_debug(void) { }
67 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
69 struct perf_counter
*group_leader
= counter
->group_leader
;
72 * Depending on whether it is a standalone or sibling counter,
73 * add it straight to the context's counter list, or to the group
74 * leader's sibling list:
76 if (counter
->group_leader
== counter
)
77 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
79 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
80 group_leader
->nr_siblings
++;
83 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
87 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
89 struct perf_counter
*sibling
, *tmp
;
91 list_del_init(&counter
->list_entry
);
92 list_del_rcu(&counter
->event_entry
);
94 if (counter
->group_leader
!= counter
)
95 counter
->group_leader
->nr_siblings
--;
98 * If this was a group counter with sibling counters then
99 * upgrade the siblings to singleton counters by adding them
100 * to the context list directly:
102 list_for_each_entry_safe(sibling
, tmp
,
103 &counter
->sibling_list
, list_entry
) {
105 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
106 sibling
->group_leader
= sibling
;
111 counter_sched_out(struct perf_counter
*counter
,
112 struct perf_cpu_context
*cpuctx
,
113 struct perf_counter_context
*ctx
)
115 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
118 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
119 counter
->hw_ops
->disable(counter
);
122 if (!is_software_counter(counter
))
123 cpuctx
->active_oncpu
--;
125 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
126 cpuctx
->exclusive
= 0;
130 group_sched_out(struct perf_counter
*group_counter
,
131 struct perf_cpu_context
*cpuctx
,
132 struct perf_counter_context
*ctx
)
134 struct perf_counter
*counter
;
136 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
139 counter_sched_out(group_counter
, cpuctx
, ctx
);
142 * Schedule out siblings (if any):
144 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
145 counter_sched_out(counter
, cpuctx
, ctx
);
147 if (group_counter
->hw_event
.exclusive
)
148 cpuctx
->exclusive
= 0;
152 * Cross CPU call to remove a performance counter
154 * We disable the counter on the hardware level first. After that we
155 * remove it from the context list.
157 static void __perf_counter_remove_from_context(void *info
)
159 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
160 struct perf_counter
*counter
= info
;
161 struct perf_counter_context
*ctx
= counter
->ctx
;
166 * If this is a task context, we need to check whether it is
167 * the current task context of this cpu. If not it has been
168 * scheduled out before the smp call arrived.
170 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
173 curr_rq_lock_irq_save(&flags
);
174 spin_lock(&ctx
->lock
);
176 counter_sched_out(counter
, cpuctx
, ctx
);
178 counter
->task
= NULL
;
182 * Protect the list operation against NMI by disabling the
183 * counters on a global level. NOP for non NMI based counters.
185 perf_flags
= hw_perf_save_disable();
186 list_del_counter(counter
, ctx
);
187 hw_perf_restore(perf_flags
);
191 * Allow more per task counters with respect to the
194 cpuctx
->max_pertask
=
195 min(perf_max_counters
- ctx
->nr_counters
,
196 perf_max_counters
- perf_reserved_percpu
);
199 spin_unlock(&ctx
->lock
);
200 curr_rq_unlock_irq_restore(&flags
);
205 * Remove the counter from a task's (or a CPU's) list of counters.
207 * Must be called with counter->mutex and ctx->mutex held.
209 * CPU counters are removed with a smp call. For task counters we only
210 * call when the task is on a CPU.
212 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
214 struct perf_counter_context
*ctx
= counter
->ctx
;
215 struct task_struct
*task
= ctx
->task
;
219 * Per cpu counters are removed via an smp call and
220 * the removal is always sucessful.
222 smp_call_function_single(counter
->cpu
,
223 __perf_counter_remove_from_context
,
229 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
232 spin_lock_irq(&ctx
->lock
);
234 * If the context is active we need to retry the smp call.
236 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
237 spin_unlock_irq(&ctx
->lock
);
242 * The lock prevents that this context is scheduled in so we
243 * can remove the counter safely, if the call above did not
246 if (!list_empty(&counter
->list_entry
)) {
248 list_del_counter(counter
, ctx
);
249 counter
->task
= NULL
;
251 spin_unlock_irq(&ctx
->lock
);
255 * Cross CPU call to disable a performance counter
257 static void __perf_counter_disable(void *info
)
259 struct perf_counter
*counter
= info
;
260 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
261 struct perf_counter_context
*ctx
= counter
->ctx
;
265 * If this is a per-task counter, need to check whether this
266 * counter's task is the current task on this cpu.
268 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
271 curr_rq_lock_irq_save(&flags
);
272 spin_lock(&ctx
->lock
);
275 * If the counter is on, turn it off.
276 * If it is in error state, leave it in error state.
278 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
279 if (counter
== counter
->group_leader
)
280 group_sched_out(counter
, cpuctx
, ctx
);
282 counter_sched_out(counter
, cpuctx
, ctx
);
283 counter
->state
= PERF_COUNTER_STATE_OFF
;
286 spin_unlock(&ctx
->lock
);
287 curr_rq_unlock_irq_restore(&flags
);
293 static void perf_counter_disable(struct perf_counter
*counter
)
295 struct perf_counter_context
*ctx
= counter
->ctx
;
296 struct task_struct
*task
= ctx
->task
;
300 * Disable the counter on the cpu that it's on
302 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
308 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
310 spin_lock_irq(&ctx
->lock
);
312 * If the counter is still active, we need to retry the cross-call.
314 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
315 spin_unlock_irq(&ctx
->lock
);
320 * Since we have the lock this context can't be scheduled
321 * in, so we can change the state safely.
323 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
324 counter
->state
= PERF_COUNTER_STATE_OFF
;
326 spin_unlock_irq(&ctx
->lock
);
330 * Disable a counter and all its children.
332 static void perf_counter_disable_family(struct perf_counter
*counter
)
334 struct perf_counter
*child
;
336 perf_counter_disable(counter
);
339 * Lock the mutex to protect the list of children
341 mutex_lock(&counter
->mutex
);
342 list_for_each_entry(child
, &counter
->child_list
, child_list
)
343 perf_counter_disable(child
);
344 mutex_unlock(&counter
->mutex
);
348 counter_sched_in(struct perf_counter
*counter
,
349 struct perf_cpu_context
*cpuctx
,
350 struct perf_counter_context
*ctx
,
353 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
356 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
357 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
359 * The new state must be visible before we turn it on in the hardware:
363 if (counter
->hw_ops
->enable(counter
)) {
364 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
369 if (!is_software_counter(counter
))
370 cpuctx
->active_oncpu
++;
373 if (counter
->hw_event
.exclusive
)
374 cpuctx
->exclusive
= 1;
380 * Return 1 for a group consisting entirely of software counters,
381 * 0 if the group contains any hardware counters.
383 static int is_software_only_group(struct perf_counter
*leader
)
385 struct perf_counter
*counter
;
387 if (!is_software_counter(leader
))
390 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
391 if (!is_software_counter(counter
))
398 * Work out whether we can put this counter group on the CPU now.
400 static int group_can_go_on(struct perf_counter
*counter
,
401 struct perf_cpu_context
*cpuctx
,
405 * Groups consisting entirely of software counters can always go on.
407 if (is_software_only_group(counter
))
410 * If an exclusive group is already on, no other hardware
411 * counters can go on.
413 if (cpuctx
->exclusive
)
416 * If this group is exclusive and there are already
417 * counters on the CPU, it can't go on.
419 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
422 * Otherwise, try to add it if all previous groups were able
429 * Cross CPU call to install and enable a performance counter
431 static void __perf_install_in_context(void *info
)
433 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
434 struct perf_counter
*counter
= info
;
435 struct perf_counter_context
*ctx
= counter
->ctx
;
436 struct perf_counter
*leader
= counter
->group_leader
;
437 int cpu
= smp_processor_id();
443 * If this is a task context, we need to check whether it is
444 * the current task context of this cpu. If not it has been
445 * scheduled out before the smp call arrived.
447 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
450 curr_rq_lock_irq_save(&flags
);
451 spin_lock(&ctx
->lock
);
454 * Protect the list operation against NMI by disabling the
455 * counters on a global level. NOP for non NMI based counters.
457 perf_flags
= hw_perf_save_disable();
459 list_add_counter(counter
, ctx
);
461 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
464 * Don't put the counter on if it is disabled or if
465 * it is in a group and the group isn't on.
467 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
468 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
472 * An exclusive counter can't go on if there are already active
473 * hardware counters, and no hardware counter can go on if there
474 * is already an exclusive counter on.
476 if (!group_can_go_on(counter
, cpuctx
, 1))
479 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
483 * This counter couldn't go on. If it is in a group
484 * then we have to pull the whole group off.
485 * If the counter group is pinned then put it in error state.
487 if (leader
!= counter
)
488 group_sched_out(leader
, cpuctx
, ctx
);
489 if (leader
->hw_event
.pinned
)
490 leader
->state
= PERF_COUNTER_STATE_ERROR
;
493 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
494 cpuctx
->max_pertask
--;
497 hw_perf_restore(perf_flags
);
499 spin_unlock(&ctx
->lock
);
500 curr_rq_unlock_irq_restore(&flags
);
504 * Attach a performance counter to a context
506 * First we add the counter to the list with the hardware enable bit
507 * in counter->hw_config cleared.
509 * If the counter is attached to a task which is on a CPU we use a smp
510 * call to enable it in the task context. The task might have been
511 * scheduled away, but we check this in the smp call again.
513 * Must be called with ctx->mutex held.
516 perf_install_in_context(struct perf_counter_context
*ctx
,
517 struct perf_counter
*counter
,
520 struct task_struct
*task
= ctx
->task
;
524 * Per cpu counters are installed via an smp call and
525 * the install is always sucessful.
527 smp_call_function_single(cpu
, __perf_install_in_context
,
532 counter
->task
= task
;
534 task_oncpu_function_call(task
, __perf_install_in_context
,
537 spin_lock_irq(&ctx
->lock
);
539 * we need to retry the smp call.
541 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
542 spin_unlock_irq(&ctx
->lock
);
547 * The lock prevents that this context is scheduled in so we
548 * can add the counter safely, if it the call above did not
551 if (list_empty(&counter
->list_entry
)) {
552 list_add_counter(counter
, ctx
);
555 spin_unlock_irq(&ctx
->lock
);
559 * Cross CPU call to enable a performance counter
561 static void __perf_counter_enable(void *info
)
563 struct perf_counter
*counter
= info
;
564 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
565 struct perf_counter_context
*ctx
= counter
->ctx
;
566 struct perf_counter
*leader
= counter
->group_leader
;
571 * If this is a per-task counter, need to check whether this
572 * counter's task is the current task on this cpu.
574 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
577 curr_rq_lock_irq_save(&flags
);
578 spin_lock(&ctx
->lock
);
580 counter
->prev_state
= counter
->state
;
581 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
583 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
586 * If the counter is in a group and isn't the group leader,
587 * then don't put it on unless the group is on.
589 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
592 if (!group_can_go_on(counter
, cpuctx
, 1))
595 err
= counter_sched_in(counter
, cpuctx
, ctx
,
600 * If this counter can't go on and it's part of a
601 * group, then the whole group has to come off.
603 if (leader
!= counter
)
604 group_sched_out(leader
, cpuctx
, ctx
);
605 if (leader
->hw_event
.pinned
)
606 leader
->state
= PERF_COUNTER_STATE_ERROR
;
610 spin_unlock(&ctx
->lock
);
611 curr_rq_unlock_irq_restore(&flags
);
617 static void perf_counter_enable(struct perf_counter
*counter
)
619 struct perf_counter_context
*ctx
= counter
->ctx
;
620 struct task_struct
*task
= ctx
->task
;
624 * Enable the counter on the cpu that it's on
626 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
631 spin_lock_irq(&ctx
->lock
);
632 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
636 * If the counter is in error state, clear that first.
637 * That way, if we see the counter in error state below, we
638 * know that it has gone back into error state, as distinct
639 * from the task having been scheduled away before the
640 * cross-call arrived.
642 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
643 counter
->state
= PERF_COUNTER_STATE_OFF
;
646 spin_unlock_irq(&ctx
->lock
);
647 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
649 spin_lock_irq(&ctx
->lock
);
652 * If the context is active and the counter is still off,
653 * we need to retry the cross-call.
655 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
659 * Since we have the lock this context can't be scheduled
660 * in, so we can change the state safely.
662 if (counter
->state
== PERF_COUNTER_STATE_OFF
)
663 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
665 spin_unlock_irq(&ctx
->lock
);
669 * Enable a counter and all its children.
671 static void perf_counter_enable_family(struct perf_counter
*counter
)
673 struct perf_counter
*child
;
675 perf_counter_enable(counter
);
678 * Lock the mutex to protect the list of children
680 mutex_lock(&counter
->mutex
);
681 list_for_each_entry(child
, &counter
->child_list
, child_list
)
682 perf_counter_enable(child
);
683 mutex_unlock(&counter
->mutex
);
686 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
687 struct perf_cpu_context
*cpuctx
)
689 struct perf_counter
*counter
;
692 spin_lock(&ctx
->lock
);
694 if (likely(!ctx
->nr_counters
))
697 flags
= hw_perf_save_disable();
698 if (ctx
->nr_active
) {
699 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
700 group_sched_out(counter
, cpuctx
, ctx
);
702 hw_perf_restore(flags
);
704 spin_unlock(&ctx
->lock
);
708 * Called from scheduler to remove the counters of the current task,
709 * with interrupts disabled.
711 * We stop each counter and update the counter value in counter->count.
713 * This does not protect us against NMI, but disable()
714 * sets the disabled bit in the control field of counter _before_
715 * accessing the counter control register. If a NMI hits, then it will
716 * not restart the counter.
718 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
720 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
721 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
722 struct pt_regs
*regs
;
724 if (likely(!cpuctx
->task_ctx
))
727 regs
= task_pt_regs(task
);
728 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
);
729 __perf_counter_sched_out(ctx
, cpuctx
);
731 cpuctx
->task_ctx
= NULL
;
734 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
736 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
740 group_sched_in(struct perf_counter
*group_counter
,
741 struct perf_cpu_context
*cpuctx
,
742 struct perf_counter_context
*ctx
,
745 struct perf_counter
*counter
, *partial_group
;
748 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
751 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
753 return ret
< 0 ? ret
: 0;
755 group_counter
->prev_state
= group_counter
->state
;
756 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
760 * Schedule in siblings as one group (if any):
762 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
763 counter
->prev_state
= counter
->state
;
764 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
765 partial_group
= counter
;
774 * Groups can be scheduled in as one unit only, so undo any
775 * partial group before returning:
777 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
778 if (counter
== partial_group
)
780 counter_sched_out(counter
, cpuctx
, ctx
);
782 counter_sched_out(group_counter
, cpuctx
, ctx
);
788 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
789 struct perf_cpu_context
*cpuctx
, int cpu
)
791 struct perf_counter
*counter
;
795 spin_lock(&ctx
->lock
);
797 if (likely(!ctx
->nr_counters
))
800 flags
= hw_perf_save_disable();
803 * First go through the list and put on any pinned groups
804 * in order to give them the best chance of going on.
806 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
807 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
808 !counter
->hw_event
.pinned
)
810 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
813 if (group_can_go_on(counter
, cpuctx
, 1))
814 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
817 * If this pinned group hasn't been scheduled,
818 * put it in error state.
820 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
821 counter
->state
= PERF_COUNTER_STATE_ERROR
;
824 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
826 * Ignore counters in OFF or ERROR state, and
827 * ignore pinned counters since we did them already.
829 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
830 counter
->hw_event
.pinned
)
834 * Listen to the 'cpu' scheduling filter constraint
837 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
840 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
841 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
845 hw_perf_restore(flags
);
847 spin_unlock(&ctx
->lock
);
851 * Called from scheduler to add the counters of the current task
852 * with interrupts disabled.
854 * We restore the counter value and then enable it.
856 * This does not protect us against NMI, but enable()
857 * sets the enabled bit in the control field of counter _before_
858 * accessing the counter control register. If a NMI hits, then it will
859 * keep the counter running.
861 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
863 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
864 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
866 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
867 cpuctx
->task_ctx
= ctx
;
870 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
872 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
874 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
877 int perf_counter_task_disable(void)
879 struct task_struct
*curr
= current
;
880 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
881 struct perf_counter
*counter
;
886 if (likely(!ctx
->nr_counters
))
889 curr_rq_lock_irq_save(&flags
);
890 cpu
= smp_processor_id();
892 /* force the update of the task clock: */
893 __task_delta_exec(curr
, 1);
895 perf_counter_task_sched_out(curr
, cpu
);
897 spin_lock(&ctx
->lock
);
900 * Disable all the counters:
902 perf_flags
= hw_perf_save_disable();
904 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
905 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
)
906 counter
->state
= PERF_COUNTER_STATE_OFF
;
909 hw_perf_restore(perf_flags
);
911 spin_unlock(&ctx
->lock
);
913 curr_rq_unlock_irq_restore(&flags
);
918 int perf_counter_task_enable(void)
920 struct task_struct
*curr
= current
;
921 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
922 struct perf_counter
*counter
;
927 if (likely(!ctx
->nr_counters
))
930 curr_rq_lock_irq_save(&flags
);
931 cpu
= smp_processor_id();
933 /* force the update of the task clock: */
934 __task_delta_exec(curr
, 1);
936 perf_counter_task_sched_out(curr
, cpu
);
938 spin_lock(&ctx
->lock
);
941 * Disable all the counters:
943 perf_flags
= hw_perf_save_disable();
945 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
946 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
948 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
949 counter
->hw_event
.disabled
= 0;
951 hw_perf_restore(perf_flags
);
953 spin_unlock(&ctx
->lock
);
955 perf_counter_task_sched_in(curr
, cpu
);
957 curr_rq_unlock_irq_restore(&flags
);
963 * Round-robin a context's counters:
965 static void rotate_ctx(struct perf_counter_context
*ctx
)
967 struct perf_counter
*counter
;
970 if (!ctx
->nr_counters
)
973 spin_lock(&ctx
->lock
);
975 * Rotate the first entry last (works just fine for group counters too):
977 perf_flags
= hw_perf_save_disable();
978 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
979 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
982 hw_perf_restore(perf_flags
);
984 spin_unlock(&ctx
->lock
);
987 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
989 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
990 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
991 const int rotate_percpu
= 0;
994 perf_counter_cpu_sched_out(cpuctx
);
995 perf_counter_task_sched_out(curr
, cpu
);
998 rotate_ctx(&cpuctx
->ctx
);
1002 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1003 perf_counter_task_sched_in(curr
, cpu
);
1007 * Cross CPU call to read the hardware counter
1009 static void __read(void *info
)
1011 struct perf_counter
*counter
= info
;
1012 unsigned long flags
;
1014 curr_rq_lock_irq_save(&flags
);
1015 counter
->hw_ops
->read(counter
);
1016 curr_rq_unlock_irq_restore(&flags
);
1019 static u64
perf_counter_read(struct perf_counter
*counter
)
1022 * If counter is enabled and currently active on a CPU, update the
1023 * value in the counter structure:
1025 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1026 smp_call_function_single(counter
->oncpu
,
1027 __read
, counter
, 1);
1030 return atomic64_read(&counter
->count
);
1033 static void put_context(struct perf_counter_context
*ctx
)
1036 put_task_struct(ctx
->task
);
1039 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1041 struct perf_cpu_context
*cpuctx
;
1042 struct perf_counter_context
*ctx
;
1043 struct task_struct
*task
;
1046 * If cpu is not a wildcard then this is a percpu counter:
1049 /* Must be root to operate on a CPU counter: */
1050 if (!capable(CAP_SYS_ADMIN
))
1051 return ERR_PTR(-EACCES
);
1053 if (cpu
< 0 || cpu
> num_possible_cpus())
1054 return ERR_PTR(-EINVAL
);
1057 * We could be clever and allow to attach a counter to an
1058 * offline CPU and activate it when the CPU comes up, but
1061 if (!cpu_isset(cpu
, cpu_online_map
))
1062 return ERR_PTR(-ENODEV
);
1064 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1074 task
= find_task_by_vpid(pid
);
1076 get_task_struct(task
);
1080 return ERR_PTR(-ESRCH
);
1082 ctx
= &task
->perf_counter_ctx
;
1085 /* Reuse ptrace permission checks for now. */
1086 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1088 return ERR_PTR(-EACCES
);
1094 static void free_counter_rcu(struct rcu_head
*head
)
1096 struct perf_counter
*counter
;
1098 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1102 static void free_counter(struct perf_counter
*counter
)
1104 if (counter
->destroy
)
1105 counter
->destroy(counter
);
1107 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1111 * Called when the last reference to the file is gone.
1113 static int perf_release(struct inode
*inode
, struct file
*file
)
1115 struct perf_counter
*counter
= file
->private_data
;
1116 struct perf_counter_context
*ctx
= counter
->ctx
;
1118 file
->private_data
= NULL
;
1120 mutex_lock(&ctx
->mutex
);
1121 mutex_lock(&counter
->mutex
);
1123 perf_counter_remove_from_context(counter
);
1125 mutex_unlock(&counter
->mutex
);
1126 mutex_unlock(&ctx
->mutex
);
1128 free_counter(counter
);
1135 * Read the performance counter - simple non blocking version for now
1138 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1142 if (count
< sizeof(cntval
))
1146 * Return end-of-file for a read on a counter that is in
1147 * error state (i.e. because it was pinned but it couldn't be
1148 * scheduled on to the CPU at some point).
1150 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1153 mutex_lock(&counter
->mutex
);
1154 cntval
= perf_counter_read(counter
);
1155 mutex_unlock(&counter
->mutex
);
1157 return put_user(cntval
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(cntval
);
1161 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1163 struct perf_counter
*counter
= file
->private_data
;
1165 return perf_read_hw(counter
, buf
, count
);
1168 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1170 struct perf_counter
*counter
= file
->private_data
;
1171 struct perf_mmap_data
*data
;
1172 unsigned int events
;
1175 data
= rcu_dereference(counter
->data
);
1177 events
= atomic_xchg(&data
->wakeup
, 0);
1182 poll_wait(file
, &counter
->waitq
, wait
);
1187 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1189 struct perf_counter
*counter
= file
->private_data
;
1193 case PERF_COUNTER_IOC_ENABLE
:
1194 perf_counter_enable_family(counter
);
1196 case PERF_COUNTER_IOC_DISABLE
:
1197 perf_counter_disable_family(counter
);
1205 static void __perf_counter_update_userpage(struct perf_counter
*counter
,
1206 struct perf_mmap_data
*data
)
1208 struct perf_counter_mmap_page
*userpg
= data
->user_page
;
1211 * Disable preemption so as to not let the corresponding user-space
1212 * spin too long if we get preempted.
1217 userpg
->index
= counter
->hw
.idx
;
1218 userpg
->offset
= atomic64_read(&counter
->count
);
1219 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1220 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1222 userpg
->data_head
= atomic_read(&data
->head
);
1228 void perf_counter_update_userpage(struct perf_counter
*counter
)
1230 struct perf_mmap_data
*data
;
1233 data
= rcu_dereference(counter
->data
);
1235 __perf_counter_update_userpage(counter
, data
);
1239 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1241 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1242 struct perf_mmap_data
*data
;
1243 int ret
= VM_FAULT_SIGBUS
;
1246 data
= rcu_dereference(counter
->data
);
1250 if (vmf
->pgoff
== 0) {
1251 vmf
->page
= virt_to_page(data
->user_page
);
1253 int nr
= vmf
->pgoff
- 1;
1255 if ((unsigned)nr
> data
->nr_pages
)
1258 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1260 get_page(vmf
->page
);
1268 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1270 struct perf_mmap_data
*data
;
1274 WARN_ON(atomic_read(&counter
->mmap_count
));
1276 size
= sizeof(struct perf_mmap_data
);
1277 size
+= nr_pages
* sizeof(void *);
1279 data
= kzalloc(size
, GFP_KERNEL
);
1283 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1284 if (!data
->user_page
)
1285 goto fail_user_page
;
1287 for (i
= 0; i
< nr_pages
; i
++) {
1288 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1289 if (!data
->data_pages
[i
])
1290 goto fail_data_pages
;
1293 data
->nr_pages
= nr_pages
;
1295 rcu_assign_pointer(counter
->data
, data
);
1300 for (i
--; i
>= 0; i
--)
1301 free_page((unsigned long)data
->data_pages
[i
]);
1303 free_page((unsigned long)data
->user_page
);
1312 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1314 struct perf_mmap_data
*data
= container_of(rcu_head
,
1315 struct perf_mmap_data
, rcu_head
);
1318 free_page((unsigned long)data
->user_page
);
1319 for (i
= 0; i
< data
->nr_pages
; i
++)
1320 free_page((unsigned long)data
->data_pages
[i
]);
1324 static void perf_mmap_data_free(struct perf_counter
*counter
)
1326 struct perf_mmap_data
*data
= counter
->data
;
1328 WARN_ON(atomic_read(&counter
->mmap_count
));
1330 rcu_assign_pointer(counter
->data
, NULL
);
1331 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1334 static void perf_mmap_open(struct vm_area_struct
*vma
)
1336 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1338 atomic_inc(&counter
->mmap_count
);
1341 static void perf_mmap_close(struct vm_area_struct
*vma
)
1343 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1345 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1346 &counter
->mmap_mutex
)) {
1347 perf_mmap_data_free(counter
);
1348 mutex_unlock(&counter
->mmap_mutex
);
1352 static struct vm_operations_struct perf_mmap_vmops
= {
1353 .open
= perf_mmap_open
,
1354 .close
= perf_mmap_close
,
1355 .fault
= perf_mmap_fault
,
1358 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1360 struct perf_counter
*counter
= file
->private_data
;
1361 unsigned long vma_size
;
1362 unsigned long nr_pages
;
1363 unsigned long locked
, lock_limit
;
1366 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1369 vma_size
= vma
->vm_end
- vma
->vm_start
;
1370 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1372 if (nr_pages
== 0 || !is_power_of_2(nr_pages
))
1375 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1378 if (vma
->vm_pgoff
!= 0)
1381 locked
= vma_size
>> PAGE_SHIFT
;
1382 locked
+= vma
->vm_mm
->locked_vm
;
1384 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1385 lock_limit
>>= PAGE_SHIFT
;
1387 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
))
1390 mutex_lock(&counter
->mmap_mutex
);
1391 if (atomic_inc_not_zero(&counter
->mmap_count
))
1394 WARN_ON(counter
->data
);
1395 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1397 atomic_set(&counter
->mmap_count
, 1);
1399 mutex_unlock(&counter
->mmap_mutex
);
1401 vma
->vm_flags
&= ~VM_MAYWRITE
;
1402 vma
->vm_flags
|= VM_RESERVED
;
1403 vma
->vm_ops
= &perf_mmap_vmops
;
1408 static const struct file_operations perf_fops
= {
1409 .release
= perf_release
,
1412 .unlocked_ioctl
= perf_ioctl
,
1413 .compat_ioctl
= perf_ioctl
,
1421 struct perf_output_handle
{
1422 struct perf_counter
*counter
;
1423 struct perf_mmap_data
*data
;
1424 unsigned int offset
;
1429 static int perf_output_begin(struct perf_output_handle
*handle
,
1430 struct perf_counter
*counter
, unsigned int size
)
1432 struct perf_mmap_data
*data
;
1433 unsigned int offset
, head
;
1436 data
= rcu_dereference(counter
->data
);
1440 if (!data
->nr_pages
)
1444 offset
= head
= atomic_read(&data
->head
);
1446 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1448 handle
->counter
= counter
;
1449 handle
->data
= data
;
1450 handle
->offset
= offset
;
1451 handle
->head
= head
;
1452 handle
->wakeup
= (offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
);
1462 static void perf_output_copy(struct perf_output_handle
*handle
,
1463 void *buf
, unsigned int len
)
1465 unsigned int pages_mask
;
1466 unsigned int offset
;
1470 offset
= handle
->offset
;
1471 pages_mask
= handle
->data
->nr_pages
- 1;
1472 pages
= handle
->data
->data_pages
;
1475 unsigned int page_offset
;
1478 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1479 page_offset
= offset
& (PAGE_SIZE
- 1);
1480 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1482 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1489 handle
->offset
= offset
;
1491 WARN_ON_ONCE(handle
->offset
> handle
->head
);
1494 #define perf_output_put(handle, x) \
1495 perf_output_copy((handle), &(x), sizeof(x))
1497 static void perf_output_end(struct perf_output_handle
*handle
, int nmi
)
1499 if (handle
->wakeup
) {
1500 (void)atomic_xchg(&handle
->data
->wakeup
, POLL_IN
);
1501 __perf_counter_update_userpage(handle
->counter
, handle
->data
);
1503 handle
->counter
->wakeup_pending
= 1;
1504 set_perf_counter_pending();
1506 wake_up(&handle
->counter
->waitq
);
1511 static int perf_output_write(struct perf_counter
*counter
, int nmi
,
1512 void *buf
, ssize_t size
)
1514 struct perf_output_handle handle
;
1517 ret
= perf_output_begin(&handle
, counter
, size
);
1521 perf_output_copy(&handle
, buf
, size
);
1522 perf_output_end(&handle
, nmi
);
1528 static void perf_output_simple(struct perf_counter
*counter
,
1529 int nmi
, struct pt_regs
*regs
)
1533 struct perf_event_header header
;
1538 event
.header
.type
= PERF_EVENT_IP
;
1539 event
.ip
= instruction_pointer(regs
);
1541 size
= sizeof(event
);
1543 if (counter
->hw_event
.include_tid
) {
1544 /* namespace issues */
1545 event
.pid
= current
->group_leader
->pid
;
1546 event
.tid
= current
->pid
;
1548 event
.header
.type
|= __PERF_EVENT_TID
;
1550 size
-= sizeof(u64
);
1552 event
.header
.size
= size
;
1554 perf_output_write(counter
, nmi
, &event
, size
);
1557 static void perf_output_group(struct perf_counter
*counter
, int nmi
)
1559 struct perf_output_handle handle
;
1560 struct perf_event_header header
;
1561 struct perf_counter
*leader
, *sub
;
1569 size
= sizeof(header
) + counter
->nr_siblings
* sizeof(entry
);
1571 ret
= perf_output_begin(&handle
, counter
, size
);
1575 header
.type
= PERF_EVENT_GROUP
;
1578 perf_output_put(&handle
, header
);
1580 leader
= counter
->group_leader
;
1581 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1583 sub
->hw_ops
->read(sub
);
1585 entry
.event
= sub
->hw_event
.config
;
1586 entry
.counter
= atomic64_read(&sub
->count
);
1588 perf_output_put(&handle
, entry
);
1591 perf_output_end(&handle
, nmi
);
1594 void perf_counter_output(struct perf_counter
*counter
,
1595 int nmi
, struct pt_regs
*regs
)
1597 switch (counter
->hw_event
.record_type
) {
1598 case PERF_RECORD_SIMPLE
:
1601 case PERF_RECORD_IRQ
:
1602 perf_output_simple(counter
, nmi
, regs
);
1605 case PERF_RECORD_GROUP
:
1606 perf_output_group(counter
, nmi
);
1612 * Generic software counter infrastructure
1615 static void perf_swcounter_update(struct perf_counter
*counter
)
1617 struct hw_perf_counter
*hwc
= &counter
->hw
;
1622 prev
= atomic64_read(&hwc
->prev_count
);
1623 now
= atomic64_read(&hwc
->count
);
1624 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
1629 atomic64_add(delta
, &counter
->count
);
1630 atomic64_sub(delta
, &hwc
->period_left
);
1633 static void perf_swcounter_set_period(struct perf_counter
*counter
)
1635 struct hw_perf_counter
*hwc
= &counter
->hw
;
1636 s64 left
= atomic64_read(&hwc
->period_left
);
1637 s64 period
= hwc
->irq_period
;
1639 if (unlikely(left
<= -period
)) {
1641 atomic64_set(&hwc
->period_left
, left
);
1644 if (unlikely(left
<= 0)) {
1646 atomic64_add(period
, &hwc
->period_left
);
1649 atomic64_set(&hwc
->prev_count
, -left
);
1650 atomic64_set(&hwc
->count
, -left
);
1653 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
1655 struct perf_counter
*counter
;
1656 struct pt_regs
*regs
;
1658 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
1659 counter
->hw_ops
->read(counter
);
1661 regs
= get_irq_regs();
1663 * In case we exclude kernel IPs or are somehow not in interrupt
1664 * context, provide the next best thing, the user IP.
1666 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
1667 !counter
->hw_event
.exclude_user
)
1668 regs
= task_pt_regs(current
);
1671 perf_counter_output(counter
, 0, regs
);
1673 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
1675 return HRTIMER_RESTART
;
1678 static void perf_swcounter_overflow(struct perf_counter
*counter
,
1679 int nmi
, struct pt_regs
*regs
)
1681 perf_swcounter_update(counter
);
1682 perf_swcounter_set_period(counter
);
1683 perf_counter_output(counter
, nmi
, regs
);
1686 static int perf_swcounter_match(struct perf_counter
*counter
,
1687 enum perf_event_types type
,
1688 u32 event
, struct pt_regs
*regs
)
1690 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1693 if (perf_event_raw(&counter
->hw_event
))
1696 if (perf_event_type(&counter
->hw_event
) != type
)
1699 if (perf_event_id(&counter
->hw_event
) != event
)
1702 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
1705 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
1711 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
1712 int nmi
, struct pt_regs
*regs
)
1714 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
1715 if (counter
->hw
.irq_period
&& !neg
)
1716 perf_swcounter_overflow(counter
, nmi
, regs
);
1719 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
1720 enum perf_event_types type
, u32 event
,
1721 u64 nr
, int nmi
, struct pt_regs
*regs
)
1723 struct perf_counter
*counter
;
1725 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
1729 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
1730 if (perf_swcounter_match(counter
, type
, event
, regs
))
1731 perf_swcounter_add(counter
, nr
, nmi
, regs
);
1736 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
1739 return &cpuctx
->recursion
[3];
1742 return &cpuctx
->recursion
[2];
1745 return &cpuctx
->recursion
[1];
1747 return &cpuctx
->recursion
[0];
1750 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
1751 u64 nr
, int nmi
, struct pt_regs
*regs
)
1753 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
1754 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
1762 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
, nr
, nmi
, regs
);
1763 if (cpuctx
->task_ctx
) {
1764 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
1772 put_cpu_var(perf_cpu_context
);
1775 void perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
)
1777 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
);
1780 static void perf_swcounter_read(struct perf_counter
*counter
)
1782 perf_swcounter_update(counter
);
1785 static int perf_swcounter_enable(struct perf_counter
*counter
)
1787 perf_swcounter_set_period(counter
);
1791 static void perf_swcounter_disable(struct perf_counter
*counter
)
1793 perf_swcounter_update(counter
);
1796 static const struct hw_perf_counter_ops perf_ops_generic
= {
1797 .enable
= perf_swcounter_enable
,
1798 .disable
= perf_swcounter_disable
,
1799 .read
= perf_swcounter_read
,
1803 * Software counter: cpu wall time clock
1806 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
1808 int cpu
= raw_smp_processor_id();
1812 now
= cpu_clock(cpu
);
1813 prev
= atomic64_read(&counter
->hw
.prev_count
);
1814 atomic64_set(&counter
->hw
.prev_count
, now
);
1815 atomic64_add(now
- prev
, &counter
->count
);
1818 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
1820 struct hw_perf_counter
*hwc
= &counter
->hw
;
1821 int cpu
= raw_smp_processor_id();
1823 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
1824 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1825 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
1826 if (hwc
->irq_period
) {
1827 __hrtimer_start_range_ns(&hwc
->hrtimer
,
1828 ns_to_ktime(hwc
->irq_period
), 0,
1829 HRTIMER_MODE_REL
, 0);
1835 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
1837 hrtimer_cancel(&counter
->hw
.hrtimer
);
1838 cpu_clock_perf_counter_update(counter
);
1841 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
1843 cpu_clock_perf_counter_update(counter
);
1846 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
1847 .enable
= cpu_clock_perf_counter_enable
,
1848 .disable
= cpu_clock_perf_counter_disable
,
1849 .read
= cpu_clock_perf_counter_read
,
1853 * Software counter: task time clock
1857 * Called from within the scheduler:
1859 static u64
task_clock_perf_counter_val(struct perf_counter
*counter
, int update
)
1861 struct task_struct
*curr
= counter
->task
;
1864 delta
= __task_delta_exec(curr
, update
);
1866 return curr
->se
.sum_exec_runtime
+ delta
;
1869 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
1874 prev
= atomic64_read(&counter
->hw
.prev_count
);
1876 atomic64_set(&counter
->hw
.prev_count
, now
);
1880 atomic64_add(delta
, &counter
->count
);
1883 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
1885 struct hw_perf_counter
*hwc
= &counter
->hw
;
1887 atomic64_set(&hwc
->prev_count
, task_clock_perf_counter_val(counter
, 0));
1888 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1889 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
1890 if (hwc
->irq_period
) {
1891 __hrtimer_start_range_ns(&hwc
->hrtimer
,
1892 ns_to_ktime(hwc
->irq_period
), 0,
1893 HRTIMER_MODE_REL
, 0);
1899 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
1901 hrtimer_cancel(&counter
->hw
.hrtimer
);
1902 task_clock_perf_counter_update(counter
,
1903 task_clock_perf_counter_val(counter
, 0));
1906 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
1908 task_clock_perf_counter_update(counter
,
1909 task_clock_perf_counter_val(counter
, 1));
1912 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
1913 .enable
= task_clock_perf_counter_enable
,
1914 .disable
= task_clock_perf_counter_disable
,
1915 .read
= task_clock_perf_counter_read
,
1919 * Software counter: cpu migrations
1922 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
1924 struct task_struct
*curr
= counter
->ctx
->task
;
1927 return curr
->se
.nr_migrations
;
1928 return cpu_nr_migrations(smp_processor_id());
1931 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
1936 prev
= atomic64_read(&counter
->hw
.prev_count
);
1937 now
= get_cpu_migrations(counter
);
1939 atomic64_set(&counter
->hw
.prev_count
, now
);
1943 atomic64_add(delta
, &counter
->count
);
1946 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
1948 cpu_migrations_perf_counter_update(counter
);
1951 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
1953 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
1954 atomic64_set(&counter
->hw
.prev_count
,
1955 get_cpu_migrations(counter
));
1959 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
1961 cpu_migrations_perf_counter_update(counter
);
1964 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
1965 .enable
= cpu_migrations_perf_counter_enable
,
1966 .disable
= cpu_migrations_perf_counter_disable
,
1967 .read
= cpu_migrations_perf_counter_read
,
1970 #ifdef CONFIG_EVENT_PROFILE
1971 void perf_tpcounter_event(int event_id
)
1973 struct pt_regs
*regs
= get_irq_regs();
1976 regs
= task_pt_regs(current
);
1978 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
);
1981 extern int ftrace_profile_enable(int);
1982 extern void ftrace_profile_disable(int);
1984 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
1986 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
1989 static const struct hw_perf_counter_ops
*
1990 tp_perf_counter_init(struct perf_counter
*counter
)
1992 int event_id
= perf_event_id(&counter
->hw_event
);
1995 ret
= ftrace_profile_enable(event_id
);
1999 counter
->destroy
= tp_perf_counter_destroy
;
2000 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2002 return &perf_ops_generic
;
2005 static const struct hw_perf_counter_ops
*
2006 tp_perf_counter_init(struct perf_counter
*counter
)
2012 static const struct hw_perf_counter_ops
*
2013 sw_perf_counter_init(struct perf_counter
*counter
)
2015 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2016 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
2017 struct hw_perf_counter
*hwc
= &counter
->hw
;
2020 * Software counters (currently) can't in general distinguish
2021 * between user, kernel and hypervisor events.
2022 * However, context switches and cpu migrations are considered
2023 * to be kernel events, and page faults are never hypervisor
2026 switch (perf_event_id(&counter
->hw_event
)) {
2027 case PERF_COUNT_CPU_CLOCK
:
2028 hw_ops
= &perf_ops_cpu_clock
;
2030 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2031 hw_event
->irq_period
= 10000;
2033 case PERF_COUNT_TASK_CLOCK
:
2035 * If the user instantiates this as a per-cpu counter,
2036 * use the cpu_clock counter instead.
2038 if (counter
->ctx
->task
)
2039 hw_ops
= &perf_ops_task_clock
;
2041 hw_ops
= &perf_ops_cpu_clock
;
2043 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2044 hw_event
->irq_period
= 10000;
2046 case PERF_COUNT_PAGE_FAULTS
:
2047 case PERF_COUNT_PAGE_FAULTS_MIN
:
2048 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2049 case PERF_COUNT_CONTEXT_SWITCHES
:
2050 hw_ops
= &perf_ops_generic
;
2052 case PERF_COUNT_CPU_MIGRATIONS
:
2053 if (!counter
->hw_event
.exclude_kernel
)
2054 hw_ops
= &perf_ops_cpu_migrations
;
2059 hwc
->irq_period
= hw_event
->irq_period
;
2065 * Allocate and initialize a counter structure
2067 static struct perf_counter
*
2068 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2070 struct perf_counter_context
*ctx
,
2071 struct perf_counter
*group_leader
,
2074 const struct hw_perf_counter_ops
*hw_ops
;
2075 struct perf_counter
*counter
;
2077 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2082 * Single counters are their own group leaders, with an
2083 * empty sibling list:
2086 group_leader
= counter
;
2088 mutex_init(&counter
->mutex
);
2089 INIT_LIST_HEAD(&counter
->list_entry
);
2090 INIT_LIST_HEAD(&counter
->event_entry
);
2091 INIT_LIST_HEAD(&counter
->sibling_list
);
2092 init_waitqueue_head(&counter
->waitq
);
2094 mutex_init(&counter
->mmap_mutex
);
2096 INIT_LIST_HEAD(&counter
->child_list
);
2099 counter
->hw_event
= *hw_event
;
2100 counter
->wakeup_pending
= 0;
2101 counter
->group_leader
= group_leader
;
2102 counter
->hw_ops
= NULL
;
2105 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2106 if (hw_event
->disabled
)
2107 counter
->state
= PERF_COUNTER_STATE_OFF
;
2111 if (perf_event_raw(hw_event
)) {
2112 hw_ops
= hw_perf_counter_init(counter
);
2116 switch (perf_event_type(hw_event
)) {
2117 case PERF_TYPE_HARDWARE
:
2118 hw_ops
= hw_perf_counter_init(counter
);
2121 case PERF_TYPE_SOFTWARE
:
2122 hw_ops
= sw_perf_counter_init(counter
);
2125 case PERF_TYPE_TRACEPOINT
:
2126 hw_ops
= tp_perf_counter_init(counter
);
2135 counter
->hw_ops
= hw_ops
;
2141 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2143 * @hw_event_uptr: event type attributes for monitoring/sampling
2146 * @group_fd: group leader counter fd
2148 SYSCALL_DEFINE5(perf_counter_open
,
2149 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2150 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2152 struct perf_counter
*counter
, *group_leader
;
2153 struct perf_counter_hw_event hw_event
;
2154 struct perf_counter_context
*ctx
;
2155 struct file
*counter_file
= NULL
;
2156 struct file
*group_file
= NULL
;
2157 int fput_needed
= 0;
2158 int fput_needed2
= 0;
2161 /* for future expandability... */
2165 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2169 * Get the target context (task or percpu):
2171 ctx
= find_get_context(pid
, cpu
);
2173 return PTR_ERR(ctx
);
2176 * Look up the group leader (we will attach this counter to it):
2178 group_leader
= NULL
;
2179 if (group_fd
!= -1) {
2181 group_file
= fget_light(group_fd
, &fput_needed
);
2183 goto err_put_context
;
2184 if (group_file
->f_op
!= &perf_fops
)
2185 goto err_put_context
;
2187 group_leader
= group_file
->private_data
;
2189 * Do not allow a recursive hierarchy (this new sibling
2190 * becoming part of another group-sibling):
2192 if (group_leader
->group_leader
!= group_leader
)
2193 goto err_put_context
;
2195 * Do not allow to attach to a group in a different
2196 * task or CPU context:
2198 if (group_leader
->ctx
!= ctx
)
2199 goto err_put_context
;
2201 * Only a group leader can be exclusive or pinned
2203 if (hw_event
.exclusive
|| hw_event
.pinned
)
2204 goto err_put_context
;
2208 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2211 goto err_put_context
;
2213 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2215 goto err_free_put_context
;
2217 counter_file
= fget_light(ret
, &fput_needed2
);
2219 goto err_free_put_context
;
2221 counter
->filp
= counter_file
;
2222 mutex_lock(&ctx
->mutex
);
2223 perf_install_in_context(ctx
, counter
, cpu
);
2224 mutex_unlock(&ctx
->mutex
);
2226 fput_light(counter_file
, fput_needed2
);
2229 fput_light(group_file
, fput_needed
);
2233 err_free_put_context
:
2243 * Initialize the perf_counter context in a task_struct:
2246 __perf_counter_init_context(struct perf_counter_context
*ctx
,
2247 struct task_struct
*task
)
2249 memset(ctx
, 0, sizeof(*ctx
));
2250 spin_lock_init(&ctx
->lock
);
2251 mutex_init(&ctx
->mutex
);
2252 INIT_LIST_HEAD(&ctx
->counter_list
);
2253 INIT_LIST_HEAD(&ctx
->event_list
);
2258 * inherit a counter from parent task to child task:
2260 static struct perf_counter
*
2261 inherit_counter(struct perf_counter
*parent_counter
,
2262 struct task_struct
*parent
,
2263 struct perf_counter_context
*parent_ctx
,
2264 struct task_struct
*child
,
2265 struct perf_counter
*group_leader
,
2266 struct perf_counter_context
*child_ctx
)
2268 struct perf_counter
*child_counter
;
2271 * Instead of creating recursive hierarchies of counters,
2272 * we link inherited counters back to the original parent,
2273 * which has a filp for sure, which we use as the reference
2276 if (parent_counter
->parent
)
2277 parent_counter
= parent_counter
->parent
;
2279 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2280 parent_counter
->cpu
, child_ctx
,
2281 group_leader
, GFP_KERNEL
);
2286 * Link it up in the child's context:
2288 child_counter
->task
= child
;
2289 list_add_counter(child_counter
, child_ctx
);
2290 child_ctx
->nr_counters
++;
2292 child_counter
->parent
= parent_counter
;
2294 * inherit into child's child as well:
2296 child_counter
->hw_event
.inherit
= 1;
2299 * Get a reference to the parent filp - we will fput it
2300 * when the child counter exits. This is safe to do because
2301 * we are in the parent and we know that the filp still
2302 * exists and has a nonzero count:
2304 atomic_long_inc(&parent_counter
->filp
->f_count
);
2307 * Link this into the parent counter's child list
2309 mutex_lock(&parent_counter
->mutex
);
2310 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
2313 * Make the child state follow the state of the parent counter,
2314 * not its hw_event.disabled bit. We hold the parent's mutex,
2315 * so we won't race with perf_counter_{en,dis}able_family.
2317 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
2318 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2320 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
2322 mutex_unlock(&parent_counter
->mutex
);
2324 return child_counter
;
2327 static int inherit_group(struct perf_counter
*parent_counter
,
2328 struct task_struct
*parent
,
2329 struct perf_counter_context
*parent_ctx
,
2330 struct task_struct
*child
,
2331 struct perf_counter_context
*child_ctx
)
2333 struct perf_counter
*leader
;
2334 struct perf_counter
*sub
;
2336 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
2337 child
, NULL
, child_ctx
);
2340 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
2341 if (!inherit_counter(sub
, parent
, parent_ctx
,
2342 child
, leader
, child_ctx
))
2348 static void sync_child_counter(struct perf_counter
*child_counter
,
2349 struct perf_counter
*parent_counter
)
2351 u64 parent_val
, child_val
;
2353 parent_val
= atomic64_read(&parent_counter
->count
);
2354 child_val
= atomic64_read(&child_counter
->count
);
2357 * Add back the child's count to the parent's count:
2359 atomic64_add(child_val
, &parent_counter
->count
);
2362 * Remove this counter from the parent's list
2364 mutex_lock(&parent_counter
->mutex
);
2365 list_del_init(&child_counter
->child_list
);
2366 mutex_unlock(&parent_counter
->mutex
);
2369 * Release the parent counter, if this was the last
2372 fput(parent_counter
->filp
);
2376 __perf_counter_exit_task(struct task_struct
*child
,
2377 struct perf_counter
*child_counter
,
2378 struct perf_counter_context
*child_ctx
)
2380 struct perf_counter
*parent_counter
;
2381 struct perf_counter
*sub
, *tmp
;
2384 * If we do not self-reap then we have to wait for the
2385 * child task to unschedule (it will happen for sure),
2386 * so that its counter is at its final count. (This
2387 * condition triggers rarely - child tasks usually get
2388 * off their CPU before the parent has a chance to
2389 * get this far into the reaping action)
2391 if (child
!= current
) {
2392 wait_task_inactive(child
, 0);
2393 list_del_init(&child_counter
->list_entry
);
2395 struct perf_cpu_context
*cpuctx
;
2396 unsigned long flags
;
2400 * Disable and unlink this counter.
2402 * Be careful about zapping the list - IRQ/NMI context
2403 * could still be processing it:
2405 curr_rq_lock_irq_save(&flags
);
2406 perf_flags
= hw_perf_save_disable();
2408 cpuctx
= &__get_cpu_var(perf_cpu_context
);
2410 group_sched_out(child_counter
, cpuctx
, child_ctx
);
2412 list_del_init(&child_counter
->list_entry
);
2414 child_ctx
->nr_counters
--;
2416 hw_perf_restore(perf_flags
);
2417 curr_rq_unlock_irq_restore(&flags
);
2420 parent_counter
= child_counter
->parent
;
2422 * It can happen that parent exits first, and has counters
2423 * that are still around due to the child reference. These
2424 * counters need to be zapped - but otherwise linger.
2426 if (parent_counter
) {
2427 sync_child_counter(child_counter
, parent_counter
);
2428 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
2431 sync_child_counter(sub
, sub
->parent
);
2435 free_counter(child_counter
);
2440 * When a child task exits, feed back counter values to parent counters.
2442 * Note: we may be running in child context, but the PID is not hashed
2443 * anymore so new counters will not be added.
2445 void perf_counter_exit_task(struct task_struct
*child
)
2447 struct perf_counter
*child_counter
, *tmp
;
2448 struct perf_counter_context
*child_ctx
;
2450 child_ctx
= &child
->perf_counter_ctx
;
2452 if (likely(!child_ctx
->nr_counters
))
2455 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
2457 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
2461 * Initialize the perf_counter context in task_struct
2463 void perf_counter_init_task(struct task_struct
*child
)
2465 struct perf_counter_context
*child_ctx
, *parent_ctx
;
2466 struct perf_counter
*counter
;
2467 struct task_struct
*parent
= current
;
2469 child_ctx
= &child
->perf_counter_ctx
;
2470 parent_ctx
= &parent
->perf_counter_ctx
;
2472 __perf_counter_init_context(child_ctx
, child
);
2475 * This is executed from the parent task context, so inherit
2476 * counters that have been marked for cloning:
2479 if (likely(!parent_ctx
->nr_counters
))
2483 * Lock the parent list. No need to lock the child - not PID
2484 * hashed yet and not running, so nobody can access it.
2486 mutex_lock(&parent_ctx
->mutex
);
2489 * We dont have to disable NMIs - we are only looking at
2490 * the list, not manipulating it:
2492 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
2493 if (!counter
->hw_event
.inherit
)
2496 if (inherit_group(counter
, parent
,
2497 parent_ctx
, child
, child_ctx
))
2501 mutex_unlock(&parent_ctx
->mutex
);
2504 static void __cpuinit
perf_counter_init_cpu(int cpu
)
2506 struct perf_cpu_context
*cpuctx
;
2508 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2509 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
2511 mutex_lock(&perf_resource_mutex
);
2512 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
2513 mutex_unlock(&perf_resource_mutex
);
2515 hw_perf_counter_setup(cpu
);
2518 #ifdef CONFIG_HOTPLUG_CPU
2519 static void __perf_counter_exit_cpu(void *info
)
2521 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
2522 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
2523 struct perf_counter
*counter
, *tmp
;
2525 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
2526 __perf_counter_remove_from_context(counter
);
2528 static void perf_counter_exit_cpu(int cpu
)
2530 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2531 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
2533 mutex_lock(&ctx
->mutex
);
2534 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
2535 mutex_unlock(&ctx
->mutex
);
2538 static inline void perf_counter_exit_cpu(int cpu
) { }
2541 static int __cpuinit
2542 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
2544 unsigned int cpu
= (long)hcpu
;
2548 case CPU_UP_PREPARE
:
2549 case CPU_UP_PREPARE_FROZEN
:
2550 perf_counter_init_cpu(cpu
);
2553 case CPU_DOWN_PREPARE
:
2554 case CPU_DOWN_PREPARE_FROZEN
:
2555 perf_counter_exit_cpu(cpu
);
2565 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
2566 .notifier_call
= perf_cpu_notify
,
2569 static int __init
perf_counter_init(void)
2571 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
2572 (void *)(long)smp_processor_id());
2573 register_cpu_notifier(&perf_cpu_nb
);
2577 early_initcall(perf_counter_init
);
2579 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
2581 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
2585 perf_set_reserve_percpu(struct sysdev_class
*class,
2589 struct perf_cpu_context
*cpuctx
;
2593 err
= strict_strtoul(buf
, 10, &val
);
2596 if (val
> perf_max_counters
)
2599 mutex_lock(&perf_resource_mutex
);
2600 perf_reserved_percpu
= val
;
2601 for_each_online_cpu(cpu
) {
2602 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2603 spin_lock_irq(&cpuctx
->ctx
.lock
);
2604 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
2605 perf_max_counters
- perf_reserved_percpu
);
2606 cpuctx
->max_pertask
= mpt
;
2607 spin_unlock_irq(&cpuctx
->ctx
.lock
);
2609 mutex_unlock(&perf_resource_mutex
);
2614 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
2616 return sprintf(buf
, "%d\n", perf_overcommit
);
2620 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
2625 err
= strict_strtoul(buf
, 10, &val
);
2631 mutex_lock(&perf_resource_mutex
);
2632 perf_overcommit
= val
;
2633 mutex_unlock(&perf_resource_mutex
);
2638 static SYSDEV_CLASS_ATTR(
2641 perf_show_reserve_percpu
,
2642 perf_set_reserve_percpu
2645 static SYSDEV_CLASS_ATTR(
2648 perf_show_overcommit
,
2652 static struct attribute
*perfclass_attrs
[] = {
2653 &attr_reserve_percpu
.attr
,
2654 &attr_overcommit
.attr
,
2658 static struct attribute_group perfclass_attr_group
= {
2659 .attrs
= perfclass_attrs
,
2660 .name
= "perf_counters",
2663 static int __init
perf_counter_sysfs_init(void)
2665 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
2666 &perfclass_attr_group
);
2668 device_initcall(perf_counter_sysfs_init
);