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/highmem.h>
24 #include <linux/mutex.h>
25 #include <asm/sections.h>
26 #include <asm/semaphore.h>
27 #include <asm/irq_regs.h>
28 #include <asm/ptrace.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 static 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;
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";
61 if (!strncmp(str, sleepstr, strlen(sleepstr))) {
62 #ifdef CONFIG_SCHEDSTATS
63 prof_on = SLEEP_PROFILING;
64 if (str[strlen(sleepstr)] == ',')
65 str += strlen(sleepstr) + 1;
66 if (get_option(&str, &par))
69 "kernel sleep profiling enabled (shift: %ld)\n",
73 "kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
74 #endif /* CONFIG_SCHEDSTATS */
75 } else if (!strncmp(str, schedstr, strlen(schedstr))) {
76 prof_on = SCHED_PROFILING;
77 if (str[strlen(schedstr)] == ',')
78 str += strlen(schedstr) + 1;
79 if (get_option(&str, &par))
82 "kernel schedule profiling enabled (shift: %ld)\n",
84 } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
85 prof_on = KVM_PROFILING;
86 if (str[strlen(kvmstr)] == ',')
87 str += strlen(kvmstr) + 1;
88 if (get_option(&str, &par))
91 "kernel KVM profiling enabled (shift: %ld)\n",
93 } else if (get_option(&str, &par)) {
95 prof_on = CPU_PROFILING;
96 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
101 __setup("profile=", profile_setup);
104 void __init profile_init(void)
109 /* only text is profiled */
110 prof_len = (_etext - _stext) >> prof_shift;
111 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
114 /* Profile event notifications */
116 #ifdef CONFIG_PROFILING
118 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
119 static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
120 static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
122 void profile_task_exit(struct task_struct *task)
124 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
127 int profile_handoff_task(struct task_struct *task)
130 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
131 return (ret == NOTIFY_OK) ? 1 : 0;
134 void profile_munmap(unsigned long addr)
136 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
139 int task_handoff_register(struct notifier_block *n)
141 return atomic_notifier_chain_register(&task_free_notifier, n);
143 EXPORT_SYMBOL_GPL(task_handoff_register);
145 int task_handoff_unregister(struct notifier_block *n)
147 return atomic_notifier_chain_unregister(&task_free_notifier, n);
149 EXPORT_SYMBOL_GPL(task_handoff_unregister);
151 int profile_event_register(enum profile_type type, struct notifier_block *n)
156 case PROFILE_TASK_EXIT:
157 err = blocking_notifier_chain_register(
158 &task_exit_notifier, n);
161 err = blocking_notifier_chain_register(
162 &munmap_notifier, n);
168 EXPORT_SYMBOL_GPL(profile_event_register);
170 int profile_event_unregister(enum profile_type type, struct notifier_block *n)
175 case PROFILE_TASK_EXIT:
176 err = blocking_notifier_chain_unregister(
177 &task_exit_notifier, n);
180 err = blocking_notifier_chain_unregister(
181 &munmap_notifier, n);
187 EXPORT_SYMBOL_GPL(profile_event_unregister);
189 int register_timer_hook(int (*hook)(struct pt_regs *))
196 EXPORT_SYMBOL_GPL(register_timer_hook);
198 void unregister_timer_hook(int (*hook)(struct pt_regs *))
200 WARN_ON(hook != timer_hook);
202 /* make sure all CPUs see the NULL hook */
203 synchronize_sched(); /* Allow ongoing interrupts to complete. */
205 EXPORT_SYMBOL_GPL(unregister_timer_hook);
207 #endif /* CONFIG_PROFILING */
212 * Each cpu has a pair of open-addressed hashtables for pending
213 * profile hits. read_profile() IPI's all cpus to request them
214 * to flip buffers and flushes their contents to prof_buffer itself.
215 * Flip requests are serialized by the profile_flip_mutex. The sole
216 * use of having a second hashtable is for avoiding cacheline
217 * contention that would otherwise happen during flushes of pending
218 * profile hits required for the accuracy of reported profile hits
219 * and so resurrect the interrupt livelock issue.
221 * The open-addressed hashtables are indexed by profile buffer slot
222 * and hold the number of pending hits to that profile buffer slot on
223 * a cpu in an entry. When the hashtable overflows, all pending hits
224 * are accounted to their corresponding profile buffer slots with
225 * atomic_add() and the hashtable emptied. As numerous pending hits
226 * may be accounted to a profile buffer slot in a hashtable entry,
227 * this amortizes a number of atomic profile buffer increments likely
228 * to be far larger than the number of entries in the hashtable,
229 * particularly given that the number of distinct profile buffer
230 * positions to which hits are accounted during short intervals (e.g.
231 * several seconds) is usually very small. Exclusion from buffer
232 * flipping is provided by interrupt disablement (note that for
233 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
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.
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)
253 mutex_lock(&profile_flip_mutex);
254 j = per_cpu(cpu_profile_flip, get_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) {
265 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
266 hits[i].hits = hits[i].pc = 0;
269 mutex_unlock(&profile_flip_mutex);
272 static void profile_discard_flip_buffers(void)
276 mutex_lock(&profile_flip_mutex);
277 i = per_cpu(cpu_profile_flip, get_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 mutex_unlock(&profile_flip_mutex);
287 void profile_hits(int type, void *__pc, unsigned int nr_hits)
289 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
291 struct profile_hit *hits;
293 if (prof_on != type || !prof_buffer)
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;
299 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
305 * We buffer the global profiler buffer into a per-CPU
306 * queue and thus reduce the number of global (and possibly
307 * NUMA-alien) accesses. The write-queue is self-coalescing:
309 local_irq_save(flags);
311 for (j = 0; j < PROFILE_GRPSZ; ++j) {
312 if (hits[i + j].pc == pc) {
313 hits[i + j].hits += nr_hits;
315 } else if (!hits[i + j].hits) {
317 hits[i + j].hits = nr_hits;
321 i = (i + secondary) & (NR_PROFILE_HIT - 1);
322 } while (i != primary);
325 * Add the current hit(s) and flush the write-queue out
326 * to the global buffer:
328 atomic_add(nr_hits, &prof_buffer[pc]);
329 for (i = 0; i < NR_PROFILE_HIT; ++i) {
330 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
331 hits[i].pc = hits[i].hits = 0;
334 local_irq_restore(flags);
338 static int __devinit profile_cpu_callback(struct notifier_block *info,
339 unsigned long action, void *__cpu)
341 int node, cpu = (unsigned long)__cpu;
346 case CPU_UP_PREPARE_FROZEN:
347 node = cpu_to_node(cpu);
348 per_cpu(cpu_profile_flip, cpu) = 0;
349 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
350 page = alloc_pages_node(node,
351 GFP_KERNEL | __GFP_ZERO,
355 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
357 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
358 page = alloc_pages_node(node,
359 GFP_KERNEL | __GFP_ZERO,
363 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
367 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
368 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
372 case CPU_ONLINE_FROZEN:
373 cpu_set(cpu, prof_cpu_mask);
375 case CPU_UP_CANCELED:
376 case CPU_UP_CANCELED_FROZEN:
378 case CPU_DEAD_FROZEN:
379 cpu_clear(cpu, prof_cpu_mask);
380 if (per_cpu(cpu_profile_hits, cpu)[0]) {
381 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
382 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
385 if (per_cpu(cpu_profile_hits, cpu)[1]) {
386 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
387 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
394 #else /* !CONFIG_SMP */
395 #define profile_flip_buffers() do { } while (0)
396 #define profile_discard_flip_buffers() do { } while (0)
397 #define profile_cpu_callback NULL
399 void profile_hits(int type, void *__pc, unsigned int nr_hits)
403 if (prof_on != type || !prof_buffer)
405 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
406 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
408 #endif /* !CONFIG_SMP */
409 EXPORT_SYMBOL_GPL(profile_hits);
411 void profile_tick(int type)
413 struct pt_regs *regs = get_irq_regs();
415 if (type == CPU_PROFILING && timer_hook)
417 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
418 profile_hit(type, (void *)profile_pc(regs));
421 #ifdef CONFIG_PROC_FS
422 #include <linux/proc_fs.h>
423 #include <asm/uaccess.h>
424 #include <asm/ptrace.h>
426 static int prof_cpu_mask_read_proc(char *page, char **start, off_t off,
427 int count, int *eof, void *data)
429 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
432 len += sprintf(page + len, "\n");
436 static int prof_cpu_mask_write_proc(struct file *file,
437 const char __user *buffer, unsigned long count, void *data)
439 cpumask_t *mask = (cpumask_t *)data;
440 unsigned long full_count = count, err;
443 err = cpumask_parse_user(buffer, count, new_value);
451 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
453 struct proc_dir_entry *entry;
455 /* create /proc/irq/prof_cpu_mask */
456 entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
459 entry->data = (void *)&prof_cpu_mask;
460 entry->read_proc = prof_cpu_mask_read_proc;
461 entry->write_proc = prof_cpu_mask_write_proc;
465 * This function accesses profiling information. The returned data is
466 * binary: the sampling step and the actual contents of the profile
467 * buffer. Use of the program readprofile is recommended in order to
468 * get meaningful info out of these data.
471 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
473 unsigned long p = *ppos;
476 unsigned int sample_step = 1 << prof_shift;
478 profile_flip_buffers();
479 if (p >= (prof_len+1)*sizeof(unsigned int))
481 if (count > (prof_len+1)*sizeof(unsigned int) - p)
482 count = (prof_len+1)*sizeof(unsigned int) - p;
485 while (p < sizeof(unsigned int) && count > 0) {
486 if (put_user(*((char *)(&sample_step)+p), buf))
488 buf++; p++; count--; read++;
490 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
491 if (copy_to_user(buf, (void *)pnt, count))
499 * Writing to /proc/profile resets the counters
501 * Writing a 'profiling multiplier' value into it also re-sets the profiling
502 * interrupt frequency, on architectures that support this.
504 static ssize_t write_profile(struct file *file, const char __user *buf,
505 size_t count, loff_t *ppos)
508 extern int setup_profiling_timer(unsigned int multiplier);
510 if (count == sizeof(int)) {
511 unsigned int multiplier;
513 if (copy_from_user(&multiplier, buf, sizeof(int)))
516 if (setup_profiling_timer(multiplier))
520 profile_discard_flip_buffers();
521 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
525 static const struct file_operations proc_profile_operations = {
526 .read = read_profile,
527 .write = write_profile,
531 static void __init profile_nop(void *unused)
535 static int __init create_hash_tables(void)
539 for_each_online_cpu(cpu) {
540 int node = cpu_to_node(cpu);
543 page = alloc_pages_node(node,
544 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
548 per_cpu(cpu_profile_hits, cpu)[1]
549 = (struct profile_hit *)page_address(page);
550 page = alloc_pages_node(node,
551 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
555 per_cpu(cpu_profile_hits, cpu)[0]
556 = (struct profile_hit *)page_address(page);
562 on_each_cpu(profile_nop, NULL, 0, 1);
563 for_each_online_cpu(cpu) {
566 if (per_cpu(cpu_profile_hits, cpu)[0]) {
567 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
568 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
571 if (per_cpu(cpu_profile_hits, cpu)[1]) {
572 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
573 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
580 #define create_hash_tables() ({ 0; })
583 static int __init create_proc_profile(void)
585 struct proc_dir_entry *entry;
589 if (create_hash_tables())
591 entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL);
594 entry->proc_fops = &proc_profile_operations;
595 entry->size = (1+prof_len) * sizeof(atomic_t);
596 hotcpu_notifier(profile_cpu_callback, 0);
599 module_init(create_proc_profile);
600 #endif /* CONFIG_PROC_FS */