Skip to contentSkip to conversation

dynamically enable readprofile at runtime

RFC PATCH In review Dave Hansen 2 participants 3 messages Sep 9, 2008 – Sep 12, 2008 Driver core

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 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.

---

 linux-2.6.git-dave/Documentation/ABI/testing/sysfs-profiling |   13 +++
 linux-2.6.git-dave/include/linux/profile.h                   |    8 +-
 linux-2.6.git-dave/kernel/ksysfs.c                           |   35 +++++++++
 linux-2.6.git-dave/kernel/profile.c                          |   41 ++++++++---
 4 files changed, 84 insertions(+), 13 deletions(-)
include/linux/profile.h renamed from include/linux/profile.h~dynamic-readprofile +5 -3
diff -puN include/linux/profile.h~dynamic-readprofile include/linux/profile.h
@@ -35,7 +35,9 @@ enum profile_type {
 extern int prof_on __read_mostly;
 
 /* init basic kernel profiler */
-void __init profile_init(void);
+int profile_init(void);
+int profile_setup(char *str);
+int create_proc_profile(void);
 void profile_tick(int type);
 
 /*
@@ -84,9 +86,9 @@ struct pt_regs;
 
 #define prof_on 0
 
-static inline void profile_init(void)
+static inline int profile_init(void)
 {
-	return;
+	return 0;
 }
 
 static inline void profile_tick(int type)
kernel/ksysfs.c renamed from kernel/ksysfs.c~dynamic-readprofile +35 -0
diff -puN kernel/ksysfs.c~dynamic-readprofile kernel/ksysfs.c
@@ -14,6 +14,7 @@
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/kexec.h>
+#include <linux/profile.h>
 #include <linux/sched.h>
 
 #define KERNEL_ATTR_RO(_name) \
@@ -53,6 +54,37 @@ static ssize_t uevent_helper_store(struc
 KERNEL_ATTR_RW(uevent_helper);
 #endif
 
+#ifdef CONFIG_PROFILING
+static ssize_t profiling_show(struct kobject *kobj,
+				  struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", prof_on);
+}
+static ssize_t profiling_store(struct kobject *kobj,
+				   struct kobj_attribute *attr,
+				   const char *buf, size_t count)
+{
+	int ret;
+
+	if (prof_on)
+		return -EEXIST;
+	/*
+	 * This eventually calls into get_option() which
+	 * has a ton of callers and is not const.  It is
+	 * easiest to cast it away here.
+	 */
+	profile_setup((char *)buf);
+	ret = profile_init();
+	if (ret)
+		return ret;
+	ret = create_proc_profile();
+	if (ret)
+		return ret;
+	return count;
+}
+KERNEL_ATTR_RW(profiling);
+#endif
+
 #ifdef CONFIG_KEXEC
 static ssize_t kexec_loaded_show(struct kobject *kobj,
 				 struct kobj_attribute *attr, char *buf)
@@ -109,6 +141,9 @@ static struct attribute * kernel_attrs[]
 	&uevent_seqnum_attr.attr,
 	&uevent_helper_attr.attr,
 #endif
+#ifdef CONFIG_PROFILING
+	&profiling_attr.attr,
+#endif
 #ifdef CONFIG_KEXEC
 	&kexec_loaded_attr.attr,
 	&kexec_crash_loaded_attr.attr,
kernel/profile.c renamed from kernel/profile.c~dynamic-readprofile +31 -10

83 lines not on this page · raw message

diff -puN include/linux/posix_types.h~dynamic-readprofile include/linux/posix_types.h
diff -puN Documentation/ABI/testing/sysfs-kernel-mm~dynamic-readprofile Documentation/ABI/testing/sysfs-kernel-mm

Documentation/ABI/testing/sysfs-profiling new file +13 -0
diff -puN /dev/null Documentation/ABI/testing/sysfs-profiling
@@ -0,0 +1,13 @@
+What:		/sys/kernel/profile
+Date:		September 2008
+Contact:	Dave Hansen <[email protected]>
+Description:
+		/sys/kernel/profile is the runtime equivalent
+		of the boot-time profile= option.
+
+		You can get the same effect running:
+
+			echo 2 > /sys/kernel/profile
+
+		as you would by issuing profile=2 on the boot
+		command line.

_

#1 lore raw
Quoted 12 lines · On Tue, 09 Sep 2008 11:05:36 -0700 Dave Hansen <[email protected]> wrote:

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?

Quoted 9 lines

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.

Can it be turned off again? afaict: no?

