Skip to contentSkip to conversation

gcov: Clang support

PATCH In review Tri Vo 2 participants 3 messages Apr 17, 2019 Gcov based kernel profiling

lore
Share & tools
lore

Tri Vo

author
#0 lore raw

PATCH v5 1/1 gcov: Clang support

From: Greg Hackmann <[email protected]>

LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls llvm_gcov_init() once per module, and provides a couple of callbacks that we can use to ask for more data.

We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:

llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()

This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.

See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more details on how this works, particularly GCOVProfiler::emitProfileArcs(), GCOVProfiler::insertCounterWriteout(), and GCOVProfiler::insertFlush().

---
v5:
- Same as v4. Build error when !CONFIG_MODULES was fixed by another commit 41e72eeff32c ("module: add stubs for within_module functions")

 kernel/gcov/Kconfig   |   3 +-
 kernel/gcov/Makefile  |   1 +
 kernel/gcov/base.c    |   2 +-
 kernel/gcov/clang.c   | 586 ++++++++++++++++++++++++++++++++++++++++++
 kernel/gcov/gcc_3_4.c |  12 +
 kernel/gcov/gcc_4_7.c |  12 +
 kernel/gcov/gcov.h    |   2 +
 7 files changed, 616 insertions(+), 2 deletions(-)
 create mode 100644 kernel/gcov/clang.c
kernel/gcov/Kconfig +2 -1
index 1e3823fa799b..f71c1adcff31 100644
@@ -53,6 +53,7 @@ config GCOV_PROFILE_ALL
 choice
 	prompt "Specify GCOV format"
 	depends on GCOV_KERNEL
+	depends on CC_IS_GCC
 	---help---
 	The gcov format is usually determined by the GCC version, and the
 	default is chosen according to your GCC version. However, there are
@@ -62,7 +63,7 @@ choice
 
 config GCOV_FORMAT_3_4
 	bool "GCC 3.4 format"
-	depends on CC_IS_GCC && GCC_VERSION < 40700
+	depends on GCC_VERSION < 40700
 	---help---
 	Select this option to use the format defined by GCC 3.4.
 
kernel/gcov/Makefile +1 -0
index 45431ed679d1..d66a74b0f100 100644
@@ -4,3 +4,4 @@ ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
 obj-y := base.o fs.o
 obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o
 obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o
