2 * Performance counter core code
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
7 * For licencing details see kernel-base/COPYING
11 #include <linux/cpu.h>
12 #include <linux/smp.h>
13 #include <linux/file.h>
14 #include <linux/poll.h>
15 #include <linux/sysfs.h>
16 #include <linux/ptrace.h>
17 #include <linux/percpu.h>
18 #include <linux/uaccess.h>
19 #include <linux/syscalls.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/perf_counter.h>
25 * Each CPU has a list of per CPU counters:
27 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
29 int perf_max_counters __read_mostly
= 1;
30 static int perf_reserved_percpu __read_mostly
;
31 static int perf_overcommit __read_mostly
= 1;
34 * Mutex for (sysadmin-configurable) counter reservations:
36 static DEFINE_MUTEX(perf_resource_mutex
);
39 * Architecture provided APIs - weak aliases:
41 extern __weak
const struct hw_perf_counter_ops
*
42 hw_perf_counter_init(struct perf_counter
*counter
)
47 u64 __weak
hw_perf_save_disable(void) { return 0; }
48 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
49 void __weak
hw_perf_counter_setup(void) { barrier(); }
50 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
51 struct perf_cpu_context
*cpuctx
,
52 struct perf_counter_context
*ctx
, int cpu
)
58 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
60 struct perf_counter
*group_leader
= counter
->group_leader
;
63 * Depending on whether it is a standalone or sibling counter,
64 * add it straight to the context's counter list, or to the group
65 * leader's sibling list:
67 if (counter
->group_leader
== counter
)
68 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
70 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
74 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
76 struct perf_counter
*sibling
, *tmp
;
78 list_del_init(&counter
->list_entry
);
81 * If this was a group counter with sibling counters then
82 * upgrade the siblings to singleton counters by adding them
83 * to the context list directly:
85 list_for_each_entry_safe(sibling
, tmp
,
86 &counter
->sibling_list
, list_entry
) {
88 list_del_init(&sibling
->list_entry
);
89 list_add_tail(&sibling
->list_entry
, &ctx
->counter_list
);
90 sibling
->group_leader
= sibling
;
95 * Cross CPU call to remove a performance counter
97 * We disable the counter on the hardware level first. After that we
98 * remove it from the context list.
100 static void __perf_counter_remove_from_context(void *info
)
102 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
103 struct perf_counter
*counter
= info
;
104 struct perf_counter_context
*ctx
= counter
->ctx
;
109 * If this is a task context, we need to check whether it is
110 * the current task context of this cpu. If not it has been
111 * scheduled out before the smp call arrived.
113 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
116 curr_rq_lock_irq_save(&flags
);
117 spin_lock(&ctx
->lock
);
119 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
120 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
121 counter
->hw_ops
->disable(counter
);
123 cpuctx
->active_oncpu
--;
124 counter
->task
= NULL
;
130 * Protect the list operation against NMI by disabling the
131 * counters on a global level. NOP for non NMI based counters.
133 perf_flags
= hw_perf_save_disable();
134 list_del_counter(counter
, ctx
);
135 hw_perf_restore(perf_flags
);
139 * Allow more per task counters with respect to the
142 cpuctx
->max_pertask
=
143 min(perf_max_counters
- ctx
->nr_counters
,
144 perf_max_counters
- perf_reserved_percpu
);
147 spin_unlock(&ctx
->lock
);
148 curr_rq_unlock_irq_restore(&flags
);
153 * Remove the counter from a task's (or a CPU's) list of counters.
155 * Must be called with counter->mutex held.
157 * CPU counters are removed with a smp call. For task counters we only
158 * call when the task is on a CPU.
160 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
162 struct perf_counter_context
*ctx
= counter
->ctx
;
163 struct task_struct
*task
= ctx
->task
;
167 * Per cpu counters are removed via an smp call and
168 * the removal is always sucessful.
170 smp_call_function_single(counter
->cpu
,
171 __perf_counter_remove_from_context
,
177 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
180 spin_lock_irq(&ctx
->lock
);
182 * If the context is active we need to retry the smp call.
184 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
185 spin_unlock_irq(&ctx
->lock
);
190 * The lock prevents that this context is scheduled in so we
191 * can remove the counter safely, if the call above did not
194 if (!list_empty(&counter
->list_entry
)) {
196 list_del_counter(counter
, ctx
);
197 counter
->task
= NULL
;
199 spin_unlock_irq(&ctx
->lock
);
203 counter_sched_in(struct perf_counter
*counter
,
204 struct perf_cpu_context
*cpuctx
,
205 struct perf_counter_context
*ctx
,
208 if (counter
->state
== PERF_COUNTER_STATE_OFF
)
211 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
212 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
214 * The new state must be visible before we turn it on in the hardware:
218 if (counter
->hw_ops
->enable(counter
)) {
219 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
224 cpuctx
->active_oncpu
++;
231 * Cross CPU call to install and enable a performance counter
233 static void __perf_install_in_context(void *info
)
235 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
236 struct perf_counter
*counter
= info
;
237 struct perf_counter_context
*ctx
= counter
->ctx
;
238 int cpu
= smp_processor_id();
243 * If this is a task context, we need to check whether it is
244 * the current task context of this cpu. If not it has been
245 * scheduled out before the smp call arrived.
247 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
250 curr_rq_lock_irq_save(&flags
);
251 spin_lock(&ctx
->lock
);
254 * Protect the list operation against NMI by disabling the
255 * counters on a global level. NOP for non NMI based counters.
257 perf_flags
= hw_perf_save_disable();
259 list_add_counter(counter
, ctx
);
262 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
264 if (!ctx
->task
&& cpuctx
->max_pertask
)
265 cpuctx
->max_pertask
--;
267 hw_perf_restore(perf_flags
);
269 spin_unlock(&ctx
->lock
);
270 curr_rq_unlock_irq_restore(&flags
);
274 * Attach a performance counter to a context
276 * First we add the counter to the list with the hardware enable bit
277 * in counter->hw_config cleared.
279 * If the counter is attached to a task which is on a CPU we use a smp
280 * call to enable it in the task context. The task might have been
281 * scheduled away, but we check this in the smp call again.
284 perf_install_in_context(struct perf_counter_context
*ctx
,
285 struct perf_counter
*counter
,
288 struct task_struct
*task
= ctx
->task
;
293 * Per cpu counters are installed via an smp call and
294 * the install is always sucessful.
296 smp_call_function_single(cpu
, __perf_install_in_context
,
301 counter
->task
= task
;
303 task_oncpu_function_call(task
, __perf_install_in_context
,
306 spin_lock_irq(&ctx
->lock
);
308 * we need to retry the smp call.
310 if (ctx
->nr_active
&& list_empty(&counter
->list_entry
)) {
311 spin_unlock_irq(&ctx
->lock
);
316 * The lock prevents that this context is scheduled in so we
317 * can add the counter safely, if it the call above did not
320 if (list_empty(&counter
->list_entry
)) {
321 list_add_counter(counter
, ctx
);
324 spin_unlock_irq(&ctx
->lock
);
328 counter_sched_out(struct perf_counter
*counter
,
329 struct perf_cpu_context
*cpuctx
,
330 struct perf_counter_context
*ctx
)
332 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
335 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
336 counter
->hw_ops
->disable(counter
);
339 cpuctx
->active_oncpu
--;
344 group_sched_out(struct perf_counter
*group_counter
,
345 struct perf_cpu_context
*cpuctx
,
346 struct perf_counter_context
*ctx
)
348 struct perf_counter
*counter
;
350 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
353 counter_sched_out(group_counter
, cpuctx
, ctx
);
356 * Schedule out siblings (if any):
358 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
359 counter_sched_out(counter
, cpuctx
, ctx
);
362 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
363 struct perf_cpu_context
*cpuctx
)
365 struct perf_counter
*counter
;
368 if (likely(!ctx
->nr_counters
))
371 spin_lock(&ctx
->lock
);
372 flags
= hw_perf_save_disable();
373 if (ctx
->nr_active
) {
374 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
375 group_sched_out(counter
, cpuctx
, ctx
);
377 hw_perf_restore(flags
);
378 spin_unlock(&ctx
->lock
);
382 * Called from scheduler to remove the counters of the current task,
383 * with interrupts disabled.
385 * We stop each counter and update the counter value in counter->count.
387 * This does not protect us against NMI, but disable()
388 * sets the disabled bit in the control field of counter _before_
389 * accessing the counter control register. If a NMI hits, then it will
390 * not restart the counter.
392 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
394 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
395 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
397 if (likely(!cpuctx
->task_ctx
))
400 __perf_counter_sched_out(ctx
, cpuctx
);
402 cpuctx
->task_ctx
= NULL
;
405 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
407 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
411 group_sched_in(struct perf_counter
*group_counter
,
412 struct perf_cpu_context
*cpuctx
,
413 struct perf_counter_context
*ctx
,
416 struct perf_counter
*counter
, *partial_group
;
419 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
422 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
424 return ret
< 0 ? ret
: 0;
426 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
430 * Schedule in siblings as one group (if any):
432 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
433 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
434 partial_group
= counter
;
443 * Groups can be scheduled in as one unit only, so undo any
444 * partial group before returning:
446 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
447 if (counter
== partial_group
)
449 counter_sched_out(counter
, cpuctx
, ctx
);
451 counter_sched_out(group_counter
, cpuctx
, ctx
);
457 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
458 struct perf_cpu_context
*cpuctx
, int cpu
)
460 struct perf_counter
*counter
;
463 if (likely(!ctx
->nr_counters
))
466 spin_lock(&ctx
->lock
);
467 flags
= hw_perf_save_disable();
468 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
470 * Listen to the 'cpu' scheduling filter constraint
473 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
477 * If we scheduled in a group atomically and exclusively,
478 * or if this group can't go on, break out:
480 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
483 hw_perf_restore(flags
);
484 spin_unlock(&ctx
->lock
);
488 * Called from scheduler to add the counters of the current task
489 * with interrupts disabled.
491 * We restore the counter value and then enable it.
493 * This does not protect us against NMI, but enable()
494 * sets the enabled bit in the control field of counter _before_
495 * accessing the counter control register. If a NMI hits, then it will
496 * keep the counter running.
498 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
500 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
501 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
503 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
504 cpuctx
->task_ctx
= ctx
;
507 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
509 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
511 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
514 int perf_counter_task_disable(void)
516 struct task_struct
*curr
= current
;
517 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
518 struct perf_counter
*counter
;
523 if (likely(!ctx
->nr_counters
))
526 curr_rq_lock_irq_save(&flags
);
527 cpu
= smp_processor_id();
529 /* force the update of the task clock: */
530 __task_delta_exec(curr
, 1);
532 perf_counter_task_sched_out(curr
, cpu
);
534 spin_lock(&ctx
->lock
);
537 * Disable all the counters:
539 perf_flags
= hw_perf_save_disable();
541 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
542 counter
->state
= PERF_COUNTER_STATE_OFF
;
544 hw_perf_restore(perf_flags
);
546 spin_unlock(&ctx
->lock
);
548 curr_rq_unlock_irq_restore(&flags
);
553 int perf_counter_task_enable(void)
555 struct task_struct
*curr
= current
;
556 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
557 struct perf_counter
*counter
;
562 if (likely(!ctx
->nr_counters
))
565 curr_rq_lock_irq_save(&flags
);
566 cpu
= smp_processor_id();
568 /* force the update of the task clock: */
569 __task_delta_exec(curr
, 1);
571 perf_counter_task_sched_out(curr
, cpu
);
573 spin_lock(&ctx
->lock
);
576 * Disable all the counters:
578 perf_flags
= hw_perf_save_disable();
580 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
581 if (counter
->state
!= PERF_COUNTER_STATE_OFF
)
583 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
584 counter
->hw_event
.disabled
= 0;
586 hw_perf_restore(perf_flags
);
588 spin_unlock(&ctx
->lock
);
590 perf_counter_task_sched_in(curr
, cpu
);
592 curr_rq_unlock_irq_restore(&flags
);
598 * Round-robin a context's counters:
600 static void rotate_ctx(struct perf_counter_context
*ctx
)
602 struct perf_counter
*counter
;
605 if (!ctx
->nr_counters
)
608 spin_lock(&ctx
->lock
);
610 * Rotate the first entry last (works just fine for group counters too):
612 perf_flags
= hw_perf_save_disable();
613 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
614 list_del(&counter
->list_entry
);
615 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
618 hw_perf_restore(perf_flags
);
620 spin_unlock(&ctx
->lock
);
623 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
625 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
626 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
627 const int rotate_percpu
= 0;
630 perf_counter_cpu_sched_out(cpuctx
);
631 perf_counter_task_sched_out(curr
, cpu
);
634 rotate_ctx(&cpuctx
->ctx
);
638 perf_counter_cpu_sched_in(cpuctx
, cpu
);
639 perf_counter_task_sched_in(curr
, cpu
);
643 * Cross CPU call to read the hardware counter
645 static void __read(void *info
)
647 struct perf_counter
*counter
= info
;
650 curr_rq_lock_irq_save(&flags
);
651 counter
->hw_ops
->read(counter
);
652 curr_rq_unlock_irq_restore(&flags
);
655 static u64
perf_counter_read(struct perf_counter
*counter
)
658 * If counter is enabled and currently active on a CPU, update the
659 * value in the counter structure:
661 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
662 smp_call_function_single(counter
->oncpu
,
666 return atomic64_read(&counter
->count
);
670 * Cross CPU call to switch performance data pointers
672 static void __perf_switch_irq_data(void *info
)
674 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
675 struct perf_counter
*counter
= info
;
676 struct perf_counter_context
*ctx
= counter
->ctx
;
677 struct perf_data
*oldirqdata
= counter
->irqdata
;
680 * If this is a task context, we need to check whether it is
681 * the current task context of this cpu. If not it has been
682 * scheduled out before the smp call arrived.
685 if (cpuctx
->task_ctx
!= ctx
)
687 spin_lock(&ctx
->lock
);
690 /* Change the pointer NMI safe */
691 atomic_long_set((atomic_long_t
*)&counter
->irqdata
,
692 (unsigned long) counter
->usrdata
);
693 counter
->usrdata
= oldirqdata
;
696 spin_unlock(&ctx
->lock
);
699 static struct perf_data
*perf_switch_irq_data(struct perf_counter
*counter
)
701 struct perf_counter_context
*ctx
= counter
->ctx
;
702 struct perf_data
*oldirqdata
= counter
->irqdata
;
703 struct task_struct
*task
= ctx
->task
;
706 smp_call_function_single(counter
->cpu
,
707 __perf_switch_irq_data
,
709 return counter
->usrdata
;
713 spin_lock_irq(&ctx
->lock
);
714 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
) {
715 counter
->irqdata
= counter
->usrdata
;
716 counter
->usrdata
= oldirqdata
;
717 spin_unlock_irq(&ctx
->lock
);
720 spin_unlock_irq(&ctx
->lock
);
721 task_oncpu_function_call(task
, __perf_switch_irq_data
, counter
);
722 /* Might have failed, because task was scheduled out */
723 if (counter
->irqdata
== oldirqdata
)
726 return counter
->usrdata
;
729 static void put_context(struct perf_counter_context
*ctx
)
732 put_task_struct(ctx
->task
);
735 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
737 struct perf_cpu_context
*cpuctx
;
738 struct perf_counter_context
*ctx
;
739 struct task_struct
*task
;
742 * If cpu is not a wildcard then this is a percpu counter:
745 /* Must be root to operate on a CPU counter: */
746 if (!capable(CAP_SYS_ADMIN
))
747 return ERR_PTR(-EACCES
);
749 if (cpu
< 0 || cpu
> num_possible_cpus())
750 return ERR_PTR(-EINVAL
);
753 * We could be clever and allow to attach a counter to an
754 * offline CPU and activate it when the CPU comes up, but
757 if (!cpu_isset(cpu
, cpu_online_map
))
758 return ERR_PTR(-ENODEV
);
760 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
770 task
= find_task_by_vpid(pid
);
772 get_task_struct(task
);
776 return ERR_PTR(-ESRCH
);
778 ctx
= &task
->perf_counter_ctx
;
781 /* Reuse ptrace permission checks for now. */
782 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
784 return ERR_PTR(-EACCES
);
791 * Called when the last reference to the file is gone.
793 static int perf_release(struct inode
*inode
, struct file
*file
)
795 struct perf_counter
*counter
= file
->private_data
;
796 struct perf_counter_context
*ctx
= counter
->ctx
;
798 file
->private_data
= NULL
;
800 mutex_lock(&counter
->mutex
);
802 perf_counter_remove_from_context(counter
);
805 mutex_unlock(&counter
->mutex
);
813 * Read the performance counter - simple non blocking version for now
816 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
820 if (count
!= sizeof(cntval
))
823 mutex_lock(&counter
->mutex
);
824 cntval
= perf_counter_read(counter
);
825 mutex_unlock(&counter
->mutex
);
827 return put_user(cntval
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(cntval
);
831 perf_copy_usrdata(struct perf_data
*usrdata
, char __user
*buf
, size_t count
)
836 count
= min(count
, (size_t)usrdata
->len
);
837 if (copy_to_user(buf
, usrdata
->data
+ usrdata
->rd_idx
, count
))
840 /* Adjust the counters */
841 usrdata
->len
-= count
;
845 usrdata
->rd_idx
+= count
;
851 perf_read_irq_data(struct perf_counter
*counter
,
856 struct perf_data
*irqdata
, *usrdata
;
857 DECLARE_WAITQUEUE(wait
, current
);
860 irqdata
= counter
->irqdata
;
861 usrdata
= counter
->usrdata
;
863 if (usrdata
->len
+ irqdata
->len
>= count
)
869 spin_lock_irq(&counter
->waitq
.lock
);
870 __add_wait_queue(&counter
->waitq
, &wait
);
872 set_current_state(TASK_INTERRUPTIBLE
);
873 if (usrdata
->len
+ irqdata
->len
>= count
)
876 if (signal_pending(current
))
879 spin_unlock_irq(&counter
->waitq
.lock
);
881 spin_lock_irq(&counter
->waitq
.lock
);
883 __remove_wait_queue(&counter
->waitq
, &wait
);
884 __set_current_state(TASK_RUNNING
);
885 spin_unlock_irq(&counter
->waitq
.lock
);
887 if (usrdata
->len
+ irqdata
->len
< count
)
890 mutex_lock(&counter
->mutex
);
892 /* Drain pending data first: */
893 res
= perf_copy_usrdata(usrdata
, buf
, count
);
894 if (res
< 0 || res
== count
)
897 /* Switch irq buffer: */
898 usrdata
= perf_switch_irq_data(counter
);
899 if (perf_copy_usrdata(usrdata
, buf
+ res
, count
- res
) < 0) {
906 mutex_unlock(&counter
->mutex
);
912 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
914 struct perf_counter
*counter
= file
->private_data
;
916 switch (counter
->hw_event
.record_type
) {
917 case PERF_RECORD_SIMPLE
:
918 return perf_read_hw(counter
, buf
, count
);
920 case PERF_RECORD_IRQ
:
921 case PERF_RECORD_GROUP
:
922 return perf_read_irq_data(counter
, buf
, count
,
923 file
->f_flags
& O_NONBLOCK
);
928 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
930 struct perf_counter
*counter
= file
->private_data
;
931 unsigned int events
= 0;
934 poll_wait(file
, &counter
->waitq
, wait
);
936 spin_lock_irqsave(&counter
->waitq
.lock
, flags
);
937 if (counter
->usrdata
->len
|| counter
->irqdata
->len
)
939 spin_unlock_irqrestore(&counter
->waitq
.lock
, flags
);
944 static const struct file_operations perf_fops
= {
945 .release
= perf_release
,
950 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
952 int cpu
= raw_smp_processor_id();
954 atomic64_set(&counter
->hw
.prev_count
, cpu_clock(cpu
));
958 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
960 int cpu
= raw_smp_processor_id();
964 now
= cpu_clock(cpu
);
965 prev
= atomic64_read(&counter
->hw
.prev_count
);
966 atomic64_set(&counter
->hw
.prev_count
, now
);
967 atomic64_add(now
- prev
, &counter
->count
);
970 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
972 cpu_clock_perf_counter_update(counter
);
975 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
977 cpu_clock_perf_counter_update(counter
);
980 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
981 .enable
= cpu_clock_perf_counter_enable
,
982 .disable
= cpu_clock_perf_counter_disable
,
983 .read
= cpu_clock_perf_counter_read
,
987 * Called from within the scheduler:
989 static u64
task_clock_perf_counter_val(struct perf_counter
*counter
, int update
)
991 struct task_struct
*curr
= counter
->task
;
994 delta
= __task_delta_exec(curr
, update
);
996 return curr
->se
.sum_exec_runtime
+ delta
;
999 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
1004 prev
= atomic64_read(&counter
->hw
.prev_count
);
1006 atomic64_set(&counter
->hw
.prev_count
, now
);
1010 atomic64_add(delta
, &counter
->count
);
1013 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
1015 u64 now
= task_clock_perf_counter_val(counter
, 1);
1017 task_clock_perf_counter_update(counter
, now
);
1020 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
1022 u64 now
= task_clock_perf_counter_val(counter
, 0);
1024 atomic64_set(&counter
->hw
.prev_count
, now
);
1029 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
1031 u64 now
= task_clock_perf_counter_val(counter
, 0);
1033 task_clock_perf_counter_update(counter
, now
);
1036 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
1037 .enable
= task_clock_perf_counter_enable
,
1038 .disable
= task_clock_perf_counter_disable
,
1039 .read
= task_clock_perf_counter_read
,
1042 static u64
get_page_faults(void)
1044 struct task_struct
*curr
= current
;
1046 return curr
->maj_flt
+ curr
->min_flt
;
1049 static void page_faults_perf_counter_update(struct perf_counter
*counter
)
1054 prev
= atomic64_read(&counter
->hw
.prev_count
);
1055 now
= get_page_faults();
1057 atomic64_set(&counter
->hw
.prev_count
, now
);
1061 atomic64_add(delta
, &counter
->count
);
1064 static void page_faults_perf_counter_read(struct perf_counter
*counter
)
1066 page_faults_perf_counter_update(counter
);
1069 static int page_faults_perf_counter_enable(struct perf_counter
*counter
)
1072 * page-faults is a per-task value already,
1073 * so we dont have to clear it on switch-in.
1079 static void page_faults_perf_counter_disable(struct perf_counter
*counter
)
1081 page_faults_perf_counter_update(counter
);
1084 static const struct hw_perf_counter_ops perf_ops_page_faults
= {
1085 .enable
= page_faults_perf_counter_enable
,
1086 .disable
= page_faults_perf_counter_disable
,
1087 .read
= page_faults_perf_counter_read
,
1090 static u64
get_context_switches(void)
1092 struct task_struct
*curr
= current
;
1094 return curr
->nvcsw
+ curr
->nivcsw
;
1097 static void context_switches_perf_counter_update(struct perf_counter
*counter
)
1102 prev
= atomic64_read(&counter
->hw
.prev_count
);
1103 now
= get_context_switches();
1105 atomic64_set(&counter
->hw
.prev_count
, now
);
1109 atomic64_add(delta
, &counter
->count
);
1112 static void context_switches_perf_counter_read(struct perf_counter
*counter
)
1114 context_switches_perf_counter_update(counter
);
1117 static int context_switches_perf_counter_enable(struct perf_counter
*counter
)
1120 * ->nvcsw + curr->nivcsw is a per-task value already,
1121 * so we dont have to clear it on switch-in.
1127 static void context_switches_perf_counter_disable(struct perf_counter
*counter
)
1129 context_switches_perf_counter_update(counter
);
1132 static const struct hw_perf_counter_ops perf_ops_context_switches
= {
1133 .enable
= context_switches_perf_counter_enable
,
1134 .disable
= context_switches_perf_counter_disable
,
1135 .read
= context_switches_perf_counter_read
,
1138 static inline u64
get_cpu_migrations(void)
1140 return current
->se
.nr_migrations
;
1143 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
1148 prev
= atomic64_read(&counter
->hw
.prev_count
);
1149 now
= get_cpu_migrations();
1151 atomic64_set(&counter
->hw
.prev_count
, now
);
1155 atomic64_add(delta
, &counter
->count
);
1158 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
1160 cpu_migrations_perf_counter_update(counter
);
1163 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
1166 * se.nr_migrations is a per-task value already,
1167 * so we dont have to clear it on switch-in.
1173 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
1175 cpu_migrations_perf_counter_update(counter
);
1178 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
1179 .enable
= cpu_migrations_perf_counter_enable
,
1180 .disable
= cpu_migrations_perf_counter_disable
,
1181 .read
= cpu_migrations_perf_counter_read
,
1184 static const struct hw_perf_counter_ops
*
1185 sw_perf_counter_init(struct perf_counter
*counter
)
1187 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
1189 switch (counter
->hw_event
.type
) {
1190 case PERF_COUNT_CPU_CLOCK
:
1191 hw_ops
= &perf_ops_cpu_clock
;
1193 case PERF_COUNT_TASK_CLOCK
:
1194 hw_ops
= &perf_ops_task_clock
;
1196 case PERF_COUNT_PAGE_FAULTS
:
1197 hw_ops
= &perf_ops_page_faults
;
1199 case PERF_COUNT_CONTEXT_SWITCHES
:
1200 hw_ops
= &perf_ops_context_switches
;
1202 case PERF_COUNT_CPU_MIGRATIONS
:
1203 hw_ops
= &perf_ops_cpu_migrations
;
1212 * Allocate and initialize a counter structure
1214 static struct perf_counter
*
1215 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
1217 struct perf_counter
*group_leader
,
1220 const struct hw_perf_counter_ops
*hw_ops
;
1221 struct perf_counter
*counter
;
1223 counter
= kzalloc(sizeof(*counter
), gfpflags
);
1228 * Single counters are their own group leaders, with an
1229 * empty sibling list:
1232 group_leader
= counter
;
1234 mutex_init(&counter
->mutex
);
1235 INIT_LIST_HEAD(&counter
->list_entry
);
1236 INIT_LIST_HEAD(&counter
->sibling_list
);
1237 init_waitqueue_head(&counter
->waitq
);
1239 counter
->irqdata
= &counter
->data
[0];
1240 counter
->usrdata
= &counter
->data
[1];
1242 counter
->hw_event
= *hw_event
;
1243 counter
->wakeup_pending
= 0;
1244 counter
->group_leader
= group_leader
;
1245 counter
->hw_ops
= NULL
;
1247 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1248 if (hw_event
->disabled
)
1249 counter
->state
= PERF_COUNTER_STATE_OFF
;
1252 if (!hw_event
->raw
&& hw_event
->type
< 0)
1253 hw_ops
= sw_perf_counter_init(counter
);
1255 hw_ops
= hw_perf_counter_init(counter
);
1261 counter
->hw_ops
= hw_ops
;
1267 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1269 * @hw_event_uptr: event type attributes for monitoring/sampling
1272 * @group_fd: group leader counter fd
1275 sys_perf_counter_open(struct perf_counter_hw_event
*hw_event_uptr __user
,
1276 pid_t pid
, int cpu
, int group_fd
)
1278 struct perf_counter
*counter
, *group_leader
;
1279 struct perf_counter_hw_event hw_event
;
1280 struct perf_counter_context
*ctx
;
1281 struct file
*counter_file
= NULL
;
1282 struct file
*group_file
= NULL
;
1283 int fput_needed
= 0;
1284 int fput_needed2
= 0;
1287 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
1291 * Get the target context (task or percpu):
1293 ctx
= find_get_context(pid
, cpu
);
1295 return PTR_ERR(ctx
);
1298 * Look up the group leader (we will attach this counter to it):
1300 group_leader
= NULL
;
1301 if (group_fd
!= -1) {
1303 group_file
= fget_light(group_fd
, &fput_needed
);
1305 goto err_put_context
;
1306 if (group_file
->f_op
!= &perf_fops
)
1307 goto err_put_context
;
1309 group_leader
= group_file
->private_data
;
1311 * Do not allow a recursive hierarchy (this new sibling
1312 * becoming part of another group-sibling):
1314 if (group_leader
->group_leader
!= group_leader
)
1315 goto err_put_context
;
1317 * Do not allow to attach to a group in a different
1318 * task or CPU context:
1320 if (group_leader
->ctx
!= ctx
)
1321 goto err_put_context
;
1325 counter
= perf_counter_alloc(&hw_event
, cpu
, group_leader
, GFP_KERNEL
);
1327 goto err_put_context
;
1329 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
1331 goto err_free_put_context
;
1333 counter_file
= fget_light(ret
, &fput_needed2
);
1335 goto err_free_put_context
;
1337 counter
->filp
= counter_file
;
1338 perf_install_in_context(ctx
, counter
, cpu
);
1340 fput_light(counter_file
, fput_needed2
);
1343 fput_light(group_file
, fput_needed
);
1347 err_free_put_context
:
1357 * Initialize the perf_counter context in a task_struct:
1360 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1361 struct task_struct
*task
)
1363 memset(ctx
, 0, sizeof(*ctx
));
1364 spin_lock_init(&ctx
->lock
);
1365 INIT_LIST_HEAD(&ctx
->counter_list
);
1370 * inherit a counter from parent task to child task:
1373 inherit_counter(struct perf_counter
*parent_counter
,
1374 struct task_struct
*parent
,
1375 struct perf_counter_context
*parent_ctx
,
1376 struct task_struct
*child
,
1377 struct perf_counter_context
*child_ctx
)
1379 struct perf_counter
*child_counter
;
1381 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
1382 parent_counter
->cpu
, NULL
,
1388 * Link it up in the child's context:
1390 child_counter
->ctx
= child_ctx
;
1391 child_counter
->task
= child
;
1392 list_add_counter(child_counter
, child_ctx
);
1393 child_ctx
->nr_counters
++;
1395 child_counter
->parent
= parent_counter
;
1397 * inherit into child's child as well:
1399 child_counter
->hw_event
.inherit
= 1;
1402 * Get a reference to the parent filp - we will fput it
1403 * when the child counter exits. This is safe to do because
1404 * we are in the parent and we know that the filp still
1405 * exists and has a nonzero count:
1407 atomic_long_inc(&parent_counter
->filp
->f_count
);
1413 __perf_counter_exit_task(struct task_struct
*child
,
1414 struct perf_counter
*child_counter
,
1415 struct perf_counter_context
*child_ctx
)
1417 struct perf_counter
*parent_counter
;
1418 u64 parent_val
, child_val
;
1421 * If we do not self-reap then we have to wait for the
1422 * child task to unschedule (it will happen for sure),
1423 * so that its counter is at its final count. (This
1424 * condition triggers rarely - child tasks usually get
1425 * off their CPU before the parent has a chance to
1426 * get this far into the reaping action)
1428 if (child
!= current
) {
1429 wait_task_inactive(child
, 0);
1430 list_del_init(&child_counter
->list_entry
);
1432 struct perf_cpu_context
*cpuctx
;
1433 unsigned long flags
;
1437 * Disable and unlink this counter.
1439 * Be careful about zapping the list - IRQ/NMI context
1440 * could still be processing it:
1442 curr_rq_lock_irq_save(&flags
);
1443 perf_flags
= hw_perf_save_disable();
1445 cpuctx
= &__get_cpu_var(perf_cpu_context
);
1447 if (child_counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1448 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1449 child_counter
->hw_ops
->disable(child_counter
);
1450 cpuctx
->active_oncpu
--;
1451 child_ctx
->nr_active
--;
1452 child_counter
->oncpu
= -1;
1455 list_del_init(&child_counter
->list_entry
);
1457 child_ctx
->nr_counters
--;
1459 hw_perf_restore(perf_flags
);
1460 curr_rq_unlock_irq_restore(&flags
);
1463 parent_counter
= child_counter
->parent
;
1465 * It can happen that parent exits first, and has counters
1466 * that are still around due to the child reference. These
1467 * counters need to be zapped - but otherwise linger.
1469 if (!parent_counter
)
1472 parent_val
= atomic64_read(&parent_counter
->count
);
1473 child_val
= atomic64_read(&child_counter
->count
);
1476 * Add back the child's count to the parent's count:
1478 atomic64_add(child_val
, &parent_counter
->count
);
1480 fput(parent_counter
->filp
);
1482 kfree(child_counter
);
1486 * When a child task exist, feed back counter values to parent counters.
1488 * Note: we are running in child context, but the PID is not hashed
1489 * anymore so new counters will not be added.
1491 void perf_counter_exit_task(struct task_struct
*child
)
1493 struct perf_counter
*child_counter
, *tmp
;
1494 struct perf_counter_context
*child_ctx
;
1496 child_ctx
= &child
->perf_counter_ctx
;
1498 if (likely(!child_ctx
->nr_counters
))
1501 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
1503 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
1507 * Initialize the perf_counter context in task_struct
1509 void perf_counter_init_task(struct task_struct
*child
)
1511 struct perf_counter_context
*child_ctx
, *parent_ctx
;
1512 struct perf_counter
*counter
, *parent_counter
;
1513 struct task_struct
*parent
= current
;
1514 unsigned long flags
;
1516 child_ctx
= &child
->perf_counter_ctx
;
1517 parent_ctx
= &parent
->perf_counter_ctx
;
1519 __perf_counter_init_context(child_ctx
, child
);
1522 * This is executed from the parent task context, so inherit
1523 * counters that have been marked for cloning:
1526 if (likely(!parent_ctx
->nr_counters
))
1530 * Lock the parent list. No need to lock the child - not PID
1531 * hashed yet and not running, so nobody can access it.
1533 spin_lock_irqsave(&parent_ctx
->lock
, flags
);
1536 * We dont have to disable NMIs - we are only looking at
1537 * the list, not manipulating it:
1539 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
1540 if (!counter
->hw_event
.inherit
|| counter
->group_leader
!= counter
)
1544 * Instead of creating recursive hierarchies of counters,
1545 * we link inheritd counters back to the original parent,
1546 * which has a filp for sure, which we use as the reference
1549 parent_counter
= counter
;
1550 if (counter
->parent
)
1551 parent_counter
= counter
->parent
;
1553 if (inherit_counter(parent_counter
, parent
,
1554 parent_ctx
, child
, child_ctx
))
1558 spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
1561 static void __cpuinit
perf_counter_init_cpu(int cpu
)
1563 struct perf_cpu_context
*cpuctx
;
1565 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1566 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
1568 mutex_lock(&perf_resource_mutex
);
1569 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
1570 mutex_unlock(&perf_resource_mutex
);
1572 hw_perf_counter_setup();
1575 #ifdef CONFIG_HOTPLUG_CPU
1576 static void __perf_counter_exit_cpu(void *info
)
1578 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1579 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1580 struct perf_counter
*counter
, *tmp
;
1582 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
1583 __perf_counter_remove_from_context(counter
);
1586 static void perf_counter_exit_cpu(int cpu
)
1588 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
1591 static inline void perf_counter_exit_cpu(int cpu
) { }
1594 static int __cpuinit
1595 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
1597 unsigned int cpu
= (long)hcpu
;
1601 case CPU_UP_PREPARE
:
1602 case CPU_UP_PREPARE_FROZEN
:
1603 perf_counter_init_cpu(cpu
);
1606 case CPU_DOWN_PREPARE
:
1607 case CPU_DOWN_PREPARE_FROZEN
:
1608 perf_counter_exit_cpu(cpu
);
1618 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
1619 .notifier_call
= perf_cpu_notify
,
1622 static int __init
perf_counter_init(void)
1624 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
1625 (void *)(long)smp_processor_id());
1626 register_cpu_notifier(&perf_cpu_nb
);
1630 early_initcall(perf_counter_init
);
1632 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
1634 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
1638 perf_set_reserve_percpu(struct sysdev_class
*class,
1642 struct perf_cpu_context
*cpuctx
;
1646 err
= strict_strtoul(buf
, 10, &val
);
1649 if (val
> perf_max_counters
)
1652 mutex_lock(&perf_resource_mutex
);
1653 perf_reserved_percpu
= val
;
1654 for_each_online_cpu(cpu
) {
1655 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1656 spin_lock_irq(&cpuctx
->ctx
.lock
);
1657 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
1658 perf_max_counters
- perf_reserved_percpu
);
1659 cpuctx
->max_pertask
= mpt
;
1660 spin_unlock_irq(&cpuctx
->ctx
.lock
);
1662 mutex_unlock(&perf_resource_mutex
);
1667 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
1669 return sprintf(buf
, "%d\n", perf_overcommit
);
1673 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
1678 err
= strict_strtoul(buf
, 10, &val
);
1684 mutex_lock(&perf_resource_mutex
);
1685 perf_overcommit
= val
;
1686 mutex_unlock(&perf_resource_mutex
);
1691 static SYSDEV_CLASS_ATTR(
1694 perf_show_reserve_percpu
,
1695 perf_set_reserve_percpu
1698 static SYSDEV_CLASS_ATTR(
1701 perf_show_overcommit
,
1705 static struct attribute
*perfclass_attrs
[] = {
1706 &attr_reserve_percpu
.attr
,
1707 &attr_overcommit
.attr
,
1711 static struct attribute_group perfclass_attr_group
= {
1712 .attrs
= perfclass_attrs
,
1713 .name
= "perf_counters",
1716 static int __init
perf_counter_sysfs_init(void)
1718 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
1719 &perfclass_attr_group
);
1721 device_initcall(perf_counter_sysfs_init
);