Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

profiling: dynamically enable readprofile at runtime

Way too often, I have a machine that exhibits some kind of crappy
behavior. The CPU looks wedged in the kernel or it is spending way too
much system time and I wonder what is responsible.

I try to run readprofile. But, of course, Ubuntu doesn't enable it by
default. Dang!

The reason we boot-time enable it is that it takes a big bufffer that we
generally can only bootmem alloc. But, does it hurt to at least try and
runtime-alloc it?

To use:
echo 2 > /sys/kernel/profile

Then run readprofile like normal.

This should fix the compile issue with allmodconfig. I've compile-tested
on a bunch more configs now including a few more architectures.

Signed-off-by: Dave Hansen <dave@linux.vnet.ibm.com>
Acked-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Dave Hansen and committed by
Linus Torvalds
22b8ce94 0c2d64fb

+84 -13
+13
Documentation/ABI/testing/sysfs-profiling
··· 1 + What: /sys/kernel/profile 2 + Date: September 2008 3 + Contact: Dave Hansen <dave@linux.vnet.ibm.com> 4 + Description: 5 + /sys/kernel/profile is the runtime equivalent 6 + of the boot-time profile= option. 7 + 8 + You can get the same effect running: 9 + 10 + echo 2 > /sys/kernel/profile 11 + 12 + as you would by issuing profile=2 on the boot 13 + command line.
+5 -3
include/linux/profile.h
··· 35 35 extern int prof_on __read_mostly; 36 36 37 37 /* init basic kernel profiler */ 38 - void __init profile_init(void); 38 + int profile_init(void); 39 + int profile_setup(char *str); 40 + int create_proc_profile(void); 39 41 void profile_tick(int type); 40 42 41 43 /* ··· 86 84 87 85 #define prof_on 0 88 86 89 - static inline void profile_init(void) 87 + static inline int profile_init(void) 90 88 { 91 - return; 89 + return 0; 92 90 } 93 91 94 92 static inline void profile_tick(int type)
+35
kernel/ksysfs.c
··· 14 14 #include <linux/module.h> 15 15 #include <linux/init.h> 16 16 #include <linux/kexec.h> 17 + #include <linux/profile.h> 17 18 #include <linux/sched.h> 18 19 19 20 #define KERNEL_ATTR_RO(_name) \ ··· 52 51 return count; 53 52 } 54 53 KERNEL_ATTR_RW(uevent_helper); 54 + #endif 55 + 56 + #ifdef CONFIG_PROFILING 57 + static ssize_t profiling_show(struct kobject *kobj, 58 + struct kobj_attribute *attr, char *buf) 59 + { 60 + return sprintf(buf, "%d\n", prof_on); 61 + } 62 + static ssize_t profiling_store(struct kobject *kobj, 63 + struct kobj_attribute *attr, 64 + const char *buf, size_t count) 65 + { 66 + int ret; 67 + 68 + if (prof_on) 69 + return -EEXIST; 70 + /* 71 + * This eventually calls into get_option() which 72 + * has a ton of callers and is not const. It is 73 + * easiest to cast it away here. 74 + */ 75 + profile_setup((char *)buf); 76 + ret = profile_init(); 77 + if (ret) 78 + return ret; 79 + ret = create_proc_profile(); 80 + if (ret) 81 + return ret; 82 + return count; 83 + } 84 + KERNEL_ATTR_RW(profiling); 55 85 #endif 56 86 57 87 #ifdef CONFIG_KEXEC ··· 140 108 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET) 141 109 &uevent_seqnum_attr.attr, 142 110 &uevent_helper_attr.attr, 111 + #endif 112 + #ifdef CONFIG_PROFILING 113 + &profiling_attr.attr, 143 114 #endif 144 115 #ifdef CONFIG_KEXEC 145 116 &kexec_loaded_attr.attr,
+31 -10
kernel/profile.c
··· 22 22 #include <linux/cpu.h> 23 23 #include <linux/highmem.h> 24 24 #include <linux/mutex.h> 25 + #include <linux/slab.h> 26 + #include <linux/vmalloc.h> 25 27 #include <asm/sections.h> 26 28 #include <asm/irq_regs.h> 27 29 #include <asm/ptrace.h> ··· 52 50 static DEFINE_MUTEX(profile_flip_mutex); 53 51 #endif /* CONFIG_SMP */ 54 52 55 - static int __init profile_setup(char *str) 53 + int profile_setup(char *str) 56 54 { 57 - static char __initdata schedstr[] = "schedule"; 58 - static char __initdata sleepstr[] = "sleep"; 59 - static char __initdata kvmstr[] = "kvm"; 55 + static char schedstr[] = "schedule"; 56 + static char sleepstr[] = "sleep"; 57 + static char kvmstr[] = "kvm"; 60 58 int par; 61 59 62 60 if (!strncmp(str, sleepstr, strlen(sleepstr))) { ··· 102 100 __setup("profile=", profile_setup); 103 101 104 102 105 - void __init profile_init(void) 103 + int profile_init(void) 106 104 { 105 + int buffer_bytes; 107 106 if (!prof_on) 108 - return; 107 + return 0; 109 108 110 109 /* only text is profiled */ 111 110 prof_len = (_etext - _stext) >> prof_shift; 112 - prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t)); 111 + buffer_bytes = prof_len*sizeof(atomic_t); 112 + if (!slab_is_available()) { 113 + prof_buffer = alloc_bootmem(buffer_bytes); 114 + return 0; 115 + } 116 + 117 + prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL); 118 + if (prof_buffer) 119 + return 0; 120 + 121 + prof_buffer = alloc_pages_exact(buffer_bytes, GFP_KERNEL|__GFP_ZERO); 122 + if (prof_buffer) 123 + return 0; 124 + 125 + prof_buffer = vmalloc(buffer_bytes); 126 + if (prof_buffer) 127 + return 0; 128 + 129 + return -ENOMEM; 113 130 } 114 131 115 132 /* Profile event notifications */ ··· 548 527 { 549 528 } 550 529 551 - static int __init create_hash_tables(void) 530 + static int create_hash_tables(void) 552 531 { 553 532 int cpu; 554 533 ··· 596 575 #define create_hash_tables() ({ 0; }) 597 576 #endif 598 577 599 - static int __init create_proc_profile(void) 578 + int create_proc_profile(void) 600 579 { 601 580 struct proc_dir_entry *entry; 602 581 603 582 if (!prof_on) 604 583 return 0; 605 584 if (create_hash_tables()) 606 - return -1; 585 + return -ENOMEM; 607 586 entry = proc_create("profile", S_IWUSR | S_IRUGO, 608 587 NULL, &proc_profile_operations); 609 588 if (!entry)