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 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier
);
116 static ATOMIC_NOTIFIER_HEAD(task_free_notifier
);
117 static BLOCKING_NOTIFIER_HEAD(munmap_notifier
);
119 void profile_task_exit(struct task_struct
*task
)
121 blocking_notifier_call_chain(&task_exit_notifier
, 0, task
);
124 int profile_handoff_task(struct task_struct
*task
)
127 ret
= atomic_notifier_call_chain(&task_free_notifier
, 0, task
);
128 return (ret
== NOTIFY_OK
) ? 1 : 0;
131 void profile_munmap(unsigned long addr
)
133 blocking_notifier_call_chain(&munmap_notifier
, 0, (void *)addr
);
136 int task_handoff_register(struct notifier_block
*n
)
138 return atomic_notifier_chain_register(&task_free_notifier
, n
);
140 EXPORT_SYMBOL_GPL(task_handoff_register
);
142 int task_handoff_unregister(struct notifier_block
*n
)
144 return atomic_notifier_chain_unregister(&task_free_notifier
, n
);
146 EXPORT_SYMBOL_GPL(task_handoff_unregister
);
148 int profile_event_register(enum profile_type type
, struct notifier_block
*n
)
153 case PROFILE_TASK_EXIT
:
154 err
= blocking_notifier_chain_register(
155 &task_exit_notifier
, n
);
158 err
= blocking_notifier_chain_register(
159 &munmap_notifier
, n
);
165 EXPORT_SYMBOL_GPL(profile_event_register
);
167 int profile_event_unregister(enum profile_type type
, struct notifier_block
*n
)
172 case PROFILE_TASK_EXIT
:
173 err
= blocking_notifier_chain_unregister(
174 &task_exit_notifier
, n
);
177 err
= blocking_notifier_chain_unregister(
178 &munmap_notifier
, n
);
184 EXPORT_SYMBOL_GPL(profile_event_unregister
);
186 int register_timer_hook(int (*hook
)(struct pt_regs
*))
193 EXPORT_SYMBOL_GPL(register_timer_hook
);
195 void unregister_timer_hook(int (*hook
)(struct pt_regs
*))
197 WARN_ON(hook
!= timer_hook
);
199 /* make sure all CPUs see the NULL hook */
200 synchronize_sched(); /* Allow ongoing interrupts to complete. */
202 EXPORT_SYMBOL_GPL(unregister_timer_hook
);
207 * Each cpu has a pair of open-addressed hashtables for pending
208 * profile hits. read_profile() IPI's all cpus to request them
209 * to flip buffers and flushes their contents to prof_buffer itself.
210 * Flip requests are serialized by the profile_flip_mutex. The sole
211 * use of having a second hashtable is for avoiding cacheline
212 * contention that would otherwise happen during flushes of pending
213 * profile hits required for the accuracy of reported profile hits
214 * and so resurrect the interrupt livelock issue.
216 * The open-addressed hashtables are indexed by profile buffer slot
217 * and hold the number of pending hits to that profile buffer slot on
218 * a cpu in an entry. When the hashtable overflows, all pending hits
219 * are accounted to their corresponding profile buffer slots with
220 * atomic_add() and the hashtable emptied. As numerous pending hits
221 * may be accounted to a profile buffer slot in a hashtable entry,
222 * this amortizes a number of atomic profile buffer increments likely
223 * to be far larger than the number of entries in the hashtable,
224 * particularly given that the number of distinct profile buffer
225 * positions to which hits are accounted during short intervals (e.g.
226 * several seconds) is usually very small. Exclusion from buffer
227 * flipping is provided by interrupt disablement (note that for
228 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
230 * The hash function is meant to be lightweight as opposed to strong,
231 * and was vaguely inspired by ppc64 firmware-supported inverted
232 * pagetable hash functions, but uses a full hashtable full of finite
233 * collision chains, not just pairs of them.
237 static void __profile_flip_buffers(void *unused
)
239 int cpu
= smp_processor_id();
241 per_cpu(cpu_profile_flip
, cpu
) = !per_cpu(cpu_profile_flip
, cpu
);
244 static void profile_flip_buffers(void)
248 mutex_lock(&profile_flip_mutex
);
249 j
= per_cpu(cpu_profile_flip
, get_cpu());
251 on_each_cpu(__profile_flip_buffers
, NULL
, 1);
252 for_each_online_cpu(cpu
) {
253 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[j
];
254 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
260 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
261 hits
[i
].hits
= hits
[i
].pc
= 0;
264 mutex_unlock(&profile_flip_mutex
);
267 static void profile_discard_flip_buffers(void)
271 mutex_lock(&profile_flip_mutex
);
272 i
= per_cpu(cpu_profile_flip
, get_cpu());
274 on_each_cpu(__profile_flip_buffers
, NULL
, 1);
275 for_each_online_cpu(cpu
) {
276 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[i
];
277 memset(hits
, 0, NR_PROFILE_HIT
*sizeof(struct profile_hit
));
279 mutex_unlock(&profile_flip_mutex
);
282 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
284 unsigned long primary
, secondary
, flags
, pc
= (unsigned long)__pc
;
286 struct profile_hit
*hits
;
288 if (prof_on
!= type
|| !prof_buffer
)
290 pc
= min((pc
- (unsigned long)_stext
) >> prof_shift
, prof_len
- 1);
291 i
= primary
= (pc
& (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
292 secondary
= (~(pc
<< 1) & (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
294 hits
= per_cpu(cpu_profile_hits
, cpu
)[per_cpu(cpu_profile_flip
, cpu
)];
300 * We buffer the global profiler buffer into a per-CPU
301 * queue and thus reduce the number of global (and possibly
302 * NUMA-alien) accesses. The write-queue is self-coalescing:
304 local_irq_save(flags
);
306 for (j
= 0; j
< PROFILE_GRPSZ
; ++j
) {
307 if (hits
[i
+ j
].pc
== pc
) {
308 hits
[i
+ j
].hits
+= nr_hits
;
310 } else if (!hits
[i
+ j
].hits
) {
312 hits
[i
+ j
].hits
= nr_hits
;
316 i
= (i
+ secondary
) & (NR_PROFILE_HIT
- 1);
317 } while (i
!= primary
);
320 * Add the current hit(s) and flush the write-queue out
321 * to the global buffer:
323 atomic_add(nr_hits
, &prof_buffer
[pc
]);
324 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
325 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
326 hits
[i
].pc
= hits
[i
].hits
= 0;
329 local_irq_restore(flags
);
333 static int __devinit
profile_cpu_callback(struct notifier_block
*info
,
334 unsigned long action
, void *__cpu
)
336 int node
, cpu
= (unsigned long)__cpu
;
341 case CPU_UP_PREPARE_FROZEN
:
342 node
= cpu_to_node(cpu
);
343 per_cpu(cpu_profile_flip
, cpu
) = 0;
344 if (!per_cpu(cpu_profile_hits
, cpu
)[1]) {
345 page
= alloc_pages_node(node
,
346 GFP_KERNEL
| __GFP_ZERO
,
350 per_cpu(cpu_profile_hits
, cpu
)[1] = page_address(page
);
352 if (!per_cpu(cpu_profile_hits
, cpu
)[0]) {
353 page
= alloc_pages_node(node
,
354 GFP_KERNEL
| __GFP_ZERO
,
358 per_cpu(cpu_profile_hits
, cpu
)[0] = page_address(page
);
362 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
363 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
367 case CPU_ONLINE_FROZEN
:
368 cpu_set(cpu
, prof_cpu_mask
);
370 case CPU_UP_CANCELED
:
371 case CPU_UP_CANCELED_FROZEN
:
373 case CPU_DEAD_FROZEN
:
374 cpu_clear(cpu
, prof_cpu_mask
);
375 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
376 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
377 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
380 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
381 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
382 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
389 #else /* !CONFIG_SMP */
390 #define profile_flip_buffers() do { } while (0)
391 #define profile_discard_flip_buffers() do { } while (0)
392 #define profile_cpu_callback NULL
394 void profile_hits(int type
, void *__pc
, unsigned int nr_hits
)
398 if (prof_on
!= type
|| !prof_buffer
)
400 pc
= ((unsigned long)__pc
- (unsigned long)_stext
) >> prof_shift
;
401 atomic_add(nr_hits
, &prof_buffer
[min(pc
, prof_len
- 1)]);
403 #endif /* !CONFIG_SMP */
404 EXPORT_SYMBOL_GPL(profile_hits
);
406 void profile_tick(int type
)
408 struct pt_regs
*regs
= get_irq_regs();
410 if (type
== CPU_PROFILING
&& timer_hook
)
412 if (!user_mode(regs
) && cpu_isset(smp_processor_id(), prof_cpu_mask
))
413 profile_hit(type
, (void *)profile_pc(regs
));
416 #ifdef CONFIG_PROC_FS
417 #include <linux/proc_fs.h>
418 #include <asm/uaccess.h>
419 #include <asm/ptrace.h>
421 static int prof_cpu_mask_read_proc(char *page
, char **start
, off_t off
,
422 int count
, int *eof
, void *data
)
424 int len
= cpumask_scnprintf(page
, count
, *(cpumask_t
*)data
);
427 len
+= sprintf(page
+ len
, "\n");
431 static int prof_cpu_mask_write_proc(struct file
*file
,
432 const char __user
*buffer
, unsigned long count
, void *data
)
434 cpumask_t
*mask
= (cpumask_t
*)data
;
435 unsigned long full_count
= count
, err
;
438 err
= cpumask_parse_user(buffer
, count
, new_value
);
446 void create_prof_cpu_mask(struct proc_dir_entry
*root_irq_dir
)
448 struct proc_dir_entry
*entry
;
450 /* create /proc/irq/prof_cpu_mask */
451 entry
= create_proc_entry("prof_cpu_mask", 0600, root_irq_dir
);
454 entry
->data
= (void *)&prof_cpu_mask
;
455 entry
->read_proc
= prof_cpu_mask_read_proc
;
456 entry
->write_proc
= prof_cpu_mask_write_proc
;
460 * This function accesses profiling information. The returned data is
461 * binary: the sampling step and the actual contents of the profile
462 * buffer. Use of the program readprofile is recommended in order to
463 * get meaningful info out of these data.
466 read_profile(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
468 unsigned long p
= *ppos
;
471 unsigned int sample_step
= 1 << prof_shift
;
473 profile_flip_buffers();
474 if (p
>= (prof_len
+1)*sizeof(unsigned int))
476 if (count
> (prof_len
+1)*sizeof(unsigned int) - p
)
477 count
= (prof_len
+1)*sizeof(unsigned int) - p
;
480 while (p
< sizeof(unsigned int) && count
> 0) {
481 if (put_user(*((char *)(&sample_step
)+p
), buf
))
483 buf
++; p
++; count
--; read
++;
485 pnt
= (char *)prof_buffer
+ p
- sizeof(atomic_t
);
486 if (copy_to_user(buf
, (void *)pnt
, count
))
494 * Writing to /proc/profile resets the counters
496 * Writing a 'profiling multiplier' value into it also re-sets the profiling
497 * interrupt frequency, on architectures that support this.
499 static ssize_t
write_profile(struct file
*file
, const char __user
*buf
,
500 size_t count
, loff_t
*ppos
)
503 extern int setup_profiling_timer(unsigned int multiplier
);
505 if (count
== sizeof(int)) {
506 unsigned int multiplier
;
508 if (copy_from_user(&multiplier
, buf
, sizeof(int)))
511 if (setup_profiling_timer(multiplier
))
515 profile_discard_flip_buffers();
516 memset(prof_buffer
, 0, prof_len
* sizeof(atomic_t
));
520 static const struct file_operations proc_profile_operations
= {
521 .read
= read_profile
,
522 .write
= write_profile
,
526 static void __init
profile_nop(void *unused
)
530 static int __init
create_hash_tables(void)
534 for_each_online_cpu(cpu
) {
535 int node
= cpu_to_node(cpu
);
538 page
= alloc_pages_node(node
,
539 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
543 per_cpu(cpu_profile_hits
, cpu
)[1]
544 = (struct profile_hit
*)page_address(page
);
545 page
= alloc_pages_node(node
,
546 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
550 per_cpu(cpu_profile_hits
, cpu
)[0]
551 = (struct profile_hit
*)page_address(page
);
557 on_each_cpu(profile_nop
, NULL
, 1);
558 for_each_online_cpu(cpu
) {
561 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
562 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
563 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
566 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
567 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
568 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
575 #define create_hash_tables() ({ 0; })
578 static int __init
create_proc_profile(void)
580 struct proc_dir_entry
*entry
;
584 if (create_hash_tables())
586 entry
= proc_create("profile", S_IWUSR
| S_IRUGO
,
587 NULL
, &proc_profile_operations
);
590 entry
->size
= (1+prof_len
) * sizeof(atomic_t
);
591 hotcpu_notifier(profile_cpu_callback
, 0);
594 module_init(create_proc_profile
);
595 #endif /* CONFIG_PROC_FS */