blob: 3b7a1b055122a698c0f97a05eae8cbe6a229bb6f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/profile.c
3 * Simple profiling. Manages a direct-mapped profile hit count buffer,
4 * with configurable resolution, support for restricting the cpus on
5 * which profiling is done, and switching between cpu time and
6 * schedule() calls via kernel command line parameters passed at boot.
7 *
8 * Scheduler profiling support, Arjan van de Ven and Ingo Molnar,
9 * Red Hat, July 2004
10 * Consolidation of architecture support code for profiling,
11 * William Irwin, Oracle, July 2004
12 * Amortized hit count accounting via per-cpu open-addressed hashtables
13 * to resolve timer interrupt livelocks, William Irwin, Oracle, 2004
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/profile.h>
18#include <linux/bootmem.h>
19#include <linux/notifier.h>
20#include <linux/mm.h>
21#include <linux/cpumask.h>
22#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/highmem.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/sections.h>
26#include <asm/semaphore.h>
David Howells7d12e782006-10-05 14:55:46 +010027#include <asm/irq_regs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30struct profile_hit {
31 u32 pc, hits;
32};
33#define PROFILE_GRPSHIFT 3
34#define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
35#define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
36#define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
37
38/* Oprofile timer tick hook */
Adrian Bunkb012d342007-10-16 23:29:26 -070039static int (*timer_hook)(struct pt_regs *) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static atomic_t *prof_buffer;
42static unsigned long prof_len, prof_shift;
Ingo Molnar07031e12007-01-10 23:15:38 -080043
Ingo Molnarece8a682006-12-06 20:37:24 -080044int prof_on __read_mostly;
Ingo Molnar07031e12007-01-10 23:15:38 -080045EXPORT_SYMBOL_GPL(prof_on);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
48#ifdef CONFIG_SMP
49static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
50static DEFINE_PER_CPU(int, cpu_profile_flip);
Arjan van de Ven97d1f152006-03-23 03:00:24 -080051static DEFINE_MUTEX(profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif /* CONFIG_SMP */
53
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +010054static int __init profile_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070056 static char __initdata schedstr[] = "schedule";
Ingo Molnarece8a682006-12-06 20:37:24 -080057 static char __initdata sleepstr[] = "sleep";
Ingo Molnar07031e12007-01-10 23:15:38 -080058 static char __initdata kvmstr[] = "kvm";
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int par;
60
Ingo Molnarece8a682006-12-06 20:37:24 -080061 if (!strncmp(str, sleepstr, strlen(sleepstr))) {
Mel Gormanb3da2a72007-10-24 18:23:50 +020062#ifdef CONFIG_SCHEDSTATS
Ingo Molnarece8a682006-12-06 20:37:24 -080063 prof_on = SLEEP_PROFILING;
64 if (str[strlen(sleepstr)] == ',')
65 str += strlen(sleepstr) + 1;
66 if (get_option(&str, &par))
67 prof_shift = par;
68 printk(KERN_INFO
69 "kernel sleep profiling enabled (shift: %ld)\n",
70 prof_shift);
Mel Gormanb3da2a72007-10-24 18:23:50 +020071#else
72 printk(KERN_WARNING
73 "kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
74#endif /* CONFIG_SCHEDSTATS */
Ingo Molnara75acf82007-01-05 16:36:29 -080075 } else if (!strncmp(str, schedstr, strlen(schedstr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 prof_on = SCHED_PROFILING;
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070077 if (str[strlen(schedstr)] == ',')
78 str += strlen(schedstr) + 1;
79 if (get_option(&str, &par))
80 prof_shift = par;
81 printk(KERN_INFO
82 "kernel schedule profiling enabled (shift: %ld)\n",
83 prof_shift);
Ingo Molnar07031e12007-01-10 23:15:38 -080084 } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
85 prof_on = KVM_PROFILING;
86 if (str[strlen(kvmstr)] == ',')
87 str += strlen(kvmstr) + 1;
88 if (get_option(&str, &par))
89 prof_shift = par;
90 printk(KERN_INFO
91 "kernel KVM profiling enabled (shift: %ld)\n",
92 prof_shift);
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070093 } else if (get_option(&str, &par)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 prof_shift = par;
95 prof_on = CPU_PROFILING;
96 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
97 prof_shift);
98 }
99 return 1;
100}
101__setup("profile=", profile_setup);
102
103
104void __init profile_init(void)
105{
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100106 if (!prof_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return;
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* only text is profiled */
110 prof_len = (_etext - _stext) >> prof_shift;
111 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
112}
113
114/* Profile event notifications */
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#ifdef CONFIG_PROFILING
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100117
Alan Sterne041c682006-03-27 01:16:30 -0800118static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
119static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
120static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100121
122void profile_task_exit(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Alan Sterne041c682006-03-27 01:16:30 -0800124 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100126
127int profile_handoff_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 int ret;
Alan Sterne041c682006-03-27 01:16:30 -0800130 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return (ret == NOTIFY_OK) ? 1 : 0;
132}
133
134void profile_munmap(unsigned long addr)
135{
Alan Sterne041c682006-03-27 01:16:30 -0800136 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100139int task_handoff_register(struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Alan Sterne041c682006-03-27 01:16:30 -0800141 return atomic_notifier_chain_register(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100143EXPORT_SYMBOL_GPL(task_handoff_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100145int task_handoff_unregister(struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Alan Sterne041c682006-03-27 01:16:30 -0800147 return atomic_notifier_chain_unregister(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100149EXPORT_SYMBOL_GPL(task_handoff_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100151int profile_event_register(enum profile_type type, struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 switch (type) {
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100156 case PROFILE_TASK_EXIT:
157 err = blocking_notifier_chain_register(
158 &task_exit_notifier, n);
159 break;
160 case PROFILE_MUNMAP:
161 err = blocking_notifier_chain_register(
162 &munmap_notifier, n);
163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return err;
167}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100168EXPORT_SYMBOL_GPL(profile_event_register);
169
170int profile_event_unregister(enum profile_type type, struct notifier_block *n)
171{
172 int err = -EINVAL;
173
174 switch (type) {
175 case PROFILE_TASK_EXIT:
176 err = blocking_notifier_chain_unregister(
177 &task_exit_notifier, n);
178 break;
179 case PROFILE_MUNMAP:
180 err = blocking_notifier_chain_unregister(
181 &munmap_notifier, n);
182 break;
183 }
184
185 return err;
186}
187EXPORT_SYMBOL_GPL(profile_event_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189int register_timer_hook(int (*hook)(struct pt_regs *))
190{
191 if (timer_hook)
192 return -EBUSY;
193 timer_hook = hook;
194 return 0;
195}
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100196EXPORT_SYMBOL_GPL(register_timer_hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198void unregister_timer_hook(int (*hook)(struct pt_regs *))
199{
200 WARN_ON(hook != timer_hook);
201 timer_hook = NULL;
202 /* make sure all CPUs see the NULL hook */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700203 synchronize_sched(); /* Allow ongoing interrupts to complete. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205EXPORT_SYMBOL_GPL(unregister_timer_hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207#endif /* CONFIG_PROFILING */
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210#ifdef CONFIG_SMP
211/*
212 * Each cpu has a pair of open-addressed hashtables for pending
213 * profile hits. read_profile() IPI's all cpus to request them
214 * to flip buffers and flushes their contents to prof_buffer itself.
215 * Flip requests are serialized by the profile_flip_mutex. The sole
216 * use of having a second hashtable is for avoiding cacheline
217 * contention that would otherwise happen during flushes of pending
218 * profile hits required for the accuracy of reported profile hits
219 * and so resurrect the interrupt livelock issue.
220 *
221 * The open-addressed hashtables are indexed by profile buffer slot
222 * and hold the number of pending hits to that profile buffer slot on
223 * a cpu in an entry. When the hashtable overflows, all pending hits
224 * are accounted to their corresponding profile buffer slots with
225 * atomic_add() and the hashtable emptied. As numerous pending hits
226 * may be accounted to a profile buffer slot in a hashtable entry,
227 * this amortizes a number of atomic profile buffer increments likely
228 * to be far larger than the number of entries in the hashtable,
229 * particularly given that the number of distinct profile buffer
230 * positions to which hits are accounted during short intervals (e.g.
231 * several seconds) is usually very small. Exclusion from buffer
232 * flipping is provided by interrupt disablement (note that for
Ingo Molnarece8a682006-12-06 20:37:24 -0800233 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
234 * process context).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * The hash function is meant to be lightweight as opposed to strong,
236 * and was vaguely inspired by ppc64 firmware-supported inverted
237 * pagetable hash functions, but uses a full hashtable full of finite
238 * collision chains, not just pairs of them.
239 *
240 * -- wli
241 */
242static void __profile_flip_buffers(void *unused)
243{
244 int cpu = smp_processor_id();
245
246 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
247}
248
249static void profile_flip_buffers(void)
250{
251 int i, j, cpu;
252
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800253 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 j = per_cpu(cpu_profile_flip, get_cpu());
255 put_cpu();
256 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
257 for_each_online_cpu(cpu) {
258 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
259 for (i = 0; i < NR_PROFILE_HIT; ++i) {
260 if (!hits[i].hits) {
261 if (hits[i].pc)
262 hits[i].pc = 0;
263 continue;
264 }
265 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
266 hits[i].hits = hits[i].pc = 0;
267 }
268 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800269 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272static void profile_discard_flip_buffers(void)
273{
274 int i, cpu;
275
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800276 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 i = per_cpu(cpu_profile_flip, get_cpu());
278 put_cpu();
279 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
280 for_each_online_cpu(cpu) {
281 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
282 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
283 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800284 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Ingo Molnarece8a682006-12-06 20:37:24 -0800287void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
290 int i, j, cpu;
291 struct profile_hit *hits;
292
293 if (prof_on != type || !prof_buffer)
294 return;
295 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
296 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
297 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
298 cpu = get_cpu();
299 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
300 if (!hits) {
301 put_cpu();
302 return;
303 }
Ingo Molnarece8a682006-12-06 20:37:24 -0800304 /*
305 * We buffer the global profiler buffer into a per-CPU
306 * queue and thus reduce the number of global (and possibly
307 * NUMA-alien) accesses. The write-queue is self-coalescing:
308 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 local_irq_save(flags);
310 do {
311 for (j = 0; j < PROFILE_GRPSZ; ++j) {
312 if (hits[i + j].pc == pc) {
Ingo Molnarece8a682006-12-06 20:37:24 -0800313 hits[i + j].hits += nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto out;
315 } else if (!hits[i + j].hits) {
316 hits[i + j].pc = pc;
Ingo Molnarece8a682006-12-06 20:37:24 -0800317 hits[i + j].hits = nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto out;
319 }
320 }
321 i = (i + secondary) & (NR_PROFILE_HIT - 1);
322 } while (i != primary);
Ingo Molnarece8a682006-12-06 20:37:24 -0800323
324 /*
325 * Add the current hit(s) and flush the write-queue out
326 * to the global buffer:
327 */
328 atomic_add(nr_hits, &prof_buffer[pc]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 for (i = 0; i < NR_PROFILE_HIT; ++i) {
330 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
331 hits[i].pc = hits[i].hits = 0;
332 }
333out:
334 local_irq_restore(flags);
335 put_cpu();
336}
337
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700338static int __devinit profile_cpu_callback(struct notifier_block *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 unsigned long action, void *__cpu)
340{
341 int node, cpu = (unsigned long)__cpu;
342 struct page *page;
343
344 switch (action) {
345 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700346 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 node = cpu_to_node(cpu);
348 per_cpu(cpu_profile_flip, cpu) = 0;
349 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700350 page = alloc_pages_node(node,
Christoph Lameter4199cfa2007-10-16 01:25:34 -0700351 GFP_KERNEL | __GFP_ZERO,
Christoph Lameterfbd98162006-09-25 23:31:45 -0700352 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!page)
354 return NOTIFY_BAD;
355 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
356 }
357 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700358 page = alloc_pages_node(node,
Christoph Lameter4199cfa2007-10-16 01:25:34 -0700359 GFP_KERNEL | __GFP_ZERO,
Christoph Lameterfbd98162006-09-25 23:31:45 -0700360 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!page)
362 goto out_free;
363 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
364 }
365 break;
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100366out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
368 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
369 __free_page(page);
370 return NOTIFY_BAD;
371 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700372 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 cpu_set(cpu, prof_cpu_mask);
374 break;
375 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700376 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700378 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 cpu_clear(cpu, prof_cpu_mask);
380 if (per_cpu(cpu_profile_hits, cpu)[0]) {
381 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
382 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
383 __free_page(page);
384 }
385 if (per_cpu(cpu_profile_hits, cpu)[1]) {
386 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
387 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
388 __free_page(page);
389 }
390 break;
391 }
392 return NOTIFY_OK;
393}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#else /* !CONFIG_SMP */
395#define profile_flip_buffers() do { } while (0)
396#define profile_discard_flip_buffers() do { } while (0)
Ingo Molnar023160672006-12-06 20:38:17 -0800397#define profile_cpu_callback NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ingo Molnarece8a682006-12-06 20:37:24 -0800399void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 unsigned long pc;
402
403 if (prof_on != type || !prof_buffer)
404 return;
405 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
Ingo Molnarece8a682006-12-06 20:37:24 -0800406 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408#endif /* !CONFIG_SMP */
Andrew Mortonbbe1a59b2007-01-22 20:40:33 -0800409EXPORT_SYMBOL_GPL(profile_hits);
410
David Howells7d12e782006-10-05 14:55:46 +0100411void profile_tick(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
David Howells7d12e782006-10-05 14:55:46 +0100413 struct pt_regs *regs = get_irq_regs();
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (type == CPU_PROFILING && timer_hook)
416 timer_hook(regs);
417 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
418 profile_hit(type, (void *)profile_pc(regs));
419}
420
421#ifdef CONFIG_PROC_FS
422#include <linux/proc_fs.h>
423#include <asm/uaccess.h>
424#include <asm/ptrace.h>
425
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100426static int prof_cpu_mask_read_proc(char *page, char **start, off_t off,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int count, int *eof, void *data)
428{
429 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
430 if (count - len < 2)
431 return -EINVAL;
432 len += sprintf(page + len, "\n");
433 return len;
434}
435
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100436static int prof_cpu_mask_write_proc(struct file *file,
437 const char __user *buffer, unsigned long count, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 cpumask_t *mask = (cpumask_t *)data;
440 unsigned long full_count = count, err;
441 cpumask_t new_value;
442
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700443 err = cpumask_parse_user(buffer, count, new_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (err)
445 return err;
446
447 *mask = new_value;
448 return full_count;
449}
450
451void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
452{
453 struct proc_dir_entry *entry;
454
455 /* create /proc/irq/prof_cpu_mask */
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100456 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
457 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 entry->data = (void *)&prof_cpu_mask;
460 entry->read_proc = prof_cpu_mask_read_proc;
461 entry->write_proc = prof_cpu_mask_write_proc;
462}
463
464/*
465 * This function accesses profiling information. The returned data is
466 * binary: the sampling step and the actual contents of the profile
467 * buffer. Use of the program readprofile is recommended in order to
468 * get meaningful info out of these data.
469 */
470static ssize_t
471read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
472{
473 unsigned long p = *ppos;
474 ssize_t read;
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100475 char *pnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 unsigned int sample_step = 1 << prof_shift;
477
478 profile_flip_buffers();
479 if (p >= (prof_len+1)*sizeof(unsigned int))
480 return 0;
481 if (count > (prof_len+1)*sizeof(unsigned int) - p)
482 count = (prof_len+1)*sizeof(unsigned int) - p;
483 read = 0;
484
485 while (p < sizeof(unsigned int) && count > 0) {
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100486 if (put_user(*((char *)(&sample_step)+p), buf))
Heiko Carstens064b0222006-12-06 20:36:37 -0800487 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 buf++; p++; count--; read++;
489 }
490 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100491 if (copy_to_user(buf, (void *)pnt, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EFAULT;
493 read += count;
494 *ppos += read;
495 return read;
496}
497
498/*
499 * Writing to /proc/profile resets the counters
500 *
501 * Writing a 'profiling multiplier' value into it also re-sets the profiling
502 * interrupt frequency, on architectures that support this.
503 */
504static ssize_t write_profile(struct file *file, const char __user *buf,
505 size_t count, loff_t *ppos)
506{
507#ifdef CONFIG_SMP
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100508 extern int setup_profiling_timer(unsigned int multiplier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (count == sizeof(int)) {
511 unsigned int multiplier;
512
513 if (copy_from_user(&multiplier, buf, sizeof(int)))
514 return -EFAULT;
515
516 if (setup_profiling_timer(multiplier))
517 return -EINVAL;
518 }
519#endif
520 profile_discard_flip_buffers();
521 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
522 return count;
523}
524
Helge Deller15ad7cd2006-12-06 20:40:36 -0800525static const struct file_operations proc_profile_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 .read = read_profile,
527 .write = write_profile,
528};
529
530#ifdef CONFIG_SMP
531static void __init profile_nop(void *unused)
532{
533}
534
535static int __init create_hash_tables(void)
536{
537 int cpu;
538
539 for_each_online_cpu(cpu) {
540 int node = cpu_to_node(cpu);
541 struct page *page;
542
Christoph Lameterfbd98162006-09-25 23:31:45 -0700543 page = alloc_pages_node(node,
544 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
545 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!page)
547 goto out_cleanup;
548 per_cpu(cpu_profile_hits, cpu)[1]
549 = (struct profile_hit *)page_address(page);
Christoph Lameterfbd98162006-09-25 23:31:45 -0700550 page = alloc_pages_node(node,
551 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
552 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!page)
554 goto out_cleanup;
555 per_cpu(cpu_profile_hits, cpu)[0]
556 = (struct profile_hit *)page_address(page);
557 }
558 return 0;
559out_cleanup:
560 prof_on = 0;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700561 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 on_each_cpu(profile_nop, NULL, 0, 1);
563 for_each_online_cpu(cpu) {
564 struct page *page;
565
566 if (per_cpu(cpu_profile_hits, cpu)[0]) {
567 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
568 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
569 __free_page(page);
570 }
571 if (per_cpu(cpu_profile_hits, cpu)[1]) {
572 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
573 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
574 __free_page(page);
575 }
576 }
577 return -1;
578}
579#else
580#define create_hash_tables() ({ 0; })
581#endif
582
583static int __init create_proc_profile(void)
584{
585 struct proc_dir_entry *entry;
586
587 if (!prof_on)
588 return 0;
589 if (create_hash_tables())
590 return -1;
Paolo Ciarrocchi1ad82fd2008-01-25 21:08:33 +0100591 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
592 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return 0;
594 entry->proc_fops = &proc_profile_operations;
595 entry->size = (1+prof_len) * sizeof(atomic_t);
596 hotcpu_notifier(profile_cpu_callback, 0);
597 return 0;
598}
599module_init(create_proc_profile);
600#endif /* CONFIG_PROC_FS */