[glibc] tests: Allow tests to append tunables

Yury Khrustalev via Glibc-cvs <[email protected]>
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=5ea36b2cf4a0cbf8995e2ea82bf7485ce627e32a

commit 5ea36b2cf4a0cbf8995e2ea82bf7485ce627e32a
Author: Yury Khrustalev <[email protected]>
Date:   Thu Apr 16 14:38:02 2026 +0100

    tests: Allow tests to append tunables
    
    Many tests use Glibc tunables, and the values of the tunables are
    provided via the GLIBC_TUNABLES env variable. Tests set it in
    makefiles using
    
      tst-foo-ENV = GLIBC_TUNABLES=tunable=value
    
    This overwrites environment for this test, so if another env var is
    set elsewhere, one of these changes would be lost. The correct way
    should be to append to test's environment:
    
      tst-foo-ENV += GLIBC_TUNABLES=tunable=value
    
    However, if two or more tunables need to be set for the same test,
    the 'tunable=value' part should be appended to previously defined
    GLIBC_TUNABLES env variable, and it's not easy to achieve this via
    existing tools available for tests.
    
    Additionally, there are cases when it is useful to set ambient env
    var GLIBC_TUNABLES in order to apply it to most of the tests except
    those that require specific tunables. The existing mechanism that
    relies on tst-foo-ENV would always override the ambient env var
    even when it is not desirable.
    
    To address all of this, in this commit we add support for using
    constructs like
    
      tst-foo-TUNABLES += tunable=value
    
    Using this, the test will receive appropriate GLIBC_TUNABLES contents,
    and if there is an ambient env var GLIBC_TUNABLES, its value will be
    prepended to the env var used by the test. Even if the ambient env var
    contains the same tunable that is used by a test, the test's value will
    override the ambient value, and the test will be executed correctly.
    
    Additionally, we support cases when tests must have specific value
    of the GLIBC_TUNABLES env var (ignoring any ambient value):
    
      tst-foo-TUNABLES-only += tunable=value
    
    The existing mechanism that uses tst-foo-ENV will continue to work,
    however if the same test uses both, the new mechanism will override
    the old one.
    
    Additional benefit is that the code in makefiles becomes shorter.
    
    We also change tunable handling for malloc tests in this commit.
    
    Reviewed-by: Adhemerval Zanella  <[email protected]>

Diff:
---
 Rules               | 32 +++++++++++++++++++++++++-------
 benchtests/Makefile |  2 +-
 malloc/Makefile     | 10 ++++++----
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/Rules b/Rules
index 9d8587a148..64a337b265 100644
--- a/Rules
+++ b/Rules
@@ -348,21 +348,39 @@ $(1)-malloc-check-ENV = MALLOC_CHECK_=3 \
 endef
 $(foreach t,$(tests-malloc-check),$(eval $(call malloc-check-ENVS,$(t))))
 
+# Generate environment variable GLIBC_TUNABLES to be appended to
+# the test environment, appending tunables specifically added for
+# each test to the ambient GLIBC_TUNABLES environment variable.
+# The *-TUNABLES-only can be used by a test to override any ambient
+# tunables.
+define test-tun-joined
+$(subst $(empty) ,:,$(strip $($*-TUNABLES)))
+endef
+define test-tunables-all
+$(if $($*-TUNABLES),$(if $(GLIBC_TUNABLES),GLIBC_TUNABLES=$(GLIBC_TUNABLES):$(test-tun-joined),GLIBC_TUNABLES=$(test-tun-joined)),)
+endef
+define test-tunables-only
+GLIBC_TUNABLES=$(subst $(empty) ,:,$(strip $($*-TUNABLES-only)))
+endef
+define test-tunables
+$(if $($*-TUNABLES-only),$(test-tunables-only),$(test-tunables-all))
+endef
+
 # All malloc-hugetlb1 tests will be run with GLIBC_TUNABLES=glibc.malloc.hugetlb=1
 define malloc-hugetlb1-ENVS
-$(1)-malloc-hugetlb1-ENV += GLIBC_TUNABLES=glibc.malloc.hugetlb=1
+$(1)-malloc-hugetlb1-TUNABLES += glibc.malloc.hugetlb=1
 endef
 $(foreach t,$(tests-malloc-hugetlb1),$(eval $(call malloc-hugetlb1-ENVS,$(t))))
 
 # All malloc-hugetlb2 tests will be run with GLIBC_TUNABLE=glibc.malloc.hugetlb=2
 define malloc-hugetlb2-ENVS
