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