Skip to contentSkip to conversation

gcov kernel support

PATCH In review Peter Oberparleiter 5 participants 23 messages Jun 2, 2009 – Jun 8, 2009

Subsystems (8)
Documentation x86 VFS Kbuild +4 more subsystems
lore
Share & tools
lore
#0 lore raw

cover gcov kernel support

This patchset implements support for performing kernel code coverage measurements based on gcc's gcov mechanism. It can be used to improve kernel code quality by identifying code parts which are not exercised during test cases. Patch base is 2.6.30-rc7.

Changes since last version:
* removed __gcov_execve (assuming no kernel function will be called execve)

Patch overview:
[PATCH 1/4] kernel: constructor support
[PATCH 2/4] seq_file: add function to write binary data [PATCH 3/4] gcov: add gcov profiling infrastructure [PATCH 4/4] gcov: enable GCOV_PROFILE_ALL for x86_64

For more information see Documentation/gcov.txt and the previous post:
http://marc.info/?l=linux-kernel&m=123565658224661

#1 lore raw

PATCH 1/4 kernel: constructor support

From: Peter Oberparleiter <[email protected]>

Call constructors (gcc-generated initcall-like functions) during kernel start and module load. Constructors are e.g. used for gcov data initialization.

Disable constructor support for usermode Linux to prevent conflicts with host glibc.

---
 include/asm-generic/sections.h    |    3 +++
 include/asm-generic/vmlinux.lds.h |   11 ++++++++++-
 include/linux/init.h              |    3 +++
 include/linux/module.h            |    6 ++++++
 init/Kconfig                      |    5 +++++
 init/main.c                       |   12 ++++++++++++
 kernel/module.c                   |   16 ++++++++++++++++
 7 files changed, 55 insertions(+), 1 deletion(-)
include/asm-generic/vmlinux.lds.h +10 -1
Index: linux-2.6.30-rc7/include/asm-generic/vmlinux.lds.h
===================================================================
@@ -332,6 +332,14 @@
 /* Section used for early init (in .S files) */
 #define HEAD_TEXT  *(HEAD_TEXT_SECTION)
 
+#ifdef CONFIG_CONSTRUCTORS
+#define KERNEL_CTORS()	VMLINUX_SYMBOL(__ctors_start) = .; \
+			*(.ctors)			   \
+			VMLINUX_SYMBOL(__ctors_end) = .;
+#else
+#define KERNEL_CTORS()
+#endif
+
 /* init and exit section handling */
 #define INIT_DATA							\
 	*(.init.data)							\
@@ -340,7 +348,8 @@
 	CPU_DISCARD(init.data)						\
 	CPU_DISCARD(init.rodata)					\
 	MEM_DISCARD(init.data)						\
-	MEM_DISCARD(init.rodata)
+	MEM_DISCARD(init.rodata)					\
+	KERNEL_CTORS()
 
 #define INIT_TEXT							\
 	*(.init.text)							\
include/linux/init.h +3 -0
Index: linux-2.6.30-rc7/include/linux/init.h
===================================================================
@@ -136,6 +136,9 @@
 extern initcall_t __con_initcall_start[], __con_initcall_end[];
 extern initcall_t __security_initcall_start[], __security_initcall_end[];
 
+/* Used for contructor calls. */
+typedef void (*ctor_fn_t)(void);
+
 /* Defined in init/main.c */
 extern int do_one_initcall(initcall_t fn);
 extern char __initdata boot_command_line[];
include/linux/module.h +6 -0
Index: linux-2.6.30-rc7/include/linux/module.h
===================================================================
@@ -354,6 +354,12 @@
 	local_t ref;
 #endif
 #endif
+
+#ifdef CONFIG_CONSTRUCTORS
+	/* Constructor functions. */
+	ctor_fn_t *ctors;
+	unsigned int num_ctors;
+#endif
 };
 #ifndef MODULE_ARCH_INIT
 #define MODULE_ARCH_INIT {}
init/main.c +12 -0
Index: linux-2.6.30-rc7/init/main.c
===================================================================
@@ -700,6 +700,17 @@
 	rest_init();
 }
 
+/* Call all constructor functions linked into the kernel. */
+static void __init do_ctors(void)
+{
+#ifdef CONFIG_CONSTRUCTORS
+	ctor_fn_t *call = (ctor_fn_t *) __ctors_start;
+
+	for (; call < (ctor_fn_t *) __ctors_end; call++)
+		(*call)();
+#endif
+}
+
 int initcall_debug;
 core_param(initcall_debug, initcall_debug, bool, 0644);
 
@@ -780,6 +791,7 @@
 	usermodehelper_init();
 	driver_init();
 	init_irq_proc();
+	do_ctors();
 	do_initcalls();
 }
 
kernel/module.c +16 -0
Index: linux-2.6.30-rc7/kernel/module.c
===================================================================
@@ -2172,6 +2172,10 @@
 					sizeof(*mod->tracepoints),
 					&mod->num_tracepoints);
 #endif
+#ifdef CONFIG_CONSTRUCTORS
+	mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
+				  sizeof(*mod->ctors), &mod->num_ctors);
+#endif
 
 #ifdef CONFIG_MODVERSIONS
 	if ((mod->num_syms && !mod->crcs)
@@ -2328,6 +2332,17 @@
 	goto free_hdr;
 }
 
+/* Call module constructors. */
+static void do_mod_ctors(struct module *mod)
+{
+#ifdef CONFIG_CONSTRUCTORS
+	unsigned long i;
+
+	for (i = 0; i < mod->num_ctors; i++)
+		mod->ctors[i]();
+#endif
+}
+
 /* This is where the real work happens */
 SYSCALL_DEFINE3(init_module, void __user *, umod,
 		unsigned long, len, const char __user *, uargs)
@@ -2356,6 +2371,7 @@
 	blocking_notifier_call_chain(&module_notify_list,
 			MODULE_STATE_COMING, mod);
 
+	do_mod_ctors(mod);
 	/* Start the module */
 	if (mod->init != NULL)
 		ret = do_one_initcall(mod->init);
init/Kconfig +5 -0
Index: linux-2.6.30-rc7/init/Kconfig
===================================================================
@@ -16,6 +16,11 @@
 	default "$ARCH_DEFCONFIG"
 	default "arch/$ARCH/defconfig"
 
+config CONSTRUCTORS
+	bool
+	depends on !UML
+	default y
+
 menu "General setup"
 
 config EXPERIMENTAL
include/asm-generic/sections.h +3 -0
Index: linux-2.6.30-rc7/include/asm-generic/sections.h
===================================================================
@@ -14,6 +14,9 @@
 extern char __initdata_begin[], __initdata_end[];
 extern char __start_rodata[], __end_rodata[];
 
+/* Start and end of .ctors section - used for constructor calls. */
+extern char __ctors_start[], __ctors_end[];
+
 /* function descriptor handling (if any).  Override
  * in asm/sections.h */
 #ifndef dereference_function_descriptor
#2 lore raw

Re: [PATCH 1/4] kernel: constructor support

include/asm-generic/vmlinux.lds.h

Quoted 19 lines · On Tue, 02 Jun 2009 13:44:00 +0200 Peter Oberparleiter…

From: Peter Oberparleiter <[email protected]>

Call constructors (gcc-generated initcall-like functions) during kernel start and module load. Constructors are e.g. used for gcov data initialization.

Disable constructor support for usermode Linux to prevent conflicts with host glibc.

...

include/asm-generic/vmlinux.lds.h +0 -0

Sam's tree has mucked with this file in linux-next.

Quoted 16 lines
include/asm-generic/vmlinux.lds.h +8 -0
@@ -332,6 +332,14 @@
 /* Section used for early init (in .S files) */
 #define HEAD_TEXT  *(HEAD_TEXT_SECTION)
 
+#ifdef CONFIG_CONSTRUCTORS
+#define KERNEL_CTORS()	VMLINUX_SYMBOL(__ctors_start) = .; \
+			*(.ctors)			   \
+			VMLINUX_SYMBOL(__ctors_end) = .;
+#else
+#define KERNEL_CTORS()
+#endif
+
 /* init and exit section handling */
 #define INIT_DATA							\
 	*(.init.data)							\

include/asm-generic/vmlinux.lds.h line 353

Quoted 7 lines
include/asm-generic/vmlinux.lds.h +2 -1
@@ -340,7 +348,8 @@
 	CPU_DISCARD(init.data)						\
 	CPU_DISCARD(init.rodata)					\
 	MEM_DISCARD(init.data)						\
-	MEM_DISCARD(init.rodata)
+	MEM_DISCARD(init.rodata)					\
+	KERNEL_CTORS()
 

What we now have here is

#define INIT_DATA							\
	*(.init.data)							\
	DEV_DISCARD(init.data)						\
	CPU_DISCARD(init.data)						\
	MEM_DISCARD(init.data)						\
	*(.init.rodata)							\
	DEV_DISCARD(init.rodata)					\
	CPU_DISCARD(init.rodata)					\
	MEM_DISCARD(init.rodata)

and I don't think that you wanted KERNEL_DTORS() inside the .rodata section, so I did this:

#define INIT_DATA							\
	*(.init.data)							\
	DEV_DISCARD(init.data)						\
	CPU_DISCARD(init.data)						\
	MEM_DISCARD(init.data)						\
	KERNEL_CTORS()							\
	*(.init.rodata)							\
	DEV_DISCARD(init.rodata)					\
	CPU_DISCARD(init.rodata)					\
	MEM_DISCARD(init.rodata)

Please check that?

#3 lore raw

Re: [PATCH 1/4] kernel: constructor support

Quoted 59 lines · Andrew Morton

include/asm-generic/vmlinux.lds.h

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:00 +0200 Peter Oberparleiter…

...

include/asm-generic/vmlinux.lds.h +0 -0

Sam's tree has mucked with this file in linux-next.

Quoted 16 lines
include/asm-generic/vmlinux.lds.h +8 -0
@@ -332,6 +332,14 @@
 /* Section used for early init (in .S files) */
 #define HEAD_TEXT  *(HEAD_TEXT_SECTION)
 
+#ifdef CONFIG_CONSTRUCTORS
+#define KERNEL_CTORS()	VMLINUX_SYMBOL(__ctors_start) = .; \
+			*(.ctors)			   \
+			VMLINUX_SYMBOL(__ctors_end) = .;
+#else
+#define KERNEL_CTORS()
+#endif
+
 /* init and exit section handling */
 #define INIT_DATA							\
 	*(.init.data)							\

