blob: ad81f799a9b4ad29fee8836e6ef0651e2dba0819 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
7 *
8 * Scheduler profiling support, Arjan van de Ven and Ingo Molnar,
9 * Red Hat, July 2004
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
14 */
15
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>
21#include <linux/mm.h>
22#include <linux/cpumask.h>
23#include <linux/cpu.h>
24#include <linux/profile.h>
25#include <linux/highmem.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/sections.h>
28#include <asm/semaphore.h>
29
30struct profile_hit {
31 u32 pc, hits;
32};
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)
37
38/* Oprofile timer tick hook */
Christoph Lameter6c036522005-07-07 17:56:59 -070039int (*timer_hook)(struct pt_regs *) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static atomic_t *prof_buffer;
42static unsigned long prof_len, prof_shift;
Christoph Lameter6c036522005-07-07 17:56:59 -070043static int prof_on __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
45#ifdef CONFIG_SMP
46static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
47static DEFINE_PER_CPU(int, cpu_profile_flip);
Arjan van de Ven97d1f152006-03-23 03:00:24 -080048static DEFINE_MUTEX(profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#endif /* CONFIG_SMP */
50
51static int __init profile_setup(char * str)
52{
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070053 static char __initdata schedstr[] = "schedule";
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int par;
55
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070056 if (!strncmp(str, schedstr, strlen(schedstr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 prof_on = SCHED_PROFILING;
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070058 if (str[strlen(schedstr)] == ',')
59 str += strlen(schedstr) + 1;
60 if (get_option(&str, &par))
61 prof_shift = par;
62 printk(KERN_INFO
63 "kernel schedule profiling enabled (shift: %ld)\n",
64 prof_shift);
65 } else if (get_option(&str, &par)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 prof_shift = par;
67 prof_on = CPU_PROFILING;
68 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
69 prof_shift);
70 }
71 return 1;
72}
73__setup("profile=", profile_setup);
74
75
76void __init profile_init(void)
77{
78 if (!prof_on)
79 return;
80
81 /* only text is profiled */
82 prof_len = (_etext - _stext) >> prof_shift;
83 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
84}
85
86/* Profile event notifications */
87
88#ifdef CONFIG_PROFILING
89
90static DECLARE_RWSEM(profile_rwsem);
91static DEFINE_RWLOCK(handoff_lock);
92static struct notifier_block * task_exit_notifier;
93static struct notifier_block * task_free_notifier;
94static struct notifier_block * munmap_notifier;
95
96void profile_task_exit(struct task_struct * task)
97{
98 down_read(&profile_rwsem);
99 notifier_call_chain(&task_exit_notifier, 0, task);
100 up_read(&profile_rwsem);
101}
102
103int profile_handoff_task(struct task_struct * task)
104{
105 int ret;
106 read_lock(&handoff_lock);
107 ret = notifier_call_chain(&task_free_notifier, 0, task);
108 read_unlock(&handoff_lock);
109 return (ret == NOTIFY_OK) ? 1 : 0;
110}
111
112void profile_munmap(unsigned long addr)
113{
114 down_read(&profile_rwsem);
115 notifier_call_chain(&munmap_notifier, 0, (void *)addr);
116 up_read(&profile_rwsem);
117}
118
119int task_handoff_register(struct notifier_block * n)
120{
121 int err = -EINVAL;
122
123 write_lock(&handoff_lock);
124 err = notifier_chain_register(&task_free_notifier, n);
125 write_unlock(&handoff_lock);
126 return err;
127}
128
129int task_handoff_unregister(struct notifier_block * n)
130{
131 int err = -EINVAL;
132
133 write_lock(&handoff_lock);
134 err = notifier_chain_unregister(&task_free_notifier, n);
135 write_unlock(&handoff_lock);
136 return err;
137}
138
139int profile_event_register(enum profile_type type, struct notifier_block * n)
140{
141 int err = -EINVAL;
142
143 down_write(&profile_rwsem);
144
145 switch (type) {
146 case PROFILE_TASK_EXIT:
147 err = notifier_chain_register(&task_exit_notifier, n);
148 break;
149 case PROFILE_MUNMAP:
150 err = notifier_chain_register(&munmap_notifier, n);
151 break;
152 }
153
154 up_write(&profile_rwsem);
155
156 return err;
157}
158
159
160int profile_event_unregister(enum profile_type type, struct notifier_block * n)
161{
162 int err = -EINVAL;
163
164 down_write(&profile_rwsem);
165
166 switch (type) {
167 case PROFILE_TASK_EXIT:
168 err = notifier_chain_unregister(&task_exit_notifier, n);
169 break;
170 case PROFILE_MUNMAP:
171 err = notifier_chain_unregister(&munmap_notifier, n);
172 break;
173 }
174
175 up_write(&profile_rwsem);
176 return err;
177}
178
179int register_timer_hook(int (*hook)(struct pt_regs *))
180{
181 if (timer_hook)
182 return -EBUSY;
183 timer_hook = hook;
184 return 0;
185}
186
187void unregister_timer_hook(int (*hook)(struct pt_regs *))
188{
189 WARN_ON(hook != timer_hook);
190 timer_hook = NULL;
191 /* make sure all CPUs see the NULL hook */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700192 synchronize_sched(); /* Allow ongoing interrupts to complete. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195EXPORT_SYMBOL_GPL(register_timer_hook);
196EXPORT_SYMBOL_GPL(unregister_timer_hook);
197EXPORT_SYMBOL_GPL(task_handoff_register);
198EXPORT_SYMBOL_GPL(task_handoff_unregister);
199
200#endif /* CONFIG_PROFILING */
201
202EXPORT_SYMBOL_GPL(profile_event_register);
203EXPORT_SYMBOL_GPL(profile_event_unregister);
204
205#ifdef CONFIG_SMP
206/*
207 * Each cpu has a pair of open-addressed hashtables for pending
208 * profile hits. read_profile() IPI's all cpus to request them
209 * to flip buffers and flushes their contents to prof_buffer itself.
210 * Flip requests are serialized by the profile_flip_mutex. The sole
211 * use of having a second hashtable is for avoiding cacheline
212 * contention that would otherwise happen during flushes of pending
213 * profile hits required for the accuracy of reported profile hits
214 * and so resurrect the interrupt livelock issue.
215 *
216 * The open-addressed hashtables are indexed by profile buffer slot
217 * and hold the number of pending hits to that profile buffer slot on
218 * a cpu in an entry. When the hashtable overflows, all pending hits
219 * are accounted to their corresponding profile buffer slots with
220 * atomic_add() and the hashtable emptied. As numerous pending hits
221 * may be accounted to a profile buffer slot in a hashtable entry,
222 * this amortizes a number of atomic profile buffer increments likely
223 * to be far larger than the number of entries in the hashtable,
224 * particularly given that the number of distinct profile buffer
225 * positions to which hits are accounted during short intervals (e.g.
226 * several seconds) is usually very small. Exclusion from buffer
227 * flipping is provided by interrupt disablement (note that for
228 * SCHED_PROFILING profile_hit() may be called from process context).
229 * The hash function is meant to be lightweight as opposed to strong,
230 * and was vaguely inspired by ppc64 firmware-supported inverted
231 * pagetable hash functions, but uses a full hashtable full of finite
232 * collision chains, not just pairs of them.
233 *
234 * -- wli
235 */
236static void __profile_flip_buffers(void *unused)
237{
238 int cpu = smp_processor_id();
239
240 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
241}
242
243static void profile_flip_buffers(void)
244{
245 int i, j, cpu;
246
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800247 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 j = per_cpu(cpu_profile_flip, get_cpu());
249 put_cpu();
250 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
251 for_each_online_cpu(cpu) {
252 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
253 for (i = 0; i < NR_PROFILE_HIT; ++i) {
254 if (!hits[i].hits) {
255 if (hits[i].pc)
256 hits[i].pc = 0;
257 continue;
258 }
259 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
260 hits[i].hits = hits[i].pc = 0;
261 }
262 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800263 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static void profile_discard_flip_buffers(void)
267{
268 int i, cpu;
269
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800270 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 i = per_cpu(cpu_profile_flip, get_cpu());
272 put_cpu();
273 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
274 for_each_online_cpu(cpu) {
275 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
276 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
277 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800278 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281void profile_hit(int type, void *__pc)
282{
283 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
284 int i, j, cpu;
285 struct profile_hit *hits;
286
287 if (prof_on != type || !prof_buffer)
288 return;
289 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
290 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
291 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
292 cpu = get_cpu();
293 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
294 if (!hits) {
295 put_cpu();
296 return;
297 }
298 local_irq_save(flags);
299 do {
300 for (j = 0; j < PROFILE_GRPSZ; ++j) {
301 if (hits[i + j].pc == pc) {
302 hits[i + j].hits++;
303 goto out;
304 } else if (!hits[i + j].hits) {
305 hits[i + j].pc = pc;
306 hits[i + j].hits = 1;
307 goto out;
308 }
309 }
310 i = (i + secondary) & (NR_PROFILE_HIT - 1);
311 } while (i != primary);
312 atomic_inc(&prof_buffer[pc]);
313 for (i = 0; i < NR_PROFILE_HIT; ++i) {
314 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
315 hits[i].pc = hits[i].hits = 0;
316 }
317out:
318 local_irq_restore(flags);
319 put_cpu();
320}
321
322#ifdef CONFIG_HOTPLUG_CPU
323static int __devinit profile_cpu_callback(struct notifier_block *info,
324 unsigned long action, void *__cpu)
325{
326 int node, cpu = (unsigned long)__cpu;
327 struct page *page;
328
329 switch (action) {
330 case CPU_UP_PREPARE:
331 node = cpu_to_node(cpu);
332 per_cpu(cpu_profile_flip, cpu) = 0;
333 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
334 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
335 if (!page)
336 return NOTIFY_BAD;
337 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
338 }
339 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
340 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
341 if (!page)
342 goto out_free;
343 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
344 }
345 break;
346 out_free:
347 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
348 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
349 __free_page(page);
350 return NOTIFY_BAD;
351 case CPU_ONLINE:
352 cpu_set(cpu, prof_cpu_mask);
353 break;
354 case CPU_UP_CANCELED:
355 case CPU_DEAD:
356 cpu_clear(cpu, prof_cpu_mask);
357 if (per_cpu(cpu_profile_hits, cpu)[0]) {
358 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
359 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
360 __free_page(page);
361 }
362 if (per_cpu(cpu_profile_hits, cpu)[1]) {
363 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
364 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
365 __free_page(page);
366 }
367 break;
368 }
369 return NOTIFY_OK;
370}
371#endif /* CONFIG_HOTPLUG_CPU */
372#else /* !CONFIG_SMP */
373#define profile_flip_buffers() do { } while (0)
374#define profile_discard_flip_buffers() do { } while (0)
375
376void profile_hit(int type, void *__pc)
377{
378 unsigned long pc;
379
380 if (prof_on != type || !prof_buffer)
381 return;
382 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
383 atomic_inc(&prof_buffer[min(pc, prof_len - 1)]);
384}
385#endif /* !CONFIG_SMP */
386
387void profile_tick(int type, struct pt_regs *regs)
388{
389 if (type == CPU_PROFILING && timer_hook)
390 timer_hook(regs);
391 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
392 profile_hit(type, (void *)profile_pc(regs));
393}
394
395#ifdef CONFIG_PROC_FS
396#include <linux/proc_fs.h>
397#include <asm/uaccess.h>
398#include <asm/ptrace.h>
399
400static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
401 int count, int *eof, void *data)
402{
403 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
404 if (count - len < 2)
405 return -EINVAL;
406 len += sprintf(page + len, "\n");
407 return len;
408}
409
410static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
411 unsigned long count, void *data)
412{
413 cpumask_t *mask = (cpumask_t *)data;
414 unsigned long full_count = count, err;
415 cpumask_t new_value;
416
417 err = cpumask_parse(buffer, count, new_value);
418 if (err)
419 return err;
420
421 *mask = new_value;
422 return full_count;
423}
424
425void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
426{
427 struct proc_dir_entry *entry;
428
429 /* create /proc/irq/prof_cpu_mask */
430 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
431 return;
432 entry->nlink = 1;
433 entry->data = (void *)&prof_cpu_mask;
434 entry->read_proc = prof_cpu_mask_read_proc;
435 entry->write_proc = prof_cpu_mask_write_proc;
436}
437
438/*
439 * This function accesses profiling information. The returned data is
440 * binary: the sampling step and the actual contents of the profile
441 * buffer. Use of the program readprofile is recommended in order to
442 * get meaningful info out of these data.
443 */
444static ssize_t
445read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
446{
447 unsigned long p = *ppos;
448 ssize_t read;
449 char * pnt;
450 unsigned int sample_step = 1 << prof_shift;
451
452 profile_flip_buffers();
453 if (p >= (prof_len+1)*sizeof(unsigned int))
454 return 0;
455 if (count > (prof_len+1)*sizeof(unsigned int) - p)
456 count = (prof_len+1)*sizeof(unsigned int) - p;
457 read = 0;
458
459 while (p < sizeof(unsigned int) && count > 0) {
460 put_user(*((char *)(&sample_step)+p),buf);
461 buf++; p++; count--; read++;
462 }
463 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
464 if (copy_to_user(buf,(void *)pnt,count))
465 return -EFAULT;
466 read += count;
467 *ppos += read;
468 return read;
469}
470
471/*
472 * Writing to /proc/profile resets the counters
473 *
474 * Writing a 'profiling multiplier' value into it also re-sets the profiling
475 * interrupt frequency, on architectures that support this.
476 */
477static ssize_t write_profile(struct file *file, const char __user *buf,
478 size_t count, loff_t *ppos)
479{
480#ifdef CONFIG_SMP
481 extern int setup_profiling_timer (unsigned int multiplier);
482
483 if (count == sizeof(int)) {
484 unsigned int multiplier;
485
486 if (copy_from_user(&multiplier, buf, sizeof(int)))
487 return -EFAULT;
488
489 if (setup_profiling_timer(multiplier))
490 return -EINVAL;
491 }
492#endif
493 profile_discard_flip_buffers();
494 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
495 return count;
496}
497
498static struct file_operations proc_profile_operations = {
499 .read = read_profile,
500 .write = write_profile,
501};
502
503#ifdef CONFIG_SMP
504static void __init profile_nop(void *unused)
505{
506}
507
508static int __init create_hash_tables(void)
509{
510 int cpu;
511
512 for_each_online_cpu(cpu) {
513 int node = cpu_to_node(cpu);
514 struct page *page;
515
516 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
517 if (!page)
518 goto out_cleanup;
519 per_cpu(cpu_profile_hits, cpu)[1]
520 = (struct profile_hit *)page_address(page);
521 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
522 if (!page)
523 goto out_cleanup;
524 per_cpu(cpu_profile_hits, cpu)[0]
525 = (struct profile_hit *)page_address(page);
526 }
527 return 0;
528out_cleanup:
529 prof_on = 0;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700530 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 on_each_cpu(profile_nop, NULL, 0, 1);
532 for_each_online_cpu(cpu) {
533 struct page *page;
534
535 if (per_cpu(cpu_profile_hits, cpu)[0]) {
536 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
537 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
538 __free_page(page);
539 }
540 if (per_cpu(cpu_profile_hits, cpu)[1]) {
541 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
542 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
543 __free_page(page);
544 }
545 }
546 return -1;
547}
548#else
549#define create_hash_tables() ({ 0; })
550#endif
551
552static int __init create_proc_profile(void)
553{
554 struct proc_dir_entry *entry;
555
556 if (!prof_on)
557 return 0;
558 if (create_hash_tables())
559 return -1;
560 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
561 return 0;
562 entry->proc_fops = &proc_profile_operations;
563 entry->size = (1+prof_len) * sizeof(atomic_t);
564 hotcpu_notifier(profile_cpu_callback, 0);
565 return 0;
566}
567module_init(create_proc_profile);
568#endif /* CONFIG_PROC_FS */