[UDP]: UDP can use sk_hash to speedup lookups
[pv_ops_mirror.git] / kernel / profile.c
blobd6579d511069130980196be79005f2724a01b8d3
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>
28 #include <asm/irq_regs.h>
30 struct profile_hit {
31 u32 pc, hits;
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)
38 /* Oprofile timer tick hook */
39 int (*timer_hook)(struct pt_regs *) __read_mostly;
41 static atomic_t *prof_buffer;
42 static unsigned long prof_len, prof_shift;
44 int prof_on __read_mostly;
45 EXPORT_SYMBOL_GPL(prof_on);
47 static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
48 #ifdef CONFIG_SMP
49 static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
50 static DEFINE_PER_CPU(int, cpu_profile_flip);
51 static DEFINE_MUTEX(profile_flip_mutex);
52 #endif /* CONFIG_SMP */
54 static int __init profile_setup(char * str)
56 static char __initdata schedstr[] = "schedule";
57 static char __initdata sleepstr[] = "sleep";
58 static char __initdata kvmstr[] = "kvm";
59 int par;
61 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);
70 } else if (!strncmp(str, schedstr, strlen(schedstr))) {
71 prof_on = SCHED_PROFILING;
72 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);
79 } 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);
88 } else if (get_option(&str, &par)) {
89 prof_shift = par;
90 prof_on = CPU_PROFILING;
91 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
92 prof_shift);
94 return 1;
96 __setup("profile=", profile_setup);
99 void __init profile_init(void)
101 if (!prof_on)
102 return;
104 /* only text is profiled */
105 prof_len = (_etext - _stext) >> prof_shift;
106 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
109 /* Profile event notifications */
111 #ifdef CONFIG_PROFILING
113 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
114 static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
115 static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
117 void profile_task_exit(struct task_struct * task)
119 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
122 int profile_handoff_task(struct task_struct * task)
124 int ret;
125 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
126 return (ret == NOTIFY_OK) ? 1 : 0;
129 void profile_munmap(unsigned long addr)
131 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
134 int task_handoff_register(struct notifier_block * n)
136 return atomic_notifier_chain_register(&task_free_notifier, n);
139 int task_handoff_unregister(struct notifier_block * n)
141 return atomic_notifier_chain_unregister(&task_free_notifier, n);
144 int profile_event_register(enum profile_type type, struct notifier_block * n)
146 int err = -EINVAL;
148 switch (type) {
149 case PROFILE_TASK_EXIT:
150 err = blocking_notifier_chain_register(
151 &task_exit_notifier, n);
152 break;
153 case PROFILE_MUNMAP:
154 err = blocking_notifier_chain_register(
155 &munmap_notifier, n);
156 break;
159 return err;
163 int profile_event_unregister(enum profile_type type, struct notifier_block * n)
165 int err = -EINVAL;
167 switch (type) {
168 case PROFILE_TASK_EXIT:
169 err = blocking_notifier_chain_unregister(
170 &task_exit_notifier, n);
171 break;
172 case PROFILE_MUNMAP:
173 err = blocking_notifier_chain_unregister(
174 &munmap_notifier, n);
175 break;
178 return err;
181 int register_timer_hook(int (*hook)(struct pt_regs *))
183 if (timer_hook)
184 return -EBUSY;
185 timer_hook = hook;
186 return 0;
189 void unregister_timer_hook(int (*hook)(struct pt_regs *))
191 WARN_ON(hook != timer_hook);
192 timer_hook = NULL;
193 /* make sure all CPUs see the NULL hook */
194 synchronize_sched(); /* Allow ongoing interrupts to complete. */
197 EXPORT_SYMBOL_GPL(register_timer_hook);
198 EXPORT_SYMBOL_GPL(unregister_timer_hook);
199 EXPORT_SYMBOL_GPL(task_handoff_register);
200 EXPORT_SYMBOL_GPL(task_handoff_unregister);
202 #endif /* CONFIG_PROFILING */
204 EXPORT_SYMBOL_GPL(profile_event_register);
205 EXPORT_SYMBOL_GPL(profile_event_unregister);
207 #ifdef CONFIG_SMP
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.
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
230 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
231 * process context).
232 * 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.
237 * -- wli
239 static void __profile_flip_buffers(void *unused)
241 int cpu = smp_processor_id();
243 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
246 static void profile_flip_buffers(void)
248 int i, j, cpu;
250 mutex_lock(&profile_flip_mutex);
251 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;
262 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
263 hits[i].hits = hits[i].pc = 0;
266 mutex_unlock(&profile_flip_mutex);
269 static void profile_discard_flip_buffers(void)
271 int i, cpu;
273 mutex_lock(&profile_flip_mutex);
274 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));
281 mutex_unlock(&profile_flip_mutex);
284 void profile_hits(int type, void *__pc, unsigned int nr_hits)
286 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
287 int i, j, cpu;
288 struct profile_hit *hits;
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;
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:
306 local_irq_save(flags);
307 do {
308 for (j = 0; j < PROFILE_GRPSZ; ++j) {
309 if (hits[i + j].pc == pc) {
310 hits[i + j].hits += nr_hits;
311 goto out;
312 } else if (!hits[i + j].hits) {
313 hits[i + j].pc = pc;
314 hits[i + j].hits = nr_hits;
315 goto out;
318 i = (i + secondary) & (NR_PROFILE_HIT - 1);
319 } while (i != primary);
322 * Add the current hit(s) and flush the write-queue out
323 * to the global buffer:
325 atomic_add(nr_hits, &prof_buffer[pc]);
326 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;
330 out:
331 local_irq_restore(flags);
332 put_cpu();
335 static int __devinit profile_cpu_callback(struct notifier_block *info,
336 unsigned long action, void *__cpu)
338 int node, cpu = (unsigned long)__cpu;
339 struct page *page;
341 switch (action) {
342 case CPU_UP_PREPARE:
343 node = cpu_to_node(cpu);
344 per_cpu(cpu_profile_flip, cpu) = 0;
345 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
346 page = alloc_pages_node(node,
347 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
349 if (!page)
350 return NOTIFY_BAD;
351 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
353 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
354 page = alloc_pages_node(node,
355 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
357 if (!page)
358 goto out_free;
359 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
361 break;
362 out_free:
363 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
364 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
365 __free_page(page);
366 return NOTIFY_BAD;
367 case CPU_ONLINE:
368 cpu_set(cpu, prof_cpu_mask);
369 break;
370 case CPU_UP_CANCELED:
371 case CPU_DEAD:
372 cpu_clear(cpu, prof_cpu_mask);
373 if (per_cpu(cpu_profile_hits, cpu)[0]) {
374 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
375 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
376 __free_page(page);
378 if (per_cpu(cpu_profile_hits, cpu)[1]) {
379 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
380 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
381 __free_page(page);
383 break;
385 return NOTIFY_OK;
387 #else /* !CONFIG_SMP */
388 #define profile_flip_buffers() do { } while (0)
389 #define profile_discard_flip_buffers() do { } while (0)
390 #define profile_cpu_callback NULL
392 void profile_hits(int type, void *__pc, unsigned int nr_hits)
394 unsigned long pc;
396 if (prof_on != type || !prof_buffer)
397 return;
398 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
399 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
401 #endif /* !CONFIG_SMP */
403 EXPORT_SYMBOL_GPL(profile_hits);
405 void profile_tick(int type)
407 struct pt_regs *regs = get_irq_regs();
409 if (type == CPU_PROFILING && timer_hook)
410 timer_hook(regs);
411 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
412 profile_hit(type, (void *)profile_pc(regs));
415 #ifdef CONFIG_PROC_FS
416 #include <linux/proc_fs.h>
417 #include <asm/uaccess.h>
418 #include <asm/ptrace.h>
420 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
421 int count, int *eof, void *data)
423 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
424 if (count - len < 2)
425 return -EINVAL;
426 len += sprintf(page + len, "\n");
427 return len;
430 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
431 unsigned long count, void *data)
433 cpumask_t *mask = (cpumask_t *)data;
434 unsigned long full_count = count, err;
435 cpumask_t new_value;
437 err = cpumask_parse_user(buffer, count, new_value);
438 if (err)
439 return err;
441 *mask = new_value;
442 return full_count;
445 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
447 struct proc_dir_entry *entry;
449 /* create /proc/irq/prof_cpu_mask */
450 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
451 return;
452 entry->nlink = 1;
453 entry->data = (void *)&prof_cpu_mask;
454 entry->read_proc = prof_cpu_mask_read_proc;
455 entry->write_proc = prof_cpu_mask_write_proc;
459 * This function accesses profiling information. The returned data is
460 * binary: the sampling step and the actual contents of the profile
461 * buffer. Use of the program readprofile is recommended in order to
462 * get meaningful info out of these data.
464 static ssize_t
465 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
467 unsigned long p = *ppos;
468 ssize_t read;
469 char * pnt;
470 unsigned int sample_step = 1 << prof_shift;
472 profile_flip_buffers();
473 if (p >= (prof_len+1)*sizeof(unsigned int))
474 return 0;
475 if (count > (prof_len+1)*sizeof(unsigned int) - p)
476 count = (prof_len+1)*sizeof(unsigned int) - p;
477 read = 0;
479 while (p < sizeof(unsigned int) && count > 0) {
480 if (put_user(*((char *)(&sample_step)+p),buf))
481 return -EFAULT;
482 buf++; p++; count--; read++;
484 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
485 if (copy_to_user(buf,(void *)pnt,count))
486 return -EFAULT;
487 read += count;
488 *ppos += read;
489 return read;
493 * Writing to /proc/profile resets the counters
495 * Writing a 'profiling multiplier' value into it also re-sets the profiling
496 * interrupt frequency, on architectures that support this.
498 static ssize_t write_profile(struct file *file, const char __user *buf,
499 size_t count, loff_t *ppos)
501 #ifdef CONFIG_SMP
502 extern int setup_profiling_timer (unsigned int multiplier);
504 if (count == sizeof(int)) {
505 unsigned int multiplier;
507 if (copy_from_user(&multiplier, buf, sizeof(int)))
508 return -EFAULT;
510 if (setup_profiling_timer(multiplier))
511 return -EINVAL;
513 #endif
514 profile_discard_flip_buffers();
515 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
516 return count;
519 static const struct file_operations proc_profile_operations = {
520 .read = read_profile,
521 .write = write_profile,
524 #ifdef CONFIG_SMP
525 static void __init profile_nop(void *unused)
529 static int __init create_hash_tables(void)
531 int cpu;
533 for_each_online_cpu(cpu) {
534 int node = cpu_to_node(cpu);
535 struct page *page;
537 page = alloc_pages_node(node,
538 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
540 if (!page)
541 goto out_cleanup;
542 per_cpu(cpu_profile_hits, cpu)[1]
543 = (struct profile_hit *)page_address(page);
544 page = alloc_pages_node(node,
545 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
547 if (!page)
548 goto out_cleanup;
549 per_cpu(cpu_profile_hits, cpu)[0]
550 = (struct profile_hit *)page_address(page);
552 return 0;
553 out_cleanup:
554 prof_on = 0;
555 smp_mb();
556 on_each_cpu(profile_nop, NULL, 0, 1);
557 for_each_online_cpu(cpu) {
558 struct page *page;
560 if (per_cpu(cpu_profile_hits, cpu)[0]) {
561 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
562 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
563 __free_page(page);
565 if (per_cpu(cpu_profile_hits, cpu)[1]) {
566 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
567 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
568 __free_page(page);
571 return -1;
573 #else
574 #define create_hash_tables() ({ 0; })
575 #endif
577 static int __init create_proc_profile(void)
579 struct proc_dir_entry *entry;
581 if (!prof_on)
582 return 0;
583 if (create_hash_tables())
584 return -1;
585 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
586 return 0;
587 entry->proc_fops = &proc_profile_operations;
588 entry->size = (1+prof_len) * sizeof(atomic_t);
589 hotcpu_notifier(profile_cpu_callback, 0);
590 return 0;
592 module_init(create_proc_profile);
593 #endif /* CONFIG_PROC_FS */