blob: 5e95330e51207c48a043b2675b1d015d48d0f488 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
7 *
8 * Scheduler profiling support, Arjan van de Ven and Ingo Molnar,
9 * Red Hat, July 2004
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
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/profile.h>
18#include <linux/bootmem.h>
19#include <linux/notifier.h>
20#include <linux/mm.h>
21#include <linux/cpumask.h>
22#include <linux/cpu.h>
23#include <linux/profile.h>
24#include <linux/highmem.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/sections.h>
27#include <asm/semaphore.h>
David Howells7d12e782006-10-05 14:55:46 +010028#include <asm/irq_regs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct profile_hit {
32 u32 pc, hits;
33};
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)
38
39/* Oprofile timer tick hook */
Adrian Bunkb012d342007-10-16 23:29:26 -070040static int (*timer_hook)(struct pt_regs *) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static atomic_t *prof_buffer;
43static unsigned long prof_len, prof_shift;
Ingo Molnar07031e12007-01-10 23:15:38 -080044
Ingo Molnarece8a682006-12-06 20:37:24 -080045int prof_on __read_mostly;
Ingo Molnar07031e12007-01-10 23:15:38 -080046EXPORT_SYMBOL_GPL(prof_on);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static cpumask_t prof_cpu_mask = CPU_MASK_ALL;
49#ifdef CONFIG_SMP
50static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);
51static DEFINE_PER_CPU(int, cpu_profile_flip);
Arjan van de Ven97d1f152006-03-23 03:00:24 -080052static DEFINE_MUTEX(profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif /* CONFIG_SMP */
54
55static int __init profile_setup(char * str)
56{
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070057 static char __initdata schedstr[] = "schedule";
Ingo Molnarece8a682006-12-06 20:37:24 -080058 static char __initdata sleepstr[] = "sleep";
Ingo Molnar07031e12007-01-10 23:15:38 -080059 static char __initdata kvmstr[] = "kvm";
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int par;
61
Ingo Molnarece8a682006-12-06 20:37:24 -080062 if (!strncmp(str, sleepstr, strlen(sleepstr))) {
Mel Gormanb3da2a72007-10-24 18:23:50 +020063#ifdef CONFIG_SCHEDSTATS
Ingo Molnarece8a682006-12-06 20:37:24 -080064 prof_on = SLEEP_PROFILING;
65 if (str[strlen(sleepstr)] == ',')
66 str += strlen(sleepstr) + 1;
67 if (get_option(&str, &par))
68 prof_shift = par;
69 printk(KERN_INFO
70 "kernel sleep profiling enabled (shift: %ld)\n",
71 prof_shift);
Mel Gormanb3da2a72007-10-24 18:23:50 +020072#else
73 printk(KERN_WARNING
74 "kernel sleep profiling requires CONFIG_SCHEDSTATS\n");
75#endif /* CONFIG_SCHEDSTATS */
Ingo Molnara75acf82007-01-05 16:36:29 -080076 } else if (!strncmp(str, schedstr, strlen(schedstr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 prof_on = SCHED_PROFILING;
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070078 if (str[strlen(schedstr)] == ',')
79 str += strlen(schedstr) + 1;
80 if (get_option(&str, &par))
81 prof_shift = par;
82 printk(KERN_INFO
83 "kernel schedule profiling enabled (shift: %ld)\n",
84 prof_shift);
Ingo Molnar07031e12007-01-10 23:15:38 -080085 } 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))
90 prof_shift = par;
91 printk(KERN_INFO
92 "kernel KVM profiling enabled (shift: %ld)\n",
93 prof_shift);
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070094 } else if (get_option(&str, &par)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 prof_shift = par;
96 prof_on = CPU_PROFILING;
97 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
98 prof_shift);
99 }
100 return 1;
101}
102__setup("profile=", profile_setup);
103
104
105void __init profile_init(void)
106{
107 if (!prof_on)
108 return;
109
110 /* only text is profiled */
111 prof_len = (_etext - _stext) >> prof_shift;
112 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
113}
114
115/* Profile event notifications */
116
117#ifdef CONFIG_PROFILING
118
Alan Sterne041c682006-03-27 01:16:30 -0800119static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
120static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
121static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123void profile_task_exit(struct task_struct * task)
124{
Alan Sterne041c682006-03-27 01:16:30 -0800125 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128int profile_handoff_task(struct task_struct * task)
129{
130 int ret;
Alan Sterne041c682006-03-27 01:16:30 -0800131 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return (ret == NOTIFY_OK) ? 1 : 0;
133}
134
135void profile_munmap(unsigned long addr)
136{
Alan Sterne041c682006-03-27 01:16:30 -0800137 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140int task_handoff_register(struct notifier_block * n)
141{
Alan Sterne041c682006-03-27 01:16:30 -0800142 return atomic_notifier_chain_register(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145int task_handoff_unregister(struct notifier_block * n)
146{
Alan Sterne041c682006-03-27 01:16:30 -0800147 return atomic_notifier_chain_unregister(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150int profile_event_register(enum profile_type type, struct notifier_block * n)
151{
152 int err = -EINVAL;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 switch (type) {
155 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800156 err = blocking_notifier_chain_register(
157 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800160 err = blocking_notifier_chain_register(
161 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163 }
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return err;
166}
167
168
169int profile_event_unregister(enum profile_type type, struct notifier_block * n)
170{
171 int err = -EINVAL;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 switch (type) {
174 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800175 err = blocking_notifier_chain_unregister(
176 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
178 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800179 err = blocking_notifier_chain_unregister(
180 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return err;
185}
186
187int register_timer_hook(int (*hook)(struct pt_regs *))
188{
189 if (timer_hook)
190 return -EBUSY;
191 timer_hook = hook;
192 return 0;
193}
194
195void unregister_timer_hook(int (*hook)(struct pt_regs *))
196{
197 WARN_ON(hook != timer_hook);
198 timer_hook = NULL;
199 /* make sure all CPUs see the NULL hook */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700200 synchronize_sched(); /* Allow ongoing interrupts to complete. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203EXPORT_SYMBOL_GPL(register_timer_hook);
204EXPORT_SYMBOL_GPL(unregister_timer_hook);
205EXPORT_SYMBOL_GPL(task_handoff_register);
206EXPORT_SYMBOL_GPL(task_handoff_unregister);
Peter Chubbcd5bfea2007-08-10 13:01:10 -0700207EXPORT_SYMBOL_GPL(profile_event_register);
208EXPORT_SYMBOL_GPL(profile_event_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210#endif /* CONFIG_PROFILING */
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213#ifdef CONFIG_SMP
214/*
215 * Each cpu has a pair of open-addressed hashtables for pending
216 * profile hits. read_profile() IPI's all cpus to request them
217 * to flip buffers and flushes their contents to prof_buffer itself.
218 * Flip requests are serialized by the profile_flip_mutex. The sole
219 * use of having a second hashtable is for avoiding cacheline
220 * contention that would otherwise happen during flushes of pending
221 * profile hits required for the accuracy of reported profile hits
222 * and so resurrect the interrupt livelock issue.
223 *
224 * The open-addressed hashtables are indexed by profile buffer slot
225 * and hold the number of pending hits to that profile buffer slot on
226 * a cpu in an entry. When the hashtable overflows, all pending hits
227 * are accounted to their corresponding profile buffer slots with
228 * atomic_add() and the hashtable emptied. As numerous pending hits
229 * may be accounted to a profile buffer slot in a hashtable entry,
230 * this amortizes a number of atomic profile buffer increments likely
231 * to be far larger than the number of entries in the hashtable,
232 * particularly given that the number of distinct profile buffer
233 * positions to which hits are accounted during short intervals (e.g.
234 * several seconds) is usually very small. Exclusion from buffer
235 * flipping is provided by interrupt disablement (note that for
Ingo Molnarece8a682006-12-06 20:37:24 -0800236 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
237 * process context).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * The hash function is meant to be lightweight as opposed to strong,
239 * and was vaguely inspired by ppc64 firmware-supported inverted
240 * pagetable hash functions, but uses a full hashtable full of finite
241 * collision chains, not just pairs of them.
242 *
243 * -- wli
244 */
245static void __profile_flip_buffers(void *unused)
246{
247 int cpu = smp_processor_id();
248
249 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
250}
251
252static void profile_flip_buffers(void)
253{
254 int i, j, cpu;
255
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800256 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 j = per_cpu(cpu_profile_flip, get_cpu());
258 put_cpu();
259 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
260 for_each_online_cpu(cpu) {
261 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j];
262 for (i = 0; i < NR_PROFILE_HIT; ++i) {
263 if (!hits[i].hits) {
264 if (hits[i].pc)
265 hits[i].pc = 0;
266 continue;
267 }
268 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
269 hits[i].hits = hits[i].pc = 0;
270 }
271 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800272 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275static void profile_discard_flip_buffers(void)
276{
277 int i, cpu;
278
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800279 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 i = per_cpu(cpu_profile_flip, get_cpu());
281 put_cpu();
282 on_each_cpu(__profile_flip_buffers, NULL, 0, 1);
283 for_each_online_cpu(cpu) {
284 struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i];
285 memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit));
286 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800287 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Ingo Molnarece8a682006-12-06 20:37:24 -0800290void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
293 int i, j, cpu;
294 struct profile_hit *hits;
295
296 if (prof_on != type || !prof_buffer)
297 return;
298 pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1);
299 i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
300 secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT;
301 cpu = get_cpu();
302 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
303 if (!hits) {
304 put_cpu();
305 return;
306 }
Ingo Molnarece8a682006-12-06 20:37:24 -0800307 /*
308 * We buffer the global profiler buffer into a per-CPU
309 * queue and thus reduce the number of global (and possibly
310 * NUMA-alien) accesses. The write-queue is self-coalescing:
311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 local_irq_save(flags);
313 do {
314 for (j = 0; j < PROFILE_GRPSZ; ++j) {
315 if (hits[i + j].pc == pc) {
Ingo Molnarece8a682006-12-06 20:37:24 -0800316 hits[i + j].hits += nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto out;
318 } else if (!hits[i + j].hits) {
319 hits[i + j].pc = pc;
Ingo Molnarece8a682006-12-06 20:37:24 -0800320 hits[i + j].hits = nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 goto out;
322 }
323 }
324 i = (i + secondary) & (NR_PROFILE_HIT - 1);
325 } while (i != primary);
Ingo Molnarece8a682006-12-06 20:37:24 -0800326
327 /*
328 * Add the current hit(s) and flush the write-queue out
329 * to the global buffer:
330 */
331 atomic_add(nr_hits, &prof_buffer[pc]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 for (i = 0; i < NR_PROFILE_HIT; ++i) {
333 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
334 hits[i].pc = hits[i].hits = 0;
335 }
336out:
337 local_irq_restore(flags);
338 put_cpu();
339}
340
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700341static int __devinit profile_cpu_callback(struct notifier_block *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned long action, void *__cpu)
343{
344 int node, cpu = (unsigned long)__cpu;
345 struct page *page;
346
347 switch (action) {
348 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700349 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 node = cpu_to_node(cpu);
351 per_cpu(cpu_profile_flip, cpu) = 0;
352 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700353 page = alloc_pages_node(node,
Christoph Lameter4199cfa2007-10-16 01:25:34 -0700354 GFP_KERNEL | __GFP_ZERO,
Christoph Lameterfbd98162006-09-25 23:31:45 -0700355 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!page)
357 return NOTIFY_BAD;
358 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
359 }
360 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700361 page = alloc_pages_node(node,
Christoph Lameter4199cfa2007-10-16 01:25:34 -0700362 GFP_KERNEL | __GFP_ZERO,
Christoph Lameterfbd98162006-09-25 23:31:45 -0700363 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!page)
365 goto out_free;
366 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
367 }
368 break;
369 out_free:
370 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
371 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
372 __free_page(page);
373 return NOTIFY_BAD;
374 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700375 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 cpu_set(cpu, prof_cpu_mask);
377 break;
378 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700379 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700381 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 cpu_clear(cpu, prof_cpu_mask);
383 if (per_cpu(cpu_profile_hits, cpu)[0]) {
384 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
385 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
386 __free_page(page);
387 }
388 if (per_cpu(cpu_profile_hits, cpu)[1]) {
389 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
390 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
391 __free_page(page);
392 }
393 break;
394 }
395 return NOTIFY_OK;
396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#else /* !CONFIG_SMP */
398#define profile_flip_buffers() do { } while (0)
399#define profile_discard_flip_buffers() do { } while (0)
Ingo Molnar023160672006-12-06 20:38:17 -0800400#define profile_cpu_callback NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Ingo Molnarece8a682006-12-06 20:37:24 -0800402void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 unsigned long pc;
405
406 if (prof_on != type || !prof_buffer)
407 return;
408 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
Ingo Molnarece8a682006-12-06 20:37:24 -0800409 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411#endif /* !CONFIG_SMP */
412
Andrew Mortonbbe1a59b2007-01-22 20:40:33 -0800413EXPORT_SYMBOL_GPL(profile_hits);
414
David Howells7d12e782006-10-05 14:55:46 +0100415void profile_tick(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
David Howells7d12e782006-10-05 14:55:46 +0100417 struct pt_regs *regs = get_irq_regs();
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (type == CPU_PROFILING && timer_hook)
420 timer_hook(regs);
421 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
422 profile_hit(type, (void *)profile_pc(regs));
423}
424
425#ifdef CONFIG_PROC_FS
426#include <linux/proc_fs.h>
427#include <asm/uaccess.h>
428#include <asm/ptrace.h>
429
430static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
431 int count, int *eof, void *data)
432{
433 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
434 if (count - len < 2)
435 return -EINVAL;
436 len += sprintf(page + len, "\n");
437 return len;
438}
439
440static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
441 unsigned long count, void *data)
442{
443 cpumask_t *mask = (cpumask_t *)data;
444 unsigned long full_count = count, err;
445 cpumask_t new_value;
446
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700447 err = cpumask_parse_user(buffer, count, new_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (err)
449 return err;
450
451 *mask = new_value;
452 return full_count;
453}
454
455void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
456{
457 struct proc_dir_entry *entry;
458
459 /* create /proc/irq/prof_cpu_mask */
460 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
461 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 entry->data = (void *)&prof_cpu_mask;
463 entry->read_proc = prof_cpu_mask_read_proc;
464 entry->write_proc = prof_cpu_mask_write_proc;
465}
466
467/*
468 * This function accesses profiling information. The returned data is
469 * binary: the sampling step and the actual contents of the profile
470 * buffer. Use of the program readprofile is recommended in order to
471 * get meaningful info out of these data.
472 */
473static ssize_t
474read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
475{
476 unsigned long p = *ppos;
477 ssize_t read;
478 char * pnt;
479 unsigned int sample_step = 1 << prof_shift;
480
481 profile_flip_buffers();
482 if (p >= (prof_len+1)*sizeof(unsigned int))
483 return 0;
484 if (count > (prof_len+1)*sizeof(unsigned int) - p)
485 count = (prof_len+1)*sizeof(unsigned int) - p;
486 read = 0;
487
488 while (p < sizeof(unsigned int) && count > 0) {
Heiko Carstens064b0222006-12-06 20:36:37 -0800489 if (put_user(*((char *)(&sample_step)+p),buf))
490 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 buf++; p++; count--; read++;
492 }
493 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
494 if (copy_to_user(buf,(void *)pnt,count))
495 return -EFAULT;
496 read += count;
497 *ppos += read;
498 return read;
499}
500
501/*
502 * Writing to /proc/profile resets the counters
503 *
504 * Writing a 'profiling multiplier' value into it also re-sets the profiling
505 * interrupt frequency, on architectures that support this.
506 */
507static ssize_t write_profile(struct file *file, const char __user *buf,
508 size_t count, loff_t *ppos)
509{
510#ifdef CONFIG_SMP
511 extern int setup_profiling_timer (unsigned int multiplier);
512
513 if (count == sizeof(int)) {
514 unsigned int multiplier;
515
516 if (copy_from_user(&multiplier, buf, sizeof(int)))
517 return -EFAULT;
518
519 if (setup_profiling_timer(multiplier))
520 return -EINVAL;
521 }
522#endif
523 profile_discard_flip_buffers();
524 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
525 return count;
526}
527
Helge Deller15ad7cd2006-12-06 20:40:36 -0800528static const struct file_operations proc_profile_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 .read = read_profile,
530 .write = write_profile,
531};
532
533#ifdef CONFIG_SMP
534static void __init profile_nop(void *unused)
535{
536}
537
538static int __init create_hash_tables(void)
539{
540 int cpu;
541
542 for_each_online_cpu(cpu) {
543 int node = cpu_to_node(cpu);
544 struct page *page;
545
Christoph Lameterfbd98162006-09-25 23:31:45 -0700546 page = alloc_pages_node(node,
547 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
548 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!page)
550 goto out_cleanup;
551 per_cpu(cpu_profile_hits, cpu)[1]
552 = (struct profile_hit *)page_address(page);
Christoph Lameterfbd98162006-09-25 23:31:45 -0700553 page = alloc_pages_node(node,
554 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
555 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!page)
557 goto out_cleanup;
558 per_cpu(cpu_profile_hits, cpu)[0]
559 = (struct profile_hit *)page_address(page);
560 }
561 return 0;
562out_cleanup:
563 prof_on = 0;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700564 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 on_each_cpu(profile_nop, NULL, 0, 1);
566 for_each_online_cpu(cpu) {
567 struct page *page;
568
569 if (per_cpu(cpu_profile_hits, cpu)[0]) {
570 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]);
571 per_cpu(cpu_profile_hits, cpu)[0] = NULL;
572 __free_page(page);
573 }
574 if (per_cpu(cpu_profile_hits, cpu)[1]) {
575 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
576 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
577 __free_page(page);
578 }
579 }
580 return -1;
581}
582#else
583#define create_hash_tables() ({ 0; })
584#endif
585
586static int __init create_proc_profile(void)
587{
588 struct proc_dir_entry *entry;
589
590 if (!prof_on)
591 return 0;
592 if (create_hash_tables())
593 return -1;
594 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
595 return 0;
596 entry->proc_fops = &proc_profile_operations;
597 entry->size = (1+prof_len) * sizeof(atomic_t);
598 hotcpu_notifier(profile_cpu_callback, 0);
599 return 0;
600}
601module_init(create_proc_profile);
602#endif /* CONFIG_PROC_FS */