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>
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 int (*timer_hook
)(struct pt_regs
*) __read_mostly
;
40 static atomic_t
*prof_buffer
;
41 static unsigned long prof_len
, prof_shift
;
42 static int prof_on __read_mostly
;
43 static cpumask_t prof_cpu_mask
= CPU_MASK_ALL
;
45 static DEFINE_PER_CPU(struct profile_hit
*[2], cpu_profile_hits
);
46 static DEFINE_PER_CPU(int, cpu_profile_flip
);
47 static DEFINE_MUTEX(profile_flip_mutex
);
48 #endif /* CONFIG_SMP */
50 static int __init
profile_setup(char * str
)
52 static char __initdata schedstr
[] = "schedule";
55 if (!strncmp(str
, schedstr
, strlen(schedstr
))) {
56 prof_on
= SCHED_PROFILING
;
57 if (str
[strlen(schedstr
)] == ',')
58 str
+= strlen(schedstr
) + 1;
59 if (get_option(&str
, &par
))
62 "kernel schedule profiling enabled (shift: %ld)\n",
64 } else if (get_option(&str
, &par
)) {
66 prof_on
= CPU_PROFILING
;
67 printk(KERN_INFO
"kernel profiling enabled (shift: %ld)\n",
72 __setup("profile=", profile_setup
);
75 void __init
profile_init(void)
80 /* only text is profiled */
81 prof_len
= (_etext
- _stext
) >> prof_shift
;
82 prof_buffer
= alloc_bootmem(prof_len
*sizeof(atomic_t
));
85 /* Profile event notifications */
87 #ifdef CONFIG_PROFILING
89 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier
);
90 static ATOMIC_NOTIFIER_HEAD(task_free_notifier
);
91 static BLOCKING_NOTIFIER_HEAD(munmap_notifier
);
93 void profile_task_exit(struct task_struct
* task
)
95 blocking_notifier_call_chain(&task_exit_notifier
, 0, task
);
98 int profile_handoff_task(struct task_struct
* task
)
101 ret
= atomic_notifier_call_chain(&task_free_notifier
, 0, task
);
102 return (ret
== NOTIFY_OK
) ? 1 : 0;
105 void profile_munmap(unsigned long addr
)
107 blocking_notifier_call_chain(&munmap_notifier
, 0, (void *)addr
);
110 int task_handoff_register(struct notifier_block
* n
)
112 return atomic_notifier_chain_register(&task_free_notifier
, n
);
115 int task_handoff_unregister(struct notifier_block
* n
)
117 return atomic_notifier_chain_unregister(&task_free_notifier
, n
);
120 int profile_event_register(enum profile_type type
, struct notifier_block
* n
)
125 case PROFILE_TASK_EXIT
:
126 err
= blocking_notifier_chain_register(
127 &task_exit_notifier
, n
);
130 err
= blocking_notifier_chain_register(
131 &munmap_notifier
, n
);
139 int profile_event_unregister(enum profile_type type
, struct notifier_block
* n
)
144 case PROFILE_TASK_EXIT
:
145 err
= blocking_notifier_chain_unregister(
146 &task_exit_notifier
, n
);
149 err
= blocking_notifier_chain_unregister(
150 &munmap_notifier
, n
);
157 int register_timer_hook(int (*hook
)(struct pt_regs
*))
165 void unregister_timer_hook(int (*hook
)(struct pt_regs
*))
167 WARN_ON(hook
!= timer_hook
);
169 /* make sure all CPUs see the NULL hook */
170 synchronize_sched(); /* Allow ongoing interrupts to complete. */
173 EXPORT_SYMBOL_GPL(register_timer_hook
);
174 EXPORT_SYMBOL_GPL(unregister_timer_hook
);
175 EXPORT_SYMBOL_GPL(task_handoff_register
);
176 EXPORT_SYMBOL_GPL(task_handoff_unregister
);
178 #endif /* CONFIG_PROFILING */
180 EXPORT_SYMBOL_GPL(profile_event_register
);
181 EXPORT_SYMBOL_GPL(profile_event_unregister
);
185 * Each cpu has a pair of open-addressed hashtables for pending
186 * profile hits. read_profile() IPI's all cpus to request them
187 * to flip buffers and flushes their contents to prof_buffer itself.
188 * Flip requests are serialized by the profile_flip_mutex. The sole
189 * use of having a second hashtable is for avoiding cacheline
190 * contention that would otherwise happen during flushes of pending
191 * profile hits required for the accuracy of reported profile hits
192 * and so resurrect the interrupt livelock issue.
194 * The open-addressed hashtables are indexed by profile buffer slot
195 * and hold the number of pending hits to that profile buffer slot on
196 * a cpu in an entry. When the hashtable overflows, all pending hits
197 * are accounted to their corresponding profile buffer slots with
198 * atomic_add() and the hashtable emptied. As numerous pending hits
199 * may be accounted to a profile buffer slot in a hashtable entry,
200 * this amortizes a number of atomic profile buffer increments likely
201 * to be far larger than the number of entries in the hashtable,
202 * particularly given that the number of distinct profile buffer
203 * positions to which hits are accounted during short intervals (e.g.
204 * several seconds) is usually very small. Exclusion from buffer
205 * flipping is provided by interrupt disablement (note that for
206 * SCHED_PROFILING profile_hit() may be called from process context).
207 * The hash function is meant to be lightweight as opposed to strong,
208 * and was vaguely inspired by ppc64 firmware-supported inverted
209 * pagetable hash functions, but uses a full hashtable full of finite
210 * collision chains, not just pairs of them.
214 static void __profile_flip_buffers(void *unused
)
216 int cpu
= smp_processor_id();
218 per_cpu(cpu_profile_flip
, cpu
) = !per_cpu(cpu_profile_flip
, cpu
);
221 static void profile_flip_buffers(void)
225 mutex_lock(&profile_flip_mutex
);
226 j
= per_cpu(cpu_profile_flip
, get_cpu());
228 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
229 for_each_online_cpu(cpu
) {
230 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[j
];
231 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
237 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
238 hits
[i
].hits
= hits
[i
].pc
= 0;
241 mutex_unlock(&profile_flip_mutex
);
244 static void profile_discard_flip_buffers(void)
248 mutex_lock(&profile_flip_mutex
);
249 i
= per_cpu(cpu_profile_flip
, get_cpu());
251 on_each_cpu(__profile_flip_buffers
, NULL
, 0, 1);
252 for_each_online_cpu(cpu
) {
253 struct profile_hit
*hits
= per_cpu(cpu_profile_hits
, cpu
)[i
];
254 memset(hits
, 0, NR_PROFILE_HIT
*sizeof(struct profile_hit
));
256 mutex_unlock(&profile_flip_mutex
);
259 void profile_hit(int type
, void *__pc
)
261 unsigned long primary
, secondary
, flags
, pc
= (unsigned long)__pc
;
263 struct profile_hit
*hits
;
265 if (prof_on
!= type
|| !prof_buffer
)
267 pc
= min((pc
- (unsigned long)_stext
) >> prof_shift
, prof_len
- 1);
268 i
= primary
= (pc
& (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
269 secondary
= (~(pc
<< 1) & (NR_PROFILE_GRP
- 1)) << PROFILE_GRPSHIFT
;
271 hits
= per_cpu(cpu_profile_hits
, cpu
)[per_cpu(cpu_profile_flip
, cpu
)];
276 local_irq_save(flags
);
278 for (j
= 0; j
< PROFILE_GRPSZ
; ++j
) {
279 if (hits
[i
+ j
].pc
== pc
) {
282 } else if (!hits
[i
+ j
].hits
) {
284 hits
[i
+ j
].hits
= 1;
288 i
= (i
+ secondary
) & (NR_PROFILE_HIT
- 1);
289 } while (i
!= primary
);
290 atomic_inc(&prof_buffer
[pc
]);
291 for (i
= 0; i
< NR_PROFILE_HIT
; ++i
) {
292 atomic_add(hits
[i
].hits
, &prof_buffer
[hits
[i
].pc
]);
293 hits
[i
].pc
= hits
[i
].hits
= 0;
296 local_irq_restore(flags
);
300 #ifdef CONFIG_HOTPLUG_CPU
301 static int __devinit
profile_cpu_callback(struct notifier_block
*info
,
302 unsigned long action
, void *__cpu
)
304 int node
, cpu
= (unsigned long)__cpu
;
309 node
= cpu_to_node(cpu
);
310 per_cpu(cpu_profile_flip
, cpu
) = 0;
311 if (!per_cpu(cpu_profile_hits
, cpu
)[1]) {
312 page
= alloc_pages_node(node
,
313 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
317 per_cpu(cpu_profile_hits
, cpu
)[1] = page_address(page
);
319 if (!per_cpu(cpu_profile_hits
, cpu
)[0]) {
320 page
= alloc_pages_node(node
,
321 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
325 per_cpu(cpu_profile_hits
, cpu
)[0] = page_address(page
);
329 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
330 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
334 cpu_set(cpu
, prof_cpu_mask
);
336 case CPU_UP_CANCELED
:
338 cpu_clear(cpu
, prof_cpu_mask
);
339 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
340 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
341 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
344 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
345 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
346 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
353 #endif /* CONFIG_HOTPLUG_CPU */
354 #else /* !CONFIG_SMP */
355 #define profile_flip_buffers() do { } while (0)
356 #define profile_discard_flip_buffers() do { } while (0)
358 void profile_hit(int type
, void *__pc
)
362 if (prof_on
!= type
|| !prof_buffer
)
364 pc
= ((unsigned long)__pc
- (unsigned long)_stext
) >> prof_shift
;
365 atomic_inc(&prof_buffer
[min(pc
, prof_len
- 1)]);
367 #endif /* !CONFIG_SMP */
369 void profile_tick(int type
, struct pt_regs
*regs
)
371 if (type
== CPU_PROFILING
&& timer_hook
)
373 if (!user_mode(regs
) && cpu_isset(smp_processor_id(), prof_cpu_mask
))
374 profile_hit(type
, (void *)profile_pc(regs
));
377 #ifdef CONFIG_PROC_FS
378 #include <linux/proc_fs.h>
379 #include <asm/uaccess.h>
380 #include <asm/ptrace.h>
382 static int prof_cpu_mask_read_proc (char *page
, char **start
, off_t off
,
383 int count
, int *eof
, void *data
)
385 int len
= cpumask_scnprintf(page
, count
, *(cpumask_t
*)data
);
388 len
+= sprintf(page
+ len
, "\n");
392 static int prof_cpu_mask_write_proc (struct file
*file
, const char __user
*buffer
,
393 unsigned long count
, void *data
)
395 cpumask_t
*mask
= (cpumask_t
*)data
;
396 unsigned long full_count
= count
, err
;
399 err
= cpumask_parse(buffer
, count
, new_value
);
407 void create_prof_cpu_mask(struct proc_dir_entry
*root_irq_dir
)
409 struct proc_dir_entry
*entry
;
411 /* create /proc/irq/prof_cpu_mask */
412 if (!(entry
= create_proc_entry("prof_cpu_mask", 0600, root_irq_dir
)))
415 entry
->data
= (void *)&prof_cpu_mask
;
416 entry
->read_proc
= prof_cpu_mask_read_proc
;
417 entry
->write_proc
= prof_cpu_mask_write_proc
;
421 * This function accesses profiling information. The returned data is
422 * binary: the sampling step and the actual contents of the profile
423 * buffer. Use of the program readprofile is recommended in order to
424 * get meaningful info out of these data.
427 read_profile(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
429 unsigned long p
= *ppos
;
432 unsigned int sample_step
= 1 << prof_shift
;
434 profile_flip_buffers();
435 if (p
>= (prof_len
+1)*sizeof(unsigned int))
437 if (count
> (prof_len
+1)*sizeof(unsigned int) - p
)
438 count
= (prof_len
+1)*sizeof(unsigned int) - p
;
441 while (p
< sizeof(unsigned int) && count
> 0) {
442 put_user(*((char *)(&sample_step
)+p
),buf
);
443 buf
++; p
++; count
--; read
++;
445 pnt
= (char *)prof_buffer
+ p
- sizeof(atomic_t
);
446 if (copy_to_user(buf
,(void *)pnt
,count
))
454 * Writing to /proc/profile resets the counters
456 * Writing a 'profiling multiplier' value into it also re-sets the profiling
457 * interrupt frequency, on architectures that support this.
459 static ssize_t
write_profile(struct file
*file
, const char __user
*buf
,
460 size_t count
, loff_t
*ppos
)
463 extern int setup_profiling_timer (unsigned int multiplier
);
465 if (count
== sizeof(int)) {
466 unsigned int multiplier
;
468 if (copy_from_user(&multiplier
, buf
, sizeof(int)))
471 if (setup_profiling_timer(multiplier
))
475 profile_discard_flip_buffers();
476 memset(prof_buffer
, 0, prof_len
* sizeof(atomic_t
));
480 static struct file_operations proc_profile_operations
= {
481 .read
= read_profile
,
482 .write
= write_profile
,
486 static void __init
profile_nop(void *unused
)
490 static int __init
create_hash_tables(void)
494 for_each_online_cpu(cpu
) {
495 int node
= cpu_to_node(cpu
);
498 page
= alloc_pages_node(node
,
499 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
503 per_cpu(cpu_profile_hits
, cpu
)[1]
504 = (struct profile_hit
*)page_address(page
);
505 page
= alloc_pages_node(node
,
506 GFP_KERNEL
| __GFP_ZERO
| GFP_THISNODE
,
510 per_cpu(cpu_profile_hits
, cpu
)[0]
511 = (struct profile_hit
*)page_address(page
);
517 on_each_cpu(profile_nop
, NULL
, 0, 1);
518 for_each_online_cpu(cpu
) {
521 if (per_cpu(cpu_profile_hits
, cpu
)[0]) {
522 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[0]);
523 per_cpu(cpu_profile_hits
, cpu
)[0] = NULL
;
526 if (per_cpu(cpu_profile_hits
, cpu
)[1]) {
527 page
= virt_to_page(per_cpu(cpu_profile_hits
, cpu
)[1]);
528 per_cpu(cpu_profile_hits
, cpu
)[1] = NULL
;
535 #define create_hash_tables() ({ 0; })
538 static int __init
create_proc_profile(void)
540 struct proc_dir_entry
*entry
;
544 if (create_hash_tables())
546 if (!(entry
= create_proc_entry("profile", S_IWUSR
| S_IRUGO
, NULL
)))
548 entry
->proc_fops
= &proc_profile_operations
;
549 entry
->size
= (1+prof_len
) * sizeof(atomic_t
);
550 hotcpu_notifier(profile_cpu_callback
, 0);
553 module_init(create_proc_profile
);
554 #endif /* CONFIG_PROC_FS */