include/asm-generic/vmlinux.lds.h line 353

Quoted 7 lines
include/asm-generic/vmlinux.lds.h +2 -1
@@ -340,7 +348,8 @@
 	CPU_DISCARD(init.data)						\
 	CPU_DISCARD(init.rodata)					\
 	MEM_DISCARD(init.data)						\
-	MEM_DISCARD(init.rodata)
+	MEM_DISCARD(init.rodata)					\
+	KERNEL_CTORS()
 

What we now have here is

#define INIT_DATA							\
	*(.init.data)							\
	DEV_DISCARD(init.data)						\
	CPU_DISCARD(init.data)						\
	MEM_DISCARD(init.data)						\
	*(.init.rodata)							\
	DEV_DISCARD(init.rodata)					\
	CPU_DISCARD(init.rodata)					\
	MEM_DISCARD(init.rodata)

and I don't think that you wanted KERNEL_DTORS() inside the .rodata section, so I did this:

#define INIT_DATA							\
	*(.init.data)							\
	DEV_DISCARD(init.data)						\
	CPU_DISCARD(init.data)						\
	MEM_DISCARD(init.data)						\
	KERNEL_CTORS()							\
	*(.init.rodata)							\
	DEV_DISCARD(init.rodata)					\
	CPU_DISCARD(init.rodata)					\
	MEM_DISCARD(init.rodata)
Quoted 1 line

Please check that?

Looks good, compiles and still works. So that's an ACK from me.

#4 lore raw

PATCH 2/4 seq_file: add function to write binary data

In reply to Peter Oberparleiter

From: Peter Oberparleiter <[email protected]>

seq_write() can be used to construct seq_files containing arbitrary data. Required by the gcov-profiling interface to synthesize binary profiling data files.

---

 fs/seq_file.c            |   20 ++++++++++++++++++++
 include/linux/seq_file.h |    1 +
 2 files changed, 21 insertions(+)
fs/seq_file.c +20 -0
Index: linux-2.6.30-rc7/fs/seq_file.c
===================================================================
@@ -640,6 +640,26 @@
 }
 EXPORT_SYMBOL(seq_puts);
 
+/**
+ * seq_write - write arbitrary data to buffer
+ * @seq: seq_file identifying the buffer to which data should be written
+ * @data: data address
+ * @len: number of bytes
+ *
+ * Return 0 on success, non-zero otherwise.
+ */
+int seq_write(struct seq_file *seq, const void *data, size_t len)
+{
+	if (seq->count + len < seq->size) {
+		memcpy(seq->buf + seq->count, data, len);
+		seq->count += len;
+		return 0;
+	}
+	seq->count = seq->size;
+	return -1;
+}
+EXPORT_SYMBOL(seq_write);
+
 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
 {
 	struct list_head *lh;
include/linux/seq_file.h +1 -0
Index: linux-2.6.30-rc7/include/linux/seq_file.h
===================================================================
@@ -43,6 +43,7 @@
 int seq_escape(struct seq_file *, const char *, const char *);
 int seq_putc(struct seq_file *m, char c);
 int seq_puts(struct seq_file *m, const char *s);
+int seq_write(struct seq_file *seq, const void *data, size_t len);
 
 int seq_printf(struct seq_file *, const char *, ...)
 	__attribute__ ((format (printf,2,3)));
#5 lore raw

PATCH 3/4 gcov: add gcov profiling infrastructure

In reply to Peter Oberparleiter

From: Peter Oberparleiter <[email protected]>

Enable the use of GCC's coverage testing tool gcov [1] with the Linux kernel. gcov may be useful for:

* debugging (has this code been reached at all?)
* test improvement (how do I change my test to cover these lines?)
* minimizing kernel configurations (do I need this option if the associated code is never run?)

The profiling patch incorporates the following changes:

* change kbuild to include profiling flags
* provide functions needed by profiling code
* present profiling data as files in debugfs

Note that on some architectures, enabling gcc's profiling option "-fprofile-arcs" for the entire kernel may trigger compile/link/ run-time problems, some of which are caused by toolchain bugs and others which require adjustment of architecture code.

For this reason profiling the entire kernel is initially restricted to those architectures for which it is known to work without changes. This restriction can be lifted once an architecture has been tested and found compatible with gcc's profiling. Profiling of single files or directories is still available on all platforms (see config help text).

[1] http://gcc.gnu.org/onlinedocs/gcc/Gcov.html

---

 Documentation/gcov.txt              |  246 +++++++++++++
 Documentation/kernel-parameters.txt |    7 
 Makefile                            |   11 
 arch/Kconfig                        |    2 
 include/linux/compiler-gcc3.h       |    6 
 kernel/Makefile                     |    1 
 kernel/gcov/Kconfig                 |   48 ++
 kernel/gcov/Makefile                |    3 
 kernel/gcov/base.c                  |  148 +++++++
 kernel/gcov/fs.c                    |  674 ++++++++++++++++++++++++++++++++++++
 kernel/gcov/gcc_3_4.c               |  445 +++++++++++++++++++++++
 kernel/gcov/gcov.h                  |  128 ++++++
 scripts/Makefile.lib                |   11 
 13 files changed, 1725 insertions(+), 5 deletions(-)
Documentation/gcov.txt new file +246 -0
Index: linux-2.6.30-rc7/Documentation/gcov.txt
===================================================================
@@ -0,0 +1,246 @@
+Using gcov with the Linux kernel
+================================
+
+1. Introduction
+2. Preparation
+3. Customization
+4. Files
+5. Modules
+6. Separated build and test machines
+7. Troubleshooting
+Appendix A: sample script: gather_on_build.sh
+Appendix B: sample script: gather_on_test.sh
+
+
+1. Introduction
+===============
+
+gcov profiling kernel support enables the use of GCC's coverage testing
+tool gcov [1] with the Linux kernel. Coverage data of a running kernel
+is exported in gcov-compatible format via the "gcov" debugfs directory.
+To get coverage data for a specific file, change to the kernel build
+directory and use gcov with the -o option as follows (requires root):
+
+# cd /tmp/linux-out
+# gcov -o /sys/kernel/debug/gcov/tmp/linux-out/kernel spinlock.c
+
+This will create source code files annotated with execution counts
+in the current directory. In addition, graphical gcov front-ends such
+as lcov [2] can be used to automate the process of collecting data
+for the entire kernel and provide coverage overviews in HTML format.
+
+Possible uses:
+
+* debugging (has this line been reached at all?)
+* test improvement (how do I change my test to cover these lines?)
+* minimizing kernel configurations (do I need this option if the
+  associated code is never run?)
+
+--
+
+[1] http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
+[2] http://ltp.sourceforge.net/coverage/lcov.php
+
+
+2. Preparation
+==============
+
+Configure the kernel with:
+
+        CONFIG_DEBUGFS=y
+        CONFIG_GCOV_KERNEL=y
+
+and to get coverage data for the entire kernel:
+
+        CONFIG_GCOV_PROFILE_ALL=y
+
+Note that kernels compiled with profiling flags will be significantly
+larger and run slower. Also CONFIG_GCOV_PROFILE_ALL may not be supported
+on all architectures.
+
+Profiling data will only become accessible once debugfs has been
+mounted:
+
+        mount -t debugfs none /sys/kernel/debug
+
+
+3. Customization
+================
+
+To enable profiling for specific files or directories, add a line
+similar to the following to the respective kernel Makefile:
+
+        For a single file (e.g. main.o):
+                GCOV_PROFILE_main.o := y
+
+        For all files in one directory:
+                GCOV_PROFILE := y
+
+To exclude files from being profiled even when CONFIG_GCOV_PROFILE_ALL
+is specified, use:
+
+                GCOV_PROFILE_main.o := n
+        and:
+                GCOV_PROFILE := n
+
+Only files which are linked to the main kernel image or are compiled as
+kernel modules are supported by this mechanism.
+
+
+4. Files
+========
+
+The gcov kernel support creates the following files in debugfs:
+
+        /sys/kernel/debug/gcov
+                Parent directory for all gcov-related files.
+
+        /sys/kernel/debug/gcov/reset
+                Global reset file: resets all coverage data to zero when
+                written to.
+
+        /sys/kernel/debug/gcov/path/to/compile/dir/file.gcda
+                The actual gcov data file as understood by the gcov
+                tool. Resets file coverage data to zero when written to.
+
+        /sys/kernel/debug/gcov/path/to/compile/dir/file.gcno
+                Symbolic link to a static data file required by the gcov
+                tool. This file is generated by gcc when compiling with
+                option -ftest-coverage.
+
+
+5. Modules
+==========
+
+Kernel modules may contain cleanup code which is only run during
+module unload time. The gcov mechanism provides a means to collect
+coverage data for such code by keeping a copy of the data associated
+with the unloaded module. This data remains available through debugfs.
+Once the module is loaded again, the associated coverage counters are
+initialized with the data from its previous instantiation.
+
+This behavior can be deactivated by specifying the gcov_persist kernel
+parameter:
+
+        gcov_persist=0
+
+At run-time, a user can also choose to discard data for an unloaded
+module by writing to its data file or the global reset file.
+
+
+6. Separated build and test machines
+====================================
+
+The gcov kernel profiling infrastructure is designed to work out-of-the
+box for setups where kernels are built and run on the same machine. In
+cases where the kernel runs on a separate machine, special preparations
+must be made, depending on where the gcov tool is used:
+
+a) gcov is run on the TEST machine
+
+The gcov tool version on the test machine must be compatible with the
+gcc version used for kernel build. Also the following files need to be
+copied from build to test machine:
+
+from the source tree:
+  - all C source files + headers
+
+from the build tree:
+  - all C source files + headers
+  - all .gcda and .gcno files
+  - all links to directories
+
+It is important to note that these files need to be placed into the
+exact same file system location on the test machine as on the build
+machine. If any of the path components is symbolic link, the actual
+directory needs to be used instead (due to make's CURDIR handling).
+
+b) gcov is run on the BUILD machine
+
+The following files need to be copied after each test case from test
+to build machine:
+
+from the gcov directory in sysfs:
+  - all .gcda files
+  - all links to .gcno files
+
+These files can be copied to any location on the build machine. gcov
+must then be called with the -o option pointing to that directory.
+
+Example directory setup on the build machine:
+
+  /tmp/linux:    kernel source tree
+  /tmp/out:      kernel build directory as specified by make O=
+  /tmp/coverage: location of the files copied from the test machine
+
+  [user@build] cd /tmp/out
+  [user@build] gcov -o /tmp/coverage/tmp/out/init main.c
+
+
+7. Troubleshooting
+==================
+
+Problem:  Compilation aborts during linker step.
+Cause:    Profiling flags are specified for source files which are not
+          linked to the main kernel or which are linked by a custom
+          linker procedure.
+Solution: Exclude affected source files from profiling by specifying
+          GCOV_PROFILE := n or GCOV_PROFILE_basename.o := n in the
+          corresponding Makefile.
+
+
+Appendix A: gather_on_build.sh
+==============================
+
+Sample script to gather coverage meta files on the build machine
+(see 6a):
+
+#!/bin/bash
+
+KSRC=$1
+KOBJ=$2
+DEST=$3
+
+if [ -z "$KSRC" ] || [ -z "$KOBJ" ] || [ -z "$DEST" ]; then
+  echo "Usage: $0 <ksrc directory> <kobj directory> <output.tar.gz>" >&2
+  exit 1
+fi
+
+KSRC=$(cd $KSRC; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
+KOBJ=$(cd $KOBJ; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
+
+find $KSRC $KOBJ \( -name '*.gcno' -o -name '*.[ch]' -o -type l \) -a \
+                 -perm /u+r,g+r | tar cfz $DEST -P -T -
+
+if [ $? -eq 0 ] ; then
+  echo "$DEST successfully created, copy to test system and unpack with:"
+  echo "  tar xfz $DEST -P"
+else
+  echo "Could not create file $DEST"
+fi
+
+
+Appendix B: gather_on_test.sh
+=============================
+
+Sample script to gather coverage data files on the test machine
+(see 6b):
+
+#!/bin/bash
+
+DEST=$1
+GCDA=/sys/kernel/debug/gcov
+
+if [ -z "$DEST" ] ; then
+  echo "Usage: $0 <output.tar.gz>" >&2
+  exit 1
+fi
+
+find $GCDA -name '*.gcno' -o -name '*.gcda' | tar cfz $DEST -T -
+
+if [ $? -eq 0 ] ; then
+  echo "$DEST successfully created, copy to build system and unpack with:"
+  echo "  tar xfz $DEST"
+else
+  echo "Could not create file $DEST"
+fi
Documentation/kernel-parameters.txt +7 -0
Index: linux-2.6.30-rc7/Documentation/kernel-parameters.txt
===================================================================
@@ -48,6 +48,7 @@
 	EFI	EFI Partitioning (GPT) is enabled
 	EIDE	EIDE/ATAPI support is enabled.
 	FB	The frame buffer device is enabled.
+	GCOV	GCOV profiling is enabled.
 	HW	Appropriate hardware is enabled.
 	IA-64	IA-64 architecture is enabled.
 	IMA     Integrity measurement architecture is enabled.
@@ -771,6 +772,12 @@
 			Format: off | on
 			default: on
 
+	gcov_persist=	[GCOV] When non-zero (default), profiling data for
+			kernel modules is saved and remains accessible via
+			debugfs, even when the module is unloaded/reloaded.
+			When zero, profiling data is discarded and associated
+			debugfs files are removed at module unload time.
+
 	gdth=		[HW,SCSI]
 			See header of drivers/scsi/gdth.c.
 
Makefile +6 -5
Index: linux-2.6.30-rc7/Makefile
===================================================================
@@ -338,6 +338,7 @@
 LDFLAGS_MODULE  =
 CFLAGS_KERNEL	=
 AFLAGS_KERNEL	=
+CFLAGS_GCOV	= -fprofile-arcs -ftest-coverage
 
 
 # Use LINUXINCLUDE when you must reference the include/ directory.
@@ -364,7 +365,7 @@
 export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
 
 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
-export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
+export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE CFLAGS_GCOV
 export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
 
 # When compiling out-of-tree modules, put MODVERDIR in the module
@@ -1224,8 +1225,8 @@
 		\( -name '*.[oas]' -o -name '*.ko' -o -name '.*.cmd' \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
 		-o -name '*.symtypes' -o -name 'modules.order' \
-		-o -name 'Module.markers' -o -name '.tmp_*.o.*' \) \
-		-type f -print | xargs rm -f
+		-o -name 'Module.markers' -o -name '.tmp_*.o.*' \
+		-o -name '*.gcno' \) -type f -print | xargs rm -f
 
 # mrproper - Delete all generated files, including .config
 #
@@ -1429,8 +1430,8 @@
 	$(call cmd,rmfiles)
 	@find $(KBUILD_EXTMOD) $(RCS_FIND_IGNORE) \
 		\( -name '*.[oas]' -o -name '*.ko' -o -name '.*.cmd' \
-		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \) \
-		-type f -print | xargs rm -f
+		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
+		-o -name '*.gcno' \) -type f -print | xargs rm -f
 
 help:
 	@echo  '  Building external modules.'
