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 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 prof_on
= SLEEP_PROFILING
;
64 if (str
[strlen(sleepstr
)] == ',')
65 str
+= strlen(sleepstr
) + 1;
66 if (get_option(&str
, &par
))
69 "kernel sleep profiling enabled (shift: %ld)\n",
71 } else if (!strncmp(str
, schedstr
, strlen(schedstr
))) {
72 prof_on
= SCHED_PROFILING
;
73 if (str
[strlen(schedstr
)] == ',')
74 str
+= strlen(schedstr
) + 1;
75 if (get_option(&str
, &par
))
78 "kernel schedule profiling enabled (shift: %ld)\n",
80 } else if (!strncmp(str
, kvmstr
, strlen(kvmstr
))) {
81 prof_on
= KVM_PROFILING
;
82 if (str
[strlen(kvmstr
)] == ',')
83 str
+= strlen(kvmstr
) + 1;
84 if (get_option(&str
, &par
))
87 "kernel KVM profiling enabled (shift: %ld)\n",
89 } else if (get_option(&str
, &par
)) {
91 prof_on
= CPU_PROFILING
;
92 printk(KERN_INFO
"kernel profiling enabled (shift: %ld)\n",
97 __setup("profile=", profile_setup
);
100 void __init
profile_init(void)
105 /* only text is profiled */
106 prof_len
= (_etext
- _stext
) >> prof_shift
;
107 prof_buffer
= alloc_bootmem(prof_len
*sizeof(atomic_t
));
110 /* Profile event notifications */
112 #ifdef CONFIG_PROFILING
114 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier
);
115 static ATOMIC_NOTIFIER_HEAD(task_free_notifier
);
116 static BLOCKING_NOTIFIER_HEAD(munmap_notifier
);
118 void profile_task_exit(struct task_struct
* task
)
120 blocking_notifier_call_chain(&task_exit_notifier
, 0, task
);
123 int profile_handoff_task(struct task_struct
* task
)
126 ret
= atomic_notifier_call_chain(&task_free_notifier
, 0, task
);
127 return (ret
== NOTIFY_OK
) ? 1 : 0;
130 void profile_munmap(unsigned long addr
)
132 blocking_notifier_call_chain(&munmap_notifier
, 0, (void *)addr
);
135 int task_handoff_register(struct notifier_block
* n
)
137 return atomic_notifier_chain_register(&task_free_notifier
, n
);
140 int task_handoff_unregister(struct notifier_block
* n
)
142 return atomic_notifier_chain_unregister(&task_free_notifier
, n
);
145 int profile_event_register(enum profile_type type
, struct notifier_block
* n
)
150 case PROFILE_TASK_EXIT
:
151 err
= blocking_notifier_chain_register(
152 &task_exit_notifier
, n
);
155 err
= blocking_notifier_chain_register(
156 &munmap_notifier
, n
);
164 int profile_event_unregister(enum profile_type type
, struct notifier_block
* n
)
169 case PROFILE_TASK_EXIT
:
170 err
= blocking_notifier_chain_unregister(
171 &task_exit_notifier
, n
);
174 err
= blocking_notifier_chain_unregister(
175 &munmap_notifier
, n
);
182 int register_timer_hook(int (*hook
)(struct pt_regs
*))
190 void unregister_timer_hook(int (*hook
)(struct pt_regs
*))
192 WARN_ON(hook
!= timer_hook
);
194 /* make sure all CPUs see the NULL hook */
195 synchronize_sched(); /* Allow ongoing interrupts to complete. */
198 EXPORT_SYMBOL_GPL(register_timer_hook
);
199 EXPORT_SYMBOL_GPL(unregister_timer_hook
);
200 EXPORT_SYMBOL_GPL(task_handoff_register
);
201 EXPORT_SYMBOL_GPL(task_handoff_unregister
);
203 #endif /* CONFIG_PROFILING */
205 EXPORT_SYMBOL_GPL(profile_event_register
);
206 EXPORT_SYMBOL_GPL(profile_event_unregister
);
210 * Each cpu has a pair of open-addressed hashtables for pending
211 * profile hits. read_profile() IPI's all cpus to request them
212 * to flip buffers and flushes their contents to prof_buffer itself.
213 * Flip requests are serialized by the profile_flip_mutex. The sole
214 * use of having a second hashtable is for avoiding cacheline
215 * contention that would otherwise happen during flushes of pending
216 * profile hits required for the accuracy of reported profile hits
217 * and so resurrect the interrupt livelock issue.
219 * The open-addressed hashtables are indexed by profile buffer slot
220 * and hold the number of pending hits to that profile buffer slot on
221 * a cpu in an entry. When the hashtable overflows, all pending hits
222 * are accounted to their corresponding profile buffer slots with
223 * atomic_add() and the hashtable emptied. As numerous pending hits
224 * may be accounted to a profile buffer slot in a hashtable entry,
225 * this amortizes a number of atomic profile buffer increments likely
226 * to be far larger than the number of entries in the hashtable,
227 * particularly given that the number of distinct profile buffer
228 * positions to which hits are accounted during short intervals (e.g.
229 * several seconds) is usually very small. Exclusion from buffer
230 * flipping is provided by interrupt disablement (note that for
231 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
233 * The hash function is meant to be lightweight as opposed to strong,
234 * and was vaguely inspired by ppc64 firmware-supported inverted
235 * pagetable hash functions, but uses a full hashtable full of finite
236 * collision chains, not just pairs of them.
240 static void __profile_flip_buffers(void *unused
)
242 int cpu
= smp_processor_id();
244 per_cpu(cpu_profile_flip
, cpu
) = !per_cpu(cpu_profile_flip
, cpu
);
247 static void profile_flip_buffers(void)
251 mutex_lock(&profile_flip_mutex
);
252 j
= per_cpu(cpu_profile_flip
, get_cpu());
254 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
255 for_each_online_cpu(cpu
) {
256 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[j
];
257 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
263 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
264 hits
[i
].hits
= hits
[i
].pc
= 0;
267 mutex_unlock(&profile_flip_mutex
);
270 static void profile_discard_flip_buffers(void)
274 mutex_lock(&profile_flip_mutex
);
275 i
= per_cpu(cpu_profile_flip
, get_cpu());
277 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
278 for_each_online_cpu(cpu
) {
279 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[i
];
280 memset(hits
, 0, NR_PROFILE_HIT
*sizeof(struct profile_hit
));
282 mutex_unlock(&profile_flip_mutex
);
285 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
287 unsigned long primary
, secondary
, flags
, pc
= (unsigned long)__pc
;
289 struct profile_hit
*hits
;
291 if (prof_on
!= type
|| !prof_buffer
)
293 pc
= min((pc
- (unsigned long)_stext
) >> prof_shift
, prof_len
- 1);
294 i
= primary
= (pc
& (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
295 secondary
= (~(pc
<< 1) & (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
297 hits
= per_cpu(cpu_profile_hits
, cpu
)[per_cpu(cpu_profile_flip
, cpu
)];
303 * We buffer the global profiler buffer into a per-CPU
304 * queue and thus reduce the number of global (and possibly
305 * NUMA-alien) accesses. The write-queue is self-coalescing:
307 local_irq_save(flags
);
309 for (j
= 0; j
< PROFILE_GRPSZ
; ++j
) {
310 if (hits
[i
+ j
].pc
== pc
) {
311 hits
[i
+ j
].hits
+= nr_hits
;
313 } else if (!hits
[i
+ j
].hits
) {
315 hits
[i
+ j
].hits
= nr_hits
;
319 i
= (i
+ secondary
) & (NR_PROFILE_HIT
- 1);
320 } while (i
!= primary
);
323 * Add the current hit(s) and flush the write-queue out
324 * to the global buffer:
326 atomic_add(nr_hits
, &prof_buffer
[pc
]);
327 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
328 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
329 hits
[i
].pc
= hits
[i
].hits
= 0;
332 local_irq_restore(flags
);
336 static int __devinit
profile_cpu_callback(struct notifier_block
*info
,
337 unsigned long action
, void *__cpu
)
339 int node
, cpu
= (unsigned long)__cpu
;
344 case CPU_UP_PREPARE_FROZEN
:
345 node
= cpu_to_node(cpu
);
346 per_cpu(cpu_profile_flip
, cpu
) = 0;
347 if (!per_cpu(cpu_profile_hits
, cpu
)[1]) {
348 page
= alloc_pages_node(node
,
349 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
353 per_cpu(cpu_profile_hits
, cpu
)[1] = page_address(page
);
355 if (!per_cpu(cpu_profile_hits
, cpu
)[0]) {
356 page
= alloc_pages_node(node
,
357 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
361 per_cpu(cpu_profile_hits
, cpu
)[0] = page_address(page
);
365 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
366 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
370 case CPU_ONLINE_FROZEN
:
371 cpu_set(cpu
, prof_cpu_mask
);
373 case CPU_UP_CANCELED
:
374 case CPU_UP_CANCELED_FROZEN
:
376 case CPU_DEAD_FROZEN
:
377 cpu_clear(cpu
, prof_cpu_mask
);
378 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
379 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
380 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
383 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
384 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
385 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
392 #else /* !CONFIG_SMP */
393 #define profile_flip_buffers() do { } while (0)
394 #define profile_discard_flip_buffers() do { } while (0)
395 #define profile_cpu_callback NULL
397 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
401 if (prof_on
!= type
|| !prof_buffer
)
403 pc
= ((unsigned long)__pc
- (unsigned long)_stext
) >> prof_shift
;
404 atomic_add(nr_hits
, &prof_buffer
[min(pc
, prof_len
- 1)]);
406 #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
, const char __user
*buffer
,
436 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 if (!(entry
= create_proc_entry("prof_cpu_mask", 0600, root_irq_dir
)))
457 entry
->data
= (void *)&prof_cpu_mask
;
458 entry
->read_proc
= prof_cpu_mask_read_proc
;
459 entry
->write_proc
= prof_cpu_mask_write_proc
;
463 * This function accesses profiling information. The returned data is
464 * binary: the sampling step and the actual contents of the profile
465 * buffer. Use of the program readprofile is recommended in order to
466 * get meaningful info out of these data.
469 read_profile(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
471 unsigned long p
= *ppos
;
474 unsigned int sample_step
= 1 << prof_shift
;
476 profile_flip_buffers();
477 if (p
>= (prof_len
+1)*sizeof(unsigned int))
479 if (count
> (prof_len
+1)*sizeof(unsigned int) - p
)
480 count
= (prof_len
+1)*sizeof(unsigned int) - p
;
483 while (p
< sizeof(unsigned int) && count
> 0) {
484 if (put_user(*((char *)(&sample_step
)+p
),buf
))
486 buf
++; p
++; count
--; read
++;
488 pnt
= (char *)prof_buffer
+ p
- sizeof(atomic_t
);
489 if (copy_to_user(buf
,(void *)pnt
,count
))
497 * Writing to /proc/profile resets the counters
499 * Writing a 'profiling multiplier' value into it also re-sets the profiling
500 * interrupt frequency, on architectures that support this.
502 static ssize_t
write_profile(struct file
*file
, const char __user
*buf
,
503 size_t count
, loff_t
*ppos
)
506 extern int setup_profiling_timer (unsigned int multiplier
);
508 if (count
== sizeof(int)) {
509 unsigned int multiplier
;
511 if (copy_from_user(&multiplier
, buf
, sizeof(int)))
514 if (setup_profiling_timer(multiplier
))
518 profile_discard_flip_buffers();
519 memset(prof_buffer
, 0, prof_len
* sizeof(atomic_t
));
523 static const struct file_operations proc_profile_operations
= {
524 .read
= read_profile
,
525 .write
= write_profile
,
529 static void __init
profile_nop(void *unused
)
533 static int __init
create_hash_tables(void)
537 for_each_online_cpu(cpu
) {
538 int node
= cpu_to_node(cpu
);
541 page
= alloc_pages_node(node
,
542 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
546 per_cpu(cpu_profile_hits
, cpu
)[1]
547 = (struct profile_hit
*)page_address(page
);
548 page
= alloc_pages_node(node
,
549 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
553 per_cpu(cpu_profile_hits
, cpu
)[0]
554 = (struct profile_hit
*)page_address(page
);
560 on_each_cpu(profile_nop
, NULL
, 0, 1);
561 for_each_online_cpu(cpu
) {
564 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
565 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
566 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
569 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
570 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
571 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
578 #define create_hash_tables() ({ 0; })
581 static int __init
create_proc_profile(void)
583 struct proc_dir_entry
*entry
;
587 if (create_hash_tables())
589 if (!(entry
= create_proc_entry("profile", S_IWUSR
| S_IRUGO
, NULL
)))
591 entry
->proc_fops
= &proc_profile_operations
;
592 entry
->size
= (1+prof_len
) * sizeof(atomic_t
);
593 hotcpu_notifier(profile_cpu_callback
, 0);
596 module_init(create_proc_profile
);
597 #endif /* CONFIG_PROC_FS */