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 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";
54 static char __initdata sleepstr
[] = "sleep";
57 if (!strncmp(str
, sleepstr
, strlen(sleepstr
))) {
58 prof_on
= SLEEP_PROFILING
;
59 if (str
[strlen(sleepstr
)] == ',')
60 str
+= strlen(sleepstr
) + 1;
61 if (get_option(&str
, &par
))
64 "kernel sleep profiling enabled (shift: %ld)\n",
66 } else if (!strncmp(str
, sleepstr
, strlen(sleepstr
))) {
67 prof_on
= SCHED_PROFILING
;
68 if (str
[strlen(schedstr
)] == ',')
69 str
+= strlen(schedstr
) + 1;
70 if (get_option(&str
, &par
))
73 "kernel schedule profiling enabled (shift: %ld)\n",
75 } else if (get_option(&str
, &par
)) {
77 prof_on
= CPU_PROFILING
;
78 printk(KERN_INFO
"kernel profiling enabled (shift: %ld)\n",
83 __setup("profile=", profile_setup
);
86 void __init
profile_init(void)
91 /* only text is profiled */
92 prof_len
= (_etext
- _stext
) >> prof_shift
;
93 prof_buffer
= alloc_bootmem(prof_len
*sizeof(atomic_t
));
96 /* Profile event notifications */
98 #ifdef CONFIG_PROFILING
100 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier
);
101 static ATOMIC_NOTIFIER_HEAD(task_free_notifier
);
102 static BLOCKING_NOTIFIER_HEAD(munmap_notifier
);
104 void profile_task_exit(struct task_struct
* task
)
106 blocking_notifier_call_chain(&task_exit_notifier
, 0, task
);
109 int profile_handoff_task(struct task_struct
* task
)
112 ret
= atomic_notifier_call_chain(&task_free_notifier
, 0, task
);
113 return (ret
== NOTIFY_OK
) ? 1 : 0;
116 void profile_munmap(unsigned long addr
)
118 blocking_notifier_call_chain(&munmap_notifier
, 0, (void *)addr
);
121 int task_handoff_register(struct notifier_block
* n
)
123 return atomic_notifier_chain_register(&task_free_notifier
, n
);
126 int task_handoff_unregister(struct notifier_block
* n
)
128 return atomic_notifier_chain_unregister(&task_free_notifier
, n
);
131 int profile_event_register(enum profile_type type
, struct notifier_block
* n
)
136 case PROFILE_TASK_EXIT
:
137 err
= blocking_notifier_chain_register(
138 &task_exit_notifier
, n
);
141 err
= blocking_notifier_chain_register(
142 &munmap_notifier
, n
);
150 int profile_event_unregister(enum profile_type type
, struct notifier_block
* n
)
155 case PROFILE_TASK_EXIT
:
156 err
= blocking_notifier_chain_unregister(
157 &task_exit_notifier
, n
);
160 err
= blocking_notifier_chain_unregister(
161 &munmap_notifier
, n
);
168 int register_timer_hook(int (*hook
)(struct pt_regs
*))
176 void unregister_timer_hook(int (*hook
)(struct pt_regs
*))
178 WARN_ON(hook
!= timer_hook
);
180 /* make sure all CPUs see the NULL hook */
181 synchronize_sched(); /* Allow ongoing interrupts to complete. */
184 EXPORT_SYMBOL_GPL(register_timer_hook
);
185 EXPORT_SYMBOL_GPL(unregister_timer_hook
);
186 EXPORT_SYMBOL_GPL(task_handoff_register
);
187 EXPORT_SYMBOL_GPL(task_handoff_unregister
);
189 #endif /* CONFIG_PROFILING */
191 EXPORT_SYMBOL_GPL(profile_event_register
);
192 EXPORT_SYMBOL_GPL(profile_event_unregister
);
196 * Each cpu has a pair of open-addressed hashtables for pending
197 * profile hits. read_profile() IPI's all cpus to request them
198 * to flip buffers and flushes their contents to prof_buffer itself.
199 * Flip requests are serialized by the profile_flip_mutex. The sole
200 * use of having a second hashtable is for avoiding cacheline
201 * contention that would otherwise happen during flushes of pending
202 * profile hits required for the accuracy of reported profile hits
203 * and so resurrect the interrupt livelock issue.
205 * The open-addressed hashtables are indexed by profile buffer slot
206 * and hold the number of pending hits to that profile buffer slot on
207 * a cpu in an entry. When the hashtable overflows, all pending hits
208 * are accounted to their corresponding profile buffer slots with
209 * atomic_add() and the hashtable emptied. As numerous pending hits
210 * may be accounted to a profile buffer slot in a hashtable entry,
211 * this amortizes a number of atomic profile buffer increments likely
212 * to be far larger than the number of entries in the hashtable,
213 * particularly given that the number of distinct profile buffer
214 * positions to which hits are accounted during short intervals (e.g.
215 * several seconds) is usually very small. Exclusion from buffer
216 * flipping is provided by interrupt disablement (note that for
217 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
219 * The hash function is meant to be lightweight as opposed to strong,
220 * and was vaguely inspired by ppc64 firmware-supported inverted
221 * pagetable hash functions, but uses a full hashtable full of finite
222 * collision chains, not just pairs of them.
226 static void __profile_flip_buffers(void *unused
)
228 int cpu
= smp_processor_id();
230 per_cpu(cpu_profile_flip
, cpu
) = !per_cpu(cpu_profile_flip
, cpu
);
233 static void profile_flip_buffers(void)
237 mutex_lock(&profile_flip_mutex
);
238 j
= per_cpu(cpu_profile_flip
, get_cpu());
240 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
241 for_each_online_cpu(cpu
) {
242 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[j
];
243 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
249 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
250 hits
[i
].hits
= hits
[i
].pc
= 0;
253 mutex_unlock(&profile_flip_mutex
);
256 static void profile_discard_flip_buffers(void)
260 mutex_lock(&profile_flip_mutex
);
261 i
= per_cpu(cpu_profile_flip
, get_cpu());
263 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
264 for_each_online_cpu(cpu
) {
265 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[i
];
266 memset(hits
, 0, NR_PROFILE_HIT
*sizeof(struct profile_hit
));
268 mutex_unlock(&profile_flip_mutex
);
271 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
273 unsigned long primary
, secondary
, flags
, pc
= (unsigned long)__pc
;
275 struct profile_hit
*hits
;
277 if (prof_on
!= type
|| !prof_buffer
)
279 pc
= min((pc
- (unsigned long)_stext
) >> prof_shift
, prof_len
- 1);
280 i
= primary
= (pc
& (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
281 secondary
= (~(pc
<< 1) & (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
283 hits
= per_cpu(cpu_profile_hits
, cpu
)[per_cpu(cpu_profile_flip
, cpu
)];
289 * We buffer the global profiler buffer into a per-CPU
290 * queue and thus reduce the number of global (and possibly
291 * NUMA-alien) accesses. The write-queue is self-coalescing:
293 local_irq_save(flags
);
295 for (j
= 0; j
< PROFILE_GRPSZ
; ++j
) {
296 if (hits
[i
+ j
].pc
== pc
) {
297 hits
[i
+ j
].hits
+= nr_hits
;
299 } else if (!hits
[i
+ j
].hits
) {
301 hits
[i
+ j
].hits
= nr_hits
;
305 i
= (i
+ secondary
) & (NR_PROFILE_HIT
- 1);
306 } while (i
!= primary
);
309 * Add the current hit(s) and flush the write-queue out
310 * to the global buffer:
312 atomic_add(nr_hits
, &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;
318 local_irq_restore(flags
);
322 static int __devinit
profile_cpu_callback(struct notifier_block
*info
,
323 unsigned long action
, void *__cpu
)
325 int node
, cpu
= (unsigned long)__cpu
;
330 node
= cpu_to_node(cpu
);
331 per_cpu(cpu_profile_flip
, cpu
) = 0;
332 if (!per_cpu(cpu_profile_hits
, cpu
)[1]) {
333 page
= alloc_pages_node(node
,
334 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
338 per_cpu(cpu_profile_hits
, cpu
)[1] = page_address(page
);
340 if (!per_cpu(cpu_profile_hits
, cpu
)[0]) {
341 page
= alloc_pages_node(node
,
342 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
346 per_cpu(cpu_profile_hits
, cpu
)[0] = page_address(page
);
350 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
351 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
355 cpu_set(cpu
, prof_cpu_mask
);
357 case CPU_UP_CANCELED
:
359 cpu_clear(cpu
, prof_cpu_mask
);
360 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
361 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
362 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
365 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
366 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
367 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
374 #else /* !CONFIG_SMP */
375 #define profile_flip_buffers() do { } while (0)
376 #define profile_discard_flip_buffers() do { } while (0)
377 #define profile_cpu_callback NULL
379 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
383 if (prof_on
!= type
|| !prof_buffer
)
385 pc
= ((unsigned long)__pc
- (unsigned long)_stext
) >> prof_shift
;
386 atomic_add(nr_hits
, &prof_buffer
[min(pc
, prof_len
- 1)]);
388 #endif /* !CONFIG_SMP */
390 void profile_tick(int type
)
392 struct pt_regs
*regs
= get_irq_regs();
394 if (type
== CPU_PROFILING
&& timer_hook
)
396 if (!user_mode(regs
) && cpu_isset(smp_processor_id(), prof_cpu_mask
))
397 profile_hit(type
, (void *)profile_pc(regs
));
400 #ifdef CONFIG_PROC_FS
401 #include <linux/proc_fs.h>
402 #include <asm/uaccess.h>
403 #include <asm/ptrace.h>
405 static int prof_cpu_mask_read_proc (char *page
, char **start
, off_t off
,
406 int count
, int *eof
, void *data
)
408 int len
= cpumask_scnprintf(page
, count
, *(cpumask_t
*)data
);
411 len
+= sprintf(page
+ len
, "\n");
415 static int prof_cpu_mask_write_proc (struct file
*file
, const char __user
*buffer
,
416 unsigned long count
, void *data
)
418 cpumask_t
*mask
= (cpumask_t
*)data
;
419 unsigned long full_count
= count
, err
;
422 err
= cpumask_parse_user(buffer
, count
, new_value
);
430 void create_prof_cpu_mask(struct proc_dir_entry
*root_irq_dir
)
432 struct proc_dir_entry
*entry
;
434 /* create /proc/irq/prof_cpu_mask */
435 if (!(entry
= create_proc_entry("prof_cpu_mask", 0600, root_irq_dir
)))
438 entry
->data
= (void *)&prof_cpu_mask
;
439 entry
->read_proc
= prof_cpu_mask_read_proc
;
440 entry
->write_proc
= prof_cpu_mask_write_proc
;
444 * This function accesses profiling information. The returned data is
445 * binary: the sampling step and the actual contents of the profile
446 * buffer. Use of the program readprofile is recommended in order to
447 * get meaningful info out of these data.
450 read_profile(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
452 unsigned long p
= *ppos
;
455 unsigned int sample_step
= 1 << prof_shift
;
457 profile_flip_buffers();
458 if (p
>= (prof_len
+1)*sizeof(unsigned int))
460 if (count
> (prof_len
+1)*sizeof(unsigned int) - p
)
461 count
= (prof_len
+1)*sizeof(unsigned int) - p
;
464 while (p
< sizeof(unsigned int) && count
> 0) {
465 if (put_user(*((char *)(&sample_step
)+p
),buf
))
467 buf
++; p
++; count
--; read
++;
469 pnt
= (char *)prof_buffer
+ p
- sizeof(atomic_t
);
470 if (copy_to_user(buf
,(void *)pnt
,count
))
478 * Writing to /proc/profile resets the counters
480 * Writing a 'profiling multiplier' value into it also re-sets the profiling
481 * interrupt frequency, on architectures that support this.
483 static ssize_t
write_profile(struct file
*file
, const char __user
*buf
,
484 size_t count
, loff_t
*ppos
)
487 extern int setup_profiling_timer (unsigned int multiplier
);
489 if (count
== sizeof(int)) {
490 unsigned int multiplier
;
492 if (copy_from_user(&multiplier
, buf
, sizeof(int)))
495 if (setup_profiling_timer(multiplier
))
499 profile_discard_flip_buffers();
500 memset(prof_buffer
, 0, prof_len
* sizeof(atomic_t
));
504 static const struct file_operations proc_profile_operations
= {
505 .read
= read_profile
,
506 .write
= write_profile
,
510 static void __init
profile_nop(void *unused
)
514 static int __init
create_hash_tables(void)
518 for_each_online_cpu(cpu
) {
519 int node
= cpu_to_node(cpu
);
522 page
= alloc_pages_node(node
,
523 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
527 per_cpu(cpu_profile_hits
, cpu
)[1]
528 = (struct profile_hit
*)page_address(page
);
529 page
= alloc_pages_node(node
,
530 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
534 per_cpu(cpu_profile_hits
, cpu
)[0]
535 = (struct profile_hit
*)page_address(page
);
541 on_each_cpu(profile_nop
, NULL
, 0, 1);
542 for_each_online_cpu(cpu
) {
545 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
546 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
547 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
550 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
551 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
552 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
559 #define create_hash_tables() ({ 0; })
562 static int __init
create_proc_profile(void)
564 struct proc_dir_entry
*entry
;
568 if (create_hash_tables())
570 if (!(entry
= create_proc_entry("profile", S_IWUSR
| S_IRUGO
, NULL
)))
572 entry
->proc_fops
= &proc_profile_operations
;
573 entry
->size
= (1+prof_len
) * sizeof(atomic_t
);
574 hotcpu_notifier(profile_cpu_callback
, 0);
577 module_init(create_proc_profile
);
578 #endif /* CONFIG_PROC_FS */