Skip to content

Commit 22b8ce9

Browse files
Dave Hansentorvalds
authored andcommitted
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>
1 parent 0c2d64f commit 22b8ce9

4 files changed

Lines changed: 84 additions & 13 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.

‎include/linux/profile.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ enum profile_type {
3535
extern int prof_on __read_mostly;
3636

3737
/* 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);
3941
void profile_tick(int type);
4042

4143
/*
@@ -84,9 +86,9 @@ struct pt_regs;
8486

8587
#define prof_on 0
8688

87-
static inline void profile_init(void)
89+
static inline int profile_init(void)
8890
{
89-
return;
91+
return 0;
9092
}
9193

9294
static inline void profile_tick(int type)

‎kernel/ksysfs.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/module.h>
1515
#include <linux/init.h>
1616
#include <linux/kexec.h>
17+
#include <linux/profile.h>
1718
#include <linux/sched.h>
1819

1920
#define KERNEL_ATTR_RO(_name) \
@@ -53,6 +54,37 @@ static ssize_t uevent_helper_store(struct kobject *kobj,
5354
KERNEL_ATTR_RW(uevent_helper);
5455
#endif
5556

57+
#ifdef CONFIG_PROFILING
58+
static ssize_t profiling_show(struct kobject *kobj,
59+
struct kobj_attribute *attr, char *buf)
60+
{
61+
return sprintf(buf, "%d\n", prof_on);
62+
}
63+
static ssize_t profiling_store(struct kobject *kobj,
64+
struct kobj_attribute *attr,
65+
const char *buf, size_t count)
66+
{
67+
int ret;
68+
69+
if (prof_on)
70+
return -EEXIST;
71+
/*
72+
* This eventually calls into get_option() which
73+
* has a ton of callers and is not const. It is
74+
* easiest to cast it away here.
75+
*/
76+
profile_setup((char *)buf);
77+
ret = profile_init();
78+
if (ret)
79+
return ret;
80+
ret = create_proc_profile();
81+
if (ret)
82+
return ret;
83+
return count;
84+
}
85+
KERNEL_ATTR_RW(profiling);
86+
#endif
87+
5688
#ifdef CONFIG_KEXEC
5789
static ssize_t kexec_loaded_show(struct kobject *kobj,
5890
struct kobj_attribute *attr, char *buf)
@@ -109,6 +141,9 @@ static struct attribute * kernel_attrs[] = {
109141
&uevent_seqnum_attr.attr,
110142
&uevent_helper_attr.attr,
111143
#endif
144+
#ifdef CONFIG_PROFILING
145+
&profiling_attr.attr,
146+
#endif
112147
#ifdef CONFIG_KEXEC
113148
&kexec_loaded_attr.attr,
114149
&kexec_crash_loaded_attr.attr,

‎kernel/profile.c‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/cpu.h>
2323
#include <linux/highmem.h>
2424
#include <linux/mutex.h>
25+
#include <linux/slab.h>
26+
#include <linux/vmalloc.h>
2527
#include <asm/sections.h>
2628
#include <asm/irq_regs.h>
2729
#include <asm/ptrace.h>
@@ -50,11 +52,11 @@ static DEFINE_PER_CPU(int, cpu_profile_flip);
5052
static DEFINE_MUTEX(profile_flip_mutex);
5153
#endif /* CONFIG_SMP */
5254

53-
static int __init profile_setup(char *str)
55+
int profile_setup(char *str)
5456
{
55-
static char __initdata schedstr[] = "schedule";
56-
static char __initdata sleepstr[] = "sleep";
57-
static char __initdata kvmstr[] = "kvm";
57+
static char schedstr[] = "schedule";
58+
static char sleepstr[] = "sleep";
59+
static char kvmstr[] = "kvm";
5860
int par;
5961

6062
if (!strncmp(str, sleepstr, strlen(sleepstr))) {
@@ -100,14 +102,33 @@ static int __init profile_setup(char *str)
100102
__setup("profile=", profile_setup);
101103

102104

103-
void __init profile_init(void)
105+
int profile_init(void)
104106
{
107+
int buffer_bytes;
105108
if (!prof_on)
106-
return;
109+
return 0;
107110

108111
/* only text is profiled */
109112
prof_len = (_etext - _stext) >> prof_shift;
110-
prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
113+
buffer_bytes = prof_len*sizeof(atomic_t);
114+
if (!slab_is_available()) {
115+
prof_buffer = alloc_bootmem(buffer_bytes);
116+
return 0;
117+
}
118+
119+
prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL);
120+
if (prof_buffer)
121+
return 0;
122+
123+
prof_buffer = alloc_pages_exact(buffer_bytes, GFP_KERNEL|__GFP_ZERO);
124+
if (prof_buffer)
125+
return 0;
126+
127+
prof_buffer = vmalloc(buffer_bytes);
128+
if (prof_buffer)
129+
return 0;
130+
131+
return -ENOMEM;
111132
}
112133

113134
/* Profile event notifications */
@@ -527,7 +548,7 @@ static void __init profile_nop(void *unused)
527548
{
528549
}
529550

530-
static int __init create_hash_tables(void)
551+
static int create_hash_tables(void)
531552
{
532553
int cpu;
533554

@@ -575,14 +596,14 @@ static int __init create_hash_tables(void)
575596
#define create_hash_tables() ({ 0; })
576597
#endif
577598

578-
static int __init create_proc_profile(void)
599+
int create_proc_profile(void)
579600
{
580601
struct proc_dir_entry *entry;
581602

582603
if (!prof_on)
583604
return 0;
584605
if (create_hash_tables())
585-
return -1;
606+
return -ENOMEM;
586607
entry = proc_create("profile", S_IWUSR | S_IRUGO,
587608
NULL, &proc_profile_operations);
588609
if (!entry)

0 commit comments

Comments
 (0)