blob: 5a730fdb1a2cecf6b10c2112ba777fbb5fb7c794 [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
Alan Sterne041c682006-03-27 01:16:30 -080090static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
91static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
92static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94void profile_task_exit(struct task_struct * task)
95{
Alan Sterne041c682006-03-27 01:16:30 -080096 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99int profile_handoff_task(struct task_struct * task)
100{
101 int ret;
Alan Sterne041c682006-03-27 01:16:30 -0800102 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return (ret == NOTIFY_OK) ? 1 : 0;
104}
105
106void profile_munmap(unsigned long addr)
107{
Alan Sterne041c682006-03-27 01:16:30 -0800108 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111int task_handoff_register(struct notifier_block * n)
112{
Alan Sterne041c682006-03-27 01:16:30 -0800113 return atomic_notifier_chain_register(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116int task_handoff_unregister(struct notifier_block * n)
117{
Alan Sterne041c682006-03-27 01:16:30 -0800118 return atomic_notifier_chain_unregister(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121int profile_event_register(enum profile_type type, struct notifier_block * n)
122{
123 int err = -EINVAL;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 switch (type) {
126 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800127 err = blocking_notifier_chain_register(
128 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 break;
130 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800131 err = blocking_notifier_chain_register(
132 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 break;
134 }
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return err;
137}
138
139
140int profile_event_unregister(enum profile_type type, struct notifier_block * n)
141{
142 int err = -EINVAL;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 switch (type) {
145 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800146 err = blocking_notifier_chain_unregister(
147 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 break;
149 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800150 err = blocking_notifier_chain_unregister(
151 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 break;
153 }
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return err;
156}
157
158int register_timer_hook(int (*hook)(struct pt_regs *))
159{
160 if (timer_hook)
161 return -EBUSY;
162 timer_hook = hook;
163 return 0;
164}
165
166void unregister_timer_hook(int (*hook)(struct pt_regs *))
167{
168 WARN_ON(hook != timer_hook);
169 timer_hook = NULL;
170 /* make sure all CPUs see the NULL hook */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700171 synchronize_sched(); /* Allow ongoing interrupts to complete. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174EXPORT_SYMBOL_GPL(register_timer_hook);
175EXPORT_SYMBOL_GPL(unregister_timer_hook);
176EXPORT_SYMBOL_GPL(task_handoff_register);
177EXPORT_SYMBOL_GPL(task_handoff_unregister);
178
179#endif /* CONFIG_PROFILING */
180
181EXPORT_SYMBOL_GPL(profile_event_register);
182EXPORT_SYMBOL_GPL(profile_event_unregister);
183
184#ifdef CONFIG_SMP
185/*
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.
194 *
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.
212 *
213 * -- wli
214 */
215static void __profile_flip_buffers(void *unused)
216{
217 int cpu = smp_processor_id();
218
219 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
220}
221
222static void profile_flip_buffers(void)
223{
224 int i, j, cpu;
225
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800226 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 j = per_cpu(cpu_profile_flip, get_cpu());
228 put_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) {
233 if (!hits[i].hits) {
234 if (hits[i].pc)
235 hits[i].pc = 0;
236 continue;
237 }
238 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
239 hits[i].hits = hits[i].pc = 0;
240 }
241 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800242 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245static void profile_discard_flip_buffers(void)
246{
247 int i, cpu;
248
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800249 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 i = per_cpu(cpu_profile_flip, get_cpu());
251 put_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));
256 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800257 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260void profile_hit(int type, void *__pc)
261{
262 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
263 int i, j, cpu;
264 struct profile_hit *hits;
265
266 if (prof_on != type || !prof_buffer)
267 return;
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;
271 cpu = get_cpu();
272 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
273 if (!hits) {
274 put_cpu();
275 return;
276 }
277 local_irq_save(flags);
278 do {
279 for (j = 0; j < PROFILE_GRPSZ; ++j) {
280 if (hits[i + j].pc == pc) {
281 hits[i + j].hits++;
282 goto out;
283 } else if (!hits[i + j].hits) {
284 hits[i + j].pc = pc;
285 hits[i + j].hits = 1;
286 goto out;
287 }
288 }
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;
295 }
296out:
297 local_irq_restore(flags);
298 put_cpu();
299}
300
301#ifdef CONFIG_HOTPLUG_CPU
302static int __devinit profile_cpu_callback(struct notifier_block *info,
303 unsigned long action, void *__cpu)
304{
305 int node, cpu = (unsigned long)__cpu;
306 struct page *page;
307
308 switch (action) {
309 case CPU_UP_PREPARE:
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, GFP_KERNEL | __GFP_ZERO, 0);
314 if (!page)
315 return NOTIFY_BAD;
316 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
317 }
318 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
319 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
320 if (!page)
321 goto out_free;
322 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
323 }
324 break;
325 out_free:
326 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
327 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
328 __free_page(page);
329 return NOTIFY_BAD;
330 case CPU_ONLINE:
331 cpu_set(cpu, prof_cpu_mask);
332 break;
333 case CPU_UP_CANCELED:
334 case CPU_DEAD:
335 cpu_clear(cpu, prof_cpu_mask);
336 if (per_cpu(cpu_profile_hits, cpu)[0]) {
337 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
338 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
339 __free_page(page);
340 }
341 if (per_cpu(cpu_profile_hits, cpu)[1]) {
342 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
343 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
344 __free_page(page);
345 }
346 break;
347 }
348 return NOTIFY_OK;
349}
350#endif /* CONFIG_HOTPLUG_CPU */
351#else /* !CONFIG_SMP */
352#define profile_flip_buffers() do { } while (0)
353#define profile_discard_flip_buffers() do { } while (0)
354
355void profile_hit(int type, void *__pc)
356{
357 unsigned long pc;
358
359 if (prof_on != type || !prof_buffer)
360 return;
361 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
362 atomic_inc(&prof_buffer[min(pc, prof_len - 1)]);
363}
364#endif /* !CONFIG_SMP */
365
366void profile_tick(int type, struct pt_regs *regs)
367{
368 if (type == CPU_PROFILING && timer_hook)
369 timer_hook(regs);
370 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
371 profile_hit(type, (void *)profile_pc(regs));
372}
373
374#ifdef CONFIG_PROC_FS
375#include <linux/proc_fs.h>
376#include <asm/uaccess.h>
377#include <asm/ptrace.h>
378
379static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
380 int count, int *eof, void *data)
381{
382 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
383 if (count - len < 2)
384 return -EINVAL;
385 len += sprintf(page + len, "\n");
386 return len;
387}
388
389static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
390 unsigned long count, void *data)
391{
392 cpumask_t *mask = (cpumask_t *)data;
393 unsigned long full_count = count, err;
394 cpumask_t new_value;
395
396 err = cpumask_parse(buffer, count, new_value);
397 if (err)
398 return err;
399
400 *mask = new_value;
401 return full_count;
402}
403
404void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
405{
406 struct proc_dir_entry *entry;
407
408 /* create /proc/irq/prof_cpu_mask */
409 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
410 return;
411 entry->nlink = 1;
412 entry->data = (void *)&prof_cpu_mask;
413 entry->read_proc = prof_cpu_mask_read_proc;
414 entry->write_proc = prof_cpu_mask_write_proc;
415}
416
417/*
418 * This function accesses profiling information. The returned data is
419 * binary: the sampling step and the actual contents of the profile
420 * buffer. Use of the program readprofile is recommended in order to
421 * get meaningful info out of these data.
422 */
423static ssize_t
424read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
425{
426 unsigned long p = *ppos;
427 ssize_t read;
428 char * pnt;
429 unsigned int sample_step = 1 << prof_shift;
430
431 profile_flip_buffers();
432 if (p >= (prof_len+1)*sizeof(unsigned int))
433 return 0;
434 if (count > (prof_len+1)*sizeof(unsigned int) - p)
435 count = (prof_len+1)*sizeof(unsigned int) - p;
436 read = 0;
437
438 while (p < sizeof(unsigned int) && count > 0) {
439 put_user(*((char *)(&sample_step)+p),buf);
440 buf++; p++; count--; read++;
441 }
442 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
443 if (copy_to_user(buf,(void *)pnt,count))
444 return -EFAULT;
445 read += count;
446 *ppos += read;
447 return read;
448}
449
450/*
451 * Writing to /proc/profile resets the counters
452 *
453 * Writing a 'profiling multiplier' value into it also re-sets the profiling
454 * interrupt frequency, on architectures that support this.
455 */
456static ssize_t write_profile(struct file *file, const char __user *buf,
457 size_t count, loff_t *ppos)
458{
459#ifdef CONFIG_SMP
460 extern int setup_profiling_timer (unsigned int multiplier);
461
462 if (count == sizeof(int)) {
463 unsigned int multiplier;
464
465 if (copy_from_user(&multiplier, buf, sizeof(int)))
466 return -EFAULT;
467
468 if (setup_profiling_timer(multiplier))
469 return -EINVAL;
470 }
471#endif
472 profile_discard_flip_buffers();
473 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
474 return count;
475}
476
477static struct file_operations proc_profile_operations = {
478 .read = read_profile,
479 .write = write_profile,
480};
481
482#ifdef CONFIG_SMP
483static void __init profile_nop(void *unused)
484{
485}
486
487static int __init create_hash_tables(void)
488{
489 int cpu;
490
491 for_each_online_cpu(cpu) {
492 int node = cpu_to_node(cpu);
493 struct page *page;
494
495 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
496 if (!page)
497 goto out_cleanup;
498 per_cpu(cpu_profile_hits, cpu)[1]
499 = (struct profile_hit *)page_address(page);
500 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
501 if (!page)
502 goto out_cleanup;
503 per_cpu(cpu_profile_hits, cpu)[0]
504 = (struct profile_hit *)page_address(page);
505 }
506 return 0;
507out_cleanup:
508 prof_on = 0;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700509 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 on_each_cpu(profile_nop, NULL, 0, 1);
511 for_each_online_cpu(cpu) {
512 struct page *page;
513
514 if (per_cpu(cpu_profile_hits, cpu)[0]) {
515 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
516 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
517 __free_page(page);
518 }
519 if (per_cpu(cpu_profile_hits, cpu)[1]) {
520 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
521 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
522 __free_page(page);
523 }
524 }
525 return -1;
526}
527#else
528#define create_hash_tables() ({ 0; })
529#endif
530
531static int __init create_proc_profile(void)
532{
533 struct proc_dir_entry *entry;
534
535 if (!prof_on)
536 return 0;
537 if (create_hash_tables())
538 return -1;
539 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
540 return 0;
541 entry->proc_fops = &proc_profile_operations;
542 entry->size = (1+prof_len) * sizeof(atomic_t);
543 hotcpu_notifier(profile_cpu_callback, 0);
544 return 0;
545}
546module_init(create_proc_profile);
547#endif /* CONFIG_PROC_FS */