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/config.h>
17 #include <linux/module.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/profile.h>
25 #include <linux/highmem.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 *);
40 static atomic_t *prof_buffer;
41 static unsigned long prof_len, prof_shift;
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 DECLARE_MUTEX(profile_flip_mutex);
48 #endif /* CONFIG_SMP */
50 static int __init profile_setup(char * str)
54 if (!strncmp(str, "schedule", 8)) {
55 prof_on = SCHED_PROFILING;
56 printk(KERN_INFO "kernel schedule profiling enabled\n");
60 if (get_option(&str,&par)) {
62 prof_on = CPU_PROFILING;
63 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
68 __setup("profile=", profile_setup);
71 void __init profile_init(void)
76 /* only text is profiled */
77 prof_len = (_etext - _stext) >> prof_shift;
78 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
81 /* Profile event notifications */
83 #ifdef CONFIG_PROFILING
85 static DECLARE_RWSEM(profile_rwsem);
86 static rwlock_t handoff_lock = RW_LOCK_UNLOCKED;
87 static struct notifier_block * task_exit_notifier;
88 static struct notifier_block * task_free_notifier;
89 static struct notifier_block * munmap_notifier;
91 void profile_task_exit(struct task_struct * task)
93 down_read(&profile_rwsem);
94 notifier_call_chain(&task_exit_notifier, 0, task);
95 up_read(&profile_rwsem);
98 int profile_handoff_task(struct task_struct * task)
101 read_lock(&handoff_lock);
102 ret = notifier_call_chain(&task_free_notifier, 0, task);
103 read_unlock(&handoff_lock);
104 return (ret == NOTIFY_OK) ? 1 : 0;
107 void profile_munmap(unsigned long addr)
109 down_read(&profile_rwsem);
110 notifier_call_chain(&munmap_notifier, 0, (void *)addr);
111 up_read(&profile_rwsem);
114 int task_handoff_register(struct notifier_block * n)
118 write_lock(&handoff_lock);
119 err = notifier_chain_register(&task_free_notifier, n);
120 write_unlock(&handoff_lock);
124 int task_handoff_unregister(struct notifier_block * n)
128 write_lock(&handoff_lock);
129 err = notifier_chain_unregister(&task_free_notifier, n);
130 write_unlock(&handoff_lock);
134 int profile_event_register(enum profile_type type, struct notifier_block * n)
138 down_write(&profile_rwsem);
141 case PROFILE_TASK_EXIT:
142 err = notifier_chain_register(&task_exit_notifier, n);
145 err = notifier_chain_register(&munmap_notifier, n);
149 up_write(&profile_rwsem);
155 int profile_event_unregister(enum profile_type type, struct notifier_block * n)
159 down_write(&profile_rwsem);
162 case PROFILE_TASK_EXIT:
163 err = notifier_chain_unregister(&task_exit_notifier, n);
166 err = notifier_chain_unregister(&munmap_notifier, n);
170 up_write(&profile_rwsem);
174 int register_timer_hook(int (*hook)(struct pt_regs *))
182 void unregister_timer_hook(int (*hook)(struct pt_regs *))
184 WARN_ON(hook != timer_hook);
186 /* make sure all CPUs see the NULL hook */
187 synchronize_kernel();
190 EXPORT_SYMBOL_GPL(register_timer_hook);
191 EXPORT_SYMBOL_GPL(unregister_timer_hook);
192 EXPORT_SYMBOL_GPL(task_handoff_register);
193 EXPORT_SYMBOL_GPL(task_handoff_unregister);
195 #endif /* CONFIG_PROFILING */
197 EXPORT_SYMBOL_GPL(profile_event_register);
198 EXPORT_SYMBOL_GPL(profile_event_unregister);
202 * Each cpu has a pair of open-addressed hashtables for pending
203 * profile hits. read_profile() IPI's all cpus to request them
204 * to flip buffers and flushes their contents to prof_buffer itself.
205 * Flip requests are serialized by the profile_flip_mutex. The sole
206 * use of having a second hashtable is for avoiding cacheline
207 * contention that would otherwise happen during flushes of pending
208 * profile hits required for the accuracy of reported profile hits
209 * and so resurrect the interrupt livelock issue.
211 * The open-addressed hashtables are indexed by profile buffer slot
212 * and hold the number of pending hits to that profile buffer slot on
213 * a cpu in an entry. When the hashtable overflows, all pending hits
214 * are accounted to their corresponding profile buffer slots with
215 * atomic_add() and the hashtable emptied. As numerous pending hits
216 * may be accounted to a profile buffer slot in a hashtable entry,
217 * this amortizes a number of atomic profile buffer increments likely
218 * to be far larger than the number of entries in the hashtable,
219 * particularly given that the number of distinct profile buffer
220 * positions to which hits are accounted during short intervals (e.g.
221 * several seconds) is usually very small. Exclusion from buffer
222 * flipping is provided by interrupt disablement (note that for
223 * SCHED_PROFILING profile_hit() may be called from process context).
224 * The hash function is meant to be lightweight as opposed to strong,
225 * and was vaguely inspired by ppc64 firmware-supported inverted
226 * pagetable hash functions, but uses a full hashtable full of finite
227 * collision chains, not just pairs of them.
231 static void __profile_flip_buffers(void *unused)
233 int cpu = smp_processor_id();
235 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
238 static void profile_flip_buffers(void)
242 down(&profile_flip_mutex);
243 j = per_cpu(cpu_profile_flip, get_cpu());
245 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
246 for_each_online_cpu(cpu) {
247 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
248 for (i = 0; i < NR_PROFILE_HIT; ++i) {
254 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
255 hits[i].hits = hits[i].pc = 0;
258 up(&profile_flip_mutex);
261 static void profile_discard_flip_buffers(void)
265 down(&profile_flip_mutex);
266 i = per_cpu(cpu_profile_flip, get_cpu());
268 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
269 for_each_online_cpu(cpu) {
270 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
271 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
273 up(&profile_flip_mutex);
276 void profile_hit(int type, void *__pc)
278 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
280 struct profile_hit *hits;
282 if (prof_on != type || !prof_buffer)
284 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
285 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
286 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
288 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
293 local_irq_save(flags);
295 for (j = 0; j < PROFILE_GRPSZ; ++j) {
296 if (hits[i + j].pc == pc) {
299 } else if (!hits[i + j].hits) {
301 hits[i + j].hits = 1;
305 i = (i + secondary) & (NR_PROFILE_HIT - 1);
306 } while (i != primary);
307 atomic_inc(&prof_buffer[pc]);
308 for (i = 0; i < NR_PROFILE_HIT; ++i) {
309 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
310 hits[i].pc = hits[i].hits = 0;
313 local_irq_restore(flags);
317 #ifdef CONFIG_HOTPLUG_CPU
318 static int __devinit profile_cpu_callback(struct notifier_block *info,
319 unsigned long action, void *__cpu)
321 int node, cpu = (unsigned long)__cpu;
326 node = cpu_to_node(cpu);
327 per_cpu(cpu_profile_flip, cpu) = 0;
328 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
329 page = alloc_pages_node(node, GFP_KERNEL, 0);
332 clear_highpage(page);
333 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
335 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
336 page = alloc_pages_node(node, GFP_KERNEL, 0);
339 clear_highpage(page);
340 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
344 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
345 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
349 cpu_set(cpu, prof_cpu_mask);
351 case CPU_UP_CANCELED:
353 cpu_clear(cpu, prof_cpu_mask);
354 if (per_cpu(cpu_profile_hits, cpu)[0]) {
355 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
356 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
359 if (per_cpu(cpu_profile_hits, cpu)[1]) {
360 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
361 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
368 #endif /* CONFIG_HOTPLUG_CPU */
369 #else /* !CONFIG_SMP */
370 #define profile_flip_buffers() do { } while (0)
371 #define profile_discard_flip_buffers() do { } while (0)
373 void profile_hit(int type, void *__pc)
377 if (prof_on != type || !prof_buffer)
379 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
380 atomic_inc(&prof_buffer[min(pc, prof_len - 1)]);
382 #endif /* !CONFIG_SMP */
384 void profile_tick(int type, struct pt_regs *regs)
386 if (type == CPU_PROFILING && timer_hook)
388 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
389 profile_hit(type, (void *)profile_pc(regs));
392 #ifdef CONFIG_PROC_FS
393 #include <linux/proc_fs.h>
394 #include <asm/uaccess.h>
395 #include <asm/ptrace.h>
397 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
398 int count, int *eof, void *data)
400 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
403 len += sprintf(page + len, "\n");
407 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
408 unsigned long count, void *data)
410 cpumask_t *mask = (cpumask_t *)data;
411 unsigned long full_count = count, err;
414 err = cpumask_parse(buffer, count, new_value);
422 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
424 struct proc_dir_entry *entry;
426 /* create /proc/irq/prof_cpu_mask */
427 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
430 entry->data = (void *)&prof_cpu_mask;
431 entry->read_proc = prof_cpu_mask_read_proc;
432 entry->write_proc = prof_cpu_mask_write_proc;
436 * This function accesses profiling information. The returned data is
437 * binary: the sampling step and the actual contents of the profile
438 * buffer. Use of the program readprofile is recommended in order to
439 * get meaningful info out of these data.
442 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
444 unsigned long p = *ppos;
447 unsigned int sample_step = 1 << prof_shift;
449 profile_flip_buffers();
450 if (p >= (prof_len+1)*sizeof(unsigned int))
452 if (count > (prof_len+1)*sizeof(unsigned int) - p)
453 count = (prof_len+1)*sizeof(unsigned int) - p;
456 while (p < sizeof(unsigned int) && count > 0) {
457 put_user(*((char *)(&sample_step)+p),buf);
458 buf++; p++; count--; read++;
460 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
461 if (copy_to_user(buf,(void *)pnt,count))
469 * Writing to /proc/profile resets the counters
471 * Writing a 'profiling multiplier' value into it also re-sets the profiling
472 * interrupt frequency, on architectures that support this.
474 static ssize_t write_profile(struct file *file, const char __user *buf,
475 size_t count, loff_t *ppos)
478 extern int setup_profiling_timer (unsigned int multiplier);
480 if (count == sizeof(int)) {
481 unsigned int multiplier;
483 if (copy_from_user(&multiplier, buf, sizeof(int)))
486 if (setup_profiling_timer(multiplier))
490 profile_discard_flip_buffers();
491 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
495 static struct file_operations proc_profile_operations = {
496 .read = read_profile,
497 .write = write_profile,
501 static void __init profile_nop(void *unused)
505 static int __init create_hash_tables(void)
509 for_each_online_cpu(cpu) {
510 int node = cpu_to_node(cpu);
513 page = alloc_pages_node(node, GFP_KERNEL, 0);
516 clear_highpage(page);
517 per_cpu(cpu_profile_hits, cpu)[1]
518 = (struct profile_hit *)page_address(page);
519 page = alloc_pages_node(node, GFP_KERNEL, 0);
522 clear_highpage(page);
523 per_cpu(cpu_profile_hits, cpu)[0]
524 = (struct profile_hit *)page_address(page);
530 on_each_cpu(profile_nop, NULL, 0, 1);
531 for_each_online_cpu(cpu) {
534 if (per_cpu(cpu_profile_hits, cpu)[0]) {
535 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
536 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
539 if (per_cpu(cpu_profile_hits, cpu)[1]) {
540 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
541 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
548 #define create_hash_tables() ({ 0; })
551 static int __init create_proc_profile(void)
553 struct proc_dir_entry *entry;
557 if (create_hash_tables())
559 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
561 entry->proc_fops = &proc_profile_operations;
562 entry->size = (1+prof_len) * sizeof(atomic_t);
563 hotcpu_notifier(profile_cpu_callback, 0);
566 module_init(create_proc_profile);
567 #endif /* CONFIG_PROC_FS */