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, 116d49e352SNadia Yvette Chambers * Nadia Yvette Chambers, Oracle, July 2004 121da177e4SLinus Torvalds * Amortized hit count accounting via per-cpu open-addressed hashtables 136d49e352SNadia Yvette Chambers * to resolve timer interrupt livelocks, Nadia Yvette Chambers, 146d49e352SNadia Yvette Chambers * Oracle, 2004 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 179984de1aSPaul Gortmaker #include <linux/export.h> 181da177e4SLinus Torvalds #include <linux/profile.h> 191da177e4SLinus Torvalds #include <linux/bootmem.h> 201da177e4SLinus Torvalds #include <linux/notifier.h> 211da177e4SLinus Torvalds #include <linux/mm.h> 221da177e4SLinus Torvalds #include <linux/cpumask.h> 231da177e4SLinus Torvalds #include <linux/cpu.h> 241da177e4SLinus Torvalds #include <linux/highmem.h> 2597d1f15bSArjan van de Ven #include <linux/mutex.h> 2622b8ce94SDave Hansen #include <linux/slab.h> 2722b8ce94SDave Hansen #include <linux/vmalloc.h> 281da177e4SLinus Torvalds #include <asm/sections.h> 297d12e780SDavid Howells #include <asm/irq_regs.h> 30e8edc6e0SAlexey Dobriyan #include <asm/ptrace.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds struct profile_hit { 331da177e4SLinus Torvalds u32 pc, hits; 341da177e4SLinus Torvalds }; 351da177e4SLinus Torvalds #define PROFILE_GRPSHIFT 3 361da177e4SLinus Torvalds #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT) 371da177e4SLinus Torvalds #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit)) 381da177e4SLinus Torvalds #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ) 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds static atomic_t *prof_buffer; 411da177e4SLinus Torvalds static unsigned long prof_len, prof_shift; 4207031e14SIngo Molnar 43ece8a684SIngo Molnar int prof_on __read_mostly; 4407031e14SIngo Molnar EXPORT_SYMBOL_GPL(prof_on); 4507031e14SIngo Molnar 46c309b917SRusty Russell static cpumask_var_t prof_cpu_mask; 47ade356b9SArnd Bergmann #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS) 481da177e4SLinus Torvalds static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits); 491da177e4SLinus Torvalds static DEFINE_PER_CPU(int, cpu_profile_flip); 5097d1f15bSArjan van de Ven static DEFINE_MUTEX(profile_flip_mutex); 511da177e4SLinus Torvalds #endif /* CONFIG_SMP */ 521da177e4SLinus Torvalds 5322b8ce94SDave Hansen int profile_setup(char *str) 541da177e4SLinus Torvalds { 55f3da64d1SFabian Frederick static const char schedstr[] = "schedule"; 56f3da64d1SFabian Frederick static const char sleepstr[] = "sleep"; 57f3da64d1SFabian Frederick static const char kvmstr[] = "kvm"; 581da177e4SLinus Torvalds int par; 591da177e4SLinus Torvalds 60ece8a684SIngo Molnar if (!strncmp(str, sleepstr, strlen(sleepstr))) { 61b3da2a73SMel Gorman #ifdef CONFIG_SCHEDSTATS 62cb251765SMel Gorman force_schedstat_enabled(); 63ece8a684SIngo Molnar prof_on = SLEEP_PROFILING; 64ece8a684SIngo Molnar if (str[strlen(sleepstr)] == ',') 65ece8a684SIngo Molnar str += strlen(sleepstr) + 1; 66ece8a684SIngo Molnar if (get_option(&str, &par)) 67ece8a684SIngo Molnar prof_shift = par; 68aba871f1SFabian Frederick pr_info("kernel sleep profiling enabled (shift: %ld)\n", 69ece8a684SIngo Molnar prof_shift); 70b3da2a73SMel Gorman #else 71aba871f1SFabian Frederick pr_warn("kernel sleep profiling requires CONFIG_SCHEDSTATS\n"); 72b3da2a73SMel Gorman #endif /* CONFIG_SCHEDSTATS */ 73a75acf85SIngo Molnar } else if (!strncmp(str, schedstr, strlen(schedstr))) { 741da177e4SLinus Torvalds prof_on = SCHED_PROFILING; 75dfaa9c94SWilliam Lee Irwin III if (str[strlen(schedstr)] == ',') 76dfaa9c94SWilliam Lee Irwin III str += strlen(schedstr) + 1; 77dfaa9c94SWilliam Lee Irwin III if (get_option(&str, &par)) 78dfaa9c94SWilliam Lee Irwin III prof_shift = par; 79aba871f1SFabian Frederick pr_info("kernel schedule profiling enabled (shift: %ld)\n", 80dfaa9c94SWilliam Lee Irwin III prof_shift); 8107031e14SIngo Molnar } else if (!strncmp(str, kvmstr, strlen(kvmstr))) { 8207031e14SIngo Molnar prof_on = KVM_PROFILING; 8307031e14SIngo Molnar if (str[strlen(kvmstr)] == ',') 8407031e14SIngo Molnar str += strlen(kvmstr) + 1; 8507031e14SIngo Molnar if (get_option(&str, &par)) 8607031e14SIngo Molnar prof_shift = par; 87aba871f1SFabian Frederick pr_info("kernel KVM profiling enabled (shift: %ld)\n", 8807031e14SIngo Molnar prof_shift); 89dfaa9c94SWilliam Lee Irwin III } else if (get_option(&str, &par)) { 901da177e4SLinus Torvalds prof_shift = par; 911da177e4SLinus Torvalds prof_on = CPU_PROFILING; 92aba871f1SFabian Frederick pr_info("kernel profiling enabled (shift: %ld)\n", 931da177e4SLinus Torvalds prof_shift); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds return 1; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds __setup("profile=", profile_setup); 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds 100ce05fcc3SPaul Mundt int __ref profile_init(void) 1011da177e4SLinus Torvalds { 10222b8ce94SDave Hansen int buffer_bytes; 1031da177e4SLinus Torvalds if (!prof_on) 10422b8ce94SDave Hansen return 0; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* only text is profiled */ 1071da177e4SLinus Torvalds prof_len = (_etext - _stext) >> prof_shift; 10822b8ce94SDave Hansen buffer_bytes = prof_len*sizeof(atomic_t); 10922b8ce94SDave Hansen 110c309b917SRusty Russell if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL)) 111c309b917SRusty Russell return -ENOMEM; 112c309b917SRusty Russell 113acd89579SHugh Dickins cpumask_copy(prof_cpu_mask, cpu_possible_mask); 114acd89579SHugh Dickins 115b62f495dSMel Gorman prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN); 11622b8ce94SDave Hansen if (prof_buffer) 11722b8ce94SDave Hansen return 0; 11822b8ce94SDave Hansen 119b62f495dSMel Gorman prof_buffer = alloc_pages_exact(buffer_bytes, 120b62f495dSMel Gorman GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN); 12122b8ce94SDave Hansen if (prof_buffer) 12222b8ce94SDave Hansen return 0; 12322b8ce94SDave Hansen 124559fa6e7SJesper Juhl prof_buffer = vzalloc(buffer_bytes); 125559fa6e7SJesper Juhl if (prof_buffer) 12622b8ce94SDave Hansen return 0; 12722b8ce94SDave Hansen 128c309b917SRusty Russell free_cpumask_var(prof_cpu_mask); 12922b8ce94SDave Hansen return -ENOMEM; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* Profile event notifications */ 1331da177e4SLinus Torvalds 134e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(task_exit_notifier); 135e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(task_free_notifier); 136e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(munmap_notifier); 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds void profile_task_exit(struct task_struct *task) 1391da177e4SLinus Torvalds { 140e041c683SAlan Stern blocking_notifier_call_chain(&task_exit_notifier, 0, task); 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds int profile_handoff_task(struct task_struct *task) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds int ret; 146e041c683SAlan Stern ret = atomic_notifier_call_chain(&task_free_notifier, 0, task); 1471da177e4SLinus Torvalds return (ret == NOTIFY_OK) ? 1 : 0; 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds void profile_munmap(unsigned long addr) 1511da177e4SLinus Torvalds { 152e041c683SAlan Stern blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds int task_handoff_register(struct notifier_block *n) 1561da177e4SLinus Torvalds { 157e041c683SAlan Stern return atomic_notifier_chain_register(&task_free_notifier, n); 1581da177e4SLinus Torvalds } 1591ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(task_handoff_register); 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds int task_handoff_unregister(struct notifier_block *n) 1621da177e4SLinus Torvalds { 163e041c683SAlan Stern return atomic_notifier_chain_unregister(&task_free_notifier, n); 1641da177e4SLinus Torvalds } 1651ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(task_handoff_unregister); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds int profile_event_register(enum profile_type type, struct notifier_block *n) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds int err = -EINVAL; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds switch (type) { 1721da177e4SLinus Torvalds case PROFILE_TASK_EXIT: 173e041c683SAlan Stern err = blocking_notifier_chain_register( 174e041c683SAlan Stern &task_exit_notifier, n); 1751da177e4SLinus Torvalds break; 1761da177e4SLinus Torvalds case PROFILE_MUNMAP: 177e041c683SAlan Stern err = blocking_notifier_chain_register( 178e041c683SAlan Stern &munmap_notifier, n); 1791da177e4SLinus Torvalds break; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds return err; 1831da177e4SLinus Torvalds } 1841ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(profile_event_register); 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds int profile_event_unregister(enum profile_type type, struct notifier_block *n) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds int err = -EINVAL; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds switch (type) { 1911da177e4SLinus Torvalds case PROFILE_TASK_EXIT: 192e041c683SAlan Stern err = blocking_notifier_chain_unregister( 193e041c683SAlan Stern &task_exit_notifier, n); 1941da177e4SLinus Torvalds break; 1951da177e4SLinus Torvalds case PROFILE_MUNMAP: 196e041c683SAlan Stern err = blocking_notifier_chain_unregister( 197e041c683SAlan Stern &munmap_notifier, n); 1981da177e4SLinus Torvalds break; 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds return err; 2021da177e4SLinus Torvalds } 2031ad82fd5SPaolo Ciarrocchi EXPORT_SYMBOL_GPL(profile_event_unregister); 2041da177e4SLinus Torvalds 205ade356b9SArnd Bergmann #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS) 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * Each cpu has a pair of open-addressed hashtables for pending 2081da177e4SLinus Torvalds * profile hits. read_profile() IPI's all cpus to request them 2091da177e4SLinus Torvalds * to flip buffers and flushes their contents to prof_buffer itself. 2101da177e4SLinus Torvalds * Flip requests are serialized by the profile_flip_mutex. The sole 2111da177e4SLinus Torvalds * use of having a second hashtable is for avoiding cacheline 2121da177e4SLinus Torvalds * contention that would otherwise happen during flushes of pending 2131da177e4SLinus Torvalds * profile hits required for the accuracy of reported profile hits 2141da177e4SLinus Torvalds * and so resurrect the interrupt livelock issue. 2151da177e4SLinus Torvalds * 2161da177e4SLinus Torvalds * The open-addressed hashtables are indexed by profile buffer slot 2171da177e4SLinus Torvalds * and hold the number of pending hits to that profile buffer slot on 2181da177e4SLinus Torvalds * a cpu in an entry. When the hashtable overflows, all pending hits 2191da177e4SLinus Torvalds * are accounted to their corresponding profile buffer slots with 2201da177e4SLinus Torvalds * atomic_add() and the hashtable emptied. As numerous pending hits 2211da177e4SLinus Torvalds * may be accounted to a profile buffer slot in a hashtable entry, 2221da177e4SLinus Torvalds * this amortizes a number of atomic profile buffer increments likely 2231da177e4SLinus Torvalds * to be far larger than the number of entries in the hashtable, 2241da177e4SLinus Torvalds * particularly given that the number of distinct profile buffer 2251da177e4SLinus Torvalds * positions to which hits are accounted during short intervals (e.g. 2261da177e4SLinus Torvalds * several seconds) is usually very small. Exclusion from buffer 2271da177e4SLinus Torvalds * flipping is provided by interrupt disablement (note that for 228ece8a684SIngo Molnar * SCHED_PROFILING or SLEEP_PROFILING profile_hit() may be called from 229ece8a684SIngo Molnar * process context). 2301da177e4SLinus Torvalds * The hash function is meant to be lightweight as opposed to strong, 2311da177e4SLinus Torvalds * and was vaguely inspired by ppc64 firmware-supported inverted 2321da177e4SLinus Torvalds * pagetable hash functions, but uses a full hashtable full of finite 2331da177e4SLinus Torvalds * collision chains, not just pairs of them. 2341da177e4SLinus Torvalds * 2356d49e352SNadia Yvette Chambers * -- nyc 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds static void __profile_flip_buffers(void *unused) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds int cpu = smp_processor_id(); 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds per_cpu(cpu_profile_flip, cpu) = !per_cpu(cpu_profile_flip, cpu); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds static void profile_flip_buffers(void) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds int i, j, cpu; 2471da177e4SLinus Torvalds 24897d1f15bSArjan van de Ven mutex_lock(&profile_flip_mutex); 2491da177e4SLinus Torvalds j = per_cpu(cpu_profile_flip, get_cpu()); 2501da177e4SLinus Torvalds put_cpu(); 25115c8b6c1SJens Axboe on_each_cpu(__profile_flip_buffers, NULL, 1); 2521da177e4SLinus Torvalds for_each_online_cpu(cpu) { 2531da177e4SLinus Torvalds struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[j]; 2541da177e4SLinus Torvalds for (i = 0; i < NR_PROFILE_HIT; ++i) { 2551da177e4SLinus Torvalds if (!hits[i].hits) { 2561da177e4SLinus Torvalds if (hits[i].pc) 2571da177e4SLinus Torvalds hits[i].pc = 0; 2581da177e4SLinus Torvalds continue; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]); 2611da177e4SLinus Torvalds hits[i].hits = hits[i].pc = 0; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds } 26497d1f15bSArjan van de Ven mutex_unlock(&profile_flip_mutex); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds static void profile_discard_flip_buffers(void) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds int i, cpu; 2701da177e4SLinus Torvalds 27197d1f15bSArjan van de Ven mutex_lock(&profile_flip_mutex); 2721da177e4SLinus Torvalds i = per_cpu(cpu_profile_flip, get_cpu()); 2731da177e4SLinus Torvalds put_cpu(); 27415c8b6c1SJens Axboe on_each_cpu(__profile_flip_buffers, NULL, 1); 2751da177e4SLinus Torvalds for_each_online_cpu(cpu) { 2761da177e4SLinus Torvalds struct profile_hit *hits = per_cpu(cpu_profile_hits, cpu)[i]; 2771da177e4SLinus Torvalds memset(hits, 0, NR_PROFILE_HIT*sizeof(struct profile_hit)); 2781da177e4SLinus Torvalds } 27997d1f15bSArjan van de Ven mutex_unlock(&profile_flip_mutex); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2826f7bd76fSRakib Mullick static void do_profile_hits(int type, void *__pc, unsigned int nr_hits) 2831da177e4SLinus Torvalds { 2841da177e4SLinus Torvalds unsigned long primary, secondary, flags, pc = (unsigned long)__pc; 2851da177e4SLinus Torvalds int i, j, cpu; 2861da177e4SLinus Torvalds struct profile_hit *hits; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds pc = min((pc - (unsigned long)_stext) >> prof_shift, prof_len - 1); 2891da177e4SLinus Torvalds i = primary = (pc & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT; 2901da177e4SLinus Torvalds secondary = (~(pc << 1) & (NR_PROFILE_GRP - 1)) << PROFILE_GRPSHIFT; 2911da177e4SLinus Torvalds cpu = get_cpu(); 2921da177e4SLinus Torvalds hits = per_cpu(cpu_profile_hits, cpu)[per_cpu(cpu_profile_flip, cpu)]; 2931da177e4SLinus Torvalds if (!hits) { 2941da177e4SLinus Torvalds put_cpu(); 2951da177e4SLinus Torvalds return; 2961da177e4SLinus Torvalds } 297ece8a684SIngo Molnar /* 298ece8a684SIngo Molnar * We buffer the global profiler buffer into a per-CPU 299ece8a684SIngo Molnar * queue and thus reduce the number of global (and possibly 300ece8a684SIngo Molnar * NUMA-alien) accesses. The write-queue is self-coalescing: 301ece8a684SIngo Molnar */ 3021da177e4SLinus Torvalds local_irq_save(flags); 3031da177e4SLinus Torvalds do { 3041da177e4SLinus Torvalds for (j = 0; j < PROFILE_GRPSZ; ++j) { 3051da177e4SLinus Torvalds if (hits[i + j].pc == pc) { 306ece8a684SIngo Molnar hits[i + j].hits += nr_hits; 3071da177e4SLinus Torvalds goto out; 3081da177e4SLinus Torvalds } else if (!hits[i + j].hits) { 3091da177e4SLinus Torvalds hits[i + j].pc = pc; 310ece8a684SIngo Molnar hits[i + j].hits = nr_hits; 3111da177e4SLinus Torvalds goto out; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds i = (i + secondary) & (NR_PROFILE_HIT - 1); 3151da177e4SLinus Torvalds } while (i != primary); 316ece8a684SIngo Molnar 317ece8a684SIngo Molnar /* 318ece8a684SIngo Molnar * Add the current hit(s) and flush the write-queue out 319ece8a684SIngo Molnar * to the global buffer: 320ece8a684SIngo Molnar */ 321ece8a684SIngo Molnar atomic_add(nr_hits, &prof_buffer[pc]); 3221da177e4SLinus Torvalds for (i = 0; i < NR_PROFILE_HIT; ++i) { 3231da177e4SLinus Torvalds atomic_add(hits[i].hits, &prof_buffer[hits[i].pc]); 3241da177e4SLinus Torvalds hits[i].pc = hits[i].hits = 0; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds out: 3271da177e4SLinus Torvalds local_irq_restore(flags); 3281da177e4SLinus Torvalds put_cpu(); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 331e722d8daSSebastian Andrzej Siewior static int profile_dead_cpu(unsigned int cpu) 3321da177e4SLinus Torvalds { 3331da177e4SLinus Torvalds struct page *page; 334e722d8daSSebastian Andrzej Siewior int i; 3351da177e4SLinus Torvalds 336c309b917SRusty Russell if (prof_cpu_mask != NULL) 337c309b917SRusty Russell cpumask_clear_cpu(cpu, prof_cpu_mask); 338e722d8daSSebastian Andrzej Siewior 339e722d8daSSebastian Andrzej Siewior for (i = 0; i < 2; i++) { 340e722d8daSSebastian Andrzej Siewior if (per_cpu(cpu_profile_hits, cpu)[i]) { 341e722d8daSSebastian Andrzej Siewior page = virt_to_page(per_cpu(cpu_profile_hits, cpu)[i]); 342e722d8daSSebastian Andrzej Siewior per_cpu(cpu_profile_hits, cpu)[i] = NULL; 3431da177e4SLinus Torvalds __free_page(page); 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds } 346e722d8daSSebastian Andrzej Siewior return 0; 3471da177e4SLinus Torvalds } 348e722d8daSSebastian Andrzej Siewior 349e722d8daSSebastian Andrzej Siewior static int profile_prepare_cpu(unsigned int cpu) 350e722d8daSSebastian Andrzej Siewior { 351e722d8daSSebastian Andrzej Siewior int i, node = cpu_to_mem(cpu); 352e722d8daSSebastian Andrzej Siewior struct page *page; 353e722d8daSSebastian Andrzej Siewior 354e722d8daSSebastian Andrzej Siewior per_cpu(cpu_profile_flip, cpu) = 0; 355e722d8daSSebastian Andrzej Siewior 356e722d8daSSebastian Andrzej Siewior for (i = 0; i < 2; i++) { 357e722d8daSSebastian Andrzej Siewior if (per_cpu(cpu_profile_hits, cpu)[i]) 358e722d8daSSebastian Andrzej Siewior continue; 359e722d8daSSebastian Andrzej Siewior 360e722d8daSSebastian Andrzej Siewior page = __alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0); 361e722d8daSSebastian Andrzej Siewior if (!page) { 362e722d8daSSebastian Andrzej Siewior profile_dead_cpu(cpu); 363e722d8daSSebastian Andrzej Siewior return -ENOMEM; 3641da177e4SLinus Torvalds } 365e722d8daSSebastian Andrzej Siewior per_cpu(cpu_profile_hits, cpu)[i] = page_address(page); 366e722d8daSSebastian Andrzej Siewior 367e722d8daSSebastian Andrzej Siewior } 368e722d8daSSebastian Andrzej Siewior return 0; 369e722d8daSSebastian Andrzej Siewior } 370e722d8daSSebastian Andrzej Siewior 371e722d8daSSebastian Andrzej Siewior static int profile_online_cpu(unsigned int cpu) 372e722d8daSSebastian Andrzej Siewior { 373e722d8daSSebastian Andrzej Siewior if (prof_cpu_mask != NULL) 374e722d8daSSebastian Andrzej Siewior cpumask_set_cpu(cpu, prof_cpu_mask); 375e722d8daSSebastian Andrzej Siewior 376e722d8daSSebastian Andrzej Siewior return 0; 377e722d8daSSebastian Andrzej Siewior } 378e722d8daSSebastian Andrzej Siewior 3791da177e4SLinus Torvalds #else /* !CONFIG_SMP */ 3801da177e4SLinus Torvalds #define profile_flip_buffers() do { } while (0) 3811da177e4SLinus Torvalds #define profile_discard_flip_buffers() do { } while (0) 3821da177e4SLinus Torvalds 3836f7bd76fSRakib Mullick static void do_profile_hits(int type, void *__pc, unsigned int nr_hits) 3841da177e4SLinus Torvalds { 3851da177e4SLinus Torvalds unsigned long pc; 3861da177e4SLinus Torvalds pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift; 387ece8a684SIngo Molnar atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]); 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds #endif /* !CONFIG_SMP */ 3906f7bd76fSRakib Mullick 3916f7bd76fSRakib Mullick void profile_hits(int type, void *__pc, unsigned int nr_hits) 3926f7bd76fSRakib Mullick { 3936f7bd76fSRakib Mullick if (prof_on != type || !prof_buffer) 3946f7bd76fSRakib Mullick return; 3956f7bd76fSRakib Mullick do_profile_hits(type, __pc, nr_hits); 3966f7bd76fSRakib Mullick } 397bbe1a59bSAndrew Morton EXPORT_SYMBOL_GPL(profile_hits); 398bbe1a59bSAndrew Morton 3997d12e780SDavid Howells void profile_tick(int type) 4001da177e4SLinus Torvalds { 4017d12e780SDavid Howells struct pt_regs *regs = get_irq_regs(); 4027d12e780SDavid Howells 403c309b917SRusty Russell if (!user_mode(regs) && prof_cpu_mask != NULL && 404c309b917SRusty Russell cpumask_test_cpu(smp_processor_id(), prof_cpu_mask)) 4051da177e4SLinus Torvalds profile_hit(type, (void *)profile_pc(regs)); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 4091da177e4SLinus Torvalds #include <linux/proc_fs.h> 410583a22e7SAlexey Dobriyan #include <linux/seq_file.h> 411*7c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 4121da177e4SLinus Torvalds 413583a22e7SAlexey Dobriyan static int prof_cpu_mask_proc_show(struct seq_file *m, void *v) 4141da177e4SLinus Torvalds { 415ccbd59c1STejun Heo seq_printf(m, "%*pb\n", cpumask_pr_args(prof_cpu_mask)); 416583a22e7SAlexey Dobriyan return 0; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 419583a22e7SAlexey Dobriyan static int prof_cpu_mask_proc_open(struct inode *inode, struct file *file) 4201da177e4SLinus Torvalds { 421583a22e7SAlexey Dobriyan return single_open(file, prof_cpu_mask_proc_show, NULL); 422583a22e7SAlexey Dobriyan } 423583a22e7SAlexey Dobriyan 424583a22e7SAlexey Dobriyan static ssize_t prof_cpu_mask_proc_write(struct file *file, 425583a22e7SAlexey Dobriyan const char __user *buffer, size_t count, loff_t *pos) 426583a22e7SAlexey Dobriyan { 427c309b917SRusty Russell cpumask_var_t new_value; 428583a22e7SAlexey Dobriyan int err; 4291da177e4SLinus Torvalds 430c309b917SRusty Russell if (!alloc_cpumask_var(&new_value, GFP_KERNEL)) 431c309b917SRusty Russell return -ENOMEM; 432c309b917SRusty Russell 433c309b917SRusty Russell err = cpumask_parse_user(buffer, count, new_value); 434c309b917SRusty Russell if (!err) { 435583a22e7SAlexey Dobriyan cpumask_copy(prof_cpu_mask, new_value); 436583a22e7SAlexey Dobriyan err = count; 437c309b917SRusty Russell } 438c309b917SRusty Russell free_cpumask_var(new_value); 4391da177e4SLinus Torvalds return err; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 442583a22e7SAlexey Dobriyan static const struct file_operations prof_cpu_mask_proc_fops = { 443583a22e7SAlexey Dobriyan .open = prof_cpu_mask_proc_open, 444583a22e7SAlexey Dobriyan .read = seq_read, 445583a22e7SAlexey Dobriyan .llseek = seq_lseek, 446583a22e7SAlexey Dobriyan .release = single_release, 447583a22e7SAlexey Dobriyan .write = prof_cpu_mask_proc_write, 448583a22e7SAlexey Dobriyan }; 449583a22e7SAlexey Dobriyan 450fbd387aeSAl Viro void create_prof_cpu_mask(void) 4511da177e4SLinus Torvalds { 4521da177e4SLinus Torvalds /* create /proc/irq/prof_cpu_mask */ 453fbd387aeSAl Viro proc_create("irq/prof_cpu_mask", 0600, NULL, &prof_cpu_mask_proc_fops); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* 4571da177e4SLinus Torvalds * This function accesses profiling information. The returned data is 4581da177e4SLinus Torvalds * binary: the sampling step and the actual contents of the profile 4591da177e4SLinus Torvalds * buffer. Use of the program readprofile is recommended in order to 4601da177e4SLinus Torvalds * get meaningful info out of these data. 4611da177e4SLinus Torvalds */ 4621da177e4SLinus Torvalds static ssize_t 4631da177e4SLinus Torvalds read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos) 4641da177e4SLinus Torvalds { 4651da177e4SLinus Torvalds unsigned long p = *ppos; 4661da177e4SLinus Torvalds ssize_t read; 4671da177e4SLinus Torvalds char *pnt; 4681da177e4SLinus Torvalds unsigned int sample_step = 1 << prof_shift; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds profile_flip_buffers(); 4711da177e4SLinus Torvalds if (p >= (prof_len+1)*sizeof(unsigned int)) 4721da177e4SLinus Torvalds return 0; 4731da177e4SLinus Torvalds if (count > (prof_len+1)*sizeof(unsigned int) - p) 4741da177e4SLinus Torvalds count = (prof_len+1)*sizeof(unsigned int) - p; 4751da177e4SLinus Torvalds read = 0; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds while (p < sizeof(unsigned int) && count > 0) { 478064b022cSHeiko Carstens if (put_user(*((char *)(&sample_step)+p), buf)) 479064b022cSHeiko Carstens return -EFAULT; 4801da177e4SLinus Torvalds buf++; p++; count--; read++; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds pnt = (char *)prof_buffer + p - sizeof(atomic_t); 4831da177e4SLinus Torvalds if (copy_to_user(buf, (void *)pnt, count)) 4841da177e4SLinus Torvalds return -EFAULT; 4851da177e4SLinus Torvalds read += count; 4861da177e4SLinus Torvalds *ppos += read; 4871da177e4SLinus Torvalds return read; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* 4911da177e4SLinus Torvalds * Writing to /proc/profile resets the counters 4921da177e4SLinus Torvalds * 4931da177e4SLinus Torvalds * Writing a 'profiling multiplier' value into it also re-sets the profiling 4941da177e4SLinus Torvalds * interrupt frequency, on architectures that support this. 4951da177e4SLinus Torvalds */ 4961da177e4SLinus Torvalds static ssize_t write_profile(struct file *file, const char __user *buf, 4971da177e4SLinus Torvalds size_t count, loff_t *ppos) 4981da177e4SLinus Torvalds { 4991da177e4SLinus Torvalds #ifdef CONFIG_SMP 5001da177e4SLinus Torvalds extern int setup_profiling_timer(unsigned int multiplier); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds if (count == sizeof(int)) { 5031da177e4SLinus Torvalds unsigned int multiplier; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds if (copy_from_user(&multiplier, buf, sizeof(int))) 5061da177e4SLinus Torvalds return -EFAULT; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds if (setup_profiling_timer(multiplier)) 5091da177e4SLinus Torvalds return -EINVAL; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds #endif 5121da177e4SLinus Torvalds profile_discard_flip_buffers(); 5131da177e4SLinus Torvalds memset(prof_buffer, 0, prof_len * sizeof(atomic_t)); 5141da177e4SLinus Torvalds return count; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 51715ad7cdcSHelge Deller static const struct file_operations proc_profile_operations = { 5181da177e4SLinus Torvalds .read = read_profile, 5191da177e4SLinus Torvalds .write = write_profile, 5206038f373SArnd Bergmann .llseek = default_llseek, 5211da177e4SLinus Torvalds }; 5221da177e4SLinus Torvalds 523e722d8daSSebastian Andrzej Siewior int __ref create_proc_profile(void) 5241da177e4SLinus Torvalds { 5251da177e4SLinus Torvalds struct proc_dir_entry *entry; 526e722d8daSSebastian Andrzej Siewior #ifdef CONFIG_SMP 527e722d8daSSebastian Andrzej Siewior enum cpuhp_state online_state; 528e722d8daSSebastian Andrzej Siewior #endif 529e722d8daSSebastian Andrzej Siewior 530c270a817SSrivatsa S. Bhat int err = 0; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds if (!prof_on) 5331da177e4SLinus Torvalds return 0; 534e722d8daSSebastian Andrzej Siewior #ifdef CONFIG_SMP 535e722d8daSSebastian Andrzej Siewior err = cpuhp_setup_state(CPUHP_PROFILE_PREPARE, "PROFILE_PREPARE", 536e722d8daSSebastian Andrzej Siewior profile_prepare_cpu, profile_dead_cpu); 537e722d8daSSebastian Andrzej Siewior if (err) 538e722d8daSSebastian Andrzej Siewior return err; 539c270a817SSrivatsa S. Bhat 540e722d8daSSebastian Andrzej Siewior err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "AP_PROFILE_ONLINE", 541e722d8daSSebastian Andrzej Siewior profile_online_cpu, NULL); 542e722d8daSSebastian Andrzej Siewior if (err < 0) 543e722d8daSSebastian Andrzej Siewior goto err_state_prep; 544e722d8daSSebastian Andrzej Siewior online_state = err; 545e722d8daSSebastian Andrzej Siewior err = 0; 546e722d8daSSebastian Andrzej Siewior #endif 547c33fff0aSDenis V. Lunev entry = proc_create("profile", S_IWUSR | S_IRUGO, 548c33fff0aSDenis V. Lunev NULL, &proc_profile_operations); 5491ad82fd5SPaolo Ciarrocchi if (!entry) 550e722d8daSSebastian Andrzej Siewior goto err_state_onl; 551271a15eaSDavid Howells proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t)); 552c270a817SSrivatsa S. Bhat 553e722d8daSSebastian Andrzej Siewior return err; 554e722d8daSSebastian Andrzej Siewior err_state_onl: 555e722d8daSSebastian Andrzej Siewior #ifdef CONFIG_SMP 556e722d8daSSebastian Andrzej Siewior cpuhp_remove_state(online_state); 557e722d8daSSebastian Andrzej Siewior err_state_prep: 558e722d8daSSebastian Andrzej Siewior cpuhp_remove_state(CPUHP_PROFILE_PREPARE); 559e722d8daSSebastian Andrzej Siewior #endif 560c270a817SSrivatsa S. Bhat return err; 5611da177e4SLinus Torvalds } 562c96d6660SPaul Gortmaker subsys_initcall(create_proc_profile); 5631da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 564