]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
tests: Allow tests to append tunables
authorYury Khrustalev <yury.khrustalev@arm.com>
Thu, 16 Apr 2026 13:38:02 +0000 (14:38 +0100)
committerYury Khrustalev <yury.khrustalev@arm.com>
Mon, 27 Apr 2026 09:45:20 +0000 (10:45 +0100)
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 <adhemerval.zanella@linaro.org>
Rules
benchtests/Makefile
malloc/Makefile

diff --git a/Rules b/Rules
index 9d8587a148fc4d925b94d55791d38d2a0203e948..64a337b265694179641ffa6dc97d291ac35aa68d 100644 (file)
--- 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)
 
index 156e308451a8c51fd26614ab80fc7eccb3b2e169..53f78232ffe3cc08423909edf85a295f060ed0bc 100644 (file)
@@ -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
index fef50212982e68e15bf2e3f0652eb6535f69dc74..f95bca7f8fa15bd7f3809023c34c06cde823d4bd 100644 (file)
@@ -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