Quoted 20 lines
kernel/ksysfs.c renamed from kernel/ksysfs.c~dynamic-readprofile +20 -0
+#ifdef CONFIG_PROFILING
+static ssize_t profiling_show(struct kobject *kobj,
+				  struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", prof_on);
+}
+static ssize_t profiling_store(struct kobject *kobj,
+				   struct kobj_attribute *attr,
+				   const char *buf, size_t count)
+{
+	int ret;
+
+	if (prof_on)
+		return -EEXIST;
+	/*
+	 * This eventually calls into get_option() which
+	 * has a ton of callers and is not const.  It is
+	 * easiest to cast it away here.
+	 */
+	profile_setup((char *)buf);

kernel/ksysfs.c static ssize_t uevent_helper_store(struc · line 86

Quoted 10 lines
kernel/ksysfs.c renamed from kernel/ksysfs.c~dynamic-readprofile +10 -0
+	ret = profile_init();
+	if (ret)
+		return ret;
+	ret = create_proc_profile();
+	if (ret)
+		return ret;
+	return count;
+}
+KERNEL_ATTR_RW(profiling);
+#endif

Tested with CONFIG_SYSFS=n?

Quoted 21 lines
kernel/profile.c renamed from kernel/profile.c~dynamic-readprofile +13 -3
-void __init profile_init(void)
+int profile_init(void)
 {
+	int buffer_bytes;
 	if (!prof_on)
-		return;
+		return 0;
 
 	/* only text is profiled */
 	prof_len = (_etext - _stext) >> prof_shift;
-	prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
+	buffer_bytes = prof_len*sizeof(atomic_t);
+	if (!slab_is_available()) {
+		prof_buffer = alloc_bootmem(buffer_bytes);
+		return 0;
+	}
+
+	prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL);
+	if (prof_buffer)
+		return 0;
+

kernel/profile.c static int __init profile_setup(char *st · line 132

Quoted 10 lines
kernel/profile.c renamed from kernel/profile.c~dynamic-readprofile +9 -0
+	prof_buffer = alloc_pages_exact(buffer_bytes, GFP_KERNEL|__GFP_ZERO);
+	if (prof_buffer)
+		return 0;
+
+	prof_buffer = vmalloc(buffer_bytes);
+	if (prof_buffer)
+		return 0;
+
+	return -ENOMEM;
 }

Well that should cover it.

Did you check to see if any __GFP_NOWARNs are needed there?

alloc_bootmem() will (apparently undocumentedly and secretly) zero the memory.

kzalloc() will zero the memory.

alloc_pages_exact(__GFP_ZERO) will zero the memory.

But what about vmalloc? I see no documentation which says that it zeroes the memory, although it seems that some flavours of it will do this, but the nommu version does not. Seems all messed up.

#2 lore raw
Quoted 16 lines · Andrew Morton
Quoted 14 lines · On Tue, 09 Sep 2008 11:05:36 -0700 Dave Hansen <[email protected]> wrote:

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

Quoted 7 lines
Quoted 5 lines

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.

Can it be turned off again? afaict: no?

No. Probably wouldn't be that hard, but I wanted to see if anyone would take this, first. :)

Quoted 24 lines
Quoted 24 lines
kernel/ksysfs.c renamed from kernel/ksysfs.c~dynamic-readprofile +24 -0
+#ifdef CONFIG_PROFILING
+static ssize_t profiling_show(struct kobject *kobj,
+				  struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", prof_on);
+}
+static ssize_t profiling_store(struct kobject *kobj,
+				   struct kobj_attribute *attr,
+				   const char *buf, size_t count)
+{
+	int ret;
+
+	if (prof_on)
+		return -EEXIST;
+	/*
+	 * This eventually calls into get_option() which
+	 * has a ton of callers and is not const.  It is
+	 * easiest to cast it away here.
+	 */
+	profile_setup((char *)buf);
+	ret = profile_init();
+	if (ret)
+		return ret;
+	ret = create_proc_profile();
Quoted 8 lines

kernel/ksysfs.c static ssize_t uevent_helper_store(struc · line 86

Quoted 6 lines
kernel/ksysfs.c renamed from kernel/ksysfs.c~dynamic-readprofile +6 -0
+	if (ret)
+		return ret;
+	return count;
+}
+KERNEL_ATTR_RW(profiling);
+#endif

Tested with CONFIG_SYSFS=n?

There's a bunch of other sysfs stuff in that file. I'm actually not quite sure what magic lets it build with SYSFS=n, but it does build.

Quoted 34 lines
Quoted 21 lines
kernel/profile.c renamed from kernel/profile.c~dynamic-readprofile +13 -3
-void __init profile_init(void)
+int profile_init(void)
 {
+	int buffer_bytes;
 	if (!prof_on)
-		return;
+		return 0;
 
 	/* only text is profiled */
 	prof_len = (_etext - _stext) >> prof_shift;
-	prof_buffer = alloc_bootmem(prof_len*sizeof(atomic_t));
+	buffer_bytes = prof_len*sizeof(atomic_t);
+	if (!slab_is_available()) {
+		prof_buffer = alloc_bootmem(buffer_bytes);
+		return 0;
+	}
+
+	prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL);
+	if (prof_buffer)
+		return 0;
+

kernel/profile.c static int __init profile_setup(char *st · line 132

Quoted 10 lines
kernel/profile.c renamed from kernel/profile.c~dynamic-readprofile +9 -0
+	prof_buffer = alloc_pages_exact(buffer_bytes, GFP_KERNEL|__GFP_ZERO);
+	if (prof_buffer)
+		return 0;
+
+	prof_buffer = vmalloc(buffer_bytes);
+	if (prof_buffer)
+		return 0;
+
+	return -ENOMEM;
 }

Well that should cover it.

Quoted 1 line

Did you check to see if any __GFP_NOWARNs are needed there?

No, because the two cases I've actually tested were where the buffer is too big to be handled by the slab or buddy allocators. It never even tries to allocate the memory and doesn't issue the warnings. But, you're right, it needs them. Added in new version.

Quoted 10 lines

alloc_bootmem() will (apparently undocumentedly and secretly) zero the memory.

kzalloc() will zero the memory.

alloc_pages_exact(__GFP_ZERO) will zero the memory.

But what about vmalloc? I see no documentation which says that it zeroes the memory, although it seems that some flavours of it will do this, but the nommu version does not. Seems all messed up.

Yeah, it does seem messed up. The right thing to do for this patch is probably just to zero the vmalloc() result. I'll look into vmalloc zeroing a bit as well.

-- Dave