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 static 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";
56 if (!strncmp(str, schedstr, strlen(schedstr))) {
57 prof_on = SCHED_PROFILING;
58 if (str[strlen(schedstr)] == ',')
59 str += strlen(schedstr) + 1;
60 if (get_option(&str, &par))
63 "kernel schedule profiling enabled (shift: %ld)\n",
65 } else if (get_option(&str, &par)) {
67 prof_on = CPU_PROFILING;
68 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
73 __setup("profile=", profile_setup);
76 void __init profile_init(void)
81 /* only text is profiled */
82 prof_len = (_etext - _stext) >> prof_shift;
83 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
86 /* Profile event notifications */
88 #ifdef CONFIG_PROFILING
90 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
91 static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
92 static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
94 void profile_task_exit(struct task_struct * task)
96 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
99 int profile_handoff_task(struct task_struct * task)
102 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
103 return (ret == NOTIFY_OK) ? 1 : 0;
106 void profile_munmap(unsigned long addr)
108 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
111 int task_handoff_register(struct notifier_block * n)
113 return atomic_notifier_chain_register(&task_free_notifier, n);
116 int task_handoff_unregister(struct notifier_block * n)
118 return atomic_notifier_chain_unregister(&task_free_notifier, n);
121 int profile_event_register(enum profile_type type, struct notifier_block * n)
126 case PROFILE_TASK_EXIT:
127 err = blocking_notifier_chain_register(
128 &task_exit_notifier, n);
131 err = blocking_notifier_chain_register(
132 &munmap_notifier, n);
140 int profile_event_unregister(enum profile_type type, struct notifier_block * n)
145 case PROFILE_TASK_EXIT:
146 err = blocking_notifier_chain_unregister(
147 &task_exit_notifier, n);
150 err = blocking_notifier_chain_unregister(
151 &munmap_notifier, n);
158 int register_timer_hook(int (*hook)(struct pt_regs *))
166 void unregister_timer_hook(int (*hook)(struct pt_regs *))
168 WARN_ON(hook != timer_hook);
170 /* make sure all CPUs see the NULL hook */
171 synchronize_sched(); /* Allow ongoing interrupts to complete. */
174 EXPORT_SYMBOL_GPL(register_timer_hook);
175 EXPORT_SYMBOL_GPL(unregister_timer_hook);
176 EXPORT_SYMBOL_GPL(task_handoff_register);
177 EXPORT_SYMBOL_GPL(task_handoff_unregister);
179 #endif /* CONFIG_PROFILING */
181 EXPORT_SYMBOL_GPL(profile_event_register);
182 EXPORT_SYMBOL_GPL(profile_event_unregister);
186 * Each cpu has a pair of open-addressed hashtables for pending
187 * profile hits. read_profile() IPI's all cpus to request them
188 * to flip buffers and flushes their contents to prof_buffer itself.
189 * Flip requests are serialized by the profile_flip_mutex. The sole
190 * use of having a second hashtable is for avoiding cacheline
191 * contention that would otherwise happen during flushes of pending
192 * profile hits required for the accuracy of reported profile hits
193 * and so resurrect the interrupt livelock issue.
195 * The open-addressed hashtables are indexed by profile buffer slot
196 * and hold the number of pending hits to that profile buffer slot on
197 * a cpu in an entry. When the hashtable overflows, all pending hits
198 * are accounted to their corresponding profile buffer slots with
199 * atomic_add() and the hashtable emptied. As numerous pending hits
200 * may be accounted to a profile buffer slot in a hashtable entry,
201 * this amortizes a number of atomic profile buffer increments likely
202 * to be far larger than the number of entries in the hashtable,
203 * particularly given that the number of distinct profile buffer
204 * positions to which hits are accounted during short intervals (e.g.
205 * several seconds) is usually very small. Exclusion from buffer
206 * flipping is provided by interrupt disablement (note that for
207 * SCHED_PROFILING profile_hit() may be called from process context).
208 * The hash function is meant to be lightweight as opposed to strong,
209 * and was vaguely inspired by ppc64 firmware-supported inverted
210 * pagetable hash functions, but uses a full hashtable full of finite
211 * collision chains, not just pairs of them.
215 static void __profile_flip_buffers(void *unused)
217 int cpu = smp_processor_id();
219 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
222 static void profile_flip_buffers(void)
226 mutex_lock(&profile_flip_mutex);
227 j = per_cpu(cpu_profile_flip, get_cpu());
229 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
230 for_each_online_cpu(cpu) {
231 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
232 for (i = 0; i < NR_PROFILE_HIT; ++i) {
238 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
239 hits[i].hits = hits[i].pc = 0;
242 mutex_unlock(&profile_flip_mutex);
245 static void profile_discard_flip_buffers(void)
249 mutex_lock(&profile_flip_mutex);
250 i = per_cpu(cpu_profile_flip, get_cpu());
252 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
253 for_each_online_cpu(cpu) {
254 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
255 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
257 mutex_unlock(&profile_flip_mutex);
260 void profile_hit(int type, void *__pc)
262 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
264 struct profile_hit *hits;
266 if (prof_on != type || !prof_buffer)
268 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
269 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
270 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
272 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
277 local_irq_save(flags);
279 for (j = 0; j < PROFILE_GRPSZ; ++j) {
280 if (hits[i + j].pc == pc) {
283 } else if (!hits[i + j].hits) {
285 hits[i + j].hits = 1;
289 i = (i + secondary) & (NR_PROFILE_HIT - 1);
290 } while (i != primary);
291 atomic_inc(&prof_buffer[pc]);
292 for (i = 0; i < NR_PROFILE_HIT; ++i) {
293 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
294 hits[i].pc = hits[i].hits = 0;
297 local_irq_restore(flags);
301 #ifdef CONFIG_HOTPLUG_CPU
302 static int __devinit profile_cpu_callback(struct notifier_block *info,
303 unsigned long action, void *__cpu)
305 int node, cpu = (unsigned long)__cpu;
310 node = cpu_to_node(cpu);
311 per_cpu(cpu_profile_flip, cpu) = 0;
312 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
313 page = alloc_pages_node(node,
314 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
318 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
320 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
321 page = alloc_pages_node(node,
322 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
326 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
330 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
331 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
335 cpu_set(cpu, prof_cpu_mask);
337 case CPU_UP_CANCELED:
339 cpu_clear(cpu, prof_cpu_mask);
340 if (per_cpu(cpu_profile_hits, cpu)[0]) {
341 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
342 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
345 if (per_cpu(cpu_profile_hits, cpu)[1]) {
346 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
347 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
354 #endif /* CONFIG_HOTPLUG_CPU */
355 #else /* !CONFIG_SMP */
356 #define profile_flip_buffers() do { } while (0)
357 #define profile_discard_flip_buffers() do { } while (0)
359 void profile_hit(int type, void *__pc)
363 if (prof_on != type || !prof_buffer)
365 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
366 atomic_inc(&prof_buffer[min(pc, prof_len - 1)]);
368 #endif /* !CONFIG_SMP */
370 void profile_tick(int type)
372 struct pt_regs *regs = get_irq_regs();
374 if (type == CPU_PROFILING && timer_hook)
376 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
377 profile_hit(type, (void *)profile_pc(regs));
380 #ifdef CONFIG_PROC_FS
381 #include <linux/proc_fs.h>
382 #include <asm/uaccess.h>
383 #include <asm/ptrace.h>
385 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
386 int count, int *eof, void *data)
388 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
391 len += sprintf(page + len, "\n");
395 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
396 unsigned long count, void *data)
398 cpumask_t *mask = (cpumask_t *)data;
399 unsigned long full_count = count, err;
402 err = cpumask_parse_user(buffer, count, new_value);
410 void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
412 struct proc_dir_entry *entry;
414 /* create /proc/irq/prof_cpu_mask */
415 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
418 entry->data = (void *)&prof_cpu_mask;
419 entry->read_proc = prof_cpu_mask_read_proc;
420 entry->write_proc = prof_cpu_mask_write_proc;
424 * This function accesses profiling information. The returned data is
425 * binary: the sampling step and the actual contents of the profile
426 * buffer. Use of the program readprofile is recommended in order to
427 * get meaningful info out of these data.
430 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
432 unsigned long p = *ppos;
435 unsigned int sample_step = 1 << prof_shift;
437 profile_flip_buffers();
438 if (p >= (prof_len+1)*sizeof(unsigned int))
440 if (count > (prof_len+1)*sizeof(unsigned int) - p)
441 count = (prof_len+1)*sizeof(unsigned int) - p;
444 while (p < sizeof(unsigned int) && count > 0) {
445 put_user(*((char *)(&sample_step)+p),buf);
446 buf++; p++; count--; read++;
448 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
449 if (copy_to_user(buf,(void *)pnt,count))
457 * Writing to /proc/profile resets the counters
459 * Writing a 'profiling multiplier' value into it also re-sets the profiling
460 * interrupt frequency, on architectures that support this.
462 static ssize_t write_profile(struct file *file, const char __user *buf,
463 size_t count, loff_t *ppos)
466 extern int setup_profiling_timer (unsigned int multiplier);
468 if (count == sizeof(int)) {
469 unsigned int multiplier;
471 if (copy_from_user(&multiplier, buf, sizeof(int)))
474 if (setup_profiling_timer(multiplier))
478 profile_discard_flip_buffers();
479 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
483 static struct file_operations proc_profile_operations = {
484 .read = read_profile,
485 .write = write_profile,
489 static void __init profile_nop(void *unused)
493 static int __init create_hash_tables(void)
497 for_each_online_cpu(cpu) {
498 int node = cpu_to_node(cpu);
501 page = alloc_pages_node(node,
502 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
506 per_cpu(cpu_profile_hits, cpu)[1]
507 = (struct profile_hit *)page_address(page);
508 page = alloc_pages_node(node,
509 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
513 per_cpu(cpu_profile_hits, cpu)[0]
514 = (struct profile_hit *)page_address(page);
520 on_each_cpu(profile_nop, NULL, 0, 1);
521 for_each_online_cpu(cpu) {
524 if (per_cpu(cpu_profile_hits, cpu)[0]) {
525 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
526 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
529 if (per_cpu(cpu_profile_hits, cpu)[1]) {
530 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
531 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
538 #define create_hash_tables() ({ 0; })
541 static int __init create_proc_profile(void)
543 struct proc_dir_entry *entry;
547 if (create_hash_tables())
549 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
551 entry->proc_fops = &proc_profile_operations;
552 entry->size = (1+prof_len) * sizeof(atomic_t);
553 hotcpu_notifier(profile_cpu_callback, 0);
556 module_init(create_proc_profile);
557 #endif /* CONFIG_PROC_FS */