Skip to contentSkip to conversation

kbuild: Allow gcov to be enabled on the command line

PATCH In review Kent Overstreet 2 participants 5 messages Nov 22, 2023 – Nov 28, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: Allow gcov to be enabled on the command line

This allows gcov to be enabled for a particular kernel source subdirectory on the command line, without editing makefiles, like so:

make GCOV_PROFILE_fs_bcachefs=y

Cc Masahiro Yamada <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Nicolas Schier <[email protected]>
---
 scripts/Kbuild.include | 10 ++++++++++
 scripts/Makefile.lib   |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)
scripts/Kbuild.include +10 -0
index 7778cc97a4e0..5341736f2e30 100644
@@ -277,3 +277,13 @@ ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VER
 else
 .SECONDARY:
 endif
+
+ # expand_parents(a/b/c) = a/b/c a/b a
+expand_parents2 = $(if $(subst .,,$(1)),$(call expand_parents,$(1)),)
+expand_parents  = $(1) $(call expand_parents2,$(patsubst %/,%,$(dir $(1))))
+
+# flatten_dirs(a/b/c) = a_b_c a_b a
+flatten_dirs = $(subst /,_,$(call expand_parents,$(1)))
+
+# eval_vars(X_,a/b/c) = $(X_a_b_c) $(X_a_b) $(X_a)
+eval_vars = $(foreach var,$(call flatten_dirs,$(2)),$($(1)$(var)))
scripts/Makefile.lib +1 -1
index 1a965fe68e01..0b4581a8bc33 100644
@@ -148,7 +148,7 @@ _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
 #
 ifeq ($(CONFIG_GCOV_KERNEL),y)
 _c_flags += $(if $(patsubst n%,, \
-		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
+		$(GCOV_PROFILE_$(basetarget).o)$(call eval_vars,GCOV_PROFILE_,$(src))$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
 		$(CFLAGS_GCOV))
 endif
 
Signature · 1 line
2.42.0
#1 lore raw
Quoted 25 lines · Kent Overstreet

This allows gcov to be enabled for a particular kernel source subdirectory on the command line, without editing makefiles, like so:

make GCOV_PROFILE_fs_bcachefs=y

Cc Masahiro Yamada <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Nicolas Schier <[email protected]>
---
 scripts/Kbuild.include | 10 ++++++++++
 scripts/Makefile.lib   |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)
scripts/Kbuild.include +0 -0
index 7778cc97a4e0..5341736f2e30 100644
@@ -277,3 +277,13 @@ ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VER
 else
 .SECONDARY:
 endif

scripts/Kbuild.include ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VER · line 289

Quoted 10 lines
scripts/Kbuild.include +10 -0
+
+ # expand_parents(a/b/c) = a/b/c a/b a
+expand_parents2 = $(if $(subst .,,$(1)),$(call expand_parents,$(1)),)
+expand_parents  = $(1) $(call expand_parents2,$(patsubst %/,%,$(dir $(1))))
+
+# flatten_dirs(a/b/c) = a_b_c a_b a
+flatten_dirs = $(subst /,_,$(call expand_parents,$(1)))
+
+# eval_vars(X_,a/b/c) = $(X_a_b_c) $(X_a_b) $(X_a)
+eval_vars = $(foreach var,$(call flatten_dirs,$(2)),$($(1)$(var)))

I do not like tricky code like this.

Also, with "fs_bcachefs", it is unclear which directory is enabled.

How about this?

[1] Specify the list of directories by GCOV_PROFILE_DIRS

scripts/Makefile.lib +0 -0
index 1a965fe68e01..286a569556b3 100644
@@ -147,8 +147,12 @@ _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y)

$(CPPFLAGS_$(target-stem).lds)
# (in this order)
#
ifeq ($(CONFIG_GCOV_KERNEL),y)
+ifneq ($(filter $(obj),$(GCOV_PROFILE_DIRS)),)
+export GCOV_PROFILE_SUBDIR := y
+endif
+
_c_flags += $(if $(patsubst n%,, \
-
$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)),
\
+ $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(GCOV_PROFILE_SUBDIR)$(CONFIG_GCOV_PROFILE_ALL)),

\
                $(CFLAGS_GCOV))

endif

Usage:

  $ make GCOV_PROFILE_DIRS=fs/bcachefs

-> enable GCOV in fs/bachefs and its subdirectories.

or

  $ make GCOV_PROFILE_DIRS="drivers/gpio drivers/pinctrl"

-> enable GCOV in drivers/gpio, drivers/pinctrl, and their subdirectories.

[2] Do equivalent, but from a CONFIG option

config GCOV_PROFILE_DIRS
string "Directories to enable GCOV"

Then, you can set CONFIG_GCOV_PROFILE_DIRS="fs/bcachefs"

This might be a more natural approach because we already have CONFIG_GCOV_PROFILE_ALL, although it might eventually go away because CONFIG_GCOV_PROFILE_ALL=y is almost equivalent to CONFIG_GCOV_PROFILE_DIRS="."

scripts/Makefile.lib +0 -0
index 1a965fe68e01..286a569556b3 100644
@@ -147,8 +147,12 @@ _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y)

