1 # Copyright (C) 2013-2023 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.
91 ifneq (,$(filter yes,$(float96-fcts)))
97 ifneq (,$(filter yes,$(float128-fcts) $(float128-alias-fcts)))
109 pthread-mutex-trylock \
111 pthread-spin-trylock \
116 LDLIBS-bench-pthread-mutex-lock += -lm
117 LDLIBS-bench-pthread-mutex-trylock += -lm
118 LDLIBS-bench-pthread-spin-lock += -lm
119 LDLIBS-bench-pthread-spin-trylock += -lm
126 # String function benchmarks.
179 # Build and run locale-dependent benchmarks only if we're building natively.
180 ifeq (no,$(cross-compiling))
207 string-benchset-all := $(string-benchset) ${wcsmbs-benchset}
209 ifeq (no,$(cross-compiling))
210 # We have to generate locales
237 include ../gen-locales.mk
251 stdio-common-benchset := sprintf
253 math-benchset := math-inlines
256 benchset := $(string-benchset-all) $(stdlib-benchset) $(stdio-common-benchset) \
257 $(math-benchset) $(hash-benchset)
259 benchset := $(foreach B,$(filter %-benchset,${BENCHSET}), ${${B}})
262 CFLAGS-bench-ffs.c += -fno-builtin
263 CFLAGS-bench-ffsll.c += -fno-builtin
264 CFLAGS-bench-sqrt.c += -fno-builtin
265 CFLAGS-bench-fmin.c += -fno-builtin
266 CFLAGS-bench-fminf.c += -fno-builtin
267 CFLAGS-bench-fmax.c += -fno-builtin
268 CFLAGS-bench-fmaxf.c += -fno-builtin
269 CFLAGS-bench-trunc.c += -fno-builtin
270 CFLAGS-bench-truncf.c += -fno-builtin
271 CFLAGS-bench-roundeven.c += -fno-builtin
272 CFLAGS-bench-roundevenf.c += -fno-builtin
273 CFLAGS-bench-isnan.c += $(config-cflags-signaling-nans)
274 CFLAGS-bench-isinf.c += $(config-cflags-signaling-nans)
275 CFLAGS-bench-isfinite.c += $(config-cflags-signaling-nans)
278 bench-malloc := malloc-thread malloc-simple
280 bench-malloc := $(filter malloc-%,${BENCHSET})
283 ifeq (${STATIC-BENCHTESTS},yes)
284 +link-benchtests = $(+link-static-tests)
285 link-libc-benchtests = $(link-libc-static)
286 libm-benchtests = $(common-objpfx)math/libm.a
287 thread-library-benchtests = $(static-thread-library)
289 link-libc-benchtests = $(link-libc)
290 +link-benchtests = $(+link-tests)
291 thread-library-benchtests = $(shared-thread-library)
292 libm-benchtests = $(libm)
295 $(addprefix $(objpfx)bench-,$(bench-math)): $(libm-benchtests)
296 $(addprefix $(objpfx)bench-,$(math-benchset)): $(libm-benchtests)
297 $(addprefix $(objpfx)bench-,$(bench-pthread)): $(thread-library-benchtests)
298 $(addprefix $(objpfx)bench-,$(bench-malloc)): $(thread-library-benchtests)
299 $(addprefix $(objpfx)bench-,pthread-locks): $(libm-benchtests)
300 $(addprefix $(objpfx)bench-,pthread-mutex-locks): $(libm-benchtests)
304 # Rules to build and execute the benchmarks. Do not put any benchmark
305 # parameters beyond this point.
307 # We don't want the benchmark programs to run in parallel since that could
308 # affect their performance.
311 bench-extra-objs = json-lib.o
313 extra-objs += $(bench-extra-objs)
314 others-extras = $(bench-extra-objs)
316 # The default duration: 1 seconds.
317 ifndef BENCH_DURATION
321 CPPFLAGS-nonlib += -DDURATION=$(BENCH_DURATION) -D_ISOMAC
323 # Use clock_gettime to measure performance of functions. The default is
324 # to use the architecture-specific high precision timing instructions.
325 ifdef USE_CLOCK_GETTIME
326 CPPFLAGS-nonlib += -DUSE_CLOCK_GETTIME
328 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
329 # of functions. All x86 processors since 2010 support RDTSCP instruction.
331 CPPFLAGS-nonlib += -DUSE_RDTSCP
341 bench-deps := bench-skeleton.c bench-timing.h Makefile
343 run-bench = $(test-wrapper-env) \
345 $($*-ENV) $(test-via-rtld-prefix) $${run}
347 timing-type := $(objpfx)bench-timing-type
348 extra-objs += bench-timing-type.o
352 bench-math += $(bench-libmvec)
355 bench := $(bench-math) $(bench-pthread) $(bench-string)
357 bench := $(foreach B,$(filter bench-%,${BENCHSET}), ${${B}})
360 # NB: Use "=" instead of ":=" since sysdeps Makefiles may add more
362 binaries-bench = $(addprefix $(objpfx)bench-,$(bench))
363 extra-objs += $(addsuffix .o,$(addprefix bench-,$(bench)))
364 binaries-benchset = $(addprefix $(objpfx)bench-,$(benchset))
365 extra-objs += $(addsuffix .o,$(addprefix bench-,$(benchset)))
366 binaries-bench-malloc := $(addprefix $(objpfx)bench-,$(bench-malloc))
367 extra-objs += $(addsuffix .o,$(addprefix bench-,$(bench-malloc)))
369 # This makes sure CPPFLAGS-nonlib and CFLAGS-nonlib are passed
370 # for all these modules.
371 cpp-srcs-left := $(binaries-benchset:=.c) $(binaries-bench:=.c) \
372 $(binaries-bench-malloc:=.c) $(timing-type:=.c)
374 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
377 rm -f $(binaries-bench) $(addsuffix .o,$(binaries-bench))
378 rm -f $(binaries-benchset) $(addsuffix .o,$(binaries-benchset))
379 rm -f $(binaries-bench-malloc) $(addsuffix .o,$(binaries-bench-malloc))
380 rm -f $(timing-type) $(addsuffix .o,$(timing-type))
381 rm -f $(addprefix $(objpfx),$(bench-extra-objs))
383 # Validate the passed in BENCHSET
384 ifneq ($(strip ${BENCHSET}),)
385 VALIDBENCHSETNAMES := \
393 stdio-common-benchset \
399 INVALIDBENCHSETNAMES := $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
400 ifneq (${INVALIDBENCHSETNAMES},)
401 $(info The following values in BENCHSET are invalid: ${INVALIDBENCHSETNAMES})
402 $(info The valid ones are: ${VALIDBENCHSETNAMES})
403 $(error Invalid BENCHSET value)
407 bench: bench-build bench-set bench-func bench-malloc
409 # Target to only build the benchmark without running it. We generate locales
410 # only if we're building natively.
411 ifeq (no,$(cross-compiling))
412 bench-build: $(gen-locales) $(timing-type) $(binaries-bench) \
413 $(binaries-benchset) $(binaries-bench-malloc)
415 bench-build: $(timing-type) $(binaries-bench) $(binaries-benchset) \
416 $(binaries-bench-malloc)
419 bench-set: $(binaries-benchset)
421 echo "Running $${run}"; \
422 $(run-bench) > $${run}.out; \
425 bench-malloc: $(binaries-bench-malloc)
428 if [ `basename $${run}` = "bench-malloc-thread" ]; then \
429 for thr in 1 8 16 32; do \
430 echo "Running $${run} $${thr}"; \
431 $(run-bench) $${thr} > $${run}-$${thr}.out; \
434 for thr in 8 16 32 64 128 256 512 1024 2048 4096; do \
435 echo "Running $${run} $${thr}"; \
436 $(run-bench) $${thr} > $${run}-$${thr}.out; \
441 # Build and execute the benchmark functions. This target generates JSON
442 # formatted bench.out. Each of the programs produce independent JSON output,
443 # so one could even execute them individually and process it using any JSON
444 # capable language or tool.
445 bench-func: $(binaries-bench)
446 if [ -n '$^' ] ; then \
447 { timing_type=$$($(test-wrapper-env) \
449 $(test-via-rtld-prefix) \
451 echo "{\"timing_type\": \"$${timing_type}\","; \
452 echo " \"functions\": {"; \
454 op=$$($(run-bench) $(DETAILED_OPT)); \
458 echo "UNSUPPORTED $${run}: $${op}" >&2; \
461 echo "Running $${run}" >&2; \
462 if [ "$${run}" != "$<" ]; then \
468 echo "FAILED $${run}" >&2; \
475 } > $(objpfx)bench.out-tmp; \
476 if [ -f $(objpfx)bench.out ]; then \
477 mv -f $(objpfx)bench.out $(objpfx)bench.out.old; \
479 mv -f $(objpfx)bench.out-tmp $(objpfx)bench.out; \
480 $(PYTHON) scripts/validate_benchout.py $(objpfx)bench.out \
481 scripts/benchout.schema.json; \
484 ifeq ($(bind-now),yes)
485 link-bench-bind-now = -Wl,-z,now
488 bench-link-targets = $(timing-type) $(binaries-bench) $(binaries-benchset) \
489 $(binaries-bench-malloc)
491 $(bench-link-targets): %: %.o $(objpfx)json-lib.o \
492 $(link-extra-libs-tests) \
493 $(sort $(filter $(common-objpfx)lib%,$(link-libc-benchtests))) \
494 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
497 $(bench-link-targets): LDFLAGS += $(link-bench-bind-now)
499 $(objpfx)bench-%.c: %-inputs $(bench-deps)
500 { if [ -n "$($*-INCLUDE)" ]; then \
503 $(PYTHON) scripts/bench.py $(patsubst %-inputs,%,$<); } > $@-tmp