]>
Commit | Line | Data |
---|---|---|
3d90a007 MF |
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 | int ret = 0; | |
75 | struct perf_event *pevent; | |
76 | ||
77 | if (!counter_config[event].enabled || (perf_events[cpu][event] != NULL)) | |
78 | return ret; | |
79 | ||
80 | pevent = perf_event_create_kernel_counter(&counter_config[event].attr, | |
81 | cpu, -1, | |
82 | op_overflow_handler); | |
83 | ||
84 | if (IS_ERR(pevent)) { | |
85 | ret = PTR_ERR(pevent); | |
86 | } else if (pevent->state != PERF_EVENT_STATE_ACTIVE) { | |
81771974 | 87 | perf_event_release_kernel(pevent); |
3d90a007 MF |
88 | pr_warning("oprofile: failed to enable event %d " |
89 | "on CPU %d\n", event, cpu); | |
90 | ret = -EBUSY; | |
91 | } else { | |
92 | perf_events[cpu][event] = pevent; | |
93 | } | |
94 | ||
95 | return ret; | |
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 | goto out; | |
121 | } | |
122 | } | |
123 | ||
124 | out: | |
125 | return ret; | |
126 | } | |
127 | ||
128 | /* | |
129 | * Called by oprofile_perf_stop at the end of a profiling run. | |
130 | */ | |
131 | static void op_perf_stop(void) | |
132 | { | |
133 | int cpu, event; | |
134 | ||
135 | for_each_online_cpu(cpu) | |
136 | for (event = 0; event < num_counters; ++event) | |
137 | op_destroy_counter(cpu, event); | |
138 | } | |
139 | ||
140 | static int oprofile_perf_create_files(struct super_block *sb, struct dentry *root) | |
141 | { | |
142 | unsigned int i; | |
143 | ||
144 | for (i = 0; i < num_counters; i++) { | |
145 | struct dentry *dir; | |
146 | char buf[4]; | |
147 | ||
148 | snprintf(buf, sizeof buf, "%d", i); | |
149 | dir = oprofilefs_mkdir(sb, root, buf); | |
150 | oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled); | |
151 | oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event); | |
152 | oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count); | |
153 | oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask); | |
154 | oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel); | |
155 | oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user); | |
156 | } | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
161 | static int oprofile_perf_setup(void) | |
162 | { | |
163 | spin_lock(&oprofilefs_lock); | |
164 | op_perf_setup(); | |
165 | spin_unlock(&oprofilefs_lock); | |
166 | return 0; | |
167 | } | |
168 | ||
169 | static int oprofile_perf_start(void) | |
170 | { | |
171 | int ret = -EBUSY; | |
172 | ||
173 | mutex_lock(&oprofile_perf_mutex); | |
174 | if (!oprofile_perf_enabled) { | |
175 | ret = 0; | |
176 | op_perf_start(); | |
177 | oprofile_perf_enabled = 1; | |
178 | } | |
179 | mutex_unlock(&oprofile_perf_mutex); | |
180 | return ret; | |
181 | } | |
182 | ||
183 | static void oprofile_perf_stop(void) | |
184 | { | |
185 | mutex_lock(&oprofile_perf_mutex); | |
186 | if (oprofile_perf_enabled) | |
187 | op_perf_stop(); | |
188 | oprofile_perf_enabled = 0; | |
189 | mutex_unlock(&oprofile_perf_mutex); | |
190 | } | |
191 | ||
192 | #ifdef CONFIG_PM | |
193 | static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state) | |
194 | { | |
195 | mutex_lock(&oprofile_perf_mutex); | |
196 | if (oprofile_perf_enabled) | |
197 | op_perf_stop(); | |
198 | mutex_unlock(&oprofile_perf_mutex); | |
199 | return 0; | |
200 | } | |
201 | ||
202 | static int oprofile_perf_resume(struct platform_device *dev) | |
203 | { | |
204 | mutex_lock(&oprofile_perf_mutex); | |
205 | if (oprofile_perf_enabled && op_perf_start()) | |
206 | oprofile_perf_enabled = 0; | |
207 | mutex_unlock(&oprofile_perf_mutex); | |
208 | return 0; | |
209 | } | |
210 | ||
211 | static struct platform_driver oprofile_driver = { | |
212 | .driver = { | |
213 | .name = "oprofile-perf", | |
214 | }, | |
215 | .resume = oprofile_perf_resume, | |
216 | .suspend = oprofile_perf_suspend, | |
217 | }; | |
218 | ||
219 | static struct platform_device *oprofile_pdev; | |
220 | ||
221 | static int __init init_driverfs(void) | |
222 | { | |
223 | int ret; | |
224 | ||
225 | ret = platform_driver_register(&oprofile_driver); | |
226 | if (ret) | |
227 | goto out; | |
228 | ||
229 | oprofile_pdev = platform_device_register_simple( | |
230 | oprofile_driver.driver.name, 0, NULL, 0); | |
231 | if (IS_ERR(oprofile_pdev)) { | |
232 | ret = PTR_ERR(oprofile_pdev); | |
233 | platform_driver_unregister(&oprofile_driver); | |
234 | } | |
235 | ||
236 | out: | |
237 | return ret; | |
238 | } | |
239 | ||
240 | static void __exit exit_driverfs(void) | |
241 | { | |
242 | platform_device_unregister(oprofile_pdev); | |
243 | platform_driver_unregister(&oprofile_driver); | |
244 | } | |
245 | #else | |
246 | static int __init init_driverfs(void) { return 0; } | |
247 | #define exit_driverfs() do { } while (0) | |
248 | #endif /* CONFIG_PM */ | |
249 | ||
250 | int __init oprofile_perf_init(struct oprofile_operations *ops) | |
251 | { | |
252 | int cpu, ret = 0; | |
253 | ||
254 | memset(&perf_events, 0, sizeof(perf_events)); | |
255 | ||
256 | num_counters = perf_num_counters(); | |
257 | if (num_counters <= 0) { | |
258 | pr_info("oprofile: no performance counters\n"); | |
259 | ret = -ENODEV; | |
260 | goto out; | |
261 | } | |
262 | ||
263 | counter_config = kcalloc(num_counters, | |
264 | sizeof(struct op_counter_config), GFP_KERNEL); | |
265 | ||
266 | if (!counter_config) { | |
267 | pr_info("oprofile: failed to allocate %d " | |
268 | "counters\n", num_counters); | |
269 | ret = -ENOMEM; | |
270 | goto out; | |
271 | } | |
272 | ||
273 | ret = init_driverfs(); | |
274 | if (ret) | |
275 | goto out; | |
276 | ||
277 | for_each_possible_cpu(cpu) { | |
278 | perf_events[cpu] = kcalloc(num_counters, | |
279 | sizeof(struct perf_event *), GFP_KERNEL); | |
280 | if (!perf_events[cpu]) { | |
281 | pr_info("oprofile: failed to allocate %d perf events " | |
282 | "for cpu %d\n", num_counters, cpu); | |
283 | ret = -ENOMEM; | |
284 | goto out; | |
285 | } | |
286 | } | |
287 | ||
288 | ops->create_files = oprofile_perf_create_files; | |
289 | ops->setup = oprofile_perf_setup; | |
290 | ops->start = oprofile_perf_start; | |
291 | ops->stop = oprofile_perf_stop; | |
292 | ops->shutdown = oprofile_perf_stop; | |
293 | ops->cpu_type = op_name_from_perf_id(); | |
294 | ||
295 | if (!ops->cpu_type) | |
296 | ret = -ENODEV; | |
297 | else | |
298 | pr_info("oprofile: using %s\n", ops->cpu_type); | |
299 | ||
300 | out: | |
301 | if (ret) { | |
302 | for_each_possible_cpu(cpu) | |
303 | kfree(perf_events[cpu]); | |
304 | kfree(counter_config); | |
305 | } | |
306 | ||
307 | return ret; | |
308 | } | |
309 | ||
310 | void __exit oprofile_perf_exit(void) | |
311 | { | |
312 | int cpu, id; | |
313 | struct perf_event *event; | |
314 | ||
315 | for_each_possible_cpu(cpu) { | |
316 | for (id = 0; id < num_counters; ++id) { | |
317 | event = perf_events[cpu][id]; | |
318 | if (event) | |
319 | perf_event_release_kernel(event); | |
320 | } | |
321 | ||
322 | kfree(perf_events[cpu]); | |
323 | } | |
324 | ||
325 | kfree(counter_config); | |
326 | exit_driverfs(); | |
327 | } |