blob: 0a679b1ac855d12f1f37edc6a23867baea1784bd [file] [log] [blame]
Sheetal Sahasrabudhefa003452014-01-08 16:50:42 -05001/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -04002 *
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 Sahasrabudhe711300e2013-07-10 12:18:59 -040013#include <linux/uaccess.h>
14#include <linux/debugfs.h>
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040015#define CREATE_TRACE_POINTS
16#include "perf_trace_counters.h"
17
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -040018static unsigned int tp_pid_state;
19
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -040020DEFINE_PER_CPU(u32, previous_ccnt);
21DEFINE_PER_CPU(u32[NUM_L1_CTRS], previous_l1_cnts);
22DEFINE_PER_CPU(u32[NUM_L2_PERCPU], previous_l2_cnts);
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -050023DEFINE_PER_CPU(u32, old_pid);
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -040024/* Reset per_cpu variables that store counter values uppn CPU hotplug */
25static 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
42static struct notifier_block tracectr_cpu_hotplug_notifier_block = {
43 .notifier_call = tracectr_cpu_hotplug_notifier,
44};
45
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040046static int tracectr_notifier(struct notifier_block *self, unsigned long cmd,
47 void *v)
48{
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040049 struct thread_info *thread = v;
50 int current_pid;
Sheetal Sahasrabudhefa003452014-01-08 16:50:42 -050051 u32 cpu = thread->cpu;
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040052
53 if (cmd != THREAD_NOTIFY_SWITCH)
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -050054 return -EFAULT;
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040055
56 current_pid = thread->task->pid;
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -050057 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 Chaugule4eaee1a2013-03-14 18:37:49 -040062}
63
64static struct notifier_block tracectr_notifier_block = {
65 .notifier_call = tracectr_notifier,
66};
67
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -040068static 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
76static 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
84static 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
96static 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
121static 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 Chaugule4eaee1a2013-03-14 18:37:49 -0400127int __init init_tracecounters(void)
128{
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -0400129 struct dentry *dir;
130 struct dentry *file;
131 unsigned int value = 1;
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -0500132 int cpu;
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -0400133
134 dir = debugfs_create_dir("perf_debug_tp", NULL);
135 if (!dir)
136 return -ENOMEM;
Sheetal Sahasrabudhebf3e69b2013-08-12 13:53:59 -0400137 file = debugfs_create_file("enabled", 0660, dir,
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -0400138 &value, &fops_perftp);
139 if (!file) {
140 debugfs_remove(dir);
141 return -ENOMEM;
142 }
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -0400143 register_cpu_notifier(&tracectr_cpu_hotplug_notifier_block);
Sheetal Sahasrabudhe0b9db082013-11-20 15:04:04 -0500144 for_each_possible_cpu(cpu)
145 per_cpu(old_pid, cpu) = -1;
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -0400146 return 0;
147}
148
149int __exit exit_tracecounters(void)
150{
151 unregister_cpu_notifier(&tracectr_cpu_hotplug_notifier_block);
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -0400152 return 0;
153}
154late_initcall(init_tracecounters);