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 * Return 1 for a group consisting entirely of software counters,
424 * 0 if the group contains any hardware counters.
426 static int is_software_only_group(struct perf_counter
*leader
)
428 struct perf_counter
*counter
;
430 if (!is_software_counter(leader
))
433 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
434 if (!is_software_counter(counter
))
441 * Work out whether we can put this counter group on the CPU now.
443 static int group_can_go_on(struct perf_counter
*counter
,
444 struct perf_cpu_context
*cpuctx
,
448 * Groups consisting entirely of software counters can always go on.
450 if (is_software_only_group(counter
))
453 * If an exclusive group is already on, no other hardware
454 * counters can go on.
456 if (cpuctx
->exclusive
)
459 * If this group is exclusive and there are already
460 * counters on the CPU, it can't go on.
462 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
465 * Otherwise, try to add it if all previous groups were able
471 static void add_counter_to_ctx(struct perf_counter
*counter
,
472 struct perf_counter_context
*ctx
)
474 list_add_counter(counter
, ctx
);
476 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
477 counter
->tstamp_enabled
= ctx
->time
;
478 counter
->tstamp_running
= ctx
->time
;
479 counter
->tstamp_stopped
= ctx
->time
;
483 * Cross CPU call to install and enable a performance counter
485 static void __perf_install_in_context(void *info
)
487 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
488 struct perf_counter
*counter
= info
;
489 struct perf_counter_context
*ctx
= counter
->ctx
;
490 struct perf_counter
*leader
= counter
->group_leader
;
491 int cpu
= smp_processor_id();
497 * If this is a task context, we need to check whether it is
498 * the current task context of this cpu. If not it has been
499 * scheduled out before the smp call arrived.
501 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
504 spin_lock_irqsave(&ctx
->lock
, flags
);
505 update_context_time(ctx
);
508 * Protect the list operation against NMI by disabling the
509 * counters on a global level. NOP for non NMI based counters.
511 perf_flags
= hw_perf_save_disable();
513 add_counter_to_ctx(counter
, ctx
);
516 * Don't put the counter on if it is disabled or if
517 * it is in a group and the group isn't on.
519 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
520 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
524 * An exclusive counter can't go on if there are already active
525 * hardware counters, and no hardware counter can go on if there
526 * is already an exclusive counter on.
528 if (!group_can_go_on(counter
, cpuctx
, 1))
531 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
535 * This counter couldn't go on. If it is in a group
536 * then we have to pull the whole group off.
537 * If the counter group is pinned then put it in error state.
539 if (leader
!= counter
)
540 group_sched_out(leader
, cpuctx
, ctx
);
541 if (leader
->hw_event
.pinned
) {
542 update_group_times(leader
);
543 leader
->state
= PERF_COUNTER_STATE_ERROR
;
547 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
548 cpuctx
->max_pertask
--;
551 hw_perf_restore(perf_flags
);
553 spin_unlock_irqrestore(&ctx
->lock
, flags
);
557 * Attach a performance counter to a context
559 * First we add the counter to the list with the hardware enable bit
560 * in counter->hw_config cleared.
562 * If the counter is attached to a task which is on a CPU we use a smp
563 * call to enable it in the task context. The task might have been
564 * scheduled away, but we check this in the smp call again.
566 * Must be called with ctx->mutex held.
569 perf_install_in_context(struct perf_counter_context
*ctx
,
570 struct perf_counter
*counter
,
573 struct task_struct
*task
= ctx
->task
;
577 * Per cpu counters are installed via an smp call and
578 * the install is always sucessful.
580 smp_call_function_single(cpu
, __perf_install_in_context
,
585 counter
->task
= task
;
587 task_oncpu_function_call(task
, __perf_install_in_context
,
590 spin_lock_irq(&ctx
->lock
);
592 * we need to retry the smp call.
594 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
595 spin_unlock_irq(&ctx
->lock
);
600 * The lock prevents that this context is scheduled in so we
601 * can add the counter safely, if it the call above did not
604 if (list_empty(&counter
->list_entry
))
605 add_counter_to_ctx(counter
, ctx
);
606 spin_unlock_irq(&ctx
->lock
);
610 * Cross CPU call to enable a performance counter
612 static void __perf_counter_enable(void *info
)
614 struct perf_counter
*counter
= info
;
615 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
616 struct perf_counter_context
*ctx
= counter
->ctx
;
617 struct perf_counter
*leader
= counter
->group_leader
;
622 * If this is a per-task counter, need to check whether this
623 * counter's task is the current task on this cpu.
625 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
628 spin_lock_irqsave(&ctx
->lock
, flags
);
629 update_context_time(ctx
);
631 counter
->prev_state
= counter
->state
;
632 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
634 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
635 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
638 * If the counter is in a group and isn't the group leader,
639 * then don't put it on unless the group is on.
641 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
644 if (!group_can_go_on(counter
, cpuctx
, 1))
647 err
= counter_sched_in(counter
, cpuctx
, ctx
,
652 * If this counter can't go on and it's part of a
653 * group, then the whole group has to come off.
655 if (leader
!= counter
)
656 group_sched_out(leader
, cpuctx
, ctx
);
657 if (leader
->hw_event
.pinned
) {
658 update_group_times(leader
);
659 leader
->state
= PERF_COUNTER_STATE_ERROR
;
664 spin_unlock_irqrestore(&ctx
->lock
, flags
);
670 static void perf_counter_enable(struct perf_counter
*counter
)
672 struct perf_counter_context
*ctx
= counter
->ctx
;
673 struct task_struct
*task
= ctx
->task
;
677 * Enable the counter on the cpu that it's on
679 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
684 spin_lock_irq(&ctx
->lock
);
685 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
689 * If the counter is in error state, clear that first.
690 * That way, if we see the counter in error state below, we
691 * know that it has gone back into error state, as distinct
692 * from the task having been scheduled away before the
693 * cross-call arrived.
695 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
696 counter
->state
= PERF_COUNTER_STATE_OFF
;
699 spin_unlock_irq(&ctx
->lock
);
700 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
702 spin_lock_irq(&ctx
->lock
);
705 * If the context is active and the counter is still off,
706 * we need to retry the cross-call.
708 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
712 * Since we have the lock this context can't be scheduled
713 * in, so we can change the state safely.
715 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
716 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
717 counter
->tstamp_enabled
=
718 ctx
->time
- counter
->total_time_enabled
;
721 spin_unlock_irq(&ctx
->lock
);
724 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
727 * not supported on inherited counters
729 if (counter
->hw_event
.inherit
)
732 atomic_add(refresh
, &counter
->event_limit
);
733 perf_counter_enable(counter
);
738 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
739 struct perf_cpu_context
*cpuctx
)
741 struct perf_counter
*counter
;
744 spin_lock(&ctx
->lock
);
746 if (likely(!ctx
->nr_counters
))
748 update_context_time(ctx
);
750 flags
= hw_perf_save_disable();
751 if (ctx
->nr_active
) {
752 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
753 group_sched_out(counter
, cpuctx
, ctx
);
755 hw_perf_restore(flags
);
757 spin_unlock(&ctx
->lock
);
761 * Called from scheduler to remove the counters of the current task,
762 * with interrupts disabled.
764 * We stop each counter and update the counter value in counter->count.
766 * This does not protect us against NMI, but disable()
767 * sets the disabled bit in the control field of counter _before_
768 * accessing the counter control register. If a NMI hits, then it will
769 * not restart the counter.
771 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
773 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
774 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
775 struct pt_regs
*regs
;
777 if (likely(!cpuctx
->task_ctx
))
780 update_context_time(ctx
);
782 regs
= task_pt_regs(task
);
783 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
784 __perf_counter_sched_out(ctx
, cpuctx
);
786 cpuctx
->task_ctx
= NULL
;
789 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
791 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
795 group_sched_in(struct perf_counter
*group_counter
,
796 struct perf_cpu_context
*cpuctx
,
797 struct perf_counter_context
*ctx
,
800 struct perf_counter
*counter
, *partial_group
;
803 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
806 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
808 return ret
< 0 ? ret
: 0;
810 group_counter
->prev_state
= group_counter
->state
;
811 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
815 * Schedule in siblings as one group (if any):
817 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
818 counter
->prev_state
= counter
->state
;
819 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
820 partial_group
= counter
;
829 * Groups can be scheduled in as one unit only, so undo any
830 * partial group before returning:
832 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
833 if (counter
== partial_group
)
835 counter_sched_out(counter
, cpuctx
, ctx
);
837 counter_sched_out(group_counter
, cpuctx
, ctx
);
843 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
844 struct perf_cpu_context
*cpuctx
, int cpu
)
846 struct perf_counter
*counter
;
850 spin_lock(&ctx
->lock
);
852 if (likely(!ctx
->nr_counters
))
855 ctx
->timestamp
= perf_clock();
857 flags
= hw_perf_save_disable();
860 * First go through the list and put on any pinned groups
861 * in order to give them the best chance of going on.
863 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
864 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
865 !counter
->hw_event
.pinned
)
867 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
870 if (group_can_go_on(counter
, cpuctx
, 1))
871 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
874 * If this pinned group hasn't been scheduled,
875 * put it in error state.
877 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
878 update_group_times(counter
);
879 counter
->state
= PERF_COUNTER_STATE_ERROR
;
883 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
885 * Ignore counters in OFF or ERROR state, and
886 * ignore pinned counters since we did them already.
888 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
889 counter
->hw_event
.pinned
)
893 * Listen to the 'cpu' scheduling filter constraint
896 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
899 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
900 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
904 hw_perf_restore(flags
);
906 spin_unlock(&ctx
->lock
);
910 * Called from scheduler to add the counters of the current task
911 * with interrupts disabled.
913 * We restore the counter value and then enable it.
915 * This does not protect us against NMI, but enable()
916 * sets the enabled bit in the control field of counter _before_
917 * accessing the counter control register. If a NMI hits, then it will
918 * keep the counter running.
920 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
922 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
923 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
925 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
926 cpuctx
->task_ctx
= ctx
;
929 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
931 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
933 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
936 int perf_counter_task_disable(void)
938 struct task_struct
*curr
= current
;
939 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
940 struct perf_counter
*counter
;
945 if (likely(!ctx
->nr_counters
))
948 local_irq_save(flags
);
949 cpu
= smp_processor_id();
951 perf_counter_task_sched_out(curr
, cpu
);
953 spin_lock(&ctx
->lock
);
956 * Disable all the counters:
958 perf_flags
= hw_perf_save_disable();
960 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
961 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
962 update_group_times(counter
);
963 counter
->state
= PERF_COUNTER_STATE_OFF
;
967 hw_perf_restore(perf_flags
);
969 spin_unlock_irqrestore(&ctx
->lock
, flags
);
974 int perf_counter_task_enable(void)
976 struct task_struct
*curr
= current
;
977 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
978 struct perf_counter
*counter
;
983 if (likely(!ctx
->nr_counters
))
986 local_irq_save(flags
);
987 cpu
= smp_processor_id();
989 perf_counter_task_sched_out(curr
, cpu
);
991 spin_lock(&ctx
->lock
);
994 * Disable all the counters:
996 perf_flags
= hw_perf_save_disable();
998 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
999 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1001 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1002 counter
->tstamp_enabled
=
1003 ctx
->time
- counter
->total_time_enabled
;
1004 counter
->hw_event
.disabled
= 0;
1006 hw_perf_restore(perf_flags
);
1008 spin_unlock(&ctx
->lock
);
1010 perf_counter_task_sched_in(curr
, cpu
);
1012 local_irq_restore(flags
);
1018 * Round-robin a context's counters:
1020 static void rotate_ctx(struct perf_counter_context
*ctx
)
1022 struct perf_counter
*counter
;
1025 if (!ctx
->nr_counters
)
1028 spin_lock(&ctx
->lock
);
1030 * Rotate the first entry last (works just fine for group counters too):
1032 perf_flags
= hw_perf_save_disable();
1033 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1034 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1037 hw_perf_restore(perf_flags
);
1039 spin_unlock(&ctx
->lock
);
1042 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1044 struct perf_cpu_context
*cpuctx
;
1045 struct perf_counter_context
*ctx
;
1047 if (!atomic_read(&nr_counters
))
1050 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1051 ctx
= &curr
->perf_counter_ctx
;
1053 perf_counter_cpu_sched_out(cpuctx
);
1054 perf_counter_task_sched_out(curr
, cpu
);
1056 rotate_ctx(&cpuctx
->ctx
);
1059 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1060 perf_counter_task_sched_in(curr
, cpu
);
1064 * Cross CPU call to read the hardware counter
1066 static void __read(void *info
)
1068 struct perf_counter
*counter
= info
;
1069 struct perf_counter_context
*ctx
= counter
->ctx
;
1070 unsigned long flags
;
1072 local_irq_save(flags
);
1074 update_context_time(ctx
);
1075 counter
->pmu
->read(counter
);
1076 update_counter_times(counter
);
1077 local_irq_restore(flags
);
1080 static u64
perf_counter_read(struct perf_counter
*counter
)
1083 * If counter is enabled and currently active on a CPU, update the
1084 * value in the counter structure:
1086 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1087 smp_call_function_single(counter
->oncpu
,
1088 __read
, counter
, 1);
1089 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1090 update_counter_times(counter
);
1093 return atomic64_read(&counter
->count
);
1096 static void put_context(struct perf_counter_context
*ctx
)
1099 put_task_struct(ctx
->task
);
1102 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1104 struct perf_cpu_context
*cpuctx
;
1105 struct perf_counter_context
*ctx
;
1106 struct task_struct
*task
;
1109 * If cpu is not a wildcard then this is a percpu counter:
1112 /* Must be root to operate on a CPU counter: */
1113 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1114 return ERR_PTR(-EACCES
);
1116 if (cpu
< 0 || cpu
> num_possible_cpus())
1117 return ERR_PTR(-EINVAL
);
1120 * We could be clever and allow to attach a counter to an
1121 * offline CPU and activate it when the CPU comes up, but
1124 if (!cpu_isset(cpu
, cpu_online_map
))
1125 return ERR_PTR(-ENODEV
);
1127 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1137 task
= find_task_by_vpid(pid
);
1139 get_task_struct(task
);
1143 return ERR_PTR(-ESRCH
);
1145 ctx
= &task
->perf_counter_ctx
;
1148 /* Reuse ptrace permission checks for now. */
1149 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1151 return ERR_PTR(-EACCES
);
1157 static void free_counter_rcu(struct rcu_head
*head
)
1159 struct perf_counter
*counter
;
1161 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1165 static void perf_pending_sync(struct perf_counter
*counter
);
1167 static void free_counter(struct perf_counter
*counter
)
1169 perf_pending_sync(counter
);
1171 atomic_dec(&nr_counters
);
1172 if (counter
->hw_event
.mmap
)
1173 atomic_dec(&nr_mmap_tracking
);
1174 if (counter
->hw_event
.munmap
)
1175 atomic_dec(&nr_munmap_tracking
);
1176 if (counter
->hw_event
.comm
)
1177 atomic_dec(&nr_comm_tracking
);
1179 if (counter
->destroy
)
1180 counter
->destroy(counter
);
1182 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1186 * Called when the last reference to the file is gone.
1188 static int perf_release(struct inode
*inode
, struct file
*file
)
1190 struct perf_counter
*counter
= file
->private_data
;
1191 struct perf_counter_context
*ctx
= counter
->ctx
;
1193 file
->private_data
= NULL
;
1195 mutex_lock(&ctx
->mutex
);
1196 mutex_lock(&counter
->mutex
);
1198 perf_counter_remove_from_context(counter
);
1200 mutex_unlock(&counter
->mutex
);
1201 mutex_unlock(&ctx
->mutex
);
1203 free_counter(counter
);
1210 * Read the performance counter - simple non blocking version for now
1213 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1219 * Return end-of-file for a read on a counter that is in
1220 * error state (i.e. because it was pinned but it couldn't be
1221 * scheduled on to the CPU at some point).
1223 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1226 mutex_lock(&counter
->mutex
);
1227 values
[0] = perf_counter_read(counter
);
1229 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1230 values
[n
++] = counter
->total_time_enabled
+
1231 atomic64_read(&counter
->child_total_time_enabled
);
1232 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1233 values
[n
++] = counter
->total_time_running
+
1234 atomic64_read(&counter
->child_total_time_running
);
1235 mutex_unlock(&counter
->mutex
);
1237 if (count
< n
* sizeof(u64
))
1239 count
= n
* sizeof(u64
);
1241 if (copy_to_user(buf
, values
, count
))
1248 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1250 struct perf_counter
*counter
= file
->private_data
;
1252 return perf_read_hw(counter
, buf
, count
);
1255 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1257 struct perf_counter
*counter
= file
->private_data
;
1258 struct perf_mmap_data
*data
;
1259 unsigned int events
= POLL_HUP
;
1262 data
= rcu_dereference(counter
->data
);
1264 events
= atomic_xchg(&data
->poll
, 0);
1267 poll_wait(file
, &counter
->waitq
, wait
);
1272 static void perf_counter_reset(struct perf_counter
*counter
)
1274 (void)perf_counter_read(counter
);
1275 atomic_set(&counter
->count
, 0);
1276 perf_counter_update_userpage(counter
);
1279 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1280 void (*func
)(struct perf_counter
*))
1282 struct perf_counter_context
*ctx
= counter
->ctx
;
1283 struct perf_counter
*sibling
;
1285 spin_lock_irq(&ctx
->lock
);
1286 counter
= counter
->group_leader
;
1289 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1291 spin_unlock_irq(&ctx
->lock
);
1294 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1295 void (*func
)(struct perf_counter
*))
1297 struct perf_counter
*child
;
1299 mutex_lock(&counter
->mutex
);
1301 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1303 mutex_unlock(&counter
->mutex
);
1306 static void perf_counter_for_each(struct perf_counter
*counter
,
1307 void (*func
)(struct perf_counter
*))
1309 struct perf_counter
*child
;
1311 mutex_lock(&counter
->mutex
);
1312 perf_counter_for_each_sibling(counter
, func
);
1313 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1314 perf_counter_for_each_sibling(child
, func
);
1315 mutex_unlock(&counter
->mutex
);
1318 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1320 struct perf_counter
*counter
= file
->private_data
;
1321 void (*func
)(struct perf_counter
*);
1325 case PERF_COUNTER_IOC_ENABLE
:
1326 func
= perf_counter_enable
;
1328 case PERF_COUNTER_IOC_DISABLE
:
1329 func
= perf_counter_disable
;
1331 case PERF_COUNTER_IOC_RESET
:
1332 func
= perf_counter_reset
;
1335 case PERF_COUNTER_IOC_REFRESH
:
1336 return perf_counter_refresh(counter
, arg
);
1341 if (flags
& PERF_IOC_FLAG_GROUP
)
1342 perf_counter_for_each(counter
, func
);
1344 perf_counter_for_each_child(counter
, func
);
1350 * Callers need to ensure there can be no nesting of this function, otherwise
1351 * the seqlock logic goes bad. We can not serialize this because the arch
1352 * code calls this from NMI context.
1354 void perf_counter_update_userpage(struct perf_counter
*counter
)
1356 struct perf_mmap_data
*data
;
1357 struct perf_counter_mmap_page
*userpg
;
1360 data
= rcu_dereference(counter
->data
);
1364 userpg
= data
->user_page
;
1367 * Disable preemption so as to not let the corresponding user-space
1368 * spin too long if we get preempted.
1373 userpg
->index
= counter
->hw
.idx
;
1374 userpg
->offset
= atomic64_read(&counter
->count
);
1375 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1376 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1385 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1387 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1388 struct perf_mmap_data
*data
;
1389 int ret
= VM_FAULT_SIGBUS
;
1392 data
= rcu_dereference(counter
->data
);
1396 if (vmf
->pgoff
== 0) {
1397 vmf
->page
= virt_to_page(data
->user_page
);
1399 int nr
= vmf
->pgoff
- 1;
1401 if ((unsigned)nr
> data
->nr_pages
)
1404 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1406 get_page(vmf
->page
);
1414 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1416 struct perf_mmap_data
*data
;
1420 WARN_ON(atomic_read(&counter
->mmap_count
));
1422 size
= sizeof(struct perf_mmap_data
);
1423 size
+= nr_pages
* sizeof(void *);
1425 data
= kzalloc(size
, GFP_KERNEL
);
1429 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1430 if (!data
->user_page
)
1431 goto fail_user_page
;
1433 for (i
= 0; i
< nr_pages
; i
++) {
1434 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1435 if (!data
->data_pages
[i
])
1436 goto fail_data_pages
;
1439 data
->nr_pages
= nr_pages
;
1440 atomic_set(&data
->lock
, -1);
1442 rcu_assign_pointer(counter
->data
, data
);
1447 for (i
--; i
>= 0; i
--)
1448 free_page((unsigned long)data
->data_pages
[i
]);
1450 free_page((unsigned long)data
->user_page
);
1459 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1461 struct perf_mmap_data
*data
= container_of(rcu_head
,
1462 struct perf_mmap_data
, rcu_head
);
1465 free_page((unsigned long)data
->user_page
);
1466 for (i
= 0; i
< data
->nr_pages
; i
++)
1467 free_page((unsigned long)data
->data_pages
[i
]);
1471 static void perf_mmap_data_free(struct perf_counter
*counter
)
1473 struct perf_mmap_data
*data
= counter
->data
;
1475 WARN_ON(atomic_read(&counter
->mmap_count
));
1477 rcu_assign_pointer(counter
->data
, NULL
);
1478 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1481 static void perf_mmap_open(struct vm_area_struct
*vma
)
1483 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1485 atomic_inc(&counter
->mmap_count
);
1488 static void perf_mmap_close(struct vm_area_struct
*vma
)
1490 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1492 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1493 &counter
->mmap_mutex
)) {
1494 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1495 perf_mmap_data_free(counter
);
1496 mutex_unlock(&counter
->mmap_mutex
);
1500 static struct vm_operations_struct perf_mmap_vmops
= {
1501 .open
= perf_mmap_open
,
1502 .close
= perf_mmap_close
,
1503 .fault
= perf_mmap_fault
,
1506 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1508 struct perf_counter
*counter
= file
->private_data
;
1509 unsigned long vma_size
;
1510 unsigned long nr_pages
;
1511 unsigned long locked
, lock_limit
;
1515 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1518 vma_size
= vma
->vm_end
- vma
->vm_start
;
1519 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1522 * If we have data pages ensure they're a power-of-two number, so we
1523 * can do bitmasks instead of modulo.
1525 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1528 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1531 if (vma
->vm_pgoff
!= 0)
1534 mutex_lock(&counter
->mmap_mutex
);
1535 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1536 if (nr_pages
!= counter
->data
->nr_pages
)
1541 extra
= nr_pages
/* + 1 only account the data pages */;
1542 extra
-= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1546 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1548 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1549 lock_limit
>>= PAGE_SHIFT
;
1551 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1556 WARN_ON(counter
->data
);
1557 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1561 atomic_set(&counter
->mmap_count
, 1);
1562 vma
->vm_mm
->locked_vm
+= extra
;
1563 counter
->data
->nr_locked
= extra
;
1565 mutex_unlock(&counter
->mmap_mutex
);
1567 vma
->vm_flags
&= ~VM_MAYWRITE
;
1568 vma
->vm_flags
|= VM_RESERVED
;
1569 vma
->vm_ops
= &perf_mmap_vmops
;
1574 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1576 struct perf_counter
*counter
= filp
->private_data
;
1577 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1580 mutex_lock(&inode
->i_mutex
);
1581 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1582 mutex_unlock(&inode
->i_mutex
);
1590 static const struct file_operations perf_fops
= {
1591 .release
= perf_release
,
1594 .unlocked_ioctl
= perf_ioctl
,
1595 .compat_ioctl
= perf_ioctl
,
1597 .fasync
= perf_fasync
,
1601 * Perf counter wakeup
1603 * If there's data, ensure we set the poll() state and publish everything
1604 * to user-space before waking everybody up.
1607 void perf_counter_wakeup(struct perf_counter
*counter
)
1609 wake_up_all(&counter
->waitq
);
1611 if (counter
->pending_kill
) {
1612 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1613 counter
->pending_kill
= 0;
1620 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1622 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1623 * single linked list and use cmpxchg() to add entries lockless.
1626 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1628 struct perf_counter
*counter
= container_of(entry
,
1629 struct perf_counter
, pending
);
1631 if (counter
->pending_disable
) {
1632 counter
->pending_disable
= 0;
1633 perf_counter_disable(counter
);
1636 if (counter
->pending_wakeup
) {
1637 counter
->pending_wakeup
= 0;
1638 perf_counter_wakeup(counter
);
1642 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1644 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1648 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1649 void (*func
)(struct perf_pending_entry
*))
1651 struct perf_pending_entry
**head
;
1653 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1658 head
= &get_cpu_var(perf_pending_head
);
1661 entry
->next
= *head
;
1662 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1664 set_perf_counter_pending();
1666 put_cpu_var(perf_pending_head
);
1669 static int __perf_pending_run(void)
1671 struct perf_pending_entry
*list
;
1674 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1675 while (list
!= PENDING_TAIL
) {
1676 void (*func
)(struct perf_pending_entry
*);
1677 struct perf_pending_entry
*entry
= list
;
1684 * Ensure we observe the unqueue before we issue the wakeup,
1685 * so that we won't be waiting forever.
1686 * -- see perf_not_pending().
1697 static inline int perf_not_pending(struct perf_counter
*counter
)
1700 * If we flush on whatever cpu we run, there is a chance we don't
1704 __perf_pending_run();
1708 * Ensure we see the proper queue state before going to sleep
1709 * so that we do not miss the wakeup. -- see perf_pending_handle()
1712 return counter
->pending
.next
== NULL
;
1715 static void perf_pending_sync(struct perf_counter
*counter
)
1717 wait_event(counter
->waitq
, perf_not_pending(counter
));
1720 void perf_counter_do_pending(void)
1722 __perf_pending_run();
1726 * Callchain support -- arch specific
1729 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1738 struct perf_output_handle
{
1739 struct perf_counter
*counter
;
1740 struct perf_mmap_data
*data
;
1741 unsigned int offset
;
1746 unsigned long flags
;
1749 static void perf_output_wakeup(struct perf_output_handle
*handle
)
1751 atomic_set(&handle
->data
->poll
, POLL_IN
);
1754 handle
->counter
->pending_wakeup
= 1;
1755 perf_pending_queue(&handle
->counter
->pending
,
1756 perf_pending_counter
);
1758 perf_counter_wakeup(handle
->counter
);
1762 * Curious locking construct.
1764 * We need to ensure a later event doesn't publish a head when a former
1765 * event isn't done writing. However since we need to deal with NMIs we
1766 * cannot fully serialize things.
1768 * What we do is serialize between CPUs so we only have to deal with NMI
1769 * nesting on a single CPU.
1771 * We only publish the head (and generate a wakeup) when the outer-most
1774 static void perf_output_lock(struct perf_output_handle
*handle
)
1776 struct perf_mmap_data
*data
= handle
->data
;
1781 local_irq_save(handle
->flags
);
1782 cpu
= smp_processor_id();
1784 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
1787 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1793 static void perf_output_unlock(struct perf_output_handle
*handle
)
1795 struct perf_mmap_data
*data
= handle
->data
;
1798 data
->done_head
= data
->head
;
1800 if (!handle
->locked
)
1805 * The xchg implies a full barrier that ensures all writes are done
1806 * before we publish the new head, matched by a rmb() in userspace when
1807 * reading this position.
1809 while ((head
= atomic_xchg(&data
->done_head
, 0)))
1810 data
->user_page
->data_head
= head
;
1813 * NMI can happen here, which means we can miss a done_head update.
1816 cpu
= atomic_xchg(&data
->lock
, -1);
1817 WARN_ON_ONCE(cpu
!= smp_processor_id());
1820 * Therefore we have to validate we did not indeed do so.
1822 if (unlikely(atomic_read(&data
->done_head
))) {
1824 * Since we had it locked, we can lock it again.
1826 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1832 if (atomic_xchg(&data
->wakeup
, 0))
1833 perf_output_wakeup(handle
);
1835 local_irq_restore(handle
->flags
);
1838 static int perf_output_begin(struct perf_output_handle
*handle
,
1839 struct perf_counter
*counter
, unsigned int size
,
1840 int nmi
, int overflow
)
1842 struct perf_mmap_data
*data
;
1843 unsigned int offset
, head
;
1846 * For inherited counters we send all the output towards the parent.
1848 if (counter
->parent
)
1849 counter
= counter
->parent
;
1852 data
= rcu_dereference(counter
->data
);
1856 handle
->data
= data
;
1857 handle
->counter
= counter
;
1859 handle
->overflow
= overflow
;
1861 if (!data
->nr_pages
)
1864 perf_output_lock(handle
);
1867 offset
= head
= atomic_read(&data
->head
);
1869 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1871 handle
->offset
= offset
;
1872 handle
->head
= head
;
1874 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
1875 atomic_set(&data
->wakeup
, 1);
1880 perf_output_wakeup(handle
);
1887 static void perf_output_copy(struct perf_output_handle
*handle
,
1888 void *buf
, unsigned int len
)
1890 unsigned int pages_mask
;
1891 unsigned int offset
;
1895 offset
= handle
->offset
;
1896 pages_mask
= handle
->data
->nr_pages
- 1;
1897 pages
= handle
->data
->data_pages
;
1900 unsigned int page_offset
;
1903 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1904 page_offset
= offset
& (PAGE_SIZE
- 1);
1905 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1907 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1914 handle
->offset
= offset
;
1916 WARN_ON_ONCE(handle
->offset
> handle
->head
);
1919 #define perf_output_put(handle, x) \
1920 perf_output_copy((handle), &(x), sizeof(x))
1922 static void perf_output_end(struct perf_output_handle
*handle
)
1924 struct perf_counter
*counter
= handle
->counter
;
1925 struct perf_mmap_data
*data
= handle
->data
;
1927 int wakeup_events
= counter
->hw_event
.wakeup_events
;
1929 if (handle
->overflow
&& wakeup_events
) {
1930 int events
= atomic_inc_return(&data
->events
);
1931 if (events
>= wakeup_events
) {
1932 atomic_sub(wakeup_events
, &data
->events
);
1933 atomic_set(&data
->wakeup
, 1);
1937 perf_output_unlock(handle
);
1941 static void perf_counter_output(struct perf_counter
*counter
,
1942 int nmi
, struct pt_regs
*regs
, u64 addr
)
1945 u64 record_type
= counter
->hw_event
.record_type
;
1946 struct perf_output_handle handle
;
1947 struct perf_event_header header
;
1956 struct perf_callchain_entry
*callchain
= NULL
;
1957 int callchain_size
= 0;
1961 header
.size
= sizeof(header
);
1963 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
1964 header
.misc
|= user_mode(regs
) ?
1965 PERF_EVENT_MISC_USER
: PERF_EVENT_MISC_KERNEL
;
1967 if (record_type
& PERF_RECORD_IP
) {
1968 ip
= instruction_pointer(regs
);
1969 header
.type
|= PERF_RECORD_IP
;
1970 header
.size
+= sizeof(ip
);
1973 if (record_type
& PERF_RECORD_TID
) {
1974 /* namespace issues */
1975 tid_entry
.pid
= current
->group_leader
->pid
;
1976 tid_entry
.tid
= current
->pid
;
1978 header
.type
|= PERF_RECORD_TID
;
1979 header
.size
+= sizeof(tid_entry
);
1982 if (record_type
& PERF_RECORD_TIME
) {
1984 * Maybe do better on x86 and provide cpu_clock_nmi()
1986 time
= sched_clock();
1988 header
.type
|= PERF_RECORD_TIME
;
1989 header
.size
+= sizeof(u64
);
1992 if (record_type
& PERF_RECORD_ADDR
) {
1993 header
.type
|= PERF_RECORD_ADDR
;
1994 header
.size
+= sizeof(u64
);
1997 if (record_type
& PERF_RECORD_CONFIG
) {
1998 header
.type
|= PERF_RECORD_CONFIG
;
1999 header
.size
+= sizeof(u64
);
2002 if (record_type
& PERF_RECORD_GROUP
) {
2003 header
.type
|= PERF_RECORD_GROUP
;
2004 header
.size
+= sizeof(u64
) +
2005 counter
->nr_siblings
* sizeof(group_entry
);
2008 if (record_type
& PERF_RECORD_CALLCHAIN
) {
2009 callchain
= perf_callchain(regs
);
2012 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2014 header
.type
|= PERF_RECORD_CALLCHAIN
;
2015 header
.size
+= callchain_size
;
2019 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2023 perf_output_put(&handle
, header
);
2025 if (record_type
& PERF_RECORD_IP
)
2026 perf_output_put(&handle
, ip
);
2028 if (record_type
& PERF_RECORD_TID
)
2029 perf_output_put(&handle
, tid_entry
);
2031 if (record_type
& PERF_RECORD_TIME
)
2032 perf_output_put(&handle
, time
);
2034 if (record_type
& PERF_RECORD_ADDR
)
2035 perf_output_put(&handle
, addr
);
2037 if (record_type
& PERF_RECORD_CONFIG
)
2038 perf_output_put(&handle
, counter
->hw_event
.config
);
2041 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2043 if (record_type
& PERF_RECORD_GROUP
) {
2044 struct perf_counter
*leader
, *sub
;
2045 u64 nr
= counter
->nr_siblings
;
2047 perf_output_put(&handle
, nr
);
2049 leader
= counter
->group_leader
;
2050 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2052 sub
->pmu
->read(sub
);
2054 group_entry
.event
= sub
->hw_event
.config
;
2055 group_entry
.counter
= atomic64_read(&sub
->count
);
2057 perf_output_put(&handle
, group_entry
);
2062 perf_output_copy(&handle
, callchain
, callchain_size
);
2064 perf_output_end(&handle
);
2071 struct perf_comm_event
{
2072 struct task_struct
*task
;
2077 struct perf_event_header header
;
2084 static void perf_counter_comm_output(struct perf_counter
*counter
,
2085 struct perf_comm_event
*comm_event
)
2087 struct perf_output_handle handle
;
2088 int size
= comm_event
->event
.header
.size
;
2089 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2094 perf_output_put(&handle
, comm_event
->event
);
2095 perf_output_copy(&handle
, comm_event
->comm
,
2096 comm_event
->comm_size
);
2097 perf_output_end(&handle
);
2100 static int perf_counter_comm_match(struct perf_counter
*counter
,
2101 struct perf_comm_event
*comm_event
)
2103 if (counter
->hw_event
.comm
&&
2104 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2110 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2111 struct perf_comm_event
*comm_event
)
2113 struct perf_counter
*counter
;
2115 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2119 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2120 if (perf_counter_comm_match(counter
, comm_event
))
2121 perf_counter_comm_output(counter
, comm_event
);
2126 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2128 struct perf_cpu_context
*cpuctx
;
2130 char *comm
= comm_event
->task
->comm
;
2132 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2134 comm_event
->comm
= comm
;
2135 comm_event
->comm_size
= size
;
2137 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2139 cpuctx
= &get_cpu_var(perf_cpu_context
);
2140 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2141 put_cpu_var(perf_cpu_context
);
2143 perf_counter_comm_ctx(¤t
->perf_counter_ctx
, comm_event
);
2146 void perf_counter_comm(struct task_struct
*task
)
2148 struct perf_comm_event comm_event
;
2150 if (!atomic_read(&nr_comm_tracking
))
2153 comm_event
= (struct perf_comm_event
){
2156 .header
= { .type
= PERF_EVENT_COMM
, },
2157 .pid
= task
->group_leader
->pid
,
2162 perf_counter_comm_event(&comm_event
);
2169 struct perf_mmap_event
{
2175 struct perf_event_header header
;
2185 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2186 struct perf_mmap_event
*mmap_event
)
2188 struct perf_output_handle handle
;
2189 int size
= mmap_event
->event
.header
.size
;
2190 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2195 perf_output_put(&handle
, mmap_event
->event
);
2196 perf_output_copy(&handle
, mmap_event
->file_name
,
2197 mmap_event
->file_size
);
2198 perf_output_end(&handle
);
2201 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2202 struct perf_mmap_event
*mmap_event
)
2204 if (counter
->hw_event
.mmap
&&
2205 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2208 if (counter
->hw_event
.munmap
&&
2209 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2215 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2216 struct perf_mmap_event
*mmap_event
)
2218 struct perf_counter
*counter
;
2220 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2224 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2225 if (perf_counter_mmap_match(counter
, mmap_event
))
2226 perf_counter_mmap_output(counter
, mmap_event
);
2231 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2233 struct perf_cpu_context
*cpuctx
;
2234 struct file
*file
= mmap_event
->file
;
2241 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2243 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2246 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2248 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2252 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2257 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2259 mmap_event
->file_name
= name
;
2260 mmap_event
->file_size
= size
;
2262 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2264 cpuctx
= &get_cpu_var(perf_cpu_context
);
2265 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2266 put_cpu_var(perf_cpu_context
);
2268 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2273 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2274 unsigned long pgoff
, struct file
*file
)
2276 struct perf_mmap_event mmap_event
;
2278 if (!atomic_read(&nr_mmap_tracking
))
2281 mmap_event
= (struct perf_mmap_event
){
2284 .header
= { .type
= PERF_EVENT_MMAP
, },
2285 .pid
= current
->group_leader
->pid
,
2286 .tid
= current
->pid
,
2293 perf_counter_mmap_event(&mmap_event
);
2296 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2297 unsigned long pgoff
, struct file
*file
)
2299 struct perf_mmap_event mmap_event
;
2301 if (!atomic_read(&nr_munmap_tracking
))
2304 mmap_event
= (struct perf_mmap_event
){
2307 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2308 .pid
= current
->group_leader
->pid
,
2309 .tid
= current
->pid
,
2316 perf_counter_mmap_event(&mmap_event
);
2320 * Generic counter overflow handling.
2323 int perf_counter_overflow(struct perf_counter
*counter
,
2324 int nmi
, struct pt_regs
*regs
, u64 addr
)
2326 int events
= atomic_read(&counter
->event_limit
);
2330 * XXX event_limit might not quite work as expected on inherited
2334 counter
->pending_kill
= POLL_IN
;
2335 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2337 counter
->pending_kill
= POLL_HUP
;
2339 counter
->pending_disable
= 1;
2340 perf_pending_queue(&counter
->pending
,
2341 perf_pending_counter
);
2343 perf_counter_disable(counter
);
2346 perf_counter_output(counter
, nmi
, regs
, addr
);
2351 * Generic software counter infrastructure
2354 static void perf_swcounter_update(struct perf_counter
*counter
)
2356 struct hw_perf_counter
*hwc
= &counter
->hw
;
2361 prev
= atomic64_read(&hwc
->prev_count
);
2362 now
= atomic64_read(&hwc
->count
);
2363 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2368 atomic64_add(delta
, &counter
->count
);
2369 atomic64_sub(delta
, &hwc
->period_left
);
2372 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2374 struct hw_perf_counter
*hwc
= &counter
->hw
;
2375 s64 left
= atomic64_read(&hwc
->period_left
);
2376 s64 period
= hwc
->irq_period
;
2378 if (unlikely(left
<= -period
)) {
2380 atomic64_set(&hwc
->period_left
, left
);
2383 if (unlikely(left
<= 0)) {
2385 atomic64_add(period
, &hwc
->period_left
);
2388 atomic64_set(&hwc
->prev_count
, -left
);
2389 atomic64_set(&hwc
->count
, -left
);
2392 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2394 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2395 struct perf_counter
*counter
;
2396 struct pt_regs
*regs
;
2398 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2399 counter
->pmu
->read(counter
);
2401 regs
= get_irq_regs();
2403 * In case we exclude kernel IPs or are somehow not in interrupt
2404 * context, provide the next best thing, the user IP.
2406 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2407 !counter
->hw_event
.exclude_user
)
2408 regs
= task_pt_regs(current
);
2411 if (perf_counter_overflow(counter
, 0, regs
, 0))
2412 ret
= HRTIMER_NORESTART
;
2415 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
2420 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2421 int nmi
, struct pt_regs
*regs
, u64 addr
)
2423 perf_swcounter_update(counter
);
2424 perf_swcounter_set_period(counter
);
2425 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2426 /* soft-disable the counter */
2431 static int perf_swcounter_match(struct perf_counter
*counter
,
2432 enum perf_event_types type
,
2433 u32 event
, struct pt_regs
*regs
)
2435 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2438 if (perf_event_raw(&counter
->hw_event
))
2441 if (perf_event_type(&counter
->hw_event
) != type
)
2444 if (perf_event_id(&counter
->hw_event
) != event
)
2447 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2450 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2456 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2457 int nmi
, struct pt_regs
*regs
, u64 addr
)
2459 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2460 if (counter
->hw
.irq_period
&& !neg
)
2461 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2464 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2465 enum perf_event_types type
, u32 event
,
2466 u64 nr
, int nmi
, struct pt_regs
*regs
,
2469 struct perf_counter
*counter
;
2471 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2475 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2476 if (perf_swcounter_match(counter
, type
, event
, regs
))
2477 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2482 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2485 return &cpuctx
->recursion
[3];
2488 return &cpuctx
->recursion
[2];
2491 return &cpuctx
->recursion
[1];
2493 return &cpuctx
->recursion
[0];
2496 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2497 u64 nr
, int nmi
, struct pt_regs
*regs
,
2500 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2501 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2509 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2510 nr
, nmi
, regs
, addr
);
2511 if (cpuctx
->task_ctx
) {
2512 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2513 nr
, nmi
, regs
, addr
);
2520 put_cpu_var(perf_cpu_context
);
2524 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2526 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2529 static void perf_swcounter_read(struct perf_counter
*counter
)
2531 perf_swcounter_update(counter
);
2534 static int perf_swcounter_enable(struct perf_counter
*counter
)
2536 perf_swcounter_set_period(counter
);
2540 static void perf_swcounter_disable(struct perf_counter
*counter
)
2542 perf_swcounter_update(counter
);
2545 static const struct pmu perf_ops_generic
= {
2546 .enable
= perf_swcounter_enable
,
2547 .disable
= perf_swcounter_disable
,
2548 .read
= perf_swcounter_read
,
2552 * Software counter: cpu wall time clock
2555 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2557 int cpu
= raw_smp_processor_id();
2561 now
= cpu_clock(cpu
);
2562 prev
= atomic64_read(&counter
->hw
.prev_count
);
2563 atomic64_set(&counter
->hw
.prev_count
, now
);
2564 atomic64_add(now
- prev
, &counter
->count
);
2567 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2569 struct hw_perf_counter
*hwc
= &counter
->hw
;
2570 int cpu
= raw_smp_processor_id();
2572 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2573 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2574 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2575 if (hwc
->irq_period
) {
2576 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2577 ns_to_ktime(hwc
->irq_period
), 0,
2578 HRTIMER_MODE_REL
, 0);
2584 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2586 hrtimer_cancel(&counter
->hw
.hrtimer
);
2587 cpu_clock_perf_counter_update(counter
);
2590 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2592 cpu_clock_perf_counter_update(counter
);
2595 static const struct pmu perf_ops_cpu_clock
= {
2596 .enable
= cpu_clock_perf_counter_enable
,
2597 .disable
= cpu_clock_perf_counter_disable
,
2598 .read
= cpu_clock_perf_counter_read
,
2602 * Software counter: task time clock
2605 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2610 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2612 atomic64_add(delta
, &counter
->count
);
2615 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2617 struct hw_perf_counter
*hwc
= &counter
->hw
;
2620 now
= counter
->ctx
->time
;
2622 atomic64_set(&hwc
->prev_count
, now
);
2623 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2624 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2625 if (hwc
->irq_period
) {
2626 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2627 ns_to_ktime(hwc
->irq_period
), 0,
2628 HRTIMER_MODE_REL
, 0);
2634 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2636 hrtimer_cancel(&counter
->hw
.hrtimer
);
2637 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
2641 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2646 update_context_time(counter
->ctx
);
2647 time
= counter
->ctx
->time
;
2649 u64 now
= perf_clock();
2650 u64 delta
= now
- counter
->ctx
->timestamp
;
2651 time
= counter
->ctx
->time
+ delta
;
2654 task_clock_perf_counter_update(counter
, time
);
2657 static const struct pmu perf_ops_task_clock
= {
2658 .enable
= task_clock_perf_counter_enable
,
2659 .disable
= task_clock_perf_counter_disable
,
2660 .read
= task_clock_perf_counter_read
,
2664 * Software counter: cpu migrations
2667 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2669 struct task_struct
*curr
= counter
->ctx
->task
;
2672 return curr
->se
.nr_migrations
;
2673 return cpu_nr_migrations(smp_processor_id());
2676 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2681 prev
= atomic64_read(&counter
->hw
.prev_count
);
2682 now
= get_cpu_migrations(counter
);
2684 atomic64_set(&counter
->hw
.prev_count
, now
);
2688 atomic64_add(delta
, &counter
->count
);
2691 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2693 cpu_migrations_perf_counter_update(counter
);
2696 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2698 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2699 atomic64_set(&counter
->hw
.prev_count
,
2700 get_cpu_migrations(counter
));
2704 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2706 cpu_migrations_perf_counter_update(counter
);
2709 static const struct pmu perf_ops_cpu_migrations
= {
2710 .enable
= cpu_migrations_perf_counter_enable
,
2711 .disable
= cpu_migrations_perf_counter_disable
,
2712 .read
= cpu_migrations_perf_counter_read
,
2715 #ifdef CONFIG_EVENT_PROFILE
2716 void perf_tpcounter_event(int event_id
)
2718 struct pt_regs
*regs
= get_irq_regs();
2721 regs
= task_pt_regs(current
);
2723 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
2725 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
2727 extern int ftrace_profile_enable(int);
2728 extern void ftrace_profile_disable(int);
2730 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2732 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2735 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2737 int event_id
= perf_event_id(&counter
->hw_event
);
2740 ret
= ftrace_profile_enable(event_id
);
2744 counter
->destroy
= tp_perf_counter_destroy
;
2745 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2747 return &perf_ops_generic
;
2750 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2756 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
2758 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2759 const struct pmu
*pmu
= NULL
;
2760 struct hw_perf_counter
*hwc
= &counter
->hw
;
2763 * Software counters (currently) can't in general distinguish
2764 * between user, kernel and hypervisor events.
2765 * However, context switches and cpu migrations are considered
2766 * to be kernel events, and page faults are never hypervisor
2769 switch (perf_event_id(&counter
->hw_event
)) {
2770 case PERF_COUNT_CPU_CLOCK
:
2771 pmu
= &perf_ops_cpu_clock
;
2773 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2774 hw_event
->irq_period
= 10000;
2776 case PERF_COUNT_TASK_CLOCK
:
2778 * If the user instantiates this as a per-cpu counter,
2779 * use the cpu_clock counter instead.
2781 if (counter
->ctx
->task
)
2782 pmu
= &perf_ops_task_clock
;
2784 pmu
= &perf_ops_cpu_clock
;
2786 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2787 hw_event
->irq_period
= 10000;
2789 case PERF_COUNT_PAGE_FAULTS
:
2790 case PERF_COUNT_PAGE_FAULTS_MIN
:
2791 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2792 case PERF_COUNT_CONTEXT_SWITCHES
:
2793 pmu
= &perf_ops_generic
;
2795 case PERF_COUNT_CPU_MIGRATIONS
:
2796 if (!counter
->hw_event
.exclude_kernel
)
2797 pmu
= &perf_ops_cpu_migrations
;
2802 hwc
->irq_period
= hw_event
->irq_period
;
2808 * Allocate and initialize a counter structure
2810 static struct perf_counter
*
2811 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2813 struct perf_counter_context
*ctx
,
2814 struct perf_counter
*group_leader
,
2817 const struct pmu
*pmu
;
2818 struct perf_counter
*counter
;
2821 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2823 return ERR_PTR(-ENOMEM
);
2826 * Single counters are their own group leaders, with an
2827 * empty sibling list:
2830 group_leader
= counter
;
2832 mutex_init(&counter
->mutex
);
2833 INIT_LIST_HEAD(&counter
->list_entry
);
2834 INIT_LIST_HEAD(&counter
->event_entry
);
2835 INIT_LIST_HEAD(&counter
->sibling_list
);
2836 init_waitqueue_head(&counter
->waitq
);
2838 mutex_init(&counter
->mmap_mutex
);
2840 INIT_LIST_HEAD(&counter
->child_list
);
2843 counter
->hw_event
= *hw_event
;
2844 counter
->group_leader
= group_leader
;
2845 counter
->pmu
= NULL
;
2848 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2849 if (hw_event
->disabled
)
2850 counter
->state
= PERF_COUNTER_STATE_OFF
;
2855 * we currently do not support PERF_RECORD_GROUP on inherited counters
2857 if (hw_event
->inherit
&& (hw_event
->record_type
& PERF_RECORD_GROUP
))
2860 if (perf_event_raw(hw_event
)) {
2861 pmu
= hw_perf_counter_init(counter
);
2865 switch (perf_event_type(hw_event
)) {
2866 case PERF_TYPE_HARDWARE
:
2867 pmu
= hw_perf_counter_init(counter
);
2870 case PERF_TYPE_SOFTWARE
:
2871 pmu
= sw_perf_counter_init(counter
);
2874 case PERF_TYPE_TRACEPOINT
:
2875 pmu
= tp_perf_counter_init(counter
);
2882 else if (IS_ERR(pmu
))
2887 return ERR_PTR(err
);
2892 atomic_inc(&nr_counters
);
2893 if (counter
->hw_event
.mmap
)
2894 atomic_inc(&nr_mmap_tracking
);
2895 if (counter
->hw_event
.munmap
)
2896 atomic_inc(&nr_munmap_tracking
);
2897 if (counter
->hw_event
.comm
)
2898 atomic_inc(&nr_comm_tracking
);
2904 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2906 * @hw_event_uptr: event type attributes for monitoring/sampling
2909 * @group_fd: group leader counter fd
2911 SYSCALL_DEFINE5(perf_counter_open
,
2912 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2913 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2915 struct perf_counter
*counter
, *group_leader
;
2916 struct perf_counter_hw_event hw_event
;
2917 struct perf_counter_context
*ctx
;
2918 struct file
*counter_file
= NULL
;
2919 struct file
*group_file
= NULL
;
2920 int fput_needed
= 0;
2921 int fput_needed2
= 0;
2924 /* for future expandability... */
2928 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2932 * Get the target context (task or percpu):
2934 ctx
= find_get_context(pid
, cpu
);
2936 return PTR_ERR(ctx
);
2939 * Look up the group leader (we will attach this counter to it):
2941 group_leader
= NULL
;
2942 if (group_fd
!= -1) {
2944 group_file
= fget_light(group_fd
, &fput_needed
);
2946 goto err_put_context
;
2947 if (group_file
->f_op
!= &perf_fops
)
2948 goto err_put_context
;
2950 group_leader
= group_file
->private_data
;
2952 * Do not allow a recursive hierarchy (this new sibling
2953 * becoming part of another group-sibling):
2955 if (group_leader
->group_leader
!= group_leader
)
2956 goto err_put_context
;
2958 * Do not allow to attach to a group in a different
2959 * task or CPU context:
2961 if (group_leader
->ctx
!= ctx
)
2962 goto err_put_context
;
2964 * Only a group leader can be exclusive or pinned
2966 if (hw_event
.exclusive
|| hw_event
.pinned
)
2967 goto err_put_context
;
2970 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2972 ret
= PTR_ERR(counter
);
2973 if (IS_ERR(counter
))
2974 goto err_put_context
;
2976 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2978 goto err_free_put_context
;
2980 counter_file
= fget_light(ret
, &fput_needed2
);
2982 goto err_free_put_context
;
2984 counter
->filp
= counter_file
;
2985 mutex_lock(&ctx
->mutex
);
2986 perf_install_in_context(ctx
, counter
, cpu
);
2987 mutex_unlock(&ctx
->mutex
);
2989 fput_light(counter_file
, fput_needed2
);
2992 fput_light(group_file
, fput_needed
);
2996 err_free_put_context
:
3006 * Initialize the perf_counter context in a task_struct:
3009 __perf_counter_init_context(struct perf_counter_context
*ctx
,
3010 struct task_struct
*task
)
3012 memset(ctx
, 0, sizeof(*ctx
));
3013 spin_lock_init(&ctx
->lock
);
3014 mutex_init(&ctx
->mutex
);
3015 INIT_LIST_HEAD(&ctx
->counter_list
);
3016 INIT_LIST_HEAD(&ctx
->event_list
);
3021 * inherit a counter from parent task to child task:
3023 static struct perf_counter
*
3024 inherit_counter(struct perf_counter
*parent_counter
,
3025 struct task_struct
*parent
,
3026 struct perf_counter_context
*parent_ctx
,
3027 struct task_struct
*child
,
3028 struct perf_counter
*group_leader
,
3029 struct perf_counter_context
*child_ctx
)
3031 struct perf_counter
*child_counter
;
3034 * Instead of creating recursive hierarchies of counters,
3035 * we link inherited counters back to the original parent,
3036 * which has a filp for sure, which we use as the reference
3039 if (parent_counter
->parent
)
3040 parent_counter
= parent_counter
->parent
;
3042 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
3043 parent_counter
->cpu
, child_ctx
,
3044 group_leader
, GFP_KERNEL
);
3045 if (IS_ERR(child_counter
))
3046 return child_counter
;
3049 * Link it up in the child's context:
3051 child_counter
->task
= child
;
3052 add_counter_to_ctx(child_counter
, child_ctx
);
3054 child_counter
->parent
= parent_counter
;
3056 * inherit into child's child as well:
3058 child_counter
->hw_event
.inherit
= 1;
3061 * Get a reference to the parent filp - we will fput it
3062 * when the child counter exits. This is safe to do because
3063 * we are in the parent and we know that the filp still
3064 * exists and has a nonzero count:
3066 atomic_long_inc(&parent_counter
->filp
->f_count
);
3069 * Link this into the parent counter's child list
3071 mutex_lock(&parent_counter
->mutex
);
3072 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3075 * Make the child state follow the state of the parent counter,
3076 * not its hw_event.disabled bit. We hold the parent's mutex,
3077 * so we won't race with perf_counter_{en,dis}able_family.
3079 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3080 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3082 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3084 mutex_unlock(&parent_counter
->mutex
);
3086 return child_counter
;
3089 static int inherit_group(struct perf_counter
*parent_counter
,
3090 struct task_struct
*parent
,
3091 struct perf_counter_context
*parent_ctx
,
3092 struct task_struct
*child
,
3093 struct perf_counter_context
*child_ctx
)
3095 struct perf_counter
*leader
;
3096 struct perf_counter
*sub
;
3097 struct perf_counter
*child_ctr
;
3099 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3100 child
, NULL
, child_ctx
);
3102 return PTR_ERR(leader
);
3103 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3104 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3105 child
, leader
, child_ctx
);
3106 if (IS_ERR(child_ctr
))
3107 return PTR_ERR(child_ctr
);
3112 static void sync_child_counter(struct perf_counter
*child_counter
,
3113 struct perf_counter
*parent_counter
)
3115 u64 parent_val
, child_val
;
3117 parent_val
= atomic64_read(&parent_counter
->count
);
3118 child_val
= atomic64_read(&child_counter
->count
);
3121 * Add back the child's count to the parent's count:
3123 atomic64_add(child_val
, &parent_counter
->count
);
3124 atomic64_add(child_counter
->total_time_enabled
,
3125 &parent_counter
->child_total_time_enabled
);
3126 atomic64_add(child_counter
->total_time_running
,
3127 &parent_counter
->child_total_time_running
);
3130 * Remove this counter from the parent's list
3132 mutex_lock(&parent_counter
->mutex
);
3133 list_del_init(&child_counter
->child_list
);
3134 mutex_unlock(&parent_counter
->mutex
);
3137 * Release the parent counter, if this was the last
3140 fput(parent_counter
->filp
);
3144 __perf_counter_exit_task(struct task_struct
*child
,
3145 struct perf_counter
*child_counter
,
3146 struct perf_counter_context
*child_ctx
)
3148 struct perf_counter
*parent_counter
;
3149 struct perf_counter
*sub
, *tmp
;
3152 * If we do not self-reap then we have to wait for the
3153 * child task to unschedule (it will happen for sure),
3154 * so that its counter is at its final count. (This
3155 * condition triggers rarely - child tasks usually get
3156 * off their CPU before the parent has a chance to
3157 * get this far into the reaping action)
3159 if (child
!= current
) {
3160 wait_task_inactive(child
, 0);
3161 list_del_init(&child_counter
->list_entry
);
3162 update_counter_times(child_counter
);
3164 struct perf_cpu_context
*cpuctx
;
3165 unsigned long flags
;
3169 * Disable and unlink this counter.
3171 * Be careful about zapping the list - IRQ/NMI context
3172 * could still be processing it:
3174 local_irq_save(flags
);
3175 perf_flags
= hw_perf_save_disable();
3177 cpuctx
= &__get_cpu_var(perf_cpu_context
);
3179 group_sched_out(child_counter
, cpuctx
, child_ctx
);
3180 update_counter_times(child_counter
);
3182 list_del_init(&child_counter
->list_entry
);
3184 child_ctx
->nr_counters
--;
3186 hw_perf_restore(perf_flags
);
3187 local_irq_restore(flags
);
3190 parent_counter
= child_counter
->parent
;
3192 * It can happen that parent exits first, and has counters
3193 * that are still around due to the child reference. These
3194 * counters need to be zapped - but otherwise linger.
3196 if (parent_counter
) {
3197 sync_child_counter(child_counter
, parent_counter
);
3198 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
3201 sync_child_counter(sub
, sub
->parent
);
3205 free_counter(child_counter
);
3210 * When a child task exits, feed back counter values to parent counters.
3212 * Note: we may be running in child context, but the PID is not hashed
3213 * anymore so new counters will not be added.
3215 void perf_counter_exit_task(struct task_struct
*child
)
3217 struct perf_counter
*child_counter
, *tmp
;
3218 struct perf_counter_context
*child_ctx
;
3220 child_ctx
= &child
->perf_counter_ctx
;
3222 if (likely(!child_ctx
->nr_counters
))
3225 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3227 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3231 * Initialize the perf_counter context in task_struct
3233 void perf_counter_init_task(struct task_struct
*child
)
3235 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3236 struct perf_counter
*counter
;
3237 struct task_struct
*parent
= current
;
3239 child_ctx
= &child
->perf_counter_ctx
;
3240 parent_ctx
= &parent
->perf_counter_ctx
;
3242 __perf_counter_init_context(child_ctx
, child
);
3245 * This is executed from the parent task context, so inherit
3246 * counters that have been marked for cloning:
3249 if (likely(!parent_ctx
->nr_counters
))
3253 * Lock the parent list. No need to lock the child - not PID
3254 * hashed yet and not running, so nobody can access it.
3256 mutex_lock(&parent_ctx
->mutex
);
3259 * We dont have to disable NMIs - we are only looking at
3260 * the list, not manipulating it:
3262 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
3263 if (!counter
->hw_event
.inherit
)
3266 if (inherit_group(counter
, parent
,
3267 parent_ctx
, child
, child_ctx
))
3271 mutex_unlock(&parent_ctx
->mutex
);
3274 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3276 struct perf_cpu_context
*cpuctx
;
3278 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3279 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3281 spin_lock(&perf_resource_lock
);
3282 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3283 spin_unlock(&perf_resource_lock
);
3285 hw_perf_counter_setup(cpu
);
3288 #ifdef CONFIG_HOTPLUG_CPU
3289 static void __perf_counter_exit_cpu(void *info
)
3291 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3292 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3293 struct perf_counter
*counter
, *tmp
;
3295 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3296 __perf_counter_remove_from_context(counter
);
3298 static void perf_counter_exit_cpu(int cpu
)
3300 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3301 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3303 mutex_lock(&ctx
->mutex
);
3304 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3305 mutex_unlock(&ctx
->mutex
);
3308 static inline void perf_counter_exit_cpu(int cpu
) { }
3311 static int __cpuinit
3312 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3314 unsigned int cpu
= (long)hcpu
;
3318 case CPU_UP_PREPARE
:
3319 case CPU_UP_PREPARE_FROZEN
:
3320 perf_counter_init_cpu(cpu
);
3323 case CPU_DOWN_PREPARE
:
3324 case CPU_DOWN_PREPARE_FROZEN
:
3325 perf_counter_exit_cpu(cpu
);
3335 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3336 .notifier_call
= perf_cpu_notify
,
3339 void __init
perf_counter_init(void)
3341 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3342 (void *)(long)smp_processor_id());
3343 register_cpu_notifier(&perf_cpu_nb
);
3346 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3348 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3352 perf_set_reserve_percpu(struct sysdev_class
*class,
3356 struct perf_cpu_context
*cpuctx
;
3360 err
= strict_strtoul(buf
, 10, &val
);
3363 if (val
> perf_max_counters
)
3366 spin_lock(&perf_resource_lock
);
3367 perf_reserved_percpu
= val
;
3368 for_each_online_cpu(cpu
) {
3369 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3370 spin_lock_irq(&cpuctx
->ctx
.lock
);
3371 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3372 perf_max_counters
- perf_reserved_percpu
);
3373 cpuctx
->max_pertask
= mpt
;
3374 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3376 spin_unlock(&perf_resource_lock
);
3381 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3383 return sprintf(buf
, "%d\n", perf_overcommit
);
3387 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3392 err
= strict_strtoul(buf
, 10, &val
);
3398 spin_lock(&perf_resource_lock
);
3399 perf_overcommit
= val
;
3400 spin_unlock(&perf_resource_lock
);
3405 static SYSDEV_CLASS_ATTR(
3408 perf_show_reserve_percpu
,
3409 perf_set_reserve_percpu
3412 static SYSDEV_CLASS_ATTR(
3415 perf_show_overcommit
,
3419 static struct attribute
*perfclass_attrs
[] = {
3420 &attr_reserve_percpu
.attr
,
3421 &attr_overcommit
.attr
,
3425 static struct attribute_group perfclass_attr_group
= {
3426 .attrs
= perfclass_attrs
,
3427 .name
= "perf_counters",
3430 static int __init
perf_counter_sysfs_init(void)
3432 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3433 &perfclass_attr_group
);
3435 device_initcall(perf_counter_sysfs_init
);