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/highmem.h>
24 #include <linux/mutex.h>
25 #include <asm/sections.h>
26 #include <asm/irq_regs.h>
27 #include <asm/ptrace.h>
32 #define PROFILE_GRPSHIFT 3
33 #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT)
34 #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit))
35 #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ)
37 /* Oprofile timer tick hook */
38 static int (*timer_hook
)(struct pt_regs
*) __read_mostly
;
40 static atomic_t
*prof_buffer
;
41 static unsigned long prof_len
, prof_shift
;
43 int prof_on __read_mostly
;
44 EXPORT_SYMBOL_GPL(prof_on
);
46 static cpumask_t prof_cpu_mask
= CPU_MASK_ALL
;
48 static DEFINE_PER_CPU(struct profile_hit
*[2], cpu_profile_hits
);
49 static DEFINE_PER_CPU(int, cpu_profile_flip
);
50 static DEFINE_MUTEX(profile_flip_mutex
);
51 #endif /* CONFIG_SMP */
53 static int __init
profile_setup(char *str
)
55 static char __initdata schedstr
[] = "schedule";
56 static char __initdata sleepstr
[] = "sleep";
57 static char __initdata kvmstr
[] = "kvm";
60 if (!strncmp(str
, sleepstr
, strlen(sleepstr
))) {
61 #ifdef CONFIG_SCHEDSTATS
62 prof_on
= SLEEP_PROFILING
;
63 if (str
[strlen(sleepstr
)] == ',')
64 str
+= strlen(sleepstr
) + 1;
65 if (get_option(&str
, &par
))
68 "kernel sleep profiling enabled (shift: %ld)\n",
72 "kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
73 #endif /* CONFIG_SCHEDSTATS */
74 } else if (!strncmp(str
, schedstr
, strlen(schedstr
))) {
75 prof_on
= SCHED_PROFILING
;
76 if (str
[strlen(schedstr
)] == ',')
77 str
+= strlen(schedstr
) + 1;
78 if (get_option(&str
, &par
))
81 "kernel schedule profiling enabled (shift: %ld)\n",
83 } else if (!strncmp(str
, kvmstr
, strlen(kvmstr
))) {
84 prof_on
= KVM_PROFILING
;
85 if (str
[strlen(kvmstr
)] == ',')
86 str
+= strlen(kvmstr
) + 1;
87 if (get_option(&str
, &par
))
90 "kernel KVM profiling enabled (shift: %ld)\n",
92 } else if (get_option(&str
, &par
)) {
94 prof_on
= CPU_PROFILING
;
95 printk(KERN_INFO
"kernel profiling enabled (shift: %ld)\n",
100 __setup("profile=", profile_setup
);
103 void __init
profile_init(void)
108 /* only text is profiled */
109 prof_len
= (_etext
- _stext
) >> prof_shift
;
110 prof_buffer
= alloc_bootmem(prof_len
*sizeof(atomic_t
));
113 /* Profile event notifications */
115 #ifdef CONFIG_PROFILING
117 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier
);
118 static ATOMIC_NOTIFIER_HEAD(task_free_notifier
);
119 static BLOCKING_NOTIFIER_HEAD(munmap_notifier
);
121 void profile_task_exit(struct task_struct
*task
)
123 blocking_notifier_call_chain(&task_exit_notifier
, 0, task
);
126 int profile_handoff_task(struct task_struct
*task
)
129 ret
= atomic_notifier_call_chain(&task_free_notifier
, 0, task
);
130 return (ret
== NOTIFY_OK
) ? 1 : 0;
133 void profile_munmap(unsigned long addr
)
135 blocking_notifier_call_chain(&munmap_notifier
, 0, (void *)addr
);
138 int task_handoff_register(struct notifier_block
*n
)
140 return atomic_notifier_chain_register(&task_free_notifier
, n
);
142 EXPORT_SYMBOL_GPL(task_handoff_register
);
144 int task_handoff_unregister(struct notifier_block
*n
)
146 return atomic_notifier_chain_unregister(&task_free_notifier
, n
);
148 EXPORT_SYMBOL_GPL(task_handoff_unregister
);
150 int profile_event_register(enum profile_type type
, struct notifier_block
*n
)
155 case PROFILE_TASK_EXIT
:
156 err
= blocking_notifier_chain_register(
157 &task_exit_notifier
, n
);
160 err
= blocking_notifier_chain_register(
161 &munmap_notifier
, n
);
167 EXPORT_SYMBOL_GPL(profile_event_register
);
169 int profile_event_unregister(enum profile_type type
, struct notifier_block
*n
)
174 case PROFILE_TASK_EXIT
:
175 err
= blocking_notifier_chain_unregister(
176 &task_exit_notifier
, n
);
179 err
= blocking_notifier_chain_unregister(
180 &munmap_notifier
, n
);
186 EXPORT_SYMBOL_GPL(profile_event_unregister
);
188 int register_timer_hook(int (*hook
)(struct pt_regs
*))
195 EXPORT_SYMBOL_GPL(register_timer_hook
);
197 void unregister_timer_hook(int (*hook
)(struct pt_regs
*))
199 WARN_ON(hook
!= timer_hook
);
201 /* make sure all CPUs see the NULL hook */
202 synchronize_sched(); /* Allow ongoing interrupts to complete. */
204 EXPORT_SYMBOL_GPL(unregister_timer_hook
);
206 #endif /* CONFIG_PROFILING */
211 * Each cpu has a pair of open-addressed hashtables for pending
212 * profile hits. read_profile() IPI's all cpus to request them
213 * to flip buffers and flushes their contents to prof_buffer itself.
214 * Flip requests are serialized by the profile_flip_mutex. The sole
215 * use of having a second hashtable is for avoiding cacheline
216 * contention that would otherwise happen during flushes of pending
217 * profile hits required for the accuracy of reported profile hits
218 * and so resurrect the interrupt livelock issue.
220 * The open-addressed hashtables are indexed by profile buffer slot
221 * and hold the number of pending hits to that profile buffer slot on
222 * a cpu in an entry. When the hashtable overflows, all pending hits
223 * are accounted to their corresponding profile buffer slots with
224 * atomic_add() and the hashtable emptied. As numerous pending hits
225 * may be accounted to a profile buffer slot in a hashtable entry,
226 * this amortizes a number of atomic profile buffer increments likely
227 * to be far larger than the number of entries in the hashtable,
228 * particularly given that the number of distinct profile buffer
229 * positions to which hits are accounted during short intervals (e.g.
230 * several seconds) is usually very small. Exclusion from buffer
231 * flipping is provided by interrupt disablement (note that for
232 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
234 * The hash function is meant to be lightweight as opposed to strong,
235 * and was vaguely inspired by ppc64 firmware-supported inverted
236 * pagetable hash functions, but uses a full hashtable full of finite
237 * collision chains, not just pairs of them.
241 static void __profile_flip_buffers(void *unused
)
243 int cpu
= smp_processor_id();
245 per_cpu(cpu_profile_flip
, cpu
) = !per_cpu(cpu_profile_flip
, cpu
);
248 static void profile_flip_buffers(void)
252 mutex_lock(&profile_flip_mutex
);
253 j
= per_cpu(cpu_profile_flip
, get_cpu());
255 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
256 for_each_online_cpu(cpu
) {
257 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[j
];
258 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
264 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
265 hits
[i
].hits
= hits
[i
].pc
= 0;
268 mutex_unlock(&profile_flip_mutex
);
271 static void profile_discard_flip_buffers(void)
275 mutex_lock(&profile_flip_mutex
);
276 i
= per_cpu(cpu_profile_flip
, get_cpu());
278 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
279 for_each_online_cpu(cpu
) {
280 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[i
];
281 memset(hits
, 0, NR_PROFILE_HIT
*sizeof(struct profile_hit
));
283 mutex_unlock(&profile_flip_mutex
);
286 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
288 unsigned long primary
, secondary
, flags
, pc
= (unsigned long)__pc
;
290 struct profile_hit
*hits
;
292 if (prof_on
!= type
|| !prof_buffer
)
294 pc
= min((pc
- (unsigned long)_stext
) >> prof_shift
, prof_len
- 1);
295 i
= primary
= (pc
& (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
296 secondary
= (~(pc
<< 1) & (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
298 hits
= per_cpu(cpu_profile_hits
, cpu
)[per_cpu(cpu_profile_flip
, cpu
)];
304 * We buffer the global profiler buffer into a per-CPU
305 * queue and thus reduce the number of global (and possibly
306 * NUMA-alien) accesses. The write-queue is self-coalescing:
308 local_irq_save(flags
);
310 for (j
= 0; j
< PROFILE_GRPSZ
; ++j
) {
311 if (hits
[i
+ j
].pc
== pc
) {
312 hits
[i
+ j
].hits
+= nr_hits
;
314 } else if (!hits
[i
+ j
].hits
) {
316 hits
[i
+ j
].hits
= nr_hits
;
320 i
= (i
+ secondary
) & (NR_PROFILE_HIT
- 1);
321 } while (i
!= primary
);
324 * Add the current hit(s) and flush the write-queue out
325 * to the global buffer:
327 atomic_add(nr_hits
, &prof_buffer
[pc
]);
328 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
329 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
330 hits
[i
].pc
= hits
[i
].hits
= 0;
333 local_irq_restore(flags
);
337 static int __devinit
profile_cpu_callback(struct notifier_block
*info
,
338 unsigned long action
, void *__cpu
)
340 int node
, cpu
= (unsigned long)__cpu
;
345 case CPU_UP_PREPARE_FROZEN
:
346 node
= cpu_to_node(cpu
);
347 per_cpu(cpu_profile_flip
, cpu
) = 0;
348 if (!per_cpu(cpu_profile_hits
, cpu
)[1]) {
349 page
= alloc_pages_node(node
,
350 GFP_KERNEL
| __GFP_ZERO
,
354 per_cpu(cpu_profile_hits
, cpu
)[1] = page_address(page
);
356 if (!per_cpu(cpu_profile_hits
, cpu
)[0]) {
357 page
= alloc_pages_node(node
,
358 GFP_KERNEL
| __GFP_ZERO
,
362 per_cpu(cpu_profile_hits
, cpu
)[0] = page_address(page
);
366 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
367 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
371 case CPU_ONLINE_FROZEN
:
372 cpu_set(cpu
, prof_cpu_mask
);
374 case CPU_UP_CANCELED
:
375 case CPU_UP_CANCELED_FROZEN
:
377 case CPU_DEAD_FROZEN
:
378 cpu_clear(cpu
, prof_cpu_mask
);
379 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
380 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
381 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
384 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
385 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
386 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
393 #else /* !CONFIG_SMP */
394 #define profile_flip_buffers() do { } while (0)
395 #define profile_discard_flip_buffers() do { } while (0)
396 #define profile_cpu_callback NULL
398 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
402 if (prof_on
!= type
|| !prof_buffer
)
404 pc
= ((unsigned long)__pc
- (unsigned long)_stext
) >> prof_shift
;
405 atomic_add(nr_hits
, &prof_buffer
[min(pc
, prof_len
- 1)]);
407 #endif /* !CONFIG_SMP */
408 EXPORT_SYMBOL_GPL(profile_hits
);
410 void profile_tick(int type
)
412 struct pt_regs
*regs
= get_irq_regs();
414 if (type
== CPU_PROFILING
&& timer_hook
)
416 if (!user_mode(regs
) && cpu_isset(smp_processor_id(), prof_cpu_mask
))
417 profile_hit(type
, (void *)profile_pc(regs
));
420 #ifdef CONFIG_PROC_FS
421 #include <linux/proc_fs.h>
422 #include <asm/uaccess.h>
423 #include <asm/ptrace.h>
425 static int prof_cpu_mask_read_proc(char *page
, char **start
, off_t off
,
426 int count
, int *eof
, void *data
)
428 int len
= cpumask_scnprintf(page
, count
, *(cpumask_t
*)data
);
431 len
+= sprintf(page
+ len
, "\n");
435 static int prof_cpu_mask_write_proc(struct file
*file
,
436 const char __user
*buffer
, unsigned long count
, void *data
)
438 cpumask_t
*mask
= (cpumask_t
*)data
;
439 unsigned long full_count
= count
, err
;
442 err
= cpumask_parse_user(buffer
, count
, new_value
);
450 void create_prof_cpu_mask(struct proc_dir_entry
*root_irq_dir
)
452 struct proc_dir_entry
*entry
;
454 /* create /proc/irq/prof_cpu_mask */
455 entry
= create_proc_entry("prof_cpu_mask", 0600, root_irq_dir
);
458 entry
->data
= (void *)&prof_cpu_mask
;
459 entry
->read_proc
= prof_cpu_mask_read_proc
;
460 entry
->write_proc
= prof_cpu_mask_write_proc
;
464 * This function accesses profiling information. The returned data is
465 * binary: the sampling step and the actual contents of the profile
466 * buffer. Use of the program readprofile is recommended in order to
467 * get meaningful info out of these data.
470 read_profile(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
472 unsigned long p
= *ppos
;
475 unsigned int sample_step
= 1 << prof_shift
;
477 profile_flip_buffers();
478 if (p
>= (prof_len
+1)*sizeof(unsigned int))
480 if (count
> (prof_len
+1)*sizeof(unsigned int) - p
)
481 count
= (prof_len
+1)*sizeof(unsigned int) - p
;
484 while (p
< sizeof(unsigned int) && count
> 0) {
485 if (put_user(*((char *)(&sample_step
)+p
), buf
))
487 buf
++; p
++; count
--; read
++;
489 pnt
= (char *)prof_buffer
+ p
- sizeof(atomic_t
);
490 if (copy_to_user(buf
, (void *)pnt
, count
))
498 * Writing to /proc/profile resets the counters
500 * Writing a 'profiling multiplier' value into it also re-sets the profiling
501 * interrupt frequency, on architectures that support this.
503 static ssize_t
write_profile(struct file
*file
, const char __user
*buf
,
504 size_t count
, loff_t
*ppos
)
507 extern int setup_profiling_timer(unsigned int multiplier
);
509 if (count
== sizeof(int)) {
510 unsigned int multiplier
;
512 if (copy_from_user(&multiplier
, buf
, sizeof(int)))
515 if (setup_profiling_timer(multiplier
))
519 profile_discard_flip_buffers();
520 memset(prof_buffer
, 0, prof_len
* sizeof(atomic_t
));
524 static const struct file_operations proc_profile_operations
= {
525 .read
= read_profile
,
526 .write
= write_profile
,
530 static void __init
profile_nop(void *unused
)
534 static int __init
create_hash_tables(void)
538 for_each_online_cpu(cpu
) {
539 int node
= cpu_to_node(cpu
);
542 page
= alloc_pages_node(node
,
543 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
547 per_cpu(cpu_profile_hits
, cpu
)[1]
548 = (struct profile_hit
*)page_address(page
);
549 page
= alloc_pages_node(node
,
550 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
554 per_cpu(cpu_profile_hits
, cpu
)[0]
555 = (struct profile_hit
*)page_address(page
);
561 on_each_cpu(profile_nop
, NULL
, 0, 1);
562 for_each_online_cpu(cpu
) {
565 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
566 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
567 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
570 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
571 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
572 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
579 #define create_hash_tables() ({ 0; })
582 static int __init
create_proc_profile(void)
584 struct proc_dir_entry
*entry
;
588 if (create_hash_tables())
590 entry
= proc_create("profile", S_IWUSR
| S_IRUGO
,
591 NULL
, &proc_profile_operations
);
594 entry
->size
= (1+prof_len
) * sizeof(atomic_t
);
595 hotcpu_notifier(profile_cpu_callback
, 0);
598 module_init(create_proc_profile
);
599 #endif /* CONFIG_PROC_FS */