blob: 0b6a75e4b678a8d4e917d2a5f3ec709df18b8a23 [file] [log] [blame]
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -04001/* 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 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);
23/* Reset per_cpu variables that store counter values uppn CPU hotplug */
24static 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
41static struct notifier_block tracectr_cpu_hotplug_notifier_block = {
42 .notifier_call = tracectr_cpu_hotplug_notifier,
43};
44
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -040045static 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
62static struct notifier_block tracectr_notifier_block = {
63 .notifier_call = tracectr_notifier,
64};
65
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -040066static 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
74static 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
82static 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
94static 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
119static 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 Chaugule4eaee1a2013-03-14 18:37:49 -0400125int __init init_tracecounters(void)
126{
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -0400127 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 Sahasrabudhebf3e69b2013-08-12 13:53:59 -0400134 file = debugfs_create_file("enabled", 0660, dir,
Sheetal Sahasrabudhe711300e2013-07-10 12:18:59 -0400135 &value, &fops_perftp);
136 if (!file) {
137 debugfs_remove(dir);
138 return -ENOMEM;
139 }
Sheetal Sahasrabudhee2ec9802013-10-15 14:48:52 -0400140 register_cpu_notifier(&tracectr_cpu_hotplug_notifier_block);
141 return 0;
142}
143
144int __exit exit_tracecounters(void)
145{
146 unregister_cpu_notifier(&tracectr_cpu_hotplug_notifier_block);
Ashwin Chaugule4eaee1a2013-03-14 18:37:49 -0400147 return 0;
148}
149late_initcall(init_tracecounters);