$(CPPFLAGS_$(target-stem).lds)
# (in this order)
#
ifeq ($(CONFIG_GCOV_KERNEL),y)
+ifneq ($(filter $(obj),$(CONFIG_GCOV_PROFILE_DIRS)),)
+export GCOV_PROFILE_SUBDIR := y
+endif
+
_c_flags += $(if $(patsubst n%,, \
-
$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)),
\
+ $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(GCOV_PROFILE_SUBDIR)$(CONFIG_GCOV_PROFILE_ALL)),

\
                $(CFLAGS_GCOV))

endif

Quoted 14 lines
scripts/Makefile.lib +1 -2
index 1a965fe68e01..0b4581a8bc33 100644
@@ -148,7 +148,7 @@ _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
 #
 ifeq ($(CONFIG_GCOV_KERNEL),y)
 _c_flags += $(if $(patsubst n%,, \
-               $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
+               $(GCOV_PROFILE_$(basetarget).o)$(call eval_vars,GCOV_PROFILE_,$(src))$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
                $(CFLAGS_GCOV))
 endif
 
--
Quoted 2 lines

2.42.0

--
Best Regards
Masahiro Yamada

#2 lore raw
Quoted 42 lines · Masahiro Yamada
Quoted 25 lines · Kent Overstreet

This allows gcov to be enabled for a particular kernel source subdirectory on the command line, without editing makefiles, like so:

make GCOV_PROFILE_fs_bcachefs=y

Cc Masahiro Yamada <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Nicolas Schier <[email protected]>
---
 scripts/Kbuild.include | 10 ++++++++++
 scripts/Makefile.lib   |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)
scripts/Kbuild.include +0 -0
index 7778cc97a4e0..5341736f2e30 100644
@@ -277,3 +277,13 @@ ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VER
 else
 .SECONDARY:
 endif

scripts/Kbuild.include ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VER · line 289

Quoted 10 lines
scripts/Kbuild.include +10 -0
+
+ # expand_parents(a/b/c) = a/b/c a/b a
+expand_parents2 = $(if $(subst .,,$(1)),$(call expand_parents,$(1)),)
+expand_parents  = $(1) $(call expand_parents2,$(patsubst %/,%,$(dir $(1))))
+
+# flatten_dirs(a/b/c) = a_b_c a_b a
+flatten_dirs = $(subst /,_,$(call expand_parents,$(1)))
+
+# eval_vars(X_,a/b/c) = $(X_a_b_c) $(X_a_b) $(X_a)
+eval_vars = $(foreach var,$(call flatten_dirs,$(2)),$($(1)$(var)))

I do not like tricky code like this.

Quoted 2 lines

Also, with "fs_bcachefs", it is unclear which directory is enabled.

It's consistent with how we can specify options in makefiles for a particular file.

I suppose CONFIG_GCOV_PROFILE_DIRS would be fine, but your patch isn't complete so I can't test it.

Quoted 119 lines

How about this?

[1] Specify the list of directories by GCOV_PROFILE_DIRS

scripts/Makefile.lib +5 -1
index 1a965fe68e01..286a569556b3 100644
@@ -147,8 +147,12 @@ _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y)
 $(CPPFLAGS_$(target-stem).lds)
 # (in this order)
 #
 ifeq ($(CONFIG_GCOV_KERNEL),y)
+ifneq ($(filter $(obj),$(GCOV_PROFILE_DIRS)),)
+export GCOV_PROFILE_SUBDIR := y
+endif
+
 _c_flags += $(if $(patsubst n%,, \
-
 $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)),
 \
+

$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(GCOV_PROFILE_SUBDIR)$(CONFIG_GCOV_PROFILE_ALL)),

\
                $(CFLAGS_GCOV))

endif

Usage:

  $ make GCOV_PROFILE_DIRS=fs/bcachefs

-> enable GCOV in fs/bachefs and its subdirectories.

or

  $ make GCOV_PROFILE_DIRS="drivers/gpio drivers/pinctrl"

-> enable GCOV in drivers/gpio, drivers/pinctrl, and their subdirectories.

[2] Do equivalent, but from a CONFIG option

config GCOV_PROFILE_DIRS
string "Directories to enable GCOV"

Then, you can set CONFIG_GCOV_PROFILE_DIRS="fs/bcachefs"

This might be a more natural approach because we already have CONFIG_GCOV_PROFILE_ALL, although it might eventually go away because CONFIG_GCOV_PROFILE_ALL=y is almost equivalent to CONFIG_GCOV_PROFILE_DIRS="."

scripts/Makefile.lib +5 -1
index 1a965fe68e01..286a569556b3 100644
@@ -147,8 +147,12 @@ _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y)
 $(CPPFLAGS_$(target-stem).lds)
 # (in this order)
 #
 ifeq ($(CONFIG_GCOV_KERNEL),y)
+ifneq ($(filter $(obj),$(CONFIG_GCOV_PROFILE_DIRS)),)
+export GCOV_PROFILE_SUBDIR := y
+endif
+
 _c_flags += $(if $(patsubst n%,, \
-
 $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)),
 \
+

