11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/profile.c 31da177e4SLinus Torvalds * Simple profiling. Manages a direct-mapped profile hit count buffer, 41da177e4SLinus Torvalds * with configurable resolution, support for restricting the cpus on 51da177e4SLinus Torvalds * which profiling is done, and switching between cpu time and 61da177e4SLinus Torvalds * schedule() calls via kernel command line parameters passed at boot. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Scheduler profiling support, Arjan van de Ven and Ingo Molnar, 91da177e4SLinus Torvalds * Red Hat, July 2004 101da177e4SLinus Torvalds * Consolidation of architecture support code for profiling, 111da177e4SLinus Torvalds * William Irwin, Oracle, July 2004 121da177e4SLinus Torvalds * Amortized hit count accounting via per-cpu open-addressed hashtables 131da177e4SLinus Torvalds * to resolve timer interrupt livelocks, William Irwin, Oracle, 2004 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/profile.h> 181da177e4SLinus Torvalds #include <linux/bootmem.h> 191da177e4SLinus Torvalds #include <linux/notifier.h> 201da177e4SLinus Torvalds #include <linux/mm.h> 211da177e4SLinus Torvalds #include <linux/cpumask.h> 221da177e4SLinus Torvalds #include <linux/cpu.h> 231da177e4SLinus Torvalds #include <linux/highmem.h> 2497d1f15bSArjan van de Ven #include <linux/mutex.h> 2522b8ce94SDave Hansen #include <linux/slab.h> 2622b8ce94SDave Hansen #include <linux/vmalloc.h> 271da177e4SLinus Torvalds #include <asm/sections.h> 287d12e780SDavid Howells #include <asm/irq_regs.h> 29e8edc6e0SAlexey Dobriyan #include <asm/ptrace.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds struct profile_hit { 321da177e4SLinus Torvalds u32 pc, hits; 331da177e4SLinus Torvalds }; 341da177e4SLinus Torvalds #define PROFILE_GRPSHIFT 3 351da177e4SLinus Torvalds #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT) 361da177e4SLinus Torvalds #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit)) 371da177e4SLinus Torvalds #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ) 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds /* Oprofile timer tick hook */ 40b012d346SAdrian Bunk static int (*timer_hook)(struct pt_regs *) __read_mostly; 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds static atomic_t *prof_buffer; 431da177e4SLinus Torvalds static unsigned long prof_len, prof_shift; 4407031e14SIngo Molnar 45ece8a684SIngo Molnar int prof_on __read_mostly; 4607031e14SIngo Molnar EXPORT_SYMBOL_GPL(prof_on); 4707031e14SIngo Molnar 481da177e4SLinus Torvalds static cpumask_t prof_cpu_mask = CPU_MASK_ALL; 491da177e4SLinus Torvalds #ifdef CONFIG_SMP 501da177e4SLinus Torvalds static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits); 511da177e4SLinus Torvalds static DEFINE_PER_CPU(int, cpu_profile_flip); 5297d1f15bSArjan van de Ven static DEFINE_MUTEX(profile_flip_mutex); 531da177e4SLinus Torvalds #endif /* CONFIG_SMP */ 541da177e4SLinus Torvalds 5522b8ce94SDave Hansen int profile_setup(char *str) 561da177e4SLinus Torvalds { 5722b8ce94SDave Hansen static char schedstr[] = "schedule"; 5822b8ce94SDave Hansen static char sleepstr[] = "sleep"; 5922b8ce94SDave Hansen static char kvmstr[] = "kvm"; 601da177e4SLinus Torvalds int par; 611da177e4SLinus Torvalds 62ece8a684SIngo Molnar if (!strncmp(str, sleepstr, strlen(sleepstr))) { 63b3da2a73SMel Gorman #ifdef CONFIG_SCHEDSTATS 64ece8a684SIngo Molnar prof_on = SLEEP_PROFILING; 65ece8a684SIngo Molnar if (str[strlen(sleepstr)] == ',') 66ece8a684SIngo Molnar str += strlen(sleepstr) + 1; 67ece8a684SIngo Molnar if (get_option(&str, &par)) 68ece8a684SIngo Molnar prof_shift = par; 69ece8a684SIngo Molnar printk(KERN_INFO 70ece8a684SIngo Molnar "kernel sleep profiling enabled (shift: %ld)\n", 71ece8a684SIngo Molnar prof_shift); 72b3da2a73SMel Gorman #else 73b3da2a73SMel Gorman printk(KERN_WARNING 74b3da2a73SMel Gorman "kernel sleep profiling requires CONFIG_SCHEDSTATS\n"); 75b3da2a73SMel Gorman #endif /* CONFIG_SCHEDSTATS */ 76a75acf85SIngo Molnar } else if (!strncmp(str, schedstr, strlen(schedstr))) { 771da177e4SLinus Torvalds prof_on = SCHED_PROFILING; 78dfaa9c94SWilliam Lee Irwin III if (str[strlen(schedstr)] == ',') 79dfaa9c94SWilliam Lee Irwin III str += strlen(schedstr) + 1; 80dfaa9c94SWilliam Lee Irwin III if (get_option(&str, &par)) 81dfaa9c94SWilliam Lee Irwin III prof_shift = par; 82dfaa9c94SWilliam Lee Irwin III printk(KERN_INFO 83dfaa9c94SWilliam Lee Irwin III "kernel schedule profiling enabled (shift: %ld)\n", 84dfaa9c94SWilliam Lee Irwin III prof_shift); 8507031e14SIngo Molnar } else if (!strncmp(str, kvmstr, strlen(kvmstr))) { 8607031e14SIngo Molnar prof_on = KVM_PROFILING; 8707031e14SIngo Molnar if (str[strlen(kvmstr)] == ',') 8807031e14SIngo Molnar str += strlen(kvmstr) + 1; 8907031e14SIngo Molnar if (get_option(&str, &par)) 9007031e14SIngo Molnar prof_shift = par; 9107031e14SIngo Molnar printk(KERN_INFO 9207031e14SIngo Molnar "kernel KVM profiling enabled (shift: %ld)\n", 9307031e14SIngo Molnar prof_shift); 94dfaa9c94SWilliam Lee Irwin III } else if (get_option(&str, &par)) { 951da177e4SLinus Torvalds prof_shift = par; 961da177e4SLinus Torvalds prof_on = CPU_PROFILING; 971da177e4SLinus Torvalds printk(KERN_INFO "kernel profiling enabled (shift: %ld)\n", 981da177e4SLinus Torvalds prof_shift); 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds return 1; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds __setup("profile=", profile_setup); 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds 105ce05fcc3SPaul Mundt int __ref profile_init(void) 1061da177e4SLinus Torvalds { 10722b8ce94SDave Hansen int buffer_bytes; 1081da177e4SLinus Torvalds if (!prof_on) 10922b8ce94SDave Hansen return 0; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* only text is profiled */ 1121da177e4SLinus Torvalds prof_len = (_etext - _stext) >> prof_shift; 11322b8ce94SDave Hansen buffer_bytes = prof_len*sizeof(atomic_t); 11422b8ce94SDave Hansen if (!slab_is_available()) { 11522b8ce94SDave Hansen prof_buffer = alloc_bootmem(buffer_bytes); 11622b8ce94SDave Hansen return 0; 11722b8ce94SDave Hansen } 11822b8ce94SDave Hansen 11922b8ce94SDave Hansen prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL); 12022b8ce94SDave Hansen if (prof_buffer) 12122b8ce94SDave Hansen return 0; 12222b8ce94SDave Hansen 12322b8ce94SDave Hansen prof_buffer = alloc_pages_exact(buffer_bytes, GFP_KERNEL|__GFP_ZERO); 12422b8ce94SDave Hansen if (prof_buffer) 12522b8ce94SDave Hansen return 0; 12622b8ce94SDave Hansen 12722b8ce94SDave Hansen prof_buffer = vmalloc(buffer_bytes); 12822b8ce94SDave Hansen if (prof_buffer) 12922b8ce94SDave Hansen return 0; 13022b8ce94SDave Hansen 13122b8ce94SDave Hansen return -ENOMEM; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* Profile event notifications */ 1351da177e4SLinus Torvalds 136e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(task_exit_notifier); 137e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(task_free_notifier); 138e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(munmap_notifier); 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds void profile_task_exit(struct task_struct *task) 1411da177e4SLinus Torvalds { 142e041c683SAlan Stern blocking_notifier_call_chain(&task_exit_notifier, 0, task); 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds int profile_handoff_task(struct task_struct *task) 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds int ret; 148e041c683SAlan Stern ret = atomic_notifier_call_chain(&task_free_notifier, 0, task); 1491da177e4SLinus Torvalds return (ret == NOTIFY_OK) ? 1 : 0; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds void profile_munmap(unsigned long addr) 1531da177e4SLinus Torvalds { 154e041c683SAlan Stern blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds int task_handoff_register(struct notifier_block *n) 1581da177e4SLinus Torvalds { 159e041c683SAlan Stern return atomic_notifier_chain_register(&task_free_notifier, n); 1601da177e4SLinus Torvalds } 1611ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(task_handoff_register); 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds int task_handoff_unregister(struct notifier_block *n) 1641da177e4SLinus Torvalds { 165e041c683SAlan Stern return atomic_notifier_chain_unregister(&task_free_notifier, n); 1661da177e4SLinus Torvalds } 1671ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(task_handoff_unregister); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds int profile_event_register(enum profile_type type, struct notifier_block *n) 1701da177e4SLinus Torvalds { 1711da177e4SLinus Torvalds int err = -EINVAL; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds switch (type) { 1741da177e4SLinus Torvalds case PROFILE_TASK_EXIT: 175e041c683SAlan Stern err = blocking_notifier_chain_register( 176e041c683SAlan Stern &task_exit_notifier, n); 1771da177e4SLinus Torvalds break; 1781da177e4SLinus Torvalds case PROFILE_MUNMAP: 179e041c683SAlan Stern err = blocking_notifier_chain_register( 180e041c683SAlan Stern &munmap_notifier, n); 1811da177e4SLinus Torvalds break; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds return err; 1851da177e4SLinus Torvalds } 1861ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(profile_event_register); 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds int profile_event_unregister(enum profile_type type, struct notifier_block *n) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds int err = -EINVAL; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds switch (type) { 1931da177e4SLinus Torvalds case PROFILE_TASK_EXIT: 194e041c683SAlan Stern err = blocking_notifier_chain_unregister( 195e041c683SAlan Stern &task_exit_notifier, n); 1961da177e4SLinus Torvalds break; 1971da177e4SLinus Torvalds case PROFILE_MUNMAP: 198e041c683SAlan Stern err = blocking_notifier_chain_unregister( 199e041c683SAlan Stern &munmap_notifier, n); 2001da177e4SLinus Torvalds break; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds return err; 2041da177e4SLinus Torvalds } 2051ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(profile_event_unregister); 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds int register_timer_hook(int (*hook)(struct pt_regs *)) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds if (timer_hook) 2101da177e4SLinus Torvalds return -EBUSY; 2111da177e4SLinus Torvalds timer_hook = hook; 2121da177e4SLinus Torvalds return 0; 2131da177e4SLinus Torvalds } 2141ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(register_timer_hook); 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds void unregister_timer_hook(int (*hook)(struct pt_regs *)) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds WARN_ON(hook != timer_hook); 2191da177e4SLinus Torvalds timer_hook = NULL; 2201da177e4SLinus Torvalds /* make sure all CPUs see the NULL hook */ 221fbd568a3SPaul E. McKenney synchronize_sched(); /* Allow ongoing interrupts to complete. */ 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(unregister_timer_hook); 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds #ifdef CONFIG_SMP 2271da177e4SLinus Torvalds /* 2281da177e4SLinus Torvalds * Each cpu has a pair of open-addressed hashtables for pending 2291da177e4SLinus Torvalds * profile hits. read_profile() IPI's all cpus to request them 2301da177e4SLinus Torvalds * to flip buffers and flushes their contents to prof_buffer itself. 2311da177e4SLinus Torvalds * Flip requests are serialized by the profile_flip_mutex. The sole 2321da177e4SLinus Torvalds * use of having a second hashtable is for avoiding cacheline 2331da177e4SLinus Torvalds * contention that would otherwise happen during flushes of pending 2341da177e4SLinus Torvalds * profile hits required for the accuracy of reported profile hits 2351da177e4SLinus Torvalds * and so resurrect the interrupt livelock issue. 2361da177e4SLinus Torvalds * 2371da177e4SLinus Torvalds * The open-addressed hashtables are indexed by profile buffer slot 2381da177e4SLinus Torvalds * and hold the number of pending hits to that profile buffer slot on 2391da177e4SLinus Torvalds * a cpu in an entry. When the hashtable overflows, all pending hits 2401da177e4SLinus Torvalds * are accounted to their corresponding profile buffer slots with 2411da177e4SLinus Torvalds * atomic_add() and the hashtable emptied. As numerous pending hits 2421da177e4SLinus Torvalds * may be accounted to a profile buffer slot in a hashtable entry, 2431da177e4SLinus Torvalds * this amortizes a number of atomic profile buffer increments likely 2441da177e4SLinus Torvalds * to be far larger than the number of entries in the hashtable, 2451da177e4SLinus Torvalds * particularly given that the number of distinct profile buffer 2461da177e4SLinus Torvalds * positions to which hits are accounted during short intervals (e.g. 2471da177e4SLinus Torvalds * several seconds) is usually very small. Exclusion from buffer 2481da177e4SLinus Torvalds * flipping is provided by interrupt disablement (note that for 249ece8a684SIngo Molnar * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from 250ece8a684SIngo Molnar * process context). 2511da177e4SLinus Torvalds * The hash function is meant to be lightweight as opposed to strong, 2521da177e4SLinus Torvalds * and was vaguely inspired by ppc64 firmware-supported inverted 2531da177e4SLinus Torvalds * pagetable hash functions, but uses a full hashtable full of finite 2541da177e4SLinus Torvalds * collision chains, not just pairs of them. 2551da177e4SLinus Torvalds * 2561da177e4SLinus Torvalds * -- wli 2571da177e4SLinus Torvalds */ 2581da177e4SLinus Torvalds static void __profile_flip_buffers(void *unused) 2591da177e4SLinus Torvalds { 2601da177e4SLinus Torvalds int cpu = smp_processor_id(); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds static void profile_flip_buffers(void) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds int i, j, cpu; 2681da177e4SLinus Torvalds 26997d1f15bSArjan van de Ven mutex_lock(&profile_flip_mutex); 2701da177e4SLinus Torvalds j = per_cpu(cpu_profile_flip, get_cpu()); 2711da177e4SLinus Torvalds put_cpu(); 27215c8b6c1SJens Axboe on_each_cpu(__profile_flip_buffers, NULL, 1); 2731da177e4SLinus Torvalds for_each_online_cpu(cpu) { 2741da177e4SLinus Torvalds struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j]; 2751da177e4SLinus Torvalds for (i = 0; i < NR_PROFILE_HIT; ++i) { 2761da177e4SLinus Torvalds if (!hits[i].hits) { 2771da177e4SLinus Torvalds if (hits[i].pc) 2781da177e4SLinus Torvalds hits[i].pc = 0; 2791da177e4SLinus Torvalds continue; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]); 2821da177e4SLinus Torvalds hits[i].hits = hits[i].pc = 0; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds } 28597d1f15bSArjan van de Ven mutex_unlock(&profile_flip_mutex); 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds static void profile_discard_flip_buffers(void) 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds int i, cpu; 2911da177e4SLinus Torvalds 29297d1f15bSArjan van de Ven mutex_lock(&profile_flip_mutex); 2931da177e4SLinus Torvalds i = per_cpu(cpu_profile_flip, get_cpu()); 2941da177e4SLinus Torvalds put_cpu(); 29515c8b6c1SJens Axboe on_each_cpu(__profile_flip_buffers, NULL, 1); 2961da177e4SLinus Torvalds for_each_online_cpu(cpu) { 2971da177e4SLinus Torvalds struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i]; 2981da177e4SLinus Torvalds memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit)); 2991da177e4SLinus Torvalds } 30097d1f15bSArjan van de Ven mutex_unlock(&profile_flip_mutex); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 303ece8a684SIngo Molnar void profile_hits(int type, void *__pc, unsigned int nr_hits) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds unsigned long primary, secondary, flags, pc = (unsigned long)__pc; 3061da177e4SLinus Torvalds int i, j, cpu; 3071da177e4SLinus Torvalds struct profile_hit *hits; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds if (prof_on != type || !prof_buffer) 3101da177e4SLinus Torvalds return; 3111da177e4SLinus Torvalds pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1); 3121da177e4SLinus Torvalds i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT; 3131da177e4SLinus Torvalds secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT; 3141da177e4SLinus Torvalds cpu = get_cpu(); 3151da177e4SLinus Torvalds hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)]; 3161da177e4SLinus Torvalds if (!hits) { 3171da177e4SLinus Torvalds put_cpu(); 3181da177e4SLinus Torvalds return; 3191da177e4SLinus Torvalds } 320ece8a684SIngo Molnar /* 321ece8a684SIngo Molnar * We buffer the global profiler buffer into a per-CPU 322ece8a684SIngo Molnar * queue and thus reduce the number of global (and possibly 323ece8a684SIngo Molnar * NUMA-alien) accesses. The write-queue is self-coalescing: 324ece8a684SIngo Molnar */ 3251da177e4SLinus Torvalds local_irq_save(flags); 3261da177e4SLinus Torvalds do { 3271da177e4SLinus Torvalds for (j = 0; j < PROFILE_GRPSZ; ++j) { 3281da177e4SLinus Torvalds if (hits[i + j].pc == pc) { 329ece8a684SIngo Molnar hits[i + j].hits += nr_hits; 3301da177e4SLinus Torvalds goto out; 3311da177e4SLinus Torvalds } else if (!hits[i + j].hits) { 3321da177e4SLinus Torvalds hits[i + j].pc = pc; 333ece8a684SIngo Molnar hits[i + j].hits = nr_hits; 3341da177e4SLinus Torvalds goto out; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds i = (i + secondary) & (NR_PROFILE_HIT - 1); 3381da177e4SLinus Torvalds } while (i != primary); 339ece8a684SIngo Molnar 340ece8a684SIngo Molnar /* 341ece8a684SIngo Molnar * Add the current hit(s) and flush the write-queue out 342ece8a684SIngo Molnar * to the global buffer: 343ece8a684SIngo Molnar */ 344ece8a684SIngo Molnar atomic_add(nr_hits, &prof_buffer[pc]); 3451da177e4SLinus Torvalds for (i = 0; i < NR_PROFILE_HIT; ++i) { 3461da177e4SLinus Torvalds atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]); 3471da177e4SLinus Torvalds hits[i].pc = hits[i].hits = 0; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds out: 3501da177e4SLinus Torvalds local_irq_restore(flags); 3511da177e4SLinus Torvalds put_cpu(); 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 354*84196414SAl Viro static int __cpuinit profile_cpu_callback(struct notifier_block *info, 3551da177e4SLinus Torvalds unsigned long action, void *__cpu) 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds int node, cpu = (unsigned long)__cpu; 3581da177e4SLinus Torvalds struct page *page; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds switch (action) { 3611da177e4SLinus Torvalds case CPU_UP_PREPARE: 3628bb78442SRafael J. Wysocki case CPU_UP_PREPARE_FROZEN: 3631da177e4SLinus Torvalds node = cpu_to_node(cpu); 3641da177e4SLinus Torvalds per_cpu(cpu_profile_flip, cpu) = 0; 3651da177e4SLinus Torvalds if (!per_cpu(cpu_profile_hits, cpu)[1]) { 366fbd98167SChristoph Lameter page = alloc_pages_node(node, 3674199cfa0SChristoph Lameter GFP_KERNEL | __GFP_ZERO, 368fbd98167SChristoph Lameter 0); 3691da177e4SLinus Torvalds if (!page) 3701da177e4SLinus Torvalds return NOTIFY_BAD; 3711da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[1] = page_address(page); 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds if (!per_cpu(cpu_profile_hits, cpu)[0]) { 374fbd98167SChristoph Lameter page = alloc_pages_node(node, 3754199cfa0SChristoph Lameter GFP_KERNEL | __GFP_ZERO, 376fbd98167SChristoph Lameter 0); 3771da177e4SLinus Torvalds if (!page) 3781da177e4SLinus Torvalds goto out_free; 3791da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[0] = page_address(page); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds break; 3821da177e4SLinus Torvalds out_free: 3831da177e4SLinus Torvalds page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]); 3841da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[1] = NULL; 3851da177e4SLinus Torvalds __free_page(page); 3861da177e4SLinus Torvalds return NOTIFY_BAD; 3871da177e4SLinus Torvalds case CPU_ONLINE: 3888bb78442SRafael J. Wysocki case CPU_ONLINE_FROZEN: 3891da177e4SLinus Torvalds cpu_set(cpu, prof_cpu_mask); 3901da177e4SLinus Torvalds break; 3911da177e4SLinus Torvalds case CPU_UP_CANCELED: 3928bb78442SRafael J. Wysocki case CPU_UP_CANCELED_FROZEN: 3931da177e4SLinus Torvalds case CPU_DEAD: 3948bb78442SRafael J. Wysocki case CPU_DEAD_FROZEN: 3951da177e4SLinus Torvalds cpu_clear(cpu, prof_cpu_mask); 3961da177e4SLinus Torvalds if (per_cpu(cpu_profile_hits, cpu)[0]) { 3971da177e4SLinus Torvalds page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]); 3981da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[0] = NULL; 3991da177e4SLinus Torvalds __free_page(page); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds if (per_cpu(cpu_profile_hits, cpu)[1]) { 4021da177e4SLinus Torvalds page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]); 4031da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[1] = NULL; 4041da177e4SLinus Torvalds __free_page(page); 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds break; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds return NOTIFY_OK; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds #else /* !CONFIG_SMP */ 4111da177e4SLinus Torvalds #define profile_flip_buffers() do { } while (0) 4121da177e4SLinus Torvalds #define profile_discard_flip_buffers() do { } while (0) 41302316067SIngo Molnar #define profile_cpu_callback NULL 4141da177e4SLinus Torvalds 415ece8a684SIngo Molnar void profile_hits(int type, void *__pc, unsigned int nr_hits) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds unsigned long pc; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds if (prof_on != type || !prof_buffer) 4201da177e4SLinus Torvalds return; 4211da177e4SLinus Torvalds pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift; 422ece8a684SIngo Molnar atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds #endif /* !CONFIG_SMP */ 425bbe1a59bSAndrew Morton EXPORT_SYMBOL_GPL(profile_hits); 426bbe1a59bSAndrew Morton 4277d12e780SDavid Howells void profile_tick(int type) 4281da177e4SLinus Torvalds { 4297d12e780SDavid Howells struct pt_regs *regs = get_irq_regs(); 4307d12e780SDavid Howells 4311da177e4SLinus Torvalds if (type == CPU_PROFILING && timer_hook) 4321da177e4SLinus Torvalds timer_hook(regs); 4331da177e4SLinus Torvalds if (!user_mode(regs) && cpu_isset(smp_processor_id(), prof_cpu_mask)) 4341da177e4SLinus Torvalds profile_hit(type, (void *)profile_pc(regs)); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 4381da177e4SLinus Torvalds #include <linux/proc_fs.h> 4391da177e4SLinus Torvalds #include <asm/uaccess.h> 4401da177e4SLinus Torvalds #include <asm/ptrace.h> 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds static int prof_cpu_mask_read_proc(char *page, char **start, off_t off, 4431da177e4SLinus Torvalds int count, int *eof, void *data) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds int len = cpumask_scnprintf(page, count, *(cpumask_t *)data); 4461da177e4SLinus Torvalds if (count - len < 2) 4471da177e4SLinus Torvalds return -EINVAL; 4481da177e4SLinus Torvalds len += sprintf(page + len, "\n"); 4491da177e4SLinus Torvalds return len; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521ad82fd5SPaolo Ciarrocchi static int prof_cpu_mask_write_proc(struct file *file, 4531ad82fd5SPaolo Ciarrocchi const char __user *buffer, unsigned long count, void *data) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds cpumask_t *mask = (cpumask_t *)data; 4561da177e4SLinus Torvalds unsigned long full_count = count, err; 4571da177e4SLinus Torvalds cpumask_t new_value; 4581da177e4SLinus Torvalds 45901a3ee2bSReinette Chatre err = cpumask_parse_user(buffer, count, new_value); 4601da177e4SLinus Torvalds if (err) 4611da177e4SLinus Torvalds return err; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds *mask = new_value; 4641da177e4SLinus Torvalds return full_count; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds void create_prof_cpu_mask(struct proc_dir_entry *root_irq_dir) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds struct proc_dir_entry *entry; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds /* create /proc/irq/prof_cpu_mask */ 4721ad82fd5SPaolo Ciarrocchi entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir); 4731ad82fd5SPaolo Ciarrocchi if (!entry) 4741da177e4SLinus Torvalds return; 4751da177e4SLinus Torvalds entry->data = (void *)&prof_cpu_mask; 4761da177e4SLinus Torvalds entry->read_proc = prof_cpu_mask_read_proc; 4771da177e4SLinus Torvalds entry->write_proc = prof_cpu_mask_write_proc; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds /* 4811da177e4SLinus Torvalds * This function accesses profiling information. The returned data is 4821da177e4SLinus Torvalds * binary: the sampling step and the actual contents of the profile 4831da177e4SLinus Torvalds * buffer. Use of the program readprofile is recommended in order to 4841da177e4SLinus Torvalds * get meaningful info out of these data. 4851da177e4SLinus Torvalds */ 4861da177e4SLinus Torvalds static ssize_t 4871da177e4SLinus Torvalds read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos) 4881da177e4SLinus Torvalds { 4891da177e4SLinus Torvalds unsigned long p = *ppos; 4901da177e4SLinus Torvalds ssize_t read; 4911da177e4SLinus Torvalds char *pnt; 4921da177e4SLinus Torvalds unsigned int sample_step = 1 << prof_shift; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds profile_flip_buffers(); 4951da177e4SLinus Torvalds if (p >= (prof_len+1)*sizeof(unsigned int)) 4961da177e4SLinus Torvalds return 0; 4971da177e4SLinus Torvalds if (count > (prof_len+1)*sizeof(unsigned int) - p) 4981da177e4SLinus Torvalds count = (prof_len+1)*sizeof(unsigned int) - p; 4991da177e4SLinus Torvalds read = 0; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds while (p < sizeof(unsigned int) && count > 0) { 502064b022cSHeiko Carstens if (put_user(*((char *)(&sample_step)+p), buf)) 503064b022cSHeiko Carstens return -EFAULT; 5041da177e4SLinus Torvalds buf++; p++; count--; read++; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds pnt = (char *)prof_buffer + p - sizeof(atomic_t); 5071da177e4SLinus Torvalds if (copy_to_user(buf, (void *)pnt, count)) 5081da177e4SLinus Torvalds return -EFAULT; 5091da177e4SLinus Torvalds read += count; 5101da177e4SLinus Torvalds *ppos += read; 5111da177e4SLinus Torvalds return read; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds /* 5151da177e4SLinus Torvalds * Writing to /proc/profile resets the counters 5161da177e4SLinus Torvalds * 5171da177e4SLinus Torvalds * Writing a 'profiling multiplier' value into it also re-sets the profiling 5181da177e4SLinus Torvalds * interrupt frequency, on architectures that support this. 5191da177e4SLinus Torvalds */ 5201da177e4SLinus Torvalds static ssize_t write_profile(struct file *file, const char __user *buf, 5211da177e4SLinus Torvalds size_t count, loff_t *ppos) 5221da177e4SLinus Torvalds { 5231da177e4SLinus Torvalds #ifdef CONFIG_SMP 5241da177e4SLinus Torvalds extern int setup_profiling_timer(unsigned int multiplier); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds if (count == sizeof(int)) { 5271da177e4SLinus Torvalds unsigned int multiplier; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds if (copy_from_user(&multiplier, buf, sizeof(int))) 5301da177e4SLinus Torvalds return -EFAULT; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds if (setup_profiling_timer(multiplier)) 5331da177e4SLinus Torvalds return -EINVAL; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds #endif 5361da177e4SLinus Torvalds profile_discard_flip_buffers(); 5371da177e4SLinus Torvalds memset(prof_buffer, 0, prof_len * sizeof(atomic_t)); 5381da177e4SLinus Torvalds return count; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 54115ad7cdcSHelge Deller static const struct file_operations proc_profile_operations = { 5421da177e4SLinus Torvalds .read = read_profile, 5431da177e4SLinus Torvalds .write = write_profile, 5441da177e4SLinus Torvalds }; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds #ifdef CONFIG_SMP 547e270219fSRakib Mullick static inline void profile_nop(void *unused) 5481da177e4SLinus Torvalds { 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 55122b8ce94SDave Hansen static int create_hash_tables(void) 5521da177e4SLinus Torvalds { 5531da177e4SLinus Torvalds int cpu; 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds for_each_online_cpu(cpu) { 5561da177e4SLinus Torvalds int node = cpu_to_node(cpu); 5571da177e4SLinus Torvalds struct page *page; 5581da177e4SLinus Torvalds 559fbd98167SChristoph Lameter page = alloc_pages_node(node, 560fbd98167SChristoph Lameter GFP_KERNEL | __GFP_ZERO | GFP_THISNODE, 561fbd98167SChristoph Lameter 0); 5621da177e4SLinus Torvalds if (!page) 5631da177e4SLinus Torvalds goto out_cleanup; 5641da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[1] 5651da177e4SLinus Torvalds = (struct profile_hit *)page_address(page); 566fbd98167SChristoph Lameter page = alloc_pages_node(node, 567fbd98167SChristoph Lameter GFP_KERNEL | __GFP_ZERO | GFP_THISNODE, 568fbd98167SChristoph Lameter 0); 5691da177e4SLinus Torvalds if (!page) 5701da177e4SLinus Torvalds goto out_cleanup; 5711da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[0] 5721da177e4SLinus Torvalds = (struct profile_hit *)page_address(page); 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds return 0; 5751da177e4SLinus Torvalds out_cleanup: 5761da177e4SLinus Torvalds prof_on = 0; 577d59dd462Sakpm@osdl.org smp_mb(); 57815c8b6c1SJens Axboe on_each_cpu(profile_nop, NULL, 1); 5791da177e4SLinus Torvalds for_each_online_cpu(cpu) { 5801da177e4SLinus Torvalds struct page *page; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds if (per_cpu(cpu_profile_hits, cpu)[0]) { 5831da177e4SLinus Torvalds page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[0]); 5841da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[0] = NULL; 5851da177e4SLinus Torvalds __free_page(page); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds if (per_cpu(cpu_profile_hits, cpu)[1]) { 5881da177e4SLinus Torvalds page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[1]); 5891da177e4SLinus Torvalds per_cpu(cpu_profile_hits, cpu)[1] = NULL; 5901da177e4SLinus Torvalds __free_page(page); 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds return -1; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds #else 5961da177e4SLinus Torvalds #define create_hash_tables() ({ 0; }) 5971da177e4SLinus Torvalds #endif 5981da177e4SLinus Torvalds 599*84196414SAl Viro int __ref create_proc_profile(void) /* false positive from hotcpu_notifier */ 6001da177e4SLinus Torvalds { 6011da177e4SLinus Torvalds struct proc_dir_entry *entry; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds if (!prof_on) 6041da177e4SLinus Torvalds return 0; 6051da177e4SLinus Torvalds if (create_hash_tables()) 60622b8ce94SDave Hansen return -ENOMEM; 607c33fff0aSDenis V. Lunev entry = proc_create("profile", S_IWUSR | S_IRUGO, 608c33fff0aSDenis V. Lunev NULL, &proc_profile_operations); 6091ad82fd5SPaolo Ciarrocchi if (!entry) 6101da177e4SLinus Torvalds return 0; 6111da177e4SLinus Torvalds entry->size = (1+prof_len) * sizeof(atomic_t); 6121da177e4SLinus Torvalds hotcpu_notifier(profile_cpu_callback, 0); 6131da177e4SLinus Torvalds return 0; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds module_init(create_proc_profile); 6161da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 617