1 # Copyright (C) 2013-2024 Free Software Foundation, Inc.
2 # Copyright The GNU Toolchain Authors.
3 # This file is part of the GNU C Library.
5 # The GNU C Library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # The GNU C Library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with the GNU C Library; if not, see
17 # <https://www.gnu.org/licenses/>.
19 # Makefile for benchmark tests. The only useful target here is `bench`.
20 # Add benchmark functions in alphabetical order.
92 ifneq (,$(filter yes,$(float96-fcts)))
98 ifneq (,$(filter yes,$(float128-fcts) $(float128-alias-fcts)))
110 pthread-mutex-trylock \
112 pthread-spin-trylock \
117 LDLIBS-bench-pthread-mutex-lock += -lm
118 LDLIBS-bench-pthread-mutex-trylock += -lm
119 LDLIBS-bench-pthread-spin-lock += -lm
120 LDLIBS-bench-pthread-spin-trylock += -lm
127 # String function benchmarks.
180 # Build and run locale-dependent benchmarks only if we're building natively.
181 ifeq (no,$(cross-compiling))
208 string-benchset-all := $(string-benchset) ${wcsmbs-benchset}
210 ifeq (no,$(cross-compiling))
211 # We have to generate locales
238 include ../gen-locales.mk
252 stdio-common-benchset := sprintf
254 math-benchset := math-inlines
260 $(stdio-common-benchset) \
262 $(string-benchset-all) \
265 benchset := $(foreach B,$(filter %-benchset,${BENCHSET}), ${${B}})
268 CFLAGS-bench-ffs.c += -fno-builtin
269 CFLAGS-bench-ffsll.c += -fno-builtin
270 CFLAGS-bench-sqrt.c += -fno-builtin
271 CFLAGS-bench-fmin.c += -fno-builtin
272 CFLAGS-bench-fminf.c += -fno-builtin
273 CFLAGS-bench-fmax.c += -fno-builtin
274 CFLAGS-bench-fmaxf.c += -fno-builtin
275 CFLAGS-bench-trunc.c += -fno-builtin
276 CFLAGS-bench-truncf.c += -fno-builtin
277 CFLAGS-bench-roundeven.c += -fno-builtin
278 CFLAGS-bench-roundevenf.c += -fno-builtin
279 CFLAGS-bench-isnan.c += $(config-cflags-signaling-nans)
280 CFLAGS-bench-isinf.c += $(config-cflags-signaling-nans)
281 CFLAGS-bench-isfinite.c += $(config-cflags-signaling-nans)
289 bench-malloc := $(filter malloc-%,${BENCHSET})
292 ifeq (${STATIC-BENCHTESTS},yes)
293 +link-benchtests = $(+link-static-tests)
294 link-libc-benchtests = $(link-libc-static)
295 libm-benchtests = $(common-objpfx)math/libm.a
296 thread-library-benchtests = $(static-thread-library)
298 link-libc-benchtests = $(link-libc)
299 +link-benchtests = $(+link-tests)
300 thread-library-benchtests = $(shared-thread-library)
301 libm-benchtests = $(libm)
304 $(addprefix $(objpfx)bench-,$(bench-math)): $(libm-benchtests)
305 $(addprefix $(objpfx)bench-,$(math-benchset)): $(libm-benchtests)
306 $(addprefix $(objpfx)bench-,$(bench-pthread)): $(thread-library-benchtests)
307 $(addprefix $(objpfx)bench-,$(bench-malloc)): $(thread-library-benchtests)
308 $(addprefix $(objpfx)bench-,pthread-locks): $(libm-benchtests)
309 $(addprefix $(objpfx)bench-,pthread-mutex-locks): $(libm-benchtests)
313 # Rules to build and execute the benchmarks. Do not put any benchmark
314 # parameters beyond this point.
316 # We don't want the benchmark programs to run in parallel since that could
317 # affect their performance.
320 bench-extra-objs = json-lib.o
322 extra-objs += $(bench-extra-objs)
323 others-extras = $(bench-extra-objs)
325 # The default duration: 1 seconds.
326 ifndef BENCH_DURATION
330 CPPFLAGS-nonlib += -DDURATION=$(BENCH_DURATION) -D_ISOMAC
332 # Use clock_gettime to measure performance of functions. The default is
333 # to use the architecture-specific high precision timing instructions.
334 ifdef USE_CLOCK_GETTIME
335 CPPFLAGS-nonlib += -DUSE_CLOCK_GETTIME
337 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
338 # of functions. All x86 processors since 2010 support RDTSCP instruction.
340 CPPFLAGS-nonlib += -DUSE_RDTSCP
350 bench-deps := bench-skeleton.c bench-timing.h Makefile
352 run-bench = $(test-wrapper-env) \
354 $($*-ENV) $(test-via-rtld-prefix) $${run}
356 timing-type := $(objpfx)bench-timing-type
357 extra-objs += bench-timing-type.o
361 bench-math += $(bench-libmvec)
370 bench := $(foreach B,$(filter bench-%,${BENCHSET}), ${${B}})
373 # NB: Use "=" instead of ":=" since sysdeps Makefiles may add more
375 binaries-bench = $(addprefix $(objpfx)bench-,$(bench))
376 extra-objs += $(addsuffix .o,$(addprefix bench-,$(bench)))
377 binaries-benchset = $(addprefix $(objpfx)bench-,$(benchset))
378 extra-objs += $(addsuffix .o,$(addprefix bench-,$(benchset)))
379 binaries-bench-malloc := $(addprefix $(objpfx)bench-,$(bench-malloc))
380 extra-objs += $(addsuffix .o,$(addprefix bench-,$(bench-malloc)))
382 # This makes sure CPPFLAGS-nonlib and CFLAGS-nonlib are passed
383 # for all these modules.
385 $(binaries-bench-malloc:=.c) \
386 $(binaries-bench:=.c) \
387 $(binaries-benchset:=.c) \
391 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
394 rm -f $(binaries-bench) $(addsuffix .o,$(binaries-bench))
395 rm -f $(binaries-benchset) $(addsuffix .o,$(binaries-benchset))
396 rm -f $(binaries-bench-malloc) $(addsuffix .o,$(binaries-bench-malloc))
397 rm -f $(timing-type) $(addsuffix .o,$(timing-type))
398 rm -f $(addprefix $(objpfx),$(bench-extra-objs))
400 # Validate the passed in BENCHSET
401 ifneq ($(strip ${BENCHSET}),)
402 VALIDBENCHSETNAMES := \
410 stdio-common-benchset \
416 INVALIDBENCHSETNAMES := $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
417 ifneq (${INVALIDBENCHSETNAMES},)
418 $(info The following values in BENCHSET are invalid: ${INVALIDBENCHSETNAMES})
419 $(info The valid ones are: ${VALIDBENCHSETNAMES})
420 $(error Invalid BENCHSET value)
424 bench: bench-build bench-set bench-func bench-malloc
426 # Target to only build the benchmark without running it. We generate locales
427 # only if we're building natively.
428 ifeq (no,$(cross-compiling))
429 bench-build: $(gen-locales) $(timing-type) $(binaries-bench) \
430 $(binaries-benchset) $(binaries-bench-malloc)
432 bench-build: $(timing-type) $(binaries-bench) $(binaries-benchset) \
433 $(binaries-bench-malloc)
436 bench-set: $(binaries-benchset)
438 echo "Running $${run}"; \
439 $(run-bench) > $${run}.out; \
442 bench-malloc: $(binaries-bench-malloc)
445 if [ `basename $${run}` = "bench-malloc-thread" ]; then \
446 for thr in 1 8 16 32; do \
447 echo "Running $${run} $${thr}"; \
448 $(run-bench) $${thr} > $${run}-$${thr}.out; \
451 for thr in 8 16 32 64 128 256 512 1024 2048 4096; do \
452 echo "Running $${run} $${thr}"; \
453 $(run-bench) $${thr} > $${run}-$${thr}.out; \
458 # Build and execute the benchmark functions. This target generates JSON
459 # formatted bench.out. Each of the programs produce independent JSON output,
460 # so one could even execute them individually and process it using any JSON
461 # capable language or tool.
462 bench-func: $(binaries-bench)
463 if [ -n '$^' ] ; then \
464 { timing_type=$$($(test-wrapper-env) \
466 $(test-via-rtld-prefix) \
468 echo "{\"timing_type\": \"$${timing_type}\","; \
469 echo " \"functions\": {"; \
471 op=$$($(run-bench) $(DETAILED_OPT)); \
475 echo "UNSUPPORTED $${run}: $${op}" >&2; \
478 echo "Running $${run}" >&2; \
479 if [ "$${run}" != "$<" ]; then \
485 echo "FAILED $${run}" >&2; \
492 } > $(objpfx)bench.out-tmp; \
493 if [ -f $(objpfx)bench.out ]; then \
494 mv -f $(objpfx)bench.out $(objpfx)bench.out.old; \
496 mv -f $(objpfx)bench.out-tmp $(objpfx)bench.out; \
497 $(PYTHON) scripts/validate_benchout.py $(objpfx)bench.out \
498 scripts/benchout.schema.json; \
501 ifeq ($(bind-now),yes)
502 link-bench-bind-now = -Wl,-z,now
505 bench-link-targets = $(timing-type) $(binaries-bench) $(binaries-benchset) \
506 $(binaries-bench-malloc)
508 $(bench-link-targets): %: %.o $(objpfx)json-lib.o \
509 $(link-extra-libs-tests) \
510 $(sort $(filter $(common-objpfx)lib%,$(link-libc-benchtests))) \
511 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
514 $(bench-link-targets): LDFLAGS += $(link-bench-bind-now)
516 $(objpfx)bench-%.c: %-inputs $(bench-deps)
517 { if [ -n "$($*-INCLUDE)" ]; then \
520 $(PYTHON) scripts/bench.py $(patsubst %-inputs,%,$<); } > $@-tmp