arch/Kconfig +2 -0
Index: linux-2.6.30-rc7/arch/Kconfig
===================================================================
@@ -112,3 +112,5 @@
 
 config HAVE_DEFAULT_NO_SPIN_MUTEXES
 	bool
+
+source "kernel/gcov/Kconfig"
kernel/Makefile +1 -0
Index: linux-2.6.30-rc7/kernel/Makefile
===================================================================
@@ -70,6 +70,7 @@
 obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
 obj-$(CONFIG_AUDIT) += audit.o auditfilter.o
 obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
+obj-$(CONFIG_GCOV_KERNEL) += gcov/
 obj-$(CONFIG_AUDIT_TREE) += audit_tree.o
 obj-$(CONFIG_KPROBES) += kprobes.o
 obj-$(CONFIG_KGDB) += kgdb.o
kernel/gcov/Kconfig new file +48 -0

48 lines not on this page · raw message

kernel/gcov/Makefile new file +3 -0
Index: linux-2.6.30-rc7/kernel/gcov/Makefile
===================================================================
@@ -0,0 +1,3 @@
+EXTRA_CFLAGS := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
+
+obj-$(CONFIG_GCOV_KERNEL) := base.o fs.o gcc_3_4.o
kernel/gcov/base.c new file +148 -0

148 lines not on this page · raw message

kernel/gcov/fs.c new file +674 -0

674 lines not on this page · raw message

kernel/gcov/gcc_3_4.c new file +445 -0

445 lines not on this page · raw message

kernel/gcov/gcov.h new file +128 -0

128 lines not on this page · raw message

scripts/Makefile.lib +11 -0
Index: linux-2.6.30-rc7/scripts/Makefile.lib
===================================================================
@@ -116,6 +116,17 @@
                  $(asflags-y) $(AFLAGS_$(basetarget).o)
 _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
 
+#
+# Enable gcov profiling flags for a file, directory or for all files depending
+# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
+# (in this order)
+#
+ifeq ($(CONFIG_GCOV_KERNEL),y)
+_c_flags += $(if $(patsubst n%,, \
+		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
+		$(CFLAGS_GCOV))
+endif
+
 # If building the kernel in a separate objtree expand all occurrences
 # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
 
include/linux/compiler-gcc3.h +6 -0
Index: linux-2.6.30-rc7/include/linux/compiler-gcc3.h
===================================================================
@@ -16,6 +16,12 @@
 #define __must_check		__attribute__((warn_unused_result))
 #endif
 
+#ifdef CONFIG_GCOV_KERNEL
+# if __GNUC_MINOR__ < 4
+#   error "GCOV profiling support for gcc versions below 3.4 not included"
+# endif /* __GNUC_MINOR__ */
+#endif /* CONFIG_GCOV_KERNEL */
+
 /*
  * A trick to suppress uninitialized variable warning without generating any
  * code
#6 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 32 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…

From: Peter Oberparleiter <[email protected]>

Enable the use of GCC's coverage testing tool gcov [1] with the Linux kernel. gcov may be useful for:

* debugging (has this code been reached at all?)
* test improvement (how do I change my test to cover these lines?)
* minimizing kernel configurations (do I need this option if the associated code is never run?)

The profiling patch incorporates the following changes:

* change kbuild to include profiling flags
* provide functions needed by profiling code
* present profiling data as files in debugfs

Note that on some architectures, enabling gcc's profiling option "-fprofile-arcs" for the entire kernel may trigger compile/link/ run-time problems, some of which are caused by toolchain bugs and others which require adjustment of architecture code.

For this reason profiling the entire kernel is initially restricted to those architectures for which it is known to work without changes. This restriction can be lifted once an architecture has been tested and found compatible with gcc's profiling. Profiling of single files or directories is still available on all platforms (see config help text).

[1] http://gcc.gnu.org/onlinedocs/gcc/Gcov.html

Quoted 6 lines

...

+/*
+ * __gcov_init is called by gcc-generated constructor code for each object
+ * file compiled with -fprofile-arcs.
+ */

How does this work? At what time does gcc call the constructors, and in what context are they called, etc?

IOW, please teach me about -fprofile-arcs :)

kernel/gcov/base.c line 42

Quoted 12 lines
kernel/gcov/base.c new file +12 -0
+void __gcov_init(struct gcov_info *info)
+{
+	static unsigned int gcov_version;
+
+	mutex_lock(&gcov_lock);
+	if (gcov_version == 0) {
+		gcov_version = info->version;
+		/*
+		 * Printing gcc's version magic may prove useful for debugging
+		 * incompatibility reports.
+		 */
+		pr_info("version magic: 0x%x\n", gcov_version);

Will this be spat out as simply

version magic: 0x1234

? If so, that'll be rather obscure because people won't know what subsystem printed it. Prefixing this (and all other printks) with "gcov: " would fix that.

Quoted 11 lines
kernel/gcov/base.c new file +11 -0
+	}
+	/*
+	 * Add new profiling data structure to list and inform event
+	 * listener.
+	 */
+	info->next = gcov_info_head;
+	gcov_info_head = info;
+	if (gcov_events_enabled)
+		gcov_event(GCOV_ADD, info);
+	mutex_unlock(&gcov_lock);
+}

kernel/gcov/base.c line 108

Quoted 10 lines
kernel/gcov/base.c new file +7 -0
+EXPORT_SYMBOL(__gcov_init);
+
 
...
 
+#ifdef CONFIG_MODULES
+static inline int within(void *addr, void *start, unsigned long size)
+{
+	return ((addr >= start) && (addr < start + size));
+}

