blob: a6574a18514e9686baf9d6b77f7a6cd3f6c57ca1 [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>
23#include <linux/profile.h>
24#include <linux/highmem.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/sections.h>
27#include <asm/semaphore.h>
David Howells7d12e782006-10-05 14:55:46 +010028#include <asm/irq_regs.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 */
Christoph Lameter6c036522005-07-07 17:56:59 -070039int (*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
54static int __init profile_setup(char * str)
55{
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))) {
62 prof_on = SLEEP_PROFILING;
63 if (str[strlen(sleepstr)] == ',')
64 str += strlen(sleepstr) + 1;
65 if (get_option(&str, &par))
66 prof_shift = par;
67 printk(KERN_INFO
68 "kernel sleep profiling enabled (shift: %ld)\n",
69 prof_shift);
Ingo Molnara75acf82007-01-05 16:36:29 -080070 } else if (!strncmp(str, schedstr, strlen(schedstr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 prof_on = SCHED_PROFILING;
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070072 if (str[strlen(schedstr)] == ',')
73 str += strlen(schedstr) + 1;
74 if (get_option(&str, &par))
75 prof_shift = par;
76 printk(KERN_INFO
77 "kernel schedule profiling enabled (shift: %ld)\n",
78 prof_shift);
Ingo Molnar07031e12007-01-10 23:15:38 -080079 } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
80 prof_on = KVM_PROFILING;
81 if (str[strlen(kvmstr)] == ',')
82 str += strlen(kvmstr) + 1;
83 if (get_option(&str, &par))
84 prof_shift = par;
85 printk(KERN_INFO
86 "kernel KVM profiling enabled (shift: %ld)\n",
87 prof_shift);
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070088 } else if (get_option(&str, &par)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 prof_shift = par;
90 prof_on = CPU_PROFILING;
91 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
92 prof_shift);
93 }
94 return 1;
95}
96__setup("profile=", profile_setup);
97
98
99void __init profile_init(void)
100{
101 if (!prof_on)
102 return;
103
104 /* only text is profiled */
105 prof_len = (_etext - _stext) >> prof_shift;
106 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
107}
108
109/* Profile event notifications */
110
111#ifdef CONFIG_PROFILING
112
Alan Sterne041c682006-03-27 01:16:30 -0800113static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
114static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
115static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117void profile_task_exit(struct task_struct * task)
118{
Alan Sterne041c682006-03-27 01:16:30 -0800119 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122int profile_handoff_task(struct task_struct * task)
123{
124 int ret;
Alan Sterne041c682006-03-27 01:16:30 -0800125 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return (ret == NOTIFY_OK) ? 1 : 0;
127}
128
129void profile_munmap(unsigned long addr)
130{
Alan Sterne041c682006-03-27 01:16:30 -0800131 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134int task_handoff_register(struct notifier_block * n)
135{
Alan Sterne041c682006-03-27 01:16:30 -0800136 return atomic_notifier_chain_register(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139int task_handoff_unregister(struct notifier_block * n)
140{
Alan Sterne041c682006-03-27 01:16:30 -0800141 return atomic_notifier_chain_unregister(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144int profile_event_register(enum profile_type type, struct notifier_block * n)
145{
146 int err = -EINVAL;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 switch (type) {
149 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800150 err = blocking_notifier_chain_register(
151 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 break;
153 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800154 err = blocking_notifier_chain_register(
155 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
157 }
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return err;
160}
161
162
163int profile_event_unregister(enum profile_type type, struct notifier_block * n)
164{
165 int err = -EINVAL;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 switch (type) {
168 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800169 err = blocking_notifier_chain_unregister(
170 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
172 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800173 err = blocking_notifier_chain_unregister(
174 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 break;
176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return err;
179}
180
181int register_timer_hook(int (*hook)(struct pt_regs *))
182{
183 if (timer_hook)
184 return -EBUSY;
185 timer_hook = hook;
186 return 0;
187}
188
189void unregister_timer_hook(int (*hook)(struct pt_regs *))
190{
191 WARN_ON(hook != timer_hook);
192 timer_hook = NULL;
193 /* make sure all CPUs see the NULL hook */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700194 synchronize_sched(); /* Allow ongoing interrupts to complete. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197EXPORT_SYMBOL_GPL(register_timer_hook);
198EXPORT_SYMBOL_GPL(unregister_timer_hook);
199EXPORT_SYMBOL_GPL(task_handoff_register);
200EXPORT_SYMBOL_GPL(task_handoff_unregister);
201
202#endif /* CONFIG_PROFILING */
203
204EXPORT_SYMBOL_GPL(profile_event_register);
205EXPORT_SYMBOL_GPL(profile_event_unregister);
206
207#ifdef CONFIG_SMP
208/*
209 * Each cpu has a pair of open-addressed hashtables for pending
210 * profile hits. read_profile() IPI's all cpus to request them
211 * to flip buffers and flushes their contents to prof_buffer itself.
212 * Flip requests are serialized by the profile_flip_mutex. The sole
213 * use of having a second hashtable is for avoiding cacheline
214 * contention that would otherwise happen during flushes of pending
215 * profile hits required for the accuracy of reported profile hits
216 * and so resurrect the interrupt livelock issue.
217 *
218 * The open-addressed hashtables are indexed by profile buffer slot
219 * and hold the number of pending hits to that profile buffer slot on
220 * a cpu in an entry. When the hashtable overflows, all pending hits
221 * are accounted to their corresponding profile buffer slots with
222 * atomic_add() and the hashtable emptied. As numerous pending hits
223 * may be accounted to a profile buffer slot in a hashtable entry,
224 * this amortizes a number of atomic profile buffer increments likely
225 * to be far larger than the number of entries in the hashtable,
226 * particularly given that the number of distinct profile buffer
227 * positions to which hits are accounted during short intervals (e.g.
228 * several seconds) is usually very small. Exclusion from buffer
229 * flipping is provided by interrupt disablement (note that for
Ingo Molnarece8a682006-12-06 20:37:24 -0800230 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
231 * process context).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * The hash function is meant to be lightweight as opposed to strong,
233 * and was vaguely inspired by ppc64 firmware-supported inverted
234 * pagetable hash functions, but uses a full hashtable full of finite
235 * collision chains, not just pairs of them.
236 *
237 * -- wli
238 */
239static void __profile_flip_buffers(void *unused)
240{
241 int cpu = smp_processor_id();
242
243 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
244}
245
246static void profile_flip_buffers(void)
247{
248 int i, j, cpu;
249
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800250 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 j = per_cpu(cpu_profile_flip, get_cpu());
252 put_cpu();
253 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
254 for_each_online_cpu(cpu) {
255 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
256 for (i = 0; i < NR_PROFILE_HIT; ++i) {
257 if (!hits[i].hits) {
258 if (hits[i].pc)
259 hits[i].pc = 0;
260 continue;
261 }
262 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
263 hits[i].hits = hits[i].pc = 0;
264 }
265 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800266 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static void profile_discard_flip_buffers(void)
270{
271 int i, cpu;
272
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800273 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 i = per_cpu(cpu_profile_flip, get_cpu());
275 put_cpu();
276 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
277 for_each_online_cpu(cpu) {
278 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
279 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
280 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800281 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Ingo Molnarece8a682006-12-06 20:37:24 -0800284void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
287 int i, j, cpu;
288 struct profile_hit *hits;
289
290 if (prof_on != type || !prof_buffer)
291 return;
292 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
293 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
294 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
295 cpu = get_cpu();
296 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
297 if (!hits) {
298 put_cpu();
299 return;
300 }
Ingo Molnarece8a682006-12-06 20:37:24 -0800301 /*
302 * We buffer the global profiler buffer into a per-CPU
303 * queue and thus reduce the number of global (and possibly
304 * NUMA-alien) accesses. The write-queue is self-coalescing:
305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 local_irq_save(flags);
307 do {
308 for (j = 0; j < PROFILE_GRPSZ; ++j) {
309 if (hits[i + j].pc == pc) {
Ingo Molnarece8a682006-12-06 20:37:24 -0800310 hits[i + j].hits += nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto out;
312 } else if (!hits[i + j].hits) {
313 hits[i + j].pc = pc;
Ingo Molnarece8a682006-12-06 20:37:24 -0800314 hits[i + j].hits = nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto out;
316 }
317 }
318 i = (i + secondary) & (NR_PROFILE_HIT - 1);
319 } while (i != primary);
Ingo Molnarece8a682006-12-06 20:37:24 -0800320
321 /*
322 * Add the current hit(s) and flush the write-queue out
323 * to the global buffer:
324 */
325 atomic_add(nr_hits, &prof_buffer[pc]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 for (i = 0; i < NR_PROFILE_HIT; ++i) {
327 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
328 hits[i].pc = hits[i].hits = 0;
329 }
330out:
331 local_irq_restore(flags);
332 put_cpu();
333}
Ingo Molnar07031e12007-01-10 23:15:38 -0800334EXPORT_SYMBOL_GPL(profile_hits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700336static int __devinit profile_cpu_callback(struct notifier_block *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned long action, void *__cpu)
338{
339 int node, cpu = (unsigned long)__cpu;
340 struct page *page;
341
342 switch (action) {
343 case CPU_UP_PREPARE:
344 node = cpu_to_node(cpu);
345 per_cpu(cpu_profile_flip, cpu) = 0;
346 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700347 page = alloc_pages_node(node,
348 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
349 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!page)
351 return NOTIFY_BAD;
352 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
353 }
354 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700355 page = alloc_pages_node(node,
356 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
357 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!page)
359 goto out_free;
360 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
361 }
362 break;
363 out_free:
364 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
365 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
366 __free_page(page);
367 return NOTIFY_BAD;
368 case CPU_ONLINE:
369 cpu_set(cpu, prof_cpu_mask);
370 break;
371 case CPU_UP_CANCELED:
372 case CPU_DEAD:
373 cpu_clear(cpu, prof_cpu_mask);
374 if (per_cpu(cpu_profile_hits, cpu)[0]) {
375 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
376 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
377 __free_page(page);
378 }
379 if (per_cpu(cpu_profile_hits, cpu)[1]) {
380 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
381 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
382 __free_page(page);
383 }
384 break;
385 }
386 return NOTIFY_OK;
387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#else /* !CONFIG_SMP */
389#define profile_flip_buffers() do { } while (0)
390#define profile_discard_flip_buffers() do { } while (0)
Ingo Molnar023160672006-12-06 20:38:17 -0800391#define profile_cpu_callback NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Ingo Molnarece8a682006-12-06 20:37:24 -0800393void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 unsigned long pc;
396
397 if (prof_on != type || !prof_buffer)
398 return;
399 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
Ingo Molnarece8a682006-12-06 20:37:24 -0800400 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402#endif /* !CONFIG_SMP */
403
David Howells7d12e782006-10-05 14:55:46 +0100404void profile_tick(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
David Howells7d12e782006-10-05 14:55:46 +0100406 struct pt_regs *regs = get_irq_regs();
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (type == CPU_PROFILING && timer_hook)
409 timer_hook(regs);
410 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
411 profile_hit(type, (void *)profile_pc(regs));
412}
413
414#ifdef CONFIG_PROC_FS
415#include <linux/proc_fs.h>
416#include <asm/uaccess.h>
417#include <asm/ptrace.h>
418
419static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
420 int count, int *eof, void *data)
421{
422 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
423 if (count - len < 2)
424 return -EINVAL;
425 len += sprintf(page + len, "\n");
426 return len;
427}
428
429static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
430 unsigned long count, void *data)
431{
432 cpumask_t *mask = (cpumask_t *)data;
433 unsigned long full_count = count, err;
434 cpumask_t new_value;
435
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700436 err = cpumask_parse_user(buffer, count, new_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (err)
438 return err;
439
440 *mask = new_value;
441 return full_count;
442}
443
444void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
445{
446 struct proc_dir_entry *entry;
447
448 /* create /proc/irq/prof_cpu_mask */
449 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
450 return;
451 entry->nlink = 1;
452 entry->data = (void *)&prof_cpu_mask;
453 entry->read_proc = prof_cpu_mask_read_proc;
454 entry->write_proc = prof_cpu_mask_write_proc;
455}
456
457/*
458 * This function accesses profiling information. The returned data is
459 * binary: the sampling step and the actual contents of the profile
460 * buffer. Use of the program readprofile is recommended in order to
461 * get meaningful info out of these data.
462 */
463static ssize_t
464read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
465{
466 unsigned long p = *ppos;
467 ssize_t read;
468 char * pnt;
469 unsigned int sample_step = 1 << prof_shift;
470
471 profile_flip_buffers();
472 if (p >= (prof_len+1)*sizeof(unsigned int))
473 return 0;
474 if (count > (prof_len+1)*sizeof(unsigned int) - p)
475 count = (prof_len+1)*sizeof(unsigned int) - p;
476 read = 0;
477
478 while (p < sizeof(unsigned int) && count > 0) {
Heiko Carstens064b0222006-12-06 20:36:37 -0800479 if (put_user(*((char *)(&sample_step)+p),buf))
480 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 buf++; p++; count--; read++;
482 }
483 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
484 if (copy_to_user(buf,(void *)pnt,count))
485 return -EFAULT;
486 read += count;
487 *ppos += read;
488 return read;
489}
490
491/*
492 * Writing to /proc/profile resets the counters
493 *
494 * Writing a 'profiling multiplier' value into it also re-sets the profiling
495 * interrupt frequency, on architectures that support this.
496 */
497static ssize_t write_profile(struct file *file, const char __user *buf,
498 size_t count, loff_t *ppos)
499{
500#ifdef CONFIG_SMP
501 extern int setup_profiling_timer (unsigned int multiplier);
502
503 if (count == sizeof(int)) {
504 unsigned int multiplier;
505
506 if (copy_from_user(&multiplier, buf, sizeof(int)))
507 return -EFAULT;
508
509 if (setup_profiling_timer(multiplier))
510 return -EINVAL;
511 }
512#endif
513 profile_discard_flip_buffers();
514 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
515 return count;
516}
517
Helge Deller15ad7cd2006-12-06 20:40:36 -0800518static const struct file_operations proc_profile_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 .read = read_profile,
520 .write = write_profile,
521};
522
523#ifdef CONFIG_SMP
524static void __init profile_nop(void *unused)
525{
526}
527
528static int __init create_hash_tables(void)
529{
530 int cpu;
531
532 for_each_online_cpu(cpu) {
533 int node = cpu_to_node(cpu);
534 struct page *page;
535
Christoph Lameterfbd98162006-09-25 23:31:45 -0700536 page = alloc_pages_node(node,
537 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
538 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!page)
540 goto out_cleanup;
541 per_cpu(cpu_profile_hits, cpu)[1]
542 = (struct profile_hit *)page_address(page);
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)[0]
549 = (struct profile_hit *)page_address(page);
550 }
551 return 0;
552out_cleanup:
553 prof_on = 0;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700554 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 on_each_cpu(profile_nop, NULL, 0, 1);
556 for_each_online_cpu(cpu) {
557 struct page *page;
558
559 if (per_cpu(cpu_profile_hits, cpu)[0]) {
560 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
561 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
562 __free_page(page);
563 }
564 if (per_cpu(cpu_profile_hits, cpu)[1]) {
565 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
566 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
567 __free_page(page);
568 }
569 }
570 return -1;
571}
572#else
573#define create_hash_tables() ({ 0; })
574#endif
575
576static int __init create_proc_profile(void)
577{
578 struct proc_dir_entry *entry;
579
580 if (!prof_on)
581 return 0;
582 if (create_hash_tables())
583 return -1;
584 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
585 return 0;
586 entry->proc_fops = &proc_profile_operations;
587 entry->size = (1+prof_len) * sizeof(atomic_t);
588 hotcpu_notifier(profile_cpu_callback, 0);
589 return 0;
590}
591module_init(create_proc_profile);
592#endif /* CONFIG_PROC_FS */