+obj-$(CONFIG_CC_IS_CLANG) += clang.o
kernel/gcov/base.c +1 -1
index 799d42072727..0ffe9f194080 100644
@@ -64,7 +64,7 @@ static int gcov_module_notifier(struct notifier_block *nb, unsigned long event,
 
 	/* Remove entries located in module from linked list. */
 	while ((info = gcov_info_next(info))) {
-		if (within_module((unsigned long)info, mod)) {
+		if (gcov_info_within_module(info, mod)) {
 			gcov_info_unlink(prev, info);
 			if (gcov_events_enabled)
 				gcov_event(GCOV_REMOVE, info);
kernel/gcov/clang.c new file +586 -0

586 lines not on this page · raw message

kernel/gcov/gcc_3_4.c +12 -0
index 2dddecbdbe6e..801ee4b0b969 100644
@@ -137,6 +137,18 @@ void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
 		gcov_info_head = info->next;
 }
 
+/**
+ * gcov_info_within_module - check if a profiling data set belongs to a module
+ * @info: profiling data set
+ * @mod: module
+ *
+ * Returns true if profiling data belongs module, false otherwise.
+ */
+bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
+{
+	return within_module((unsigned long)info, mod);
+}
+
 /* Symbolic links to be created for each profiling data file. */
 const struct gcov_link gcov_link[] = {
 	{ OBJ_TREE, "gcno" },	/* Link to .gcno file in $(objtree). */
kernel/gcov/gcc_4_7.c +12 -0
index ca5e5c0ef853..ec37563674d6 100644
@@ -150,6 +150,18 @@ void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
 		gcov_info_head = info->next;
 }
 
+/**
+ * gcov_info_within_module - check if a profiling data set belongs to a module
+ * @info: profiling data set
+ * @mod: module
+ *
+ * Returns true if profiling data belongs module, false otherwise.
+ */
+bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
+{
+	return within_module((unsigned long)info, mod);
+}
+
 /* Symbolic links to be created for each profiling data file. */
 const struct gcov_link gcov_link[] = {
 	{ OBJ_TREE, "gcno" },	/* Link to .gcno file in $(objtree). */
kernel/gcov/gcov.h +2 -0
index 0ecf1d664ec3..6ab2c1808c9d 100644
@@ -15,6 +15,7 @@
 #ifndef GCOV_H
 #define GCOV_H GCOV_H
 
+#include <linux/module.h>
 #include <linux/types.h>
 
 /*
@@ -46,6 +47,7 @@ unsigned int gcov_info_version(struct gcov_info *info);
 struct gcov_info *gcov_info_next(struct gcov_info *info);
 void gcov_info_link(struct gcov_info *info);
 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info);
+bool gcov_info_within_module(struct gcov_info *info, struct module *mod);
 
 /* Base interface. */
 enum gcov_action {

--
2.21.0.392.gf8f6787159e-goog

#1 lore raw
Quoted 23 lines · On Wed, 17 Apr 2019 15:53:28 -0700 Tri Vo <[email protected]> wrote:

LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls llvm_gcov_init() once per module, and provides a couple of callbacks that we can use to ask for more data.

We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:

llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()

This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.

Quoted 3 lines

See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more details on how this works, particularly GCOVProfiler::emitProfileArcs(), GCOVProfiler::insertCounterWriteout(), and GCOVProfiler::insertFlush().

checkpatch speaketh truth.

Also, I'll change those Co-authored-by's to the documented Co-developed-by.

From: Andrew Morton <[email protected]>
Subject: gcov-clang-support-checkpatch-fixes

WARNING: Non-standard signature: Co-authored-by:
#31:

WARNING: Non-standard signature: Co-authored-by:
#32:

WARNING: Possible unnecessary 'out of memory' message #158: FILE: kernel/gcov/clang.c:90:
+ if (!info) {
+ pr_warn_ratelimited("failed to allocate gcov info\n");

WARNING: Possible unnecessary 'out of memory' message #193: FILE: kernel/gcov/clang.c:125:
+ if (!info) {
+ pr_warn_ratelimited("failed to allocate gcov function info for %s\n",

WARNING: line over 80 characters
#546: FILE: kernel/gcov/clang.c:478:
+ pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);

total: 0 errors, 5 warnings, 663 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

./patches/gcov-clang-support.patch has style problems, please review.

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

Please run checkpatch prior to sending patches

Cc Daniel Mentz <[email protected]>
Cc Greg Hackmann <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Peter Oberparleiter <[email protected]>
Cc Petri Gynther <[email protected]>
Cc Prasad Sodagudi <[email protected]>
Cc Trilok Soni <[email protected]>
Cc Tri Vo <[email protected]>

---

 kernel/gcov/clang.c |    9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/gcov/clang.c renamed from kernel/gcov/clang.c~gcov-clang-support-checkpatch-fixes +2 -7
@@ -86,10 +86,8 @@ void llvm_gcov_init(llvm_gcov_callback w
 {
 	struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
 
-	if (!info) {
-		pr_warn_ratelimited("failed to allocate gcov info\n");
+	if (!info)
 		return;
-	}
 
 	INIT_LIST_HEAD(&info->head);
 	INIT_LIST_HEAD(&info->functions);
@@ -121,11 +119,8 @@ void llvm_gcda_emit_function(u32 ident,
 {
 	struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
 
-	if (!info) {
-		pr_warn_ratelimited("failed to allocate gcov function info for %s\n",
-				function_name ?: "UNKNOWN");
+	if (!info)
 		return;
-	}
 
 	INIT_LIST_HEAD(&info->head);
 	info->ident = ident;

_

author
#2 lore raw
Quoted 65 lines · Andrew Morton
Quoted 23 lines · On Wed, 17 Apr 2019 15:53:28 -0700 Tri Vo <[email protected]> wrote:

LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls llvm_gcov_init() once per module, and provides a couple of callbacks that we can use to ask for more data.

We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:

llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()

This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.

Quoted 3 lines

See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more details on how this works, particularly GCOVProfiler::emitProfileArcs(), GCOVProfiler::insertCounterWriteout(), and GCOVProfiler::insertFlush().

checkpatch speaketh truth.

Also, I'll change those Co-authored-by's to the documented Co-developed-by.

From: Andrew Morton <[email protected]>
Subject: gcov-clang-support-checkpatch-fixes

WARNING: Non-standard signature: Co-authored-by:
#31:

WARNING: Non-standard signature: Co-authored-by:
#32:

WARNING: Possible unnecessary 'out of memory' message #158: FILE: kernel/gcov/clang.c:90:

(quoted diff) +2 -0
+       if (!info) {
+               pr_warn_ratelimited("failed to allocate gcov info\n");

WARNING: Possible unnecessary 'out of memory' message #193: FILE: kernel/gcov/clang.c:125:

(quoted diff) +2 -0
+       if (!info) {
+               pr_warn_ratelimited("failed to allocate gcov function info for %s\n",

WARNING: line over 80 characters
#546: FILE: kernel/gcov/clang.c:478:
+ pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);

total: 0 errors, 5 warnings, 663 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

Quoted 6 lines

./patches/gcov-clang-support.patch has style problems, please review.

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

Please run checkpatch prior to sending patches

Thanks! I'll keep that in mind.