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 * Nadia Yvette Chambers, Oracle, July 2004
12 * Amortized hit count accounting via per-cpu open-addressed hashtables
13 * to resolve timer interrupt livelocks, Nadia Yvette Chambers,
17 #include <linux/export.h>
18 #include <linux/profile.h>
19 #include <linux/bootmem.h>
20 #include <linux/notifier.h>
22 #include <linux/cpumask.h>
23 #include <linux/cpu.h>
24 #include <linux/highmem.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <asm/sections.h>
29 #include <asm/irq_regs.h>
30 #include <asm/ptrace.h>
35 #define PROFILE_GRPSHIFT 3
36 #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
37 #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
38 #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
40 /* Oprofile timer tick hook */
41 static int (*timer_hook)(struct pt_regs *) __read_mostly;
43 static atomic_t *prof_buffer;
44 static unsigned long prof_len, prof_shift;
46 int prof_on __read_mostly;
47 EXPORT_SYMBOL_GPL(prof_on);
49 static cpumask_var_t prof_cpu_mask;
51 static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
52 static DEFINE_PER_CPU(int, cpu_profile_flip);
53 static DEFINE_MUTEX(profile_flip_mutex);
54 #endif /* CONFIG_SMP */
56 int profile_setup(char *str)
58 static char schedstr[] = "schedule";
59 static char sleepstr[] = "sleep";
60 static char kvmstr[] = "kvm";
63 if (!strncmp(str, sleepstr, strlen(sleepstr))) {
64 #ifdef CONFIG_SCHEDSTATS
65 prof_on = SLEEP_PROFILING;
66 if (str[strlen(sleepstr)] == ',')
67 str += strlen(sleepstr) + 1;
68 if (get_option(&str, &par))
71 "kernel sleep profiling enabled (shift: %ld)\n",
75 "kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
76 #endif /* CONFIG_SCHEDSTATS */
77 } else if (!strncmp(str, schedstr, strlen(schedstr))) {
78 prof_on = SCHED_PROFILING;
79 if (str[strlen(schedstr)] == ',')
80 str += strlen(schedstr) + 1;
81 if (get_option(&str, &par))
84 "kernel schedule profiling enabled (shift: %ld)\n",
86 } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
87 prof_on = KVM_PROFILING;
88 if (str[strlen(kvmstr)] == ',')
89 str += strlen(kvmstr) + 1;
90 if (get_option(&str, &par))
93 "kernel KVM profiling enabled (shift: %ld)\n",
95 } else if (get_option(&str, &par)) {
97 prof_on = CPU_PROFILING;
98 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
103 __setup("profile=", profile_setup);
106 int __ref profile_init(void)
112 /* only text is profiled */
113 prof_len = (_etext - _stext) >> prof_shift;
114 buffer_bytes = prof_len*sizeof(atomic_t);
116 if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL))
119 cpumask_copy(prof_cpu_mask, cpu_possible_mask);
121 prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
125 prof_buffer = alloc_pages_exact(buffer_bytes,
126 GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
130 prof_buffer = vzalloc(buffer_bytes);
134 free_cpumask_var(prof_cpu_mask);
138 /* Profile event notifications */
140 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
141 static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
142 static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
144 void profile_task_exit(struct task_struct *task)
146 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
149 int profile_handoff_task(struct task_struct *task)
152 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
153 return (ret == NOTIFY_OK) ? 1 : 0;
156 void profile_munmap(unsigned long addr)
158 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
161 int task_handoff_register(struct notifier_block *n)
163 return atomic_notifier_chain_register(&task_free_notifier, n);
165 EXPORT_SYMBOL_GPL(task_handoff_register);
167 int task_handoff_unregister(struct notifier_block *n)
169 return atomic_notifier_chain_unregister(&task_free_notifier, n);
171 EXPORT_SYMBOL_GPL(task_handoff_unregister);
173 int profile_event_register(enum profile_type type, struct notifier_block *n)
178 case PROFILE_TASK_EXIT:
179 err = blocking_notifier_chain_register(
180 &task_exit_notifier, n);
183 err = blocking_notifier_chain_register(
184 &munmap_notifier, n);
190 EXPORT_SYMBOL_GPL(profile_event_register);
192 int profile_event_unregister(enum profile_type type, struct notifier_block *n)
197 case PROFILE_TASK_EXIT:
198 err = blocking_notifier_chain_unregister(
199 &task_exit_notifier, n);
202 err = blocking_notifier_chain_unregister(
203 &munmap_notifier, n);
209 EXPORT_SYMBOL_GPL(profile_event_unregister);
211 int register_timer_hook(int (*hook)(struct pt_regs *))
218 EXPORT_SYMBOL_GPL(register_timer_hook);
220 void unregister_timer_hook(int (*hook)(struct pt_regs *))
222 WARN_ON(hook != timer_hook);
224 /* make sure all CPUs see the NULL hook */
225 synchronize_sched(); /* Allow ongoing interrupts to complete. */
227 EXPORT_SYMBOL_GPL(unregister_timer_hook);
232 * Each cpu has a pair of open-addressed hashtables for pending
233 * profile hits. read_profile() IPI's all cpus to request them
234 * to flip buffers and flushes their contents to prof_buffer itself.
235 * Flip requests are serialized by the profile_flip_mutex. The sole
236 * use of having a second hashtable is for avoiding cacheline
237 * contention that would otherwise happen during flushes of pending
238 * profile hits required for the accuracy of reported profile hits
239 * and so resurrect the interrupt livelock issue.
241 * The open-addressed hashtables are indexed by profile buffer slot
242 * and hold the number of pending hits to that profile buffer slot on
243 * a cpu in an entry. When the hashtable overflows, all pending hits
244 * are accounted to their corresponding profile buffer slots with
245 * atomic_add() and the hashtable emptied. As numerous pending hits
246 * may be accounted to a profile buffer slot in a hashtable entry,
247 * this amortizes a number of atomic profile buffer increments likely
248 * to be far larger than the number of entries in the hashtable,
249 * particularly given that the number of distinct profile buffer
250 * positions to which hits are accounted during short intervals (e.g.
251 * several seconds) is usually very small. Exclusion from buffer
252 * flipping is provided by interrupt disablement (note that for
253 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
255 * The hash function is meant to be lightweight as opposed to strong,
256 * and was vaguely inspired by ppc64 firmware-supported inverted
257 * pagetable hash functions, but uses a full hashtable full of finite
258 * collision chains, not just pairs of them.
262 static void __profile_flip_buffers(void *unused)
264 int cpu = smp_processor_id();
266 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
269 static void profile_flip_buffers(void)
273 mutex_lock(&profile_flip_mutex);
274 j = per_cpu(cpu_profile_flip, get_cpu());
276 on_each_cpu(__profile_flip_buffers, NULL, 1);
277 for_each_online_cpu(cpu) {
278 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
279 for (i = 0; i < NR_PROFILE_HIT; ++i) {
285 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
286 hits[i].hits = hits[i].pc = 0;
289 mutex_unlock(&profile_flip_mutex);
292 static void profile_discard_flip_buffers(void)
296 mutex_lock(&profile_flip_mutex);
297 i = per_cpu(cpu_profile_flip, get_cpu());
299 on_each_cpu(__profile_flip_buffers, NULL, 1);
300 for_each_online_cpu(cpu) {
301 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
302 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
304 mutex_unlock(&profile_flip_mutex);
307 static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
309 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
311 struct profile_hit *hits;
313 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
314 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
315 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
317 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
323 * We buffer the global profiler buffer into a per-CPU
324 * queue and thus reduce the number of global (and possibly
325 * NUMA-alien) accesses. The write-queue is self-coalescing:
327 local_irq_save(flags);
329 for (j = 0; j < PROFILE_GRPSZ; ++j) {
330 if (hits[i + j].pc == pc) {
331 hits[i + j].hits += nr_hits;
333 } else if (!hits[i + j].hits) {
335 hits[i + j].hits = nr_hits;
339 i = (i + secondary) & (NR_PROFILE_HIT - 1);
340 } while (i != primary);
343 * Add the current hit(s) and flush the write-queue out
344 * to the global buffer:
346 atomic_add(nr_hits, &prof_buffer[pc]);
347 for (i = 0; i < NR_PROFILE_HIT; ++i) {
348 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
349 hits[i].pc = hits[i].hits = 0;
352 local_irq_restore(flags);
356 static int __cpuinit profile_cpu_callback(struct notifier_block *info,
357 unsigned long action, void *__cpu)
359 int node, cpu = (unsigned long)__cpu;
364 case CPU_UP_PREPARE_FROZEN:
365 node = cpu_to_mem(cpu);
366 per_cpu(cpu_profile_flip, cpu) = 0;
367 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
368 page = alloc_pages_exact_node(node,
369 GFP_KERNEL | __GFP_ZERO,
372 return notifier_from_errno(-ENOMEM);
373 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
375 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
376 page = alloc_pages_exact_node(node,
377 GFP_KERNEL | __GFP_ZERO,
381 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
385 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
386 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
388 return notifier_from_errno(-ENOMEM);
390 case CPU_ONLINE_FROZEN:
391 if (prof_cpu_mask != NULL)
392 cpumask_set_cpu(cpu, prof_cpu_mask);
394 case CPU_UP_CANCELED:
395 case CPU_UP_CANCELED_FROZEN:
397 case CPU_DEAD_FROZEN:
398 if (prof_cpu_mask != NULL)
399 cpumask_clear_cpu(cpu, prof_cpu_mask);
400 if (per_cpu(cpu_profile_hits, cpu)[0]) {
401 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
402 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
405 if (per_cpu(cpu_profile_hits, cpu)[1]) {
406 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
407 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
414 #else /* !CONFIG_SMP */
415 #define profile_flip_buffers() do { } while (0)
416 #define profile_discard_flip_buffers() do { } while (0)
417 #define profile_cpu_callback NULL
419 static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
422 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
423 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
425 #endif /* !CONFIG_SMP */
427 void profile_hits(int type, void *__pc, unsigned int nr_hits)
429 if (prof_on != type || !prof_buffer)
431 do_profile_hits(type, __pc, nr_hits);
433 EXPORT_SYMBOL_GPL(profile_hits);
435 void profile_tick(int type)
437 struct pt_regs *regs = get_irq_regs();
439 if (type == CPU_PROFILING && timer_hook)
441 if (!user_mode(regs) && prof_cpu_mask != NULL &&
442 cpumask_test_cpu(smp_processor_id(), prof_cpu_mask))
443 profile_hit(type, (void *)profile_pc(regs));
446 #ifdef CONFIG_PROC_FS
447 #include <linux/proc_fs.h>
448 #include <linux/seq_file.h>
449 #include <asm/uaccess.h>
451 static int prof_cpu_mask_proc_show(struct seq_file *m, void *v)
453 seq_cpumask(m, prof_cpu_mask);
458 static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file)
460 return single_open(file, prof_cpu_mask_proc_show, NULL);
463 static ssize_t prof_cpu_mask_proc_write(struct file *file,
464 const char __user *buffer, size_t count, loff_t *pos)
466 cpumask_var_t new_value;
469 if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
472 err = cpumask_parse_user(buffer, count, new_value);
474 cpumask_copy(prof_cpu_mask, new_value);
477 free_cpumask_var(new_value);
481 static const struct file_operations prof_cpu_mask_proc_fops = {
482 .open = prof_cpu_mask_proc_open,
485 .release = single_release,
486 .write = prof_cpu_mask_proc_write,
489 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
491 /* create /proc/irq/prof_cpu_mask */
492 proc_create("prof_cpu_mask", 0600, root_irq_dir, &prof_cpu_mask_proc_fops);
496 * This function accesses profiling information. The returned data is
497 * binary: the sampling step and the actual contents of the profile
498 * buffer. Use of the program readprofile is recommended in order to
499 * get meaningful info out of these data.
502 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
504 unsigned long p = *ppos;
507 unsigned int sample_step = 1 << prof_shift;
509 profile_flip_buffers();
510 if (p >= (prof_len+1)*sizeof(unsigned int))
512 if (count > (prof_len+1)*sizeof(unsigned int) - p)
513 count = (prof_len+1)*sizeof(unsigned int) - p;
516 while (p < sizeof(unsigned int) && count > 0) {
517 if (put_user(*((char *)(&sample_step)+p), buf))
519 buf++; p++; count--; read++;
521 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
522 if (copy_to_user(buf, (void *)pnt, count))
530 * Writing to /proc/profile resets the counters
532 * Writing a 'profiling multiplier' value into it also re-sets the profiling
533 * interrupt frequency, on architectures that support this.
535 static ssize_t write_profile(struct file *file, const char __user *buf,
536 size_t count, loff_t *ppos)
539 extern int setup_profiling_timer(unsigned int multiplier);
541 if (count == sizeof(int)) {
542 unsigned int multiplier;
544 if (copy_from_user(&multiplier, buf, sizeof(int)))
547 if (setup_profiling_timer(multiplier))
551 profile_discard_flip_buffers();
552 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
556 static const struct file_operations proc_profile_operations = {
557 .read = read_profile,
558 .write = write_profile,
559 .llseek = default_llseek,
563 static void profile_nop(void *unused)
567 static int create_hash_tables(void)
571 for_each_online_cpu(cpu) {
572 int node = cpu_to_mem(cpu);
575 page = alloc_pages_exact_node(node,
576 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
580 per_cpu(cpu_profile_hits, cpu)[1]
581 = (struct profile_hit *)page_address(page);
582 page = alloc_pages_exact_node(node,
583 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
587 per_cpu(cpu_profile_hits, cpu)[0]
588 = (struct profile_hit *)page_address(page);
594 on_each_cpu(profile_nop, NULL, 1);
595 for_each_online_cpu(cpu) {
598 if (per_cpu(cpu_profile_hits, cpu)[0]) {
599 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
600 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
603 if (per_cpu(cpu_profile_hits, cpu)[1]) {
604 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
605 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
612 #define create_hash_tables() ({ 0; })
615 int __ref create_proc_profile(void) /* false positive from hotcpu_notifier */
617 struct proc_dir_entry *entry;
621 if (create_hash_tables())
623 entry = proc_create("profile", S_IWUSR | S_IRUGO,
624 NULL, &proc_profile_operations);
627 entry->size = (1+prof_len) * sizeof(atomic_t);
628 hotcpu_notifier(profile_cpu_callback, 0);
631 module_init(create_proc_profile);
632 #endif /* CONFIG_PROC_FS */