blob: 5b20fe977bed0812ccb79f6cd76c43a9e0890720 [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 */
Christoph Lameter6c036522005-07-07 17:56:59 -070040int (*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))) {
63 prof_on = SLEEP_PROFILING;
64 if (str[strlen(sleepstr)] == ',')
65 str += strlen(sleepstr) + 1;
66 if (get_option(&str, &par))
67 prof_shift = par;
68 printk(KERN_INFO
69 "kernel sleep profiling enabled (shift: %ld)\n",
70 prof_shift);
Ingo Molnara75acf82007-01-05 16:36:29 -080071 } else if (!strncmp(str, schedstr, strlen(schedstr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 prof_on = SCHED_PROFILING;
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070073 if (str[strlen(schedstr)] == ',')
74 str += strlen(schedstr) + 1;
75 if (get_option(&str, &par))
76 prof_shift = par;
77 printk(KERN_INFO
78 "kernel schedule profiling enabled (shift: %ld)\n",
79 prof_shift);
Ingo Molnar07031e12007-01-10 23:15:38 -080080 } 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))
85 prof_shift = par;
86 printk(KERN_INFO
87 "kernel KVM profiling enabled (shift: %ld)\n",
88 prof_shift);
William Lee Irwin IIIdfaa9c92005-05-16 21:53:58 -070089 } else if (get_option(&str, &par)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 prof_shift = par;
91 prof_on = CPU_PROFILING;
92 printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n",
93 prof_shift);
94 }
95 return 1;
96}
97__setup("profile=", profile_setup);
98
99
100void __init profile_init(void)
101{
102 if (!prof_on)
103 return;
104
105 /* only text is profiled */
106 prof_len = (_etext - _stext) >> prof_shift;
107 prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
108}
109
110/* Profile event notifications */
111
112#ifdef CONFIG_PROFILING
113
Alan Sterne041c682006-03-27 01:16:30 -0800114static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
115static ATOMIC_NOTIFIER_HEAD(task_free_notifier);
116static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118void profile_task_exit(struct task_struct * task)
119{
Alan Sterne041c682006-03-27 01:16:30 -0800120 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123int profile_handoff_task(struct task_struct * task)
124{
125 int ret;
Alan Sterne041c682006-03-27 01:16:30 -0800126 ret = atomic_notifier_call_chain(&task_free_notifier, 0, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return (ret == NOTIFY_OK) ? 1 : 0;
128}
129
130void profile_munmap(unsigned long addr)
131{
Alan Sterne041c682006-03-27 01:16:30 -0800132 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135int task_handoff_register(struct notifier_block * n)
136{
Alan Sterne041c682006-03-27 01:16:30 -0800137 return atomic_notifier_chain_register(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140int task_handoff_unregister(struct notifier_block * n)
141{
Alan Sterne041c682006-03-27 01:16:30 -0800142 return atomic_notifier_chain_unregister(&task_free_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145int profile_event_register(enum profile_type type, struct notifier_block * n)
146{
147 int err = -EINVAL;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 switch (type) {
150 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800151 err = blocking_notifier_chain_register(
152 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800155 err = blocking_notifier_chain_register(
156 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 break;
158 }
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return err;
161}
162
163
164int profile_event_unregister(enum profile_type type, struct notifier_block * n)
165{
166 int err = -EINVAL;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 switch (type) {
169 case PROFILE_TASK_EXIT:
Alan Sterne041c682006-03-27 01:16:30 -0800170 err = blocking_notifier_chain_unregister(
171 &task_exit_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
173 case PROFILE_MUNMAP:
Alan Sterne041c682006-03-27 01:16:30 -0800174 err = blocking_notifier_chain_unregister(
175 &munmap_notifier, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 }
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return err;
180}
181
182int register_timer_hook(int (*hook)(struct pt_regs *))
183{
184 if (timer_hook)
185 return -EBUSY;
186 timer_hook = hook;
187 return 0;
188}
189
190void unregister_timer_hook(int (*hook)(struct pt_regs *))
191{
192 WARN_ON(hook != timer_hook);
193 timer_hook = NULL;
194 /* make sure all CPUs see the NULL hook */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -0700195 synchronize_sched(); /* Allow ongoing interrupts to complete. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198EXPORT_SYMBOL_GPL(register_timer_hook);
199EXPORT_SYMBOL_GPL(unregister_timer_hook);
200EXPORT_SYMBOL_GPL(task_handoff_register);
201EXPORT_SYMBOL_GPL(task_handoff_unregister);
202
203#endif /* CONFIG_PROFILING */
204
205EXPORT_SYMBOL_GPL(profile_event_register);
206EXPORT_SYMBOL_GPL(profile_event_unregister);
207
208#ifdef CONFIG_SMP
209/*
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.
218 *
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
Ingo Molnarece8a682006-12-06 20:37:24 -0800231 * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from
232 * process context).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * 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.
237 *
238 * -- wli
239 */
240static void __profile_flip_buffers(void *unused)
241{
242 int cpu = smp_processor_id();
243
244 per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu);
245}
246
247static void profile_flip_buffers(void)
248{
249 int i, j, cpu;
250
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800251 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 j = per_cpu(cpu_profile_flip, get_cpu());
253 put_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) {
258 if (!hits[i].hits) {
259 if (hits[i].pc)
260 hits[i].pc = 0;
261 continue;
262 }
263 atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]);
264 hits[i].hits = hits[i].pc = 0;
265 }
266 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800267 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270static void profile_discard_flip_buffers(void)
271{
272 int i, cpu;
273
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800274 mutex_lock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 i = per_cpu(cpu_profile_flip, get_cpu());
276 put_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));
281 }
Arjan van de Ven97d1f152006-03-23 03:00:24 -0800282 mutex_unlock(&profile_flip_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Ingo Molnarece8a682006-12-06 20:37:24 -0800285void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 unsigned long primary, secondary, flags, pc = (unsigned long)__pc;
288 int i, j, cpu;
289 struct profile_hit *hits;
290
291 if (prof_on != type || !prof_buffer)
292 return;
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;
296 cpu = get_cpu();
297 hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)];
298 if (!hits) {
299 put_cpu();
300 return;
301 }
Ingo Molnarece8a682006-12-06 20:37:24 -0800302 /*
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:
306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 local_irq_save(flags);
308 do {
309 for (j = 0; j < PROFILE_GRPSZ; ++j) {
310 if (hits[i + j].pc == pc) {
Ingo Molnarece8a682006-12-06 20:37:24 -0800311 hits[i + j].hits += nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto out;
313 } else if (!hits[i + j].hits) {
314 hits[i + j].pc = pc;
Ingo Molnarece8a682006-12-06 20:37:24 -0800315 hits[i + j].hits = nr_hits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto out;
317 }
318 }
319 i = (i + secondary) & (NR_PROFILE_HIT - 1);
320 } while (i != primary);
Ingo Molnarece8a682006-12-06 20:37:24 -0800321
322 /*
323 * Add the current hit(s) and flush the write-queue out
324 * to the global buffer:
325 */
326 atomic_add(nr_hits, &prof_buffer[pc]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 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;
330 }
331out:
332 local_irq_restore(flags);
333 put_cpu();
334}
335
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700336static int __devinit profile_cpu_callback(struct notifier_block *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned long action, void *__cpu)
338{
339 int node, cpu = (unsigned long)__cpu;
340 struct page *page;
341
342 switch (action) {
343 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700344 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 node = cpu_to_node(cpu);
346 per_cpu(cpu_profile_flip, cpu) = 0;
347 if (!per_cpu(cpu_profile_hits, cpu)[1]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700348 page = alloc_pages_node(node,
349 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
350 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!page)
352 return NOTIFY_BAD;
353 per_cpu(cpu_profile_hits, cpu)[1] = page_address(page);
354 }
355 if (!per_cpu(cpu_profile_hits, cpu)[0]) {
Christoph Lameterfbd98162006-09-25 23:31:45 -0700356 page = alloc_pages_node(node,
357 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
358 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!page)
360 goto out_free;
361 per_cpu(cpu_profile_hits, cpu)[0] = page_address(page);
362 }
363 break;
364 out_free:
365 page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]);
366 per_cpu(cpu_profile_hits, cpu)[1] = NULL;
367 __free_page(page);
368 return NOTIFY_BAD;
369 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700370 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 cpu_set(cpu, prof_cpu_mask);
372 break;
373 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700374 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700376 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 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;
381 __free_page(page);
382 }
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;
386 __free_page(page);
387 }
388 break;
389 }
390 return NOTIFY_OK;
391}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#else /* !CONFIG_SMP */
393#define profile_flip_buffers() do { } while (0)
394#define profile_discard_flip_buffers() do { } while (0)
Ingo Molnar023160672006-12-06 20:38:17 -0800395#define profile_cpu_callback NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Ingo Molnarece8a682006-12-06 20:37:24 -0800397void profile_hits(int type, void *__pc, unsigned int nr_hits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 unsigned long pc;
400
401 if (prof_on != type || !prof_buffer)
402 return;
403 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
Ingo Molnarece8a682006-12-06 20:37:24 -0800404 atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406#endif /* !CONFIG_SMP */
407
Andrew Mortonbbe1a59b2007-01-22 20:40:33 -0800408EXPORT_SYMBOL_GPL(profile_hits);
409
David Howells7d12e782006-10-05 14:55:46 +0100410void profile_tick(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
David Howells7d12e782006-10-05 14:55:46 +0100412 struct pt_regs *regs = get_irq_regs();
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (type == CPU_PROFILING && timer_hook)
415 timer_hook(regs);
416 if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask))
417 profile_hit(type, (void *)profile_pc(regs));
418}
419
420#ifdef CONFIG_PROC_FS
421#include <linux/proc_fs.h>
422#include <asm/uaccess.h>
423#include <asm/ptrace.h>
424
425static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
426 int count, int *eof, void *data)
427{
428 int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
429 if (count - len < 2)
430 return -EINVAL;
431 len += sprintf(page + len, "\n");
432 return len;
433}
434
435static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
436 unsigned long count, void *data)
437{
438 cpumask_t *mask = (cpumask_t *)data;
439 unsigned long full_count = count, err;
440 cpumask_t new_value;
441
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700442 err = cpumask_parse_user(buffer, count, new_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (err)
444 return err;
445
446 *mask = new_value;
447 return full_count;
448}
449
450void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir)
451{
452 struct proc_dir_entry *entry;
453
454 /* create /proc/irq/prof_cpu_mask */
455 if (!(entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir)))
456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 entry->data = (void *)&prof_cpu_mask;
458 entry->read_proc = prof_cpu_mask_read_proc;
459 entry->write_proc = prof_cpu_mask_write_proc;
460}
461
462/*
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.
467 */
468static ssize_t
469read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
470{
471 unsigned long p = *ppos;
472 ssize_t read;
473 char * pnt;
474 unsigned int sample_step = 1 << prof_shift;
475
476 profile_flip_buffers();
477 if (p >= (prof_len+1)*sizeof(unsigned int))
478 return 0;
479 if (count > (prof_len+1)*sizeof(unsigned int) - p)
480 count = (prof_len+1)*sizeof(unsigned int) - p;
481 read = 0;
482
483 while (p < sizeof(unsigned int) && count > 0) {
Heiko Carstens064b0222006-12-06 20:36:37 -0800484 if (put_user(*((char *)(&sample_step)+p),buf))
485 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 buf++; p++; count--; read++;
487 }
488 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
489 if (copy_to_user(buf,(void *)pnt,count))
490 return -EFAULT;
491 read += count;
492 *ppos += read;
493 return read;
494}
495
496/*
497 * Writing to /proc/profile resets the counters
498 *
499 * Writing a 'profiling multiplier' value into it also re-sets the profiling
500 * interrupt frequency, on architectures that support this.
501 */
502static ssize_t write_profile(struct file *file, const char __user *buf,
503 size_t count, loff_t *ppos)
504{
505#ifdef CONFIG_SMP
506 extern int setup_profiling_timer (unsigned int multiplier);
507
508 if (count == sizeof(int)) {
509 unsigned int multiplier;
510
511 if (copy_from_user(&multiplier, buf, sizeof(int)))
512 return -EFAULT;
513
514 if (setup_profiling_timer(multiplier))
515 return -EINVAL;
516 }
517#endif
518 profile_discard_flip_buffers();
519 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
520 return count;
521}
522
Helge Deller15ad7cd2006-12-06 20:40:36 -0800523static const struct file_operations proc_profile_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 .read = read_profile,
525 .write = write_profile,
526};
527
528#ifdef CONFIG_SMP
529static void __init profile_nop(void *unused)
530{
531}
532
533static int __init create_hash_tables(void)
534{
535 int cpu;
536
537 for_each_online_cpu(cpu) {
538 int node = cpu_to_node(cpu);
539 struct page *page;
540
Christoph Lameterfbd98162006-09-25 23:31:45 -0700541 page = alloc_pages_node(node,
542 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
543 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (!page)
545 goto out_cleanup;
546 per_cpu(cpu_profile_hits, cpu)[1]
547 = (struct profile_hit *)page_address(page);
Christoph Lameterfbd98162006-09-25 23:31:45 -0700548 page = alloc_pages_node(node,
549 GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
550 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!page)
552 goto out_cleanup;
553 per_cpu(cpu_profile_hits, cpu)[0]
554 = (struct profile_hit *)page_address(page);
555 }
556 return 0;
557out_cleanup:
558 prof_on = 0;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700559 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 on_each_cpu(profile_nop, NULL, 0, 1);
561 for_each_online_cpu(cpu) {
562 struct page *page;
563
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;
567 __free_page(page);
568 }
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;
572 __free_page(page);
573 }
574 }
575 return -1;
576}
577#else
578#define create_hash_tables() ({ 0; })
579#endif
580
581static int __init create_proc_profile(void)
582{
583 struct proc_dir_entry *entry;
584
585 if (!prof_on)
586 return 0;
587 if (create_hash_tables())
588 return -1;
589 if (!(entry = create_proc_entry("profile", S_IWUSR | S_IRUGO, NULL)))
590 return 0;
591 entry->proc_fops = &proc_profile_operations;
592 entry->size = (1+prof_len) * sizeof(atomic_t);
593 hotcpu_notifier(profile_cpu_callback, 0);
594 return 0;
595}
596module_init(create_proc_profile);
597#endif /* CONFIG_PROC_FS */