[PATCH] DVB: misc. updates to the dvb-core
[linux-2.6/history.git] / kernel / profile.c
blobe7ff9b32d8220d1e57e9ff9c8fb9f38406bdae28
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/config.h>
17 #include <linux/module.h>
18 #include <linux/profile.h>
19 #include <linux/bootmem.h>
20 #include <linux/notifier.h>
21 #include <linux/mm.h>
22 #include <linux/cpumask.h>
23 #include <linux/cpu.h>
24 #include <linux/profile.h>
25 #include <linux/highmem.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 static atomic_t *prof_buffer;
38 static unsigned long prof_len, prof_shift;
39 static int prof_on;
40 static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
41 #ifdef CONFIG_SMP
42 static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
43 static DEFINE_PER_CPU(int, cpu_profile_flip);
44 static DECLARE_MUTEX(profile_flip_mutex);
45 #endif /* CONFIG_SMP */
47 static int __init profile_setup(char * str)
49 int par;
51 if (!strncmp(str, "schedule", 8)) {
52 prof_on = SCHED_PROFILING;
53 printk(KERN_INFO "kernel schedule profiling enabled\n");
54 if (str[7] == ',')
55 str += 8;
57 if (get_option(&str,&par)) {
58 prof_shift = par;
59 prof_on = CPU_PROFILING;
60 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
61 prof_shift);
63 return 1;
65 __setup("profile=", profile_setup);
68 void __init profile_init(void)
70 if (!prof_on)
71 return;
73 /* only text is profiled */
74 prof_len = (_etext - _stext) >> prof_shift;
75 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
78 /* Profile event notifications */
80 #ifdef CONFIG_PROFILING
82 static DECLARE_RWSEM(profile_rwsem);
83 static rwlock_t handoff_lock = RW_LOCK_UNLOCKED;
84 static struct notifier_block * task_exit_notifier;
85 static struct notifier_block * task_free_notifier;
86 static struct notifier_block * munmap_notifier;
88 void profile_task_exit(struct task_struct * task)
90 down_read(&profile_rwsem);
91 notifier_call_chain(&task_exit_notifier, 0, task);
92 up_read(&profile_rwsem);
95 int profile_handoff_task(struct task_struct * task)
97 int ret;
98 read_lock(&handoff_lock);
99 ret = notifier_call_chain(&task_free_notifier, 0, task);
100 read_unlock(&handoff_lock);
101 return (ret == NOTIFY_OK) ? 1 : 0;
104 void profile_munmap(unsigned long addr)
106 down_read(&profile_rwsem);
107 notifier_call_chain(&munmap_notifier, 0, (void *)addr);
108 up_read(&profile_rwsem);
111 int task_handoff_register(struct notifier_block * n)
113 int err = -EINVAL;
115 write_lock(&handoff_lock);
116 err = notifier_chain_register(&task_free_notifier, n);
117 write_unlock(&handoff_lock);
118 return err;
121 int task_handoff_unregister(struct notifier_block * n)
123 int err = -EINVAL;
125 write_lock(&handoff_lock);
126 err = notifier_chain_unregister(&task_free_notifier, n);
127 write_unlock(&handoff_lock);
128 return err;
131 int profile_event_register(enum profile_type type, struct notifier_block * n)
133 int err = -EINVAL;
135 down_write(&profile_rwsem);
137 switch (type) {
138 case PROFILE_TASK_EXIT:
139 err = notifier_chain_register(&task_exit_notifier, n);
140 break;
141 case PROFILE_MUNMAP:
142 err = notifier_chain_register(&munmap_notifier, n);
143 break;
146 up_write(&profile_rwsem);
148 return err;
152 int profile_event_unregister(enum profile_type type, struct notifier_block * n)
154 int err = -EINVAL;
156 down_write(&profile_rwsem);
158 switch (type) {
159 case PROFILE_TASK_EXIT:
160 err = notifier_chain_unregister(&task_exit_notifier, n);
161 break;
162 case PROFILE_MUNMAP:
163 err = notifier_chain_unregister(&munmap_notifier, n);
164 break;
167 up_write(&profile_rwsem);
168 return err;
171 static struct notifier_block * profile_listeners;
172 static rwlock_t profile_lock = RW_LOCK_UNLOCKED;
174 int register_profile_notifier(struct notifier_block * nb)
176 int err;
177 write_lock_irq(&profile_lock);
178 err = notifier_chain_register(&profile_listeners, nb);
179 write_unlock_irq(&profile_lock);
180 return err;
184 int unregister_profile_notifier(struct notifier_block * nb)
186 int err;
187 write_lock_irq(&profile_lock);
188 err = notifier_chain_unregister(&profile_listeners, nb);
189 write_unlock_irq(&profile_lock);
190 return err;
194 void profile_hook(struct pt_regs * regs)
196 read_lock(&profile_lock);
197 notifier_call_chain(&profile_listeners, 0, regs);
198 read_unlock(&profile_lock);
201 EXPORT_SYMBOL_GPL(register_profile_notifier);
202 EXPORT_SYMBOL_GPL(unregister_profile_notifier);
203 EXPORT_SYMBOL_GPL(task_handoff_register);
204 EXPORT_SYMBOL_GPL(task_handoff_unregister);
206 #endif /* CONFIG_PROFILING */
208 EXPORT_SYMBOL_GPL(profile_event_register);
209 EXPORT_SYMBOL_GPL(profile_event_unregister);
211 #ifdef CONFIG_SMP
213 * Each cpu has a pair of open-addressed hashtables for pending
214 * profile hits. read_profile() IPI's all cpus to request them
215 * to flip buffers and flushes their contents to prof_buffer itself.
216 * Flip requests are serialized by the profile_flip_mutex. The sole
217 * use of having a second hashtable is for avoiding cacheline
218 * contention that would otherwise happen during flushes of pending
219 * profile hits required for the accuracy of reported profile hits
220 * and so resurrect the interrupt livelock issue.
222 * The open-addressed hashtables are indexed by profile buffer slot
223 * and hold the number of pending hits to that profile buffer slot on
224 * a cpu in an entry. When the hashtable overflows, all pending hits
225 * are accounted to their corresponding profile buffer slots with
226 * atomic_add() and the hashtable emptied. As numerous pending hits
227 * may be accounted to a profile buffer slot in a hashtable entry,
228 * this amortizes a number of atomic profile buffer increments likely
229 * to be far larger than the number of entries in the hashtable,
230 * particularly given that the number of distinct profile buffer
231 * positions to which hits are accounted during short intervals (e.g.
232 * several seconds) is usually very small. Exclusion from buffer
233 * flipping is provided by interrupt disablement (note that for
234 * SCHED_PROFILING profile_hit() may be called from process context).
235 * 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.
240 * -- wli
242 static void __profile_flip_buffers(void *unused)
244 int cpu = smp_processor_id();
246 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
249 static void profile_flip_buffers(void)
251 int i, j, cpu;
253 down(&profile_flip_mutex);
254 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;
265 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
266 hits[i].hits = hits[i].pc = 0;
269 up(&profile_flip_mutex);
272 static void profile_discard_flip_buffers(void)
274 int i, cpu;
276 down(&profile_flip_mutex);
277 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));
284 up(&profile_flip_mutex);
287 void profile_hit(int type, void *__pc)
289 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
290 int i, j, cpu;
291 struct profile_hit *hits;
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;
304 local_irq_save(flags);
305 do {
306 for (j = 0; j < PROFILE_GRPSZ; ++j) {
307 if (hits[i + j].pc == pc) {
308 hits[i + j].hits++;
309 goto out;
310 } else if (!hits[i + j].hits) {
311 hits[i + j].pc = pc;
312 hits[i + j].hits = 1;
313 goto out;
316 i = (i + secondary) & (NR_PROFILE_HIT - 1);
317 } while (i != primary);
318 atomic_inc(&prof_buffer[pc]);
319 for (i = 0; i < NR_PROFILE_HIT; ++i) {
320 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
321 hits[i].pc = hits[i].hits = 0;
323 out:
324 local_irq_restore(flags);
325 put_cpu();
328 #ifdef CONFIG_HOTPLUG_CPU
329 static int __devinit profile_cpu_callback(struct notifier_block *info,
330 unsigned long action, void *__cpu)
332 int node, cpu = (unsigned long)__cpu;
333 struct page *page;
335 switch (action) {
336 case CPU_UP_PREPARE:
337 node = cpu_to_node(cpu);
338 per_cpu(cpu_profile_flip, cpu) = 0;
339 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
340 page = alloc_pages_node(node, GFP_KERNEL, 0);
341 if (!page)
342 return NOTIFY_BAD;
343 clear_highpage(page);
344 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
346 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
347 page = alloc_pages_node(node, GFP_KERNEL, 0);
348 if (!page)
349 goto out_free;
350 clear_highpage(page);
351 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
353 break;
354 out_free:
355 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
356 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
357 __free_page(page);
358 return NOTIFY_BAD;
359 case CPU_ONLINE:
360 cpu_set(cpu, prof_cpu_mask);
361 break;
362 case CPU_UP_CANCELED:
363 case CPU_DEAD:
364 cpu_clear(cpu, prof_cpu_mask);
365 if (per_cpu(cpu_profile_hits, cpu)[0]) {
366 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
367 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
368 __free_page(page);
370 if (per_cpu(cpu_profile_hits, cpu)[1]) {
371 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
372 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
373 __free_page(page);
375 break;
377 return NOTIFY_OK;
379 #endif /* CONFIG_HOTPLUG_CPU */
380 #else /* !CONFIG_SMP */
381 #define profile_flip_buffers() do { } while (0)
382 #define profile_discard_flip_buffers() do { } while (0)
384 void profile_hit(int type, void *__pc)
386 unsigned long pc;
388 if (prof_on != type || !prof_buffer)
389 return;
390 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
391 atomic_inc(&prof_buffer[min(pc, prof_len - 1)]);
393 #endif /* !CONFIG_SMP */
395 void profile_tick(int type, struct pt_regs *regs)
397 if (type == CPU_PROFILING)
398 profile_hook(regs);
399 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
400 profile_hit(type, (void *)profile_pc(regs));
403 #ifdef CONFIG_PROC_FS
404 #include <linux/proc_fs.h>
405 #include <asm/uaccess.h>
406 #include <asm/ptrace.h>
408 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
409 int count, int *eof, void *data)
411 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
412 if (count - len < 2)
413 return -EINVAL;
414 len += sprintf(page + len, "\n");
415 return len;
418 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
419 unsigned long count, void *data)
421 cpumask_t *mask = (cpumask_t *)data;
422 unsigned long full_count = count, err;
423 cpumask_t new_value;
425 err = cpumask_parse(buffer, count, new_value);
426 if (err)
427 return err;
429 *mask = new_value;
430 return full_count;
433 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
435 struct proc_dir_entry *entry;
437 /* create /proc/irq/prof_cpu_mask */
438 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
439 return;
440 entry->nlink = 1;
441 entry->data = (void *)&prof_cpu_mask;
442 entry->read_proc = prof_cpu_mask_read_proc;
443 entry->write_proc = prof_cpu_mask_write_proc;
447 * This function accesses profiling information. The returned data is
448 * binary: the sampling step and the actual contents of the profile
449 * buffer. Use of the program readprofile is recommended in order to
450 * get meaningful info out of these data.
452 static ssize_t
453 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
455 unsigned long p = *ppos;
456 ssize_t read;
457 char * pnt;
458 unsigned int sample_step = 1 << prof_shift;
460 profile_flip_buffers();
461 if (p >= (prof_len+1)*sizeof(unsigned int))
462 return 0;
463 if (count > (prof_len+1)*sizeof(unsigned int) - p)
464 count = (prof_len+1)*sizeof(unsigned int) - p;
465 read = 0;
467 while (p < sizeof(unsigned int) && count > 0) {
468 put_user(*((char *)(&sample_step)+p),buf);
469 buf++; p++; count--; read++;
471 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
472 if (copy_to_user(buf,(void *)pnt,count))
473 return -EFAULT;
474 read += count;
475 *ppos += read;
476 return read;
480 * Writing to /proc/profile resets the counters
482 * Writing a 'profiling multiplier' value into it also re-sets the profiling
483 * interrupt frequency, on architectures that support this.
485 static ssize_t write_profile(struct file *file, const char __user *buf,
486 size_t count, loff_t *ppos)
488 #ifdef CONFIG_SMP
489 extern int setup_profiling_timer (unsigned int multiplier);
491 if (count == sizeof(int)) {
492 unsigned int multiplier;
494 if (copy_from_user(&multiplier, buf, sizeof(int)))
495 return -EFAULT;
497 if (setup_profiling_timer(multiplier))
498 return -EINVAL;
500 #endif
501 profile_discard_flip_buffers();
502 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
503 return count;
506 static struct file_operations proc_profile_operations = {
507 .read = read_profile,
508 .write = write_profile,
511 #ifdef CONFIG_SMP
512 static void __init profile_nop(void *unused)
516 static int __init create_hash_tables(void)
518 int cpu;
520 for_each_online_cpu(cpu) {
521 int node = cpu_to_node(cpu);
522 struct page *page;
524 page = alloc_pages_node(node, GFP_KERNEL, 0);
525 if (!page)
526 goto out_cleanup;
527 clear_highpage(page);
528 per_cpu(cpu_profile_hits, cpu)[1]
529 = (struct profile_hit *)page_address(page);
530 page = alloc_pages_node(node, GFP_KERNEL, 0);
531 if (!page)
532 goto out_cleanup;
533 clear_highpage(page);
534 per_cpu(cpu_profile_hits, cpu)[0]
535 = (struct profile_hit *)page_address(page);
537 return 0;
538 out_cleanup:
539 prof_on = 0;
540 mb();
541 on_each_cpu(profile_nop, NULL, 0, 1);
542 for_each_online_cpu(cpu) {
543 struct page *page;
545 if (per_cpu(cpu_profile_hits, cpu)[0]) {
546 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
547 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
548 __free_page(page);
550 if (per_cpu(cpu_profile_hits, cpu)[1]) {
551 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
552 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
553 __free_page(page);
556 return -1;
558 #else
559 #define create_hash_tables() ({ 0; })
560 #endif
562 static int __init create_proc_profile(void)
564 struct proc_dir_entry *entry;
566 if (!prof_on)
567 return 0;
568 if (create_hash_tables())
569 return -1;
570 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
571 return 0;
572 entry->proc_fops = &proc_profile_operations;
573 entry->size = (1+prof_len) * sizeof(atomic_t);
574 hotcpu_notifier(profile_cpu_callback, 0);
575 return 0;
577 module_init(create_proc_profile);
578 #endif /* CONFIG_PROC_FS */