Skip to contentSkip to conversation

profile: add support for modules and make output human readable

PATCH In review Cliff Frey 2 participants 2 messages Mar 12, 2011 – Mar 14, 2011

Subsystems (2)
lore
Share & tools
lore

Cliff Frey

author
#0 lore raw

PATCH 1/1 profile: add support for modules and make output human readable

This changes /proc/profile to include kernel modules and also print out data in human readable form (it does symbol lookups of all kernel addresses). It also adds /proc/profile_m which gives a high level summary of samples in userspace/kernel/each module.

This makes it easy to profile the kernel and kernel modules on embedded systems where cross compiling oprofile is too difficult. It is necessary to compile the kernel with ksyms_all for this change to be useful.

Example usage:

    router:/# sysctl -w kernel.profiler_enabled=1
    router:/# sha1sum /dev/urandom
    ^C
    router:/# cat /proc/profile
    c1007468    759 default_idle
    c10fad98    183 sha_transform
    c10fe248      6 copy_to_user
    c11524d8     33 mix_pool_bytes_extract
    c1152968     12 extract_buf
    c116e608      1 serial8250_handle_port

This change is relative to v2.6.32. This change is not cleaned up or ready for the mainline kernel, but I believe that it is useful for cases where one wants profiling information about kernel modules, but does not have oprofile available.

I am open to changing/improving the patch if there is interest in getting it into mainline. I will understand if there is no enthusiasm for this change as it is imaginable that improving oprofile to the point where it is easily used on cross compiled/embedded systems would be a better long term direction. However, I'd appreciate any feedback about the change, and this patch has allowed me to greatly improve the performance of systems that I work on.

Some specific areas for improvement in the patch include:

- Add support for SMP.
- Leave the /proc/profile interface untouched, and just add a new handler for human-readable output.
- Change human-readable output to be done incrementally instead of using seq_file, so that it does not stall the kernel for seconds at a time (because of all the symbol lookups)
- Cleanup types/statics/enabled logic which is more complex than necessary.
- Cleanup the profiler_enabled logic (this can probably just be removed)
- Make this a separate kernel config option.

---
 include/linux/kernel.h |    3 +
 include/linux/module.h |    6 ++
 kernel/module.c        |   20 ++++-
 kernel/profile.c       |  228 +++++++++++++++++++++++++++++++++++++++--------
 kernel/sysctl.c        |   10 ++
 5 files changed, 226 insertions(+), 41 deletions(-)
include/linux/kernel.h +3 -0
index f4e3184..ded869d 100644
@@ -307,6 +307,9 @@ extern int panic_timeout;
 extern int panic_on_oops;
 extern int panic_on_unrecovered_nmi;
 extern int panic_on_io_nmi;
+#ifdef CONFIG_PROFILING
+extern int profiler_enabled;
+#endif
 extern const char *print_tainted(void);
 extern void add_taint(unsigned flag);
 extern int test_taint(unsigned flag);
include/linux/module.h +6 -0
index 482efc8..e350a8e 100644
@@ -327,6 +327,12 @@ struct module
 	struct module_notes_attrs *notes_attrs;
 #endif
 
+#ifdef CONFIG_PROFILING
+	/* The number of ticks for this module */
+	atomic_t ticks;
+	atomic_t *prof_buffer;
+#endif
+
 	/* Per-cpu data. */
 	void *percpu;
 
kernel/module.c +18 -2
index 5842a71..9eb36b5 100644
@@ -78,7 +78,7 @@ EXPORT_TRACEPOINT_SYMBOL(module_get);
  * (delete uses stop_machine/add uses RCU list operations). */
 DEFINE_MUTEX(module_mutex);
 EXPORT_SYMBOL_GPL(module_mutex);
-static LIST_HEAD(modules);
+LIST_HEAD(modules);
 
 /* Block module loading/unloading? */
 int modules_disabled = 0;
@@ -1540,6 +1540,13 @@ static void free_module(struct module *mod)
 	/* Finally, free the core (containing the module structure) */
 	module_free(mod, mod->module_core);
 
+#ifdef CONFIG_PROFILING
+	if (mod->prof_buffer) {
+		vfree(mod->prof_buffer);
+		mod->prof_buffer = 0;
+	}
+#endif
+
 #ifdef CONFIG_MPU
 	update_protections(current->mm);
 #endif
@@ -2409,7 +2416,16 @@ static noinline struct module *load_module(void __user *umod,
 			goto cleanup;
 	}
 #endif
