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,
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>
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>
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;
43 int prof_on __read_mostly;
44 static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
46 static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
47 static DEFINE_PER_CPU(int, cpu_profile_flip);
48 static DEFINE_MUTEX(profile_flip_mutex);
49 #endif /* CONFIG_SMP */
51 static int __init profile_setup(char * str)
53 static char __initdata schedstr[] = "schedule";
54 static char __initdata sleepstr[] = "sleep";
57 if (!strncmp(str, sleepstr, strlen(sleepstr))) {
58 prof_on = SLEEP_PROFILING;
59 if (str[strlen(sleepstr)] == ',')
60 str += strlen(sleepstr) + 1;
61 if (get_option(&str, &par))
64 "kernel sleep profiling enabled (shift: %ld)\n",
66 } else if (!strncmp(str, sleepstr, strlen(sleepstr))) {
67 prof_on = SCHED_PROFILING;
68 if (str[strlen(schedstr)] == ',')
69 str += strlen(schedstr) + 1;
70 if (get_option(&str, &par))
73 "kernel schedule profiling enabled (shift: %ld)\n",
75 } else if (get_option(&str, &par)) {
77 prof_on = CPU_PROFILING;
78 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
83 __setup("profile=", profile_setup);
86 void __init profile_init(void)
91 /* only text is profiled */
92 prof_len = (_etext - _stext) >> prof_shift;
93 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
96 /* Profile event notifications */
98 #ifdef CONFIG_PROFILING
100 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
101 static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
102 static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
104 void profile_task_exit(struct task_struct * task)
106 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
109 int profile_handoff_task(struct task_struct * task)
112 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
113 return (ret == NOTIFY_OK) ? 1 : 0;
116 void profile_munmap(unsigned long addr)
118 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
121 int task_handoff_register(struct notifier_block * n)
123 return atomic_notifier_chain_register(&task_free_notifier, n);
126 int task_handoff_unregister(struct notifier_block * n)
128 return atomic_notifier_chain_unregister(&task_free_notifier, n);
131 int profile_event_register(enum profile_type type, struct notifier_block * n)
136 case PROFILE_TASK_EXIT:
137 err = blocking_notifier_chain_register(
138 &task_exit_notifier, n);
141 err = blocking_notifier_chain_register(
142 &munmap_notifier, n);
150 int profile_event_unregister(enum profile_type type, struct notifier_block * n)
155 case PROFILE_TASK_EXIT:
156 err = blocking_notifier_chain_unregister(
157 &task_exit_notifier, n);
160 err = blocking_notifier_chain_unregister(
161 &munmap_notifier, n);
168 int register_timer_hook(int (*hook)(struct pt_regs *))
176 void unregister_timer_hook(int (*hook)(struct pt_regs *))
178 WARN_ON(hook != timer_hook);
180 /* make sure all CPUs see the NULL hook */
181 synchronize_sched(); /* Allow ongoing interrupts to complete. */
184 EXPORT_SYMBOL_GPL(register_timer_hook);
185 EXPORT_SYMBOL_GPL(unregister_timer_hook);
186 EXPORT_SYMBOL_GPL(task_handoff_register);
187 EXPORT_SYMBOL_GPL(task_handoff_unregister);
189 #endif /* CONFIG_PROFILING */
191 EXPORT_SYMBOL_GPL(profile_event_register);
192 EXPORT_SYMBOL_GPL(profile_event_unregister);
196 * Each cpu has a pair of open-addressed hashtables for pending
197 * profile hits. read_profile() IPI's all cpus to request them
198 * to flip buffers and flushes their contents to prof_buffer itself.
199 * Flip requests are serialized by the profile_flip_mutex. The sole
200 * use of having a second hashtable is for avoiding cacheline
201 * contention that would otherwise happen during flushes of pending
202 * profile hits required for the accuracy of reported profile hits
203 * and so resurrect the interrupt livelock issue.
205 * The open-addressed hashtables are indexed by profile buffer slot
206 * and hold the number of pending hits to that profile buffer slot on
207 * a cpu in an entry. When the hashtable overflows, all pending hits
208 * are accounted to their corresponding profile buffer slots with
209 * atomic_add() and the hashtable emptied. As numerous pending hits
210 * may be accounted to a profile buffer slot in a hashtable entry,
211 * this amortizes a number of atomic profile buffer increments likely
212 * to be far larger than the number of entries in the hashtable,
213 * particularly given that the number of distinct profile buffer
214 * positions to which hits are accounted during short intervals (e.g.
215 * several seconds) is usually very small. Exclusion from buffer
216 * flipping is provided by interrupt disablement (note that for
217 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
219 * The hash function is meant to be lightweight as opposed to strong,
220 * and was vaguely inspired by ppc64 firmware-supported inverted
221 * pagetable hash functions, but uses a full hashtable full of finite
222 * collision chains, not just pairs of them.
226 static void __profile_flip_buffers(void *unused)
228 int cpu = smp_processor_id();
230 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
233 static void profile_flip_buffers(void)
237 mutex_lock(&profile_flip_mutex);
238 j = per_cpu(cpu_profile_flip, get_cpu());
240 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
241 for_each_online_cpu(cpu) {
242 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
243 for (i = 0; i < NR_PROFILE_HIT; ++i) {
249 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
250 hits[i].hits = hits[i].pc = 0;
253 mutex_unlock(&profile_flip_mutex);
256 static void profile_discard_flip_buffers(void)
260 mutex_lock(&profile_flip_mutex);
261 i = per_cpu(cpu_profile_flip, get_cpu());
263 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
264 for_each_online_cpu(cpu) {
265 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
266 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
268 mutex_unlock(&profile_flip_mutex);
271 void profile_hits(int type, void *__pc, unsigned int nr_hits)
273 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
275 struct profile_hit *hits;
277 if (prof_on != type || !prof_buffer)
279 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
280 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
281 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
283 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
289 * We buffer the global profiler buffer into a per-CPU
290 * queue and thus reduce the number of global (and possibly
291 * NUMA-alien) accesses. The write-queue is self-coalescing:
293 local_irq_save(flags);
295 for (j = 0; j < PROFILE_GRPSZ; ++j) {
296 if (hits[i + j].pc == pc) {
297 hits[i + j].hits += nr_hits;
299 } else if (!hits[i + j].hits) {
301 hits[i + j].hits = nr_hits;
305 i = (i + secondary) & (NR_PROFILE_HIT - 1);
306 } while (i != primary);
309 * Add the current hit(s) and flush the write-queue out
310 * to the global buffer:
312 atomic_add(nr_hits, &prof_buffer[pc]);
313 for (i = 0; i < NR_PROFILE_HIT; ++i) {
314 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
315 hits[i].pc = hits[i].hits = 0;
318 local_irq_restore(flags);
322 #ifdef CONFIG_HOTPLUG_CPU
323 static int __devinit profile_cpu_callback(struct notifier_block *info,
324 unsigned long action, void *__cpu)
326 int node, cpu = (unsigned long)__cpu;
331 node = cpu_to_node(cpu);
332 per_cpu(cpu_profile_flip, cpu) = 0;
333 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
334 page = alloc_pages_node(node,
335 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
339 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
341 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
342 page = alloc_pages_node(node,
343 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
347 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
351 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
352 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
356 cpu_set(cpu, prof_cpu_mask);
358 case CPU_UP_CANCELED:
360 cpu_clear(cpu, prof_cpu_mask);
361 if (per_cpu(cpu_profile_hits, cpu)[0]) {
362 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
363 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
366 if (per_cpu(cpu_profile_hits, cpu)[1]) {
367 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
368 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
375 #endif /* CONFIG_HOTPLUG_CPU */
376 #else /* !CONFIG_SMP */
377 #define profile_flip_buffers() do { } while (0)
378 #define profile_discard_flip_buffers() do { } while (0)
380 void profile_hits(int type, void *__pc, unsigned int nr_hits)
384 if (prof_on != type || !prof_buffer)
386 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
387 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
389 #endif /* !CONFIG_SMP */
391 void profile_tick(int type)
393 struct pt_regs *regs = get_irq_regs();
395 if (type == CPU_PROFILING && timer_hook)
397 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
398 profile_hit(type, (void *)profile_pc(regs));
401 #ifdef CONFIG_PROC_FS
402 #include <linux/proc_fs.h>
403 #include <asm/uaccess.h>
404 #include <asm/ptrace.h>
406 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
407 int count, int *eof, void *data)
409 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
412 len += sprintf(page + len, "\n");
416 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
417 unsigned long count, void *data)
419 cpumask_t *mask = (cpumask_t *)data;
420 unsigned long full_count = count, err;
423 err = cpumask_parse_user(buffer, count, new_value);
431 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
433 struct proc_dir_entry *entry;
435 /* create /proc/irq/prof_cpu_mask */
436 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
439 entry->data = (void *)&prof_cpu_mask;
440 entry->read_proc = prof_cpu_mask_read_proc;
441 entry->write_proc = prof_cpu_mask_write_proc;
445 * This function accesses profiling information. The returned data is
446 * binary: the sampling step and the actual contents of the profile
447 * buffer. Use of the program readprofile is recommended in order to
448 * get meaningful info out of these data.
451 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
453 unsigned long p = *ppos;
456 unsigned int sample_step = 1 << prof_shift;
458 profile_flip_buffers();
459 if (p >= (prof_len+1)*sizeof(unsigned int))
461 if (count > (prof_len+1)*sizeof(unsigned int) - p)
462 count = (prof_len+1)*sizeof(unsigned int) - p;
465 while (p < sizeof(unsigned int) && count > 0) {
466 if (put_user(*((char *)(&sample_step)+p),buf))
468 buf++; p++; count--; read++;
470 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
471 if (copy_to_user(buf,(void *)pnt,count))
479 * Writing to /proc/profile resets the counters
481 * Writing a 'profiling multiplier' value into it also re-sets the profiling
482 * interrupt frequency, on architectures that support this.
484 static ssize_t write_profile(struct file *file, const char __user *buf,
485 size_t count, loff_t *ppos)
488 extern int setup_profiling_timer (unsigned int multiplier);
490 if (count == sizeof(int)) {
491 unsigned int multiplier;
493 if (copy_from_user(&multiplier, buf, sizeof(int)))
496 if (setup_profiling_timer(multiplier))
500 profile_discard_flip_buffers();
501 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
505 static struct file_operations proc_profile_operations = {
506 .read = read_profile,
507 .write = write_profile,
511 static void __init profile_nop(void *unused)
515 static int __init create_hash_tables(void)
519 for_each_online_cpu(cpu) {
520 int node = cpu_to_node(cpu);
523 page = alloc_pages_node(node,
524 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
528 per_cpu(cpu_profile_hits, cpu)[1]
529 = (struct profile_hit *)page_address(page);
530 page = alloc_pages_node(node,
531 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
535 per_cpu(cpu_profile_hits, cpu)[0]
536 = (struct profile_hit *)page_address(page);
542 on_each_cpu(profile_nop, NULL, 0, 1);
543 for_each_online_cpu(cpu) {
546 if (per_cpu(cpu_profile_hits, cpu)[0]) {
547 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
548 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
551 if (per_cpu(cpu_profile_hits, cpu)[1]) {
552 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
553 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
560 #define create_hash_tables() ({ 0; })
563 static int __init create_proc_profile(void)
565 struct proc_dir_entry *entry;
569 if (create_hash_tables())
571 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
573 entry->proc_fops = &proc_profile_operations;
574 entry->size = (1+prof_len) * sizeof(atomic_t);
575 hotcpu_notifier(profile_cpu_callback, 0);
578 module_init(create_proc_profile);
579 #endif /* CONFIG_PROC_FS */