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
);
322 update_context_time(ctx
);
325 * If the counter is on, turn it off.
326 * If it is in error state, leave it in error state.
328 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
329 update_context_time(ctx
);
330 update_counter_times(counter
);
331 if (counter
== counter
->group_leader
)
332 group_sched_out(counter
, cpuctx
, ctx
);
334 counter_sched_out(counter
, cpuctx
, ctx
);
335 counter
->state
= PERF_COUNTER_STATE_OFF
;
338 spin_unlock_irqrestore(&ctx
->lock
, flags
);
344 static void perf_counter_disable(struct perf_counter
*counter
)
346 struct perf_counter_context
*ctx
= counter
->ctx
;
347 struct task_struct
*task
= ctx
->task
;
351 * Disable the counter on the cpu that it's on
353 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
359 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
361 spin_lock_irq(&ctx
->lock
);
363 * If the counter is still active, we need to retry the cross-call.
365 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
366 spin_unlock_irq(&ctx
->lock
);
371 * Since we have the lock this context can't be scheduled
372 * in, so we can change the state safely.
374 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
375 update_counter_times(counter
);
376 counter
->state
= PERF_COUNTER_STATE_OFF
;
379 spin_unlock_irq(&ctx
->lock
);
383 * Disable a counter and all its children.
385 static void perf_counter_disable_family(struct perf_counter
*counter
)
387 struct perf_counter
*child
;
389 perf_counter_disable(counter
);
392 * Lock the mutex to protect the list of children
394 mutex_lock(&counter
->mutex
);
395 list_for_each_entry(child
, &counter
->child_list
, child_list
)
396 perf_counter_disable(child
);
397 mutex_unlock(&counter
->mutex
);
401 counter_sched_in(struct perf_counter
*counter
,
402 struct perf_cpu_context
*cpuctx
,
403 struct perf_counter_context
*ctx
,
406 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
409 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
410 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
412 * The new state must be visible before we turn it on in the hardware:
416 if (counter
->hw_ops
->enable(counter
)) {
417 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
422 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
424 if (!is_software_counter(counter
))
425 cpuctx
->active_oncpu
++;
428 if (counter
->hw_event
.exclusive
)
429 cpuctx
->exclusive
= 1;
435 * Return 1 for a group consisting entirely of software counters,
436 * 0 if the group contains any hardware counters.
438 static int is_software_only_group(struct perf_counter
*leader
)
440 struct perf_counter
*counter
;
442 if (!is_software_counter(leader
))
445 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
446 if (!is_software_counter(counter
))
453 * Work out whether we can put this counter group on the CPU now.
455 static int group_can_go_on(struct perf_counter
*counter
,
456 struct perf_cpu_context
*cpuctx
,
460 * Groups consisting entirely of software counters can always go on.
462 if (is_software_only_group(counter
))
465 * If an exclusive group is already on, no other hardware
466 * counters can go on.
468 if (cpuctx
->exclusive
)
471 * If this group is exclusive and there are already
472 * counters on the CPU, it can't go on.
474 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
477 * Otherwise, try to add it if all previous groups were able
483 static void add_counter_to_ctx(struct perf_counter
*counter
,
484 struct perf_counter_context
*ctx
)
486 list_add_counter(counter
, ctx
);
488 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
489 counter
->tstamp_enabled
= ctx
->time
;
490 counter
->tstamp_running
= ctx
->time
;
491 counter
->tstamp_stopped
= ctx
->time
;
495 * Cross CPU call to install and enable a performance counter
497 static void __perf_install_in_context(void *info
)
499 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
500 struct perf_counter
*counter
= info
;
501 struct perf_counter_context
*ctx
= counter
->ctx
;
502 struct perf_counter
*leader
= counter
->group_leader
;
503 int cpu
= smp_processor_id();
509 * If this is a task context, we need to check whether it is
510 * the current task context of this cpu. If not it has been
511 * scheduled out before the smp call arrived.
513 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
516 spin_lock_irqsave(&ctx
->lock
, flags
);
517 update_context_time(ctx
);
520 * Protect the list operation against NMI by disabling the
521 * counters on a global level. NOP for non NMI based counters.
523 perf_flags
= hw_perf_save_disable();
525 add_counter_to_ctx(counter
, ctx
);
528 * Don't put the counter on if it is disabled or if
529 * it is in a group and the group isn't on.
531 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
532 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
536 * An exclusive counter can't go on if there are already active
537 * hardware counters, and no hardware counter can go on if there
538 * is already an exclusive counter on.
540 if (!group_can_go_on(counter
, cpuctx
, 1))
543 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
547 * This counter couldn't go on. If it is in a group
548 * then we have to pull the whole group off.
549 * If the counter group is pinned then put it in error state.
551 if (leader
!= counter
)
552 group_sched_out(leader
, cpuctx
, ctx
);
553 if (leader
->hw_event
.pinned
) {
554 update_group_times(leader
);
555 leader
->state
= PERF_COUNTER_STATE_ERROR
;
559 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
560 cpuctx
->max_pertask
--;
563 hw_perf_restore(perf_flags
);
565 spin_unlock_irqrestore(&ctx
->lock
, flags
);
569 * Attach a performance counter to a context
571 * First we add the counter to the list with the hardware enable bit
572 * in counter->hw_config cleared.
574 * If the counter is attached to a task which is on a CPU we use a smp
575 * call to enable it in the task context. The task might have been
576 * scheduled away, but we check this in the smp call again.
578 * Must be called with ctx->mutex held.
581 perf_install_in_context(struct perf_counter_context
*ctx
,
582 struct perf_counter
*counter
,
585 struct task_struct
*task
= ctx
->task
;
589 * Per cpu counters are installed via an smp call and
590 * the install is always sucessful.
592 smp_call_function_single(cpu
, __perf_install_in_context
,
597 counter
->task
= task
;
599 task_oncpu_function_call(task
, __perf_install_in_context
,
602 spin_lock_irq(&ctx
->lock
);
604 * we need to retry the smp call.
606 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
607 spin_unlock_irq(&ctx
->lock
);
612 * The lock prevents that this context is scheduled in so we
613 * can add the counter safely, if it the call above did not
616 if (list_empty(&counter
->list_entry
))
617 add_counter_to_ctx(counter
, ctx
);
618 spin_unlock_irq(&ctx
->lock
);
622 * Cross CPU call to enable a performance counter
624 static void __perf_counter_enable(void *info
)
626 struct perf_counter
*counter
= info
;
627 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
628 struct perf_counter_context
*ctx
= counter
->ctx
;
629 struct perf_counter
*leader
= counter
->group_leader
;
634 * If this is a per-task counter, need to check whether this
635 * counter's task is the current task on this cpu.
637 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
640 spin_lock_irqsave(&ctx
->lock
, flags
);
641 update_context_time(ctx
);
643 counter
->prev_state
= counter
->state
;
644 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
646 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
647 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
650 * If the counter is in a group and isn't the group leader,
651 * then don't put it on unless the group is on.
653 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
656 if (!group_can_go_on(counter
, cpuctx
, 1))
659 err
= counter_sched_in(counter
, cpuctx
, ctx
,
664 * If this counter can't go on and it's part of a
665 * group, then the whole group has to come off.
667 if (leader
!= counter
)
668 group_sched_out(leader
, cpuctx
, ctx
);
669 if (leader
->hw_event
.pinned
) {
670 update_group_times(leader
);
671 leader
->state
= PERF_COUNTER_STATE_ERROR
;
676 spin_unlock_irqrestore(&ctx
->lock
, flags
);
682 static void perf_counter_enable(struct perf_counter
*counter
)
684 struct perf_counter_context
*ctx
= counter
->ctx
;
685 struct task_struct
*task
= ctx
->task
;
689 * Enable the counter on the cpu that it's on
691 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
696 spin_lock_irq(&ctx
->lock
);
697 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
701 * If the counter is in error state, clear that first.
702 * That way, if we see the counter in error state below, we
703 * know that it has gone back into error state, as distinct
704 * from the task having been scheduled away before the
705 * cross-call arrived.
707 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
708 counter
->state
= PERF_COUNTER_STATE_OFF
;
711 spin_unlock_irq(&ctx
->lock
);
712 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
714 spin_lock_irq(&ctx
->lock
);
717 * If the context is active and the counter is still off,
718 * we need to retry the cross-call.
720 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
724 * Since we have the lock this context can't be scheduled
725 * in, so we can change the state safely.
727 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
728 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
729 counter
->tstamp_enabled
=
730 ctx
->time
- counter
->total_time_enabled
;
733 spin_unlock_irq(&ctx
->lock
);
736 static void perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
738 atomic_add(refresh
, &counter
->event_limit
);
739 perf_counter_enable(counter
);
743 * Enable a counter and all its children.
745 static void perf_counter_enable_family(struct perf_counter
*counter
)
747 struct perf_counter
*child
;
749 perf_counter_enable(counter
);
752 * Lock the mutex to protect the list of children
754 mutex_lock(&counter
->mutex
);
755 list_for_each_entry(child
, &counter
->child_list
, child_list
)
756 perf_counter_enable(child
);
757 mutex_unlock(&counter
->mutex
);
760 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
761 struct perf_cpu_context
*cpuctx
)
763 struct perf_counter
*counter
;
766 spin_lock(&ctx
->lock
);
768 if (likely(!ctx
->nr_counters
))
770 update_context_time(ctx
);
772 flags
= hw_perf_save_disable();
773 if (ctx
->nr_active
) {
774 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
775 group_sched_out(counter
, cpuctx
, ctx
);
777 hw_perf_restore(flags
);
779 spin_unlock(&ctx
->lock
);
783 * Called from scheduler to remove the counters of the current task,
784 * with interrupts disabled.
786 * We stop each counter and update the counter value in counter->count.
788 * This does not protect us against NMI, but disable()
789 * sets the disabled bit in the control field of counter _before_
790 * accessing the counter control register. If a NMI hits, then it will
791 * not restart the counter.
793 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
795 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
796 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
797 struct pt_regs
*regs
;
799 if (likely(!cpuctx
->task_ctx
))
802 update_context_time(ctx
);
804 regs
= task_pt_regs(task
);
805 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
);
806 __perf_counter_sched_out(ctx
, cpuctx
);
808 cpuctx
->task_ctx
= NULL
;
811 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
813 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
817 group_sched_in(struct perf_counter
*group_counter
,
818 struct perf_cpu_context
*cpuctx
,
819 struct perf_counter_context
*ctx
,
822 struct perf_counter
*counter
, *partial_group
;
825 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
828 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
830 return ret
< 0 ? ret
: 0;
832 group_counter
->prev_state
= group_counter
->state
;
833 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
837 * Schedule in siblings as one group (if any):
839 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
840 counter
->prev_state
= counter
->state
;
841 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
842 partial_group
= counter
;
851 * Groups can be scheduled in as one unit only, so undo any
852 * partial group before returning:
854 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
855 if (counter
== partial_group
)
857 counter_sched_out(counter
, cpuctx
, ctx
);
859 counter_sched_out(group_counter
, cpuctx
, ctx
);
865 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
866 struct perf_cpu_context
*cpuctx
, int cpu
)
868 struct perf_counter
*counter
;
872 spin_lock(&ctx
->lock
);
874 if (likely(!ctx
->nr_counters
))
877 ctx
->timestamp
= perf_clock();
879 flags
= hw_perf_save_disable();
882 * First go through the list and put on any pinned groups
883 * in order to give them the best chance of going on.
885 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
886 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
887 !counter
->hw_event
.pinned
)
889 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
892 if (group_can_go_on(counter
, cpuctx
, 1))
893 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
896 * If this pinned group hasn't been scheduled,
897 * put it in error state.
899 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
900 update_group_times(counter
);
901 counter
->state
= PERF_COUNTER_STATE_ERROR
;
905 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
907 * Ignore counters in OFF or ERROR state, and
908 * ignore pinned counters since we did them already.
910 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
911 counter
->hw_event
.pinned
)
915 * Listen to the 'cpu' scheduling filter constraint
918 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
921 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
922 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
926 hw_perf_restore(flags
);
928 spin_unlock(&ctx
->lock
);
932 * Called from scheduler to add the counters of the current task
933 * with interrupts disabled.
935 * We restore the counter value and then enable it.
937 * This does not protect us against NMI, but enable()
938 * sets the enabled bit in the control field of counter _before_
939 * accessing the counter control register. If a NMI hits, then it will
940 * keep the counter running.
942 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
944 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
945 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
947 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
948 cpuctx
->task_ctx
= ctx
;
951 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
953 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
955 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
958 int perf_counter_task_disable(void)
960 struct task_struct
*curr
= current
;
961 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
962 struct perf_counter
*counter
;
967 if (likely(!ctx
->nr_counters
))
970 local_irq_save(flags
);
971 cpu
= smp_processor_id();
973 perf_counter_task_sched_out(curr
, cpu
);
975 spin_lock(&ctx
->lock
);
978 * Disable all the counters:
980 perf_flags
= hw_perf_save_disable();
982 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
983 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
984 update_group_times(counter
);
985 counter
->state
= PERF_COUNTER_STATE_OFF
;
989 hw_perf_restore(perf_flags
);
991 spin_unlock_irqrestore(&ctx
->lock
, flags
);
996 int perf_counter_task_enable(void)
998 struct task_struct
*curr
= current
;
999 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1000 struct perf_counter
*counter
;
1001 unsigned long flags
;
1005 if (likely(!ctx
->nr_counters
))
1008 local_irq_save(flags
);
1009 cpu
= smp_processor_id();
1011 perf_counter_task_sched_out(curr
, cpu
);
1013 spin_lock(&ctx
->lock
);
1016 * Disable all the counters:
1018 perf_flags
= hw_perf_save_disable();
1020 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1021 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1023 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1024 counter
->tstamp_enabled
=
1025 ctx
->time
- counter
->total_time_enabled
;
1026 counter
->hw_event
.disabled
= 0;
1028 hw_perf_restore(perf_flags
);
1030 spin_unlock(&ctx
->lock
);
1032 perf_counter_task_sched_in(curr
, cpu
);
1034 local_irq_restore(flags
);
1040 * Round-robin a context's counters:
1042 static void rotate_ctx(struct perf_counter_context
*ctx
)
1044 struct perf_counter
*counter
;
1047 if (!ctx
->nr_counters
)
1050 spin_lock(&ctx
->lock
);
1052 * Rotate the first entry last (works just fine for group counters too):
1054 perf_flags
= hw_perf_save_disable();
1055 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1056 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1059 hw_perf_restore(perf_flags
);
1061 spin_unlock(&ctx
->lock
);
1064 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1066 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1067 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1068 const int rotate_percpu
= 0;
1071 perf_counter_cpu_sched_out(cpuctx
);
1072 perf_counter_task_sched_out(curr
, cpu
);
1075 rotate_ctx(&cpuctx
->ctx
);
1079 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1080 perf_counter_task_sched_in(curr
, cpu
);
1084 * Cross CPU call to read the hardware counter
1086 static void __read(void *info
)
1088 struct perf_counter
*counter
= info
;
1089 struct perf_counter_context
*ctx
= counter
->ctx
;
1090 unsigned long flags
;
1092 local_irq_save(flags
);
1094 update_context_time(ctx
);
1095 counter
->hw_ops
->read(counter
);
1096 update_counter_times(counter
);
1097 local_irq_restore(flags
);
1100 static u64
perf_counter_read(struct perf_counter
*counter
)
1103 * If counter is enabled and currently active on a CPU, update the
1104 * value in the counter structure:
1106 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1107 smp_call_function_single(counter
->oncpu
,
1108 __read
, counter
, 1);
1109 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1110 update_counter_times(counter
);
1113 return atomic64_read(&counter
->count
);
1116 static void put_context(struct perf_counter_context
*ctx
)
1119 put_task_struct(ctx
->task
);
1122 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1124 struct perf_cpu_context
*cpuctx
;
1125 struct perf_counter_context
*ctx
;
1126 struct task_struct
*task
;
1129 * If cpu is not a wildcard then this is a percpu counter:
1132 /* Must be root to operate on a CPU counter: */
1133 if (!capable(CAP_SYS_ADMIN
))
1134 return ERR_PTR(-EACCES
);
1136 if (cpu
< 0 || cpu
> num_possible_cpus())
1137 return ERR_PTR(-EINVAL
);
1140 * We could be clever and allow to attach a counter to an
1141 * offline CPU and activate it when the CPU comes up, but
1144 if (!cpu_isset(cpu
, cpu_online_map
))
1145 return ERR_PTR(-ENODEV
);
1147 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1157 task
= find_task_by_vpid(pid
);
1159 get_task_struct(task
);
1163 return ERR_PTR(-ESRCH
);
1165 ctx
= &task
->perf_counter_ctx
;
1168 /* Reuse ptrace permission checks for now. */
1169 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1171 return ERR_PTR(-EACCES
);
1177 static void free_counter_rcu(struct rcu_head
*head
)
1179 struct perf_counter
*counter
;
1181 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1185 static void perf_pending_sync(struct perf_counter
*counter
);
1187 static void free_counter(struct perf_counter
*counter
)
1189 perf_pending_sync(counter
);
1191 if (counter
->destroy
)
1192 counter
->destroy(counter
);
1194 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1198 * Called when the last reference to the file is gone.
1200 static int perf_release(struct inode
*inode
, struct file
*file
)
1202 struct perf_counter
*counter
= file
->private_data
;
1203 struct perf_counter_context
*ctx
= counter
->ctx
;
1205 file
->private_data
= NULL
;
1207 mutex_lock(&ctx
->mutex
);
1208 mutex_lock(&counter
->mutex
);
1210 perf_counter_remove_from_context(counter
);
1212 mutex_unlock(&counter
->mutex
);
1213 mutex_unlock(&ctx
->mutex
);
1215 free_counter(counter
);
1222 * Read the performance counter - simple non blocking version for now
1225 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1231 * Return end-of-file for a read on a counter that is in
1232 * error state (i.e. because it was pinned but it couldn't be
1233 * scheduled on to the CPU at some point).
1235 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1238 mutex_lock(&counter
->mutex
);
1239 values
[0] = perf_counter_read(counter
);
1241 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1242 values
[n
++] = counter
->total_time_enabled
+
1243 atomic64_read(&counter
->child_total_time_enabled
);
1244 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1245 values
[n
++] = counter
->total_time_running
+
1246 atomic64_read(&counter
->child_total_time_running
);
1247 mutex_unlock(&counter
->mutex
);
1249 if (count
< n
* sizeof(u64
))
1251 count
= n
* sizeof(u64
);
1253 if (copy_to_user(buf
, values
, count
))
1260 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1262 struct perf_counter
*counter
= file
->private_data
;
1264 return perf_read_hw(counter
, buf
, count
);
1267 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1269 struct perf_counter
*counter
= file
->private_data
;
1270 struct perf_mmap_data
*data
;
1271 unsigned int events
;
1274 data
= rcu_dereference(counter
->data
);
1276 events
= atomic_xchg(&data
->wakeup
, 0);
1281 poll_wait(file
, &counter
->waitq
, wait
);
1286 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1288 struct perf_counter
*counter
= file
->private_data
;
1292 case PERF_COUNTER_IOC_ENABLE
:
1293 perf_counter_enable_family(counter
);
1295 case PERF_COUNTER_IOC_DISABLE
:
1296 perf_counter_disable_family(counter
);
1298 case PERF_COUNTER_IOC_REFRESH
:
1299 perf_counter_refresh(counter
, arg
);
1308 * Callers need to ensure there can be no nesting of this function, otherwise
1309 * the seqlock logic goes bad. We can not serialize this because the arch
1310 * code calls this from NMI context.
1312 void perf_counter_update_userpage(struct perf_counter
*counter
)
1314 struct perf_mmap_data
*data
;
1315 struct perf_counter_mmap_page
*userpg
;
1318 data
= rcu_dereference(counter
->data
);
1322 userpg
= data
->user_page
;
1325 * Disable preemption so as to not let the corresponding user-space
1326 * spin too long if we get preempted.
1331 userpg
->index
= counter
->hw
.idx
;
1332 userpg
->offset
= atomic64_read(&counter
->count
);
1333 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1334 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1343 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1345 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1346 struct perf_mmap_data
*data
;
1347 int ret
= VM_FAULT_SIGBUS
;
1350 data
= rcu_dereference(counter
->data
);
1354 if (vmf
->pgoff
== 0) {
1355 vmf
->page
= virt_to_page(data
->user_page
);
1357 int nr
= vmf
->pgoff
- 1;
1359 if ((unsigned)nr
> data
->nr_pages
)
1362 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1364 get_page(vmf
->page
);
1372 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1374 struct perf_mmap_data
*data
;
1378 WARN_ON(atomic_read(&counter
->mmap_count
));
1380 size
= sizeof(struct perf_mmap_data
);
1381 size
+= nr_pages
* sizeof(void *);
1383 data
= kzalloc(size
, GFP_KERNEL
);
1387 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1388 if (!data
->user_page
)
1389 goto fail_user_page
;
1391 for (i
= 0; i
< nr_pages
; i
++) {
1392 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1393 if (!data
->data_pages
[i
])
1394 goto fail_data_pages
;
1397 data
->nr_pages
= nr_pages
;
1399 rcu_assign_pointer(counter
->data
, data
);
1404 for (i
--; i
>= 0; i
--)
1405 free_page((unsigned long)data
->data_pages
[i
]);
1407 free_page((unsigned long)data
->user_page
);
1416 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1418 struct perf_mmap_data
*data
= container_of(rcu_head
,
1419 struct perf_mmap_data
, rcu_head
);
1422 free_page((unsigned long)data
->user_page
);
1423 for (i
= 0; i
< data
->nr_pages
; i
++)
1424 free_page((unsigned long)data
->data_pages
[i
]);
1428 static void perf_mmap_data_free(struct perf_counter
*counter
)
1430 struct perf_mmap_data
*data
= counter
->data
;
1432 WARN_ON(atomic_read(&counter
->mmap_count
));
1434 rcu_assign_pointer(counter
->data
, NULL
);
1435 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1438 static void perf_mmap_open(struct vm_area_struct
*vma
)
1440 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1442 atomic_inc(&counter
->mmap_count
);
1445 static void perf_mmap_close(struct vm_area_struct
*vma
)
1447 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1449 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1450 &counter
->mmap_mutex
)) {
1451 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_pages
+ 1;
1452 perf_mmap_data_free(counter
);
1453 mutex_unlock(&counter
->mmap_mutex
);
1457 static struct vm_operations_struct perf_mmap_vmops
= {
1458 .open
= perf_mmap_open
,
1459 .close
= perf_mmap_close
,
1460 .fault
= perf_mmap_fault
,
1463 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1465 struct perf_counter
*counter
= file
->private_data
;
1466 unsigned long vma_size
;
1467 unsigned long nr_pages
;
1468 unsigned long locked
, lock_limit
;
1471 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1474 vma_size
= vma
->vm_end
- vma
->vm_start
;
1475 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1478 * If we have data pages ensure they're a power-of-two number, so we
1479 * can do bitmasks instead of modulo.
1481 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1484 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1487 if (vma
->vm_pgoff
!= 0)
1490 mutex_lock(&counter
->mmap_mutex
);
1491 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1492 if (nr_pages
!= counter
->data
->nr_pages
)
1497 locked
= vma
->vm_mm
->locked_vm
;
1498 locked
+= nr_pages
+ 1;
1500 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1501 lock_limit
>>= PAGE_SHIFT
;
1503 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1508 WARN_ON(counter
->data
);
1509 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1513 atomic_set(&counter
->mmap_count
, 1);
1514 vma
->vm_mm
->locked_vm
+= nr_pages
+ 1;
1516 mutex_unlock(&counter
->mmap_mutex
);
1518 vma
->vm_flags
&= ~VM_MAYWRITE
;
1519 vma
->vm_flags
|= VM_RESERVED
;
1520 vma
->vm_ops
= &perf_mmap_vmops
;
1525 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1527 struct perf_counter
*counter
= filp
->private_data
;
1528 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1531 mutex_lock(&inode
->i_mutex
);
1532 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1533 mutex_unlock(&inode
->i_mutex
);
1541 static const struct file_operations perf_fops
= {
1542 .release
= perf_release
,
1545 .unlocked_ioctl
= perf_ioctl
,
1546 .compat_ioctl
= perf_ioctl
,
1548 .fasync
= perf_fasync
,
1552 * Perf counter wakeup
1554 * If there's data, ensure we set the poll() state and publish everything
1555 * to user-space before waking everybody up.
1558 void perf_counter_wakeup(struct perf_counter
*counter
)
1560 struct perf_mmap_data
*data
;
1563 data
= rcu_dereference(counter
->data
);
1565 atomic_set(&data
->wakeup
, POLL_IN
);
1567 * Ensure all data writes are issued before updating the
1568 * user-space data head information. The matching rmb()
1569 * will be in userspace after reading this value.
1572 data
->user_page
->data_head
= atomic_read(&data
->head
);
1576 wake_up_all(&counter
->waitq
);
1578 if (counter
->pending_kill
) {
1579 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1580 counter
->pending_kill
= 0;
1587 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1589 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1590 * single linked list and use cmpxchg() to add entries lockless.
1593 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1595 struct perf_counter
*counter
= container_of(entry
,
1596 struct perf_counter
, pending
);
1598 if (counter
->pending_disable
) {
1599 counter
->pending_disable
= 0;
1600 perf_counter_disable(counter
);
1603 if (counter
->pending_wakeup
) {
1604 counter
->pending_wakeup
= 0;
1605 perf_counter_wakeup(counter
);
1609 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1611 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1615 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1616 void (*func
)(struct perf_pending_entry
*))
1618 struct perf_pending_entry
**head
;
1620 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1625 head
= &get_cpu_var(perf_pending_head
);
1628 entry
->next
= *head
;
1629 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1631 set_perf_counter_pending();
1633 put_cpu_var(perf_pending_head
);
1636 static int __perf_pending_run(void)
1638 struct perf_pending_entry
*list
;
1641 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1642 while (list
!= PENDING_TAIL
) {
1643 void (*func
)(struct perf_pending_entry
*);
1644 struct perf_pending_entry
*entry
= list
;
1651 * Ensure we observe the unqueue before we issue the wakeup,
1652 * so that we won't be waiting forever.
1653 * -- see perf_not_pending().
1664 static inline int perf_not_pending(struct perf_counter
*counter
)
1667 * If we flush on whatever cpu we run, there is a chance we don't
1671 __perf_pending_run();
1675 * Ensure we see the proper queue state before going to sleep
1676 * so that we do not miss the wakeup. -- see perf_pending_handle()
1679 return counter
->pending
.next
== NULL
;
1682 static void perf_pending_sync(struct perf_counter
*counter
)
1684 wait_event(counter
->waitq
, perf_not_pending(counter
));
1687 void perf_counter_do_pending(void)
1689 __perf_pending_run();
1693 * Callchain support -- arch specific
1696 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1705 struct perf_output_handle
{
1706 struct perf_counter
*counter
;
1707 struct perf_mmap_data
*data
;
1708 unsigned int offset
;
1715 static inline void __perf_output_wakeup(struct perf_output_handle
*handle
)
1718 handle
->counter
->pending_wakeup
= 1;
1719 perf_pending_queue(&handle
->counter
->pending
,
1720 perf_pending_counter
);
1722 perf_counter_wakeup(handle
->counter
);
1725 static int perf_output_begin(struct perf_output_handle
*handle
,
1726 struct perf_counter
*counter
, unsigned int size
,
1727 int nmi
, int overflow
)
1729 struct perf_mmap_data
*data
;
1730 unsigned int offset
, head
;
1733 data
= rcu_dereference(counter
->data
);
1737 handle
->counter
= counter
;
1739 handle
->overflow
= overflow
;
1741 if (!data
->nr_pages
)
1745 offset
= head
= atomic_read(&data
->head
);
1747 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1749 handle
->data
= data
;
1750 handle
->offset
= offset
;
1751 handle
->head
= head
;
1752 handle
->wakeup
= (offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
);
1757 __perf_output_wakeup(handle
);
1764 static void perf_output_copy(struct perf_output_handle
*handle
,
1765 void *buf
, unsigned int len
)
1767 unsigned int pages_mask
;
1768 unsigned int offset
;
1772 offset
= handle
->offset
;
1773 pages_mask
= handle
->data
->nr_pages
- 1;
1774 pages
= handle
->data
->data_pages
;
1777 unsigned int page_offset
;
1780 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1781 page_offset
= offset
& (PAGE_SIZE
- 1);
1782 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1784 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1791 handle
->offset
= offset
;
1793 WARN_ON_ONCE(handle
->offset
> handle
->head
);
1796 #define perf_output_put(handle, x) \
1797 perf_output_copy((handle), &(x), sizeof(x))
1799 static void perf_output_end(struct perf_output_handle
*handle
)
1801 int wakeup_events
= handle
->counter
->hw_event
.wakeup_events
;
1803 if (handle
->overflow
&& wakeup_events
) {
1804 int events
= atomic_inc_return(&handle
->data
->events
);
1805 if (events
>= wakeup_events
) {
1806 atomic_sub(wakeup_events
, &handle
->data
->events
);
1807 __perf_output_wakeup(handle
);
1809 } else if (handle
->wakeup
)
1810 __perf_output_wakeup(handle
);
1814 static void perf_counter_output(struct perf_counter
*counter
,
1815 int nmi
, struct pt_regs
*regs
)
1818 u64 record_type
= counter
->hw_event
.record_type
;
1819 struct perf_output_handle handle
;
1820 struct perf_event_header header
;
1829 struct perf_callchain_entry
*callchain
= NULL
;
1830 int callchain_size
= 0;
1833 header
.type
= PERF_EVENT_COUNTER_OVERFLOW
;
1834 header
.size
= sizeof(header
);
1836 if (record_type
& PERF_RECORD_IP
) {
1837 ip
= instruction_pointer(regs
);
1838 header
.type
|= __PERF_EVENT_IP
;
1839 header
.size
+= sizeof(ip
);
1842 if (record_type
& PERF_RECORD_TID
) {
1843 /* namespace issues */
1844 tid_entry
.pid
= current
->group_leader
->pid
;
1845 tid_entry
.tid
= current
->pid
;
1847 header
.type
|= __PERF_EVENT_TID
;
1848 header
.size
+= sizeof(tid_entry
);
1851 if (record_type
& PERF_RECORD_GROUP
) {
1852 header
.type
|= __PERF_EVENT_GROUP
;
1853 header
.size
+= sizeof(u64
) +
1854 counter
->nr_siblings
* sizeof(group_entry
);
1857 if (record_type
& PERF_RECORD_CALLCHAIN
) {
1858 callchain
= perf_callchain(regs
);
1861 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
1863 header
.type
|= __PERF_EVENT_CALLCHAIN
;
1864 header
.size
+= callchain_size
;
1868 if (record_type
& PERF_RECORD_TIME
) {
1870 * Maybe do better on x86 and provide cpu_clock_nmi()
1872 time
= sched_clock();
1874 header
.type
|= __PERF_EVENT_TIME
;
1875 header
.size
+= sizeof(u64
);
1878 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
1882 perf_output_put(&handle
, header
);
1884 if (record_type
& PERF_RECORD_IP
)
1885 perf_output_put(&handle
, ip
);
1887 if (record_type
& PERF_RECORD_TID
)
1888 perf_output_put(&handle
, tid_entry
);
1890 if (record_type
& PERF_RECORD_GROUP
) {
1891 struct perf_counter
*leader
, *sub
;
1892 u64 nr
= counter
->nr_siblings
;
1894 perf_output_put(&handle
, nr
);
1896 leader
= counter
->group_leader
;
1897 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1899 sub
->hw_ops
->read(sub
);
1901 group_entry
.event
= sub
->hw_event
.config
;
1902 group_entry
.counter
= atomic64_read(&sub
->count
);
1904 perf_output_put(&handle
, group_entry
);
1909 perf_output_copy(&handle
, callchain
, callchain_size
);
1911 if (record_type
& PERF_RECORD_TIME
)
1912 perf_output_put(&handle
, time
);
1914 perf_output_end(&handle
);
1921 struct perf_mmap_event
{
1927 struct perf_event_header header
;
1937 static void perf_counter_mmap_output(struct perf_counter
*counter
,
1938 struct perf_mmap_event
*mmap_event
)
1940 struct perf_output_handle handle
;
1941 int size
= mmap_event
->event
.header
.size
;
1942 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
1947 perf_output_put(&handle
, mmap_event
->event
);
1948 perf_output_copy(&handle
, mmap_event
->file_name
,
1949 mmap_event
->file_size
);
1950 perf_output_end(&handle
);
1953 static int perf_counter_mmap_match(struct perf_counter
*counter
,
1954 struct perf_mmap_event
*mmap_event
)
1956 if (counter
->hw_event
.mmap
&&
1957 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
1960 if (counter
->hw_event
.munmap
&&
1961 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
1967 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
1968 struct perf_mmap_event
*mmap_event
)
1970 struct perf_counter
*counter
;
1972 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
1976 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
1977 if (perf_counter_mmap_match(counter
, mmap_event
))
1978 perf_counter_mmap_output(counter
, mmap_event
);
1983 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
1985 struct perf_cpu_context
*cpuctx
;
1986 struct file
*file
= mmap_event
->file
;
1993 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
1995 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
1998 name
= dentry_path(file
->f_dentry
, buf
, PATH_MAX
);
2000 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2004 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2009 size
= ALIGN(strlen(name
), sizeof(u64
));
2011 mmap_event
->file_name
= name
;
2012 mmap_event
->file_size
= size
;
2014 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2016 cpuctx
= &get_cpu_var(perf_cpu_context
);
2017 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2018 put_cpu_var(perf_cpu_context
);
2020 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2025 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2026 unsigned long pgoff
, struct file
*file
)
2028 struct perf_mmap_event mmap_event
= {
2031 .header
= { .type
= PERF_EVENT_MMAP
, },
2032 .pid
= current
->group_leader
->pid
,
2033 .tid
= current
->pid
,
2040 perf_counter_mmap_event(&mmap_event
);
2043 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2044 unsigned long pgoff
, struct file
*file
)
2046 struct perf_mmap_event mmap_event
= {
2049 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2050 .pid
= current
->group_leader
->pid
,
2051 .tid
= current
->pid
,
2058 perf_counter_mmap_event(&mmap_event
);
2062 * Generic counter overflow handling.
2065 int perf_counter_overflow(struct perf_counter
*counter
,
2066 int nmi
, struct pt_regs
*regs
)
2068 int events
= atomic_read(&counter
->event_limit
);
2071 counter
->pending_kill
= POLL_IN
;
2072 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2074 counter
->pending_kill
= POLL_HUP
;
2076 counter
->pending_disable
= 1;
2077 perf_pending_queue(&counter
->pending
,
2078 perf_pending_counter
);
2080 perf_counter_disable(counter
);
2083 perf_counter_output(counter
, nmi
, regs
);
2088 * Generic software counter infrastructure
2091 static void perf_swcounter_update(struct perf_counter
*counter
)
2093 struct hw_perf_counter
*hwc
= &counter
->hw
;
2098 prev
= atomic64_read(&hwc
->prev_count
);
2099 now
= atomic64_read(&hwc
->count
);
2100 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2105 atomic64_add(delta
, &counter
->count
);
2106 atomic64_sub(delta
, &hwc
->period_left
);
2109 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2111 struct hw_perf_counter
*hwc
= &counter
->hw
;
2112 s64 left
= atomic64_read(&hwc
->period_left
);
2113 s64 period
= hwc
->irq_period
;
2115 if (unlikely(left
<= -period
)) {
2117 atomic64_set(&hwc
->period_left
, left
);
2120 if (unlikely(left
<= 0)) {
2122 atomic64_add(period
, &hwc
->period_left
);
2125 atomic64_set(&hwc
->prev_count
, -left
);
2126 atomic64_set(&hwc
->count
, -left
);
2129 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2131 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2132 struct perf_counter
*counter
;
2133 struct pt_regs
*regs
;
2135 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2136 counter
->hw_ops
->read(counter
);
2138 regs
= get_irq_regs();
2140 * In case we exclude kernel IPs or are somehow not in interrupt
2141 * context, provide the next best thing, the user IP.
2143 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2144 !counter
->hw_event
.exclude_user
)
2145 regs
= task_pt_regs(current
);
2148 if (perf_counter_overflow(counter
, 0, regs
))
2149 ret
= HRTIMER_NORESTART
;
2152 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
2157 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2158 int nmi
, struct pt_regs
*regs
)
2160 perf_swcounter_update(counter
);
2161 perf_swcounter_set_period(counter
);
2162 if (perf_counter_overflow(counter
, nmi
, regs
))
2163 /* soft-disable the counter */
2168 static int perf_swcounter_match(struct perf_counter
*counter
,
2169 enum perf_event_types type
,
2170 u32 event
, struct pt_regs
*regs
)
2172 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2175 if (perf_event_raw(&counter
->hw_event
))
2178 if (perf_event_type(&counter
->hw_event
) != type
)
2181 if (perf_event_id(&counter
->hw_event
) != event
)
2184 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2187 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2193 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2194 int nmi
, struct pt_regs
*regs
)
2196 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2197 if (counter
->hw
.irq_period
&& !neg
)
2198 perf_swcounter_overflow(counter
, nmi
, regs
);
2201 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2202 enum perf_event_types type
, u32 event
,
2203 u64 nr
, int nmi
, struct pt_regs
*regs
)
2205 struct perf_counter
*counter
;
2207 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2211 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2212 if (perf_swcounter_match(counter
, type
, event
, regs
))
2213 perf_swcounter_add(counter
, nr
, nmi
, regs
);
2218 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2221 return &cpuctx
->recursion
[3];
2224 return &cpuctx
->recursion
[2];
2227 return &cpuctx
->recursion
[1];
2229 return &cpuctx
->recursion
[0];
2232 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2233 u64 nr
, int nmi
, struct pt_regs
*regs
)
2235 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2236 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2244 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
, nr
, nmi
, regs
);
2245 if (cpuctx
->task_ctx
) {
2246 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2254 put_cpu_var(perf_cpu_context
);
2257 void perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
)
2259 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
);
2262 static void perf_swcounter_read(struct perf_counter
*counter
)
2264 perf_swcounter_update(counter
);
2267 static int perf_swcounter_enable(struct perf_counter
*counter
)
2269 perf_swcounter_set_period(counter
);
2273 static void perf_swcounter_disable(struct perf_counter
*counter
)
2275 perf_swcounter_update(counter
);
2278 static const struct hw_perf_counter_ops perf_ops_generic
= {
2279 .enable
= perf_swcounter_enable
,
2280 .disable
= perf_swcounter_disable
,
2281 .read
= perf_swcounter_read
,
2285 * Software counter: cpu wall time clock
2288 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2290 int cpu
= raw_smp_processor_id();
2294 now
= cpu_clock(cpu
);
2295 prev
= atomic64_read(&counter
->hw
.prev_count
);
2296 atomic64_set(&counter
->hw
.prev_count
, now
);
2297 atomic64_add(now
- prev
, &counter
->count
);
2300 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2302 struct hw_perf_counter
*hwc
= &counter
->hw
;
2303 int cpu
= raw_smp_processor_id();
2305 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2306 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2307 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2308 if (hwc
->irq_period
) {
2309 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2310 ns_to_ktime(hwc
->irq_period
), 0,
2311 HRTIMER_MODE_REL
, 0);
2317 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2319 hrtimer_cancel(&counter
->hw
.hrtimer
);
2320 cpu_clock_perf_counter_update(counter
);
2323 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2325 cpu_clock_perf_counter_update(counter
);
2328 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
2329 .enable
= cpu_clock_perf_counter_enable
,
2330 .disable
= cpu_clock_perf_counter_disable
,
2331 .read
= cpu_clock_perf_counter_read
,
2335 * Software counter: task time clock
2338 static void task_clock_perf_counter_update(struct perf_counter
*counter
)
2343 now
= counter
->ctx
->time
;
2345 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2347 atomic64_add(delta
, &counter
->count
);
2350 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2352 struct hw_perf_counter
*hwc
= &counter
->hw
;
2355 now
= counter
->ctx
->time
;
2357 atomic64_set(&hwc
->prev_count
, now
);
2358 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2359 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2360 if (hwc
->irq_period
) {
2361 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2362 ns_to_ktime(hwc
->irq_period
), 0,
2363 HRTIMER_MODE_REL
, 0);
2369 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2371 hrtimer_cancel(&counter
->hw
.hrtimer
);
2372 task_clock_perf_counter_update(counter
);
2375 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2377 update_context_time(counter
->ctx
);
2378 task_clock_perf_counter_update(counter
);
2381 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
2382 .enable
= task_clock_perf_counter_enable
,
2383 .disable
= task_clock_perf_counter_disable
,
2384 .read
= task_clock_perf_counter_read
,
2388 * Software counter: cpu migrations
2391 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2393 struct task_struct
*curr
= counter
->ctx
->task
;
2396 return curr
->se
.nr_migrations
;
2397 return cpu_nr_migrations(smp_processor_id());
2400 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2405 prev
= atomic64_read(&counter
->hw
.prev_count
);
2406 now
= get_cpu_migrations(counter
);
2408 atomic64_set(&counter
->hw
.prev_count
, now
);
2412 atomic64_add(delta
, &counter
->count
);
2415 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2417 cpu_migrations_perf_counter_update(counter
);
2420 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2422 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2423 atomic64_set(&counter
->hw
.prev_count
,
2424 get_cpu_migrations(counter
));
2428 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2430 cpu_migrations_perf_counter_update(counter
);
2433 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
2434 .enable
= cpu_migrations_perf_counter_enable
,
2435 .disable
= cpu_migrations_perf_counter_disable
,
2436 .read
= cpu_migrations_perf_counter_read
,
2439 #ifdef CONFIG_EVENT_PROFILE
2440 void perf_tpcounter_event(int event_id
)
2442 struct pt_regs
*regs
= get_irq_regs();
2445 regs
= task_pt_regs(current
);
2447 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
);
2450 extern int ftrace_profile_enable(int);
2451 extern void ftrace_profile_disable(int);
2453 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2455 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2458 static const struct hw_perf_counter_ops
*
2459 tp_perf_counter_init(struct perf_counter
*counter
)
2461 int event_id
= perf_event_id(&counter
->hw_event
);
2464 ret
= ftrace_profile_enable(event_id
);
2468 counter
->destroy
= tp_perf_counter_destroy
;
2469 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2471 return &perf_ops_generic
;
2474 static const struct hw_perf_counter_ops
*
2475 tp_perf_counter_init(struct perf_counter
*counter
)
2481 static const struct hw_perf_counter_ops
*
2482 sw_perf_counter_init(struct perf_counter
*counter
)
2484 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2485 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
2486 struct hw_perf_counter
*hwc
= &counter
->hw
;
2489 * Software counters (currently) can't in general distinguish
2490 * between user, kernel and hypervisor events.
2491 * However, context switches and cpu migrations are considered
2492 * to be kernel events, and page faults are never hypervisor
2495 switch (perf_event_id(&counter
->hw_event
)) {
2496 case PERF_COUNT_CPU_CLOCK
:
2497 hw_ops
= &perf_ops_cpu_clock
;
2499 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2500 hw_event
->irq_period
= 10000;
2502 case PERF_COUNT_TASK_CLOCK
:
2504 * If the user instantiates this as a per-cpu counter,
2505 * use the cpu_clock counter instead.
2507 if (counter
->ctx
->task
)
2508 hw_ops
= &perf_ops_task_clock
;
2510 hw_ops
= &perf_ops_cpu_clock
;
2512 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2513 hw_event
->irq_period
= 10000;
2515 case PERF_COUNT_PAGE_FAULTS
:
2516 case PERF_COUNT_PAGE_FAULTS_MIN
:
2517 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2518 case PERF_COUNT_CONTEXT_SWITCHES
:
2519 hw_ops
= &perf_ops_generic
;
2521 case PERF_COUNT_CPU_MIGRATIONS
:
2522 if (!counter
->hw_event
.exclude_kernel
)
2523 hw_ops
= &perf_ops_cpu_migrations
;
2528 hwc
->irq_period
= hw_event
->irq_period
;
2534 * Allocate and initialize a counter structure
2536 static struct perf_counter
*
2537 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2539 struct perf_counter_context
*ctx
,
2540 struct perf_counter
*group_leader
,
2543 const struct hw_perf_counter_ops
*hw_ops
;
2544 struct perf_counter
*counter
;
2547 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2549 return ERR_PTR(-ENOMEM
);
2552 * Single counters are their own group leaders, with an
2553 * empty sibling list:
2556 group_leader
= counter
;
2558 mutex_init(&counter
->mutex
);
2559 INIT_LIST_HEAD(&counter
->list_entry
);
2560 INIT_LIST_HEAD(&counter
->event_entry
);
2561 INIT_LIST_HEAD(&counter
->sibling_list
);
2562 init_waitqueue_head(&counter
->waitq
);
2564 mutex_init(&counter
->mmap_mutex
);
2566 INIT_LIST_HEAD(&counter
->child_list
);
2569 counter
->hw_event
= *hw_event
;
2570 counter
->group_leader
= group_leader
;
2571 counter
->hw_ops
= NULL
;
2574 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2575 if (hw_event
->disabled
)
2576 counter
->state
= PERF_COUNTER_STATE_OFF
;
2580 if (perf_event_raw(hw_event
)) {
2581 hw_ops
= hw_perf_counter_init(counter
);
2585 switch (perf_event_type(hw_event
)) {
2586 case PERF_TYPE_HARDWARE
:
2587 hw_ops
= hw_perf_counter_init(counter
);
2590 case PERF_TYPE_SOFTWARE
:
2591 hw_ops
= sw_perf_counter_init(counter
);
2594 case PERF_TYPE_TRACEPOINT
:
2595 hw_ops
= tp_perf_counter_init(counter
);
2602 else if (IS_ERR(hw_ops
))
2603 err
= PTR_ERR(hw_ops
);
2607 return ERR_PTR(err
);
2610 counter
->hw_ops
= hw_ops
;
2616 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2618 * @hw_event_uptr: event type attributes for monitoring/sampling
2621 * @group_fd: group leader counter fd
2623 SYSCALL_DEFINE5(perf_counter_open
,
2624 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2625 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2627 struct perf_counter
*counter
, *group_leader
;
2628 struct perf_counter_hw_event hw_event
;
2629 struct perf_counter_context
*ctx
;
2630 struct file
*counter_file
= NULL
;
2631 struct file
*group_file
= NULL
;
2632 int fput_needed
= 0;
2633 int fput_needed2
= 0;
2636 /* for future expandability... */
2640 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2644 * Get the target context (task or percpu):
2646 ctx
= find_get_context(pid
, cpu
);
2648 return PTR_ERR(ctx
);
2651 * Look up the group leader (we will attach this counter to it):
2653 group_leader
= NULL
;
2654 if (group_fd
!= -1) {
2656 group_file
= fget_light(group_fd
, &fput_needed
);
2658 goto err_put_context
;
2659 if (group_file
->f_op
!= &perf_fops
)
2660 goto err_put_context
;
2662 group_leader
= group_file
->private_data
;
2664 * Do not allow a recursive hierarchy (this new sibling
2665 * becoming part of another group-sibling):
2667 if (group_leader
->group_leader
!= group_leader
)
2668 goto err_put_context
;
2670 * Do not allow to attach to a group in a different
2671 * task or CPU context:
2673 if (group_leader
->ctx
!= ctx
)
2674 goto err_put_context
;
2676 * Only a group leader can be exclusive or pinned
2678 if (hw_event
.exclusive
|| hw_event
.pinned
)
2679 goto err_put_context
;
2682 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2684 ret
= PTR_ERR(counter
);
2685 if (IS_ERR(counter
))
2686 goto err_put_context
;
2688 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2690 goto err_free_put_context
;
2692 counter_file
= fget_light(ret
, &fput_needed2
);
2694 goto err_free_put_context
;
2696 counter
->filp
= counter_file
;
2697 mutex_lock(&ctx
->mutex
);
2698 perf_install_in_context(ctx
, counter
, cpu
);
2699 mutex_unlock(&ctx
->mutex
);
2701 fput_light(counter_file
, fput_needed2
);
2704 fput_light(group_file
, fput_needed
);
2708 err_free_put_context
:
2718 * Initialize the perf_counter context in a task_struct:
2721 __perf_counter_init_context(struct perf_counter_context
*ctx
,
2722 struct task_struct
*task
)
2724 memset(ctx
, 0, sizeof(*ctx
));
2725 spin_lock_init(&ctx
->lock
);
2726 mutex_init(&ctx
->mutex
);
2727 INIT_LIST_HEAD(&ctx
->counter_list
);
2728 INIT_LIST_HEAD(&ctx
->event_list
);
2733 * inherit a counter from parent task to child task:
2735 static struct perf_counter
*
2736 inherit_counter(struct perf_counter
*parent_counter
,
2737 struct task_struct
*parent
,
2738 struct perf_counter_context
*parent_ctx
,
2739 struct task_struct
*child
,
2740 struct perf_counter
*group_leader
,
2741 struct perf_counter_context
*child_ctx
)
2743 struct perf_counter
*child_counter
;
2746 * Instead of creating recursive hierarchies of counters,
2747 * we link inherited counters back to the original parent,
2748 * which has a filp for sure, which we use as the reference
2751 if (parent_counter
->parent
)
2752 parent_counter
= parent_counter
->parent
;
2754 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2755 parent_counter
->cpu
, child_ctx
,
2756 group_leader
, GFP_KERNEL
);
2757 if (IS_ERR(child_counter
))
2758 return child_counter
;
2761 * Link it up in the child's context:
2763 child_counter
->task
= child
;
2764 add_counter_to_ctx(child_counter
, child_ctx
);
2766 child_counter
->parent
= parent_counter
;
2768 * inherit into child's child as well:
2770 child_counter
->hw_event
.inherit
= 1;
2773 * Get a reference to the parent filp - we will fput it
2774 * when the child counter exits. This is safe to do because
2775 * we are in the parent and we know that the filp still
2776 * exists and has a nonzero count:
2778 atomic_long_inc(&parent_counter
->filp
->f_count
);
2781 * Link this into the parent counter's child list
2783 mutex_lock(&parent_counter
->mutex
);
2784 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
2787 * Make the child state follow the state of the parent counter,
2788 * not its hw_event.disabled bit. We hold the parent's mutex,
2789 * so we won't race with perf_counter_{en,dis}able_family.
2791 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
2792 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2794 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
2796 mutex_unlock(&parent_counter
->mutex
);
2798 return child_counter
;
2801 static int inherit_group(struct perf_counter
*parent_counter
,
2802 struct task_struct
*parent
,
2803 struct perf_counter_context
*parent_ctx
,
2804 struct task_struct
*child
,
2805 struct perf_counter_context
*child_ctx
)
2807 struct perf_counter
*leader
;
2808 struct perf_counter
*sub
;
2809 struct perf_counter
*child_ctr
;
2811 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
2812 child
, NULL
, child_ctx
);
2814 return PTR_ERR(leader
);
2815 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
2816 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
2817 child
, leader
, child_ctx
);
2818 if (IS_ERR(child_ctr
))
2819 return PTR_ERR(child_ctr
);
2824 static void sync_child_counter(struct perf_counter
*child_counter
,
2825 struct perf_counter
*parent_counter
)
2827 u64 parent_val
, child_val
;
2829 parent_val
= atomic64_read(&parent_counter
->count
);
2830 child_val
= atomic64_read(&child_counter
->count
);
2833 * Add back the child's count to the parent's count:
2835 atomic64_add(child_val
, &parent_counter
->count
);
2836 atomic64_add(child_counter
->total_time_enabled
,
2837 &parent_counter
->child_total_time_enabled
);
2838 atomic64_add(child_counter
->total_time_running
,
2839 &parent_counter
->child_total_time_running
);
2842 * Remove this counter from the parent's list
2844 mutex_lock(&parent_counter
->mutex
);
2845 list_del_init(&child_counter
->child_list
);
2846 mutex_unlock(&parent_counter
->mutex
);
2849 * Release the parent counter, if this was the last
2852 fput(parent_counter
->filp
);
2856 __perf_counter_exit_task(struct task_struct
*child
,
2857 struct perf_counter
*child_counter
,
2858 struct perf_counter_context
*child_ctx
)
2860 struct perf_counter
*parent_counter
;
2861 struct perf_counter
*sub
, *tmp
;
2864 * If we do not self-reap then we have to wait for the
2865 * child task to unschedule (it will happen for sure),
2866 * so that its counter is at its final count. (This
2867 * condition triggers rarely - child tasks usually get
2868 * off their CPU before the parent has a chance to
2869 * get this far into the reaping action)
2871 if (child
!= current
) {
2872 wait_task_inactive(child
, 0);
2873 list_del_init(&child_counter
->list_entry
);
2874 update_counter_times(child_counter
);
2876 struct perf_cpu_context
*cpuctx
;
2877 unsigned long flags
;
2881 * Disable and unlink this counter.
2883 * Be careful about zapping the list - IRQ/NMI context
2884 * could still be processing it:
2886 local_irq_save(flags
);
2887 perf_flags
= hw_perf_save_disable();
2889 cpuctx
= &__get_cpu_var(perf_cpu_context
);
2891 group_sched_out(child_counter
, cpuctx
, child_ctx
);
2892 update_counter_times(child_counter
);
2894 list_del_init(&child_counter
->list_entry
);
2896 child_ctx
->nr_counters
--;
2898 hw_perf_restore(perf_flags
);
2899 local_irq_restore(flags
);
2902 parent_counter
= child_counter
->parent
;
2904 * It can happen that parent exits first, and has counters
2905 * that are still around due to the child reference. These
2906 * counters need to be zapped - but otherwise linger.
2908 if (parent_counter
) {
2909 sync_child_counter(child_counter
, parent_counter
);
2910 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
2913 sync_child_counter(sub
, sub
->parent
);
2917 free_counter(child_counter
);
2922 * When a child task exits, feed back counter values to parent counters.
2924 * Note: we may be running in child context, but the PID is not hashed
2925 * anymore so new counters will not be added.
2927 void perf_counter_exit_task(struct task_struct
*child
)
2929 struct perf_counter
*child_counter
, *tmp
;
2930 struct perf_counter_context
*child_ctx
;
2932 child_ctx
= &child
->perf_counter_ctx
;
2934 if (likely(!child_ctx
->nr_counters
))
2937 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
2939 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
2943 * Initialize the perf_counter context in task_struct
2945 void perf_counter_init_task(struct task_struct
*child
)
2947 struct perf_counter_context
*child_ctx
, *parent_ctx
;
2948 struct perf_counter
*counter
;
2949 struct task_struct
*parent
= current
;
2951 child_ctx
= &child
->perf_counter_ctx
;
2952 parent_ctx
= &parent
->perf_counter_ctx
;
2954 __perf_counter_init_context(child_ctx
, child
);
2957 * This is executed from the parent task context, so inherit
2958 * counters that have been marked for cloning:
2961 if (likely(!parent_ctx
->nr_counters
))
2965 * Lock the parent list. No need to lock the child - not PID
2966 * hashed yet and not running, so nobody can access it.
2968 mutex_lock(&parent_ctx
->mutex
);
2971 * We dont have to disable NMIs - we are only looking at
2972 * the list, not manipulating it:
2974 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
2975 if (!counter
->hw_event
.inherit
)
2978 if (inherit_group(counter
, parent
,
2979 parent_ctx
, child
, child_ctx
))
2983 mutex_unlock(&parent_ctx
->mutex
);
2986 static void __cpuinit
perf_counter_init_cpu(int cpu
)
2988 struct perf_cpu_context
*cpuctx
;
2990 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
2991 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
2993 mutex_lock(&perf_resource_mutex
);
2994 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
2995 mutex_unlock(&perf_resource_mutex
);
2997 hw_perf_counter_setup(cpu
);
3000 #ifdef CONFIG_HOTPLUG_CPU
3001 static void __perf_counter_exit_cpu(void *info
)
3003 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3004 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3005 struct perf_counter
*counter
, *tmp
;
3007 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3008 __perf_counter_remove_from_context(counter
);
3010 static void perf_counter_exit_cpu(int cpu
)
3012 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3013 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3015 mutex_lock(&ctx
->mutex
);
3016 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3017 mutex_unlock(&ctx
->mutex
);
3020 static inline void perf_counter_exit_cpu(int cpu
) { }
3023 static int __cpuinit
3024 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3026 unsigned int cpu
= (long)hcpu
;
3030 case CPU_UP_PREPARE
:
3031 case CPU_UP_PREPARE_FROZEN
:
3032 perf_counter_init_cpu(cpu
);
3035 case CPU_DOWN_PREPARE
:
3036 case CPU_DOWN_PREPARE_FROZEN
:
3037 perf_counter_exit_cpu(cpu
);
3047 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3048 .notifier_call
= perf_cpu_notify
,
3051 static int __init
perf_counter_init(void)
3053 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3054 (void *)(long)smp_processor_id());
3055 register_cpu_notifier(&perf_cpu_nb
);
3059 early_initcall(perf_counter_init
);
3061 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3063 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3067 perf_set_reserve_percpu(struct sysdev_class
*class,
3071 struct perf_cpu_context
*cpuctx
;
3075 err
= strict_strtoul(buf
, 10, &val
);
3078 if (val
> perf_max_counters
)
3081 mutex_lock(&perf_resource_mutex
);
3082 perf_reserved_percpu
= val
;
3083 for_each_online_cpu(cpu
) {
3084 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3085 spin_lock_irq(&cpuctx
->ctx
.lock
);
3086 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3087 perf_max_counters
- perf_reserved_percpu
);
3088 cpuctx
->max_pertask
= mpt
;
3089 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3091 mutex_unlock(&perf_resource_mutex
);
3096 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3098 return sprintf(buf
, "%d\n", perf_overcommit
);
3102 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3107 err
= strict_strtoul(buf
, 10, &val
);
3113 mutex_lock(&perf_resource_mutex
);
3114 perf_overcommit
= val
;
3115 mutex_unlock(&perf_resource_mutex
);
3120 static SYSDEV_CLASS_ATTR(
3123 perf_show_reserve_percpu
,
3124 perf_set_reserve_percpu
3127 static SYSDEV_CLASS_ATTR(
3130 perf_show_overcommit
,
3134 static struct attribute
*perfclass_attrs
[] = {
3135 &attr_reserve_percpu
.attr
,
3136 &attr_overcommit
.attr
,
3140 static struct attribute_group perfclass_attr_group
= {
3141 .attrs
= perfclass_attrs
,
3142 .name
= "perf_counters",
3145 static int __init
perf_counter_sysfs_init(void)
3147 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3148 &perfclass_attr_group
);
3150 device_initcall(perf_counter_sysfs_init
);