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>
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 /* Oprofile timer tick hook */
38 int (*timer_hook)(struct pt_regs *) __read_mostly;
40 static atomic_t *prof_buffer;
41 static unsigned long prof_len, prof_shift;
42 static int prof_on __read_mostly;
43 static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
45 static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
46 static DEFINE_PER_CPU(int, cpu_profile_flip);
47 static DEFINE_MUTEX(profile_flip_mutex);
48 #endif /* CONFIG_SMP */
50 static int __init profile_setup(char * str)
52 static char __initdata schedstr[] = "schedule";
55 if (!strncmp(str, schedstr, strlen(schedstr))) {
56 prof_on = SCHED_PROFILING;
57 if (str[strlen(schedstr)] == ',')
58 str += strlen(schedstr) + 1;
59 if (get_option(&str, &par))
62 "kernel schedule profiling enabled (shift: %ld)\n",
64 } else if (get_option(&str, &par)) {
66 prof_on = CPU_PROFILING;
67 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
72 __setup("profile=", profile_setup);
75 void __init profile_init(void)
80 /* only text is profiled */
81 prof_len = (_etext - _stext) >> prof_shift;
82 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
85 /* Profile event notifications */
87 #ifdef CONFIG_PROFILING
89 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
90 static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
91 static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
93 void profile_task_exit(struct task_struct * task)
95 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
98 int profile_handoff_task(struct task_struct * task)
101 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
102 return (ret == NOTIFY_OK) ? 1 : 0;
105 void profile_munmap(unsigned long addr)
107 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
110 int task_handoff_register(struct notifier_block * n)
112 return atomic_notifier_chain_register(&task_free_notifier, n);
115 int task_handoff_unregister(struct notifier_block * n)
117 return atomic_notifier_chain_unregister(&task_free_notifier, n);
120 int profile_event_register(enum profile_type type, struct notifier_block * n)
125 case PROFILE_TASK_EXIT:
126 err = blocking_notifier_chain_register(
127 &task_exit_notifier, n);
130 err = blocking_notifier_chain_register(
131 &munmap_notifier, n);
139 int profile_event_unregister(enum profile_type type, struct notifier_block * n)
144 case PROFILE_TASK_EXIT:
145 err = blocking_notifier_chain_unregister(
146 &task_exit_notifier, n);
149 err = blocking_notifier_chain_unregister(
150 &munmap_notifier, n);
157 int register_timer_hook(int (*hook)(struct pt_regs *))
165 void unregister_timer_hook(int (*hook)(struct pt_regs *))
167 WARN_ON(hook != timer_hook);
169 /* make sure all CPUs see the NULL hook */
170 synchronize_sched(); /* Allow ongoing interrupts to complete. */
173 EXPORT_SYMBOL_GPL(register_timer_hook);
174 EXPORT_SYMBOL_GPL(unregister_timer_hook);
175 EXPORT_SYMBOL_GPL(task_handoff_register);
176 EXPORT_SYMBOL_GPL(task_handoff_unregister);
178 #endif /* CONFIG_PROFILING */
180 EXPORT_SYMBOL_GPL(profile_event_register);
181 EXPORT_SYMBOL_GPL(profile_event_unregister);
185 * Each cpu has a pair of open-addressed hashtables for pending
186 * profile hits. read_profile() IPI's all cpus to request them
187 * to flip buffers and flushes their contents to prof_buffer itself.
188 * Flip requests are serialized by the profile_flip_mutex. The sole
189 * use of having a second hashtable is for avoiding cacheline
190 * contention that would otherwise happen during flushes of pending
191 * profile hits required for the accuracy of reported profile hits
192 * and so resurrect the interrupt livelock issue.
194 * The open-addressed hashtables are indexed by profile buffer slot
195 * and hold the number of pending hits to that profile buffer slot on
196 * a cpu in an entry. When the hashtable overflows, all pending hits
197 * are accounted to their corresponding profile buffer slots with
198 * atomic_add() and the hashtable emptied. As numerous pending hits
199 * may be accounted to a profile buffer slot in a hashtable entry,
200 * this amortizes a number of atomic profile buffer increments likely
201 * to be far larger than the number of entries in the hashtable,
202 * particularly given that the number of distinct profile buffer
203 * positions to which hits are accounted during short intervals (e.g.
204 * several seconds) is usually very small. Exclusion from buffer
205 * flipping is provided by interrupt disablement (note that for
206 * SCHED_PROFILING profile_hit() may be called from process context).
207 * The hash function is meant to be lightweight as opposed to strong,
208 * and was vaguely inspired by ppc64 firmware-supported inverted
209 * pagetable hash functions, but uses a full hashtable full of finite
210 * collision chains, not just pairs of them.
214 static void __profile_flip_buffers(void *unused)
216 int cpu = smp_processor_id();
218 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
221 static void profile_flip_buffers(void)
225 mutex_lock(&profile_flip_mutex);
226 j = per_cpu(cpu_profile_flip, get_cpu());
228 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
229 for_each_online_cpu(cpu) {
230 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
231 for (i = 0; i < NR_PROFILE_HIT; ++i) {
237 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
238 hits[i].hits = hits[i].pc = 0;
241 mutex_unlock(&profile_flip_mutex);
244 static void profile_discard_flip_buffers(void)
248 mutex_lock(&profile_flip_mutex);
249 i = per_cpu(cpu_profile_flip, get_cpu());
251 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
252 for_each_online_cpu(cpu) {
253 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
254 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
256 mutex_unlock(&profile_flip_mutex);
259 void profile_hit(int type, void *__pc)
261 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
263 struct profile_hit *hits;
265 if (prof_on != type || !prof_buffer)
267 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
268 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
269 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
271 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
276 local_irq_save(flags);
278 for (j = 0; j < PROFILE_GRPSZ; ++j) {
279 if (hits[i + j].pc == pc) {
282 } else if (!hits[i + j].hits) {
284 hits[i + j].hits = 1;
288 i = (i + secondary) & (NR_PROFILE_HIT - 1);
289 } while (i != primary);
290 atomic_inc(&prof_buffer[pc]);
291 for (i = 0; i < NR_PROFILE_HIT; ++i) {
292 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
293 hits[i].pc = hits[i].hits = 0;
296 local_irq_restore(flags);
300 #ifdef CONFIG_HOTPLUG_CPU
301 static int __devinit profile_cpu_callback(struct notifier_block *info,
302 unsigned long action, void *__cpu)
304 int node, cpu = (unsigned long)__cpu;
309 node = cpu_to_node(cpu);
310 per_cpu(cpu_profile_flip, cpu) = 0;
311 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
312 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
315 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
317 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
318 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
321 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
325 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
326 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
330 cpu_set(cpu, prof_cpu_mask);
332 case CPU_UP_CANCELED:
334 cpu_clear(cpu, prof_cpu_mask);
335 if (per_cpu(cpu_profile_hits, cpu)[0]) {
336 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
337 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
340 if (per_cpu(cpu_profile_hits, cpu)[1]) {
341 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
342 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
349 #endif /* CONFIG_HOTPLUG_CPU */
350 #else /* !CONFIG_SMP */
351 #define profile_flip_buffers() do { } while (0)
352 #define profile_discard_flip_buffers() do { } while (0)
354 void profile_hit(int type, void *__pc)
358 if (prof_on != type || !prof_buffer)
360 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
361 atomic_inc(&prof_buffer[min(pc, prof_len - 1)]);
363 #endif /* !CONFIG_SMP */
365 void profile_tick(int type, struct pt_regs *regs)
367 if (type == CPU_PROFILING && timer_hook)
369 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
370 profile_hit(type, (void *)profile_pc(regs));
373 #ifdef CONFIG_PROC_FS
374 #include <linux/proc_fs.h>
375 #include <asm/uaccess.h>
376 #include <asm/ptrace.h>
378 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
379 int count, int *eof, void *data)
381 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
384 len += sprintf(page + len, "\n");
388 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
389 unsigned long count, void *data)
391 cpumask_t *mask = (cpumask_t *)data;
392 unsigned long full_count = count, err;
395 err = cpumask_parse(buffer, count, new_value);
403 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
405 struct proc_dir_entry *entry;
407 /* create /proc/irq/prof_cpu_mask */
408 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
411 entry->data = (void *)&prof_cpu_mask;
412 entry->read_proc = prof_cpu_mask_read_proc;
413 entry->write_proc = prof_cpu_mask_write_proc;
417 * This function accesses profiling information. The returned data is
418 * binary: the sampling step and the actual contents of the profile
419 * buffer. Use of the program readprofile is recommended in order to
420 * get meaningful info out of these data.
423 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
425 unsigned long p = *ppos;
428 unsigned int sample_step = 1 << prof_shift;
430 profile_flip_buffers();
431 if (p >= (prof_len+1)*sizeof(unsigned int))
433 if (count > (prof_len+1)*sizeof(unsigned int) - p)
434 count = (prof_len+1)*sizeof(unsigned int) - p;
437 while (p < sizeof(unsigned int) && count > 0) {
438 put_user(*((char *)(&sample_step)+p),buf);
439 buf++; p++; count--; read++;
441 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
442 if (copy_to_user(buf,(void *)pnt,count))
450 * Writing to /proc/profile resets the counters
452 * Writing a 'profiling multiplier' value into it also re-sets the profiling
453 * interrupt frequency, on architectures that support this.
455 static ssize_t write_profile(struct file *file, const char __user *buf,
456 size_t count, loff_t *ppos)
459 extern int setup_profiling_timer (unsigned int multiplier);
461 if (count == sizeof(int)) {
462 unsigned int multiplier;
464 if (copy_from_user(&multiplier, buf, sizeof(int)))
467 if (setup_profiling_timer(multiplier))
471 profile_discard_flip_buffers();
472 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
476 static struct file_operations proc_profile_operations = {
477 .read = read_profile,
478 .write = write_profile,
482 static void __init profile_nop(void *unused)
486 static int __init create_hash_tables(void)
490 for_each_online_cpu(cpu) {
491 int node = cpu_to_node(cpu);
494 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
497 per_cpu(cpu_profile_hits, cpu)[1]
498 = (struct profile_hit *)page_address(page);
499 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
502 per_cpu(cpu_profile_hits, cpu)[0]
503 = (struct profile_hit *)page_address(page);
509 on_each_cpu(profile_nop, NULL, 0, 1);
510 for_each_online_cpu(cpu) {
513 if (per_cpu(cpu_profile_hits, cpu)[0]) {
514 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
515 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
518 if (per_cpu(cpu_profile_hits, cpu)[1]) {
519 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
520 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
527 #define create_hash_tables() ({ 0; })
530 static int __init create_proc_profile(void)
532 struct proc_dir_entry *entry;
536 if (create_hash_tables())
538 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
540 entry->proc_fops = &proc_profile_operations;
541 entry->size = (1+prof_len) * sizeof(atomic_t);
542 hotcpu_notifier(profile_cpu_callback, 0);
545 module_init(create_proc_profile);
546 #endif /* CONFIG_PROC_FS */