-
+#ifdef CONFIG_PROFILING
+	{
+		extern unsigned prof_shift;
+		int alloc_size = (mod->core_text_size >> prof_shift) * sizeof(atomic_t);
+		mod->prof_buffer = vmalloc(alloc_size);
+		if (mod->prof_buffer)
+			memset(mod->prof_buffer, 0, alloc_size);
+		atomic_set(&mod->ticks, 0);
+	}
+#endif
 	/* Now do relocations. */
 	for (i = 1; i < hdr->e_shnum; i++) {
 		const char *strtab = (char *)sechdrs[strindex].sh_addr;
kernel/profile.c +189 -39

322 lines not on this page · raw message

kernel/sysctl.c +10 -0
index 0d949c5..b7d16e5 100644
@@ -1032,6 +1032,16 @@ static struct ctl_table kern_table[] = {
 		.proc_handler	= &proc_dointvec,
 	},
 #endif
+#ifdef CONFIG_PROFILING
+	{
+		.ctl_name       = CTL_UNNUMBERED,
+		.procname       = "profiler_enabled",
+		.data           = &profiler_enabled,
+		.maxlen         = sizeof(int),
+		.mode           = 0644,
+		.proc_handler   = &proc_dointvec,
+	},
+#endif
 /*
  * NOTE: do not add new entries to this table unless you have read
  * Documentation/sysctl/ctl_unnumbered.txt
Signature · 1 line
1.7.4.1.203.g07873
#1 lore raw
Quoted 4 lines · Cliff Frey

This changes /proc/profile to include kernel modules and also print out data in human readable form (it does symbol lookups of all kernel addresses). It also adds /proc/profile_m which gives a high level summary of samples in userspace/kernel/each module.

I like the idea of extending this profiler to modules.

But the symbol lookup will make existing readprofile much slower than it used to be. Please make it a new file.

In general it seems to still need quite a lot of work though:

Quoted 1 line

- Make this a separate kernel config option.

This means it'll be off when you need it. The cool thing about it was always that it was always there.

kernel/module.c static noinline struct module *load_module(void __user *umod, · line 2421

Quoted 5 lines

#endif

kernel/module.c +3 -1
-
+#ifdef CONFIG_PROFILING
+	{
+		extern unsigned prof_shift;

This should be in a header.

kernel/profile.c __setup("profile=", profile_setup); · line 122

Quoted 9 lines
kernel/profile.c +4 -0
 	return 1;
@@ -105,6 +116,11 @@ __setup("profile=", profile_setup);
 int __ref profile_init(void)
 {
 	int buffer_bytes;
+
+	// XXX turn on profiling
+	prof_shift = 4;
+	prof_on = CPU_PROFILING;

Was this supposed to be a submission ready patch?

kernel/profile.c void unregister_timer_hook(int (*hook)(struct pt_regs *)) · line 248

Quoted 13 lines
kernel/profile.c +5 -0
+
 	if (!prof_on)
 		return 0;
 
@@ -225,8 +241,13 @@ void unregister_timer_hook(int (*hook)(struct pt_regs *))
 }
 EXPORT_SYMBOL_GPL(unregister_timer_hook);
 
+static inline int within(unsigned long addr, void *start, unsigned long size)
+{
+	return ((void *)addr >= start && (void *)addr < start + size);
+}
 

I'm pretty sure we already have that in kernel.h

kernel/profile.c void unregister_timer_hook(int (*hook)(struct pt_regs *)) · line 250

Quoted 2 lines
kernel/profile.c +1 -0
 #ifdef CONFIG_SMP
+#error "Meraki has not added SMP profiling support yet"

Ok that explains the missing locking. Obviously that's not usable for a lot of people.

Quoted 13 lines
kernel/profile.c +3 -0
 /*
  * Each cpu has a pair of open-addressed hashtables for pending
  * profile hits. read_profile() IPI's all cpus to request them
@@ -420,23 +441,53 @@ out_free:
 void profile_hits(int type, void *__pc, unsigned int nr_hits)
 {
 	unsigned long pc;
+	unsigned long pc_kernel_offset;
+	struct module *mod = NULL;
 
+	pc = (unsigned long) __pc;
 	if (prof_on != type || !prof_buffer)
 		return;

kernel/profile.c out_free: · line 457

Quoted 10 lines
kernel/profile.c +8 -2
-	pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
-	atomic_add(nr_hits, &prof_buffer[min(pc, prof_len - 1)]);
+	pc_kernel_offset = (pc - (unsigned long)_stext) >> prof_shift;
+	if (pc_kernel_offset < prof_len) {
+		atomic_add(nr_hits, &prof_buffer[pc_kernel_offset]);
+		atomic_add(nr_hits, &known_kernel_hits);
+		return;
+	}
+
+	list_for_each_entry(mod, &modules, list)

You cannot just walk this list without any protection. Modules can be unloaded any time. Or maybe make it depend
on the !MODULE_UNLOAD

Review out_free: · line 460

Quoted 3 lines
(quoted diff) +2 -0
+				if (lastcount > 5)
+					seq_printf(m, "%08lx % 6d %s\n",
 lastpc, lastcount, lastname);

Weird magic number

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only