Skip to contentSkip to conversation

kernel modules profile support (2.6.6)

PATCH New Randy.Dunlap 1 participant 1 message May 13, 2004 Proc filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel modules profile support (2.6.6)

Hi,

This patch (below) is an update of a 2.4.18 kernel patch that supports module profiling in the in-kernel profiler.

Originally from: hirao ([email protected])

'readprofile2' changes to util-linux-2.12pre (as well as the kernel patch) are available at:
http://developer.osdl.org/rddunlap/modprofile/

Usage info:
http://developer.osdl.org/rddunlap/modprofile/profile_modules.txt

--
~Randy

 arch/i386/Kconfig         |    6 
 fs/proc/proc_misc.c       |   72 --------
 include/asm-i386/hw_irq.h |   30 ++-
 include/linux/proc_fs.h   |    2 
 include/linux/profile.h   |   84 +++++++++
 kernel/Makefile           |    2 
 kernel/kallsyms.c         |   15 +
 kernel/module.c           |   13 +
 kernel/profile.c          |  410 +++++++++++++++++++++++++++++++++++++++++++++-
 scripts/kallsyms.c        |    8 
 10 files changed, 559 insertions(+), 83 deletions(-)
arch/i386/Kconfig +6 -0
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-266-pv/arch/i386/Kconfig linux-266-modprof/arch/i386/Kconfig
@@ -1294,6 +1294,12 @@ config X86_MPPARSE
 	depends on X86_LOCAL_APIC && !X86_VISWS
 	default y
 
+config MODULE_PROFILE
+	bool "Module profiling support"
+	select PROFILING
+	help
+	  This enables profiling of kernel loadable modules.
+
 endmenu
 
 source "security/Kconfig"
fs/proc/proc_misc.c +8 -64

95 lines not on this page · raw message

