2 * Performance counter core code
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/sysfs.h>
19 #include <linux/ptrace.h>
20 #include <linux/percpu.h>
21 #include <linux/vmstat.h>
22 #include <linux/hardirq.h>
23 #include <linux/rculist.h>
24 #include <linux/uaccess.h>
25 #include <linux/syscalls.h>
26 #include <linux/anon_inodes.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/perf_counter.h>
29 #include <linux/dcache.h>
31 #include <asm/irq_regs.h>
34 * Each CPU has a list of per CPU counters:
36 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
38 int perf_max_counters __read_mostly
= 1;
39 static int perf_reserved_percpu __read_mostly
;
40 static int perf_overcommit __read_mostly
= 1;
42 static atomic_t nr_counters __read_mostly
;
43 static atomic_t nr_mmap_tracking __read_mostly
;
44 static atomic_t nr_munmap_tracking __read_mostly
;
45 static atomic_t nr_comm_tracking __read_mostly
;
47 int sysctl_perf_counter_priv __read_mostly
; /* do we need to be privileged */
48 int sysctl_perf_counter_mlock __read_mostly
= 512; /* 'free' kb per user */
51 * Lock for (sysadmin-configurable) counter reservations:
53 static DEFINE_SPINLOCK(perf_resource_lock
);
56 * Architecture provided APIs - weak aliases:
58 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
63 void __weak
hw_perf_disable(void) { barrier(); }
64 void __weak
hw_perf_enable(void) { barrier(); }
66 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
67 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
68 struct perf_cpu_context
*cpuctx
,
69 struct perf_counter_context
*ctx
, int cpu
)
74 void __weak
perf_counter_print_debug(void) { }
76 static DEFINE_PER_CPU(int, disable_count
);
78 void __perf_disable(void)
80 __get_cpu_var(disable_count
)++;
83 bool __perf_enable(void)
85 return !--__get_cpu_var(disable_count
);
88 void perf_disable(void)
94 void perf_enable(void)
101 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
103 struct perf_counter
*group_leader
= counter
->group_leader
;
106 * Depending on whether it is a standalone or sibling counter,
107 * add it straight to the context's counter list, or to the group
108 * leader's sibling list:
110 if (group_leader
== counter
)
111 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
113 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
114 group_leader
->nr_siblings
++;
117 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
122 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
124 struct perf_counter
*sibling
, *tmp
;
128 list_del_init(&counter
->list_entry
);
129 list_del_rcu(&counter
->event_entry
);
131 if (counter
->group_leader
!= counter
)
132 counter
->group_leader
->nr_siblings
--;
135 * If this was a group counter with sibling counters then
136 * upgrade the siblings to singleton counters by adding them
137 * to the context list directly:
139 list_for_each_entry_safe(sibling
, tmp
,
140 &counter
->sibling_list
, list_entry
) {
142 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
143 sibling
->group_leader
= sibling
;
148 counter_sched_out(struct perf_counter
*counter
,
149 struct perf_cpu_context
*cpuctx
,
150 struct perf_counter_context
*ctx
)
152 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
155 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
156 counter
->tstamp_stopped
= ctx
->time
;
157 counter
->pmu
->disable(counter
);
160 if (!is_software_counter(counter
))
161 cpuctx
->active_oncpu
--;
163 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
164 cpuctx
->exclusive
= 0;
168 group_sched_out(struct perf_counter
*group_counter
,
169 struct perf_cpu_context
*cpuctx
,
170 struct perf_counter_context
*ctx
)
172 struct perf_counter
*counter
;
174 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
177 counter_sched_out(group_counter
, cpuctx
, ctx
);
180 * Schedule out siblings (if any):
182 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
183 counter_sched_out(counter
, cpuctx
, ctx
);
185 if (group_counter
->hw_event
.exclusive
)
186 cpuctx
->exclusive
= 0;
190 * Cross CPU call to remove a performance counter
192 * We disable the counter on the hardware level first. After that we
193 * remove it from the context list.
195 static void __perf_counter_remove_from_context(void *info
)
197 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
198 struct perf_counter
*counter
= info
;
199 struct perf_counter_context
*ctx
= counter
->ctx
;
203 * If this is a task context, we need to check whether it is
204 * the current task context of this cpu. If not it has been
205 * scheduled out before the smp call arrived.
207 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
210 spin_lock_irqsave(&ctx
->lock
, flags
);
212 counter_sched_out(counter
, cpuctx
, ctx
);
214 counter
->task
= NULL
;
217 * Protect the list operation against NMI by disabling the
218 * counters on a global level. NOP for non NMI based counters.
221 list_del_counter(counter
, ctx
);
226 * Allow more per task counters with respect to the
229 cpuctx
->max_pertask
=
230 min(perf_max_counters
- ctx
->nr_counters
,
231 perf_max_counters
- perf_reserved_percpu
);
234 spin_unlock_irqrestore(&ctx
->lock
, flags
);
239 * Remove the counter from a task's (or a CPU's) list of counters.
241 * Must be called with counter->mutex and ctx->mutex held.
243 * CPU counters are removed with a smp call. For task counters we only
244 * call when the task is on a CPU.
246 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
248 struct perf_counter_context
*ctx
= counter
->ctx
;
249 struct task_struct
*task
= ctx
->task
;
253 * Per cpu counters are removed via an smp call and
254 * the removal is always sucessful.
256 smp_call_function_single(counter
->cpu
,
257 __perf_counter_remove_from_context
,
263 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
266 spin_lock_irq(&ctx
->lock
);
268 * If the context is active we need to retry the smp call.
270 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
271 spin_unlock_irq(&ctx
->lock
);
276 * The lock prevents that this context is scheduled in so we
277 * can remove the counter safely, if the call above did not
280 if (!list_empty(&counter
->list_entry
)) {
281 list_del_counter(counter
, ctx
);
282 counter
->task
= NULL
;
284 spin_unlock_irq(&ctx
->lock
);
287 static inline u64
perf_clock(void)
289 return cpu_clock(smp_processor_id());
293 * Update the record of the current time in a context.
295 static void update_context_time(struct perf_counter_context
*ctx
)
297 u64 now
= perf_clock();
299 ctx
->time
+= now
- ctx
->timestamp
;
300 ctx
->timestamp
= now
;
304 * Update the total_time_enabled and total_time_running fields for a counter.
306 static void update_counter_times(struct perf_counter
*counter
)
308 struct perf_counter_context
*ctx
= counter
->ctx
;
311 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
314 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
316 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
317 run_end
= counter
->tstamp_stopped
;
321 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
325 * Update total_time_enabled and total_time_running for all counters in a group.
327 static void update_group_times(struct perf_counter
*leader
)
329 struct perf_counter
*counter
;
331 update_counter_times(leader
);
332 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
333 update_counter_times(counter
);
337 * Cross CPU call to disable a performance counter
339 static void __perf_counter_disable(void *info
)
341 struct perf_counter
*counter
= info
;
342 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
343 struct perf_counter_context
*ctx
= counter
->ctx
;
347 * If this is a per-task counter, need to check whether this
348 * counter's task is the current task on this cpu.
350 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
353 spin_lock_irqsave(&ctx
->lock
, flags
);
356 * If the counter is on, turn it off.
357 * If it is in error state, leave it in error state.
359 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
360 update_context_time(ctx
);
361 update_counter_times(counter
);
362 if (counter
== counter
->group_leader
)
363 group_sched_out(counter
, cpuctx
, ctx
);
365 counter_sched_out(counter
, cpuctx
, ctx
);
366 counter
->state
= PERF_COUNTER_STATE_OFF
;
369 spin_unlock_irqrestore(&ctx
->lock
, flags
);
375 static void perf_counter_disable(struct perf_counter
*counter
)
377 struct perf_counter_context
*ctx
= counter
->ctx
;
378 struct task_struct
*task
= ctx
->task
;
382 * Disable the counter on the cpu that it's on
384 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
390 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
392 spin_lock_irq(&ctx
->lock
);
394 * If the counter is still active, we need to retry the cross-call.
396 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
397 spin_unlock_irq(&ctx
->lock
);
402 * Since we have the lock this context can't be scheduled
403 * in, so we can change the state safely.
405 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
406 update_counter_times(counter
);
407 counter
->state
= PERF_COUNTER_STATE_OFF
;
410 spin_unlock_irq(&ctx
->lock
);
414 counter_sched_in(struct perf_counter
*counter
,
415 struct perf_cpu_context
*cpuctx
,
416 struct perf_counter_context
*ctx
,
419 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
422 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
423 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
425 * The new state must be visible before we turn it on in the hardware:
429 if (counter
->pmu
->enable(counter
)) {
430 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
435 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
437 if (!is_software_counter(counter
))
438 cpuctx
->active_oncpu
++;
441 if (counter
->hw_event
.exclusive
)
442 cpuctx
->exclusive
= 1;
448 group_sched_in(struct perf_counter
*group_counter
,
449 struct perf_cpu_context
*cpuctx
,
450 struct perf_counter_context
*ctx
,
453 struct perf_counter
*counter
, *partial_group
;
456 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
459 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
461 return ret
< 0 ? ret
: 0;
463 group_counter
->prev_state
= group_counter
->state
;
464 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
468 * Schedule in siblings as one group (if any):
470 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
471 counter
->prev_state
= counter
->state
;
472 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
473 partial_group
= counter
;
482 * Groups can be scheduled in as one unit only, so undo any
483 * partial group before returning:
485 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
486 if (counter
== partial_group
)
488 counter_sched_out(counter
, cpuctx
, ctx
);
490 counter_sched_out(group_counter
, cpuctx
, ctx
);
496 * Return 1 for a group consisting entirely of software counters,
497 * 0 if the group contains any hardware counters.
499 static int is_software_only_group(struct perf_counter
*leader
)
501 struct perf_counter
*counter
;
503 if (!is_software_counter(leader
))
506 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
507 if (!is_software_counter(counter
))
514 * Work out whether we can put this counter group on the CPU now.
516 static int group_can_go_on(struct perf_counter
*counter
,
517 struct perf_cpu_context
*cpuctx
,
521 * Groups consisting entirely of software counters can always go on.
523 if (is_software_only_group(counter
))
526 * If an exclusive group is already on, no other hardware
527 * counters can go on.
529 if (cpuctx
->exclusive
)
532 * If this group is exclusive and there are already
533 * counters on the CPU, it can't go on.
535 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
538 * Otherwise, try to add it if all previous groups were able
544 static void add_counter_to_ctx(struct perf_counter
*counter
,
545 struct perf_counter_context
*ctx
)
547 list_add_counter(counter
, ctx
);
548 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
549 counter
->tstamp_enabled
= ctx
->time
;
550 counter
->tstamp_running
= ctx
->time
;
551 counter
->tstamp_stopped
= ctx
->time
;
555 * Cross CPU call to install and enable a performance counter
557 static void __perf_install_in_context(void *info
)
559 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
560 struct perf_counter
*counter
= info
;
561 struct perf_counter_context
*ctx
= counter
->ctx
;
562 struct perf_counter
*leader
= counter
->group_leader
;
563 int cpu
= smp_processor_id();
568 * If this is a task context, we need to check whether it is
569 * the current task context of this cpu. If not it has been
570 * scheduled out before the smp call arrived.
572 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
575 spin_lock_irqsave(&ctx
->lock
, flags
);
576 update_context_time(ctx
);
579 * Protect the list operation against NMI by disabling the
580 * counters on a global level. NOP for non NMI based counters.
584 add_counter_to_ctx(counter
, ctx
);
587 * Don't put the counter on if it is disabled or if
588 * it is in a group and the group isn't on.
590 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
591 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
595 * An exclusive counter can't go on if there are already active
596 * hardware counters, and no hardware counter can go on if there
597 * is already an exclusive counter on.
599 if (!group_can_go_on(counter
, cpuctx
, 1))
602 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
606 * This counter couldn't go on. If it is in a group
607 * then we have to pull the whole group off.
608 * If the counter group is pinned then put it in error state.
610 if (leader
!= counter
)
611 group_sched_out(leader
, cpuctx
, ctx
);
612 if (leader
->hw_event
.pinned
) {
613 update_group_times(leader
);
614 leader
->state
= PERF_COUNTER_STATE_ERROR
;
618 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
619 cpuctx
->max_pertask
--;
624 spin_unlock_irqrestore(&ctx
->lock
, flags
);
628 * Attach a performance counter to a context
630 * First we add the counter to the list with the hardware enable bit
631 * in counter->hw_config cleared.
633 * If the counter is attached to a task which is on a CPU we use a smp
634 * call to enable it in the task context. The task might have been
635 * scheduled away, but we check this in the smp call again.
637 * Must be called with ctx->mutex held.
640 perf_install_in_context(struct perf_counter_context
*ctx
,
641 struct perf_counter
*counter
,
644 struct task_struct
*task
= ctx
->task
;
648 * Per cpu counters are installed via an smp call and
649 * the install is always sucessful.
651 smp_call_function_single(cpu
, __perf_install_in_context
,
656 counter
->task
= task
;
658 task_oncpu_function_call(task
, __perf_install_in_context
,
661 spin_lock_irq(&ctx
->lock
);
663 * we need to retry the smp call.
665 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
666 spin_unlock_irq(&ctx
->lock
);
671 * The lock prevents that this context is scheduled in so we
672 * can add the counter safely, if it the call above did not
675 if (list_empty(&counter
->list_entry
))
676 add_counter_to_ctx(counter
, ctx
);
677 spin_unlock_irq(&ctx
->lock
);
681 * Cross CPU call to enable a performance counter
683 static void __perf_counter_enable(void *info
)
685 struct perf_counter
*counter
= info
;
686 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
687 struct perf_counter_context
*ctx
= counter
->ctx
;
688 struct perf_counter
*leader
= counter
->group_leader
;
693 * If this is a per-task counter, need to check whether this
694 * counter's task is the current task on this cpu.
696 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
699 spin_lock_irqsave(&ctx
->lock
, flags
);
700 update_context_time(ctx
);
702 counter
->prev_state
= counter
->state
;
703 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
705 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
706 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
709 * If the counter is in a group and isn't the group leader,
710 * then don't put it on unless the group is on.
712 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
715 if (!group_can_go_on(counter
, cpuctx
, 1)) {
719 if (counter
== leader
)
720 err
= group_sched_in(counter
, cpuctx
, ctx
,
723 err
= counter_sched_in(counter
, cpuctx
, ctx
,
730 * If this counter can't go on and it's part of a
731 * group, then the whole group has to come off.
733 if (leader
!= counter
)
734 group_sched_out(leader
, cpuctx
, ctx
);
735 if (leader
->hw_event
.pinned
) {
736 update_group_times(leader
);
737 leader
->state
= PERF_COUNTER_STATE_ERROR
;
742 spin_unlock_irqrestore(&ctx
->lock
, flags
);
748 static void perf_counter_enable(struct perf_counter
*counter
)
750 struct perf_counter_context
*ctx
= counter
->ctx
;
751 struct task_struct
*task
= ctx
->task
;
755 * Enable the counter on the cpu that it's on
757 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
762 spin_lock_irq(&ctx
->lock
);
763 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
767 * If the counter is in error state, clear that first.
768 * That way, if we see the counter in error state below, we
769 * know that it has gone back into error state, as distinct
770 * from the task having been scheduled away before the
771 * cross-call arrived.
773 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
774 counter
->state
= PERF_COUNTER_STATE_OFF
;
777 spin_unlock_irq(&ctx
->lock
);
778 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
780 spin_lock_irq(&ctx
->lock
);
783 * If the context is active and the counter is still off,
784 * we need to retry the cross-call.
786 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
790 * Since we have the lock this context can't be scheduled
791 * in, so we can change the state safely.
793 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
794 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
795 counter
->tstamp_enabled
=
796 ctx
->time
- counter
->total_time_enabled
;
799 spin_unlock_irq(&ctx
->lock
);
802 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
805 * not supported on inherited counters
807 if (counter
->hw_event
.inherit
)
810 atomic_add(refresh
, &counter
->event_limit
);
811 perf_counter_enable(counter
);
816 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
817 struct perf_cpu_context
*cpuctx
)
819 struct perf_counter
*counter
;
821 spin_lock(&ctx
->lock
);
823 if (likely(!ctx
->nr_counters
))
825 update_context_time(ctx
);
828 if (ctx
->nr_active
) {
829 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
830 if (counter
!= counter
->group_leader
)
831 counter_sched_out(counter
, cpuctx
, ctx
);
833 group_sched_out(counter
, cpuctx
, ctx
);
838 spin_unlock(&ctx
->lock
);
842 * Called from scheduler to remove the counters of the current task,
843 * with interrupts disabled.
845 * We stop each counter and update the counter value in counter->count.
847 * This does not protect us against NMI, but disable()
848 * sets the disabled bit in the control field of counter _before_
849 * accessing the counter control register. If a NMI hits, then it will
850 * not restart the counter.
852 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
854 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
855 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
856 struct pt_regs
*regs
;
858 if (likely(!cpuctx
->task_ctx
))
861 update_context_time(ctx
);
863 regs
= task_pt_regs(task
);
864 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
865 __perf_counter_sched_out(ctx
, cpuctx
);
867 cpuctx
->task_ctx
= NULL
;
870 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
872 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
874 __perf_counter_sched_out(ctx
, cpuctx
);
875 cpuctx
->task_ctx
= NULL
;
878 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
880 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
884 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
885 struct perf_cpu_context
*cpuctx
, int cpu
)
887 struct perf_counter
*counter
;
890 spin_lock(&ctx
->lock
);
892 if (likely(!ctx
->nr_counters
))
895 ctx
->timestamp
= perf_clock();
900 * First go through the list and put on any pinned groups
901 * in order to give them the best chance of going on.
903 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
904 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
905 !counter
->hw_event
.pinned
)
907 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
910 if (counter
!= counter
->group_leader
)
911 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
913 if (group_can_go_on(counter
, cpuctx
, 1))
914 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
918 * If this pinned group hasn't been scheduled,
919 * put it in error state.
921 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
922 update_group_times(counter
);
923 counter
->state
= PERF_COUNTER_STATE_ERROR
;
927 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
929 * Ignore counters in OFF or ERROR state, and
930 * ignore pinned counters since we did them already.
932 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
933 counter
->hw_event
.pinned
)
937 * Listen to the 'cpu' scheduling filter constraint
940 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
943 if (counter
!= counter
->group_leader
) {
944 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
947 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
948 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
955 spin_unlock(&ctx
->lock
);
959 * Called from scheduler to add the counters of the current task
960 * with interrupts disabled.
962 * We restore the counter value and then enable it.
964 * This does not protect us against NMI, but enable()
965 * sets the enabled bit in the control field of counter _before_
966 * accessing the counter control register. If a NMI hits, then it will
967 * keep the counter running.
969 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
971 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
972 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
974 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
975 cpuctx
->task_ctx
= ctx
;
978 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
980 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
982 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
985 int perf_counter_task_disable(void)
987 struct task_struct
*curr
= current
;
988 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
989 struct perf_counter
*counter
;
992 if (likely(!ctx
->nr_counters
))
995 local_irq_save(flags
);
997 __perf_counter_task_sched_out(ctx
);
999 spin_lock(&ctx
->lock
);
1002 * Disable all the counters:
1006 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1007 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
1008 update_group_times(counter
);
1009 counter
->state
= PERF_COUNTER_STATE_OFF
;
1015 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1020 int perf_counter_task_enable(void)
1022 struct task_struct
*curr
= current
;
1023 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1024 struct perf_counter
*counter
;
1025 unsigned long flags
;
1028 if (likely(!ctx
->nr_counters
))
1031 local_irq_save(flags
);
1032 cpu
= smp_processor_id();
1034 __perf_counter_task_sched_out(ctx
);
1036 spin_lock(&ctx
->lock
);
1039 * Disable all the counters:
1043 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1044 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1046 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1047 counter
->tstamp_enabled
=
1048 ctx
->time
- counter
->total_time_enabled
;
1049 counter
->hw_event
.disabled
= 0;
1053 spin_unlock(&ctx
->lock
);
1055 perf_counter_task_sched_in(curr
, cpu
);
1057 local_irq_restore(flags
);
1062 static void perf_log_period(struct perf_counter
*counter
, u64 period
);
1064 static void perf_adjust_freq(struct perf_counter_context
*ctx
)
1066 struct perf_counter
*counter
;
1071 spin_lock(&ctx
->lock
);
1072 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1073 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1076 if (!counter
->hw_event
.freq
|| !counter
->hw_event
.irq_freq
)
1079 events
= HZ
* counter
->hw
.interrupts
* counter
->hw
.irq_period
;
1080 period
= div64_u64(events
, counter
->hw_event
.irq_freq
);
1082 delta
= (s64
)(1 + period
- counter
->hw
.irq_period
);
1085 irq_period
= counter
->hw
.irq_period
+ delta
;
1090 perf_log_period(counter
, irq_period
);
1092 counter
->hw
.irq_period
= irq_period
;
1093 counter
->hw
.interrupts
= 0;
1095 spin_unlock(&ctx
->lock
);
1099 * Round-robin a context's counters:
1101 static void rotate_ctx(struct perf_counter_context
*ctx
)
1103 struct perf_counter
*counter
;
1105 if (!ctx
->nr_counters
)
1108 spin_lock(&ctx
->lock
);
1110 * Rotate the first entry last (works just fine for group counters too):
1113 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1114 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1119 spin_unlock(&ctx
->lock
);
1122 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1124 struct perf_cpu_context
*cpuctx
;
1125 struct perf_counter_context
*ctx
;
1127 if (!atomic_read(&nr_counters
))
1130 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1131 ctx
= &curr
->perf_counter_ctx
;
1133 perf_adjust_freq(&cpuctx
->ctx
);
1134 perf_adjust_freq(ctx
);
1136 perf_counter_cpu_sched_out(cpuctx
);
1137 __perf_counter_task_sched_out(ctx
);
1139 rotate_ctx(&cpuctx
->ctx
);
1142 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1143 perf_counter_task_sched_in(curr
, cpu
);
1147 * Cross CPU call to read the hardware counter
1149 static void __read(void *info
)
1151 struct perf_counter
*counter
= info
;
1152 struct perf_counter_context
*ctx
= counter
->ctx
;
1153 unsigned long flags
;
1155 local_irq_save(flags
);
1157 update_context_time(ctx
);
1158 counter
->pmu
->read(counter
);
1159 update_counter_times(counter
);
1160 local_irq_restore(flags
);
1163 static u64
perf_counter_read(struct perf_counter
*counter
)
1166 * If counter is enabled and currently active on a CPU, update the
1167 * value in the counter structure:
1169 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1170 smp_call_function_single(counter
->oncpu
,
1171 __read
, counter
, 1);
1172 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1173 update_counter_times(counter
);
1176 return atomic64_read(&counter
->count
);
1179 static void put_context(struct perf_counter_context
*ctx
)
1182 put_task_struct(ctx
->task
);
1185 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1187 struct perf_cpu_context
*cpuctx
;
1188 struct perf_counter_context
*ctx
;
1189 struct task_struct
*task
;
1192 * If cpu is not a wildcard then this is a percpu counter:
1195 /* Must be root to operate on a CPU counter: */
1196 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1197 return ERR_PTR(-EACCES
);
1199 if (cpu
< 0 || cpu
> num_possible_cpus())
1200 return ERR_PTR(-EINVAL
);
1203 * We could be clever and allow to attach a counter to an
1204 * offline CPU and activate it when the CPU comes up, but
1207 if (!cpu_isset(cpu
, cpu_online_map
))
1208 return ERR_PTR(-ENODEV
);
1210 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1220 task
= find_task_by_vpid(pid
);
1222 get_task_struct(task
);
1226 return ERR_PTR(-ESRCH
);
1228 ctx
= &task
->perf_counter_ctx
;
1231 /* Reuse ptrace permission checks for now. */
1232 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1234 return ERR_PTR(-EACCES
);
1240 static void free_counter_rcu(struct rcu_head
*head
)
1242 struct perf_counter
*counter
;
1244 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1248 static void perf_pending_sync(struct perf_counter
*counter
);
1250 static void free_counter(struct perf_counter
*counter
)
1252 perf_pending_sync(counter
);
1254 atomic_dec(&nr_counters
);
1255 if (counter
->hw_event
.mmap
)
1256 atomic_dec(&nr_mmap_tracking
);
1257 if (counter
->hw_event
.munmap
)
1258 atomic_dec(&nr_munmap_tracking
);
1259 if (counter
->hw_event
.comm
)
1260 atomic_dec(&nr_comm_tracking
);
1262 if (counter
->destroy
)
1263 counter
->destroy(counter
);
1265 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1269 * Called when the last reference to the file is gone.
1271 static int perf_release(struct inode
*inode
, struct file
*file
)
1273 struct perf_counter
*counter
= file
->private_data
;
1274 struct perf_counter_context
*ctx
= counter
->ctx
;
1276 file
->private_data
= NULL
;
1278 mutex_lock(&ctx
->mutex
);
1279 mutex_lock(&counter
->mutex
);
1281 perf_counter_remove_from_context(counter
);
1283 mutex_unlock(&counter
->mutex
);
1284 mutex_unlock(&ctx
->mutex
);
1286 free_counter(counter
);
1293 * Read the performance counter - simple non blocking version for now
1296 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1302 * Return end-of-file for a read on a counter that is in
1303 * error state (i.e. because it was pinned but it couldn't be
1304 * scheduled on to the CPU at some point).
1306 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1309 mutex_lock(&counter
->mutex
);
1310 values
[0] = perf_counter_read(counter
);
1312 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1313 values
[n
++] = counter
->total_time_enabled
+
1314 atomic64_read(&counter
->child_total_time_enabled
);
1315 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1316 values
[n
++] = counter
->total_time_running
+
1317 atomic64_read(&counter
->child_total_time_running
);
1318 mutex_unlock(&counter
->mutex
);
1320 if (count
< n
* sizeof(u64
))
1322 count
= n
* sizeof(u64
);
1324 if (copy_to_user(buf
, values
, count
))
1331 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1333 struct perf_counter
*counter
= file
->private_data
;
1335 return perf_read_hw(counter
, buf
, count
);
1338 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1340 struct perf_counter
*counter
= file
->private_data
;
1341 struct perf_mmap_data
*data
;
1342 unsigned int events
= POLL_HUP
;
1345 data
= rcu_dereference(counter
->data
);
1347 events
= atomic_xchg(&data
->poll
, 0);
1350 poll_wait(file
, &counter
->waitq
, wait
);
1355 static void perf_counter_reset(struct perf_counter
*counter
)
1357 (void)perf_counter_read(counter
);
1358 atomic64_set(&counter
->count
, 0);
1359 perf_counter_update_userpage(counter
);
1362 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1363 void (*func
)(struct perf_counter
*))
1365 struct perf_counter_context
*ctx
= counter
->ctx
;
1366 struct perf_counter
*sibling
;
1368 spin_lock_irq(&ctx
->lock
);
1369 counter
= counter
->group_leader
;
1372 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1374 spin_unlock_irq(&ctx
->lock
);
1377 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1378 void (*func
)(struct perf_counter
*))
1380 struct perf_counter
*child
;
1382 mutex_lock(&counter
->mutex
);
1384 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1386 mutex_unlock(&counter
->mutex
);
1389 static void perf_counter_for_each(struct perf_counter
*counter
,
1390 void (*func
)(struct perf_counter
*))
1392 struct perf_counter
*child
;
1394 mutex_lock(&counter
->mutex
);
1395 perf_counter_for_each_sibling(counter
, func
);
1396 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1397 perf_counter_for_each_sibling(child
, func
);
1398 mutex_unlock(&counter
->mutex
);
1401 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1403 struct perf_counter
*counter
= file
->private_data
;
1404 void (*func
)(struct perf_counter
*);
1408 case PERF_COUNTER_IOC_ENABLE
:
1409 func
= perf_counter_enable
;
1411 case PERF_COUNTER_IOC_DISABLE
:
1412 func
= perf_counter_disable
;
1414 case PERF_COUNTER_IOC_RESET
:
1415 func
= perf_counter_reset
;
1418 case PERF_COUNTER_IOC_REFRESH
:
1419 return perf_counter_refresh(counter
, arg
);
1424 if (flags
& PERF_IOC_FLAG_GROUP
)
1425 perf_counter_for_each(counter
, func
);
1427 perf_counter_for_each_child(counter
, func
);
1433 * Callers need to ensure there can be no nesting of this function, otherwise
1434 * the seqlock logic goes bad. We can not serialize this because the arch
1435 * code calls this from NMI context.
1437 void perf_counter_update_userpage(struct perf_counter
*counter
)
1439 struct perf_mmap_data
*data
;
1440 struct perf_counter_mmap_page
*userpg
;
1443 data
= rcu_dereference(counter
->data
);
1447 userpg
= data
->user_page
;
1450 * Disable preemption so as to not let the corresponding user-space
1451 * spin too long if we get preempted.
1456 userpg
->index
= counter
->hw
.idx
;
1457 userpg
->offset
= atomic64_read(&counter
->count
);
1458 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1459 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1468 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1470 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1471 struct perf_mmap_data
*data
;
1472 int ret
= VM_FAULT_SIGBUS
;
1475 data
= rcu_dereference(counter
->data
);
1479 if (vmf
->pgoff
== 0) {
1480 vmf
->page
= virt_to_page(data
->user_page
);
1482 int nr
= vmf
->pgoff
- 1;
1484 if ((unsigned)nr
> data
->nr_pages
)
1487 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1489 get_page(vmf
->page
);
1497 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1499 struct perf_mmap_data
*data
;
1503 WARN_ON(atomic_read(&counter
->mmap_count
));
1505 size
= sizeof(struct perf_mmap_data
);
1506 size
+= nr_pages
* sizeof(void *);
1508 data
= kzalloc(size
, GFP_KERNEL
);
1512 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1513 if (!data
->user_page
)
1514 goto fail_user_page
;
1516 for (i
= 0; i
< nr_pages
; i
++) {
1517 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1518 if (!data
->data_pages
[i
])
1519 goto fail_data_pages
;
1522 data
->nr_pages
= nr_pages
;
1523 atomic_set(&data
->lock
, -1);
1525 rcu_assign_pointer(counter
->data
, data
);
1530 for (i
--; i
>= 0; i
--)
1531 free_page((unsigned long)data
->data_pages
[i
]);
1533 free_page((unsigned long)data
->user_page
);
1542 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1544 struct perf_mmap_data
*data
= container_of(rcu_head
,
1545 struct perf_mmap_data
, rcu_head
);
1548 free_page((unsigned long)data
->user_page
);
1549 for (i
= 0; i
< data
->nr_pages
; i
++)
1550 free_page((unsigned long)data
->data_pages
[i
]);
1554 static void perf_mmap_data_free(struct perf_counter
*counter
)
1556 struct perf_mmap_data
*data
= counter
->data
;
1558 WARN_ON(atomic_read(&counter
->mmap_count
));
1560 rcu_assign_pointer(counter
->data
, NULL
);
1561 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1564 static void perf_mmap_open(struct vm_area_struct
*vma
)
1566 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1568 atomic_inc(&counter
->mmap_count
);
1571 static void perf_mmap_close(struct vm_area_struct
*vma
)
1573 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1575 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1576 &counter
->mmap_mutex
)) {
1577 struct user_struct
*user
= current_user();
1579 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
1580 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1581 perf_mmap_data_free(counter
);
1582 mutex_unlock(&counter
->mmap_mutex
);
1586 static struct vm_operations_struct perf_mmap_vmops
= {
1587 .open
= perf_mmap_open
,
1588 .close
= perf_mmap_close
,
1589 .fault
= perf_mmap_fault
,
1592 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1594 struct perf_counter
*counter
= file
->private_data
;
1595 struct user_struct
*user
= current_user();
1596 unsigned long vma_size
;
1597 unsigned long nr_pages
;
1598 unsigned long user_locked
, user_lock_limit
;
1599 unsigned long locked
, lock_limit
;
1600 long user_extra
, extra
;
1603 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1606 vma_size
= vma
->vm_end
- vma
->vm_start
;
1607 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1610 * If we have data pages ensure they're a power-of-two number, so we
1611 * can do bitmasks instead of modulo.
1613 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1616 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1619 if (vma
->vm_pgoff
!= 0)
1622 mutex_lock(&counter
->mmap_mutex
);
1623 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1624 if (nr_pages
!= counter
->data
->nr_pages
)
1629 user_extra
= nr_pages
+ 1;
1630 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1631 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
1634 if (user_locked
> user_lock_limit
)
1635 extra
= user_locked
- user_lock_limit
;
1637 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1638 lock_limit
>>= PAGE_SHIFT
;
1639 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1641 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1646 WARN_ON(counter
->data
);
1647 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1651 atomic_set(&counter
->mmap_count
, 1);
1652 atomic_long_add(user_extra
, &user
->locked_vm
);
1653 vma
->vm_mm
->locked_vm
+= extra
;
1654 counter
->data
->nr_locked
= extra
;
1656 mutex_unlock(&counter
->mmap_mutex
);
1658 vma
->vm_flags
&= ~VM_MAYWRITE
;
1659 vma
->vm_flags
|= VM_RESERVED
;
1660 vma
->vm_ops
= &perf_mmap_vmops
;
1665 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1667 struct perf_counter
*counter
= filp
->private_data
;
1668 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1671 mutex_lock(&inode
->i_mutex
);
1672 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1673 mutex_unlock(&inode
->i_mutex
);
1681 static const struct file_operations perf_fops
= {
1682 .release
= perf_release
,
1685 .unlocked_ioctl
= perf_ioctl
,
1686 .compat_ioctl
= perf_ioctl
,
1688 .fasync
= perf_fasync
,
1692 * Perf counter wakeup
1694 * If there's data, ensure we set the poll() state and publish everything
1695 * to user-space before waking everybody up.
1698 void perf_counter_wakeup(struct perf_counter
*counter
)
1700 wake_up_all(&counter
->waitq
);
1702 if (counter
->pending_kill
) {
1703 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1704 counter
->pending_kill
= 0;
1711 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1713 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1714 * single linked list and use cmpxchg() to add entries lockless.
1717 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1719 struct perf_counter
*counter
= container_of(entry
,
1720 struct perf_counter
, pending
);
1722 if (counter
->pending_disable
) {
1723 counter
->pending_disable
= 0;
1724 perf_counter_disable(counter
);
1727 if (counter
->pending_wakeup
) {
1728 counter
->pending_wakeup
= 0;
1729 perf_counter_wakeup(counter
);
1733 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1735 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1739 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1740 void (*func
)(struct perf_pending_entry
*))
1742 struct perf_pending_entry
**head
;
1744 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1749 head
= &get_cpu_var(perf_pending_head
);
1752 entry
->next
= *head
;
1753 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1755 set_perf_counter_pending();
1757 put_cpu_var(perf_pending_head
);
1760 static int __perf_pending_run(void)
1762 struct perf_pending_entry
*list
;
1765 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1766 while (list
!= PENDING_TAIL
) {
1767 void (*func
)(struct perf_pending_entry
*);
1768 struct perf_pending_entry
*entry
= list
;
1775 * Ensure we observe the unqueue before we issue the wakeup,
1776 * so that we won't be waiting forever.
1777 * -- see perf_not_pending().
1788 static inline int perf_not_pending(struct perf_counter
*counter
)
1791 * If we flush on whatever cpu we run, there is a chance we don't
1795 __perf_pending_run();
1799 * Ensure we see the proper queue state before going to sleep
1800 * so that we do not miss the wakeup. -- see perf_pending_handle()
1803 return counter
->pending
.next
== NULL
;
1806 static void perf_pending_sync(struct perf_counter
*counter
)
1808 wait_event(counter
->waitq
, perf_not_pending(counter
));
1811 void perf_counter_do_pending(void)
1813 __perf_pending_run();
1817 * Callchain support -- arch specific
1820 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1829 struct perf_output_handle
{
1830 struct perf_counter
*counter
;
1831 struct perf_mmap_data
*data
;
1832 unsigned int offset
;
1837 unsigned long flags
;
1840 static void perf_output_wakeup(struct perf_output_handle
*handle
)
1842 atomic_set(&handle
->data
->poll
, POLL_IN
);
1845 handle
->counter
->pending_wakeup
= 1;
1846 perf_pending_queue(&handle
->counter
->pending
,
1847 perf_pending_counter
);
1849 perf_counter_wakeup(handle
->counter
);
1853 * Curious locking construct.
1855 * We need to ensure a later event doesn't publish a head when a former
1856 * event isn't done writing. However since we need to deal with NMIs we
1857 * cannot fully serialize things.
1859 * What we do is serialize between CPUs so we only have to deal with NMI
1860 * nesting on a single CPU.
1862 * We only publish the head (and generate a wakeup) when the outer-most
1865 static void perf_output_lock(struct perf_output_handle
*handle
)
1867 struct perf_mmap_data
*data
= handle
->data
;
1872 local_irq_save(handle
->flags
);
1873 cpu
= smp_processor_id();
1875 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
1878 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1884 static void perf_output_unlock(struct perf_output_handle
*handle
)
1886 struct perf_mmap_data
*data
= handle
->data
;
1889 data
->done_head
= data
->head
;
1891 if (!handle
->locked
)
1896 * The xchg implies a full barrier that ensures all writes are done
1897 * before we publish the new head, matched by a rmb() in userspace when
1898 * reading this position.
1900 while ((head
= atomic_xchg(&data
->done_head
, 0)))
1901 data
->user_page
->data_head
= head
;
1904 * NMI can happen here, which means we can miss a done_head update.
1907 cpu
= atomic_xchg(&data
->lock
, -1);
1908 WARN_ON_ONCE(cpu
!= smp_processor_id());
1911 * Therefore we have to validate we did not indeed do so.
1913 if (unlikely(atomic_read(&data
->done_head
))) {
1915 * Since we had it locked, we can lock it again.
1917 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1923 if (atomic_xchg(&data
->wakeup
, 0))
1924 perf_output_wakeup(handle
);
1926 local_irq_restore(handle
->flags
);
1929 static int perf_output_begin(struct perf_output_handle
*handle
,
1930 struct perf_counter
*counter
, unsigned int size
,
1931 int nmi
, int overflow
)
1933 struct perf_mmap_data
*data
;
1934 unsigned int offset
, head
;
1937 * For inherited counters we send all the output towards the parent.
1939 if (counter
->parent
)
1940 counter
= counter
->parent
;
1943 data
= rcu_dereference(counter
->data
);
1947 handle
->data
= data
;
1948 handle
->counter
= counter
;
1950 handle
->overflow
= overflow
;
1952 if (!data
->nr_pages
)
1955 perf_output_lock(handle
);
1958 offset
= head
= atomic_read(&data
->head
);
1960 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1962 handle
->offset
= offset
;
1963 handle
->head
= head
;
1965 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
1966 atomic_set(&data
->wakeup
, 1);
1971 perf_output_wakeup(handle
);
1978 static void perf_output_copy(struct perf_output_handle
*handle
,
1979 void *buf
, unsigned int len
)
1981 unsigned int pages_mask
;
1982 unsigned int offset
;
1986 offset
= handle
->offset
;
1987 pages_mask
= handle
->data
->nr_pages
- 1;
1988 pages
= handle
->data
->data_pages
;
1991 unsigned int page_offset
;
1994 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1995 page_offset
= offset
& (PAGE_SIZE
- 1);
1996 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1998 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2005 handle
->offset
= offset
;
2008 * Check we didn't copy past our reservation window, taking the
2009 * possible unsigned int wrap into account.
2011 WARN_ON_ONCE(((int)(handle
->head
- handle
->offset
)) < 0);
2014 #define perf_output_put(handle, x) \
2015 perf_output_copy((handle), &(x), sizeof(x))
2017 static void perf_output_end(struct perf_output_handle
*handle
)
2019 struct perf_counter
*counter
= handle
->counter
;
2020 struct perf_mmap_data
*data
= handle
->data
;
2022 int wakeup_events
= counter
->hw_event
.wakeup_events
;
2024 if (handle
->overflow
&& wakeup_events
) {
2025 int events
= atomic_inc_return(&data
->events
);
2026 if (events
>= wakeup_events
) {
2027 atomic_sub(wakeup_events
, &data
->events
);
2028 atomic_set(&data
->wakeup
, 1);
2032 perf_output_unlock(handle
);
2036 static void perf_counter_output(struct perf_counter
*counter
,
2037 int nmi
, struct pt_regs
*regs
, u64 addr
)
2040 u64 record_type
= counter
->hw_event
.record_type
;
2041 struct perf_output_handle handle
;
2042 struct perf_event_header header
;
2051 struct perf_callchain_entry
*callchain
= NULL
;
2052 int callchain_size
= 0;
2059 header
.size
= sizeof(header
);
2061 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
2062 header
.misc
|= perf_misc_flags(regs
);
2064 if (record_type
& PERF_RECORD_IP
) {
2065 ip
= perf_instruction_pointer(regs
);
2066 header
.type
|= PERF_RECORD_IP
;
2067 header
.size
+= sizeof(ip
);
2070 if (record_type
& PERF_RECORD_TID
) {
2071 /* namespace issues */
2072 tid_entry
.pid
= current
->group_leader
->pid
;
2073 tid_entry
.tid
= current
->pid
;
2075 header
.type
|= PERF_RECORD_TID
;
2076 header
.size
+= sizeof(tid_entry
);
2079 if (record_type
& PERF_RECORD_TIME
) {
2081 * Maybe do better on x86 and provide cpu_clock_nmi()
2083 time
= sched_clock();
2085 header
.type
|= PERF_RECORD_TIME
;
2086 header
.size
+= sizeof(u64
);
2089 if (record_type
& PERF_RECORD_ADDR
) {
2090 header
.type
|= PERF_RECORD_ADDR
;
2091 header
.size
+= sizeof(u64
);
2094 if (record_type
& PERF_RECORD_CONFIG
) {
2095 header
.type
|= PERF_RECORD_CONFIG
;
2096 header
.size
+= sizeof(u64
);
2099 if (record_type
& PERF_RECORD_CPU
) {
2100 header
.type
|= PERF_RECORD_CPU
;
2101 header
.size
+= sizeof(cpu_entry
);
2103 cpu_entry
.cpu
= raw_smp_processor_id();
2106 if (record_type
& PERF_RECORD_GROUP
) {
2107 header
.type
|= PERF_RECORD_GROUP
;
2108 header
.size
+= sizeof(u64
) +
2109 counter
->nr_siblings
* sizeof(group_entry
);
2112 if (record_type
& PERF_RECORD_CALLCHAIN
) {
2113 callchain
= perf_callchain(regs
);
2116 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2118 header
.type
|= PERF_RECORD_CALLCHAIN
;
2119 header
.size
+= callchain_size
;
2123 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2127 perf_output_put(&handle
, header
);
2129 if (record_type
& PERF_RECORD_IP
)
2130 perf_output_put(&handle
, ip
);
2132 if (record_type
& PERF_RECORD_TID
)
2133 perf_output_put(&handle
, tid_entry
);
2135 if (record_type
& PERF_RECORD_TIME
)
2136 perf_output_put(&handle
, time
);
2138 if (record_type
& PERF_RECORD_ADDR
)
2139 perf_output_put(&handle
, addr
);
2141 if (record_type
& PERF_RECORD_CONFIG
)
2142 perf_output_put(&handle
, counter
->hw_event
.config
);
2144 if (record_type
& PERF_RECORD_CPU
)
2145 perf_output_put(&handle
, cpu_entry
);
2148 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2150 if (record_type
& PERF_RECORD_GROUP
) {
2151 struct perf_counter
*leader
, *sub
;
2152 u64 nr
= counter
->nr_siblings
;
2154 perf_output_put(&handle
, nr
);
2156 leader
= counter
->group_leader
;
2157 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2159 sub
->pmu
->read(sub
);
2161 group_entry
.event
= sub
->hw_event
.config
;
2162 group_entry
.counter
= atomic64_read(&sub
->count
);
2164 perf_output_put(&handle
, group_entry
);
2169 perf_output_copy(&handle
, callchain
, callchain_size
);
2171 perf_output_end(&handle
);
2178 struct perf_comm_event
{
2179 struct task_struct
*task
;
2184 struct perf_event_header header
;
2191 static void perf_counter_comm_output(struct perf_counter
*counter
,
2192 struct perf_comm_event
*comm_event
)
2194 struct perf_output_handle handle
;
2195 int size
= comm_event
->event
.header
.size
;
2196 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2201 perf_output_put(&handle
, comm_event
->event
);
2202 perf_output_copy(&handle
, comm_event
->comm
,
2203 comm_event
->comm_size
);
2204 perf_output_end(&handle
);
2207 static int perf_counter_comm_match(struct perf_counter
*counter
,
2208 struct perf_comm_event
*comm_event
)
2210 if (counter
->hw_event
.comm
&&
2211 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2217 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2218 struct perf_comm_event
*comm_event
)
2220 struct perf_counter
*counter
;
2222 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2226 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2227 if (perf_counter_comm_match(counter
, comm_event
))
2228 perf_counter_comm_output(counter
, comm_event
);
2233 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2235 struct perf_cpu_context
*cpuctx
;
2237 char *comm
= comm_event
->task
->comm
;
2239 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2241 comm_event
->comm
= comm
;
2242 comm_event
->comm_size
= size
;
2244 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2246 cpuctx
= &get_cpu_var(perf_cpu_context
);
2247 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2248 put_cpu_var(perf_cpu_context
);
2250 perf_counter_comm_ctx(¤t
->perf_counter_ctx
, comm_event
);
2253 void perf_counter_comm(struct task_struct
*task
)
2255 struct perf_comm_event comm_event
;
2257 if (!atomic_read(&nr_comm_tracking
))
2260 comm_event
= (struct perf_comm_event
){
2263 .header
= { .type
= PERF_EVENT_COMM
, },
2264 .pid
= task
->group_leader
->pid
,
2269 perf_counter_comm_event(&comm_event
);
2276 struct perf_mmap_event
{
2282 struct perf_event_header header
;
2292 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2293 struct perf_mmap_event
*mmap_event
)
2295 struct perf_output_handle handle
;
2296 int size
= mmap_event
->event
.header
.size
;
2297 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2302 perf_output_put(&handle
, mmap_event
->event
);
2303 perf_output_copy(&handle
, mmap_event
->file_name
,
2304 mmap_event
->file_size
);
2305 perf_output_end(&handle
);
2308 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2309 struct perf_mmap_event
*mmap_event
)
2311 if (counter
->hw_event
.mmap
&&
2312 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2315 if (counter
->hw_event
.munmap
&&
2316 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2322 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2323 struct perf_mmap_event
*mmap_event
)
2325 struct perf_counter
*counter
;
2327 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2331 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2332 if (perf_counter_mmap_match(counter
, mmap_event
))
2333 perf_counter_mmap_output(counter
, mmap_event
);
2338 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2340 struct perf_cpu_context
*cpuctx
;
2341 struct file
*file
= mmap_event
->file
;
2348 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2350 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2353 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2355 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2359 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2364 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2366 mmap_event
->file_name
= name
;
2367 mmap_event
->file_size
= size
;
2369 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2371 cpuctx
= &get_cpu_var(perf_cpu_context
);
2372 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2373 put_cpu_var(perf_cpu_context
);
2375 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2380 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2381 unsigned long pgoff
, struct file
*file
)
2383 struct perf_mmap_event mmap_event
;
2385 if (!atomic_read(&nr_mmap_tracking
))
2388 mmap_event
= (struct perf_mmap_event
){
2391 .header
= { .type
= PERF_EVENT_MMAP
, },
2392 .pid
= current
->group_leader
->pid
,
2393 .tid
= current
->pid
,
2400 perf_counter_mmap_event(&mmap_event
);
2403 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2404 unsigned long pgoff
, struct file
*file
)
2406 struct perf_mmap_event mmap_event
;
2408 if (!atomic_read(&nr_munmap_tracking
))
2411 mmap_event
= (struct perf_mmap_event
){
2414 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2415 .pid
= current
->group_leader
->pid
,
2416 .tid
= current
->pid
,
2423 perf_counter_mmap_event(&mmap_event
);
2430 static void perf_log_period(struct perf_counter
*counter
, u64 period
)
2432 struct perf_output_handle handle
;
2436 struct perf_event_header header
;
2441 .type
= PERF_EVENT_PERIOD
,
2443 .size
= sizeof(freq_event
),
2445 .time
= sched_clock(),
2449 if (counter
->hw
.irq_period
== period
)
2452 ret
= perf_output_begin(&handle
, counter
, sizeof(freq_event
), 0, 0);
2456 perf_output_put(&handle
, freq_event
);
2457 perf_output_end(&handle
);
2461 * Generic counter overflow handling.
2464 int perf_counter_overflow(struct perf_counter
*counter
,
2465 int nmi
, struct pt_regs
*regs
, u64 addr
)
2467 int events
= atomic_read(&counter
->event_limit
);
2470 counter
->hw
.interrupts
++;
2473 * XXX event_limit might not quite work as expected on inherited
2477 counter
->pending_kill
= POLL_IN
;
2478 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2480 counter
->pending_kill
= POLL_HUP
;
2482 counter
->pending_disable
= 1;
2483 perf_pending_queue(&counter
->pending
,
2484 perf_pending_counter
);
2486 perf_counter_disable(counter
);
2489 perf_counter_output(counter
, nmi
, regs
, addr
);
2494 * Generic software counter infrastructure
2497 static void perf_swcounter_update(struct perf_counter
*counter
)
2499 struct hw_perf_counter
*hwc
= &counter
->hw
;
2504 prev
= atomic64_read(&hwc
->prev_count
);
2505 now
= atomic64_read(&hwc
->count
);
2506 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2511 atomic64_add(delta
, &counter
->count
);
2512 atomic64_sub(delta
, &hwc
->period_left
);
2515 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2517 struct hw_perf_counter
*hwc
= &counter
->hw
;
2518 s64 left
= atomic64_read(&hwc
->period_left
);
2519 s64 period
= hwc
->irq_period
;
2521 if (unlikely(left
<= -period
)) {
2523 atomic64_set(&hwc
->period_left
, left
);
2526 if (unlikely(left
<= 0)) {
2528 atomic64_add(period
, &hwc
->period_left
);
2531 atomic64_set(&hwc
->prev_count
, -left
);
2532 atomic64_set(&hwc
->count
, -left
);
2535 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2537 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2538 struct perf_counter
*counter
;
2539 struct pt_regs
*regs
;
2542 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2543 counter
->pmu
->read(counter
);
2545 regs
= get_irq_regs();
2547 * In case we exclude kernel IPs or are somehow not in interrupt
2548 * context, provide the next best thing, the user IP.
2550 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2551 !counter
->hw_event
.exclude_user
)
2552 regs
= task_pt_regs(current
);
2555 if (perf_counter_overflow(counter
, 0, regs
, 0))
2556 ret
= HRTIMER_NORESTART
;
2559 period
= max_t(u64
, 10000, counter
->hw
.irq_period
);
2560 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
2565 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2566 int nmi
, struct pt_regs
*regs
, u64 addr
)
2568 perf_swcounter_update(counter
);
2569 perf_swcounter_set_period(counter
);
2570 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2571 /* soft-disable the counter */
2576 static int perf_swcounter_match(struct perf_counter
*counter
,
2577 enum perf_event_types type
,
2578 u32 event
, struct pt_regs
*regs
)
2580 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2583 if (perf_event_raw(&counter
->hw_event
))
2586 if (perf_event_type(&counter
->hw_event
) != type
)
2589 if (perf_event_id(&counter
->hw_event
) != event
)
2592 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2595 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2601 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2602 int nmi
, struct pt_regs
*regs
, u64 addr
)
2604 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2605 if (counter
->hw
.irq_period
&& !neg
)
2606 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2609 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2610 enum perf_event_types type
, u32 event
,
2611 u64 nr
, int nmi
, struct pt_regs
*regs
,
2614 struct perf_counter
*counter
;
2616 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2620 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2621 if (perf_swcounter_match(counter
, type
, event
, regs
))
2622 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2627 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2630 return &cpuctx
->recursion
[3];
2633 return &cpuctx
->recursion
[2];
2636 return &cpuctx
->recursion
[1];
2638 return &cpuctx
->recursion
[0];
2641 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2642 u64 nr
, int nmi
, struct pt_regs
*regs
,
2645 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2646 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2654 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2655 nr
, nmi
, regs
, addr
);
2656 if (cpuctx
->task_ctx
) {
2657 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2658 nr
, nmi
, regs
, addr
);
2665 put_cpu_var(perf_cpu_context
);
2669 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2671 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2674 static void perf_swcounter_read(struct perf_counter
*counter
)
2676 perf_swcounter_update(counter
);
2679 static int perf_swcounter_enable(struct perf_counter
*counter
)
2681 perf_swcounter_set_period(counter
);
2685 static void perf_swcounter_disable(struct perf_counter
*counter
)
2687 perf_swcounter_update(counter
);
2690 static const struct pmu perf_ops_generic
= {
2691 .enable
= perf_swcounter_enable
,
2692 .disable
= perf_swcounter_disable
,
2693 .read
= perf_swcounter_read
,
2697 * Software counter: cpu wall time clock
2700 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2702 int cpu
= raw_smp_processor_id();
2706 now
= cpu_clock(cpu
);
2707 prev
= atomic64_read(&counter
->hw
.prev_count
);
2708 atomic64_set(&counter
->hw
.prev_count
, now
);
2709 atomic64_add(now
- prev
, &counter
->count
);
2712 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2714 struct hw_perf_counter
*hwc
= &counter
->hw
;
2715 int cpu
= raw_smp_processor_id();
2717 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2718 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2719 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2720 if (hwc
->irq_period
) {
2721 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
2722 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2723 ns_to_ktime(period
), 0,
2724 HRTIMER_MODE_REL
, 0);
2730 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2732 if (counter
->hw
.irq_period
)
2733 hrtimer_cancel(&counter
->hw
.hrtimer
);
2734 cpu_clock_perf_counter_update(counter
);
2737 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2739 cpu_clock_perf_counter_update(counter
);
2742 static const struct pmu perf_ops_cpu_clock
= {
2743 .enable
= cpu_clock_perf_counter_enable
,
2744 .disable
= cpu_clock_perf_counter_disable
,
2745 .read
= cpu_clock_perf_counter_read
,
2749 * Software counter: task time clock
2752 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2757 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2759 atomic64_add(delta
, &counter
->count
);
2762 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2764 struct hw_perf_counter
*hwc
= &counter
->hw
;
2767 now
= counter
->ctx
->time
;
2769 atomic64_set(&hwc
->prev_count
, now
);
2770 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2771 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2772 if (hwc
->irq_period
) {
2773 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
2774 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2775 ns_to_ktime(period
), 0,
2776 HRTIMER_MODE_REL
, 0);
2782 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2784 if (counter
->hw
.irq_period
)
2785 hrtimer_cancel(&counter
->hw
.hrtimer
);
2786 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
2790 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2795 update_context_time(counter
->ctx
);
2796 time
= counter
->ctx
->time
;
2798 u64 now
= perf_clock();
2799 u64 delta
= now
- counter
->ctx
->timestamp
;
2800 time
= counter
->ctx
->time
+ delta
;
2803 task_clock_perf_counter_update(counter
, time
);
2806 static const struct pmu perf_ops_task_clock
= {
2807 .enable
= task_clock_perf_counter_enable
,
2808 .disable
= task_clock_perf_counter_disable
,
2809 .read
= task_clock_perf_counter_read
,
2813 * Software counter: cpu migrations
2816 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2818 struct task_struct
*curr
= counter
->ctx
->task
;
2821 return curr
->se
.nr_migrations
;
2822 return cpu_nr_migrations(smp_processor_id());
2825 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2830 prev
= atomic64_read(&counter
->hw
.prev_count
);
2831 now
= get_cpu_migrations(counter
);
2833 atomic64_set(&counter
->hw
.prev_count
, now
);
2837 atomic64_add(delta
, &counter
->count
);
2840 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2842 cpu_migrations_perf_counter_update(counter
);
2845 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2847 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2848 atomic64_set(&counter
->hw
.prev_count
,
2849 get_cpu_migrations(counter
));
2853 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2855 cpu_migrations_perf_counter_update(counter
);
2858 static const struct pmu perf_ops_cpu_migrations
= {
2859 .enable
= cpu_migrations_perf_counter_enable
,
2860 .disable
= cpu_migrations_perf_counter_disable
,
2861 .read
= cpu_migrations_perf_counter_read
,
2864 #ifdef CONFIG_EVENT_PROFILE
2865 void perf_tpcounter_event(int event_id
)
2867 struct pt_regs
*regs
= get_irq_regs();
2870 regs
= task_pt_regs(current
);
2872 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
2874 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
2876 extern int ftrace_profile_enable(int);
2877 extern void ftrace_profile_disable(int);
2879 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2881 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2884 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2886 int event_id
= perf_event_id(&counter
->hw_event
);
2889 ret
= ftrace_profile_enable(event_id
);
2893 counter
->destroy
= tp_perf_counter_destroy
;
2894 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2896 return &perf_ops_generic
;
2899 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2905 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
2907 const struct pmu
*pmu
= NULL
;
2910 * Software counters (currently) can't in general distinguish
2911 * between user, kernel and hypervisor events.
2912 * However, context switches and cpu migrations are considered
2913 * to be kernel events, and page faults are never hypervisor
2916 switch (perf_event_id(&counter
->hw_event
)) {
2917 case PERF_COUNT_CPU_CLOCK
:
2918 pmu
= &perf_ops_cpu_clock
;
2921 case PERF_COUNT_TASK_CLOCK
:
2923 * If the user instantiates this as a per-cpu counter,
2924 * use the cpu_clock counter instead.
2926 if (counter
->ctx
->task
)
2927 pmu
= &perf_ops_task_clock
;
2929 pmu
= &perf_ops_cpu_clock
;
2932 case PERF_COUNT_PAGE_FAULTS
:
2933 case PERF_COUNT_PAGE_FAULTS_MIN
:
2934 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2935 case PERF_COUNT_CONTEXT_SWITCHES
:
2936 pmu
= &perf_ops_generic
;
2938 case PERF_COUNT_CPU_MIGRATIONS
:
2939 if (!counter
->hw_event
.exclude_kernel
)
2940 pmu
= &perf_ops_cpu_migrations
;
2948 * Allocate and initialize a counter structure
2950 static struct perf_counter
*
2951 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2953 struct perf_counter_context
*ctx
,
2954 struct perf_counter
*group_leader
,
2957 const struct pmu
*pmu
;
2958 struct perf_counter
*counter
;
2959 struct hw_perf_counter
*hwc
;
2962 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2964 return ERR_PTR(-ENOMEM
);
2967 * Single counters are their own group leaders, with an
2968 * empty sibling list:
2971 group_leader
= counter
;
2973 mutex_init(&counter
->mutex
);
2974 INIT_LIST_HEAD(&counter
->list_entry
);
2975 INIT_LIST_HEAD(&counter
->event_entry
);
2976 INIT_LIST_HEAD(&counter
->sibling_list
);
2977 init_waitqueue_head(&counter
->waitq
);
2979 mutex_init(&counter
->mmap_mutex
);
2981 INIT_LIST_HEAD(&counter
->child_list
);
2984 counter
->hw_event
= *hw_event
;
2985 counter
->group_leader
= group_leader
;
2986 counter
->pmu
= NULL
;
2989 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2990 if (hw_event
->disabled
)
2991 counter
->state
= PERF_COUNTER_STATE_OFF
;
2996 if (hw_event
->freq
&& hw_event
->irq_freq
)
2997 hwc
->irq_period
= div64_u64(TICK_NSEC
, hw_event
->irq_freq
);
2999 hwc
->irq_period
= hw_event
->irq_period
;
3002 * we currently do not support PERF_RECORD_GROUP on inherited counters
3004 if (hw_event
->inherit
&& (hw_event
->record_type
& PERF_RECORD_GROUP
))
3007 if (perf_event_raw(hw_event
)) {
3008 pmu
= hw_perf_counter_init(counter
);
3012 switch (perf_event_type(hw_event
)) {
3013 case PERF_TYPE_HARDWARE
:
3014 pmu
= hw_perf_counter_init(counter
);
3017 case PERF_TYPE_SOFTWARE
:
3018 pmu
= sw_perf_counter_init(counter
);
3021 case PERF_TYPE_TRACEPOINT
:
3022 pmu
= tp_perf_counter_init(counter
);
3029 else if (IS_ERR(pmu
))
3034 return ERR_PTR(err
);
3039 atomic_inc(&nr_counters
);
3040 if (counter
->hw_event
.mmap
)
3041 atomic_inc(&nr_mmap_tracking
);
3042 if (counter
->hw_event
.munmap
)
3043 atomic_inc(&nr_munmap_tracking
);
3044 if (counter
->hw_event
.comm
)
3045 atomic_inc(&nr_comm_tracking
);
3051 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3053 * @hw_event_uptr: event type attributes for monitoring/sampling
3056 * @group_fd: group leader counter fd
3058 SYSCALL_DEFINE5(perf_counter_open
,
3059 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
3060 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
3062 struct perf_counter
*counter
, *group_leader
;
3063 struct perf_counter_hw_event hw_event
;
3064 struct perf_counter_context
*ctx
;
3065 struct file
*counter_file
= NULL
;
3066 struct file
*group_file
= NULL
;
3067 int fput_needed
= 0;
3068 int fput_needed2
= 0;
3071 /* for future expandability... */
3075 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
3079 * Get the target context (task or percpu):
3081 ctx
= find_get_context(pid
, cpu
);
3083 return PTR_ERR(ctx
);
3086 * Look up the group leader (we will attach this counter to it):
3088 group_leader
= NULL
;
3089 if (group_fd
!= -1) {
3091 group_file
= fget_light(group_fd
, &fput_needed
);
3093 goto err_put_context
;
3094 if (group_file
->f_op
!= &perf_fops
)
3095 goto err_put_context
;
3097 group_leader
= group_file
->private_data
;
3099 * Do not allow a recursive hierarchy (this new sibling
3100 * becoming part of another group-sibling):
3102 if (group_leader
->group_leader
!= group_leader
)
3103 goto err_put_context
;
3105 * Do not allow to attach to a group in a different
3106 * task or CPU context:
3108 if (group_leader
->ctx
!= ctx
)
3109 goto err_put_context
;
3111 * Only a group leader can be exclusive or pinned
3113 if (hw_event
.exclusive
|| hw_event
.pinned
)
3114 goto err_put_context
;
3117 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
3119 ret
= PTR_ERR(counter
);
3120 if (IS_ERR(counter
))
3121 goto err_put_context
;
3123 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
3125 goto err_free_put_context
;
3127 counter_file
= fget_light(ret
, &fput_needed2
);
3129 goto err_free_put_context
;
3131 counter
->filp
= counter_file
;
3132 mutex_lock(&ctx
->mutex
);
3133 perf_install_in_context(ctx
, counter
, cpu
);
3134 mutex_unlock(&ctx
->mutex
);
3136 fput_light(counter_file
, fput_needed2
);
3139 fput_light(group_file
, fput_needed
);
3143 err_free_put_context
:
3153 * Initialize the perf_counter context in a task_struct:
3156 __perf_counter_init_context(struct perf_counter_context
*ctx
,
3157 struct task_struct
*task
)
3159 memset(ctx
, 0, sizeof(*ctx
));
3160 spin_lock_init(&ctx
->lock
);
3161 mutex_init(&ctx
->mutex
);
3162 INIT_LIST_HEAD(&ctx
->counter_list
);
3163 INIT_LIST_HEAD(&ctx
->event_list
);
3168 * inherit a counter from parent task to child task:
3170 static struct perf_counter
*
3171 inherit_counter(struct perf_counter
*parent_counter
,
3172 struct task_struct
*parent
,
3173 struct perf_counter_context
*parent_ctx
,
3174 struct task_struct
*child
,
3175 struct perf_counter
*group_leader
,
3176 struct perf_counter_context
*child_ctx
)
3178 struct perf_counter
*child_counter
;
3181 * Instead of creating recursive hierarchies of counters,
3182 * we link inherited counters back to the original parent,
3183 * which has a filp for sure, which we use as the reference
3186 if (parent_counter
->parent
)
3187 parent_counter
= parent_counter
->parent
;
3189 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
3190 parent_counter
->cpu
, child_ctx
,
3191 group_leader
, GFP_KERNEL
);
3192 if (IS_ERR(child_counter
))
3193 return child_counter
;
3196 * Link it up in the child's context:
3198 child_counter
->task
= child
;
3199 add_counter_to_ctx(child_counter
, child_ctx
);
3201 child_counter
->parent
= parent_counter
;
3203 * inherit into child's child as well:
3205 child_counter
->hw_event
.inherit
= 1;
3208 * Get a reference to the parent filp - we will fput it
3209 * when the child counter exits. This is safe to do because
3210 * we are in the parent and we know that the filp still
3211 * exists and has a nonzero count:
3213 atomic_long_inc(&parent_counter
->filp
->f_count
);
3216 * Link this into the parent counter's child list
3218 mutex_lock(&parent_counter
->mutex
);
3219 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3222 * Make the child state follow the state of the parent counter,
3223 * not its hw_event.disabled bit. We hold the parent's mutex,
3224 * so we won't race with perf_counter_{en,dis}able_family.
3226 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3227 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3229 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3231 mutex_unlock(&parent_counter
->mutex
);
3233 return child_counter
;
3236 static int inherit_group(struct perf_counter
*parent_counter
,
3237 struct task_struct
*parent
,
3238 struct perf_counter_context
*parent_ctx
,
3239 struct task_struct
*child
,
3240 struct perf_counter_context
*child_ctx
)
3242 struct perf_counter
*leader
;
3243 struct perf_counter
*sub
;
3244 struct perf_counter
*child_ctr
;
3246 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3247 child
, NULL
, child_ctx
);
3249 return PTR_ERR(leader
);
3250 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3251 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3252 child
, leader
, child_ctx
);
3253 if (IS_ERR(child_ctr
))
3254 return PTR_ERR(child_ctr
);
3259 static void sync_child_counter(struct perf_counter
*child_counter
,
3260 struct perf_counter
*parent_counter
)
3264 child_val
= atomic64_read(&child_counter
->count
);
3267 * Add back the child's count to the parent's count:
3269 atomic64_add(child_val
, &parent_counter
->count
);
3270 atomic64_add(child_counter
->total_time_enabled
,
3271 &parent_counter
->child_total_time_enabled
);
3272 atomic64_add(child_counter
->total_time_running
,
3273 &parent_counter
->child_total_time_running
);
3276 * Remove this counter from the parent's list
3278 mutex_lock(&parent_counter
->mutex
);
3279 list_del_init(&child_counter
->child_list
);
3280 mutex_unlock(&parent_counter
->mutex
);
3283 * Release the parent counter, if this was the last
3286 fput(parent_counter
->filp
);
3290 __perf_counter_exit_task(struct task_struct
*child
,
3291 struct perf_counter
*child_counter
,
3292 struct perf_counter_context
*child_ctx
)
3294 struct perf_counter
*parent_counter
;
3297 * If we do not self-reap then we have to wait for the
3298 * child task to unschedule (it will happen for sure),
3299 * so that its counter is at its final count. (This
3300 * condition triggers rarely - child tasks usually get
3301 * off their CPU before the parent has a chance to
3302 * get this far into the reaping action)
3304 if (child
!= current
) {
3305 wait_task_inactive(child
, 0);
3306 update_counter_times(child_counter
);
3307 list_del_counter(child_counter
, child_ctx
);
3309 struct perf_cpu_context
*cpuctx
;
3310 unsigned long flags
;
3313 * Disable and unlink this counter.
3315 * Be careful about zapping the list - IRQ/NMI context
3316 * could still be processing it:
3318 local_irq_save(flags
);
3321 cpuctx
= &__get_cpu_var(perf_cpu_context
);
3323 group_sched_out(child_counter
, cpuctx
, child_ctx
);
3324 update_counter_times(child_counter
);
3326 list_del_counter(child_counter
, child_ctx
);
3329 local_irq_restore(flags
);
3332 parent_counter
= child_counter
->parent
;
3334 * It can happen that parent exits first, and has counters
3335 * that are still around due to the child reference. These
3336 * counters need to be zapped - but otherwise linger.
3338 if (parent_counter
) {
3339 sync_child_counter(child_counter
, parent_counter
);
3340 free_counter(child_counter
);
3345 * When a child task exits, feed back counter values to parent counters.
3347 * Note: we may be running in child context, but the PID is not hashed
3348 * anymore so new counters will not be added.
3350 void perf_counter_exit_task(struct task_struct
*child
)
3352 struct perf_counter
*child_counter
, *tmp
;
3353 struct perf_counter_context
*child_ctx
;
3355 WARN_ON_ONCE(child
!= current
);
3357 child_ctx
= &child
->perf_counter_ctx
;
3359 if (likely(!child_ctx
->nr_counters
))
3363 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3365 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3368 * If the last counter was a group counter, it will have appended all
3369 * its siblings to the list, but we obtained 'tmp' before that which
3370 * will still point to the list head terminating the iteration.
3372 if (!list_empty(&child_ctx
->counter_list
))
3377 * Initialize the perf_counter context in task_struct
3379 void perf_counter_init_task(struct task_struct
*child
)
3381 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3382 struct perf_counter
*counter
;
3383 struct task_struct
*parent
= current
;
3385 child_ctx
= &child
->perf_counter_ctx
;
3386 parent_ctx
= &parent
->perf_counter_ctx
;
3388 __perf_counter_init_context(child_ctx
, child
);
3391 * This is executed from the parent task context, so inherit
3392 * counters that have been marked for cloning:
3395 if (likely(!parent_ctx
->nr_counters
))
3399 * Lock the parent list. No need to lock the child - not PID
3400 * hashed yet and not running, so nobody can access it.
3402 mutex_lock(&parent_ctx
->mutex
);
3405 * We dont have to disable NMIs - we are only looking at
3406 * the list, not manipulating it:
3408 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
3409 if (counter
!= counter
->group_leader
)
3412 if (!counter
->hw_event
.inherit
)
3415 if (inherit_group(counter
, parent
,
3416 parent_ctx
, child
, child_ctx
))
3420 mutex_unlock(&parent_ctx
->mutex
);
3423 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3425 struct perf_cpu_context
*cpuctx
;
3427 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3428 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3430 spin_lock(&perf_resource_lock
);
3431 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3432 spin_unlock(&perf_resource_lock
);
3434 hw_perf_counter_setup(cpu
);
3437 #ifdef CONFIG_HOTPLUG_CPU
3438 static void __perf_counter_exit_cpu(void *info
)
3440 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3441 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3442 struct perf_counter
*counter
, *tmp
;
3444 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3445 __perf_counter_remove_from_context(counter
);
3447 static void perf_counter_exit_cpu(int cpu
)
3449 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3450 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3452 mutex_lock(&ctx
->mutex
);
3453 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3454 mutex_unlock(&ctx
->mutex
);
3457 static inline void perf_counter_exit_cpu(int cpu
) { }
3460 static int __cpuinit
3461 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3463 unsigned int cpu
= (long)hcpu
;
3467 case CPU_UP_PREPARE
:
3468 case CPU_UP_PREPARE_FROZEN
:
3469 perf_counter_init_cpu(cpu
);
3472 case CPU_DOWN_PREPARE
:
3473 case CPU_DOWN_PREPARE_FROZEN
:
3474 perf_counter_exit_cpu(cpu
);
3484 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3485 .notifier_call
= perf_cpu_notify
,
3488 void __init
perf_counter_init(void)
3490 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3491 (void *)(long)smp_processor_id());
3492 register_cpu_notifier(&perf_cpu_nb
);
3495 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3497 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3501 perf_set_reserve_percpu(struct sysdev_class
*class,
3505 struct perf_cpu_context
*cpuctx
;
3509 err
= strict_strtoul(buf
, 10, &val
);
3512 if (val
> perf_max_counters
)
3515 spin_lock(&perf_resource_lock
);
3516 perf_reserved_percpu
= val
;
3517 for_each_online_cpu(cpu
) {
3518 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3519 spin_lock_irq(&cpuctx
->ctx
.lock
);
3520 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3521 perf_max_counters
- perf_reserved_percpu
);
3522 cpuctx
->max_pertask
= mpt
;
3523 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3525 spin_unlock(&perf_resource_lock
);
3530 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3532 return sprintf(buf
, "%d\n", perf_overcommit
);
3536 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3541 err
= strict_strtoul(buf
, 10, &val
);
3547 spin_lock(&perf_resource_lock
);
3548 perf_overcommit
= val
;
3549 spin_unlock(&perf_resource_lock
);
3554 static SYSDEV_CLASS_ATTR(
3557 perf_show_reserve_percpu
,
3558 perf_set_reserve_percpu
3561 static SYSDEV_CLASS_ATTR(
3564 perf_show_overcommit
,
3568 static struct attribute
*perfclass_attrs
[] = {
3569 &attr_reserve_percpu
.attr
,
3570 &attr_overcommit
.attr
,
3574 static struct attribute_group perfclass_attr_group
= {
3575 .attrs
= perfclass_attrs
,
3576 .name
= "perf_counters",
3579 static int __init
perf_counter_sysfs_init(void)
3581 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3582 &perfclass_attr_group
);
3584 device_initcall(perf_counter_sysfs_init
);