Sheetal Sahasrabudhe | fa00345 | 2014-01-08 16:50:42 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved. |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 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 | 954248e | 2014-02-07 12:21:17 -0500 | [diff] [blame^] | 24 | DEFINE_PER_CPU(u32, hotplug_flag); |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 25 | /* Reset per_cpu variables that store counter values uppn CPU hotplug */ |
| 26 | static int tracectr_cpu_hotplug_notifier(struct notifier_block *self, |
| 27 | unsigned long action, void *hcpu) |
| 28 | { |
| 29 | int ret = NOTIFY_OK; |
| 30 | int cpu = (int)hcpu; |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 31 | |
Sheetal Sahasrabudhe | 954248e | 2014-02-07 12:21:17 -0500 | [diff] [blame^] | 32 | if ((action & (~CPU_TASKS_FROZEN)) == CPU_STARTING) |
| 33 | per_cpu(hotplug_flag, cpu) = 1; |
| 34 | |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 35 | return ret; |
| 36 | } |
| 37 | |
| 38 | static struct notifier_block tracectr_cpu_hotplug_notifier_block = { |
| 39 | .notifier_call = tracectr_cpu_hotplug_notifier, |
| 40 | }; |
| 41 | |
Sheetal Sahasrabudhe | 954248e | 2014-02-07 12:21:17 -0500 | [diff] [blame^] | 42 | static void setup_prev_cnts(u32 cpu) |
| 43 | { |
| 44 | int i; |
| 45 | u32 cnten_val; |
| 46 | |
| 47 | /* Read PMCNTENSET */ |
| 48 | asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r"(cnten_val)); |
| 49 | /* Disable all the counters that were enabled */ |
| 50 | asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r"(cnten_val)); |
| 51 | if (cnten_val & CC) { |
| 52 | /* Read value */ |
| 53 | asm volatile("mrc p15, 0, %0, c9, c13, 0" |
| 54 | : "=r"(per_cpu(previous_ccnt, cpu))); |
| 55 | } |
| 56 | |
| 57 | for (i = 0; i < NUM_L1_CTRS; i++) { |
| 58 | if (cnten_val & (1 << i)) { |
| 59 | /* Select */ |
| 60 | asm volatile("mcr p15, 0, %0, c9, c12, 5" |
| 61 | : : "r"(i)); |
| 62 | /* Read value */ |
| 63 | asm volatile("mrc p15, 0, %0, c9, c13, 2" |
| 64 | : "=r"(per_cpu(previous_l1_cnts[i], cpu))); |
| 65 | } |
| 66 | } |
| 67 | /* Enable all the counters that were disabled */ |
| 68 | asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r"(cnten_val)); |
| 69 | } |
| 70 | |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 71 | static int tracectr_notifier(struct notifier_block *self, unsigned long cmd, |
| 72 | void *v) |
| 73 | { |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 74 | struct thread_info *thread = v; |
| 75 | int current_pid; |
Sheetal Sahasrabudhe | fa00345 | 2014-01-08 16:50:42 -0500 | [diff] [blame] | 76 | u32 cpu = thread->cpu; |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 77 | |
| 78 | if (cmd != THREAD_NOTIFY_SWITCH) |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame] | 79 | return -EFAULT; |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 80 | |
| 81 | current_pid = thread->task->pid; |
Sheetal Sahasrabudhe | 954248e | 2014-02-07 12:21:17 -0500 | [diff] [blame^] | 82 | if (per_cpu(old_pid, cpu) != -1) { |
| 83 | if (per_cpu(hotplug_flag, cpu) == 1) { |
| 84 | per_cpu(hotplug_flag, cpu) = 0; |
| 85 | setup_prev_cnts(cpu); |
| 86 | } else |
| 87 | trace_sched_switch_with_ctrs(per_cpu(old_pid, cpu), |
| 88 | current_pid); |
| 89 | } |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame] | 90 | per_cpu(old_pid, cpu) = current_pid; |
| 91 | return NOTIFY_OK; |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static struct notifier_block tracectr_notifier_block = { |
| 95 | .notifier_call = tracectr_notifier, |
| 96 | }; |
| 97 | |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 98 | static void enable_tp_pid(void) |
| 99 | { |
| 100 | if (tp_pid_state == 0) { |
| 101 | tp_pid_state = 1; |
| 102 | thread_register_notifier(&tracectr_notifier_block); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void disable_tp_pid(void) |
| 107 | { |
| 108 | if (tp_pid_state == 1) { |
| 109 | tp_pid_state = 0; |
| 110 | thread_unregister_notifier(&tracectr_notifier_block); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static ssize_t read_enabled_perftp_file_bool(struct file *file, |
| 115 | char __user *user_buf, size_t count, loff_t *ppos) |
| 116 | { |
| 117 | char buf[2]; |
| 118 | buf[1] = '\n'; |
| 119 | if (tp_pid_state == 0) |
| 120 | buf[0] = '0'; |
| 121 | else |
| 122 | buf[0] = '1'; |
| 123 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 124 | } |
| 125 | |
| 126 | static ssize_t write_enabled_perftp_file_bool(struct file *file, |
| 127 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 128 | { |
| 129 | char buf[32]; |
| 130 | size_t buf_size; |
| 131 | |
| 132 | buf_size = min(count, (sizeof(buf)-1)); |
| 133 | if (copy_from_user(buf, user_buf, buf_size)) |
| 134 | return -EFAULT; |
| 135 | switch (buf[0]) { |
| 136 | case 'y': |
| 137 | case 'Y': |
| 138 | case '1': |
| 139 | enable_tp_pid(); |
| 140 | break; |
| 141 | case 'n': |
| 142 | case 'N': |
| 143 | case '0': |
| 144 | disable_tp_pid(); |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | return count; |
| 149 | } |
| 150 | |
| 151 | static const struct file_operations fops_perftp = { |
| 152 | .read = read_enabled_perftp_file_bool, |
| 153 | .write = write_enabled_perftp_file_bool, |
| 154 | .llseek = default_llseek, |
| 155 | }; |
| 156 | |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 157 | int __init init_tracecounters(void) |
| 158 | { |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 159 | struct dentry *dir; |
| 160 | struct dentry *file; |
| 161 | unsigned int value = 1; |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame] | 162 | int cpu; |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 163 | |
| 164 | dir = debugfs_create_dir("perf_debug_tp", NULL); |
| 165 | if (!dir) |
| 166 | return -ENOMEM; |
Sheetal Sahasrabudhe | bf3e69b | 2013-08-12 13:53:59 -0400 | [diff] [blame] | 167 | file = debugfs_create_file("enabled", 0660, dir, |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 168 | &value, &fops_perftp); |
| 169 | if (!file) { |
| 170 | debugfs_remove(dir); |
| 171 | return -ENOMEM; |
| 172 | } |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 173 | register_cpu_notifier(&tracectr_cpu_hotplug_notifier_block); |
Sheetal Sahasrabudhe | 0b9db08 | 2013-11-20 15:04:04 -0500 | [diff] [blame] | 174 | for_each_possible_cpu(cpu) |
| 175 | per_cpu(old_pid, cpu) = -1; |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | int __exit exit_tracecounters(void) |
| 180 | { |
| 181 | unregister_cpu_notifier(&tracectr_cpu_hotplug_notifier_block); |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | late_initcall(init_tracecounters); |