Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | #include <asm/thread_notify.h> |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
| 14 | #include <linux/debugfs.h> |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 15 | #define CREATE_TRACE_POINTS |
| 16 | #include "perf_trace_counters.h" |
| 17 | |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 18 | static unsigned int tp_pid_state; |
| 19 | |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 20 | DEFINE_PER_CPU(u32, previous_ccnt); |
| 21 | DEFINE_PER_CPU(u32[NUM_L1_CTRS], previous_l1_cnts); |
| 22 | DEFINE_PER_CPU(u32[NUM_L2_PERCPU], previous_l2_cnts); |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame^] | 23 | DEFINE_PER_CPU(u32, old_pid); |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 24 | /* Reset per_cpu variables that store counter values uppn CPU hotplug */ |
| 25 | static int tracectr_cpu_hotplug_notifier(struct notifier_block *self, |
| 26 | unsigned long action, void *hcpu) |
| 27 | { |
| 28 | int ret = NOTIFY_OK; |
| 29 | int cpu = (int)hcpu; |
| 30 | int i; |
| 31 | |
| 32 | if ((action & (~CPU_TASKS_FROZEN)) == CPU_UP_PREPARE) { |
| 33 | per_cpu(previous_ccnt, cpu) = 0; |
| 34 | for (i = 0; i < NUM_L1_CTRS; i++) |
| 35 | per_cpu(previous_l1_cnts[i], cpu) = 0; |
| 36 | for (i = 0; i < NUM_L2_PERCPU; i++) |
| 37 | per_cpu(previous_l2_cnts[i], cpu) = 0; |
| 38 | } |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | static struct notifier_block tracectr_cpu_hotplug_notifier_block = { |
| 43 | .notifier_call = tracectr_cpu_hotplug_notifier, |
| 44 | }; |
| 45 | |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 46 | static int tracectr_notifier(struct notifier_block *self, unsigned long cmd, |
| 47 | void *v) |
| 48 | { |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 49 | struct thread_info *thread = v; |
| 50 | int current_pid; |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame^] | 51 | u32 cpu = smp_processor_id(); |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 52 | |
| 53 | if (cmd != THREAD_NOTIFY_SWITCH) |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame^] | 54 | return -EFAULT; |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 55 | |
| 56 | current_pid = thread->task->pid; |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame^] | 57 | if (per_cpu(old_pid, cpu) != -1) |
| 58 | trace_sched_switch_with_ctrs(per_cpu(old_pid, cpu), |
| 59 | current_pid); |
| 60 | per_cpu(old_pid, cpu) = current_pid; |
| 61 | return NOTIFY_OK; |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static struct notifier_block tracectr_notifier_block = { |
| 65 | .notifier_call = tracectr_notifier, |
| 66 | }; |
| 67 | |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 68 | static void enable_tp_pid(void) |
| 69 | { |
| 70 | if (tp_pid_state == 0) { |
| 71 | tp_pid_state = 1; |
| 72 | thread_register_notifier(&tracectr_notifier_block); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void disable_tp_pid(void) |
| 77 | { |
| 78 | if (tp_pid_state == 1) { |
| 79 | tp_pid_state = 0; |
| 80 | thread_unregister_notifier(&tracectr_notifier_block); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static ssize_t read_enabled_perftp_file_bool(struct file *file, |
| 85 | char __user *user_buf, size_t count, loff_t *ppos) |
| 86 | { |
| 87 | char buf[2]; |
| 88 | buf[1] = '\n'; |
| 89 | if (tp_pid_state == 0) |
| 90 | buf[0] = '0'; |
| 91 | else |
| 92 | buf[0] = '1'; |
| 93 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 94 | } |
| 95 | |
| 96 | static ssize_t write_enabled_perftp_file_bool(struct file *file, |
| 97 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 98 | { |
| 99 | char buf[32]; |
| 100 | size_t buf_size; |
| 101 | |
| 102 | buf_size = min(count, (sizeof(buf)-1)); |
| 103 | if (copy_from_user(buf, user_buf, buf_size)) |
| 104 | return -EFAULT; |
| 105 | switch (buf[0]) { |
| 106 | case 'y': |
| 107 | case 'Y': |
| 108 | case '1': |
| 109 | enable_tp_pid(); |
| 110 | break; |
| 111 | case 'n': |
| 112 | case 'N': |
| 113 | case '0': |
| 114 | disable_tp_pid(); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | return count; |
| 119 | } |
| 120 | |
| 121 | static const struct file_operations fops_perftp = { |
| 122 | .read = read_enabled_perftp_file_bool, |
| 123 | .write = write_enabled_perftp_file_bool, |
| 124 | .llseek = default_llseek, |
| 125 | }; |
| 126 | |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 127 | int __init init_tracecounters(void) |
| 128 | { |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 129 | struct dentry *dir; |
| 130 | struct dentry *file; |
| 131 | unsigned int value = 1; |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame^] | 132 | int cpu; |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 133 | |
| 134 | dir = debugfs_create_dir("perf_debug_tp", NULL); |
| 135 | if (!dir) |
| 136 | return -ENOMEM; |
Sheetal Sahasrabudhe | bf3e69b | 2013-08-12 13:53:59 -0400 | [diff] [blame] | 137 | file = debugfs_create_file("enabled", 0660, dir, |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 138 | &value, &fops_perftp); |
| 139 | if (!file) { |
| 140 | debugfs_remove(dir); |
| 141 | return -ENOMEM; |
| 142 | } |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 143 | register_cpu_notifier(&tracectr_cpu_hotplug_notifier_block); |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame^] | 144 | for_each_possible_cpu(cpu) |
| 145 | per_cpu(old_pid, cpu) = -1; |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | int __exit exit_tracecounters(void) |
| 150 | { |
| 151 | unregister_cpu_notifier(&tracectr_cpu_hotplug_notifier_block); |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | late_initcall(init_tracecounters); |