That's our fourth implementation of within() (at least). All basically identical. Whine.

Quoted 8 lines

...

kernel/gcov/fs.c new file +5 -0
+static int __init gcov_persist_setup(char *str)
+{
+	int val;
+	char delim;
+

kernel/gcov/fs.c line 85

Quoted 10 lines
kernel/gcov/fs.c new file +10 -0
+	if (sscanf(str, "%d %c", &val, &delim) != 1) {
+		pr_warning("invalid gcov_persist parameter '%s'\n", str);
+		return 0;
+	}
+	pr_info("setting gcov_persist to %d\n", val);
+	gcov_persist = val;
+
+	return 1;
+}
+__setup("gcov_persist=", gcov_persist_setup);

hm, what's the input format here? It looks like

gcov_persist=1 x

and " x" is checked for, but ignored.

Confused. What's this all for? Can't we just us plain old strtoul(), etc?

Quoted 7 lines
kernel/gcov/fs.c new file +7 -0
+/*
+ * seq_file.start() implementation for gcov data files. Note that the
+ * gcov_iterator interface is designed to be more restrictive than seq_file
+ * (no start from arbitrary position, etc.), to simplify the iterator
+ * implementation.
+ */
+static void *gcov_seq_start(struct seq_file *seq, loff_t *pos)

kernel/gcov/fs.c line 103

Quoted 10 lines
kernel/gcov/fs.c new file +10 -0
+{
+	loff_t i;
+
+	gcov_iter_start(seq->private);
+	for (i = 0; i < *pos; i++) {
+		if (gcov_iter_next(seq->private))
+			return NULL;
+	}
+	return seq->private;
+}

This looks like it could be very expensive if used inappropriately. I guess the answer to that is "don't use it inapprpriately", yes?

kernel/gcov/gcc_3_4.c line 132

Quoted 13 lines

...

kernel/gcov/gcc_3_4.c new file +10 -0
+struct gcov_info *gcov_info_dup(struct gcov_info *info)
+{
+	struct gcov_info *dup;
+	unsigned int i;
+	unsigned int active;
+
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Quoted 23 lines
kernel/gcov/gcc_3_4.c new file +23 -0
+	if (!dup)
+		return NULL;
+	dup->version		= info->version;
+	dup->stamp		= info->stamp;
+	dup->n_functions	= info->n_functions;
+	dup->ctr_mask		= info->ctr_mask;
+	/* Duplicate filename. */
+	dup->filename		= kstrdup(info->filename, GFP_KERNEL);
+	if (!dup->filename)
+		goto err_free;
+	/* Duplicate table of functions. */
+	dup->functions = kmemdup(info->functions, info->n_functions *
+				 get_fn_size(info), GFP_KERNEL);
+	if (!dup->functions)
+		goto err_free;
+	/* Duplicate counter arrays. */
+	for (i = 0; i < active ; i++) {
+		struct gcov_ctr_info *ctr = &info->counts[i];
+		size_t size = ctr->num * sizeof(gcov_type);
+
+		dup->counts[i].num = ctr->num;
+		dup->counts[i].merge = ctr->merge;
+		dup->counts[i].values = kmemdup(ctr->values, size, GFP_KERNEL);

kernel/gcov/gcc_3_4.c line 165

Quoted 10 lines
kernel/gcov/gcc_3_4.c new file +10 -0
+		if (!dup->counts[i].values)
+			goto err_free;
+	}
+	return dup;
+
+err_free:
+	gcov_info_free(dup);
+	return NULL;
+}
+

It all looks very nice to me.

#7 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 18 lines · Andrew Morton

kernel/gcov/base.c line 42

Quoted 12 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
kernel/gcov/base.c new file +12 -0
+void __gcov_init(struct gcov_info *info)
+{
+	static unsigned int gcov_version;
+
+	mutex_lock(&gcov_lock);
+	if (gcov_version == 0) {
+		gcov_version = info->version;
+		/*
+		 * Printing gcc's version magic may prove useful for debugging
+		 * incompatibility reports.
+		 */
+		pr_info("version magic: 0x%x\n", gcov_version);

Will this be spat out as simply

Quoted 5 lines

version magic: 0x1234

? If so, that'll be rather obscure because people won't know what subsystem printed it. Prefixing this (and all other printks) with "gcov: " would fix that.

Great minds think alike, see a few lines above:

+#define pr_fmt(fmt) "gcov: " fmt

cheers

#8 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

In reply to Andrew Morton
Quoted 11 lines · Andrew Morton
Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…

+/*
+ * __gcov_init is called by gcc-generated constructor code for each object
+ * file compiled with -fprofile-arcs.
+ */

How does this work? At what time does gcc call the constructors, and in what context are they called, etc?

IOW, please teach me about -fprofile-arcs :)

No problem :) -fprofile-arcs cause gcc to insert profiling code which relies on gcc's constructor mechanism for initialization purposes (so the two are separate things while the former makes use of the latter). gcc's constructor mechanism also has uses besides -fprofile-arcs, such as calling C++ object constructors and the like.

In userland, constructor functions are called by one of the gcc libraries just before the main() function. For the kernel, the kernel constructor patch mimics a similar behavior:
* for code which was compiled in, constructor functions are called
in do_basic_setup(), just before processing initcalls
* for modules, init_module() calls them before mod->init().
The context is in both cases that of the calling functions.

Note that any function can be made a constructor function by tagging it with __attribute__((constructor)), though I'm not sure if that has any feasible use.

Quoted 15 lines

kernel/gcov/base.c line 42

Quoted 12 lines
kernel/gcov/base.c new file +12 -0
+void __gcov_init(struct gcov_info *info)
+{
+	static unsigned int gcov_version;
+
+	mutex_lock(&gcov_lock);
+	if (gcov_version == 0) {
+		gcov_version = info->version;
+		/*
+		 * Printing gcc's version magic may prove useful for debugging
+		 * incompatibility reports.
+		 */
+		pr_info("version magic: 0x%x\n", gcov_version);

Will this be spat out as simply

Quoted 5 lines

version magic: 0x1234

? If so, that'll be rather obscure because people won't know what subsystem printed it. Prefixing this (and all other printks) with "gcov: " would fix that.

As Michael already pointed out that's already being taken care of by the
#define pr_fmt.

Quoted 17 lines
Quoted 17 lines
kernel/gcov/base.c new file +13 -0
+	}
+	/*
+	 * Add new profiling data structure to list and inform event
+	 * listener.
+	 */
+	info->next = gcov_info_head;
+	gcov_info_head = info;
+	if (gcov_events_enabled)
+		gcov_event(GCOV_ADD, info);
+	mutex_unlock(&gcov_lock);
+}
+EXPORT_SYMBOL(__gcov_init);
+
 
...
Quoted 8 lines

kernel/gcov/base.c line 108

Quoted 5 lines
kernel/gcov/base.c new file +5 -0
+#ifdef CONFIG_MODULES
+static inline int within(void *addr, void *start, unsigned long size)
+{
+	return ((addr >= start) && (addr < start + size));
+}

That's our fourth implementation of within() (at least). All basically identical. Whine.

I know, I know.. and I will try to come up with another patch that consolidates all those within() implementations - but seeing that this might take more discussions to get right, I'd rather only start with that after making sure that the gcov code is finalized.

Quoted 21 lines
Quoted 8 lines

...

kernel/gcov/fs.c new file +5 -0
+static int __init gcov_persist_setup(char *str)
+{
+	int val;
+	char delim;
+

kernel/gcov/fs.c line 85

Quoted 10 lines
kernel/gcov/fs.c new file +10 -0
+	if (sscanf(str, "%d %c", &val, &delim) != 1) {
+		pr_warning("invalid gcov_persist parameter '%s'\n", str);
+		return 0;
+	}
+	pr_info("setting gcov_persist to %d\n", val);
+	gcov_persist = val;
+
+	return 1;
+}
+__setup("gcov_persist=", gcov_persist_setup);

hm, what's the input format here? It looks like

Quoted 5 lines

gcov_persist=1 x

and " x" is checked for, but ignored.

Confused. What's this all for? Can't we just us plain old strtoul(), etc?

Right - the sscanf would make sense if kernel parameters could contain spaces (in that case it catches <number><blanks><garbage> input) which it can't so strtoul() would indeed make more sense. I'll prepare an updated patch and send it out later today.

Quoted 12 lines
Quoted 12 lines
kernel/gcov/fs.c new file +12 -0
+/*
+ * seq_file.start() implementation for gcov data files. Note that the
+ * gcov_iterator interface is designed to be more restrictive than seq_file
+ * (no start from arbitrary position, etc.), to simplify the iterator
+ * implementation.
+ */
+static void *gcov_seq_start(struct seq_file *seq, loff_t *pos)
+{
+	loff_t i;
+
+	gcov_iter_start(seq->private);
+	for (i = 0; i < *pos; i++) {
Quoted 8 lines

kernel/gcov/fs.c line 103

Quoted 5 lines
kernel/gcov/fs.c new file +5 -0
+		if (gcov_iter_next(seq->private))
+			return NULL;
+	}
+	return seq->private;
+}

This looks like it could be very expensive if used inappropriately. I guess the answer to that is "don't use it inapprpriately", yes?

Yes :) The expensiveness is more than made up for by the reduced code complexity of the iterator interface. Also the extra effort is only spent when actually reading coverage files, i.e. not while performing a test.

Quoted 14 lines

kernel/gcov/gcc_3_4.c line 132

Quoted 12 lines

...

kernel/gcov/gcc_3_4.c new file +10 -0
+struct gcov_info *gcov_info_dup(struct gcov_info *info)
+{
+	struct gcov_info *dup;
+	unsigned int i;
+	unsigned int active;
+
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

Quoted 27 lines
Quoted 27 lines
kernel/gcov/gcc_3_4.c new file +27 -0
+	if (!dup)
+		return NULL;
+	dup->version		= info->version;
+	dup->stamp		= info->stamp;
+	dup->n_functions	= info->n_functions;
+	dup->ctr_mask		= info->ctr_mask;
+	/* Duplicate filename. */
+	dup->filename		= kstrdup(info->filename, GFP_KERNEL);
+	if (!dup->filename)
+		goto err_free;
+	/* Duplicate table of functions. */
+	dup->functions = kmemdup(info->functions, info->n_functions *
+				 get_fn_size(info), GFP_KERNEL);
+	if (!dup->functions)
+		goto err_free;
+	/* Duplicate counter arrays. */
+	for (i = 0; i < active ; i++) {
+		struct gcov_ctr_info *ctr = &info->counts[i];
+		size_t size = ctr->num * sizeof(gcov_type);
+
+		dup->counts[i].num = ctr->num;
+		dup->counts[i].merge = ctr->merge;
+		dup->counts[i].values = kmemdup(ctr->values, size, GFP_KERNEL);
+		if (!dup->counts[i].values)
+			goto err_free;
+	}
+	return dup;
Quoted 8 lines

kernel/gcov/gcc_3_4.c line 165

Quoted 6 lines
kernel/gcov/gcc_3_4.c new file +6 -0
+
+err_free:
+	gcov_info_free(dup);
+	return NULL;
+}
+