-$(1)-malloc-hugetlb2-ENV += GLIBC_TUNABLES=glibc.malloc.hugetlb=2
+$(1)-malloc-hugetlb2-TUNABLES += glibc.malloc.hugetlb=2
 endef
 $(foreach t,$(tests-malloc-hugetlb2),$(eval $(call malloc-hugetlb2-ENVS,$(t))))
 
 # All malloc-largetcache tests will be run with GLIBC_TUNABLE=glibc.malloc.tcache_max=1048576
 define malloc-largetcache-ENVS
-$(1)-malloc-largetcache-ENV += GLIBC_TUNABLES=glibc.malloc.tcache_max=1048576
+$(1)-malloc-largetcache-TUNABLES += glibc.malloc.tcache_max=1048576
 endef
 $(foreach t,$(tests-malloc-largetcache),$(eval $(call malloc-largetcache-ENVS,$(t))))
 
@@ -373,12 +391,12 @@ endef
 $(foreach t,$(tests-mcheck),$(eval $(call mcheck-ENVS,$(t))))
 
 ifneq "$(strip $(tests) $(tests-container) $(tests-internal) $(xtests) $(test-srcs))" ""
+
 # These are the implicit rules for making test outputs
 # from the test programs and whatever input files are present.
-
 define make-test-out
-$(if $($*-ENV-only),$(test-wrapper-env-only) $($*-ENV-only),\
-     $(test-wrapper-env) $(run-program-env) $($*-ENV)) \
+$(if $($*-ENV-only),$(test-wrapper-env-only) $($*-ENV-only) $(test-tunables),\
+     $(test-wrapper-env) $(run-program-env) $($*-ENV) $(test-tunables)) \
 $(host-test-program-cmd) $($*-ARGS)
 endef
 $(objpfx)%.out: %.input $(objpfx)%
@@ -394,7 +412,7 @@ $(objpfx)%.out: /dev/null $(objpfx)%	# Make it 2nd arg for canned sequence.
 # tests-container.
 $(tests-container:%=$(objpfx)%.out): $(objpfx)%.out : $(if $(wildcard $(objpfx)%.files),$(objpfx)%.files,/dev/null) $(objpfx)%
 	$(test-wrapper-env) $(run-program-env) $(test-via-rtld-prefix) \
-	  $(common-objpfx)support/test-container env $(run-program-env) $($*-ENV) \
+	  $(common-objpfx)support/test-container env $(run-program-env) $($*-ENV) $(test-tunables) \
 	  $(host-test-program-cmd) $($*-ARGS) > $@; \
 	$(evaluate-test)
 
diff --git a/benchtests/Makefile b/benchtests/Makefile
index 156e308451..53f78232ff 100644
--- a/benchtests/Makefile
+++ b/benchtests/Makefile
@@ -447,7 +447,7 @@ bench-deps := bench-skeleton.c bench-timing.h Makefile
 
 run-bench = $(test-wrapper-env) \
 	    $(run-program-env) \
-	    $($*-ENV) $(test-via-rtld-prefix) $${run}
+	    $($*-ENV) $(test-tunables) $(test-via-rtld-prefix) $${run}
 
 timing-type := $(objpfx)bench-timing-type
 extra-objs += bench-timing-type.o
diff --git a/malloc/Makefile b/malloc/Makefile
index fef5021298..f95bca7f8f 100644
--- a/malloc/Makefile
+++ b/malloc/Makefile
@@ -438,17 +438,19 @@ malloc-check-env = \
   MALLOC_CHECK_=3 \
   LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
 
-malloc-check-tunables-env = \
-  GLIBC_TUNABLES=glibc.malloc.check=3 \
-  LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
-
 tst-malloc-check-ENV = $(malloc-check-env)
 tst-malloc-usable-ENV = $(malloc-check-env)
 tst-malloc-usable-threaded-main-ENV = $(malloc-check-env)
 tst-malloc-usable-threaded-worker-ENV = $(malloc-check-env)
+
+malloc-check-tunables-env = LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
+malloc-check-tunables-tun = glibc.malloc.check=3
 tst-malloc-usable-tunables-ENV = $(malloc-check-tunables-env)
+tst-malloc-usable-tunables-TUNABLES += $(malloc-check-tunables-tun)
 tst-malloc-usable-tunables-threaded-main-ENV = $(malloc-check-tunables-env)
+tst-malloc-usable-tunables-threaded-main-TUNABLES += $(malloc-check-tunables-tun)
 tst-malloc-usable-tunables-threaded-worker-ENV = $(malloc-check-tunables-env)
+tst-malloc-usable-tunables-threaded-worker-TUNABLES += $(malloc-check-tunables-tun)
 
 CPPFLAGS-malloc-debug.c += -DUSE_TCACHE=0
 CPPFLAGS-malloc.c += -DUSE_TCACHE=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.