include/asm-i386/hw_irq.h +23 -7
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-266-pv/include/asm-i386/hw_irq.h linux-266-modprof/include/asm-i386/hw_irq.h
@@ -77,15 +77,20 @@ extern atomic_t irq_mis_count;
 static inline void x86_do_profile(struct pt_regs * regs)
 {
 	unsigned long eip;
-	extern unsigned long prof_cpu_mask;
+#ifdef CONFIG_MODULE_PROFILE
+	unsigned int * targ_prof_buffer;
+	unsigned long idx;
+#endif
  
 	profile_hook(regs);
  
-	if (user_mode(regs))
+	if (user_mode(regs)) {
 		return;
+	}
  
-	if (!prof_buffer)
+	if (!prof_buffer) {
 		return;
+	}
 
 	eip = regs->eip;
  
@@ -93,19 +98,30 @@ static inline void x86_do_profile(struct
 	 * Only measure the CPUs specified by /proc/irq/prof_cpu_mask.
 	 * (default is all CPUs.)
 	 */
-	if (!((1<<smp_processor_id()) & prof_cpu_mask))
+	if (!((1<<smp_processor_id()) & prof_cpu_mask)) {
 		return;
+	}
 
-	eip -= (unsigned long)_stext;
+#ifdef CONFIG_MODULE_PROFILE
+	/*
+	 * get profiling buffer address and offset
+	 * @pc	: program counter
+	 * @idx	: profiling buffer offset
+	 */
+	targ_prof_buffer = srch_prof_buffer(eip, &idx);
+	atomic_inc((atomic_t *)&targ_prof_buffer[idx]);
+#else
+	eip -= (unsigned long) &_stext;
 	eip >>= prof_shift;
 	/*
 	 * Don't ignore out-of-bounds EIP values silently,
 	 * put them into the last histogram slot, so if
 	 * present, they will show up as a sharp peak.
 	 */
-	if (eip > prof_len-1)
-		eip = prof_len-1;
+	if (eip > prof_len - 1)
+		eip = prof_len - 1;
 	atomic_inc((atomic_t *)&prof_buffer[eip]);
+#endif
 }
  
 #if defined(CONFIG_X86_IO_APIC)
include/linux/proc_fs.h +2 -0
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-266-pv/include/linux/proc_fs.h linux-266-modprof/include/linux/proc_fs.h
@@ -82,6 +82,7 @@ extern struct proc_dir_entry *proc_net;
 extern struct proc_dir_entry *proc_bus;
 extern struct proc_dir_entry *proc_root_driver;
 extern struct proc_dir_entry *proc_root_kcore;
+extern struct proc_dir_entry *proc_root_mprof;
 
 extern void proc_root_init(void);
 extern void proc_misc_init(void);
@@ -114,6 +115,7 @@ extern struct dentry *proc_lookup(struct
 extern struct file_operations proc_kcore_operations;
 extern struct file_operations proc_kmsg_operations;
 extern struct file_operations ppc_htab_operations;
+extern struct file_operations proc_mprof_operations;
 
 /*
  * proc_tty.c
include/linux/profile.h +83 -1

109 lines not on this page · raw message

kernel/kallsyms.c +9 -6
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-266-pv/kernel/kallsyms.c linux-266-modprof/kernel/kallsyms.c
@@ -171,21 +171,23 @@ static int get_ksymbol_mod(struct kallsy
 	return 1;
 }
 
-static void get_ksymbol_core(struct kallsym_iter *iter)
+/* Returns space to next name. */
+static unsigned long get_ksymbol_core(struct kallsym_iter *iter)
 {
-	unsigned stemlen;
+	unsigned stemlen, off = iter->nameoff;
 
 	/* First char of each symbol name indicates prefix length
 	   shared with previous name (stem compression). */
-	stemlen = kallsyms_names[iter->nameoff++];
+	stemlen = kallsyms_names[off++];
 
-	strlcpy(iter->name+stemlen, kallsyms_names+iter->nameoff, 128-stemlen);
-	iter->nameoff += strlen(kallsyms_names + iter->nameoff) + 1;
+	strlcpy(iter->name+stemlen, kallsyms_names + off, 128-stemlen);
+	off += strlen(kallsyms_names + off) + 1;
 	iter->owner = NULL;
 	iter->value = kallsyms_addresses[iter->pos];
 	iter->type = 't';
 
 	upcase_if_global(iter);
+	return off - iter->nameoff;
 }
 
 static void reset_iter(struct kallsym_iter *iter)
@@ -210,9 +212,10 @@ static int update_iter(struct kallsym_it
 
 	/* We need to iterate through the previous symbols: can be slow */
 	for (; iter->pos != pos; iter->pos++) {
-		get_ksymbol_core(iter);
+		iter->nameoff += get_ksymbol_core(iter);
 		cond_resched();
 	}
+	get_ksymbol_core(iter);
 	return 1;
 }
 
kernel/Makefile +1 -1
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-266-pv/kernel/Makefile linux-266-modprof/kernel/Makefile
@@ -7,7 +7,7 @@ obj-y     = sched.o fork.o exec_domain.o
 	    sysctl.o capability.o ptrace.o timer.o user.o \
 	    signal.o sys.o kmod.o workqueue.o pid.o \
 	    rcupdate.o intermodule.o extable.o params.o posix-timers.o \
-	    kthread.o
+	    kthread.o profile.o
 
 obj-$(CONFIG_FUTEX) += futex.o
 obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
kernel/module.c +13 -0
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-266-pv/kernel/module.c linux-266-modprof/kernel/module.c
@@ -33,6 +33,7 @@
 #include <linux/err.h>
 #include <linux/vermagic.h>
 #include <linux/notifier.h>
+#include <linux/profile.h>
 #include <linux/stop_machine.h>
 #include <asm/uaccess.h>
 #include <asm/semaphore.h>
@@ -947,6 +948,10 @@ static unsigned long resolve_symbol(Elf_
 /* Free a module, remove from lists, etc (must hold module mutex). */
 static void free_module(struct module *mod)
 {
+#ifdef CONFIG_MODULE_PROFILE
+	delete_module_profile(mod);
+#endif
+
 	/* Delete from various lists */
 	spin_lock_irq(&modlist_lock);
 	list_del(&mod->list);
@@ -1559,6 +1564,14 @@ static struct module *load_module(void _
 	if (err < 0)
 		goto arch_cleanup;
 
+#ifdef CONFIG_MODULE_PROFILE
+	if (create_module_profile(mod)) {
+		printk(KERN_WARNING "%s: creation of module "
+			"profiling buffer failed for module(%s).\n",
+			__FUNCTION__, mod->name);
+	}
+#endif
+
 	/* Get rid of temporary copy */
 	vfree(hdr);
 
kernel/profile.c +408 -2

431 lines not on this page · raw message

scripts/kallsyms.c +6 -2
diff -Naurp -X /home/rddunlap/doc/dontdiff-osdl linux-266-pv/scripts/kallsyms.c linux-266-modprof/scripts/kallsyms.c
@@ -115,7 +115,10 @@ write_src(void)
 		if (!symbol_valid(&table[i]))
 			continue;
 		
-		if (table[i].addr == last_addr)
+		if (table[i].addr == last_addr &&
+		    last_addr != _etext)
+			/* don't duplicate addresses, except always
+			 * make sure that _etext is in kallsyms */
 			continue;
 
 		printf("\tPTR\t%#llx\n", table[i].addr);
@@ -140,7 +143,8 @@ write_src(void)
 		if (!symbol_valid(&table[i]))
 			continue;
 		
-		if (table[i].addr == last_addr)
+		if (table[i].addr == last_addr &&
+		    last_addr != _etext)
 			continue;
 
 		for (k = 0; table[i].sym[k] && table[i].sym[k] == prev[k]; ++k)