It all looks very nice to me.

Thanks :)

#9 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 10 lines · Peter Oberparleiter
Quoted 8 lines · Andrew Morton

Review

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
(quoted diff) +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Quoted 4 lines

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

Quoted 20 lines
Quoted 20 lines
Quoted 5 lines
(quoted diff) +5 -0
+static int __init gcov_persist_setup(char *str)
+{
+	int val;
+	char delim;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+	if (sscanf(str, "%d %c", &val, &delim) != 1) {
+		pr_warning("invalid gcov_persist parameter '%s'\n", str);
+		return 0;
+	}
+	pr_info("setting gcov_persist to %d\n", val);
+	gcov_persist = val;
+
+	return 1;
+}
+__setup("gcov_persist=", gcov_persist_setup);

hm, what's the input format here? It looks like

gcov_persist=1 x

Quoted 8 lines
Quoted 3 lines

and " x" is checked for, but ignored.

Confused. What's this all for? Can't we just us plain old strtoul(), etc?

Right - the sscanf would make sense if kernel parameters could contain spaces (in that case it catches <number><blanks><garbage> input) which it can't so strtoul() would indeed make more sense. I'll prepare an updated patch and send it out later today.

See below for the updated patch that uses strtoul instead of sscanf. This patch replaces kernel-constructor-support.patch in the -mm tree:

================
From: Peter Oberparleiter <[email protected]>

Enable the use of GCC's coverage testing tool gcov [1] with the Linux kernel. gcov may be useful for:

* debugging (has this code been reached at all?)
* test improvement (how do I change my test to cover these lines?)
* minimizing kernel configurations (do I need this option if the associated code is never run?)

The profiling patch incorporates the following changes:

* change kbuild to include profiling flags
* provide functions needed by profiling code
* present profiling data as files in debugfs

Note that on some architectures, enabling gcc's profiling option "-fprofile-arcs" for the entire kernel may trigger compile/link/ run-time problems, some of which are caused by toolchain bugs and others which require adjustment of architecture code.

For this reason profiling the entire kernel is initially restricted to those architectures for which it is known to work without changes. This restriction can be lifted once an architecture has been tested and found compatible with gcc's profiling. Profiling of single files or directories is still available on all platforms (see config help text).

[1] http://gcc.gnu.org/onlinedocs/gcc/Gcov.html

Cc Andi Kleen <[email protected]>
Cc Huang Ying <[email protected]>
Cc Li Wei <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Rusty Russell <[email protected]>
Cc WANG Cong <[email protected]>
Cc Sam Ravnborg <[email protected]>
Cc Jeff Dike <[email protected]>
Cc Al Viro <[email protected]>

---

 Documentation/gcov.txt              |  246 +++++++++
 Documentation/kernel-parameters.txt |    7 
 Makefile                            |   11 
 arch/Kconfig                        |    2 
 include/linux/compiler-gcc3.h       |    6 
 kernel/Makefile                     |    1 
 kernel/gcov/Kconfig                 |   48 +
 kernel/gcov/Makefile                |    3 
 kernel/gcov/base.c                  |  148 +++++
 kernel/gcov/fs.c                    |  674 ++++++++++++++++++++++++++
 kernel/gcov/gcc_3_4.c               |  445 +++++++++++++++++
 kernel/gcov/gcov.h                  |  128 ++++
 scripts/Makefile.lib                |   11 
 13 files changed, 1725 insertions(+), 5 deletions(-)
Documentation/gcov.txt new file +246 -0
Index: linux-2.6.30-rc7-mm/Documentation/gcov.txt
===================================================================
@@ -0,0 +1,246 @@
+Using gcov with the Linux kernel
+================================
+
+1. Introduction
+2. Preparation
+3. Customization
+4. Files
+5. Modules
+6. Separated build and test machines
+7. Troubleshooting
+Appendix A: sample script: gather_on_build.sh
+Appendix B: sample script: gather_on_test.sh
+
+
+1. Introduction
+===============
+
+gcov profiling kernel support enables the use of GCC's coverage testing
+tool gcov [1] with the Linux kernel. Coverage data of a running kernel
+is exported in gcov-compatible format via the "gcov" debugfs directory.
+To get coverage data for a specific file, change to the kernel build
+directory and use gcov with the -o option as follows (requires root):
+
+# cd /tmp/linux-out
+# gcov -o /sys/kernel/debug/gcov/tmp/linux-out/kernel spinlock.c
+
+This will create source code files annotated with execution counts
+in the current directory. In addition, graphical gcov front-ends such
+as lcov [2] can be used to automate the process of collecting data
+for the entire kernel and provide coverage overviews in HTML format.
+
+Possible uses:
+
+* debugging (has this line been reached at all?)
+* test improvement (how do I change my test to cover these lines?)
+* minimizing kernel configurations (do I need this option if the
+  associated code is never run?)
+
+--
+
+[1] http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
+[2] http://ltp.sourceforge.net/coverage/lcov.php
+
+
+2. Preparation
+==============
+
+Configure the kernel with:
+
+        CONFIG_DEBUGFS=y
+        CONFIG_GCOV_KERNEL=y
+
+and to get coverage data for the entire kernel:
+
+        CONFIG_GCOV_PROFILE_ALL=y
+
+Note that kernels compiled with profiling flags will be significantly
+larger and run slower. Also CONFIG_GCOV_PROFILE_ALL may not be supported
+on all architectures.
+
+Profiling data will only become accessible once debugfs has been
+mounted:
+
+        mount -t debugfs none /sys/kernel/debug
+
+
+3. Customization
+================
+
+To enable profiling for specific files or directories, add a line
+similar to the following to the respective kernel Makefile:
+
+        For a single file (e.g. main.o):
+                GCOV_PROFILE_main.o := y
+
+        For all files in one directory:
+                GCOV_PROFILE := y
+
+To exclude files from being profiled even when CONFIG_GCOV_PROFILE_ALL
+is specified, use:
+
+                GCOV_PROFILE_main.o := n
+        and:
+                GCOV_PROFILE := n
+
+Only files which are linked to the main kernel image or are compiled as
+kernel modules are supported by this mechanism.
+
+
+4. Files
+========
+
+The gcov kernel support creates the following files in debugfs:
+
+        /sys/kernel/debug/gcov
+                Parent directory for all gcov-related files.
+
+        /sys/kernel/debug/gcov/reset
+                Global reset file: resets all coverage data to zero when
+                written to.
+
+        /sys/kernel/debug/gcov/path/to/compile/dir/file.gcda
+                The actual gcov data file as understood by the gcov
+                tool. Resets file coverage data to zero when written to.
+
+        /sys/kernel/debug/gcov/path/to/compile/dir/file.gcno
+                Symbolic link to a static data file required by the gcov
+                tool. This file is generated by gcc when compiling with
+                option -ftest-coverage.
+
+
+5. Modules
+==========
+
+Kernel modules may contain cleanup code which is only run during
+module unload time. The gcov mechanism provides a means to collect
+coverage data for such code by keeping a copy of the data associated
+with the unloaded module. This data remains available through debugfs.
+Once the module is loaded again, the associated coverage counters are
+initialized with the data from its previous instantiation.
+
+This behavior can be deactivated by specifying the gcov_persist kernel
+parameter:
+
+        gcov_persist=0
+
+At run-time, a user can also choose to discard data for an unloaded
+module by writing to its data file or the global reset file.
+
+
+6. Separated build and test machines
+====================================
+
+The gcov kernel profiling infrastructure is designed to work out-of-the
+box for setups where kernels are built and run on the same machine. In
+cases where the kernel runs on a separate machine, special preparations
+must be made, depending on where the gcov tool is used:
+
+a) gcov is run on the TEST machine
+
+The gcov tool version on the test machine must be compatible with the
+gcc version used for kernel build. Also the following files need to be
+copied from build to test machine:
+
+from the source tree:
+  - all C source files + headers
+
+from the build tree:
+  - all C source files + headers
+  - all .gcda and .gcno files
+  - all links to directories
+
+It is important to note that these files need to be placed into the
+exact same file system location on the test machine as on the build
+machine. If any of the path components is symbolic link, the actual
+directory needs to be used instead (due to make's CURDIR handling).
+
+b) gcov is run on the BUILD machine
+
+The following files need to be copied after each test case from test
+to build machine:
+
+from the gcov directory in sysfs:
+  - all .gcda files
+  - all links to .gcno files
+
+These files can be copied to any location on the build machine. gcov
+must then be called with the -o option pointing to that directory.
+
+Example directory setup on the build machine:
+
+  /tmp/linux:    kernel source tree
+  /tmp/out:      kernel build directory as specified by make O=
+  /tmp/coverage: location of the files copied from the test machine
+
+  [user@build] cd /tmp/out
+  [user@build] gcov -o /tmp/coverage/tmp/out/init main.c
+
+
+7. Troubleshooting
+==================
+
+Problem:  Compilation aborts during linker step.
+Cause:    Profiling flags are specified for source files which are not
+          linked to the main kernel or which are linked by a custom
+          linker procedure.
+Solution: Exclude affected source files from profiling by specifying
+          GCOV_PROFILE := n or GCOV_PROFILE_basename.o := n in the
+          corresponding Makefile.
+
+
+Appendix A: gather_on_build.sh
+==============================
+
+Sample script to gather coverage meta files on the build machine
+(see 6a):
+
+#!/bin/bash
+
+KSRC=$1
+KOBJ=$2
+DEST=$3
+
+if [ -z "$KSRC" ] || [ -z "$KOBJ" ] || [ -z "$DEST" ]; then
+  echo "Usage: $0 <ksrc directory> <kobj directory> <output.tar.gz>" >&2
+  exit 1
+fi
+
+KSRC=$(cd $KSRC; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
+KOBJ=$(cd $KOBJ; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
+
+find $KSRC $KOBJ \( -name '*.gcno' -o -name '*.[ch]' -o -type l \) -a \
+                 -perm /u+r,g+r | tar cfz $DEST -P -T -
+
+if [ $? -eq 0 ] ; then
+  echo "$DEST successfully created, copy to test system and unpack with:"
+  echo "  tar xfz $DEST -P"
+else
+  echo "Could not create file $DEST"
+fi
+
+
+Appendix B: gather_on_test.sh
+=============================
+
+Sample script to gather coverage data files on the test machine
+(see 6b):
+
+#!/bin/bash
+
+DEST=$1
+GCDA=/sys/kernel/debug/gcov
+
+if [ -z "$DEST" ] ; then
+  echo "Usage: $0 <output.tar.gz>" >&2
+  exit 1
+fi
+
+find $GCDA -name '*.gcno' -o -name '*.gcda' | tar cfz $DEST -T -
+
+if [ $? -eq 0 ] ; then
+  echo "$DEST successfully created, copy to build system and unpack with:"
+  echo "  tar xfz $DEST"
+else
+  echo "Could not create file $DEST"
+fi
Documentation/kernel-parameters.txt +7 -0
Index: linux-2.6.30-rc7-mm/Documentation/kernel-parameters.txt
===================================================================
@@ -48,6 +48,7 @@ parameter is applicable:
 	EFI	EFI Partitioning (GPT) is enabled
 	EIDE	EIDE/ATAPI support is enabled.
 	FB	The frame buffer device is enabled.
+	GCOV	GCOV profiling is enabled.
 	HW	Appropriate hardware is enabled.
 	IA-64	IA-64 architecture is enabled.
 	IMA     Integrity measurement architecture is enabled.
@@ -770,6 +771,12 @@ and is between 256 and 4096 characters. 
 			Format: off | on
 			default: on
 
+	gcov_persist=	[GCOV] When non-zero (default), profiling data for
+			kernel modules is saved and remains accessible via
+			debugfs, even when the module is unloaded/reloaded.
+			When zero, profiling data is discarded and associated
+			debugfs files are removed at module unload time.
+
 	gdth=		[HW,SCSI]
 			See header of drivers/scsi/gdth.c.
 
Makefile +6 -5
Index: linux-2.6.30-rc7-mm/Makefile
===================================================================
@@ -338,6 +338,7 @@ AFLAGS_MODULE   = $(MODFLAGS)
 LDFLAGS_MODULE  =
 CFLAGS_KERNEL	=
 AFLAGS_KERNEL	=
+CFLAGS_GCOV	= -fprofile-arcs -ftest-coverage
 
 
 # Use LINUXINCLUDE when you must reference the include/ directory.
@@ -364,7 +365,7 @@ export CPP AR NM STRIP OBJCOPY OBJDUMP M
 export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
 
 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
-export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
+export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE CFLAGS_GCOV
 export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
 
 # When compiling out-of-tree modules, put MODVERDIR in the module
@@ -1224,8 +1225,8 @@ clean: archclean $(clean-dirs)
 		\( -name '*.[oas]' -o -name '*.ko' -o -name '.*.cmd' \
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
 		-o -name '*.symtypes' -o -name 'modules.order' \
-		-o -name 'Module.markers' -o -name '.tmp_*.o.*' \) \
-		-type f -print | xargs rm -f
+		-o -name 'Module.markers' -o -name '.tmp_*.o.*' \
+		-o -name '*.gcno' \) -type f -print | xargs rm -f
 
 # mrproper - Delete all generated files, including .config
 #
@@ -1429,8 +1430,8 @@ clean: $(clean-dirs)
 	$(call cmd,rmfiles)
 	@find $(KBUILD_EXTMOD) $(RCS_FIND_IGNORE) \
 		\( -name '*.[oas]' -o -name '*.ko' -o -name '.*.cmd' \
-		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \) \
-		-type f -print | xargs rm -f
+		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
+		-o -name '*.gcno' \) -type f -print | xargs rm -f
 
 help:
 	@echo  '  Building external modules.'
