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(); }
52 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
54 struct perf_counter
*group_leader
= counter
->group_leader
;
57 * Depending on whether it is a standalone or sibling counter,
58 * add it straight to the context's counter list, or to the group
59 * leader's sibling list:
61 if (counter
->group_leader
== counter
)
62 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
64 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
68 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
70 struct perf_counter
*sibling
, *tmp
;
72 list_del_init(&counter
->list_entry
);
75 * If this was a group counter with sibling counters then
76 * upgrade the siblings to singleton counters by adding them
77 * to the context list directly:
79 list_for_each_entry_safe(sibling
, tmp
,
80 &counter
->sibling_list
, list_entry
) {
82 list_del_init(&sibling
->list_entry
);
83 list_add_tail(&sibling
->list_entry
, &ctx
->counter_list
);
84 sibling
->group_leader
= sibling
;
89 * Cross CPU call to remove a performance counter
91 * We disable the counter on the hardware level first. After that we
92 * remove it from the context list.
94 static void __perf_counter_remove_from_context(void *info
)
96 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
97 struct perf_counter
*counter
= info
;
98 struct perf_counter_context
*ctx
= counter
->ctx
;
103 * If this is a task context, we need to check whether it is
104 * the current task context of this cpu. If not it has been
105 * scheduled out before the smp call arrived.
107 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
110 curr_rq_lock_irq_save(&flags
);
111 spin_lock(&ctx
->lock
);
113 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
114 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
115 counter
->hw_ops
->disable(counter
);
117 cpuctx
->active_oncpu
--;
118 counter
->task
= NULL
;
124 * Protect the list operation against NMI by disabling the
125 * counters on a global level. NOP for non NMI based counters.
127 perf_flags
= hw_perf_save_disable();
128 list_del_counter(counter
, ctx
);
129 hw_perf_restore(perf_flags
);
133 * Allow more per task counters with respect to the
136 cpuctx
->max_pertask
=
137 min(perf_max_counters
- ctx
->nr_counters
,
138 perf_max_counters
- perf_reserved_percpu
);
141 spin_unlock(&ctx
->lock
);
142 curr_rq_unlock_irq_restore(&flags
);
147 * Remove the counter from a task's (or a CPU's) list of counters.
149 * Must be called with counter->mutex held.
151 * CPU counters are removed with a smp call. For task counters we only
152 * call when the task is on a CPU.
154 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
156 struct perf_counter_context
*ctx
= counter
->ctx
;
157 struct task_struct
*task
= ctx
->task
;
161 * Per cpu counters are removed via an smp call and
162 * the removal is always sucessful.
164 smp_call_function_single(counter
->cpu
,
165 __perf_counter_remove_from_context
,
171 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
174 spin_lock_irq(&ctx
->lock
);
176 * If the context is active we need to retry the smp call.
178 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
179 spin_unlock_irq(&ctx
->lock
);
184 * The lock prevents that this context is scheduled in so we
185 * can remove the counter safely, if the call above did not
188 if (!list_empty(&counter
->list_entry
)) {
190 list_del_counter(counter
, ctx
);
191 counter
->task
= NULL
;
193 spin_unlock_irq(&ctx
->lock
);
197 counter_sched_in(struct perf_counter
*counter
,
198 struct perf_cpu_context
*cpuctx
,
199 struct perf_counter_context
*ctx
,
202 if (counter
->state
== PERF_COUNTER_STATE_OFF
)
205 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
206 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
208 * The new state must be visible before we turn it on in the hardware:
212 if (counter
->hw_ops
->enable(counter
)) {
213 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
218 cpuctx
->active_oncpu
++;
225 * Cross CPU call to install and enable a performance counter
227 static void __perf_install_in_context(void *info
)
229 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
230 struct perf_counter
*counter
= info
;
231 struct perf_counter_context
*ctx
= counter
->ctx
;
232 int cpu
= smp_processor_id();
237 * If this is a task context, we need to check whether it is
238 * the current task context of this cpu. If not it has been
239 * scheduled out before the smp call arrived.
241 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
244 curr_rq_lock_irq_save(&flags
);
245 spin_lock(&ctx
->lock
);
248 * Protect the list operation against NMI by disabling the
249 * counters on a global level. NOP for non NMI based counters.
251 perf_flags
= hw_perf_save_disable();
253 list_add_counter(counter
, ctx
);
256 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
258 if (!ctx
->task
&& cpuctx
->max_pertask
)
259 cpuctx
->max_pertask
--;
261 hw_perf_restore(perf_flags
);
263 spin_unlock(&ctx
->lock
);
264 curr_rq_unlock_irq_restore(&flags
);
268 * Attach a performance counter to a context
270 * First we add the counter to the list with the hardware enable bit
271 * in counter->hw_config cleared.
273 * If the counter is attached to a task which is on a CPU we use a smp
274 * call to enable it in the task context. The task might have been
275 * scheduled away, but we check this in the smp call again.
278 perf_install_in_context(struct perf_counter_context
*ctx
,
279 struct perf_counter
*counter
,
282 struct task_struct
*task
= ctx
->task
;
287 * Per cpu counters are installed via an smp call and
288 * the install is always sucessful.
290 smp_call_function_single(cpu
, __perf_install_in_context
,
295 counter
->task
= task
;
297 task_oncpu_function_call(task
, __perf_install_in_context
,
300 spin_lock_irq(&ctx
->lock
);
302 * we need to retry the smp call.
304 if (ctx
->nr_active
&& list_empty(&counter
->list_entry
)) {
305 spin_unlock_irq(&ctx
->lock
);
310 * The lock prevents that this context is scheduled in so we
311 * can add the counter safely, if it the call above did not
314 if (list_empty(&counter
->list_entry
)) {
315 list_add_counter(counter
, ctx
);
318 spin_unlock_irq(&ctx
->lock
);
322 counter_sched_out(struct perf_counter
*counter
,
323 struct perf_cpu_context
*cpuctx
,
324 struct perf_counter_context
*ctx
)
326 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
329 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
330 counter
->hw_ops
->disable(counter
);
333 cpuctx
->active_oncpu
--;
338 group_sched_out(struct perf_counter
*group_counter
,
339 struct perf_cpu_context
*cpuctx
,
340 struct perf_counter_context
*ctx
)
342 struct perf_counter
*counter
;
344 counter_sched_out(group_counter
, cpuctx
, ctx
);
347 * Schedule out siblings (if any):
349 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
350 counter_sched_out(counter
, cpuctx
, ctx
);
353 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
354 struct perf_cpu_context
*cpuctx
)
356 struct perf_counter
*counter
;
358 if (likely(!ctx
->nr_counters
))
361 spin_lock(&ctx
->lock
);
362 if (ctx
->nr_active
) {
363 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
364 group_sched_out(counter
, cpuctx
, ctx
);
366 spin_unlock(&ctx
->lock
);
370 * Called from scheduler to remove the counters of the current task,
371 * with interrupts disabled.
373 * We stop each counter and update the counter value in counter->count.
375 * This does not protect us against NMI, but disable()
376 * sets the disabled bit in the control field of counter _before_
377 * accessing the counter control register. If a NMI hits, then it will
378 * not restart the counter.
380 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
382 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
383 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
385 if (likely(!cpuctx
->task_ctx
))
388 __perf_counter_sched_out(ctx
, cpuctx
);
390 cpuctx
->task_ctx
= NULL
;
393 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
395 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
399 group_sched_in(struct perf_counter
*group_counter
,
400 struct perf_cpu_context
*cpuctx
,
401 struct perf_counter_context
*ctx
,
404 struct perf_counter
*counter
, *partial_group
;
407 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
411 * Schedule in siblings as one group (if any):
413 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
414 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
415 partial_group
= counter
;
425 * Groups can be scheduled in as one unit only, so undo any
426 * partial group before returning:
428 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
429 if (counter
== partial_group
)
431 counter_sched_out(counter
, cpuctx
, ctx
);
433 counter_sched_out(group_counter
, cpuctx
, ctx
);
439 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
440 struct perf_cpu_context
*cpuctx
, int cpu
)
442 struct perf_counter
*counter
;
444 if (likely(!ctx
->nr_counters
))
447 spin_lock(&ctx
->lock
);
448 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
450 * Listen to the 'cpu' scheduling filter constraint
453 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
457 * If we scheduled in a group atomically and
458 * exclusively, break out:
460 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
463 spin_unlock(&ctx
->lock
);
467 * Called from scheduler to add the counters of the current task
468 * with interrupts disabled.
470 * We restore the counter value and then enable it.
472 * This does not protect us against NMI, but enable()
473 * sets the enabled bit in the control field of counter _before_
474 * accessing the counter control register. If a NMI hits, then it will
475 * keep the counter running.
477 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
479 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
480 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
482 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
483 cpuctx
->task_ctx
= ctx
;
486 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
488 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
490 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
493 int perf_counter_task_disable(void)
495 struct task_struct
*curr
= current
;
496 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
497 struct perf_counter
*counter
;
502 if (likely(!ctx
->nr_counters
))
505 curr_rq_lock_irq_save(&flags
);
506 cpu
= smp_processor_id();
508 /* force the update of the task clock: */
509 __task_delta_exec(curr
, 1);
511 perf_counter_task_sched_out(curr
, cpu
);
513 spin_lock(&ctx
->lock
);
516 * Disable all the counters:
518 perf_flags
= hw_perf_save_disable();
520 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
521 counter
->state
= PERF_COUNTER_STATE_OFF
;
523 hw_perf_restore(perf_flags
);
525 spin_unlock(&ctx
->lock
);
527 curr_rq_unlock_irq_restore(&flags
);
532 int perf_counter_task_enable(void)
534 struct task_struct
*curr
= current
;
535 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
536 struct perf_counter
*counter
;
541 if (likely(!ctx
->nr_counters
))
544 curr_rq_lock_irq_save(&flags
);
545 cpu
= smp_processor_id();
547 /* force the update of the task clock: */
548 __task_delta_exec(curr
, 1);
550 perf_counter_task_sched_out(curr
, cpu
);
552 spin_lock(&ctx
->lock
);
555 * Disable all the counters:
557 perf_flags
= hw_perf_save_disable();
559 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
560 if (counter
->state
!= PERF_COUNTER_STATE_OFF
)
562 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
563 counter
->hw_event
.disabled
= 0;
565 hw_perf_restore(perf_flags
);
567 spin_unlock(&ctx
->lock
);
569 perf_counter_task_sched_in(curr
, cpu
);
571 curr_rq_unlock_irq_restore(&flags
);
577 * Round-robin a context's counters:
579 static void rotate_ctx(struct perf_counter_context
*ctx
)
581 struct perf_counter
*counter
;
584 if (!ctx
->nr_counters
)
587 spin_lock(&ctx
->lock
);
589 * Rotate the first entry last (works just fine for group counters too):
591 perf_flags
= hw_perf_save_disable();
592 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
593 list_del(&counter
->list_entry
);
594 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
597 hw_perf_restore(perf_flags
);
599 spin_unlock(&ctx
->lock
);
602 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
604 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
605 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
606 const int rotate_percpu
= 0;
609 perf_counter_cpu_sched_out(cpuctx
);
610 perf_counter_task_sched_out(curr
, cpu
);
613 rotate_ctx(&cpuctx
->ctx
);
617 perf_counter_cpu_sched_in(cpuctx
, cpu
);
618 perf_counter_task_sched_in(curr
, cpu
);
622 * Cross CPU call to read the hardware counter
624 static void __read(void *info
)
626 struct perf_counter
*counter
= info
;
629 curr_rq_lock_irq_save(&flags
);
630 counter
->hw_ops
->read(counter
);
631 curr_rq_unlock_irq_restore(&flags
);
634 static u64
perf_counter_read(struct perf_counter
*counter
)
637 * If counter is enabled and currently active on a CPU, update the
638 * value in the counter structure:
640 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
641 smp_call_function_single(counter
->oncpu
,
645 return atomic64_read(&counter
->count
);
649 * Cross CPU call to switch performance data pointers
651 static void __perf_switch_irq_data(void *info
)
653 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
654 struct perf_counter
*counter
= info
;
655 struct perf_counter_context
*ctx
= counter
->ctx
;
656 struct perf_data
*oldirqdata
= counter
->irqdata
;
659 * If this is a task context, we need to check whether it is
660 * the current task context of this cpu. If not it has been
661 * scheduled out before the smp call arrived.
664 if (cpuctx
->task_ctx
!= ctx
)
666 spin_lock(&ctx
->lock
);
669 /* Change the pointer NMI safe */
670 atomic_long_set((atomic_long_t
*)&counter
->irqdata
,
671 (unsigned long) counter
->usrdata
);
672 counter
->usrdata
= oldirqdata
;
675 spin_unlock(&ctx
->lock
);
678 static struct perf_data
*perf_switch_irq_data(struct perf_counter
*counter
)
680 struct perf_counter_context
*ctx
= counter
->ctx
;
681 struct perf_data
*oldirqdata
= counter
->irqdata
;
682 struct task_struct
*task
= ctx
->task
;
685 smp_call_function_single(counter
->cpu
,
686 __perf_switch_irq_data
,
688 return counter
->usrdata
;
692 spin_lock_irq(&ctx
->lock
);
693 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
) {
694 counter
->irqdata
= counter
->usrdata
;
695 counter
->usrdata
= oldirqdata
;
696 spin_unlock_irq(&ctx
->lock
);
699 spin_unlock_irq(&ctx
->lock
);
700 task_oncpu_function_call(task
, __perf_switch_irq_data
, counter
);
701 /* Might have failed, because task was scheduled out */
702 if (counter
->irqdata
== oldirqdata
)
705 return counter
->usrdata
;
708 static void put_context(struct perf_counter_context
*ctx
)
711 put_task_struct(ctx
->task
);
714 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
716 struct perf_cpu_context
*cpuctx
;
717 struct perf_counter_context
*ctx
;
718 struct task_struct
*task
;
721 * If cpu is not a wildcard then this is a percpu counter:
724 /* Must be root to operate on a CPU counter: */
725 if (!capable(CAP_SYS_ADMIN
))
726 return ERR_PTR(-EACCES
);
728 if (cpu
< 0 || cpu
> num_possible_cpus())
729 return ERR_PTR(-EINVAL
);
732 * We could be clever and allow to attach a counter to an
733 * offline CPU and activate it when the CPU comes up, but
736 if (!cpu_isset(cpu
, cpu_online_map
))
737 return ERR_PTR(-ENODEV
);
739 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
749 task
= find_task_by_vpid(pid
);
751 get_task_struct(task
);
755 return ERR_PTR(-ESRCH
);
757 ctx
= &task
->perf_counter_ctx
;
760 /* Reuse ptrace permission checks for now. */
761 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
763 return ERR_PTR(-EACCES
);
770 * Called when the last reference to the file is gone.
772 static int perf_release(struct inode
*inode
, struct file
*file
)
774 struct perf_counter
*counter
= file
->private_data
;
775 struct perf_counter_context
*ctx
= counter
->ctx
;
777 file
->private_data
= NULL
;
779 mutex_lock(&counter
->mutex
);
781 perf_counter_remove_from_context(counter
);
784 mutex_unlock(&counter
->mutex
);
792 * Read the performance counter - simple non blocking version for now
795 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
799 if (count
!= sizeof(cntval
))
802 mutex_lock(&counter
->mutex
);
803 cntval
= perf_counter_read(counter
);
804 mutex_unlock(&counter
->mutex
);
806 return put_user(cntval
, (u64 __user
*) buf
) ? -EFAULT
: sizeof(cntval
);
810 perf_copy_usrdata(struct perf_data
*usrdata
, char __user
*buf
, size_t count
)
815 count
= min(count
, (size_t)usrdata
->len
);
816 if (copy_to_user(buf
, usrdata
->data
+ usrdata
->rd_idx
, count
))
819 /* Adjust the counters */
820 usrdata
->len
-= count
;
824 usrdata
->rd_idx
+= count
;
830 perf_read_irq_data(struct perf_counter
*counter
,
835 struct perf_data
*irqdata
, *usrdata
;
836 DECLARE_WAITQUEUE(wait
, current
);
839 irqdata
= counter
->irqdata
;
840 usrdata
= counter
->usrdata
;
842 if (usrdata
->len
+ irqdata
->len
>= count
)
848 spin_lock_irq(&counter
->waitq
.lock
);
849 __add_wait_queue(&counter
->waitq
, &wait
);
851 set_current_state(TASK_INTERRUPTIBLE
);
852 if (usrdata
->len
+ irqdata
->len
>= count
)
855 if (signal_pending(current
))
858 spin_unlock_irq(&counter
->waitq
.lock
);
860 spin_lock_irq(&counter
->waitq
.lock
);
862 __remove_wait_queue(&counter
->waitq
, &wait
);
863 __set_current_state(TASK_RUNNING
);
864 spin_unlock_irq(&counter
->waitq
.lock
);
866 if (usrdata
->len
+ irqdata
->len
< count
)
869 mutex_lock(&counter
->mutex
);
871 /* Drain pending data first: */
872 res
= perf_copy_usrdata(usrdata
, buf
, count
);
873 if (res
< 0 || res
== count
)
876 /* Switch irq buffer: */
877 usrdata
= perf_switch_irq_data(counter
);
878 if (perf_copy_usrdata(usrdata
, buf
+ res
, count
- res
) < 0) {
885 mutex_unlock(&counter
->mutex
);
891 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
893 struct perf_counter
*counter
= file
->private_data
;
895 switch (counter
->hw_event
.record_type
) {
896 case PERF_RECORD_SIMPLE
:
897 return perf_read_hw(counter
, buf
, count
);
899 case PERF_RECORD_IRQ
:
900 case PERF_RECORD_GROUP
:
901 return perf_read_irq_data(counter
, buf
, count
,
902 file
->f_flags
& O_NONBLOCK
);
907 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
909 struct perf_counter
*counter
= file
->private_data
;
910 unsigned int events
= 0;
913 poll_wait(file
, &counter
->waitq
, wait
);
915 spin_lock_irqsave(&counter
->waitq
.lock
, flags
);
916 if (counter
->usrdata
->len
|| counter
->irqdata
->len
)
918 spin_unlock_irqrestore(&counter
->waitq
.lock
, flags
);
923 static const struct file_operations perf_fops
= {
924 .release
= perf_release
,
929 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
934 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
938 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
940 int cpu
= raw_smp_processor_id();
942 atomic64_set(&counter
->count
, cpu_clock(cpu
));
945 static const struct hw_perf_counter_ops perf_ops_cpu_clock
= {
946 .enable
= cpu_clock_perf_counter_enable
,
947 .disable
= cpu_clock_perf_counter_disable
,
948 .read
= cpu_clock_perf_counter_read
,
952 * Called from within the scheduler:
954 static u64
task_clock_perf_counter_val(struct perf_counter
*counter
, int update
)
956 struct task_struct
*curr
= counter
->task
;
959 delta
= __task_delta_exec(curr
, update
);
961 return curr
->se
.sum_exec_runtime
+ delta
;
964 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
969 prev
= atomic64_read(&counter
->hw
.prev_count
);
971 atomic64_set(&counter
->hw
.prev_count
, now
);
975 atomic64_add(delta
, &counter
->count
);
978 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
980 u64 now
= task_clock_perf_counter_val(counter
, 1);
982 task_clock_perf_counter_update(counter
, now
);
985 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
987 u64 now
= task_clock_perf_counter_val(counter
, 0);
989 atomic64_set(&counter
->hw
.prev_count
, now
);
994 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
996 u64 now
= task_clock_perf_counter_val(counter
, 0);
998 task_clock_perf_counter_update(counter
, now
);
1001 static const struct hw_perf_counter_ops perf_ops_task_clock
= {
1002 .enable
= task_clock_perf_counter_enable
,
1003 .disable
= task_clock_perf_counter_disable
,
1004 .read
= task_clock_perf_counter_read
,
1007 static u64
get_page_faults(void)
1009 struct task_struct
*curr
= current
;
1011 return curr
->maj_flt
+ curr
->min_flt
;
1014 static void page_faults_perf_counter_update(struct perf_counter
*counter
)
1019 prev
= atomic64_read(&counter
->hw
.prev_count
);
1020 now
= get_page_faults();
1022 atomic64_set(&counter
->hw
.prev_count
, now
);
1026 atomic64_add(delta
, &counter
->count
);
1029 static void page_faults_perf_counter_read(struct perf_counter
*counter
)
1031 page_faults_perf_counter_update(counter
);
1034 static int page_faults_perf_counter_enable(struct perf_counter
*counter
)
1037 * page-faults is a per-task value already,
1038 * so we dont have to clear it on switch-in.
1044 static void page_faults_perf_counter_disable(struct perf_counter
*counter
)
1046 page_faults_perf_counter_update(counter
);
1049 static const struct hw_perf_counter_ops perf_ops_page_faults
= {
1050 .enable
= page_faults_perf_counter_enable
,
1051 .disable
= page_faults_perf_counter_disable
,
1052 .read
= page_faults_perf_counter_read
,
1055 static u64
get_context_switches(void)
1057 struct task_struct
*curr
= current
;
1059 return curr
->nvcsw
+ curr
->nivcsw
;
1062 static void context_switches_perf_counter_update(struct perf_counter
*counter
)
1067 prev
= atomic64_read(&counter
->hw
.prev_count
);
1068 now
= get_context_switches();
1070 atomic64_set(&counter
->hw
.prev_count
, now
);
1074 atomic64_add(delta
, &counter
->count
);
1077 static void context_switches_perf_counter_read(struct perf_counter
*counter
)
1079 context_switches_perf_counter_update(counter
);
1082 static int context_switches_perf_counter_enable(struct perf_counter
*counter
)
1085 * ->nvcsw + curr->nivcsw is a per-task value already,
1086 * so we dont have to clear it on switch-in.
1092 static void context_switches_perf_counter_disable(struct perf_counter
*counter
)
1094 context_switches_perf_counter_update(counter
);
1097 static const struct hw_perf_counter_ops perf_ops_context_switches
= {
1098 .enable
= context_switches_perf_counter_enable
,
1099 .disable
= context_switches_perf_counter_disable
,
1100 .read
= context_switches_perf_counter_read
,
1103 static inline u64
get_cpu_migrations(void)
1105 return current
->se
.nr_migrations
;
1108 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
1113 prev
= atomic64_read(&counter
->hw
.prev_count
);
1114 now
= get_cpu_migrations();
1116 atomic64_set(&counter
->hw
.prev_count
, now
);
1120 atomic64_add(delta
, &counter
->count
);
1123 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
1125 cpu_migrations_perf_counter_update(counter
);
1128 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
1131 * se.nr_migrations is a per-task value already,
1132 * so we dont have to clear it on switch-in.
1138 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
1140 cpu_migrations_perf_counter_update(counter
);
1143 static const struct hw_perf_counter_ops perf_ops_cpu_migrations
= {
1144 .enable
= cpu_migrations_perf_counter_enable
,
1145 .disable
= cpu_migrations_perf_counter_disable
,
1146 .read
= cpu_migrations_perf_counter_read
,
1149 static const struct hw_perf_counter_ops
*
1150 sw_perf_counter_init(struct perf_counter
*counter
)
1152 const struct hw_perf_counter_ops
*hw_ops
= NULL
;
1154 switch (counter
->hw_event
.type
) {
1155 case PERF_COUNT_CPU_CLOCK
:
1156 hw_ops
= &perf_ops_cpu_clock
;
1158 case PERF_COUNT_TASK_CLOCK
:
1159 hw_ops
= &perf_ops_task_clock
;
1161 case PERF_COUNT_PAGE_FAULTS
:
1162 hw_ops
= &perf_ops_page_faults
;
1164 case PERF_COUNT_CONTEXT_SWITCHES
:
1165 hw_ops
= &perf_ops_context_switches
;
1167 case PERF_COUNT_CPU_MIGRATIONS
:
1168 hw_ops
= &perf_ops_cpu_migrations
;
1177 * Allocate and initialize a counter structure
1179 static struct perf_counter
*
1180 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
1182 struct perf_counter
*group_leader
,
1185 const struct hw_perf_counter_ops
*hw_ops
;
1186 struct perf_counter
*counter
;
1188 counter
= kzalloc(sizeof(*counter
), gfpflags
);
1193 * Single counters are their own group leaders, with an
1194 * empty sibling list:
1197 group_leader
= counter
;
1199 mutex_init(&counter
->mutex
);
1200 INIT_LIST_HEAD(&counter
->list_entry
);
1201 INIT_LIST_HEAD(&counter
->sibling_list
);
1202 init_waitqueue_head(&counter
->waitq
);
1204 counter
->irqdata
= &counter
->data
[0];
1205 counter
->usrdata
= &counter
->data
[1];
1207 counter
->hw_event
= *hw_event
;
1208 counter
->wakeup_pending
= 0;
1209 counter
->group_leader
= group_leader
;
1210 counter
->hw_ops
= NULL
;
1212 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1213 if (hw_event
->disabled
)
1214 counter
->state
= PERF_COUNTER_STATE_OFF
;
1217 if (!hw_event
->raw
&& hw_event
->type
< 0)
1218 hw_ops
= sw_perf_counter_init(counter
);
1220 hw_ops
= hw_perf_counter_init(counter
);
1226 counter
->hw_ops
= hw_ops
;
1232 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1234 * @hw_event_uptr: event type attributes for monitoring/sampling
1237 * @group_fd: group leader counter fd
1240 sys_perf_counter_open(struct perf_counter_hw_event
*hw_event_uptr __user
,
1241 pid_t pid
, int cpu
, int group_fd
)
1243 struct perf_counter
*counter
, *group_leader
;
1244 struct perf_counter_hw_event hw_event
;
1245 struct perf_counter_context
*ctx
;
1246 struct file
*counter_file
= NULL
;
1247 struct file
*group_file
= NULL
;
1248 int fput_needed
= 0;
1249 int fput_needed2
= 0;
1252 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
1256 * Get the target context (task or percpu):
1258 ctx
= find_get_context(pid
, cpu
);
1260 return PTR_ERR(ctx
);
1263 * Look up the group leader (we will attach this counter to it):
1265 group_leader
= NULL
;
1266 if (group_fd
!= -1) {
1268 group_file
= fget_light(group_fd
, &fput_needed
);
1270 goto err_put_context
;
1271 if (group_file
->f_op
!= &perf_fops
)
1272 goto err_put_context
;
1274 group_leader
= group_file
->private_data
;
1276 * Do not allow a recursive hierarchy (this new sibling
1277 * becoming part of another group-sibling):
1279 if (group_leader
->group_leader
!= group_leader
)
1280 goto err_put_context
;
1282 * Do not allow to attach to a group in a different
1283 * task or CPU context:
1285 if (group_leader
->ctx
!= ctx
)
1286 goto err_put_context
;
1290 counter
= perf_counter_alloc(&hw_event
, cpu
, group_leader
, GFP_KERNEL
);
1292 goto err_put_context
;
1294 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
1296 goto err_free_put_context
;
1298 counter_file
= fget_light(ret
, &fput_needed2
);
1300 goto err_free_put_context
;
1302 counter
->filp
= counter_file
;
1303 perf_install_in_context(ctx
, counter
, cpu
);
1305 fput_light(counter_file
, fput_needed2
);
1308 fput_light(group_file
, fput_needed
);
1312 err_free_put_context
:
1322 * Initialize the perf_counter context in a task_struct:
1325 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1326 struct task_struct
*task
)
1328 memset(ctx
, 0, sizeof(*ctx
));
1329 spin_lock_init(&ctx
->lock
);
1330 INIT_LIST_HEAD(&ctx
->counter_list
);
1335 * inherit a counter from parent task to child task:
1338 inherit_counter(struct perf_counter
*parent_counter
,
1339 struct task_struct
*parent
,
1340 struct perf_counter_context
*parent_ctx
,
1341 struct task_struct
*child
,
1342 struct perf_counter_context
*child_ctx
)
1344 struct perf_counter
*child_counter
;
1346 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
1347 parent_counter
->cpu
, NULL
,
1353 * Link it up in the child's context:
1355 child_counter
->ctx
= child_ctx
;
1356 child_counter
->task
= child
;
1357 list_add_counter(child_counter
, child_ctx
);
1358 child_ctx
->nr_counters
++;
1360 child_counter
->parent
= parent_counter
;
1362 * inherit into child's child as well:
1364 child_counter
->hw_event
.inherit
= 1;
1367 * Get a reference to the parent filp - we will fput it
1368 * when the child counter exits. This is safe to do because
1369 * we are in the parent and we know that the filp still
1370 * exists and has a nonzero count:
1372 atomic_long_inc(&parent_counter
->filp
->f_count
);
1378 __perf_counter_exit_task(struct task_struct
*child
,
1379 struct perf_counter
*child_counter
,
1380 struct perf_counter_context
*child_ctx
)
1382 struct perf_counter
*parent_counter
;
1383 u64 parent_val
, child_val
;
1386 * If we do not self-reap then we have to wait for the
1387 * child task to unschedule (it will happen for sure),
1388 * so that its counter is at its final count. (This
1389 * condition triggers rarely - child tasks usually get
1390 * off their CPU before the parent has a chance to
1391 * get this far into the reaping action)
1393 if (child
!= current
) {
1394 wait_task_inactive(child
, 0);
1395 list_del_init(&child_counter
->list_entry
);
1397 struct perf_cpu_context
*cpuctx
;
1398 unsigned long flags
;
1402 * Disable and unlink this counter.
1404 * Be careful about zapping the list - IRQ/NMI context
1405 * could still be processing it:
1407 curr_rq_lock_irq_save(&flags
);
1408 perf_flags
= hw_perf_save_disable();
1410 cpuctx
= &__get_cpu_var(perf_cpu_context
);
1412 if (child_counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1413 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1414 child_counter
->hw_ops
->disable(child_counter
);
1415 cpuctx
->active_oncpu
--;
1416 child_ctx
->nr_active
--;
1417 child_counter
->oncpu
= -1;
1420 list_del_init(&child_counter
->list_entry
);
1422 child_ctx
->nr_counters
--;
1424 hw_perf_restore(perf_flags
);
1425 curr_rq_unlock_irq_restore(&flags
);
1428 parent_counter
= child_counter
->parent
;
1430 * It can happen that parent exits first, and has counters
1431 * that are still around due to the child reference. These
1432 * counters need to be zapped - but otherwise linger.
1434 if (!parent_counter
)
1437 parent_val
= atomic64_read(&parent_counter
->count
);
1438 child_val
= atomic64_read(&child_counter
->count
);
1441 * Add back the child's count to the parent's count:
1443 atomic64_add(child_val
, &parent_counter
->count
);
1445 fput(parent_counter
->filp
);
1447 kfree(child_counter
);
1451 * When a child task exist, feed back counter values to parent counters.
1453 * Note: we are running in child context, but the PID is not hashed
1454 * anymore so new counters will not be added.
1456 void perf_counter_exit_task(struct task_struct
*child
)
1458 struct perf_counter
*child_counter
, *tmp
;
1459 struct perf_counter_context
*child_ctx
;
1461 child_ctx
= &child
->perf_counter_ctx
;
1463 if (likely(!child_ctx
->nr_counters
))
1466 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
1468 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
1472 * Initialize the perf_counter context in task_struct
1474 void perf_counter_init_task(struct task_struct
*child
)
1476 struct perf_counter_context
*child_ctx
, *parent_ctx
;
1477 struct perf_counter
*counter
, *parent_counter
;
1478 struct task_struct
*parent
= current
;
1479 unsigned long flags
;
1481 child_ctx
= &child
->perf_counter_ctx
;
1482 parent_ctx
= &parent
->perf_counter_ctx
;
1484 __perf_counter_init_context(child_ctx
, child
);
1487 * This is executed from the parent task context, so inherit
1488 * counters that have been marked for cloning:
1491 if (likely(!parent_ctx
->nr_counters
))
1495 * Lock the parent list. No need to lock the child - not PID
1496 * hashed yet and not running, so nobody can access it.
1498 spin_lock_irqsave(&parent_ctx
->lock
, flags
);
1501 * We dont have to disable NMIs - we are only looking at
1502 * the list, not manipulating it:
1504 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
1505 if (!counter
->hw_event
.inherit
|| counter
->group_leader
!= counter
)
1509 * Instead of creating recursive hierarchies of counters,
1510 * we link inheritd counters back to the original parent,
1511 * which has a filp for sure, which we use as the reference
1514 parent_counter
= counter
;
1515 if (counter
->parent
)
1516 parent_counter
= counter
->parent
;
1518 if (inherit_counter(parent_counter
, parent
,
1519 parent_ctx
, child
, child_ctx
))
1523 spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
1526 static void __cpuinit
perf_counter_init_cpu(int cpu
)
1528 struct perf_cpu_context
*cpuctx
;
1530 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1531 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
1533 mutex_lock(&perf_resource_mutex
);
1534 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
1535 mutex_unlock(&perf_resource_mutex
);
1537 hw_perf_counter_setup();
1540 #ifdef CONFIG_HOTPLUG_CPU
1541 static void __perf_counter_exit_cpu(void *info
)
1543 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1544 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1545 struct perf_counter
*counter
, *tmp
;
1547 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
1548 __perf_counter_remove_from_context(counter
);
1551 static void perf_counter_exit_cpu(int cpu
)
1553 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
1556 static inline void perf_counter_exit_cpu(int cpu
) { }
1559 static int __cpuinit
1560 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
1562 unsigned int cpu
= (long)hcpu
;
1566 case CPU_UP_PREPARE
:
1567 case CPU_UP_PREPARE_FROZEN
:
1568 perf_counter_init_cpu(cpu
);
1571 case CPU_DOWN_PREPARE
:
1572 case CPU_DOWN_PREPARE_FROZEN
:
1573 perf_counter_exit_cpu(cpu
);
1583 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
1584 .notifier_call
= perf_cpu_notify
,
1587 static int __init
perf_counter_init(void)
1589 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
1590 (void *)(long)smp_processor_id());
1591 register_cpu_notifier(&perf_cpu_nb
);
1595 early_initcall(perf_counter_init
);
1597 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
1599 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
1603 perf_set_reserve_percpu(struct sysdev_class
*class,
1607 struct perf_cpu_context
*cpuctx
;
1611 err
= strict_strtoul(buf
, 10, &val
);
1614 if (val
> perf_max_counters
)
1617 mutex_lock(&perf_resource_mutex
);
1618 perf_reserved_percpu
= val
;
1619 for_each_online_cpu(cpu
) {
1620 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1621 spin_lock_irq(&cpuctx
->ctx
.lock
);
1622 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
1623 perf_max_counters
- perf_reserved_percpu
);
1624 cpuctx
->max_pertask
= mpt
;
1625 spin_unlock_irq(&cpuctx
->ctx
.lock
);
1627 mutex_unlock(&perf_resource_mutex
);
1632 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
1634 return sprintf(buf
, "%d\n", perf_overcommit
);
1638 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
1643 err
= strict_strtoul(buf
, 10, &val
);
1649 mutex_lock(&perf_resource_mutex
);
1650 perf_overcommit
= val
;
1651 mutex_unlock(&perf_resource_mutex
);
1656 static SYSDEV_CLASS_ATTR(
1659 perf_show_reserve_percpu
,
1660 perf_set_reserve_percpu
1663 static SYSDEV_CLASS_ATTR(
1666 perf_show_overcommit
,
1670 static struct attribute
*perfclass_attrs
[] = {
1671 &attr_reserve_percpu
.attr
,
1672 &attr_overcommit
.attr
,
1676 static struct attribute_group perfclass_attr_group
= {
1677 .attrs
= perfclass_attrs
,
1678 .name
= "perf_counters",
1681 static int __init
perf_counter_sysfs_init(void)
1683 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
1684 &perfclass_attr_group
);
1686 device_initcall(perf_counter_sysfs_init
);