diff options
author | Robert Richter <robert.richter@amd.com> | 2010-10-11 13:38:39 -0400 |
---|---|---|
committer | Robert Richter <robert.richter@amd.com> | 2010-10-11 13:38:39 -0400 |
commit | 0361e02342f60b64a7075755d5851ed4e6f98c7d (patch) | |
tree | 76ca78323ad1d4b1ecf1e8137f6b48eddcbebe3a /drivers/oprofile | |
parent | 4fdaa7b682b413dfb7ca9fa74ff45b1e0cb3dade (diff) | |
parent | e9677b3ce207a07fad5746b6f7ddc70cae79de0a (diff) |
Merge branch 'oprofile/perf' into oprofile/core
Conflicts:
arch/arm/oprofile/common.c
Signed-off-by: Robert Richter <robert.richter@amd.com>
Diffstat (limited to 'drivers/oprofile')
-rw-r--r-- | drivers/oprofile/oprofile_perf.c | 323 |
1 files changed, 323 insertions, 0 deletions
diff --git a/drivers/oprofile/oprofile_perf.c b/drivers/oprofile/oprofile_perf.c new file mode 100644 index 000000000000..b17235a24a4d --- /dev/null +++ b/drivers/oprofile/oprofile_perf.c | |||
@@ -0,0 +1,323 @@ | |||
1 | /* | ||
2 | * Copyright 2010 ARM Ltd. | ||
3 | * | ||
4 | * Perf-events backend for OProfile. | ||
5 | */ | ||
6 | #include <linux/perf_event.h> | ||
7 | #include <linux/oprofile.h> | ||
8 | #include <linux/slab.h> | ||
9 | |||
10 | /* | ||
11 | * Per performance monitor configuration as set via oprofilefs. | ||
12 | */ | ||
13 | struct op_counter_config { | ||
14 | unsigned long count; | ||
15 | unsigned long enabled; | ||
16 | unsigned long event; | ||
17 | unsigned long unit_mask; | ||
18 | unsigned long kernel; | ||
19 | unsigned long user; | ||
20 | struct perf_event_attr attr; | ||
21 | }; | ||
22 | |||
23 | static int oprofile_perf_enabled; | ||
24 | static DEFINE_MUTEX(oprofile_perf_mutex); | ||
25 | |||
26 | static struct op_counter_config *counter_config; | ||
27 | static struct perf_event **perf_events[nr_cpumask_bits]; | ||
28 | static int num_counters; | ||
29 | |||
30 | /* | ||
31 | * Overflow callback for oprofile. | ||
32 | */ | ||
33 | static void op_overflow_handler(struct perf_event *event, int unused, | ||
34 | struct perf_sample_data *data, struct pt_regs *regs) | ||
35 | { | ||
36 | int id; | ||
37 | u32 cpu = smp_processor_id(); | ||
38 | |||
39 | for (id = 0; id < num_counters; ++id) | ||
40 | if (perf_events[cpu][id] == event) | ||
41 | break; | ||
42 | |||
43 | if (id != num_counters) | ||
44 | oprofile_add_sample(regs, id); | ||
45 | else | ||
46 | pr_warning("oprofile: ignoring spurious overflow " | ||
47 | "on cpu %u\n", cpu); | ||
48 | } | ||
49 | |||
50 | /* | ||
51 | * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile | ||
52 | * settings in counter_config. Attributes are created as `pinned' events and | ||
53 | * so are permanently scheduled on the PMU. | ||
54 | */ | ||
55 | static void op_perf_setup(void) | ||
56 | { | ||
57 | int i; | ||
58 | u32 size = sizeof(struct perf_event_attr); | ||
59 | struct perf_event_attr *attr; | ||
60 | |||
61 | for (i = 0; i < num_counters; ++i) { | ||
62 | attr = &counter_config[i].attr; | ||
63 | memset(attr, 0, size); | ||
64 | attr->type = PERF_TYPE_RAW; | ||
65 | attr->size = size; | ||
66 | attr->config = counter_config[i].event; | ||
67 | attr->sample_period = counter_config[i].count; | ||
68 | attr->pinned = 1; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | static int op_create_counter(int cpu, int event) | ||
73 | { | ||
74 | struct perf_event *pevent; | ||
75 | |||
76 | if (!counter_config[event].enabled || perf_events[cpu][event]) | ||
77 | return 0; | ||
78 | |||
79 | pevent = perf_event_create_kernel_counter(&counter_config[event].attr, | ||
80 | cpu, -1, | ||
81 | op_overflow_handler); | ||
82 | |||
83 | if (IS_ERR(pevent)) | ||
84 | return PTR_ERR(pevent); | ||
85 | |||
86 | if (pevent->state != PERF_EVENT_STATE_ACTIVE) { | ||
87 | perf_event_release_kernel(pevent); | ||
88 | pr_warning("oprofile: failed to enable event %d " | ||
89 | "on CPU %d\n", event, cpu); | ||
90 | return -EBUSY; | ||
91 | } | ||
92 | |||
93 | perf_events[cpu][event] = pevent; | ||
94 | |||
95 | return 0; | ||
96 | } | ||
97 | |||
98 | static void op_destroy_counter(int cpu, int event) | ||
99 | { | ||
100 | struct perf_event *pevent = perf_events[cpu][event]; | ||
101 | |||
102 | if (pevent) { | ||
103 | perf_event_release_kernel(pevent); | ||
104 | perf_events[cpu][event] = NULL; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | /* | ||
109 | * Called by oprofile_perf_start to create active perf events based on the | ||
110 | * perviously configured attributes. | ||
111 | */ | ||
112 | static int op_perf_start(void) | ||
113 | { | ||
114 | int cpu, event, ret = 0; | ||
115 | |||
116 | for_each_online_cpu(cpu) { | ||
117 | for (event = 0; event < num_counters; ++event) { | ||
118 | ret = op_create_counter(cpu, event); | ||
119 | if (ret) | ||
120 | return ret; | ||
121 | } | ||
122 | } | ||
123 | |||
124 | return ret; | ||
125 | } | ||
126 | |||
127 | /* | ||
128 | * Called by oprofile_perf_stop at the end of a profiling run. | ||
129 | */ | ||
130 | static void op_perf_stop(void) | ||
131 | { | ||
132 | int cpu, event; | ||
133 | |||
134 | for_each_online_cpu(cpu) | ||
135 | for (event = 0; event < num_counters; ++event) | ||
136 | op_destroy_counter(cpu, event); | ||
137 | } | ||
138 | |||
139 | static int oprofile_perf_create_files(struct super_block *sb, struct dentry *root) | ||
140 | { | ||
141 | unsigned int i; | ||
142 | |||
143 | for (i = 0; i < num_counters; i++) { | ||
144 | struct dentry *dir; | ||
145 | char buf[4]; | ||
146 | |||
147 | snprintf(buf, sizeof buf, "%d", i); | ||
148 | dir = oprofilefs_mkdir(sb, root, buf); | ||
149 | oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled); | ||
150 | oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event); | ||
151 | oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count); | ||
152 | oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask); | ||
153 | oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel); | ||
154 | oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user); | ||
155 | } | ||
156 | |||
157 | return 0; | ||
158 | } | ||
159 | |||
160 | static int oprofile_perf_setup(void) | ||
161 | { | ||
162 | spin_lock(&oprofilefs_lock); | ||
163 | op_perf_setup(); | ||
164 | spin_unlock(&oprofilefs_lock); | ||
165 | return 0; | ||
166 | } | ||
167 | |||
168 | static int oprofile_perf_start(void) | ||
169 | { | ||
170 | int ret = -EBUSY; | ||
171 | |||
172 | mutex_lock(&oprofile_perf_mutex); | ||
173 | if (!oprofile_perf_enabled) { | ||
174 | ret = 0; | ||
175 | op_perf_start(); | ||
176 | oprofile_perf_enabled = 1; | ||
177 | } | ||
178 | mutex_unlock(&oprofile_perf_mutex); | ||
179 | return ret; | ||
180 | } | ||
181 | |||
182 | static void oprofile_perf_stop(void) | ||
183 | { | ||
184 | mutex_lock(&oprofile_perf_mutex); | ||
185 | if (oprofile_perf_enabled) | ||
186 | op_perf_stop(); | ||
187 | oprofile_perf_enabled = 0; | ||
188 | mutex_unlock(&oprofile_perf_mutex); | ||
189 | } | ||
190 | |||
191 | #ifdef CONFIG_PM | ||
192 | static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state) | ||
193 | { | ||
194 | mutex_lock(&oprofile_perf_mutex); | ||
195 | if (oprofile_perf_enabled) | ||
196 | op_perf_stop(); | ||
197 | mutex_unlock(&oprofile_perf_mutex); | ||
198 | return 0; | ||
199 | } | ||
200 | |||
201 | static int oprofile_perf_resume(struct platform_device *dev) | ||
202 | { | ||
203 | mutex_lock(&oprofile_perf_mutex); | ||
204 | if (oprofile_perf_enabled && op_perf_start()) | ||
205 | oprofile_perf_enabled = 0; | ||
206 | mutex_unlock(&oprofile_perf_mutex); | ||
207 | return 0; | ||
208 | } | ||
209 | |||
210 | static struct platform_driver oprofile_driver = { | ||
211 | .driver = { | ||
212 | .name = "oprofile-perf", | ||
213 | }, | ||
214 | .resume = oprofile_perf_resume, | ||
215 | .suspend = oprofile_perf_suspend, | ||
216 | }; | ||
217 | |||
218 | static struct platform_device *oprofile_pdev; | ||
219 | |||
220 | static int __init init_driverfs(void) | ||
221 | { | ||
222 | int ret; | ||
223 | |||
224 | ret = platform_driver_register(&oprofile_driver); | ||
225 | if (ret) | ||
226 | return ret; | ||
227 | |||
228 | oprofile_pdev = platform_device_register_simple( | ||
229 | oprofile_driver.driver.name, 0, NULL, 0); | ||
230 | if (IS_ERR(oprofile_pdev)) { | ||
231 | ret = PTR_ERR(oprofile_pdev); | ||
232 | platform_driver_unregister(&oprofile_driver); | ||
233 | } | ||
234 | |||
235 | return ret; | ||
236 | } | ||
237 | |||
238 | static void __exit exit_driverfs(void) | ||
239 | { | ||
240 | platform_device_unregister(oprofile_pdev); | ||
241 | platform_driver_unregister(&oprofile_driver); | ||
242 | } | ||
243 | #else | ||
244 | static int __init init_driverfs(void) { return 0; } | ||
245 | #define exit_driverfs() do { } while (0) | ||
246 | #endif /* CONFIG_PM */ | ||
247 | |||
248 | void oprofile_perf_exit(void) | ||
249 | { | ||
250 | int cpu, id; | ||
251 | struct perf_event *event; | ||
252 | |||
253 | for_each_possible_cpu(cpu) { | ||
254 | for (id = 0; id < num_counters; ++id) { | ||
255 | event = perf_events[cpu][id]; | ||
256 | if (event) | ||
257 | perf_event_release_kernel(event); | ||
258 | } | ||
259 | |||
260 | kfree(perf_events[cpu]); | ||
261 | } | ||
262 | |||
263 | kfree(counter_config); | ||
264 | exit_driverfs(); | ||
265 | } | ||
266 | |||
267 | int __init oprofile_perf_init(struct oprofile_operations *ops) | ||
268 | { | ||
269 | int cpu, ret = 0; | ||
270 | |||
271 | ret = init_driverfs(); | ||
272 | if (ret) | ||
273 | return ret; | ||
274 | |||
275 | memset(&perf_events, 0, sizeof(perf_events)); | ||
276 | |||
277 | num_counters = perf_num_counters(); | ||
278 | if (num_counters <= 0) { | ||
279 | pr_info("oprofile: no performance counters\n"); | ||
280 | ret = -ENODEV; | ||
281 | goto out; | ||
282 | } | ||
283 | |||
284 | counter_config = kcalloc(num_counters, | ||
285 | sizeof(struct op_counter_config), GFP_KERNEL); | ||
286 | |||
287 | if (!counter_config) { | ||
288 | pr_info("oprofile: failed to allocate %d " | ||
289 | "counters\n", num_counters); | ||
290 | ret = -ENOMEM; | ||
291 | num_counters = 0; | ||
292 | goto out; | ||
293 | } | ||
294 | |||
295 | for_each_possible_cpu(cpu) { | ||
296 | perf_events[cpu] = kcalloc(num_counters, | ||
297 | sizeof(struct perf_event *), GFP_KERNEL); | ||
298 | if (!perf_events[cpu]) { | ||
299 | pr_info("oprofile: failed to allocate %d perf events " | ||
300 | "for cpu %d\n", num_counters, cpu); | ||
301 | ret = -ENOMEM; | ||
302 | goto out; | ||
303 | } | ||
304 | } | ||
305 | |||
306 | ops->create_files = oprofile_perf_create_files; | ||
307 | ops->setup = oprofile_perf_setup; | ||
308 | ops->start = oprofile_perf_start; | ||
309 | ops->stop = oprofile_perf_stop; | ||
310 | ops->shutdown = oprofile_perf_stop; | ||
311 | ops->cpu_type = op_name_from_perf_id(); | ||
312 | |||
313 | if (!ops->cpu_type) | ||
314 | ret = -ENODEV; | ||
315 | else | ||
316 | pr_info("oprofile: using %s\n", ops->cpu_type); | ||
317 | |||
318 | out: | ||
319 | if (ret) | ||
320 | oprofile_perf_exit(); | ||
321 | |||
322 | return ret; | ||
323 | } | ||