arch/Kconfig +2 -0
Index: linux-2.6.30-rc7-mm/arch/Kconfig
===================================================================
@@ -112,3 +112,5 @@ config HAVE_DMA_API_DEBUG
 
 config HAVE_DEFAULT_NO_SPIN_MUTEXES
 	bool
+
+source "kernel/gcov/Kconfig"
include/linux/compiler-gcc3.h +6 -0
Index: linux-2.6.30-rc7-mm/include/linux/compiler-gcc3.h
===================================================================
@@ -16,6 +16,12 @@
 #define __must_check		__attribute__((warn_unused_result))
 #endif
 
+#ifdef CONFIG_GCOV_KERNEL
+# if __GNUC_MINOR__ < 4
+#   error "GCOV profiling support for gcc versions below 3.4 not included"
+# endif /* __GNUC_MINOR__ */
+#endif /* CONFIG_GCOV_KERNEL */
+
 /*
  * A trick to suppress uninitialized variable warning without generating any
  * code
kernel/Makefile +1 -0
Index: linux-2.6.30-rc7-mm/kernel/Makefile
===================================================================
@@ -72,6 +72,7 @@ obj-$(CONFIG_KPROBES_SANITY_TEST) += tes
 obj-$(CONFIG_UTRACE) += utrace.o
 obj-$(CONFIG_AUDIT) += audit.o auditfilter.o
 obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
+obj-$(CONFIG_GCOV_KERNEL) += gcov/
 obj-$(CONFIG_AUDIT_TREE) += audit_tree.o
 obj-$(CONFIG_KPROBES) += kprobes.o
 obj-$(CONFIG_KGDB) += kgdb.o
kernel/gcov/Kconfig new file +48 -0

48 lines not on this page · raw message

kernel/gcov/Makefile new file +3 -0
Index: linux-2.6.30-rc7-mm/kernel/gcov/Makefile
===================================================================
@@ -0,0 +1,3 @@
+EXTRA_CFLAGS := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
+
+obj-$(CONFIG_GCOV_KERNEL) := base.o fs.o gcc_3_4.o
kernel/gcov/base.c new file +148 -0

148 lines not on this page · raw message

kernel/gcov/fs.c new file +667 -0

667 lines not on this page · raw message

kernel/gcov/gcc_3_4.c new file +445 -0

445 lines not on this page · raw message

kernel/gcov/gcov.h new file +128 -0

128 lines not on this page · raw message

scripts/Makefile.lib +11 -0
Index: linux-2.6.30-rc7-mm/scripts/Makefile.lib
===================================================================
@@ -116,6 +116,17 @@ _a_flags       = $(KBUILD_CPPFLAGS) $(KB
                  $(asflags-y) $(AFLAGS_$(basetarget).o)
 _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
 
+#
+# Enable gcov profiling flags for a file, directory or for all files depending
+# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
+# (in this order)
+#
+ifeq ($(CONFIG_GCOV_KERNEL),y)
+_c_flags += $(if $(patsubst n%,, \
+		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
+		$(CFLAGS_GCOV))
+endif
+
 # If building the kernel in a separate objtree expand all occurrences
 # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
 
#10 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 11 lines · Peter Oberparleiter
Quoted 10 lines · Peter Oberparleiter
Quoted 8 lines · Andrew Morton

kernel/gcov/gcc_3_4.c line 132

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
kernel/gcov/gcc_3_4.c new file +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Quoted 8 lines
Quoted 4 lines

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

The real question is how big can active be, and it looks like <= 5 is the answer:

+/* Determine number of active counters. Based on gcc magic. */ +static unsigned int num_counter_active(struct gcov_info *info) +{
+ unsigned int i;
+ unsigned int result = 0;
+
+ for (i = 0; i < GCOV_COUNTERS; i++) {
+ if (counter_active(info, i))
+ result++;
+ }
+ return result;
+}

+#define GCOV_COUNTERS 5

So that seems reasonable.

I assumed these were the per BB counters, but it seems they're counter _types_. Where are the BB counters, in the values array? If so perhaps that is the candidate for vmalloc.

cheers

#11 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

In reply to Peter Oberparleiter
Quoted 7 lines · On Wed, 03 Jun 2009 17:26:22 +0200 Peter Oberparleiter…
Quoted 4 lines

Right - the sscanf would make sense if kernel parameters could contain spaces (in that case it catches <number><blanks><garbage> input) which it can't so strtoul() would indeed make more sense. I'll prepare an updated patch and send it out later today.

See below for the updated patch that uses strtoul instead of sscanf. This patch replaces kernel-constructor-support.patch in the -mm tree:

umm, no it doesn't. I get the below incremental patch, against gcov-add-gcov-profiling-infrastructure.patch:

kernel/gcov/fs.c renamed from kernel/gcov/fs.c~gcov-add-gcov-profiling-infrastructure-update +2 -9
@@ -70,15 +70,8 @@ static int gcov_persist = 1;
 
 static int __init gcov_persist_setup(char *str)
 {
-	int val;
-	char delim;
-
-	if (sscanf(str, "%d %c", &val, &delim) != 1) {
-		pr_warning("invalid gcov_persist parameter '%s'\n", str);
-		return 0;
-	}
-	pr_info("setting gcov_persist to %d\n", val);
-	gcov_persist = val;
+	gcov_persist = simple_strtoul(str, NULL, 0);
+	pr_info("setting gcov_persist to %d\n", gcov_persist);
 
 	return 1;
 }

_

arguably we should use strict_strtoul(), but the kernel is a lot less fussy about boot parameters than it is with sysfs writes, etc. If you fat-finger your grub.conf, you lose and we don't tell you.

#12 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 9 lines · Andrew Morton
Quoted 3 lines · On Wed, 03 Jun 2009 17:26:22 +0200 Peter Oberparleiter…
Quoted 1 line

Right - the sscanf would make sense if kernel parameters could contain spaces (in that case it catches <number><blanks><garbage> input) which it can't so strtoul() would indeed make more sense. I'll prepare an updated patch and send it out later today.

See below for the updated patch that uses strtoul instead of sscanf. This patch replaces kernel-constructor-support.patch in the -mm tree:

umm, no it doesn't. I get the below incremental patch, against gcov-add-gcov-profiling-infrastructure.patch:

Ah, right. That answers my question whether further changes should be posted as complete or incremental patches.

