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
= 128; /* 'free' kb per counter */
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 u64 __weak
hw_perf_save_disable(void) { return 0; }
64 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
65 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
66 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
67 struct perf_cpu_context
*cpuctx
,
68 struct perf_counter_context
*ctx
, int cpu
)
73 void __weak
perf_counter_print_debug(void) { }
76 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
78 struct perf_counter
*group_leader
= counter
->group_leader
;
81 * Depending on whether it is a standalone or sibling counter,
82 * add it straight to the context's counter list, or to the group
83 * leader's sibling list:
85 if (group_leader
== counter
)
86 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
88 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
89 group_leader
->nr_siblings
++;
92 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
96 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
98 struct perf_counter
*sibling
, *tmp
;
100 list_del_init(&counter
->list_entry
);
101 list_del_rcu(&counter
->event_entry
);
103 if (counter
->group_leader
!= counter
)
104 counter
->group_leader
->nr_siblings
--;
107 * If this was a group counter with sibling counters then
108 * upgrade the siblings to singleton counters by adding them
109 * to the context list directly:
111 list_for_each_entry_safe(sibling
, tmp
,
112 &counter
->sibling_list
, list_entry
) {
114 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
115 sibling
->group_leader
= sibling
;
120 counter_sched_out(struct perf_counter
*counter
,
121 struct perf_cpu_context
*cpuctx
,
122 struct perf_counter_context
*ctx
)
124 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
127 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
128 counter
->tstamp_stopped
= ctx
->time
;
129 counter
->pmu
->disable(counter
);
132 if (!is_software_counter(counter
))
133 cpuctx
->active_oncpu
--;
135 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
136 cpuctx
->exclusive
= 0;
140 group_sched_out(struct perf_counter
*group_counter
,
141 struct perf_cpu_context
*cpuctx
,
142 struct perf_counter_context
*ctx
)
144 struct perf_counter
*counter
;
146 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
149 counter_sched_out(group_counter
, cpuctx
, ctx
);
152 * Schedule out siblings (if any):
154 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
155 counter_sched_out(counter
, cpuctx
, ctx
);
157 if (group_counter
->hw_event
.exclusive
)
158 cpuctx
->exclusive
= 0;
162 * Cross CPU call to remove a performance counter
164 * We disable the counter on the hardware level first. After that we
165 * remove it from the context list.
167 static void __perf_counter_remove_from_context(void *info
)
169 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
170 struct perf_counter
*counter
= info
;
171 struct perf_counter_context
*ctx
= counter
->ctx
;
176 * If this is a task context, we need to check whether it is
177 * the current task context of this cpu. If not it has been
178 * scheduled out before the smp call arrived.
180 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
183 spin_lock_irqsave(&ctx
->lock
, flags
);
185 counter_sched_out(counter
, cpuctx
, ctx
);
187 counter
->task
= NULL
;
191 * Protect the list operation against NMI by disabling the
192 * counters on a global level. NOP for non NMI based counters.
194 perf_flags
= hw_perf_save_disable();
195 list_del_counter(counter
, ctx
);
196 hw_perf_restore(perf_flags
);
200 * Allow more per task counters with respect to the
203 cpuctx
->max_pertask
=
204 min(perf_max_counters
- ctx
->nr_counters
,
205 perf_max_counters
- perf_reserved_percpu
);
208 spin_unlock_irqrestore(&ctx
->lock
, flags
);
213 * Remove the counter from a task's (or a CPU's) list of counters.
215 * Must be called with counter->mutex and ctx->mutex held.
217 * CPU counters are removed with a smp call. For task counters we only
218 * call when the task is on a CPU.
220 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
222 struct perf_counter_context
*ctx
= counter
->ctx
;
223 struct task_struct
*task
= ctx
->task
;
227 * Per cpu counters are removed via an smp call and
228 * the removal is always sucessful.
230 smp_call_function_single(counter
->cpu
,
231 __perf_counter_remove_from_context
,
237 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
240 spin_lock_irq(&ctx
->lock
);
242 * If the context is active we need to retry the smp call.
244 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
245 spin_unlock_irq(&ctx
->lock
);
250 * The lock prevents that this context is scheduled in so we
251 * can remove the counter safely, if the call above did not
254 if (!list_empty(&counter
->list_entry
)) {
256 list_del_counter(counter
, ctx
);
257 counter
->task
= NULL
;
259 spin_unlock_irq(&ctx
->lock
);
262 static inline u64
perf_clock(void)
264 return cpu_clock(smp_processor_id());
268 * Update the record of the current time in a context.
270 static void update_context_time(struct perf_counter_context
*ctx
)
272 u64 now
= perf_clock();
274 ctx
->time
+= now
- ctx
->timestamp
;
275 ctx
->timestamp
= now
;
279 * Update the total_time_enabled and total_time_running fields for a counter.
281 static void update_counter_times(struct perf_counter
*counter
)
283 struct perf_counter_context
*ctx
= counter
->ctx
;
286 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
289 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
291 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
292 run_end
= counter
->tstamp_stopped
;
296 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
300 * Update total_time_enabled and total_time_running for all counters in a group.
302 static void update_group_times(struct perf_counter
*leader
)
304 struct perf_counter
*counter
;
306 update_counter_times(leader
);
307 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
308 update_counter_times(counter
);
312 * Cross CPU call to disable a performance counter
314 static void __perf_counter_disable(void *info
)
316 struct perf_counter
*counter
= info
;
317 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
318 struct perf_counter_context
*ctx
= counter
->ctx
;
322 * If this is a per-task counter, need to check whether this
323 * counter's task is the current task on this cpu.
325 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
328 spin_lock_irqsave(&ctx
->lock
, flags
);
331 * If the counter is on, turn it off.
332 * If it is in error state, leave it in error state.
334 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
335 update_context_time(ctx
);
336 update_counter_times(counter
);
337 if (counter
== counter
->group_leader
)
338 group_sched_out(counter
, cpuctx
, ctx
);
340 counter_sched_out(counter
, cpuctx
, ctx
);
341 counter
->state
= PERF_COUNTER_STATE_OFF
;
344 spin_unlock_irqrestore(&ctx
->lock
, flags
);
350 static void perf_counter_disable(struct perf_counter
*counter
)
352 struct perf_counter_context
*ctx
= counter
->ctx
;
353 struct task_struct
*task
= ctx
->task
;
357 * Disable the counter on the cpu that it's on
359 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
365 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
367 spin_lock_irq(&ctx
->lock
);
369 * If the counter is still active, we need to retry the cross-call.
371 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
372 spin_unlock_irq(&ctx
->lock
);
377 * Since we have the lock this context can't be scheduled
378 * in, so we can change the state safely.
380 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
381 update_counter_times(counter
);
382 counter
->state
= PERF_COUNTER_STATE_OFF
;
385 spin_unlock_irq(&ctx
->lock
);
389 counter_sched_in(struct perf_counter
*counter
,
390 struct perf_cpu_context
*cpuctx
,
391 struct perf_counter_context
*ctx
,
394 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
397 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
398 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
400 * The new state must be visible before we turn it on in the hardware:
404 if (counter
->pmu
->enable(counter
)) {
405 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
410 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
412 if (!is_software_counter(counter
))
413 cpuctx
->active_oncpu
++;
416 if (counter
->hw_event
.exclusive
)
417 cpuctx
->exclusive
= 1;
423 group_sched_in(struct perf_counter
*group_counter
,
424 struct perf_cpu_context
*cpuctx
,
425 struct perf_counter_context
*ctx
,
428 struct perf_counter
*counter
, *partial_group
;
431 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
434 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
436 return ret
< 0 ? ret
: 0;
438 group_counter
->prev_state
= group_counter
->state
;
439 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
443 * Schedule in siblings as one group (if any):
445 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
446 counter
->prev_state
= counter
->state
;
447 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
448 partial_group
= counter
;
457 * Groups can be scheduled in as one unit only, so undo any
458 * partial group before returning:
460 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
461 if (counter
== partial_group
)
463 counter_sched_out(counter
, cpuctx
, ctx
);
465 counter_sched_out(group_counter
, cpuctx
, ctx
);
471 * Return 1 for a group consisting entirely of software counters,
472 * 0 if the group contains any hardware counters.
474 static int is_software_only_group(struct perf_counter
*leader
)
476 struct perf_counter
*counter
;
478 if (!is_software_counter(leader
))
481 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
482 if (!is_software_counter(counter
))
489 * Work out whether we can put this counter group on the CPU now.
491 static int group_can_go_on(struct perf_counter
*counter
,
492 struct perf_cpu_context
*cpuctx
,
496 * Groups consisting entirely of software counters can always go on.
498 if (is_software_only_group(counter
))
501 * If an exclusive group is already on, no other hardware
502 * counters can go on.
504 if (cpuctx
->exclusive
)
507 * If this group is exclusive and there are already
508 * counters on the CPU, it can't go on.
510 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
513 * Otherwise, try to add it if all previous groups were able
519 static void add_counter_to_ctx(struct perf_counter
*counter
,
520 struct perf_counter_context
*ctx
)
522 list_add_counter(counter
, ctx
);
524 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
525 counter
->tstamp_enabled
= ctx
->time
;
526 counter
->tstamp_running
= ctx
->time
;
527 counter
->tstamp_stopped
= ctx
->time
;
531 * Cross CPU call to install and enable a performance counter
533 static void __perf_install_in_context(void *info
)
535 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
536 struct perf_counter
*counter
= info
;
537 struct perf_counter_context
*ctx
= counter
->ctx
;
538 struct perf_counter
*leader
= counter
->group_leader
;
539 int cpu
= smp_processor_id();
545 * If this is a task context, we need to check whether it is
546 * the current task context of this cpu. If not it has been
547 * scheduled out before the smp call arrived.
549 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
552 spin_lock_irqsave(&ctx
->lock
, flags
);
553 update_context_time(ctx
);
556 * Protect the list operation against NMI by disabling the
557 * counters on a global level. NOP for non NMI based counters.
559 perf_flags
= hw_perf_save_disable();
561 add_counter_to_ctx(counter
, ctx
);
564 * Don't put the counter on if it is disabled or if
565 * it is in a group and the group isn't on.
567 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
568 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
572 * An exclusive counter can't go on if there are already active
573 * hardware counters, and no hardware counter can go on if there
574 * is already an exclusive counter on.
576 if (!group_can_go_on(counter
, cpuctx
, 1))
579 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
583 * This counter couldn't go on. If it is in a group
584 * then we have to pull the whole group off.
585 * If the counter group is pinned then put it in error state.
587 if (leader
!= counter
)
588 group_sched_out(leader
, cpuctx
, ctx
);
589 if (leader
->hw_event
.pinned
) {
590 update_group_times(leader
);
591 leader
->state
= PERF_COUNTER_STATE_ERROR
;
595 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
596 cpuctx
->max_pertask
--;
599 hw_perf_restore(perf_flags
);
601 spin_unlock_irqrestore(&ctx
->lock
, flags
);
605 * Attach a performance counter to a context
607 * First we add the counter to the list with the hardware enable bit
608 * in counter->hw_config cleared.
610 * If the counter is attached to a task which is on a CPU we use a smp
611 * call to enable it in the task context. The task might have been
612 * scheduled away, but we check this in the smp call again.
614 * Must be called with ctx->mutex held.
617 perf_install_in_context(struct perf_counter_context
*ctx
,
618 struct perf_counter
*counter
,
621 struct task_struct
*task
= ctx
->task
;
625 * Per cpu counters are installed via an smp call and
626 * the install is always sucessful.
628 smp_call_function_single(cpu
, __perf_install_in_context
,
633 counter
->task
= task
;
635 task_oncpu_function_call(task
, __perf_install_in_context
,
638 spin_lock_irq(&ctx
->lock
);
640 * we need to retry the smp call.
642 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
643 spin_unlock_irq(&ctx
->lock
);
648 * The lock prevents that this context is scheduled in so we
649 * can add the counter safely, if it the call above did not
652 if (list_empty(&counter
->list_entry
))
653 add_counter_to_ctx(counter
, ctx
);
654 spin_unlock_irq(&ctx
->lock
);
658 * Cross CPU call to enable a performance counter
660 static void __perf_counter_enable(void *info
)
662 struct perf_counter
*counter
= info
;
663 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
664 struct perf_counter_context
*ctx
= counter
->ctx
;
665 struct perf_counter
*leader
= counter
->group_leader
;
670 * If this is a per-task counter, need to check whether this
671 * counter's task is the current task on this cpu.
673 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
676 spin_lock_irqsave(&ctx
->lock
, flags
);
677 update_context_time(ctx
);
679 counter
->prev_state
= counter
->state
;
680 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
682 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
683 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
686 * If the counter is in a group and isn't the group leader,
687 * then don't put it on unless the group is on.
689 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
692 if (!group_can_go_on(counter
, cpuctx
, 1))
694 else if (counter
== leader
)
695 err
= group_sched_in(counter
, cpuctx
, ctx
,
698 err
= counter_sched_in(counter
, cpuctx
, ctx
,
703 * If this counter can't go on and it's part of a
704 * group, then the whole group has to come off.
706 if (leader
!= counter
)
707 group_sched_out(leader
, cpuctx
, ctx
);
708 if (leader
->hw_event
.pinned
) {
709 update_group_times(leader
);
710 leader
->state
= PERF_COUNTER_STATE_ERROR
;
715 spin_unlock_irqrestore(&ctx
->lock
, flags
);
721 static void perf_counter_enable(struct perf_counter
*counter
)
723 struct perf_counter_context
*ctx
= counter
->ctx
;
724 struct task_struct
*task
= ctx
->task
;
728 * Enable the counter on the cpu that it's on
730 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
735 spin_lock_irq(&ctx
->lock
);
736 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
740 * If the counter is in error state, clear that first.
741 * That way, if we see the counter in error state below, we
742 * know that it has gone back into error state, as distinct
743 * from the task having been scheduled away before the
744 * cross-call arrived.
746 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
747 counter
->state
= PERF_COUNTER_STATE_OFF
;
750 spin_unlock_irq(&ctx
->lock
);
751 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
753 spin_lock_irq(&ctx
->lock
);
756 * If the context is active and the counter is still off,
757 * we need to retry the cross-call.
759 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
763 * Since we have the lock this context can't be scheduled
764 * in, so we can change the state safely.
766 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
767 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
768 counter
->tstamp_enabled
=
769 ctx
->time
- counter
->total_time_enabled
;
772 spin_unlock_irq(&ctx
->lock
);
775 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
778 * not supported on inherited counters
780 if (counter
->hw_event
.inherit
)
783 atomic_add(refresh
, &counter
->event_limit
);
784 perf_counter_enable(counter
);
789 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
790 struct perf_cpu_context
*cpuctx
)
792 struct perf_counter
*counter
;
795 spin_lock(&ctx
->lock
);
797 if (likely(!ctx
->nr_counters
))
799 update_context_time(ctx
);
801 flags
= hw_perf_save_disable();
802 if (ctx
->nr_active
) {
803 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
804 group_sched_out(counter
, cpuctx
, ctx
);
806 hw_perf_restore(flags
);
808 spin_unlock(&ctx
->lock
);
812 * Called from scheduler to remove the counters of the current task,
813 * with interrupts disabled.
815 * We stop each counter and update the counter value in counter->count.
817 * This does not protect us against NMI, but disable()
818 * sets the disabled bit in the control field of counter _before_
819 * accessing the counter control register. If a NMI hits, then it will
820 * not restart the counter.
822 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
824 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
825 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
826 struct pt_regs
*regs
;
828 if (likely(!cpuctx
->task_ctx
))
831 update_context_time(ctx
);
833 regs
= task_pt_regs(task
);
834 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
835 __perf_counter_sched_out(ctx
, cpuctx
);
837 cpuctx
->task_ctx
= NULL
;
840 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
842 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
846 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
847 struct perf_cpu_context
*cpuctx
, int cpu
)
849 struct perf_counter
*counter
;
853 spin_lock(&ctx
->lock
);
855 if (likely(!ctx
->nr_counters
))
858 ctx
->timestamp
= perf_clock();
860 flags
= hw_perf_save_disable();
863 * First go through the list and put on any pinned groups
864 * in order to give them the best chance of going on.
866 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
867 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
868 !counter
->hw_event
.pinned
)
870 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
873 if (group_can_go_on(counter
, cpuctx
, 1))
874 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
877 * If this pinned group hasn't been scheduled,
878 * put it in error state.
880 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
881 update_group_times(counter
);
882 counter
->state
= PERF_COUNTER_STATE_ERROR
;
886 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
888 * Ignore counters in OFF or ERROR state, and
889 * ignore pinned counters since we did them already.
891 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
892 counter
->hw_event
.pinned
)
896 * Listen to the 'cpu' scheduling filter constraint
899 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
902 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
903 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
907 hw_perf_restore(flags
);
909 spin_unlock(&ctx
->lock
);
913 * Called from scheduler to add the counters of the current task
914 * with interrupts disabled.
916 * We restore the counter value and then enable it.
918 * This does not protect us against NMI, but enable()
919 * sets the enabled bit in the control field of counter _before_
920 * accessing the counter control register. If a NMI hits, then it will
921 * keep the counter running.
923 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
925 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
926 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
928 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
929 cpuctx
->task_ctx
= ctx
;
932 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
934 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
936 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
939 int perf_counter_task_disable(void)
941 struct task_struct
*curr
= current
;
942 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
943 struct perf_counter
*counter
;
948 if (likely(!ctx
->nr_counters
))
951 local_irq_save(flags
);
952 cpu
= smp_processor_id();
954 perf_counter_task_sched_out(curr
, cpu
);
956 spin_lock(&ctx
->lock
);
959 * Disable all the counters:
961 perf_flags
= hw_perf_save_disable();
963 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
964 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
965 update_group_times(counter
);
966 counter
->state
= PERF_COUNTER_STATE_OFF
;
970 hw_perf_restore(perf_flags
);
972 spin_unlock_irqrestore(&ctx
->lock
, flags
);
977 int perf_counter_task_enable(void)
979 struct task_struct
*curr
= current
;
980 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
981 struct perf_counter
*counter
;
986 if (likely(!ctx
->nr_counters
))
989 local_irq_save(flags
);
990 cpu
= smp_processor_id();
992 perf_counter_task_sched_out(curr
, cpu
);
994 spin_lock(&ctx
->lock
);
997 * Disable all the counters:
999 perf_flags
= hw_perf_save_disable();
1001 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1002 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1004 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1005 counter
->tstamp_enabled
=
1006 ctx
->time
- counter
->total_time_enabled
;
1007 counter
->hw_event
.disabled
= 0;
1009 hw_perf_restore(perf_flags
);
1011 spin_unlock(&ctx
->lock
);
1013 perf_counter_task_sched_in(curr
, cpu
);
1015 local_irq_restore(flags
);
1021 * Round-robin a context's counters:
1023 static void rotate_ctx(struct perf_counter_context
*ctx
)
1025 struct perf_counter
*counter
;
1028 if (!ctx
->nr_counters
)
1031 spin_lock(&ctx
->lock
);
1033 * Rotate the first entry last (works just fine for group counters too):
1035 perf_flags
= hw_perf_save_disable();
1036 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1037 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1040 hw_perf_restore(perf_flags
);
1042 spin_unlock(&ctx
->lock
);
1045 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1047 struct perf_cpu_context
*cpuctx
;
1048 struct perf_counter_context
*ctx
;
1050 if (!atomic_read(&nr_counters
))
1053 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1054 ctx
= &curr
->perf_counter_ctx
;
1056 perf_counter_cpu_sched_out(cpuctx
);
1057 perf_counter_task_sched_out(curr
, cpu
);
1059 rotate_ctx(&cpuctx
->ctx
);
1062 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1063 perf_counter_task_sched_in(curr
, cpu
);
1067 * Cross CPU call to read the hardware counter
1069 static void __read(void *info
)
1071 struct perf_counter
*counter
= info
;
1072 struct perf_counter_context
*ctx
= counter
->ctx
;
1073 unsigned long flags
;
1075 local_irq_save(flags
);
1077 update_context_time(ctx
);
1078 counter
->pmu
->read(counter
);
1079 update_counter_times(counter
);
1080 local_irq_restore(flags
);
1083 static u64
perf_counter_read(struct perf_counter
*counter
)
1086 * If counter is enabled and currently active on a CPU, update the
1087 * value in the counter structure:
1089 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1090 smp_call_function_single(counter
->oncpu
,
1091 __read
, counter
, 1);
1092 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1093 update_counter_times(counter
);
1096 return atomic64_read(&counter
->count
);
1099 static void put_context(struct perf_counter_context
*ctx
)
1102 put_task_struct(ctx
->task
);
1105 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1107 struct perf_cpu_context
*cpuctx
;
1108 struct perf_counter_context
*ctx
;
1109 struct task_struct
*task
;
1112 * If cpu is not a wildcard then this is a percpu counter:
1115 /* Must be root to operate on a CPU counter: */
1116 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1117 return ERR_PTR(-EACCES
);
1119 if (cpu
< 0 || cpu
> num_possible_cpus())
1120 return ERR_PTR(-EINVAL
);
1123 * We could be clever and allow to attach a counter to an
1124 * offline CPU and activate it when the CPU comes up, but
1127 if (!cpu_isset(cpu
, cpu_online_map
))
1128 return ERR_PTR(-ENODEV
);
1130 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1140 task
= find_task_by_vpid(pid
);
1142 get_task_struct(task
);
1146 return ERR_PTR(-ESRCH
);
1148 ctx
= &task
->perf_counter_ctx
;
1151 /* Reuse ptrace permission checks for now. */
1152 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1154 return ERR_PTR(-EACCES
);
1160 static void free_counter_rcu(struct rcu_head
*head
)
1162 struct perf_counter
*counter
;
1164 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1168 static void perf_pending_sync(struct perf_counter
*counter
);
1170 static void free_counter(struct perf_counter
*counter
)
1172 perf_pending_sync(counter
);
1174 atomic_dec(&nr_counters
);
1175 if (counter
->hw_event
.mmap
)
1176 atomic_dec(&nr_mmap_tracking
);
1177 if (counter
->hw_event
.munmap
)
1178 atomic_dec(&nr_munmap_tracking
);
1179 if (counter
->hw_event
.comm
)
1180 atomic_dec(&nr_comm_tracking
);
1182 if (counter
->destroy
)
1183 counter
->destroy(counter
);
1185 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1189 * Called when the last reference to the file is gone.
1191 static int perf_release(struct inode
*inode
, struct file
*file
)
1193 struct perf_counter
*counter
= file
->private_data
;
1194 struct perf_counter_context
*ctx
= counter
->ctx
;
1196 file
->private_data
= NULL
;
1198 mutex_lock(&ctx
->mutex
);
1199 mutex_lock(&counter
->mutex
);
1201 perf_counter_remove_from_context(counter
);
1203 mutex_unlock(&counter
->mutex
);
1204 mutex_unlock(&ctx
->mutex
);
1206 free_counter(counter
);
1213 * Read the performance counter - simple non blocking version for now
1216 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1222 * Return end-of-file for a read on a counter that is in
1223 * error state (i.e. because it was pinned but it couldn't be
1224 * scheduled on to the CPU at some point).
1226 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1229 mutex_lock(&counter
->mutex
);
1230 values
[0] = perf_counter_read(counter
);
1232 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1233 values
[n
++] = counter
->total_time_enabled
+
1234 atomic64_read(&counter
->child_total_time_enabled
);
1235 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1236 values
[n
++] = counter
->total_time_running
+
1237 atomic64_read(&counter
->child_total_time_running
);
1238 mutex_unlock(&counter
->mutex
);
1240 if (count
< n
* sizeof(u64
))
1242 count
= n
* sizeof(u64
);
1244 if (copy_to_user(buf
, values
, count
))
1251 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1253 struct perf_counter
*counter
= file
->private_data
;
1255 return perf_read_hw(counter
, buf
, count
);
1258 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1260 struct perf_counter
*counter
= file
->private_data
;
1261 struct perf_mmap_data
*data
;
1262 unsigned int events
= POLL_HUP
;
1265 data
= rcu_dereference(counter
->data
);
1267 events
= atomic_xchg(&data
->poll
, 0);
1270 poll_wait(file
, &counter
->waitq
, wait
);
1275 static void perf_counter_reset(struct perf_counter
*counter
)
1277 (void)perf_counter_read(counter
);
1278 atomic_set(&counter
->count
, 0);
1279 perf_counter_update_userpage(counter
);
1282 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1283 void (*func
)(struct perf_counter
*))
1285 struct perf_counter_context
*ctx
= counter
->ctx
;
1286 struct perf_counter
*sibling
;
1288 spin_lock_irq(&ctx
->lock
);
1289 counter
= counter
->group_leader
;
1292 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1294 spin_unlock_irq(&ctx
->lock
);
1297 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1298 void (*func
)(struct perf_counter
*))
1300 struct perf_counter
*child
;
1302 mutex_lock(&counter
->mutex
);
1304 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1306 mutex_unlock(&counter
->mutex
);
1309 static void perf_counter_for_each(struct perf_counter
*counter
,
1310 void (*func
)(struct perf_counter
*))
1312 struct perf_counter
*child
;
1314 mutex_lock(&counter
->mutex
);
1315 perf_counter_for_each_sibling(counter
, func
);
1316 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1317 perf_counter_for_each_sibling(child
, func
);
1318 mutex_unlock(&counter
->mutex
);
1321 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1323 struct perf_counter
*counter
= file
->private_data
;
1324 void (*func
)(struct perf_counter
*);
1328 case PERF_COUNTER_IOC_ENABLE
:
1329 func
= perf_counter_enable
;
1331 case PERF_COUNTER_IOC_DISABLE
:
1332 func
= perf_counter_disable
;
1334 case PERF_COUNTER_IOC_RESET
:
1335 func
= perf_counter_reset
;
1338 case PERF_COUNTER_IOC_REFRESH
:
1339 return perf_counter_refresh(counter
, arg
);
1344 if (flags
& PERF_IOC_FLAG_GROUP
)
1345 perf_counter_for_each(counter
, func
);
1347 perf_counter_for_each_child(counter
, func
);
1353 * Callers need to ensure there can be no nesting of this function, otherwise
1354 * the seqlock logic goes bad. We can not serialize this because the arch
1355 * code calls this from NMI context.
1357 void perf_counter_update_userpage(struct perf_counter
*counter
)
1359 struct perf_mmap_data
*data
;
1360 struct perf_counter_mmap_page
*userpg
;
1363 data
= rcu_dereference(counter
->data
);
1367 userpg
= data
->user_page
;
1370 * Disable preemption so as to not let the corresponding user-space
1371 * spin too long if we get preempted.
1376 userpg
->index
= counter
->hw
.idx
;
1377 userpg
->offset
= atomic64_read(&counter
->count
);
1378 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1379 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1388 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1390 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1391 struct perf_mmap_data
*data
;
1392 int ret
= VM_FAULT_SIGBUS
;
1395 data
= rcu_dereference(counter
->data
);
1399 if (vmf
->pgoff
== 0) {
1400 vmf
->page
= virt_to_page(data
->user_page
);
1402 int nr
= vmf
->pgoff
- 1;
1404 if ((unsigned)nr
> data
->nr_pages
)
1407 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1409 get_page(vmf
->page
);
1417 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1419 struct perf_mmap_data
*data
;
1423 WARN_ON(atomic_read(&counter
->mmap_count
));
1425 size
= sizeof(struct perf_mmap_data
);
1426 size
+= nr_pages
* sizeof(void *);
1428 data
= kzalloc(size
, GFP_KERNEL
);
1432 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1433 if (!data
->user_page
)
1434 goto fail_user_page
;
1436 for (i
= 0; i
< nr_pages
; i
++) {
1437 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1438 if (!data
->data_pages
[i
])
1439 goto fail_data_pages
;
1442 data
->nr_pages
= nr_pages
;
1443 atomic_set(&data
->lock
, -1);
1445 rcu_assign_pointer(counter
->data
, data
);
1450 for (i
--; i
>= 0; i
--)
1451 free_page((unsigned long)data
->data_pages
[i
]);
1453 free_page((unsigned long)data
->user_page
);
1462 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1464 struct perf_mmap_data
*data
= container_of(rcu_head
,
1465 struct perf_mmap_data
, rcu_head
);
1468 free_page((unsigned long)data
->user_page
);
1469 for (i
= 0; i
< data
->nr_pages
; i
++)
1470 free_page((unsigned long)data
->data_pages
[i
]);
1474 static void perf_mmap_data_free(struct perf_counter
*counter
)
1476 struct perf_mmap_data
*data
= counter
->data
;
1478 WARN_ON(atomic_read(&counter
->mmap_count
));
1480 rcu_assign_pointer(counter
->data
, NULL
);
1481 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1484 static void perf_mmap_open(struct vm_area_struct
*vma
)
1486 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1488 atomic_inc(&counter
->mmap_count
);
1491 static void perf_mmap_close(struct vm_area_struct
*vma
)
1493 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1495 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1496 &counter
->mmap_mutex
)) {
1497 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1498 perf_mmap_data_free(counter
);
1499 mutex_unlock(&counter
->mmap_mutex
);
1503 static struct vm_operations_struct perf_mmap_vmops
= {
1504 .open
= perf_mmap_open
,
1505 .close
= perf_mmap_close
,
1506 .fault
= perf_mmap_fault
,
1509 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1511 struct perf_counter
*counter
= file
->private_data
;
1512 unsigned long vma_size
;
1513 unsigned long nr_pages
;
1514 unsigned long locked
, lock_limit
;
1518 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1521 vma_size
= vma
->vm_end
- vma
->vm_start
;
1522 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1525 * If we have data pages ensure they're a power-of-two number, so we
1526 * can do bitmasks instead of modulo.
1528 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1531 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1534 if (vma
->vm_pgoff
!= 0)
1537 mutex_lock(&counter
->mmap_mutex
);
1538 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1539 if (nr_pages
!= counter
->data
->nr_pages
)
1544 extra
= nr_pages
/* + 1 only account the data pages */;
1545 extra
-= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1549 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1551 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1552 lock_limit
>>= PAGE_SHIFT
;
1554 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1559 WARN_ON(counter
->data
);
1560 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1564 atomic_set(&counter
->mmap_count
, 1);
1565 vma
->vm_mm
->locked_vm
+= extra
;
1566 counter
->data
->nr_locked
= extra
;
1568 mutex_unlock(&counter
->mmap_mutex
);
1570 vma
->vm_flags
&= ~VM_MAYWRITE
;
1571 vma
->vm_flags
|= VM_RESERVED
;
1572 vma
->vm_ops
= &perf_mmap_vmops
;
1577 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1579 struct perf_counter
*counter
= filp
->private_data
;
1580 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1583 mutex_lock(&inode
->i_mutex
);
1584 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1585 mutex_unlock(&inode
->i_mutex
);
1593 static const struct file_operations perf_fops
= {
1594 .release
= perf_release
,
1597 .unlocked_ioctl
= perf_ioctl
,
1598 .compat_ioctl
= perf_ioctl
,
1600 .fasync
= perf_fasync
,
1604 * Perf counter wakeup
1606 * If there's data, ensure we set the poll() state and publish everything
1607 * to user-space before waking everybody up.
1610 void perf_counter_wakeup(struct perf_counter
*counter
)
1612 wake_up_all(&counter
->waitq
);
1614 if (counter
->pending_kill
) {
1615 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1616 counter
->pending_kill
= 0;
1623 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1625 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1626 * single linked list and use cmpxchg() to add entries lockless.
1629 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1631 struct perf_counter
*counter
= container_of(entry
,
1632 struct perf_counter
, pending
);
1634 if (counter
->pending_disable
) {
1635 counter
->pending_disable
= 0;
1636 perf_counter_disable(counter
);
1639 if (counter
->pending_wakeup
) {
1640 counter
->pending_wakeup
= 0;
1641 perf_counter_wakeup(counter
);
1645 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1647 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1651 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1652 void (*func
)(struct perf_pending_entry
*))
1654 struct perf_pending_entry
**head
;
1656 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1661 head
= &get_cpu_var(perf_pending_head
);
1664 entry
->next
= *head
;
1665 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1667 set_perf_counter_pending();
1669 put_cpu_var(perf_pending_head
);
1672 static int __perf_pending_run(void)
1674 struct perf_pending_entry
*list
;
1677 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1678 while (list
!= PENDING_TAIL
) {
1679 void (*func
)(struct perf_pending_entry
*);
1680 struct perf_pending_entry
*entry
= list
;
1687 * Ensure we observe the unqueue before we issue the wakeup,
1688 * so that we won't be waiting forever.
1689 * -- see perf_not_pending().
1700 static inline int perf_not_pending(struct perf_counter
*counter
)
1703 * If we flush on whatever cpu we run, there is a chance we don't
1707 __perf_pending_run();
1711 * Ensure we see the proper queue state before going to sleep
1712 * so that we do not miss the wakeup. -- see perf_pending_handle()
1715 return counter
->pending
.next
== NULL
;
1718 static void perf_pending_sync(struct perf_counter
*counter
)
1720 wait_event(counter
->waitq
, perf_not_pending(counter
));
1723 void perf_counter_do_pending(void)
1725 __perf_pending_run();
1729 * Callchain support -- arch specific
1732 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1741 struct perf_output_handle
{
1742 struct perf_counter
*counter
;
1743 struct perf_mmap_data
*data
;
1744 unsigned int offset
;
1749 unsigned long flags
;
1752 static void perf_output_wakeup(struct perf_output_handle
*handle
)
1754 atomic_set(&handle
->data
->poll
, POLL_IN
);
1757 handle
->counter
->pending_wakeup
= 1;
1758 perf_pending_queue(&handle
->counter
->pending
,
1759 perf_pending_counter
);
1761 perf_counter_wakeup(handle
->counter
);
1765 * Curious locking construct.
1767 * We need to ensure a later event doesn't publish a head when a former
1768 * event isn't done writing. However since we need to deal with NMIs we
1769 * cannot fully serialize things.
1771 * What we do is serialize between CPUs so we only have to deal with NMI
1772 * nesting on a single CPU.
1774 * We only publish the head (and generate a wakeup) when the outer-most
1777 static void perf_output_lock(struct perf_output_handle
*handle
)
1779 struct perf_mmap_data
*data
= handle
->data
;
1784 local_irq_save(handle
->flags
);
1785 cpu
= smp_processor_id();
1787 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
1790 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1796 static void perf_output_unlock(struct perf_output_handle
*handle
)
1798 struct perf_mmap_data
*data
= handle
->data
;
1801 data
->done_head
= data
->head
;
1803 if (!handle
->locked
)
1808 * The xchg implies a full barrier that ensures all writes are done
1809 * before we publish the new head, matched by a rmb() in userspace when
1810 * reading this position.
1812 while ((head
= atomic_xchg(&data
->done_head
, 0)))
1813 data
->user_page
->data_head
= head
;
1816 * NMI can happen here, which means we can miss a done_head update.
1819 cpu
= atomic_xchg(&data
->lock
, -1);
1820 WARN_ON_ONCE(cpu
!= smp_processor_id());
1823 * Therefore we have to validate we did not indeed do so.
1825 if (unlikely(atomic_read(&data
->done_head
))) {
1827 * Since we had it locked, we can lock it again.
1829 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1835 if (atomic_xchg(&data
->wakeup
, 0))
1836 perf_output_wakeup(handle
);
1838 local_irq_restore(handle
->flags
);
1841 static int perf_output_begin(struct perf_output_handle
*handle
,
1842 struct perf_counter
*counter
, unsigned int size
,
1843 int nmi
, int overflow
)
1845 struct perf_mmap_data
*data
;
1846 unsigned int offset
, head
;
1849 * For inherited counters we send all the output towards the parent.
1851 if (counter
->parent
)
1852 counter
= counter
->parent
;
1855 data
= rcu_dereference(counter
->data
);
1859 handle
->data
= data
;
1860 handle
->counter
= counter
;
1862 handle
->overflow
= overflow
;
1864 if (!data
->nr_pages
)
1867 perf_output_lock(handle
);
1870 offset
= head
= atomic_read(&data
->head
);
1872 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1874 handle
->offset
= offset
;
1875 handle
->head
= head
;
1877 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
1878 atomic_set(&data
->wakeup
, 1);
1883 perf_output_wakeup(handle
);
1890 static void perf_output_copy(struct perf_output_handle
*handle
,
1891 void *buf
, unsigned int len
)
1893 unsigned int pages_mask
;
1894 unsigned int offset
;
1898 offset
= handle
->offset
;
1899 pages_mask
= handle
->data
->nr_pages
- 1;
1900 pages
= handle
->data
->data_pages
;
1903 unsigned int page_offset
;
1906 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1907 page_offset
= offset
& (PAGE_SIZE
- 1);
1908 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1910 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1917 handle
->offset
= offset
;
1919 WARN_ON_ONCE(handle
->offset
> handle
->head
);
1922 #define perf_output_put(handle, x) \
1923 perf_output_copy((handle), &(x), sizeof(x))
1925 static void perf_output_end(struct perf_output_handle
*handle
)
1927 struct perf_counter
*counter
= handle
->counter
;
1928 struct perf_mmap_data
*data
= handle
->data
;
1930 int wakeup_events
= counter
->hw_event
.wakeup_events
;
1932 if (handle
->overflow
&& wakeup_events
) {
1933 int events
= atomic_inc_return(&data
->events
);
1934 if (events
>= wakeup_events
) {
1935 atomic_sub(wakeup_events
, &data
->events
);
1936 atomic_set(&data
->wakeup
, 1);
1940 perf_output_unlock(handle
);
1944 static void perf_counter_output(struct perf_counter
*counter
,
1945 int nmi
, struct pt_regs
*regs
, u64 addr
)
1948 u64 record_type
= counter
->hw_event
.record_type
;
1949 struct perf_output_handle handle
;
1950 struct perf_event_header header
;
1959 struct perf_callchain_entry
*callchain
= NULL
;
1960 int callchain_size
= 0;
1967 header
.size
= sizeof(header
);
1969 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
1970 header
.misc
|= user_mode(regs
) ?
1971 PERF_EVENT_MISC_USER
: PERF_EVENT_MISC_KERNEL
;
1973 if (record_type
& PERF_RECORD_IP
) {
1974 ip
= instruction_pointer(regs
);
1975 header
.type
|= PERF_RECORD_IP
;
1976 header
.size
+= sizeof(ip
);
1979 if (record_type
& PERF_RECORD_TID
) {
1980 /* namespace issues */
1981 tid_entry
.pid
= current
->group_leader
->pid
;
1982 tid_entry
.tid
= current
->pid
;
1984 header
.type
|= PERF_RECORD_TID
;
1985 header
.size
+= sizeof(tid_entry
);
1988 if (record_type
& PERF_RECORD_TIME
) {
1990 * Maybe do better on x86 and provide cpu_clock_nmi()
1992 time
= sched_clock();
1994 header
.type
|= PERF_RECORD_TIME
;
1995 header
.size
+= sizeof(u64
);
1998 if (record_type
& PERF_RECORD_ADDR
) {
1999 header
.type
|= PERF_RECORD_ADDR
;
2000 header
.size
+= sizeof(u64
);
2003 if (record_type
& PERF_RECORD_CONFIG
) {
2004 header
.type
|= PERF_RECORD_CONFIG
;
2005 header
.size
+= sizeof(u64
);
2008 if (record_type
& PERF_RECORD_CPU
) {
2009 header
.type
|= PERF_RECORD_CPU
;
2010 header
.size
+= sizeof(cpu_entry
);
2012 cpu_entry
.cpu
= raw_smp_processor_id();
2015 if (record_type
& PERF_RECORD_GROUP
) {
2016 header
.type
|= PERF_RECORD_GROUP
;
2017 header
.size
+= sizeof(u64
) +
2018 counter
->nr_siblings
* sizeof(group_entry
);
2021 if (record_type
& PERF_RECORD_CALLCHAIN
) {
2022 callchain
= perf_callchain(regs
);
2025 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2027 header
.type
|= PERF_RECORD_CALLCHAIN
;
2028 header
.size
+= callchain_size
;
2032 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2036 perf_output_put(&handle
, header
);
2038 if (record_type
& PERF_RECORD_IP
)
2039 perf_output_put(&handle
, ip
);
2041 if (record_type
& PERF_RECORD_TID
)
2042 perf_output_put(&handle
, tid_entry
);
2044 if (record_type
& PERF_RECORD_TIME
)
2045 perf_output_put(&handle
, time
);
2047 if (record_type
& PERF_RECORD_ADDR
)
2048 perf_output_put(&handle
, addr
);
2050 if (record_type
& PERF_RECORD_CONFIG
)
2051 perf_output_put(&handle
, counter
->hw_event
.config
);
2053 if (record_type
& PERF_RECORD_CPU
)
2054 perf_output_put(&handle
, cpu_entry
);
2057 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2059 if (record_type
& PERF_RECORD_GROUP
) {
2060 struct perf_counter
*leader
, *sub
;
2061 u64 nr
= counter
->nr_siblings
;
2063 perf_output_put(&handle
, nr
);
2065 leader
= counter
->group_leader
;
2066 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2068 sub
->pmu
->read(sub
);
2070 group_entry
.event
= sub
->hw_event
.config
;
2071 group_entry
.counter
= atomic64_read(&sub
->count
);
2073 perf_output_put(&handle
, group_entry
);
2078 perf_output_copy(&handle
, callchain
, callchain_size
);
2080 perf_output_end(&handle
);
2087 struct perf_comm_event
{
2088 struct task_struct
*task
;
2093 struct perf_event_header header
;
2100 static void perf_counter_comm_output(struct perf_counter
*counter
,
2101 struct perf_comm_event
*comm_event
)
2103 struct perf_output_handle handle
;
2104 int size
= comm_event
->event
.header
.size
;
2105 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2110 perf_output_put(&handle
, comm_event
->event
);
2111 perf_output_copy(&handle
, comm_event
->comm
,
2112 comm_event
->comm_size
);
2113 perf_output_end(&handle
);
2116 static int perf_counter_comm_match(struct perf_counter
*counter
,
2117 struct perf_comm_event
*comm_event
)
2119 if (counter
->hw_event
.comm
&&
2120 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2126 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2127 struct perf_comm_event
*comm_event
)
2129 struct perf_counter
*counter
;
2131 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2135 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2136 if (perf_counter_comm_match(counter
, comm_event
))
2137 perf_counter_comm_output(counter
, comm_event
);
2142 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2144 struct perf_cpu_context
*cpuctx
;
2146 char *comm
= comm_event
->task
->comm
;
2148 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2150 comm_event
->comm
= comm
;
2151 comm_event
->comm_size
= size
;
2153 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2155 cpuctx
= &get_cpu_var(perf_cpu_context
);
2156 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2157 put_cpu_var(perf_cpu_context
);
2159 perf_counter_comm_ctx(¤t
->perf_counter_ctx
, comm_event
);
2162 void perf_counter_comm(struct task_struct
*task
)
2164 struct perf_comm_event comm_event
;
2166 if (!atomic_read(&nr_comm_tracking
))
2169 comm_event
= (struct perf_comm_event
){
2172 .header
= { .type
= PERF_EVENT_COMM
, },
2173 .pid
= task
->group_leader
->pid
,
2178 perf_counter_comm_event(&comm_event
);
2185 struct perf_mmap_event
{
2191 struct perf_event_header header
;
2201 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2202 struct perf_mmap_event
*mmap_event
)
2204 struct perf_output_handle handle
;
2205 int size
= mmap_event
->event
.header
.size
;
2206 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2211 perf_output_put(&handle
, mmap_event
->event
);
2212 perf_output_copy(&handle
, mmap_event
->file_name
,
2213 mmap_event
->file_size
);
2214 perf_output_end(&handle
);
2217 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2218 struct perf_mmap_event
*mmap_event
)
2220 if (counter
->hw_event
.mmap
&&
2221 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2224 if (counter
->hw_event
.munmap
&&
2225 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2231 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2232 struct perf_mmap_event
*mmap_event
)
2234 struct perf_counter
*counter
;
2236 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2240 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2241 if (perf_counter_mmap_match(counter
, mmap_event
))
2242 perf_counter_mmap_output(counter
, mmap_event
);
2247 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2249 struct perf_cpu_context
*cpuctx
;
2250 struct file
*file
= mmap_event
->file
;
2257 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2259 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2262 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2264 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2268 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2273 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2275 mmap_event
->file_name
= name
;
2276 mmap_event
->file_size
= size
;
2278 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2280 cpuctx
= &get_cpu_var(perf_cpu_context
);
2281 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2282 put_cpu_var(perf_cpu_context
);
2284 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2289 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2290 unsigned long pgoff
, struct file
*file
)
2292 struct perf_mmap_event mmap_event
;
2294 if (!atomic_read(&nr_mmap_tracking
))
2297 mmap_event
= (struct perf_mmap_event
){
2300 .header
= { .type
= PERF_EVENT_MMAP
, },
2301 .pid
= current
->group_leader
->pid
,
2302 .tid
= current
->pid
,
2309 perf_counter_mmap_event(&mmap_event
);
2312 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2313 unsigned long pgoff
, struct file
*file
)
2315 struct perf_mmap_event mmap_event
;
2317 if (!atomic_read(&nr_munmap_tracking
))
2320 mmap_event
= (struct perf_mmap_event
){
2323 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2324 .pid
= current
->group_leader
->pid
,
2325 .tid
= current
->pid
,
2332 perf_counter_mmap_event(&mmap_event
);
2336 * Generic counter overflow handling.
2339 int perf_counter_overflow(struct perf_counter
*counter
,
2340 int nmi
, struct pt_regs
*regs
, u64 addr
)
2342 int events
= atomic_read(&counter
->event_limit
);
2346 * XXX event_limit might not quite work as expected on inherited
2350 counter
->pending_kill
= POLL_IN
;
2351 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2353 counter
->pending_kill
= POLL_HUP
;
2355 counter
->pending_disable
= 1;
2356 perf_pending_queue(&counter
->pending
,
2357 perf_pending_counter
);
2359 perf_counter_disable(counter
);
2362 perf_counter_output(counter
, nmi
, regs
, addr
);
2367 * Generic software counter infrastructure
2370 static void perf_swcounter_update(struct perf_counter
*counter
)
2372 struct hw_perf_counter
*hwc
= &counter
->hw
;
2377 prev
= atomic64_read(&hwc
->prev_count
);
2378 now
= atomic64_read(&hwc
->count
);
2379 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2384 atomic64_add(delta
, &counter
->count
);
2385 atomic64_sub(delta
, &hwc
->period_left
);
2388 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2390 struct hw_perf_counter
*hwc
= &counter
->hw
;
2391 s64 left
= atomic64_read(&hwc
->period_left
);
2392 s64 period
= hwc
->irq_period
;
2394 if (unlikely(left
<= -period
)) {
2396 atomic64_set(&hwc
->period_left
, left
);
2399 if (unlikely(left
<= 0)) {
2401 atomic64_add(period
, &hwc
->period_left
);
2404 atomic64_set(&hwc
->prev_count
, -left
);
2405 atomic64_set(&hwc
->count
, -left
);
2408 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2410 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2411 struct perf_counter
*counter
;
2412 struct pt_regs
*regs
;
2414 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2415 counter
->pmu
->read(counter
);
2417 regs
= get_irq_regs();
2419 * In case we exclude kernel IPs or are somehow not in interrupt
2420 * context, provide the next best thing, the user IP.
2422 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2423 !counter
->hw_event
.exclude_user
)
2424 regs
= task_pt_regs(current
);
2427 if (perf_counter_overflow(counter
, 0, regs
, 0))
2428 ret
= HRTIMER_NORESTART
;
2431 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
2436 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2437 int nmi
, struct pt_regs
*regs
, u64 addr
)
2439 perf_swcounter_update(counter
);
2440 perf_swcounter_set_period(counter
);
2441 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2442 /* soft-disable the counter */
2447 static int perf_swcounter_match(struct perf_counter
*counter
,
2448 enum perf_event_types type
,
2449 u32 event
, struct pt_regs
*regs
)
2451 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2454 if (perf_event_raw(&counter
->hw_event
))
2457 if (perf_event_type(&counter
->hw_event
) != type
)
2460 if (perf_event_id(&counter
->hw_event
) != event
)
2463 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2466 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2472 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2473 int nmi
, struct pt_regs
*regs
, u64 addr
)
2475 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2476 if (counter
->hw
.irq_period
&& !neg
)
2477 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2480 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2481 enum perf_event_types type
, u32 event
,
2482 u64 nr
, int nmi
, struct pt_regs
*regs
,
2485 struct perf_counter
*counter
;
2487 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2491 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2492 if (perf_swcounter_match(counter
, type
, event
, regs
))
2493 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2498 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2501 return &cpuctx
->recursion
[3];
2504 return &cpuctx
->recursion
[2];
2507 return &cpuctx
->recursion
[1];
2509 return &cpuctx
->recursion
[0];
2512 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2513 u64 nr
, int nmi
, struct pt_regs
*regs
,
2516 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2517 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2525 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2526 nr
, nmi
, regs
, addr
);
2527 if (cpuctx
->task_ctx
) {
2528 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2529 nr
, nmi
, regs
, addr
);
2536 put_cpu_var(perf_cpu_context
);
2540 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2542 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2545 static void perf_swcounter_read(struct perf_counter
*counter
)
2547 perf_swcounter_update(counter
);
2550 static int perf_swcounter_enable(struct perf_counter
*counter
)
2552 perf_swcounter_set_period(counter
);
2556 static void perf_swcounter_disable(struct perf_counter
*counter
)
2558 perf_swcounter_update(counter
);
2561 static const struct pmu perf_ops_generic
= {
2562 .enable
= perf_swcounter_enable
,
2563 .disable
= perf_swcounter_disable
,
2564 .read
= perf_swcounter_read
,
2568 * Software counter: cpu wall time clock
2571 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2573 int cpu
= raw_smp_processor_id();
2577 now
= cpu_clock(cpu
);
2578 prev
= atomic64_read(&counter
->hw
.prev_count
);
2579 atomic64_set(&counter
->hw
.prev_count
, now
);
2580 atomic64_add(now
- prev
, &counter
->count
);
2583 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2585 struct hw_perf_counter
*hwc
= &counter
->hw
;
2586 int cpu
= raw_smp_processor_id();
2588 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2589 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2590 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2591 if (hwc
->irq_period
) {
2592 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2593 ns_to_ktime(hwc
->irq_period
), 0,
2594 HRTIMER_MODE_REL
, 0);
2600 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2602 hrtimer_cancel(&counter
->hw
.hrtimer
);
2603 cpu_clock_perf_counter_update(counter
);
2606 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2608 cpu_clock_perf_counter_update(counter
);
2611 static const struct pmu perf_ops_cpu_clock
= {
2612 .enable
= cpu_clock_perf_counter_enable
,
2613 .disable
= cpu_clock_perf_counter_disable
,
2614 .read
= cpu_clock_perf_counter_read
,
2618 * Software counter: task time clock
2621 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2626 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2628 atomic64_add(delta
, &counter
->count
);
2631 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2633 struct hw_perf_counter
*hwc
= &counter
->hw
;
2636 now
= counter
->ctx
->time
;
2638 atomic64_set(&hwc
->prev_count
, now
);
2639 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2640 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2641 if (hwc
->irq_period
) {
2642 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2643 ns_to_ktime(hwc
->irq_period
), 0,
2644 HRTIMER_MODE_REL
, 0);
2650 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2652 hrtimer_cancel(&counter
->hw
.hrtimer
);
2653 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
2657 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2662 update_context_time(counter
->ctx
);
2663 time
= counter
->ctx
->time
;
2665 u64 now
= perf_clock();
2666 u64 delta
= now
- counter
->ctx
->timestamp
;
2667 time
= counter
->ctx
->time
+ delta
;
2670 task_clock_perf_counter_update(counter
, time
);
2673 static const struct pmu perf_ops_task_clock
= {
2674 .enable
= task_clock_perf_counter_enable
,
2675 .disable
= task_clock_perf_counter_disable
,
2676 .read
= task_clock_perf_counter_read
,
2680 * Software counter: cpu migrations
2683 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2685 struct task_struct
*curr
= counter
->ctx
->task
;
2688 return curr
->se
.nr_migrations
;
2689 return cpu_nr_migrations(smp_processor_id());
2692 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2697 prev
= atomic64_read(&counter
->hw
.prev_count
);
2698 now
= get_cpu_migrations(counter
);
2700 atomic64_set(&counter
->hw
.prev_count
, now
);
2704 atomic64_add(delta
, &counter
->count
);
2707 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2709 cpu_migrations_perf_counter_update(counter
);
2712 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2714 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2715 atomic64_set(&counter
->hw
.prev_count
,
2716 get_cpu_migrations(counter
));
2720 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2722 cpu_migrations_perf_counter_update(counter
);
2725 static const struct pmu perf_ops_cpu_migrations
= {
2726 .enable
= cpu_migrations_perf_counter_enable
,
2727 .disable
= cpu_migrations_perf_counter_disable
,
2728 .read
= cpu_migrations_perf_counter_read
,
2731 #ifdef CONFIG_EVENT_PROFILE
2732 void perf_tpcounter_event(int event_id
)
2734 struct pt_regs
*regs
= get_irq_regs();
2737 regs
= task_pt_regs(current
);
2739 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
2741 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
2743 extern int ftrace_profile_enable(int);
2744 extern void ftrace_profile_disable(int);
2746 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2748 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2751 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2753 int event_id
= perf_event_id(&counter
->hw_event
);
2756 ret
= ftrace_profile_enable(event_id
);
2760 counter
->destroy
= tp_perf_counter_destroy
;
2761 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2763 return &perf_ops_generic
;
2766 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2772 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
2774 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2775 const struct pmu
*pmu
= NULL
;
2776 struct hw_perf_counter
*hwc
= &counter
->hw
;
2779 * Software counters (currently) can't in general distinguish
2780 * between user, kernel and hypervisor events.
2781 * However, context switches and cpu migrations are considered
2782 * to be kernel events, and page faults are never hypervisor
2785 switch (perf_event_id(&counter
->hw_event
)) {
2786 case PERF_COUNT_CPU_CLOCK
:
2787 pmu
= &perf_ops_cpu_clock
;
2789 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2790 hw_event
->irq_period
= 10000;
2792 case PERF_COUNT_TASK_CLOCK
:
2794 * If the user instantiates this as a per-cpu counter,
2795 * use the cpu_clock counter instead.
2797 if (counter
->ctx
->task
)
2798 pmu
= &perf_ops_task_clock
;
2800 pmu
= &perf_ops_cpu_clock
;
2802 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2803 hw_event
->irq_period
= 10000;
2805 case PERF_COUNT_PAGE_FAULTS
:
2806 case PERF_COUNT_PAGE_FAULTS_MIN
:
2807 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2808 case PERF_COUNT_CONTEXT_SWITCHES
:
2809 pmu
= &perf_ops_generic
;
2811 case PERF_COUNT_CPU_MIGRATIONS
:
2812 if (!counter
->hw_event
.exclude_kernel
)
2813 pmu
= &perf_ops_cpu_migrations
;
2818 hwc
->irq_period
= hw_event
->irq_period
;
2824 * Allocate and initialize a counter structure
2826 static struct perf_counter
*
2827 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2829 struct perf_counter_context
*ctx
,
2830 struct perf_counter
*group_leader
,
2833 const struct pmu
*pmu
;
2834 struct perf_counter
*counter
;
2837 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2839 return ERR_PTR(-ENOMEM
);
2842 * Single counters are their own group leaders, with an
2843 * empty sibling list:
2846 group_leader
= counter
;
2848 mutex_init(&counter
->mutex
);
2849 INIT_LIST_HEAD(&counter
->list_entry
);
2850 INIT_LIST_HEAD(&counter
->event_entry
);
2851 INIT_LIST_HEAD(&counter
->sibling_list
);
2852 init_waitqueue_head(&counter
->waitq
);
2854 mutex_init(&counter
->mmap_mutex
);
2856 INIT_LIST_HEAD(&counter
->child_list
);
2859 counter
->hw_event
= *hw_event
;
2860 counter
->group_leader
= group_leader
;
2861 counter
->pmu
= NULL
;
2864 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2865 if (hw_event
->disabled
)
2866 counter
->state
= PERF_COUNTER_STATE_OFF
;
2871 * we currently do not support PERF_RECORD_GROUP on inherited counters
2873 if (hw_event
->inherit
&& (hw_event
->record_type
& PERF_RECORD_GROUP
))
2876 if (perf_event_raw(hw_event
)) {
2877 pmu
= hw_perf_counter_init(counter
);
2881 switch (perf_event_type(hw_event
)) {
2882 case PERF_TYPE_HARDWARE
:
2883 pmu
= hw_perf_counter_init(counter
);
2886 case PERF_TYPE_SOFTWARE
:
2887 pmu
= sw_perf_counter_init(counter
);
2890 case PERF_TYPE_TRACEPOINT
:
2891 pmu
= tp_perf_counter_init(counter
);
2898 else if (IS_ERR(pmu
))
2903 return ERR_PTR(err
);
2908 atomic_inc(&nr_counters
);
2909 if (counter
->hw_event
.mmap
)
2910 atomic_inc(&nr_mmap_tracking
);
2911 if (counter
->hw_event
.munmap
)
2912 atomic_inc(&nr_munmap_tracking
);
2913 if (counter
->hw_event
.comm
)
2914 atomic_inc(&nr_comm_tracking
);
2920 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2922 * @hw_event_uptr: event type attributes for monitoring/sampling
2925 * @group_fd: group leader counter fd
2927 SYSCALL_DEFINE5(perf_counter_open
,
2928 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2929 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2931 struct perf_counter
*counter
, *group_leader
;
2932 struct perf_counter_hw_event hw_event
;
2933 struct perf_counter_context
*ctx
;
2934 struct file
*counter_file
= NULL
;
2935 struct file
*group_file
= NULL
;
2936 int fput_needed
= 0;
2937 int fput_needed2
= 0;
2940 /* for future expandability... */
2944 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2948 * Get the target context (task or percpu):
2950 ctx
= find_get_context(pid
, cpu
);
2952 return PTR_ERR(ctx
);
2955 * Look up the group leader (we will attach this counter to it):
2957 group_leader
= NULL
;
2958 if (group_fd
!= -1) {
2960 group_file
= fget_light(group_fd
, &fput_needed
);
2962 goto err_put_context
;
2963 if (group_file
->f_op
!= &perf_fops
)
2964 goto err_put_context
;
2966 group_leader
= group_file
->private_data
;
2968 * Do not allow a recursive hierarchy (this new sibling
2969 * becoming part of another group-sibling):
2971 if (group_leader
->group_leader
!= group_leader
)
2972 goto err_put_context
;
2974 * Do not allow to attach to a group in a different
2975 * task or CPU context:
2977 if (group_leader
->ctx
!= ctx
)
2978 goto err_put_context
;
2980 * Only a group leader can be exclusive or pinned
2982 if (hw_event
.exclusive
|| hw_event
.pinned
)
2983 goto err_put_context
;
2986 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2988 ret
= PTR_ERR(counter
);
2989 if (IS_ERR(counter
))
2990 goto err_put_context
;
2992 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2994 goto err_free_put_context
;
2996 counter_file
= fget_light(ret
, &fput_needed2
);
2998 goto err_free_put_context
;
3000 counter
->filp
= counter_file
;
3001 mutex_lock(&ctx
->mutex
);
3002 perf_install_in_context(ctx
, counter
, cpu
);
3003 mutex_unlock(&ctx
->mutex
);
3005 fput_light(counter_file
, fput_needed2
);
3008 fput_light(group_file
, fput_needed
);
3012 err_free_put_context
:
3022 * Initialize the perf_counter context in a task_struct:
3025 __perf_counter_init_context(struct perf_counter_context
*ctx
,
3026 struct task_struct
*task
)
3028 memset(ctx
, 0, sizeof(*ctx
));
3029 spin_lock_init(&ctx
->lock
);
3030 mutex_init(&ctx
->mutex
);
3031 INIT_LIST_HEAD(&ctx
->counter_list
);
3032 INIT_LIST_HEAD(&ctx
->event_list
);
3037 * inherit a counter from parent task to child task:
3039 static struct perf_counter
*
3040 inherit_counter(struct perf_counter
*parent_counter
,
3041 struct task_struct
*parent
,
3042 struct perf_counter_context
*parent_ctx
,
3043 struct task_struct
*child
,
3044 struct perf_counter
*group_leader
,
3045 struct perf_counter_context
*child_ctx
)
3047 struct perf_counter
*child_counter
;
3050 * Instead of creating recursive hierarchies of counters,
3051 * we link inherited counters back to the original parent,
3052 * which has a filp for sure, which we use as the reference
3055 if (parent_counter
->parent
)
3056 parent_counter
= parent_counter
->parent
;
3058 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
3059 parent_counter
->cpu
, child_ctx
,
3060 group_leader
, GFP_KERNEL
);
3061 if (IS_ERR(child_counter
))
3062 return child_counter
;
3065 * Link it up in the child's context:
3067 child_counter
->task
= child
;
3068 add_counter_to_ctx(child_counter
, child_ctx
);
3070 child_counter
->parent
= parent_counter
;
3072 * inherit into child's child as well:
3074 child_counter
->hw_event
.inherit
= 1;
3077 * Get a reference to the parent filp - we will fput it
3078 * when the child counter exits. This is safe to do because
3079 * we are in the parent and we know that the filp still
3080 * exists and has a nonzero count:
3082 atomic_long_inc(&parent_counter
->filp
->f_count
);
3085 * Link this into the parent counter's child list
3087 mutex_lock(&parent_counter
->mutex
);
3088 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3091 * Make the child state follow the state of the parent counter,
3092 * not its hw_event.disabled bit. We hold the parent's mutex,
3093 * so we won't race with perf_counter_{en,dis}able_family.
3095 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3096 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3098 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3100 mutex_unlock(&parent_counter
->mutex
);
3102 return child_counter
;
3105 static int inherit_group(struct perf_counter
*parent_counter
,
3106 struct task_struct
*parent
,
3107 struct perf_counter_context
*parent_ctx
,
3108 struct task_struct
*child
,
3109 struct perf_counter_context
*child_ctx
)
3111 struct perf_counter
*leader
;
3112 struct perf_counter
*sub
;
3113 struct perf_counter
*child_ctr
;
3115 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3116 child
, NULL
, child_ctx
);
3118 return PTR_ERR(leader
);
3119 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3120 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3121 child
, leader
, child_ctx
);
3122 if (IS_ERR(child_ctr
))
3123 return PTR_ERR(child_ctr
);
3128 static void sync_child_counter(struct perf_counter
*child_counter
,
3129 struct perf_counter
*parent_counter
)
3131 u64 parent_val
, child_val
;
3133 parent_val
= atomic64_read(&parent_counter
->count
);
3134 child_val
= atomic64_read(&child_counter
->count
);
3137 * Add back the child's count to the parent's count:
3139 atomic64_add(child_val
, &parent_counter
->count
);
3140 atomic64_add(child_counter
->total_time_enabled
,
3141 &parent_counter
->child_total_time_enabled
);
3142 atomic64_add(child_counter
->total_time_running
,
3143 &parent_counter
->child_total_time_running
);
3146 * Remove this counter from the parent's list
3148 mutex_lock(&parent_counter
->mutex
);
3149 list_del_init(&child_counter
->child_list
);
3150 mutex_unlock(&parent_counter
->mutex
);
3153 * Release the parent counter, if this was the last
3156 fput(parent_counter
->filp
);
3160 __perf_counter_exit_task(struct task_struct
*child
,
3161 struct perf_counter
*child_counter
,
3162 struct perf_counter_context
*child_ctx
)
3164 struct perf_counter
*parent_counter
;
3165 struct perf_counter
*sub
, *tmp
;
3168 * If we do not self-reap then we have to wait for the
3169 * child task to unschedule (it will happen for sure),
3170 * so that its counter is at its final count. (This
3171 * condition triggers rarely - child tasks usually get
3172 * off their CPU before the parent has a chance to
3173 * get this far into the reaping action)
3175 if (child
!= current
) {
3176 wait_task_inactive(child
, 0);
3177 list_del_init(&child_counter
->list_entry
);
3178 update_counter_times(child_counter
);
3180 struct perf_cpu_context
*cpuctx
;
3181 unsigned long flags
;
3185 * Disable and unlink this counter.
3187 * Be careful about zapping the list - IRQ/NMI context
3188 * could still be processing it:
3190 local_irq_save(flags
);
3191 perf_flags
= hw_perf_save_disable();
3193 cpuctx
= &__get_cpu_var(perf_cpu_context
);
3195 group_sched_out(child_counter
, cpuctx
, child_ctx
);
3196 update_counter_times(child_counter
);
3198 list_del_init(&child_counter
->list_entry
);
3200 child_ctx
->nr_counters
--;
3202 hw_perf_restore(perf_flags
);
3203 local_irq_restore(flags
);
3206 parent_counter
= child_counter
->parent
;
3208 * It can happen that parent exits first, and has counters
3209 * that are still around due to the child reference. These
3210 * counters need to be zapped - but otherwise linger.
3212 if (parent_counter
) {
3213 sync_child_counter(child_counter
, parent_counter
);
3214 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
3217 sync_child_counter(sub
, sub
->parent
);
3221 free_counter(child_counter
);
3226 * When a child task exits, feed back counter values to parent counters.
3228 * Note: we may be running in child context, but the PID is not hashed
3229 * anymore so new counters will not be added.
3231 void perf_counter_exit_task(struct task_struct
*child
)
3233 struct perf_counter
*child_counter
, *tmp
;
3234 struct perf_counter_context
*child_ctx
;
3236 child_ctx
= &child
->perf_counter_ctx
;
3238 if (likely(!child_ctx
->nr_counters
))
3241 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3243 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3247 * Initialize the perf_counter context in task_struct
3249 void perf_counter_init_task(struct task_struct
*child
)
3251 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3252 struct perf_counter
*counter
;
3253 struct task_struct
*parent
= current
;
3255 child_ctx
= &child
->perf_counter_ctx
;
3256 parent_ctx
= &parent
->perf_counter_ctx
;
3258 __perf_counter_init_context(child_ctx
, child
);
3261 * This is executed from the parent task context, so inherit
3262 * counters that have been marked for cloning:
3265 if (likely(!parent_ctx
->nr_counters
))
3269 * Lock the parent list. No need to lock the child - not PID
3270 * hashed yet and not running, so nobody can access it.
3272 mutex_lock(&parent_ctx
->mutex
);
3275 * We dont have to disable NMIs - we are only looking at
3276 * the list, not manipulating it:
3278 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
3279 if (!counter
->hw_event
.inherit
)
3282 if (inherit_group(counter
, parent
,
3283 parent_ctx
, child
, child_ctx
))
3287 mutex_unlock(&parent_ctx
->mutex
);
3290 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3292 struct perf_cpu_context
*cpuctx
;
3294 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3295 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3297 spin_lock(&perf_resource_lock
);
3298 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3299 spin_unlock(&perf_resource_lock
);
3301 hw_perf_counter_setup(cpu
);
3304 #ifdef CONFIG_HOTPLUG_CPU
3305 static void __perf_counter_exit_cpu(void *info
)
3307 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3308 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3309 struct perf_counter
*counter
, *tmp
;
3311 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3312 __perf_counter_remove_from_context(counter
);
3314 static void perf_counter_exit_cpu(int cpu
)
3316 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3317 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3319 mutex_lock(&ctx
->mutex
);
3320 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3321 mutex_unlock(&ctx
->mutex
);
3324 static inline void perf_counter_exit_cpu(int cpu
) { }
3327 static int __cpuinit
3328 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3330 unsigned int cpu
= (long)hcpu
;
3334 case CPU_UP_PREPARE
:
3335 case CPU_UP_PREPARE_FROZEN
:
3336 perf_counter_init_cpu(cpu
);
3339 case CPU_DOWN_PREPARE
:
3340 case CPU_DOWN_PREPARE_FROZEN
:
3341 perf_counter_exit_cpu(cpu
);
3351 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3352 .notifier_call
= perf_cpu_notify
,
3355 void __init
perf_counter_init(void)
3357 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3358 (void *)(long)smp_processor_id());
3359 register_cpu_notifier(&perf_cpu_nb
);
3362 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3364 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3368 perf_set_reserve_percpu(struct sysdev_class
*class,
3372 struct perf_cpu_context
*cpuctx
;
3376 err
= strict_strtoul(buf
, 10, &val
);
3379 if (val
> perf_max_counters
)
3382 spin_lock(&perf_resource_lock
);
3383 perf_reserved_percpu
= val
;
3384 for_each_online_cpu(cpu
) {
3385 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3386 spin_lock_irq(&cpuctx
->ctx
.lock
);
3387 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3388 perf_max_counters
- perf_reserved_percpu
);
3389 cpuctx
->max_pertask
= mpt
;
3390 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3392 spin_unlock(&perf_resource_lock
);
3397 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3399 return sprintf(buf
, "%d\n", perf_overcommit
);
3403 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3408 err
= strict_strtoul(buf
, 10, &val
);
3414 spin_lock(&perf_resource_lock
);
3415 perf_overcommit
= val
;
3416 spin_unlock(&perf_resource_lock
);
3421 static SYSDEV_CLASS_ATTR(
3424 perf_show_reserve_percpu
,
3425 perf_set_reserve_percpu
3428 static SYSDEV_CLASS_ATTR(
3431 perf_show_overcommit
,
3435 static struct attribute
*perfclass_attrs
[] = {
3436 &attr_reserve_percpu
.attr
,
3437 &attr_overcommit
.attr
,
3441 static struct attribute_group perfclass_attr_group
= {
3442 .attrs
= perfclass_attrs
,
3443 .name
= "perf_counters",
3446 static int __init
perf_counter_sysfs_init(void)
3448 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3449 &perfclass_attr_group
);
3451 device_initcall(perf_counter_sysfs_init
);