$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(GCOV_PROFILE_SUBDIR)$(CONFIG_GCOV_PROFILE_ALL)),

\
                $(CFLAGS_GCOV))

endif

Quoted 14 lines
scripts/Makefile.lib +1 -2
index 1a965fe68e01..0b4581a8bc33 100644
@@ -148,7 +148,7 @@ _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
 #
 ifeq ($(CONFIG_GCOV_KERNEL),y)
 _c_flags += $(if $(patsubst n%,, \
-               $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
+               $(GCOV_PROFILE_$(basetarget).o)$(call eval_vars,GCOV_PROFILE_,$(src))$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
                $(CFLAGS_GCOV))
 endif
 
--
Quoted 2 lines

2.42.0

--
Best Regards
Masahiro Yamada

#3 lore raw
Quoted 44 lines · Kent Overstreet
Quoted 42 lines · Masahiro Yamada
Quoted 25 lines · Kent Overstreet

This allows gcov to be enabled for a particular kernel source subdirectory on the command line, without editing makefiles, like so:

make GCOV_PROFILE_fs_bcachefs=y

Cc Masahiro Yamada <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Nicolas Schier <[email protected]>
---
 scripts/Kbuild.include | 10 ++++++++++
 scripts/Makefile.lib   |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)
scripts/Kbuild.include +0 -0
index 7778cc97a4e0..5341736f2e30 100644
@@ -277,3 +277,13 @@ ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VER
 else
 .SECONDARY:
 endif

scripts/Kbuild.include ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VER · line 289

Quoted 10 lines
scripts/Kbuild.include +10 -0
+
+ # expand_parents(a/b/c) = a/b/c a/b a
+expand_parents2 = $(if $(subst .,,$(1)),$(call expand_parents,$(1)),)
+expand_parents  = $(1) $(call expand_parents2,$(patsubst %/,%,$(dir $(1))))
+
+# flatten_dirs(a/b/c) = a_b_c a_b a
+flatten_dirs = $(subst /,_,$(call expand_parents,$(1)))
+
+# eval_vars(X_,a/b/c) = $(X_a_b_c) $(X_a_b) $(X_a)
+eval_vars = $(foreach var,$(call flatten_dirs,$(2)),$($(1)$(var)))

I do not like tricky code like this.

Quoted 5 lines
Quoted 2 lines

Also, with "fs_bcachefs", it is unclear which directory is enabled.

It's consistent with how we can specify options in makefiles for a particular file.

It is consistent in a bad way.

You used "GCOV_PROFILE_" prefix
for the full directory path, while it is already used as a file name which is relative to the current directory.

Quoted 3 lines

I suppose CONFIG_GCOV_PROFILE_DIRS would be fine, but your patch isn't complete so I can't test it.

I do not understand what you mean by "isn't complete".

It is just a matter of adding the config entry somewhere.

I added a patch just in case, though.

Note, both approach pros and cons.

The command line approach works for external modules.

With the CONFIG option approach, you can easily see which directories are profiled by checking the .config, but it is not easy to enable gcov for external modules.

Signature · 2 lines
Best Regards
Masahiro Yamada
kernel/gcov/Kconfig +4 -0
index 04f4ebdc3cf5..c95d93189ecc 100644
@@ -37,6 +37,10 @@ config GCOV_KERNEL
 config ARCH_HAS_GCOV_PROFILE_ALL
 	def_bool n
 
+config GCOV_PROFILE_DIRS
+	string "Directories to enable gcov profiling"
+	depends on GCOV_KERNEL
+
 config GCOV_PROFILE_ALL
 	bool "Profile entire Kernel"
 	depends on !COMPILE_TEST
scripts/Makefile.lib +5 -1
index 1a965fe68e01..5d80f784b922 100644
@@ -147,8 +147,12 @@ _cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
 # (in this order)
 #
 ifeq ($(CONFIG_GCOV_KERNEL),y)
+ifneq ($(filter $(obj),$(CONFIG_GCOV_PROFILE_DIRS)),)
+export GCOV_PROFILE_SUBDIR := y
+endif
+
 _c_flags += $(if $(patsubst n%,, \
-		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
+		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(GCOV_PROFILE_SUBDIR)$(CONFIG_GCOV_PROFILE_ALL)), \
 		$(CFLAGS_GCOV))
 endif
 
#4 lore raw
Quoted 6 lines · Masahiro Yamada

On Sun, Nov 26, 2023 at 4:56 AM Kent Overstreet

Quoted 2 lines

It's consistent with how we can specify options in makefiles for a particular file.

It is consistent in a bad way.

That's a new meaning for consistent that I'm unfamiliar with.

Quoted 4 lines

You used "GCOV_PROFILE_" prefix
for the full directory path, while it is already
used as a file name which is relative to the
current directory.

And the current directory when you're building the entire kernel is the top level directory.

Quoted 7 lines
Quoted 2 lines

I suppose CONFIG_GCOV_PROFILE_DIRS would be fine, but your patch isn't complete so I can't test it.

I do not understand what you mean by "isn't complete".

It is just a matter of adding the config entry somewhere.

Yes, not complete, meaning you haven't even tested it.