Quoted 22 lines
kernel/gcov/fs.c renamed from kernel/gcov/fs.c~gcov-add-gcov-profiling-infrastructure-update +2 -9
@@ -70,15 +70,8 @@ static int gcov_persist = 1;
 
 static int __init gcov_persist_setup(char *str)
 {
-	int val;
-	char delim;
-
-	if (sscanf(str, "%d %c", &val, &delim) != 1) {
-		pr_warning("invalid gcov_persist parameter '%s'\n", str);
-		return 0;
-	}
-	pr_info("setting gcov_persist to %d\n", val);
-	gcov_persist = val;
+	gcov_persist = simple_strtoul(str, NULL, 0);
+	pr_info("setting gcov_persist to %d\n", gcov_persist);
 
 	return 1;
 }

_

Quoted 3 lines

arguably we should use strict_strtoul(), but the kernel is a lot less fussy about boot parameters than it is with sysfs writes, etc. If you fat-finger your grub.conf, you lose and we don't tell you.

Agreed. Which leads me to another question: is it ok to post many small patches for -mm for minor changes like this one (simple_ -> strict_) or do you rather prefer these to be collected into somewhat larger patches that fix multiple minor issues?

#13 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 2 lines · On Thu, 04 Jun 2009 10:26:39 +0200 Peter Oberparleiter…

Ah, right. That answers my question whether further changes should be posted as complete or incremental patches.

yup. Incrementals are a bit more hassle, but they're hugely more friendly to people who have already reviewed the code.

And occasionally when I turn a replacement patch into an incremental, it shows up things which the submitter hadn't intended be included.

Quoted 4 lines

is it ok to post many small
patches for -mm for minor changes like this one (simple_ -> strict_) or do you rather prefer these to be collected into somewhat larger patches that fix multiple minor issues?

I think one patch per "issue" would be best. Although combo patches at this stage would not be a huge problem.

#14 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

In reply to Andrew Morton
Quoted 24 lines · Andrew Morton

On Wed, 03 Jun 2009 17:26:22 +0200
Peter Oberparleiter <[email protected]> wrote:

kernel/gcov/fs.c renamed from kernel/gcov/fs.c~gcov-add-gcov-profiling-infrastructure-update +2 -9
@@ -70,15 +70,8 @@ static int gcov_persist = 1;
 
 static int __init gcov_persist_setup(char *str)
 {
-	int val;
-	char delim;
-
-	if (sscanf(str, "%d %c", &val, &delim) != 1) {
-		pr_warning("invalid gcov_persist parameter '%s'\n", str);
-		return 0;
-	}
-	pr_info("setting gcov_persist to %d\n", val);
-	gcov_persist = val;
+	gcov_persist = simple_strtoul(str, NULL, 0);
+	pr_info("setting gcov_persist to %d\n", gcov_persist);
 
 	return 1;
 }

_

Quoted 3 lines

arguably we should use strict_strtoul(), but the kernel is a lot less fussy about boot parameters than it is with sysfs writes, etc. If you fat-finger your grub.conf, you lose and we don't tell you.

To improve user-friendliness a bit:

Subject: gcov: use strict_strtoul

From: Peter Oberparleiter <[email protected]>

Use strict_strtoul to identify kernel parameter format errors.

---
 kernel/gcov/fs.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/gcov/fs.c +7 -1
Index: linux-2.6.30-rc8-mm1/kernel/gcov/fs.c
===================================================================
@@ -70,7 +70,13 @@ static int gcov_persist = 1;
 
 static int __init gcov_persist_setup(char *str)
 {
-	gcov_persist = simple_strtoul(str, NULL, 0);
+	unsigned long val;
+
+	if (strict_strtoul(str, 0, &val)) {
+		pr_warning("invalid gcov_persist parameter '%s'\n", str);
+		return 0;
+	}
+	gcov_persist = val;
 	pr_info("setting gcov_persist to %d\n", gcov_persist);
 
 	return 1;
#15 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

In reply to Peter Oberparleiter
Quoted 11 lines · Peter Oberparleiter
Quoted 10 lines · Peter Oberparleiter
Quoted 8 lines · Andrew Morton

kernel/gcov/gcc_3_4.c line 132

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
kernel/gcov/gcc_3_4.c new file +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Quoted 8 lines
Quoted 4 lines

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

Unless you want virtually continious memory, you don't need to bother vmalloc().

kmalloc() and get_free_pages() are all fine for this.

#16 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 11 lines · Amerigo Wang
Quoted 10 lines · Peter Oberparleiter
Quoted 9 lines · Peter Oberparleiter
Quoted 7 lines · Andrew Morton

kernel/gcov/gcc_3_4.c line 132

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
kernel/gcov/gcc_3_4.c new file +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

Quoted 8 lines
Quoted 3 lines

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

Unless you want virtually continious memory, you don't need to bother vmalloc().

kmalloc() and get_free_pages() are all fine for this.

kmalloc() requires contiguous pages to serve an allocation request larger than a single page. The longer a kernel runs, the more fragmented the pool of free pages gets and the probability to find enough contiguous free pages is significantly reduced.

In this case (having had a 3rd look), I found allocations of up to ~50kb, so to be sure, I'll switch that particular allocation to vmalloc().

#17 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 24 lines · On Fri, 05 Jun 2009 11:23:04 +0200 Peter Oberparleiter…
Quoted 14 lines · Amerigo Wang
Quoted 13 lines · Peter Oberparleiter
Quoted 12 lines · Peter Oberparleiter
Quoted 7 lines · Andrew Morton

kernel/gcov/gcc_3_4.c line 132

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
kernel/gcov/gcc_3_4.c new file +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

Quoted 8 lines
Quoted 3 lines

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

Unless you want virtually continious memory, you don't need to bother vmalloc().

kmalloc() and get_free_pages() are all fine for this.

Quoted 7 lines

kmalloc() requires contiguous pages to serve an allocation request larger than a single page. The longer a kernel runs, the more fragmented the pool of free pages gets and the probability to find enough contiguous free pages is significantly reduced.

In this case (having had a 3rd look), I found allocations of up to ~50kb, so to be sure, I'll switch that particular allocation to vmalloc().

Well, vmalloc() isn't magic. It can suffer internal fragmentation of the fixed-sized virtual address arena.

Is it possible to redo the data structures so that the large array isn't needed? Use a list, or move the data elsewhere or such?

#18 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 23 lines · Andrew Morton
Quoted 21 lines · On Fri, 05 Jun 2009 11:23:04 +0200 Peter Oberparleiter…
Quoted 10 lines · Amerigo Wang
Quoted 9 lines · Peter Oberparleiter
Quoted 8 lines · Peter Oberparleiter
Quoted 7 lines · Andrew Morton

kernel/gcov/gcc_3_4.c line 132

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
kernel/gcov/gcc_3_4.c new file +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

Unless you want virtually continious memory, you don't need to bother vmalloc().

kmalloc() and get_free_pages() are all fine for this.

kmalloc() requires contiguous pages to serve an allocation request larger than a single page. The longer a kernel runs, the more fragmented the pool of free pages gets and the probability to find enough contiguous free pages is significantly reduced.

Quoted 7 lines
Quoted 1 line

In this case (having had a 3rd look), I found allocations of up to ~50kb, so to be sure, I'll switch that particular allocation to vmalloc().

Well, vmalloc() isn't magic. It can suffer internal fragmentation of the fixed-sized virtual address arena.

Is it possible to redo the data structures so that the large array isn't needed? Use a list, or move the data elsewhere or such?

Unfortunately not - the format of the data is dictated by gcc. Any attempt to break it down into page-sized chunks would only imitate what vmalloc() already does.

Note though that this function is not called very often - it's only used to preserve coverage data for modules which are unloaded. And I only saw the 50kb counter data size for one file: kernel/sched.c (using a debugging patch).

So hm, I'm not sure about this anymore. I can also leave it at kmalloc() - chances are slim that anyone will actually experience a problem and if they do, they get an "order-n allocation failed" message so theres a hint at the cause for the problem.

#19 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 39 lines · Peter Oberparleiter
Quoted 29 lines · Andrew Morton
Quoted 27 lines · On Fri, 05 Jun 2009 11:23:04 +0200 Peter Oberparleiter…
Quoted 14 lines · Amerigo Wang
Quoted 13 lines · Peter Oberparleiter
Quoted 12 lines · Peter Oberparleiter
Quoted 7 lines · Andrew Morton

kernel/gcov/gcc_3_4.c line 132

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
kernel/gcov/gcc_3_4.c new file +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

Quoted 7 lines
Quoted 3 lines

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

Unless you want virtually continious memory, you don't need to bother vmalloc().

kmalloc() and get_free_pages() are all fine for this.

kmalloc() requires contiguous pages to serve an allocation request larger than a single page. The longer a kernel runs, the more fragmented the pool of free pages gets and the probability to find enough contiguous free pages is significantly reduced.

Quoted 8 lines
Quoted 2 lines

In this case (having had a 3rd look), I found allocations of up to ~50kb, so to be sure, I'll switch that particular allocation to vmalloc().

Well, vmalloc() isn't magic. It can suffer internal fragmentation of the fixed-sized virtual address arena.

Is it possible to redo the data structures so that the large array isn't needed? Use a list, or move the data elsewhere or such?

Quoted 8 lines

Unfortunately not - the format of the data is dictated by gcc. Any attempt to break it down into page-sized chunks would only imitate what vmalloc() already does.

Note though that this function is not called very often - it's only used to preserve coverage data for modules which are unloaded. And I only saw the 50kb counter data size for one file: kernel/sched.c (using a debugging patch).

Isn't it also called from gcov_seq_open() ?

Quoted 4 lines

So hm, I'm not sure about this anymore. I can also leave it at kmalloc()
- chances are slim that anyone will actually experience a problem and if they do, they get an "order-n allocation failed" message so theres a hint at the cause for the problem.

Why are we duping it anyway? Rather than allocating it in the beginning, is it because gcc-generated code is writing directly to the original copy?

If there's any chance of memory allocation failure it'd be preferable for it to happen before the test run that generates the coverage data, that way you know before hand that you are out of memory. Rather than running some (possibly long & involved) test case, and then losing all your data.

cheers

Signature · 6 lines
Michael Ellerman
OzLabs, LTC Prism Kernel Team

email: [email protected]
stime: [email protected]
notes: Michael Ellerman/Australia/IBM
#20 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

Quoted 42 lines · Michael Ellerman
Quoted 41 lines · Peter Oberparleiter
Quoted 29 lines · Andrew Morton
Quoted 27 lines · On Fri, 05 Jun 2009 11:23:04 +0200 Peter Oberparleiter…
Quoted 14 lines · Amerigo Wang
Quoted 13 lines · Peter Oberparleiter
Quoted 12 lines · Peter Oberparleiter
Quoted 7 lines · Andrew Morton

Review

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
(quoted diff) +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

Quoted 7 lines
Quoted 3 lines

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

Unless you want virtually continious memory, you don't need to bother vmalloc().

kmalloc() and get_free_pages() are all fine for this.

kmalloc() requires contiguous pages to serve an allocation request larger than a single page. The longer a kernel runs, the more fragmented the pool of free pages gets and the probability to find enough contiguous free pages is significantly reduced.

Quoted 7 lines
Quoted 2 lines

In this case (having had a 3rd look), I found allocations of up to ~50kb, so to be sure, I'll switch that particular allocation to vmalloc().

Well, vmalloc() isn't magic. It can suffer internal fragmentation of the fixed-sized virtual address arena.

Is it possible to redo the data structures so that the large array isn't needed? Use a list, or move the data elsewhere or such?

Unfortunately not - the format of the data is dictated by gcc. Any attempt to break it down into page-sized chunks would only imitate what vmalloc() already does.

Quoted 6 lines
Quoted 4 lines

Note though that this function is not called very often - it's only used to preserve coverage data for modules which are unloaded. And I only saw the 50kb counter data size for one file: kernel/sched.c (using a debugging patch).

Isn't it also called from gcov_seq_open() ?

Duh, of course - I totally forgot about that one. It used to be on module unload only but is, as you rightly pointed out, now basically called every time a coverage file is opened.

Quoted 9 lines
Quoted 5 lines

So hm, I'm not sure about this anymore. I can also leave it at kmalloc()
- chances are slim that anyone will actually experience a problem and if they do, they get an "order-n allocation failed" message so theres a hint at the cause for the problem.

Why are we duping it anyway? Rather than allocating it in the beginning, is it because gcc-generated code is writing directly to the original copy?

Yes - gcc's profiling code is writing to the original data structure. We're duping it on open() so that
a) data is consistent across reads() from the same file description
b) a non-vetoable module unload does not leave us with a defunct file descriptor

