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); |
| 23 | /* Reset per_cpu variables that store counter values uppn CPU hotplug */ |
| 24 | static int tracectr_cpu_hotplug_notifier(struct notifier_block *self, |
| 25 | unsigned long action, void *hcpu) |
| 26 | { |
| 27 | int ret = NOTIFY_OK; |
| 28 | int cpu = (int)hcpu; |
| 29 | int i; |
| 30 | |
| 31 | if ((action & (~CPU_TASKS_FROZEN)) == CPU_UP_PREPARE) { |
| 32 | per_cpu(previous_ccnt, cpu) = 0; |
| 33 | for (i = 0; i < NUM_L1_CTRS; i++) |
| 34 | per_cpu(previous_l1_cnts[i], cpu) = 0; |
| 35 | for (i = 0; i < NUM_L2_PERCPU; i++) |
| 36 | per_cpu(previous_l2_cnts[i], cpu) = 0; |
| 37 | } |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | static struct notifier_block tracectr_cpu_hotplug_notifier_block = { |
| 42 | .notifier_call = tracectr_cpu_hotplug_notifier, |
| 43 | }; |
| 44 | |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 45 | static int tracectr_notifier(struct notifier_block *self, unsigned long cmd, |
| 46 | void *v) |
| 47 | { |
| 48 | static int old_pid = -1; |
| 49 | struct thread_info *thread = v; |
| 50 | int current_pid; |
| 51 | |
| 52 | if (cmd != THREAD_NOTIFY_SWITCH) |
| 53 | return old_pid; |
| 54 | |
| 55 | current_pid = thread->task->pid; |
| 56 | if (old_pid != -1) |
| 57 | trace_sched_switch_with_ctrs(old_pid, current_pid); |
| 58 | old_pid = current_pid; |
| 59 | return old_pid; |
| 60 | } |
| 61 | |
| 62 | static struct notifier_block tracectr_notifier_block = { |
| 63 | .notifier_call = tracectr_notifier, |
| 64 | }; |
| 65 | |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 66 | static void enable_tp_pid(void) |
| 67 | { |
| 68 | if (tp_pid_state == 0) { |
| 69 | tp_pid_state = 1; |
| 70 | thread_register_notifier(&tracectr_notifier_block); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static void disable_tp_pid(void) |
| 75 | { |
| 76 | if (tp_pid_state == 1) { |
| 77 | tp_pid_state = 0; |
| 78 | thread_unregister_notifier(&tracectr_notifier_block); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static ssize_t read_enabled_perftp_file_bool(struct file *file, |
| 83 | char __user *user_buf, size_t count, loff_t *ppos) |
| 84 | { |
| 85 | char buf[2]; |
| 86 | buf[1] = '\n'; |
| 87 | if (tp_pid_state == 0) |
| 88 | buf[0] = '0'; |
| 89 | else |
| 90 | buf[0] = '1'; |
| 91 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 92 | } |
| 93 | |
| 94 | static ssize_t write_enabled_perftp_file_bool(struct file *file, |
| 95 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 96 | { |
| 97 | char buf[32]; |
| 98 | size_t buf_size; |
| 99 | |
| 100 | buf_size = min(count, (sizeof(buf)-1)); |
| 101 | if (copy_from_user(buf, user_buf, buf_size)) |
| 102 | return -EFAULT; |
| 103 | switch (buf[0]) { |
| 104 | case 'y': |
| 105 | case 'Y': |
| 106 | case '1': |
| 107 | enable_tp_pid(); |
| 108 | break; |
| 109 | case 'n': |
| 110 | case 'N': |
| 111 | case '0': |
| 112 | disable_tp_pid(); |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | return count; |
| 117 | } |
| 118 | |
| 119 | static const struct file_operations fops_perftp = { |
| 120 | .read = read_enabled_perftp_file_bool, |
| 121 | .write = write_enabled_perftp_file_bool, |
| 122 | .llseek = default_llseek, |
| 123 | }; |
| 124 | |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 125 | int __init init_tracecounters(void) |
| 126 | { |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 127 | struct dentry *dir; |
| 128 | struct dentry *file; |
| 129 | unsigned int value = 1; |
| 130 | |
| 131 | dir = debugfs_create_dir("perf_debug_tp", NULL); |
| 132 | if (!dir) |
| 133 | return -ENOMEM; |
Sheetal Sahasrabudhe | bf3e69b | 2013-08-12 13:53:59 -0400 | [diff] [blame] | 134 | file = debugfs_create_file("enabled", 0660, dir, |
Sheetal Sahasrabudhe | 711300e | 2013-07-10 12:18:59 -0400 | [diff] [blame] | 135 | &value, &fops_perftp); |
| 136 | if (!file) { |
| 137 | debugfs_remove(dir); |
| 138 | return -ENOMEM; |
| 139 | } |
Sheetal Sahasrabudhe | e2ec980 | 2013-10-15 14:48:52 -0400 | [diff] [blame^] | 140 | register_cpu_notifier(&tracectr_cpu_hotplug_notifier_block); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int __exit exit_tracecounters(void) |
| 145 | { |
| 146 | unregister_cpu_notifier(&tracectr_cpu_hotplug_notifier_block); |
Ashwin Chaugule | 4eaee1a | 2013-03-14 18:37:49 -0400 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | late_initcall(init_tracecounters); |