1 # Copyright (C) 2013-2022 Free Software Foundation, Inc.
2 # This file is part of the GNU C Library.
4 # The GNU C Library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # The GNU C Library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with the GNU C Library; if not, see
16 # <https://www.gnu.org/licenses/>.
18 # Makefile for benchmark tests. The only useful target here is `bench`.
19 # Add benchmark functions in alphabetical order.
88 ifneq (,$(filter yes,$(float96-fcts)))
94 ifneq (,$(filter yes,$(float128-fcts) $(float128-alias-fcts)))
105 pthread-mutex-locks \
116 bench := $(bench-math) $(bench-pthread) $(bench-string)
118 bench := $(foreach B,$(filter bench-%,${BENCHSET}), ${${B}})
121 # String function benchmarks.
174 # Build and run locale-dependent benchmarks only if we're building natively.
175 ifeq (no,$(cross-compiling))
201 string-benchset-all := $(string-benchset) ${wcsmbs-benchset}
203 ifeq (no,$(cross-compiling))
204 # We have to generate locales
231 include ../gen-locales.mk
234 stdlib-benchset := strtod
236 stdio-common-benchset := sprintf
238 math-benchset := math-inlines
241 benchset := $(string-benchset-all) $(stdlib-benchset) $(stdio-common-benchset) \
244 benchset := $(foreach B,$(filter %-benchset,${BENCHSET}), ${${B}})
247 CFLAGS-bench-ffs.c += -fno-builtin
248 CFLAGS-bench-ffsll.c += -fno-builtin
249 CFLAGS-bench-sqrt.c += -fno-builtin
250 CFLAGS-bench-fmin.c += -fno-builtin
251 CFLAGS-bench-fminf.c += -fno-builtin
252 CFLAGS-bench-fmax.c += -fno-builtin
253 CFLAGS-bench-fmaxf.c += -fno-builtin
254 CFLAGS-bench-trunc.c += -fno-builtin
255 CFLAGS-bench-truncf.c += -fno-builtin
256 CFLAGS-bench-roundeven.c += -fno-builtin
257 CFLAGS-bench-roundevenf.c += -fno-builtin
258 CFLAGS-bench-isnan.c += -fsignaling-nans
259 CFLAGS-bench-isinf.c += -fsignaling-nans
260 CFLAGS-bench-isfinite.c += -fsignaling-nans
263 bench-malloc := malloc-thread malloc-simple
265 bench-malloc := $(filter malloc-%,${BENCHSET})
268 ifeq (${STATIC-BENCHTESTS},yes)
269 +link-benchtests = $(+link-static-tests)
270 link-libc-benchtests = $(link-libc-static)
271 libm-benchtests = $(common-objpfx)math/libm.a
272 thread-library-benchtests = $(static-thread-library)
274 link-libc-benchtests = $(link-libc)
275 +link-benchtests = $(+link-tests)
276 thread-library-benchtests = $(shared-thread-library)
277 libm-benchtests = $(libm)
280 $(addprefix $(objpfx)bench-,$(bench-math)): $(libm-benchtests)
281 $(addprefix $(objpfx)bench-,$(math-benchset)): $(libm-benchtests)
282 $(addprefix $(objpfx)bench-,$(bench-pthread)): $(thread-library-benchtests)
283 $(addprefix $(objpfx)bench-,$(bench-malloc)): $(thread-library-benchtests)
284 $(addprefix $(objpfx)bench-,pthread-locks): $(libm-benchtests)
285 $(addprefix $(objpfx)bench-,pthread-mutex-locks): $(libm-benchtests)
289 # Rules to build and execute the benchmarks. Do not put any benchmark
290 # parameters beyond this point.
292 # We don't want the benchmark programs to run in parallel since that could
293 # affect their performance.
296 bench-extra-objs = json-lib.o
298 extra-objs += $(bench-extra-objs)
299 others-extras = $(bench-extra-objs)
301 # NB: Use "=" instead of ":=" since sysdeps Makefiles may add more
303 binaries-bench = $(addprefix $(objpfx)bench-,$(bench))
304 extra-objs += $(addsuffix .o,$(addprefix bench-,$(bench)))
305 binaries-benchset = $(addprefix $(objpfx)bench-,$(benchset))
306 extra-objs += $(addsuffix .o,$(addprefix bench-,$(benchset)))
307 binaries-bench-malloc := $(addprefix $(objpfx)bench-,$(bench-malloc))
308 extra-objs += $(addsuffix .o,$(addprefix bench-,$(bench-malloc)))
310 # The default duration: 1 seconds.
311 ifndef BENCH_DURATION
315 CPPFLAGS-nonlib += -DDURATION=$(BENCH_DURATION) -D_ISOMAC
317 # Use clock_gettime to measure performance of functions. The default is
318 # to use the architecture-specific high precision timing instructions.
319 ifdef USE_CLOCK_GETTIME
320 CPPFLAGS-nonlib += -DUSE_CLOCK_GETTIME
322 # On x86 processors, use RDTSCP, instead of RDTSC, to measure performance
323 # of functions. All x86 processors since 2010 support RDTSCP instruction.
325 CPPFLAGS-nonlib += -DUSE_RDTSCP
335 bench-deps := bench-skeleton.c bench-timing.h Makefile
337 run-bench = $(test-wrapper-env) \
339 $($*-ENV) $(test-via-rtld-prefix) $${run}
341 timing-type := $(objpfx)bench-timing-type
342 extra-objs += bench-timing-type.o
346 # This makes sure CPPFLAGS-nonlib and CFLAGS-nonlib are passed
347 # for all these modules.
348 cpp-srcs-left := $(binaries-benchset:=.c) $(binaries-bench:=.c) \
349 $(binaries-bench-malloc:=.c) $(timing-type:=.c)
351 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
354 rm -f $(binaries-bench) $(addsuffix .o,$(binaries-bench))
355 rm -f $(binaries-benchset) $(addsuffix .o,$(binaries-benchset))
356 rm -f $(binaries-bench-malloc) $(addsuffix .o,$(binaries-bench-malloc))
357 rm -f $(timing-type) $(addsuffix .o,$(timing-type))
358 rm -f $(addprefix $(objpfx),$(bench-extra-objs))
360 # Validate the passed in BENCHSET
361 ifneq ($(strip ${BENCHSET}),)
362 VALIDBENCHSETNAMES := bench-pthread bench-math bench-string string-benchset \
363 wcsmbs-benchset stdlib-benchset stdio-common-benchset math-benchset \
364 malloc-thread malloc-simple
365 INVALIDBENCHSETNAMES := $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
366 ifneq (${INVALIDBENCHSETNAMES},)
367 $(info The following values in BENCHSET are invalid: ${INVALIDBENCHSETNAMES})
368 $(info The valid ones are: ${VALIDBENCHSETNAMES})
369 $(error Invalid BENCHSET value)
373 bench: bench-build bench-set bench-func bench-malloc
375 # Target to only build the benchmark without running it. We generate locales
376 # only if we're building natively.
377 ifeq (no,$(cross-compiling))
378 bench-build: $(gen-locales) $(timing-type) $(binaries-bench) \
379 $(binaries-benchset) $(binaries-bench-malloc)
381 bench-build: $(timing-type) $(binaries-bench) $(binaries-benchset) \
382 $(binaries-bench-malloc)
385 bench-set: $(binaries-benchset)
387 echo "Running $${run}"; \
388 $(run-bench) > $${run}.out; \
391 bench-malloc: $(binaries-bench-malloc)
394 if [ `basename $${run}` = "bench-malloc-thread" ]; then \
395 for thr in 1 8 16 32; do \
396 echo "Running $${run} $${thr}"; \
397 $(run-bench) $${thr} > $${run}-$${thr}.out; \
400 for thr in 8 16 32 64 128 256 512 1024 2048 4096; do \
401 echo "Running $${run} $${thr}"; \
402 $(run-bench) $${thr} > $${run}-$${thr}.out; \
407 # Build and execute the benchmark functions. This target generates JSON
408 # formatted bench.out. Each of the programs produce independent JSON output,
409 # so one could even execute them individually and process it using any JSON
410 # capable language or tool.
411 bench-func: $(binaries-bench)
412 if [ -n '$^' ] ; then \
413 { timing_type=$$($(test-wrapper-env) \
415 $(test-via-rtld-prefix) \
417 echo "{\"timing_type\": \"$${timing_type}\","; \
418 echo " \"functions\": {"; \
420 if ! [ "x$${run}" = "x$<" ]; then \
423 echo "Running $${run}" >&2; \
424 $(run-bench) $(DETAILED_OPT); \
429 } > $(objpfx)bench.out-tmp; \
430 if [ -f $(objpfx)bench.out ]; then \
431 mv -f $(objpfx)bench.out $(objpfx)bench.out.old; \
433 mv -f $(objpfx)bench.out-tmp $(objpfx)bench.out; \
434 $(PYTHON) scripts/validate_benchout.py $(objpfx)bench.out \
435 scripts/benchout.schema.json; \
438 ifeq ($(bind-now),yes)
439 link-bench-bind-now = -Wl,-z,now
442 bench-link-targets = $(timing-type) $(binaries-bench) $(binaries-benchset) \
443 $(binaries-bench-malloc)
445 $(bench-link-targets): %: %.o $(objpfx)json-lib.o \
446 $(link-extra-libs-tests) \
447 $(sort $(filter $(common-objpfx)lib%,$(link-libc-benchtests))) \
448 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
451 $(bench-link-targets): LDFLAGS += $(link-bench-bind-now)
453 $(objpfx)bench-%.c: %-inputs $(bench-deps)
454 { if [ -n "$($*-INCLUDE)" ]; then \
457 $(PYTHON) scripts/bench.py $(patsubst %-inputs,%,$<); } > $@-tmp