Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6/suspend2-2.6.18.git] / kernel / profile.c
blobd5bd75e7501cadef54651a3f886514f0fbcc6f92
1 /*
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.
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
16 #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>
25 #include <linux/mutex.h>
26 #include <asm/sections.h>
27 #include <asm/semaphore.h>
29 struct profile_hit {
30 u32 pc, hits;
32 #define PROFILE_GRPSHIFT 3
33 #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
34 #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
35 #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
37 /* Oprofile timer tick hook */
38 int (*timer_hook)(struct pt_regs *) __read_mostly;
40 static atomic_t *prof_buffer;
41 static unsigned long prof_len, prof_shift;
42 static int prof_on __read_mostly;
43 static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
44 #ifdef CONFIG_SMP
45 static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
46 static DEFINE_PER_CPU(int, cpu_profile_flip);
47 static DEFINE_MUTEX(profile_flip_mutex);
48 #endif /* CONFIG_SMP */
50 static int __init profile_setup(char * str)
52 static char __initdata schedstr[] = "schedule";
53 int par;
55 if (!strncmp(str, schedstr, strlen(schedstr))) {
56 prof_on = SCHED_PROFILING;
57 if (str[strlen(schedstr)] == ',')
58 str += strlen(schedstr) + 1;
59 if (get_option(&str, &par))
60 prof_shift = par;
61 printk(KERN_INFO
62 "kernel schedule profiling enabled (shift: %ld)\n",
63 prof_shift);
64 } else if (get_option(&str, &par)) {
65 prof_shift = par;
66 prof_on = CPU_PROFILING;
67 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
68 prof_shift);
70 return 1;
72 __setup("profile=", profile_setup);
75 void __init profile_init(void)
77 if (!prof_on)
78 return;
80 /* only text is profiled */
81 prof_len = (_etext - _stext) >> prof_shift;
82 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
85 /* Profile event notifications */
87 #ifdef CONFIG_PROFILING
89 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
90 static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
91 static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
93 void profile_task_exit(struct task_struct * task)
95 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
98 int profile_handoff_task(struct task_struct * task)
100 int ret;
101 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
102 return (ret == NOTIFY_OK) ? 1 : 0;
105 void profile_munmap(unsigned long addr)
107 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
110 int task_handoff_register(struct notifier_block * n)
112 return atomic_notifier_chain_register(&task_free_notifier, n);
115 int task_handoff_unregister(struct notifier_block * n)
117 return atomic_notifier_chain_unregister(&task_free_notifier, n);
120 int profile_event_register(enum profile_type type, struct notifier_block * n)
122 int err = -EINVAL;
124 switch (type) {
125 case PROFILE_TASK_EXIT:
126 err = blocking_notifier_chain_register(
127 &task_exit_notifier, n);
128 break;
129 case PROFILE_MUNMAP:
130 err = blocking_notifier_chain_register(
131 &munmap_notifier, n);
132 break;
135 return err;
139 int profile_event_unregister(enum profile_type type, struct notifier_block * n)
141 int err = -EINVAL;
143 switch (type) {
144 case PROFILE_TASK_EXIT:
145 err = blocking_notifier_chain_unregister(
146 &task_exit_notifier, n);
147 break;
148 case PROFILE_MUNMAP:
149 err = blocking_notifier_chain_unregister(
150 &munmap_notifier, n);
151 break;
154 return err;
157 int register_timer_hook(int (*hook)(struct pt_regs *))
159 if (timer_hook)
160 return -EBUSY;
161 timer_hook = hook;
162 return 0;
165 void unregister_timer_hook(int (*hook)(struct pt_regs *))
167 WARN_ON(hook != timer_hook);
168 timer_hook = NULL;
169 /* make sure all CPUs see the NULL hook */
170 synchronize_sched(); /* Allow ongoing interrupts to complete. */
173 EXPORT_SYMBOL_GPL(register_timer_hook);
174 EXPORT_SYMBOL_GPL(unregister_timer_hook);
175 EXPORT_SYMBOL_GPL(task_handoff_register);
176 EXPORT_SYMBOL_GPL(task_handoff_unregister);
178 #endif /* CONFIG_PROFILING */
180 EXPORT_SYMBOL_GPL(profile_event_register);
181 EXPORT_SYMBOL_GPL(profile_event_unregister);
183 #ifdef CONFIG_SMP
185 * Each cpu has a pair of open-addressed hashtables for pending
186 * profile hits. read_profile() IPI's all cpus to request them
187 * to flip buffers and flushes their contents to prof_buffer itself.
188 * Flip requests are serialized by the profile_flip_mutex. The sole
189 * use of having a second hashtable is for avoiding cacheline
190 * contention that would otherwise happen during flushes of pending
191 * profile hits required for the accuracy of reported profile hits
192 * and so resurrect the interrupt livelock issue.
194 * The open-addressed hashtables are indexed by profile buffer slot
195 * and hold the number of pending hits to that profile buffer slot on
196 * a cpu in an entry. When the hashtable overflows, all pending hits
197 * are accounted to their corresponding profile buffer slots with
198 * atomic_add() and the hashtable emptied. As numerous pending hits
199 * may be accounted to a profile buffer slot in a hashtable entry,
200 * this amortizes a number of atomic profile buffer increments likely
201 * to be far larger than the number of entries in the hashtable,
202 * particularly given that the number of distinct profile buffer
203 * positions to which hits are accounted during short intervals (e.g.
204 * several seconds) is usually very small. Exclusion from buffer
205 * flipping is provided by interrupt disablement (note that for
206 * SCHED_PROFILING profile_hit() may be called from process context).
207 * The hash function is meant to be lightweight as opposed to strong,
208 * and was vaguely inspired by ppc64 firmware-supported inverted
209 * pagetable hash functions, but uses a full hashtable full of finite
210 * collision chains, not just pairs of them.
212 * -- wli
214 static void __profile_flip_buffers(void *unused)
216 int cpu = smp_processor_id();
218 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
221 static void profile_flip_buffers(void)
223 int i, j, cpu;
225 mutex_lock(&profile_flip_mutex);
226 j = per_cpu(cpu_profile_flip, get_cpu());
227 put_cpu();
228 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
229 for_each_online_cpu(cpu) {
230 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
231 for (i = 0; i < NR_PROFILE_HIT; ++i) {
232 if (!hits[i].hits) {
233 if (hits[i].pc)
234 hits[i].pc = 0;
235 continue;
237 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
238 hits[i].hits = hits[i].pc = 0;
241 mutex_unlock(&profile_flip_mutex);
244 static void profile_discard_flip_buffers(void)
246 int i, cpu;
248 mutex_lock(&profile_flip_mutex);
249 i = per_cpu(cpu_profile_flip, get_cpu());
250 put_cpu();
251 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
252 for_each_online_cpu(cpu) {
253 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
254 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
256 mutex_unlock(&profile_flip_mutex);
259 void profile_hit(int type, void *__pc)
261 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
262 int i, j, cpu;
263 struct profile_hit *hits;
265 if (prof_on != type || !prof_buffer)
266 return;
267 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
268 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
269 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
270 cpu = get_cpu();
271 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
272 if (!hits) {
273 put_cpu();
274 return;
276 local_irq_save(flags);
277 do {
278 for (j = 0; j < PROFILE_GRPSZ; ++j) {
279 if (hits[i + j].pc == pc) {
280 hits[i + j].hits++;
281 goto out;
282 } else if (!hits[i + j].hits) {
283 hits[i + j].pc = pc;
284 hits[i + j].hits = 1;
285 goto out;
288 i = (i + secondary) & (NR_PROFILE_HIT - 1);
289 } while (i != primary);
290 atomic_inc(&prof_buffer[pc]);
291 for (i = 0; i < NR_PROFILE_HIT; ++i) {
292 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
293 hits[i].pc = hits[i].hits = 0;
295 out:
296 local_irq_restore(flags);
297 put_cpu();
300 #ifdef CONFIG_HOTPLUG_CPU
301 static int __devinit profile_cpu_callback(struct notifier_block *info,
302 unsigned long action, void *__cpu)
304 int node, cpu = (unsigned long)__cpu;
305 struct page *page;
307 switch (action) {
308 case CPU_UP_PREPARE:
309 node = cpu_to_node(cpu);
310 per_cpu(cpu_profile_flip, cpu) = 0;
311 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
312 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
313 if (!page)
314 return NOTIFY_BAD;
315 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
317 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
318 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
319 if (!page)
320 goto out_free;
321 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
323 break;
324 out_free:
325 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
326 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
327 __free_page(page);
328 return NOTIFY_BAD;
329 case CPU_ONLINE:
330 cpu_set(cpu, prof_cpu_mask);
331 break;
332 case CPU_UP_CANCELED:
333 case CPU_DEAD:
334 cpu_clear(cpu, prof_cpu_mask);
335 if (per_cpu(cpu_profile_hits, cpu)[0]) {
336 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
337 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
338 __free_page(page);
340 if (per_cpu(cpu_profile_hits, cpu)[1]) {
341 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
342 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
343 __free_page(page);
345 break;
347 return NOTIFY_OK;
349 #endif /* CONFIG_HOTPLUG_CPU */
350 #else /* !CONFIG_SMP */
351 #define profile_flip_buffers() do { } while (0)
352 #define profile_discard_flip_buffers() do { } while (0)
354 void profile_hit(int type, void *__pc)
356 unsigned long pc;
358 if (prof_on != type || !prof_buffer)
359 return;
360 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
361 atomic_inc(&prof_buffer[min(pc, prof_len - 1)]);
363 #endif /* !CONFIG_SMP */
365 void profile_tick(int type, struct pt_regs *regs)
367 if (type == CPU_PROFILING && timer_hook)
368 timer_hook(regs);
369 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
370 profile_hit(type, (void *)profile_pc(regs));
373 #ifdef CONFIG_PROC_FS
374 #include <linux/proc_fs.h>
375 #include <asm/uaccess.h>
376 #include <asm/ptrace.h>
378 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
379 int count, int *eof, void *data)
381 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
382 if (count - len < 2)
383 return -EINVAL;
384 len += sprintf(page + len, "\n");
385 return len;
388 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
389 unsigned long count, void *data)
391 cpumask_t *mask = (cpumask_t *)data;
392 unsigned long full_count = count, err;
393 cpumask_t new_value;
395 err = cpumask_parse(buffer, count, new_value);
396 if (err)
397 return err;
399 *mask = new_value;
400 return full_count;
403 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
405 struct proc_dir_entry *entry;
407 /* create /proc/irq/prof_cpu_mask */
408 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
409 return;
410 entry->nlink = 1;
411 entry->data = (void *)&prof_cpu_mask;
412 entry->read_proc = prof_cpu_mask_read_proc;
413 entry->write_proc = prof_cpu_mask_write_proc;
417 * This function accesses profiling information. The returned data is
418 * binary: the sampling step and the actual contents of the profile
419 * buffer. Use of the program readprofile is recommended in order to
420 * get meaningful info out of these data.
422 static ssize_t
423 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
425 unsigned long p = *ppos;
426 ssize_t read;
427 char * pnt;
428 unsigned int sample_step = 1 << prof_shift;
430 profile_flip_buffers();
431 if (p >= (prof_len+1)*sizeof(unsigned int))
432 return 0;
433 if (count > (prof_len+1)*sizeof(unsigned int) - p)
434 count = (prof_len+1)*sizeof(unsigned int) - p;
435 read = 0;
437 while (p < sizeof(unsigned int) && count > 0) {
438 put_user(*((char *)(&sample_step)+p),buf);
439 buf++; p++; count--; read++;
441 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
442 if (copy_to_user(buf,(void *)pnt,count))
443 return -EFAULT;
444 read += count;
445 *ppos += read;
446 return read;
450 * Writing to /proc/profile resets the counters
452 * Writing a 'profiling multiplier' value into it also re-sets the profiling
453 * interrupt frequency, on architectures that support this.
455 static ssize_t write_profile(struct file *file, const char __user *buf,
456 size_t count, loff_t *ppos)
458 #ifdef CONFIG_SMP
459 extern int setup_profiling_timer (unsigned int multiplier);
461 if (count == sizeof(int)) {
462 unsigned int multiplier;
464 if (copy_from_user(&multiplier, buf, sizeof(int)))
465 return -EFAULT;
467 if (setup_profiling_timer(multiplier))
468 return -EINVAL;
470 #endif
471 profile_discard_flip_buffers();
472 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
473 return count;
476 static struct file_operations proc_profile_operations = {
477 .read = read_profile,
478 .write = write_profile,
481 #ifdef CONFIG_SMP
482 static void __init profile_nop(void *unused)
486 static int __init create_hash_tables(void)
488 int cpu;
490 for_each_online_cpu(cpu) {
491 int node = cpu_to_node(cpu);
492 struct page *page;
494 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
495 if (!page)
496 goto out_cleanup;
497 per_cpu(cpu_profile_hits, cpu)[1]
498 = (struct profile_hit *)page_address(page);
499 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
500 if (!page)
501 goto out_cleanup;
502 per_cpu(cpu_profile_hits, cpu)[0]
503 = (struct profile_hit *)page_address(page);
505 return 0;
506 out_cleanup:
507 prof_on = 0;
508 smp_mb();
509 on_each_cpu(profile_nop, NULL, 0, 1);
510 for_each_online_cpu(cpu) {
511 struct page *page;
513 if (per_cpu(cpu_profile_hits, cpu)[0]) {
514 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
515 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
516 __free_page(page);
518 if (per_cpu(cpu_profile_hits, cpu)[1]) {
519 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
520 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
521 __free_page(page);
524 return -1;
526 #else
527 #define create_hash_tables() ({ 0; })
528 #endif
530 static int __init create_proc_profile(void)
532 struct proc_dir_entry *entry;
534 if (!prof_on)
535 return 0;
536 if (create_hash_tables())
537 return -1;
538 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
539 return 0;
540 entry->proc_fops = &proc_profile_operations;
541 entry->size = (1+prof_len) * sizeof(atomic_t);
542 hotcpu_notifier(profile_cpu_callback, 0);
543 return 0;
545 module_init(create_proc_profile);
546 #endif /* CONFIG_PROC_FS */