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)
44 return ERR_PTR(-EINVAL);
47 u64 __weak hw_perf_save_disable(void) { return 0; }
48 void __weak hw_perf_restore(u64 ctrl) { }
49 void __weak hw_perf_counter_setup(void) { }
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->hw_ops->disable(counter);
115 counter->state = PERF_COUNTER_STATE_INACTIVE;
117 cpuctx->active_oncpu--;
118 counter->task = NULL;
123 * Protect the list operation against NMI by disabling the
124 * counters on a global level. NOP for non NMI based counters.
126 perf_flags = hw_perf_save_disable();
127 list_del_counter(counter, ctx);
128 hw_perf_restore(perf_flags);
132 * Allow more per task counters with respect to the
135 cpuctx->max_pertask =
136 min(perf_max_counters - ctx->nr_counters,
137 perf_max_counters - perf_reserved_percpu);
140 spin_unlock(&ctx->lock);
141 curr_rq_unlock_irq_restore(&flags);
146 * Remove the counter from a task's (or a CPU's) list of counters.
148 * Must be called with counter->mutex held.
150 * CPU counters are removed with a smp call. For task counters we only
151 * call when the task is on a CPU.
153 static void perf_counter_remove_from_context(struct perf_counter *counter)
155 struct perf_counter_context *ctx = counter->ctx;
156 struct task_struct *task = ctx->task;
160 * Per cpu counters are removed via an smp call and
161 * the removal is always sucessful.
163 smp_call_function_single(counter->cpu,
164 __perf_counter_remove_from_context,
170 task_oncpu_function_call(task, __perf_counter_remove_from_context,
173 spin_lock_irq(&ctx->lock);
175 * If the context is active we need to retry the smp call.
177 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
178 spin_unlock_irq(&ctx->lock);
183 * The lock prevents that this context is scheduled in so we
184 * can remove the counter safely, if the call above did not
187 if (!list_empty(&counter->list_entry)) {
189 list_del_counter(counter, ctx);
190 counter->task = NULL;
192 spin_unlock_irq(&ctx->lock);
196 * Cross CPU call to install and enable a preformance counter
198 static void __perf_install_in_context(void *info)
200 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
201 struct perf_counter *counter = info;
202 struct perf_counter_context *ctx = counter->ctx;
203 int cpu = smp_processor_id();
208 * If this is a task context, we need to check whether it is
209 * the current task context of this cpu. If not it has been
210 * scheduled out before the smp call arrived.
212 if (ctx->task && cpuctx->task_ctx != ctx)
215 curr_rq_lock_irq_save(&flags);
216 spin_lock(&ctx->lock);
219 * Protect the list operation against NMI by disabling the
220 * counters on a global level. NOP for non NMI based counters.
222 perf_flags = hw_perf_save_disable();
223 list_add_counter(counter, ctx);
224 hw_perf_restore(perf_flags);
228 if (cpuctx->active_oncpu < perf_max_counters) {
229 counter->state = PERF_COUNTER_STATE_ACTIVE;
230 counter->oncpu = cpu;
232 cpuctx->active_oncpu++;
233 counter->hw_ops->enable(counter);
236 if (!ctx->task && cpuctx->max_pertask)
237 cpuctx->max_pertask--;
239 spin_unlock(&ctx->lock);
240 curr_rq_unlock_irq_restore(&flags);
244 * Attach a performance counter to a context
246 * First we add the counter to the list with the hardware enable bit
247 * in counter->hw_config cleared.
249 * If the counter is attached to a task which is on a CPU we use a smp
250 * call to enable it in the task context. The task might have been
251 * scheduled away, but we check this in the smp call again.
254 perf_install_in_context(struct perf_counter_context *ctx,
255 struct perf_counter *counter,
258 struct task_struct *task = ctx->task;
263 * Per cpu counters are installed via an smp call and
264 * the install is always sucessful.
266 smp_call_function_single(cpu, __perf_install_in_context,
271 counter->task = task;
273 task_oncpu_function_call(task, __perf_install_in_context,
276 spin_lock_irq(&ctx->lock);
278 * we need to retry the smp call.
280 if (ctx->nr_active && list_empty(&counter->list_entry)) {
281 spin_unlock_irq(&ctx->lock);
286 * The lock prevents that this context is scheduled in so we
287 * can add the counter safely, if it the call above did not
290 if (list_empty(&counter->list_entry)) {
291 list_add_counter(counter, ctx);
294 spin_unlock_irq(&ctx->lock);
298 counter_sched_out(struct perf_counter *counter,
299 struct perf_cpu_context *cpuctx,
300 struct perf_counter_context *ctx)
302 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
305 counter->hw_ops->disable(counter);
306 counter->state = PERF_COUNTER_STATE_INACTIVE;
309 cpuctx->active_oncpu--;
314 group_sched_out(struct perf_counter *group_counter,
315 struct perf_cpu_context *cpuctx,
316 struct perf_counter_context *ctx)
318 struct perf_counter *counter;
320 counter_sched_out(group_counter, cpuctx, ctx);
323 * Schedule out siblings (if any):
325 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
326 counter_sched_out(counter, cpuctx, ctx);
330 * Called from scheduler to remove the counters of the current task,
331 * with interrupts disabled.
333 * We stop each counter and update the counter value in counter->count.
335 * This does not protect us against NMI, but disable()
336 * sets the disabled bit in the control field of counter _before_
337 * accessing the counter control register. If a NMI hits, then it will
338 * not restart the counter.
340 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
342 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
343 struct perf_counter_context *ctx = &task->perf_counter_ctx;
344 struct perf_counter *counter;
346 if (likely(!cpuctx->task_ctx))
349 spin_lock(&ctx->lock);
350 if (ctx->nr_active) {
351 list_for_each_entry(counter, &ctx->counter_list, list_entry)
352 group_sched_out(counter, cpuctx, ctx);
354 spin_unlock(&ctx->lock);
355 cpuctx->task_ctx = NULL;
359 counter_sched_in(struct perf_counter *counter,
360 struct perf_cpu_context *cpuctx,
361 struct perf_counter_context *ctx,
364 if (counter->state == PERF_COUNTER_STATE_OFF)
367 counter->hw_ops->enable(counter);
368 counter->state = PERF_COUNTER_STATE_ACTIVE;
369 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
371 cpuctx->active_oncpu++;
376 group_sched_in(struct perf_counter *group_counter,
377 struct perf_cpu_context *cpuctx,
378 struct perf_counter_context *ctx,
381 struct perf_counter *counter;
384 counter_sched_in(group_counter, cpuctx, ctx, cpu);
387 * Schedule in siblings as one group (if any):
389 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
390 counter_sched_in(counter, cpuctx, ctx, cpu);
398 * Called from scheduler to add the counters of the current task
399 * with interrupts disabled.
401 * We restore the counter value and then enable it.
403 * This does not protect us against NMI, but enable()
404 * sets the enabled bit in the control field of counter _before_
405 * accessing the counter control register. If a NMI hits, then it will
406 * keep the counter running.
408 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
410 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
411 struct perf_counter_context *ctx = &task->perf_counter_ctx;
412 struct perf_counter *counter;
414 if (likely(!ctx->nr_counters))
417 spin_lock(&ctx->lock);
418 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
419 if (ctx->nr_active == cpuctx->max_pertask)
423 * Listen to the 'cpu' scheduling filter constraint
426 if (counter->cpu != -1 && counter->cpu != cpu)
430 * If we scheduled in a group atomically and
431 * exclusively, break out:
433 if (group_sched_in(counter, cpuctx, ctx, cpu))
436 spin_unlock(&ctx->lock);
438 cpuctx->task_ctx = ctx;
441 int perf_counter_task_disable(void)
443 struct task_struct *curr = current;
444 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
445 struct perf_counter *counter;
450 if (likely(!ctx->nr_counters))
453 curr_rq_lock_irq_save(&flags);
454 cpu = smp_processor_id();
456 /* force the update of the task clock: */
457 __task_delta_exec(curr, 1);
459 perf_counter_task_sched_out(curr, cpu);
461 spin_lock(&ctx->lock);
464 * Disable all the counters:
466 perf_flags = hw_perf_save_disable();
468 list_for_each_entry(counter, &ctx->counter_list, list_entry)
469 counter->state = PERF_COUNTER_STATE_OFF;
471 hw_perf_restore(perf_flags);
473 spin_unlock(&ctx->lock);
475 curr_rq_unlock_irq_restore(&flags);
480 int perf_counter_task_enable(void)
482 struct task_struct *curr = current;
483 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
484 struct perf_counter *counter;
489 if (likely(!ctx->nr_counters))
492 curr_rq_lock_irq_save(&flags);
493 cpu = smp_processor_id();
495 /* force the update of the task clock: */
496 __task_delta_exec(curr, 1);
498 spin_lock(&ctx->lock);
501 * Disable all the counters:
503 perf_flags = hw_perf_save_disable();
505 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
506 if (counter->state != PERF_COUNTER_STATE_OFF)
508 counter->state = PERF_COUNTER_STATE_INACTIVE;
509 counter->hw_event.disabled = 0;
511 hw_perf_restore(perf_flags);
513 spin_unlock(&ctx->lock);
515 perf_counter_task_sched_in(curr, cpu);
517 curr_rq_unlock_irq_restore(&flags);
522 void perf_counter_task_tick(struct task_struct *curr, int cpu)
524 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
525 struct perf_counter *counter;
528 if (likely(!ctx->nr_counters))
531 perf_counter_task_sched_out(curr, cpu);
533 spin_lock(&ctx->lock);
536 * Rotate the first entry last (works just fine for group counters too):
538 perf_flags = hw_perf_save_disable();
539 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
540 list_del(&counter->list_entry);
541 list_add_tail(&counter->list_entry, &ctx->counter_list);
544 hw_perf_restore(perf_flags);
546 spin_unlock(&ctx->lock);
548 perf_counter_task_sched_in(curr, cpu);
552 * Cross CPU call to read the hardware counter
554 static void __read(void *info)
556 struct perf_counter *counter = info;
559 curr_rq_lock_irq_save(&flags);
560 counter->hw_ops->read(counter);
561 curr_rq_unlock_irq_restore(&flags);
564 static u64 perf_counter_read(struct perf_counter *counter)
567 * If counter is enabled and currently active on a CPU, update the
568 * value in the counter structure:
570 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
571 smp_call_function_single(counter->oncpu,
575 return atomic64_read(&counter->count);
579 * Cross CPU call to switch performance data pointers
581 static void __perf_switch_irq_data(void *info)
583 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
584 struct perf_counter *counter = info;
585 struct perf_counter_context *ctx = counter->ctx;
586 struct perf_data *oldirqdata = counter->irqdata;
589 * If this is a task context, we need to check whether it is
590 * the current task context of this cpu. If not it has been
591 * scheduled out before the smp call arrived.
594 if (cpuctx->task_ctx != ctx)
596 spin_lock(&ctx->lock);
599 /* Change the pointer NMI safe */
600 atomic_long_set((atomic_long_t *)&counter->irqdata,
601 (unsigned long) counter->usrdata);
602 counter->usrdata = oldirqdata;
605 spin_unlock(&ctx->lock);
608 static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
610 struct perf_counter_context *ctx = counter->ctx;
611 struct perf_data *oldirqdata = counter->irqdata;
612 struct task_struct *task = ctx->task;
615 smp_call_function_single(counter->cpu,
616 __perf_switch_irq_data,
618 return counter->usrdata;
622 spin_lock_irq(&ctx->lock);
623 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
624 counter->irqdata = counter->usrdata;
625 counter->usrdata = oldirqdata;
626 spin_unlock_irq(&ctx->lock);
629 spin_unlock_irq(&ctx->lock);
630 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
631 /* Might have failed, because task was scheduled out */
632 if (counter->irqdata == oldirqdata)
635 return counter->usrdata;
638 static void put_context(struct perf_counter_context *ctx)
641 put_task_struct(ctx->task);
644 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
646 struct perf_cpu_context *cpuctx;
647 struct perf_counter_context *ctx;
648 struct task_struct *task;
651 * If cpu is not a wildcard then this is a percpu counter:
654 /* Must be root to operate on a CPU counter: */
655 if (!capable(CAP_SYS_ADMIN))
656 return ERR_PTR(-EACCES);
658 if (cpu < 0 || cpu > num_possible_cpus())
659 return ERR_PTR(-EINVAL);
662 * We could be clever and allow to attach a counter to an
663 * offline CPU and activate it when the CPU comes up, but
666 if (!cpu_isset(cpu, cpu_online_map))
667 return ERR_PTR(-ENODEV);
669 cpuctx = &per_cpu(perf_cpu_context, cpu);
679 task = find_task_by_vpid(pid);
681 get_task_struct(task);
685 return ERR_PTR(-ESRCH);
687 ctx = &task->perf_counter_ctx;
690 /* Reuse ptrace permission checks for now. */
691 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
693 return ERR_PTR(-EACCES);
700 * Called when the last reference to the file is gone.
702 static int perf_release(struct inode *inode, struct file *file)
704 struct perf_counter *counter = file->private_data;
705 struct perf_counter_context *ctx = counter->ctx;
707 file->private_data = NULL;
709 mutex_lock(&counter->mutex);
711 perf_counter_remove_from_context(counter);
714 mutex_unlock(&counter->mutex);
722 * Read the performance counter - simple non blocking version for now
725 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
729 if (count != sizeof(cntval))
732 mutex_lock(&counter->mutex);
733 cntval = perf_counter_read(counter);
734 mutex_unlock(&counter->mutex);
736 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
740 perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
745 count = min(count, (size_t)usrdata->len);
746 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
749 /* Adjust the counters */
750 usrdata->len -= count;
754 usrdata->rd_idx += count;
760 perf_read_irq_data(struct perf_counter *counter,
765 struct perf_data *irqdata, *usrdata;
766 DECLARE_WAITQUEUE(wait, current);
769 irqdata = counter->irqdata;
770 usrdata = counter->usrdata;
772 if (usrdata->len + irqdata->len >= count)
778 spin_lock_irq(&counter->waitq.lock);
779 __add_wait_queue(&counter->waitq, &wait);
781 set_current_state(TASK_INTERRUPTIBLE);
782 if (usrdata->len + irqdata->len >= count)
785 if (signal_pending(current))
788 spin_unlock_irq(&counter->waitq.lock);
790 spin_lock_irq(&counter->waitq.lock);
792 __remove_wait_queue(&counter->waitq, &wait);
793 __set_current_state(TASK_RUNNING);
794 spin_unlock_irq(&counter->waitq.lock);
796 if (usrdata->len + irqdata->len < count)
799 mutex_lock(&counter->mutex);
801 /* Drain pending data first: */
802 res = perf_copy_usrdata(usrdata, buf, count);
803 if (res < 0 || res == count)
806 /* Switch irq buffer: */
807 usrdata = perf_switch_irq_data(counter);
808 if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) {
815 mutex_unlock(&counter->mutex);
821 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
823 struct perf_counter *counter = file->private_data;
825 switch (counter->hw_event.record_type) {
826 case PERF_RECORD_SIMPLE:
827 return perf_read_hw(counter, buf, count);
829 case PERF_RECORD_IRQ:
830 case PERF_RECORD_GROUP:
831 return perf_read_irq_data(counter, buf, count,
832 file->f_flags & O_NONBLOCK);
837 static unsigned int perf_poll(struct file *file, poll_table *wait)
839 struct perf_counter *counter = file->private_data;
840 unsigned int events = 0;
843 poll_wait(file, &counter->waitq, wait);
845 spin_lock_irqsave(&counter->waitq.lock, flags);
846 if (counter->usrdata->len || counter->irqdata->len)
848 spin_unlock_irqrestore(&counter->waitq.lock, flags);
853 static const struct file_operations perf_fops = {
854 .release = perf_release,
859 static void cpu_clock_perf_counter_enable(struct perf_counter *counter)
863 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
867 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
869 int cpu = raw_smp_processor_id();
871 atomic64_set(&counter->count, cpu_clock(cpu));
874 static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
875 .enable = cpu_clock_perf_counter_enable,
876 .disable = cpu_clock_perf_counter_disable,
877 .read = cpu_clock_perf_counter_read,
881 * Called from within the scheduler:
883 static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
885 struct task_struct *curr = counter->task;
888 WARN_ON_ONCE(counter->task != current);
890 delta = __task_delta_exec(curr, update);
892 return curr->se.sum_exec_runtime + delta;
895 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
900 prev = atomic64_read(&counter->hw.prev_count);
902 atomic64_set(&counter->hw.prev_count, now);
906 atomic64_add(delta, &counter->count);
909 static void task_clock_perf_counter_read(struct perf_counter *counter)
911 u64 now = task_clock_perf_counter_val(counter, 1);
913 task_clock_perf_counter_update(counter, now);
916 static void task_clock_perf_counter_enable(struct perf_counter *counter)
918 u64 now = task_clock_perf_counter_val(counter, 0);
920 atomic64_set(&counter->hw.prev_count, now);
923 static void task_clock_perf_counter_disable(struct perf_counter *counter)
925 u64 now = task_clock_perf_counter_val(counter, 0);
927 task_clock_perf_counter_update(counter, now);
930 static const struct hw_perf_counter_ops perf_ops_task_clock = {
931 .enable = task_clock_perf_counter_enable,
932 .disable = task_clock_perf_counter_disable,
933 .read = task_clock_perf_counter_read,
936 static u64 get_page_faults(void)
938 struct task_struct *curr = current;
940 return curr->maj_flt + curr->min_flt;
943 static void page_faults_perf_counter_update(struct perf_counter *counter)
948 prev = atomic64_read(&counter->hw.prev_count);
949 now = get_page_faults();
951 atomic64_set(&counter->hw.prev_count, now);
955 atomic64_add(delta, &counter->count);
958 static void page_faults_perf_counter_read(struct perf_counter *counter)
960 page_faults_perf_counter_update(counter);
963 static void page_faults_perf_counter_enable(struct perf_counter *counter)
966 * page-faults is a per-task value already,
967 * so we dont have to clear it on switch-in.
971 static void page_faults_perf_counter_disable(struct perf_counter *counter)
973 page_faults_perf_counter_update(counter);
976 static const struct hw_perf_counter_ops perf_ops_page_faults = {
977 .enable = page_faults_perf_counter_enable,
978 .disable = page_faults_perf_counter_disable,
979 .read = page_faults_perf_counter_read,
982 static u64 get_context_switches(void)
984 struct task_struct *curr = current;
986 return curr->nvcsw + curr->nivcsw;
989 static void context_switches_perf_counter_update(struct perf_counter *counter)
994 prev = atomic64_read(&counter->hw.prev_count);
995 now = get_context_switches();
997 atomic64_set(&counter->hw.prev_count, now);
1001 atomic64_add(delta, &counter->count);
1004 static void context_switches_perf_counter_read(struct perf_counter *counter)
1006 context_switches_perf_counter_update(counter);
1009 static void context_switches_perf_counter_enable(struct perf_counter *counter)
1012 * ->nvcsw + curr->nivcsw is a per-task value already,
1013 * so we dont have to clear it on switch-in.
1017 static void context_switches_perf_counter_disable(struct perf_counter *counter)
1019 context_switches_perf_counter_update(counter);
1022 static const struct hw_perf_counter_ops perf_ops_context_switches = {
1023 .enable = context_switches_perf_counter_enable,
1024 .disable = context_switches_perf_counter_disable,
1025 .read = context_switches_perf_counter_read,
1028 static inline u64 get_cpu_migrations(void)
1030 return current->se.nr_migrations;
1033 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1038 prev = atomic64_read(&counter->hw.prev_count);
1039 now = get_cpu_migrations();
1041 atomic64_set(&counter->hw.prev_count, now);
1045 atomic64_add(delta, &counter->count);
1048 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1050 cpu_migrations_perf_counter_update(counter);
1053 static void cpu_migrations_perf_counter_enable(struct perf_counter *counter)
1056 * se.nr_migrations is a per-task value already,
1057 * so we dont have to clear it on switch-in.
1061 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1063 cpu_migrations_perf_counter_update(counter);
1066 static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
1067 .enable = cpu_migrations_perf_counter_enable,
1068 .disable = cpu_migrations_perf_counter_disable,
1069 .read = cpu_migrations_perf_counter_read,
1072 static const struct hw_perf_counter_ops *
1073 sw_perf_counter_init(struct perf_counter *counter)
1075 const struct hw_perf_counter_ops *hw_ops = NULL;
1077 switch (counter->hw_event.type) {
1078 case PERF_COUNT_CPU_CLOCK:
1079 hw_ops = &perf_ops_cpu_clock;
1081 case PERF_COUNT_TASK_CLOCK:
1082 hw_ops = &perf_ops_task_clock;
1084 case PERF_COUNT_PAGE_FAULTS:
1085 hw_ops = &perf_ops_page_faults;
1087 case PERF_COUNT_CONTEXT_SWITCHES:
1088 hw_ops = &perf_ops_context_switches;
1090 case PERF_COUNT_CPU_MIGRATIONS:
1091 hw_ops = &perf_ops_cpu_migrations;
1100 * Allocate and initialize a counter structure
1102 static struct perf_counter *
1103 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1105 struct perf_counter *group_leader,
1108 const struct hw_perf_counter_ops *hw_ops;
1109 struct perf_counter *counter;
1111 counter = kzalloc(sizeof(*counter), gfpflags);
1116 * Single counters are their own group leaders, with an
1117 * empty sibling list:
1120 group_leader = counter;
1122 mutex_init(&counter->mutex);
1123 INIT_LIST_HEAD(&counter->list_entry);
1124 INIT_LIST_HEAD(&counter->sibling_list);
1125 init_waitqueue_head(&counter->waitq);
1127 counter->irqdata = &counter->data[0];
1128 counter->usrdata = &counter->data[1];
1130 counter->hw_event = *hw_event;
1131 counter->wakeup_pending = 0;
1132 counter->group_leader = group_leader;
1133 counter->hw_ops = NULL;
1135 if (hw_event->disabled)
1136 counter->state = PERF_COUNTER_STATE_OFF;
1139 if (!hw_event->raw && hw_event->type < 0)
1140 hw_ops = sw_perf_counter_init(counter);
1142 hw_ops = hw_perf_counter_init(counter);
1148 counter->hw_ops = hw_ops;
1154 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1156 * @hw_event_uptr: event type attributes for monitoring/sampling
1159 * @group_fd: group leader counter fd
1162 sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1163 pid_t pid, int cpu, int group_fd)
1165 struct perf_counter *counter, *group_leader;
1166 struct perf_counter_hw_event hw_event;
1167 struct perf_counter_context *ctx;
1168 struct file *counter_file = NULL;
1169 struct file *group_file = NULL;
1170 int fput_needed = 0;
1171 int fput_needed2 = 0;
1174 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
1178 * Get the target context (task or percpu):
1180 ctx = find_get_context(pid, cpu);
1182 return PTR_ERR(ctx);
1185 * Look up the group leader (we will attach this counter to it):
1187 group_leader = NULL;
1188 if (group_fd != -1) {
1190 group_file = fget_light(group_fd, &fput_needed);
1192 goto err_put_context;
1193 if (group_file->f_op != &perf_fops)
1194 goto err_put_context;
1196 group_leader = group_file->private_data;
1198 * Do not allow a recursive hierarchy (this new sibling
1199 * becoming part of another group-sibling):
1201 if (group_leader->group_leader != group_leader)
1202 goto err_put_context;
1204 * Do not allow to attach to a group in a different
1205 * task or CPU context:
1207 if (group_leader->ctx != ctx)
1208 goto err_put_context;
1212 counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL);
1214 goto err_put_context;
1216 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1218 goto err_free_put_context;
1220 counter_file = fget_light(ret, &fput_needed2);
1222 goto err_free_put_context;
1224 counter->filp = counter_file;
1225 perf_install_in_context(ctx, counter, cpu);
1227 fput_light(counter_file, fput_needed2);
1230 fput_light(group_file, fput_needed);
1234 err_free_put_context:
1244 * Initialize the perf_counter context in a task_struct:
1247 __perf_counter_init_context(struct perf_counter_context *ctx,
1248 struct task_struct *task)
1250 memset(ctx, 0, sizeof(*ctx));
1251 spin_lock_init(&ctx->lock);
1252 INIT_LIST_HEAD(&ctx->counter_list);
1257 * inherit a counter from parent task to child task:
1260 inherit_counter(struct perf_counter *parent_counter,
1261 struct task_struct *parent,
1262 struct perf_counter_context *parent_ctx,
1263 struct task_struct *child,
1264 struct perf_counter_context *child_ctx)
1266 struct perf_counter *child_counter;
1268 child_counter = perf_counter_alloc(&parent_counter->hw_event,
1269 parent_counter->cpu, NULL,
1275 * Link it up in the child's context:
1277 child_counter->ctx = child_ctx;
1278 child_counter->task = child;
1279 list_add_counter(child_counter, child_ctx);
1280 child_ctx->nr_counters++;
1282 child_counter->parent = parent_counter;
1283 parent_counter->nr_inherited++;
1285 * inherit into child's child as well:
1287 child_counter->hw_event.inherit = 1;
1290 * Get a reference to the parent filp - we will fput it
1291 * when the child counter exits. This is safe to do because
1292 * we are in the parent and we know that the filp still
1293 * exists and has a nonzero count:
1295 atomic_long_inc(&parent_counter->filp->f_count);
1301 __perf_counter_exit_task(struct task_struct *child,
1302 struct perf_counter *child_counter,
1303 struct perf_counter_context *child_ctx)
1305 struct perf_counter *parent_counter;
1306 u64 parent_val, child_val;
1307 unsigned long flags;
1311 * Disable and unlink this counter.
1313 * Be careful about zapping the list - IRQ/NMI context
1314 * could still be processing it:
1316 curr_rq_lock_irq_save(&flags);
1317 perf_flags = hw_perf_save_disable();
1319 if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) {
1320 struct perf_cpu_context *cpuctx;
1322 cpuctx = &__get_cpu_var(perf_cpu_context);
1324 child_counter->hw_ops->disable(child_counter);
1325 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
1326 child_counter->oncpu = -1;
1328 cpuctx->active_oncpu--;
1329 child_ctx->nr_active--;
1332 list_del_init(&child_counter->list_entry);
1334 hw_perf_restore(perf_flags);
1335 curr_rq_unlock_irq_restore(&flags);
1337 parent_counter = child_counter->parent;
1339 * It can happen that parent exits first, and has counters
1340 * that are still around due to the child reference. These
1341 * counters need to be zapped - but otherwise linger.
1343 if (!parent_counter)
1346 parent_val = atomic64_read(&parent_counter->count);
1347 child_val = atomic64_read(&child_counter->count);
1350 * Add back the child's count to the parent's count:
1352 atomic64_add(child_val, &parent_counter->count);
1354 fput(parent_counter->filp);
1356 kfree(child_counter);
1360 * When a child task exist, feed back counter values to parent counters.
1362 * Note: we are running in child context, but the PID is not hashed
1363 * anymore so new counters will not be added.
1365 void perf_counter_exit_task(struct task_struct *child)
1367 struct perf_counter *child_counter, *tmp;
1368 struct perf_counter_context *child_ctx;
1370 child_ctx = &child->perf_counter_ctx;
1372 if (likely(!child_ctx->nr_counters))
1375 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1377 __perf_counter_exit_task(child, child_counter, child_ctx);
1381 * Initialize the perf_counter context in task_struct
1383 void perf_counter_init_task(struct task_struct *child)
1385 struct perf_counter_context *child_ctx, *parent_ctx;
1386 struct perf_counter *counter, *parent_counter;
1387 struct task_struct *parent = current;
1388 unsigned long flags;
1390 child_ctx = &child->perf_counter_ctx;
1391 parent_ctx = &parent->perf_counter_ctx;
1393 __perf_counter_init_context(child_ctx, child);
1396 * This is executed from the parent task context, so inherit
1397 * counters that have been marked for cloning:
1400 if (likely(!parent_ctx->nr_counters))
1404 * Lock the parent list. No need to lock the child - not PID
1405 * hashed yet and not running, so nobody can access it.
1407 spin_lock_irqsave(&parent_ctx->lock, flags);
1410 * We dont have to disable NMIs - we are only looking at
1411 * the list, not manipulating it:
1413 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
1414 if (!counter->hw_event.inherit || counter->group_leader != counter)
1418 * Instead of creating recursive hierarchies of counters,
1419 * we link inheritd counters back to the original parent,
1420 * which has a filp for sure, which we use as the reference
1423 parent_counter = counter;
1424 if (counter->parent)
1425 parent_counter = counter->parent;
1427 if (inherit_counter(parent_counter, parent,
1428 parent_ctx, child, child_ctx))
1432 spin_unlock_irqrestore(&parent_ctx->lock, flags);
1435 static void __cpuinit perf_counter_init_cpu(int cpu)
1437 struct perf_cpu_context *cpuctx;
1439 cpuctx = &per_cpu(perf_cpu_context, cpu);
1440 __perf_counter_init_context(&cpuctx->ctx, NULL);
1442 mutex_lock(&perf_resource_mutex);
1443 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
1444 mutex_unlock(&perf_resource_mutex);
1446 hw_perf_counter_setup();
1449 #ifdef CONFIG_HOTPLUG_CPU
1450 static void __perf_counter_exit_cpu(void *info)
1452 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1453 struct perf_counter_context *ctx = &cpuctx->ctx;
1454 struct perf_counter *counter, *tmp;
1456 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
1457 __perf_counter_remove_from_context(counter);
1460 static void perf_counter_exit_cpu(int cpu)
1462 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
1465 static inline void perf_counter_exit_cpu(int cpu) { }
1468 static int __cpuinit
1469 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1471 unsigned int cpu = (long)hcpu;
1475 case CPU_UP_PREPARE:
1476 case CPU_UP_PREPARE_FROZEN:
1477 perf_counter_init_cpu(cpu);
1480 case CPU_DOWN_PREPARE:
1481 case CPU_DOWN_PREPARE_FROZEN:
1482 perf_counter_exit_cpu(cpu);
1492 static struct notifier_block __cpuinitdata perf_cpu_nb = {
1493 .notifier_call = perf_cpu_notify,
1496 static int __init perf_counter_init(void)
1498 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
1499 (void *)(long)smp_processor_id());
1500 register_cpu_notifier(&perf_cpu_nb);
1504 early_initcall(perf_counter_init);
1506 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
1508 return sprintf(buf, "%d\n", perf_reserved_percpu);
1512 perf_set_reserve_percpu(struct sysdev_class *class,
1516 struct perf_cpu_context *cpuctx;
1520 err = strict_strtoul(buf, 10, &val);
1523 if (val > perf_max_counters)
1526 mutex_lock(&perf_resource_mutex);
1527 perf_reserved_percpu = val;
1528 for_each_online_cpu(cpu) {
1529 cpuctx = &per_cpu(perf_cpu_context, cpu);
1530 spin_lock_irq(&cpuctx->ctx.lock);
1531 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
1532 perf_max_counters - perf_reserved_percpu);
1533 cpuctx->max_pertask = mpt;
1534 spin_unlock_irq(&cpuctx->ctx.lock);
1536 mutex_unlock(&perf_resource_mutex);
1541 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
1543 return sprintf(buf, "%d\n", perf_overcommit);
1547 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
1552 err = strict_strtoul(buf, 10, &val);
1558 mutex_lock(&perf_resource_mutex);
1559 perf_overcommit = val;
1560 mutex_unlock(&perf_resource_mutex);
1565 static SYSDEV_CLASS_ATTR(
1568 perf_show_reserve_percpu,
1569 perf_set_reserve_percpu
1572 static SYSDEV_CLASS_ATTR(
1575 perf_show_overcommit,
1579 static struct attribute *perfclass_attrs[] = {
1580 &attr_reserve_percpu.attr,
1581 &attr_overcommit.attr,
1585 static struct attribute_group perfclass_attr_group = {
1586 .attrs = perfclass_attrs,
1587 .name = "perf_counters",
1590 static int __init perf_counter_sysfs_init(void)
1592 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
1593 &perfclass_attr_group);
1595 device_initcall(perf_counter_sysfs_init);