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>
29 #include <asm/ptrace.h>
34 #define PROFILE_GRPSHIFT 3
35 #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
36 #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
37 #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
39 /* Oprofile timer tick hook */
40 static int (*timer_hook
)(struct pt_regs
*) __read_mostly
;
42 static atomic_t
*prof_buffer
;
43 static unsigned long prof_len
, prof_shift
;
45 int prof_on __read_mostly
;
46 EXPORT_SYMBOL_GPL(prof_on
);
48 static cpumask_t prof_cpu_mask
= CPU_MASK_ALL
;
50 static DEFINE_PER_CPU(struct profile_hit
*[2], cpu_profile_hits
);
51 static DEFINE_PER_CPU(int, cpu_profile_flip
);
52 static DEFINE_MUTEX(profile_flip_mutex
);
53 #endif /* CONFIG_SMP */
55 static int __init
profile_setup(char *str
)
57 static char __initdata schedstr
[] = "schedule";
58 static char __initdata sleepstr
[] = "sleep";
59 static char __initdata kvmstr
[] = "kvm";
62 if (!strncmp(str
, sleepstr
, strlen(sleepstr
))) {
63 #ifdef CONFIG_SCHEDSTATS
64 prof_on
= SLEEP_PROFILING
;
65 if (str
[strlen(sleepstr
)] == ',')
66 str
+= strlen(sleepstr
) + 1;
67 if (get_option(&str
, &par
))
70 "kernel sleep profiling enabled (shift: %ld)\n",
74 "kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
75 #endif /* CONFIG_SCHEDSTATS */
76 } else if (!strncmp(str
, schedstr
, strlen(schedstr
))) {
77 prof_on
= SCHED_PROFILING
;
78 if (str
[strlen(schedstr
)] == ',')
79 str
+= strlen(schedstr
) + 1;
80 if (get_option(&str
, &par
))
83 "kernel schedule profiling enabled (shift: %ld)\n",
85 } else if (!strncmp(str
, kvmstr
, strlen(kvmstr
))) {
86 prof_on
= KVM_PROFILING
;
87 if (str
[strlen(kvmstr
)] == ',')
88 str
+= strlen(kvmstr
) + 1;
89 if (get_option(&str
, &par
))
92 "kernel KVM profiling enabled (shift: %ld)\n",
94 } else if (get_option(&str
, &par
)) {
96 prof_on
= CPU_PROFILING
;
97 printk(KERN_INFO
"kernel profiling enabled (shift: %ld)\n",
102 __setup("profile=", profile_setup
);
105 void __init
profile_init(void)
110 /* only text is profiled */
111 prof_len
= (_etext
- _stext
) >> prof_shift
;
112 prof_buffer
= alloc_bootmem(prof_len
*sizeof(atomic_t
));
115 /* Profile event notifications */
117 #ifdef CONFIG_PROFILING
119 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier
);
120 static ATOMIC_NOTIFIER_HEAD(task_free_notifier
);
121 static BLOCKING_NOTIFIER_HEAD(munmap_notifier
);
123 void profile_task_exit(struct task_struct
*task
)
125 blocking_notifier_call_chain(&task_exit_notifier
, 0, task
);
128 int profile_handoff_task(struct task_struct
*task
)
131 ret
= atomic_notifier_call_chain(&task_free_notifier
, 0, task
);
132 return (ret
== NOTIFY_OK
) ? 1 : 0;
135 void profile_munmap(unsigned long addr
)
137 blocking_notifier_call_chain(&munmap_notifier
, 0, (void *)addr
);
140 int task_handoff_register(struct notifier_block
*n
)
142 return atomic_notifier_chain_register(&task_free_notifier
, n
);
144 EXPORT_SYMBOL_GPL(task_handoff_register
);
146 int task_handoff_unregister(struct notifier_block
*n
)
148 return atomic_notifier_chain_unregister(&task_free_notifier
, n
);
150 EXPORT_SYMBOL_GPL(task_handoff_unregister
);
152 int profile_event_register(enum profile_type type
, struct notifier_block
*n
)
157 case PROFILE_TASK_EXIT
:
158 err
= blocking_notifier_chain_register(
159 &task_exit_notifier
, n
);
162 err
= blocking_notifier_chain_register(
163 &munmap_notifier
, n
);
169 EXPORT_SYMBOL_GPL(profile_event_register
);
171 int profile_event_unregister(enum profile_type type
, struct notifier_block
*n
)
176 case PROFILE_TASK_EXIT
:
177 err
= blocking_notifier_chain_unregister(
178 &task_exit_notifier
, n
);
181 err
= blocking_notifier_chain_unregister(
182 &munmap_notifier
, n
);
188 EXPORT_SYMBOL_GPL(profile_event_unregister
);
190 int register_timer_hook(int (*hook
)(struct pt_regs
*))
197 EXPORT_SYMBOL_GPL(register_timer_hook
);
199 void unregister_timer_hook(int (*hook
)(struct pt_regs
*))
201 WARN_ON(hook
!= timer_hook
);
203 /* make sure all CPUs see the NULL hook */
204 synchronize_sched(); /* Allow ongoing interrupts to complete. */
206 EXPORT_SYMBOL_GPL(unregister_timer_hook
);
208 #endif /* CONFIG_PROFILING */
213 * Each cpu has a pair of open-addressed hashtables for pending
214 * profile hits. read_profile() IPI's all cpus to request them
215 * to flip buffers and flushes their contents to prof_buffer itself.
216 * Flip requests are serialized by the profile_flip_mutex. The sole
217 * use of having a second hashtable is for avoiding cacheline
218 * contention that would otherwise happen during flushes of pending
219 * profile hits required for the accuracy of reported profile hits
220 * and so resurrect the interrupt livelock issue.
222 * The open-addressed hashtables are indexed by profile buffer slot
223 * and hold the number of pending hits to that profile buffer slot on
224 * a cpu in an entry. When the hashtable overflows, all pending hits
225 * are accounted to their corresponding profile buffer slots with
226 * atomic_add() and the hashtable emptied. As numerous pending hits
227 * may be accounted to a profile buffer slot in a hashtable entry,
228 * this amortizes a number of atomic profile buffer increments likely
229 * to be far larger than the number of entries in the hashtable,
230 * particularly given that the number of distinct profile buffer
231 * positions to which hits are accounted during short intervals (e.g.
232 * several seconds) is usually very small. Exclusion from buffer
233 * flipping is provided by interrupt disablement (note that for
234 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
236 * The hash function is meant to be lightweight as opposed to strong,
237 * and was vaguely inspired by ppc64 firmware-supported inverted
238 * pagetable hash functions, but uses a full hashtable full of finite
239 * collision chains, not just pairs of them.
243 static void __profile_flip_buffers(void *unused
)
245 int cpu
= smp_processor_id();
247 per_cpu(cpu_profile_flip
, cpu
) = !per_cpu(cpu_profile_flip
, cpu
);
250 static void profile_flip_buffers(void)
254 mutex_lock(&profile_flip_mutex
);
255 j
= per_cpu(cpu_profile_flip
, get_cpu());
257 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
258 for_each_online_cpu(cpu
) {
259 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[j
];
260 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
266 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
267 hits
[i
].hits
= hits
[i
].pc
= 0;
270 mutex_unlock(&profile_flip_mutex
);
273 static void profile_discard_flip_buffers(void)
277 mutex_lock(&profile_flip_mutex
);
278 i
= per_cpu(cpu_profile_flip
, get_cpu());
280 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
281 for_each_online_cpu(cpu
) {
282 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[i
];
283 memset(hits
, 0, NR_PROFILE_HIT
*sizeof(struct profile_hit
));
285 mutex_unlock(&profile_flip_mutex
);
288 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
290 unsigned long primary
, secondary
, flags
, pc
= (unsigned long)__pc
;
292 struct profile_hit
*hits
;
294 if (prof_on
!= type
|| !prof_buffer
)
296 pc
= min((pc
- (unsigned long)_stext
) >> prof_shift
, prof_len
- 1);
297 i
= primary
= (pc
& (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
298 secondary
= (~(pc
<< 1) & (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
300 hits
= per_cpu(cpu_profile_hits
, cpu
)[per_cpu(cpu_profile_flip
, cpu
)];
306 * We buffer the global profiler buffer into a per-CPU
307 * queue and thus reduce the number of global (and possibly
308 * NUMA-alien) accesses. The write-queue is self-coalescing:
310 local_irq_save(flags
);
312 for (j
= 0; j
< PROFILE_GRPSZ
; ++j
) {
313 if (hits
[i
+ j
].pc
== pc
) {
314 hits
[i
+ j
].hits
+= nr_hits
;
316 } else if (!hits
[i
+ j
].hits
) {
318 hits
[i
+ j
].hits
= nr_hits
;
322 i
= (i
+ secondary
) & (NR_PROFILE_HIT
- 1);
323 } while (i
!= primary
);
326 * Add the current hit(s) and flush the write-queue out
327 * to the global buffer:
329 atomic_add(nr_hits
, &prof_buffer
[pc
]);
330 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
331 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
332 hits
[i
].pc
= hits
[i
].hits
= 0;
335 local_irq_restore(flags
);
339 static int __devinit
profile_cpu_callback(struct notifier_block
*info
,
340 unsigned long action
, void *__cpu
)
342 int node
, cpu
= (unsigned long)__cpu
;
347 case CPU_UP_PREPARE_FROZEN
:
348 node
= cpu_to_node(cpu
);
349 per_cpu(cpu_profile_flip
, cpu
) = 0;
350 if (!per_cpu(cpu_profile_hits
, cpu
)[1]) {
351 page
= alloc_pages_node(node
,
352 GFP_KERNEL
| __GFP_ZERO
,
356 per_cpu(cpu_profile_hits
, cpu
)[1] = page_address(page
);
358 if (!per_cpu(cpu_profile_hits
, cpu
)[0]) {
359 page
= alloc_pages_node(node
,
360 GFP_KERNEL
| __GFP_ZERO
,
364 per_cpu(cpu_profile_hits
, cpu
)[0] = page_address(page
);
368 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
369 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
373 case CPU_ONLINE_FROZEN
:
374 cpu_set(cpu
, prof_cpu_mask
);
376 case CPU_UP_CANCELED
:
377 case CPU_UP_CANCELED_FROZEN
:
379 case CPU_DEAD_FROZEN
:
380 cpu_clear(cpu
, prof_cpu_mask
);
381 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
382 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
383 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
386 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
387 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
388 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
395 #else /* !CONFIG_SMP */
396 #define profile_flip_buffers() do { } while (0)
397 #define profile_discard_flip_buffers() do { } while (0)
398 #define profile_cpu_callback NULL
400 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
404 if (prof_on
!= type
|| !prof_buffer
)
406 pc
= ((unsigned long)__pc
- (unsigned long)_stext
) >> prof_shift
;
407 atomic_add(nr_hits
, &prof_buffer
[min(pc
, prof_len
- 1)]);
409 #endif /* !CONFIG_SMP */
410 EXPORT_SYMBOL_GPL(profile_hits
);
412 void profile_tick(int type
)
414 struct pt_regs
*regs
= get_irq_regs();
416 if (type
== CPU_PROFILING
&& timer_hook
)
418 if (!user_mode(regs
) && cpu_isset(smp_processor_id(), prof_cpu_mask
))
419 profile_hit(type
, (void *)profile_pc(regs
));
422 #ifdef CONFIG_PROC_FS
423 #include <linux/proc_fs.h>
424 #include <asm/uaccess.h>
425 #include <asm/ptrace.h>
427 static int prof_cpu_mask_read_proc(char *page
, char **start
, off_t off
,
428 int count
, int *eof
, void *data
)
430 int len
= cpumask_scnprintf(page
, count
, *(cpumask_t
*)data
);
433 len
+= sprintf(page
+ len
, "\n");
437 static int prof_cpu_mask_write_proc(struct file
*file
,
438 const char __user
*buffer
, unsigned long count
, void *data
)
440 cpumask_t
*mask
= (cpumask_t
*)data
;
441 unsigned long full_count
= count
, err
;
444 err
= cpumask_parse_user(buffer
, count
, new_value
);
452 void create_prof_cpu_mask(struct proc_dir_entry
*root_irq_dir
)
454 struct proc_dir_entry
*entry
;
456 /* create /proc/irq/prof_cpu_mask */
457 entry
= create_proc_entry("prof_cpu_mask", 0600, root_irq_dir
);
460 entry
->data
= (void *)&prof_cpu_mask
;
461 entry
->read_proc
= prof_cpu_mask_read_proc
;
462 entry
->write_proc
= prof_cpu_mask_write_proc
;
466 * This function accesses profiling information. The returned data is
467 * binary: the sampling step and the actual contents of the profile
468 * buffer. Use of the program readprofile is recommended in order to
469 * get meaningful info out of these data.
472 read_profile(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
474 unsigned long p
= *ppos
;
477 unsigned int sample_step
= 1 << prof_shift
;
479 profile_flip_buffers();
480 if (p
>= (prof_len
+1)*sizeof(unsigned int))
482 if (count
> (prof_len
+1)*sizeof(unsigned int) - p
)
483 count
= (prof_len
+1)*sizeof(unsigned int) - p
;
486 while (p
< sizeof(unsigned int) && count
> 0) {
487 if (put_user(*((char *)(&sample_step
)+p
), buf
))
489 buf
++; p
++; count
--; read
++;
491 pnt
= (char *)prof_buffer
+ p
- sizeof(atomic_t
);
492 if (copy_to_user(buf
, (void *)pnt
, count
))
500 * Writing to /proc/profile resets the counters
502 * Writing a 'profiling multiplier' value into it also re-sets the profiling
503 * interrupt frequency, on architectures that support this.
505 static ssize_t
write_profile(struct file
*file
, const char __user
*buf
,
506 size_t count
, loff_t
*ppos
)
509 extern int setup_profiling_timer(unsigned int multiplier
);
511 if (count
== sizeof(int)) {
512 unsigned int multiplier
;
514 if (copy_from_user(&multiplier
, buf
, sizeof(int)))
517 if (setup_profiling_timer(multiplier
))
521 profile_discard_flip_buffers();
522 memset(prof_buffer
, 0, prof_len
* sizeof(atomic_t
));
526 static const struct file_operations proc_profile_operations
= {
527 .read
= read_profile
,
528 .write
= write_profile
,
532 static void __init
profile_nop(void *unused
)
536 static int __init
create_hash_tables(void)
540 for_each_online_cpu(cpu
) {
541 int node
= cpu_to_node(cpu
);
544 page
= alloc_pages_node(node
,
545 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
549 per_cpu(cpu_profile_hits
, cpu
)[1]
550 = (struct profile_hit
*)page_address(page
);
551 page
= alloc_pages_node(node
,
552 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
556 per_cpu(cpu_profile_hits
, cpu
)[0]
557 = (struct profile_hit
*)page_address(page
);
563 on_each_cpu(profile_nop
, NULL
, 0, 1);
564 for_each_online_cpu(cpu
) {
567 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
568 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
569 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
572 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
573 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
574 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
581 #define create_hash_tables() ({ 0; })
584 static int __init
create_proc_profile(void)
586 struct proc_dir_entry
*entry
;
590 if (create_hash_tables())
592 entry
= create_proc_entry("profile", S_IWUSR
| S_IRUGO
, NULL
);
595 entry
->proc_fops
= &proc_profile_operations
;
596 entry
->size
= (1+prof_len
) * sizeof(atomic_t
);
597 hotcpu_notifier(profile_cpu_callback
, 0);
600 module_init(create_proc_profile
);
601 #endif /* CONFIG_PROC_FS */