2 * Performance counter core code
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
8 * For licensing details see kernel-base/COPYING
13 #include <linux/cpu.h>
14 #include <linux/smp.h>
15 #include <linux/file.h>
16 #include <linux/poll.h>
17 #include <linux/sysfs.h>
18 #include <linux/ptrace.h>
19 #include <linux/percpu.h>
20 #include <linux/vmstat.h>
21 #include <linux/hardirq.h>
22 #include <linux/rculist.h>
23 #include <linux/uaccess.h>
24 #include <linux/syscalls.h>
25 #include <linux/anon_inodes.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/perf_counter.h>
28 #include <linux/dcache.h>
30 #include <asm/irq_regs.h>
33 * Each CPU has a list of per CPU counters:
35 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
37 int perf_max_counters __read_mostly
= 1;
38 static int perf_reserved_percpu __read_mostly
;
39 static int perf_overcommit __read_mostly
= 1;
42 * Mutex for (sysadmin-configurable) counter reservations:
44 static DEFINE_MUTEX(perf_resource_mutex
);
47 * Architecture provided APIs - weak aliases:
49 extern __weak
const struct hw_perf_counter_ops
*
50 hw_perf_counter_init(struct perf_counter
*counter
)
55 u64 __weak
hw_perf_save_disable(void) { return 0; }
56 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
57 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
58 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
59 struct perf_cpu_context
*cpuctx
,
60 struct perf_counter_context
*ctx
, int cpu
)
65 void __weak
perf_counter_print_debug(void) { }
68 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
70 struct perf_counter
*group_leader
= counter
->group_leader
;
73 * Depending on whether it is a standalone or sibling counter,
74 * add it straight to the context's counter list, or to the group
75 * leader's sibling list:
77 if (counter
->group_leader
== counter
)
78 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
80 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
81 group_leader
->nr_siblings
++;
84 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
88 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
90 struct perf_counter
*sibling
, *tmp
;
92 list_del_init(&counter
->list_entry
);
93 list_del_rcu(&counter
->event_entry
);
95 if (counter
->group_leader
!= counter
)
96 counter
->group_leader
->nr_siblings
--;
99 * If this was a group counter with sibling counters then
100 * upgrade the siblings to singleton counters by adding them
101 * to the context list directly:
103 list_for_each_entry_safe(sibling
, tmp
,
104 &counter
->sibling_list
, list_entry
) {
106 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
107 sibling
->group_leader
= sibling
;
112 counter_sched_out(struct perf_counter
*counter
,
113 struct perf_cpu_context
*cpuctx
,
114 struct perf_counter_context
*ctx
)
116 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
119 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
120 counter
->tstamp_stopped
= ctx
->time
;
121 counter
->hw_ops
->disable(counter
);
124 if (!is_software_counter(counter
))
125 cpuctx
->active_oncpu
--;
127 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
128 cpuctx
->exclusive
= 0;
132 group_sched_out(struct perf_counter
*group_counter
,
133 struct perf_cpu_context
*cpuctx
,
134 struct perf_counter_context
*ctx
)
136 struct perf_counter
*counter
;
138 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
141 counter_sched_out(group_counter
, cpuctx
, ctx
);
144 * Schedule out siblings (if any):
146 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
147 counter_sched_out(counter
, cpuctx
, ctx
);
149 if (group_counter
->hw_event
.exclusive
)
150 cpuctx
->exclusive
= 0;
154 * Cross CPU call to remove a performance counter
156 * We disable the counter on the hardware level first. After that we
157 * remove it from the context list.
159 static void __perf_counter_remove_from_context(void *info
)
161 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
162 struct perf_counter
*counter
= info
;
163 struct perf_counter_context
*ctx
= counter
->ctx
;
168 * If this is a task context, we need to check whether it is
169 * the current task context of this cpu. If not it has been
170 * scheduled out before the smp call arrived.
172 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
175 spin_lock_irqsave(&ctx
->lock
, flags
);
177 counter_sched_out(counter
, cpuctx
, ctx
);
179 counter
->task
= NULL
;
183 * Protect the list operation against NMI by disabling the
184 * counters on a global level. NOP for non NMI based counters.
186 perf_flags
= hw_perf_save_disable();
187 list_del_counter(counter
, ctx
);
188 hw_perf_restore(perf_flags
);
192 * Allow more per task counters with respect to the
195 cpuctx
->max_pertask
=
196 min(perf_max_counters
- ctx
->nr_counters
,
197 perf_max_counters
- perf_reserved_percpu
);
200 spin_unlock_irqrestore(&ctx
->lock
, flags
);
205 * Remove the counter from a task's (or a CPU's) list of counters.
207 * Must be called with counter->mutex and ctx->mutex held.
209 * CPU counters are removed with a smp call. For task counters we only
210 * call when the task is on a CPU.
212 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
214 struct perf_counter_context
*ctx
= counter
->ctx
;
215 struct task_struct
*task
= ctx
->task
;
219 * Per cpu counters are removed via an smp call and
220 * the removal is always sucessful.
222 smp_call_function_single(counter
->cpu
,
223 __perf_counter_remove_from_context
,
229 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
232 spin_lock_irq(&ctx
->lock
);
234 * If the context is active we need to retry the smp call.
236 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
237 spin_unlock_irq(&ctx
->lock
);
242 * The lock prevents that this context is scheduled in so we
243 * can remove the counter safely, if the call above did not
246 if (!list_empty(&counter
->list_entry
)) {
248 list_del_counter(counter
, ctx
);
249 counter
->task
= NULL
;
251 spin_unlock_irq(&ctx
->lock
);
254 static inline u64
perf_clock(void)
256 return cpu_clock(smp_processor_id());
260 * Update the record of the current time in a context.
262 static void update_context_time(struct perf_counter_context
*ctx
)
264 u64 now
= perf_clock();
266 ctx
->time
+= now
- ctx
->timestamp
;
267 ctx
->timestamp
= now
;
271 * Update the total_time_enabled and total_time_running fields for a counter.
273 static void update_counter_times(struct perf_counter
*counter
)
275 struct perf_counter_context
*ctx
= counter
->ctx
;
278 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
281 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
283 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
284 run_end
= counter
->tstamp_stopped
;
288 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
292 * Update total_time_enabled and total_time_running for all counters in a group.
294 static void update_group_times(struct perf_counter
*leader
)
296 struct perf_counter
*counter
;
298 update_counter_times(leader
);
299 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
300 update_counter_times(counter
);
304 * Cross CPU call to disable a performance counter
306 static void __perf_counter_disable(void *info
)
308 struct perf_counter
*counter
= info
;
309 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
310 struct perf_counter_context
*ctx
= counter
->ctx
;
314 * If this is a per-task counter, need to check whether this
315 * counter's task is the current task on this cpu.
317 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
320 spin_lock_irqsave(&ctx
->lock
, flags
);
323 * If the counter is on, turn it off.
324 * If it is in error state, leave it in error state.
326 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
327 update_context_time(ctx
);
328 update_counter_times(counter
);
329 if (counter
== counter
->group_leader
)
330 group_sched_out(counter
, cpuctx
, ctx
);
332 counter_sched_out(counter
, cpuctx
, ctx
);
333 counter
->state
= PERF_COUNTER_STATE_OFF
;
336 spin_unlock_irqrestore(&ctx
->lock
, flags
);
342 static void perf_counter_disable(struct perf_counter
*counter
)
344 struct perf_counter_context
*ctx
= counter
->ctx
;
345 struct task_struct
*task
= ctx
->task
;
349 * Disable the counter on the cpu that it's on
351 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
357 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
359 spin_lock_irq(&ctx
->lock
);
361 * If the counter is still active, we need to retry the cross-call.
363 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
364 spin_unlock_irq(&ctx
->lock
);
369 * Since we have the lock this context can't be scheduled
370 * in, so we can change the state safely.
372 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
373 update_counter_times(counter
);
374 counter
->state
= PERF_COUNTER_STATE_OFF
;
377 spin_unlock_irq(&ctx
->lock
);
381 * Disable a counter and all its children.
383 static void perf_counter_disable_family(struct perf_counter
*counter
)
385 struct perf_counter
*child
;
387 perf_counter_disable(counter
);
390 * Lock the mutex to protect the list of children
392 mutex_lock(&counter
->mutex
);
393 list_for_each_entry(child
, &counter
->child_list
, child_list
)
394 perf_counter_disable(child
);
395 mutex_unlock(&counter
->mutex
);
399 counter_sched_in(struct perf_counter
*counter
,
400 struct perf_cpu_context
*cpuctx
,
401 struct perf_counter_context
*ctx
,
404 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
407 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
408 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
410 * The new state must be visible before we turn it on in the hardware:
414 if (counter
->hw_ops
->enable(counter
)) {
415 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
420 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
422 if (!is_software_counter(counter
))
423 cpuctx
->active_oncpu
++;
426 if (counter
->hw_event
.exclusive
)
427 cpuctx
->exclusive
= 1;
433 * Return 1 for a group consisting entirely of software counters,
434 * 0 if the group contains any hardware counters.
436 static int is_software_only_group(struct perf_counter
*leader
)
438 struct perf_counter
*counter
;
440 if (!is_software_counter(leader
))
443 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
444 if (!is_software_counter(counter
))
451 * Work out whether we can put this counter group on the CPU now.
453 static int group_can_go_on(struct perf_counter
*counter
,
454 struct perf_cpu_context
*cpuctx
,
458 * Groups consisting entirely of software counters can always go on.
460 if (is_software_only_group(counter
))
463 * If an exclusive group is already on, no other hardware
464 * counters can go on.
466 if (cpuctx
->exclusive
)
469 * If this group is exclusive and there are already
470 * counters on the CPU, it can't go on.
472 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
475 * Otherwise, try to add it if all previous groups were able
481 static void add_counter_to_ctx(struct perf_counter
*counter
,
482 struct perf_counter_context
*ctx
)
484 list_add_counter(counter
, ctx
);
486 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
487 counter
->tstamp_enabled
= ctx
->time
;
488 counter
->tstamp_running
= ctx
->time
;
489 counter
->tstamp_stopped
= ctx
->time
;
493 * Cross CPU call to install and enable a performance counter
495 static void __perf_install_in_context(void *info
)
497 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
498 struct perf_counter
*counter
= info
;
499 struct perf_counter_context
*ctx
= counter
->ctx
;
500 struct perf_counter
*leader
= counter
->group_leader
;
501 int cpu
= smp_processor_id();
507 * If this is a task context, we need to check whether it is
508 * the current task context of this cpu. If not it has been
509 * scheduled out before the smp call arrived.
511 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
514 spin_lock_irqsave(&ctx
->lock
, flags
);
515 update_context_time(ctx
);
518 * Protect the list operation against NMI by disabling the
519 * counters on a global level. NOP for non NMI based counters.
521 perf_flags
= hw_perf_save_disable();
523 add_counter_to_ctx(counter
, ctx
);
526 * Don't put the counter on if it is disabled or if
527 * it is in a group and the group isn't on.
529 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
530 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
534 * An exclusive counter can't go on if there are already active
535 * hardware counters, and no hardware counter can go on if there
536 * is already an exclusive counter on.
538 if (!group_can_go_on(counter
, cpuctx
, 1))
541 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
545 * This counter couldn't go on. If it is in a group
546 * then we have to pull the whole group off.
547 * If the counter group is pinned then put it in error state.
549 if (leader
!= counter
)
550 group_sched_out(leader
, cpuctx
, ctx
);
551 if (leader
->hw_event
.pinned
) {
552 update_group_times(leader
);
553 leader
->state
= PERF_COUNTER_STATE_ERROR
;
557 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
558 cpuctx
->max_pertask
--;
561 hw_perf_restore(perf_flags
);
563 spin_unlock_irqrestore(&ctx
->lock
, flags
);
567 * Attach a performance counter to a context
569 * First we add the counter to the list with the hardware enable bit
570 * in counter->hw_config cleared.
572 * If the counter is attached to a task which is on a CPU we use a smp
573 * call to enable it in the task context. The task might have been
574 * scheduled away, but we check this in the smp call again.
576 * Must be called with ctx->mutex held.
579 perf_install_in_context(struct perf_counter_context
*ctx
,
580 struct perf_counter
*counter
,
583 struct task_struct
*task
= ctx
->task
;
587 * Per cpu counters are installed via an smp call and
588 * the install is always sucessful.
590 smp_call_function_single(cpu
, __perf_install_in_context
,
595 counter
->task
= task
;
597 task_oncpu_function_call(task
, __perf_install_in_context
,
600 spin_lock_irq(&ctx
->lock
);
602 * we need to retry the smp call.
604 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
605 spin_unlock_irq(&ctx
->lock
);
610 * The lock prevents that this context is scheduled in so we
611 * can add the counter safely, if it the call above did not
614 if (list_empty(&counter
->list_entry
))
615 add_counter_to_ctx(counter
, ctx
);
616 spin_unlock_irq(&ctx
->lock
);
620 * Cross CPU call to enable a performance counter
622 static void __perf_counter_enable(void *info
)
624 struct perf_counter
*counter
= info
;
625 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
626 struct perf_counter_context
*ctx
= counter
->ctx
;
627 struct perf_counter
*leader
= counter
->group_leader
;
632 * If this is a per-task counter, need to check whether this
633 * counter's task is the current task on this cpu.
635 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
638 spin_lock_irqsave(&ctx
->lock
, flags
);
639 update_context_time(ctx
);
641 counter
->prev_state
= counter
->state
;
642 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
644 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
645 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
648 * If the counter is in a group and isn't the group leader,
649 * then don't put it on unless the group is on.
651 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
654 if (!group_can_go_on(counter
, cpuctx
, 1))
657 err
= counter_sched_in(counter
, cpuctx
, ctx
,
662 * If this counter can't go on and it's part of a
663 * group, then the whole group has to come off.
665 if (leader
!= counter
)
666 group_sched_out(leader
, cpuctx
, ctx
);
667 if (leader
->hw_event
.pinned
) {
668 update_group_times(leader
);
669 leader
->state
= PERF_COUNTER_STATE_ERROR
;
674 spin_unlock_irqrestore(&ctx
->lock
, flags
);
680 static void perf_counter_enable(struct perf_counter
*counter
)
682 struct perf_counter_context
*ctx
= counter
->ctx
;
683 struct task_struct
*task
= ctx
->task
;
687 * Enable the counter on the cpu that it's on
689 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
694 spin_lock_irq(&ctx
->lock
);
695 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
699 * If the counter is in error state, clear that first.
700 * That way, if we see the counter in error state below, we
701 * know that it has gone back into error state, as distinct
702 * from the task having been scheduled away before the
703 * cross-call arrived.
705 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
706 counter
->state
= PERF_COUNTER_STATE_OFF
;
709 spin_unlock_irq(&ctx
->lock
);
710 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
712 spin_lock_irq(&ctx
->lock
);
715 * If the context is active and the counter is still off,
716 * we need to retry the cross-call.
718 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
722 * Since we have the lock this context can't be scheduled
723 * in, so we can change the state safely.
725 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
726 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
727 counter
->tstamp_enabled
=
728 ctx
->time
- counter
->total_time_enabled
;
731 spin_unlock_irq(&ctx
->lock
);
734 static void perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
736 atomic_add(refresh
, &counter
->event_limit
);
737 perf_counter_enable(counter
);
741 * Enable a counter and all its children.
743 static void perf_counter_enable_family(struct perf_counter
*counter
)
745 struct perf_counter
*child
;
747 perf_counter_enable(counter
);
750 * Lock the mutex to protect the list of children
752 mutex_lock(&counter
->mutex
);
753 list_for_each_entry(child
, &counter
->child_list
, child_list
)
754 perf_counter_enable(child
);
755 mutex_unlock(&counter
->mutex
);
758 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
759 struct perf_cpu_context
*cpuctx
)
761 struct perf_counter
*counter
;
764 spin_lock(&ctx
->lock
);
766 if (likely(!ctx
->nr_counters
))
768 update_context_time(ctx
);
770 flags
= hw_perf_save_disable();
771 if (ctx
->nr_active
) {
772 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
773 group_sched_out(counter
, cpuctx
, ctx
);
775 hw_perf_restore(flags
);
777 spin_unlock(&ctx
->lock
);
781 * Called from scheduler to remove the counters of the current task,
782 * with interrupts disabled.
784 * We stop each counter and update the counter value in counter->count.
786 * This does not protect us against NMI, but disable()
787 * sets the disabled bit in the control field of counter _before_
788 * accessing the counter control register. If a NMI hits, then it will
789 * not restart the counter.
791 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
793 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
794 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
795 struct pt_regs
*regs
;
797 if (likely(!cpuctx
->task_ctx
))
800 regs
= task_pt_regs(task
);
801 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
);
802 __perf_counter_sched_out(ctx
, cpuctx
);
804 cpuctx
->task_ctx
= NULL
;
807 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
809 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
813 group_sched_in(struct perf_counter
*group_counter
,
814 struct perf_cpu_context
*cpuctx
,
815 struct perf_counter_context
*ctx
,
818 struct perf_counter
*counter
, *partial_group
;
821 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
824 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
826 return ret
< 0 ? ret
: 0;
828 group_counter
->prev_state
= group_counter
->state
;
829 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
833 * Schedule in siblings as one group (if any):
835 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
836 counter
->prev_state
= counter
->state
;
837 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
838 partial_group
= counter
;
847 * Groups can be scheduled in as one unit only, so undo any
848 * partial group before returning:
850 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
851 if (counter
== partial_group
)
853 counter_sched_out(counter
, cpuctx
, ctx
);
855 counter_sched_out(group_counter
, cpuctx
, ctx
);
861 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
862 struct perf_cpu_context
*cpuctx
, int cpu
)
864 struct perf_counter
*counter
;
868 spin_lock(&ctx
->lock
);
870 if (likely(!ctx
->nr_counters
))
873 ctx
->timestamp
= perf_clock();
875 flags
= hw_perf_save_disable();
878 * First go through the list and put on any pinned groups
879 * in order to give them the best chance of going on.
881 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
882 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
883 !counter
->hw_event
.pinned
)
885 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
888 if (group_can_go_on(counter
, cpuctx
, 1))
889 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
892 * If this pinned group hasn't been scheduled,
893 * put it in error state.
895 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
896 update_group_times(counter
);
897 counter
->state
= PERF_COUNTER_STATE_ERROR
;
901 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
903 * Ignore counters in OFF or ERROR state, and
904 * ignore pinned counters since we did them already.
906 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
907 counter
->hw_event
.pinned
)
911 * Listen to the 'cpu' scheduling filter constraint
914 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
917 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
918 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
922 hw_perf_restore(flags
);
924 spin_unlock(&ctx
->lock
);
928 * Called from scheduler to add the counters of the current task
929 * with interrupts disabled.
931 * We restore the counter value and then enable it.
933 * This does not protect us against NMI, but enable()
934 * sets the enabled bit in the control field of counter _before_
935 * accessing the counter control register. If a NMI hits, then it will
936 * keep the counter running.
938 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
940 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
941 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
943 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
944 cpuctx
->task_ctx
= ctx
;
947 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
949 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
951 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
954 int perf_counter_task_disable(void)
956 struct task_struct
*curr
= current
;
957 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
958 struct perf_counter
*counter
;
963 if (likely(!ctx
->nr_counters
))
966 local_irq_save(flags
);
967 cpu
= smp_processor_id();
969 perf_counter_task_sched_out(curr
, cpu
);
971 spin_lock(&ctx
->lock
);
974 * Disable all the counters:
976 perf_flags
= hw_perf_save_disable();
978 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
979 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
980 update_group_times(counter
);
981 counter
->state
= PERF_COUNTER_STATE_OFF
;
985 hw_perf_restore(perf_flags
);
987 spin_unlock_irqrestore(&ctx
->lock
, flags
);
992 int perf_counter_task_enable(void)
994 struct task_struct
*curr
= current
;
995 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
996 struct perf_counter
*counter
;
1001 if (likely(!ctx
->nr_counters
))
1004 local_irq_save(flags
);
1005 cpu
= smp_processor_id();
1007 perf_counter_task_sched_out(curr
, cpu
);
1009 spin_lock(&ctx
->lock
);
1012 * Disable all the counters:
1014 perf_flags
= hw_perf_save_disable();
1016 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1017 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1019 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1020 counter
->tstamp_enabled
=
1021 ctx
->time
- counter
->total_time_enabled
;
1022 counter
->hw_event
.disabled
= 0;
1024 hw_perf_restore(perf_flags
);
1026 spin_unlock(&ctx
->lock
);
1028 perf_counter_task_sched_in(curr
, cpu
);
1030 local_irq_restore(flags
);
1036 * Round-robin a context's counters:
1038 static void rotate_ctx(struct perf_counter_context
*ctx
)
1040 struct perf_counter
*counter
;
1043 if (!ctx
->nr_counters
)
1046 spin_lock(&ctx
->lock
);
1048 * Rotate the first entry last (works just fine for group counters too):
1050 perf_flags
= hw_perf_save_disable();
1051 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1052 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1055 hw_perf_restore(perf_flags
);
1057 spin_unlock(&ctx
->lock
);
1060 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1062 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1063 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1064 const int rotate_percpu
= 0;
1067 perf_counter_cpu_sched_out(cpuctx
);
1068 perf_counter_task_sched_out(curr
, cpu
);
1071 rotate_ctx(&cpuctx
->ctx
);
1075 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1076 perf_counter_task_sched_in(curr
, cpu
);
1080 * Cross CPU call to read the hardware counter
1082 static void __read(void *info
)
1084 struct perf_counter
*counter
= info
;
1085 struct perf_counter_context
*ctx
= counter
->ctx
;
1086 unsigned long flags
;
1088 local_irq_save(flags
);
1090 update_context_time(ctx
);
1091 counter
->hw_ops
->read(counter
);
1092 update_counter_times(counter
);
1093 local_irq_restore(flags
);
1096 static u64
perf_counter_read(struct perf_counter
*counter
)
1099 * If counter is enabled and currently active on a CPU, update the
1100 * value in the counter structure:
1102 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1103 smp_call_function_single(counter
->oncpu
,
1104 __read
, counter
, 1);
1105 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1106 update_counter_times(counter
);
1109 return atomic64_read(&counter
->count
);
1112 static void put_context(struct perf_counter_context
*ctx
)
1115 put_task_struct(ctx
->task
);
1118 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1120 struct perf_cpu_context
*cpuctx
;
1121 struct perf_counter_context
*ctx
;
1122 struct task_struct
*task
;
1125 * If cpu is not a wildcard then this is a percpu counter:
1128 /* Must be root to operate on a CPU counter: */
1129 if (!capable(CAP_SYS_ADMIN
))
1130 return ERR_PTR(-EACCES
);
1132 if (cpu
< 0 || cpu
> num_possible_cpus())
1133 return ERR_PTR(-EINVAL
);
1136 * We could be clever and allow to attach a counter to an
1137 * offline CPU and activate it when the CPU comes up, but
1140 if (!cpu_isset(cpu
, cpu_online_map
))
1141 return ERR_PTR(-ENODEV
);
1143 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1153 task
= find_task_by_vpid(pid
);
1155 get_task_struct(task
);
1159 return ERR_PTR(-ESRCH
);
1161 ctx
= &task
->perf_counter_ctx
;
1164 /* Reuse ptrace permission checks for now. */
1165 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1167 return ERR_PTR(-EACCES
);
1173 static void free_counter_rcu(struct rcu_head
*head
)
1175 struct perf_counter
*counter
;
1177 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1181 static void perf_pending_sync(struct perf_counter
*counter
);
1183 static void free_counter(struct perf_counter
*counter
)
1185 perf_pending_sync(counter
);
1187 if (counter
->destroy
)
1188 counter
->destroy(counter
);
1190 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1194 * Called when the last reference to the file is gone.
1196 static int perf_release(struct inode
*inode
, struct file
*file
)
1198 struct perf_counter
*counter
= file
->private_data
;
1199 struct perf_counter_context
*ctx
= counter
->ctx
;
1201 file
->private_data
= NULL
;
1203 mutex_lock(&ctx
->mutex
);
1204 mutex_lock(&counter
->mutex
);
1206 perf_counter_remove_from_context(counter
);
1208 mutex_unlock(&counter
->mutex
);
1209 mutex_unlock(&ctx
->mutex
);
1211 free_counter(counter
);
1218 * Read the performance counter - simple non blocking version for now
1221 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1227 * Return end-of-file for a read on a counter that is in
1228 * error state (i.e. because it was pinned but it couldn't be
1229 * scheduled on to the CPU at some point).
1231 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1234 mutex_lock(&counter
->mutex
);
1235 values
[0] = perf_counter_read(counter
);
1237 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1238 values
[n
++] = counter
->total_time_enabled
+
1239 atomic64_read(&counter
->child_total_time_enabled
);
1240 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1241 values
[n
++] = counter
->total_time_running
+
1242 atomic64_read(&counter
->child_total_time_running
);
1243 mutex_unlock(&counter
->mutex
);
1245 if (count
< n
* sizeof(u64
))
1247 count
= n
* sizeof(u64
);
1249 if (copy_to_user(buf
, values
, count
))
1256 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1258 struct perf_counter
*counter
= file
->private_data
;
1260 return perf_read_hw(counter
, buf
, count
);
1263 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1265 struct perf_counter
*counter
= file
->private_data
;
1266 struct perf_mmap_data
*data
;
1267 unsigned int events
;
1270 data
= rcu_dereference(counter
->data
);
1272 events
= atomic_xchg(&data
->wakeup
, 0);
1277 poll_wait(file
, &counter
->waitq
, wait
);
1282 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1284 struct perf_counter
*counter
= file
->private_data
;
1288 case PERF_COUNTER_IOC_ENABLE
:
1289 perf_counter_enable_family(counter
);
1291 case PERF_COUNTER_IOC_DISABLE
:
1292 perf_counter_disable_family(counter
);
1294 case PERF_COUNTER_IOC_REFRESH
:
1295 perf_counter_refresh(counter
, arg
);
1304 * Callers need to ensure there can be no nesting of this function, otherwise
1305 * the seqlock logic goes bad. We can not serialize this because the arch
1306 * code calls this from NMI context.
1308 void perf_counter_update_userpage(struct perf_counter
*counter
)
1310 struct perf_mmap_data
*data
;
1311 struct perf_counter_mmap_page
*userpg
;
1314 data
= rcu_dereference(counter
->data
);
1318 userpg
= data
->user_page
;
1321 * Disable preemption so as to not let the corresponding user-space
1322 * spin too long if we get preempted.
1327 userpg
->index
= counter
->hw
.idx
;
1328 userpg
->offset
= atomic64_read(&counter
->count
);
1329 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1330 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1339 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1341 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1342 struct perf_mmap_data
*data
;
1343 int ret
= VM_FAULT_SIGBUS
;
1346 data
= rcu_dereference(counter
->data
);
1350 if (vmf
->pgoff
== 0) {
1351 vmf
->page
= virt_to_page(data
->user_page
);
1353 int nr
= vmf
->pgoff
- 1;
1355 if ((unsigned)nr
> data
->nr_pages
)
1358 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1360 get_page(vmf
->page
);
1368 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1370 struct perf_mmap_data
*data
;
1374 WARN_ON(atomic_read(&counter
->mmap_count
));
1376 size
= sizeof(struct perf_mmap_data
);
1377 size
+= nr_pages
* sizeof(void *);
1379 data
= kzalloc(size
, GFP_KERNEL
);
1383 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1384 if (!data
->user_page
)
1385 goto fail_user_page
;
1387 for (i
= 0; i
< nr_pages
; i
++) {
1388 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1389 if (!data
->data_pages
[i
])
1390 goto fail_data_pages
;
1393 data
->nr_pages
= nr_pages
;
1395 rcu_assign_pointer(counter
->data
, data
);
1400 for (i
--; i
>= 0; i
--)
1401 free_page((unsigned long)data
->data_pages
[i
]);
1403 free_page((unsigned long)data
->user_page
);
1412 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1414 struct perf_mmap_data
*data
= container_of(rcu_head
,
1415 struct perf_mmap_data
, rcu_head
);
1418 free_page((unsigned long)data
->user_page
);
1419 for (i
= 0; i
< data
->nr_pages
; i
++)
1420 free_page((unsigned long)data
->data_pages
[i
]);
1424 static void perf_mmap_data_free(struct perf_counter
*counter
)
1426 struct perf_mmap_data
*data
= counter
->data
;
1428 WARN_ON(atomic_read(&counter
->mmap_count
));
1430 rcu_assign_pointer(counter
->data
, NULL
);
1431 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1434 static void perf_mmap_open(struct vm_area_struct
*vma
)
1436 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1438 atomic_inc(&counter
->mmap_count
);
1441 static void perf_mmap_close(struct vm_area_struct
*vma
)
1443 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1445 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1446 &counter
->mmap_mutex
)) {
1447 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_pages
+ 1;
1448 perf_mmap_data_free(counter
);
1449 mutex_unlock(&counter
->mmap_mutex
);
1453 static struct vm_operations_struct perf_mmap_vmops
= {
1454 .open
= perf_mmap_open
,
1455 .close
= perf_mmap_close
,
1456 .fault
= perf_mmap_fault
,
1459 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1461 struct perf_counter
*counter
= file
->private_data
;
1462 unsigned long vma_size
;
1463 unsigned long nr_pages
;
1464 unsigned long locked
, lock_limit
;
1467 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1470 vma_size
= vma
->vm_end
- vma
->vm_start
;
1471 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1474 * If we have data pages ensure they're a power-of-two number, so we
1475 * can do bitmasks instead of modulo.
1477 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1480 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1483 if (vma
->vm_pgoff
!= 0)
1486 mutex_lock(&counter
->mmap_mutex
);
1487 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1488 if (nr_pages
!= counter
->data
->nr_pages
)
1493 locked
= vma
->vm_mm
->locked_vm
;
1494 locked
+= nr_pages
+ 1;
1496 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1497 lock_limit
>>= PAGE_SHIFT
;
1499 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1504 WARN_ON(counter
->data
);
1505 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1509 atomic_set(&counter
->mmap_count
, 1);
1510 vma
->vm_mm
->locked_vm
+= nr_pages
+ 1;
1512 mutex_unlock(&counter
->mmap_mutex
);
1514 vma
->vm_flags
&= ~VM_MAYWRITE
;
1515 vma
->vm_flags
|= VM_RESERVED
;
1516 vma
->vm_ops
= &perf_mmap_vmops
;
1521 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1523 struct perf_counter
*counter
= filp
->private_data
;
1524 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1527 mutex_lock(&inode
->i_mutex
);
1528 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1529 mutex_unlock(&inode
->i_mutex
);
1537 static const struct file_operations perf_fops
= {
1538 .release
= perf_release
,
1541 .unlocked_ioctl
= perf_ioctl
,
1542 .compat_ioctl
= perf_ioctl
,
1544 .fasync
= perf_fasync
,
1548 * Perf counter wakeup
1550 * If there's data, ensure we set the poll() state and publish everything
1551 * to user-space before waking everybody up.
1554 void perf_counter_wakeup(struct perf_counter
*counter
)
1556 struct perf_mmap_data
*data
;
1559 data
= rcu_dereference(counter
->data
);
1561 atomic_set(&data
->wakeup
, POLL_IN
);
1563 * Ensure all data writes are issued before updating the
1564 * user-space data head information. The matching rmb()
1565 * will be in userspace after reading this value.
1568 data
->user_page
->data_head
= atomic_read(&data
->head
);
1572 wake_up_all(&counter
->waitq
);
1574 if (counter
->pending_kill
) {
1575 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1576 counter
->pending_kill
= 0;
1583 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1585 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1586 * single linked list and use cmpxchg() to add entries lockless.
1589 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1591 struct perf_counter
*counter
= container_of(entry
,
1592 struct perf_counter
, pending
);
1594 if (counter
->pending_disable
) {
1595 counter
->pending_disable
= 0;
1596 perf_counter_disable(counter
);
1599 if (counter
->pending_wakeup
) {
1600 counter
->pending_wakeup
= 0;
1601 perf_counter_wakeup(counter
);
1605 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1607 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1611 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1612 void (*func
)(struct perf_pending_entry
*))
1614 struct perf_pending_entry
**head
;
1616 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1621 head
= &get_cpu_var(perf_pending_head
);
1624 entry
->next
= *head
;
1625 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1627 set_perf_counter_pending();
1629 put_cpu_var(perf_pending_head
);
1632 static int __perf_pending_run(void)
1634 struct perf_pending_entry
*list
;
1637 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1638 while (list
!= PENDING_TAIL
) {
1639 void (*func
)(struct perf_pending_entry
*);
1640 struct perf_pending_entry
*entry
= list
;
1647 * Ensure we observe the unqueue before we issue the wakeup,
1648 * so that we won't be waiting forever.
1649 * -- see perf_not_pending().
1660 static inline int perf_not_pending(struct perf_counter
*counter
)
1663 * If we flush on whatever cpu we run, there is a chance we don't
1667 __perf_pending_run();
1671 * Ensure we see the proper queue state before going to sleep
1672 * so that we do not miss the wakeup. -- see perf_pending_handle()
1675 return counter
->pending
.next
== NULL
;
1678 static void perf_pending_sync(struct perf_counter
*counter
)
1680 wait_event(counter
->waitq
, perf_not_pending(counter
));
1683 void perf_counter_do_pending(void)
1685 __perf_pending_run();
1689 * Callchain support -- arch specific
1692 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1701 struct perf_output_handle
{
1702 struct perf_counter
*counter
;
1703 struct perf_mmap_data
*data
;
1704 unsigned int offset
;
1711 static inline void __perf_output_wakeup(struct perf_output_handle
*handle
)
1714 handle
->counter
->pending_wakeup
= 1;
1715 perf_pending_queue(&handle
->counter
->pending
,
1716 perf_pending_counter
);
1718 perf_counter_wakeup(handle
->counter
);
1721 static int perf_output_begin(struct perf_output_handle
*handle
,
1722 struct perf_counter
*counter
, unsigned int size
,
1723 int nmi
, int overflow
)
1725 struct perf_mmap_data
*data
;
1726 unsigned int offset
, head
;
1729 data
= rcu_dereference(counter
->data
);
1733 handle
->counter
= counter
;
1735 handle
->overflow
= overflow
;
1737 if (!data
->nr_pages
)
1741 offset
= head
= atomic_read(&data
->head
);
1743 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1745 handle
->data
= data
;
1746 handle
->offset
= offset
;
1747 handle
->head
= head
;
1748 handle
->wakeup
= (offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
);
1753 __perf_output_wakeup(handle
);
1760 static void perf_output_copy(struct perf_output_handle
*handle
,
1761 void *buf
, unsigned int len
)
1763 unsigned int pages_mask
;
1764 unsigned int offset
;
1768 offset
= handle
->offset
;
1769 pages_mask
= handle
->data
->nr_pages
- 1;
1770 pages
= handle
->data
->data_pages
;
1773 unsigned int page_offset
;
1776 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1777 page_offset
= offset
& (PAGE_SIZE
- 1);
1778 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1780 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1787 handle
->offset
= offset
;
1789 WARN_ON_ONCE(handle
->offset
> handle
->head
);
1792 #define perf_output_put(handle, x) \
1793 perf_output_copy((handle), &(x), sizeof(x))
1795 static void perf_output_end(struct perf_output_handle
*handle
)
1797 int wakeup_events
= handle
->counter
->hw_event
.wakeup_events
;
1799 if (handle
->overflow
&& wakeup_events
) {
1800 int events
= atomic_inc_return(&handle
->data
->events
);
1801 if (events
>= wakeup_events
) {
1802 atomic_sub(wakeup_events
, &handle
->data
->events
);
1803 __perf_output_wakeup(handle
);
1805 } else if (handle
->wakeup
)
1806 __perf_output_wakeup(handle
);
1810 static void perf_counter_output(struct perf_counter
*counter
,
1811 int nmi
, struct pt_regs
*regs
)
1814 u64 record_type
= counter
->hw_event
.record_type
;
1815 struct perf_output_handle handle
;
1816 struct perf_event_header header
;
1825 struct perf_callchain_entry
*callchain
= NULL
;
1826 int callchain_size
= 0;
1829 header
.type
= PERF_EVENT_COUNTER_OVERFLOW
;
1830 header
.size
= sizeof(header
);
1832 if (record_type
& PERF_RECORD_IP
) {
1833 ip
= instruction_pointer(regs
);
1834 header
.type
|= __PERF_EVENT_IP
;
1835 header
.size
+= sizeof(ip
);
1838 if (record_type
& PERF_RECORD_TID
) {
1839 /* namespace issues */
1840 tid_entry
.pid
= current
->group_leader
->pid
;
1841 tid_entry
.tid
= current
->pid
;
1843 header
.type
|= __PERF_EVENT_TID
;
1844 header
.size
+= sizeof(tid_entry
);
1847 if (record_type
& PERF_RECORD_GROUP
) {
1848 header
.type
|= __PERF_EVENT_GROUP
;
1849 header
.size
+= sizeof(u64
) +
1850 counter
->nr_siblings
* sizeof(group_entry
);
1853 if (record_type
& PERF_RECORD_CALLCHAIN
) {
1854 callchain
= perf_callchain(regs
);
1857 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
1859 header
.type
|= __PERF_EVENT_CALLCHAIN
;
1860 header
.size
+= callchain_size
;
1864 if (record_type
& PERF_RECORD_TIME
) {
1866 * Maybe do better on x86 and provide cpu_clock_nmi()
1868 time
= sched_clock();
1870 header
.type
|= __PERF_EVENT_TIME
;
1871 header
.size
+= sizeof(u64
);
1874 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
1878 perf_output_put(&handle
, header
);
1880 if (record_type
& PERF_RECORD_IP
)
1881 perf_output_put(&handle
, ip
);
1883 if (record_type
& PERF_RECORD_TID
)
1884 perf_output_put(&handle
, tid_entry
);
1886 if (record_type
& PERF_RECORD_GROUP
) {
1887 struct perf_counter
*leader
, *sub
;
1888 u64 nr
= counter
->nr_siblings
;
1890 perf_output_put(&handle
, nr
);
1892 leader
= counter
->group_leader
;
1893 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1895 sub
->hw_ops
->read(sub
);
1897 group_entry
.event
= sub
->hw_event
.config
;
1898 group_entry
.counter
= atomic64_read(&sub
->count
);
1900 perf_output_put(&handle
, group_entry
);
1905 perf_output_copy(&handle
, callchain
, callchain_size
);
1907 if (record_type
& PERF_RECORD_TIME
)
1908 perf_output_put(&handle
, time
);
1910 perf_output_end(&handle
);
1917 struct perf_mmap_event
{
1923 struct perf_event_header header
;
1933 static void perf_counter_mmap_output(struct perf_counter
*counter
,
1934 struct perf_mmap_event
*mmap_event
)
1936 struct perf_output_handle handle
;
1937 int size
= mmap_event
->event
.header
.size
;
1938 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
1943 perf_output_put(&handle
, mmap_event
->event
);
1944 perf_output_copy(&handle
, mmap_event
->file_name
,
1945 mmap_event
->file_size
);
1946 perf_output_end(&handle
);
1949 static int perf_counter_mmap_match(struct perf_counter
*counter
,
1950 struct perf_mmap_event
*mmap_event
)
1952 if (counter
->hw_event
.mmap
&&
1953 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
1956 if (counter
->hw_event
.munmap
&&
1957 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
1963 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
1964 struct perf_mmap_event
*mmap_event
)
1966 struct perf_counter
*counter
;
1968 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
1972 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
1973 if (perf_counter_mmap_match(counter
, mmap_event
))
1974 perf_counter_mmap_output(counter
, mmap_event
);
1979 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
1981 struct perf_cpu_context
*cpuctx
;
1982 struct file
*file
= mmap_event
->file
;
1989 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
1991 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
1994 name
= dentry_path(file
->f_dentry
, buf
, PATH_MAX
);
1996 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2000 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2005 size
= ALIGN(strlen(name
), sizeof(u64
));
2007 mmap_event
->file_name
= name
;
2008 mmap_event
->file_size
= size
;
2010 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2012 cpuctx
= &get_cpu_var(perf_cpu_context
);
2013 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2014 put_cpu_var(perf_cpu_context
);
2016 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2021 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2022 unsigned long pgoff
, struct file
*file
)
2024 struct perf_mmap_event mmap_event
= {
2027 .header
= { .type
= PERF_EVENT_MMAP
, },
2028 .pid
= current
->group_leader
->pid
,
2029 .tid
= current
->pid
,
2036 perf_counter_mmap_event(&mmap_event
);
2039 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2040 unsigned long pgoff
, struct file
*file
)
2042 struct perf_mmap_event mmap_event
= {
2045 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2046 .pid
= current
->group_leader
->pid
,
2047 .tid
= current
->pid
,
2054 perf_counter_mmap_event(&mmap_event
);
2058 * Generic counter overflow handling.
2061 int perf_counter_overflow(struct perf_counter
*counter
,
2062 int nmi
, struct pt_regs
*regs
)
2064 int events
= atomic_read(&counter
->event_limit
);
2067 counter
->pending_kill
= POLL_IN
;
2068 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2070 counter
->pending_kill
= POLL_HUP
;
2072 counter
->pending_disable
= 1;
2073 perf_pending_queue(&counter
->pending
,
2074 perf_pending_counter
);
2076 perf_counter_disable(counter
);
2079 perf_counter_output(counter
, nmi
, regs
);
2084 * Generic software counter infrastructure
2087 static void perf_swcounter_update(struct perf_counter
*counter
)
2089 struct hw_perf_counter
*hwc
= &counter
->hw
;
2094 prev
= atomic64_read(&hwc
->prev_count
);
2095 now
= atomic64_read(&hwc
->count
);
2096 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2101 atomic64_add(delta
, &counter
->count
);
2102 atomic64_sub(delta
, &hwc
->period_left
);
2105 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2107 struct hw_perf_counter
*hwc
= &counter
->hw
;
2108 s64 left
= atomic64_read(&hwc
->period_left
);
2109 s64 period
= hwc
->irq_period
;
2111 if (unlikely(left
<= -period
)) {
2113 atomic64_set(&hwc
->period_left
, left
);
2116 if (unlikely(left
<= 0)) {
2118 atomic64_add(period
, &hwc
->period_left
);
2121 atomic64_set(&hwc
->prev_count
, -left
);
2122 atomic64_set(&hwc
->count
, -left
);
2125 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2127 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2128 struct perf_counter
*counter
;
2129 struct pt_regs
*regs
;
2131 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2132 counter
->hw_ops
->read(counter
);
2134 regs
= get_irq_regs();
2136 * In case we exclude kernel IPs or are somehow not in interrupt
2137 * context, provide the next best thing, the user IP.
2139 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2140 !counter
->hw_event
.exclude_user
)
2141 regs
= task_pt_regs(current
);
2144 if (perf_counter_overflow(counter
, 0, regs
))
2145 ret
= HRTIMER_NORESTART
;
2148 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
2153 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2154 int nmi
, struct pt_regs
*regs
)
2156 perf_swcounter_update(counter
);
2157 perf_swcounter_set_period(counter
);
2158 if (perf_counter_overflow(counter
, nmi
, regs
))
2159 /* soft-disable the counter */
2164 static int perf_swcounter_match(struct perf_counter
*counter
,
2165 enum perf_event_types type
,
2166 u32 event
, struct pt_regs
*regs
)
2168 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2171 if (perf_event_raw(&counter
->hw_event
))
2174 if (perf_event_type(&counter
->hw_event
) != type
)
2177 if (perf_event_id(&counter
->hw_event
) != event
)
2180 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2183 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2189 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2190 int nmi
, struct pt_regs
*regs
)
2192 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2193 if (counter
->hw
.irq_period
&& !neg
)
2194 perf_swcounter_overflow(counter
, nmi
, regs
);
2197 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2198 enum perf_event_types type
, u32 event
,
2199 u64 nr
, int nmi
, struct pt_regs
*regs
)
2201 struct perf_counter
*counter
;
2203 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2207 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2208 if (perf_swcounter_match(counter
, type
, event
, regs
))
2209 perf_swcounter_add(counter
, nr
, nmi
, regs
);
2214 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2217 return &cpuctx
->recursion
[3];
2220 return &cpuctx
->recursion
[2];
2223 return &cpuctx
->recursion
[1];
2225 return &cpuctx
->recursion
[0];
2228 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2229 u64 nr
, int nmi
, struct pt_regs
*regs
)
2231 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2232 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2240 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
, nr
, nmi
, regs
);
2241 if (cpuctx
->task_ctx
) {
2242 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2250 put_cpu_var(perf_cpu_context
);
2253 void perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
)
2255 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
);
2258 static void perf_swcounter_read(struct perf_counter
*counter
)
2260 perf_swcounter_update(counter
);
2263 static int perf_swcounter_enable(struct perf_counter
*counter
)
2265 perf_swcounter_set_period(counter
);
2269 static void perf_swcounter_disable(struct perf_counter
*counter
)
2271 perf_swcounter_update(counter
);
2274 static const struct hw_perf_counter_ops perf_ops_generic
= {
2275 .enable
= perf_swcounter_enable
,
2276 .disable
= perf_swcounter_disable
,
2277 .read
= perf_swcounter_read
,
2281 * Software counter: cpu wall time clock
2284 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2286 int cpu
= raw_smp_processor_id();
2290 now
= cpu_clock(cpu
);
2291 prev
= atomic64_read(&counter
->hw
.prev_count
);
2292 atomic64_set(&counter
->hw
.prev_count
, now
);
2293 atomic64_add(now
- prev
, &counter
->count
);
2296 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2298 struct hw_perf_counter
*hwc
= &counter
->hw
;
2299 int cpu
= raw_smp_processor_id();
2301 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2302 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2303 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2304 if (hwc
->irq_period
) {
2305 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2306 ns_to_ktime(hwc
->irq_period
), 0,
2307 HRTIMER_MODE_REL
, 0);
2313 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2315 hrtimer_cancel(&counter
->hw
.hrtimer
);
2316 cpu_clock_perf_counter_update(counter
);
2319 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2321 cpu_clock_perf_counter_update(counter
);
2324 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
2325 .enable
= cpu_clock_perf_counter_enable
,
2326 .disable
= cpu_clock_perf_counter_disable
,
2327 .read
= cpu_clock_perf_counter_read
,
2331 * Software counter: task time clock
2334 static void task_clock_perf_counter_update(struct perf_counter
*counter
)
2339 update_context_time(counter
->ctx
);
2340 now
= counter
->ctx
->time
;
2342 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2344 atomic64_add(delta
, &counter
->count
);
2347 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2349 struct hw_perf_counter
*hwc
= &counter
->hw
;
2352 update_context_time(counter
->ctx
);
2353 now
= counter
->ctx
->time
;
2355 atomic64_set(&hwc
->prev_count
, now
);
2356 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2357 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2358 if (hwc
->irq_period
) {
2359 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2360 ns_to_ktime(hwc
->irq_period
), 0,
2361 HRTIMER_MODE_REL
, 0);
2367 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2369 hrtimer_cancel(&counter
->hw
.hrtimer
);
2370 task_clock_perf_counter_update(counter
);
2373 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2375 task_clock_perf_counter_update(counter
);
2378 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
2379 .enable
= task_clock_perf_counter_enable
,
2380 .disable
= task_clock_perf_counter_disable
,
2381 .read
= task_clock_perf_counter_read
,
2385 * Software counter: cpu migrations
2388 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2390 struct task_struct
*curr
= counter
->ctx
->task
;
2393 return curr
->se
.nr_migrations
;
2394 return cpu_nr_migrations(smp_processor_id());
2397 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2402 prev
= atomic64_read(&counter
->hw
.prev_count
);
2403 now
= get_cpu_migrations(counter
);
2405 atomic64_set(&counter
->hw
.prev_count
, now
);
2409 atomic64_add(delta
, &counter
->count
);
2412 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2414 cpu_migrations_perf_counter_update(counter
);
2417 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2419 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2420 atomic64_set(&counter
->hw
.prev_count
,
2421 get_cpu_migrations(counter
));
2425 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2427 cpu_migrations_perf_counter_update(counter
);
2430 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
2431 .enable
= cpu_migrations_perf_counter_enable
,
2432 .disable
= cpu_migrations_perf_counter_disable
,
2433 .read
= cpu_migrations_perf_counter_read
,
2436 #ifdef CONFIG_EVENT_PROFILE
2437 void perf_tpcounter_event(int event_id
)
2439 struct pt_regs
*regs
= get_irq_regs();
2442 regs
= task_pt_regs(current
);
2444 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
);
2447 extern int ftrace_profile_enable(int);
2448 extern void ftrace_profile_disable(int);
2450 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2452 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2455 static const struct hw_perf_counter_ops
*
2456 tp_perf_counter_init(struct perf_counter
*counter
)
2458 int event_id
= perf_event_id(&counter
->hw_event
);
2461 ret
= ftrace_profile_enable(event_id
);
2465 counter
->destroy
= tp_perf_counter_destroy
;
2466 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2468 return &perf_ops_generic
;
2471 static const struct hw_perf_counter_ops
*
2472 tp_perf_counter_init(struct perf_counter
*counter
)
2478 static const struct hw_perf_counter_ops
*
2479 sw_perf_counter_init(struct perf_counter
*counter
)
2481 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2482 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
2483 struct hw_perf_counter
*hwc
= &counter
->hw
;
2486 * Software counters (currently) can't in general distinguish
2487 * between user, kernel and hypervisor events.
2488 * However, context switches and cpu migrations are considered
2489 * to be kernel events, and page faults are never hypervisor
2492 switch (perf_event_id(&counter
->hw_event
)) {
2493 case PERF_COUNT_CPU_CLOCK
:
2494 hw_ops
= &perf_ops_cpu_clock
;
2496 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2497 hw_event
->irq_period
= 10000;
2499 case PERF_COUNT_TASK_CLOCK
:
2501 * If the user instantiates this as a per-cpu counter,
2502 * use the cpu_clock counter instead.
2504 if (counter
->ctx
->task
)
2505 hw_ops
= &perf_ops_task_clock
;
2507 hw_ops
= &perf_ops_cpu_clock
;
2509 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2510 hw_event
->irq_period
= 10000;
2512 case PERF_COUNT_PAGE_FAULTS
:
2513 case PERF_COUNT_PAGE_FAULTS_MIN
:
2514 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2515 case PERF_COUNT_CONTEXT_SWITCHES
:
2516 hw_ops
= &perf_ops_generic
;
2518 case PERF_COUNT_CPU_MIGRATIONS
:
2519 if (!counter
->hw_event
.exclude_kernel
)
2520 hw_ops
= &perf_ops_cpu_migrations
;
2525 hwc
->irq_period
= hw_event
->irq_period
;
2531 * Allocate and initialize a counter structure
2533 static struct perf_counter
*
2534 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2536 struct perf_counter_context
*ctx
,
2537 struct perf_counter
*group_leader
,
2540 const struct hw_perf_counter_ops
*hw_ops
;
2541 struct perf_counter
*counter
;
2544 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2546 return ERR_PTR(-ENOMEM
);
2549 * Single counters are their own group leaders, with an
2550 * empty sibling list:
2553 group_leader
= counter
;
2555 mutex_init(&counter
->mutex
);
2556 INIT_LIST_HEAD(&counter
->list_entry
);
2557 INIT_LIST_HEAD(&counter
->event_entry
);
2558 INIT_LIST_HEAD(&counter
->sibling_list
);
2559 init_waitqueue_head(&counter
->waitq
);
2561 mutex_init(&counter
->mmap_mutex
);
2563 INIT_LIST_HEAD(&counter
->child_list
);
2566 counter
->hw_event
= *hw_event
;
2567 counter
->group_leader
= group_leader
;
2568 counter
->hw_ops
= NULL
;
2571 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2572 if (hw_event
->disabled
)
2573 counter
->state
= PERF_COUNTER_STATE_OFF
;
2577 if (perf_event_raw(hw_event
)) {
2578 hw_ops
= hw_perf_counter_init(counter
);
2582 switch (perf_event_type(hw_event
)) {
2583 case PERF_TYPE_HARDWARE
:
2584 hw_ops
= hw_perf_counter_init(counter
);
2587 case PERF_TYPE_SOFTWARE
:
2588 hw_ops
= sw_perf_counter_init(counter
);
2591 case PERF_TYPE_TRACEPOINT
:
2592 hw_ops
= tp_perf_counter_init(counter
);
2599 else if (IS_ERR(hw_ops
))
2600 err
= PTR_ERR(hw_ops
);
2604 return ERR_PTR(err
);
2607 counter
->hw_ops
= hw_ops
;
2613 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2615 * @hw_event_uptr: event type attributes for monitoring/sampling
2618 * @group_fd: group leader counter fd
2620 SYSCALL_DEFINE5(perf_counter_open
,
2621 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2622 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2624 struct perf_counter
*counter
, *group_leader
;
2625 struct perf_counter_hw_event hw_event
;
2626 struct perf_counter_context
*ctx
;
2627 struct file
*counter_file
= NULL
;
2628 struct file
*group_file
= NULL
;
2629 int fput_needed
= 0;
2630 int fput_needed2
= 0;
2633 /* for future expandability... */
2637 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2641 * Get the target context (task or percpu):
2643 ctx
= find_get_context(pid
, cpu
);
2645 return PTR_ERR(ctx
);
2648 * Look up the group leader (we will attach this counter to it):
2650 group_leader
= NULL
;
2651 if (group_fd
!= -1) {
2653 group_file
= fget_light(group_fd
, &fput_needed
);
2655 goto err_put_context
;
2656 if (group_file
->f_op
!= &perf_fops
)
2657 goto err_put_context
;
2659 group_leader
= group_file
->private_data
;
2661 * Do not allow a recursive hierarchy (this new sibling
2662 * becoming part of another group-sibling):
2664 if (group_leader
->group_leader
!= group_leader
)
2665 goto err_put_context
;
2667 * Do not allow to attach to a group in a different
2668 * task or CPU context:
2670 if (group_leader
->ctx
!= ctx
)
2671 goto err_put_context
;
2673 * Only a group leader can be exclusive or pinned
2675 if (hw_event
.exclusive
|| hw_event
.pinned
)
2676 goto err_put_context
;
2679 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2681 ret
= PTR_ERR(counter
);
2682 if (IS_ERR(counter
))
2683 goto err_put_context
;
2685 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2687 goto err_free_put_context
;
2689 counter_file
= fget_light(ret
, &fput_needed2
);
2691 goto err_free_put_context
;
2693 counter
->filp
= counter_file
;
2694 mutex_lock(&ctx
->mutex
);
2695 perf_install_in_context(ctx
, counter
, cpu
);
2696 mutex_unlock(&ctx
->mutex
);
2698 fput_light(counter_file
, fput_needed2
);
2701 fput_light(group_file
, fput_needed
);
2705 err_free_put_context
:
2715 * Initialize the perf_counter context in a task_struct:
2718 __perf_counter_init_context(struct perf_counter_context
*ctx
,
2719 struct task_struct
*task
)
2721 memset(ctx
, 0, sizeof(*ctx
));
2722 spin_lock_init(&ctx
->lock
);
2723 mutex_init(&ctx
->mutex
);
2724 INIT_LIST_HEAD(&ctx
->counter_list
);
2725 INIT_LIST_HEAD(&ctx
->event_list
);
2730 * inherit a counter from parent task to child task:
2732 static struct perf_counter
*
2733 inherit_counter(struct perf_counter
*parent_counter
,
2734 struct task_struct
*parent
,
2735 struct perf_counter_context
*parent_ctx
,
2736 struct task_struct
*child
,
2737 struct perf_counter
*group_leader
,
2738 struct perf_counter_context
*child_ctx
)
2740 struct perf_counter
*child_counter
;
2743 * Instead of creating recursive hierarchies of counters,
2744 * we link inherited counters back to the original parent,
2745 * which has a filp for sure, which we use as the reference
2748 if (parent_counter
->parent
)
2749 parent_counter
= parent_counter
->parent
;
2751 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2752 parent_counter
->cpu
, child_ctx
,
2753 group_leader
, GFP_KERNEL
);
2754 if (IS_ERR(child_counter
))
2755 return child_counter
;
2758 * Link it up in the child's context:
2760 child_counter
->task
= child
;
2761 add_counter_to_ctx(child_counter
, child_ctx
);
2763 child_counter
->parent
= parent_counter
;
2765 * inherit into child's child as well:
2767 child_counter
->hw_event
.inherit
= 1;
2770 * Get a reference to the parent filp - we will fput it
2771 * when the child counter exits. This is safe to do because
2772 * we are in the parent and we know that the filp still
2773 * exists and has a nonzero count:
2775 atomic_long_inc(&parent_counter
->filp
->f_count
);
2778 * Link this into the parent counter's child list
2780 mutex_lock(&parent_counter
->mutex
);
2781 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
2784 * Make the child state follow the state of the parent counter,
2785 * not its hw_event.disabled bit. We hold the parent's mutex,
2786 * so we won't race with perf_counter_{en,dis}able_family.
2788 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
2789 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2791 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
2793 mutex_unlock(&parent_counter
->mutex
);
2795 return child_counter
;
2798 static int inherit_group(struct perf_counter
*parent_counter
,
2799 struct task_struct
*parent
,
2800 struct perf_counter_context
*parent_ctx
,
2801 struct task_struct
*child
,
2802 struct perf_counter_context
*child_ctx
)
2804 struct perf_counter
*leader
;
2805 struct perf_counter
*sub
;
2806 struct perf_counter
*child_ctr
;
2808 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
2809 child
, NULL
, child_ctx
);
2811 return PTR_ERR(leader
);
2812 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
2813 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
2814 child
, leader
, child_ctx
);
2815 if (IS_ERR(child_ctr
))
2816 return PTR_ERR(child_ctr
);
2821 static void sync_child_counter(struct perf_counter
*child_counter
,
2822 struct perf_counter
*parent_counter
)
2824 u64 parent_val
, child_val
;
2826 parent_val
= atomic64_read(&parent_counter
->count
);
2827 child_val
= atomic64_read(&child_counter
->count
);
2830 * Add back the child's count to the parent's count:
2832 atomic64_add(child_val
, &parent_counter
->count
);
2833 atomic64_add(child_counter
->total_time_enabled
,
2834 &parent_counter
->child_total_time_enabled
);
2835 atomic64_add(child_counter
->total_time_running
,
2836 &parent_counter
->child_total_time_running
);
2839 * Remove this counter from the parent's list
2841 mutex_lock(&parent_counter
->mutex
);
2842 list_del_init(&child_counter
->child_list
);
2843 mutex_unlock(&parent_counter
->mutex
);
2846 * Release the parent counter, if this was the last
2849 fput(parent_counter
->filp
);
2853 __perf_counter_exit_task(struct task_struct
*child
,
2854 struct perf_counter
*child_counter
,
2855 struct perf_counter_context
*child_ctx
)
2857 struct perf_counter
*parent_counter
;
2858 struct perf_counter
*sub
, *tmp
;
2861 * If we do not self-reap then we have to wait for the
2862 * child task to unschedule (it will happen for sure),
2863 * so that its counter is at its final count. (This
2864 * condition triggers rarely - child tasks usually get
2865 * off their CPU before the parent has a chance to
2866 * get this far into the reaping action)
2868 if (child
!= current
) {
2869 wait_task_inactive(child
, 0);
2870 list_del_init(&child_counter
->list_entry
);
2871 update_counter_times(child_counter
);
2873 struct perf_cpu_context
*cpuctx
;
2874 unsigned long flags
;
2878 * Disable and unlink this counter.
2880 * Be careful about zapping the list - IRQ/NMI context
2881 * could still be processing it:
2883 local_irq_save(flags
);
2884 perf_flags
= hw_perf_save_disable();
2886 cpuctx
= &__get_cpu_var(perf_cpu_context
);
2888 group_sched_out(child_counter
, cpuctx
, child_ctx
);
2889 update_counter_times(child_counter
);
2891 list_del_init(&child_counter
->list_entry
);
2893 child_ctx
->nr_counters
--;
2895 hw_perf_restore(perf_flags
);
2896 local_irq_restore(flags
);
2899 parent_counter
= child_counter
->parent
;
2901 * It can happen that parent exits first, and has counters
2902 * that are still around due to the child reference. These
2903 * counters need to be zapped - but otherwise linger.
2905 if (parent_counter
) {
2906 sync_child_counter(child_counter
, parent_counter
);
2907 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
2910 sync_child_counter(sub
, sub
->parent
);
2914 free_counter(child_counter
);
2919 * When a child task exits, feed back counter values to parent counters.
2921 * Note: we may be running in child context, but the PID is not hashed
2922 * anymore so new counters will not be added.
2924 void perf_counter_exit_task(struct task_struct
*child
)
2926 struct perf_counter
*child_counter
, *tmp
;
2927 struct perf_counter_context
*child_ctx
;
2929 child_ctx
= &child
->perf_counter_ctx
;
2931 if (likely(!child_ctx
->nr_counters
))
2934 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
2936 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
2940 * Initialize the perf_counter context in task_struct
2942 void perf_counter_init_task(struct task_struct
*child
)
2944 struct perf_counter_context
*child_ctx
, *parent_ctx
;
2945 struct perf_counter
*counter
;
2946 struct task_struct
*parent
= current
;
2948 child_ctx
= &child
->perf_counter_ctx
;
2949 parent_ctx
= &parent
->perf_counter_ctx
;
2951 __perf_counter_init_context(child_ctx
, child
);
2954 * This is executed from the parent task context, so inherit
2955 * counters that have been marked for cloning:
2958 if (likely(!parent_ctx
->nr_counters
))
2962 * Lock the parent list. No need to lock the child - not PID
2963 * hashed yet and not running, so nobody can access it.
2965 mutex_lock(&parent_ctx
->mutex
);
2968 * We dont have to disable NMIs - we are only looking at
2969 * the list, not manipulating it:
2971 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
2972 if (!counter
->hw_event
.inherit
)
2975 if (inherit_group(counter
, parent
,
2976 parent_ctx
, child
, child_ctx
))
2980 mutex_unlock(&parent_ctx
->mutex
);
2983 static void __cpuinit
perf_counter_init_cpu(int cpu
)
2985 struct perf_cpu_context
*cpuctx
;
2987 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2988 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
2990 mutex_lock(&perf_resource_mutex
);
2991 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
2992 mutex_unlock(&perf_resource_mutex
);
2994 hw_perf_counter_setup(cpu
);
2997 #ifdef CONFIG_HOTPLUG_CPU
2998 static void __perf_counter_exit_cpu(void *info
)
3000 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3001 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3002 struct perf_counter
*counter
, *tmp
;
3004 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3005 __perf_counter_remove_from_context(counter
);
3007 static void perf_counter_exit_cpu(int cpu
)
3009 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3010 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3012 mutex_lock(&ctx
->mutex
);
3013 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3014 mutex_unlock(&ctx
->mutex
);
3017 static inline void perf_counter_exit_cpu(int cpu
) { }
3020 static int __cpuinit
3021 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3023 unsigned int cpu
= (long)hcpu
;
3027 case CPU_UP_PREPARE
:
3028 case CPU_UP_PREPARE_FROZEN
:
3029 perf_counter_init_cpu(cpu
);
3032 case CPU_DOWN_PREPARE
:
3033 case CPU_DOWN_PREPARE_FROZEN
:
3034 perf_counter_exit_cpu(cpu
);
3044 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3045 .notifier_call
= perf_cpu_notify
,
3048 static int __init
perf_counter_init(void)
3050 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3051 (void *)(long)smp_processor_id());
3052 register_cpu_notifier(&perf_cpu_nb
);
3056 early_initcall(perf_counter_init
);
3058 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3060 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3064 perf_set_reserve_percpu(struct sysdev_class
*class,
3068 struct perf_cpu_context
*cpuctx
;
3072 err
= strict_strtoul(buf
, 10, &val
);
3075 if (val
> perf_max_counters
)
3078 mutex_lock(&perf_resource_mutex
);
3079 perf_reserved_percpu
= val
;
3080 for_each_online_cpu(cpu
) {
3081 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3082 spin_lock_irq(&cpuctx
->ctx
.lock
);
3083 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3084 perf_max_counters
- perf_reserved_percpu
);
3085 cpuctx
->max_pertask
= mpt
;
3086 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3088 mutex_unlock(&perf_resource_mutex
);
3093 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3095 return sprintf(buf
, "%d\n", perf_overcommit
);
3099 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3104 err
= strict_strtoul(buf
, 10, &val
);
3110 mutex_lock(&perf_resource_mutex
);
3111 perf_overcommit
= val
;
3112 mutex_unlock(&perf_resource_mutex
);
3117 static SYSDEV_CLASS_ATTR(
3120 perf_show_reserve_percpu
,
3121 perf_set_reserve_percpu
3124 static SYSDEV_CLASS_ATTR(
3127 perf_show_overcommit
,
3131 static struct attribute
*perfclass_attrs
[] = {
3132 &attr_reserve_percpu
.attr
,
3133 &attr_overcommit
.attr
,
3137 static struct attribute_group perfclass_attr_group
= {
3138 .attrs
= perfclass_attrs
,
3139 .name
= "perf_counters",
3142 static int __init
perf_counter_sysfs_init(void)
3144 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3145 &perfclass_attr_group
);
3147 device_initcall(perf_counter_sysfs_init
);