kernel profiling with gprof
PATCH 1/1 kernel profiling with gprof
On behalf of SGI we are pleased to make available this patch that allows kernel profiling with gprof. This allows call graph profiling that may be more useful than statistical PC sampling. The patch has two components, a kernel part that collects call arc data and makes it available under /proc, and a user level command, kernprof, that controls kernel profiling and creates the data file for gprof. Collection of call arc data is based on gcc's -pg option that instruments the kernel with calls to mcount().
****
IMPORTANT: Due to bugs in all versions of gcc/egcs I am aware of, unmodified kernels compiled by unmodified compilers do not work with this patch. The fault is with gcc, see
http://egcs.cygnus.com/ml/egcs-patches/2000-04/msg00332.html for a description of the problem. To use this patch there are two options. Either patch the compiler as suggested in the above posting (an incomplete but adequate for our purposes patch) or remove the FASTCALLs from linux as done by the IKB patch. I personally prefer the compiler patch so the patch below does not mess with FASTCALLs.
****
With this patch gprof profiling typically goes like this. You use kernprof to select gprof profiling, the sampling rate and to enable data collection. You then run your experiment and finally you use kernprof again to stop data gathering and to generate a data file for gprof (gmon.out by default). Then you run gprof on the kernel binary (the vmlinux one) and the file produced by kernprof.
kernprof itself is an extension of readprofile (and does what readprofile does in addition to the gprof stuff). We apologize for the lack of a manpage at this time, one will be made available shortly.
Comments and suggestions are welcome.
---
Dimitris Michailidis
SGI Linux Group
[email protected]
build2/dm/2.2.8-profile/Documentation/Configure.help renamed from usr/src/linux-2.2.8/linux/Documentation/Configure.help +11 -0
@@ -9750,6 +9750,17 @@ keys are documented in Documentation/sysrq.txt. Don't say Y unless you really know what this hack does. +Extended Profiling Support +CONFIG_EXTENDED_PROFILING + This option makes the kernel produce profiling data to support + call graph profiling (with gprof) or stack trace profiling (future work). + When this option is enabled, the kernel is compiled with frame pointers. + +Kernel mcount() Instrumentation +CONFIG_PROF_MCOUNT + This option arranges for the compiler to instrument the kernel with calls + to mcount(). This is needed to profile with gprof. + ISDN subsystem CONFIG_ISDN ISDN ("Integrated Services Digital Networks", called RNIS in France)
build2/dm/2.2.8-profile/Makefile renamed from usr/src/linux-2.2.8/linux/Makefile +7 -1
@@ -86,7 +86,13 @@ # standard CFLAGS # -CFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer +CFLAGS = -Wall -Wstrict-prototypes -O2 + +ifdef CONFIG_PROF_MCOUNT + CFLAGS += -pg +else + CFLAGS += -fomit-frame-pointer +endif ifdef CONFIG_SMP CFLAGS += -D__SMP__
build2/dm/2.2.8-profile/arch/i386/config.in renamed from usr/src/linux-2.2.8/linux/arch/i386/config.in +4 -0
@@ -199,5 +199,9 @@ #bool 'Debug kmalloc/kfree' CONFIG_DEBUG_MALLOC bool 'Magic SysRq key' CONFIG_MAGIC_SYSRQ +bool 'Extended Profiling' CONFIG_EXTENDED_PROFILING +if [ "$CONFIG_EXTENDED_PROFILING" = "y" ]; then + bool 'Instrument the kernel with calls to mcount()' CONFIG_PROF_MCOUNT y +fi endmenu
build2/dm/2.2.8-profile/arch/i386/kernel/Makefile renamed from usr/src/linux-2.2.8/linux/arch/i386/kernel/Makefile +7 -0
@@ -18,6 +18,10 @@ OX_OBJS := i386_ksyms.o MX_OBJS := +ifdef CONFIG_EXTENDED_PROFILING +O_OBJS += profile.o +endif + ifdef CONFIG_PCI O_OBJS += bios32.o endif
@@ -52,5 +56,8 @@ head.o: head.S $(TOPDIR)/include/linux/tasks.h $(CC) -D__ASSEMBLY__ $(AFLAGS) -traditional -c $*.S -o $*.o + +profile.o: profile.c $(TOPDIR)/include/linux/profile.h + $(CC) $(subst -pg,,$(CFLAGS)) -c $< include $(TOPDIR)/Rules.make
build2/dm/2.2.8-profile/arch/i386/kernel/irq.h renamed from usr/src/linux-2.2.8/linux/arch/i386/kernel/irq.h +9 -0
@@ -3,6 +3,10 @@#include <asm/irq.h> +#ifdef CONFIG_EXTENDED_PROFILING +#include <linux/profile.h> +#endif +/* * Interrupt controller descriptor. This is all we need * to describe about the low-level hardware.
@@ -238,7 +242,12 @@*/ static inline void x86_do_profile (unsigned long eip) { +#ifdef CONFIG_EXTENDED_PROFILING +if ((prof_params.state & (PROFILE_ON |PROF_PC )) == (PROFILE_ON |PROF_PC ) && + current->pid) { +#else if (prof_buffer && current->pid) { +#endif eip -= (unsigned long ) &_stext; eip >>= prof_shift;/*
build2/dm/2.2.8-profile/arch/i386/kernel/profile.c renamed from usr/src/linux-2.2.8/linux/arch/i386/kernel/profile.c +133 -0
133 lines not on this page · raw message
build2/dm/2.2.8-profile/arch/i386/kernel/smp.c renamed from usr/src/linux-2.2.8/linux/arch/i386/kernel/smp.c +32 -18
114 lines not on this page · raw message
build2/dm/2.2.8-profile/fs/proc/array.c renamed from usr/src/linux-2.2.8/linux/fs/proc/array.c +136 -0
172 lines not on this page · raw message
build2/dm/2.2.8-profile/fs/proc/root.c renamed from usr/src/linux-2.2.8/linux/fs/proc/root.c +34 -0
@@ -21,6 +21,9 @@#ifdef CONFIG_ZORRO #include <linux/zorro.h> #endif +#ifdef CONFIG_EXTENDED_PROFILING +#include <linux/profile.h> +#endif /* * Offset of the first process in the /proc root directory..
@@ -178,6 +181,16 @@ };#endif +#ifdef CONFIG_EXTENDED_PROFILING +struct proc_dir_entry proc_prof_root = { +PROC_PROF ,4 ,"prof" , +S_IFDIR |S_IRUGO |S_IXUGO ,2 ,0 ,0 , +0 , &proc_dir_inode_operations, +NULL ,NULL , +NULL , &proc_root,NULL +}; +#endif +#if defined(CONFIG_SUN_OPENPROMFS ) || defined(CONFIG_SUN_OPENPROMFS_MODULE )static int (*proc_openprom_defreaddir_ptr)(struct file *,void *,filldir_t );
@@ -653,6 +666,18 @@S_IFREG |S_IRUGO |S_IWUSR ,1 ,0 ,0 ,0 , &proc_profile_inode_operations }; +#ifdef CONFIG_PROF_MCOUNT +static struct proc_dir_entry proc_prof_froms = { +PROC_PROF_FROMS ,11 ,"gprof_froms" , +S_IFREG |S_IRUGO ,1 ,0 ,0 , +0 , &proc_froms_inode_operations +}; +static struct proc_dir_entry proc_prof_tos = { +PROC_PROF_TOS ,9 ,"gprof_tos" , +S_IFREG |S_IRUGO ,1 ,0 ,0 , +0 , &proc_tos_inode_operations +}; +#endif static struct proc_dir_entry proc_root_slab = {PROC_SLABINFO ,8 ,"slabinfo" , S_IFREG | S_IRUGO, 1, 0, 0,
@@ -732,6 +757,15 @@if (prof_shift) {proc_register (&proc_root, &proc_root_profile); proc_root_profile.size = (1 +prof_len) *sizeof (unsigned int ); +#ifdef CONFIG_EXTENDED_PROFILING +proc_register (&proc_root, &proc_prof_root); +#ifdef CONFIG_PROF_MCOUNT +proc_register (&proc_prof_root, &proc_prof_froms); + proc_prof_froms.size = prof_params.fromssize; +proc_register (&proc_prof_root, &proc_prof_tos); + proc_prof_tos.size = prof_params.tossize; +#endif +#endif }proc_tty_init ();
build2/dm/2.2.8-profile/include/linux/proc_fs.h renamed from usr/src/linux-2.2.8/linux/include/linux/proc_fs.h +19 -0
@@ -52,6 +52,9 @@PROC_STRAM ,PROC_SOUND ,PROC_MTRR ,/* whether enabled or not */ +#ifdef CONFIG_EXTENDED_PROFILING +PROC_PROF , +#endif PROC_FS };
@@ -242,6 +245,15 @@ PROC_CODA_FS_LAST }; +#ifdef CONFIG_EXTENDED_PROFILING + +enum prof_directory_inos { +PROC_PROF_FROMS =PROC_CODA_FS_LAST , +PROC_PROF_TOS , +PROC_PROF_LAST +}; +#endif +/* Finally, the dynamically allocatable proc entries are reserved: */ #define PROC_DYNAMIC_FIRST 4096
@@ -312,6 +324,9 @@extern struct proc_dir_entry proc_pid_fd;extern struct proc_dir_entry proc_mca;extern struct proc_dir_entry *proc_bus; +#ifdef CONFIG_EXTENDED_PROFILING +extern struct proc_dir_entry proc_prof_root; +#endif extern struct inode_operations proc_scsi_inode_operations;
@@ -421,6 +436,10 @@#endif extern struct inode_operations proc_omirr_inode_operations;extern struct inode_operations proc_ppc_htab_inode_operations; +#ifdef CONFIG_PROF_MCOUNT +extern struct inode_operations proc_froms_inode_operations; +extern struct inode_operations proc_tos_inode_operations; +#endif /* * generic.c
build2/dm/2.2.8-profile/include/linux/profile.h renamed from usr/src/linux-2.2.8/linux/include/linux/profile.h +168 -0
168 lines not on this page · raw message
build2/dm/2.2.8-profile/init/main.c renamed from usr/src/linux-2.2.8/linux/init/main.c +13 -0
@@ -50,6 +50,10 @@extern void nubus_init (void );#endif +#ifdef CONFIG_EXTENDED_PROFILING +#include <linux/profile.h> +#endif +/* * Versions of gcc older than that listed below may actually compile * and link okay, but the end product can have subtle run time bugs.
@@ -344,6 +348,10 @@extern void md_setup (char *str,int *ints)__init ;#endif +#ifdef CONFIG_EXTENDED_PROFILING +extern long prof_init (long ,long ); +#endif +/* * Boot command-line arguments */
@@ -1153,8 +1161,13 @@/* only text is profiled */ prof_len = (unsigned long ) &_etext - (unsigned long ) &_stext; prof_len >>= prof_shift; +/* add one bucket for symbols past _etext, e.g. for modules */ + prof_len++; memory_start += prof_len *sizeof (unsigned int );memset (prof_buffer,0 , prof_len *sizeof (unsigned int )); +#ifdef CONFIG_EXTENDED_PROFILING + memory_start =prof_init (memory_start, memory_end); +#endif } memory_start =kmem_cache_init (memory_start, memory_end);
build2/dm/2.2.8-profile/scripts/gmon_out.h renamed from usr/src/linux-2.2.8/linux/scripts/gmon_out.h +77 -0
77 lines not on this page · raw message
build2/dm/2.2.8-profile/scripts/kernprof.c renamed from usr/src/linux-2.2.8/linux/scripts/kernprof.c +571 -0
571 lines not on this page · raw message
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/