Quoted 7 lines

If there's any chance of memory allocation failure it'd be preferable for it to happen before the test run that generates the coverage data, that way you know before hand that you are out of memory. Rather than running some (possibly long & involved) test case, and then losing all your data.

I agree - though I would not want to artificially limit the number of times a coverage file can be opened concurrently (which would need to be done in case of pre-allocation). If, after a long and difficult test you're running out of memory while collecting coverage data, you could try to free up some memory manually. Alternatively, assuming that the gcov-kdump tool will be ported to the upstream version as well, you could then create a kernel dump and extract coverage information from that.

So even with vmalloc() not being a perfect solution, it should reduce the number of allocation failures that might be seen after long running test cases and is therefore the method of choice.

===================
Subject: gcov: use vmalloc for duplicating counter data

From: Peter Oberparleiter <[email protected]>

Coverage data is duplicated during each open() of a data file and when modules are unloaded. The contained data areas can get large (>50 kb) so that kmalloc()-based allocations may fail due to memory fragmentation, especially when a system has run for a long time. As we only need virtually contiguous memory for the duplicate, the use of vmalloc() can help prevent this problem.

---
 kernel/gcov/gcc_3_4.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/gcov/gcc_3_4.c +4 -2
Index: linux-2.6.30-rc8-mm1/kernel/gcov/gcc_3_4.c
===================================================================
@@ -18,6 +18,7 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/seq_file.h>
+#include <linux/vmalloc.h>
 #include "gcov.h"
 
 /* Symbolic links to be created for each profiling data file. */
@@ -152,9 +153,10 @@ struct gcov_info *gcov_info_dup(struct g
 
 		dup->counts[i].num = ctr->num;
 		dup->counts[i].merge = ctr->merge;
-		dup->counts[i].values = kmemdup(ctr->values, size, GFP_KERNEL);
+		dup->counts[i].values = vmalloc(size);
 		if (!dup->counts[i].values)
 			goto err_free;
+		memcpy(dup->counts[i].values, ctr->values, size);
 	}
 	return dup;
 
@@ -173,7 +175,7 @@ void gcov_info_free(struct gcov_info *in
 	unsigned int i;
 
 	for (i = 0; i < active ; i++)
-		kfree(info->counts[i].values);
+		vfree(info->counts[i].values);
 	kfree(info->functions);
 	kfree(info->filename);
 	kfree(info);
#21 lore raw

Re: [PATCH 3/4] gcov: add gcov profiling infrastructure

In reply to Peter Oberparleiter
Quoted 24 lines · Peter Oberparleiter
Quoted 14 lines · Amerigo Wang
Quoted 13 lines · Peter Oberparleiter
Quoted 12 lines · Peter Oberparleiter
Quoted 7 lines · Andrew Morton

kernel/gcov/gcc_3_4.c line 132

Quoted 4 lines · On Tue, 02 Jun 2009 13:44:02 +0200 Peter Oberparleiter…
kernel/gcov/gcc_3_4.c new file +4 -0
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);

How large can this allocation be?

Hm, good question. Having a look at my test system, I see coverage data files of up to 60kb size. With counters making up the largest part of those, I'd guess the allocation size can be around ~55kb. I assume that makes it a candidate for vmalloc?

Quoted 8 lines
Quoted 3 lines

A further run with debug output showed that the maximum size is actually around 4k, so in my opinion, there is no need to switch to vmalloc.

Unless you want virtually continious memory, you don't need to bother vmalloc().

kmalloc() and get_free_pages() are all fine for this.

Quoted 9 lines

kmalloc() requires contiguous pages to serve an allocation request larger than a single page. The longer a kernel runs, the more fragmented the pool of free pages gets and the probability to find enough contiguous free pages is significantly reduced.

In this case (having had a 3rd look), I found allocations of up to ~50kb, so to be sure, I'll switch that particular allocation to vmalloc().

Well, at least get_free_pages() is fine, 50Kb is about 16 pages on x86, so the order is 4, not big for get_free_pages()...

And note that vmalloc() needs extra efforts to do page mapping...

#22 lore raw

PATCH 4/4 gcov: enable GCOV_PROFILE_ALL for x86_64

In reply to Peter Oberparleiter

From: Peter Oberparleiter <[email protected]>

Enable gcov profiling of the entire kernel on x86_64. Required changes include disabling profiling for:

* arch/kernel/acpi/realmode and arch/kernel/boot/compressed:
not linked to main kernel
* arch/vdso, arch/kernel/vsyscall_64 and arch/kernel/hpet:
profiling causes segfaults during boot (incompatible context)

---
 arch/x86/boot/Makefile                 |    1 +
 arch/x86/boot/compressed/Makefile      |    1 +
 arch/x86/kernel/Makefile               |    2 ++
 arch/x86/kernel/acpi/realmode/Makefile |    1 +
 arch/x86/vdso/Makefile                 |    1 +
 kernel/gcov/Kconfig                    |    2 +-
 6 files changed, 7 insertions(+), 1 deletion(-)
kernel/gcov/Kconfig +1 -1
Index: linux-2.6.30-rc7/kernel/gcov/Kconfig
===================================================================
@@ -34,7 +34,7 @@
 config GCOV_PROFILE_ALL
 	bool "Profile entire Kernel"
 	depends on GCOV_KERNEL
-	depends on S390 || X86_32
+	depends on S390 || X86
 	default n
 	---help---
 	This options activates profiling for the entire kernel.
arch/x86/kernel/acpi/realmode/Makefile +1 -0
Index: linux-2.6.30-rc7/arch/x86/kernel/acpi/realmode/Makefile
===================================================================
@@ -42,6 +42,7 @@
 		   $(call cc-option, -mpreferred-stack-boundary=2)
 KBUILD_CFLAGS	+= $(call cc-option, -m32)
 KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
+GCOV_PROFILE := n
 
 WAKEUP_OBJS = $(addprefix $(obj)/,$(wakeup-y))
 
arch/x86/boot/Makefile +1 -0
Index: linux-2.6.30-rc7/arch/x86/boot/Makefile
===================================================================
@@ -69,6 +69,7 @@
 		   $(call cc-option, -mpreferred-stack-boundary=2)
 KBUILD_CFLAGS	+= $(call cc-option, -m32)
 KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
+GCOV_PROFILE := n
 
 $(obj)/bzImage: asflags-y  := $(SVGA_MODE)
 
arch/x86/boot/compressed/Makefile +1 -0
Index: linux-2.6.30-rc7/arch/x86/boot/compressed/Makefile
===================================================================
@@ -15,6 +15,7 @@
 KBUILD_CFLAGS += $(call cc-option,-fno-stack-protector)
 
 KBUILD_AFLAGS  := $(KBUILD_CFLAGS) -D__ASSEMBLY__
+GCOV_PROFILE := n
 
 LDFLAGS := -m elf_$(UTS_MACHINE)
 LDFLAGS_vmlinux := -T
arch/x86/vdso/Makefile +1 -0
Index: linux-2.6.30-rc7/arch/x86/vdso/Makefile
===================================================================
@@ -123,6 +123,7 @@
 		       -Wl,-T,$(filter %.lds,$^) $(filter %.o,$^)
 
 VDSO_LDFLAGS = -fPIC -shared $(call ld-option, -Wl$(comma)--hash-style=sysv)
+GCOV_PROFILE := n
 
 #
 # Install the unstripped copy of vdso*.so listed in $(vdso-install-y).
arch/x86/kernel/Makefile +2 -0
Index: linux-2.6.30-rc7/arch/x86/kernel/Makefile
===================================================================
@@ -24,6 +24,8 @@
 CFLAGS_hpet.o		:= $(nostackp)
 CFLAGS_tsc.o		:= $(nostackp)
 CFLAGS_paravirt.o	:= $(nostackp)
+GCOV_PROFILE_vsyscall_64.o	:= n
+GCOV_PROFILE_hpet.o		:= n
 
 obj-y			:= process_$(BITS).o signal.o entry_$(BITS).o
 obj-y			+= traps.o irq.o irq_$(BITS).o dumpstack_$(BITS).o